driver core: create bus_is_registered()
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 8 Feb 2023 11:13:25 +0000 (12:13 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 9 Feb 2023 09:43:35 +0000 (10:43 +0100)
A local function to the driver core to determine if a bus really is
registered with the kernel or not.  To be used only by the driver core
code, as part of the driver registration path as it's not really "safe"
because the bus could be unregistered instantly after being called.

Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Link: https://lore.kernel.org/r/20230208111330.439504-17-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/base/base.h
drivers/base/bus.c
drivers/base/driver.c

index 9e06c18c7a644226c8ba2ade8f7cb794c03cf5e3..726a12a244c06174666a0ef8121c4363c16c0a99 100644 (file)
@@ -146,6 +146,7 @@ extern int bus_add_device(struct device *dev);
 extern void bus_probe_device(struct device *dev);
 extern void bus_remove_device(struct device *dev);
 void bus_notify(struct device *dev, enum bus_notifier_event value);
+bool bus_is_registered(const struct bus_type *bus);
 
 extern int bus_add_driver(struct device_driver *drv);
 extern void bus_remove_driver(struct device_driver *drv);
index 98edc7aa4fc4fdc793933a848f533661921aaf23..c82c65fde8a1c4103d7d42d4360c48e9d8befb28 100644 (file)
@@ -1322,6 +1322,22 @@ struct device_driver *driver_find(const char *name, struct bus_type *bus)
 }
 EXPORT_SYMBOL_GPL(driver_find);
 
+/*
+ * Warning, the value could go to "removed" instantly after calling this function, so be very
+ * careful when calling it...
+ */
+bool bus_is_registered(const struct bus_type *bus)
+{
+       struct subsys_private *sp = bus_to_subsys(bus);
+       bool is_initialized = false;
+
+       if (sp) {
+               is_initialized = true;
+               subsys_put(sp);
+       }
+       return is_initialized;
+}
+
 int __init buses_init(void)
 {
        bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
index aa5e5166a6719717bfd0de662d5f8bfcdfe05eeb..c8436c26ed6ab3012218020bf7528a0b1604860a 100644 (file)
@@ -224,7 +224,7 @@ int driver_register(struct device_driver *drv)
        int ret;
        struct device_driver *other;
 
-       if (!drv->bus->p) {
+       if (!bus_is_registered(drv->bus)) {
                pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
                           drv->name, drv->bus->name);
                return -EINVAL;