RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit()
Subsystems (2)
PATCH 1/1 RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit()
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.
--- drivers/infiniband/ulp/rtrs/rtrs-clt.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-clt.c +9 -5
@@ -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);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 19 lines
rtrs_clt_put_permit()releases a permit withclear_bit_unlock()and then callswaitqueue_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 laterwaitqueue_active()load. The comment onwaitqueue_active()ininclude/linux/wait.hrequires a full barrier on the waker side for this lockless check.The waiter,
rtrs_clt_get_permit(), callsprepare_to_wait(), which adds it topermits_waitand sets the task state (a full barrier), and then looks for a free bit inpermits_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 inTASK_UNINTERRUPTIBLEwith 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.
Quoted 2 lines
Fixes 6a98d71daea1 ("RDMA/rtrs: client: main functionality")Signed-off-by Zhenwen Liang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Leon Romanovsky
AppliedQuoted 9 lines · Zhenwen Liang
rtrs_clt_put_permit()releases a permit withclear_bit_unlock()and then callswaitqueue_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 laterwaitqueue_active()load. The comment onwaitqueue_active()ininclude/linux/wait.hrequires a full barrier on the waker side for this lockless check.[...]
Applied, thanks!
[1/1] RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit()
https://git.kernel.org/rdma/rdma/c/7255edf9367014
Best regards,
Signature · 1 line
Leon Romanovsky <[email protected]>