diff mbox series

[1/4] gpiolib: cdev: Flag invalid GPIOs as used

Message ID 20201204164739.781812-2-maz@kernel.org
State New
Headers show
Series [1/4] gpiolib: cdev: Flag invalid GPIOs as used | expand

Commit Message

Marc Zyngier Dec. 4, 2020, 4:47 p.m. UTC
When reporting the state of a GPIO to userspace, we never check
for the actual validity of the line, meaning we report invalid
lines as being usable. A subsequent request will fail though,
which is an inconsistent behaviour from a userspace perspective.

Instead, let's check for the validity of the line and report it
as used if it is invalid. This allows a tool such as gpioinfo
to report something sensible:

gpiochip3 - 4 lines:
	line   0:      unnamed       unused   input  active-high
	line   1:      unnamed       kernel   input  active-high [used]
	line   2:      unnamed       kernel   input  active-high [used]
	line   3:      unnamed       unused   input  active-high

In this example, lines 1 and 2 are invalid, and cannot be used by
userspace.

Signed-off-by: Marc Zyngier <maz@kernel.org>
---
 drivers/gpio/gpiolib-cdev.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Johan Hovold Dec. 7, 2020, 2:16 p.m. UTC | #1
On Fri, Dec 04, 2020 at 04:47:36PM +0000, Marc Zyngier wrote:
> When reporting the state of a GPIO to userspace, we never check

> for the actual validity of the line, meaning we report invalid

> lines as being usable. A subsequent request will fail though,

> which is an inconsistent behaviour from a userspace perspective.

> 

> Instead, let's check for the validity of the line and report it

> as used if it is invalid. This allows a tool such as gpioinfo

> to report something sensible:

> 

> gpiochip3 - 4 lines:

> 	line   0:      unnamed       unused   input  active-high

> 	line   1:      unnamed       kernel   input  active-high [used]

> 	line   2:      unnamed       kernel   input  active-high [used]

> 	line   3:      unnamed       unused   input  active-high

> 

> In this example, lines 1 and 2 are invalid, and cannot be used by

> userspace.

> 

> Signed-off-by: Marc Zyngier <maz@kernel.org>

> ---

>  drivers/gpio/gpiolib-cdev.c | 1 +

>  1 file changed, 1 insertion(+)

> 

> diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c

> index e9faeaf65d14..a0fcb4ccaa02 100644

> --- a/drivers/gpio/gpiolib-cdev.c

> +++ b/drivers/gpio/gpiolib-cdev.c

> @@ -1910,6 +1910,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,

>  	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||

>  	    test_bit(FLAG_EXPORT, &desc->flags) ||

>  	    test_bit(FLAG_SYSFS, &desc->flags) ||

> +	    !gpiochip_line_is_valid(gc, info->offset) ||

>  	    !ok_for_pinctrl)

>  		info->flags |= GPIO_V2_LINE_FLAG_USED;


So this is somewhat separate from the rest of the series in case it
applies also to gpio chips with reserved ranges (e.g.
"gpio-reserved-ranges" devicetree property). Are they currently reported
as available?

Looks like this will work well also for USB gpio controllers with static
muxing configured in EEPROM, especially as that is how we already report
pins reported as unavailable by pinctrl (i.e. ok_for_pinctrl).

Johan
Marc Zyngier Dec. 7, 2020, 2:59 p.m. UTC | #2
On 2020-12-07 14:16, Johan Hovold wrote:
> On Fri, Dec 04, 2020 at 04:47:36PM +0000, Marc Zyngier wrote:

>> When reporting the state of a GPIO to userspace, we never check

>> for the actual validity of the line, meaning we report invalid

>> lines as being usable. A subsequent request will fail though,

>> which is an inconsistent behaviour from a userspace perspective.

>> 

>> Instead, let's check for the validity of the line and report it

>> as used if it is invalid. This allows a tool such as gpioinfo

>> to report something sensible:

>> 

>> gpiochip3 - 4 lines:

>> 	line   0:      unnamed       unused   input  active-high

>> 	line   1:      unnamed       kernel   input  active-high [used]

>> 	line   2:      unnamed       kernel   input  active-high [used]

>> 	line   3:      unnamed       unused   input  active-high

>> 

>> In this example, lines 1 and 2 are invalid, and cannot be used by

>> userspace.

>> 

>> Signed-off-by: Marc Zyngier <maz@kernel.org>

>> ---

>>  drivers/gpio/gpiolib-cdev.c | 1 +

>>  1 file changed, 1 insertion(+)

>> 

>> diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c

>> index e9faeaf65d14..a0fcb4ccaa02 100644

>> --- a/drivers/gpio/gpiolib-cdev.c

>> +++ b/drivers/gpio/gpiolib-cdev.c

>> @@ -1910,6 +1910,7 @@ static void gpio_desc_to_lineinfo(struct 

>> gpio_desc *desc,

>>  	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||

>>  	    test_bit(FLAG_EXPORT, &desc->flags) ||

>>  	    test_bit(FLAG_SYSFS, &desc->flags) ||

>> +	    !gpiochip_line_is_valid(gc, info->offset) ||

>>  	    !ok_for_pinctrl)

>>  		info->flags |= GPIO_V2_LINE_FLAG_USED;

> 

> So this is somewhat separate from the rest of the series in case it

> applies also to gpio chips with reserved ranges (e.g.

> "gpio-reserved-ranges" devicetree property). Are they currently 

> reported

> as available?


I don't have any HW that uses this, but gpiolib-of.c makes use of it to
expose the valid GPIO range. I expect these systems suffer from the same
issue.

> Looks like this will work well also for USB gpio controllers with 

> static

> muxing configured in EEPROM, especially as that is how we already 

> report

> pins reported as unavailable by pinctrl (i.e. ok_for_pinctrl).


Thanks,

         M.
-- 
Jazz is not dead. It just smells funny...
Linus Walleij Dec. 9, 2020, 9:25 a.m. UTC | #3
On Fri, Dec 4, 2020 at 5:47 PM Marc Zyngier <maz@kernel.org> wrote:

> When reporting the state of a GPIO to userspace, we never check
> for the actual validity of the line, meaning we report invalid
> lines as being usable. A subsequent request will fail though,
> which is an inconsistent behaviour from a userspace perspective.
>
> Instead, let's check for the validity of the line and report it
> as used if it is invalid. This allows a tool such as gpioinfo
> to report something sensible:
>
> gpiochip3 - 4 lines:
>         line   0:      unnamed       unused   input  active-high
>         line   1:      unnamed       kernel   input  active-high [used]
>         line   2:      unnamed       kernel   input  active-high [used]
>         line   3:      unnamed       unused   input  active-high
>
> In this example, lines 1 and 2 are invalid, and cannot be used by
> userspace.
>
> Signed-off-by: Marc Zyngier <maz@kernel.org>

This makes sense, so patch applied (unless Bartosz tells me
otherwise, then I'll pull it out again).

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index e9faeaf65d14..a0fcb4ccaa02 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -1910,6 +1910,7 @@  static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
 	    test_bit(FLAG_EXPORT, &desc->flags) ||
 	    test_bit(FLAG_SYSFS, &desc->flags) ||
+	    !gpiochip_line_is_valid(gc, info->offset) ||
 	    !ok_for_pinctrl)
 		info->flags |= GPIO_V2_LINE_FLAG_USED;