Skip to contentSkip to conversation

net/mlx5: fix probe optimization race condition

PATCH Superseded Rongwei Liu 4 participants 6 messages Aug 28, 2025 – Sep 2, 2025 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/mlx5: fix probe optimization race condition

With dedicated RDMA link monitor, there are two threads which can update the IB device port information.

Add a new flag to avoid the race condition. Update should go through RDMA link monitor once ready.

Fixes 51fb5c40c826 ("net/mlx5: optimize device probing")
---
 drivers/common/mlx5/linux/mlx5_nl.c     |  7 ++-
 drivers/common/mlx5/mlx5_common.h       |  1 +
 drivers/net/mlx5/linux/mlx5_ethdev_os.c | 69 ++++---------------------
 drivers/net/mlx5/linux/mlx5_os.c        |  9 +++-
 4 files changed, 25 insertions(+), 61 deletions(-)
drivers/common/mlx5/linux/mlx5_nl.c +5 -2
index dd69e229e3..84c12efdc7 100644
@@ -1171,8 +1171,12 @@ mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info
 			data.ibindex = dev_info->ibindex;
 	}
 
+	/* Update should be done via monitor thread to avoid race condition */
+	if (dev_info->async_mon_ready) {
+		rte_errno = ENODEV;
+		return 0;
+	}
 	ret = mlx5_nl_port_info(nl, pindex, &data);
-
 	if (dev_info->probe_opt && !strcmp(dev_info->ibname, name)) {
 		if ((!ret || ret == -ENODEV) && dev_info->port_info &&
 		    pindex <= dev_info->port_num) {
@@ -1182,7 +1186,6 @@ mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info
 			dev_info->port_info[pindex].valid = 1;
 		}
 	}
-
 	return ret ? 0 : data.ifindex;
 }
 
drivers/common/mlx5/mlx5_common.h +1 -0
index bea1382911..b49f0c850e 100644
@@ -185,6 +185,7 @@ struct mlx5_dev_info {
 	uint32_t ibindex;
 	char ibname[MLX5_FS_NAME_MAX];
 	uint8_t probe_opt;
+	uint8_t async_mon_ready;
 	struct mlx5_port_nl_info *port_info;
 };
 
drivers/net/mlx5/linux/mlx5_ethdev_os.c +11 -58

95 lines not on this page · raw message

drivers/net/mlx5/linux/mlx5_os.c +8 -1
index 85b3fabaf5..edfe61ea55 100644
@@ -3051,7 +3051,7 @@ mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
 		DRV_LOG(ERR, "Failed to allocate intr_handle.");
 		return;
 	}
-	if (sh->cdev->config.probe_opt &&
+	if (sh->cdev->dev_info.probe_opt &&
 	    sh->cdev->dev_info.port_num > 1 &&
 	    !sh->rdma_monitor_supp) {
 		nlsk_fd = mlx5_nl_rdma_monitor_init();
@@ -3076,8 +3076,15 @@ mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
 				close(nlsk_fd);
 				return;
 			}
+			sh->cdev->dev_info.async_mon_ready = 1;
 		} else {
 			close(nlsk_fd);
+			if (sh->cdev->dev_info.probe_opt) {
+				DRV_LOG(INFO, "Failed to create rdma link monitor, disable probe optimization");
+				sh->cdev->dev_info.probe_opt = 0;
+				mlx5_free(sh->cdev->dev_info.port_info);
+				sh->cdev->dev_info.port_info = NULL;
+			}
 		}
 	}
 	nlsk_fd = mlx5_nl_init(NETLINK_ROUTE, RTMGRP_LINK);
Signature · 1 line
2.27.0
#1 lore raw
Quoted 6 lines · On Thu, 28 Aug 2025 06:21:34 +0300 Rongwei Liu <[email protected]> wrote:

With dedicated RDMA link monitor, there are two threads which can update the IB device port information.

Add a new flag to avoid the race condition. Update should go through RDMA link monitor once ready.

Quoted 6 lines
Fixes 51fb5c40c826 ("net/mlx5: optimize device probing")

---

If variable is modified (with out locking) on two threads it needs to atomic or volatile.

#2 lore raw
Quoted 16 lines · Stephen Hemminger
Quoted 6 lines · On Thu, 28 Aug 2025 06:21:34 +0300 Rongwei Liu <[email protected]> wrote:

With dedicated RDMA link monitor, there are two threads which can update the IB device port information.

Add a new flag to avoid the race condition. Update should go through RDMA link monitor once ready.

Quoted 6 lines
Fixes 51fb5c40c826 ("net/mlx5: optimize device probing")

---

Quoted 2 lines

If variable is modified (with out locking) on two threads it needs to atomic or volatile.

Exactly. Before this patch, it' user responsebility to seperate probe probing and sf manipualtion. Obviously, customer didn't follow this very well.
Now logic change to:
1. Update all port information in probing thread.
2. Probe thread initiate the dedicated rdma monitor thread. Once ready, all port update will go to this thread.
3. Next port probing won't trigger PMD port information update.

No lock is required then.

#3 lore raw

28/08/2025 06:49, rongwei liu:

Quoted 20 lines · Stephen Hemminger
Quoted 16 lines
Quoted 6 lines · On Thu, 28 Aug 2025 06:21:34 +0300 Rongwei Liu <[email protected]> wrote:

With dedicated RDMA link monitor, there are two threads which can update the IB device port information.

Add a new flag to avoid the race condition. Update should go through RDMA link monitor once ready.

Quoted 6 lines
Fixes 51fb5c40c826 ("net/mlx5: optimize device probing")

---

Quoted 2 lines

If variable is modified (with out locking) on two threads it needs to atomic or volatile.

Quoted 8 lines

Exactly. Before this patch, it' user responsebility to seperate probe probing and sf manipualtion. Obviously, customer didn't follow this very well.
Now logic change to:
1. Update all port information in probing thread.
2. Probe thread initiate the dedicated rdma monitor thread. Once ready, all port update will go to this thread.
3. Next port probing won't trigger PMD port information update.

No lock is required then.

This is the kind of info we need in a commit log.
Please make a v2, thanks.

#4 lore raw

PATCH v2 1/1 net/mlx5: fix probe optimization race condition

With dedicated RDMA link monitor, there are two threads which can update the IB device port information.

Add a new flag to avoid the race condition. Update should go through RDMA link monitor once ready.

Current logic is:
1. Update all port information in probing thread.
2. Probe thread initiates the dedicated rdma monitor thread. Once ready, port information update will be handled by this thread.
3. Next probing won't trigger PMD port information update.

No lock is required.

Fixes 51fb5c40c826 ("common/mlx5: optimize device probing")
---
 drivers/common/mlx5/linux/mlx5_nl.c     |  7 ++-
 drivers/common/mlx5/mlx5_common.h       |  1 +
 drivers/net/mlx5/linux/mlx5_ethdev_os.c | 69 ++++---------------------
 drivers/net/mlx5/linux/mlx5_os.c        |  9 +++-
 4 files changed, 25 insertions(+), 61 deletions(-)
drivers/common/mlx5/linux/mlx5_nl.c +5 -2
index dd69e229e3..84c12efdc7 100644
@@ -1171,8 +1171,12 @@ mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info
 			data.ibindex = dev_info->ibindex;
 	}
 
+	/* Update should be done via monitor thread to avoid race condition */
+	if (dev_info->async_mon_ready) {
+		rte_errno = ENODEV;
+		return 0;
+	}
 	ret = mlx5_nl_port_info(nl, pindex, &data);
-
 	if (dev_info->probe_opt && !strcmp(dev_info->ibname, name)) {
 		if ((!ret || ret == -ENODEV) && dev_info->port_info &&
 		    pindex <= dev_info->port_num) {
@@ -1182,7 +1186,6 @@ mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info
 			dev_info->port_info[pindex].valid = 1;
 		}
 	}
-
 	return ret ? 0 : data.ifindex;
 }
 
drivers/common/mlx5/mlx5_common.h +1 -0
index bea1382911..b49f0c850e 100644
@@ -185,6 +185,7 @@ struct mlx5_dev_info {
 	uint32_t ibindex;
 	char ibname[MLX5_FS_NAME_MAX];
 	uint8_t probe_opt;
+	uint8_t async_mon_ready;
 	struct mlx5_port_nl_info *port_info;
 };
 
drivers/net/mlx5/linux/mlx5_ethdev_os.c +11 -58

95 lines not on this page · raw message

drivers/net/mlx5/linux/mlx5_os.c +8 -1
index 85b3fabaf5..edfe61ea55 100644
@@ -3051,7 +3051,7 @@ mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
 		DRV_LOG(ERR, "Failed to allocate intr_handle.");
 		return;
 	}
-	if (sh->cdev->config.probe_opt &&
+	if (sh->cdev->dev_info.probe_opt &&
 	    sh->cdev->dev_info.port_num > 1 &&
 	    !sh->rdma_monitor_supp) {
 		nlsk_fd = mlx5_nl_rdma_monitor_init();
@@ -3076,8 +3076,15 @@ mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
 				close(nlsk_fd);
 				return;
 			}
+			sh->cdev->dev_info.async_mon_ready = 1;
 		} else {
 			close(nlsk_fd);
+			if (sh->cdev->dev_info.probe_opt) {
+				DRV_LOG(INFO, "Failed to create rdma link monitor, disable probe optimization");
+				sh->cdev->dev_info.probe_opt = 0;
+				mlx5_free(sh->cdev->dev_info.port_info);
+				sh->cdev->dev_info.port_info = NULL;
+			}
 		}
 	}
 	nlsk_fd = mlx5_nl_init(NETLINK_ROUTE, RTMGRP_LINK);
Signature · 1 line
2.27.0
#5 lore raw

Hi,

Quoted 12 lines · Rongwei Liu

With dedicated RDMA link monitor, there are two threads which can update the IB device port information.

Add a new flag to avoid the race condition. Update should go through RDMA link monitor once ready.

Current logic is:
1. Update all port information in probing thread.
2. Probe thread initiates the dedicated rdma monitor thread. Once ready, port information update will be handled by this thread.
3. Next probing won't trigger PMD port information update.

Quoted 7 lines

No lock is required.

Fixes 51fb5c40c826 ("common/mlx5: optimize device probing")

Patch applied to next-net-mlx,

Kindest regards
Raslan Darawsheh