RDMA/irdma: Wait for all refs to drop when destroying RoCE QPs
Subsystems (2)
PATCH 1/2 RDMA/irdma: Wait for all refs to drop when destroying RoCE QPs
Commit 6f52370970ac ("RDMA/irdma: Fix deadlock during netdev reset with active connections") made QP destruction skip the refcount wait if the device is being reset in order to fix a deadlock that could occur for iWARP mode QPs due to refs still being held by the CM.
RoCE mode QPs are not subject to that issue and should still wait for all of the references to be dropped. Otherwise, the QP could be freed while the AEQ handler or disconnect work still holds a reference, leading to a UAF. In RoCE mode, any remaining references held by the AEQ handler/disconnect work are always transient so the wait is bounded.
Fix this by restoring the original logic for RoCE QPs.
--- drivers/infiniband/hw/irdma/verbs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/irdma/verbs.c +1 -1
@@ -592,7 +592,7 @@ static void _irdma_destroy_qp(struct ib_qp *ibqp) }irdma_qp_rem_ref (&iwqp->ibqp); -if (!iwdev->rf->reset) +if (!iwdev->rf->reset || iwdev->roce_mode)wait_for_completion (&iwqp->free_qp);irdma_free_lsmm_rsrc (iwqp);irdma_cqp_qp_destroy_cmd (&iwdev->rf->sc_dev, &iwqp->sc_qp);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 2/2 RDMA/irdma: Scrub software flush completions on QP destroy
From: Owen Gentry <[email protected]>
When a kernel QP transitions to error state, irdma_flush_worker() can append software-generated flush completions. During QP teardown, irdma scrubs HW CQEs but leaves SW CQEs intact, causing subsequent CQ polling to dereference the freed QP.
In addition, _irdma_destroy_qp() calls irdma_modify_qp_to_err() before cancelling iwqp->dwork_flush with cancel_delayed_work_sync(), which needlessly arms the flush work during teardown and leaves a window where concurrent paths can re-schedule dwork_flush after CQEs are scrubbed and before the QP is freed.
Disable and drain iwqp->dwork_flush via disable_delayed_work_sync() before calling irdma_modify_qp_to_err(), and free matching SW CQEs from iwcq->cmpl_generated in irdma_clean_cqes().
--- drivers/infiniband/hw/irdma/verbs.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/infiniband/hw/irdma/verbs.c +10 -3
@@ -514,10 +514,17 @@ static struct irdma_pbl *irdma_get_pbl(unsigned long va,static void irdma_clean_cqes (struct irdma_qp *iwqp,struct irdma_cq *iwcq) {struct irdma_cq_uk *ukcq = &iwcq->sc_cq.cq_uk; +struct irdma_cmpl_gen *cmpl, *tmp;unsigned long flags;spin_lock_irqsave (&iwcq->lock, flags);irdma_uk_clean_cq (&iwqp->sc_qp.qp_uk, ukcq); +list_for_each_entry_safe (cmpl, tmp, &iwcq->cmpl_generated, list) { +if (cmpl->cpi.qp_handle != &iwqp->sc_qp.qp_uk) +continue ; +list_del (&cmpl->list); +kfree (cmpl); + }spin_unlock_irqrestore (&iwcq->lock, flags); }
@@ -577,12 +584,12 @@ static void _irdma_destroy_qp(struct ib_qp *ibqp) iwqp->sc_qp.qp_uk.destroy_pending = true; +if (!iwqp->user_mode) +disable_delayed_work_sync (&iwqp->dwork_flush); +if (iwqp->iwarp_state >=IRDMA_QP_STATE_IDLE )irdma_modify_qp_to_err (&iwqp->sc_qp); -if (!iwqp->user_mode) -cancel_delayed_work_sync (&iwqp->dwork_flush); -if (!iwqp->user_mode) {if (iwqp->iwscq) {irdma_clean_cqes (iwqp, iwqp->iwscq);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] RDMA/irdma: use-after-free from…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] RDMA/irdma: use-after-free from incomplete CQ scrub in irdma_clean_cqes() [1]
--
commit ec08c5024cb50836ff76a6f7d89b61c4ac172b85
Author: Jacob Moroni <[email protected]>
RDMA/irdma: Wait for all refs to drop when destroying RoCE QPs
This commit restores waiting on iwqp->free_qp when destroying RoCE QPs during a device reset so that transient references from the AEQ handler or disconnect work finish before the QP is freed.
drivers/infiniband/hw/irdma/verbs.c static void _irdma_destroy_qp(struct ib_qp *ibqp) · line 598
Quoted 13 lines
drivers/infiniband/hw/irdma/verbs.c +1
-1@@ -592,7 +592,7 @@ static void _irdma_destroy_qp(struct ib_qp *ibqp) }irdma_qp_rem_ref (&iwqp->ibqp); -if (!iwdev->rf->reset) +if (!iwdev->rf->reset || iwdev->roce_mode)wait_for_completion (&iwqp->free_qp);irdma_free_lsmm_rsrc (iwqp);irdma_cqp_qp_destroy_cmd (&iwdev->rf->sc_dev, &iwqp->sc_qp);
[Severity: High]
This problem wasn't introduced by this patch, but can _irdma_destroy_qp() leave behind stale completions for the QP on iwcq->cmpl_generated and iwcq->resize_list [1]?
Earlier in _irdma_destroy_qp(), transitioning a kernel QP to error can queue software flush completions onto iwcq->cmpl_generated, and earlier irdma_resize_cq() calls can leave unpolled CQ buffers on iwcq->resize_list. However, irdma_clean_cqes() only scrubs the active CQ ring:
drivers/infiniband/hw/irdma/verbs.c:irdma_clean_cqes() {
struct irdma_cq_uk *ukcq = &iwcq->sc_cq.cq_uk;
unsigned long flags;
spin_lock_irqsave(&iwcq->lock, flags);
irdma_uk_clean_cq(&iwqp->sc_qp.qp_uk, ukcq);
spin_unlock_irqrestore(&iwcq->lock, flags);
}After _irdma_destroy_qp() frees the QP, a subsequent irdma_poll_cq() can pull those remaining entries and dereference the dangling qp_handle in irdma_uk_cq_poll_cmpl() and irdma_process_cqe(), causing a use-after-free of struct irdma_qp.
[1] https://sashiko.dev/#/bug/linux-e3f313d2-d1fd-48a1-8be3-bb6bfb32a341
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
The cmpl_generated part is what patch 2/2 of this series fixes. It disables the flush work before the QP is moved to error and then scrubs the QP's entries from cmpl_generated.
The resize_list part is not reachable. Entries are only added there when irdma_resize_cq() is called without udata, i.e. for a kernel CQ, and there is no caller for that since commit 6c45efd8f9bb ("RDMA/core: Remove unused ib_resize_cq() implementation").
In fact, a lot of "resize CQ" related logic can be removed from IRDMA now that it's not a supported kernel verb. I'll add it to my TODOs.
Thanks,
Jake
[email protected]
AI reviewSashiko has finished reviewing this 2-patch series:
Sashiko has finished reviewing this 2-patch series:
- Patch 2: no issues found.
- Patch 1 ("RDMA/irdma: Wait for all refs to drop when destroyin..."): 1 related pre-existing bug noted
A detailed review has been sent as a separate reply to patch 1.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Leon Romanovsky
AppliedQuoted 5 lines · Jacob Moroni
Commit
6f52370970ac("RDMA/irdma: Fix deadlock during netdev reset with active connections") made QP destruction skip the refcount wait if the device is being reset in order to fix a deadlock that could occur for iWARP mode QPs due to refs still being held by the CM.
Quoted 8 lines
RoCE mode QPs are not subject to that issue and should still wait for all of the references to be dropped. Otherwise, the QP could be freed while the AEQ handler or disconnect work still holds a reference, leading to a UAF. In RoCE mode, any remaining references held by the AEQ handler/disconnect work are always transient so the wait is bounded.
[...]
Applied, thanks!
[1/2] RDMA/irdma: Wait for all refs to drop when destroying RoCE QPs
https://git.kernel.org/rdma/rdma/c/f33738f9319fd0 [2/2] RDMA/irdma: Scrub software flush completions on QP destroy https://git.kernel.org/rdma/rdma/c/78d9dd829f1250
Best regards,
Signature · 1 line
Leon Romanovsky <[email protected]>