Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2080579ybz; Sat, 18 Apr 2020 15:04:08 -0700 (PDT) X-Google-Smtp-Source: APiQypI+bdCNJFCSlNzXz/4zSYN5ehG8WNqPYCkm5xwX+DU499N3lOHqsJViOKPNhlNoYK02/Pie X-Received: by 2002:a05:6402:14c1:: with SMTP id f1mr8649809edx.221.1587247448171; Sat, 18 Apr 2020 15:04:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587247448; cv=none; d=google.com; s=arc-20160816; b=cSeGBasNi6RkNfW/hmH1/QyTzIjUg8swqE6LIL7PosfoGuy96VH9wuDbI1AHBEdt7b DPD0mL90wLFPI3J20YgN+AGvoBnU/GKmkGl7Ra+ql12IOgCIgAGEztE+bDhEkb8usoTU plRJ3sR8mjUY6Wy7HerYgOLkKStjGCYpZqII+xyZ82ZCvUBTULXW2g72Yh8EI9bfthYF TiNohQdr61RFe2Y0yqfEtAbY2DS0BDkQIpQ8G27RA6+GusRgEERAI6rrF1VxOrtuamtA gnvlhAXw9iHqhekEhAuqQ06DBCHnfo2VMluHvl9Gyvoon1yJt/h3zhNMuIcezkbZxwQI +ggg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=k2F1YT95vbFuray7AER8KtaCXNMm/Pj9Jbm9f59uato=; b=bO/W3yBMTd4fTKGRVXLIduJaDaefLDx25goA+3U4y6JOuaKKS5q4Qglb/F9gh+QRBa F4uMGOBrbUUTgTMoU/3IJL6QPfq7aFe0njmZkdTNcY84Q2Sw2ySI8Yr/sQXj8LjJc7Ek fkS1aihiMEZK8ZCPGGZ/QsIVNGIPRaTz1yArJj3lBMOkEHdliL1DQXI6tckiIPRJAH5H R8qMSVuZQy/mA+NpT7rN6/AWztpYdJBw/ipWvHn6d7uN3kUNjlbsYxpIOOrQEvDlSMuI D72adYbXw2hYWU+71fhnLXOpX09Fu6qwxOLVa4Z99mSWGE3BpqsEh4W7QE7zamsV7s75 BN/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="F/DcX3qn"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hk16si17019216ejb.263.2020.04.18.15.03.45; Sat, 18 Apr 2020 15:04:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="F/DcX3qn"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728228AbgDRWCL (ORCPT + 99 others); Sat, 18 Apr 2020 18:02:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726887AbgDRWCK (ORCPT ); Sat, 18 Apr 2020 18:02:10 -0400 Received: from mail-il1-x141.google.com (mail-il1-x141.google.com [IPv6:2607:f8b0:4864:20::141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 317A0C061A0C; Sat, 18 Apr 2020 15:02:09 -0700 (PDT) Received: by mail-il1-x141.google.com with SMTP id q10so4528062ile.0; Sat, 18 Apr 2020 15:02:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=k2F1YT95vbFuray7AER8KtaCXNMm/Pj9Jbm9f59uato=; b=F/DcX3qn0mWP3wxePs/f6Ber+8vcImQLEQ1CnbNQTcIfCmABYQGfIKgMiyysqaCEL/ t90eT6S1ZjddO24lMhbAMC2HLortBaB77fS2qG+7q98k3eyT2Z2DWO/6uMtB1ap4ln0a jAYld2+pC/YdCQf8AunPt9C7hDJRYHbT+nbebEVsFwuaW54I01nc8i2TmQB9ioCJb9Zq OrQXNVF/JswexSm6WLEkxiidIfrvi78E95/ZjahRUOUamjJJ404iE775MJsQbOFTWdJH KoTGv6MK30ZK1Pa/TJon0K7fVbjB49hVTNztmqJsFug7Izfg2MhnwZ+Dll0OnNhTUVj8 qk9g== 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=k2F1YT95vbFuray7AER8KtaCXNMm/Pj9Jbm9f59uato=; b=XNWeOnN6cb9LlcHTOIs7pq6wiQaF8VXG74BVvJ41VKXRhKg8everwam/iNzVRzwq9A tCJBpeWR9g11/sevzDMqgMaB0x49cF3q9bst+cJ+tF2rmIt3z/R0E2f+Y4l0SONprY6Q 3MOVT6prPU0NkRN8GE5SEQB8ve5gKjw7qSienTAfkkGIZdwE9CmN5k8yWS/9cwM/ZEc0 RmGPbLsBugk+C+nAW1Yf2CwawiWqAwsbBg0LpDidjISHZELmbMl7UGrG3WgiYdjwzq3c 0N/AOUNY+sLcNZ2p0EMHoFSwZRDsRiZv7QzJSggR7V7HH552cKuQQHX7Zr0l3YMCXd+J 1TPg== X-Gm-Message-State: AGi0PuZQ+fqF0BjTeFaqj/gr+5xHnMG8A4JhyMQAXgGDeWnE37qNXA7y J7bwGsKCSRkf6wXN3p3+UDBI7diyAWdUvgmF1hA= X-Received: by 2002:a05:6e02:790:: with SMTP id q16mr8812413ils.60.1587247328320; Sat, 18 Apr 2020 15:02:08 -0700 (PDT) MIME-Version: 1.0 References: <20200417213951.29837-1-richard.weiyang@gmail.com> <20200418031922.GR17661@paulmck-ThinkPad-P72> In-Reply-To: <20200418031922.GR17661@paulmck-ThinkPad-P72> From: Wei Yang Date: Sun, 19 Apr 2020 06:02:12 +0800 Message-ID: Subject: Re: [PATCH] rcu: simplify the calculation of rcu_state.ncpus To: "Paul E. McKenney" Cc: josh@joshtriplett.org, rcu@vger.kernel.org, Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Apr 18, 2020 at 11:19 AM Paul E. McKenney wrote: > > On Fri, Apr 17, 2020 at 09:39:51PM +0000, Wei Yang wrote: > > There is only 1 bit set in mask, which means the difference between > > oldmask and the new one would be at the position where the bit is set in > > mask. > > > > Based on this knowledge, rcu_state.ncpus could be calculated by checking > > whether mask is already set in oldmask. > > Nice!!! Good eyes! > > > BTW, the comment at the last of this line is mysterious. Not sure it > > could be removed or not. > > The "^^^" in that comment says to look at the comment on the preceding > line. Memory-ordering functions like smp_store_release() are supposed > to have comments indicating what they are ordering. ;-) > > Could you please do the following things and resubmit? > > 1. Forward-port to -rcu branch dev? This tree lives here: > git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git > > 2. Given that oldmask is used only to test to see if a new bit > was set, why not just replace oldmask with a bool variable > that is set to "!(rnp->expmaskinitnext & mask)" before the > bit is ORed into rnp->expmaskinitnext? > > 3. Put the comment inside the "if" statement with the > smp_store_release(). > > 4. In -rcu, you will find a ASSERT_EXCLUSIVE_WRITER() statement > that should also be placed inside the "if" statement with > the smp_store_release(). > Oops, my email client EAT this mail. Hope this mail will not be banned. I adjust the code a little according to your suggestion like below. Is this what you expected? diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index f288477ee1c2..f01367a80b70 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -3732,10 +3732,9 @@ void rcu_cpu_starting(unsigned int cpu) { unsigned long flags; unsigned long mask; - int nbits; - unsigned long oldmask; struct rcu_data *rdp; struct rcu_node *rnp; + bool has_seen; if (per_cpu(rcu_cpu_started, cpu)) return; @@ -3747,13 +3746,13 @@ void rcu_cpu_starting(unsigned int cpu) mask = rdp->grpmask; raw_spin_lock_irqsave_rcu_node(rnp, flags); WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext | mask); - oldmask = rnp->expmaskinitnext; + has_seen = rnp->expmaskinitnext & mask; rnp->expmaskinitnext |= mask; - oldmask ^= rnp->expmaskinitnext; - nbits = bitmap_weight(&oldmask, BITS_PER_LONG); - /* Allow lockless access for expedited grace periods. */ - smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */ - ASSERT_EXCLUSIVE_WRITER(rcu_state.ncpus); + if (!has_seen) { + /* Allow lockless access for expedited grace periods. */ + smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + 1); /* ^^^ */ + ASSERT_EXCLUSIVE_WRITER(rcu_state.ncpus); + } rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */ rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq); rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags); > Thanx, Paul > > > Signed-off-by: Wei Yang > > --- > > kernel/rcu/tree.c | 6 ++---- > > 1 file changed, 2 insertions(+), 4 deletions(-) > > > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > > index d91c9156fab2..f0d9251fa663 100644 > > --- a/kernel/rcu/tree.c > > +++ b/kernel/rcu/tree.c > > @@ -3364,7 +3364,6 @@ void rcu_cpu_starting(unsigned int cpu) > > { > > unsigned long flags; > > unsigned long mask; > > - int nbits; > > unsigned long oldmask; > > struct rcu_data *rdp; > > struct rcu_node *rnp; > > @@ -3381,10 +3380,9 @@ void rcu_cpu_starting(unsigned int cpu) > > rnp->qsmaskinitnext |= mask; > > oldmask = rnp->expmaskinitnext; > > rnp->expmaskinitnext |= mask; > > - oldmask ^= rnp->expmaskinitnext; > > - nbits = bitmap_weight(&oldmask, BITS_PER_LONG); > > /* Allow lockless access for expedited grace periods. */ > > - smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */ > > + if (!(oldmask & mask)) > > + smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + 1); /* ^^^ */ > > rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */ > > rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq); > > rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags); > > -- > > 2.23.0 > >