Skip to contentSkip to conversation

gpio: omap: do not register driver in probe()

PATCH Applied Danilo Krummrich 3 participants 3 messages Jan 27 – Jan 28

Subsystems (3)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH v2 1/1 gpio: omap: do not register driver in probe()

Commit 11a78b794496 ("ARM: OMAP: MPUIO wake updates") registers the omap_mpuio_driver from omap_mpuio_init(), which is called from omap_gpio_probe().

However, it neither makes sense to register drivers from probe() callbacks of other drivers, nor does the driver core allow registering drivers with a device lock already being held.

The latter was revealed by commit dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()") leading to a potential deadlock condition described in [1].

Additionally, the omap_mpuio_driver is never unregistered from the driver core, even if the module is unloaded.

Hence, register the omap_mpuio_driver from the module initcall and unregister it in module_exit().

Fixes dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()")
Fixes 11a78b794496 ("ARM: OMAP: MPUIO wake updates")
---
 drivers/gpio/gpio-omap.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/gpio/gpio-omap.c +18 -4
index e136e81794df..e39723b5901b 100644
@@ -799,10 +799,13 @@ static struct platform_device omap_mpuio_device = {
 
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
-	platform_set_drvdata(&omap_mpuio_device, bank);
+	static bool registered;
 
-	if (platform_driver_register(&omap_mpuio_driver) == 0)
-		(void) platform_device_register(&omap_mpuio_device);
+	platform_set_drvdata(&omap_mpuio_device, bank);
+	if (!registered) {
+		(void)platform_device_register(&omap_mpuio_device);
+		registered = true;
+	}
 }
 
 /*---------------------------------------------------------------------*/
@@ -1575,13 +1578,24 @@ static struct platform_driver omap_gpio_driver = {
  */
 static int __init omap_gpio_drv_reg(void)
 {
-	return platform_driver_register(&omap_gpio_driver);
+	int ret;
+
+	ret = platform_driver_register(&omap_mpuio_driver);
+	if (ret)
+		return ret;
+
+	ret = platform_driver_register(&omap_gpio_driver);
+	if (ret)
+		platform_driver_unregister(&omap_mpuio_driver);
+
+	return ret;
 }
 postcore_initcall(omap_gpio_drv_reg);
 
 static void __exit omap_gpio_exit(void)
 {
 	platform_driver_unregister(&omap_gpio_driver);
+	platform_driver_unregister(&omap_mpuio_driver);
 }
 module_exit(omap_gpio_exit);
 

base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377

Signature · 1 line
2.52.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 16 lines · Danilo Krummrich

Commit 11a78b794496 ("ARM: OMAP: MPUIO wake updates") registers the omap_mpuio_driver from omap_mpuio_init(), which is called from omap_gpio_probe().

However, it neither makes sense to register drivers from probe() callbacks of other drivers, nor does the driver core allow registering drivers with a device lock already being held.

The latter was revealed by commit dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()") leading to a potential deadlock condition described in [1].

Additionally, the omap_mpuio_driver is never unregistered from the driver core, even if the module is unloaded.

Quoted 8 lines

Hence, register the omap_mpuio_driver from the module initcall and unregister it in module_exit().

Fixes dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()")
Fixes 11a78b794496 ("ARM: OMAP: MPUIO wake updates")
Quoted 56 lines
---
 drivers/gpio/gpio-omap.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/gpio/gpio-omap.c +18 -4
index e136e81794df..e39723b5901b 100644
@@ -799,10 +799,13 @@ static struct platform_device omap_mpuio_device = {
 
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
-       platform_set_drvdata(&omap_mpuio_device, bank);
+       static bool registered;
 
-       if (platform_driver_register(&omap_mpuio_driver) == 0)
-               (void) platform_device_register(&omap_mpuio_device);
+       platform_set_drvdata(&omap_mpuio_device, bank);
+       if (!registered) {
+               (void)platform_device_register(&omap_mpuio_device);
+               registered = true;
+       }
 }
 
 /*---------------------------------------------------------------------*/
@@ -1575,13 +1578,24 @@ static struct platform_driver omap_gpio_driver = {
  */
 static int __init omap_gpio_drv_reg(void)
 {
-       return platform_driver_register(&omap_gpio_driver);
+       int ret;
+
+       ret = platform_driver_register(&omap_mpuio_driver);
+       if (ret)
+               return ret;
+
+       ret = platform_driver_register(&omap_gpio_driver);
+       if (ret)
+               platform_driver_unregister(&omap_mpuio_driver);
+
+       return ret;
 }
 postcore_initcall(omap_gpio_drv_reg);
 
 static void __exit omap_gpio_exit(void)
 {
        platform_driver_unregister(&omap_gpio_driver);
+       platform_driver_unregister(&omap_mpuio_driver);
 }
 module_exit(omap_gpio_exit);
 

base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377 --
2.52.0

#2 lore raw
In reply to Danilo Krummrich
Quoted 9 lines · Danilo Krummrich

Commit 11a78b794496 ("ARM: OMAP: MPUIO wake updates") registers the omap_mpuio_driver from omap_mpuio_init(), which is called from omap_gpio_probe().

However, it neither makes sense to register drivers from probe() callbacks of other drivers, nor does the driver core allow registering drivers with a device lock already being held.

[...]

Applied, thanks!

[1/1] gpio: omap: do not register driver in probe()

      https://git.kernel.org/brgl/c/730e5ebff40c852e3ea57b71bf02a4b89c69435f

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>