Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3731550rwb; Fri, 16 Dec 2022 21:23:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf6CSX/zggGup5PkF6cds/t+BO+U0m3hBdva40wOMWtst8Ux6NCDScsgdKKc6iRNvnna5ZYB X-Received: by 2002:a17:906:4b04:b0:7c1:2075:199a with SMTP id y4-20020a1709064b0400b007c12075199amr28205295eju.76.1671254593772; Fri, 16 Dec 2022 21:23:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671254593; cv=none; d=google.com; s=arc-20160816; b=pJRFpibJT9wPyMc2truYHS2pE4ft4KZ9jgqoSSBMmDXUTJ9WURBv+HhTw6phTCcNf6 XrabYhZ71XuC4DJdpTb+IEsGAEep2PxwIyZBQTIq34aRCSqgdj95j10cNzlFbouu0t51 wLyU1++xQOTGGsY2TZQ3yR35CwYNLxzYXrfX1Kii8Zwpf+Kw0ZOyO+3jCU+CQM2U9yTc YfXFmH+7UM5A43Fkq3IBzMbXcV4GROXv//7U/XzZQPpD/OggOqWTfiUV+eWLGMjKd/88 b9BngYvv1uy1UT13fIxvUbUwO1OJe/JnaxcsTUk5hM5JIs391w8akScbEwxQSwS6IdV7 xeNA== 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-transfer-encoding :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=MQBAX5QNosRPVgXKRyusmxn3K/4NgubceOjo5lzrY58=; b=XXGkqJpZw7nA+q9zWL5D8+6M5MGkZFDpcf+Zw25KrQOHCfNaJktxq5h+Wim0hWkk2I bsER2LX5mdigIuGGYMgZJkStbMNUI7Owg/Kzjc24lnwVhZNOfhIQZaHzctZCcsqEuZ68 Urg0+0uxLELVFbrKqUOLq85Ax69VsPok/suvIBkMTjxqb78Gdyud1PpFi2HA9R3Ygd4+ 9w0fLBHEPBIZv5Icn9oTnsu+39s7rLo7d0hyTvKw2D551NyWIy/ZXq2/yn8+3wZYzAEZ cVt9G68/gDCqxxVgG1MyBEc4oJC2mfm7iVYCsL+V9D/26sdrc/gOF4NrtCa8jyQfdOtX QF4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TJElZYcw; 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 wz3-20020a170906fe4300b007c151fd4953si4354238ejb.213.2022.12.16.21.22.57; Fri, 16 Dec 2022 21:23:13 -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=TJElZYcw; 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 S229552AbiLQFP3 (ORCPT + 69 others); Sat, 17 Dec 2022 00:15:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229911AbiLQFPX (ORCPT ); Sat, 17 Dec 2022 00:15:23 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5CF06E9E6; Fri, 16 Dec 2022 21:15:21 -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 F0A1C60921; Sat, 17 Dec 2022 05:15:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5474AC433EF; Sat, 17 Dec 2022 05:15:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671254120; bh=radMw15X9qeTFbbSlTf0SfWRvZQR4SUmgAmlVZ0Dkks=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=TJElZYcw3WiLt+sFW2jJLAOm3NFnqwNLphQxnt9FqgBzoWmsSztEcekEiRpJRxFsJ p8jF5bKJUqioud7G4QhfgvmnnpIywDSRh5z1/76AEaiZ2tgeHVyr4ga6wMA/maMmHN llRMDp2zjz1YWijfTk/vUVyOQcnhVJIA/m3MYD82dktDvCbrUWEig2oJdtxtqttRX3 GObFol9HdasJJcALoj5grsaPUnGrFnfrhzIoU/hfk/YcCNo7vOLkmksZSgjUp8ygef D4i6RXQ9pnfw8H8AS6o7mu0e0yrDELGE8On7RKlI/FdtrwnS8ApyVXJBSgH5skWytg zy2nvdJRU0+Cw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id EC0D55C0A2D; Fri, 16 Dec 2022 21:15:19 -0800 (PST) Date: Fri, 16 Dec 2022 21:15:19 -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: <20221217051519.GI4001@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20221216165144.GA4001@paulmck-ThinkPad-P17-Gen-1> <54F1102C-2577-4238-83B3-D38BA7ED9087@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 Fri, Dec 16, 2022 at 10:21:25PM -0500, Joel Fernandes wrote: > On Fri, Dec 16, 2022 at 10:19 PM Joel Fernandes wrote: > > > > Hi, > > On the related subject of this function, I drew a diagram for one of > > the reasons why per-CPU unlock counts have to be scanned first, for a > > particular index, before the per-CPU lock counts, and not the other > > way. Otherwise, a reader that got preempted after reading the index, > > can suddenly get scheduled during the inactive index's scan, and cause > > the total lock and unlock counts to falsely match: > > https://i.imgur.com/79fDWdQ.png > > Better diagram: https://i.imgur.com/PXKJnmW.png > (Added the preemption reasoning for Reader 0). Nice!!! The other way to look at this is using a memory-ordering viewpoint. This is a member of the message-passing litmus-test family, and the reader must read the variables in the opposite order that the writer writes them. (See the infamous test6.pdf file, "MP" pattern.) Thanx, Paul > thanks, > > - Joel > > > > Cheers, > > > > - Joel > > > > > > > > On Fri, Dec 16, 2022 at 11:54 AM Joel Fernandes wrote: > > > > > > > > > > > > > On Dec 16, 2022, at 11:51 AM, Paul E. McKenney wrote: > > > > > > > > On Fri, Dec 16, 2022 at 04:32:39PM +0000, Joel Fernandes wrote: > > > >> On Thu, Dec 15, 2022 at 05:09:14PM -0800, Paul E. McKenney wrote: > > > >> [...] > > > >>>>>> 2. unlock()'s smp_mb() happened before Flip+smp_mb() , now the reader > > > >>>>>> has no new smp_mb() that happens AFTER the flip happened. So it can > > > >>>>>> totally sample the old idx again -- that particular reader will > > > >>>>>> increment twice, but the next time, it will see the flipped one. > > > >>>>> > > > >>>>> I will let you transliterate both. ;-) > > > >>>> > > > >>>> I think I see what you mean now :) > > > >>>> > > > >>>> I believe the access I am referring to is the read of idx on one side and > > > >>>> the write to idx on the other. However that is incomplete and I need to > > > >>>> pair that with some of other access on both sides. > > > >>>> > > > >>>> So perhaps this: > > > >>>> > > > >>>> Writer does flip + smp_mb + read unlock counts [1] > > > >>>> > > > >>>> Reader does: > > > >>>> read idx + smp_mb() + increment lock counts [2] > > > >>>> > > > >>>> And subsequently reader does > > > >>>> Smp_mb() + increment unlock count. [3] > > > >>>> > > > >>>> So [1] races with either [2] or [2]+[3]. > > > >>>> > > > >>>> Is that fair? > > > >>> > > > >>> That does look much better, thank you! > > > >> > > > >> Perhaps a comment with an ASCII diagram will help? > > > >> > > > >> > > > >> Case 2: > > > >> Both the reader and the updater see each other's writes too late, but because > > > >> of memory barriers on both sides, they will eventually see each other's write > > > >> with respect to their own. This is similar to the store-buffer problem. This > > > >> let's a single reader contribute a maximum (unlock minus lock) imbalance of 2. > > > >> > > > >> The following diagram shows the subtle worst case followed by a simplified > > > >> store-buffer explanation. > > > >> > > > >> READER UPDATER > > > >> ------------- ---------- > > > >> // idx is initially 0. > > > >> read_lock() { > > > >> READ(idx) = 0; > > > >> lock[0]++; --------------------------------------------, > > > >> flip() { | > > > >> smp_mb(); | > > > >> smp_mb(); | > > > >> } | > > > >> | > > > >> // RSCS | > > > >> | > > > >> read_unlock() { | > > > >> smp_mb(); | > > > >> idx++; // P | > > > >> smp_mb(); | > > > >> } | > > > >> | > > > >> scan_readers_idx(0) { | > > > >> count all unlock[0]; | > > > >> | | > > > >> | | > > > >> unlock[0]++; //X <--not-counted--`-----, | > > > >> | | > > > >> } V `------, > > > >> // Will make sure next scan | > > > >> // will not miss this unlock (X) | > > > >> // if other side saw flip (P) ,--` > > > >> // Call this MB [1] | > > > >> // Order write(idx) with | > > > >> // next scan's unlock. | > > > >> smp_mb(); ,---` > > > >> read_lock() { | > > > >> READ(idx)=0; | > > > >> lock[0]++; ----------------> count all lock[0]; | > > > >> smp_mb(); | } | > > > >> } | | V > > > >> | `---> // Incorrect contribution to lock counting > > > >> | // upto a maximum of 2 times. > > > >> | > > > >> `---> // Pairs with MB [1]. Makes sure that > > > >> // the next read_lock()'s' idx read (Y) is ordered > > > >> // with above write to unlock[0] (X). > > > >> | > > > >> rcu_read_unlock() { | > > > >> smp_mb(); <---------------` > > > >> unlock[0]++; > > > >> } > > > >> > > > >> read_lock() { > > > >> READ(idx) = 1; //Y > > > >> lock[1]++; > > > >> ... > > > >> } > > > >> scan_readers_idx(0) { > > > >> count all unlock[0]; //Q > > > >> ... > > > >> > > > >> > > > >> thanks, > > > >> > > > >> - Joel > > > >> > > > >> } > > > >> > > > >> This makes it similar to the store buffer pattern. Using X, Y, P and Q > > > >> annotated above, we get: > > > >> > > > >> READER UPDATER > > > >> X (write) P (write) > > > >> > > > >> smp_mb(); smp_mb(); > > > >> > > > >> Y (read) Q (read) > > > > > > > > Given that this diagram is more than 50 lines long, it might go better in > > > > a design document describing this part of RCU. Perhaps less detail or > > > > segmented, but the same general idea as this guy: > > > > > > > > Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst > > > > > > Yes, this sounds like a good place to add it and perhaps we refer to it from the C source file? I can take this up to do over the holidays, if you prefer. > > > > > > Thanks, > > > > > > - Joel > > > > > > > > > > > > > > Thoughts? > > > > > > > > Thanx, Paul