Skip to content

Commit ef0ff68

Browse files
Alexander Duyckgregkh
authored andcommitted
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>
1 parent ed88747 commit ef0ff68

3 files changed

Lines changed: 48 additions & 20 deletions

File tree

‎drivers/base/base.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct driver_private {
6565
* binding of drivers which were unable to get all the resources needed by
6666
* the device; typically because it depends on another driver getting
6767
* probed first.
68+
* @async_driver - pointer to device driver awaiting probe via async_probe
6869
* @device - pointer back to the struct device that this structure is
6970
* associated with.
7071
* @dead - This device is currently either in the process of or has been
@@ -80,6 +81,7 @@ struct device_private {
8081
struct klist_node knode_bus;
8182
struct klist_node knode_class;
8283
struct list_head deferred_probe;
84+
struct device_driver *async_driver;
8385
struct device *device;
8486
u8 dead:1;
8587
};

‎drivers/base/bus.c‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -610,17 +610,6 @@ static ssize_t uevent_store(struct device_driver *drv, const char *buf,
610610
}
611611
static DRIVER_ATTR_WO(uevent);
612612

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-
624613
/**
625614
* bus_add_driver - Add a driver to the bus.
626615
* @drv: driver.
@@ -653,15 +642,9 @@ int bus_add_driver(struct device_driver *drv)
653642

654643
klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
655644
if (drv->bus->p->drivers_autoprobe) {
656-
if (driver_allows_async_probing(drv)) {
657-
pr_debug("bus: '%s': probing driver %s asynchronously\n",
658-
drv->bus->name, drv->name);
659-
async_schedule(driver_attach_async, drv);
660-
} else {
661-
error = driver_attach(drv);
662-
if (error)
663-
goto out_unregister;
664-
}
645+
error = driver_attach(drv);
646+
if (error)
647+
goto out_unregister;
665648
}
666649
module_add_driver(drv->owner, drv);
667650

‎drivers/base/dd.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,30 @@ int device_driver_attach(struct device_driver *drv, struct device *dev)
925925
return ret;
926926
}
927927

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+
928952
static int __driver_attach(struct device *dev, void *data)
929953
{
930954
struct device_driver *drv = data;
@@ -952,6 +976,25 @@ static int __driver_attach(struct device *dev, void *data)
952976
return ret;
953977
} /* ret > 0 means positive match */
954978

979+
if (driver_allows_async_probing(drv)) {
980+
/*
981+
* Instead of probing the device synchronously we will
982+
* probe it asynchronously to allow for more parallelism.
983+
*
984+
* We only take the device lock here in order to guarantee
985+
* that the dev->driver and async_driver fields are protected
986+
*/
987+
dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
988+
device_lock(dev);
989+
if (!dev->driver) {
990+
get_device(dev);
991+
dev->p->async_driver = drv;
992+
async_schedule(__driver_attach_async_helper, dev);
993+
}
994+
device_unlock(dev);
995+
return 0;
996+
}
997+
955998
device_driver_attach(drv, dev);
956999

9571000
return 0;

0 commit comments

Comments
 (0)