Skip to content

Commit 3451a49

Browse files
Alexander Duyckgregkh
authored andcommitted
driver core: Establish order of operations for device_add and device_del via bitflag
Add an additional bit flag to the device_private struct named "dead". This additional flag provides a guarantee that when a device_del is executed on a given interface an async worker will not attempt to attach the driver following the earlier device_del call. Previously this guarantee was not present and could result in the device_del call attempting to remove a driver from an interface only to have the async worker attempt to probe the driver later when it finally completes the asynchronous probe call. One additional change added was that I pulled the check for dev->driver out of the __device_attach_driver call and instead placed it in the __device_attach_async_helper call. This was motivated by the fact that the only other caller of this, __device_attach, had already taken the device_lock() and checked for dev->driver. Instead of testing for this twice in this path it makes more sense to just consolidate the dev->dead and dev->driver checks together into one set of checks. Reviewed-by: Dan Williams <[email protected]> Reviewed-by: Rafael J. Wysocki <[email protected]> Signed-off-by: Alexander Duyck <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0fe6f78 commit 3451a49

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

drivers/base/base.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ struct driver_private {
6767
* probed first.
6868
* @device - pointer back to the struct device that this structure is
6969
* associated with.
70+
* @dead - This device is currently either in the process of or has been
71+
* removed from the system. Any asynchronous events scheduled for this
72+
* device should exit without taking any action.
7073
*
7174
* Nothing outside of the driver core should ever touch these fields.
7275
*/
@@ -78,6 +81,7 @@ struct device_private {
7881
struct klist_node knode_class;
7982
struct list_head deferred_probe;
8083
struct device *device;
84+
u8 dead:1;
8185
};
8286
#define to_device_private_parent(obj) \
8387
container_of(obj, struct device_private, knode_parent)

drivers/base/core.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,6 +2080,17 @@ void device_del(struct device *dev)
20802080
struct kobject *glue_dir = NULL;
20812081
struct class_interface *class_intf;
20822082

2083+
/*
2084+
* Hold the device lock and set the "dead" flag to guarantee that
2085+
* the update behavior is consistent with the other bitfields near
2086+
* it and that we cannot have an asynchronous probe routine trying
2087+
* to run while we are tearing out the bus/class/sysfs from
2088+
* underneath the device.
2089+
*/
2090+
device_lock(dev);
2091+
dev->p->dead = true;
2092+
device_unlock(dev);
2093+
20832094
/* Notify clients of device removal. This call must come
20842095
* before dpm_sysfs_remove().
20852096
*/

drivers/base/dd.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -731,15 +731,6 @@ static int __device_attach_driver(struct device_driver *drv, void *_data)
731731
bool async_allowed;
732732
int ret;
733733

734-
/*
735-
* Check if device has already been claimed. This may
736-
* happen with driver loading, device discovery/registration,
737-
* and deferred probe processing happens all at once with
738-
* multiple threads.
739-
*/
740-
if (dev->driver)
741-
return -EBUSY;
742-
743734
ret = driver_match_device(drv, dev);
744735
if (ret == 0) {
745736
/* no match */
@@ -774,6 +765,15 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
774765

775766
device_lock(dev);
776767

768+
/*
769+
* Check if device has already been removed or claimed. This may
770+
* happen with driver loading, device discovery/registration,
771+
* and deferred probe processing happens all at once with
772+
* multiple threads.
773+
*/
774+
if (dev->p->dead || dev->driver)
775+
goto out_unlock;
776+
777777
if (dev->parent)
778778
pm_runtime_get_sync(dev->parent);
779779

@@ -784,7 +784,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
784784

785785
if (dev->parent)
786786
pm_runtime_put(dev->parent);
787-
787+
out_unlock:
788788
device_unlock(dev);
789789

790790
put_device(dev);
@@ -897,7 +897,7 @@ static int __driver_attach(struct device *dev, void *data)
897897
if (dev->parent && dev->bus->need_parent_lock)
898898
device_lock(dev->parent);
899899
device_lock(dev);
900-
if (!dev->driver)
900+
if (!dev->p->dead && !dev->driver)
901901
driver_probe_device(drv, dev);
902902
device_unlock(dev);
903903
if (dev->parent && dev->bus->need_parent_lock)

0 commit comments

Comments
 (0)