public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Peter Zijlstra <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: torvalds@linux-foundation.org, linux-kernel@vger.kernel.org,
	tglx@linutronix.de, peterz@infradead.org,
	paulmck@linux.vnet.ibm.com, will.deacon@arm.com, hpa@zytor.com,
	dvyukov@google.com, mingo@kernel.org, akpm@linux-foundation.org
Subject: [tip:locking/core] atomic: Implement atomic_read_ctrl()
Date: Wed, 23 Sep 2015 01:48:38 -0700	[thread overview]
Message-ID: <tip-e3e72ab80a3fac0b88e07d358a2c75724ccd66b4@git.kernel.org> (raw)
In-Reply-To: <20150918115637.GM3604@twins.programming.kicks-ass.net>

Commit-ID:  e3e72ab80a3fac0b88e07d358a2c75724ccd66b4
Gitweb:     http://git.kernel.org/tip/e3e72ab80a3fac0b88e07d358a2c75724ccd66b4
Author:     Peter Zijlstra <peterz@infradead.org>
AuthorDate: Fri, 18 Sep 2015 13:22:52 +0200
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Wed, 23 Sep 2015 09:54:29 +0200

atomic: Implement atomic_read_ctrl()

Provide atomic_read_ctrl() to mirror READ_ONCE_CTRL(), such that we can
more conveniently use atomics in control dependencies.

Since we can assume atomic_read() implies a READ_ONCE(), we must only
emit an extra smp_read_barrier_depends() in order to upgrade to
READ_ONCE_CTRL() semantics.

Requested-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Will Deacon <will.deacon@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Cc: oleg@redhat.com
Link: http://lkml.kernel.org/r/20150918115637.GM3604@twins.programming.kicks-ass.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 Documentation/memory-barriers.txt | 17 +++++++++--------
 include/asm-generic/atomic-long.h |  3 ++-
 include/linux/atomic.h            | 18 ++++++++++++++++++
 3 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/Documentation/memory-barriers.txt b/Documentation/memory-barriers.txt
index 2ba8461..41ffd7e 100644
--- a/Documentation/memory-barriers.txt
+++ b/Documentation/memory-barriers.txt
@@ -637,7 +637,8 @@ as follows:
 	b = p;  /* BUG: Compiler and CPU can both reorder!!! */
 
 Finally, the READ_ONCE_CTRL() includes an smp_read_barrier_depends()
-that DEC Alpha needs in order to respect control depedencies.
+that DEC Alpha needs in order to respect control depedencies. Alternatively
+use one of atomic{,64}_read_ctrl().
 
 So don't leave out the READ_ONCE_CTRL().
 
@@ -796,9 +797,9 @@ site: https://www.cl.cam.ac.uk/~pes20/ppcmem/index.html.
 
 In summary:
 
-  (*) Control dependencies must be headed by READ_ONCE_CTRL().
-      Or, as a much less preferable alternative, interpose
-      smp_read_barrier_depends() between a READ_ONCE() and the
+  (*) Control dependencies must be headed by READ_ONCE_CTRL(),
+      atomic{,64}_read_ctrl(). Or, as a much less preferable alternative,
+      interpose smp_read_barrier_depends() between a READ_ONCE() and the
       control-dependent write.
 
   (*) Control dependencies can order prior loads against later stores.
@@ -820,10 +821,10 @@ In summary:
       and WRITE_ONCE() can help to preserve the needed conditional.
 
   (*) Control dependencies require that the compiler avoid reordering the
-      dependency into nonexistence.  Careful use of READ_ONCE_CTRL()
-      or smp_read_barrier_depends() can help to preserve your control
-      dependency.  Please see the Compiler Barrier section for more
-      information.
+      dependency into nonexistence.  Careful use of READ_ONCE_CTRL(),
+      atomic{,64}_read_ctrl() or smp_read_barrier_depends() can help to
+      preserve your control dependency.  Please see the Compiler Barrier
+      section for more information.
 
   (*) Control dependencies pair normally with other types of barriers.
 
diff --git a/include/asm-generic/atomic-long.h b/include/asm-generic/atomic-long.h
index c7a5c1a..8942cdc 100644
--- a/include/asm-generic/atomic-long.h
+++ b/include/asm-generic/atomic-long.h
@@ -35,7 +35,7 @@ typedef atomic_t atomic_long_t;
 #endif
 
 #define ATOMIC_LONG_READ_OP(mo)						\
-static inline long atomic_long_read##mo(atomic_long_t *l)		\
+static inline long atomic_long_read##mo(const atomic_long_t *l)		\
 {									\
 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
 									\
@@ -43,6 +43,7 @@ static inline long atomic_long_read##mo(atomic_long_t *l)		\
 }
 ATOMIC_LONG_READ_OP()
 ATOMIC_LONG_READ_OP(_acquire)
+ATOMIC_LONG_READ_OP(_ctrl)
 
 #undef ATOMIC_LONG_READ_OP
 
diff --git a/include/linux/atomic.h b/include/linux/atomic.h
index 29dafa1..e326469 100644
--- a/include/linux/atomic.h
+++ b/include/linux/atomic.h
@@ -4,6 +4,15 @@
 #include <asm/atomic.h>
 #include <asm/barrier.h>
 
+#ifndef atomic_read_ctrl
+static inline int atomic_read_ctrl(const atomic_t *v)
+{
+	int val = atomic_read(v);
+	smp_read_barrier_depends(); /* Enforce control dependency. */
+	return val;
+}
+#endif
+
 /*
  * Relaxed variants of xchg, cmpxchg and some atomic operations.
  *
@@ -455,6 +464,15 @@ static inline int atomic_dec_if_positive(atomic_t *v)
 #include <asm-generic/atomic64.h>
 #endif
 
+#ifndef atomic64_read_ctrl
+static inline long long atomic64_read_ctrl(const atomic64_t *v)
+{
+	long long val = atomic64_read(v);
+	smp_read_barrier_depends(); /* Enforce control dependency. */
+	return val;
+}
+#endif
+
 #ifndef atomic64_andnot
 static inline void atomic64_andnot(long long i, atomic64_t *v)
 {

  parent reply	other threads:[~2015-09-23  8:49 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17 13:24 [PATCH] kernel: fix data race in put_pid Dmitry Vyukov
2015-09-17 16:08 ` Oleg Nesterov
2015-09-17 16:41   ` Dmitry Vyukov
2015-09-17 17:44     ` Oleg Nesterov
2015-09-17 17:57       ` Dmitry Vyukov
2015-09-17 17:59         ` Dmitry Vyukov
2015-09-17 18:09         ` Oleg Nesterov
2015-09-17 18:38           ` Dmitry Vyukov
2015-09-18  8:51           ` Peter Zijlstra
2015-09-18  8:57             ` Peter Zijlstra
2015-09-18  9:27               ` Peter Zijlstra
2015-09-18 12:31                 ` James Hogan
2015-09-18 12:34                   ` Peter Zijlstra
2015-09-18 15:56               ` Paul E. McKenney
2015-09-18  9:06             ` Dmitry Vyukov
2015-09-18  9:28               ` Will Deacon
2015-09-18  9:33                 ` Peter Zijlstra
2015-09-18 11:22                 ` Peter Zijlstra
2015-09-18 11:30                   ` Will Deacon
2015-09-18 11:50                   ` Dmitry Vyukov
2015-09-18 11:56                     ` Peter Zijlstra
2015-09-18 12:19                       ` Peter Zijlstra
2015-09-18 12:44                         ` Will Deacon
2015-09-18 13:10                           ` Peter Zijlstra
2015-09-18 13:44                       ` Oleg Nesterov
2015-09-18 13:49                         ` Peter Zijlstra
2015-09-18 13:53                         ` Dmitry Vyukov
2015-09-18 14:41                           ` Oleg Nesterov
2015-09-22  8:38                       ` Dmitry Vyukov
2015-09-23  8:48                       ` tip-bot for Peter Zijlstra [this message]
2015-09-18 16:15                   ` Eric Dumazet
2015-09-18 16:20                     ` Peter Zijlstra
2015-09-18 15:57                 ` Paul E. McKenney
2015-09-18 13:28             ` Oleg Nesterov
2015-09-18 13:31               ` Oleg Nesterov
2015-09-18 13:46               ` Peter Zijlstra
2015-09-18 15:00                 ` Oleg Nesterov
2015-09-18 15:30                 ` Oleg Nesterov
2015-09-18 16:00                 ` Paul E. McKenney
2015-09-17 17:56     ` 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=tip-e3e72ab80a3fac0b88e07d358a2c75724ccd66b4@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=akpm@linux-foundation.org \
    --cc=dvyukov@google.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=will.deacon@arm.com \
    /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