Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1570986imm; Fri, 11 May 2018 19:21:33 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq/rmwwMSR740x4WpojNJySARURsaueVPt0IbMIPqsvj+jSN2hh57IFCKTNxcb5ueWZB6b4 X-Received: by 2002:a63:3706:: with SMTP id e6-v6mr1207587pga.281.1526091693864; Fri, 11 May 2018 19:21:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526091693; cv=none; d=google.com; s=arc-20160816; b=lvn2y2qs9mzWJbbzk0ihi+WCOpuL+QuQHaE8R+wQPowlf0HPwXpZupz4KCcmWneYwG DrLiF4zMSqv19RF6Z/70xCCFbn1D2LvbH127aXN/I5MdNca2k8fneEBC3tvT6oLLExif uiFBU5PTmzRrkSgd4t5a8TuKB3JbZHazTk6oQue/WgoYD738yZzYHMtfd+yywbb+wTn0 LcsNkWjhQLdRxZF8H4nwbBJRQTcdb8g7YP+FnhtFcmmE7z1EpSmHEUzgwVhrySNPvCv5 0amDRIBeTBz8VUm8eWsH+v6myqM707P1PHmxSyXu7ruxM/wRnk4P7iCypa7jnPBPpoGR 2cgw== 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:cc:to:from:date:dkim-signature :arc-authentication-results; bh=uij4LwPQBCEuorMJLRlEcpXkY7ddkfpkyO5JtJcPGY0=; b=ri3dB8wjQo/ijfNShNSCjKD52Vca9q7OHCYWdyKt7N9b+EMVnYT+PomteortDRYRWv 63iKpDSf0GpRPsDp2ynvXG+mFeDtlSefGkIU1RVCKGIGAS8bSxG5pRn7vhzdcluQctQw L8oqSbKJE8modAHe0D/szlQXLUEoA6RYMDydUZGU/eVV5yMKGioq1uEYn1/H+1SLxBst 0lSc2jWoE2lHJkijSurEaO4wjzzYUKA3ki5wVydWr4VbkhbYOetGUgiTHE5MyS1kiG8h Af78Yv3FGMdtlxEBmpkb0VKpAQtdMSkwEj2tITNn3P5ThdfgsbA/OpCWiF4fi+2aAXu+ G2oA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes-org.20150623.gappssmtp.com header.s=20150623 header.b=edWHJHeW; 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 k5-v6si3626907pgo.0.2018.05.11.19.20.44; Fri, 11 May 2018 19:21:33 -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=edWHJHeW; 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 S1751096AbeELCUg (ORCPT + 99 others); Fri, 11 May 2018 22:20:36 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:34683 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750776AbeELCUe (ORCPT ); Fri, 11 May 2018 22:20:34 -0400 Received: by mail-pl0-f66.google.com with SMTP id ay10-v6so4230744plb.1 for ; Fri, 11 May 2018 19:20:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=uij4LwPQBCEuorMJLRlEcpXkY7ddkfpkyO5JtJcPGY0=; b=edWHJHeW2IgzKZlQ5Saj+ZIcqPHkpTRUGTWouG6K+E1LE4d1MksikAZwFi85lpUcdU pI3uunho7JxCMWAmzmJWXFEAHXE1jkWzPesrxMEfY2jtfBnDgZ9xmtfCcgY9wsex7Mrq ty88J8rsQ8zwgX2jLeP+mJPEn1ma3iQz0I/l0GK60Qh+Z2QQk+rFQwN4iaO5lBRLuel+ BbghRco5PWDQzirY+Y/2m8D4ENX44SLXOMJMEWAGFsnNZtiyGJRb81r4SOtbVui0fK4e cD1YSswyDJvOVrNA5l7VKBsywZuaHuz9GoDG0GjJBZiW6SAUN2HE9ag3c1iIm9bSskJB 7czQ== 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:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=uij4LwPQBCEuorMJLRlEcpXkY7ddkfpkyO5JtJcPGY0=; b=pmZcKlVvRNec9oDsxbwnTdJc9cFJbo5GWtgWq9e/w+JLJxmGLkyNKsd8cbgjr0IvZb bGUHKanW4vrsf+nGYJldUu0NHekoDcz6Ow1szCrgoKR/Z5UYMkV/SL93Eob5wHgL/ibs RpF3BODNb1NcmQsDWuzagENRh57oFweFbMa9PA6Fm+PVy2+R/jRbjTksd6Hkhf2XiibX KKuQBFDzTFtFCQLINEhKjq/HZRRT5HJZCyRF9Gya4Tot6UCCPXrDDVwyfTd/S67/rbQR eAX6wV1ieUks+UjXSQWOs6NiC8+JT++UC4P+trNaeBmbp+OraaMgOgoRD9ILDbcAs4bJ 9Gww== X-Gm-Message-State: ALKqPweQSx50EVqammuwz3QsgWynihmUtyf3qmQUF4SzmEKiFHOGH1il aB8kUW1d/pQB419m9gpJ2SzdxHeLwK0= X-Received: by 2002:a17:902:7146:: with SMTP id u6-v6mr547529plm.289.1526091634154; Fri, 11 May 2018 19:20:34 -0700 (PDT) Received: from localhost ([2620:0:1000:1600:3122:ea9c:d178:eb]) by smtp.gmail.com with ESMTPSA id t69-v6sm10696036pfi.177.2018.05.11.19.20.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 11 May 2018 19:20:33 -0700 (PDT) Date: Fri, 11 May 2018 19:20:32 -0700 From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: rdunlap@infradead.org, paulmck@linux.vnet.ibm.com Subject: [PATCH v2] rcu: Add comment documenting how rcu_seq_snap works Message-ID: <20180512022032.GA194946@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) --- v2 changes: Corrections as suggested by Randy. 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..533bc1087371 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. Lets 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