From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Linus Walleij <linus.walleij@linaro.org>,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [PATCH 4/8] gpiolib: sanitize the return value of gpio_chip::get()
Date: Mon, 24 Feb 2025 18:30:08 +0200 [thread overview]
Message-ID: <Z7yekJ8uRh8dphKn@black.fi.intel.com> (raw)
In-Reply-To: <20250210-gpio-sanitize-retvals-v1-4-12ea88506cb2@linaro.org>
On Mon, Feb 10, 2025 at 11:51:58AM +0100, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> As per the API contract, the get() callback is only allowed to return 0,
> 1 or a negative error number. Add a wrapper around the callback calls
> that filters out anything else.
...
> +static int gpiochip_get(struct gpio_chip *gc, unsigned int offset)
> +{
> + int ret;
> +
> + lockdep_assert_held(&gc->gpiodev->srcu);
> +
> + if (!gc->get)
> + return -EIO;
> +
> + ret = gc->get(gc, offset);
> + if (ret > 1)
Perhaps use the respective GPIO macro instead? Otherwise it's not clear what
the meaning of 1 is.
> + ret = -EBADE;
> +
> + return ret;
> +}
> +
> static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
> {
> - return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
> + return gpiochip_get(gc, gpio_chip_hwgpio(desc));
> }
...
> for_each_set_bit(i, mask, gc->ngpio) {
> - value = gc->get(gc, i);
> + value = gpiochip_get(gc, i);
This will delay the function for checking every time if the get() exists. Which
must be here.
> if (value < 0)
> return value;
> __assign_bit(i, bits, value);
What I would expect here is something like this:
static int gpio_chip_get_value_nocheck(struct gpio_chip *gc, unsigned int offset)
{
int ret;
lockdep_assert_held(&gc->gpiodev->srcu);
ret = gc->get(gc, offset);
if (ret > GPIO_LINE_DIRECTION_IN)
ret = -EBADE;
return ret;
}
static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
{
return gc->get ? gpio_chip_get_value_nocheck(gc, gpio_chip_hwgpio(desc)) : -EIO;
}
But I see the downside of it as it might lurk without RCU lock if get is not
defined. So, up to you.
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2025-02-24 16:30 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-10 10:51 [PATCH 0/8] gpiolib: sanitize return values of callbacks Bartosz Golaszewski
2025-02-10 10:51 ` [PATCH 1/8] gpiolib: check the return value of gpio_chip::get_direction() Bartosz Golaszewski
2025-02-19 2:42 ` Mark Brown
2025-02-19 8:38 ` Marek Szyprowski
2025-02-19 8:50 ` Bartosz Golaszewski
2025-02-19 9:13 ` Marek Szyprowski
2025-02-19 9:22 ` Bartosz Golaszewski
2025-02-25 13:19 ` Antonio Borneo
2025-02-10 10:51 ` [PATCH 2/8] gpiolib: sanitize the return value of gpio_chip::request() Bartosz Golaszewski
2025-02-10 10:51 ` [PATCH 3/8] gpiolib: sanitize the return value of gpio_chip::set_config() Bartosz Golaszewski
2025-02-10 10:51 ` [PATCH 4/8] gpiolib: sanitize the return value of gpio_chip::get() Bartosz Golaszewski
2025-02-24 16:30 ` Andy Shevchenko [this message]
2025-02-25 10:35 ` Bartosz Golaszewski
2025-02-10 10:51 ` [PATCH 5/8] gpiolib: sanitize the return value of gpio_chip::get_multiple() Bartosz Golaszewski
2025-02-10 10:52 ` [PATCH 6/8] gpiolib: sanitize the return value of gpio_chip::direction_output() Bartosz Golaszewski
2025-02-10 10:52 ` [PATCH 7/8] gpiolib: sanitize the return value of gpio_chip::direction_input() Bartosz Golaszewski
2025-02-10 10:52 ` [PATCH 8/8] gpiolib: sanitize the return value of gpio_chip::get_direction() Bartosz Golaszewski
2025-02-24 16:33 ` Andy Shevchenko
2025-02-24 19:55 ` Bartosz Golaszewski
2025-02-24 20:25 ` Andy Shevchenko
2025-02-25 10:13 ` Marek Szyprowski
2025-02-25 10:17 ` Bartosz Golaszewski
2025-02-14 9:21 ` [PATCH 0/8] gpiolib: sanitize return values of callbacks Linus Walleij
2025-02-17 10:51 ` (subset) " Bartosz Golaszewski
2025-02-24 9:05 ` Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z7yekJ8uRh8dphKn@black.fi.intel.com \
--to=andriy.shevchenko@intel.com \
--cc=bartosz.golaszewski@linaro.org \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox