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@xxxxxxxxx> > Date: Tue, 31 Oct 2017 11:12:03 +0800 > Subject: [PATCH] memorder: Add one solution for one snippet > > Signed-off-by: Yubin Ruan <ablacktshirt@xxxxxxxxx> > --- > 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@xxxxxxxxxxxxxxxxxx> 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@xxxxxxxxx> Signed-off-by: Paul E. McKenney <paulmck@xxxxxxxxxxxxxxxxxx> 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 -- To unsubscribe from this list: send the line "unsubscribe perfbook" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html