[PATCH] gpiolib: sysfs: Fix improper error handling on failed export

Boerge Struempfel posted 1 patch 2 years, 1 month ago
drivers/gpio/gpiolib-sysfs.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
[PATCH] gpiolib: sysfs: Fix improper error handling on failed export
Posted by Boerge Struempfel 2 years, 1 month ago
If gpio_set_transitory fails, we should free the gpio again. Most
notably, the flag FLAG_REQUESTED has previously been set in
gpiod_request_commit, and should be reset on failure.

Signed-off-by: Boerge Struempfel <boerge.struempfel@gmail.com>
---
 drivers/gpio/gpiolib-sysfs.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index 6f309a3b2d9a..12d853845bb8 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -474,14 +474,17 @@ static ssize_t export_store(const struct class *class,
 		goto done;
 
 	status = gpiod_set_transitory(desc, false);
-	if (!status) {
-		status = gpiod_export(desc, true);
-		if (status < 0)
-			gpiod_free(desc);
-		else
-			set_bit(FLAG_SYSFS, &desc->flags);
+	if (status) {
+		gpiod_free(desc);
+		goto done;
 	}
 
+	status = gpiod_export(desc, true);
+	if (status < 0)
+		gpiod_free(desc);
+	else
+		set_bit(FLAG_SYSFS, &desc->flags);
+
 done:
 	if (status)
 		pr_debug("%s: status %d\n", __func__, status);
-- 
2.42.0
Re: [PATCH] gpiolib: sysfs: Fix improper error handling on failed export
Posted by Andy Shevchenko 2 years, 1 month ago
On Thu, Nov 23, 2023 at 3:30 PM Boerge Struempfel
<boerge.struempfel@gmail.com> wrote:
>
> If gpio_set_transitory fails, we should free the gpio again. Most

We refer to functions as func() in the text and comments (note parentheses).

GPIO

> notably, the flag FLAG_REQUESTED has previously been set in
> gpiod_request_commit, and should be reset on failure.

Same about func().

...

Seems the correct fix, but you may also add that no existing user is
returning anything except 0 or ENOTSUPP that is converted to 0 in
GPIOLIB core code. Hence no Fixes tag is needed, but still possible if
maintainers want it.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] gpiolib: sysfs: Fix improper error handling on failed export
Posted by Börge Strümpfel 2 years, 1 month ago
Hi Andy

thank you for your feedback

On Thu, Nov 23, 2023 at 3:25 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Thu, Nov 23, 2023 at 3:30 PM Boerge Struempfel
> <boerge.struempfel@gmail.com> wrote:
> >
> > If gpio_set_transitory fails, we should free the gpio again. Most
>
> We refer to functions as func() in the text and comments (note parentheses).
>
> GPIO

Thanks for letting me know, I will update the the commit message in
regards to this.

>
> > notably, the flag FLAG_REQUESTED has previously been set in
> > gpiod_request_commit, and should be reset on failure.
>
> Same about func().
>
> ...
>
> Seems the correct fix, but you may also add that no existing user is
> returning anything except 0 or ENOTSUPP that is converted to 0 in
> GPIOLIB core code. Hence no Fixes tag is needed, but still possible if
> maintainers want it.
>

You are right. For now, all mainline users are returning 0. We only found
this due to downstream-specific code. I'll add a comment about this not
affecting any existing users to the commit message.

> --
> With Best Regards,
> Andy Shevchenko
Re: [PATCH] gpiolib: sysfs: Fix improper error handling on failed export
Posted by Börge Strümpfel 2 years, 1 month ago
Hi Andy,

On Thu, Nov 23, 2023 at 4:01 PM Börge Strümpfel
<boerge.struempfel@gmail.com> wrote:
>
> Hi Andy
>
> thank you for your feedback
>
> On Thu, Nov 23, 2023 at 3:25 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> >
> > On Thu, Nov 23, 2023 at 3:30 PM Boerge Struempfel
> > <boerge.struempfel@gmail.com> wrote:
> > >
> > > If gpio_set_transitory fails, we should free the gpio again. Most
> >
> > We refer to functions as func() in the text and comments (note parentheses).
> >
> > GPIO
>
> Thanks for letting me know, I will update the the commit message in
> regards to this.
>
> >
> > > notably, the flag FLAG_REQUESTED has previously been set in
> > > gpiod_request_commit, and should be reset on failure.
> >
> > Same about func().
> >
> > ...
> >
> > Seems the correct fix, but you may also add that no existing user is
> > returning anything except 0 or ENOTSUPP that is converted to 0 in
> > GPIOLIB core code. Hence no Fixes tag is needed, but still possible if
> > maintainers want it.
> >
>
> You are right. For now, all mainline users are returning 0. We only found
> this due to downstream-specific code. I'll add a comment about this not
> affecting any existing users to the commit message.
>

A small update:
I looked through the possible users again, and there seems to be at least
the possibility for some other return values. The reason for this is, that
the .set_config() of the specific gpio driver is called during the
gpiod_set_transitory() call. For example the .set_config() of gpio-aspeed
might in certain (somewhat unlikely) cases return -EPROBE_DEFER as
well as -EINVAL.

However I don't think, that these conditional paths can be reached on a
properly configured system.

Kind Regards,
Börge Strümpfel