* [PATCH] kernel: Emphasize the return value of READ_ONCE() is honored
@ 2017-09-03 0:43 Boqun Feng
0 siblings, 0 replies; only message in thread
From: Boqun Feng @ 2017-09-03 0:43 UTC (permalink / raw)
To: linux-kernel
Cc: Boqun Feng, Akira Yokosawa, Paul E . McKenney, Christopher Li,
linux-sparse
READ_ONCE() is used around in kernel to provide a control dependency,
and to make the control dependency valid, we must 1) make the load of
READ_ONCE() actually happen and 2) make sure compilers take the return
value of READ_ONCE() serious. 1) is already done and commented,
and in current implementation, 2) is also considered done in the
same way as 1): a 'volatile' load.
Whereas, during a recent discussion brought up by Akira Yokosawa on
memory-barriers.txt:
https://marc.info/?l=linux-kernel&m=150052964519882&w=2
, a problem is discovered, which would be triggered if 2) is not
achieved. Moreover, according to Paul Mckenney, using volatile might not
actually give us what we want for 2) depending on compiler writers'
definition of 'volatile'. Therefore it's necessary to emphasize 2) as a
part of the semantics of READ_ONCE(), this not only fits the conceptual
semantics we have been using, but also makes the implementation
requirement more accurate.
In the future, we can either make compiler writers accept our use of
'volatile', or(if that fails) find another way to provide this
guarantee.
Cc: Akira Yokosawa <akiyks@gmail.com>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
include/linux/compiler.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index eca8ad75e28b..b386dbf8c65c 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -305,6 +305,32 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
* mutilate accesses that either do not require ordering or that interact
* with an explicit memory barrier or atomic instruction that provides the
* required ordering.
+ *
+ * The return value of READ_ONCE() should be honored by compilers, IOW,
+ * compilers must treat the return value of READ_ONCE() as an unknown value at
+ * compile time, i.e. no optimization should be done based on the value of a
+ * READ_ONCE(). For example, the following code snippet:
+ *
+ * int a = 0;
+ * int x = 0;
+ *
+ * void some_func() {
+ * int t = READ_ONCE(a);
+ * if (!t)
+ * WRITE_ONCE(x, 1);
+ * }
+ *
+ * , should never be optimized as:
+ *
+ * void some_func() {
+ * int t = READ_ONCE(a);
+ * WRITE_ONCE(x, 1);
+ * }
+ *
+ * because the compiler is 'smart' enough to think the value of 'a' is never
+ * changed.
+ *
+ * We provide this guarantee by making READ_ONCE() a *volatile* load.
*/
#define __READ_ONCE(x, check) \
--
2.14.1
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2017-09-03 0:43 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-03 0:43 [PATCH] kernel: Emphasize the return value of READ_ONCE() is honored Boqun Feng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).