RDMA/rxe: Fix MW/MR lifetime races
Subsystems (3)
v2 cover RDMA/rxe: Fix MW/MR lifetime races
Soft-RoCE keeps MW-to-MR bindings and type-2 MW-to-QP references across verbs operations and responder packets. Several paths currently assume those references remain stable without taking the MW lock or reserving the MR state. As a result, the responder can acquire a zero reference, a bind can race with MR invalidation or deregistration, a type-2 MW can outlive its QP, or the pool can force-free an object with outstanding references. An unprivileged user with access to an RXe device can use these races to corrupt kernel memory and escalate privileges.
This series fixes those races with four focused, individually revertible changes:
1. Move MW lookup, validation, and MR reference acquisition under mw->lock.
2. Use num_mw as an atomic state reservation while an MR changes state.
3. Track and invalidate type-2 MWs bound to a QP before destroying that QP.
4. Stop force-freeing sleepable pool objects after a timeout.
Patch 4 is hardening: it prevents pool cleanup from turning an outstanding reference into a use-after-free, rather than fixing the reported bind and deregistration race directly.
Before the fix, a concurrent MW bind and MR deregistration reproducer made KASAN report a slab use-after-free in rxe_mr_copy() from rxe_receiver() on the RXe responder workqueue. With this series, the same 120-second test no longer triggers KASAN. MW READ, WRITE, partial READ, invalidate, and rebind still pass.
Changes in v2:
- Patch 3 now tracks type-2 MWs on a per-QP list instead of scanning the global MW pool. This fixes the pool-element type mismatch, avoids a concurrent MW deallocation race, and eliminates the unbounded global scan.
- Patch 3 clears qp->valid and stops the send task before invalidating MWs so a late bind cannot attach an MW after invalidation.
- Patch 3 yields between MW invalidations.
- Patches 1, 2, and 4 are unchanged.
v1: https://lore.kernel.org/linux-rdma/[email protected]/
Dongliang Qin (4):
RDMA/rxe: Take MR reference under MW lock
RDMA/rxe: Reserve MR state during MW binding
RDMA/rxe: Invalidate MWs on QP destroy
RDMA/rxe: Do not force cleanup on pool timeout
drivers/infiniband/sw/rxe/rxe_loc.h | 8 +- drivers/infiniband/sw/rxe/rxe_mr.c | 70 +++++++++++++-- drivers/infiniband/sw/rxe/rxe_mw.c | 122 +++++++++++++++++++------- drivers/infiniband/sw/rxe/rxe_pool.c | 14 +-- drivers/infiniband/sw/rxe/rxe_qp.c | 2 + drivers/infiniband/sw/rxe/rxe_resp.c | 38 +------- drivers/infiniband/sw/rxe/rxe_verbs.c | 21 ++++- drivers/infiniband/sw/rxe/rxe_verbs.h | 3 + 8 files changed, 190 insertions(+), 88 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/4 RDMA/rxe: Take MR reference under MW lock
The responder validates an MW, drops it, then reads mw->mr and takes the MR reference. A bind or invalidate can swap the MR in this window, so the responder may acquire a zero reference or use an MR that has already been freed.
Move the MW lookup, validation, and MR reference acquisition into rxe_mw_get_mr(), and perform all of them while holding mw->lock.
--- drivers/infiniband/sw/rxe/rxe_loc.h | 3 ++- drivers/infiniband/sw/rxe/rxe_mw.c | 34 +++++++++++++++++-------- drivers/infiniband/sw/rxe/rxe_resp.c | 38 +++------------------------- 3 files changed, 28 insertions(+), 47 deletions(-)
drivers/infiniband/sw/rxe/rxe_loc.h +2 -1
@@ -86,7 +86,8 @@ int rxe_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata);int rxe_dealloc_mw (struct ib_mw *ibmw);int rxe_bind_mw (struct rxe_qp *qp,struct rxe_send_wqe *wqe);int rxe_invalidate_mw (struct rxe_qp *qp,u32 rkey); -struct rxe_mw *rxe_lookup_mw (struct rxe_qp *qp,int access,u32 rkey); +struct rxe_mr *rxe_mw_get_mr (struct rxe_qp *qp,int access,u32 rkey, +u64 *offset);void rxe_mw_cleanup (struct rxe_pool_elem *elem);/* rxe_net.c */
drivers/infiniband/sw/rxe/rxe_mw.c +23 -11
@@ -291,26 +291,38 @@ int rxe_invalidate_mw(struct rxe_qp *qp, u32 rkey)return ret; } -struct rxe_mw *rxe_lookup_mw (struct rxe_qp *qp,int access,u32 rkey) +struct rxe_mr *rxe_mw_get_mr (struct rxe_qp *qp,int access,u32 rkey, +u64 *offset) {struct rxe_dev *rxe =to_rdev (qp->ibqp.device); -struct rxe_pd *pd =to_rpd (qp->ibqp.pd); +struct rxe_mr *mr =NULL ;struct rxe_mw *mw; -int index = rkey >>8 ; - mw =rxe_pool_get_index (&rxe->mw_pool, index); + mw =rxe_pool_get_index (&rxe->mw_pool, rkey >>8 );if (!mw)return NULL ; -if (unlikely ((mw->rkey != rkey) ||rxe_mw_pd (mw) != pd || - (mw->ibmw.type ==IB_MW_TYPE_2 && mw->qp != qp) || - (mw->length ==0 ) || ((access & mw->access) != access) || - mw->state !=RXE_MW_STATE_VALID )) { -rxe_put (mw); -return NULL ; +spin_lock_bh (&mw->lock); + +if (mw->rkey == rkey &&rxe_mw_pd (mw) ==to_rpd (qp->ibqp.pd) && + (mw->ibmw.type !=IB_MW_TYPE_2 || mw->qp == qp) && + mw->length !=0 && mw->state ==RXE_MW_STATE_VALID && + (access & mw->access) == access) { + mr = mw->mr; +if (mr && mr->state ==RXE_MR_STATE_VALID &&rxe_get (mr)) { +if (offset) + *offset = (mw->access &IB_ZERO_BASED ) ? + mw->addr :0 ; + }else { + mr =NULL ; + } } -return mw; +spin_unlock_bh (&mw->lock); + +rxe_put (mw); + +return mr; }void rxe_mw_cleanup (struct rxe_pool_elem *elem)
drivers/infiniband/sw/rxe/rxe_resp.c +3 -35
@@ -468,7 +468,6 @@ static enum resp_states check_rkey(struct rxe_qp *qp, struct rxe_pkt_info *pkt) {struct rxe_mr *mr =NULL ; -struct rxe_mw *mw =NULL ;u64 va;u32 rkey;u32 resid;
@@ -520,26 +519,12 @@ static enum resp_states check_rkey(struct rxe_qp *qp, pktlen =payload_size (pkt);if (rkey_is_mw (rkey)) { - mw =rxe_lookup_mw (qp, access, rkey); -if (!mw) { -rxe_dbg_qp (qp,"no MW matches rkey %#x\n" , rkey); - state =get_rkey_violation_state (pkt); - goto err; - } - - mr = mw->mr; + mr =rxe_mw_get_mr (qp, access, rkey, &qp->resp.offset);if (!mr) { -rxe_dbg_qp (qp,"MW doesn't have an MR\n" ); +rxe_dbg_qp (qp,"no MW/MR matches rkey %#x\n" , rkey); state =get_rkey_violation_state (pkt); goto err; } - -if (mw->access &IB_ZERO_BASED ) - qp->resp.offset = mw->addr; - -rxe_get (mr); -rxe_put (mw); - mw =NULL ; }else { mr =lookup_mr (qp->pd, access, rkey,RXE_LOOKUP_REMOTE );if (!mr) {
@@ -605,8 +590,6 @@ static enum resp_states check_rkey(struct rxe_qp *qp, qp->resp.mr =NULL ;if (mr)rxe_put (mr); -if (mw) -rxe_put (mw);return state; }
@@ -894,24 +877,9 @@ static struct rxe_mr *rxe_recheck_mr(struct rxe_qp *qp, u32 rkey) {struct rxe_dev *rxe =to_rdev (qp->ibqp.device);struct rxe_mr *mr; -struct rxe_mw *mw;if (rkey_is_mw (rkey)) { - mw =rxe_pool_get_index (&rxe->mw_pool, rkey >>8 ); -if (!mw) -return NULL ; - - mr = mw->mr; -if (mw->rkey != rkey || mw->state !=RXE_MW_STATE_VALID || - !mr || mr->state !=RXE_MR_STATE_VALID ) { -rxe_put (mw); -return NULL ; - } - -rxe_get (mr); -rxe_put (mw); - -return mr; +return rxe_mw_get_mr (qp,IB_ACCESS_REMOTE_READ , rkey,NULL ); } mr =rxe_pool_get_index (&rxe->mr_pool, rkey >>8 );
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 1/4] RDMA/rxe: Take MR reference under MW lock
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
The responder validates an MW, drops it, then reads
mw->mrand takes the MR reference. A bind or invalidate can swap the MR in this window, so the responder may acquire a zero reference or use an MR that has already been freed.
Quoted 7 lines
Move the MW lookup, validation, and MR reference acquisition into
rxe_mw_get_mr(), and perform all of them while holdingmw->lock.Fixes cdd0b85675ae ("RDMA/rxe: Implement memory access through MWs")Signed-off-by Dongliang Qin <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/4 RDMA/rxe: Reserve MR state during MW binding
In reply to Dongliang QinMR invalidation, fast registration, reregistration, and deregistration only test num_mw before changing MR state. A concurrent type-2 MW bind can increment num_mw after that test and leave an MW bound to an MR that is being destroyed.
Reserve num_mw == -1 while the MR state is changing, and allow binds only while num_mw is nonnegative. This keeps the MW binding count and MR lifetime synchronized without adding a new object.
An unprivileged user with access to the device can use this race to corrupt kernel memory and escalate privileges.
A concurrent bind and deregistration reproducer made KASAN report:
BUG: KASAN: slab-use-after-free in rxe_mr_copy+0xd5/0x3d0 Read of size 1 at addr ff11000105d217b0 by task kworker/u16:3/44 Workqueue: rxe_wq do_work Call Trace: rxe_mr_copy+0xd5/0x3d0 rxe_receiver+0x1b50/0x3ed0 do_work+0xbb/0x250 process_one_work+0x412/0x780 worker_thread+0x341/0x5b0 kthread+0x1b8/0x210 Freed by task 1151: kasan_save_stack+0x33/0x60 kfree+0x17c/0x450 rxe_mr_cleanup+0x3c/0x90 __rxe_cleanup+0x145/0x1e0 rxe_dereg_mr+0x4e/0x110 ib_dereg_mr_user+0xa4/0x1a0 ib_uverbs_ioctl+0x12f/0x1c0
--- drivers/infiniband/sw/rxe/rxe_loc.h | 4 ++ drivers/infiniband/sw/rxe/rxe_mr.c | 70 +++++++++++++++++++++++++-- drivers/infiniband/sw/rxe/rxe_mw.c | 44 +++++++++-------- drivers/infiniband/sw/rxe/rxe_verbs.c | 13 ++++- 4 files changed, 105 insertions(+), 26 deletions(-)
drivers/infiniband/sw/rxe/rxe_loc.h +4 -0
@@ -72,6 +72,10 @@ enum resp_states rxe_mr_do_atomic_op(struct rxe_mr *mr, u64 iova, int opcode,enum resp_states rxe_mr_do_atomic_write (struct rxe_mr *mr,u64 iova,u64 value);struct rxe_mr *lookup_mr (struct rxe_pd *pd,int access,u32 key,enum rxe_mr_lookup_type type); +int rxe_mr_get_mw (struct rxe_mr *mr); +void rxe_mr_put_mw (struct rxe_mr *mr); +bool rxe_mr_reserve_mw_state (struct rxe_mr *mr); +void rxe_mr_release_mw_state (struct rxe_mr *mr);int mr_check_range (struct rxe_mr *mr,u64 iova,size_t length);int advance_dma_data (struct rxe_dma_info *dma,unsigned int length);int rxe_invalidate_mr (struct rxe_qp *qp,u32 key);
drivers/infiniband/sw/rxe/rxe_mr.c +65 -5
120 lines not on this page · raw message
drivers/infiniband/sw/rxe/rxe_mw.c +25 -19
81 lines not on this page · raw message
drivers/infiniband/sw/rxe/rxe_verbs.c +11 -2
@@ -1337,6 +1337,11 @@ static struct ib_mr *rxe_rereg_user_mr(struct ib_mr *ibmr, int flags,return ERR_PTR (-EOPNOTSUPP ); } +if (!rxe_mr_reserve_mw_state (mr)) { +rxe_err_mr (mr,"mr has mws bound\n" ); +return ERR_PTR (-EINVAL ); + } +if (flags &IB_MR_REREG_PD ) {rxe_put (old_pd);rxe_get (pd);
@@ -1346,6 +1351,8 @@ static struct ib_mr *rxe_rereg_user_mr(struct ib_mr *ibmr, int flags,if (flags &IB_MR_REREG_ACCESS ) mr->access = access; +rxe_mr_release_mw_state (mr); +return NULL ; }
@@ -1401,13 +1408,15 @@ static int rxe_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)struct rxe_mr *mr =to_rmr (ibmr);int err, cleanup_err; -/* See IBA 10.6.7.2.6 */ -if (atomic_read (&mr->num_mw) >0 ) { +/* See IBA 10.6.7.2.6. Leave num_mw set to -1 for destruction. */ +if (!rxe_mr_reserve_mw_state (mr)) { err = -EINVAL ;rxe_dbg_mr (mr,"mr has mw's bound\n" ); goto err_out; } + mr->state =RXE_MR_STATE_INVALID ; + cleanup_err =rxe_cleanup (mr);if (cleanup_err)rxe_err_mr (mr,"cleanup failed, err = %d\n" , cleanup_err);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 2/4] RDMA/rxe: Reserve MR state during MW binding
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
MR invalidation, fast registration, reregistration, and deregistration only test
num_mwbefore changing MR state. A concurrent type-2 MW bind can incrementnum_mwafter that test and leave an MW bound to an MR that is being destroyed.Reserve
num_mw== -1 while the MR state is changing, and allow binds only whilenum_mwis nonnegative. This keeps the MW binding count and MR lifetime synchronized without adding a new object.An unprivileged user with access to the device can use this race to corrupt kernel memory and escalate privileges.
A concurrent bind and deregistration reproducer made KASAN report:
Quoted 7 lines
BUG: KASAN: slab-use-after-free in rxe_mr_copy+0xd5/0x3d0[ ... ]
Fixes 32a577b4c3a9 ("RDMA/rxe: Add support for bind MW work requests")Signed-off-by Dongliang Qin <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/4 RDMA/rxe: Invalidate MWs on QP destroy
In reply to Dongliang QinA type-2 MW holds a reference to the QP that bound it. If the QP is destroyed while an MW is still bound, the MW keeps the QP alive and the responder can retain a stale QP association.
Scanning the global MW pool to find these MWs is unsafe: pool entries are struct rxe_pool_elem pointers, and concurrent deallocation can invalidate the object being visited. It is also unnecessary because only MWs bound to the QP being destroyed are relevant.
Track type-2 MWs on a per-QP list. Clear qp->valid and stop the send task before invalidating the list so a late bind cannot attach a new MW after invalidation. Yield between MWs to avoid spending a long time in a non-preemptible loop when a QP has many bound MWs.
---
Changes in v2:
- Track type-2 MWs bound to a QP in a per-QP list instead of scanning the global MW pool.
- Fix the XArray iterator type mismatch by no longer treating pool elements as struct rxe_mw pointers.
- Clear qp->valid and stop the send task before invalidating MWs so a late bind cannot attach a new MW after invalidation.
- Yield between MW invalidations to avoid long non-preemptible loops.
v1: https://lore.kernel.org/linux-rdma/[email protected]/
drivers/infiniband/sw/rxe/rxe_loc.h | 1 +drivers/infiniband/sw/rxe/rxe_mw.c | 44 +++++++++++++++++++++++++--
drivers/infiniband/sw/rxe/rxe_qp.c | 2 ++ drivers/infiniband/sw/rxe/rxe_verbs.c | 8 +++++ drivers/infiniband/sw/rxe/rxe_verbs.h | 3 ++ 5 files changed, 55 insertions(+), 3 deletions(-)
drivers/infiniband/sw/rxe/rxe_loc.h +1 -0
@@ -90,6 +90,7 @@ int rxe_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata);int rxe_dealloc_mw (struct ib_mw *ibmw);int rxe_bind_mw (struct rxe_qp *qp,struct rxe_send_wqe *wqe);int rxe_invalidate_mw (struct rxe_qp *qp,u32 rkey); +void rxe_invalidate_mws (struct rxe_qp *qp);struct rxe_mr *rxe_mw_get_mr (struct rxe_qp *qp,int access,u32 rkey,u64 *offset);void rxe_mw_cleanup (struct rxe_pool_elem *elem);
drivers/infiniband/sw/rxe/rxe_mw.c +41 -3
70 lines not on this page · raw message
drivers/infiniband/sw/rxe/rxe_qp.c +2 -0
@@ -220,6 +220,8 @@ static void rxe_qp_init_misc(struct rxe_dev *rxe, struct rxe_qp *qp, }spin_lock_init (&qp->state_lock); +spin_lock_init (&qp->mw_lock); +INIT_LIST_HEAD (&qp->mw_list);spin_lock_init (&qp->sq.sq_lock);spin_lock_init (&qp->rq.producer_lock);
drivers/infiniband/sw/rxe/rxe_verbs.c +8 -0
@@ -650,6 +650,7 @@ static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,static int rxe_destroy_qp (struct ib_qp *ibqp,struct ib_udata *udata) {struct rxe_qp *qp =to_rqp (ibqp); +unsigned long flags;int err; err =rxe_qp_chk_destroy (qp);
@@ -658,6 +659,13 @@ static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) goto err_out; } +spin_lock_irqsave (&qp->state_lock, flags); + qp->valid =0 ; +spin_unlock_irqrestore (&qp->state_lock, flags); + +rxe_disable_task (&qp->send_task); +rxe_invalidate_mws (qp); + err =rxe_cleanup (qp);if (err)rxe_err_qp (qp,"cleanup failed, err = %d\n" , err);
drivers/infiniband/sw/rxe/rxe_verbs.h +3 -0
@@ -288,6 +288,8 @@ struct rxe_qp {struct timer_list rnr_nak_timer;spinlock_t state_lock;/* guard requester and completer */ +spinlock_t mw_lock; +struct list_head mw_list;struct execute_work cleanup_work; };
@@ -387,6 +389,7 @@ struct rxe_mw {int access;u64 addr;u64 length; +struct list_head qp_list; };struct rxe_mcg {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 3/4] RDMA/rxe: Invalidate MWs on QP destroy
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
A type-2 MW holds a reference to the QP that bound it. If the QP is destroyed while an MW is still bound, the MW keeps the QP alive and the responder can retain a stale QP association.
Scanning the global MW pool to find these MWs is unsafe: pool entries are struct
rxe_pool_elempointers, and concurrent deallocation can invalidate the object being visited. It is also unnecessary because only MWs bound to the QP being destroyed are relevant.Track type-2 MWs on a per-QP list. Clear
qp->validand stop the send task before invalidating the list so a late bind cannot attach a new MW after invalidation. Yield between MWs to avoid spending a long time in a non-preemptible loop when a QP has many bound MWs.
Quoted 4 lines
Fixes 32a577b4c3a9 ("RDMA/rxe: Add support for bind MW work requests")Signed-off-by Dongliang Qin <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/4 RDMA/rxe: Do not force cleanup on pool timeout
In reply to Dongliang QinThe sleepable pool cleanup path waits up to 50 seconds for the last reference and then continues cleanup anyway. If a reference is still held, that turns a lifetime bug into a use-after-free.
Wait for completion instead. A stuck object is easier to diagnose than a stale pointer; the existing non-sleepable path is unchanged.
--- drivers/infiniband/sw/rxe/rxe_pool.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/infiniband/sw/rxe/rxe_pool.c +2 -12
@@ -178,7 +178,7 @@ int __rxe_cleanup(struct rxe_pool_elem *elem, bool sleepable) {struct rxe_pool *pool = elem->pool;struct xarray *xa = &pool->xa; -int ret, err =0 ; +int err =0 ;void *xa_ret;if (sleepable)
@@ -201,17 +201,7 @@ int __rxe_cleanup(struct rxe_pool_elem *elem, bool sleepable)* return to rdma-core */ if (sleepable) { -if (!completion_done (&elem->complete)) { - ret =wait_for_completion_timeout (&elem->complete, -msecs_to_jiffies (50000 )); - -/* Shouldn't happen. There are still references to -* the object but, rather than deadlock, free the -* object or pass back to rdma-core. -*/ -if (WARN_ON (!ret)) - err = -ETIMEDOUT ; - } +wait_for_completion (&elem->complete); }else {unsigned long until = jiffies +RXE_POOL_TIMEOUT ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 4/4] RDMA/rxe: Do not force cleanup on pool timeout
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
The sleepable pool cleanup path waits up to 50 seconds for the last reference and then continues cleanup anyway. If a reference is still held, that turns a lifetime bug into a use-after-free.
Wait for completion instead. A stuck object is easier to diagnose than a stale pointer; the existing non-sleepable path is unchanged.
Fixes 215d0a755e1b ("RDMA/rxe: Stop lookup of partially built objects")Signed-off-by Dongliang Qin <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Any comments on this patch ?
Quoted 1 line · Dongliang Qin
Any comments on this patch ?
It is hard to say anything meaningful for AI-generated RXE patches.
Thanks
Quoted 4 lines · Leon Romanovsky
Quoted 1 line · Dongliang Qin
Any comments on this patch ?
It is hard to say anything meaningful for AI-generated RXE patches.
And it doesn't apply.
Thanks
Quoted 3 lines
Thanks