Skip to contentSkip to conversation

net/mlx5: Check device memory pointer before usage

PATCH Applied Leon Romanovsky 2 participants 7 messages Jul 1, 2025 – Jul 4, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/mlx5: Check device memory pointer before usage

From: Stav Aviram <[email protected]>

Add a NULL check before accessing device memory to prevent a crash if dev->dm allocation in mlx5_init_once() fails.

Fixes c9b9dcb430b3 ("net/mlx5: Move device memory management to mlx5_core")

---
Changelog:
v1:
* Removed extra IS_ERR(dm) check.
v0:
https://lore.kernel.org/all/e389fa6ef075af1049cd7026b912d736ebe3ad23.1751279408.git.leonro@nvidia.com

---
 drivers/infiniband/hw/mlx5/dm.c                  | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/lib/dm.c | 4 ++--
 drivers/net/ethernet/mellanox/mlx5/core/main.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/hw/mlx5/dm.c +1 -1
index b4c97fb62abf..9ded2b7c1e31 100644
@@ -282,7 +282,7 @@ static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx,
 	int err;
 	u64 address;
 
-	if (!MLX5_CAP_DEV_MEM(dm_db->dev, memic))
+	if (!dm_db || !MLX5_CAP_DEV_MEM(dm_db->dev, memic))
 		return ERR_PTR(-EOPNOTSUPP);
 
 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
drivers/net/ethernet/mellanox/mlx5/core/lib/dm.c +2 -2
index 7c5516b0a844..8115071c34a4 100644
@@ -30,7 +30,7 @@ struct mlx5_dm *mlx5_dm_create(struct mlx5_core_dev *dev)
 
 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
 	if (!dm)
-		return ERR_PTR(-ENOMEM);
+		return NULL;
 
 	spin_lock_init(&dm->lock);
 
@@ -96,7 +96,7 @@ struct mlx5_dm *mlx5_dm_create(struct mlx5_core_dev *dev)
 err_steering:
 	kfree(dm);
 
-	return ERR_PTR(-ENOMEM);
+	return NULL;
 }
 
 void mlx5_dm_cleanup(struct mlx5_core_dev *dev)
drivers/net/ethernet/mellanox/mlx5/core/main.c +1 -1
index 41e8660c819c..e9baf4221cde 100644
@@ -1102,7 +1102,7 @@ static int mlx5_init_once(struct mlx5_core_dev *dev)
 	}
 
 	dev->dm = mlx5_dm_create(dev);
-	if (IS_ERR(dev->dm))
+	if (!dev->dm)
 		mlx5_core_warn(dev, "Failed to init device memory %ld\n", PTR_ERR(dev->dm));
 
 	dev->tracer = mlx5_fw_tracer_create(dev);
Signature · 1 line
2.50.0
reviewer
#1 lore raw
Quoted 25 lines · Leon Romanovsky

From: Stav Aviram <[email protected]>

Add a NULL check before accessing device memory to prevent a crash if dev->dm allocation in mlx5_init_once() fails.

Fixes c9b9dcb430b3 ("net/mlx5: Move device memory management to mlx5_core")

---
Changelog:
v1:
* Removed extra IS_ERR(dm) check.
v0:
https://lore.kernel.org/all/e389fa6ef075af1049cd7026b912d736ebe3ad23.1751279408.git.leonro@nvidia.com

---
 drivers/infiniband/hw/mlx5/dm.c                  | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/lib/dm.c | 4 ++--
 drivers/net/ethernet/mellanox/mlx5/core/main.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/hw/mlx5/dm.c +0 -0
index b4c97fb62abf..9ded2b7c1e31 100644

drivers/infiniband/hw/mlx5/dm.c static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx, · line 286

Quoted 6 lines
drivers/infiniband/hw/mlx5/dm.c +1 -1
@@ -282,7 +282,7 @@ static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx,
 	int err;
 	u64 address;
 
-	if (!MLX5_CAP_DEV_MEM(dm_db->dev, memic))
+	if (!dm_db || !MLX5_CAP_DEV_MEM(dm_db->dev, memic))
 		return ERR_PTR(-EOPNOTSUPP);

nit: -EOPNOTSUPP doesn't feel like the right error code
in the !dm_db case.

Quoted 2 lines
drivers/infiniband/hw/mlx5/dm.c +0 -0
 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);

...

#2 lore raw
Quoted 29 lines · Simon Horman
Quoted 28 lines · Leon Romanovsky

From: Stav Aviram <[email protected]>

Add a NULL check before accessing device memory to prevent a crash if dev->dm allocation in mlx5_init_once() fails.

Fixes c9b9dcb430b3 ("net/mlx5: Move device memory management to mlx5_core")

---
Changelog:
v1:
* Removed extra IS_ERR(dm) check.
v0:
https://lore.kernel.org/all/e389fa6ef075af1049cd7026b912d736ebe3ad23.1751279408.git.leonro@nvidia.com

---
 drivers/infiniband/hw/mlx5/dm.c                  | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/lib/dm.c | 4 ++--
 drivers/net/ethernet/mellanox/mlx5/core/main.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/hw/mlx5/dm.c +0 -0
index b4c97fb62abf..9ded2b7c1e31 100644
@@ -282,7 +282,7 @@ static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx,
 	int err;
 	u64 address;
 
Quoted 6 lines

drivers/infiniband/hw/mlx5/dm.c static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx, · line 286

Quoted 3 lines
drivers/infiniband/hw/mlx5/dm.c +1 -1
-	if (!MLX5_CAP_DEV_MEM(dm_db->dev, memic))
+	if (!dm_db || !MLX5_CAP_DEV_MEM(dm_db->dev, memic))
 		return ERR_PTR(-EOPNOTSUPP);

nit: -EOPNOTSUPP doesn't feel like the right error code
in the !dm_db case.

Why? This error is returned to the user through mlx5_ib_alloc_dm().

Thanks

Quoted 5 lines
Quoted 3 lines
drivers/infiniband/hw/mlx5/dm.c +0 -0
 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);

...

reviewer
#3 lore raw
Quoted 30 lines · Leon Romanovsky
Quoted 29 lines · Simon Horman
Quoted 28 lines · Leon Romanovsky

From: Stav Aviram <[email protected]>

Add a NULL check before accessing device memory to prevent a crash if dev->dm allocation in mlx5_init_once() fails.

Fixes c9b9dcb430b3 ("net/mlx5: Move device memory management to mlx5_core")

---
Changelog:
v1:
* Removed extra IS_ERR(dm) check.
v0:
https://lore.kernel.org/all/e389fa6ef075af1049cd7026b912d736ebe3ad23.1751279408.git.leonro@nvidia.com

---
 drivers/infiniband/hw/mlx5/dm.c                  | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/lib/dm.c | 4 ++--
 drivers/net/ethernet/mellanox/mlx5/core/main.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/hw/mlx5/dm.c +0 -0
index b4c97fb62abf..9ded2b7c1e31 100644
@@ -282,7 +282,7 @@ static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx,
 	int err;
 	u64 address;
 
Quoted 8 lines
Quoted 6 lines

drivers/infiniband/hw/mlx5/dm.c static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx, · line 286

Quoted 3 lines
drivers/infiniband/hw/mlx5/dm.c +1 -1
-	if (!MLX5_CAP_DEV_MEM(dm_db->dev, memic))
+	if (!dm_db || !MLX5_CAP_DEV_MEM(dm_db->dev, memic))
 		return ERR_PTR(-EOPNOTSUPP);

nit: -EOPNOTSUPP doesn't feel like the right error code
in the !dm_db case.

Why? This error is returned to the user through mlx5_ib_alloc_dm().

Because, as I understand things, such a case would be due to a memory allocation failure, not by the device not supporting a feature.

handle_alloc_dm_memic() already returns ERR_PTR(-ENOMEM) if kzalloc() fails. I'd suggest doing so for the !dm_db case too.

But I don't feel particularly strongly about this.

#4 lore raw
Quoted 38 lines · Simon Horman
Quoted 37 lines · Leon Romanovsky
Quoted 35 lines · Simon Horman
Quoted 25 lines · Leon Romanovsky

From: Stav Aviram <[email protected]>

Add a NULL check before accessing device memory to prevent a crash if dev->dm allocation in mlx5_init_once() fails.

Fixes c9b9dcb430b3 ("net/mlx5: Move device memory management to mlx5_core")

---
Changelog:
v1:
* Removed extra IS_ERR(dm) check.
v0:
https://lore.kernel.org/all/e389fa6ef075af1049cd7026b912d736ebe3ad23.1751279408.git.leonro@nvidia.com

---
 drivers/infiniband/hw/mlx5/dm.c                  | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/lib/dm.c | 4 ++--
 drivers/net/ethernet/mellanox/mlx5/core/main.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/hw/mlx5/dm.c +0 -0
index b4c97fb62abf..9ded2b7c1e31 100644

drivers/infiniband/hw/mlx5/dm.c static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx, · line 286

Quoted 6 lines
drivers/infiniband/hw/mlx5/dm.c +1 -1
@@ -282,7 +282,7 @@ static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx,
 	int err;
 	u64 address;
 
-	if (!MLX5_CAP_DEV_MEM(dm_db->dev, memic))
+	if (!dm_db || !MLX5_CAP_DEV_MEM(dm_db->dev, memic))
 		return ERR_PTR(-EOPNOTSUPP);

nit: -EOPNOTSUPP doesn't feel like the right error code
in the !dm_db case.

Quoted 7 lines
Quoted 1 line

Why? This error is returned to the user through mlx5_ib_alloc_dm().

Because, as I understand things, such a case would be due to a memory allocation failure, not by the device not supporting a feature.

handle_alloc_dm_memic() already returns ERR_PTR(-ENOMEM) if kzalloc() fails. I'd suggest doing so for the !dm_db case too.

!dm_db case can be because of missing capabilities and EOPNOTSUPP is a way to inform users about it.

Quoted 2 lines

But I don't feel particularly strongly about this.

reviewer
#5 lore raw
Quoted 41 lines · Leon Romanovsky
Quoted 40 lines · Simon Horman
Quoted 38 lines · Leon Romanovsky
Quoted 29 lines · Simon Horman
Quoted 28 lines · Leon Romanovsky

From: Stav Aviram <[email protected]>

Add a NULL check before accessing device memory to prevent a crash if dev->dm allocation in mlx5_init_once() fails.

Fixes c9b9dcb430b3 ("net/mlx5: Move device memory management to mlx5_core")

---
Changelog:
v1:
* Removed extra IS_ERR(dm) check.
v0:
https://lore.kernel.org/all/e389fa6ef075af1049cd7026b912d736ebe3ad23.1751279408.git.leonro@nvidia.com

---
 drivers/infiniband/hw/mlx5/dm.c                  | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/lib/dm.c | 4 ++--
 drivers/net/ethernet/mellanox/mlx5/core/main.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/hw/mlx5/dm.c +0 -0
index b4c97fb62abf..9ded2b7c1e31 100644
@@ -282,7 +282,7 @@ static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx,
 	int err;
 	u64 address;
 
Quoted 6 lines

drivers/infiniband/hw/mlx5/dm.c static struct ib_dm *handle_alloc_dm_memic(struct ib_ucontext *ctx, · line 286

Quoted 3 lines
drivers/infiniband/hw/mlx5/dm.c +1 -1
-	if (!MLX5_CAP_DEV_MEM(dm_db->dev, memic))
+	if (!dm_db || !MLX5_CAP_DEV_MEM(dm_db->dev, memic))
 		return ERR_PTR(-EOPNOTSUPP);

nit: -EOPNOTSUPP doesn't feel like the right error code
in the !dm_db case.

Why? This error is returned to the user through mlx5_ib_alloc_dm().

Quoted 8 lines
Quoted 5 lines

Because, as I understand things, such a case would be due to a memory allocation failure, not by the device not supporting a feature.

handle_alloc_dm_memic() already returns ERR_PTR(-ENOMEM) if kzalloc() fails. I'd suggest doing so for the !dm_db case too.

!dm_db case can be because of missing capabilities and EOPNOTSUPP is a way to inform users about it.

Understood. No further objections from my side.

...