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=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 A92ADC2B9F4 for ; Fri, 25 Jun 2021 10:41:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 351BF61443 for ; Fri, 25 Jun 2021 10:41:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 351BF61443 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 25DCC6B0036; Fri, 25 Jun 2021 06:41:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 20DC86B005D; Fri, 25 Jun 2021 06:41:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D75C6B006C; Fri, 25 Jun 2021 06:41:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0099.hostedemail.com [216.40.44.99]) by kanga.kvack.org (Postfix) with ESMTP id D183D6B0036 for ; Fri, 25 Jun 2021 06:41:10 -0400 (EDT) Received: from smtpin34.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 0880322864 for ; Fri, 25 Jun 2021 10:41:11 +0000 (UTC) X-FDA: 78291903942.34.EB3765B Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf12.hostedemail.com (Postfix) with ESMTP id 0C891551 for ; Fri, 25 Jun 2021 10:41:09 +0000 (UTC) Received: by mail-pg1-f178.google.com with SMTP id y14so7230528pgs.12 for ; Fri, 25 Jun 2021 03:41:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=mHJbYA6bbS+Rn1vMcZPcer89nM3y6eLuvzQ4DpVNe4g=; b=DbOMbmrTXCIGPSyTPRlDQ2Gt4wxvzQ+PuqZoZ3l24O2N+LCxnScqPt/uqSTF4pE1KC Bb4Zr/TwkdJL/G2vjrZhJWKAinpmz1Tjo0UwkKEfyiZ0cEGtf9Cj2LURFUi0Hr3Xbfqv +T/hezLmZ5GFlq2Hu8nmoyKsaNx1+MOTEt0D0S6td0XqTuO3wQjBPER8JyJb/EujFrWN pdI5Q7TwWRIf5k9kN64WRgTbHXx/BdMXJVDX3FS6eE+f97rq5yAyYcB9KC9Ha6QGWPc0 wpz0fa8WbWHXG26tNmbDi/D8ztAXaJFhgXacuN++7hwWwjtTh/efYf76NssAiZR0CO6i MKlg== 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=mHJbYA6bbS+Rn1vMcZPcer89nM3y6eLuvzQ4DpVNe4g=; b=dhvlMBgxB1EqkuoB4Tywn6IUl7/NWtpp9/+nkXEWpM4Gf7EkcTmIGhIT2HnFAbgtEj Rtbc4thzLuT+QD4fBsohfVewJLN6APBinaO1ROGHeozH5CPKoI79GjpKxba/kENX6ws2 XdLIBK7u3+xCdhLZKrHqAfRl+XlLsdDryN//25xGiknBZE8o2an0eql25kdhiRYsONvX 2bcvlwH1vm9luV7T+Lk0hOWzrW6tFadgyE4QmJ1TJm9MZ9xoeagrmT+x/Tu63QCWfYhr EJ5AVluSRiw4K3G6nzo+z9lVv1cCBdULL2ZjZOBLCOKeNH8lxWBEls8iJirMXS6O3Uj6 7kjQ== X-Gm-Message-State: AOAM532WzfIHHLiQ3btdtui3zY0Zn4UVOQDtk05HqHgosEe0KLhjFxvM OFUUSQi6PVZxcyXQouR1PzTaUrPGt2deklocnVeT+A== X-Google-Smtp-Source: ABdhPJzNFdimxcH+hedzqw2q/ehH4P2k1STjQ7cFSMt7KGDcDuAx8yYqyWBJx4FP/SSvlHfuSow3ThQjEVe5500M7jM= X-Received: by 2002:a05:6a00:2162:b029:308:9346:2f55 with SMTP id r2-20020a056a002162b029030893462f55mr6289438pff.49.1624617668717; Fri, 25 Jun 2021 03:41:08 -0700 (PDT) MIME-Version: 1.0 References: <20210624123930.1769093-1-linmiaohe@huawei.com> <20210624123930.1769093-3-linmiaohe@huawei.com> <1b38b33f-316e-1816-216f-9923f612ceb6@huawei.com> <01117bc0-53b1-d81a-a4d8-2a1dbe5dcd94@huawei.com> <97fdc2f3-6757-7ca1-6323-02b618b85894@huawei.com> In-Reply-To: <97fdc2f3-6757-7ca1-6323-02b618b85894@huawei.com> From: Muchun Song Date: Fri, 25 Jun 2021 18:40:30 +0800 Message-ID: Subject: Re: [Phishing Risk] [External] [PATCH 2/3] mm/zsmalloc.c: combine two atomic ops in zs_pool_dec_isolated() To: Miaohe Lin Cc: Andrew Morton , Minchan Kim , ngupta@vflare.org, senozhatsky@chromium.org, LKML , Linux Memory Management List Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=DbOMbmrT; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf12.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-Rspamd-Server: rspam02 X-Stat-Signature: x81k9kbf583thhf5cb83qujsxz7wakbs X-Rspamd-Queue-Id: 0C891551 X-HE-Tag: 1624617669-72957 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 Fri, Jun 25, 2021 at 5:32 PM Miaohe Lin wrote: > > On 2021/6/25 16:46, Miaohe Lin wrote: > > On 2021/6/25 15:29, Muchun Song wrote: > >> On Fri, Jun 25, 2021 at 2:32 PM Miaohe Lin wrote: > >>> > >>> On 2021/6/25 13:01, Muchun Song wrote: > >>>> On Thu, Jun 24, 2021 at 8:40 PM Miaohe Lin wrote: > >>>>> > >>>>> atomic_long_dec_and_test() is equivalent to atomic_long_dec() and > >>>>> atomic_long_read() == 0. Use it to make code more succinct. > >>>> > >>>> Actually, they are not equal. atomic_long_dec_and_test implies a > >>>> full memory barrier around it but atomic_long_dec and atomic_long_read > >>>> don't. > >>>> > >>> > >>> Many thanks for comment. They are indeed not completely equal as you said. > >>> What I mean is they can do the same things we want in this specified context. > >>> Thanks again. > >> > >> I don't think so. Using individual operations can eliminate memory barriers. > >> We will pay for the barrier if we use atomic_long_dec_and_test here. > > > > The combination of atomic_long_dec and atomic_long_read usecase is rare and looks somehow > > weird. I think it's worth to do this with the cost of barrier. > > > > It seems there is race between zs_pool_dec_isolated and zs_unregister_migration if pool->destroying > is reordered before the atomic_long_dec and atomic_long_read ops. So this memory barrier is necessary: > > zs_pool_dec_isolated zs_unregister_migration > pool->destroying != true > pool->destroying = true; > smp_mb(); > wait_for_isolated_drain > wait_event with atomic_long_read(&pool->isolated_pages) != 0 > atomic_long_dec(&pool->isolated_pages); > atomic_long_read(&pool->isolated_pages) == 0 I am not familiar with zsmalloc. So I do not know whether the race that you mentioned above exists. But If it exists, the fix also does not make sense to me. If there should be inserted a smp_mb between atomic_long_dec and atomic_long_read, you should insert smp_mb__after_atomic instead of using atomic_long_dec_and_test. Because smp_mb__after_atomic can be optimized on certain architecture (e.g. x86_64). Thanks. > > Thus wake_up_all is missed. > And the comment in zs_pool_dec_isolated() said: > /* > * There's no possibility of racing, since wait_for_isolated_drain() > * checks the isolated count under &class->lock after enqueuing > * on migration_wait. > */ > > But I found &class->lock is indeed not acquired for wait_for_isolated_drain(). So I think the above race > is possible. Does this make senses for you ? > Thanks. > > >> > >>> > >>>> That RMW operations that have a return value is equal to the following. > >>>> > >>>> smp_mb__before_atomic() > >>>> non-RMW operations or RMW operations that have no return value > >>>> smp_mb__after_atomic() > >>>> > >>>> Thanks. > >>>> > >>>>> > >>>>> Signed-off-by: Miaohe Lin > >>>>> --- > >>>>> mm/zsmalloc.c | 3 +-- > >>>>> 1 file changed, 1 insertion(+), 2 deletions(-) > >>>>> > >>>>> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c > >>>>> index 1476289b619f..0b4b23740d78 100644 > >>>>> --- a/mm/zsmalloc.c > >>>>> +++ b/mm/zsmalloc.c > >>>>> @@ -1828,13 +1828,12 @@ static void putback_zspage_deferred(struct zs_pool *pool, > >>>>> static inline void zs_pool_dec_isolated(struct zs_pool *pool) > >>>>> { > >>>>> VM_BUG_ON(atomic_long_read(&pool->isolated_pages) <= 0); > >>>>> - atomic_long_dec(&pool->isolated_pages); > >>>>> /* > >>>>> * There's no possibility of racing, since wait_for_isolated_drain() > >>>>> * checks the isolated count under &class->lock after enqueuing > >>>>> * on migration_wait. > >>>>> */ > >>>>> - if (atomic_long_read(&pool->isolated_pages) == 0 && pool->destroying) > >>>>> + if (atomic_long_dec_and_test(&pool->isolated_pages) && pool->destroying) > >>>>> wake_up_all(&pool->migration_wait); > >>>>> } > >>>>> > >>>>> -- > >>>>> 2.23.0 > >>>>> > >>>> . > >>>> > >>> > >> . > >> > > >