Merge branch 'locking/arch-atomic' into locking/core, because the topic is ready
authorIngo Molnar <mingo@kernel.org>
Thu, 7 Jul 2016 07:12:02 +0000 (09:12 +0200)
committerIngo Molnar <mingo@kernel.org>
Thu, 7 Jul 2016 07:12:02 +0000 (09:12 +0200)
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Documentation/memory-barriers.txt
arch/arm64/include/asm/barrier.h
arch/arm64/include/asm/cmpxchg.h
kernel/jump_label.c
kernel/locking/qspinlock.c

index 147ae8ec836f85666110634ff5565f4016de1d80..a4d0a99de04da76d62bc0ceebae88fd5283473e4 100644 (file)
@@ -806,6 +806,41 @@ out-guess your code.  More generally, although READ_ONCE() does force
 the compiler to actually emit code for a given load, it does not force
 the compiler to use the results.
 
+In addition, control dependencies apply only to the then-clause and
+else-clause of the if-statement in question.  In particular, it does
+not necessarily apply to code following the if-statement:
+
+       q = READ_ONCE(a);
+       if (q) {
+               WRITE_ONCE(b, p);
+       } else {
+               WRITE_ONCE(b, r);
+       }
+       WRITE_ONCE(c, 1);  /* BUG: No ordering against the read from "a". */
+
+It is tempting to argue that there in fact is ordering because the
+compiler cannot reorder volatile accesses and also cannot reorder
+the writes to "b" with the condition.  Unfortunately for this line
+of reasoning, the compiler might compile the two writes to "b" as
+conditional-move instructions, as in this fanciful pseudo-assembly
+language:
+
+       ld r1,a
+       ld r2,p
+       ld r3,r
+       cmp r1,$0
+       cmov,ne r4,r2
+       cmov,eq r4,r3
+       st r4,b
+       st $1,c
+
+A weakly ordered CPU would have no dependency of any sort between the load
+from "a" and the store to "c".  The control dependencies would extend
+only to the pair of cmov instructions and the store depending on them.
+In short, control dependencies apply only to the stores in the then-clause
+and else-clause of the if-statement in question (including functions
+invoked by those two clauses), not to code following that if-statement.
+
 Finally, control dependencies do -not- provide transitivity.  This is
 demonstrated by two related examples, with the initial values of
 x and y both being zero:
@@ -869,6 +904,12 @@ In summary:
       atomic{,64}_read() can help to preserve your control dependency.
       Please see the COMPILER BARRIER section for more information.
 
+  (*) Control dependencies apply only to the then-clause and else-clause
+      of the if-statement containing the control dependency, including
+      any functions that these two clauses call.  Control dependencies
+      do -not- apply to code following the if-statement containing the
+      control dependency.
+
   (*) Control dependencies pair normally with other types of barriers.
 
   (*) Control dependencies do -not- provide transitivity.  If you
index dae5c49618db38021b418379bcb9fe52529ec862..4eea7f618dcefff4be6e3f4d99c503cba5e7c779 100644 (file)
@@ -91,6 +91,19 @@ do {                                                                 \
        __u.__val;                                                      \
 })
 
+#define smp_cond_load_acquire(ptr, cond_expr)                          \
+({                                                                     \
+       typeof(ptr) __PTR = (ptr);                                      \
+       typeof(*ptr) VAL;                                               \
+       for (;;) {                                                      \
+               VAL = smp_load_acquire(__PTR);                          \
+               if (cond_expr)                                          \
+                       break;                                          \
+               __cmpwait_relaxed(__PTR, VAL);                          \
+       }                                                               \
+       VAL;                                                            \
+})
+
 #include <asm-generic/barrier.h>
 
 #endif /* __ASSEMBLY__ */
index 510c7b4044547f82750ca9295e6d35bdeb0b67bf..bd86a79491bce84d34576d261214216282265d9a 100644 (file)
@@ -224,4 +224,55 @@ __CMPXCHG_GEN(_mb)
        __ret;                                                          \
 })
 
+#define __CMPWAIT_CASE(w, sz, name)                                    \
+static inline void __cmpwait_case_##name(volatile void *ptr,           \
+                                        unsigned long val)             \
+{                                                                      \
+       unsigned long tmp;                                              \
+                                                                       \
+       asm volatile(                                                   \
+       "       ldxr" #sz "\t%" #w "[tmp], %[v]\n"              \
+       "       eor     %" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"     \
+       "       cbnz    %" #w "[tmp], 1f\n"                             \
+       "       wfe\n"                                                  \
+       "1:"                                                            \
+       : [tmp] "=&r" (tmp), [v] "+Q" (*(unsigned long *)ptr)           \
+       : [val] "r" (val));                                             \
+}
+
+__CMPWAIT_CASE(w, b, 1);
+__CMPWAIT_CASE(w, h, 2);
+__CMPWAIT_CASE(w,  , 4);
+__CMPWAIT_CASE( ,  , 8);
+
+#undef __CMPWAIT_CASE
+
+#define __CMPWAIT_GEN(sfx)                                             \
+static inline void __cmpwait##sfx(volatile void *ptr,                  \
+                                 unsigned long val,                    \
+                                 int size)                             \
+{                                                                      \
+       switch (size) {                                                 \
+       case 1:                                                         \
+               return __cmpwait_case##sfx##_1(ptr, (u8)val);           \
+       case 2:                                                         \
+               return __cmpwait_case##sfx##_2(ptr, (u16)val);          \
+       case 4:                                                         \
+               return __cmpwait_case##sfx##_4(ptr, val);               \
+       case 8:                                                         \
+               return __cmpwait_case##sfx##_8(ptr, val);               \
+       default:                                                        \
+               BUILD_BUG();                                            \
+       }                                                               \
+                                                                       \
+       unreachable();                                                  \
+}
+
+__CMPWAIT_GEN()
+
+#undef __CMPWAIT_GEN
+
+#define __cmpwait_relaxed(ptr, val) \
+       __cmpwait((ptr), (unsigned long)(val), sizeof(*(ptr)))
+
 #endif /* __ASM_CMPXCHG_H */
index 05254eeb4b4e485be75bacff667c8ed3aab4a200..ac4ab953b49c882efbe55c2fcd66871e75f49bef 100644 (file)
@@ -422,7 +422,7 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val,
        return notifier_from_errno(ret);
 }
 
-struct notifier_block jump_label_module_nb = {
+static struct notifier_block jump_label_module_nb = {
        .notifier_call = jump_label_module_notify,
        .priority = 1, /* higher than tracepoints */
 };
index 730655533440e6cd898b93af6404ba05e505906e..b2caec7315af5b622a00f8babfd55bb3f27fb315 100644 (file)
@@ -619,7 +619,7 @@ release:
        /*
         * release the node
         */
-       this_cpu_dec(mcs_nodes[0].count);
+       __this_cpu_dec(mcs_nodes[0].count);
 }
 EXPORT_SYMBOL(queued_spin_lock_slowpath);