Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp83613rwj; Thu, 22 Dec 2022 05:19:15 -0800 (PST) X-Google-Smtp-Source: AMrXdXttzjVYBLj9jQe+JrYZTEtVunKjIp1c+Ssn7ZWNpPk6j0EUIBBXySoVzbt/ompVkoJzv7KK X-Received: by 2002:a17:906:7188:b0:7c0:f117:6990 with SMTP id h8-20020a170906718800b007c0f1176990mr4647680ejk.41.1671715155070; Thu, 22 Dec 2022 05:19:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671715155; cv=none; d=google.com; s=arc-20160816; b=MJOrPVLMQ+/ZXInCfqF7aqDqEMmTOmFukcliQjYQysn+rDY1kOiHrqqLzKCXnmPNF+ 3FxJZ4pqBO7E2zVnQH+dDGjRbSz6GbpSE5bL0T6DyBRZauahxsGmVioqOB07iF1M9Z7l 5x2OI08tCWq3URA8MenjZ1fv4R152e/dXSdCllMfPeVVMrOOpmB2dhCP8fAKhGGYpDFS Ii+H814JdBHUxmu2e5Gz9xN8H4YBCyUUgMb+DUBxVMPD+2ttoZzWHFh9Pc9l9Dj4aSHo TJOedZrUt8PQrZ7SinPTJrgkrR5TS85vFb0d6Hrtx2QeHKRyhkG9YCyPrBuZAa0SJ6iB NOZQ== 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:message-id:subject:cc:to:from:date:dkim-signature; bh=Ihap36OtYRdyJIZ9vFqRJzvn0FF8T3Uf+BMigi0PoOc=; b=asIzkp7TtIVotHigVcCkSEXaG0uT63Plq3ZAPOk6JLE/Ki+uqKsSaOOcJKz/voA5Or nduuvYXq8+FYuTt4SiO4Gsi3eL/+u/ZkkKMA0O6c7GsMfNRBuSUKrVcnYNJ4qGh3j6cJ hN1mTvsdJ77xsRzB1qmLtgHW88nKv2YCQ6KoiAfed26JCLbmITbdNco478T8XbmAOoEq RZXXgsi+GIVOScVaPZsknYtO1ixr4VwuNE+kgQMpJVDg3kdcqAf+lw23xXsIZc3WuRoB SC0/LYwMteJBt54wplsTEWhBy+rn641uBGjlJRLdIOiTEmwBsZ+yIHYPvkvxJCSiUC1a V0VA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BxxeCHxA; 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 nb10-20020a1709071c8a00b007c773e7d8dfsi485682ejc.203.2022.12.22.05.18.59; Thu, 22 Dec 2022 05:19:15 -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=BxxeCHxA; 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 S235529AbiLVMML (ORCPT + 67 others); Thu, 22 Dec 2022 07:12:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235777AbiLVMLe (ORCPT ); Thu, 22 Dec 2022 07:11:34 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5196B36D42; Thu, 22 Dec 2022 04:03:25 -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 sin.source.kernel.org (Postfix) with ESMTPS id 4F7F3CE19BB; Thu, 22 Dec 2022 12:03:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F10E6C433D2; Thu, 22 Dec 2022 12:03:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671710602; bh=juXlLY89CiB9bJ9o17px1v8bs7xEFLxq9o/YkeYNwvg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=BxxeCHxAMVIThDcrxhuGCdBUO2YtMPtf5b0Acf6+0I8BGI7do+Tclh9jQgSSqEgyW dVgpCG6SSH5CzjlfPTZ7kylDtjvA4WMECdQEXSopfTE+O/nu+PatRbvNyGMsUI/3hE 0UnYbgAKzG9G8wa9MwxAwyJS2kF9mKd8CXyX+lH4eOkGZ2Vzu6qzVa+RB4YwbUm41g MLK4+xh3SK/W4B67xN/gcHzrmWMvgwnOgbe91MuVcpg4Fr4uhMfiJ42JQw2OUFRL9p MO557ODWyoovr88Y/RnW2NKOd7CcYLkjQ7xb28qpFwsVVA2WiPwtYxB3iYilDcs/YR BX12fqUGDv8dQ== Date: Thu, 22 Dec 2022 13:03:19 +0100 From: Frederic Weisbecker To: "Joel Fernandes (Google)" Cc: linux-kernel@vger.kernel.org, Mathieu Desnoyers , Boqun Feng , Josh Triplett , Lai Jiangshan , "Paul E. McKenney" , rcu@vger.kernel.org, Steven Rostedt , neeraj.iitr10@gmail.com Subject: Re: [PATCH] srcu: Remove memory barrier "E" as it does not do anything Message-ID: <20221222120319.GA44533@lothringen> References: <20221222002011.1858494-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221222002011.1858494-1-joel@joelfernandes.org> 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 22, 2022 at 12:20:11AM +0000, Joel Fernandes (Google) wrote: > During a flip, we have a full memory barrier before srcu_idx is incremented. > > The idea is we intend to order the first phase scan's read of lock > counters with the flipping of the index. > > However, such ordering is already enforced because of the > control-dependency between the 2 scans. We would be flipping the index > only if lock and unlock counts matched. > > But such match will not happen if there was a pending reader before the flip > in the first place (observation courtesy Mathieu Desnoyers). > > The litmus test below shows this: > (test courtesy Frederic Weisbecker, Changes for ctrldep by Boqun/me): > > C srcu > > {} > > // updater > P0(int *IDX, int *LOCK0, int *UNLOCK0, int *LOCK1, int *UNLOCK1) > { > int lock1; > int unlock1; > int lock0; > int unlock0; > > // SCAN1 > unlock1 = READ_ONCE(*UNLOCK1); > smp_mb(); // A > lock1 = READ_ONCE(*LOCK1); > > // FLIP > if (lock1 == unlock1) { // Control dep > smp_mb(); // E > WRITE_ONCE(*IDX, 1); > smp_mb(); // D > > // SCAN2 > unlock0 = READ_ONCE(*UNLOCK0); > smp_mb(); // A > lock0 = READ_ONCE(*LOCK0); > } > } > > // reader > P1(int *IDX, int *LOCK0, int *UNLOCK0, int *LOCK1, int *UNLOCK1) > { > int tmp; > int idx1; > int idx2; > > // 1st reader > idx1 = READ_ONCE(*IDX); > if (idx1 == 0) { > tmp = READ_ONCE(*LOCK0); > WRITE_ONCE(*LOCK0, tmp + 1); > smp_mb(); /* B and C */ > tmp = READ_ONCE(*UNLOCK0); > WRITE_ONCE(*UNLOCK0, tmp + 1); > } else { > tmp = READ_ONCE(*LOCK1); > WRITE_ONCE(*LOCK1, tmp + 1); > smp_mb(); /* B and C */ > tmp = READ_ONCE(*UNLOCK1); > WRITE_ONCE(*UNLOCK1, tmp + 1); > } > > // second reader > idx2 = READ_ONCE(*IDX); > if (idx2 == 0) { > tmp = READ_ONCE(*LOCK0); > WRITE_ONCE(*LOCK0, tmp + 1); > smp_mb(); /* B and C */ > tmp = READ_ONCE(*UNLOCK0); > WRITE_ONCE(*UNLOCK0, tmp + 1); > } else { > tmp = READ_ONCE(*LOCK1); > WRITE_ONCE(*LOCK1, tmp + 1); > smp_mb(); /* B and C */ > tmp = READ_ONCE(*UNLOCK1); > WRITE_ONCE(*UNLOCK1, tmp + 1); > } > } > > The following bad condition will not occur even if memory barrier E > is dropped: > > (* bad condition: SCAN1 saw lock count changes though 1st reader saw flip *) > exists (0:lock1=1 /\ 1:idx1=1 /\ 1:idx2=1) Good catch! The litmus test can be shorten with removing the second reader and limit the exists clause to 0:lock1=1 : --- C srcu-E {} // updater P0(int *IDX, int *LOCK0, int *UNLOCK0, int *LOCK1, int *UNLOCK1) { int lock1; int unlock1; int lock0; int unlock0; // SCAN1 unlock1 = READ_ONCE(*UNLOCK1); smp_mb(); // A lock1 = READ_ONCE(*LOCK1); // FLIP if (lock1 == unlock1) { // Control dep WRITE_ONCE(*IDX, 1); smp_mb(); // D // SCAN2 unlock0 = READ_ONCE(*UNLOCK0); smp_mb(); // A lock0 = READ_ONCE(*LOCK0); } } // reader P1(int *IDX, int *LOCK0, int *UNLOCK0, int *LOCK1, int *UNLOCK1) { int tmp; int idx1; int idx2; // 1st reader idx1 = READ_ONCE(*IDX); if (idx1 == 0) { tmp = READ_ONCE(*LOCK0); WRITE_ONCE(*LOCK0, tmp + 1); smp_mb(); /* B and C */ tmp = READ_ONCE(*UNLOCK0); WRITE_ONCE(*UNLOCK0, tmp + 1); } else { tmp = READ_ONCE(*LOCK1); WRITE_ONCE(*LOCK1, tmp + 1); smp_mb(); /* B and C */ tmp = READ_ONCE(*UNLOCK1); WRITE_ONCE(*UNLOCK1, tmp + 1); } } exists (0:lock1=1) (* only happens if the control dep is removed *) -- > diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c > index 1c304fec89c0..d1368d64fdba 100644 > --- a/kernel/rcu/srcutree.c > +++ b/kernel/rcu/srcutree.c > @@ -983,15 +983,9 @@ static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount) > static void srcu_flip(struct srcu_struct *ssp) > { > /* > - * Ensure that if this updater saw a given reader's increment > - * from __srcu_read_lock(), that reader was using an old value > - * of ->srcu_idx. Also ensure that if a given reader sees the > - * new value of ->srcu_idx, this updater's earlier scans cannot > - * have seen that reader's increments (which is OK, because this > - * grace period need not wait on that reader). > + * Control dependency locks==unlocks ensures that if a reader sees the > + * flip, previous scan would only see before-flip reader lock counts. I would personally prefer that we keep the detailed comment explaining the ordering requirements here, just mentioning it's performed via control dependency now. This way we don't need to debate that again in 10 years. Thanks. > */ > - smp_mb(); /* E */ /* Pairs with B and C. */ > - > WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); > > /* > -- > 2.39.0.314.g84b9a713c41-goog >