Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH] RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit()
@ 2026-11-29  2:47 Zhenwen Liang
  2026-11-29  2:51 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Zhenwen Liang @ 2026-11-29  2:47 UTC (permalink / raw)
  To: Md. Haris Iqbal, Jack Wang; +Cc: Jason Gunthorpe, Leon Romanovsky, linux-rdma

rtrs_clt_put_permit() releases a permit with clear_bit_unlock() and then
calls waitqueue_active() without holding the wait queue lock.
clear_bit_unlock() is a release operation: it orders earlier accesses
before the clear, but it does not order the clear before the later
waitqueue_active() load. The comment on waitqueue_active() in
include/linux/wait.h requires a full barrier on the waker side for this
lockless check.

The waiter, rtrs_clt_get_permit(), calls prepare_to_wait(), which adds
it to permits_wait and sets the task state (a full barrier), and then
looks for a free bit in permits_map. Without a barrier on the waker
side, the waker can see an empty wait queue while the waiter still sees
the bit set, and the wakeup is lost. The waiter sleeps in
TASK_UNINTERRUPTIBLE with no timeout; if the freed permit was the last
outstanding one and no later put follows, nothing wakes it.

The existing comment argues that a sleeping waiter must have added
itself to the list before the put finished. That does not hold on weakly
ordered CPUs, because the waker's load of the wait queue head can be
satisfied before its clear of the bit becomes visible. Add
smp_mb__after_atomic() right after clear_bit_unlock() in
__rtrs_put_permit(), as Documentation/atomic_bitops.txt and
Documentation/atomic_t.txt prescribe for upgrading the ordering of an
RMW bit operation, and replace the comment.

This does not remove every race on permits_map. The waiter looks for a
free permit with find_next_zero_bit()/find_first_zero_bit() in
__rtrs_get_permit(), whose plain loads race with clear_bit_unlock(); in
the Linux kernel memory model this is a data race. The barrier fixes the
reordering on the waker side that loses the wakeup; the data race
between those plain loads and the clear remains. It is left as it is
because the find_*_bit() helpers have no marked variant and changing
them would go beyond this function. Marking the waiter's re-check after
prepare_to_wait() would also remove the race in the memory model; that
is left for a separate change.

A litmus test of this pattern (the waiter's enqueue, full barrier and
bit test against the waker's release clear and wait queue check) showed
the reordering 2875 times in 10^8 runs on arm64 (Neoverse-V2), and 6343
times with an LL/SC clear; with smp_mb__after_atomic() added, 0 in 10^8
for both. The same pattern on x86-64 gave 0 in 10^8, as expected,
because clear_bit_unlock() is a locked instruction there. herd7's
AArch64 and PPC models allow the pattern and forbid it with the barrier.

The problem was found, and this patch prepared, with the help of a
formal analysis tool we are developing that uses AI assistance; I have
reviewed the analysis and the patch.

Compile-tested with clang on arm64 (W=1) and x86-64, and checked that
the barrier is emitted between the clear and the wait queue load in
arm64 object code; not runtime-tested beyond the litmus tests.

Fixes: 6a98d71daea1 ("RDMA/rtrs: client: main functionality")
Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
---
 drivers/infiniband/ulp/rtrs/rtrs-clt.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt.c b/drivers/infiniband/ulp/rtrs/rtrs-clt.c
index 85eaa2bdb470..c6befc4303af 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-clt.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-clt.c
@@ -102,6 +102,12 @@ static inline void __rtrs_put_permit(struct rtrs_clt_sess *clt,
 				      struct rtrs_permit *permit)
 {
 	clear_bit_unlock(permit->mem_id, clt->permits_map);
+	/*
+	 * Order the clear before the lockless waitqueue_active() load in
+	 * rtrs_clt_put_permit(). Pairs with the barrier in set_current_state()
+	 * in prepare_to_wait() in rtrs_clt_get_permit().
+	 */
+	smp_mb__after_atomic();
 }
 
 /**
@@ -162,11 +168,9 @@ void rtrs_clt_put_permit(struct rtrs_clt_sess *clt,
 	__rtrs_put_permit(clt, permit);
 
 	/*
-	 * rtrs_clt_get_permit() adds itself to the &clt->permits_wait list
-	 * before calling schedule(). So if rtrs_clt_get_permit() is sleeping
-	 * it must have added itself to &clt->permits_wait before
-	 * __rtrs_put_permit() finished.
-	 * Hence it is safe to guard wake_up() with a waitqueue_active() test.
+	 * The barrier in __rtrs_put_permit() orders the clear before this
+	 * check: either a waiter in rtrs_clt_get_permit() sees the cleared
+	 * bit, or we see the waiter on &clt->permits_wait and wake it up.
 	 */
 	if (waitqueue_active(&clt->permits_wait))
 		wake_up(&clt->permits_wait);
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit()
  2026-11-29  2:47 [PATCH] RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit() Zhenwen Liang
@ 2026-11-29  2:51 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-29  2:51 UTC (permalink / raw)
  To: Zhenwen Liang; +Cc: linux-rdma

> rtrs_clt_put_permit() releases a permit with clear_bit_unlock() and then
> calls waitqueue_active() without holding the wait queue lock.
> clear_bit_unlock() is a release operation: it orders earlier accesses
> before the clear, but it does not order the clear before the later
> waitqueue_active() load. The comment on waitqueue_active() in
> include/linux/wait.h requires a full barrier on the waker side for this
> lockless check.
> 
> The waiter, rtrs_clt_get_permit(), calls prepare_to_wait(), which adds
> it to permits_wait and sets the task state (a full barrier), and then
> looks for a free bit in permits_map. Without a barrier on the waker
> side, the waker can see an empty wait queue while the waiter still sees
> the bit set, and the wakeup is lost. The waiter sleeps in
> TASK_UNINTERRUPTIBLE with no timeout; if the freed permit was the last
> outstanding one and no later put follows, nothing wakes it.
> [ ... ]
> the barrier is emitted between the clear and the wait queue load in
> arm64 object code; not runtime-tested beyond the litmus tests.
> 
> Fixes: 6a98d71daea1 ("RDMA/rtrs: client: main functionality")
> Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129024708.279044-1-zhenwenliang@google.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-29  2:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29  2:47 [PATCH] RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit() Zhenwen Liang
2026-11-29  2:51 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox