Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp7253940imm; Sun, 20 May 2018 22:48:53 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoohoHczCldIc2hDo6CYpzQLPI0eUUcJPAP7ATK19+eFWReKqABInvljEaQaaukpHSqt8/3 X-Received: by 2002:a65:6648:: with SMTP id z8-v6mr9473998pgv.397.1526881733313; Sun, 20 May 2018 22:48:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526881733; cv=none; d=google.com; s=arc-20160816; b=qhbv3gZk4PyHSBSyUBiPa9e+aRUziWbnJ/glorZbN+/QQFgOpqoAvVWRDva/TfQ3Hs RKW5ZG7DskUdYXBL1SkKfvUwKWg4++Qfy715RPrr522WBedtVtjI4uqfLDYHR6RmrjHd LA8nb+aP8LkqvDjgU9ygatXCMEB8959MPx7dvNvLbnvLCRkQh90pjddaBwcPN1sG1Vnz gKUznkFHoL0sNNlKZPX+3yP3GOfQM2gIp/koYYsYUCr+zrHwMvHXdRP1RwCfMZM9u1N4 UbQ37wNYeRRJe/98e2gFCBO0L8kaC6JAUwrFWFP/3kmVZaPS4lFonHZq9u0f0zkhctnE kO7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=KiEBFd8JtcV/7wxS0bPL0I4PmRM2yOyOIfzyY/wY0ls=; b=vWpVK+04E15la7gzcLxp+2+x3PEhbrrfGAowTZnG8NpxuaTzVbgNj5ayK3xotYO1WY G1zfhK4r3Fc0A/6kiUX36B7W+tNevN9n+8h7yiP7idilzs501bpvv4WcvBmhnnT5WExy SlqUmsqF4zPh7L6DzRD2V177gm0SRdKI3nB7GZQVfrKKa71Bcl5UFSwhWzuvwV7OHnHH rH0qX36u7gP1KoV5sZYcg3/wlP8jh+W3g9r8CKlOV4av4jVI/Jp/0zYZFXPWXlZzIcEl klI/uSgNn843iEH1IvXl7Q3RX1xiNt5uc+/EXPgCPctwIqKjge+2P75mrCOc0cZyYz0f Goww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=SRWsuywH; 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 p1-v6si13140284pld.218.2018.05.20.22.48.38; Sun, 20 May 2018 22:48:53 -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 header.s=google header.b=SRWsuywH; 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 S1750965AbeEUFs3 (ORCPT + 99 others); Mon, 21 May 2018 01:48:29 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:37800 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750871AbeEUFs2 (ORCPT ); Mon, 21 May 2018 01:48:28 -0400 Received: by mail-pf0-f195.google.com with SMTP id e9-v6so6536273pfi.4 for ; Sun, 20 May 2018 22:48:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=KiEBFd8JtcV/7wxS0bPL0I4PmRM2yOyOIfzyY/wY0ls=; b=SRWsuywHhY1igbZzJeisEO9xU+8OD3s/sHwfNewYv5QVYm0ehIxAT6tc4Rm/o7vFw/ DhejqathQqwKsGJmV6CQ2z0H7VME1uxr0nJlFlL4wNMdRo0J+9Te7Y5Bt9s6IJF5FlTa UJ2CR4foe+puxNp4tX0Rjc8bFpLBfWpmVZwgc= 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:references :mime-version:content-disposition:in-reply-to:user-agent; bh=KiEBFd8JtcV/7wxS0bPL0I4PmRM2yOyOIfzyY/wY0ls=; b=QbTMC47pDMA7NZPrczKU4Hryk9h43RCQOsDF6ewAnZamXJs8Y6dBABKSTDbtsuZwES Ka7x9aTHUHTXSdafYON8V+2RjrMo6koMQubDXlXZJTQuH0GqAxAdnwIITuKCrJaz0Pga u4Vw0o/kYgHTl9rNMXhZPrVv3pjMPP2q+nRlRWxhsgPhp6IaKCy7Ra17ORzv5NX6RtzO zEK2YHokPLLoh5QEzCNqSwvGUpEV9gbKpPPq2bast6S9x7BYkTCQZxnbdCFsjXVpMfDM xzDQL9uyLJAj1/67NZ/9r6HEBaBo67WYRtN78pLkLQd02/a/tQsocCbdXttrVZCTyvuK s/8g== X-Gm-Message-State: ALKqPwfDg4sK9zP2ip05Swcu2dN5Q554KUu1G8o+zcePG8711++rdaBd fMB7uSKODMxYaYyjgbss43aOBA== X-Received: by 2002:a62:3c10:: with SMTP id j16-v6mr18698328pfa.7.1526881707607; Sun, 20 May 2018 22:48:27 -0700 (PDT) Received: from localhost ([2620:0:1000:1600:3122:ea9c:d178:eb]) by smtp.gmail.com with ESMTPSA id 73-v6sm27109834pfo.153.2018.05.20.22.48.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 20 May 2018 22:48:27 -0700 (PDT) Date: Sun, 20 May 2018 22:48:26 -0700 From: Joel Fernandes To: Randy Dunlap Cc: Joel Fernandes , linux-kernel@vger.kernel.org, "Paul E. McKenney" , Josh Triplett , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , byungchul.park@lge.com, kernel-team@android.com Subject: Re: [PATCH v3 1/4] rcu: Add comment documenting how rcu_seq_snap works Message-ID: <20180521054826.GA137980@joelaf.mtv.corp.google.com> References: <20180521044220.123933-1-joel@joelfernandes.org> <20180521044220.123933-2-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 On Sun, May 20, 2018 at 09:50:25PM -0700, Randy Dunlap wrote: > On 05/20/2018 09:42 PM, Joel Fernandes wrote: > > 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) > > --- > > kernel/rcu/rcu.h | 33 ++++++++++++++++++++++++++++++++- > > 1 file changed, 32 insertions(+), 1 deletion(-) > > > > diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h > > index 0453a7d12b3f..d4396c96f614 100644 > > --- a/kernel/rcu/rcu.h > > +++ b/kernel/rcu/rcu.h > > @@ -91,7 +91,38 @@ 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. > > + * > > + * For example, since 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, its sufficient if we > > it's > > > + * 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. Incase the lower bit was set, the overflow > > In case > > > + * 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 incase the > > in case > > > + * overflow didn't occur. This masking is needed because incase RCU was idle > > in case > > > + * (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; > > > > cheers. > -- > ~Randy Thanks Randy. Fixed, updated patch below. Paul, let me know if you want me to send it separately or if you can pick it up from below. Also I realize I need some better automated tools to catch these issues (spelling errors in commit, diffs etc). Probably checkpatch.pl should have such checks for these common things too. ----------8<---------- From 1c1f8ce04bca656a3c07e555048545d4a59e44cf Mon Sep 17 00:00:00 2001 From: Joel Fernandes Date: Sun, 20 May 2018 19:37:18 -0700 Subject: [PATCH v3.5] rcu: Add comment documenting how rcu_seq_snap works 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) --- kernel/rcu/rcu.h | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h index 0453a7d12b3f..00df3da98317 100644 --- a/kernel/rcu/rcu.h +++ b/kernel/rcu/rcu.h @@ -91,7 +91,38 @@ 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. + * + * For example, since 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, its 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