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.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable 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 22D7DC433B4 for ; Fri, 14 May 2021 03:27:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A9D9961440 for ; Fri, 14 May 2021 03:27:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A9D9961440 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D447E6B0036; Thu, 13 May 2021 23:27:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1AF46B006E; Thu, 13 May 2021 23:27:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE2AD6B0070; Thu, 13 May 2021 23:27:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0043.hostedemail.com [216.40.44.43]) by kanga.kvack.org (Postfix) with ESMTP id 8D0C76B0036 for ; Thu, 13 May 2021 23:27:35 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 2198A12C2 for ; Fri, 14 May 2021 03:27:35 +0000 (UTC) X-FDA: 78138401670.27.CE6283A Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf29.hostedemail.com (Postfix) with ESMTP id 42A02132 for ; Fri, 14 May 2021 03:27:33 +0000 (UTC) IronPort-SDR: Sd9FLeEOoYkpXY34uYg6Z3clV1891pSdXK2yTDNpmTAv6fuvtkjY8XYTIsRY1afFTDowODp4W6 NxTnts5Pvp5g== X-IronPort-AV: E=McAfee;i="6200,9189,9983"; a="221112525" X-IronPort-AV: E=Sophos;i="5.82,298,1613462400"; d="scan'208";a="221112525" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 May 2021 20:27:31 -0700 IronPort-SDR: qYb2Cd2KQ3lTadR1ao0Y7/v/ZTDBmJXywCSIXhnLMMMmC3hqPzHkUSzT1FZrPiBOI0i1Os7DbL va5iY3oN6ydg== X-IronPort-AV: E=Sophos;i="5.82,298,1613462400"; d="scan'208";a="401429050" Received: from yhuang6-desk1.sh.intel.com (HELO yhuang6-desk1.ccr.corp.intel.com) ([10.239.13.1]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 May 2021 20:27:28 -0700 From: "Huang, Ying" To: Peter Zijlstra Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Daniel Jordan , Dan Carpenter , Andrea Parri , Andi Kleen , Dave Hansen , Omar Sandoval , Paul McKenney , Tejun Heo , Will Deacon , Miaohe Lin Subject: Re: [PATCH] mm, swap: Remove unnecessary smp_rmb() in swap_type_to_swap_info() References: <20210513064837.3949064-1-ying.huang@intel.com> Date: Fri, 14 May 2021 11:27:26 +0800 In-Reply-To: (Peter Zijlstra's message of "Thu, 13 May 2021 14:46:10 +0200") Message-ID: <87cztuf10h.fsf@yhuang6-desk1.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Rspamd-Queue-Id: 42A02132 Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf29.hostedemail.com: domain of ying.huang@intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=ying.huang@intel.com X-Rspamd-Server: rspam04 X-Stat-Signature: oytxhu9qfueku11pbgpm6ka15hguifbu X-HE-Tag: 1620962853-442261 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: Peter Zijlstra writes: > On Thu, May 13, 2021 at 02:48:37PM +0800, 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(); >> - WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1); >> + WRITE_ONCE(swap_info[type], p); >> + nr_swapfiles++; > > Ah, I think I see what you meant to say, it would perhaps help if you > write it like so: > > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 149e77454e3c..94735248dcd2 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -99,11 +99,10 @@ 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. */ > - return READ_ONCE(swap_info[type]); > + return READ_ONCE(swap_info[type]); /* rcu_dereference() */ > } > > static inline unsigned char swap_count(unsigned char ent) > @@ -2869,14 +2868,11 @@ 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.) > + * Publish the swap_info_struct. > */ > - smp_wmb(); > - WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1); > + smp_store_release(&swap_info[type], p); /* rcu_assign_pointer() */ > + nr_swapfiles++; > } else { > defer = p; > p = swap_info[type]; OK. It seems that this helps people to understand. I will use this in the next version. Best Regards, Huang, Ying