Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp4507689imm; Mon, 25 Jun 2018 17:35:46 -0700 (PDT) X-Google-Smtp-Source: ADUXVKL2z31SbJUefdZ8wdPQU3wqqcNRpvQCE/tA1a2AvSOw8XBYghX5PRSIjOmqhajZk4ZSNTu6 X-Received: by 2002:a65:51cb:: with SMTP id i11-v6mr12641626pgq.320.1529973346757; Mon, 25 Jun 2018 17:35:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529973346; cv=none; d=google.com; s=arc-20160816; b=qrO9z0B4zkSGaIV2LxXccGyEUkfIZT2ha2nqBvgAGscKVO0pm6/KkR5paNSdP/tMTk r9IyQckqcl0ZezlYPkBylVzc40U2KH0LI+KZ3Ba27DDUG6fqCq0rNr3NOgLXqE0Ma4rd A0YVRDJcZq+GG7+M8x9IvEWY+/K0mYz7TvRbUItLHfg3g6cg4hD84LttvWbMWAC2VFcK 7A73o6h4XiBZnHoWaXo5WotI7IL1YcFnnkWxtyarrJ9k3JWHU+QUC1RFKF9N7L8ZaW9v YNxUZyD5mUR/iQnicgXswWQRt9WhWOixwEDs01UrtVkT3/75AI2hGVeXCoZWmSPAsmL0 2vmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:references:in-reply-to:date :subject:cc:to:from:arc-authentication-results; bh=mExWh5J4ze88NUosta+ctRBPekf2nsrSOpJN24OCoG4=; b=iHqi603VVCrlKKZH2ryT1GGD3rr7dL6XWBB1ZxbYCFouTlgf4+o5CWDT2/xi80JeM5 RyR05+EBcnYweUdOpobZnxNV6pir3BrPrOWQa/jgt1pnIxXszOb9bBSWeOJS7fHntULB OgkcH+XnoDjpqzE64kko5Zib1Q97ws72XkXL3fQ02awCL3ByS59ywFEq+ZAqqBJwIezR 0YNMdBwuE3SQteBT4DCmHqGFGAit/xspCqvqBNqAOer3JJ/hkp6ggF7jMfcJGk92bkf+ iuhJHtKUZTORmDmNouXS2LnWYJLo36aHzIEK84a7/5AVjWNqXq03FUCUcJiDssfw2p20 INYQ== 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 b1-v6si225782plc.403.2018.06.25.17.35.31; Mon, 25 Jun 2018 17:35:46 -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 S964979AbeFZAdp (ORCPT + 99 others); Mon, 25 Jun 2018 20:33:45 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:36538 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755745AbeFZAdT (ORCPT ); Mon, 25 Jun 2018 20:33:19 -0400 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w5Q0Seif140927 for ; Mon, 25 Jun 2018 20:33:18 -0400 Received: from e14.ny.us.ibm.com (e14.ny.us.ibm.com [129.33.205.204]) by mx0b-001b2d01.pphosted.com with ESMTP id 2ju96rbd1d-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 25 Jun 2018 20:33:18 -0400 Received: from localhost by e14.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 25 Jun 2018 20:33:17 -0400 Received: from b01cxnp22034.gho.pok.ibm.com (9.57.198.24) by e14.ny.us.ibm.com (146.89.104.201) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Mon, 25 Jun 2018 20:33:12 -0400 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp22034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w5Q0XBT45112232 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 26 Jun 2018 00:33:11 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F1BA9B2070; Mon, 25 Jun 2018 20:33:05 -0400 (EDT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D3200B2065; Mon, 25 Jun 2018 20:33:05 -0400 (EDT) Received: from paulmck-ThinkPad-W541 (unknown [9.70.82.159]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Mon, 25 Jun 2018 20:33:05 -0400 (EDT) Received: by paulmck-ThinkPad-W541 (Postfix, from userid 1000) id 0F40D16CA0A2; Mon, 25 Jun 2018 17:35:15 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: mingo@kernel.org, jiangshanlai@gmail.com, dipankar@in.ibm.com, akpm@linux-foundation.org, mathieu.desnoyers@efficios.com, josh@joshtriplett.org, tglx@linutronix.de, peterz@infradead.org, rostedt@goodmis.org, dhowells@redhat.com, edumazet@google.com, fweisbec@gmail.com, oleg@redhat.com, joel@joelfernandes.org, "Paul E . McKenney" Subject: [PATCH tip/core/rcu 16/27] rcu: Add comment documenting how rcu_seq_snap works Date: Mon, 25 Jun 2018 17:35:02 -0700 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180626003448.GA26209@linux.vnet.ibm.com> References: <20180626003448.GA26209@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18062600-0052-0000-0000-000003040982 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009255; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000266; SDB=6.01052336; UDB=6.00539469; IPR=6.00830262; MB=3.00021856; MTD=3.00000008; XFM=3.00000015; UTC=2018-06-26 00:33:16 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18062600-0053-0000-0000-00005D23DD94 Message-Id: <20180626003513.27812-16-paulmck@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-06-25_11:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1806260005 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) Signed-off-by: Paul E. McKenney --- 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 aa215d6355f8..60f089d08c47 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.1