Skip to contentSkip to conversation

drivers/base: stop new probing during shutdown

PATCH In review Pingfan Liu 2 participants 2 messages Jul 19, 2018 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/base: stop new probing during shutdown

There is a race window in device_shutdown(), which may cause -1. parent device shut down before child or
-2. no shutdown on a new probing device.

For 1st, taking the following scenario:
device_shutdown new plugin device
list_del_init(parent_dev);
spin_unlock(list_lock);
device_add(child) probe child
shutdown parent_dev

       --> now child is on the tail of devices_kset

For 2nd, taking the following scenario:
device_shutdown new plugin device
device_add(dev)
device_lock(dev);
...

  device_unlock(dev);
                                                  probe dev
       --> now, the new occurred dev has no opportunity to shutdown

To fix this race issue, just prevent the new probing request. With this logic, device_shutdown() is more similar to dpm_prepare().

Cc Rafael J. Wysocki <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/base/core.c +3 -0
index df3e1a4..3aba4ad 100644
@@ -2809,6 +2809,9 @@ void device_shutdown(void)
 {
 	struct device *dev, *parent;
 
+	wait_for_device_probe();
+	device_block_probing();
+
 	spin_lock(&devices_kset->list_lock);
 	/*
 	 * Walk the devices list backward, shutting down each in turn.
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · CEST Pingfan Liu

There is a race window in device_shutdown(), which may cause -1. parent device shut down before child or
-2. no shutdown on a new probing device.

For 1st, taking the following scenario:
device_shutdown new plugin device
list_del_init(parent_dev);
spin_unlock(list_lock);
device_add(child) probe child
shutdown parent_dev

       --> now child is on the tail of devices_kset

For 2nd, taking the following scenario:
device_shutdown new plugin device
device_add(dev)
device_lock(dev);
...

Quoted 6 lines
  device_unlock(dev);
                                                  probe dev
       --> now, the new occurred dev has no opportunity to shutdown

To fix this race issue, just prevent the new probing request. With this logic, device_shutdown() is more similar to dpm_prepare().

Right.

This still doesn't prevent new children of a device from being added during shutdown (something like the PM ->prepare callback would be needed for that), but at least it prevents drivers from binding to the new devices.

Quoted 22 lines
Cc Rafael J. Wysocki <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/base/core.c +3 -0
index df3e1a4..3aba4ad 100644
@@ -2809,6 +2809,9 @@ void device_shutdown(void)
 {
 	struct device *dev, *parent;
 
+	wait_for_device_probe();
+	device_block_probing();
+
 	spin_lock(&devices_kset->list_lock);
 	/*
 	 * Walk the devices list backward, shutting down each in turn.