Skip to contentSkip to conversation

net/rds: use wq_has_sleeper() in rds_transition_frwr_state()

PATCH In review Zhenwen Liang 3 participants 5 messages Nov 30 – Dec 2

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/rds: use wq_has_sleeper() in rds_transition_frwr_state()

rds_transition_frwr_state() decrements i_fastreg_inuse_count with atomic_dec() and then checks waitqueue_active(). Nothing orders the decrement before the wait queue load, so on arm64 or powerpc rds_ib_conn_path_shutdown() can miss the wakeup and sleep until its 1 s timeout. Use wq_has_sleeper(), which adds the missing full barrier.

Allison Henderson pointed out that the sashiko review of a similar fix flagged this site. The analysis was checked, 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.

Fixes 3a2886cca703 ("net/rds: Keep track of and wait for FRWR segments in use upon shutdown")

---
Compile-tested only (arm64).
Allison's comment: https://lore.kernel.org/r/[email protected]/

 net/rds/ib_frmr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rds/ib_frmr.c +1 -1
index 8397aa4a17ac..9a9424a499a3 100644
@@ -45,7 +45,7 @@ rds_transition_frwr_state(struct rds_ib_mr *ibmr,
 		 */
 		smp_mb__before_atomic();
 		atomic_dec(&ibmr->ic->i_fastreg_inuse_count);
-		if (waitqueue_active(&rds_ib_ring_empty_wait))
+		if (wq_has_sleeper(&rds_ib_ring_empty_wait))
 			wake_up(&rds_ib_ring_empty_wait);
 	}
 }
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

rds_transition_frwr_state() decrements i_fastreg_inuse_count with atomic_dec() and then checks waitqueue_active(). Nothing orders the decrement before the wait queue load, so on arm64 or powerpc rds_ib_conn_path_shutdown() can miss the wakeup and sleep until its 1 s timeout. Use wq_has_sleeper(), which adds the missing full barrier.

Quoted 8 lines

Allison Henderson pointed out that the sashiko review of a similar fix flagged this site. The analysis was checked, 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.

Fixes 3a2886cca703 ("net/rds: Keep track of and wait for FRWR segments in use upon shutdown")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Zhenwen Liang
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 2 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The patch fixes only one of the two unordered wakers of rds_ib_ring_empty_wait.
- [High] The wakeup that the patch makes reliable fires before the shutdown wait condition can be true, and nothing wakes the waiter once…

--

net/rds: use wq_has_sleeper() in rds_transition_frwr_state()

This patch replaces waitqueue_active() with wq_has_sleeper() in rds_transition_frwr_state(). The full barrier then orders the i_fastreg_inuse_count decrement before the wait queue check. The aim is to stop rds_ib_conn_path_shutdown() from missing the wakeup and sleeping until its 1 s timeout on weakly ordered architectures.

net/rds/ib_frmr.c rds_transition_frwr_state(struct rds_ib_mr *ibmr, · line 49

Quoted 11 lines
net/rds/ib_frmr.c +1 -1
index 8397aa4a17ac8..9a9424a499a37 100644
@@ -45,7 +45,7 @@ rds_transition_frwr_state(struct rds_ib_mr *ibmr,
 		 */
 		smp_mb__before_atomic();
 		atomic_dec(&ibmr->ic->i_fastreg_inuse_count);
-		if (waitqueue_active(&rds_ib_ring_empty_wait))
+		if (wq_has_sleeper(&rds_ib_ring_empty_wait))
 			wake_up(&rds_ib_ring_empty_wait);

[Severity: High]
Is there a second waker of rds_ib_ring_empty_wait with the same missing barrier? rds_ib_ring_free() in net/rds/ib_ring.c does a void atomic RMW and then calls waitqueue_active() with no barrier in between:

net/rds/ib_ring.c:rds_ib_ring_free() {
	ring->w_free_ptr = (ring->w_free_ptr + val) % ring->w_nr;
	atomic_add(val, &ring->w_free_ctr);

	if (__rds_ib_ring_empty(ring) &&
	    waitqueue_active(&rds_ib_ring_empty_wait))
		wake_up(&rds_ib_ring_empty_wait);
}

It wakes the same waiter, because rds_ib_conn_path_shutdown_check_wait() requires rds_ib_ring_empty(&ic->i_recv_ring).

During shutdown, flushed receive WRs go through rds_ib_recv_cqe_handler()->rds_ib_ring_free(). If that is the last event that satisfies the wait condition, the store buffering problem described in the commit message applies here too.

The similar fix mentioned in the commit message (commit 6d0c8b707391) changed release_in_xmit() in net/rds/send.c. It did not touch ib_ring.c.

rds_ib_sub_signaled() uses atomic_sub_return(), which is fully ordered. That makes rds_ib_ring_free() the only unordered waker left on this wait queue.

Should rds_ib_ring_free() also be converted to wq_has_sleeper() in this patch?

[Severity: High]
Can this wakeup fire before the shutdown condition is able to become true?

rds_ib_conn_path_shutdown_check_wait() needs both counters to have settled:

	(atomic_read(&ic->i_fastreg_inuse_count)) == 0 &&
	(atomic_read(&ic->i_fastreg_wrs) == RDS_IB_DEFAULT_FR_WR)

Every FRWR post first takes an i_fastreg_wrs slot with atomic_dec_return(). While the last WR is in flight, i_fastreg_wrs is therefore one below RDS_IB_DEFAULT_FR_WR.

On completion, rds_ib_mr_cqe_handler() wakes the waiter through rds_transition_frwr_state(). It only gives the slot back at the end:

net/rds/ib_frmr.c:rds_ib_mr_cqe_handler() {
	...
	if (frmr->fr_inv) {
		rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_FREE);
		frmr->fr_inv = false;
		wake_up(&frmr->fr_inv_done);
	}
	...
	smp_mb__before_atomic();
	atomic_inc(&ic->i_fastreg_wrs);
}

Suppose the shutdown thread re-checks between the wake and the atomic_inc(). It sees inuse_count == 0 with i_fastreg_wrs still one short, and goes back to sleep.

Nothing wakes rds_ib_ring_empty_wait after the increment, and poll_scq() does not add a wakeup either. The waiter would then sleep until the 1000 ms timeout, on any architecture.

The error paths do things in the same order, with wider windows. In rds_ib_post_reg_frmr(), the wake happens when ib_post_send() fails. Then printk_ratelimit()/pr_warn() runs, and i_fastreg_wrs is only restored at out_inc:

	rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);

	if (printk_ratelimit())
		pr_warn("RDS/IB: %s returned error(%d)\n",
			__func__, ret);
	goto out_inc;
	...
out_inc:
	atomic_inc(&ibmr->ic->i_fastreg_wrs);

Two other paths have the same order:

- the ib_post_send() error path in rds_ib_post_inv()
- the wc->status != IB_WC_SUCCESS branch of rds_ib_mr_cqe_handler(), which flushed WRs take during disconnect

Would it work better to do the wq_has_sleeper()/wake_up() after each atomic_inc(&i_fastreg_wrs), instead of only adding a barrier at this wake site?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130164729.981519-1-zhenwenliang%40google.com
#3 lore raw
Quoted 2 lines · On Tue, Dec 01, 2026 at 04:47:34PM +0000, [email protected] wrote:

- [High] The patch fixes only one of the two unordered wakers of rds_ib_ring_empty_wait.

rds_ib_ring_free() is fixed by a separate patch, which Allison has reviewed:
https://lore.kernel.org/r/[email protected]/

Quoted 2 lines

- [High] The wakeup that the patch makes reliable fires before the shutdown wait condition can be true, and nothing wakes the waiter once…

Right. Every caller of rds_transition_frwr_state() still holds an i_fastreg_wrs slot at that point, so the wait condition cannot hold there, and this patch alone does not help. I will send a v2 that wakes the waiter after the slot is returned.

pw-bot: cr