* Re: [PATCH bpf-next v2] rqspinlock: Keep claiming the lock while the queue head is still the tail
2026-11-18 10:47 [PATCH bpf-next v2] rqspinlock: Keep claiming the lock while the queue head is still the tail Shashank Mohan Jain
@ 2026-11-18 11:27 ` bot+bpf-ci
2026-11-19 1:22 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: bot+bpf-ci @ 2026-11-18 11:27 UTC (permalink / raw)
To: jain.sm, memxor, ast
Cc: daniel, andrii, eddyz87, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, bpf, linux-kernel, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2276 bytes --]
> rqspinlock: Keep claiming the lock while the queue head is still the tail
TL;DR: Nits only: two comments in rqspinlock.c (queue-head claim and pending
undo path) still describe the old behaviour and could be updated to match the
new retry loop.
> diff --git a/kernel/bpf/rqspinlock.c b/kernel/bpf/rqspinlock.c
> --- a/kernel/bpf/rqspinlock.c
> +++ b/kernel/bpf/rqspinlock.c
[ ... ]
> @@ -624,20 +624,23 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
> * Otherwise, we only need to grab the lock.
> */
>
> - /*
> - * Note: at this point: (val & _Q_PENDING_MASK) == 0, because of the
> - * above wait condition, therefore any concurrent setting of
> - * PENDING will make the uncontended transition fail.
> - */
> if ((val & _Q_TAIL_MASK) == tail) {
> - if (atomic_try_cmpxchg_relaxed(&lock->val, &val, _Q_LOCKED_VAL))
> - goto release; /* No contention */
> + u32 new = _Q_LOCKED_VAL;
> +
> + for (;;) {
> + if (atomic_try_cmpxchg_relaxed(&lock->val, &val, new))
> + goto release; /* No queued successor */
> + if ((val & _Q_TAIL_MASK) != tail)
> + break;
> + /* A pending claimant can abandon before publishing a tail. */
> + new = _Q_LOCKED_VAL | (val & _Q_PENDING_MASK);
> + }
> }
This isn't a bug, but the comment block just above this hunk still
describes the old behaviour:
* If the queue head is the only one in the queue (lock value == tail)
* and nobody is pending, clear the tail code and grab the lock.
The new loop also clears the tail when pending is set, keeping the
pending bit (the n,1,0 -> 0,1,1 transition). Could that comment be
updated?
Similarly, the comment on the pending claimant's undo path still says:
* Undo and queue; our setting of PENDING might have made the
* n,0,0 -> 0,0,0 transition fail and it will now be waiting
* on @next to become !NULL.
With this change the head no longer waits on @next in that case while the
tail is still its own. Could that comment be adjusted in this patch as
well?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44290867653
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH bpf-next v2] rqspinlock: Keep claiming the lock while the queue head is still the tail
2026-11-18 10:47 [PATCH bpf-next v2] rqspinlock: Keep claiming the lock while the queue head is still the tail Shashank Mohan Jain
2026-11-18 11:27 ` bot+bpf-ci
@ 2026-11-19 1:22 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-11-19 1:22 UTC (permalink / raw)
To: Shashank Mohan Jain, Kumar Kartikeya Dwivedi
Cc: Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel
On Wed, Nov 18, 2026 at 02:47 AM Shashank Mohan Jain <jain.sm@gmail.com> wrote:
> Only a different tail shows that a waiter did xchg_tail() and will
> link into our node. So while the tail is still ours, retry the claim
> and keep the PENDING bit of a waiter that may still be backing out,
On x86 that waiter doesn't always back out.
queued_fetch_set_pending_acquire() is lock btsl and then a separate load:
val |= atomic_read(&lock->val) & ~_Q_PENDING_MASK;
With the patch the head can remove the tail between the two:
CPU 0 (H, head) CPU 1
lock btsl -> H,1,0
try_cmpxchg(H,0,0 -> 0,0,1) fails
try_cmpxchg(H,1,0 -> 0,1,1)
atomic_read() == 0,1,1
val = 0,0,1, no contention
waits for H as the pending waiter
Today that load sees tail H and CPU 1 calls clear_pending().
The new case works only because the head keeps PENDING.
Does your TAL model have btsl and the load as two steps?
> Suggested-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> The diff is Kumar's from the v1 review.
Pls keep his authorship and SOB.
> + u32 new = _Q_LOCKED_VAL;
> +
> + for (;;) {
> + if (atomic_try_cmpxchg_relaxed(&lock->val, &val, new))
> + goto release; /* No queued successor */
> + if ((val & _Q_TAIL_MASK) != tail)
> + break;
PENDING is zero in val on the first pass. The wait loop above checks it.
No need for 'new' and the second tail check.
Two comments in this function still describe the old code:
* and nobody is pending, clear the tail code and grab the lock.
* n,0,0 -> 0,0,0 transition fail and it will now be waiting
* on @next to become !NULL.
The head clears the tail with PENDING set now.
It waits for next only after the tail has moved.
Would the following be simpler?
diff --git a/kernel/bpf/rqspinlock.c b/kernel/bpf/rqspinlock.c
index 111ec80..14d3798 100644
--- a/kernel/bpf/rqspinlock.c
+++ b/kernel/bpf/rqspinlock.c
@@ -378,9 +378,7 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
/*
* If we observe contention, there is a concurrent locker.
*
- * Undo and queue; our setting of PENDING might have made the
- * n,0,0 -> 0,0,0 transition fail and it will now be waiting
- * on @next to become !NULL.
+ * Undo and queue.
*/
if (unlikely(val & ~_Q_LOCKED_MASK)) {
@@ -616,29 +614,24 @@ waitq_timeout:
/*
* claim the lock:
*
- * n,0,0 -> 0,0,1 : lock, uncontended
+ * n,*,0 -> 0,*,1 : lock, uncontended
* *,*,0 -> *,*,1 : lock, contended
*
- * If the queue head is the only one in the queue (lock value == tail)
- * and nobody is pending, clear the tail code and grab the lock.
- * Otherwise, we only need to grab the lock.
- */
-
- /*
- * Note: at this point: (val & _Q_PENDING_MASK) == 0, because of the
- * above wait condition, therefore any concurrent setting of
- * PENDING will make the uncontended transition fail.
+ * If the queue head is the only one in the queue, clear the tail code
+ * and grab the lock. Otherwise, we only need to grab the lock.
+ *
+ * A waiter that sets PENDING now may leave with -EDEADLK and never
+ * queue behind us, so retry as long as the tail is ours. PENDING
+ * belongs to that waiter, keep it. It either clears the bit or, on
+ * x86, no longer sees our tail and becomes the pending waiter.
*/
- if ((val & _Q_TAIL_MASK) == tail) {
- if (atomic_try_cmpxchg_relaxed(&lock->val, &val, _Q_LOCKED_VAL))
- goto release; /* No contention */
+ while ((val & _Q_TAIL_MASK) == tail) {
+ if (atomic_try_cmpxchg_relaxed(&lock->val, &val,
+ (val & _Q_PENDING_MASK) | _Q_LOCKED_VAL))
+ goto release;
}
- /*
- * Either somebody is queued behind us or _Q_PENDING_VAL got set
- * which will then detect the remaining tail and queue behind us
- * ensuring we'll see a @next.
- */
+ /* Somebody is queued behind us, ensuring we'll see a @next. */
set_locked(lock);
/*
pw-bot: cr
^ permalink raw reply related [flat|nested] 3+ messages in thread