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=-8.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,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 E2C95C433B4 for ; Thu, 13 May 2021 12:36:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 68124613CA for ; Thu, 13 May 2021 12:36:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 68124613CA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E6E8F6B0036; Thu, 13 May 2021 08:36:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DF6CC6B006E; Thu, 13 May 2021 08:36:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C496F6B0070; Thu, 13 May 2021 08:36:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0164.hostedemail.com [216.40.44.164]) by kanga.kvack.org (Postfix) with ESMTP id 8EB146B0036 for ; Thu, 13 May 2021 08:36:37 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 2C7BC8249980 for ; Thu, 13 May 2021 12:36:37 +0000 (UTC) X-FDA: 78136156434.30.A833AAF Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) by imf03.hostedemail.com (Postfix) with ESMTP id 48353C0007F8 for ; Thu, 13 May 2021 12:36:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=2ooNLbXkTEFrnDrmch+4N2vrCFIT9f6rQmy04qi/aI0=; b=QzamFht2I/KHT8GUirs3Qhkzhp CFf0h475mZ86FQbYN7e1puF6i/qH7IrwM0iTnFZ/gE3O3Abzc/1kqS5agj2Loou5DOzBRYhF6aQsU lfkLr/4nQ/z05ltk8l3rKhIrKhLV/n8v5REyXJpDONDJ3fc7kdgob3zX735UcA+//Q/itMb02jyJa u5mDQSoYgzm2ObzSnJ1T72SEq5RYdjFuDxmtu//oI85o43Z/NiY9qp7OTlSRozaNUhJtMfcH+hdic h7Ts+OiiL0znohQC/CuU3FkATYL+YzC5amXUqkk0QpFWAW4jNstE5SHFwAsgmoEWfAXRTLpv1eclx wypHBUTA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lhAXp-005Wgs-Ie; Thu, 13 May 2021 12:36:25 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id E3DD630022A; Thu, 13 May 2021 14:34:19 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id BB0172BCF6229; Thu, 13 May 2021 14:34:19 +0200 (CEST) Date: Thu, 13 May 2021 14:34:19 +0200 From: Peter Zijlstra To: Muchun Song Cc: Miaohe Lin , Huang Ying , Andrew Morton , linux-mm@kvack.org, linux-kernel , Daniel Jordan , Dan Carpenter , Andrea Parri , Andi Kleen , Dave Hansen , Omar Sandoval , Paul McKenney , Tejun Heo , Will Deacon Subject: Re: [PATCH] mm, swap: Remove unnecessary smp_rmb() in swap_type_to_swap_info() Message-ID: References: <20210513064837.3949064-1-ying.huang@intel.com> <176421ba-77d9-8422-21e0-b5e2951533bd@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b=QzamFht2; spf=none (imf03.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org; dmarc=none X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 48353C0007F8 X-Stat-Signature: efzemnxjqiktp9gcnk5k5ybq6yjj7t9t Received-SPF: none (infradead.org>: No applicable sender policy available) receiver=imf03; identity=mailfrom; envelope-from=""; helo=desiato.infradead.org; client-ip=90.155.92.199 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620909388-828822 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 Thu, May 13, 2021 at 05:54:42PM +0800, Muchun Song wrote: > On Thu, May 13, 2021 at 5:11 PM Miaohe Lin wrote: > > On 2021/5/13 14:48, Huang Ying wrote: > > > mm/swapfile.c | 18 +++++++++--------- > > > 1 file changed, 9 insertions(+), 9 deletions(-) > > > > > > diff --git a/mm/swapfile.c b/mm/swapfile.c > > > index 2aad85751991..4c1fb28bbe0e 100644 > > > --- a/mm/swapfile.c > > > +++ b/mm/swapfile.c > > > @@ -100,10 +100,14 @@ atomic_t nr_rotate_swap = ATOMIC_INIT(0); > > > > > > static struct swap_info_struct *swap_type_to_swap_info(int type) > > > { > > > - if (type >= READ_ONCE(nr_swapfiles)) > > > + if (type >= MAX_SWAPFILES) > > > return NULL; > > > > > > - smp_rmb(); /* Pairs with smp_wmb in alloc_swap_info. */ > > > + /* > > > + * The data dependency ordering from the READ_ONCE() pairs > > > + * with smp_wmb() in alloc_swap_info() to guarantee the > > > + * swap_info_struct fields are read after swap_info[type]. > > > + */ > > > return READ_ONCE(swap_info[type]); > > > } > > > > > > @@ -2884,14 +2888,10 @@ static struct swap_info_struct *alloc_swap_info(void) > > > } > > > if (type >= nr_swapfiles) { > > > p->type = type; > > > - WRITE_ONCE(swap_info[type], p); > > > - /* > > > - * Write swap_info[type] before nr_swapfiles, in case a > > > - * racing procfs swap_start() or swap_next() is reading them. > > > - * (We never shrink nr_swapfiles, we never free this entry.) > > > - */ > > > + /* Paired with READ_ONCE() in swap_type_to_swap_info() */ > > > smp_wmb(); > > > > Many thank for your patch. The patch looks fine to me. There is one question: > > > > There is no smp_rmb() paired with above smp_wmb(). What is this smp_wmb() used for ? > > Could you please have a explanation ? > > The comment is very clear, it matches READ_ONCE() which implies a > data dependence barrier on some archs. This statement doesn't make sense; this isn't code that needs to be correct on 'some' archs, it needs to be unconditionally correct. Also, you cannot pair with a single memop, there is no order in a set of one element. And if you depend on a data dependency, you need a store order; but you just removed the store order. in which case the data dependency is also moot. All of this is utter confusion. Possibly correct, but a complete trainwreck non-the-less. Either you say ordering is irrelevant, because we only ever increase the number of swapfiles and therefore any load is either NULL or the correct pointer, as guaranteed by WRITE_ONCE()/READ_ONCE() avoiding load/store tearing. Or you need the data dependency, but then you also need the store order like: CPU0 CPU1 if (type >= READ_ONCE(nr_swapfiles)) WRITE_ONCE(swap_info[type], p); return NULL; /* data-dependency on type */ smp_wmb(); return READ_ONCE(swap_info[type]); WRITE_ONCE(nr_swapfiles, nr_swapfiles+1); But you cannot have half of both and expect any of it to make sense.