fix fwnode_irq_get_byname() returnvalue
Subsystems (5)
cover fix fwnode_irq_get_byname() returnvalue
The fix fwnode_irq_get_byname() may have returned zero if mapping the IRQ fails. This contradicts the documentation. Furthermore, returning zero or errno on error is unepected and can easily lead to problems like:
int probe(foo)
{
...
ret = fwnode_irq_get_byname(...); if (ret < 0) return ret;
...
}
or
int probe(foo)
{
...
ret = fwnode_irq_get_byname(...); if (ret <= 0) return ret;
...
}
which are both likely to be wrong. First treats zero as successful call and misses the IRQ mapping failure. Second returns zero from probe even though it detects the IRQ mapping failure correvtly.
Here we change the fwnode_irq_get_byname() to always return a negative errno upon failure. I have also audited following callers:
drivers/i2c/i2c-smbus.cdrivers/iio/accel/adxl355_core.cdrivers/iio/gyro/fxas21002c_core.cdrivers/iio/imu/adis16480.cdrivers/iio/imu/bmi160/bmi160_core.cdrivers/iio/imu/bmi160/bmi160_core.c
and it seems to me these calls will be Ok after the change. The i2c-smbus.c will gain a functional change (bugfix?) as after this patch the probe will return -EINVAL should the IRQ mapping fail. The series will also adjust the return value check for zero to be omitted.
---
Matti Vaittinen (2):
drivers: fwnode: fix fwnode_irq_get_byname()
i2c: i2c-smbus: fwnode_irq_get_byname() return value fix
drivers/base/property.c | 9 +++++++-- drivers/i2c/i2c-smbus.c | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-)
base-commit: 247f34f7b80357943234f93f247a1ae6b6c3a740
Signature · 14 lines
2.37.3 -- Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =]
PATCH 1/2 drivers: fwnode: fix fwnode_irq_get_byname()
The fwnode_irq_get_byname() does return 0 upon device-tree IRQ mapping failure. This is contradicting the function documentation and can potentially be a source of errors like:
int probe(...) {
...
irq = fwnode_irq_get_byname();
if (irq <= 0)
return irq;
...
}Here we do correctly check the return value from fwnode_irq_get_byname() but the driver probe will now return success. (There was already one such user in-tree).
Change the fwnode_irq_get_byname() to work as documented and according to the common convention and abd always return a negative errno upon failure.
---
I did a quick audit for the callers at v6.1-rc2:drivers/i2c/i2c-smbus.cdrivers/iio/accel/adxl355_core.cdrivers/iio/gyro/fxas21002c_core.cdrivers/iio/imu/adis16480.cdrivers/iio/imu/bmi160/bmi160_core.cdrivers/iio/imu/bmi160/bmi160_core.c
I did not spot any errors to be caused by this change. There will be a functional change in i2c-smbus.c as the probe will now return -EINVAL should the IRQ dt-mapping fail. It'd be nice if this was checked to be Ok by the peeps knowing the i2c-smbus :)
--- drivers/base/property.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/base/property.c +7 -2
@@ -964,7 +964,7 @@ EXPORT_SYMBOL(fwnode_irq_get);*/ int fwnode_irq_get_byname (const struct fwnode_handle *fwnode,const char *name) { -int index; +int index, ret;if (!name)return -EINVAL ;
@@ -973,7 +973,12 @@ int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)if (index <0 )return index; -return fwnode_irq_get (fwnode, index); + ret =fwnode_irq_get (fwnode, index); + +if (!ret) +return -EINVAL ; + +return ret; }EXPORT_SYMBOL (fwnode_irq_get_byname);
Signature · 14 lines
2.37.3 -- Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =]
PATCH 2/2 i2c: i2c-smbus: fwnode_irq_get_byname() return value fix
In reply to Matti VaittinenThe fwnode_irq_get_byname() was changed to not return 0 upon failure so return value check can be adjusted to reflect the change.
---
Depends on the mentioned return value change which is in patch 1/2. The return value change does also cause a functional change here. Eg. when IRQ mapping fails, the fwnode_irq_get_byname() no longer returns zero. This will cause also the probe here to return nonzero failure. I guess this is desired behaviour.
--- drivers/i2c/i2c-smbus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/i2c-smbus.c +1 -1
@@ -130,7 +130,7 @@ static int smbalert_probe(struct i2c_client *ara, }else { irq =fwnode_irq_get_byname (dev_fwnode (adapter->dev.parent),"smbus_alert" ); -if (irq <=0 ) +if (irq <0 )return irq; }
Signature · 14 lines
2.37.3 -- Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =]
Re: [PATCH 1/2] drivers: fwnode: fix fwnode_irq_get_byname()
In reply to Matti VaittinenMoi,
Quoted 35 lines · Matti Vaittinen
The
fwnode_irq_get_byname()does return 0 upon device-tree IRQ mapping failure. This is contradicting the function documentation and can potentially be a source of errors like:int probe(...) { ... irq = fwnode_irq_get_byname(); if (irq <= 0) return irq; ... }Here we do correctly check the return value from
fwnode_irq_get_byname()but the driver probe will now return success. (There was already one such user in-tree).Change the
fwnode_irq_get_byname()to work as documented and according to the common convention and abd always return a negative errno upon failure.Fixes ca0acb511c21 ("device property: Add fwnode_irq_get_byname")Suggested-by Sakari Ailus <[email protected]>Signed-off-by Matti Vaittinen <[email protected]>---
I did a quick audit for the callers at v6.1-rc2:
drivers/i2c/i2c-smbus.cdrivers/iio/accel/adxl355_core.cdrivers/iio/gyro/fxas21002c_core.cdrivers/iio/imu/adis16480.cdrivers/iio/imu/bmi160/bmi160_core.cdrivers/iio/imu/bmi160/bmi160_core.c
Quoted 1 line
I did not spot any errors to be caused by this change. There will be a
It won't as you're decreasing the possible values the function may return...
Quoted 3 lines
functional change in
i2c-smbus.cas the probe will now return-EINVALshould the IRQ dt-mapping fail. It'd be nice if this was checked to be Ok by the peeps knowing the i2c-smbus :)
FWIW, for both patches (but see below):
Quoted 18 lines
--- drivers/base/property.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/base/property.c +1
-1@@ -964,7 +964,7 @@ EXPORT_SYMBOL(fwnode_irq_get);*/ int fwnode_irq_get_byname (const struct fwnode_handle *fwnode,const char *name) { -int index; +int index, ret;if (!name)return -EINVAL ;
drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name) · line 977
Quoted 6 lines
drivers/base/property.c +2
-1@@ -973,7 +973,12 @@ int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)if (index <0 )return index; -return fwnode_irq_get (fwnode, index); + ret =fwnode_irq_get (fwnode, index); +
This newline is extra.
Or:
return ret ?: -EINVAL;
Or even:
return fwnode_irq_get(fwnode, index) ?: -EINVAL;
Up to you.
Quoted 6 lines
drivers/base/property.c +4
-0+if (!ret) +return -EINVAL ; + +return ret; }EXPORT_SYMBOL (fwnode_irq_get_byname);
Signature · 3 lines
Terveisin, Sakari Ailus
Re: [PATCH 1/2] drivers: fwnode: fix fwnode_irq_get_byname()
Moro Sakari,
Thanks for the review (and the suggestion)!
Quoted 38 lines · Sakari Ailus
Moi,
Quoted 35 lines · Matti Vaittinen
The
fwnode_irq_get_byname()does return 0 upon device-tree IRQ mapping failure. This is contradicting the function documentation and can potentially be a source of errors like:int probe(...) { ... irq = fwnode_irq_get_byname(); if (irq <= 0) return irq; ... }Here we do correctly check the return value from
fwnode_irq_get_byname()but the driver probe will now return success. (There was already one such user in-tree).Change the
fwnode_irq_get_byname()to work as documented and according to the common convention and abd always return a negative errno upon failure.Fixes ca0acb511c21 ("device property: Add fwnode_irq_get_byname")Suggested-by Sakari Ailus <[email protected]>Signed-off-by Matti Vaittinen <[email protected]>---
I did a quick audit for the callers at v6.1-rc2:
drivers/i2c/i2c-smbus.cdrivers/iio/accel/adxl355_core.cdrivers/iio/gyro/fxas21002c_core.cdrivers/iio/imu/adis16480.cdrivers/iio/imu/bmi160/bmi160_core.cdrivers/iio/imu/bmi160/bmi160_core.c
Quoted 5 lines
Quoted 1 line
I did not spot any errors to be caused by this change. There will be a
It won't as you're decreasing the possible values the function may return...
Unless someone had implemented special handling for the IRQ mapping failure...
Quoted 37 lines
Quoted 3 lines
functional change in
i2c-smbus.cas the probe will now return-EINVALshould the IRQ dt-mapping fail. It'd be nice if this was checked to be Ok by the peeps knowing the i2c-smbus :)FWIW, for both patches (but see below):
Reviewed-by Sakari Ailus <[email protected]>Quoted 18 lines
--- drivers/base/property.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/base/property.c +1
-1@@ -964,7 +964,7 @@ EXPORT_SYMBOL(fwnode_irq_get);*/ int fwnode_irq_get_byname (const struct fwnode_handle *fwnode,const char *name) { -int index; +int index, ret;if (!name)return -EINVAL ;drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name) · line 977
Quoted 6 lines
drivers/base/property.c +2
-1@@ -973,7 +973,12 @@ int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)if (index <0 )return index; -return fwnode_irq_get (fwnode, index); + ret =fwnode_irq_get (fwnode, index); +This newline is extra.
Or:
Quoted 8 lines
return ret ?: -EINVAL;Or even:
return fwnode_irq_get(fwnode, index) ?: -EINVAL;Up to you.
My personal preference is to not use the ternary. I think the plain clarity of if() just in many places justifies burning couple of lines more :)
Yours
-- Matti
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Andy Shevchenko
reviewerRe: [PATCH 1/2] drivers: fwnode: fix fwnode_irq_get_byname()
In reply to Matti VaittinenQuoted 20 lines · Matti Vaittinen
The
fwnode_irq_get_byname()does return 0 upon device-tree IRQ mapping failure. This is contradicting the function documentation and can potentially be a source of errors like:int probe(...) { ... irq = fwnode_irq_get_byname(); if (irq <= 0) return irq; ... }Here we do correctly check the return value from
fwnode_irq_get_byname()but the driver probe will now return success. (There was already one such user in-tree).Change the
fwnode_irq_get_byname()to work as documented and according to the common convention and abd always return a negative errno upon failure.
...
drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
Quoted 3 lines
drivers/base/property.c +2
-0+ ret =fwnode_irq_get (fwnode, index); +
Redundant blank line and better to use traditional pattern:
drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name) · line 981
Quoted 4 lines
drivers/base/property.c +4
-0+if (!ret) +return -EINVAL ; + +return ret;
if (ret) return ret; /* We treat mapping errors as invalid case */ return -EINVAL;
Quoted 1 line
}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 39 lines · Matti Vaittinen
The fix
fwnode_irq_get_byname()may have returned zero if mapping the IRQ fails. This contradicts the documentation. Furthermore, returning zero or errno on error is unepected and can easily lead to problems like:int probe(foo)
{
...ret = fwnode_irq_get_byname(...); if (ret < 0) return ret;...
}or
int probe(foo)
{
...ret = fwnode_irq_get_byname(...); if (ret <= 0) return ret;...
}which are both likely to be wrong. First treats zero as successful call and misses the IRQ mapping failure. Second returns zero from probe even though it detects the IRQ mapping failure correvtly.
Here we change the
fwnode_irq_get_byname()to always return a negative errno upon failure. I have also audited following callers:
drivers/i2c/i2c-smbus.cdrivers/iio/accel/adxl355_core.cdrivers/iio/gyro/fxas21002c_core.cdrivers/iio/imu/adis16480.cdrivers/iio/imu/bmi160/bmi160_core.cdrivers/iio/imu/bmi160/bmi160_core.c
Quoted 4 lines
and it seems to me these calls will be Ok after the change. The
i2c-smbus.cwill gain a functional change (bugfix?) as after this patch the probe will return-EINVALshould the IRQ mapping fail. The series will also adjust the return value check for zero to be omitted.
Thanks for doing this, no major comments except worrying about fwnode_irq_get() which is left untouched an hence different error domain for the same family of API.
For these patches:
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] drivers: fwnode: fix fwnode_irq_get_byname()
In reply to Andy ShevchenkoHi Andy,
Thanks for the review.
Quoted 29 lines · Andy Shevchenko
Quoted 20 lines · Matti Vaittinen
The
fwnode_irq_get_byname()does return 0 upon device-tree IRQ mapping failure. This is contradicting the function documentation and can potentially be a source of errors like:int probe(...) { ... irq = fwnode_irq_get_byname(); if (irq <= 0) return irq; ... }Here we do correctly check the return value from
fwnode_irq_get_byname()but the driver probe will now return success. (There was already one such user in-tree).Change the
fwnode_irq_get_byname()to work as documented and according to the common convention and abd always return a negative errno upon failure....
drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
Quoted 3 lines
drivers/base/property.c +2
-0+ ret =fwnode_irq_get (fwnode, index); +Redundant blank line and better to use traditional pattern: >
Quoted 12 lines
drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name) · line 981
Quoted 4 lines
drivers/base/property.c +4
-0+if (!ret) +return -EINVAL ; + +return ret;if (ret) return ret; /* We treat mapping errors as invalid case */ return -EINVAL;Quoted 1 line
}
I like the added comment - but in this case I don't prefer the "traditional pattern" you suggest. We do check for a very special error case indicated by ret 0.
if (!ret)
return -EINVAL;
makes it obvious the zero is special error.
if (ret)
return ret;
the traditional pattern makes this look like traditional error return - which it is not. The comment you added is nice though. It could be just before the check for
if (!ret).
I can cook v2 later when I have finished my current task - which may or may not take a while though....
Yours
-- Matti
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Andy Shevchenko
reviewerRe: [PATCH 1/2] drivers: fwnode: fix fwnode_irq_get_byname()
Quoted 2 lines · Matti
Quoted 1 line · Andy Shevchenko
On Tue, Oct 25, 2022 at 11:50:59AM +0300, Matti Vaittinen wrote:
...
Quoted 18 lines
Quoted 5 lines
drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
Quoted 3 lines
drivers/base/property.c +2
-0+ ret =fwnode_irq_get (fwnode, index); +Redundant blank line and better to use traditional pattern: >
Quoted 12 lines
drivers/base/property.c int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name) · line 981
Quoted 4 lines
drivers/base/property.c +4
-0+if (!ret) +return -EINVAL ; + +return ret;if (ret) return ret; /* We treat mapping errors as invalid case */ return -EINVAL;Quoted 1 line
}
Quoted 8 lines
I like the added comment - but in this case I don't prefer the "traditional pattern" you suggest. We do check for a very special error case indicated by ret 0.
if (!ret)
return -EINVAL;makes it obvious the zero is special error.
I don't think so. It makes ! easily to went through the cracks. If you want an explicit, use ' == 0' and add a comment.
Quoted 8 lines
if (ret)
return ret;the traditional pattern makes this look like traditional error return - which it is not. The comment you added is nice though. It could be just before the check for
if (!ret).
Signature · 2 lines
With Best Regards, Andy Shevchenko