Skip to contentSkip to conversation

bus/vdev: reduce scope of device list lock

PATCH Superseded Thomas Monjalon 4 participants 10 messages May 21, 2018 – May 22, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bus/vdev: reduce scope of device list lock

The lock vdev_device_list_lock was taken before calling "remove" function for the device.
So it prevents to remove sub-devices (as in failsafe) inside its own "remove" function, because of a deadlock.

The lock is now only protecting the device list inside the bus driver.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")
---
 drivers/bus/vdev/vdev.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/bus/vdev/vdev.c +4 -6
index 099b9ff85..2fbc86806 100644
@@ -293,25 +293,23 @@ rte_vdev_uninit(const char *name)
 	if (name == NULL)
 		return -EINVAL;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
-
 	dev = find_vdev(name);
 	if (!dev) {
 		ret = -ENOENT;
-		goto unlock;
+		return ret;
 	}
 
 	ret = vdev_remove_driver(dev);
 	if (ret)
-		goto unlock;
+		return ret;
 
+	rte_spinlock_lock(&vdev_device_list_lock);
 	TAILQ_REMOVE(&vdev_device_list, dev, next);
 	devargs = dev->device.devargs;
 	rte_devargs_remove(devargs->bus->name, devargs->name);
 	free(dev);
-
-unlock:
 	rte_spinlock_unlock(&vdev_device_list_lock);
+
 	return ret;
 }
 
Signature · 1 line
2.16.2
#1 lore raw

PATCH v2 1/1 bus/vdev: fix scope of device list lock

The lock vdev_device_list_lock was taken before calling "remove" function for the device.
So it prevents to remove sub-devices (as in failsafe) inside its own "remove" function, because of a deadlock.

The lock is now only protecting the device list inside the bus driver.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")

---
v2: reduce scope more by moving unlock

---
 drivers/bus/vdev/vdev.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/bus/vdev/vdev.c +5 -6
index 099b9ff85..470cff46c 100644
@@ -293,25 +293,24 @@ rte_vdev_uninit(const char *name)
 	if (name == NULL)
 		return -EINVAL;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
-
 	dev = find_vdev(name);
 	if (!dev) {
 		ret = -ENOENT;
-		goto unlock;
+		return ret;
 	}
 
 	ret = vdev_remove_driver(dev);
 	if (ret)
-		goto unlock;
+		return ret;
 
+	rte_spinlock_lock(&vdev_device_list_lock);
 	TAILQ_REMOVE(&vdev_device_list, dev, next);
+	rte_spinlock_unlock(&vdev_device_list_lock);
+
 	devargs = dev->device.devargs;
 	rte_devargs_remove(devargs->bus->name, devargs->name);
 	free(dev);
 
-unlock:
-	rte_spinlock_unlock(&vdev_device_list_lock);
 	return ret;
 }
 
Signature · 1 line
2.16.2
Tested-by on v2 1/1
#2 lore raw

Re: [PATCH v2] bus/vdev: fix scope of device list lock

From: Thomas Monjalon

Quoted 10 lines

The lock vdev_device_list_lock was taken before calling "remove" function for the device.
So it prevents to remove sub-devices (as in failsafe) inside its own "remove" function, because of a deadlock.

The lock is now only protecting the device list inside the bus driver.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")
#4 lore raw
In reply to Thomas Monjalon
Quoted 21 lines · Thomas Monjalon

The lock vdev_device_list_lock was taken before calling "remove" function for the device.
So it prevents to remove sub-devices (as in failsafe) inside its own "remove" function, because of a deadlock.

The lock is now only protecting the device list inside the bus driver.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")
---
  drivers/bus/vdev/vdev.c | 10 ++++------
  1 file changed, 4 insertions(+), 6 deletions(-)
drivers/bus/vdev/vdev.c +0 -0
index 099b9ff85..2fbc86806 100644
@@ -293,25 +293,23 @@ rte_vdev_uninit(const char *name)
  	if (name == NULL)

drivers/bus/vdev/vdev.c rte_vdev_uninit(const char *name) · line 300

Quoted 10 lines
drivers/bus/vdev/vdev.c +1 -3
  		return -EINVAL;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
-
  	dev = find_vdev(name);
  	if (!dev) {
  		ret = -ENOENT;
-		goto unlock;
+		return ret;
  	}

Without that lock, all of this would be racy - find_dev would iterate a tailq that might change under its feet, and tailq_remove may be called with a pointer that has already been removed.

How about changing the lock to a recursive lock? Failsafe would be removing devices from within the same thread, correct?

Quoted 19 lines
drivers/bus/vdev/vdev.c +3 -3
  	ret = vdev_remove_driver(dev);
  	if (ret)
-		goto unlock;
+		return ret;
 
+	rte_spinlock_lock(&vdev_device_list_lock);
  	TAILQ_REMOVE(&vdev_device_list, dev, next);
  	devargs = dev->device.devargs;
  	rte_devargs_remove(devargs->bus->name, devargs->name);
  	free(dev);
-
-unlock:
  	rte_spinlock_unlock(&vdev_device_list_lock);
+
  	return ret;
  }
 
 
Signature · 2 lines
Thanks,
Anatoly
Acked-by
#3 lore raw

Re: [PATCH v2] bus/vdev: fix scope of device list lock

In reply to Matan Azrad
Quoted 8 lines · Matan Azrad

From: Thomas Monjalon

Quoted 5 lines

The lock vdev_device_list_lock was taken before calling "remove" function for the device.
So it prevents to remove sub-devices (as in failsafe) inside its own "remove" function, because of a deadlock.

Quoted 6 lines
Quoted 5 lines

The lock is now only protecting the device list inside the bus driver.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")

If these locks were necessary, they would be missing as well for rte_devargs. Jianfeng inquired about it, I think it should be followed upon.

Restricting the scope of the lock here could maybe re-introduce the bug that motivated their introduction in the first place, as the devargs_remove() is not in the critical section anymore.

However, this is an rte_devargs issue, not a vdev bus one, so the fix makes sense and I'd like to have it ASAP for failsafe. Without a vdev bus maintainer left:

Signature · 2 lines
Gaëtan Rivet
6WIND
#5 lore raw
In reply to Burakov, Anatoly

22/05/2018 11:05, Burakov, Anatoly:

Quoted 11 lines · Thomas Monjalon
Quoted 10 lines

The lock vdev_device_list_lock was taken before calling "remove" function for the device.
So it prevents to remove sub-devices (as in failsafe) inside its own "remove" function, because of a deadlock.

The lock is now only protecting the device list inside the bus driver.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")
Quoted 8 lines
Quoted 1 line

Without that lock, all of this would be racy - find_dev would iterate a tailq that might change under its feet, and tailq_remove may be called with a pointer that has already been removed.

How about changing the lock to a recursive lock? Failsafe would be removing devices from within the same thread, correct?

Yes it could work.
I will give it a try.

#6 lore raw

PATCH v3 1/1 bus/vdev: replace device list lock by a recursive one

A device like failsafe can manage sub-devices.
When removing such device, it removes its sub-devices and try to take the same vdev_device_list_lock.
It was causing a deadlock because the lock was not recursive.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")

---
v3: try recursive lock
WARNING: not yet tested!

---
 drivers/bus/vdev/vdev.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/bus/vdev/vdev.c +14 -12
index 099b9ff85..6139dd551 100644
@@ -36,7 +36,9 @@ TAILQ_HEAD(vdev_device_list, rte_vdev_device);
 
 static struct vdev_device_list vdev_device_list =
 	TAILQ_HEAD_INITIALIZER(vdev_device_list);
-static rte_spinlock_t vdev_device_list_lock = RTE_SPINLOCK_INITIALIZER;
+/* The lock needs to be recursive because a vdev can manage another vdev. */
+static rte_spinlock_recursive_t vdev_device_list_lock =
+	RTE_SPINLOCK_RECURSIVE_INITIALIZER;
 
 struct vdev_driver_list vdev_driver_list =
 	TAILQ_HEAD_INITIALIZER(vdev_driver_list);
@@ -249,7 +251,7 @@ rte_vdev_init(const char *name, const char *args)
 	struct rte_devargs *devargs;
 	int ret;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
+	rte_spinlock_recursive_lock(&vdev_device_list_lock);
 	ret = insert_vdev(name, args, &dev);
 	if (ret == 0) {
 		ret = vdev_probe_all_drivers(dev);
@@ -263,7 +265,7 @@ rte_vdev_init(const char *name, const char *args)
 			free(dev);
 		}
 	}
-	rte_spinlock_unlock(&vdev_device_list_lock);
+	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 	return ret;
 }
 
@@ -293,7 +295,7 @@ rte_vdev_uninit(const char *name)
 	if (name == NULL)
 		return -EINVAL;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
+	rte_spinlock_recursive_lock(&vdev_device_list_lock);
 
 	dev = find_vdev(name);
 	if (!dev) {
@@ -311,7 +313,7 @@ rte_vdev_uninit(const char *name)
 	free(dev);
 
 unlock:
-	rte_spinlock_unlock(&vdev_device_list_lock);
+	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 	return ret;
 }
 
@@ -355,7 +357,7 @@ vdev_action(const struct rte_mp_msg *mp_msg, const void *peer)
 		ou->num = 1;
 		num = 0;
 
-		rte_spinlock_lock(&vdev_device_list_lock);
+		rte_spinlock_recursive_lock(&vdev_device_list_lock);
 		TAILQ_FOREACH(dev, &vdev_device_list, next) {
 			devname = rte_vdev_device_name(dev);
 			if (strlen(devname) == 0) {
@@ -369,7 +371,7 @@ vdev_action(const struct rte_mp_msg *mp_msg, const void *peer)
 					 devname, strerror(rte_errno));
 			num++;
 		}
-		rte_spinlock_unlock(&vdev_device_list_lock);
+		rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 
 		ou->type = VDEV_SCAN_REP;
 		ou->num = num;
@@ -445,10 +447,10 @@ vdev_scan(void)
 		if (!dev)
 			return -1;
 
-		rte_spinlock_lock(&vdev_device_list_lock);
+		rte_spinlock_recursive_lock(&vdev_device_list_lock);
 
 		if (find_vdev(devargs->name)) {
-			rte_spinlock_unlock(&vdev_device_list_lock);
+			rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 			free(dev);
 			continue;
 		}
@@ -459,7 +461,7 @@ vdev_scan(void)
 
 		TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
 
-		rte_spinlock_unlock(&vdev_device_list_lock);
+		rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 	}
 
 	return 0;
@@ -498,7 +500,7 @@ vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 	const struct rte_vdev_device *vstart;
 	struct rte_vdev_device *dev;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
+	rte_spinlock_recursive_lock(&vdev_device_list_lock);
 	if (start != NULL) {
 		vstart = RTE_DEV_TO_VDEV_CONST(start);
 		dev = TAILQ_NEXT(vstart, next);
@@ -510,7 +512,7 @@ vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
 			break;
 		dev = TAILQ_NEXT(dev, next);
 	}
-	rte_spinlock_unlock(&vdev_device_list_lock);
+	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 
 	return dev ? &dev->device : NULL;
 }
Signature · 1 line
2.16.2
Tested-by on v3 1/1
#7 lore raw

Re: [PATCH v3] bus/vdev: replace device list lock by a recursive one

From: Thomas Monjalon

Quoted 9 lines

A device like failsafe can manage sub-devices.
When removing such device, it removes its sub-devices and try to take the same vdev_device_list_lock.
It was causing a deadlock because the lock was not recursive.

Fixes 35f462839b69 ("bus/vdev: add lock on device list")
Quoted 123 lines

---
v3: try recursive lock
WARNING: not yet tested!

---
 drivers/bus/vdev/vdev.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/bus/vdev/vdev.c index renamed from drivers/bus/vdev/vdev.c +0 -0

099b9ff85..6139dd551 100644

drivers/bus/vdev/vdev.c +8 -6
@@ -36,7 +36,9 @@ TAILQ_HEAD(vdev_device_list, rte_vdev_device);
 
 static struct vdev_device_list vdev_device_list =
 	TAILQ_HEAD_INITIALIZER(vdev_device_list);
-static rte_spinlock_t vdev_device_list_lock = RTE_SPINLOCK_INITIALIZER;
+/* The lock needs to be recursive because a vdev can manage another
+vdev. */ static rte_spinlock_recursive_t vdev_device_list_lock =
+	RTE_SPINLOCK_RECURSIVE_INITIALIZER;
 
 struct vdev_driver_list vdev_driver_list =
 	TAILQ_HEAD_INITIALIZER(vdev_driver_list);
@@ -249,7 +251,7 @@ rte_vdev_init(const char *name, const char *args)
 	struct rte_devargs *devargs;
 	int ret;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
+	rte_spinlock_recursive_lock(&vdev_device_list_lock);
 	ret = insert_vdev(name, args, &dev);
 	if (ret == 0) {
 		ret = vdev_probe_all_drivers(dev);
@@ -263,7 +265,7 @@ rte_vdev_init(const char *name, const char *args)
 			free(dev);
 		}
 	}
-	rte_spinlock_unlock(&vdev_device_list_lock);
+	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 	return ret;
 }
 
@@ -293,7 +295,7 @@ rte_vdev_uninit(const char *name)
 	if (name == NULL)
 		return -EINVAL;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
+	rte_spinlock_recursive_lock(&vdev_device_list_lock);
 
 	dev = find_vdev(name);
 	if (!dev) {
@@ -311,7 +313,7 @@ rte_vdev_uninit(const char *name)
 	free(dev);
 
 unlock:
-	rte_spinlock_unlock(&vdev_device_list_lock);
+	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 	return ret;
 }
 
@@ -355,7 +357,7 @@ vdev_action(const struct rte_mp_msg *mp_msg, const
 void *peer)
 		ou->num = 1;
 		num = 0;
 
-		rte_spinlock_lock(&vdev_device_list_lock);
+		rte_spinlock_recursive_lock(&vdev_device_list_lock);
 		TAILQ_FOREACH(dev, &vdev_device_list, next) {
 			devname = rte_vdev_device_name(dev);
drivers/bus/vdev/vdev.c +1 -1
 			if (strlen(devname) == 0) {
@@ -369,7 +371,7 @@ vdev_action(const struct rte_mp_msg *mp_msg, const
 void *peer)
 					 devname, strerror(rte_errno));
 			num++;
 		}
-		rte_spinlock_unlock(&vdev_device_list_lock);
+		rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 
 		ou->type = VDEV_SCAN_REP;
drivers/bus/vdev/vdev.c +2 -2
 		ou->num = num;
@@ -445,10 +447,10 @@ vdev_scan(void)
 		if (!dev)
 			return -1;
 
-		rte_spinlock_lock(&vdev_device_list_lock);
+		rte_spinlock_recursive_lock(&vdev_device_list_lock);
 
 		if (find_vdev(devargs->name)) {
-			rte_spinlock_unlock(&vdev_device_list_lock);
+
 	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 			free(dev);
 			continue;
drivers/bus/vdev/vdev.c +2 -2
 		}
@@ -459,7 +461,7 @@ vdev_scan(void)
 
 		TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
 
-		rte_spinlock_unlock(&vdev_device_list_lock);
+		rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 	}
 
 	return 0;
@@ -498,7 +500,7 @@ vdev_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
 	const struct rte_vdev_device *vstart;
 	struct rte_vdev_device *dev;
 
-	rte_spinlock_lock(&vdev_device_list_lock);
+	rte_spinlock_recursive_lock(&vdev_device_list_lock);
 	if (start != NULL) {
 		vstart = RTE_DEV_TO_VDEV_CONST(start);
drivers/bus/vdev/vdev.c +1 -1
 		dev = TAILQ_NEXT(vstart, next);
@@ -510,7 +512,7 @@ vdev_find_device(const struct rte_device *start,
 rte_dev_cmp_t cmp,
 			break;
 		dev = TAILQ_NEXT(dev, next);
 	}
-	rte_spinlock_unlock(&vdev_device_list_lock);
+	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
 
 	return dev ? &dev->device : NULL;

}
--
2.16.2

Acked-by on v3 1/1
#8 lore raw

Re: [PATCH v3] bus/vdev: replace device list lock by a recursive one

In reply to Thomas Monjalon
Quoted 5 lines · Thomas Monjalon

A device like failsafe can manage sub-devices.
When removing such device, it removes its sub-devices and try to take the same vdev_device_list_lock.
It was causing a deadlock because the lock was not recursive.

Quoted 8 lines
Fixes 35f462839b69 ("bus/vdev: add lock on device list")

---
v3: try recursive lock
WARNING: not yet tested!
---

LGTM

Signature · 2 lines
Thanks,
Anatoly
author Tested-by Matan Azrad Applied
#9 lore raw

Re: [PATCH v3] bus/vdev: replace device list lock by a recursive one

22/05/2018 15:34, Burakov, Anatoly:

Quoted 15 lines · Thomas Monjalon
Quoted 5 lines

A device like failsafe can manage sub-devices.
When removing such device, it removes its sub-devices and try to take the same vdev_device_list_lock.
It was causing a deadlock because the lock was not recursive.

Quoted 8 lines
Fixes 35f462839b69 ("bus/vdev: add lock on device list")

---
v3: try recursive lock
WARNING: not yet tested!
---

Quoted 3 lines

LGTM

Applied