Skip to content

Commit 29166a0

Browse files
diandersgregkh
authored andcommitted
driver core: Don't let a device probe until it's ready
[ Upstream commit a2225b6 ] The moment we link a "struct device" into the list of devices for the bus, it's possible probe can happen. This is because another thread can load the driver at any time and that can cause the device to probe. This has been seen in practice with a stack crawl that looks like this [1]: really_probe() __driver_probe_device() driver_probe_device() __driver_attach() bus_for_each_dev() driver_attach() bus_add_driver() driver_register() __platform_driver_register() init_module() [some module] do_one_initcall() do_init_module() load_module() __arm64_sys_finit_module() invoke_syscall() As a result of the above, it was seen that device_links_driver_bound() could be called for the device before "dev->fwnode->dev" was assigned. This prevented __fw_devlink_pickup_dangling_consumers() from being called which meant that other devices waiting on our driver's sub-nodes were stuck deferring forever. It's believed that this problem is showing up suddenly for two reasons: 1. Android has recently (last ~1 year) implemented an optimization to the order it loads modules [2]. When devices opt-in to this faster loading, modules are loaded one-after-the-other very quickly. This is unlike how other distributions do it. The reproduction of this problem has only been seen on devices that opt-in to Android's "parallel module loading". 2. Android devices typically opt-in to fw_devlink, and the most noticeable issue is the NULL "dev->fwnode->dev" in device_links_driver_bound(). fw_devlink is somewhat new code and also not in use by all Linux devices. Even though the specific symptom where "dev->fwnode->dev" wasn't assigned could be fixed by moving that assignment higher in device_add(), other parts of device_add() (like the call to device_pm_add()) are also important to run before probe. Only moving the "dev->fwnode->dev" assignment would likely fix the current symptoms but lead to difficult-to-debug problems in the future. Fix the problem by preventing probe until device_add() has run far enough that the device is ready to probe. If somehow we end up trying to probe before we're allowed, __driver_probe_device() will return -EPROBE_DEFER which will make certain the device is noticed. In the race condition that was seen with Android's faster module loading, we will temporarily add the device to the deferred list and then take it off immediately when device_add() probes the device. Instead of adding another flag to the bitfields already in "struct device", instead add a new "flags" field and use that. This allows us to freely change the bit from different thread without worrying about corrupting nearby bits (and means threads changing other bit won't corrupt us). [1] Captured on a machine running a downstream 6.6 kernel [2] https://cs.android.com/android/platform/superproject/main/+/main:system/core/libmodprobe/libmodprobe.cpp?q=LoadModulesParallel Cc: stable@vger.kernel.org Fixes: 2023c61 ("Driver core: add new device to bus's list before probing") Reviewed-by: Alan Stern <stern@rowland.harvard.edu> Reviewed-by: Rafael J. Wysocki (Intel) <rafael@kernel.org> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Acked-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Douglas Anderson <dianders@chromium.org> Link: https://patch.msgid.link/20260406162231.v5.1.Id750b0fbcc94f23ed04b7aecabcead688d0d8c17@changeid Signed-off-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Douglas Anderson <dianders@chromium.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 886f97f commit 29166a0

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

drivers/base/core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,21 @@ int device_add(struct device *dev)
36803680
fw_devlink_link_device(dev);
36813681
}
36823682

3683+
/*
3684+
* The moment the device was linked into the bus's "klist_devices" in
3685+
* bus_add_device() then it's possible that probe could have been
3686+
* attempted in a different thread via userspace loading a driver
3687+
* matching the device. "ready_to_probe" being unset would have
3688+
* blocked those attempts. Now that all of the above initialization has
3689+
* happened, unblock probe. If probe happens through another thread
3690+
* after this point but before bus_probe_device() runs then it's fine.
3691+
* bus_probe_device() -> device_initial_probe() -> __device_attach()
3692+
* will notice (under device_lock) that the device is already bound.
3693+
*/
3694+
device_lock(dev);
3695+
dev_set_ready_to_probe(dev);
3696+
device_unlock(dev);
3697+
36833698
bus_probe_device(dev);
36843699

36853700
/*

drivers/base/dd.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,26 @@ static int __driver_probe_device(struct device_driver *drv, struct device *dev)
785785
if (dev->driver)
786786
return -EBUSY;
787787

788+
/*
789+
* In device_add(), the "struct device" gets linked into the subsystem's
790+
* list of devices and broadcast to userspace (via uevent) before we're
791+
* quite ready to probe. Those open pathways to driver probe before
792+
* we've finished enough of device_add() to reliably support probe.
793+
* Detect this and tell other pathways to try again later. device_add()
794+
* itself will also try to probe immediately after setting
795+
* "ready_to_probe".
796+
*/
797+
if (!dev_ready_to_probe(dev))
798+
return dev_err_probe(dev, -EPROBE_DEFER, "Device not ready to probe\n");
799+
800+
/*
801+
* Set can_match = true after calling dev_ready_to_probe(), so
802+
* driver_deferred_probe_add() won't actually add the device to the
803+
* deferred probe list when dev_ready_to_probe() returns false.
804+
*
805+
* When dev_ready_to_probe() returns false, it means that device_add()
806+
* will do another probe() attempt for us.
807+
*/
788808
dev->can_match = true;
789809
pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
790810
drv->bus->name, __func__, dev_name(dev), drv->name);

include/linux/device.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,21 @@ struct device_physical_location {
602602
bool lid;
603603
};
604604

605+
/**
606+
* enum struct_device_flags - Flags in struct device
607+
*
608+
* Each flag should have a set of accessor functions created via
609+
* __create_dev_flag_accessors() for each access.
610+
*
611+
* @DEV_FLAG_READY_TO_PROBE: If set then device_add() has finished enough
612+
* initialization that probe could be called.
613+
*/
614+
enum struct_device_flags {
615+
DEV_FLAG_READY_TO_PROBE = 0,
616+
617+
DEV_FLAG_COUNT
618+
};
619+
605620
/**
606621
* struct device - The basic device structure
607622
* @parent: The device's "parent" device, the device to which it is attached.
@@ -693,6 +708,7 @@ struct device_physical_location {
693708
* and optionall (if the coherent mask is large enough) also
694709
* for dma allocations. This flag is managed by the dma ops
695710
* instance from ->dma_supported.
711+
* @flags: DEV_FLAG_XXX flags. Use atomic bitfield operations to modify.
696712
*
697713
* At the lowest level, every device in a Linux system is represented by an
698714
* instance of struct device. The device structure contains the information
@@ -805,8 +821,36 @@ struct device {
805821
#ifdef CONFIG_DMA_OPS_BYPASS
806822
bool dma_ops_bypass : 1;
807823
#endif
824+
825+
DECLARE_BITMAP(flags, DEV_FLAG_COUNT);
808826
};
809827

828+
#define __create_dev_flag_accessors(accessor_name, flag_name) \
829+
static inline bool dev_##accessor_name(const struct device *dev) \
830+
{ \
831+
return test_bit(flag_name, dev->flags); \
832+
} \
833+
static inline void dev_set_##accessor_name(struct device *dev) \
834+
{ \
835+
set_bit(flag_name, dev->flags); \
836+
} \
837+
static inline void dev_clear_##accessor_name(struct device *dev) \
838+
{ \
839+
clear_bit(flag_name, dev->flags); \
840+
} \
841+
static inline void dev_assign_##accessor_name(struct device *dev, bool value) \
842+
{ \
843+
assign_bit(flag_name, dev->flags, value); \
844+
} \
845+
static inline bool dev_test_and_set_##accessor_name(struct device *dev) \
846+
{ \
847+
return test_and_set_bit(flag_name, dev->flags); \
848+
}
849+
850+
__create_dev_flag_accessors(ready_to_probe, DEV_FLAG_READY_TO_PROBE);
851+
852+
#undef __create_dev_flag_accessors
853+
810854
/**
811855
* struct device_link - Device link representation.
812856
* @supplier: The device on the supplier end of the link.

0 commit comments

Comments
 (0)