Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp2183427rwo; Thu, 3 Aug 2023 06:04:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlFh9ekMyDjZGkjtNbYJQ7LuPnuJz2SCSqR/hIPROz5G5u9GRGkusTvuMbiSOuvsehnPwkE0 X-Received: by 2002:a05:6a20:3c8c:b0:13f:68fd:6ae8 with SMTP id b12-20020a056a203c8c00b0013f68fd6ae8mr2198827pzj.57.1691067877779; Thu, 03 Aug 2023 06:04:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691067877; cv=none; d=google.com; s=arc-20160816; b=E87hXrOCiDlwnvRY8jRijS3VuZfhHLzFqTndOvzvP1LeQmYaMFfVMgqZGPu8IcxX9u 8S8S2RoxzIk/DWes1hCqB3aABsI5aIhS12NaT8a5D83VyEq7w1SN+RvBjq8RSJA+vWs7 Z7Nj5MMrR0YotEEQRfryXbLFzGrUxfGfYr/1WBXB2LW8RfPIvA4JtVckUNdL7kLgYy5Z PCMuHtFLhAM3dfHgfRNDo8bkkBy/d9lEyq0ZJajg073hAzwGBwMxCHWzV80c0fQA3gYB UGhuy8NKKaSv/jbhaFWDxD1Rvkx7IfnDu4I11zr3dJcpG3vaBrddhj9aw9ygWCva5JUj fyUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=Xx2oQNP8dRig7LZXLqCiP6SuqwiYqFZRo7LyhqCpqHE=; fh=UxoAL61hAni+p/YMaQ4/Bjuop9kPwNI+dHVoNZEQoFk=; b=ItIXjBI8eztqdZ7KaLQMOY9KYTaqBM5jJFiR+tRbOCKItQ/RIqJC0248omiVw6gQ4N IBEgS/OMgDKhqhiYZTgGFaO5QkxS9k1eFBQ9aj2pGvHi82+F80eQ/u7SQB6+ynjBOCXl Dxlth78Zqb3ibx+aYBAUgNcw7jHUfGiY8ap4BmBJZYIKDGysXbEYhsc/etvCiyrLfHtx 5l3kPmF7OtMH8A2X9U6CAf8YidPDHf3quSKRCCoZSDcAVPZCmycppNlfijXkumIVpYxs 47Z7Cm54qNsMuwr/dDHBGVVBgs1dvHqbFlkaFZ6ebJR71tfE3wk0aWT4f1Pvcf3irDfg 2ccw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=IWonB6QU; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i7-20020a655b87000000b0055c0477e26dsi5413018pgr.475.2023.08.03.06.04.15; Thu, 03 Aug 2023 06:04:37 -0700 (PDT) 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=@gmail.com header.s=20221208 header.b=IWonB6QU; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235050AbjHCMJH (ORCPT + 99 others); Thu, 3 Aug 2023 08:09:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234546AbjHCMJF (ORCPT ); Thu, 3 Aug 2023 08:09:05 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76B142D5F; Thu, 3 Aug 2023 05:09:04 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-686f090310dso803433b3a.0; Thu, 03 Aug 2023 05:09:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691064544; x=1691669344; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Xx2oQNP8dRig7LZXLqCiP6SuqwiYqFZRo7LyhqCpqHE=; b=IWonB6QUJ4N8aOKnWPyxE8cS5xjIgORpLfILdZrQsbCOO+rypbS3BZuhJ5KnTCcCFw EKXkMK4V/+m0CuWpQlg7DK+azW1pksjVKIuso4yhfG0CBhGGFsGtAgFeqQbJaL1T57P4 1eSFYN823Wr3GtpwgzB+R+U/ian13/p8aL0tkVOwnDr+lNmCDXcz7UxxirryGzDC0WQt Wz3JvSPqCxOyV9i8zgawCugOmAlBAO9fQ8pinx8H2W4WCVhVcqvET5JPaLI0ttTn9W15 DDatRD3T2sqadjeIhlY/QycfDE5FlnIbMic4TJ342EP/TC4kb0AfxZF8W++k0AKpcYLu 04/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691064544; x=1691669344; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xx2oQNP8dRig7LZXLqCiP6SuqwiYqFZRo7LyhqCpqHE=; b=hmrRD+9GjOFHqs0d8Hv0stjO9TyA7/QOsxFfYCpcXe4jDskezdZ2rhap4qmTFjeB// vZl+bN/Zwucf8Kcn9+oC0CU4pCn3BIpgS7nHhT+tBP7aAhqkdIx9tA/M/nvih4tvGMqr R+fN8lDT24BsP1hWxFiChdhXSTB0Ddk56Wij7z0k2ZC0qjn0jGyK3p4FByxh6EnJL3BH 08usM8FwGj2vVGStijPFPz84S6JkLU2JwuqPdr7ohAdm5HK2gTGX0UO5Ow6M/EswitAR YhkybyC/APmSxhDWUe5Tw8AsdNqxpqlIcWRofTwZykYbea8Opl+sa5LCBbGEUmPQospS Wt9A== X-Gm-Message-State: ABy/qLZXa6QUvLVa5VLH2xElQuy6P2jOoVHJpYhIwcMLKMS4UU1WVYvw VvE3yMp/G2HBNcmwZMhefoGTlO6sAZo= X-Received: by 2002:a05:6a00:ad4:b0:681:c372:5aa4 with SMTP id c20-20020a056a000ad400b00681c3725aa4mr23931679pfl.27.1691064543845; Thu, 03 Aug 2023 05:09:03 -0700 (PDT) Received: from smtpclient.apple ([2402:d0c0:2:a2a::1]) by smtp.gmail.com with ESMTPSA id v14-20020a62a50e000000b00681783cfc85sm13083358pfm.144.2023.08.03.05.08.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 03 Aug 2023 05:09:03 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.400.51.1.1\)) Subject: Re: [PATCH 1/2] docs: rcu: Add cautionary note on plain-accesses to requirements From: Alan Huang In-Reply-To: <20230803032408.2514989-1-joel@joelfernandes.org> Date: Thu, 3 Aug 2023 20:08:42 +0800 Cc: linux-kernel@vger.kernel.org, rcu@vger.kernel.org, Will Deacon , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Josh Triplett , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Jonathan Corbet Content-Transfer-Encoding: quoted-printable Message-Id: References: <20230803032408.2514989-1-joel@joelfernandes.org> To: "Joel Fernandes (Google)" X-Mailer: Apple Mail (2.3731.400.51.1.1) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 > 2023=E5=B9=B48=E6=9C=883=E6=97=A5 11:24=EF=BC=8CJoel Fernandes = (Google) =E5=86=99=E9=81=93=EF=BC=9A >=20 > Add a detailed note to explain the potential side effects of > plain-accessing the gp pointer using a plain load, without using the > rcu_dereference() macros; which might trip neighboring code that does > use rcu_dereference(). >=20 > I haven't verified this with a compiler, but this is what I gather = from > the below link using Will's experience with READ_ONCE(). >=20 > Link: = https://lore.kernel.org/all/20230728124412.GA21303@willie-the-truck/ > Cc: Will Deacon > Signed-off-by: Joel Fernandes (Google) > --- > .../RCU/Design/Requirements/Requirements.rst | 32 +++++++++++++++++++ > 1 file changed, 32 insertions(+) >=20 > diff --git a/Documentation/RCU/Design/Requirements/Requirements.rst = b/Documentation/RCU/Design/Requirements/Requirements.rst > index f3b605285a87..e0b896d3fb9b 100644 > --- a/Documentation/RCU/Design/Requirements/Requirements.rst > +++ b/Documentation/RCU/Design/Requirements/Requirements.rst > @@ -376,6 +376,38 @@ mechanism, most commonly locking or reference = counting > .. |high-quality implementation of C11 memory_order_consume [PDF]| = replace:: high-quality implementation of C11 ``memory_order_consume`` = [PDF] > .. _high-quality implementation of C11 memory_order_consume [PDF]: = http://www.rdrop.com/users/paulmck/RCU/consume.2015.07.13a.pdf >=20 > +Note that, there can be strange side effects (due to compiler = optimizations) if > +``gp`` is ever accessed using a plain load (i.e. without = ``READ_ONCE()`` or > +``rcu_dereference()``) potentially hurting any succeeding > +``rcu_dereference()``. For example, consider the code: > + > + :: > + > + 1 bool do_something_gp(void) > + 2 { > + 3 void *tmp; > + 4 rcu_read_lock(); > + 5 tmp =3D gp; // Plain-load of GP. > + 6 printk("Point gp =3D %p\n", tmp); > + 7 > + 8 p =3D rcu_dereference(gp); > + 9 if (p) { > + 10 do_something(p->a, p->b); > + 11 rcu_read_unlock(); > + 12 return true; > + 13 } > + 14 rcu_read_unlock(); > + 15 return false; > + 16 } > + > +The behavior of plain accesses involved in a data race is = non-deterministic in > +the face of compiler optimizations. Since accesses to the ``gp`` = pointer is > +by-design a data race, the compiler could trip this code by caching = the value > +of ``gp`` into a register in line 5, and then using the value of the = register > +to satisfy the load in line 10. Thus it is important to never mix Will=E2=80=99s example is: // Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently foo =3D *ptr; bar =3D READ_ONCE(*ptr); baz =3D *ptr; Then the compiler is within its right to reorder it to: foo =3D *ptr; baz =3D *ptr; bar =3D READ_ONCE(*ptr); So, the result foo =3D=3D baz =3D=3D 0 but bar =3D=3D 1 is perfectly = legal. But the example here is different, the compiler can not use the value = loaded from line 5 unless the compiler can deduce that the tmp is equals to p in which case = the address dependency doesn=E2=80=99t exist anymore. What am I missing here? > +plain accesses of a memory location with rcu_dereference() of the = same memory > +location, in code involved in a data race. > + > In short, updaters use rcu_assign_pointer() and readers use > rcu_dereference(), and these two RCU API elements work together to > ensure that readers have a consistent view of newly added data = elements. > --=20 > 2.41.0.585.gd2178a4bd4-goog >=20