[PATCH v3] driver core: Check drivers_autoprobe for all added devices

Vincent Liu posted 1 patch 3 months, 2 weeks ago
There is a newer version of this series
drivers/base/bus.c |  3 +--
drivers/base/dd.c  | 10 +++++++++-
2 files changed, 10 insertions(+), 3 deletions(-)
[PATCH v3] driver core: Check drivers_autoprobe for all added devices
Posted by Vincent Liu 3 months, 2 weeks ago
When a device is hot-plugged, the drivers_autoprobe sysfs attribute is
not checked (at least for PCI devices). This means that
drivers_autoprobe is not working as intended, e.g. hot-plugged PCI
devices will still be autoprobed and bound to drivers even with
drivers_autoprobe disabled.

The problem likely started when device_add() was removed from
pci_bus_add_device() in commit 4f535093cf8f ("PCI: Put pci_dev in device
tree as early as possible") which means that the check for
drivers_autoprobe which used to happen in bus_probe_device() is no
longer present (previously bus_add_device() calls bus_probe_device()).
Conveniently, in commit 91703041697c ("PCI: Allow built-in drivers to
use async initial probing") device_attach() was replaced with
device_initial_probe() which faciliates this change to push the check
for drivers_autoprobe into device_initial_probe().

Make sure all devices check drivers_autoprobe by pushing the
drivers_autoprobe check into device_initial_probe(). This will only
affect devices on the PCI bus for now as device_initial_probe() is only
called by pci_bus_add_device() and bus_probe_device(), but
bus_probe_device() already checks for autoprobe, so callers of
bus_probe_device() should not observe changes on autoprobing.
Note also that pushing this check into device_initial_probe() rather
than device_attach() makes it only affect automatic probing of
drivers (e.g. when a device is hot-plugged), userspace can still choose
to manually bind a driver by writing to drivers_probe sysfs attribute,
even with autoprobe disabled.

Any future callers of device_initial_probe() will respect the
drivers_autoprobe sysfs attribute, which is the intended purpose of
drivers_autoprobe.

Signed-off-by: Vincent Liu <vincent.liu@nutanix.com>

Link: https://lore.kernel.org/20251001151508.1684592-1-vincent.liu@nutanix.com
---
v1->v2: Change commit subject to include driver core (no code change)
	https://lore.kernel.org/20251001151508.1684592-1-vincent.liu@nutanix.com
v2->v3: Updated commit message to use PCI and include history (no code change)
	https://lore.kernel.org/20251013181459.517736-1-vincent.liu@nutanix.com/
---
 drivers/base/bus.c |  3 +--
 drivers/base/dd.c  | 10 +++++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 5e75e1bce551..320e155c6be7 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -533,8 +533,7 @@ void bus_probe_device(struct device *dev)
 	if (!sp)
 		return;
 
-	if (sp->drivers_autoprobe)
-		device_initial_probe(dev);
+	device_initial_probe(dev);
 
 	mutex_lock(&sp->mutex);
 	list_for_each_entry(sif, &sp->interfaces, node)
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 13ab98e033ea..37fc57e44e54 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -1077,7 +1077,15 @@ EXPORT_SYMBOL_GPL(device_attach);
 
 void device_initial_probe(struct device *dev)
 {
-	__device_attach(dev, true);
+	struct subsys_private *sp = bus_to_subsys(dev->bus);
+
+	if (!sp)
+		return;
+
+	if (sp->drivers_autoprobe)
+		__device_attach(dev, true);
+
+	subsys_put(sp);
 }
 
 /*
-- 
2.43.7
Re: [PATCH v3] driver core: Check drivers_autoprobe for all added devices
Posted by Vincent Liu 3 months ago
On 22 Oct 2025, at 13:07, Vincent Liu <vincent.liu@nutanix.com> wrote:
> 
> When a device is hot-plugged, the drivers_autoprobe sysfs attribute is
> not checked (at least for PCI devices). This means that
> drivers_autoprobe is not working as intended, e.g. hot-plugged PCI
> devices will still be autoprobed and bound to drivers even with
> drivers_autoprobe disabled.
> 
> The problem likely started when device_add() was removed from
> pci_bus_add_device() in commit 4f535093cf8f ("PCI: Put pci_dev in device
> tree as early as possible") which means that the check for
> drivers_autoprobe which used to happen in bus_probe_device() is no
> longer present (previously bus_add_device() calls bus_probe_device()).
> Conveniently, in commit 91703041697c ("PCI: Allow built-in drivers to
> use async initial probing") device_attach() was replaced with
> device_initial_probe() which faciliates this change to push the check
> for drivers_autoprobe into device_initial_probe().
> 
> Make sure all devices check drivers_autoprobe by pushing the
> drivers_autoprobe check into device_initial_probe(). This will only
> affect devices on the PCI bus for now as device_initial_probe() is only
> called by pci_bus_add_device() and bus_probe_device(), but
> bus_probe_device() already checks for autoprobe, so callers of
> bus_probe_device() should not observe changes on autoprobing.
> Note also that pushing this check into device_initial_probe() rather
> than device_attach() makes it only affect automatic probing of
> drivers (e.g. when a device is hot-plugged), userspace can still choose
> to manually bind a driver by writing to drivers_probe sysfs attribute,
> even with autoprobe disabled.
> 
> Any future callers of device_initial_probe() will respect the
> drivers_autoprobe sysfs attribute, which is the intended purpose of
> drivers_autoprobe.
> 
> Signed-off-by: Vincent Liu <vincent.liu@nutanix.com>
> 
> Link: https://lore.kernel.org/20251001151508.1684592-1-vincent.liu@nutanix.com
> ---
> v1->v2: Change commit subject to include driver core (no code change)
> https://lore.kernel.org/20251001151508.1684592-1-vincent.liu@nutanix.com
> v2->v3: Updated commit message to use PCI and include history (no code change)
> https://lore.kernel.org/20251013181459.517736-1-vincent.liu@nutanix.com/
> ---
> drivers/base/bus.c |  3 +--
> drivers/base/dd.c  | 10 +++++++++-
> 2 files changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/base/bus.c b/drivers/base/bus.c
> index 5e75e1bce551..320e155c6be7 100644
> --- a/drivers/base/bus.c
> +++ b/drivers/base/bus.c
> @@ -533,8 +533,7 @@ void bus_probe_device(struct device *dev)
> if (!sp)
> return;
> 
> - if (sp->drivers_autoprobe)
> - device_initial_probe(dev);
> + device_initial_probe(dev);
> 
> mutex_lock(&sp->mutex);
> list_for_each_entry(sif, &sp->interfaces, node)
> diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> index 13ab98e033ea..37fc57e44e54 100644
> --- a/drivers/base/dd.c
> +++ b/drivers/base/dd.c
> @@ -1077,7 +1077,15 @@ EXPORT_SYMBOL_GPL(device_attach);
> 
> void device_initial_probe(struct device *dev)
> {
> - __device_attach(dev, true);
> + struct subsys_private *sp = bus_to_subsys(dev->bus);
> +
> + if (!sp)
> + return;
> +
> + if (sp->drivers_autoprobe)
> + __device_attach(dev, true);
> +
> + subsys_put(sp);
> }
> 
> /*
> -- 
> 2.43.7
> 

Any further comments regarding this new commit message?

Thanks,
Vincent