Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp67015rwj; Thu, 22 Dec 2022 20:54:14 -0800 (PST) X-Google-Smtp-Source: AMrXdXu99C/Q4c5Z0VLfDRk21D+9WuIHqqaVP+eYvRBbcWmsFVoexQjbxUOLerJW67XQ0amkFyyh X-Received: by 2002:a17:902:ff02:b0:186:a5a7:cc4e with SMTP id f2-20020a170902ff0200b00186a5a7cc4emr9330289plj.17.1671771254150; Thu, 22 Dec 2022 20:54:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671771254; cv=none; d=google.com; s=arc-20160816; b=nRk8FzaJc0V70dtZwUsrncnOEcfMgUg0fCxWF4JhW2eGFxhGFEyDf3ZUDDF0njpJDW l525Hb3D0UVtOppjhnge3nY+SNEIjXKyD9lTOi6/ibhGSVvDpG52YeseiXiVpRQPPM/0 wEzooowaL5zXFCjg0njfcj62kxQgqtsVeSFfMcb0jBh0uDg9G0Q95nDeu2j2yMZbX+qW DDDS6W8gI1bOguGQQ5r5OPzS1kehUqebZgiQGfWSfQnex8tL6YS38qFhuwZTOEvw9owo tFhMZaF3rCspfXURjTApgOAdEJS5AbyvECsUmBpIKF5EKqTZHab/AJvqLpr5cqap9+D1 zaUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=k6ADJ2h5z8TSdQPkqKlezNJ1k2pSsiVVtLJbj/jfKTM=; b=bQPkaiqIb4W+mgXIjTA2Bfp9SOK+pJLUfpw12ih3Ck0W7k93ILKnNlU0a502ELLEsr T742YZrLzKbMkzyzbHidmhsqedecYh7pKjP91KNuM/i/Z+n9OtwLLTMbQwzcZej3v3oR 0uCyifUdwFTk780yOh2T4riNWbzC84FWtwixADUwGBEOq1KVxKTA+/1F96X8vmAMSFO3 ugPGA6wfTPbfHxMXaieMviQvHao5ITMdt7BJ6DUSQgKSenlVTHjJsPjJ1hzaUAECSDoJ UHxhlt8MO4OGCEbaj6aFVYn3aYqqsz3Wlgw0uXLLC6LWFoWeNWR/hqV327TOnFA2q8+F 3tgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=ebxINr9j; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d21-20020a170902b71500b00176c9015627si2297324pls.94.2022.12.22.20.54.05; Thu, 22 Dec 2022 20:54:14 -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=@joelfernandes.org header.s=google header.b=ebxINr9j; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229874AbiLWEnz (ORCPT + 67 others); Thu, 22 Dec 2022 23:43:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229549AbiLWEnx (ORCPT ); Thu, 22 Dec 2022 23:43:53 -0500 Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6F661DDC4 for ; Thu, 22 Dec 2022 20:43:51 -0800 (PST) Received: by mail-lj1-x236.google.com with SMTP id f16so3900620ljc.8 for ; Thu, 22 Dec 2022 20:43:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=k6ADJ2h5z8TSdQPkqKlezNJ1k2pSsiVVtLJbj/jfKTM=; b=ebxINr9jSAupNaYYR420TFcbxcHc/q2a9Kwh0oaBPVfOF2DpYKCqGKL6CyHrQikVAY lHEkPYNWtZifgMqztz2qcsuU2uJXO7GYUrOhFt81v7VzZLyso2KEDkwNdlzi5khkbhCN 9/8UF5m1DCtcxPbexNA3PlNyvxoU7hWPAiVgg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=k6ADJ2h5z8TSdQPkqKlezNJ1k2pSsiVVtLJbj/jfKTM=; b=w2z1xt1CieAc7U22dgWjZVvf3DkXBcBuRwlSIpYq24IJK7tDlwyniZ7/kYq8ne3pwv G64xqJ+mco1Ujy9CfXIjn0GBaEXycQhXcneSFgL4A8+lVMrB+jrTNxrnOGLc5a27U05K L1+L9wTDs+Bo49tAqC517Y9AWXRaZRW4T9fab7ASAdlkqTx2g7DqFo627f3n3CWH6PsD bGkd75l82DQ+g+7VPHtl7Dph/vp8wGvd7thk/jB+r6ojR9lv/2dK+gVtGOwTnZgHIoFo 0IdyjwXg7V7CqLHh/rB2emQaRjgtvLQ7gZzPRyWitaNGiBwpSaShaN1P7/E1fLE4bSbJ qlJQ== X-Gm-Message-State: AFqh2kpZhIC3QOqIZezje1tUj8m7i4epage3TmeJP+syAG55UDDza/iy 6DpfZmdXf9SrzEgHEFkqFHiMseDluGJisI0Qb/+zsCXnMBxs74El X-Received: by 2002:a2e:908b:0:b0:279:1349:b2e2 with SMTP id l11-20020a2e908b000000b002791349b2e2mr401513ljg.382.1671770630034; Thu, 22 Dec 2022 20:43:50 -0800 (PST) MIME-Version: 1.0 References: <20221222185314.GR4001@paulmck-ThinkPad-P17-Gen-1> <20221222194511.GS4001@paulmck-ThinkPad-P17-Gen-1> In-Reply-To: <20221222194511.GS4001@paulmck-ThinkPad-P17-Gen-1> From: Joel Fernandes Date: Thu, 22 Dec 2022 23:43:38 -0500 Message-ID: Subject: Re: [RFC 0/2] srcu: Remove pre-flip memory barrier To: paulmck@kernel.org Cc: Frederic Weisbecker , Mathieu Desnoyers , linux-kernel@vger.kernel.org, Josh Triplett , Lai Jiangshan , rcu@vger.kernel.org, Steven Rostedt Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 2:45 PM Paul E. McKenney wrote= : > > On Thu, Dec 22, 2022 at 01:56:17PM -0500, Joel Fernandes wrote: > > > > > > > On Dec 22, 2022, at 1:53 PM, Paul E. McKenney wr= ote: > > > > > > =EF=BB=BFOn Thu, Dec 22, 2022 at 01:19:06PM -0500, Joel Fernandes wro= te: > > >> > > >> > > >>>> On Dec 22, 2022, at 11:43 AM, Paul E. McKenney wrote: > > >>> > > >>> =EF=BB=BFOn Thu, Dec 22, 2022 at 01:40:10PM +0100, Frederic Weisbec= ker wrote: > > >>>>> On Wed, Dec 21, 2022 at 12:11:42PM -0500, Mathieu Desnoyers wrote= : > > >>>>> On 2022-12-21 06:59, Frederic Weisbecker wrote: > > >>>>>>> On Tue, Dec 20, 2022 at 10:34:19PM -0500, Mathieu Desnoyers wro= te: > > >>>>> [...] > > >>>>>>> > > >>>>>>> The memory ordering constraint I am concerned about here is: > > >>>>>>> > > >>>>>>> * [...] In addition, > > >>>>>>> * each CPU having an SRCU read-side critical section that exten= ds beyond > > >>>>>>> * the return from synchronize_srcu() is guaranteed to have exec= uted a > > >>>>>>> * full memory barrier after the beginning of synchronize_srcu()= and before > > >>>>>>> * the beginning of that SRCU read-side critical section. [...] > > >>>>>>> > > >>>>>>> So if we have a SRCU read-side critical section that begins aft= er the beginning > > >>>>>>> of synchronize_srcu, but before its first memory barrier, it wo= uld miss the > > >>>>>>> guarantee that the full memory barrier is issued before the beg= inning of that > > >>>>>>> SRCU read-side critical section. IOW, that memory barrier needs= to be at the > > >>>>>>> very beginning of the grace period. > > >>>>>> > > >>>>>> I'm confused, what's wrong with this ? > > >>>>>> > > >>>>>> UPDATER READER > > >>>>>> ------- ------ > > >>>>>> STORE X =3D 1 STORE srcu_read_lock++ > > >>>>>> // rcu_seq_snap() smp_mb() > > >>>>>> smp_mb() READ X > > >>>>>> // scans > > >>>>>> READ srcu_read_lock > > >>>>> > > >>>>> What you refer to here is only memory ordering of the store to X = and load > > >>>>> from X wrt loading/increment of srcu_read_lock, which is internal= to the > > >>>>> srcu implementation. If we really want to model the provided high= -level > > >>>>> memory ordering guarantees, we should consider a scenario where S= RCU is used > > >>>>> for its memory ordering properties to synchronize other variables= . > > >>>>> > > >>>>> I'm concerned about the following Dekker scenario, where synchron= ize_srcu() > > >>>>> and srcu_read_lock/unlock would be used instead of memory barrier= s: > > >>>>> > > >>>>> Initial state: X =3D 0, Y =3D 0 > > >>>>> > > >>>>> Thread A Thread B > > >>>>> --------------------------------------------- > > >>>>> STORE X =3D 1 STORE Y =3D 1 > > >>>>> synchronize_srcu() > > >>>>> srcu_read_lock() > > >>>>> r1 =3D LOAD X > > >>>>> srcu_read_unlock() > > >>>>> r0 =3D LOAD Y > > >>>>> > > >>>>> BUG_ON(!r0 && !r1) > > >>>>> > > >>>>> So in the synchronize_srcu implementation, there appears to be tw= o > > >>>>> major scenarios: either srcu_gp_start_if_needed starts a gp or ex= pedited gp, > > >>>>> or it uses an already started gp/expedited gp. When snapshotting = with > > >>>>> rcu_seq_snap, the fact that the memory barrier is after the ssp->= srcu_gp_seq > > >>>>> load means that it does not order prior memory accesses before th= at load. > > >>>>> This sequence value is then used to identify which gp_seq to wait= for when > > >>>>> piggy-backing on another already-started gp. I worry about reorde= ring > > >>>>> between STORE X =3D 1 and load of ssp->srcu_gp_seq, which is then= used to > > >>>>> piggy-back on an already-started gp. > > >>>>> > > >>>>> I suspect that the implicit barrier in srcu_read_lock() invoked a= t the > > >>>>> beginning of srcu_gp_start_if_needed() is really the barrier that= makes > > >>>>> all this behave as expected. But without documentation it's rathe= r hard to > > >>>>> follow. > > >>>> > > >>>> Oh ok I see now. It might be working that way by accident or on fo= rgotten > > >>>> purpose. In any case, we really want to add a comment above that > > >>>> __srcu_read_lock_nmisafe() call. > > >>> > > >>> Another test for the safety (or not) of removing either D or E is > > >>> to move that WRITE_ONCE() to follow (or, respectively, precede) the > > >>> adjacent scans. > > >> > > >> Good idea, though I believe the MBs that the above talk about are no= t the flip ones. They are the ones in synchronize_srcu() beginning and end,= that order with respect to grace period start and end. > > >> > > >> So that (flipping MBs) is unrelated, or did I miss something? > > > > > > The thought is to manually similate in the source code the maximum > > > memory-reference reordering that a maximally hostile compiler and CPU > > > would be permitted to carry out. So yes, given that there are other > > > memory barriers before and after, these other memory barriers limit h= ow > > > far the flip may be moved in the source code. > > > > > > Here I am talking about the memory barriers associated with the flip, > > > but the same trick can of course be applied to other memory barriers. > > > In general, remove a given memory barrier and (in the source code) > > > maximally rearrange the memory references that were previously ordere= d > > > by the memory barrier in question. > > > > > > Again, the presence of other memory barriers will limit the permitted > > > maximal source-code rearrangement. > > > > > > Makes sense if the memory barrier is explicit. In this case, the memory= barriers are implicit apparently, with a srcu_read_lock() in the beginning= of synchronize_rcu() having the implicit / indirect memory barrier. So I a= m not sure if that can be implemented without breaking SRCU readers. > > First, are we talking about the same barrier? I am talking about E. No, in the last part you replied to above, Mathieu and Frederic were talking about the need for memory barriers in synchronize_srcu(). That has nothing to do with E: I suspect that the implicit barrier in srcu_read_lock() invoked at the beginning of srcu_gp_start_if_needed() is really the barrier that makes all this behave as expected. We need to order code prior to synchronize_srcu() wrt the start of the grace period, so that readers that started after the grace period started see those side-effects as they may not be waited on (they are too late). > Alternatively, remove E and hammer it on a weakly ordered system. But E is useless, see below patch link with litmus test [1]. I don't see what hammering it on a weakly ordered system buys if it does not even execute for the cases when it is supposed to help because of causality with the control dependency of the prior scan :-/ Thanks, -Joel [1] https://lore.kernel.org/rcu/19A03A16-9F38-481F-824D-4C883AB8B105@joelfe= rnandes.org/T/#m60e37d1277a8dd626bb95f92083e4e602c429704