Skip to contentSkip to conversation

RDMA/mlx5: Fix NULL pointer dereference on INTEGRITY MR dereg retry

PATCH In review Tao Cui 2 participants 2 messages Jun 8 – Jun 9

Subsystems (2)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 1/1 RDMA/mlx5: Fix NULL pointer dereference on INTEGRITY MR dereg retry

From: Tao Cui <[email protected]>

In __mlx5_ib_dereg_mr(), the INTEGRITY MR cleanup block destroys PSVs and frees mr->sig before calling mlx5r_handle_mkey_cleanup(). If the mkey destroy fails and the function is retried, mr->sig is already NULL but the PSV destroy code dereferences it unconditionally.

Add a NULL check on mr->sig to guard the PSV destroy and kfree block, making the retry path safe. This is consistent with the existing NULL checks on mr->mtt_mr and mr->klm_mr in the same cleanup block.

Fixes e6fb246ccafb ("RDMA/mlx5: Consolidate MR destruction to mlx5_ib_dereg_mr()")
---
 drivers/infiniband/hw/mlx5/mr.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +12 -9
index 254e6aa4ccaf..3b5216752017 100644
@@ -1436,15 +1436,18 @@ static int __mlx5_ib_dereg_mr(struct ib_mr *ibmr)
 			mr->klm_mr = NULL;
 		}
 
-		if (mlx5_core_destroy_psv(dev->mdev,
-					  mr->sig->psv_memory.psv_idx))
-			mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
-				     mr->sig->psv_memory.psv_idx);
-		if (mlx5_core_destroy_psv(dev->mdev, mr->sig->psv_wire.psv_idx))
-			mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
-				     mr->sig->psv_wire.psv_idx);
-		kfree(mr->sig);
-		mr->sig = NULL;
+		if (mr->sig) {
+			if (mlx5_core_destroy_psv(dev->mdev,
+						  mr->sig->psv_memory.psv_idx))
+				mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
+					     mr->sig->psv_memory.psv_idx);
+			if (mlx5_core_destroy_psv(dev->mdev,
+						  mr->sig->psv_wire.psv_idx))
+				mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
+					     mr->sig->psv_wire.psv_idx);
+			kfree(mr->sig);
+			mr->sig = NULL;
+		}
 	}
 
 	/* Stop DMA */
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Tao Cui

From: Tao Cui <[email protected]>

In __mlx5_ib_dereg_mr(), the INTEGRITY MR cleanup block destroys PSVs and frees mr->sig before calling mlx5r_handle_mkey_cleanup(). If the mkey destroy fails and the function is retried, mr->sig is already NULL but the PSV destroy code dereferences it unconditionally.

Add a NULL check on mr->sig to guard the PSV destroy and kfree block, making the retry path safe. This is consistent with the existing NULL checks on mr->mtt_mr and mr->klm_mr in the same cleanup block.

This is unnecessary defensive coding.
The kernel verbs contract is that destroy should never fails for kernel callers, and ULPs do not retry on failure. That already holds in-tree: no kernel caller inspects the dereg return value for anything beyond logging.

This convention is already present in the code for other verbs, see ib_destroy_cq() in include/rdma/ib_verbs.h, which WARN_ONCEs on kernel failure ("Destroy of kernel CQ shouldn't fail").

The motivation is spelled out in commit 43d781b9fa56 ("RDMA: Allow fail of destroy CQ").

Michael

Quoted 38 lines
Fixes e6fb246ccafb ("RDMA/mlx5: Consolidate MR destruction to mlx5_ib_dereg_mr()")
---
  drivers/infiniband/hw/mlx5/mr.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +12 -9
index 254e6aa4ccaf..3b5216752017 100644
@@ -1436,15 +1436,18 @@ static int __mlx5_ib_dereg_mr(struct ib_mr *ibmr)
  			mr->klm_mr = NULL;
  		}
 
-		if (mlx5_core_destroy_psv(dev->mdev,
-					  mr->sig->psv_memory.psv_idx))
-			mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
-				     mr->sig->psv_memory.psv_idx);
-		if (mlx5_core_destroy_psv(dev->mdev, mr->sig->psv_wire.psv_idx))
-			mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
-				     mr->sig->psv_wire.psv_idx);
-		kfree(mr->sig);
-		mr->sig = NULL;
+		if (mr->sig) {
+			if (mlx5_core_destroy_psv(dev->mdev,
+						  mr->sig->psv_memory.psv_idx))
+				mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
+					     mr->sig->psv_memory.psv_idx);
+			if (mlx5_core_destroy_psv(dev->mdev,
+						  mr->sig->psv_wire.psv_idx))
+				mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
+					     mr->sig->psv_wire.psv_idx);
+			kfree(mr->sig);
+			mr->sig = NULL;
+		}
  	}
 
  	/* Stop DMA */