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

driver core: platform: simplify __platform_driver_probe()

__platform_driver_probe() pokes around in some bus and driver private
lists and locks in a way that is not needed at all. The code only wants
to know if a device was bound to the driver that was registered, so walk
all devices on the bus to see if there was a match. If there is not a
match, return an error. This is the same logic as was originally
present, but just done in a simpler and more obvious way that is not a
layering violation.

Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Link: https://lore.kernel.org/r/20230131082459.301603-2-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

+15 -11
+15 -11
drivers/base/platform.c
··· 883 883 return -ENXIO; 884 884 } 885 885 886 + static int is_bound_to_driver(struct device *dev, void *driver) 887 + { 888 + if (dev->driver == driver) 889 + return 1; 890 + return 0; 891 + } 892 + 886 893 /** 887 894 * __platform_driver_probe - register driver for non-hotpluggable device 888 895 * @drv: platform driver structure ··· 943 936 if (retval) 944 937 return retval; 945 938 946 - /* 947 - * Fixup that section violation, being paranoid about code scanning 948 - * the list of drivers in order to probe new devices. Check to see 949 - * if the probe was successful, and make sure any forced probes of 950 - * new devices fail. 951 - */ 952 - spin_lock(&drv->driver.bus->p->klist_drivers.k_lock); 939 + /* Force all new probes of this driver to fail */ 953 940 drv->probe = platform_probe_fail; 954 - if (list_empty(&drv->driver.p->klist_devices.k_list)) 955 - retval = -ENODEV; 956 - spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock); 957 941 958 - if (retval) 942 + /* Walk all platform devices and see if any actually bound to this driver. 943 + * If not, return an error as the device should have done so by now. 944 + */ 945 + if (!bus_for_each_dev(&platform_bus_type, NULL, &drv->driver, is_bound_to_driver)) { 946 + retval = -ENODEV; 959 947 platform_driver_unregister(drv); 948 + } 949 + 960 950 return retval; 961 951 } 962 952 EXPORT_SYMBOL_GPL(__platform_driver_probe);