From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Yubin Ruan <ablacktshirt@gmail.com>
Cc: Akira Yokosawa <akiyks@gmail.com>, perfbook@vger.kernel.org
Subject: Re: [PATCH] memorder: Add one solution for one snippet
Date: Mon, 30 Oct 2017 20:50:09 -0700 [thread overview]
Message-ID: <20171031035009.GG3659@linux.vnet.ibm.com> (raw)
In-Reply-To: <70204d71-e19e-4bb6-8064-984550c38456@gmail.com>
On Tue, Oct 31, 2017 at 11:14:47AM +0800, Yubin Ruan wrote:
> >From 256adc5ccd239288c3f38cd193072d6666ab1e82 Mon Sep 17 00:00:00 2001
> From: Yubin Ruan <ablacktshirt@gmail.com>
> Date: Tue, 31 Oct 2017 11:12:03 +0800
> Subject: [PATCH] memorder: Add one solution for one snippet
>
> Signed-off-by: Yubin Ruan <ablacktshirt@gmail.com>
> ---
> memorder/memorder.tex | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/memorder/memorder.tex b/memorder/memorder.tex
> index f8886b4..a1c96df 100644
> --- a/memorder/memorder.tex
> +++ b/memorder/memorder.tex
> @@ -3055,6 +3055,8 @@ surprise to \co{do_something()}.\footnote{
> Tracking down the bug consumed a holiday weekend not just
> for your editor, but also for several of his colleagues.
> In short, this is not a new problem.}
> +In this case, \co{tmp} should be declared as \co{volatile} to prevent
> +the transformation by the compiler.
>
> Compilers can also fuse stores.
> The most infamous example is probably the progress-bar example
Declaring tmp as volatile could in some sense solve the problem, but at
the expense of preventing the compiler from caching tmp in a register.
It is better to use READ_ONCE() on the initial read from p, or failing
that, to declare p (not tmp) volatile.
Thanx, Paul
------------------------------------------------------------------------
commit d1f08db32120f079da699c9deebde22af96a202a
Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Date: Mon Oct 30 20:44:33 2017 -0700
memorder: Show how to use READ_ONCE() to prevent load replication
Reported-by: Yubin Ruan <ablacktshirt@gmail.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
diff --git a/memorder/memorder.tex b/memorder/memorder.tex
index f8886b42468c..a3b8f7c86946 100644
--- a/memorder/memorder.tex
+++ b/memorder/memorder.tex
@@ -3055,6 +3055,21 @@ surprise to \co{do_something()}.\footnote{
Tracking down the bug consumed a holiday weekend not just
for your editor, but also for several of his colleagues.
In short, this is not a new problem.}
+To prevent the compiler from replicating the load, use \co{READ_ONCE()},
+for example as follows:
+
+\vspace{5pt}
+\begin{minipage}[t]{\columnwidth}
+\scriptsize
+\begin{verbatim}
+ 1 tmp = READ_ONCE(p);
+ 2 if (tmp != NULL && tmp <= q)
+ 3 do_something(tmp);
+\end{verbatim}
+\end{minipage}
+\vspace{5pt}
+
+Alternatively, the variable \co{p} could be declared \co{volatile}.
Compilers can also fuse stores.
The most infamous example is probably the progress-bar example
next prev parent reply other threads:[~2017-10-31 3:50 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-16 11:01 Is WRITE_ONCE() enough to prevent invention of stores? Akira Yokosawa
2017-09-17 1:07 ` Paul E. McKenney
2017-09-17 11:04 ` Akira Yokosawa
2017-09-17 21:55 ` Paul E. McKenney
2017-09-17 22:51 ` Akira Yokosawa
2017-10-30 18:14 ` Paul E. McKenney
2017-10-31 3:03 ` Yubin Ruan
2017-10-31 3:14 ` [PATCH] memorder: Add one solution for one snippet Yubin Ruan
2017-10-31 3:50 ` Paul E. McKenney [this message]
2017-10-31 5:04 ` Yubin Ruan
2017-10-31 3:45 ` Is WRITE_ONCE() enough to prevent invention of stores? Paul E. McKenney
2017-10-31 15:36 ` Akira Yokosawa
2017-10-31 16:27 ` Paul E. McKenney
2017-10-31 22:25 ` Akira Yokosawa
2017-11-01 20:15 ` 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=20171031035009.GG3659@linux.vnet.ibm.com \
--to=paulmck@linux.vnet.ibm.com \
--cc=ablacktshirt@gmail.com \
--cc=akiyks@gmail.com \
--cc=perfbook@vger.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