Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1036546imm; Wed, 23 May 2018 09:15:45 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpQvmF5JDIZhMNQMPnNKL9etftfZ2a/3wms8RAfi6Jbhud0INQuMFA8EpwQCA+HH1xLIzCW X-Received: by 2002:a62:c2:: with SMTP id 185-v6mr3524867pfa.238.1527092145000; Wed, 23 May 2018 09:15:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527092144; cv=none; d=google.com; s=arc-20160816; b=Ei62e24Ae1hky0uJSWNATtpU1ewSuiCC/0JWValvgdc4PNtOhsFhsNG2g+5EJT2+jD S6VpzMB3ELQQcK03k8oY+G3PAD4GYFfD30kwfTPbYxaVCAYzmpvMuQ3quh2iKbDgvgUb Y5oEXV4KaVPhTRhHG9440Nk+L3kAydmlx5j73zefJEz+MNX51NzBSf698B5MJSs0dRJy yqDy3pMSPfmv5I8wm4qTNOUFL7/i0bYXljup8cwB7HPo8yTdeOYWUTlJfBFGAaV9OLyy rZy66JX9UDy6sSiXv5V6DUbz9P4SOR5QNYq41LGhZ0dSDENiEGN6H9L/wU7+K20DRAnq oMhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:subject:cc:to :from:date:arc-authentication-results; bh=NjlukLblY7sF/1WnfOLERHqb7oLkmRyMuegv1bUflLc=; b=fMOtgsV+BVDQVvMieqIOv021tAGEfWYjgXv9/C6ptgTodVPfSgVpxYOcuHQDvf/LOC mw7NWg6MzgX2DIUI4AVQ0aQXBS5QfGx1r1ahoAik0+UsFH9H4qld7xoJxSxjAG19UIcl dggg2pG1zTfIDyxiTloCVtG7aVoCy8dqNR9CokIYAbpjckQULKUxVFYbC3jI5O5KLEaB gtDAJxpDmCc8zvcM+QMKR5EU/4nHXu/KCzfEywf/mJzIlpHdhsC2SgcZsYXaSwmGclI7 3lNdEHG+AFF48uDKKvYFiPsiiuW9ldlpOyxXhN4Til83IT3f1r9NtrOcDA64Tdt6ddLz ROAQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b8-v6si18853626ple.469.2018.05.23.09.15.16; Wed, 23 May 2018 09:15:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754743AbeEWQNs (ORCPT + 99 others); Wed, 23 May 2018 12:13:48 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:51832 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751646AbeEWQNp (ORCPT ); Wed, 23 May 2018 12:13:45 -0400 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w4NG5mC6040919 for ; Wed, 23 May 2018 12:13:44 -0400 Received: from e33.co.us.ibm.com (e33.co.us.ibm.com [32.97.110.151]) by mx0a-001b2d01.pphosted.com with ESMTP id 2j5b809kph-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 23 May 2018 12:13:44 -0400 Received: from localhost by e33.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 23 May 2018 10:13:43 -0600 Received: from b01cxnp23032.gho.pok.ibm.com (9.57.198.27) by e33.co.us.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 23 May 2018 10:13:38 -0600 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp23032.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4NG2Lp52425152; Wed, 23 May 2018 16:02:21 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A6D61B206A; Wed, 23 May 2018 13:04:10 -0400 (EDT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 75C47B2072; Wed, 23 May 2018 13:04:10 -0400 (EDT) Received: from paulmck-ThinkPad-W541 (unknown [9.70.82.108]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Wed, 23 May 2018 13:04:10 -0400 (EDT) Received: by paulmck-ThinkPad-W541 (Postfix, from userid 1000) id 4839716C5DD1; Wed, 23 May 2018 09:04:00 -0700 (PDT) Date: Wed, 23 May 2018 09:04:00 -0700 From: "Paul E. McKenney" To: Joel Fernandes Cc: linux-kernel@vger.kernel.org, "Joel Fernandes (Google)" , Boqun Feng , byungchul.park@lge.com, Ingo Molnar , Josh Triplett , kernel-team@android.com, Lai Jiangshan , Mathieu Desnoyers , Peter Zilstra , Steven Rostedt Subject: Re: [PATCH 2/4] rcu: Add comment documenting how rcu_seq_snap works Reply-To: paulmck@linux.vnet.ibm.com References: <20180523063815.198302-1-joel@joelfernandes.org> <20180523063815.198302-3-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180523063815.198302-3-joel@joelfernandes.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-GCONF: 00 x-cbid: 18052316-0008-0000-0000-000009D7E758 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009072; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000261; SDB=6.01036557; UDB=6.00530281; IPR=6.00815684; MB=3.00021260; MTD=3.00000008; XFM=3.00000015; UTC=2018-05-23 16:13:42 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18052316-0009-0000-0000-0000476CA3BE Message-Id: <20180523160400.GL3803@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-05-23_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805230159 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 22, 2018 at 11:38:13PM -0700, Joel Fernandes wrote: > From: "Joel Fernandes (Google)" > > rcu_seq_snap may be tricky to decipher. Lets document how it works with > an example to make it easier. > > Signed-off-by: Joel Fernandes (Google) Very good, queued for further review, thank you! Thanx, Paul > --- > kernel/rcu/rcu.h | 34 +++++++++++++++++++++++++++++++++- > 1 file changed, 33 insertions(+), 1 deletion(-) > > diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h > index 0453a7d12b3f..5819a37f19fb 100644 > --- a/kernel/rcu/rcu.h > +++ b/kernel/rcu/rcu.h > @@ -91,7 +91,39 @@ static inline void rcu_seq_end(unsigned long *sp) > WRITE_ONCE(*sp, rcu_seq_endval(sp)); > } > > -/* Take a snapshot of the update side's sequence number. */ > +/* > + * rcu_seq_snap - Take a snapshot of the update side's sequence number. > + * > + * This function returns the earliest value of the grace-period sequence number > + * that will indicate that a full grace period has elapsed since the current > + * time. Once the grace-period sequence number has reached this value, it will > + * be safe to invoke all callbacks that have been registered prior to the > + * current time. This value is the current grace-period number plus two to the > + * power of the number of low-order bits reserved for state, then rounded up to > + * the next value in which the state bits are all zero. > + * > + * In the current design, RCU_SEQ_STATE_MASK=3 and the least significant bit of > + * the seq is used to track if a GP is in progress or not. Given this, it is > + * sufficient if we add (6+1) and mask with ~3 to get the next GP. Let's see > + * why with an example: > + * > + * Say the current seq is 12 which is 0b1100 (GP is 3 and state bits are 0b00). > + * To get to the next GP number of 4, we have to add 0b100 to this (0x1 << 2) > + * to account for the shift due to 2 state bits. Now, if the current seq is > + * 13 (GP is 3 and state bits are 0b01), then it means the current grace period > + * is already in progress so the next GP that a future call back will be queued > + * to run at is GP+2 = 5, not 4. To account for the extra +1, we just overflow > + * the 2 lower bits by adding 0b11. In case the lower bit was set, the overflow > + * will cause the extra +1 to the GP, along with the usual +1 explained before. > + * This gives us GP+2. Finally we mask the lower to bits by ~0x3 in case the > + * overflow didn't occur. This masking is needed because in case RCU was idle > + * (no GP in progress so lower 2 bits are 0b00), then the overflow of the lower > + * 2 state bits wouldn't occur, so we mask to zero out those lower 2 bits. > + * > + * In other words, the next seq can be obtained by (0b11 + 0b100) & (~0b11) > + * which can be generalized to: > + * seq + (RCU_SEQ_STATE_MASK + (RCU_SEQ_STATE_MASK + 1)) & (~RCU_SEQ_STATE_MASK) > + */ > static inline unsigned long rcu_seq_snap(unsigned long *sp) > { > unsigned long s; > -- > 2.17.0.441.gb46fe60e1d-goog >