Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp7276902imm; Sun, 20 May 2018 23:19:45 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrzMKJ93KN8OTSR3ai29B6tYjGIHvUcLHH23w6wCgX4dD2egjP5zOFR6Yow3KzcRBvY8V0z X-Received: by 2002:a62:b509:: with SMTP id y9-v6mr18365940pfe.121.1526883585033; Sun, 20 May 2018 23:19:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526883585; cv=none; d=google.com; s=arc-20160816; b=YusUxcWJ+DlgqCFTmRbokD1ymmH8VMpk3oWEaqVg/iZeksca6DgHmGmD95HRZkfhcZ WkN1TR7ISN9HvaWf6oDZQzoCtjuD1pzNyeSVMnRqzKlm/3UDyC3+injTDcYu+KvNoORd yLEM/Egl2/AIG2qt6wIkqna+D7awCJAVgkFUjvFZYt7nghPPfzTQqQklaZWLqZ8a/RxZ BBj7rRjsfyoCpeJVlPC+wF53Arqm5DBeNVjW2okhcjDev1tht2E6O3imWLSPGBJ+kPIV l9jjqLEFo50msIlEax3uqFBBV7e6q/xzFV4+hp7vgAZ62MtHmqtGI6H6VJ0+UvN18xKX F60A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=zLt9s5fO4M6nTv7ob2PHDg0jqGSNqDrNeW+F69BFIXs=; b=OlvYq1VgLcEtafbUdGSakpOEcfrTl1Js/rNNWrPmUoshOYAHXPnXaAApX7XCYy2BnT 0jFOhZgFQoCB7btOU8mjyIBLQpq0RfGp5h75KOJbpWszVcjzofKf+UXTCtJd5s9RCUvE s0MQzzuOfRpy/EM5Pjk16ChY3pODrnoZ6s2Lbb5cFlOdg3MNNJEFFK2KBgTVnKeQKU17 uNAAxwW0jFU5yfXuNy1RLasGdrJ5sveYqckojfPwh2bQwavKWrnV9I4C99TABQx1ENLd NBa/+A2NzTyXeHCw/0LpxgXxJynHhKFH5s/oXH9anrpoXDQbn0o0xCMJvMLBi4bmHsSj Fkag== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=FexCqLIc; 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 n4-v6si10646510pgn.691.2018.05.20.23.19.30; Sun, 20 May 2018 23:19:44 -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=fail header.i=@infradead.org header.s=merlin.20170209 header.b=FexCqLIc; 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 S1751055AbeEUGSz (ORCPT + 99 others); Mon, 21 May 2018 02:18:55 -0400 Received: from merlin.infradead.org ([205.233.59.134]:50760 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750907AbeEUGSw (ORCPT ); Mon, 21 May 2018 02:18:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=zLt9s5fO4M6nTv7ob2PHDg0jqGSNqDrNeW+F69BFIXs=; b=FexCqLIcu2ls0R+A2e2tTJS8gB xGoYU2ugfpn8o46uA0+cb6TH+6TAt9lIJ73Z0pWLRrAIItzR3zvkZR3BsWLqaJi/PZxKF64BAuP+T T96tzwt3kOBEDjazMClb1up+ZIHYBNgh7t/eMPUUKalLbZrWTG2xxV0YIZYK3+vfpOfHLoE6PkyVA kvyQ80N1wLvsxMexr4v/sKCr4LDqW0Y4d6EcinL7N6ugEkSWp6DX7CobxIFAlcTlkbzotUmqEIbH+ xeVwhq38VkbTOFl+vU+krukCO1qp3iSISVjA6WzK+mxnW5stPUYAM7g9RzPhjuddKDNwamrjBVmFg O1mRAaiA==; Received: from static-50-53-52-16.bvtn.or.frontiernet.net ([50.53.52.16] helo=dragon.dunlab) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fKe9H-0000Kv-Fj; Mon, 21 May 2018 06:18:34 +0000 Subject: Re: [PATCH v3 1/4] rcu: Add comment documenting how rcu_seq_snap works To: Joel Fernandes 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 References: <20180521044220.123933-1-joel@joelfernandes.org> <20180521044220.123933-2-joel@joelfernandes.org> <20180521054826.GA137980@joelaf.mtv.corp.google.com> From: Randy Dunlap Message-ID: <2802db99-bfbd-d73c-711c-13077fce5842@infradead.org> Date: Sun, 20 May 2018 23:18:20 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180521054826.GA137980@joelaf.mtv.corp.google.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/20/2018 10:48 PM, Joel Fernandes wrote: > ----------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 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. 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; > -- ~Randy