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

driver-core: add asynchronous probing support for drivers

Some devices take a long time when initializing, and not all drivers are
suited to initialize their devices when they are open. For example,
input drivers need to interrogate their devices in order to publish
device's capabilities before userspace will open them. When such drivers
are compiled into kernel they may stall entire kernel initialization.

This change allows drivers request for their probe functions to be
called asynchronously during driver and device registration (manual
binding is still synchronous). Because async_schedule is used to perform
asynchronous calls module loading will still wait for the probing to
complete.

Note that the end goal is to make the probing asynchronous by default,
so annotating drivers with PROBE_PREFER_ASYNCHRONOUS is a temporary
measure that allows us to speed up boot process while we validating and
fixing the rest of the drivers and preparing userspace.

This change is based on earlier patch by "Luis R. Rodriguez"
<mcgrof@suse.com>

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Dmitry Torokhov and committed by
Greg Kroah-Hartman
765230b5 ecc86170

+187 -32
+1
drivers/base/base.h
··· 116 116 { 117 117 return drv->bus->match ? drv->bus->match(dev, drv) : 1; 118 118 } 119 + extern bool driver_allows_async_probing(struct device_driver *drv); 119 120 120 121 extern int driver_add_groups(struct device_driver *drv, 121 122 const struct attribute_group **groups);
+23 -8
drivers/base/bus.c
··· 10 10 * 11 11 */ 12 12 13 + #include <linux/async.h> 13 14 #include <linux/device.h> 14 15 #include <linux/module.h> 15 16 #include <linux/errno.h> ··· 550 549 { 551 550 struct bus_type *bus = dev->bus; 552 551 struct subsys_interface *sif; 553 - int ret; 554 552 555 553 if (!bus) 556 554 return; 557 555 558 - if (bus->p->drivers_autoprobe) { 559 - ret = device_attach(dev); 560 - WARN_ON(ret < 0); 561 - } 556 + if (bus->p->drivers_autoprobe) 557 + device_initial_probe(dev); 562 558 563 559 mutex_lock(&bus->p->mutex); 564 560 list_for_each_entry(sif, &bus->p->interfaces, node) ··· 657 659 } 658 660 static DRIVER_ATTR_WO(uevent); 659 661 662 + static void driver_attach_async(void *_drv, async_cookie_t cookie) 663 + { 664 + struct device_driver *drv = _drv; 665 + int ret; 666 + 667 + ret = driver_attach(drv); 668 + 669 + pr_debug("bus: '%s': driver %s async attach completed: %d\n", 670 + drv->bus->name, drv->name, ret); 671 + } 672 + 660 673 /** 661 674 * bus_add_driver - Add a driver to the bus. 662 675 * @drv: driver. ··· 700 691 701 692 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 702 693 if (drv->bus->p->drivers_autoprobe) { 703 - error = driver_attach(drv); 704 - if (error) 705 - goto out_unregister; 694 + if (driver_allows_async_probing(drv)) { 695 + pr_debug("bus: '%s': probing driver %s asynchronously\n", 696 + drv->bus->name, drv->name); 697 + async_schedule(driver_attach_async, drv); 698 + } else { 699 + error = driver_attach(drv); 700 + if (error) 701 + goto out_unregister; 702 + } 706 703 } 707 704 module_add_driver(drv->owner, drv); 708 705
+135 -24
drivers/base/dd.c
··· 417 417 return ret; 418 418 } 419 419 420 - static int __device_attach(struct device_driver *drv, void *data) 420 + bool driver_allows_async_probing(struct device_driver *drv) 421 421 { 422 - struct device *dev = data; 422 + return drv->probe_type == PROBE_PREFER_ASYNCHRONOUS; 423 + } 424 + 425 + struct device_attach_data { 426 + struct device *dev; 427 + 428 + /* 429 + * Indicates whether we are are considering asynchronous probing or 430 + * not. Only initial binding after device or driver registration 431 + * (including deferral processing) may be done asynchronously, the 432 + * rest is always synchronous, as we expect it is being done by 433 + * request from userspace. 434 + */ 435 + bool check_async; 436 + 437 + /* 438 + * Indicates if we are binding synchronous or asynchronous drivers. 439 + * When asynchronous probing is enabled we'll execute 2 passes 440 + * over drivers: first pass doing synchronous probing and second 441 + * doing asynchronous probing (if synchronous did not succeed - 442 + * most likely because there was no driver requiring synchronous 443 + * probing - and we found asynchronous driver during first pass). 444 + * The 2 passes are done because we can't shoot asynchronous 445 + * probe for given device and driver from bus_for_each_drv() since 446 + * driver pointer is not guaranteed to stay valid once 447 + * bus_for_each_drv() iterates to the next driver on the bus. 448 + */ 449 + bool want_async; 450 + 451 + /* 452 + * We'll set have_async to 'true' if, while scanning for matching 453 + * driver, we'll encounter one that requests asynchronous probing. 454 + */ 455 + bool have_async; 456 + }; 457 + 458 + static int __device_attach_driver(struct device_driver *drv, void *_data) 459 + { 460 + struct device_attach_data *data = _data; 461 + struct device *dev = data->dev; 462 + bool async_allowed; 463 + 464 + /* 465 + * Check if device has already been claimed. This may 466 + * happen with driver loading, device discovery/registration, 467 + * and deferred probe processing happens all at once with 468 + * multiple threads. 469 + */ 470 + if (dev->driver) 471 + return -EBUSY; 423 472 424 473 if (!driver_match_device(drv, dev)) 425 474 return 0; 426 475 476 + async_allowed = driver_allows_async_probing(drv); 477 + 478 + if (async_allowed) 479 + data->have_async = true; 480 + 481 + if (data->check_async && async_allowed != data->want_async) 482 + return 0; 483 + 427 484 return driver_probe_device(drv, dev); 485 + } 486 + 487 + static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) 488 + { 489 + struct device *dev = _dev; 490 + struct device_attach_data data = { 491 + .dev = dev, 492 + .check_async = true, 493 + .want_async = true, 494 + }; 495 + 496 + device_lock(dev); 497 + 498 + bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver); 499 + dev_dbg(dev, "async probe completed\n"); 500 + 501 + pm_request_idle(dev); 502 + 503 + device_unlock(dev); 504 + 505 + put_device(dev); 506 + } 507 + 508 + int __device_attach(struct device *dev, bool allow_async) 509 + { 510 + int ret = 0; 511 + 512 + device_lock(dev); 513 + if (dev->driver) { 514 + if (klist_node_attached(&dev->p->knode_driver)) { 515 + ret = 1; 516 + goto out_unlock; 517 + } 518 + ret = device_bind_driver(dev); 519 + if (ret == 0) 520 + ret = 1; 521 + else { 522 + dev->driver = NULL; 523 + ret = 0; 524 + } 525 + } else { 526 + struct device_attach_data data = { 527 + .dev = dev, 528 + .check_async = allow_async, 529 + .want_async = false, 530 + }; 531 + 532 + ret = bus_for_each_drv(dev->bus, NULL, &data, 533 + __device_attach_driver); 534 + if (!ret && allow_async && data.have_async) { 535 + /* 536 + * If we could not find appropriate driver 537 + * synchronously and we are allowed to do 538 + * async probes and there are drivers that 539 + * want to probe asynchronously, we'll 540 + * try them. 541 + */ 542 + dev_dbg(dev, "scheduling asynchronous probe\n"); 543 + get_device(dev); 544 + async_schedule(__device_attach_async_helper, dev); 545 + } else { 546 + pm_request_idle(dev); 547 + } 548 + } 549 + out_unlock: 550 + device_unlock(dev); 551 + return ret; 428 552 } 429 553 430 554 /** ··· 567 443 */ 568 444 int device_attach(struct device *dev) 569 445 { 570 - int ret = 0; 571 - 572 - device_lock(dev); 573 - if (dev->driver) { 574 - if (klist_node_attached(&dev->p->knode_driver)) { 575 - ret = 1; 576 - goto out_unlock; 577 - } 578 - ret = device_bind_driver(dev); 579 - if (ret == 0) 580 - ret = 1; 581 - else { 582 - dev->driver = NULL; 583 - ret = 0; 584 - } 585 - } else { 586 - ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach); 587 - pm_request_idle(dev); 588 - } 589 - out_unlock: 590 - device_unlock(dev); 591 - return ret; 446 + return __device_attach(dev, false); 592 447 } 593 448 EXPORT_SYMBOL_GPL(device_attach); 449 + 450 + void device_initial_probe(struct device *dev) 451 + { 452 + __device_attach(dev, true); 453 + } 594 454 595 455 static int __driver_attach(struct device *dev, void *data) 596 456 { ··· 630 522 631 523 drv = dev->driver; 632 524 if (drv) { 525 + if (driver_allows_async_probing(drv)) 526 + async_synchronize_full(); 527 + 633 528 pm_runtime_get_sync(dev); 634 529 635 530 driver_sysfs_remove(dev);
+28
include/linux/device.h
··· 196 196 extern struct klist *bus_get_device_klist(struct bus_type *bus); 197 197 198 198 /** 199 + * enum probe_type - device driver probe type to try 200 + * Device drivers may opt in for special handling of their 201 + * respective probe routines. This tells the core what to 202 + * expect and prefer. 203 + * 204 + * @PROBE_SYNCHRONOUS: Default. Drivers expect their probe routines 205 + * to run synchronously with driver and device registration 206 + * (with the exception of -EPROBE_DEFER handling - re-probing 207 + * always ends up being done asynchronously). 208 + * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which 209 + * probing order is not essential for booting the system may 210 + * opt into executing their probes asynchronously. 211 + * 212 + * Note that the end goal is to switch the kernel to use asynchronous 213 + * probing by default, so annotating drivers with 214 + * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us 215 + * to speed up boot process while we are validating the rest of the 216 + * drivers. 217 + */ 218 + enum probe_type { 219 + PROBE_SYNCHRONOUS, 220 + PROBE_PREFER_ASYNCHRONOUS, 221 + }; 222 + 223 + /** 199 224 * struct device_driver - The basic device driver structure 200 225 * @name: Name of the device driver. 201 226 * @bus: The bus which the device of this driver belongs to. 202 227 * @owner: The module owner. 203 228 * @mod_name: Used for built-in modules. 204 229 * @suppress_bind_attrs: Disables bind/unbind via sysfs. 230 + * @probe_type: Type of the probe (synchronous or asynchronous) to use. 205 231 * @of_match_table: The open firmware table. 206 232 * @acpi_match_table: The ACPI match table. 207 233 * @probe: Called to query the existence of a specific device, ··· 261 235 const char *mod_name; /* used for built-in modules */ 262 236 263 237 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ 238 + enum probe_type probe_type; 264 239 265 240 const struct of_device_id *of_match_table; 266 241 const struct acpi_device_id *acpi_match_table; ··· 1002 975 extern void device_release_driver(struct device *dev); 1003 976 extern int __must_check device_attach(struct device *dev); 1004 977 extern int __must_check driver_attach(struct device_driver *drv); 978 + extern void device_initial_probe(struct device *dev); 1005 979 extern int __must_check device_reprobe(struct device *dev); 1006 980 1007 981 /*