Skip to contentSkip to conversation

common/mlx5: fix shared MR ranges allocation

PATCH In review Bing Zhao 2 participants 2 messages Nov 23, 2021

lore
Share & tools
lore

Bing Zhao

author
#0 lore raw

PATCH 1/1 common/mlx5: fix shared MR ranges allocation

Memory regions (MRs) were allocated in one chunk of memory with a mempool registration object. However, MRs can be reused among different mempool registrations.

When the registration that allocated the MRs originally was destroyed, the dangling pointers to the MRs could be left in other registrations sharing these MRs.

Splitting the memory allocation of registration structure and MRs in this commit solves this pointer reference issue.

Fixes 690b2a88c2f7 ("common/mlx5: add mempool registration facilities")
---
 drivers/common/mlx5/mlx5_common_mr.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_common_mr.c +11 -2
index 47121d8ff7..01f35ebcdf 100644
@@ -1515,15 +1515,23 @@ mlx5_mempool_reg_create(struct rte_mempool *mp, unsigned int mrs_n,
 	struct mlx5_mempool_reg *mpr = NULL;
 
 	mpr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
-			  sizeof(*mpr) + mrs_n * sizeof(mpr->mrs[0]),
+			  sizeof(struct mlx5_mempool_reg),
 			  RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
 	if (mpr == NULL) {
 		DRV_LOG(ERR, "Cannot allocate mempool %s registration object",
 			mp->name);
 		return NULL;
 	}
+	mpr->mrs = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
+			       mrs_n * sizeof(struct mlx5_mempool_mr),
+			       RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
+	if (!mpr->mrs) {
+		DRV_LOG(ERR, "Cannot allocate mempool %s registration MRs",
+			mp->name);
+		mlx5_free(mpr);
+		return NULL;
+	}
 	mpr->mp = mp;
-	mpr->mrs = (struct mlx5_mempool_mr *)(mpr + 1);
 	mpr->mrs_n = mrs_n;
 	mpr->is_extmem = is_extmem;
 	return mpr;
@@ -1544,6 +1552,7 @@ mlx5_mempool_reg_destroy(struct mlx5_mr_share_cache *share_cache,
 
 		for (i = 0; i < mpr->mrs_n; i++)
 			share_cache->dereg_mr_cb(&mpr->mrs[i].pmd_mr);
+		mlx5_free(mpr->mrs);
 	}
 	mlx5_free(mpr);
 }
Signature · 1 line
2.27.0
#1 lore raw

Hi,

Quoted 17 lines

-----Original Message-----
From: Bing Zhao <[email protected]>
Sent: Tuesday, November 23, 2021 2:49 PM
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>
Cc: [email protected]; Raslan Darawsheh <[email protected]>; Dmitry Kozlyuk <[email protected]>
Subject: [PATCH] common/mlx5: fix shared MR ranges allocation

Memory regions (MRs) were allocated in one chunk of memory with a mempool registration object. However, MRs can be reused among different mempool registrations.

When the registration that allocated the MRs originally was destroyed, the dangling pointers to the MRs could be left in other registrations sharing these MRs.

Quoted 8 lines

Splitting the memory allocation of registration structure and MRs in this commit solves this pointer reference issue.

Fixes 690b2a88c2f7 ("common/mlx5: add mempool registration facilities")

Patch applied to next-net-mlx,

Kindest regards,
Raslan Darawsheh