Skip to contentSkip to conversation

gpiolib: fix allocation of mixed dynamic/static GPIOs

PATCH Superseded Andreas Kemnade 3 participants 8 messages Apr 26, 2023 – Apr 27, 2023 GPIO

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: fix allocation of mixed dynamic/static GPIOs

If static allocation and dynamic allocation GPIOs are present, dynamic allocation pollutes the numberspace for static allocation, causing static allocation to fail.
Enfore dynamic allocation above GPIO_DYNAMIC_BASE.

Seen on a GTA04 when omap-gpio (static) and twl-gpio (dynamic) raced.
On that device it is fixed invasively by
commit 92bf78b33b0b4 ("gpio: omap: use dynamic allocation of base") but lets also fix that for devices where there is still a mixture of static and dynamic allocation.

Fixes 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS")
Suggested-by [email protected]
---
 drivers/gpio/gpiolib.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpio/gpiolib.c +4 -0
index 19bd23044b017..18b68d0aec7db 100644
@@ -188,6 +188,10 @@ static int gpiochip_find_base(int ngpio)
 	int base = GPIO_DYNAMIC_BASE;
 
 	list_for_each_entry(gdev, &gpio_devices, list) {
+		/* do not pollute area for static allocation */
+		if (gdev->base < GPIO_DYNAMIC_BASE)
+			continue;
+
 		/* found a free space? */
 		if (gdev->base >= base + ngpio)
 			break;
Signature · 1 line
2.39.2
#1 lore raw

Le 27/04/2023 à 00:03, Andreas Kemnade a écrit :

Quoted 6 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

If static allocation and dynamic allocation GPIOs are present, dynamic allocation pollutes the numberspace for static allocation, causing static allocation to fail.
Enfore dynamic allocation above GPIO_DYNAMIC_BASE.

Hum ....

Commit 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS") was supposed to enforce dynamic allocation above GPIO_DYNAMIC_BASE already.

Can you describe what is going wrong exactly with the above commit ?

Thanks
Christophe

Quoted 33 lines

Seen on a GTA04 when omap-gpio (static) and twl-gpio (dynamic) raced.
On that device it is fixed invasively by
commit 92bf78b33b0b4 ("gpio: omap: use dynamic allocation of base") but lets also fix that for devices where there is still a mixture of static and dynamic allocation.

Fixes 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS")
Suggested-by [email protected]
---
  drivers/gpio/gpiolib.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/gpio/gpiolib.c +4 -1
index 19bd23044b017..18b68d0aec7db 100644
@@ -188,6 +188,10 @@ static int gpiochip_find_base(int ngpio)
         int base = GPIO_DYNAMIC_BASE;
 
         list_for_each_entry(gdev, &gpio_devices, list) {
+               /* do not pollute area for static allocation */
+               if (gdev->base < GPIO_DYNAMIC_BASE)
+                       continue;
+
                 /* found a free space? */
                 if (gdev->base >= base + ngpio)
                         break;
--

2.39.2

#2 lore raw
Quoted 11 lines · Christophe Leroy
Quoted 6 lines · Andreas Kemnade

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

If static allocation and dynamic allocation GPIOs are present, dynamic allocation pollutes the numberspace for static allocation, causing static allocation to fail.
Enfore dynamic allocation above GPIO_DYNAMIC_BASE.

Quoted 6 lines

Hum ....

Commit 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS") was supposed to enforce dynamic allocation above GPIO_DYNAMIC_BASE already.

Can you describe what is going wrong exactly with the above commit ?

Above commit only works to the first dynamic allocation, if you need more than one with static ones present it mistakenly will give you a base _below_ DYNAMIC_BASE.

However, this change is just PoC I proposed, the conditional and action should be slightly different to cover a corner case, when statically allocated chip overlaps the DYNAMIC_BASE, i.e. gdev->base < DYNAMIC_BASE, while gdev->base + gdev->ngpio >= DYNAMIC_BASE.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Le 27/04/2023 à 08:00, Andy Shevchenko a écrit :

Quoted 15 lines
Quoted 13 lines · Christophe Leroy
Quoted 6 lines · Andreas Kemnade

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

If static allocation and dynamic allocation GPIOs are present, dynamic allocation pollutes the numberspace for static allocation, causing static allocation to fail.
Enfore dynamic allocation above GPIO_DYNAMIC_BASE.

Hum ....

Quoted 8 lines
Quoted 4 lines

Commit 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS") was supposed to enforce dynamic allocation above GPIO_DYNAMIC_BASE already.

Can you describe what is going wrong exactly with the above commit ?

Above commit only works to the first dynamic allocation, if you need more than one with static ones present it mistakenly will give you a base _below_ DYNAMIC_BASE.

Ah right, that needs to be fixed.

Quoted 6 lines

However, this change is just PoC I proposed, the conditional and action should be slightly different to cover a corner case, when statically allocated chip overlaps the DYNAMIC_BASE, i.e. gdev->base < DYNAMIC_BASE, while gdev->base + gdev->ngpio >= DYNAMIC_BASE.

Yes you are right, that's gdev->base + gdev->ngpio that should be checked.

Christophe

#4 lore raw
Quoted 28 lines · On Thu, 27 Apr 2023 06:20:34 +0000 Christophe Leroy…

Le 27/04/2023 à 08:00, Andy Shevchenko a écrit :

Quoted 15 lines
Quoted 13 lines · Christophe Leroy
Quoted 6 lines · Andreas Kemnade

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

If static allocation and dynamic allocation GPIOs are present, dynamic allocation pollutes the numberspace for static allocation, causing static allocation to fail.
Enfore dynamic allocation above GPIO_DYNAMIC_BASE.

Hum ....

Quoted 8 lines
Quoted 4 lines

Commit 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS") was supposed to enforce dynamic allocation above GPIO_DYNAMIC_BASE already.

Can you describe what is going wrong exactly with the above commit ?

Above commit only works to the first dynamic allocation, if you need more than one with static ones present it mistakenly will give you a base _below_ DYNAMIC_BASE.

Ah right, that needs to be fixed.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

However, this change is just PoC I proposed, the conditional and action should be slightly different to cover a corner case, when statically allocated chip overlaps the DYNAMIC_BASE, i.e. gdev->base < DYNAMIC_BASE, while gdev->base + gdev->ngpio >= DYNAMIC_BASE.

Yes you are right, that's gdev->base + gdev->ngpio that should be checked.

and that not with simple continue or base might simply stay at DYNAMIC_BASE.

I will send a v2 of this patch with refined logic.

Regards,
Andreas

#5 lore raw
Quoted 38 lines · Andreas Kemnade
Quoted 34 lines · On Thu, 27 Apr 2023 06:20:34 +0000 Christophe Leroy…
Quoted 15 lines · Andy Shevchenko
Quoted 13 lines · Christophe Leroy
Quoted 6 lines · Andreas Kemnade

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

If static allocation and dynamic allocation GPIOs are present, dynamic allocation pollutes the numberspace for static allocation, causing static allocation to fail.
Enfore dynamic allocation above GPIO_DYNAMIC_BASE.

Hum ....

Quoted 8 lines
Quoted 4 lines

Commit 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS") was supposed to enforce dynamic allocation above GPIO_DYNAMIC_BASE already.

Can you describe what is going wrong exactly with the above commit ?

Above commit only works to the first dynamic allocation, if you need more than one with static ones present it mistakenly will give you a base _below_ DYNAMIC_BASE.

Ah right, that needs to be fixed.

Quoted 6 lines

However, this change is just PoC I proposed, the conditional and action should be slightly different to cover a corner case, when statically allocated chip overlaps the DYNAMIC_BASE, i.e. gdev->base < DYNAMIC_BASE, while gdev->base + gdev->ngpio >= DYNAMIC_BASE.

Quoted 5 lines
Quoted 2 lines

Yes you are right, that's gdev->base + gdev->ngpio that should be checked.

and that not with simple continue or base might simply stay at DYNAMIC_BASE.

I will send a v2 of this patch with refined logic.

Actually it would be nice to integrate a warning (if we don't have it yet) when adding a GPIO chip with a static allocation and which will overlap the dynamic base. Can you add that into your v2?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Le 27/04/2023 à 12:46, Andy Shevchenko a écrit :

Quoted 41 lines · Andreas Kemnade
Quoted 40 lines
Quoted 36 lines · On Thu, 27 Apr 2023 06:20:34 +0000 Christophe Leroy…
Quoted 15 lines · Andy Shevchenko
Quoted 13 lines · Christophe Leroy
Quoted 6 lines · Andreas Kemnade

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

If static allocation and dynamic allocation GPIOs are present, dynamic allocation pollutes the numberspace for static allocation, causing static allocation to fail.
Enfore dynamic allocation above GPIO_DYNAMIC_BASE.

Hum ....

Quoted 8 lines
Quoted 4 lines

Commit 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS") was supposed to enforce dynamic allocation above GPIO_DYNAMIC_BASE already.

Can you describe what is going wrong exactly with the above commit ?

Above commit only works to the first dynamic allocation, if you need more than one with static ones present it mistakenly will give you a base _below_ DYNAMIC_BASE.

Ah right, that needs to be fixed.

Quoted 6 lines

However, this change is just PoC I proposed, the conditional and action should be slightly different to cover a corner case, when statically allocated chip overlaps the DYNAMIC_BASE, i.e. gdev->base < DYNAMIC_BASE, while gdev->base + gdev->ngpio >= DYNAMIC_BASE.

Yes you are right, that's gdev->base + gdev->ngpio that should be checked.

Quoted 8 lines
Quoted 3 lines

and that not with simple continue or base might simply stay at DYNAMIC_BASE.

I will send a v2 of this patch with refined logic.

Actually it would be nice to integrate a warning (if we don't have it yet) when adding a GPIO chip with a static allocation and which will overlap the dynamic base. Can you add that into your v2?

At the time being we have a warning for all static allocations, allthough their has been some discussion about reverting it, see commit 502df79b8605 ("gpiolib: Warn on drivers still using static gpiobase allocation")

Christophe

#7 lore raw
Quoted 4 lines · Christophe Leroy
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Andreas Kemnade

On Thu, 27 Apr 2023 06:20:34 +0000
Christophe Leroy <[email protected]> wrote:

...

Quoted 11 lines
Quoted 6 lines
Quoted 1 line

I will send a v2 of this patch with refined logic.

Actually it would be nice to integrate a warning (if we don't have it yet) when adding a GPIO chip with a static allocation and which will overlap the dynamic base. Can you add that into your v2?

At the time being we have a warning for all static allocations, allthough their has been some discussion about reverting it, see commit 502df79b8605 ("gpiolib: Warn on drivers still using static gpiobase allocation")

Ah, even better! Then no need to have a specific one, thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko