diff mbox

[14/54] gpio: pch: Be sure to clamp return value

Message ID 1450794054-22948-1-git-send-email-linus.walleij@linaro.org
State New
Headers show

Commit Message

Linus Walleij Dec. 22, 2015, 2:20 p.m. UTC
As we want gpio_chip .get() calls to be able to return negative
error codes and propagate to drivers, we need to go over all
drivers and make sure their return values are clamped to [0,1].
We do this by using the ret = !!(val) design pattern.

Cc: Thierry Reding <treding@nvidia.com>
Cc: Daniel Krueger <daniel.krueger@systec-electronic.com>
Cc: Jean Delvare <jdelvare@suse.de>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/gpio/gpio-pch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Jean Delvare Jan. 4, 2016, 9:41 a.m. UTC | #1
Hi Linus,

On Tue, 22 Dec 2015 15:20:54 +0100, Linus Walleij wrote:
> As we want gpio_chip .get() calls to be able to return negative
> error codes and propagate to drivers, we need to go over all
> drivers and make sure their return values are clamped to [0,1].
> We do this by using the ret = !!(val) design pattern.
> 
> Cc: Thierry Reding <treding@nvidia.com>
> Cc: Daniel Krueger <daniel.krueger@systec-electronic.com>
> Cc: Jean Delvare <jdelvare@suse.de>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  drivers/gpio/gpio-pch.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpio/gpio-pch.c b/drivers/gpio/gpio-pch.c
> index af0715f8524b..8c45b74dcf21 100644
> --- a/drivers/gpio/gpio-pch.c
> +++ b/drivers/gpio/gpio-pch.c
> @@ -127,7 +127,7 @@ static int pch_gpio_get(struct gpio_chip *gpio, unsigned nr)
>  {
>  	struct pch_gpio *chip =	container_of(gpio, struct pch_gpio, gpio);
>  
> -	return ioread32(&chip->reg->pi) & (1 << nr);
> +	return !!(ioread32(&chip->reg->pi) & (1 << nr));
>  }
>  
>  static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,

I would prefer:

	return (ioread32(&chip->reg->pi) >> nr) & 1;

which is faster for the same result.

At x86 assembly level, your approach requires 5 CPU instructions (mov,
shl, test, setne and movzbl), mine only 2 CPU instructions (shr and
and.)
Linus Walleij Jan. 5, 2016, 9:46 a.m. UTC | #2
On Mon, Jan 4, 2016 at 10:41 AM, Jean Delvare <jdelvare@suse.de> wrote:
> On Tue, 22 Dec 2015 15:20:54 +0100, Linus Walleij wrote:
>> As we want gpio_chip .get() calls to be able to return negative
>> error codes and propagate to drivers, we need to go over all
>> drivers and make sure their return values are clamped to [0,1].
>> We do this by using the ret = !!(val) design pattern.
>>
>> Cc: Thierry Reding <treding@nvidia.com>
>> Cc: Daniel Krueger <daniel.krueger@systec-electronic.com>
>> Cc: Jean Delvare <jdelvare@suse.de>
>> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
>> ---
>>  drivers/gpio/gpio-pch.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/gpio/gpio-pch.c b/drivers/gpio/gpio-pch.c
>> index af0715f8524b..8c45b74dcf21 100644
>> --- a/drivers/gpio/gpio-pch.c
>> +++ b/drivers/gpio/gpio-pch.c
>> @@ -127,7 +127,7 @@ static int pch_gpio_get(struct gpio_chip *gpio, unsigned nr)
>>  {
>>       struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
>>
>> -     return ioread32(&chip->reg->pi) & (1 << nr);
>> +     return !!(ioread32(&chip->reg->pi) & (1 << nr));
>>  }
>>
>>  static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
>
> I would prefer:
>
>         return (ioread32(&chip->reg->pi) >> nr) & 1;
>
> which is faster for the same result.
>
> At x86 assembly level, your approach requires 5 CPU instructions (mov,
> shl, test, setne and movzbl), mine only 2 CPU instructions (shr and
> and.)

I was mainly going over and fixing all drivers with the simplest
pattern I can think of, already merged this as it was a regression
basically, but a patch based on my GPIO devel branch or Linux-next
to fix it the way you want it would be
appreciated.

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jean Delvare Jan. 5, 2016, 1:21 p.m. UTC | #3
Hi Linus,

On Tue, 5 Jan 2016 10:46:06 +0100, Linus Walleij wrote:
> On Mon, Jan 4, 2016 at 10:41 AM, Jean Delvare <jdelvare@suse.de> wrote:
> > On Tue, 22 Dec 2015 15:20:54 +0100, Linus Walleij wrote:
> >> As we want gpio_chip .get() calls to be able to return negative
> >> error codes and propagate to drivers, we need to go over all
> >> drivers and make sure their return values are clamped to [0,1].
> >> We do this by using the ret = !!(val) design pattern.
> >>
> >> Cc: Thierry Reding <treding@nvidia.com>
> >> Cc: Daniel Krueger <daniel.krueger@systec-electronic.com>
> >> Cc: Jean Delvare <jdelvare@suse.de>
> >> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> >> ---
> >>  drivers/gpio/gpio-pch.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/gpio/gpio-pch.c b/drivers/gpio/gpio-pch.c
> >> index af0715f8524b..8c45b74dcf21 100644
> >> --- a/drivers/gpio/gpio-pch.c
> >> +++ b/drivers/gpio/gpio-pch.c
> >> @@ -127,7 +127,7 @@ static int pch_gpio_get(struct gpio_chip *gpio, unsigned nr)
> >>  {
> >>       struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
> >>
> >> -     return ioread32(&chip->reg->pi) & (1 << nr);
> >> +     return !!(ioread32(&chip->reg->pi) & (1 << nr));
> >>  }
> >>
> >>  static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
> >
> > I would prefer:
> >
> >         return (ioread32(&chip->reg->pi) >> nr) & 1;
> >
> > which is faster for the same result.
> >
> > At x86 assembly level, your approach requires 5 CPU instructions (mov,
> > shl, test, setne and movzbl), mine only 2 CPU instructions (shr and
> > and.)
> 
> I was mainly going over and fixing all drivers with the simplest
> pattern I can think of, already merged this as it was a regression
> basically, but a patch based on my GPIO devel branch or Linux-next
> to fix it the way you want it would be
> appreciated.

Fair enough, will do.
diff mbox

Patch

diff --git a/drivers/gpio/gpio-pch.c b/drivers/gpio/gpio-pch.c
index af0715f8524b..8c45b74dcf21 100644
--- a/drivers/gpio/gpio-pch.c
+++ b/drivers/gpio/gpio-pch.c
@@ -127,7 +127,7 @@  static int pch_gpio_get(struct gpio_chip *gpio, unsigned nr)
 {
 	struct pch_gpio *chip =	container_of(gpio, struct pch_gpio, gpio);
 
-	return ioread32(&chip->reg->pi) & (1 << nr);
+	return !!(ioread32(&chip->reg->pi) & (1 << nr));
 }
 
 static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,