From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88932C433DB for ; Mon, 21 Dec 2020 19:20:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 12D0522B3F for ; Mon, 21 Dec 2020 19:20:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 12D0522B3F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7267E6B005C; Mon, 21 Dec 2020 14:20:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D61B6B005D; Mon, 21 Dec 2020 14:20:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C4E56B0068; Mon, 21 Dec 2020 14:20:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0207.hostedemail.com [216.40.44.207]) by kanga.kvack.org (Postfix) with ESMTP id 431376B005C for ; Mon, 21 Dec 2020 14:20:41 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0B653824999B for ; Mon, 21 Dec 2020 19:20:41 +0000 (UTC) X-FDA: 77618256282.22.books02_3e0176627459 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id CEFE518038E6C for ; Mon, 21 Dec 2020 19:20:40 +0000 (UTC) X-HE-Tag: books02_3e0176627459 X-Filterd-Recvd-Size: 6228 Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Dec 2020 19:20:40 +0000 (UTC) Received: by mail-lf1-f42.google.com with SMTP id o17so26360453lfg.4 for ; Mon, 21 Dec 2020 11:20:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vUnUklljz54AklJ/4EYyGGDSEKEpQNWIZ2ClZ96iVF4=; b=d1K2ld8SA6F/za4a6CZm0iTta7Dp706o9UHjEsvzJYHNeT9VXuoH+dWLNfm7MB8S2a Q/SIVL8bJtZTpimpbTyhsEQT11FnAqTZuAuNR0y6CCyP7Vg6esvDJMcvz+EZdwyk5NAB 4ttGSZuBDYPJmBUSv0vOh1uzULKDGV8/TT5LU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vUnUklljz54AklJ/4EYyGGDSEKEpQNWIZ2ClZ96iVF4=; b=qanBdsg3GBgs/9z26QzS79dkBO8OHyHjT500Ijo2tHdv5pqLJm+mMPL/s7e+vZhxdy MRS7sYQh7Qw+ZAyzdapnTVSsNQTNIWaI4DqU6p6W9hRrBqiU4sNofTOa8qWFTRT93/jT OztzBaBorBDuLCjxVib6fiDyZVdEOhThUnXZhwVSOiAJvTz5tF+R6G+L1CZhnNiFbi18 cwEpaASyKz+Y2CgArHJKg3IJvIncR4B1mMTN8VXgiDQYAmTj45nbCyjqTlitvwkN4HVe eUKbBD86G3OMUBaSIUoXBr6WRgW8AJyHHpS37hHl1usIGhM8AicMOEkIoiWaUsTuFqJN Ivyg== X-Gm-Message-State: AOAM532BmEjxfOJxG99c7ALHW9xm5a9qvFvHg/2R+5KPFAxFvYHCbHrT dc7iFhyH1KvmISg2VAaDA3f2vcCrP0yvRS2dmHu4mg== X-Google-Smtp-Source: ABdhPJzqa7NO56hNQ/VQuBNjQXKjNoRtYGbC1iIsUZiW++x/B/Fr0mahPbfbElKMX4ZIrropW1FBP2BGkUKY6suQAyA= X-Received: by 2002:a2e:b8c4:: with SMTP id s4mr5369682ljp.269.1608578437281; Mon, 21 Dec 2020 11:20:37 -0800 (PST) MIME-Version: 1.0 References: <18669bd607ae9efbf4e00e36532c7aa167d0fa12.camel@gmx.de> <20201220002228.38697-1-vitaly.wool@konsulko.com> In-Reply-To: From: Vitaly Wool Date: Mon, 21 Dec 2020 20:20:26 +0100 Message-ID: Subject: Re: [PATCH] zsmalloc: do not use bit_spin_lock To: Minchan Kim Cc: Mike Galbraith , LKML , linux-mm , Barry Song , Sebastian Andrzej Siewior , NitinGupta , sergey.senozhatsky.work@gmail.com, Andrew Morton , shakeelb@google.com Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Dec 21, 2020 at 6:24 PM Minchan Kim wrote: > > On Sun, Dec 20, 2020 at 02:22:28AM +0200, Vitaly Wool wrote: > > zsmalloc takes bit spinlock in its _map() callback and releases it > > only in unmap() which is unsafe and leads to zswap complaining > > about scheduling in atomic context. > > > > To fix that and to improve RT properties of zsmalloc, remove that > > bit spinlock completely and use a bit flag instead. > > I don't want to use such open code for the lock. > > I see from Mike's patch, recent zswap change introduced the lockdep > splat bug and you want to improve zsmalloc to fix the zswap bug and > introduce this patch with allowing preemption enabling. This understanding is upside down. The code in zswap you are referring to is not buggy. You may claim that it is suboptimal but there is nothing wrong in taking a mutex. > https://lore.kernel.org/linux-mm/fae85e4440a8ef6f13192476bd33a4826416fc58.camel@gmx.de/ > > zs_[un/map]_object is designed to be used in fast path(i.e., > zs_map_object/4K page copy/zs_unmap_object) so the spinlock is > perfectly fine for API point of view. However, zswap introduced > using the API with mutex_lock/crypto_wait_req where allowing > preemption, which was wrong. Taking a spinlock in one callback and releasing it in another is unsafe and error prone. What if unmap was called on completion of a DMA-like transfer from another context, like a threaded IRQ handler? In that case this spinlock might never be released. Anyway I can come up with a zswap patch explicitly stating that zsmalloc is not fully compliant with zswap / zpool API to avoid confusion for the time being. Would that be ok with you? Best regards, Vitaly > Furthermore, the zs_map_object already has a few more places where > disablepreemptions(migrate_read_lock, get_cpu_var and kmap_atomic). > > Without making those locks preemptible all at once, zswap will still > see the lockdep warning. > > > > > Signed-off-by: Vitaly Wool > > --- > > mm/zsmalloc.c | 13 ++++++++----- > > 1 file changed, 8 insertions(+), 5 deletions(-) > > > > diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c > > index 7289f502ffac..ff26546a7fed 100644 > > --- a/mm/zsmalloc.c > > +++ b/mm/zsmalloc.c > > @@ -876,22 +876,25 @@ static unsigned long obj_to_head(struct page *page, void *obj) > > > > static inline int testpin_tag(unsigned long handle) > > { > > - return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle); > > + return test_bit(HANDLE_PIN_BIT, (unsigned long *)handle); > > } > > > > static inline int trypin_tag(unsigned long handle) > > { > > - return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle); > > + return !test_and_set_bit(HANDLE_PIN_BIT, (unsigned long *)handle); > > } > > > > -static void pin_tag(unsigned long handle) __acquires(bitlock) > > +static void pin_tag(unsigned long handle) > > { > > - bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle); > > + preempt_disable(); > > + while(test_and_set_bit(HANDLE_PIN_BIT, (unsigned long *)handle)) > > + cpu_relax(); > > + preempt_enable(); > > } > > > > static void unpin_tag(unsigned long handle) __releases(bitlock) > > { > > - bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle); > > + clear_bit(HANDLE_PIN_BIT, (unsigned long *)handle); > > } > > > > static void reset_page(struct page *page) > > -- > > 2.20.1 > >