diff mbox series

[5/6] common/mlx5: fix device list operation concurrency

Message ID 20210831203732.3411134-6-michaelba@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: Raslan Darawsheh
Headers show
Series mlx5: some independent fixes | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Michael Baum Aug. 31, 2021, 8:37 p.m. UTC
The MLX5 common driver has a global list of mlx5 devices which are
probed.

In probe function it create one and insert it to the list. Similarly it
removes the device in remove function.
These operations are not safe as there can be such operations in
parallel, by different threads.

Add global lock for the list and use it to insert or remove.

Fixes: 8a41f4deccc3 ("common/mlx5: introduce layer for multiple class drivers")
Cc: stable@dpdk.org

Signed-off-by: Michael Baum <michaelba@nvidia.com>
---
 drivers/common/mlx5/mlx5_common.c | 6 ++++++
 1 file changed, 6 insertions(+)
diff mbox series

Patch

diff --git a/drivers/common/mlx5/mlx5_common.c b/drivers/common/mlx5/mlx5_common.c
index f6e440dca1..4321cb3a9c 100644
--- a/drivers/common/mlx5/mlx5_common.c
+++ b/drivers/common/mlx5/mlx5_common.c
@@ -50,6 +50,7 @@  static TAILQ_HEAD(mlx5_drivers, mlx5_class_driver) drivers_list =
 /* Head of devices. */
 static TAILQ_HEAD(mlx5_devices, mlx5_common_device) devices_list =
 				TAILQ_HEAD_INITIALIZER(devices_list);
+static pthread_mutex_t devices_list_lock;
 
 static const struct {
 	const char *name;
@@ -222,7 +223,9 @@  mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size)
 static void
 dev_release(struct mlx5_common_device *dev)
 {
+	pthread_mutex_lock(&devices_list_lock);
 	TAILQ_REMOVE(&devices_list, dev, next);
+	pthread_mutex_unlock(&devices_list_lock);
 	rte_free(dev);
 }
 
@@ -315,7 +318,9 @@  mlx5_common_dev_probe(struct rte_device *eal_dev)
 		if (!dev)
 			return -ENOMEM;
 		dev->dev = eal_dev;
+		pthread_mutex_lock(&devices_list_lock);
 		TAILQ_INSERT_HEAD(&devices_list, dev, next);
+		pthread_mutex_unlock(&devices_list_lock);
 		new_device = true;
 	}
 	/*
@@ -440,6 +445,7 @@  mlx5_common_init(void)
 	if (mlx5_common_initialized)
 		return;
 
+	pthread_mutex_init(&devices_list_lock, NULL);
 	mlx5_glue_constructor();
 	mlx5_common_driver_init();
 	mlx5_common_initialized = true;