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. Fixes: cdd0b85675ae ("RDMA/rxe: Implement memory access through MWs") Cc: stable@vger.kernel.org Signed-off-by: Dongliang Qin --- 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(-) diff --git a/drivers/infiniband/sw/rxe/rxe_loc.h b/drivers/infiniband/sw/rxe/rxe_loc.h index 64d636bf80fd2..2cbec92566f70 100644 --- a/drivers/infiniband/sw/rxe/rxe_loc.h +++ b/drivers/infiniband/sw/rxe/rxe_loc.h @@ -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 */ diff --git a/drivers/infiniband/sw/rxe/rxe_mw.c b/drivers/infiniband/sw/rxe/rxe_mw.c index bddb7a2578313..04f795adacf53 100644 --- a/drivers/infiniband/sw/rxe/rxe_mw.c +++ b/drivers/infiniband/sw/rxe/rxe_mw.c @@ -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) diff --git a/drivers/infiniband/sw/rxe/rxe_resp.c b/drivers/infiniband/sw/rxe/rxe_resp.c index 02b16e2b49b8f..f5a957026fe87 100644 --- a/drivers/infiniband/sw/rxe/rxe_resp.c +++ b/drivers/infiniband/sw/rxe/rxe_resp.c @@ -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); -- 2.43.0