Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

driver core: Probe devices asynchronously instead of the driver

Probe devices asynchronously instead of the driver. This results in us
seeing the same behavior if the device is registered before the driver or
after. This way we can avoid serializing the initialization should the
driver not be loaded until after the devices have already been added.

The motivation behind this is that if we have a set of devices that
take a significant amount of time to load we can greatly reduce the time to
load by processing them in parallel instead of one at a time. In addition,
each device can exist on a different node so placing a single thread on one
CPU to initialize all of the devices for a given driver can result in poor
performance on a system with multiple nodes.

This approach can reduce the time needed to scan SCSI LUNs significantly.
The only way to realize that speedup is by enabling more concurrency which
is what is achieved with this patch.

To achieve this it was necessary to add a new member "async_driver" to the
device_private structure to store the driver pointer while we wait on the
deferred probe call.

Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
Reviewed-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alexander Duyck and committed by
Greg Kroah-Hartman
ef0ff683 ed88747c

+48 -20
+2
drivers/base/base.h
··· 65 65 * binding of drivers which were unable to get all the resources needed by 66 66 * the device; typically because it depends on another driver getting 67 67 * probed first. 68 + * @async_driver - pointer to device driver awaiting probe via async_probe 68 69 * @device - pointer back to the struct device that this structure is 69 70 * associated with. 70 71 * @dead - This device is currently either in the process of or has been ··· 81 80 struct klist_node knode_bus; 82 81 struct klist_node knode_class; 83 82 struct list_head deferred_probe; 83 + struct device_driver *async_driver; 84 84 struct device *device; 85 85 u8 dead:1; 86 86 };
+3 -20
drivers/base/bus.c
··· 610 610 } 611 611 static DRIVER_ATTR_WO(uevent); 612 612 613 - static void driver_attach_async(void *_drv, async_cookie_t cookie) 614 - { 615 - struct device_driver *drv = _drv; 616 - int ret; 617 - 618 - ret = driver_attach(drv); 619 - 620 - pr_debug("bus: '%s': driver %s async attach completed: %d\n", 621 - drv->bus->name, drv->name, ret); 622 - } 623 - 624 613 /** 625 614 * bus_add_driver - Add a driver to the bus. 626 615 * @drv: driver. ··· 642 653 643 654 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 644 655 if (drv->bus->p->drivers_autoprobe) { 645 - if (driver_allows_async_probing(drv)) { 646 - pr_debug("bus: '%s': probing driver %s asynchronously\n", 647 - drv->bus->name, drv->name); 648 - async_schedule(driver_attach_async, drv); 649 - } else { 650 - error = driver_attach(drv); 651 - if (error) 652 - goto out_unregister; 653 - } 656 + error = driver_attach(drv); 657 + if (error) 658 + goto out_unregister; 654 659 } 655 660 module_add_driver(drv->owner, drv); 656 661
+43
drivers/base/dd.c
··· 925 925 return ret; 926 926 } 927 927 928 + static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie) 929 + { 930 + struct device *dev = _dev; 931 + struct device_driver *drv; 932 + int ret = 0; 933 + 934 + __device_driver_lock(dev, dev->parent); 935 + 936 + drv = dev->p->async_driver; 937 + 938 + /* 939 + * If device has been removed or someone has already successfully 940 + * bound a driver before us just skip the driver probe call. 941 + */ 942 + if (!dev->p->dead && !dev->driver) 943 + ret = driver_probe_device(drv, dev); 944 + 945 + __device_driver_unlock(dev, dev->parent); 946 + 947 + dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret); 948 + 949 + put_device(dev); 950 + } 951 + 928 952 static int __driver_attach(struct device *dev, void *data) 929 953 { 930 954 struct device_driver *drv = data; ··· 975 951 dev_dbg(dev, "Bus failed to match device: %d", ret); 976 952 return ret; 977 953 } /* ret > 0 means positive match */ 954 + 955 + if (driver_allows_async_probing(drv)) { 956 + /* 957 + * Instead of probing the device synchronously we will 958 + * probe it asynchronously to allow for more parallelism. 959 + * 960 + * We only take the device lock here in order to guarantee 961 + * that the dev->driver and async_driver fields are protected 962 + */ 963 + dev_dbg(dev, "probing driver %s asynchronously\n", drv->name); 964 + device_lock(dev); 965 + if (!dev->driver) { 966 + get_device(dev); 967 + dev->p->async_driver = drv; 968 + async_schedule(__driver_attach_async_helper, dev); 969 + } 970 + device_unlock(dev); 971 + return 0; 972 + } 978 973 979 974 device_driver_attach(drv, dev); 980 975