Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3054689rwb; Fri, 16 Dec 2022 09:31:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf4BajxgiwZYqzVSUi1ziQI/Qxs7GlPmeyduUF4wlevoFbfg1V/8g2wUv56z9iJREhgZzvmS X-Received: by 2002:a05:6a20:12cc:b0:9d:efbf:8151 with SMTP id v12-20020a056a2012cc00b0009defbf8151mr47729385pzg.26.1671211866349; Fri, 16 Dec 2022 09:31:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671211866; cv=none; d=google.com; s=arc-20160816; b=CY9ET7dGOnr4oW4s516hq/1Cy/fMt7aJjXR769jAnDKzdK2lAkLO4QHb6nv1VsxQen nR8kaUYD/BIGdOrJItFF6VUveTfv+jwMFQN6omNPNe5q99LN56R2DiuBIJZq0EdbchUW GVwBH9qh1Zl+0yrnvvGRxe/0KyUQFZddI1LDR+HyDkqofxmyzEEUZ7IRH50Xk7tv4nQ7 naxCNuhUGuYFk6+HzPWZoQ7QnlsPgK9FAviPk6q+ARe1nrapol6uVTnBseVMTzPlTkQW WEA62VnjLRnsK8nDNLD6aOQEtppJD0NRM+yL6cABOQi0x0Hx5hCzNmIU24zW4tdeyGjv htWA== 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=040XnRpu7N0iYJG33X2d0YTMFCHmlK7RNK6g/ykA4HQ=; b=iOZQXnV4SuHxitZAUtJRIqCBUPBcF14qdv/FAtQe6hEldOLWYwbJ16o3nZnQqDYOi8 Brjk/0KSlStX4kdHuLWMtI/PddZc++CNFC//105XpRRpwchDKPlbyVMt55xdekBbNdr6 v7Xo5u9sGIHu7k2OEVPfzYaXwyrP0drOx8pO1D4fTYKJDSyGsTanGwowTuG10hpe/ATu Q+iwKRRP2ESyY5mwx8E4cIDR/nvwmqnZTx5L3/lBuytrsaVjB4NOzROtO2dZTAJgOb1W qUeE9oV5/MC49s5SqlSKTbGwOAjJTvUsy2aFdYcgymukhkR8fMj64pMoFBgEFoDKhZ0x YGeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SQKLkqy0; 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 v17-20020a63d551000000b00478a6de4d30si3431319pgi.201.2022.12.16.09.30.56; Fri, 16 Dec 2022 09:31:06 -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=SQKLkqy0; 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 S231235AbiLPQwS (ORCPT + 68 others); Fri, 16 Dec 2022 11:52:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231627AbiLPQvy (ORCPT ); Fri, 16 Dec 2022 11:51:54 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C32C4EE; Fri, 16 Dec 2022 08:51:45 -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 2DCDB6218C; Fri, 16 Dec 2022 16:51:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87019C433D2; Fri, 16 Dec 2022 16:51:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671209504; bh=0hb/MJls16WL36VuUKuWGxKZpEmgVcdJEgDtAMt34GQ=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=SQKLkqy0iGnLoq3dZ7fi5Ctww6L1vl4qFurAYaHlZZtGnA3LQJ5nfYTyHEx8XLhqz z/yu4YXGh/TPrQ5uqt+43Cqsms74PWfIWFibZF8ANtSM1a8aYJPxN5GOxqPulxzS5I 402Rglycu6IMcei7UUqgYjWo57YTzAF8yFwbgi2z9UIjyJrtA6hoc8hu/xYVm9rGiv o/BVybtGgnaUA+oIxILxijRDX9bqQ/m//tTXbAO66G99jHw9Jbc2rU0k5ZJrsZMdBj HH1TP0HZZYjrTImp6QEFDg66RBp1SA5Y7VGvhzu1DrWA1HvpmCsGLqWDLfEYRhOoDa qeQCUrpUdrIAQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 2AD3E5C0AC7; Fri, 16 Dec 2022 08:51:44 -0800 (PST) Date: Fri, 16 Dec 2022 08:51:44 -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: <20221216165144.GA4001@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20221215201356.GM4001@paulmck-ThinkPad-P17-Gen-1> <20221216010914.GX4001@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 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 Thoughts? Thanx, Paul