Re: [PATCH v4 next 0/9] locking/osq_lock: Optimisations to osq_lock code
From: Haakon Bugge
Date: Thu Sep 10 2026 - 06:26:20 EST
> On 9 Sep 2026, at 22:33, Waiman Long <longman@xxxxxxxxxx> wrote:
[snip]
> > 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
>
> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index b4233dc2c2b0..51cecf297692 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -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);
> }
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:
Tested-by: Håkon Bugge <haakon.bugge@xxxxxxxxxx>
Thxs, Håkon