All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpio: check first for invalid gpio
@ 2016-01-01  8:30 Sudip Mukherjee
  2016-01-27  0:24 ` Alexandre Courbot
  0 siblings, 1 reply; 2+ messages in thread
From: Sudip Mukherjee @ 2016-01-01  8:30 UTC (permalink / raw)
  To: Linus Walleij, Alexandre Courbot
  Cc: linux-kernel, linux-gpio, Sudip Mukherjee

Even if an invalid gpio number is sent to gpio_to_desc() we try to find
it in the list before checking if it is invalid. We can check it first
so that it will save us from traversing the list incase of invalid gpio.

Signed-off-by: Sudip Mukherjee <sudip@vectorindia.org>
---
 drivers/gpio/gpiolib.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index ca66302..17a1cd5 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -70,6 +70,11 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
 	struct gpio_chip *chip;
 	unsigned long flags;
 
+	if (!gpio_is_valid(gpio)) {
+		WARN(1, "invalid GPIO %d\n", gpio);
+		return NULL;
+	}
+
 	spin_lock_irqsave(&gpio_lock, flags);
 
 	list_for_each_entry(chip, &gpio_chips, list) {
@@ -81,9 +86,6 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
 
 	spin_unlock_irqrestore(&gpio_lock, flags);
 
-	if (!gpio_is_valid(gpio))
-		WARN(1, "invalid GPIO %d\n", gpio);
-
 	return NULL;
 }
 EXPORT_SYMBOL_GPL(gpio_to_desc);
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] gpio: check first for invalid gpio
  2016-01-01  8:30 [PATCH] gpio: check first for invalid gpio Sudip Mukherjee
@ 2016-01-27  0:24 ` Alexandre Courbot
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Courbot @ 2016-01-27  0:24 UTC (permalink / raw)
  To: Sudip Mukherjee; +Cc: Linus Walleij, Linux Kernel Mailing List, linux-gpio

On Fri, Jan 1, 2016 at 5:30 PM, Sudip Mukherjee
<sudipm.mukherjee@gmail.com> wrote:
> Even if an invalid gpio number is sent to gpio_to_desc() we try to find
> it in the list before checking if it is invalid. We can check it first
> so that it will save us from traversing the list incase of invalid gpio.

The motivation for having that test at the end is that being provided
an invalid GPIO number should be the exceptional case (i.e. it does
not happen unless there is an error in the client code). Thus for
valid GPIOs (the vast majority) we find an entry in the list and save
that test.

>
> Signed-off-by: Sudip Mukherjee <sudip@vectorindia.org>
> ---
>  drivers/gpio/gpiolib.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index ca66302..17a1cd5 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -70,6 +70,11 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
>         struct gpio_chip *chip;
>         unsigned long flags;
>
> +       if (!gpio_is_valid(gpio)) {
> +               WARN(1, "invalid GPIO %d\n", gpio);
> +               return NULL;
> +       }
> +
>         spin_lock_irqsave(&gpio_lock, flags);
>
>         list_for_each_entry(chip, &gpio_chips, list) {
> @@ -81,9 +86,6 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
>
>         spin_unlock_irqrestore(&gpio_lock, flags);
>
> -       if (!gpio_is_valid(gpio))
> -               WARN(1, "invalid GPIO %d\n", gpio);
> -
>         return NULL;
>  }
>  EXPORT_SYMBOL_GPL(gpio_to_desc);
> --
> 1.9.1
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-01-27  0:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-01  8:30 [PATCH] gpio: check first for invalid gpio Sudip Mukherjee
2016-01-27  0:24 ` Alexandre Courbot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.