locking/osq_lock: Optimisations to osq_lock code
v4 cover locking/osq_lock: Optimisations to osq_lock code
This is a continuation of some patches I wrote over two years ago. They go a lot further and reduce the per-cpu data to a structure that only contains two cpu numbers.
I've fixed some broken/missing memory barriers but left the initial xchg() when acquiring the lock as a full barrier, I think it could be relaxed.
Tested with a userspace harness that can conditionally sleep at various points.
David Laight (9):
locking/osq_lock: Add some comments about how it works locking/osq_lock: Save the cpu number for 'prev' not the node address locking/osq_lock: Set prev_cpu=0 instead of locked=1 locking/osq_lock: Delete 'fast path' code from osq_unlock() locking/osq_lock: Avoid writing to node->next in the osq_lock() fast
path
locking/osq: Use cpu number for 'next' pointer locking/osq: Use 'unsigned int' for next/prev/tail locking/osq: inline encode_cpu() and rename decode_cpu() locking/osq_lock: Swap next<->prev and tail<->head
include/linux/osq_lock.h | 10 +- kernel/locking/osq_lock.c | 286 ++++++++++++++++++++------------------ 2 files changed, 154 insertions(+), 142 deletions(-)
Signature · 1 line
2.39.5
PATCH v4 1/9 locking/osq_lock: Add some comments about how it works
No code changes, just some extra explanations.
--- kernel/locking/osq_lock.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
kernel/locking/osq_lock.c +24 -3
@@ -4,12 +4,33 @@#include <linux/osq_lock.h> /* -* An MCS like lock especially tailored for optimistic spinning for sleeping -* lock implementations (mutex, rwsem, etc). +* An MCS like spin lock especially tailored for optimistic spinning for +* sleeping lock implementations (mutex, rwsem, etc). +* Each CPU spins on a local variable to avoid cache-line bounces. * -* Using a single mcs node per CPU is safe because sleeping locks should not be +* The CPU that holds the osq_lock checks the mutex/rwsem, the other CPU spin +* in osq_lock() until either the osq_lock is obtained or the scheduler +* requests the process be preempted. +* +* Using a single osq node per CPU is safe because sleeping locks should not be * called from interrupt context and we have preemption disabled while * spinning. +* +* The osq_nodes for the spinning CPU are put on a double-linked (non circular) +* list. The list 'pointers' can either be the address of the osq_node or the +* associated CPU number, the CPU numbers are offset by one so that zero can +* be used like a NULL ponter. +* The mutex/rwsem contains a pointer (CPU number) to the tail of the list. +* There is no equivalent pointer to the list head - the 'head' is the +* osq_node of the CPU that acquired the osq lock. +* +* The 'next' pointer of the tail must be NULL, all the other 'next' pointers +* must either be valid or transiently NULL. +* The 'prev' pointers only need to be valid when node->prev makes sense and, +* even then, can be transiently invalid (ie refer to the wrong node). +* They are only used for the node->prev->next = node->next update when +* 'node' is being removed. Atomically checking node->prev->next == node +* ensures the list doesn't get corrupted. */ struct optimistic_spin_node {
Signature · 1 line
2.39.5
PATCH v4 2/9 locking/osq_lock: Save the cpu number for 'prev' not the node address
In reply to David LaightThe cpu number of node->prev is needed for both the vcpu_is_preempted() test and to update lock->tail.
This saves reading the cache line for the other cpu's per-cpu data.
The cpu member of optimistic_spin_node is no longer needed.
Merges patches 2 and 3 from v3.
--- kernel/locking/osq_lock.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)
kernel/locking/osq_lock.c +14 -19
@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
@@ -50,11 +50,6 @@ static inline int encode_cpu(int cpu_nr)return cpu_nr +1 ; } -static inline int node_cpu (struct optimistic_spin_node *node) -{ -return node->cpu -1 ; -} -static inline struct optimistic_spin_node *decode_cpu (int encoded_cpu_val) {int cpu_nr = encoded_cpu_val -1 ;
@@ -114,13 +109,12 @@ osq_wait_next(struct optimistic_spin_queue *lock,bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev, *next; +struct optimistic_spin_node *prev_ptr, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev; node->locked =0 ; node->next =NULL ; - node->cpu = curr;/* * We need both ACQUIRE (pairs with corresponding RELEASE in
@@ -128,11 +122,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)* the node fields we just initialised) semantics when updating * the lock tail. */ - old =atomic_xchg (&lock->tail, curr); -if (old ==OSQ_UNLOCKED_VAL ) + prev =atomic_xchg (&lock->tail, curr); +if (prev ==OSQ_UNLOCKED_VAL )return true; - prev =decode_cpu (old); + prev_ptr =decode_cpu (prev); node->prev = prev;/*
@@ -147,7 +141,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)*/ smp_wmb (); -WRITE_ONCE (prev->next, node); +WRITE_ONCE (prev_ptr->next, node);/* * Normally @prev is untouchable after the above store; because at that
@@ -165,7 +159,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* polling, be careful. */ if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node_cpu (node->prev)))) +vcpu_is_preempted (node->prev -1 )))return true;/* unqueue */
@@ -182,8 +176,8 @@ bool osq_lock(struct optimistic_spin_queue *lock)* cpu_relax() below implies a compiler barrier which would * prevent this comparison being optimized away. */ -if (data_race (prev->next) == node && -cmpxchg (&prev->next, node,NULL ) == node) +if (data_race (prev_ptr->next) == node && +cmpxchg (&prev_ptr->next, node,NULL ) == node)break ;/*
@@ -201,6 +195,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* case its step-C will write us a new @node->prev pointer. */ prev =READ_ONCE (node->prev); + prev_ptr =decode_cpu (prev); }/*
@@ -210,7 +205,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* back to @prev. */ - next =osq_wait_next (lock, node, prev->cpu); + next =osq_wait_next (lock, node, prev);if (!next)return false;
@@ -223,7 +218,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)*/ WRITE_ONCE (next->prev, prev); -WRITE_ONCE (prev->next, next); +WRITE_ONCE (prev_ptr->next, next);return false; }
Signature · 1 line
2.39.5
PATCH v4 3/9 locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David LaightThere is no need for separate prev_cpu and locked members of struct optimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.
When cancelling a lock request explicitly set prev_cpu to zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path' osq_lock() call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.
--- kernel/locking/osq_lock.c | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-)
kernel/locking/osq_lock.c +28 -29
@@ -35,7 +35,6 @@struct optimistic_spin_node {struct optimistic_spin_node *next; -int locked;/* 1 if lock acquired */ int prev;/* CPU number offset by 1 */ };
@@ -113,7 +112,6 @@ bool osq_lock(struct optimistic_spin_queue *lock)int curr =encode_cpu (smp_processor_id ());int prev; - node->locked =0 ; node->next =NULL ;/*
@@ -158,46 +156,47 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on * polling, be careful. */ -if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node->prev -1 ))) -return true; + prev =smp_cond_load_relaxed (&node->prev, !VAL ||need_resched () || +vcpu_is_preempted (VAL -1 )); -/* unqueue */ /* -* Step - A -- stabilize @prev +* Step - A * -* Undo our @prev->next assignment; this will make @prev's -* unlock()/unqueue() wait for a next pointer since @lock points to us -* (or later). +* Loop until either node->prev is zero (lock acquired) or we +* atomically change prev->next from node to NULL (stopping prev +* handing on the lock). +* Note that 'prev' can unlink itself concurrently with this +* test so that prev/prev_ptr can be stale, but since it +* is per-cpu data the memory can always be read. */ -for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +for (;; prev =READ_ONCE (node->prev)) { +if (!prev) +/* Lock acquired */ +return true; + + prev_ptr =decode_cpu (prev); +if (data_race (prev_ptr->next) == node &&cmpxchg (&prev_ptr->next, node,NULL ) == node)break ;/* -* We can only fail the cmpxchg() racing against an unlock(), -* in which case we should observe @node->locked becoming -* true. +* 'prev' must have unlinked (or be in the process of unlinking) +* itself from the list. */ -if (smp_load_acquire (&node->locked)) -return true;cpu_relax (); - -/* -* Or we race against a concurrent unqueue()'s step-B, in which -* case its step-C will write us a new @node->prev pointer. -*/ - prev =READ_ONCE (node->prev); - prev_ptr =decode_cpu (prev); } +/* +* If 'prev' tries to remove itself from the list before we write +* a new value to prev->next it will spin in osq_wait_next(). +*/ + +/* Invalidate prev_cpu matching osq_unlock() */ + node->prev =0 ; +/* * Step - B -- stabilize @next *
@@ -240,11 +239,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) { -WRITE_ONCE (next->locked,1 ); +WRITE_ONCE (next->prev,0 );return ; } next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +WRITE_ONCE (next->prev,0 ); }
Signature · 1 line
2.39.5
PATCH v4 4/9 locking/osq_lock: Delete 'fast path' code from osq_unlock()
In reply to David LaightThe 'fast path' code in osq_unlock() is pretty much exactly the same as the first pass of the loop in osq_wait_next() except that it doesn't have the optimisation to avoid the locked RMW when not the tail of the list.
So just call osq_wait_next().
Move the assignment next->prev_cpu = old_cpu into osq_wait_next() as it is always the next line.
Rename osq_wait_next() to osq_unlink_from_next() since that is what is does.
Change osq_wait_next() to use atomic_cmpxchg_release() (not _acquire) on lock->tail.
This is what osq_unlock() did and seems right to me.
Add an smp_wmb() before the 'prev->next = next' assignment when cancelling a lock. The previous 'next->prev = prev' assignment lets the 'prev' cpu complete an unlocking sequence and do its 'next->prev = prev' assignment first - corrupting the list.
--- kernel/locking/osq_lock.c | 140 ++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 67 deletions(-)
kernel/locking/osq_lock.c +73 -67
@@ -57,52 +57,67 @@ static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) }/* -* Get a stable @node->next pointer, either for unlock() or unqueue() purposes. -* Can return NULL in case we were the last queued and we updated @lock instead. +* Unlink the current cpu's node from the lock's node->prev list. * -* If osq_lock() is being cancelled there must be a previous node -* and 'old_cpu' is its CPU #. -* For osq_unlock() there is never a previous node and old_cpu is -* set to OSQ_UNLOCKED_VAL. +* More specifically atomically write its node->prev over the link that +* currently points to node. +* This is either: +* lock->tail = node->prev +* or: +* node->next->prev = node->prev +* The first is a simple cmpxchg(), the second is protected against +* node->next trying to unlink itself (after need_resched() is set) by using +* an xchg() on node->next that sets it to NULL. +* +* When a lock request is being cancelled the caller needs 'next' to +* set node->prev->next = next. */ static inline struct optimistic_spin_node * -osq_wait_next (struct optimistic_spin_queue *lock, -struct optimistic_spin_node *node, -int old_cpu) +osq_unlink_from_next (struct optimistic_spin_queue *lock,int prev) {int curr =encode_cpu (smp_processor_id ()); +struct optimistic_spin_node *node, *next;for (;;) { -if (atomic_read (&lock->tail) == curr && -atomic_cmpxchg_acquire (&lock->tail, curr, old_cpu) == curr) { +int tail =atomic_read (&lock->tail); +if (curr == tail && +atomic_try_cmpxchg_release (&lock->tail, &tail, prev)) {/* -* We were the last queued, we moved @lock back. @prev -* will now observe @lock and will complete its -* unlock()/unqueue(). +* We were the last queued, lock->tail now references +* prev (or is 0 if the list is now empty). +* If prev was spinning in this loop it can continue. */ return NULL ; } + node =this_cpu_ptr (&osq_node); +/* -* We must xchg() the @node->next value, because if we were to -* leave it in, a concurrent unlock()/unqueue() from -* @node->next might complete Step-A and think its @prev is -* still valid. +* We must xchg() the @node->next value to ensure that a +* concurrent unqueue() from @node->next will find an invalid +* @prev value (node_next->prev->next != node_next). * -* If the concurrent unlock()/unqueue() wins the race, we'll -* wait for either @lock to point to us, through its Step-B, or -* wait for a new @node->next from its Step-C. +* If @node->next is already NULL then we need to wait until +* the concurrent unqueue completes. */ if (node->next) { -struct optimistic_spin_node *next; - next =xchg (&node->next,NULL );if (next) -return next; +break ; }cpu_relax (); } + +/* +* When called from osq_unlock() prev is zero and this hands +* over the lock ownership. +* When called while unqueueing in osq_lock() this completes the +* backwards link, the forwards link is done by the caller. +*/ +WRITE_ONCE (next->prev, prev); + +return next; }bool osq_lock (struct optimistic_spin_queue *lock)
@@ -130,7 +145,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)/* * osq_lock() unqueue * -* node->prev = prev osq_wait_next() +* node->prev = prev osq_unlink_from_next() * WMB MB * prev->next = node next->prev = prev // unqueue-C *
@@ -160,8 +175,6 @@ bool osq_lock(struct optimistic_spin_queue *lock) vcpu_is_preempted(VAL - 1));/* -* Step - A -* * Loop until either node->prev is zero (lock acquired) or we * atomically change prev->next from node to NULL (stopping prev * handing on the lock).
@@ -191,59 +204,52 @@ bool osq_lock(struct optimistic_spin_queue *lock)/* * If 'prev' tries to remove itself from the list before we write -* a new value to prev->next it will spin in osq_wait_next(). +* a new value to prev->next it will spin in osq_unlink_from_next(). +* This means we can no longer be given the lock and always +* return false. */ -/* Invalidate prev_cpu matching osq_unlock() */ +/* +* Invalidate prev matching osq_unlock(). +* This isn't necessary but ensures that both unlocked and fast-path +* locked nodes (where the initial xchg() returned 0) have prev set +* to zero. +* If nothing else it lets the lock chain be followed from lock->tail +* whch may help diagnostics. +*/ node->prev =0 ;/* -* Step - B -- stabilize @next -* -* Similar to unlock(), wait for @node->next or move @lock from @node -* back to @prev. +* Now that the linkage to prev cannot change underneath us +* remove ourselves from the node->prev list. +* This does: +* (node->next ? node->next->prev : lock->tail) = node->prev */ - - next =osq_wait_next (lock, node, prev); -if (!next) -return false; + next =osq_unlink_from_next (lock, prev);/* -* Step - C -- unlink -* -* @prev is stable because its still waiting for a new @prev->next -* pointer, @next is stable because our @node->next pointer is NULL and -* it will wait in Step-A. +* Finally mend the node->next list that was 'broken' to +* stop node->prev trying to unlink from us. +* If next is NULL then lock->tail is prev_ptr and another node +* can be added - so we must not re-write the NULL. */ - -WRITE_ONCE (next->prev, prev); -WRITE_ONCE (prev_ptr->next, next); +if (next) { +/* +* This must happen after the write to node->next->prev. +* If swapped then prev could unlink itself before our +* write to node->next->prev and the the wrong value would +* end up in node->next->prev. +* Probably can't actually happen due to re-ordering of writes, +* but could happen without a compiler barrier. +*/ +smp_wmb (); +WRITE_ONCE (prev_ptr->next, next); + }return false; }void osq_unlock (struct optimistic_spin_queue *lock) { -struct optimistic_spin_node *node, *next; -int curr =encode_cpu (smp_processor_id ()); - -/* -* Fast path for the uncontended case. -*/ -if (atomic_try_cmpxchg_release (&lock->tail, &curr,OSQ_UNLOCKED_VAL )) -return ; - -/* -* Second most likely case. -*/ - node =this_cpu_ptr (&osq_node); - next =xchg (&node->next,NULL ); -if (next) { -WRITE_ONCE (next->prev,0 ); -return ; - } - - next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL ); -if (next) -WRITE_ONCE (next->prev,0 ); +osq_unlink_from_next (lock,OSQ_UNLOCKED_VAL ); }
Signature · 1 line
2.39.5
PATCH v4 5/9 locking/osq_lock: Avoid writing to node->next in the osq_lock() fast path
In reply to David Laightosq_unlink_from_next() is called by osq_unlock() and when osq_lock() returns false (lock not acquired).osq_unlink_from_next() will either have done an explicit xchg(&node->next, NULL) or a cmpxchg() that checked that node was lock->tail.
In both cases node->next will be NULL in exit.
Since it can't be changed when not referenced by an osq_lock there is no need to initialise it at the top of osq_lock().
The atomic_xchg(&lock->tail, curr) could probably changed back to the '_acquire' version or even the _relaxed version. The important barrier is after the write to node->prev.
Defer determining the address of the CPU's 'node' until after the atomic_exchange() so that it isn't done in the uncontented path.
--- kernel/locking/osq_lock.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/locking/osq_lock.c +5 -4
@@ -86,6 +86,9 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, int prev)* We were the last queued, lock->tail now references * prev (or is 0 if the list is now empty). * If prev was spinning in this loop it can continue. +* +* Since we are the tail of the list, node->next +* must be NULL. */ return NULL ; }
@@ -122,13 +125,10 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, int prev)bool osq_lock (struct optimistic_spin_queue *lock) { -struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev_ptr, *next; +struct optimistic_spin_node *node, *prev_ptr, *next;int curr =encode_cpu (smp_processor_id ());int prev; - node->next =NULL ; -/* * We need both ACQUIRE (pairs with corresponding RELEASE in * unlock() uncontended, or fastpath) and RELEASE (to publish
@@ -139,6 +139,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)if (prev ==OSQ_UNLOCKED_VAL )return true; + node =this_cpu_ptr (&osq_node); prev_ptr =decode_cpu (prev); node->prev = prev;
Signature · 1 line
2.39.5
PATCH v4 6/9 locking/osq: Use cpu number for 'next' pointer
In reply to David LaightThere is only one write done through node->next (setting prev) when 'node' is being removed.
So the code can consistently use the cpu numbers pretty much throughout (this was suggested by Linux a while back).
This reduces struct optimistic_spin_node to 8 bytes.
This is currently padded out to a cache line which is silly. Change to be __aligned(8) so that it isn't split between cache lines.
Accesses to 'other cpu' data are very limited and only happen during the enque and deque operation.
--- kernel/locking/osq_lock.c | 43 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-)
kernel/locking/osq_lock.c +22 -21
@@ -24,21 +24,21 @@* There is no equivalent pointer to the list head - the 'head' is the * osq_node of the CPU that acquired the osq lock. * -* The 'next' pointer of the tail must be NULL, all the other 'next' pointers -* must either be valid or transiently NULL. -* The 'prev' pointers only need to be valid when node->prev makes sense and, -* even then, can be transiently invalid (ie refer to the wrong node). -* They are only used for the node->prev->next = node->next update when -* 'node' is being removed. Atomically checking node->prev->next == node +* The 'next' pointer of the tail must be zero, all the other 'next' pointers +* must either be valid or transiently zero. +* The 'prev' pointer is zero unless the node is waiting for the lock, when +* waiting it may refer to the wrong node (node->prev->next != node). +* The 'prev' value is only needed for the node->prev->next = node->next update +* when 'node' is being removed. Atomically checking node->prev->next == node * ensures the list doesn't get corrupted. */ struct optimistic_spin_node { -struct optimistic_spin_node *next; -int prev;/* CPU number offset by 1 */ -}; +int next;/* CPU number offset by 1, 0 if no next */ +int prev;/* CPU number offset by 1, 0 if lock held */ +}__aligned(8) ; -static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node); +static DEFINE_PER_CPU(struct optimistic_spin_node, osq_node);/* * We use the value 0 to represent "no CPU", thus the encoded value
@@ -72,11 +72,12 @@ static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)* When a lock request is being cancelled the caller needs 'next' to * set node->prev->next = next. */ -static inline struct optimistic_spin_node * +static inline int osq_unlink_from_next (struct optimistic_spin_queue *lock,int prev) {int curr =encode_cpu (smp_processor_id ()); -struct optimistic_spin_node *node, *next; +struct optimistic_spin_node *node; +int next;for (;;) {int tail =atomic_read (&lock->tail);
@@ -88,9 +89,9 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, int prev)* If prev was spinning in this loop it can continue. * * Since we are the tail of the list, node->next -* must be NULL. +* must be zero. */ -return NULL ; +return 0 ; } node =this_cpu_ptr (&osq_node);
@@ -104,7 +105,7 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, int prev)* the concurrent unqueue completes. */ if (node->next) { - next =xchg (&node->next,NULL ); + next =xchg (&node->next,0 );if (next)break ; }
@@ -118,16 +119,16 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, int prev)* When called while unqueueing in osq_lock() this completes the * backwards link, the forwards link is done by the caller. */ -WRITE_ONCE (next->prev, prev); +WRITE_ONCE (decode_cpu (next)->prev, prev);return next; }bool osq_lock (struct optimistic_spin_queue *lock) { -struct optimistic_spin_node *node, *prev_ptr, *next; +struct optimistic_spin_node *node, *prev_ptr;int curr =encode_cpu (smp_processor_id ()); -int prev; +int next, prev;/* * We need both ACQUIRE (pairs with corresponding RELEASE in
@@ -155,7 +156,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)*/ smp_wmb (); -WRITE_ONCE (prev_ptr->next, node); +WRITE_ONCE (prev_ptr->next, curr);/* * Normally @prev is untouchable after the above store; because at that
@@ -191,8 +192,8 @@ bool osq_lock(struct optimistic_spin_queue *lock) prev_ptr =decode_cpu (prev); -if (data_race (prev_ptr->next) == node && -cmpxchg (&prev_ptr->next, node,NULL ) == node) +if (data_race (prev_ptr->next) == curr && +cmpxchg (&prev_ptr->next, curr,0 ) == curr)break ;/*
Signature · 1 line
2.39.5
PATCH v4 7/9 locking/osq: Use 'unsigned int' for next/prev/tail
In reply to David LaightConsistently use 'unsigned int' for all the 'offset by 1' cpu numbers. This makes the code only use one set of xchg primitives. The unsigned type gives marginally better code inside per_cpu_ptr().
--- include/linux/osq_lock.h | 8 ++++---- kernel/locking/osq_lock.c | 31 +++++++++++++++---------------- 2 files changed, 19 insertions(+), 20 deletions(-)
include/linux/osq_lock.h +4 -4
@@ -12,17 +12,17 @@ struct optimistic_spin_queue {* Stores an encoded value of the CPU # of the tail node in the queue. * If the queue is empty, then it's set to OSQ_UNLOCKED_VAL. */ -atomic_t tail; +unsigned int tail; };#define OSQ_UNLOCKED_VAL (0)/* Init macro and function. */ -#define OSQ_LOCK_UNLOCKED { ATOMIC_INIT(OSQ_UNLOCKED_VAL) } +#define OSQ_LOCK_UNLOCKED { OSQ_UNLOCKED_VAL }static inline void osq_lock_init (struct optimistic_spin_queue *lock) { -atomic_set (&lock->tail,OSQ_UNLOCKED_VAL ); +WRITE_ONCE (lock->tail,OSQ_UNLOCKED_VAL ); }extern bool osq_lock (struct optimistic_spin_queue *lock);
@@ -30,7 +30,7 @@ extern void osq_unlock(struct optimistic_spin_queue *lock);static inline bool osq_is_locked (struct optimistic_spin_queue *lock) { -return atomic_read (&lock->tail) !=OSQ_UNLOCKED_VAL ; +return READ_ONCE (lock->tail) !=OSQ_UNLOCKED_VAL ; }#endif
kernel/locking/osq_lock.c +15 -16
@@ -34,8 +34,8 @@*/ struct optimistic_spin_node { -int next;/* CPU number offset by 1, 0 if no next */ -int prev;/* CPU number offset by 1, 0 if lock held */ +unsigned int next;/* CPU number offset by 1, 0 if no next */ +unsigned int prev;/* CPU number offset by 1, 0 if lock held */ }__aligned(8) ; static DEFINE_PER_CPU(struct optimistic_spin_node, osq_node);
@@ -44,16 +44,15 @@ static DEFINE_PER_CPU(struct optimistic_spin_node, osq_node);* We use the value 0 to represent "no CPU", thus the encoded value * will be the CPU number incremented by 1. */ -static inline int encode_cpu (int cpu_nr) +static inline unsigned int encode_cpu (unsigned int cpu_nr) {return cpu_nr +1 ; } -static inline struct optimistic_spin_node *decode_cpu (int encoded_cpu_val) +static inline struct optimistic_spin_node * +decode_cpu (unsigned int encoded_cpu_val) { -int cpu_nr = encoded_cpu_val -1 ; - -return per_cpu_ptr (&osq_node, cpu_nr); +return per_cpu_ptr (&osq_node, encoded_cpu_val -1 ); }/*
@@ -72,17 +71,17 @@ static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)* When a lock request is being cancelled the caller needs 'next' to * set node->prev->next = next. */ -static inline int -osq_unlink_from_next (struct optimistic_spin_queue *lock,int prev) +static inline unsigned int +osq_unlink_from_next (struct optimistic_spin_queue *lock,unsigned int prev) { -int curr =encode_cpu (smp_processor_id ()); +unsigned int curr =encode_cpu (smp_processor_id ());struct optimistic_spin_node *node; -int next; +unsigned int next;for (;;) { -int tail =atomic_read (&lock->tail); +unsigned int tail =READ_ONCE (lock->tail);if (curr == tail && -atomic_try_cmpxchg_release (&lock->tail, &tail, prev)) { +try_cmpxchg_release (&lock->tail, &tail, prev)) {/* * We were the last queued, lock->tail now references * prev (or is 0 if the list is now empty).
@@ -127,8 +126,8 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, int prev)bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node, *prev_ptr; -int curr =encode_cpu (smp_processor_id ()); -int next, prev; +unsigned int curr =encode_cpu (smp_processor_id ()); +unsigned int next, prev;/* * We need both ACQUIRE (pairs with corresponding RELEASE in
@@ -136,7 +135,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* the node fields we just initialised) semantics when updating * the lock tail. */ - prev =atomic_xchg (&lock->tail, curr); + prev =xchg (&lock->tail, curr);if (prev ==OSQ_UNLOCKED_VAL )return true;
Signature · 1 line
2.39.5
PATCH v4 8/9 locking/osq: inline encode_cpu() and rename decode_cpu()
In reply to David LaightHaving a function to add the 1 offset to the cpu number doesn't make the code any easier to read.
Just use smp_processor_id() + 1.
Rename decode_cpu() to cpu_spin_node() to match what it does.
No functional change.
--- kernel/locking/osq_lock.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-)
kernel/locking/osq_lock.c +7 -16
@@ -40,19 +40,10 @@ struct optimistic_spin_node { static DEFINE_PER_CPU(struct optimistic_spin_node, osq_node); -/* -* We use the value 0 to represent "no CPU", thus the encoded value -* will be the CPU number incremented by 1. -*/ -static inline unsigned int encode_cpu (unsigned int cpu_nr) -{ -return cpu_nr +1 ; -} -static inline struct optimistic_spin_node * -decode_cpu (unsigned int encoded_cpu_val) +cpu_spin_node (unsigned int offset_cpu_num) { -return per_cpu_ptr (&osq_node, encoded_cpu_val -1 ); +return per_cpu_ptr (&osq_node, offset_cpu_num -1 ); }/*
@@ -74,7 +65,7 @@ decode_cpu(unsigned int encoded_cpu_val)static inline unsigned int osq_unlink_from_next (struct optimistic_spin_queue *lock,unsigned int prev) { -unsigned int curr =encode_cpu (smp_processor_id ()); +unsigned int curr =smp_processor_id () +1 ;struct optimistic_spin_node *node;unsigned int next;
@@ -118,7 +109,7 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, unsigned int prev)* When called while unqueueing in osq_lock() this completes the * backwards link, the forwards link is done by the caller. */ -WRITE_ONCE (decode_cpu (next)->prev, prev); +WRITE_ONCE (cpu_spin_node (next)->prev, prev);return next; }
@@ -126,7 +117,7 @@ osq_unlink_from_next(struct optimistic_spin_queue *lock, unsigned int prev)bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node, *prev_ptr; -unsigned int curr =encode_cpu (smp_processor_id ()); +unsigned int curr =smp_processor_id () +1 ;unsigned int next, prev;/*
@@ -140,7 +131,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)return true; node =this_cpu_ptr (&osq_node); - prev_ptr =decode_cpu (prev); + prev_ptr =cpu_spin_node (prev); node->prev = prev;/*
@@ -189,7 +180,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)/* Lock acquired */ return true; - prev_ptr =decode_cpu (prev); + prev_ptr =cpu_spin_node (prev);if (data_race (prev_ptr->next) == curr &&cmpxchg (&prev_ptr->next, curr,0 ) == curr)
Signature · 1 line
2.39.5
PATCH v4 9/9 locking/osq_lock: Swap next<->prev and tail<->head
In reply to David LaightI think it is easier to understand this code if you think that cpu add themselves to the head of the list and the cpu at the tail owns the lock.
Remember nodes are never removed from the list - they only remove themselves.
So swap the field names over.
Pretty much all the code has already been changed in this series so it doesn't make that much difference to the overall lines changed.
--- include/linux/osq_lock.h | 8 +- kernel/locking/osq_lock.c | 165 +++++++++++++++++++------------------- 2 files changed, 86 insertions(+), 87 deletions(-)
include/linux/osq_lock.h +4 -4
@@ -9,10 +9,10 @@struct optimistic_spin_queue {/* -* Stores an encoded value of the CPU # of the tail node in the queue. +* Stores an encoded value of the CPU # of the head node in the queue. * If the queue is empty, then it's set to OSQ_UNLOCKED_VAL. */ -unsigned int tail; +unsigned int head; };#define OSQ_UNLOCKED_VAL (0)
@@ -22,7 +22,7 @@ struct optimistic_spin_queue {static inline void osq_lock_init (struct optimistic_spin_queue *lock) { -WRITE_ONCE (lock->tail,OSQ_UNLOCKED_VAL ); +WRITE_ONCE (lock->head,OSQ_UNLOCKED_VAL ); }extern bool osq_lock (struct optimistic_spin_queue *lock);
@@ -30,7 +30,7 @@ extern void osq_unlock(struct optimistic_spin_queue *lock);static inline bool osq_is_locked (struct optimistic_spin_queue *lock) { -return READ_ONCE (lock->tail) !=OSQ_UNLOCKED_VAL ; +return READ_ONCE (lock->head) !=OSQ_UNLOCKED_VAL ; }#endif
kernel/locking/osq_lock.c +82 -83
296 lines not on this page · raw message
Signature · 1 line
2.39.5
Quoted 3 lines · David Laight
I've fixed some broken/missing memory barriers but left the initial
xchg()when acquiring the lock as a full barrier, I think it could be relaxed.
Well, it should almost certainly be at least an atomic_cmpxchg_acquire(), since that's what osq_wait_next() uses for the contention case.
It's a bit odd that the first initial xchg uses a different memory ordering than the later one. Maybe there's some reason for it.
But even more importantly, that code right now explicitly *states* that it needs a full barrier ("We need both ACQUIRE [..] and RELEASE"), so that *comment* would also have to be fixed with a why the ordering isn't as important as it states.
And finally: none of that will ever be noticeable on x86, since there are no memory orderings on atomics there: lock is all-or-nothing.
End result: I'd love to see actual performance numbers if they exist. And any memory ordering change would require explaining why it's ok and some other architecture to test it.
Or am I missing something?
Linus
Quoted 8 lines · On Mon, 7 Sep 2026 09:08:28 -0700 Linus Torvalds…
Quoted 3 lines · David Laight
I've fixed some broken/missing memory barriers but left the initial
xchg()when acquiring the lock as a full barrier, I think it could be relaxed.Well, it should almost certainly be at least an
atomic_cmpxchg_acquire(), since that's whatosq_wait_next()uses for the contention case.
I'm not sure, but am no expert on acquire/release barriers. The 'fast path' osq_lock() code only has one memory access so there isn't anything to sequence it with.
The important one is the smp_wmb() a bit lower down that ensures the list tail (or head) is written before the back link.
When that was missing things went badly wrong.
(I think the WRITE_ONCE() could be a store_release() instead.)
The ACQUIRE semantics were added to ensure the 'node->next = NULL' assignment happened before the xchg().
That assignment goes away in patch 5.
But I'd want someone who really understands arm64 to comment.
Quoted 3 lines
It's a bit odd that the first initial xchg uses a different memory ordering than the later one. Maybe there's some reason for it.
I think the 'entry' ones want to be acquire and the 'exit' ones release. osq_unlock() used release, but the equivalent code in osq_wait_next() used acquire.
They can't both have been correct!
Quoted 5 lines
But even more importantly, that code right now explicitly *states* that it needs a full barrier ("We need both ACQUIRE [..] and RELEASE"), so that *comment* would also have to be fixed with a why the ordering isn't as important as it states.
I left that comment alone - matching the xchg().
Even though there are now no fields to publish.
Quoted 2 lines
And finally: none of that will ever be noticeable on x86, since there are no memory orderings on atomics there: lock is all-or-nothing.
Indeed.
I don't have a little arm test system, never mind a big one where this would all show up.
Quoted 3 lines
End result: I'd love to see actual performance numbers if they exist. And any memory ordering change would require explaining why it's ok and some other architecture to test it.
This could even be one of the strange places where making the code slower actually speeds things up overall.osq_lock() is only used for contended sleep locks, and then not even for the first thread to be waiting.
If you get a lot of threads queued you really need to fix the locking!
Quoted 2 lines
Or am I missing something?
Probably the same thing as I am....
David
Quoted 2 lines
Linus
Quoted 24 lines · David Laight
Quoted 8 lines · On Mon, 7 Sep 2026 09:08:28 -0700 Linus Torvalds…
Quoted 3 lines · David Laight
I've fixed some broken/missing memory barriers but left the initial
xchg()when acquiring the lock as a full barrier, I think it could be relaxed.Well, it should almost certainly be at least an
atomic_cmpxchg_acquire(), since that's whatosq_wait_next()uses for the contention case.I'm not sure, but am no expert on acquire/release barriers. The 'fast path'
osq_lock()code only has one memory access so there isn't anything to sequence it with.
The important one is thesmp_wmb()a bit lower down that ensures the list tail (or head) is written before the back link.
When that was missing things went badly wrong.
(I think theWRITE_ONCE()could be astore_release()instead.)
Quoted 4 lines
The ACQUIRE semantics were added to ensure the '
node->next= NULL' assignment happened before thexchg().
That assignment goes away in patch 5.
But I'd want someone who really understands arm64 to comment.
These are preliminary results. I added osq_lock's to my mutual-exclusion selftest [1], which has not yet been reviewed. The test is based on v7.3-rc2.
For lock acquisition, I used:
preempt_disable();
while (!osq_lock(&el->mx_osq_lock.lock)) {
preempt_enable();
cond_resched();
preempt_disable();
}with the corresponding release:
osq_unlock(&el->mx_osq_lock.lock); preempt_enable();
Assuming that this is a correct use of the OSQ API, the OSQ test fails on a 160-CPU bare-metal Arm system. The same test passes on a 512-CPU AMD x86_64 system as expected, showing at least that the test is capable of passing.
I then applied this series, but the OSQ test still failed in the same way on Arm. I observed no new mutex or rwsem test failures on Arm, and the test continued to pass on the x86_64 system.
Thxs, Håkon
[1] https://lore.kernel.org/lkml/[email protected]/
Quoted 45 lines
Quoted 4 lines
It's a bit odd that the first initial xchg uses a different memory ordering than the later one. Maybe there's some reason for it.
I think the 'entry' ones want to be acquire and the 'exit' ones release.
osq_unlock()used release, but the equivalent code inosq_wait_next()used acquire.
They can't both have been correct!Quoted 5 lines
But even more importantly, that code right now explicitly *states* that it needs a full barrier ("We need both ACQUIRE [..] and RELEASE"), so that *comment* would also have to be fixed with a why the ordering isn't as important as it states.
I left that comment alone - matching the
xchg().
Even though there are now no fields to publish.Quoted 2 lines
And finally: none of that will ever be noticeable on x86, since there are no memory orderings on atomics there: lock is all-or-nothing.
Indeed.
I don't have a little arm test system, never mind a big one where this would all show up.Quoted 3 lines
End result: I'd love to see actual performance numbers if they exist. And any memory ordering change would require explaining why it's ok and some other architecture to test it.
This could even be one of the strange places where making the code slower actually speeds things up overall.
osq_lock()is only used for contended sleep locks, and then not even for the first thread to be waiting.
If you get a lot of threads queued you really need to fix the locking!Quoted 2 lines
Or am I missing something?
Probably the same thing as I am....
David
Quoted 2 lines
Linus
Waiman Long
reviewerRe: [PATCH v4 next 1/9] locking/osq_lock: Add some comments about how it works
In reply to David LaightQuoted 24 lines · David Laight
No code changes, just some extra explanations.
Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)kernel/locking/osq_lock.c +5
-3@@ -4,12 +4,33 @@#include <linux/osq_lock.h> /* -* An MCS like lock especially tailored for optimistic spinning for sleeping -* lock implementations (mutex, rwsem, etc). +* An MCS like spin lock especially tailored for optimistic spinning for +* sleeping lock implementations (mutex, rwsem, etc). +* Each CPU spins on a local variable to avoid cache-line bounces. * -* Using a single mcs node per CPU is safe because sleeping locks should not be +* The CPU that holds the osq_lock checks the mutex/rwsem, the other CPU spin +* in osq_lock() until either the osq_lock is obtained or the scheduler
kernel/locking/osq_lock.c line 22
Quoted 10 lines
kernel/locking/osq_lock.c +8
-0+* requests the process be preempted. +* +* Using a single osq node per CPU is safe because sleeping locks should not be * called from interrupt context and we have preemption disabled while * spinning. +* +* The osq_nodes for the spinning CPU are put on a double-linked (non circular) +* list. The list 'pointers' can either be the address of the osq_node or the +* associated CPU number, the CPU numbers are offset by one so that zero can +* be used like a NULL ponter.
Typo: "ponter" -> "pointer".
This description seems to describe the optimistic_spin_node structure after patch 2. So should you swap patch 1 and 2 to be more accurate?
Saying that 'pointers' can either be the osq_node address or the associated CPU numbers is a bit vague. next is the osq_node address and prev is the offset'ed CPU numbers. It is not really either one or the other. Other than that, the description looks good.
Cheers,
Longman
Re: [PATCH v4 next 2/9] locking/osq_lock: Save the cpu number for 'prev' not the node address
In reply to David LaightQuoted 119 lines · David Laight
The cpu number of
node->previs needed for both thevcpu_is_preempted()test and to updatelock->tail.
This saves reading the cache line for the other cpu's per-cpu data.The cpu member of
optimistic_spin_nodeis no longer needed.Merges patches 2 and 3 from v3.
Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)kernel/locking/osq_lock.c +13
-18@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);@@ -50,11 +50,6 @@ static inline int encode_cpu(int cpu_nr)return cpu_nr +1 ; } -static inline int node_cpu (struct optimistic_spin_node *node) -{ -return node->cpu -1 ; -} -static inline struct optimistic_spin_node *decode_cpu (int encoded_cpu_val) {int cpu_nr = encoded_cpu_val -1 ;@@ -114,13 +109,12 @@ osq_wait_next(struct optimistic_spin_queue *lock,bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev, *next; +struct optimistic_spin_node *prev_ptr, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev; node->locked =0 ; node->next =NULL ; - node->cpu = curr;/* * We need both ACQUIRE (pairs with corresponding RELEASE in @@ -128,11 +122,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)* the node fields we just initialised) semantics when updating * the lock tail. */ - old =atomic_xchg (&lock->tail, curr); -if (old ==OSQ_UNLOCKED_VAL ) + prev =atomic_xchg (&lock->tail, curr); +if (prev ==OSQ_UNLOCKED_VAL )return true; - prev =decode_cpu (old); + prev_ptr =decode_cpu (prev); node->prev = prev;/* @@ -147,7 +141,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)*/ smp_wmb (); -WRITE_ONCE (prev->next, node); +WRITE_ONCE (prev_ptr->next, node);/* * Normally @prev is untouchable after the above store; because at that @@ -165,7 +159,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* polling, be careful. */ if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node_cpu (node->prev)))) +vcpu_is_preempted (node->prev -1 )))return true;/* unqueue */ @@ -182,8 +176,8 @@ bool osq_lock(struct optimistic_spin_queue *lock)* cpu_relax() below implies a compiler barrier which would * prevent this comparison being optimized away. */ -if (data_race (prev->next) == node && -cmpxchg (&prev->next, node,NULL ) == node) +if (data_race (prev_ptr->next) == node && +cmpxchg (&prev_ptr->next, node,NULL ) == node)break ;/* @@ -201,6 +195,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* case its step-C will write us a new @node->prev pointer. */ prev =READ_ONCE (node->prev); + prev_ptr =decode_cpu (prev); }/* @@ -210,7 +205,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* back to @prev. */ - next =osq_wait_next (lock, node, prev->cpu); + next =osq_wait_next (lock, node, prev);if (!next)return false;
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 224
Quoted 8 lines
kernel/locking/osq_lock.c +1
-1@@ -223,7 +218,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)*/ WRITE_ONCE (next->prev, prev); -WRITE_ONCE (prev->next, next); +WRITE_ONCE (prev_ptr->next, next);return false; }
LKTM
Waiman Long
reviewerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David LaightQuoted 21 lines · David Laight
There is no need for separate
prev_cpuand locked members of structoptimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.When cancelling a lock request explicitly set
prev_cputo zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path'osq_lock()call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-)kernel/locking/osq_lock.c +0
-0
kernel/locking/osq_lock.c line 40
Quoted 7 lines
kernel/locking/osq_lock.c +0
-1@@ -35,7 +35,6 @@struct optimistic_spin_node {struct optimistic_spin_node *next; -int locked;/* 1 if lock acquired */ int prev;/* CPU number offset by 1 */ };
I think we should document the fact that prev=0 can be viewed as a marker that the osq lock has been acquired.
Quoted 9 lines
kernel/locking/osq_lock.c +0
-1@@ -113,7 +112,6 @@ bool osq_lock(struct optimistic_spin_queue *lock)int curr =encode_cpu (smp_processor_id ());int prev; - node->locked =0 ; node->next =NULL ;/*
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 161
Quoted 9 lines
kernel/locking/osq_lock.c +2
-3@@ -158,46 +156,47 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on * polling, be careful. */ -if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node->prev -1 ))) -return true; + prev =smp_cond_load_relaxed (&node->prev, !VAL ||need_resched () || +vcpu_is_preempted (VAL -1 ));
What is the purpose of assigning the return value to prev and immediately read node->prev into prev again in the next statement?
Quoted 4 lines
kernel/locking/osq_lock.c +1
-2-/* unqueue */ /* -* Step - A -- stabilize @prev +* Step - A
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 170
Quoted 10 lines
kernel/locking/osq_lock.c +6
-3* -* Undo our @prev->next assignment; this will make @prev's -* unlock()/unqueue() wait for a next pointer since @lock points to us -* (or later). +* Loop until either node->prev is zero (lock acquired) or we +* atomically change prev->next from node to NULL (stopping prev +* handing on the lock). +* Note that 'prev' can unlink itself concurrently with this +* test so that prev/prev_ptr can be stale, but since it +* is per-cpu data the memory can always be read.
I don't quite understand what you mean by "it is per-cpu data the memory can always be read".
Quoted 14 lines
kernel/locking/osq_lock.c +7
-5*/ -for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +for (;; prev =READ_ONCE (node->prev)) { +if (!prev) +/* Lock acquired */ +return true; + + prev_ptr =decode_cpu (prev); +
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 186
Quoted 10 lines
kernel/locking/osq_lock.c +2
-3if (data_race (prev_ptr->next) == node &&cmpxchg (&prev_ptr->next, node,NULL ) == node)break ;/* -* We can only fail the cmpxchg() racing against an unlock(), -* in which case we should observe @node->locked becoming -* true. +* 'prev' must have unlinked (or be in the process of unlinking) +* itself from the list.
Should we also moved the deleted comment about cpu_relax() to here?
Cheers,
Longman
Re: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
Quoted 23 lines · On Wed, 9 Sep 2026 14:01:59 -0400 Waiman Long <[email protected]> wrote:
Quoted 22 lines · David Laight
There is no need for separate
prev_cpuand locked members of structoptimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.When cancelling a lock request explicitly set
prev_cputo zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path'osq_lock()call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-)kernel/locking/osq_lock.c +0
-0@@ -35,7 +35,6 @@
Quoted 8 lines
kernel/locking/osq_lock.c line 40
Quoted 6 lines
kernel/locking/osq_lock.c +0
-1struct optimistic_spin_node {struct optimistic_spin_node *next; -int locked;/* 1 if lock acquired */ int prev;/* CPU number offset by 1 */ };I think we should document the fact that prev=0 can be viewed as a marker that the osq lock has been acquired.
I think that happens a bit later in the series.
Trying to keep the comments in step is quite hard work. That is part the reason why patch 1 partially describes 'where we are aiming at'.
I also got in a slight mess with pointers that are cpu numbers. Mostly trying to keep the comments concise.
Quoted 7 lines
Quoted 7 lines
kernel/locking/osq_lock.c +0
-1@@ -113,7 +112,6 @@ bool osq_lock(struct optimistic_spin_queue *lock)int curr =encode_cpu (smp_processor_id ());int prev; - node->locked =0 ; node->next =NULL ;
Quoted 13 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 161
Quoted 11 lines
kernel/locking/osq_lock.c +2
-3/* @@ -158,46 +156,47 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on * polling, be careful. */ -if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node->prev -1 ))) -return true; + prev =smp_cond_load_relaxed (&node->prev, !VAL ||need_resched () || +vcpu_is_preempted (VAL -1 ));What is the purpose of assigning the return value to prev and immediately read
node->previnto prev again in the next statement?
It isn't, it is only re-read when the code goes around the loop.
Quoted 34 lines
Quoted 4 lines
kernel/locking/osq_lock.c +1
-2-/* unqueue */ /* -* Step - A -- stabilize @prev +* Step - A kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 170
Quoted 10 lines
kernel/locking/osq_lock.c +6
-3* -* Undo our @prev->next assignment; this will make @prev's -* unlock()/unqueue() wait for a next pointer since @lock points to us -* (or later). +* Loop until either node->prev is zero (lock acquired) or we +* atomically change prev->next from node to NULL (stopping prev +* handing on the lock). +* Note that 'prev' can unlink itself concurrently with this +* test so that prev/prev_ptr can be stale, but since it +* is per-cpu data the memory can always be read. I don't quite understand what you mean by "it is per-cpu data the memory can always be read".
Quoted 18 lines
kernel/locking/osq_lock.c +7
-5*/ -for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +for (;; prev =READ_ONCE (node->prev)) { +if (!prev) +/* Lock acquired */ +return true; + + prev_ptr =decode_cpu (prev); +if (data_race (prev_ptr->next) == node &&cmpxchg (&prev_ptr->next, node,NULL ) == node)break ;
Quoted 8 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 186
Quoted 6 lines
kernel/locking/osq_lock.c +2
-3/* -* We can only fail the cmpxchg() racing against an unlock(), -* in which case we should observe @node->locked becoming -* true. +* 'prev' must have unlinked (or be in the process of unlinking) +* itself from the list. Should we also moved the deleted comment about
cpu_relax()to here?
I nearly gave that comment its own paragraph in the commit message. It was added when the data_race() was added (because of a KASAN splat). The real comment should have been that the initial compare is only there to make it less likely that an expensive atomic operation fails. The loop now has a READ_ONCE() at the top so nothing can be lifted out of the loop.
Thinking about it, I'm not even sure the test is worth while. If 'prev' isn't zero/NULL then we expect to be able to unlink ourselves from it - so the cmpxchg() is expected to succeed.
The extra compare only makes sense when there is a reasonably likely hood of the cmpxchg failing.
I could add a patch to remove it/them.
David
Quoted 4 lines
Cheers,
Longman
Quoted 40 lines · On Wed, 9 Sep 2026 14:15:03 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 22 lines · David Laight
Quoted 8 lines · On Mon, 7 Sep 2026 09:08:28 -0700 Linus Torvalds…
Quoted 3 lines · David Laight
I've fixed some broken/missing memory barriers but left the initial
xchg()when acquiring the lock as a full barrier, I think it could be relaxed.Well, it should almost certainly be at least an
atomic_cmpxchg_acquire(), since that's whatosq_wait_next()uses for the contention case.I'm not sure, but am no expert on acquire/release barriers. The 'fast path'
osq_lock()code only has one memory access so there isn't anything to sequence it with.
The important one is thesmp_wmb()a bit lower down that ensures the list tail (or head) is written before the back link.
When that was missing things went badly wrong.
(I think theWRITE_ONCE()could be astore_release()instead.)Quoted 4 lines
The ACQUIRE semantics were added to ensure the '
node->next= NULL' assignment happened before thexchg().
That assignment goes away in patch 5.
But I'd want someone who really understands arm64 to comment.These are preliminary results. I added osq_lock's to my mutual-exclusion selftest [1], which has not yet been reviewed. The test is based on v7.3-rc2.
For lock acquisition, I used:
preempt_disable(); while (!osq_lock(&el->mx_osq_lock.lock)) { preempt_enable(); cond_resched(); preempt_disable(); }
Quoted 6 lines
with the corresponding release:
osq_unlock(&el->mx_osq_lock.lock); preempt_enable();Assuming that this is a correct use of the OSQ API,
Looks reasonable.
I doubt the rwsem code ever stresses it that much.
Quoted 1 line
the OSQ test fails on a 160-CPU bare-metal Arm system.
The inter-cpu delays will definitely show up any memory ordering issues. I don't have access to anything of that nature.
Quoted 7 lines
The same test passes on a 512-CPU
AMDx86_64system as expected, showing at least that the test is capable of passing.I then applied this series, but the OSQ test still failed in the same way on Arm. I observed no new mutex or rwsem test failures on Arm, and the test continued to pass on the
x86_64system.
At least I haven't made it worse :-)
Might be worth removing all the _release and _acquire (so all the xchg become full barriers) to see if that makes a difference.
For testing you want the option of compiling a separate copy of the lock code into the module itself.
Then you can test changes to the lock code as well as changes to the test itself.
I did that for mul_u64_add_u64_div_u64() so I could test the 32bit code on x86-64.
It required some pretty horrid #defines - and I missed redefining EXPORT_SYMBOL() to be a no-op.
David
Quoted 54 lines
Thxs, Håkon
[1] https://lore.kernel.org/lkml/[email protected]/
Quoted 45 lines
Quoted 4 lines
It's a bit odd that the first initial xchg uses a different memory ordering than the later one. Maybe there's some reason for it.
I think the 'entry' ones want to be acquire and the 'exit' ones release.
osq_unlock()used release, but the equivalent code inosq_wait_next()used acquire.
They can't both have been correct!Quoted 5 lines
But even more importantly, that code right now explicitly *states* that it needs a full barrier ("We need both ACQUIRE [..] and RELEASE"), so that *comment* would also have to be fixed with a why the ordering isn't as important as it states.
I left that comment alone - matching the
xchg().
Even though there are now no fields to publish.Quoted 2 lines
And finally: none of that will ever be noticeable on x86, since there are no memory orderings on atomics there: lock is all-or-nothing.
Indeed.
I don't have a little arm test system, never mind a big one where this would all show up.Quoted 3 lines
End result: I'd love to see actual performance numbers if they exist. And any memory ordering change would require explaining why it's ok and some other architecture to test it.
This could even be one of the strange places where making the code slower actually speeds things up overall.
osq_lock()is only used for contended sleep locks, and then not even for the first thread to be waiting.
If you get a lot of threads queued you really need to fix the locking!Quoted 2 lines
Or am I missing something?
Probably the same thing as I am....
David
Quoted 2 lines
Linus
Waiman Long
reviewerQuoted 39 lines · Haakon Bugge
Quoted 20 lines · David Laight
Quoted 6 lines · On Mon, 7 Sep 2026 09:08:28 -0700 Linus Torvalds…
Quoted 2 lines · David Laight
I've fixed some broken/missing memory barriers but left the initial
xchg()when acquiring the lock as a full barrier, I think it could be relaxed.Well, it should almost certainly be at least an
atomic_cmpxchg_acquire(), since that's whatosq_wait_next()uses for the contention case.I'm not sure, but am no expert on acquire/release barriers. The 'fast path'
osq_lock()code only has one memory access so there isn't anything to sequence it with.
The important one is thesmp_wmb()a bit lower down that ensures the list tail (or head) is written before the back link.
When that was missing things went badly wrong.
(I think theWRITE_ONCE()could be astore_release()instead.)Quoted 4 lines
The ACQUIRE semantics were added to ensure the '
node->next= NULL' assignment happened before thexchg().
That assignment goes away in patch 5.
But I'd want someone who really understands arm64 to comment.These are preliminary results. I added osq_lock's to my mutual-exclusion selftest [1], which has not yet been reviewed. The test is based on v7.3-rc2.
For lock acquisition, I used:
preempt_disable(); while (!osq_lock(&el->mx_osq_lock.lock)) { preempt_enable(); cond_resched(); preempt_disable(); }with the corresponding release:
Quoted 7 lines
osq_unlock(&el->mx_osq_lock.lock); preempt_enable();Assuming that this is a correct use of the OSQ API, the OSQ test fails on a 160-CPU bare-metal Arm system. The same test passes on a 512-CPU AMD
x86_64system as expected, showing at least that the test is capable of passing.
osq_unlock() must provide the release barrier. I think the two "WRITE_ONCE(next->locked, 1)" should have been "smp_store_release(&next->locked, 1)". There is an xchg() call before the WRITE_ONCE's, but it is on a different cacheline so it may not apply.
Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
Thanks,
Longman
Waiman Long
reviewerQuoted 43 lines · Waiman Long
Quoted 42 lines · Haakon Bugge
Quoted 20 lines · David Laight
Quoted 6 lines · On Mon, 7 Sep 2026 09:08:28 -0700 Linus Torvalds…
Quoted 2 lines · David Laight
I've fixed some broken/missing memory barriers but left the initial
xchg()when acquiring the lock as a full barrier, I think it could be relaxed.Well, it should almost certainly be at least an
atomic_cmpxchg_acquire(), since that's whatosq_wait_next()uses for the contention case.I'm not sure, but am no expert on acquire/release barriers. The 'fast path'
osq_lock()code only has one memory access so there isn't anything to sequence it with.
The important one is thesmp_wmb()a bit lower down that ensures the list tail (or head) is written before the back link.
When that was missing things went badly wrong.
(I think theWRITE_ONCE()could be astore_release()instead.)Quoted 4 lines
The ACQUIRE semantics were added to ensure the '
node->next= NULL' assignment happened before thexchg().
That assignment goes away in patch 5.
But I'd want someone who really understands arm64 to comment.These are preliminary results. I added osq_lock's to my mutual-exclusion selftest [1], which has not yet been reviewed. The test is based on v7.3-rc2.
For lock acquisition, I used:
preempt_disable();
while (!osq_lock(&el->mx_osq_lock.lock)) {
preempt_enable();
cond_resched();
preempt_disable();
}with the corresponding release:
osq_unlock(&el->mx_osq_lock.lock);
preempt_enable();
Quoted 8 lines
Quoted 4 lines
Assuming that this is a correct use of the OSQ API, the OSQ test fails on a 160-CPU bare-metal Arm system. The same test passes on a 512-CPU AMD
x86_64system as expected, showing at least that the test is capable of passing.
osq_unlock()must provide the release barrier. I think the two "WRITE_ONCE(next->locked, 1)" should have been "smp_store_release(&next->locked, 1)". There is anxchg()call before the WRITE_ONCE's, but it is on a different cacheline so it may not apply.Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
osq_lock/unlock() is special in the sense that lock transfer can happen either in the lock cacheline or the node->locked cacheline. Try the patch below to see if it helps to pass the test.
Thanks,
Longman
kernel/locking/osq_lock.c +0 -0
@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/
- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||
vcpu_is_preempted(node_cpu(node->prev))))
return true;
@@ -224,11 +224,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node = this_cpu_ptr(&osq_node);
next = xchg(&node->next, NULL);
if (next) {
- WRITE_ONCE(next->locked, 1);
+ smp_store_release(&next->locked, 1);
return;
}
next = osq_wait_next(lock, node, OSQ_UNLOCKED_VAL); if (next)
- WRITE_ONCE(next->locked, 1);
+ smp_store_release(&next->locked, 1); }
Haakon Bugge
Tested-byQuoted 3 lines · Waiman Long
[snip]
Quoted 28 lines
Quoted 2 lines
Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
osq_lock/unlock() is special in the sense that lock transfer can happen either in the lock cacheline or the
node->lockedcacheline. Try the patch below to see if it helps to pass the test.Thanks,
Longmankernel/locking/osq_lock.c +0
-0@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/kernel/locking/osq_lock.c +1
-1- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||vcpu_is_preempted(node_cpu(node->prev))))
return true;kernel/locking/osq_lock.c +0
-0@@ -224,11 +224,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) {
kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234
Quoted 10 lines
kernel/locking/osq_lock.c +2
-2-WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 );return ; } next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 ); }
The test passes with the above patch:
# dmesg|grep mx
[ 7.010502] mx_test: osq_lock padding: 8 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 12.014144] mx_test: osq_lock padding: 16 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 17.017572] mx_test: osq_lock padding: 24 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 22.019636] mx_test: osq_lock padding: 32 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 27.022192] mx_test: osq_lock padding: 40 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 32.024907] mx_test: osq_lock padding: 48 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 37.026035] mx_test: osq_lock padding: 56 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds
I'll use David's advise about including the osq_lock code in my test, so I can test it as a module, which will be more thorough.
If you submit this patch, feel free to add:
Thxs, Håkon
Quoted 37 lines · On Thu, 10 Sep 2026 09:45:47 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 1 line · Waiman Long
[snip]
Quoted 33 lines
Quoted 2 lines
Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
osq_lock/unlock() is special in the sense that lock transfer can happen either in the lock cacheline or the
node->lockedcacheline. Try the patch below to see if it helps to pass the test.Thanks,
Longmankernel/locking/osq_lock.c +0
-0@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/kernel/locking/osq_lock.c +1
-1- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||vcpu_is_preempted(node_cpu(node->prev))))
return true;kernel/locking/osq_lock.c +1
-1@@ -224,11 +224,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) { -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 );return ; }
Quoted 7 lines
kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234
Quoted 5 lines
kernel/locking/osq_lock.c +1
-1next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 ); }The test passes with the above patch:
Do you know which part matters?
Quoted 10 lines
# dmesg|grep mx
[ 7.010502] mx_test: osq_lock padding: 8 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 12.014144] mx_test: osq_lock padding: 16 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 17.017572] mx_test: osq_lock padding: 24 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 22.019636] mx_test: osq_lock padding: 32 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 27.022192] mx_test: osq_lock padding: 40 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 32.024907] mx_test: osq_lock padding: 48 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 37.026035] mx_test: osq_lock padding: 56 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds
Quoted 2 lines
I'll use David's advise about including the
osq_lockcode in my test, so I can test it as a module, which will be more thorough.
At least with a build/run option...
Quoted 1 line
If you submit this patch, feel free to add:
I'll roll it into my patches (as 1/n).
David
Quoted 7 lines
Tested-by Håkon Bugge <[email protected]>Thxs, Håkon
Quoted 40 lines
Quoted 37 lines · On Thu, 10 Sep 2026 09:45:47 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 1 line · Waiman Long
[snip]
Quoted 33 lines
Quoted 2 lines
Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
osq_lock/unlock() is special in the sense that lock transfer can happen either in the lock cacheline or the
node->lockedcacheline. Try the patch below to see if it helps to pass the test.Thanks,
Longmankernel/locking/osq_lock.c +0
-0@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/kernel/locking/osq_lock.c +1
-1- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||vcpu_is_preempted(node_cpu(node->prev))))
return true;kernel/locking/osq_lock.c +1
-1@@ -224,11 +224,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) { -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 );return ; }
Quoted 7 lines
Quoted 7 lines
kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234
Quoted 5 lines
kernel/locking/osq_lock.c +1
-1next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 ); }The test passes with the above patch:
Confirming that a much more thorough test (permutating the test array size and padding) passed.
What concerns me is that I am unable to observe this bug testing mutexes or rwlocks.
Quoted 1 line
Do you know which part matters?
No, but now that I am able to test the OSQ locks as a module, I'll quickly find out.
Quoted 9 lines
Quoted 9 lines
# dmesg|grep mx
[ 7.010502] mx_test: osq_lock padding: 8 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 12.014144] mx_test: osq_lock padding: 16 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 17.017572] mx_test: osq_lock padding: 24 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 22.019636] mx_test: osq_lock padding: 32 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 27.022192] mx_test: osq_lock padding: 40 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 32.024907] mx_test: osq_lock padding: 48 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 37.026035] mx_test: osq_lock padding: 56 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds
Quoted 4 lines
Quoted 2 lines
I'll use David's advise about including the
osq_lockcode in my test, so I can test it as a module, which will be more thorough.At least with a build/run option...
Yes, I'll send a v2 of my mx_test including OSQ locks both as compiled-in and as a module. For the latter, I just did:
#include "osq_lock.c"
Quoted 3 lines
Quoted 1 line
If you submit this patch, feel free to add:
I'll roll it into my patches (as 1/n).
Be aware that Waiman's patch did not apply on the top of your series, so the testing is solely v7.3-rc2 plus Waiman's patch.
Thxs, Håkon
Quoted 48 lines · On Thu, 10 Sep 2026 11:31:19 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 40 lines
Quoted 37 lines · On Thu, 10 Sep 2026 09:45:47 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 1 line · Waiman Long
[snip]
Quoted 33 lines
Quoted 2 lines
Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
osq_lock/unlock() is special in the sense that lock transfer can happen either in the lock cacheline or the
node->lockedcacheline. Try the patch below to see if it helps to pass the test.Thanks,
Longmankernel/locking/osq_lock.c +0
-0@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/kernel/locking/osq_lock.c +1
-1- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||vcpu_is_preempted(node_cpu(node->prev))))
return true;kernel/locking/osq_lock.c +1
-1@@ -224,11 +224,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) { -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 );return ; }Quoted 7 lines
Quoted 7 lines
kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234
Quoted 5 lines
kernel/locking/osq_lock.c +1
-1next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 ); }The test passes with the above patch:
Quoted 5 lines
Confirming that a much more thorough test (permutating the test array size and padding) passed.
What concerns me is that I am unable to observe this bug testing mutexes or rwlocks.
The explicit test will be a lot more aggressive.
Especially if the lock hold time matters.
Quoted 5 lines
Quoted 2 lines
Do you know which part matters?
No, but now that I am able to test the OSQ locks as a module, I'll quickly find out.
That also means you can quickly check which _acquire/_release are definitely required.
I'm pretty sure that (with my patches) the initial xchg() at the top of osq_lock() only needs _acquire (_release was added to publish node->cpu). But I think it doesn't even need _acquire.osq_lock() itself relies on a data dependency.
Any concurrent osq_lock() relies on the smp_wmp() a bit further down (I think that could be a store_release).
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
# dmesg|grep mx
[ 7.010502] mx_test: osq_lock padding: 8 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 12.014144] mx_test: osq_lock padding: 16 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 17.017572] mx_test: osq_lock padding: 24 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 22.019636] mx_test: osq_lock padding: 32 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 27.022192] mx_test: osq_lock padding: 40 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 32.024907] mx_test: osq_lock padding: 48 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 37.026035] mx_test: osq_lock padding: 56 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 secondsI'll use David's advise about including the
osq_lockcode in my test, so I can test it as a module, which will be more thorough.
Quoted 6 lines
Quoted 1 line
At least with a build/run option...
Yes, I'll send a v2 of my
mx_testincluding OSQ locks both as compiled-in and as a module. For the latter, I just did:#include "
osq_lock.c"
I tried to rename everything just to be certain the correct functions are called.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
If you submit this patch, feel free to add:
I'll roll it into my patches (as 1/n).
Be aware that Waiman's patch did not apply on the top of your series, so the testing is solely v7.3-rc2 plus Waiman's patch.
The equivalent changes should be obvious. Note that I merged the unlock and lock-fail paths.
David
Quoted 4 lines
Thxs, Håkon
Quoted 54 lines · David Laight
Quoted 48 lines · On Thu, 10 Sep 2026 11:31:19 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 47 lines
Quoted 44 lines · On Thu, 10 Sep 2026 09:45:47 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 1 line · Waiman Long
[snip]
Quoted 28 lines
Quoted 2 lines
Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
osq_lock/unlock() is special in the sense that lock transfer can happen either in the lock cacheline or the
node->lockedcacheline. Try the patch below to see if it helps to pass the test.Thanks,
Longmankernel/locking/osq_lock.c +0
-0@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||vcpu_is_preempted(node_cpu(node->prev))))
return true;kernel/locking/osq_lock.c +0
-0@@ -224,11 +224,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) {kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234
Quoted 10 lines
kernel/locking/osq_lock.c +2
-2-WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 );return ; } next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 ); }The test passes with the above patch:
Quoted 8 lines
Quoted 5 lines
Confirming that a much more thorough test (permutating the test array size and padding) passed.
What concerns me is that I am unable to observe this bug testing mutexes or rwlocks.
The explicit test will be a lot more aggressive.
Especially if the lock hold time matters.
The algorithm is the same for all lock types. osq_lock failed, whereas mutex and rwlock, based on osq_lock, passes. Weird.
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Do you know which part matters?
No, but now that I am able to test the OSQ locks as a module, I'll quickly find out.
Only the first hunk is allegedly required:
@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/
- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
+ if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||
vcpu_is_preempted(node_cpu(node->prev))))
return true;I say allegedly because a passing test doesn't prove anything, it just gives a good indication that it is working.
Quoted 2 lines
That also means you can quickly check which _acquire/_release are definitely required.
I did it in another way. I just added an "atomic_xchg" test to my mutual exclusion framework.
Lock acquire:
while (atomic_xchg_acquire(&el->mx_atomic_xchg.lock, 1) != 0)
cpu_relax();Lock release:
atomic_set_release(&el->mx_atomic_xchg.lock, 0);
This passes. It also (obviously) passes with atomic_xchg() in the lock acquire. But the _release _is_ required in the lock release.
Quoted 3 lines
I'm pretty sure that (with my patches) the initial
xchg()at the top ofosq_lock()only needs_acquire(_releasewas added to publishnode->cpu). But I think it doesn't even need_acquire.
atomic_xchg_acquire() relaxes ordering as compared to atomic_xchg(). So, atomic_xchg_acquire() in the top of osq_lock() and atomic_try_cmpxchg_release() in osq_unlock() makes sense to me.
Quoted 19 lines
osq_lock()itself relies on a data dependency.
Any concurrentosq_lock()relies on thesmp_wmp()a bit further down (I think that could be astore_release).Quoted 15 lines
Quoted 14 lines
Quoted 10 lines
# dmesg|grep mx
[ 7.010502] mx_test: osq_lock padding: 8 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 12.014144] mx_test: osq_lock padding: 16 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 17.017572] mx_test: osq_lock padding: 24 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 22.019636] mx_test: osq_lock padding: 32 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 27.022192] mx_test: osq_lock padding: 40 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 seconds [ 32.024907] mx_test: osq_lock padding: 48 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.004 seconds [ 37.026035] mx_test: osq_lock padding: 56 result: SUCCESS sum: 0 elements: 1000 elapsed: 5.000 secondsQuoted 2 lines
I'll use David's advise about including the
osq_lockcode in my test, so I can test it as a module, which will be more thorough.At least with a build/run option...
Quoted 7 lines
Quoted 4 lines
Yes, I'll send a v2 of my
mx_testincluding OSQ locks both as compiled-in and as a module. For the latter, I just did:#include "
osq_lock.c"I tried to rename everything just to be certain the correct functions are called.
I put in a WARN_ON_ONCE() and it fired :-)
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
If you submit this patch, feel free to add:
I'll roll it into my patches (as 1/n).
Be aware that Waiman's patch did not apply on the top of your series, so the testing is solely v7.3-rc2 plus Waiman's patch.
The equivalent changes should be obvious. Note that I merged the unlock and lock-fail paths.
I like to change one thing at a time. FYI, I am unable to work on this until Monday. I'll re-apply your series and Waiman's first hunk and re-test.
Thxs, Håkon
Waiman Long
reviewerQuoted 52 lines · Haakon Bugge
Quoted 52 lines · David Laight
Quoted 47 lines · On Thu, 10 Sep 2026 11:31:19 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 38 lines
Quoted 35 lines · On Thu, 10 Sep 2026 09:45:47 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 1 line · Waiman Long
[snip]
Quoted 32 lines
Quoted 2 lines
Could you make that change to the existing code and rerun the test again on arm64 to see if it can pass?
osq_lock/unlock() is special in the sense that lock transfer can happen either in the lock cacheline or the
node->lockedcacheline. Try the patch below to see if it helps to pass the test.Thanks,
Longmankernel/locking/osq_lock.c +0
-0@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on
* polling, be careful.
*/- if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() ||vcpu_is_preempted(node_cpu(node->prev))))
return true;kernel/locking/osq_lock.c +1
-1@@ -224,11 +224,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) { -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 );return ; }Quoted 6 lines
Quoted 6 lines
kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234
Quoted 5 lines
kernel/locking/osq_lock.c +1
-1next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +smp_store_release (&next->locked,1 ); }The test passes with the above patch:
Confirming that a much more thorough test (permutating the test array size and padding) passed.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
What concerns me is that I am unable to observe this bug testing mutexes or rwlocks.
The explicit test will be a lot more aggressive.
Especially if the lock hold time matters.The algorithm is the same for all lock types.
osq_lockfailed, whereas mutex and rwlock, based onosq_lock, passes. Weird.
The purpose of osq_lock is for queuing the lock waiters with minimal contention on the lock cacheline. Even when the locking semantics isn't fully correct, it won't have an ill effect on the locking behavior of rwsem and mutex. We may have 2 waiters spinning on the lock cacheline instead of one, for instance.
Quoted 14 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Do you know which part matters?
No, but now that I am able to test the OSQ locks as a module, I'll quickly find out.
Only the first hunk is allegedly required:
(quoted diff) +1
-1@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock) * is implemented with a monitor-wait. vcpu_is_preempted() relies on * polling, be careful. */ - if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() || vcpu_is_preempted(node_cpu(node->prev)))) return true;
Quoted 2 lines
I say allegedly because a passing test doesn't prove anything, it just gives a good indication that it is working.
Yes, as said in my patch, the other two hunks are not really necessary for arm64 due to what how its barriers work.
Cheers,
Longman
On Thu, 10 Sep 2026 11:31:19 +0000
Haakon Bugge <[email protected]> wrote:
...
Quoted 2 lines
What concerns me is that I am unable to observe this bug testing mutexes or rwlocks.
That might be because there is only one osq_lock so you always get contention - whereas the other cases have separate locks. You do still need to hit the same array element for it to matter.
I'm not sure is makes much sense to have a lot of locks/counters.
David
On Thu, 10 Sep 2026 15:30:03 +0000
Haakon Bugge <[email protected]> wrote:
...
Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Do you know which part matters?
No, but now that I am able to test the OSQ locks as a module, I'll quickly find out.
Only the first hunk is allegedly required:
(quoted diff) +1
-1@@ -143,7 +143,7 @@ bool osq_lock(struct optimistic_spin_queue *lock) * is implemented with a monitor-wait. vcpu_is_preempted() relies on * polling, be careful. */ - if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || + if (smp_cond_load_acquire(&node->locked, VAL || need_resched() || vcpu_is_preempted(node_cpu(node->prev)))) return true;
Quoted 2 lines
I say allegedly because a passing test doesn't prove anything, it just gives a good indication that it is working.
Indeed.
Quoted 7 lines
Quoted 2 lines
That also means you can quickly check which _acquire/_release are definitely required.
I did it in another way. I just added an "
atomic_xchg" test to my mutual exclusion framework.Lock acquire:
Quoted 8 lines
while (atomic_xchg_acquire(&el->mx_atomic_xchg.lock, 1) != 0) cpu_relax();Lock release:
atomic_set_release(&el->mx_atomic_xchg.lock, 0);This passes. It also (obviously) passes with
atomic_xchg()in the lock acquire. But the_release_is_ required in the lock release.
Isn't that accessing another branch of the 'big union'?
So will be overlaying some other field - possibly the count itself? It seems unlikely that adding a barrier between the lock and unlock (or unlock and lock) will have any effect.
In any case I think it is enough to use smp_rmb(), smp_wmb() or smp_mb().
David
Quoted 5 lines · David Laight
On Thu, 10 Sep 2026 15:30:03 +0000
Haakon Bugge <[email protected]> wrote:
[snip]
Quoted 7 lines
Quoted 7 lines
I did it in another way. I just added an "
atomic_xchg" test to my mutual exclusion framework.Lock acquire:
while (atomic_xchg_acquire(&el->mx_atomic_xchg.lock, 1) != 0) cpu_relax();
Quoted 7 lines
Quoted 5 lines
Lock release:
atomic_set_release(&el->mx_atomic_xchg.lock, 0);This passes. It also (obviously) passes with
atomic_xchg()in the lock acquire. But the_release_is_ required in the lock release.Isn't that accessing another branch of the 'big union'?
Not sure I quite understand. During the run of one particular test, it is _only_ the corresponding struct in the union that is accessed.
Quoted 1 line
So will be overlaying some other field - possibly the count itself?
pahole gives:
struct {
atomic_t lock __attribute__((__aligned__(4))); /* 0 4 */
/* XXX 4 bytes hole, try to pack */
long int counter; /* 8 8 */
} __attribute__((__aligned__(8))) mx_atomic_xchg __attribute__((__aligned__(8))); /* 0 16 */Quoted 3 lines
It seems unlikely that adding a barrier between the lock and unlock (or unlock and lock) will have any effect.
In any case I think it is enough to usesmp_rmb(),smp_wmb()orsmp_mb().
That might well be, but the intent here was only to mimic the sequence atomic_xchg() in osq_lock() and corresponding atomic_try_cmpxchg_release() in osq_unlock().
Thxs, Håkon
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David LaightQuoted 35 lines · David Laight
Quoted 23 lines · On Wed, 9 Sep 2026 14:01:59 -0400 Waiman Long <[email protected]> wrote:
Quoted 22 lines · David Laight
There is no need for separate
prev_cpuand locked members of structoptimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.When cancelling a lock request explicitly set
prev_cputo zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path'osq_lock()call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-)kernel/locking/osq_lock.c +0
-0@@ -35,7 +35,6 @@Quoted 8 lines
kernel/locking/osq_lock.c line 40
Quoted 6 lines
kernel/locking/osq_lock.c +0
-1struct optimistic_spin_node {struct optimistic_spin_node *next; -int locked;/* 1 if lock acquired */ int prev;/* CPU number offset by 1 */ };I think we should document the fact that prev=0 can be viewed as a marker that the osq lock has been acquired.
Quoted 2 lines
I think that happens a bit later in the series. Trying to keep the comments in step is quite hard work.
Still, that's what you gotta do. Now you're asking us to try and reverse engineer things while reviewing.
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David Laightkernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 176
Quoted 9 lines · David Laight
kernel/locking/osq_lock.c +4
-5-for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +for (;; prev =READ_ONCE (node->prev)) { +if (!prev) +/* Lock acquired */ +return true;
This lacks {}, but also, is not ACQUIRE like the return you remove below. Did you want smp_acquire__after_ctrl_dep() in there?
Quoted 6 lines
kernel/locking/osq_lock.c +3
-0+ + prev_ptr =decode_cpu (prev); +if (data_race (prev_ptr->next) == node &&cmpxchg (&prev_ptr->next, node,NULL ) == node)break ;
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 189
Quoted 10 lines
kernel/locking/osq_lock.c +2
-5/* -* We can only fail the cmpxchg() racing against an unlock(), -* in which case we should observe @node->locked becoming -* true. +* 'prev' must have unlinked (or be in the process of unlinking) +* itself from the list. */ -if (smp_load_acquire (&node->locked)) -return true;
This is an ACQUIRE.
Quoted 10 lines
kernel/locking/osq_lock.c +0
-7cpu_relax (); - -/* -* Or we race against a concurrent unqueue()'s step-B, in which -* case its step-C will write us a new @node->prev pointer. -*/ - prev =READ_ONCE (node->prev); - prev_ptr =decode_cpu (prev); }
Re: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
Quoted 14 lines · On Mon, 14 Sep 2026 14:03:52 +0200 Peter Zijlstra <[email protected]> wrote:
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 176
Quoted 9 lines · David Laight
kernel/locking/osq_lock.c +4
-5-for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +for (;; prev =READ_ONCE (node->prev)) { +if (!prev) +/* Lock acquired */ +return true;This lacks {}, but also, is not ACQUIRE like the return you remove below. Did you want
smp_acquire__after_ctrl_dep()in there?
I need to get my head around the acquire/release logic.
I think the READ_ONCE() in the for(;;) needs to be an acquire (matching the smp_cond_load - which seems to have been wrong for ages). I may have decided that because the smp_cond_load was relaxed this read could be as well.
(They both need to be the same.)
Quoted 5 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 180
Quoted 5 lines
kernel/locking/osq_lock.c +3
-0+ + prev_ptr =decode_cpu (prev); + if (data_race(prev_ptr->next) == node &&
I'm also going to remove that 'optimisation' check. It is there to avoid the atomic below - but it only fails under race conditions.
David
Quoted 25 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 189
Quoted 12 lines
kernel/locking/osq_lock.c +2
-5cmpxchg(&prev_ptr->next, node, NULL) == node)break ;/* -* We can only fail the cmpxchg() racing against an unlock(), -* in which case we should observe @node->locked becoming -* true. +* 'prev' must have unlinked (or be in the process of unlinking) +* itself from the list. */ -if (smp_load_acquire (&node->locked)) -return true;This is an ACQUIRE.
Quoted 10 lines
kernel/locking/osq_lock.c +0
-7cpu_relax (); - -/* -* Or we race against a concurrent unqueue()'s step-B, in which -* case its step-C will write us a new @node->prev pointer. -*/ - prev =READ_ONCE (node->prev); - prev_ptr =decode_cpu (prev); }
Re: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to Peter ZijlstraQuoted 36 lines · On Mon, 14 Sep 2026 14:01:47 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 35 lines · David Laight
Quoted 31 lines · On Wed, 9 Sep 2026 14:01:59 -0400 Waiman Long <[email protected]> wrote:
Quoted 21 lines · David Laight
There is no need for separate
prev_cpuand locked members of structoptimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.When cancelling a lock request explicitly set
prev_cputo zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path'osq_lock()call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-)kernel/locking/osq_lock.c +0
-0kernel/locking/osq_lock.c line 40
Quoted 7 lines
kernel/locking/osq_lock.c +0
-1@@ -35,7 +35,6 @@struct optimistic_spin_node {struct optimistic_spin_node *next; -int locked;/* 1 if lock acquired */ int prev;/* CPU number offset by 1 */ };I think we should document the fact that prev=0 can be viewed as a marker that the osq lock has been acquired.
Quoted 5 lines
Quoted 2 lines
I think that happens a bit later in the series. Trying to keep the comments in step is quite hard work.
Still, that's what you gotta do. Now you're asking us to try and reverse engineer things while reviewing.
That's what I had to do before changing the code :-) It'll win a 'zero unless waiting' comment in the next version.
David
Quoted 16 lines · On Mon, 14 Sep 2026 11:46:02 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 3 lines · David Laight
On Thu, 10 Sep 2026 15:30:03 +0000
Haakon Bugge <[email protected]> wrote:[snip]
Quoted 10 lines
Quoted 10 lines
I did it in another way. I just added an "
atomic_xchg" test to my mutual exclusion framework.Lock acquire:
while (atomic_xchg_acquire(&el->mx_atomic_xchg.lock, 1) != 0) cpu_relax();Lock release:
atomic_set_release(&el->mx_atomic_xchg.lock, 0);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
This passes. It also (obviously) passes with
atomic_xchg()in the lock acquire. But the_release_is_ required in the lock release.Isn't that accessing another branch of the 'big union'?
Not sure I quite understand. During the run of one particular test, it is _only_ the corresponding struct in the union that is accessed.
I misunderstood what you'd done.
I thought you were adding an extra barrier to the osq_lock test. One of the possibilities is that it is speculating all the way through into the osq_unlock() code and the first read there is being done before the read of node->locked in smp_cond_load_relaxed(). If adding smp_rmb() between osq_lock() and osq_unlock() doesn't fix it then that isn't the problem.
This code really wants the cpu to speculate around the loop in smp_cond_load_xxx() so that it doesn't load any other cache lines. (I've not looked at the object code (any architecture) to try to guess what happens.)
David
Quoted 22 lines
Quoted 2 lines
So will be overlaying some other field - possibly the count itself?
pahole gives:
struct { atomic_t lock __attribute__((__aligned__(4))); /* 0 4 */ /* XXX 4 bytes hole, try to pack */ long int counter; /* 8 8 */ } __attribute__((__aligned__(8))) mx_atomic_xchg __attribute__((__aligned__(8))); /* 0 16 */Quoted 3 lines
It seems unlikely that adding a barrier between the lock and unlock (or unlock and lock) will have any effect.
In any case I think it is enough to usesmp_rmb(),smp_wmb()orsmp_mb().That might well be, but the intent here was only to mimic the sequence
atomic_xchg()inosq_lock()and correspondingatomic_try_cmpxchg_release()inosq_unlock().Thxs, Håkon
Peter Zijlstra
maintainerRe: [PATCH v4 next 2/9] locking/osq_lock: Save the cpu number for 'prev' not the node address
In reply to David LaightQuoted 31 lines · David Laight
The cpu number of
node->previs needed for both thevcpu_is_preempted()test and to updatelock->tail.
This saves reading the cache line for the other cpu's per-cpu data.The cpu member of
optimistic_spin_nodeis no longer needed.Merges patches 2 and 3 from v3.
Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)kernel/locking/osq_lock.c +2
-2@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
kernel/locking/osq_lock.c osq_wait_next(struct optimistic_spin_queue *lock, · line 114
Quoted 8 lines
kernel/locking/osq_lock.c +2
-2@@ -114,13 +109,12 @@ osq_wait_next(struct optimistic_spin_queue *lock,bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev, *next; +struct optimistic_spin_node *prev_ptr, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev;
I'm not a fan in the asymmetry of the naming, that is very confusing at best.
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David LaightQuoted 18 lines · David Laight
Quoted 14 lines · On Mon, 14 Sep 2026 14:03:52 +0200 Peter Zijlstra <[email protected]> wrote:
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 176
Quoted 9 lines · David Laight
kernel/locking/osq_lock.c +4
-5-for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +for (;; prev =READ_ONCE (node->prev)) { +if (!prev) +/* Lock acquired */ +return true;This lacks {}, but also, is not ACQUIRE like the return you remove below. Did you want
smp_acquire__after_ctrl_dep()in there?
Quoted 6 lines
I need to get my head around the acquire/release logic.
I think theREAD_ONCE()in the for(;;) needs to be an acquire (matching thesmp_cond_load- which seems to have been wrong for ages). I may have decided that because thesmp_cond_loadwas relaxed this read could be as well.
(They both need to be the same.)
You cannot change ordering and not mention in the changelog. If you're unsure, retain existing ordering and make note in changelog.
Quoted 10 lines
Quoted 5 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 180
Quoted 5 lines
kernel/locking/osq_lock.c +3
-0+ + prev_ptr =decode_cpu (prev); + if (data_race(prev_ptr->next) == node &&I'm also going to remove that 'optimisation' check. It is there to avoid the atomic below - but it only fails under race conditions.
Quoted 4 lines
David
Quoted 2 lines
Quoted 2 lines
kernel/locking/osq_lock.c +0
-0cmpxchg(&prev_ptr->next, node, NULL) == node)break ;
Someone will put it back eventually. It is typically good form to have this pre check. It improved the contended behaviour of these loops. The regular load can be a forward, while CAS requires an exclusive load -- or something along those lines.
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David LaightQuoted 5 lines · David Laight
There is no need for separate
prev_cpuand locked members of structoptimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.
Quoted 8 lines
When cancelling a lock request explicitly set
prev_cputo zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path'osq_lock()call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.Signed-off-by David Laight <[email protected]>---
kernel/locking/osq_lock.c +5 -4
@@ -170,10 +170,11 @@ bool osq_lock(struct optimistic_spin_que* is per-cpu data the memory can always be read. */ -for (;; prev =READ_ONCE (node->prev)) { -if (!prev) -/* Lock acquired */ +for (;;) { +if (!prev) { +smp_acquire__after_ctrl_dep ();return true; + } prev_ptr =decode_cpu (prev);
@@ -185,8 +186,8 @@ bool osq_lock(struct optimistic_spin_que* 'prev' must have unlinked (or be in the process of unlinking) * itself from the list. */ -cpu_relax (); + prev =READ_ONCE (node->prev); }/*
Re: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to Peter ZijlstraQuoted 26 lines · On Tue, 15 Sep 2026 10:40:47 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 18 lines · David Laight
Quoted 14 lines · On Mon, 14 Sep 2026 14:03:52 +0200 Peter Zijlstra <[email protected]> wrote:
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 176
Quoted 9 lines · David Laight
kernel/locking/osq_lock.c +4
-5-for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +for (;; prev =READ_ONCE (node->prev)) { +if (!prev) +/* Lock acquired */ +return true;This lacks {}, but also, is not ACQUIRE like the return you remove below. Did you want
smp_acquire__after_ctrl_dep()in there?Quoted 6 lines
I need to get my head around the acquire/release logic.
I think theREAD_ONCE()in the for(;;) needs to be an acquire (matching thesmp_cond_load- which seems to have been wrong for ages). I may have decided that because thesmp_cond_loadwas relaxed this read could be as well.
(They both need to be the same.)
Quoted 2 lines
You cannot change ordering and not mention in the changelog. If you're unsure, retain existing ordering and make note in changelog.
The existing ordering seems to have been broken.
I'll rework it for the next version.
(It rather depends on what happens to the patch to change the smp_cond_load.)
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 180
Quoted 6 lines
kernel/locking/osq_lock.c +3
-0+ + prev_ptr =decode_cpu (prev); + if (data_race(prev_ptr->next) == node &&I'm also going to remove that 'optimisation' check. It is there to avoid the atomic below - but it only fails under race conditions.
David
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 182
Quoted 2 lines
kernel/locking/osq_lock.c +0
-0cmpxchg(&prev_ptr->next, node, NULL) == node)break ;Someone will put it back eventually. It is typically good form to have this pre check. It improved the contended behaviour of these loops. The regular load can be a forward, while CAS requires an exclusive load -- or something along those lines.
I know. I think it really depends on the likelyhood of the compare failing. It there is a reasonable chance of it failing you definitely need the compare.
David
Re: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to Peter ZijlstraQuoted 19 lines · On Tue, 15 Sep 2026 10:50:13 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 13 lines · David Laight
There is no need for separate
prev_cpuand locked members of structoptimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.When cancelling a lock request explicitly set
prev_cputo zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path'osq_lock()call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.Signed-off-by David Laight <[email protected]>---
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_que · line 175
Quoted 9 lines
kernel/locking/osq_lock.c +3
-3@@ -170,10 +170,11 @@ bool osq_lock(struct optimistic_spin_que* is per-cpu data the memory can always be read. */ -for (;; prev =READ_ONCE (node->prev)) { -if (!prev) -/* Lock acquired */ +for (;;) { +if (!prev) { +smp_acquire__after_ctrl_dep ();
Do you think the equivalent might fix the existing code?
I think it is only going to matter if there isn't another read barrier before the osq_unlock() - which is unlikely except for the test program.
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_que · line 190
Quoted 12 lines
kernel/locking/osq_lock.c +2
-1return true; + } prev_ptr =decode_cpu (prev);@@ -185,8 +186,8 @@ bool osq_lock(struct optimistic_spin_que* 'prev' must have unlinked (or be in the process of unlinking) * itself from the list. */ -cpu_relax (); + prev =READ_ONCE (node->prev);
A matter of taste, I prefer it at the top.
David
Quoted 3 lines
kernel/locking/osq_lock.c +0
-0}/*
Peter Zijlstra
maintainerRe: [PATCH v4 next 2/9] locking/osq_lock: Save the cpu number for 'prev' not the node address
In reply to Peter ZijlstraQuoted 31 lines · Peter Zijlstra
Quoted 29 lines · David Laight
The cpu number of
node->previs needed for both thevcpu_is_preempted()test and to updatelock->tail.
This saves reading the cache line for the other cpu's per-cpu data.The cpu member of
optimistic_spin_nodeis no longer needed.Merges patches 2 and 3 from v3.
Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)kernel/locking/osq_lock.c +2
-2@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
Quoted 12 lines
kernel/locking/osq_lock.c osq_wait_next(struct optimistic_spin_queue *lock, · line 114
Quoted 9 lines
kernel/locking/osq_lock.c +2
-2@@ -114,13 +109,12 @@ osq_wait_next(struct optimistic_spin_queue *lock,bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev, *next; +struct optimistic_spin_node *prev_ptr, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev;I'm not a fan in the asymmetry of the naming, that is very confusing at best.
Maybe something like so?
---
kernel/locking/osq_lock.c +12 -17
@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev_cpu;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
@@ -50,11 +50,6 @@ static inline int encode_cpu(int cpu_nr)return cpu_nr +1 ; } -static inline int node_cpu (struct optimistic_spin_node *node) -{ -return node->cpu -1 ; -} -static inline struct optimistic_spin_node *decode_cpu (int encoded_cpu_val) {int cpu_nr = encoded_cpu_val -1 ;
@@ -116,11 +111,10 @@ bool osq_lock(struct optimistic_spin_questruct optimistic_spin_node *node =this_cpu_ptr (&osq_node);struct optimistic_spin_node *prev, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev_cpu; node->locked =0 ; node->next =NULL ; - node->cpu = curr;/* * We need both ACQUIRE (pairs with corresponding RELEASE in
@@ -128,12 +122,12 @@ bool osq_lock(struct optimistic_spin_que* the node fields we just initialised) semantics when updating * the lock tail. */ - old =atomic_xchg (&lock->tail, curr); -if (old ==OSQ_UNLOCKED_VAL ) + prev_cpu =atomic_xchg (&lock->tail, curr); +if (prev_cpu ==OSQ_UNLOCKED_VAL )return true; - prev =decode_cpu (old); - node->prev = prev; + prev =decode_cpu (prev_cpu); + node->prev_cpu = prev_cpu;/* * osq_lock() unqueue
@@ -165,7 +159,7 @@ bool osq_lock(struct optimistic_spin_que* polling, be careful. */ if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node_cpu (node->prev)))) +vcpu_is_preempted (node->prev_cpu -1 )))return true;/* unqueue */
@@ -200,7 +194,8 @@ bool osq_lock(struct optimistic_spin_que* Or we race against a concurrent unqueue()'s step-B, in which * case its step-C will write us a new @node->prev pointer. */ - prev =READ_ONCE (node->prev); + prev_cpu =READ_ONCE (node->prev_cpu); + prev =decode_cpu (prev_cpu); }/*
@@ -210,7 +205,7 @@ bool osq_lock(struct optimistic_spin_que* back to @prev. */ - next =osq_wait_next (lock, node, prev->cpu); + next =osq_wait_next (lock, node, prev_cpu);if (!next)return false;
@@ -222,7 +217,7 @@ bool osq_lock(struct optimistic_spin_que* it will wait in Step-A. */ -WRITE_ONCE (next->prev, prev); +WRITE_ONCE (next->prev_cpu, prev_cpu);WRITE_ONCE (prev->next, next);return false;
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David LaightQuoted 25 lines · David Laight
Quoted 22 lines · On Tue, 15 Sep 2026 10:50:13 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 13 lines · David Laight
There is no need for separate
prev_cpuand locked members of structoptimistic_spin_node.
Using a single field simplifies the code slightly.
It also removes any possibility of the two values being out of sync.When cancelling a lock request explicitly set
prev_cputo zero. Nothing actually looks at the field, but it means that it will be zero after a subsequent 'fast path'osq_lock()call making things consistent. The cache line is likely to be dirty (or be dirtied) so there shouldn't be a performance hit.Signed-off-by David Laight <[email protected]>---
kernel/locking/osq_lock.c +0
-0@@ -170,10 +170,11 @@ bool osq_lock(struct optimistic_spin_que* is per-cpu data the memory can always be read. */
Quoted 8 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_que · line 175
Quoted 6 lines
kernel/locking/osq_lock.c +3
-3-for (;; prev =READ_ONCE (node->prev)) { -if (!prev) -/* Lock acquired */ +for (;;) { +if (!prev) { +smp_acquire__after_ctrl_dep ();Do you think the equivalent might fix the existing code?
I am not sure what you and waiman are on about. The wall of text with that commit did not contain anything resembling a solid description of any problems that might need fixing.
Re: [PATCH v4 next 2/9] locking/osq_lock: Save the cpu number for 'prev' not the node address
In reply to Peter ZijlstraQuoted 45 lines · On Tue, 15 Sep 2026 12:26:02 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 31 lines · Peter Zijlstra
Quoted 29 lines · David Laight
The cpu number of
node->previs needed for both thevcpu_is_preempted()test and to updatelock->tail.
This saves reading the cache line for the other cpu's per-cpu data.The cpu member of
optimistic_spin_nodeis no longer needed.Merges patches 2 and 3 from v3.
Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)kernel/locking/osq_lock.c +2
-2@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);Quoted 12 lines
kernel/locking/osq_lock.c osq_wait_next(struct optimistic_spin_queue *lock, · line 114
Quoted 9 lines
kernel/locking/osq_lock.c +2
-2@@ -114,13 +109,12 @@ osq_wait_next(struct optimistic_spin_queue *lock,bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev, *next; +struct optimistic_spin_node *prev_ptr, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev;I'm not a fan in the asymmetry of the naming, that is very confusing at best.
Quoted 1 line
Maybe something like so?
I was trying to avoid changing lines that weren't affected by the patch. So I left the original variable names alone.
Every time I come back to this code it makes my head hurt.
Perhaps I should do an early patch to rename everything to this/prev/next_cpu/ptr (although maybe without the _ptr).
At one point I considered using OSQ_UNLOCKED_VAL (instead of 0) for node->prev/next_cpu but decided against it because it make the code even harder to read.
I think it was added when the list tail was changed from a pointer to the cpu number to reduce its size - the value changed from NULL to 0. Having a non-zero initialiser (eg -1) is just asking for trouble!
David
Quoted 95 lines
---
kernel/locking/osq_lock.c +12
-17@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev_cpu;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);@@ -50,11 +50,6 @@ static inline int encode_cpu(int cpu_nr)return cpu_nr +1 ; } -static inline int node_cpu (struct optimistic_spin_node *node) -{ -return node->cpu -1 ; -} -static inline struct optimistic_spin_node *decode_cpu (int encoded_cpu_val) {int cpu_nr = encoded_cpu_val -1 ;@@ -116,11 +111,10 @@ bool osq_lock(struct optimistic_spin_questruct optimistic_spin_node *node =this_cpu_ptr (&osq_node);struct optimistic_spin_node *prev, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev_cpu; node->locked =0 ; node->next =NULL ; - node->cpu = curr;/* * We need both ACQUIRE (pairs with corresponding RELEASE in @@ -128,12 +122,12 @@ bool osq_lock(struct optimistic_spin_que* the node fields we just initialised) semantics when updating * the lock tail. */ - old =atomic_xchg (&lock->tail, curr); -if (old ==OSQ_UNLOCKED_VAL ) + prev_cpu =atomic_xchg (&lock->tail, curr); +if (prev_cpu ==OSQ_UNLOCKED_VAL )return true; - prev =decode_cpu (old); - node->prev = prev; + prev =decode_cpu (prev_cpu); + node->prev_cpu = prev_cpu;/* * osq_lock() unqueue @@ -165,7 +159,7 @@ bool osq_lock(struct optimistic_spin_que* polling, be careful. */ if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node_cpu (node->prev)))) +vcpu_is_preempted (node->prev_cpu -1 )))return true;/* unqueue */ @@ -200,7 +194,8 @@ bool osq_lock(struct optimistic_spin_que* Or we race against a concurrent unqueue()'s step-B, in which * case its step-C will write us a new @node->prev pointer. */ - prev =READ_ONCE (node->prev); + prev_cpu =READ_ONCE (node->prev_cpu); + prev =decode_cpu (prev_cpu); }/* @@ -210,7 +205,7 @@ bool osq_lock(struct optimistic_spin_que* back to @prev. */ - next =osq_wait_next (lock, node, prev->cpu); + next =osq_wait_next (lock, node, prev_cpu);if (!next)return false;@@ -222,7 +217,7 @@ bool osq_lock(struct optimistic_spin_que* it will wait in Step-A. */ -WRITE_ONCE (next->prev, prev); +WRITE_ONCE (next->prev_cpu, prev_cpu);WRITE_ONCE (prev->next, next);return false;
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to David Laightkernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 195
Quoted 4 lines · David Laight
kernel/locking/osq_lock.c +4
-0+/* +* If 'prev' tries to remove itself from the list before we write +* a new value to prev->next it will spin in osq_wait_next(). +*/
This comment pertains to what?
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 198
Quoted 2 lines
kernel/locking/osq_lock.c +2
-0+/* Invalidate prev_cpu matching osq_unlock() */ + node->prev =0 ;
I'm confused, osq_unlock() does no such thing.
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
Quoted 9 lines · Peter Zijlstra
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 195
Quoted 4 lines · David Laight
kernel/locking/osq_lock.c +4
-0+/* +* If 'prev' tries to remove itself from the list before we write +* a new value to prev->next it will spin in osq_wait_next(). +*/ This comment pertains to what?
Quoted 4 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 198
Quoted 2 lines
kernel/locking/osq_lock.c +2
-0+/* Invalidate prev_cpu matching osq_unlock() */ + node->prev =0 ;I'm confused,
osq_unlock()does no such thing.
The next patch adds a comment stating that this store is not strictly needed. And only serves to ensure node->prev ends up being 0 in all paths.
That comment *should* obviously have been here. Also, I would *much* rather have this store at the top, next to node->next = NULL; That is much saner.
Re: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
On Tue, 15 Sep 2026 15:15:10 +0200
Peter Zijlstra <[email protected]> wrote:
I'm glad you are actually going through these patches.
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · David Laight
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 195
Quoted 5 lines
kernel/locking/osq_lock.c +4
-0+/* +* If 'prev' tries to remove itself from the list before we write +* a new value to prev->next it will spin in osq_wait_next(). +*/ This comment pertains to what?
IIRC, perhaps it needs:
Having written prev->next = NULL ...
Quoted 5 lines
Quoted 5 lines
kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 198
Quoted 3 lines
kernel/locking/osq_lock.c +2
-0+/* Invalidate prev_cpu matching osq_unlock() */ + node->prev =0 ;I'm confused,
osq_unlock()does no such thing.
osq_unlock() sets it to zero on the node that it passes the lock to (since that node is now at the head of the list).
The comment probably need a few more words.
Quoted 3 lines
The next patch adds a comment stating that this store is not strictly needed. And only serves to ensure
node->prevends up being 0 in all paths.
I need to look again (the system with the patches is off) to see why it might be non-zero after this patch.
The original unlock code just set 'next->locked' and left 'next->prev' pointing into the abyss.
(One of the AI bots reported something about locked and prev getting out of sync.)
Quoted 3 lines
That comment *should* obviously have been here. Also, I would *much* rather have this store at the top, next to
node->next= NULL; That is much saner.
Part of the original reason I started these patches was to remove the writes to the current cpu's 'node' in the fast path acquire. That completely saves reading the cache line.
It also means the initial xchg need only have acquire semantics.
David
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
Quoted 9 lines · David Laight
Quoted 3 lines
That comment *should* obviously have been here. Also, I would *much* rather have this store at the top, next to
node->next= NULL; That is much saner.Part of the original reason I started these patches was to remove the writes to the current cpu's 'node' in the fast path acquire. That completely saves reading the cache line.
It also means the initial xchg need only have acquire semantics.
That makes no sense... you *have* to write node->prev_cpu, and since you get to have the line dirty for that, a few more stores to the same line are 'free', no?
Trying to make the code keep invariants like that also makes it needlessly convoluted.
If you need a store -- and you do, then you might as well write the whole thing.
And if you think it makes a difference, your changelogs completely lack any factual data to that effect. That is, there are no numbers. And without numbers, we're just not doing this.
Peter Zijlstra
maintainerRe: [PATCH v4 next 4/9] locking/osq_lock: Delete 'fast path' code from osq_unlock()
In reply to David LaightQuoted 10 lines · David Laight
The 'fast path' code in
osq_unlock()is pretty much exactly the same as the first pass of the loop inosq_wait_next()except that it doesn't have the optimisation to avoid the locked RMW when not the tail of the list.
So just callosq_wait_next().Move the assignment
next->prev_cpu=old_cpuintoosq_wait_next()as it is always the next line.
Renameosq_wait_next()toosq_unlink_from_next()since that is what is does.
Quoted 9 lines
Change
osq_wait_next()to useatomic_cmpxchg_release()(not_acquire) onlock->tail.
This is whatosq_unlock()did and seems right to me.Add an
smp_wmb()before the 'prev->next= next' assignment when cancelling a lock. The previous 'next->prev= prev' assignment lets the 'prev' cpu complete an unlocking sequence and do its 'next->prev= prev' assignment first - corrupting the list.
This is horrible. From the Changelog alone this should be at least 4 patches.
I've tried to recreate this one patch and ended up with 5.
Please, small steps, no random changes and definitely no maybe/perhaps like comments when talking about memory barriers.
See here:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=locking/osq
Waiman Long
reviewerRe: [PATCH v4 next 2/9] locking/osq_lock: Save the cpu number for 'prev' not the node address
In reply to Peter ZijlstraQuoted 48 lines · Peter Zijlstra
Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · David Laight
The cpu number of
node->previs needed for both thevcpu_is_preempted()test and to updatelock->tail.
This saves reading the cache line for the other cpu's per-cpu data.The cpu member of
optimistic_spin_nodeis no longer needed.Merges patches 2 and 3 from v3.
Signed-off-by David Laight <[email protected]>--- kernel/locking/osq_lock.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)kernel/locking/osq_lock.c +2
-2@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev;/* CPU number offset by 1 */ };Quoted 12 lines
kernel/locking/osq_lock.c osq_wait_next(struct optimistic_spin_queue *lock, · line 114
Quoted 10 lines
kernel/locking/osq_lock.c +2
-2static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);@@ -114,13 +109,12 @@ osq_wait_next(struct optimistic_spin_queue *lock,bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev, *next; +struct optimistic_spin_node *prev_ptr, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev;I'm not a fan in the asymmetry of the naming, that is very confusing at best.
Maybe something like so?
---
kernel/locking/osq_lock.c line 40
Quoted 9 lines
kernel/locking/osq_lock.c +2
-2@@ -34,9 +34,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev_cpu;/* CPU number offset by 1 */ };
Yes, I like the prev_cpu name better than prev. It makes the code easier to understand.
Cheers,
Longman
Re: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
In reply to Peter ZijlstraQuoted 7 lines · On Tue, 15 Sep 2026 16:01:52 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines · David Laight
Quoted 3 lines
That comment *should* obviously have been here. Also, I would *much* rather have this store at the top, next to
node->next= NULL; That is much saner.
Quoted 8 lines
Quoted 4 lines
Part of the original reason I started these patches was to remove the writes to the current cpu's 'node' in the fast path acquire. That completely saves reading the cache line.
It also means the initial xchg need only have acquire semantics.That makes no sense... you *have* to write
node->prev_cpu, and since you get to have the line dirty for that, a few more stores to the same line are 'free', no?
But you don't always have to do that write.
If the lock isn't held then you only have to update osq_lock->tail to contain the current cpu number.
The per-cpu data remains zero - the same as when unlocked (and initialised).
David
Quoted 10 lines
Trying to make the code keep invariants like that also makes it needlessly convoluted.
If you need a store -- and you do, then you might as well write the whole thing.
And if you think it makes a difference, your changelogs completely lack any factual data to that effect. That is, there are no numbers. And without numbers, we're just not doing this.
Peter Zijlstra
maintainerRe: [PATCH v4 next 3/9] locking/osq_lock: Set prev_cpu=0 instead of locked=1
Quoted 15 lines · David Laight
Quoted 12 lines · On Tue, 15 Sep 2026 16:01:52 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 9 lines · David Laight
Quoted 3 lines
That comment *should* obviously have been here. Also, I would *much* rather have this store at the top, next to
node->next= NULL; That is much saner.Part of the original reason I started these patches was to remove the writes to the current cpu's 'node' in the fast path acquire. That completely saves reading the cache line.
It also means the initial xchg need only have acquire semantics.
Quoted 8 lines
Quoted 3 lines
That makes no sense... you *have* to write
node->prev_cpu, and since you get to have the line dirty for that, a few more stores to the same line are 'free', no?But you don't always have to do that write.
If the lock isn't held then you only have to updateosq_lock->tailto contain the current cpu number.
The per-cpu data remains zero - the same as when unlocked (and initialised).
Then do all that in a separate patch.
This series as a whole is a giant trainwreck. The patches do too much and say too little.
Peter Zijlstra
maintainerRe: [PATCH v4 next 4/9] locking/osq_lock: Delete 'fast path' code from osq_unlock()
In reply to Peter ZijlstraQuoted 3 lines · Peter Zijlstra
See here:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=locking/osq
I've updated this, it now includes:
- your patches 2,3,6
- my rewrite for your patch 4, in 5 parts
I've re-shuffled things a bit to move all the Fixes to the front.
It builds and boots. I'll let it sit there for a little bit for the robots to chew on. Lacking robot screams I'll move it into tip/locking/core.
If you want to optimize that fast path, please do small patches on top of this.
Don't bother posting stuff like 8 and 9. Esp. 9 is just wrong, CPUs go in a FIFO queue, so the 'new' CPU is at the tail.
[tip: locking/core] locking/osq: Use CPU number for 'next' pointer
In reply to David LaightThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 8e7c6217be7f8c016975f5b0954f6736b9ac42cf Gitweb: https://git.kernel.org/tip/8e7c6217be7f8c016975f5b0954f6736b9ac42cf Author: David Laight <[email protected]> AuthorDate: Mon, 07 Sep 2026 09:41:30 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Nov 2026 11:16:24 +01:00
locking/osq: Use CPU number for 'next' pointer
There is only one write done through node->next (setting prev) when 'node' is being removed. So the code can consistently use the CPU numbers pretty much throughout (this was suggested by Linus a while back).
This reduces struct optimistic_spin_node to 8 bytes. This is currently padded out to a cache line which is silly. Change to be __aligned(8) so that it isn't split between cache lines.
Accesses to 'other CPU' data are very limited and only happen during the enqueue and dequeue operation.
[ peterz: Rewrote it completely ]
--- kernel/locking/osq_lock.c | 49 ++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 25 deletions(-)
kernel/locking/osq_lock.c +24 -25
@@ -13,11 +13,11 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next; +int next_cpu;/* CPU number offset by 1 */ int prev_cpu;/* CPU number offset by 1 */ -}; +}__aligned(8) ; -static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node); +static DEFINE_PER_CPU(struct optimistic_spin_node, osq_node);/* * We use the value 0 to represent "no CPU", thus the encoded value
@@ -44,22 +44,21 @@ static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)* For osq_unlock() there is never a previous node and old_cpu is * set to OSQ_UNLOCKED_VAL. */ -static inline struct optimistic_spin_node * -osq_wait_unlink_next (struct optimistic_spin_queue *lock, -int old_cpu) +static inline int +osq_wait_unlink_next (struct optimistic_spin_queue *lock,int old_cpu) {struct optimistic_spin_node *next, *node =this_cpu_ptr (&osq_node); -int curr =encode_cpu (smp_processor_id ()); +int next_cpu, node_cpu =encode_cpu (smp_processor_id ());for (;;) { -if (atomic_read (&lock->tail) == curr && -atomic_cmpxchg_release (&lock->tail, curr, old_cpu) == curr) { +if (atomic_read (&lock->tail) == node_cpu && +atomic_cmpxchg_release (&lock->tail, node_cpu, old_cpu) == node_cpu) {/* * We were the last queued, we moved @lock back. @prev * will now observe @lock and will complete its * unlock()/unqueue(). */ -return NULL ; +return 0 ; }/*
@@ -72,9 +71,9 @@ osq_wait_unlink_next(struct optimistic_spin_queue *lock,* wait for either @lock to point to us, through its Step-B, or * wait for a new @node->next from its Step-C. */ -if (node->next) { - next =xchg (&node->next,NULL ); -if (next) +if (node->next_cpu) { + next_cpu =xchg (&node->next_cpu,0 ); +if (next_cpu)break ; }
@@ -84,16 +83,16 @@ osq_wait_unlink_next(struct optimistic_spin_queue *lock,/* * Unlink @node from @next. */ + next =decode_cpu (next_cpu);WRITE_ONCE (next->prev_cpu, old_cpu); -return next; +return next_cpu; }/* * Set prev->next; this is the counterpart of osq_wait_unlink_next() in that * by setting ->next the wait is terminated and progress is resumed. */ -static inline void osq_link_next (struct optimistic_spin_node *prev, -struct optimistic_spin_node *next) +static inline void osq_link_next (struct optimistic_spin_node *prev,int next_cpu) {/* * Suppose:
@@ -128,17 +127,17 @@ static inline void osq_link_next(struct optimistic_spin_node *prev,* * Which would result in list corruption. */ -smp_store_release (&prev->next, next); +smp_store_release (&prev->next_cpu, next_cpu); }bool osq_lock (struct optimistic_spin_queue *lock) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node); -struct optimistic_spin_node *prev, *next; -int curr =encode_cpu (smp_processor_id ()); -int prev_cpu; +int node_cpu =encode_cpu (smp_processor_id ()); +struct optimistic_spin_node *prev; +int prev_cpu, next_cpu; - node->next =NULL ; + node->next_cpu =0 ;/* * We need both ACQUIRE (pairs with corresponding RELEASE in
@@ -146,13 +145,13 @@ bool osq_lock(struct optimistic_spin_queue *lock)* the node fields we just initialised) semantics when updating * the lock tail. */ - prev_cpu =atomic_xchg (&lock->tail, curr); + prev_cpu =atomic_xchg (&lock->tail, node_cpu);if (prev_cpu ==OSQ_UNLOCKED_VAL )return true; prev =decode_cpu (prev_cpu); node->prev_cpu = prev_cpu; -osq_link_next (prev, node); +osq_link_next (prev, node_cpu);/* * Normally @prev is untouchable after the above store; because at that
@@ -191,8 +190,8 @@ bool osq_lock(struct optimistic_spin_queue *lock) prev =decode_cpu (prev_cpu); -if (data_race (prev->next) == node && -cmpxchg (&prev->next, node,NULL ) == node) +if (data_race (prev->next_cpu) == node_cpu && +cmpxchg (&prev->next_cpu, node_cpu,0 ) == node_cpu)break ;/*
[tip: locking/core] locking/osq: Set prev_cpu=0 instead of locked=1
In reply to David LaightThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: ee2e144a4e8538c4d2de0301792b03608b79e71e Gitweb: https://git.kernel.org/tip/ee2e144a4e8538c4d2de0301792b03608b79e71e Author: David Laight <[email protected]> AuthorDate: Mon, 07 Sep 2026 09:41:27 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Nov 2026 11:16:24 +01:00
locking/osq: Set prev_cpu=0 instead of locked=1
There is no need for separate prev_cpu and locked members of struct optimistic_spin_node. Using a single field simplifies the code slightly. It also removes any possibility of the two values being out of sync.
[ peterz: Removed the random prev_node store ]
--- kernel/locking/osq_lock.c | 45 ++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 26 deletions(-)
kernel/locking/osq_lock.c +19 -26
@@ -14,7 +14,6 @@struct optimistic_spin_node {struct optimistic_spin_node *next; -int locked;/* 1 if lock acquired */ int prev_cpu;/* CPU number offset by 1 */ };
@@ -134,7 +133,6 @@ bool osq_lock(struct optimistic_spin_queue *lock)int curr =encode_cpu (smp_processor_id ());int prev_cpu; - node->locked =0 ; node->next =NULL ;/*
@@ -166,44 +164,39 @@ bool osq_lock(struct optimistic_spin_queue *lock)* is implemented with a monitor-wait. vcpu_is_preempted() relies on * polling, be careful. */ -if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node->prev_cpu -1 ))) -return true; + prev_cpu =smp_cond_load_relaxed (&node->prev_cpu, !VAL ||need_resched () || +vcpu_is_preempted (VAL -1 ));/* unqueue */ /* * Step - A -- stabilize @prev * -* Undo our @prev->next assignment; this will make @prev's -* unlock()/unqueue() wait for a next pointer since @lock points to us -* (or later). +* Loop until either node->prev_cpu is zero (lock acquired) or we +* atomically change prev->next from node to NULL (stopping prev +* handing on the lock). +* Note that 'prev' can unlink itself concurrently with this +* test so that prev/prev_ptr can be stale, but since it +* is per-cpu data the memory can always be read. */ for (;;) { -/* -* cpu_relax() below implies a compiler barrier which would -* prevent this comparison being optimized away. -*/ +if (!prev_cpu) { +smp_acquire__after_ctrl_dep (); +return true; + } + + prev =decode_cpu (prev_cpu); +if (data_race (prev->next) == node &&cmpxchg (&prev->next, node,NULL ) == node)break ;/* -* We can only fail the cmpxchg() racing against an unlock(), -* in which case we should observe @node->locked becoming -* true. +* 'prev' must have unlinked (or be in the process of unlinking) +* itself from the list. */ -if (smp_load_acquire (&node->locked)) -return true; -cpu_relax (); - -/* -* Or we race against a concurrent unqueue()'s step-B, in which -* case its step-C will write us a new @node->prev pointer. -*/ prev_cpu =READ_ONCE (node->prev_cpu); - prev =decode_cpu (prev_cpu); }/*
@@ -248,11 +241,11 @@ void osq_unlock(struct optimistic_spin_queue *lock) node =this_cpu_ptr (&osq_node); next =xchg (&node->next,NULL );if (next) { -WRITE_ONCE (next->locked,1 ); +WRITE_ONCE (next->prev_cpu,0 );return ; } next =osq_wait_next (lock, node,OSQ_UNLOCKED_VAL );if (next) -WRITE_ONCE (next->locked,1 ); +WRITE_ONCE (next->prev_cpu,0 ); }
[tip: locking/core] locking/osq: Save the CPU number for 'prev', not the node address
In reply to David LaightThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 72c440a4e225879063809c3d0bcdd7e6544069fe Gitweb: https://git.kernel.org/tip/72c440a4e225879063809c3d0bcdd7e6544069fe Author: David Laight <[email protected]> AuthorDate: Mon, 07 Sep 2026 09:41:26 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Nov 2026 11:16:24 +01:00
locking/osq: Save the CPU number for 'prev', not the node address
The CPU number of node->prev is needed for both the vcpu_is_preempted() test and to update lock->tail. This saves reading the cache line for the other CPU's per-CPU data.
The CPU member of optimistic_spin_node is no longer needed.
[ peterz: Renamed stuff ]
--- kernel/locking/osq_lock.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-)
kernel/locking/osq_lock.c +12 -17
@@ -13,9 +13,9 @@*/ struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; +struct optimistic_spin_node *next;int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ +int prev_cpu;/* CPU number offset by 1 */ }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
@@ -29,11 +29,6 @@ static inline int encode_cpu(int cpu_nr)return cpu_nr +1 ; } -static inline int node_cpu (struct optimistic_spin_node *node) -{ -return node->cpu -1 ; -} -static inline struct optimistic_spin_node *decode_cpu (int encoded_cpu_val) {int cpu_nr = encoded_cpu_val -1 ;
@@ -137,11 +132,10 @@ bool osq_lock(struct optimistic_spin_queue *lock)struct optimistic_spin_node *node =this_cpu_ptr (&osq_node);struct optimistic_spin_node *prev, *next;int curr =encode_cpu (smp_processor_id ()); -int old; +int prev_cpu; node->locked =0 ; node->next =NULL ; - node->cpu = curr;/* * We need both ACQUIRE (pairs with corresponding RELEASE in
@@ -149,12 +143,12 @@ bool osq_lock(struct optimistic_spin_queue *lock)* the node fields we just initialised) semantics when updating * the lock tail. */ - old =atomic_xchg (&lock->tail, curr); -if (old ==OSQ_UNLOCKED_VAL ) + prev_cpu =atomic_xchg (&lock->tail, curr); +if (prev_cpu ==OSQ_UNLOCKED_VAL )return true; - prev =decode_cpu (old); - node->prev = prev; + prev =decode_cpu (prev_cpu); + node->prev_cpu = prev_cpu;osq_link_next (prev, node);/*
@@ -173,7 +167,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* polling, be careful. */ if (smp_cond_load_relaxed (&node->locked,VAL ||need_resched () || -vcpu_is_preempted (node_cpu (node->prev)))) +vcpu_is_preempted (node->prev_cpu -1 )))return true;/* unqueue */
@@ -208,7 +202,8 @@ bool osq_lock(struct optimistic_spin_queue *lock)* Or we race against a concurrent unqueue()'s step-B, in which * case its step-C will write us a new @node->prev pointer. */ - prev =READ_ONCE (node->prev); + prev_cpu =READ_ONCE (node->prev_cpu); + prev =decode_cpu (prev_cpu); }/*
@@ -218,7 +213,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* back to @prev. */ - next =osq_wait_next (lock, node, prev->cpu); + next =osq_wait_next (lock, node, prev_cpu);if (!next)return false;
@@ -230,7 +225,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)* it will wait in Step-A. */ -WRITE_ONCE (next->prev, prev); +WRITE_ONCE (next->prev_cpu, prev_cpu);osq_link_next (prev, next);return false;