Skip to contentSkip to conversation

RDMA/siw: don't complete a fenced READ twice on flush

PATCH In review Cygnus Qiu 3 participants 3 messages Nov 27

Subsystems (5)
lore
Share & tools
lore

Cygnus Qiu

author
#0 lore raw

PATCH v2 1/1 RDMA/siw: don't complete a fenced READ twice on flush

siw_sq_flush() completes a QUEUED READ in the TX slot, assuming the READ is not in the ORQ yet. That holds only while a full ORQ fences it. siw_check_tx_fence() copies a fenced READ into the ORQ when a Read Response frees a slot, and siw_activate_tx_from_sq() copies a READ into a free slot. In both cases the READ stays QUEUED until the TX path sends it, and a flush in that window completes it twice.

A peer can hold the window open. It advertises IRD 1, so the ORQ holds one READ: READ1 is in the ORQ and sent, READ2 waits fenced in the TX slot. The peer then sends Read Response 1 and a FIN in one TCP segment:

softirq (TCP receive) siw_cm_wq worker
tcp_data_queue()

  |-tcp_fin()
  | \-siw_cm_llp_state_change()
  |   Sets tx_suspend and
  |   queues the close.
  |
  \-tcp_data_ready()
    \-siw_qp_llp_data_ready()
      |-down_read_trylock(state_lock)
      |-tcp_read_sock()
      | \-siw_tcp_rx_data()
      |   \-siw_rdmap_complete()
      |     \-siw_check_tx_fence()
      |       |-siw_read_to_orq()
      |       | READ2 enters the ORQ.
      |       |
      |       \-siw_sq_start()
      |         orq_fence is now 0.
      |         TX is suspended:
      |         READ2 stays QUEUED.
      |
      \-up_read(state_lock)
                                        siw_cm_work_handler()
                                        \-siw_qp_llp_close()
                                          |-down_write(state_lock)
                                          \-siw_sq_flush()
                                            |-siw_sqe_complete()
                                            | READ2 from the ORQ.
                                            |
                                            \-siw_sqe_complete()
                                              READ2 again, from the
                                              TX slot.

The FIN only queues the close, so the receive path normally gets qp->state_lock first.

nfsd's svcrdma gets two flush completions for one Read chunk:

svc_rdma_wc_read_done() \-svc_rdma_recv_ctxt_put()
Runs twice for the same ctxt. The second llist_add() links ctxt->rc_node to itself.

svc_rdma_free()
\-svc_rdma_recv_ctxts_destroy()

    |-svc_rdma_recv_ctxt_destroy()
    | Frees ctxt.
    |
    |-llist_del_first()
    | Reads the freed ctxt and returns it again.
    |

\-svc_rdma_recv_ctxt_destroy()
Frees ctxt a second time.

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 orq_fence is set. orq_fence now decides whether the TX slot completes the READ, so move it out of the bitfield it shared with tx_suspend:
siw_cm_llp_state_change() sets tx_suspend without taking a QP lock, and a lost update could drop or duplicate the completion.

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 with a two-segment Read chunk and answers as above. KASAN reports the read of the freed ctxt; here the second destroy then faults in kfree():

  BUG: KASAN: slab-use-after-free in llist_del_first+0x150/0x170
  Read of size 8 at addr ffff8881119d9000 by task nfsd/183

  Call Trace:
   <TASK>
   dump_stack_lvl+0x5d/0x80
   print_report+0x14b/0x4b0
   kasan_report+0xfa/0x120
   llist_del_first+0x150/0x170
   svc_rdma_recv_ctxts_destroy+0xf3/0x1b0
   svc_rdma_free+0xd8/0x2e0
   svc_xprt_free+0x22c/0x420
   svc_recv+0x103f/0x25b0
   nfsd+0x35c/0xc80
   kthread+0x34d/0x460
   ret_from_fork+0x339/0x620
   ret_from_fork_asm+0x1a/0x30
   </TASK>

  Allocated by task 183:
   kasan_save_stack+0x2f/0x50
   kasan_save_track+0x14/0x40
   __kasan_kmalloc+0x7f/0x90
   __kmalloc_node_noprof+0x207/0x680
   svc_rdma_post_recvs+0x664/0xbf0
   svc_rdma_accept+0xc27/0x1980
   svc_recv+0x12c2/0x25b0
   nfsd+0x35c/0xc80
   kthread+0x34d/0x460
   ret_from_fork+0x339/0x620
   ret_from_fork_asm+0x1a/0x30

  Freed by task 183:
   kasan_save_stack+0x2f/0x50
   kasan_save_track+0x14/0x40
   kasan_save_free_info+0x3b/0x60
   __kasan_slab_free+0x57/0x80
   kfree+0x162/0x500
   svc_rdma_recv_ctxts_destroy+0xe7/0x1b0
   svc_rdma_free+0xd8/0x2e0
   svc_xprt_free+0x22c/0x420
   svc_recv+0x103f/0x25b0
   nfsd+0x35c/0xc80
   kthread+0x34d/0x460
   ret_from_fork+0x339/0x620
   ret_from_fork_asm+0x1a/0x30

  The buggy address belongs to the object at ffff8881119d9000
   which belongs to the cache kmalloc-2k of size 2048
  The buggy address is located 0 bytes inside of
   freed 2048-byte region [ffff8881119d9000, ffff8881119d9800)

  BUG: unable to handle page fault for address: ffffeb860011a6c8
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  Oops: Oops: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:kfree+0x69/0x500
  Call Trace:
   <TASK>
   svc_rdma_recv_ctxts_destroy+0xdf/0x1b0
   svc_rdma_free+0xd8/0x2e0
   svc_xprt_free+0x22c/0x420
   svc_recv+0x103f/0x25b0
   nfsd+0x35c/0xc80
   kthread+0x34d/0x460
   ret_from_fork+0x339/0x620
   ret_from_fork_asm+0x1a/0x30
   </TASK>

Without KASAN, SLAB_FREELIST_HARDENED catches the double free as "kernel BUG at mm/slub.c:605" in kfree() from svc_rdma_recv_ctxts_destroy(). With neither KASAN nor SLAB_FREELIST_HARDENED, nfsd pops the freed ctxt again. If another kmalloc-2k allocation has overwritten it by then, nfsd faults; if not, nfsd frees it again and repeats, so later allocations can get that one object many times.

With this patch the READ is flushed once and KASAN stays silent.

Fixes f29dd55b0236 ("rdma/siw: queue pair methods")

---
Changes in v2:
- Rewrite the commit message: charts for the race and the svcrdma free path, and the full KASAN report.
- Give orq_fence its own byte, as Sashiko suggested on v1. The lost update was not reproduced.
- v1: https://lore.kernel.org/r/[email protected]

Tested on mainline 4405c18c95d2 (v7.4-rc3+) in QEMU. Applies to rdma for-next 94e81a1f3ffa, wip/leon-for-next d24184a64765 and wip/leon-for-rc e7732ec135b5.

Unpatched, the KASAN use-after-free report came in 5 of 5 runs, the hardened BUG in 4 of 4, and with neither, nfsd faulted or spun in svc_rdma_recv_ctxts_destroy() in 15 of 15; add_key() as an unprivileged user got three keys backed by one object in 1 of 6. Patched, the KASAN build flushed once in 3 of 3 runs, and the build with neither showed no fault, spin or shared key in 10 of 10.

Controls (FIN after the second Read Request, or no Read Response) flush once with and without the patch. The rdma-core tests could not
run: rdma-core 66's siw provider asserts in siw_destroy_cq() on the first test, on an unpatched kernel and with v1.

A reproducer and the test logs are available on request.

 drivers/infiniband/sw/siw/siw.h    | 2 +-
 drivers/infiniband/sw/siw/siw_qp.c | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/infiniband/sw/siw/siw.h +1 -1
index dbc998248b..82dc88b6a4 100644
@@ -394,12 +394,12 @@ struct siw_iwarp_tx {
 
 	u32 mpa_crc;
 	bool mpa_crc_enabled;
+	bool orq_fence; /* ORQ full or Send fenced: not a bitfield */
 
 	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 */
 	u8 gso_seg_limit; /* Maximum segments for GSO, 0 = unbound */
drivers/infiniband/sw/siw/siw_qp.c +4 -3
index bb780e3904..2cc0f466e7 100644
@@ -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
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

siw_sq_flush() completes a QUEUED READ in the TX slot, assuming the READ is not in the ORQ yet. That holds only while a full ORQ fences it. siw_check_tx_fence() copies a fenced READ into the ORQ when a Read Response frees a slot, and siw_activate_tx_from_sq() copies a READ into a free slot. In both cases the READ stays QUEUED until the TX path sends it, and a flush in that window completes it twice.

A peer can hold the window open. It advertises IRD 1, so the ORQ holds one READ: READ1 is in the ORQ and sent, READ2 waits fenced in the TX slot. The peer then sends Read Response 1 and a FIN in one TCP segment:

softirq (TCP receive) siw_cm_wq worker
tcp_data_queue()
|-tcp_fin()
| \-siw_cm_llp_state_change()
[ ... ]
With this patch the READ is flushed once and KASAN stays silent.

Quoted 3 lines
Fixes f29dd55b0236 ("rdma/siw: queue pair methods")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v2 1/1
#2 lore raw
In reply to Cygnus Qiu
Quoted 200 lines · Cygnus Qiu

siw_sq_flush() completes a QUEUED READ in the TX slot, assuming the READ is not in the ORQ yet. That holds only while a full ORQ fences it. siw_check_tx_fence() copies a fenced READ into the ORQ when a Read Response frees a slot, and siw_activate_tx_from_sq() copies a READ into a free slot. In both cases the READ stays QUEUED until the TX path sends it, and a flush in that window completes it twice.

A peer can hold the window open. It advertises IRD 1, so the ORQ holds one READ: READ1 is in the ORQ and sent, READ2 waits fenced in the TX slot. The peer then sends Read Response 1 and a FIN in one TCP segment:

   softirq (TCP receive)                 siw_cm_wq worker
   tcp_data_queue()
   |-tcp_fin()
   | \-siw_cm_llp_state_change()
   |   Sets tx_suspend and
   |   queues the close.
   |
   \-tcp_data_ready()
     \-siw_qp_llp_data_ready()
       |-down_read_trylock(state_lock)
       |-tcp_read_sock()
       | \-siw_tcp_rx_data()
       |   \-siw_rdmap_complete()
       |     \-siw_check_tx_fence()
       |       |-siw_read_to_orq()
       |       | READ2 enters the ORQ.
       |       |
       |       \-siw_sq_start()
       |         orq_fence is now 0.
       |         TX is suspended:
       |         READ2 stays QUEUED.
       |
       \-up_read(state_lock)
                                         siw_cm_work_handler()
                                         \-siw_qp_llp_close()
                                           |-down_write(state_lock)
                                           \-siw_sq_flush()
                                             |-siw_sqe_complete()
                                             | READ2 from the ORQ.
                                             |
                                             \-siw_sqe_complete()
                                               READ2 again, from the
                                               TX slot.

The FIN only queues the close, so the receive path normally gets qp->state_lock first.

nfsd's svcrdma gets two flush completions for one Read chunk:

   svc_rdma_wc_read_done()
   \-svc_rdma_recv_ctxt_put()
     Runs twice for the same ctxt. The second llist_add()
     links ctxt->rc_node to itself.

   svc_rdma_free()
   \-svc_rdma_recv_ctxts_destroy()
     |-svc_rdma_recv_ctxt_destroy()
     | Frees ctxt.
     |
     |-llist_del_first()
     | Reads the freed ctxt and returns it again.
     |
     \-svc_rdma_recv_ctxt_destroy()
       Frees ctxt a second time.

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 orq_fence is set. orq_fence now decides whether the TX slot completes the READ, so move it out of the bitfield it shared with tx_suspend:
siw_cm_llp_state_change() sets tx_suspend without taking a QP lock, and a lost update could drop or duplicate the completion.

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 with a two-segment Read chunk and answers as above. KASAN reports the read of the freed ctxt; here the second destroy then faults in kfree():

   BUG: KASAN: slab-use-after-free in llist_del_first+0x150/0x170
   Read of size 8 at addr ffff8881119d9000 by task nfsd/183

   Call Trace:
    <TASK>
    dump_stack_lvl+0x5d/0x80
    print_report+0x14b/0x4b0
    kasan_report+0xfa/0x120
    llist_del_first+0x150/0x170
    svc_rdma_recv_ctxts_destroy+0xf3/0x1b0
    svc_rdma_free+0xd8/0x2e0
    svc_xprt_free+0x22c/0x420
    svc_recv+0x103f/0x25b0
    nfsd+0x35c/0xc80
    kthread+0x34d/0x460
    ret_from_fork+0x339/0x620
    ret_from_fork_asm+0x1a/0x30
    </TASK>

   Allocated by task 183:
    kasan_save_stack+0x2f/0x50
    kasan_save_track+0x14/0x40
    __kasan_kmalloc+0x7f/0x90
    __kmalloc_node_noprof+0x207/0x680
    svc_rdma_post_recvs+0x664/0xbf0
    svc_rdma_accept+0xc27/0x1980
    svc_recv+0x12c2/0x25b0
    nfsd+0x35c/0xc80
    kthread+0x34d/0x460
    ret_from_fork+0x339/0x620
    ret_from_fork_asm+0x1a/0x30

   Freed by task 183:
    kasan_save_stack+0x2f/0x50
    kasan_save_track+0x14/0x40
    kasan_save_free_info+0x3b/0x60
    __kasan_slab_free+0x57/0x80
    kfree+0x162/0x500
    svc_rdma_recv_ctxts_destroy+0xe7/0x1b0
    svc_rdma_free+0xd8/0x2e0
    svc_xprt_free+0x22c/0x420
    svc_recv+0x103f/0x25b0
    nfsd+0x35c/0xc80
    kthread+0x34d/0x460
    ret_from_fork+0x339/0x620
    ret_from_fork_asm+0x1a/0x30

   The buggy address belongs to the object at ffff8881119d9000
    which belongs to the cache kmalloc-2k of size 2048
   The buggy address is located 0 bytes inside of
    freed 2048-byte region [ffff8881119d9000, ffff8881119d9800)

   BUG: unable to handle page fault for address: ffffeb860011a6c8
   #PF: supervisor read access in kernel mode
   #PF: error_code(0x0000) - not-present page
   Oops: Oops: 0000 [#1] SMP KASAN NOPTI
   RIP: 0010:kfree+0x69/0x500
   Call Trace:
    <TASK>
    svc_rdma_recv_ctxts_destroy+0xdf/0x1b0
    svc_rdma_free+0xd8/0x2e0
    svc_xprt_free+0x22c/0x420
    svc_recv+0x103f/0x25b0
    nfsd+0x35c/0xc80
    kthread+0x34d/0x460
    ret_from_fork+0x339/0x620
    ret_from_fork_asm+0x1a/0x30
    </TASK>

Without KASAN, SLAB_FREELIST_HARDENED catches the double free as "kernel BUG at mm/slub.c:605" in kfree() from svc_rdma_recv_ctxts_destroy(). With neither KASAN nor SLAB_FREELIST_HARDENED, nfsd pops the freed ctxt again. If another kmalloc-2k allocation has overwritten it by then, nfsd faults; if not, nfsd frees it again and repeats, so later allocations can get that one object many times.

With this patch the READ is flushed once and KASAN stays silent.

Fixes f29dd55b0236 ("rdma/siw: queue pair methods")

---
Changes in v2:
- Rewrite the commit message: charts for the race and the svcrdma free path, and the full KASAN report.
- Give orq_fence its own byte, as Sashiko suggested on v1. The lost update was not reproduced.
- v1: https://lore.kernel.org/r/[email protected]

Tested on mainline 4405c18c95d2 (v7.4-rc3+) in QEMU. Applies to rdma for-next 94e81a1f3ffa, wip/leon-for-next d24184a64765 and wip/leon-for-rc e7732ec135b5.

Unpatched, the KASAN use-after-free report came in 5 of 5 runs, the hardened BUG in 4 of 4, and with neither, nfsd faulted or spun in svc_rdma_recv_ctxts_destroy() in 15 of 15; add_key() as an unprivileged user got three keys backed by one object in 1 of 6. Patched, the KASAN build flushed once in 3 of 3 runs, and the build with neither showed no fault, spin or shared key in 10 of 10.

Controls (FIN after the second Read Request, or no Read Response) flush once with and without the patch. The rdma-core tests could not
run: rdma-core 66's siw provider asserts in siw_destroy_cq() on the first test, on an unpatched kernel and with v1.

A reproducer and the test logs are available on request.

  drivers/infiniband/sw/siw/siw.h    | 2 +-
  drivers/infiniband/sw/siw/siw_qp.c | 7 ++++---
  2 files changed, 5 insertions(+), 4 deletions(-)
drivers/infiniband/sw/siw/siw.h +0 -0
index dbc998248b..82dc88b6a4 100644

drivers/infiniband/sw/siw/siw.h struct siw_iwarp_tx { · line 397

Quoted 4 lines
drivers/infiniband/sw/siw/siw.h +1 -0
@@ -394,12 +394,12 @@ struct siw_iwarp_tx {
 
  	u32 mpa_crc;
  	bool mpa_crc_enabled;
+	bool orq_fence; /* ORQ full or Send fenced: not a bitfield */

"not a bitfield" is a strange /* comment */ to me. A brief explanation of why the change was made fits very well in the commit message.

Quoted 18 lines
drivers/infiniband/sw/siw/siw.h +0 -1
  	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 */
  	u8 gso_seg_limit; /* Maximum segments for GSO, 0 = unbound */
drivers/infiniband/sw/siw/siw_qp.c +0 -1
index bb780e3904..2cc0f466e7 100644
@@ -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);

Otherwise: Excellent finding, thanks!