[12/21] driver core: bus: bus iterator cleanups

Message ID 20230208111330.439504-13-gregkh@linuxfoundation.org
State New
Headers
Series driver core: bus: remove private "backpointer" from struct bus_type |

Commit Message

Greg KH Feb. 8, 2023, 11:13 a.m. UTC
  Convert the bus_for_each_dev(), bus_find_device, and bus_for_each_drv()
functions to use bus_to_subsys() and not use the back-pointer to the
private structure.

Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/base/bus.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
  

Comments

Geert Uytterhoeven Feb. 21, 2023, 12:54 p.m. UTC | #1
Hi Greg,

On Wed, Feb 8, 2023 at 12:15 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
> Convert the bus_for_each_dev(), bus_find_device, and bus_for_each_drv()
> functions to use bus_to_subsys() and not use the back-pointer to the
> private structure.
>
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Thanks for your patch, which is now commit 83b9148df2c95e23 ("driver
core: bus: bus iterator cleanups") in driver-core-next.

I have bisected an early kernel crash on the Renesas Salvator-XS
board to this commit:

    Unable to handle kernel NULL pointer dereference at virtual
address 0000000000000028
    ...
    Call trace:
     __lock_acquire+0x530/0x20f0
     lock_acquire.part.0+0xc8/0x210
     lock_acquire+0x64/0x80
     _raw_spin_lock+0x4c/0x60
     bus_to_subsys+0x24/0xac
     bus_for_each_dev+0x30/0xcc
     soc_device_match+0x4c/0xe0
     r8a7795_sysc_init+0x18/0x60
     rcar_sysc_pd_init+0xb0/0x33c
     do_one_initcall+0x128/0x2bc

> --- a/drivers/base/bus.c
> +++ b/drivers/base/bus.c
> @@ -355,18 +355,20 @@ static struct device *next_device(struct klist_iter *i)
>  int bus_for_each_dev(const struct bus_type *bus, struct device *start,
>                      void *data, int (*fn)(struct device *, void *))
>  {
> +       struct subsys_private *sp = bus_to_subsys(bus);

If bus_to_subsys() is called from an early_initcall(), bus_kset is
still NULL, causing a crash.

>         struct klist_iter i;
>         struct device *dev;
>         int error = 0;
>
> -       if (!bus || !bus->p)

Before, the !bus->sp check prevented the crash...

> +       if (!sp)
>                 return -EINVAL;

... and instructed soc_device_match() to go into
early_soc_dev_attr mode.

I have sent a fix
"[PATCH] driver core: bus: Handle early calls to bus_to_subsys()"
https://lore.kernel.org/r/0a92979f6e790737544638e8a4c19b0564e660a2.1676983596.git.geert+renesas@glider.be

Gr{oetje,eeting}s,

                        Geert
  

Patch

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index b594804c716d..c007524151d2 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -355,18 +355,20 @@  static struct device *next_device(struct klist_iter *i)
 int bus_for_each_dev(const struct bus_type *bus, struct device *start,
 		     void *data, int (*fn)(struct device *, void *))
 {
+	struct subsys_private *sp = bus_to_subsys(bus);
 	struct klist_iter i;
 	struct device *dev;
 	int error = 0;
 
-	if (!bus || !bus->p)
+	if (!sp)
 		return -EINVAL;
 
-	klist_iter_init_node(&bus->p->klist_devices, &i,
+	klist_iter_init_node(&sp->klist_devices, &i,
 			     (start ? &start->p->knode_bus : NULL));
 	while (!error && (dev = next_device(&i)))
 		error = fn(dev, data);
 	klist_iter_exit(&i);
+	subsys_put(sp);
 	return error;
 }
 EXPORT_SYMBOL_GPL(bus_for_each_dev);
@@ -390,18 +392,20 @@  struct device *bus_find_device(const struct bus_type *bus,
 			       struct device *start, const void *data,
 			       int (*match)(struct device *dev, const void *data))
 {
+	struct subsys_private *sp = bus_to_subsys(bus);
 	struct klist_iter i;
 	struct device *dev;
 
-	if (!bus || !bus->p)
+	if (!sp)
 		return NULL;
 
-	klist_iter_init_node(&bus->p->klist_devices, &i,
+	klist_iter_init_node(&sp->klist_devices, &i,
 			     (start ? &start->p->knode_bus : NULL));
 	while ((dev = next_device(&i)))
 		if (match(dev, data) && get_device(dev))
 			break;
 	klist_iter_exit(&i);
+	subsys_put(sp);
 	return dev;
 }
 EXPORT_SYMBOL_GPL(bus_find_device);
@@ -440,18 +444,20 @@  static struct device_driver *next_driver(struct klist_iter *i)
 int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
 		     void *data, int (*fn)(struct device_driver *, void *))
 {
+	struct subsys_private *sp = bus_to_subsys(bus);
 	struct klist_iter i;
 	struct device_driver *drv;
 	int error = 0;
 
-	if (!bus)
+	if (!sp)
 		return -EINVAL;
 
-	klist_iter_init_node(&bus->p->klist_drivers, &i,
+	klist_iter_init_node(&sp->klist_drivers, &i,
 			     start ? &start->p->knode_bus : NULL);
 	while ((drv = next_driver(&i)) && !error)
 		error = fn(drv, data);
 	klist_iter_exit(&i);
+	subsys_put(sp);
 	return error;
 }
 EXPORT_SYMBOL_GPL(bus_for_each_drv);