Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1583916imm; Fri, 11 May 2018 19:40:35 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrGViMvlBMT7DVqm1FskAPlXiyDWNcKt/CEDJorPcbQ5aQEFjrEQMThC19vfLY8cRUroBju X-Received: by 2002:a63:7a44:: with SMTP id j4-v6mr1291295pgn.172.1526092835502; Fri, 11 May 2018 19:40:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526092835; cv=none; d=google.com; s=arc-20160816; b=1FjHrRRDJF9/n/oPIyA9US+/Y1Cz+oil2pfvRa5xz2Uu2bLyb+7mY0GoSXTm7LYJFe luADOCuVkfu4oSdAT4GUN76g6s65+eM1waDvex+CC/c+RTSTThcXXtkFzfP9k8roH9Ha 8f5gH9xPB/FvlS3/DGyGzopNYeQWX0oaIiDNiq6A7MGKR0nNhfQs1qqLllr1FRkgK3uQ 5Q1L4OfqUgsUG/YN70yDdkWR5IcoS73K2PEtxZ4lnJVN+OXosCn4NjFLmndHhyMTyJBI 7MVA1I+uRQETUahs/oaIUAXa00izah7axuyNO3SPOexX2oKhcSypSP4Lesj4PFdO+SK4 Vccw== 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=/G92HSQtw5qVlMyGkLI6JNdr68jXy4aP3jQQLE6SvyM=; b=C7mVdP84kkL3iiAzTu2uzeT8hAzpGWF9VN2rrWcFdXHNFmN423okQV9pORSIGMc+Pt IX6NE2sSSHMz/L23ty6DMzmmvyAe0cdSJ9FqogCdX7YggYTd69W7Hce16moSvpRfk2ww c9VVral+wnLgWcuS1l6VrWlk1dAqNX21grnQ4wnpDu8GEuLabqDXHwjtdhK5KG91pAcD zcnvu2DqjAjNP15WnAREvGGddJc3FEFEQkJl9oWTwEMeNErO5ARzWgjF7XBpKlSDzZRj RH5XM3lZHacxh+NsgZ+XWA+ed29NAy4Rl6VQVEcqRy8wjeh6lGPjYuTmVZkvLIGywCAC TaTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=obR/KjM8; 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 34-v6si4416847plm.495.2018.05.11.19.40.20; Fri, 11 May 2018 19:40:35 -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=bombadil.20170209 header.b=obR/KjM8; 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 S1751533AbeELCi3 (ORCPT + 99 others); Fri, 11 May 2018 22:38:29 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:49062 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751297AbeELCi2 (ORCPT ); Fri, 11 May 2018 22:38:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.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=/G92HSQtw5qVlMyGkLI6JNdr68jXy4aP3jQQLE6SvyM=; b=obR/KjM8fRpf8GxrURAkOSE3Y LVNeYSUyGPCGvqtET/tppp3oESAYVVje+hs+NyuPooQbCYr/EySMwu3cpnxvLdT6a56ZA/dWgitkU MsYs/jL0OgrcHlh3bVyERM60Yw2/clPMrWWbFCmq7M1ycDA1ImYc+nNUmnAVTC8XzIGfKwsXLFf8H LGJKLUVh+ibIWIgSKvsG0+6aJs71jHXcNEduwx7rkWhp9la7rcc18y5HvoN4NUndedTfEQ/hd+Fou GD0/5D5B5bGOGdxbNLzC7lgsr9rpXjZvYBp7KthctKhLHuEGYAOyY2sk1XtkHONMC8HgwCAowrluF VP7U+m5Rg==; Received: from [96.71.163.243] (helo=vortex.dunlab) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fHKQT-0000qZ-Gv; Sat, 12 May 2018 02:38:25 +0000 Subject: Re: [PATCH v2] rcu: Add comment documenting how rcu_seq_snap works To: "Joel Fernandes (Google)" , linux-kernel@vger.kernel.org Cc: paulmck@linux.vnet.ibm.com References: <20180512022032.GA194946@joelaf.mtv.corp.google.com> From: Randy Dunlap Message-ID: <5d6bfed3-2552-8ff5-5ad9-4d002740e08a@infradead.org> Date: Fri, 11 May 2018 19:38:16 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180512022032.GA194946@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/11/2018 07:20 PM, Joel Fernandes (Google) wrote: > 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 it's > + * add (2+1) and mask with ~1. Lets see why with an example: Let's I.e., Let's not be so casual with (dropping) apostrophes. But v3 can wait for other comments. :) > + * > + * 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; > -- ~Randy