[dpdk-dev] bus/vdev: reduce scope of device list lock

Message ID 20180521161156.25724-1-thomas@monjalon.net (mailing list archive)
State Superseded, archived
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Thomas Monjalon May 21, 2018, 4:11 p.m. UTC
  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")

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
---
 drivers/bus/vdev/vdev.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
  

Comments

Burakov, Anatoly May 22, 2018, 9:05 a.m. UTC | #1
On 21-May-18 5:11 PM, Thomas Monjalon wrote:
> 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")
> 
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> ---
>   drivers/bus/vdev/vdev.c | 10 ++++------
>   1 file changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/bus/vdev/vdev.c b/drivers/bus/vdev/vdev.c
> index 099b9ff85..2fbc86806 100644
> --- a/drivers/bus/vdev/vdev.c
> +++ b/drivers/bus/vdev/vdev.c
> @@ -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;
>   	}

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?

>   
>   	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;
>   }
>   
>
  
Thomas Monjalon May 22, 2018, 9:20 a.m. UTC | #2
22/05/2018 11:05, Burakov, Anatoly:
> On 21-May-18 5:11 PM, Thomas Monjalon wrote:
> > 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")
> > 
> > Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> 
> 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.
  

Patch

diff --git a/drivers/bus/vdev/vdev.c b/drivers/bus/vdev/vdev.c
index 099b9ff85..2fbc86806 100644
--- a/drivers/bus/vdev/vdev.c
+++ b/drivers/bus/vdev/vdev.c
@@ -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;
 }