Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1481150imm; Fri, 11 May 2018 17:33:48 -0700 (PDT) X-Google-Smtp-Source: AB8JxZorRRWWImhC78fF7ijy6Wj1edRvcofjovv2yR6lAHIJeWa4TkxCAFnueIb4VFwugSchr7W+ X-Received: by 2002:a63:ba04:: with SMTP id k4-v6mr972067pgf.338.1526085228408; Fri, 11 May 2018 17:33:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526085228; cv=none; d=google.com; s=arc-20160816; b=mdI9sni7yJF4H3O8sBaXYworCQ3ZuUVvYfL36mB8Jtk/CuOwjzTOQ+0/fMmnCGIrVd TwtmJEWa7LiLh+ryCWqiva5fTx3WIOe7MDEr/4tF0BTBrbAZ0XGNx/5Q7pHeAKWvo7PX xPW1dW01COjB56/US/WSFzWMTKtSgUQY7zkWa2TVm1un9AdZdZ81UQkdKrj6DDnHxB3+ xs9YRlEWSbcO3iGwI9MEdrYGvB/52EQvjYCQAGS5DczKpE1nTyqC1x6uJszTjsWqhBIH PkPyfRQdpFPUPjeEewb3N8QBoOUdqNYBrVV05n6jWZP0DHEpQehuSErciEXbw+gEqNCw cotw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:to:from:date:dkim-signature :arc-authentication-results; bh=LLX5JeZDvmKvBC+sH7k0tqOqJ2n6L+fcIGMpDc5vul0=; b=eex66n7cfIGV0A9E4xLZ5LoHj82o8r5iCP10SHemIS1li4ILySxK4kJUbESvcvbiu2 8kcrjOSLEZBnfPoe3MBR/5mB0kTApi+7OvkgbfzJc+Bj2dRrsOzfdNo/dGh04/lmS88e bTzHU3x4xuKgZu5UQY+xttdzk8CFh8YL2zFmBX9+nWMjCYTDcMQpAbBnYFjMSUZMy9L5 xYnnznKXQHfBhHEV4MMVQu+wbWbXmDhuOJMVzR4EtbUrJp+3+KcPFgo2ZKmxM8VZZoY0 YlMUVknHMPUeFXmkF7K2a7pVvLo9jq+80lwFe5NXSzELgstkEBGKh30LCPlLS9vU3fCB E/CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes-org.20150623.gappssmtp.com header.s=20150623 header.b=riUD2U3E; 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 e16-v6si4063743pli.476.2018.05.11.17.33.20; Fri, 11 May 2018 17:33:48 -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=riUD2U3E; 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 S1751260AbeELAdJ (ORCPT + 99 others); Fri, 11 May 2018 20:33:09 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:41777 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750776AbeELAdI (ORCPT ); Fri, 11 May 2018 20:33:08 -0400 Received: by mail-pl0-f66.google.com with SMTP id az12-v6so4128089plb.8 for ; Fri, 11 May 2018 17:33:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=LLX5JeZDvmKvBC+sH7k0tqOqJ2n6L+fcIGMpDc5vul0=; b=riUD2U3Ep307TvLSNRlaZ+bM2raTCp1erH1Ls1IfVoXUlVtGDxlI0cb6jsoYXlxdgz 62kjVfrVoaNXjBGTUKqomMESL2kz4Uespo5GFnUpHe+eLkkkyoWpbJ5PLooduk1+fJxv 3ugMQ5zzEsIwrwdO5E9AB9x9jiV9Ywv6/cCupipf8FFxVbNRqfD/KtRCIxtCknq7Ufgq GYjBSfow5g56KscIfhJHVXGItA5YZ31w8FAYQMDpcMdGW/Gv6Y/rznDWjDWBvu01e6iI MOqh96MZlGx3jszyUR8mhWA2d4f1PNB77t0V2HC/F8P1ADQuMhWbaolpqKuAJ+gUcXSZ fGlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=LLX5JeZDvmKvBC+sH7k0tqOqJ2n6L+fcIGMpDc5vul0=; b=OYQOGNShZ+fVpg5rK0L9qo4AIY0evviJ3bGFFDRHcinMxG3xAZhdma+lombD3nooIv +PtVzvNWo5HfgPL5E1lSdBoen8geyPLJ8UMV+zdBC3ctC1WlqNkKtTdOiFmVOql7k1ng +oOecVx4wSPGfKQ2caXt6qypGdpk0Wokd33oYqf38vRpt7zqU10m0S038qwbq5J2jMEo 1pJYTMx4Kawyn1xKQv6JQ/NSR+jltuzfV2v8vD68M58b1szKKgqqnVB8dKGtmhbL6t1G kB5Cb5itxXawD7jtgP6uiKuLpW2me+uwqoOuANEu8AVAjS9Mi62ZvJhE4mijj7Rhel/e iAVA== X-Gm-Message-State: ALKqPwcWTJkKzAuxh+Wp2IYNHhm99harur6ZvPMteZxhADfG6r5/kWEv 9/mXszT4IZsFeD2y+ai/8C5QnoDrYBk= X-Received: by 2002:a17:902:9048:: with SMTP id w8-v6mr285045plz.34.1526085187795; Fri, 11 May 2018 17:33:07 -0700 (PDT) Received: from localhost ([2620:0:1000:1600:3122:ea9c:d178:eb]) by smtp.gmail.com with ESMTPSA id s17-v6sm11101452pfi.165.2018.05.11.17.33.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 11 May 2018 17:33:07 -0700 (PDT) Date: Fri, 11 May 2018 17:33:06 -0700 From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org, paulmck@linux.vnet.ibm.com Subject: [PATCH] rcu: Add comment documenting how rcu_seq_snap works Message-ID: <20180512003306.GA170941@joelaf.mtv.corp.google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.2 (2017-12-15) 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 | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h index 003671825d62..004ace3d22c2 100644 --- a/kernel/rcu/rcu.h +++ b/kernel/rcu/rcu.h @@ -91,7 +91,28 @@ 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. Lets see why with an example: + * + * Say the current seq is 6 which is 0x110 (gp is 3 and state bit is 0). + * To get the next GP number, we have to atleast add 0x10 to this (0x1 << 1) to + * account for the state bit. However, if the current seq is 7 (GP num is 3 + * and state bit is 1), then it means the current grace period is already + * in progress so the next the callback will run is at gp+2. To account for + * the extra +1, we just overflow the LSB by adding another 0x1 and masking + * with ~0x1. Incase no GP was in progress (RCU is idle), then the adding + * by 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