Skip to content

Commit d89e588

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking: Introduce smp_mb__after_spinlock()
Since its inception, our understanding of ACQUIRE, esp. as applied to spinlocks, has changed somewhat. Also, I wonder if, with a simple change, we cannot make it provide more. The problem with the comment is that the STORE done by spin_lock isn't itself ordered by the ACQUIRE, and therefore a later LOAD can pass over it and cross with any prior STORE, rendering the default WMB insufficient (pointed out by Alan). Now, this is only really a problem on PowerPC and ARM64, both of which already defined smp_mb__before_spinlock() as a smp_mb(). At the same time, we can get a much stronger construct if we place that same barrier _inside_ the spin_lock(). In that case we upgrade the RCpc spinlock to an RCsc. That would make all schedule() calls fully transitive against one another. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Will Deacon <will.deacon@arm.com> Cc: Alan Stern <stern@rowland.harvard.edu> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Nicholas Piggin <npiggin@gmail.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Paul McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ff7a5fb commit d89e588

5 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎arch/arm64/include/asm/spinlock.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,5 +367,7 @@ static inline int arch_read_trylock(arch_rwlock_t *rw)
367367
* smp_mb__before_spinlock() can restore the required ordering.
368368
*/
369369
#define smp_mb__before_spinlock() smp_mb()
370+
/* See include/linux/spinlock.h */
371+
#define smp_mb__after_spinlock() smp_mb()
370372

371373
#endif /* __ASM_SPINLOCK_H */

‎arch/powerpc/include/asm/spinlock.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,8 @@ static inline void arch_write_unlock(arch_rwlock_t *rw)
342342
#define arch_read_relax(lock) __rw_yield(lock)
343343
#define arch_write_relax(lock) __rw_yield(lock)
344344

345+
/* See include/linux/spinlock.h */
346+
#define smp_mb__after_spinlock() smp_mb()
347+
345348
#endif /* __KERNEL__ */
346349
#endif /* __ASM_SPINLOCK_H */

‎include/linux/atomic.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
* Besides, if an arch has a special barrier for acquire/release, it could
3939
* implement its own __atomic_op_* and use the same framework for building
4040
* variants
41+
*
42+
* If an architecture overrides __atomic_op_acquire() it will probably want
43+
* to define smp_mb__after_spinlock().
4144
*/
4245
#ifndef __atomic_op_acquire
4346
#define __atomic_op_acquire(op, args...) \

‎include/linux/spinlock.h‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,42 @@ do { \
130130
#define smp_mb__before_spinlock() smp_wmb()
131131
#endif
132132

133+
/*
134+
* This barrier must provide two things:
135+
*
136+
* - it must guarantee a STORE before the spin_lock() is ordered against a
137+
* LOAD after it, see the comments at its two usage sites.
138+
*
139+
* - it must ensure the critical section is RCsc.
140+
*
141+
* The latter is important for cases where we observe values written by other
142+
* CPUs in spin-loops, without barriers, while being subject to scheduling.
143+
*
144+
* CPU0 CPU1 CPU2
145+
*
146+
* for (;;) {
147+
* if (READ_ONCE(X))
148+
* break;
149+
* }
150+
* X=1
151+
* <sched-out>
152+
* <sched-in>
153+
* r = X;
154+
*
155+
* without transitivity it could be that CPU1 observes X!=0 breaks the loop,
156+
* we get migrated and CPU2 sees X==0.
157+
*
158+
* Since most load-store architectures implement ACQUIRE with an smp_mb() after
159+
* the LL/SC loop, they need no further barriers. Similarly all our TSO
160+
* architectures imply an smp_mb() for each atomic instruction and equally don't
161+
* need more.
162+
*
163+
* Architectures that can implement ACQUIRE better need to take care.
164+
*/
165+
#ifndef smp_mb__after_spinlock
166+
#define smp_mb__after_spinlock() do { } while (0)
167+
#endif
168+
133169
/**
134170
* raw_spin_unlock_wait - wait until the spinlock gets unlocked
135171
* @lock: the spinlock in question.

‎kernel/sched/core.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,8 +1967,8 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
19671967
* reordered with p->state check below. This pairs with mb() in
19681968
* set_current_state() the waiting thread does.
19691969
*/
1970-
smp_mb__before_spinlock();
19711970
raw_spin_lock_irqsave(&p->pi_lock, flags);
1971+
smp_mb__after_spinlock();
19721972
if (!(p->state & state))
19731973
goto out;
19741974

@@ -3281,8 +3281,8 @@ static void __sched notrace __schedule(bool preempt)
32813281
* can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
32823282
* done by the caller to avoid the race with signal_wake_up().
32833283
*/
3284-
smp_mb__before_spinlock();
32853284
rq_lock(rq, &rf);
3285+
smp_mb__after_spinlock();
32863286

32873287
/* Promote REQ to ACT */
32883288
rq->clock_update_flags <<= 1;

0 commit comments

Comments
 (0)