gpiolib: Make gpiod_put() error pointer aware
PATCH 1/1 gpiolib: Make gpiod_put() error pointer aware
When non-optional GPIO is requested and failed, the variable that holds the (invalid) descriptor can contain an error pointer. However, gpiod_put() ignores that fact and tries to cleanup never requested descriptor. Make sure gpiod_put() ignores that as well.
While at it, do the same for the gpiod_put_array().
Note, it arguable needs to be present in the stubs as those are usually called when CONFIG_GPIOLIB=n and GPIOs are requested using gpiod_get_optional() or similar APIs.
--- drivers/gpio/gpiolib.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +7 -2
@@ -5111,8 +5111,10 @@ EXPORT_SYMBOL_GPL(gpiod_get_array_optional);*/ void gpiod_put (struct gpio_desc *desc) { -if (desc) -gpiod_free (desc); +if (IS_ERR_OR_NULL (desc)) +return ; + +gpiod_free (desc); }EXPORT_SYMBOL_GPL (gpiod_put);
@@ -5124,6 +5126,9 @@ void gpiod_put_array(struct gpio_descs *descs) {unsigned int i; +if (IS_ERR_OR_NULL (descs)) +return ; +for (i =0 ; i < descs->ndescs; i++)gpiod_put (descs->desc[i]);
Signature · 1 line
2.47.2
Quoted 6 lines · Andy Shevchenko
When non-optional GPIO is requested and failed, the variable that holds the (invalid) descriptor can contain an error pointer. However,
gpiod_put()ignores that fact and tries to cleanup never requested descriptor. Make suregpiod_put()ignores that as well.
Quoted 8 lines
While at it, do the same for the
gpiod_put_array().Note, it arguable needs to be present in the stubs as those are usually called when
CONFIG_GPIOLIB=nand GPIOs are requested usinggpiod_get_optional()or similar APIs.Signed-off-by Andy Shevchenko <[email protected]>---
I'm not a fan of this. Silently ignoring NULL makes sense in the context of _optional() calls where we want to do nothing on GPIOs that aren't there. But this encourages people to get sloppy and just ignore error pointers returned from gpiod_get()? Also: all other calls error out on IS_ERR(desc) so why would we make it an exception? If anything, the broadcom SPI driver this is about should store the return value of gpiod_get() in a local variable, check it and then assign NULL to the actual descriptor stored in the driver data.
We return errors for a reason, I don't like the idea of just ignoring them in gpiod_put().
Bartosz
Quoted 14 lines · Bartosz Golaszewski
Quoted 11 lines · Andy Shevchenko
When non-optional GPIO is requested and failed, the variable that holds the (invalid) descriptor can contain an error pointer. However,
gpiod_put()ignores that fact and tries to cleanup never requested descriptor. Make suregpiod_put()ignores that as well.While at it, do the same for the
gpiod_put_array().Note, it arguable needs to be present in the stubs as those are usually called when
CONFIG_GPIOLIB=nand GPIOs are requested usinggpiod_get_optional()or similar APIs.
Quoted 6 lines
I'm not a fan of this. Silently ignoring NULL makes sense in the context of
_optional()calls where we want to do nothing on GPIOs that aren't there.But this encourages people to get sloppy and just ignore error pointers returned from
gpiod_get()?
From where did you come to this conclusion, please? We have many subsystems that ignore invalid resource on the release stage, starting from platform device driver core.
Quoted 2 lines
Also: all other calls error out on
IS_ERR(desc)so why would we make it an exception?
Because it's _release_ stage that participates in the cleaning up of the allocated resources in error paths. It's a common approach in the kernel. I would rather ask what makes GPIOLIB so special about it?
Quoted 3 lines
If anything, the broadcom SPI driver this is about should store the return value of
gpiod_get()in a local variable, check it and then assign NULL to the actual descriptor stored in the driver data.
Broadcom SPI driver just reveals this disadvantage in GPIOLIB.
Quoted 2 lines
We return errors for a reason, I don't like the idea of just ignoring them in
gpiod_put().
Yes, how does one links to the other, please?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 20 lines · Andy Shevchenko
Quoted 17 lines · Bartosz Golaszewski
Quoted 11 lines · Andy Shevchenko
When non-optional GPIO is requested and failed, the variable that holds the (invalid) descriptor can contain an error pointer. However,
gpiod_put()ignores that fact and tries to cleanup never requested descriptor. Make suregpiod_put()ignores that as well.While at it, do the same for the
gpiod_put_array().Note, it arguable needs to be present in the stubs as those are usually called when
CONFIG_GPIOLIB=nand GPIOs are requested usinggpiod_get_optional()or similar APIs.I'm not a fan of this. Silently ignoring NULL makes sense in the context of
_optional()calls where we want to do nothing on GPIOs that aren't there.
Quoted 7 lines
Quoted 2 lines
But this encourages people to get sloppy and just ignore error pointers returned from
gpiod_get()?From where did you come to this conclusion, please? We have many subsystems that ignore invalid resource on the release stage, starting from platform device driver core.
The fact that many people do something does not mean it's correct. Many other subsystem scream loudly when that happens, so I would be ok with adding a big WARN_ON(IS_ERR(desc)).
Quoted 7 lines
Quoted 2 lines
Also: all other calls error out on
IS_ERR(desc)so why would we make it an exception?Because it's _release_ stage that participates in the cleaning up of the allocated resources in error paths. It's a common approach in the kernel. I would rather ask what makes GPIOLIB so special about it?
Just because it's the release stage, does not mean you shouldn't care about the correctness of the consumer code. Passing an IS_ERR(descr) to any of the GPIO APIs can happen if the user ignores an error returned by gpiod_get(). That's not alright.
Bart
Quoted 21 lines · Bartosz Golaszewski
Quoted 19 lines · Andy Shevchenko
Quoted 17 lines · Bartosz Golaszewski
Quoted 10 lines · Andy Shevchenko
When non-optional GPIO is requested and failed, the variable that holds the (invalid) descriptor can contain an error pointer. However,
gpiod_put()ignores that fact and tries to cleanup never requested descriptor. Make suregpiod_put()ignores that as well.While at it, do the same for the
gpiod_put_array().Note, it arguable needs to be present in the stubs as those are usually called when
CONFIG_GPIOLIB=nand GPIOs are requested usinggpiod_get_optional()or similar APIs.I'm not a fan of this. Silently ignoring NULL makes sense in the context of
_optional()calls where we want to do nothing on GPIOs that aren't there.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
But this encourages people to get sloppy and just ignore error pointers returned from
gpiod_get()?From where did you come to this conclusion, please? We have many subsystems that ignore invalid resource on the release stage, starting from platform device driver core.
The fact that many people do something does not mean it's correct.
And it doesn't tell it is incorrect either. We are going to conclude that there are pros and cons on each of the approaches, but I don't see much a point in yours, sorry.
Quoted 2 lines
Many other subsystem scream loudly when that happens, so I would be ok with adding a big WARN_ON(IS_ERR(desc)).
I disagree. This is not that case where passing an error pointer should be an issue.
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
Also: all other calls error out on
IS_ERR(desc)so why would we make it an exception?Because it's _release_ stage that participates in the cleaning up of the allocated resources in error paths. It's a common approach in the kernel. I would rather ask what makes GPIOLIB so special about it?
Just because it's the release stage, does not mean you shouldn't care about the correctness of the consumer code. Passing an
IS_ERR(descr)to any of the GPIO APIs can happen if the user ignores an error returned bygpiod_get(). That's not alright.
Have you ever seen such a code in the cases when it's okay (like in platform device driver users)? I do not. So, the above is based on the hypothetical assumption that somebody will make silly things. If you _really_ care about checking the error, add __must_check to the respective functions.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
But this encourages people to get sloppy and just ignore error pointers returned from
gpiod_get()?From where did you come to this conclusion, please? We have many subsystems that ignore invalid resource on the release stage, starting from platform device driver core.
Quoted 6 lines
Quoted 1 line
The fact that many people do something does not mean it's correct.
And it doesn't tell it is incorrect either. We are going to conclude that there are pros and cons on each of the approaches, but I don't see much a point in yours, sorry.
My point is:
You get a descriptor with gpiod_get_optional(). You can get three types of a pointer back:
1. Valid descriptor: you can use it in all GPIO consumer functions.
2. NULL-pointer: you can still use it in all GPIO consumer functions. They will act as if they succeeded. This is expected as this is how the "optional" functionality is implemented. Had it been written in rust, we'd do it better but we use the tools we have. It's very much a "valid" value to pass to gpiod routines.
3. IS_ERR() value. If you try to pass it to any of the GPIO consumer functions, they will return it back to you and print a warning. Why? Because it's an invalid object. And there's no reason to make gpiod_put() an exemption. You never could have used an IS_ERR() correctly. Look at what devres does - if it got an IS_ERR(), it never schedules a release action.
Quoted 18 lines
Quoted 2 lines
Many other subsystem scream loudly when that happens, so I would be ok with adding a big WARN_ON(IS_ERR(desc)).
I disagree. This is not that case where passing an error pointer should be an issue.
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
Also: all other calls error out on
IS_ERR(desc)so why would we make it an exception?Because it's _release_ stage that participates in the cleaning up of the allocated resources in error paths. It's a common approach in the kernel. I would rather ask what makes GPIOLIB so special about it?
Just because it's the release stage, does not mean you shouldn't care about the correctness of the consumer code. Passing an
IS_ERR(descr)to any of the GPIO APIs can happen if the user ignores an error returned bygpiod_get(). That's not alright.
Quoted 5 lines
Have you ever seen such a code in the cases when it's okay (like in platform device driver users)? I do not. So, the above is based on the hypothetical assumption that somebody will make silly things. If you _really_ care about checking the error, add
__must_checkto the respective functions.
They already have but people do the following (like in the affected SPI driver):
struct driver_data *data = kzalloc();
Then elsewhere:
data->gpiod = gpiod_get();
if (IS_ERR(data->gpiod))
return PTR_ERR(data->gpiod);
The data struct now contains the IS_ERR() value. I don't think it makes any sense for it to carry it around and I don't want to enable it.
Bart
Quoted 10 lines · Bartosz Golaszewski
Quoted 8 lines · Andy Shevchenko
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
But this encourages people to get sloppy and just ignore error pointers returned from
gpiod_get()?From where did you come to this conclusion, please? We have many subsystems that ignore invalid resource on the release stage, starting from platform device driver core.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
The fact that many people do something does not mean it's correct.
And it doesn't tell it is incorrect either. We are going to conclude that there are pros and cons on each of the approaches, but I don't see much a point in yours, sorry.
My point is:
I understand your point, but I disagree with the omni appliance of 3.
Quoted 13 lines
You get a descriptor with
gpiod_get_optional(). You can get three types of a pointer back:1. Valid descriptor: you can use it in all GPIO consumer functions.
2. NULL-pointer: you can still use it in all GPIO consumer functions. They will act as if they succeeded. This is expected as this is how the "optional" functionality is implemented. Had it been written in rust, we'd do it better but we use the tools we have. It's very much a "valid" value to pass to gpiod routines.
3.IS_ERR()value. If you try to pass it to any of the GPIO consumer functions, they will return it back to you and print a warning. Why? Because it's an invalid object. And there's no reason to makegpiod_put()an exemption.
No, the release is special as it's used in error paths and there often it is simpler for all just to ignore invalid pointers rather then put a burden on the driver developers. The gpiod_set_*() and gpiod_get_*() over _assumed requested_ descriptor of course should fail any attempt to be supplied with an invalid pointer.
Quoted 3 lines
You never could have used an
IS_ERR()
correctly. Look at what devres does - if it got anIS_ERR(), it never schedules a release action.
This is again unrelated. devres is an upper layer and we don't care about its implementation which is logically correct since we assume to put on the list only _valid_ resources.
Quoted 29 lines
Quoted 18 lines
Quoted 2 lines
Many other subsystem scream loudly when that happens, so I would be ok with adding a big WARN_ON(IS_ERR(desc)).
I disagree. This is not that case where passing an error pointer should be an issue.
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
Also: all other calls error out on
IS_ERR(desc)so why would we make it an exception?Because it's _release_ stage that participates in the cleaning up of the allocated resources in error paths. It's a common approach in the kernel. I would rather ask what makes GPIOLIB so special about it?
Just because it's the release stage, does not mean you shouldn't care about the correctness of the consumer code. Passing an
IS_ERR(descr)to any of the GPIO APIs can happen if the user ignores an error returned bygpiod_get(). That's not alright.Quoted 4 lines
Have you ever seen such a code in the cases when it's okay (like in platform device driver users)? I do not. So, the above is based on the hypothetical assumption that somebody will make silly things. If you _really_ care about checking the error, add
__must_checkto the respective functions.They already have but people do the following (like in the affected SPI driver):
struct
driver_data*data =kzalloc();Then elsewhere:
Quoted 7 lines
data->gpiod=gpiod_get();
if (IS_ERR(data->gpiod))return PTR_ERR(data->gpiod);The data struct now contains the
IS_ERR()value. I don't think it makes any sense for it to carry it around and I don't want to enable it.
It's up to the driver developer.
Again, if you want the result of gpiod_get() to be tested, mark it so. That's why I think your initial point is mistargetting.
Ask yourself, why we don't fail kfree() on a NULL object? (from kfree() p.o.v. it's an invalid one) Or if you want to be more precise in analogue with the gpiod_get_optional(), consider
p = kmalloc(0); kfree(p);
and this is valid code despite p being not NULL!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 84 lines · Andy Shevchenko
Quoted 10 lines · Bartosz Golaszewski
Quoted 8 lines · Andy Shevchenko
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
But this encourages people to get sloppy and just ignore error pointers returned from
gpiod_get()?From where did you come to this conclusion, please? We have many subsystems that ignore invalid resource on the release stage, starting from platform device driver core.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
The fact that many people do something does not mean it's correct.
And it doesn't tell it is incorrect either. We are going to conclude that there are pros and cons on each of the approaches, but I don't see much a point in yours, sorry.
My point is:
I understand your point, but I disagree with the omni appliance of 3.
Quoted 13 lines
You get a descriptor with
gpiod_get_optional(). You can get three types of a pointer back:1. Valid descriptor: you can use it in all GPIO consumer functions.
2. NULL-pointer: you can still use it in all GPIO consumer functions. They will act as if they succeeded. This is expected as this is how the "optional" functionality is implemented. Had it been written in rust, we'd do it better but we use the tools we have. It's very much a "valid" value to pass to gpiod routines.
3.IS_ERR()value. If you try to pass it to any of the GPIO consumer functions, they will return it back to you and print a warning. Why? Because it's an invalid object. And there's no reason to makegpiod_put()an exemption.No, the release is special as it's used in error paths and there often it is simpler for all just to ignore invalid pointers rather then put a burden on the driver developers. The gpiod_set_*() and gpiod_get_*() over
_assumedrequested_descriptor of course should fail any attempt to be supplied with an invalid pointer.Quoted 3 lines
You never could have used an
IS_ERR()
correctly. Look at what devres does - if it got anIS_ERR(), it never schedules a release action.This is again unrelated. devres is an upper layer and we don't care about its implementation which is logically correct since we assume to put on the list only _valid_ resources.
Quoted 33 lines
Quoted 18 lines
Quoted 2 lines
Many other subsystem scream loudly when that happens, so I would be ok with adding a big WARN_ON(IS_ERR(desc)).
I disagree. This is not that case where passing an error pointer should be an issue.
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
Also: all other calls error out on
IS_ERR(desc)so why would we make it an exception?Because it's _release_ stage that participates in the cleaning up of the allocated resources in error paths. It's a common approach in the kernel. I would rather ask what makes GPIOLIB so special about it?
Just because it's the release stage, does not mean you shouldn't care about the correctness of the consumer code. Passing an
IS_ERR(descr)to any of the GPIO APIs can happen if the user ignores an error returned bygpiod_get(). That's not alright.Quoted 4 lines
Have you ever seen such a code in the cases when it's okay (like in platform device driver users)? I do not. So, the above is based on the hypothetical assumption that somebody will make silly things. If you _really_ care about checking the error, add
__must_checkto the respective functions.They already have but people do the following (like in the affected SPI driver):
struct
driver_data*data =kzalloc();Then elsewhere:
data->gpiod=gpiod_get();
if (IS_ERR(data->gpiod))return PTR_ERR(data->gpiod);
Quoted 9 lines
Quoted 3 lines
The data struct now contains the
IS_ERR()value. I don't think it makes any sense for it to carry it around and I don't want to enable it.It's up to the driver developer.
Again, if you want the result of
gpiod_get()to be tested, mark it so. That's why I think your initial point is mistargetting.
It's already annotated, I said it before.
Quoted 8 lines
Ask yourself, why we don't fail
kfree()on a NULL object? (fromkfree()p.o.v. it's an invalid one) Or if you want to be more precise in analogue with thegpiod_get_optional(), considerp = kmalloc(0); kfree(p);and this is valid code despite p being not NULL!
We're discussing an interface returning an object, not a simple buffer so it's a bad example. Try feeding an IS_ERR() to regmap_exit() or numerous other deinit functions and tell me if that works.
I explained why I believe this change is wrong and I will allow myself to not accept it unless Linus is very positively in favor.
Bartosz
Quoted 2 lines · Bartosz Golaszewski
On Thu, Apr 3, 2025 at 3:22 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 2 lines
I explained why I believe this change is wrong and I will allow myself to not accept it unless Linus is very positively in favor.
I am on the opposite site. With all burden on the users' shoulders...
And your _any deinit_ function example is wrong. This is not a deinit case, this is resource allocation / reservation / etc and freeing it. Freeing invalid resource is bad, freeing an optional resource, or resource which has never been allocated — is okay. It makes device driver developer's life easier. And many kernel APIs are written in that form. Since you again pointed out that gpiod_get() is annotated, there is very unlikely somebody deliberately will ignore those errors.
P.S.
I will continue insisting that this is an inconvenience (or bug) in gpiod_put().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
I explained why I believe this change is wrong and I will allow myself to not accept it unless Linus is very positively in favor.
I am neutral on this unfortunately so my opinion doesn't help... I'm simply fine either way :/
Yours,
Linus Walleij