From: Qi Zhang Date: Thu, 12 Jul 2018 14:01:41 +0000 (+0800) Subject: eal: fix hotplug add and remove X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=196e9a486cb1a0fd929a15244e7d9169ef5ede00;p=dpdk.git eal: fix hotplug add and remove If hotplug add an already plugged PCI device, it will cause rte_pci_device->device.name be corrupted due to unexpected rte_devargs_remove. Also if try to hotplug remove an already unplugged device, it will cause segment fault due to unexpected bus->unplug on a rte_device whose driver is NULL. The patch fix these issues. Fixes: 7e8b26650146 ("eal: fix hotplug add / remove") Cc: stable@dpdk.org Signed-off-by: Qi Zhang Acked-by: Gaetan Rivet --- diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c index b78845f023..678dbcac7c 100644 --- a/lib/librte_eal/common/eal_common_dev.c +++ b/lib/librte_eal/common/eal_common_dev.c @@ -67,18 +67,6 @@ struct dev_next_ctx { #define CLSCTX(ptr) \ (((struct dev_next_ctx *)(intptr_t)ptr)->cls_str) -static int cmp_detached_dev_name(const struct rte_device *dev, - const void *_name) -{ - const char *name = _name; - - /* skip attached devices */ - if (dev->driver != NULL) - return 1; - - return strcmp(dev->name, name); -} - static int cmp_dev_name(const struct rte_device *dev, const void *_name) { const char *name = _name; @@ -176,14 +164,19 @@ int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devn if (ret) goto err_devarg; - dev = bus->find_device(NULL, cmp_detached_dev_name, devname); + dev = bus->find_device(NULL, cmp_dev_name, devname); if (dev == NULL) { - RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n", + RTE_LOG(ERR, EAL, "Cannot find device (%s)\n", devname); ret = -ENODEV; goto err_devarg; } + if (dev->driver != NULL) { + RTE_LOG(ERR, EAL, "Device is already plugged\n"); + return -EEXIST; + } + ret = bus->plug(dev); if (ret) { RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n", @@ -225,6 +218,11 @@ rte_eal_hotplug_remove(const char *busname, const char *devname) return -EINVAL; } + if (dev->driver == NULL) { + RTE_LOG(ERR, EAL, "Device is already unplugged\n"); + return -ENOENT; + } + ret = bus->unplug(dev); if (ret) RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",