Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3787205imm; Sun, 13 May 2018 20:17:31 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoVTt7TWW/Dpn8909v1NF9/SPac6TCEI+g/H45rczqoGfRLfMtYUF8k7S7OlGunjuiKTS7W X-Received: by 2002:a62:6402:: with SMTP id y2-v6mr8342836pfb.71.1526267851929; Sun, 13 May 2018 20:17:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526267851; cv=none; d=google.com; s=arc-20160816; b=CocK5IySv2T+6Zeen4Ww7QJWg3n9th6qtt5V7f4P6w/8MJhIfvoPI9+l37t4t79+9X 6yz9dL7rl7nX+fPTEfsE9taR3e7HJoCPf+9srcJilv3LUcq7K1ewCRrEGW2DAKMoxHic nQrnDrZ/wgyos9XK7X+vOL2QbdIrPi1JhPToPqyNp3kfF5DeZ5wON90ALFeJLDLFk6M9 zmhSdolI7oRzq/FFdSu6xBrbo6iCrs9sO5odJR3Y8qTyBhu63aVHPmr1zwg+VFXigXAb hgClK/Q8jyxpsb0ZZYdu2u37/ntWUYmwgkMwP79ixsV01w4n87MjX/SYmRAVa9geaPfi Bppw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=cdR0NebBJ7Trf42dUqSYTdhp7Jmo7zpMqeuqQL+SPn4=; b=D4caX5rkazwvQdvemXK58cMu950g7nZI+kJ3jzTRJM0ZeA7kQ/qhybyyxOx0baeLp2 w4M1YPfgQ1qydX4iXOMfBL0hyffOKeWwjM4u4k3iqRvOd+w3hpy+c4CVANe422EionFz nvCv8swT53cJhHFFSNnj0RdjzYrMSbKukYWUyrKBm9m8Qe2qkwkirct/SgnbynoKmFHH mRrGfNbW/BGjjyz/knZPRwiW1lpdmrGNxKhZP1Iduq2/qyJnwjNtL72x8n+EJ3UfdE8P pPnc22tqjKS8XvllQOYHaNlEtdx9kyY16CDWyV/eqBZqhGzIB+KNzz+vj8UGTOF2YgJl tduA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes-org.20150623.gappssmtp.com header.s=20150623 header.b=z0YDY/SS; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b2-v6si5868834pgs.110.2018.05.13.20.17.16; Sun, 13 May 2018 20:17:31 -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; dkim=pass header.i=@joelfernandes-org.20150623.gappssmtp.com header.s=20150623 header.b=z0YDY/SS; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752382AbeENDQG (ORCPT + 99 others); Sun, 13 May 2018 23:16:06 -0400 Received: from mail-pl0-f68.google.com ([209.85.160.68]:44129 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752191AbeENDQB (ORCPT ); Sun, 13 May 2018 23:16:01 -0400 Received: by mail-pl0-f68.google.com with SMTP id e6-v6so6490207plt.11 for ; Sun, 13 May 2018 20:16:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=cdR0NebBJ7Trf42dUqSYTdhp7Jmo7zpMqeuqQL+SPn4=; b=z0YDY/SSZQ0+J/Eg5FhU4xxr7e42656JMZO2zQG+ctlz7nkGX1ZOJleonZA7v8liZR gzm7I6ScFgZEPQeyp1iawTOXmg6UymFylP4ZtP3JzJihp/svrxtK8lTSKEquZQlQnWWh NC4hNRuRZSLa5mawUgd9RiARzJKlWwOBWAulVdp/2SO42gp8hYO/XW/zZoorMlU1JA4y c4263c0UZFLV8t8e1yQSceTMyTEvWK/We/QfFiA7clD0/lGdUeZx2V1IEdOmk/lxv2J3 TTIkx57tCsHrml4+i2Ie1ERZ7J4/6K18DwcXJ9u/kKfGQJaK9QXPtiPpMuvnPTIVtEO9 NQrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=cdR0NebBJ7Trf42dUqSYTdhp7Jmo7zpMqeuqQL+SPn4=; b=PHurc7G94F3oeuiR3J2VekfBP3Cuad26L/ghVbi4KjoY58PdFl2sdTNH1o7bXM54Rs G1c/oWcAe6KRFo+Q1aqbqXKj7Ssge4Z4VXrYfckEbR1W7sS0pA2wX2wxHZMmLd9tnxTs Ikv9iWNfVsW0wThNqi20HTahmgZZ3EkneONvdCBEHJAVFx09OGOXYjR4U9qJF3H0KACz wt4nRh1QVduzUKVQWBO6aknwxnVAoRuuip9OXwdJnMVtgSA8hLMGMI3VnXQ2VgrOTLjR S6CAaXVEo1/CnlDnWd+TBYDbK7jXqgx8FomZATAB+5o41llZRpqZtbfwx8/5AIu3a0ju nIOA== X-Gm-Message-State: ALKqPwc561Riu2e5IfcC3uaOcdaVzBM6u+WVkiYbO40OXNZ2ht2YUix2 NB6kSBseF31rTMQDtHGVSE2J9oxBPvQ= X-Received: by 2002:a17:902:5952:: with SMTP id e18-v6mr8057143plj.351.1526267760360; Sun, 13 May 2018 20:16:00 -0700 (PDT) Received: from joelaf.mtv.corp.google.com ([2620:0:1000:1600:3122:ea9c:d178:eb]) by smtp.gmail.com with ESMTPSA id u14-v6sm19265635pfa.101.2018.05.13.20.15.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 13 May 2018 20:15:59 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , "Paul E. McKenney" , Josh Triplett , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , byungchul.park@lge.com, kernel-team@android.com Subject: [PATCH RFC 1/8] rcu: Add comment documenting how rcu_seq_snap works Date: Sun, 13 May 2018 20:15:34 -0700 Message-Id: <20180514031541.67247-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog In-Reply-To: <20180514031541.67247-1-joel@joelfernandes.org> References: <20180514031541.67247-1-joel@joelfernandes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org rcu_seq_snap may be tricky for someone looking at it for the first time. Lets document how it works with an example to make it easier. Signed-off-by: Joel Fernandes (Google) --- kernel/rcu/rcu.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h index 003671825d62..fc3170914ac7 100644 --- a/kernel/rcu/rcu.h +++ b/kernel/rcu/rcu.h @@ -91,7 +91,29 @@ 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. */ +/* + * Take a snapshot of the update side's sequence number. + * + * This function predicts what the grace period number will be the next + * time an RCU callback will be executed, given the current grace period's + * number. This can be gp+1 if RCU is idle, or gp+2 if a grace period is + * already in progress. + * + * We do this with a single addition and masking. + * For example, if RCU_SEQ_STATE_MASK=1 and the least significant bit (LSB) of + * the seq is used to track if a GP is in progress or not, its sufficient if we + * add (2+1) and mask with ~1. Let's see why with an example: + * + * Say the current seq is 6 which is 0b110 (gp is 3 and state bit is 0). + * To get the next GP number, we have to at least add 0b10 to this (0x1 << 1) + * to account for the state bit. However, if the current seq is 7 (gp is 3 and + * state bit is 1), then it means the current grace period is already in + * progress so the next time the callback will run is at the end of grace + * period number gp+2. To account for the extra +1, we just overflow the LSB by + * adding another 0x1 and masking with ~0x1. In case no GP was in progress (RCU + * is idle), then the addition of the extra 0x1 and masking will have no + * effect. This is calculated as below. + */ static inline unsigned long rcu_seq_snap(unsigned long *sp) { unsigned long s; -- 2.17.0.441.gb46fe60e1d-goog