RDMA/siw: don't complete a fenced READ twice on flush
Subsystems (5)
PATCH 1/1 RDMA/siw: don't complete a fenced READ twice on flush
siw_sq_flush() completes the READ in the TX slot while it is still SIW_WR_QUEUED, assuming a queued READ is not in the ORQ yet. That only holds when a full ORQ fenced it. siw_activate_tx_from_sq() copies a READ into a free ORQ slot, and siw_check_tx_fence() copies a fenced READ into the ORQ once a Read Response frees a slot; in both cases the WQE stays QUEUED until the TX path sends the Read Request. A flush in that window completes the READ from the ORQ and again from the TX slot: two IB_WC_WR_FLUSH_ERR completions for one work request.
The peer controls the window. It advertises IRD 1, so the ORQ holds one READ, and answers the first Read Request with a Read Response and a FIN in one TCP segment: the FIN suspends TX before the Read Response moves the next READ into the ORQ, and the close flushes it.
svcrdma's svc_rdma_wc_read_done() then puts the receive context twice, which links it into sc_recv_ctxts as a loop, and svc_rdma_free() reads it after freeing it and frees it a second time, a double free. svcrdma pulls the Read chunk before it decodes the RPC, so the peer needs no RPC credentials and no export access.
Complete a QUEUED READ from the TX slot only while the ORQ fence is set.
Found with afb-afl, an LLM-driven kernel audit/repro pipeline; the fix and this changelog were also drafted with LLM assistance. Reproduced on an unmodified KASAN build of mainline: a plain TCP client sends nfsd's svcrdma listener on siw an RDMA_NOMSG call whose Read chunk has two segments and answers the first Read Request as above. svcrdma_wc_read_flush fires twice for the chunk (5 of 5 runs), then:
BUG: KASAN: slab-use-after-free in llist_del_first+0x150/0x170 Read of size 8 at addr ffff8881119d9000 by task nfsd/183 llist_del_first+0x150/0x170 svc_rdma_recv_ctxts_destroy+0xf3/0x1b0 svc_rdma_free+0xd8/0x2e0 svc_xprt_free+0x22c/0x420 Freed by task 183: kfree+0x162/0x500 svc_rdma_recv_ctxts_destroy+0xe7/0x1b0
Without KASAN and with SLAB_FREELIST_HARDENED, the double free is caught as "kernel BUG at mm/slub.c:605" in kfree() from svc_rdma_recv_ctxts_destroy() (4 of 4 runs).
With this patch the chunk is flushed once and KASAN stays silent (4 of 4 runs). The rdma-core tests could not run: the siw provider of rdma-core 66 fails an assertion in siw_destroy_cq() on the first test, with and without this patch.
---
Tested on mainline 4405c18c95d2 (v7.4-rc3+) in QEMU. Applies to mainline 8d7fe9885c62.
Sending the FIN only after the second Read Request has gone out, or with no Read Response at all (the fenced READ never enters the ORQ), gives one flush completion before and after the patch.
siw_sq_flush() now relies on orq_fence. That bit shares a byte with tx_suspend, which siw_cm_llp_state_change() sets without the QP state lock. A lost update there already stalls the fenced READ in siw_check_tx_fence(); with this patch it would also lose that READ's flush completion. I did not try to hit that window.
A reproducer and the test logs are available on request.
drivers/infiniband/sw/siw/siw_qp.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/infiniband/sw/siw/siw_qp.c +4 -3
@@ -1188,10 +1188,11 @@ void siw_sq_flush(struct siw_qp *qp)if (tx_type (wqe) !=SIW_OP_READ_RESPONSE && ((tx_type (wqe) !=SIW_OP_READ &&tx_type (wqe) !=SIW_OP_READ_LOCAL_INV ) || - wqe->wr_status ==SIW_WR_QUEUED )) + (wqe->wr_status ==SIW_WR_QUEUED && + qp->tx_ctx.orq_fence)))/* -* An in-progress Read Request is already in -* the ORQ +* A Read Request is already in the ORQ unless +* a full ORQ fenced it */ siw_sqe_complete (qp, &wqe->sqe, wqe->bytes,SIW_WC_WR_FLUSH_ERR );
Signature · 1 line
2.56.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] RDMA/siw: data race on tx_ctx bitfields clobbers orq_fence in siw_sq_flush() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] RDMA/siw: data race on tx_ctx bitfields clobbers orq_fence in siw_sq_flush()
--
commit 2dacef6eebec78b6dccffd22a13876b9c48ca403
Author: Cygnus Qiu <[email protected]>
RDMA/siw: don't complete a fenced READ twice on flush
This commit gates completing a SIW_WR_QUEUED READ from the TX slot in siw_sq_flush() on qp->tx_ctx.orq_fence so a READ already copied into the ORQ is not completed twice.
Quoted 9 lines
drivers/infiniband/sw/siw/siw_qp.c +0
-1@@ -1188,10 +1188,11 @@ void siw_sq_flush(struct siw_qp *qp) if (tx_type(wqe) != SIW_OP_READ_RESPONSE && ((tx_type(wqe) != SIW_OP_READ && tx_type(wqe) != SIW_OP_READ_LOCAL_INV) || - wqe->wr_status ==SIW_WR_QUEUED ))
drivers/infiniband/sw/siw/siw_qp.c void siw_sq_flush(struct siw_qp *qp) · line 1198
Quoted 10 lines
drivers/infiniband/sw/siw/siw_qp.c +4
-2+ (wqe->wr_status == SIW_WR_QUEUED && + qp->tx_ctx.orq_fence)))/* -* An in-progress Read Request is already in -* the ORQ +* A Read Request is already in the ORQ unless +* a full ORQ fenced it */ siw_sqe_complete (qp, &wqe->sqe, wqe->bytes,SIW_WC_WR_FLUSH_ERR );
[Severity: Medium]
Can a concurrent update to qp->tx_ctx.tx_suspend clobber qp->tx_ctx.orq_fence and cause siw_sq_flush() to drop or duplicate the flush completion?
In struct siw_iwarp_tx, orq_fence and tx_suspend are u8 bitfields packed into the same byte:
drivers/infiniband/sw/siw/siw.h:struct siw_iwarp_tx {
...
u8 do_crc : 1; /* do crc for segment */
u8 use_sendpage : 1; /* send w/o copy */ u8 tx_suspend : 1; /* stop sending DDP segs. */ u8 pad : 2; /* # pad in current fpdu */
u8 orq_fence : 1; /* ORQ full or Send fenced */ u8 in_syscall : 1; /* TX out of user context */ u8 zcopy_tx : 1; /* Use TCP_SENDPAGE if possible */
};
siw_activate_tx_from_sq() and siw_check_tx_fence() update orq_fence under qp->orq_lock, whereas siw_cm_llp_state_change() sets tx_suspend = 1 on inbound TCP_CLOSE or TCP_CLOSE_WAIT without holding qp->orq_lock, qp->sq_lock or qp->state_lock:
CPU0 CPU1siw_activate_tx_from_sq() siw_cm_llp_state_change()
spin_lock(&qp->orq_lock) /* load shared bitfield byte
... (orq_fence == 0) */
qp->tx_ctx.orq_fence = 1;
spin_unlock(&qp->orq_lock)
cep->qp->tx_ctx.tx_suspend = 1;
/* stores orq_fence = 0 */
...
siw_sq_flush()
/* orq_fence == 0: skips completing
fenced READ in tx_wqe(qp) */If the clobber loses orq_fence = 1, the fenced READ is never moved to the ORQ and siw_sq_flush() now skips completing it from tx_wqe(qp), hanging callers waiting on SQ drain. Conversely, if tx_suspend = 1 races with siw_check_tx_fence() clearing orq_fence = 0 after copying the READ to the ORQ, restoring orq_fence = 1 causes siw_sq_flush() to complete the READ from both the ORQ and tx_wqe(qp).
Making orq_fence (or the concurrently accessed flags in struct siw_iwarp_tx) a separate u8 field would avoid the shared-byte read-modify- write race.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1