Skip to contentSkip to conversation

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

PATCH Applied Danilo Krummrich 4 participants 12 messages Jan 23 – Jan 27

Subsystems (3)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 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")

---
In addition to this fix, the omap_mpuio_device (struct platform_device) should not be declared as global static, especially when their backing memory can become invalue due to the module being unloaded. Devices are reference counted and should be allocated dynamically. This needs a separate fix.

Besides that, the whole construct seems a bit questionable and I'm not exactly sure what should be achieved by registering the *same* static device everytime probe() is called for the omap_gpio_driver.

However, for the purpose of avoiding the described potential deadlock in combination with commit dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()"), this patch only addresses the driver registration issue.

---
 drivers/gpio/gpio-omap.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpio/gpio-omap.c +13 -4
index e136e81794df..8db71a2db9ff 100644
@@ -800,9 +800,7 @@ static struct platform_device omap_mpuio_device = {
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
 	platform_set_drvdata(&omap_mpuio_device, bank);
-
-	if (platform_driver_register(&omap_mpuio_driver) == 0)
-		(void) platform_device_register(&omap_mpuio_device);
+	(void)platform_device_register(&omap_mpuio_device);
 }
 
 /*---------------------------------------------------------------------*/
@@ -1575,13 +1573,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: ed1ac3c977dd6b119405fa36dd41f7151bd5b4de

Signature · 1 line
2.52.0
author maintainer
#1 lore raw
Quoted 4 lines · Danilo Krummrich

However, for the purpose of avoiding the described potential deadlock in combination with commit dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()"), this patch only addresses the driver registration issue.

I.e. unless there are any concerns, I'd like to take this one through the driver-core tree.

author maintainer
#4 lore raw
In reply to Danilo Krummrich

drivers/gpio/gpio-omap.c static struct platform_device omap_mpuio_device = { · line 804

Quoted 13 lines · Danilo Krummrich
drivers/gpio/gpio-omap.c +1 -3
index e136e81794df..8db71a2db9ff 100644
@@ -800,9 +800,7 @@ static struct platform_device omap_mpuio_device = {
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
 	platform_set_drvdata(&omap_mpuio_device, bank);
-
-	if (platform_driver_register(&omap_mpuio_driver) == 0)
-		(void) platform_device_register(&omap_mpuio_device);
+	(void)platform_device_register(&omap_mpuio_device);
 }

On a second look, it recognize that this did abuse the fact that platform_driver_register() fails when attempting to register a driver multiple times to avoid registering the same static device multiple times.

So, I guess this has to be changed to:

drivers/gpio/gpio-omap.c +6 -1
index 8db71a2db9ff..3e1ac34994fb 100644
@@ -799,8 +799,13 @@ static struct platform_device omap_mpuio_device = {
 
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
+       static bool registered = false;
+
        platform_set_drvdata(&omap_mpuio_device, bank);
-       (void)platform_device_register(&omap_mpuio_device);
+       if (!registered) {
+               (void)platform_device_register(&omap_mpuio_device);
+               registered = true;
+       }
 }
 
 /*---------------------------------------------------------------------*/
maintainer
#5 lore raw
Quoted 28 lines · Danilo Krummrich

drivers/gpio/gpio-omap.c static struct platform_device omap_mpuio_device = { · line 804

Quoted 13 lines · Danilo Krummrich
drivers/gpio/gpio-omap.c +1 -3
index e136e81794df..8db71a2db9ff 100644
@@ -800,9 +800,7 @@ static struct platform_device omap_mpuio_device = {
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
 	platform_set_drvdata(&omap_mpuio_device, bank);
-
-	if (platform_driver_register(&omap_mpuio_driver) == 0)
-		(void) platform_device_register(&omap_mpuio_device);
+	(void)platform_device_register(&omap_mpuio_device);
 }

On a second look, it recognize that this did abuse the fact that platform_driver_register() fails when attempting to register a driver multiple times to avoid registering the same static device multiple times.

So, I guess this has to be changed to:

drivers/gpio/gpio-omap.c +0 -0
index 8db71a2db9ff..3e1ac34994fb 100644
@@ -799,8 +799,13 @@ static struct platform_device omap_mpuio_device = {
 
 static inline void omap_mpuio_init(struct gpio_bank *bank)

drivers/gpio/gpio-omap.c static struct platform_device omap_mpuio_device = { · line 809

Quoted 10 lines
drivers/gpio/gpio-omap.c +6 -1
 {
+       static bool registered = false;
+
        platform_set_drvdata(&omap_mpuio_device, bank);
-       (void)platform_device_register(&omap_mpuio_device);
+       if (!registered) {
+               (void)platform_device_register(&omap_mpuio_device);
+               registered = true;
+       }
 }

But there are no platform resources for this at all, shouldn't this be a faux device instead?

That being said, ignoring the return value of platform_device_register() is probably not something we want to keep around.

thanks,

greg k-h

author maintainer
#6 lore raw
Quoted 28 lines · Greg KH
Quoted 27 lines · Danilo Krummrich

drivers/gpio/gpio-omap.c static struct platform_device omap_mpuio_device = { · line 804

Quoted 13 lines · Danilo Krummrich
drivers/gpio/gpio-omap.c +1 -3
index e136e81794df..8db71a2db9ff 100644
@@ -800,9 +800,7 @@ static struct platform_device omap_mpuio_device = {
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
 	platform_set_drvdata(&omap_mpuio_device, bank);
-
-	if (platform_driver_register(&omap_mpuio_driver) == 0)
-		(void) platform_device_register(&omap_mpuio_device);
+	(void)platform_device_register(&omap_mpuio_device);
 }

On a second look, it recognize that this did abuse the fact that platform_driver_register() fails when attempting to register a driver multiple times to avoid registering the same static device multiple times.

So, I guess this has to be changed to:

drivers/gpio/gpio-omap.c +0 -0
index 8db71a2db9ff..3e1ac34994fb 100644
@@ -799,8 +799,13 @@ static struct platform_device omap_mpuio_device = {
 
Quoted 14 lines

drivers/gpio/gpio-omap.c static struct platform_device omap_mpuio_device = { · line 809

Quoted 11 lines
drivers/gpio/gpio-omap.c +6 -1
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
+       static bool registered = false;
+
        platform_set_drvdata(&omap_mpuio_device, bank);
-       (void)platform_device_register(&omap_mpuio_device);
+       if (!registered) {
+               (void)platform_device_register(&omap_mpuio_device);
+               registered = true;
+       }
 }

But there are no platform resources for this at all, shouldn't this be a faux device instead?

Probably, but that's for another patch, since this one may potentially be backported beyond the existence of the faux bus.

Quoted 2 lines

That being said, ignoring the return value of platform_device_register() is probably not something we want to keep around.

Yes, as mentioned below the commit message, there are a couple of things that need to be followed up on here.

With this patch I only intend to fix the deadlock condition and otherwise keep all the existing semantics as it is.

I.e. maybe it is intentional and this driver should not abort probing if this can't be registered for some reason.

maintainer Reviewed-by
#7 lore raw
Quoted 50 lines · Danilo Krummrich
Quoted 28 lines · Greg KH
Quoted 27 lines · Danilo Krummrich

drivers/gpio/gpio-omap.c static struct platform_device omap_mpuio_device = { · line 804

Quoted 13 lines · Danilo Krummrich
drivers/gpio/gpio-omap.c +1 -3
index e136e81794df..8db71a2db9ff 100644
@@ -800,9 +800,7 @@ static struct platform_device omap_mpuio_device = {
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
 	platform_set_drvdata(&omap_mpuio_device, bank);
-
-	if (platform_driver_register(&omap_mpuio_driver) == 0)
-		(void) platform_device_register(&omap_mpuio_device);
+	(void)platform_device_register(&omap_mpuio_device);
 }

On a second look, it recognize that this did abuse the fact that platform_driver_register() fails when attempting to register a driver multiple times to avoid registering the same static device multiple times.

So, I guess this has to be changed to:

drivers/gpio/gpio-omap.c +0 -0
index 8db71a2db9ff..3e1ac34994fb 100644
@@ -799,8 +799,13 @@ static struct platform_device omap_mpuio_device = {
 
Quoted 14 lines

drivers/gpio/gpio-omap.c static struct platform_device omap_mpuio_device = { · line 809

Quoted 11 lines
drivers/gpio/gpio-omap.c +6 -1
 static inline void omap_mpuio_init(struct gpio_bank *bank)
 {
+       static bool registered = false;
+
        platform_set_drvdata(&omap_mpuio_device, bank);
-       (void)platform_device_register(&omap_mpuio_device);
+       if (!registered) {
+               (void)platform_device_register(&omap_mpuio_device);
+               registered = true;
+       }
 }

But there are no platform resources for this at all, shouldn't this be a faux device instead?

Probably, but that's for another patch, since this one may potentially be backported beyond the existence of the faux bus.

Quoted 2 lines

That being said, ignoring the return value of platform_device_register() is probably not something we want to keep around.

Quoted 8 lines

Yes, as mentioned below the commit message, there are a couple of things that need to be followed up on here.

With this patch I only intend to fix the deadlock condition and otherwise keep all the existing semantics as it is.

I.e. maybe it is intentional and this driver should not abort probing if this can't be registered for some reason.

Ok, fair enough, fixing this immediate bug is good enough for me.

author maintainer
#8 lore raw
Quoted 17 lines · Greg KH
Quoted 16 lines · Danilo Krummrich
Quoted 2 lines · Greg KH

But there are no platform resources for this at all, shouldn't this be a faux device instead?

Probably, but that's for another patch, since this one may potentially be backported beyond the existence of the faux bus.

Quoted 2 lines

That being said, ignoring the return value of platform_device_register() is probably not something we want to keep around.

Yes, as mentioned below the commit message, there are a couple of things that need to be followed up on here.

With this patch I only intend to fix the deadlock condition and otherwise keep all the existing semantics as it is.

Quoted 6 lines
Quoted 2 lines

I.e. maybe it is intentional and this driver should not abort probing if this can't be registered for some reason.

Ok, fair enough, fixing this immediate bug is good enough for me.

Just for the record, the things that should still be addressed:

(1) Potential UAF on module unload: The device is declared as global static (should use a dynamic allocation instead); device is never unregistered.

(2) Handle return value of *_device_register().

(3) Use faux bus; maybe not directly suitable as omap_mpuio_driver only uses struct dev_pm_ops callbacks. Though, we could extend struct faux_device_ops accordingly.

- Danilo

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

However, for the purpose of avoiding the described potential deadlock in combination with commit dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()"), this patch only addresses the driver registration issue.

I.e. unless there are any concerns, I'd like to take this one through the driver-core tree.

It looks good to me, but it's a GPIO driver, this can go together with GPIO fixes for v6.19-rc8 as usual.

Bart

author maintainer
#3 lore raw
Quoted 8 lines · Bartosz Golaszewski
Quoted 7 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich

However, for the purpose of avoiding the described potential deadlock in combination with commit dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()"), this patch only addresses the driver registration issue.

Quoted 5 lines
Quoted 2 lines

I.e. unless there are any concerns, I'd like to take this one through the driver-core tree.

It looks good to me, but it's a GPIO driver, this can go together with GPIO fixes for v6.19-rc8 as usual.

Commit [1] reveals the issue and was originally scheduled for a driver-core fixes PR, so it made sense for this patch to go together with [1]. However, I told Linus [2] that I will hold back [1] for now and send it with the -rc1 PR to give it some more time in linux-next.

So, if you can take this patch for -rc8, that'd be great.

Thanks,
Danilo

[1] dc23806a7c47 ("driver core: enforce device_lock for driver_match_device()")
[2] https://lore.kernel.org/driver-core/[email protected]/

#9 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()

      commit: 3cb53b083fa665ec14c52962f50b9c1df48cf87b

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
author maintainer
#10 lore raw
Quoted 4 lines · Bartosz Golaszewski

Applied, thanks!

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

      commit: 3cb53b083fa665ec14c52962f50b9c1df48cf87b

I think you missed to pick up the diff in [1]. Please let me know if you want a fixup patch for this.

(One unrelated note, just be because I noticed by accident. Your applied patches seem to use https://lore.kernel.org for Link: tags, while it has been agreed to use https://patch.msgid.link instead [2].

I just mention it since it might be unintentional, i.e. it might be that your tooling does not consider it.

Personally, I use b4 and have the following in my .gitconfig:

	[b4]
		linkmask = https://patch.msgid.link/%s

Not sure this is still necessary in the latest version though.)

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CAHk-=wj5MATvT-FR8qNpXuuBGiJdjY1kRfhtzuyBSpTKR+=Vtw@mail.gmail.com/

#11 lore raw
Quoted 10 lines · Danilo Krummrich
Quoted 4 lines · Bartosz Golaszewski

Applied, thanks!

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

      commit: 3cb53b083fa665ec14c52962f50b9c1df48cf87b

I think you missed to pick up the diff in [1]. Please let me know if you want a fixup patch for this.

Yes, can you send a v2 with that included? I'm dropping this patch from my queue for now.

Quoted 4 lines

(One unrelated note, just be because I noticed by accident. Your applied patches seem to use https://lore.kernel.org for Link: tags, while it has been agreed to use https://patch.msgid.link instead [2].

Quoted 8 lines

I just mention it since it might be unintentional, i.e. it might be that your tooling does not consider it.

Personally, I use b4 and have the following in my .gitconfig:

[b4]
linkmask = https://patch.msgid.link/%s

Yeah, I do use b4. I changed the linkmask now, thanks.

Bartosz

Quoted 4 lines

Not sure this is still necessary in the latest version though.)

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CAHk-=wj5MATvT-FR8qNpXuuBGiJdjY1kRfhtzuyBSpTKR+=Vtw@mail.gmail.com/