Skip to content

Commit 8d53fa1

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/qspinlock: Clarify xchg_tail() ordering
While going over the code I noticed that xchg_tail() is a RELEASE but had no obvious pairing commented. It pairs with a somewhat unique address dependency through decode_tail(). So the store-release of xchg_tail() is paired by the address dependency of the load of xchg_tail followed by the dereference from the pointer computed from that load. The @old -> @Prev transformation itself is pure, and therefore does not depend on external state, so that is immaterial wrt. ordering. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Pan Xinhui <xinhui.pan@linux.vnet.ibm.com> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Waiman Long <waiman.long@hpe.com> Cc: Will Deacon <will.deacon@arm.com> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ae0b5c2 commit 8d53fa1

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

‎kernel/locking/qspinlock.c‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static DEFINE_PER_CPU_ALIGNED(struct mcs_spinlock, mcs_nodes[MAX_NODES]);
9090
* therefore increment the cpu number by one.
9191
*/
9292

93-
static inline u32 encode_tail(int cpu, int idx)
93+
static inline __pure u32 encode_tail(int cpu, int idx)
9494
{
9595
u32 tail;
9696

@@ -103,7 +103,7 @@ static inline u32 encode_tail(int cpu, int idx)
103103
return tail;
104104
}
105105

106-
static inline struct mcs_spinlock *decode_tail(u32 tail)
106+
static inline __pure struct mcs_spinlock *decode_tail(u32 tail)
107107
{
108108
int cpu = (tail >> _Q_TAIL_CPU_OFFSET) - 1;
109109
int idx = (tail & _Q_TAIL_IDX_MASK) >> _Q_TAIL_IDX_OFFSET;
@@ -455,6 +455,8 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
455455
* pending stuff.
456456
*
457457
* p,*,* -> n,*,*
458+
*
459+
* RELEASE, such that the stores to @node must be complete.
458460
*/
459461
old = xchg_tail(lock, tail);
460462
next = NULL;
@@ -465,6 +467,15 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
465467
*/
466468
if (old & _Q_TAIL_MASK) {
467469
prev = decode_tail(old);
470+
/*
471+
* The above xchg_tail() is also a load of @lock which generates,
472+
* through decode_tail(), a pointer.
473+
*
474+
* The address dependency matches the RELEASE of xchg_tail()
475+
* such that the access to @prev must happen after.
476+
*/
477+
smp_read_barrier_depends();
478+
468479
WRITE_ONCE(prev->next, node);
469480

470481
pv_wait_node(node, prev);

0 commit comments

Comments
 (0)