Skip to content

Commit 1f03e8d

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/barriers: Replace smp_cond_acquire() with smp_cond_load_acquire()
This new form allows using hardware assisted waiting. Some hardware (ARM64 and x86) allow monitoring an address for changes, so by providing a pointer we can use this to replace the cpu_relax() with hardware optimized methods in the future. Requested-by: Will Deacon <will.deacon@arm.com> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Paul E. 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 245050c commit 1f03e8d

5 files changed

Lines changed: 31 additions & 18 deletions

File tree

‎include/linux/compiler.h‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,21 +305,34 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
305305
})
306306

307307
/**
308-
* smp_cond_acquire() - Spin wait for cond with ACQUIRE ordering
308+
* smp_cond_load_acquire() - (Spin) wait for cond with ACQUIRE ordering
309+
* @ptr: pointer to the variable to wait on
309310
* @cond: boolean expression to wait for
310311
*
311312
* Equivalent to using smp_load_acquire() on the condition variable but employs
312313
* the control dependency of the wait to reduce the barrier on many platforms.
313314
*
315+
* Due to C lacking lambda expressions we load the value of *ptr into a
316+
* pre-named variable @VAL to be used in @cond.
317+
*
314318
* The control dependency provides a LOAD->STORE order, the additional RMB
315319
* provides LOAD->LOAD order, together they provide LOAD->{LOAD,STORE} order,
316320
* aka. ACQUIRE.
317321
*/
318-
#define smp_cond_acquire(cond) do { \
319-
while (!(cond)) \
320-
cpu_relax(); \
321-
smp_rmb(); /* ctrl + rmb := acquire */ \
322-
} while (0)
322+
#ifndef smp_cond_load_acquire
323+
#define smp_cond_load_acquire(ptr, cond_expr) ({ \
324+
typeof(ptr) __PTR = (ptr); \
325+
typeof(*ptr) VAL; \
326+
for (;;) { \
327+
VAL = READ_ONCE(*__PTR); \
328+
if (cond_expr) \
329+
break; \
330+
cpu_relax(); \
331+
} \
332+
smp_rmb(); /* ctrl + rmb := acquire */ \
333+
VAL; \
334+
})
335+
#endif
323336

324337
#endif /* __KERNEL__ */
325338

‎kernel/locking/qspinlock.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
475475
* sequentiality; this is because not all clear_pending_set_locked()
476476
* implementations imply full barriers.
477477
*/
478-
smp_cond_acquire(!(atomic_read(&lock->val) & _Q_LOCKED_MASK));
478+
smp_cond_load_acquire(&lock->val.counter, !(VAL & _Q_LOCKED_MASK));
479479

480480
/*
481481
* take ownership and clear the pending bit.
@@ -562,7 +562,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
562562
*
563563
* The PV pv_wait_head_or_lock function, if active, will acquire
564564
* the lock and return a non-zero value. So we have to skip the
565-
* smp_cond_acquire() call. As the next PV queue head hasn't been
565+
* smp_cond_load_acquire() call. As the next PV queue head hasn't been
566566
* designated yet, there is no way for the locked value to become
567567
* _Q_SLOW_VAL. So both the set_locked() and the
568568
* atomic_cmpxchg_relaxed() calls will be safe.
@@ -573,7 +573,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
573573
if ((val = pv_wait_head_or_lock(lock, node)))
574574
goto locked;
575575

576-
smp_cond_acquire(!((val = atomic_read(&lock->val)) & _Q_LOCKED_PENDING_MASK));
576+
val = smp_cond_load_acquire(&lock->val.counter, !(VAL & _Q_LOCKED_PENDING_MASK));
577577

578578
locked:
579579
/*
@@ -593,9 +593,9 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
593593
break;
594594
}
595595
/*
596-
* The smp_cond_acquire() call above has provided the necessary
597-
* acquire semantics required for locking. At most two
598-
* iterations of this loop may be ran.
596+
* The smp_cond_load_acquire() call above has provided the
597+
* necessary acquire semantics required for locking. At most
598+
* two iterations of this loop may be ran.
599599
*/
600600
old = atomic_cmpxchg_relaxed(&lock->val, val, _Q_LOCKED_VAL);
601601
if (old == val)

‎kernel/sched/core.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
19351935
* chain to provide order. Instead we do:
19361936
*
19371937
* 1) smp_store_release(X->on_cpu, 0)
1938-
* 2) smp_cond_acquire(!X->on_cpu)
1938+
* 2) smp_cond_load_acquire(!X->on_cpu)
19391939
*
19401940
* Example:
19411941
*
@@ -1946,7 +1946,7 @@ static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
19461946
* sched-out X
19471947
* smp_store_release(X->on_cpu, 0);
19481948
*
1949-
* smp_cond_acquire(!X->on_cpu);
1949+
* smp_cond_load_acquire(&X->on_cpu, !VAL);
19501950
* X->state = WAKING
19511951
* set_task_cpu(X,2)
19521952
*
@@ -1972,7 +1972,7 @@ static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
19721972
* This means that any means of doing remote wakeups must order the CPU doing
19731973
* the wakeup against the CPU the task is going to end up running on. This,
19741974
* however, is already required for the regular Program-Order guarantee above,
1975-
* since the waking CPU is the one issueing the ACQUIRE (smp_cond_acquire).
1975+
* since the waking CPU is the one issueing the ACQUIRE (smp_cond_load_acquire).
19761976
*
19771977
*/
19781978

@@ -2045,7 +2045,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
20452045
* This ensures that tasks getting woken will be fully ordered against
20462046
* their previous state and preserve Program Order.
20472047
*/
2048-
smp_cond_acquire(!p->on_cpu);
2048+
smp_cond_load_acquire(&p->on_cpu, !VAL);
20492049

20502050
p->sched_contributes_to_load = !!task_contributes_to_load(p);
20512051
p->state = TASK_WAKING;

‎kernel/sched/sched.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
11131113
* In particular, the load of prev->state in finish_task_switch() must
11141114
* happen before this.
11151115
*
1116-
* Pairs with the smp_cond_acquire() in try_to_wake_up().
1116+
* Pairs with the smp_cond_load_acquire() in try_to_wake_up().
11171117
*/
11181118
smp_store_release(&prev->on_cpu, 0);
11191119
#endif

‎kernel/smp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void __init call_function_init(void)
107107
*/
108108
static __always_inline void csd_lock_wait(struct call_single_data *csd)
109109
{
110-
smp_cond_acquire(!(csd->flags & CSD_FLAG_LOCK));
110+
smp_cond_load_acquire(&csd->flags, !(VAL & CSD_FLAG_LOCK));
111111
}
112112

113113
static __always_inline void csd_lock(struct call_single_data *csd)

0 commit comments

Comments
 (0)