mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alan Huang <mmpgouride@gmail.com>
To: "Joel Fernandes (Google)" <joel@joelfernandes.org>
Cc: linux-kernel@vger.kernel.org, rcu@vger.kernel.org,
	Will Deacon <will@kernel.org>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Frederic Weisbecker <frederic@kernel.org>,
	Neeraj Upadhyay <quic_neeraju@quicinc.com>,
	Josh Triplett <josh@joshtriplett.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Lai Jiangshan <jiangshanlai@gmail.com>,
	Zqiang <qiang.zhang1211@gmail.com>,
	Jonathan Corbet <corbet@lwn.net>
Subject: Re: [PATCH 1/2] docs: rcu: Add cautionary note on plain-accesses to requirements
Date: Thu, 3 Aug 2023 20:08:42 +0800	[thread overview]
Message-ID: <A272EB21-28F9-4FFE-A3BB-6689807CC3ED@gmail.com> (raw)
In-Reply-To: <20230803032408.2514989-1-joel@joelfernandes.org>


> 2023年8月3日 11:24,Joel Fernandes (Google) <joel@joelfernandes.org> 写道:
> 
> 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().
> 
> 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().
> 
> Link: https://lore.kernel.org/all/20230728124412.GA21303@willie-the-truck/
> Cc: Will Deacon <will@kernel.org>
> Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> ---
> .../RCU/Design/Requirements/Requirements.rst  | 32 +++++++++++++++++++
> 1 file changed, 32 insertions(+)
> 
> 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
> 
> +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 = gp; // Plain-load of GP.
> +       6   printk("Point gp = %p\n", tmp);
> +       7
> +       8   p = 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’s example is:

	 // Assume *ptr is initially 0 and somebody else writes it to 1
	 // concurrently

	 foo = *ptr;
	 bar = READ_ONCE(*ptr);
	 baz = *ptr;

Then the compiler is within its right to reorder it to:

	foo = *ptr;
 	baz = *ptr;
 	bar = READ_ONCE(*ptr);

So, the result foo == baz == 0 but bar == 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’t 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.
> -- 
> 2.41.0.585.gd2178a4bd4-goog
> 


  parent reply	other threads:[~2023-08-03 12:09 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-03  3:24 Joel Fernandes (Google)
2023-08-03  3:24 ` [PATCH 2/2] docs: memory-barriers: Add note on plain-accesses to address-dependency barriers Joel Fernandes (Google)
2023-08-03 18:52   ` Paul E. McKenney
2023-08-04  5:11     ` Joel Fernandes
2023-08-04 13:52       ` Paul E. McKenney
2023-08-04 16:27         ` Joel Fernandes
2023-08-04 18:02           ` Paul E. McKenney
2023-08-03 12:08 ` Alan Huang [this message]
2023-08-03 12:35   ` [PATCH 1/2] docs: rcu: Add cautionary note on plain-accesses to requirements Joel Fernandes
2023-08-03 13:36     ` Alan Huang
2023-08-03 16:01       ` Joel Fernandes
2023-08-03 19:25         ` Alan Huang
2023-08-03 23:05           ` Joel Fernandes
2023-08-04 15:47             ` Alan Huang
2023-08-04 16:15               ` Joel Fernandes
2023-08-04  0:01           ` Paul E. McKenney
2023-08-04 12:33             ` Joel Fernandes
2023-08-04 14:00               ` Paul E. McKenney
2023-08-04 16:17                 ` Joel Fernandes
2023-08-04 17:02                   ` Paul E. McKenney
2023-08-04 16:33             ` Alan Huang
2023-08-04 17:27               ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A272EB21-28F9-4FFE-A3BB-6689807CC3ED@gmail.com \
    --to=mmpgouride@gmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=corbet@lwn.net \
    --cc=frederic@kernel.org \
    --cc=jiangshanlai@gmail.com \
    --cc=joel@joelfernandes.org \
    --cc=josh@joshtriplett.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=paulmck@kernel.org \
    --cc=qiang.zhang1211@gmail.com \
    --cc=quic_neeraju@quicinc.com \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®