Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1596559rwb; Thu, 15 Dec 2022 12:06:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf7XjyW/fXTjQ0x0zvDEgkk9GiwQgU6cdnwbEwIXfjJ0aJxni88S9NS4swlzG8uZLZaAFmFk X-Received: by 2002:a05:6402:5505:b0:45c:835b:8fb5 with SMTP id fi5-20020a056402550500b0045c835b8fb5mr25540326edb.32.1671134771115; Thu, 15 Dec 2022 12:06:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671134771; cv=none; d=google.com; s=arc-20160816; b=pQvCjtmldAGT1BtoDCzlciidTTc1af2TlCVzKCK+6FFEG5NrET7HxkPM+6kVxDsGFh Gev76oLd0wCkjkBdeLCRGBvg5brjlmpwQQ8iDVVImDMkTWiOZ38VZLLI2FZ+VML9l7yF Dn537uB3M6Fvu4CoJ5AQb4rqHTZrQEW9wGFJb99H9iSNb/619y/6d91c0/rQFvCsgVPM 7vhACsEw+/cBGK7pgjm4ZyMecqneJsc2lnITPLrmHNfHJwFWOysxMrCKylk2E8ZGi8Rl BxcKL5EtNquzsyyEryOI/Fw5WRKyqCXvSRJbW9ffKcsqzukx8a4SSlt6BwqFMZvMb00f T4bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=vddf4rKq3NfZepOSwtTE72DtG6TZq81ieEe+LeHq57U=; b=otSJs/5ve57Z96Y5OGFVhR7+10Xhps6GlhWVFfTHHBu++7Flr1foLdYGQ60nTz1z8d WnC29JvIx0VyDmHdggrBxYgT1zIK0O7HtCK72AcpXyYiBzTGM8NA8/a+2fKR+6aiMRiy FzX18JYBklZ4yjjV/iEXBKRkVGaNjOKhZbjC51ioyn/dIPt4drbepqIBqdHAi3dytzBR f0BR1dzvcogweI/KW8U1aVqzNdslJNHbL/1isVqUVVNa4BVydeGjqXsqG+V2mjwvhU7l Xmwa6SvlybJR2sawW2xAmByzRAF8tiBg78Kh3m8zkLU1j4XgN3Clz3ewXqdpCi7PlnLM fbuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nbVOWxpp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j17-20020a05640211d100b0046751bddcf0si279152edw.425.2022.12.15.12.05.54; Thu, 15 Dec 2022 12:06:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nbVOWxpp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229786AbiLOT7A (ORCPT + 69 others); Thu, 15 Dec 2022 14:59:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229543AbiLOT65 (ORCPT ); Thu, 15 Dec 2022 14:58:57 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68CB22ED51; Thu, 15 Dec 2022 11:58:56 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 058CA61EFE; Thu, 15 Dec 2022 19:58:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54873C433EF; Thu, 15 Dec 2022 19:58:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671134335; bh=Zx9ma7/5lFMJQesW6o8ux/+Vin72c34PJvibgWse7nA=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=nbVOWxpp4fPBvH9uzkyllCc0lUc7Om6MHWaMdiKkYiUFWHDH9dE/iVOYftqxUg4R1 3Of7Oj+OvDa1lZD6vDiC72JK6ME4Xl8KkBMKZsRS1UxpMA1WpkXCKG62eoVdFaEjD5 Uh5FmonE446lolDNLwXmkiJThLXYL+CIsdzpdlnEwjGc7UR8r50KqwGS0ZlTlE+97g XlOHr/EesM+bvb+Sdc7jPQSEhH0O06ykO/NrTADrQQWau0DkcLQqZiZQse3d5hTVlL JWISKFYZXIcKJyADnLh5TfLtz5NBcuwU35QK4wehyFkynkARmt5G9o2PFu6cL4sW2R qmK0Gpt10dd4Q== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id E33AB5C09D0; Thu, 15 Dec 2022 11:58:54 -0800 (PST) Date: Thu, 15 Dec 2022 11:58:54 -0800 From: "Paul E. McKenney" To: Joel Fernandes Cc: Frederic Weisbecker , boqun.feng@gmail.com, neeraj.iitr10@gmail.com, urezki@gmail.com, rcu@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH RFC] srcu: Yet more detail for srcu_readers_active_idx_check() comments Message-ID: <20221215195854.GL4001@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20221214191355.GA2596199@paulmck-ThinkPad-P17-Gen-1> <20221215165452.GA1957735@lothringen> <20221215170834.GH4001@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 15, 2022 at 05:48:46PM +0000, Joel Fernandes wrote: > On Thu, Dec 15, 2022 at 5:08 PM Paul E. McKenney wrote: > > > > Scenario for the reader to increment the old idx once: > > > > > > _ Assume ssp->srcu_idx is initially 0. > > > _ The READER reads idx that is 0 > > > _ The updater runs and flips the idx that is now 1 > > > _ The reader resumes with 0 as an index but on the next srcu_read_lock() > > > it will see the new idx which is 1 > > > > > > What could be the scenario for it to increment the old idx twice? > > > > Unless I am missing something, the reader must reference the > > srcu_unlock_count[old_idx] and then do smp_mb() before it will be > > absolutely guaranteed of seeing the new value of ->srcu_idx. > > I think both of you are right depending on how the flip raced with the > first reader's unlock in that specific task. There are indeed a variety of scenarios and also a variety of failure cases. > If the first read section's srcu_read_unlock() and its corresponding > smp_mb() happened before the flip, then the increment of old idx > would happen only once. The next srcu_read_lock() will read the new > index. If the srcu_read_unlock() and it's corresponding smp_mb() > happened after the flip, the old_idx will be sampled again and can be > incremented twice. So it depends on how the flip races with > srcu_read_unlock(). I do understand that a number of people like reasoning about memory-barrier ordering, courtesy of the sequentially consistent portions of the C and C++ memory models, but thinking in terms of the accesses surrounding the memory barriers has been far less error-prone. > Also, since this is all hard to reason about I started making some > diagrams, LOL. For your amusement, here is why need to scan both idx > during grace period detection: https://i.imgur.com/jz4bNKd.png Nice! I suggest placing a gap between GP 2 and GP 3. That way, you can make it very clear that Reader 1's critical section starts after the end of GP 2 (thus clearly never blocking GP 2) and before GP 3 (thus possibly having a reference to some data that is going to be freed at the end of GP 3). I also suggest coloring Reader 1 red and Reader 2 green, given that the color red generally indicates danger. Thanx, Paul