[PATCH net-next v1 2/4] net/mlx5: Don't free the WC test CQ if destroying it fails

From: Yuho Choi

Date: Fri Oct 16 2026 - 01:00:03 EST


mlx5_wc_destroy_cq() ignores the result of mlx5_core_destroy_cq() and
frees the CQ buffer and doorbell record anyway, and mlx5_core_test_wc()
then frees the struct mlx5_wc_sq that embeds the CQ. If DESTROY_CQ
fails, the CQ may still exist in firmware and retain access to its
buffer and doorbell record. mlx5_core_destroy_cq() also returns early in
that case, without waiting for EQ handlers that may already hold a
reference to the CQ.

Return the error from mlx5_wc_destroy_cq() and keep both the buffer and
the struct mlx5_wc_sq when it fails, as the regular mlx5_ib CQ destroy
path does. A device in internal error is not affected, since the mlx5
core reports success for DESTROY_CQ in that state.

Fixes: d98995b4bf98 ("net/mlx5: Reimplement write combining test")
Assisted-by: LLM
Signed-off-by: Yuho Choi <oss.patchbox@xxxxxxxxx>
---
This was found with a static analysis tool from ongoing, not yet
published research, and confirmed by code inspection. It has not been
reproduced on hardware.

drivers/net/ethernet/mellanox/mlx5/core/wc.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/wc.c b/drivers/net/ethernet/mellanox/mlx5/core/wc.c
index 7d3d4d739e2cd..df1b1555ed8f4 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/wc.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/wc.c
@@ -148,10 +148,14 @@ static int mlx5_wc_create_cq(struct mlx5_core_dev *mdev, struct mlx5_wc_cq *cq)
return err;
}

-static void mlx5_wc_destroy_cq(struct mlx5_wc_cq *cq)
+static int mlx5_wc_destroy_cq(struct mlx5_wc_cq *cq)
{
- mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
- mlx5_wq_destroy(&cq->wq_ctrl);
+ int err = mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
+
+ /* The device may still access an undestroyed CQ's buffer; keep it. */
+ if (!err)
+ mlx5_wq_destroy(&cq->wq_ctrl);
+ return err;
}

static int create_wc_sq(struct mlx5_core_dev *mdev, void *sqc_data,
@@ -360,6 +364,7 @@ static int mlx5_wc_poll_cq(struct mlx5_wc_sq *sq)
static void mlx5_core_test_wc(struct mlx5_core_dev *mdev)
{
unsigned int offset = 0;
+ bool keep_sq = false;
struct mlx5_wc_sq *sq;
int i, err;

@@ -396,11 +401,13 @@ static void mlx5_core_test_wc(struct mlx5_core_dev *mdev)
mlx5_wc_destroy_sq(sq);

err_create_sq:
- mlx5_wc_destroy_cq(&sq->cq);
+ /* An undestroyed CQ may still be in use; keep the SQ that holds it. */
+ keep_sq = mlx5_wc_destroy_cq(&sq->cq);
err_create_cq:
mlx5_free_bfreg(mdev, &sq->bfreg);
err_alloc_bfreg:
- kfree(sq);
+ if (!keep_sq)
+ kfree(sq);

if (mdev->wc_state == MLX5_WC_STATE_UNSUPPORTED)
mlx5_core_warn(mdev, "Write combining is not supported\n");
--
2.43.0