linux-acpi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] fix fwnode_irq_get_byname() returnvalue
@ 2022-10-25 15:11 Matti Vaittinen
  2022-10-25 15:11 ` [PATCH v2 1/2] drivers: fwnode: fix fwnode_irq_get_byname() Matti Vaittinen
  2022-10-25 15:12 ` [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix Matti Vaittinen
  0 siblings, 2 replies; 7+ messages in thread
From: Matti Vaittinen @ 2022-10-25 15:11 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Wolfram Sang, Akhil R,
	linux-acpi, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 1959 bytes --]

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.c
drivers/iio/accel/adxl355_core.c
drivers/iio/gyro/fxas21002c_core.c
drivers/iio/imu/adis16480.c
drivers/iio/imu/bmi160/bmi160_core.c
drivers/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.

Changelog v1 => v2:
 - minor styling

---

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
-- 
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 =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* [PATCH v2 1/2] drivers: fwnode: fix fwnode_irq_get_byname()
  2022-10-25 15:11 [PATCH v2 0/2] fix fwnode_irq_get_byname() returnvalue Matti Vaittinen
@ 2022-10-25 15:11 ` Matti Vaittinen
  2022-10-25 16:21   ` Andy Shevchenko
  2022-10-25 15:12 ` [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix Matti Vaittinen
  1 sibling, 1 reply; 7+ messages in thread
From: Matti Vaittinen @ 2022-10-25 15:11 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Wolfram Sang, Akhil R,
	linux-acpi, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 2741 bytes --]

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 <sakari.ailus@linux.intel.com>
Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

---
v1 => v2:
- change check if (!ret) => if (ret == 0), add comment.
- fix drop redundant empty line.

I did a quick audit for the callers at v6.1-rc2:
drivers/i2c/i2c-smbus.c
drivers/iio/accel/adxl355_core.c
drivers/iio/gyro/fxas21002c_core.c
drivers/iio/imu/adis16480.c
drivers/iio/imu/bmi160/bmi160_core.c
drivers/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(-)

diff --git a/drivers/base/property.c b/drivers/base/property.c
index 4d6278a84868..b4ccc64f7bd2 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -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);
+	/* We treat mapping errors as invalid case */
+	if (ret == 0)
+		return -EINVAL;
+
+	return ret;
 }
 EXPORT_SYMBOL(fwnode_irq_get_byname);
 
-- 
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 =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix
  2022-10-25 15:11 [PATCH v2 0/2] fix fwnode_irq_get_byname() returnvalue Matti Vaittinen
  2022-10-25 15:11 ` [PATCH v2 1/2] drivers: fwnode: fix fwnode_irq_get_byname() Matti Vaittinen
@ 2022-10-25 15:12 ` Matti Vaittinen
  2022-10-25 16:30   ` Andy Shevchenko
  1 sibling, 1 reply; 7+ messages in thread
From: Matti Vaittinen @ 2022-10-25 15:12 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Wolfram Sang, Akhil R,
	linux-acpi, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 1488 bytes --]

The fwnode_irq_get_byname() was changed to not return 0 upon failure so
return value check can be adjusted to reflect the change.

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

---

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(-)

diff --git a/drivers/i2c/i2c-smbus.c b/drivers/i2c/i2c-smbus.c
index 07c92c8495a3..d0cc4b7903ed 100644
--- a/drivers/i2c/i2c-smbus.c
+++ b/drivers/i2c/i2c-smbus.c
@@ -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;
 	}
 
-- 
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 =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v2 1/2] drivers: fwnode: fix fwnode_irq_get_byname()
  2022-10-25 15:11 ` [PATCH v2 1/2] drivers: fwnode: fix fwnode_irq_get_byname() Matti Vaittinen
@ 2022-10-25 16:21   ` Andy Shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2022-10-25 16:21 UTC (permalink / raw)
  To: Matti Vaittinen
  Cc: Matti Vaittinen, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Wolfram Sang, Akhil R,
	linux-acpi, linux-kernel, linux-i2c

On Tue, Oct 25, 2022 at 06:11:49PM +0300, Matti Vaittinen wrote:
> 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.

and abd ?

...

> +	ret = fwnode_irq_get(fwnode, index);
> +	/* We treat mapping errors as invalid case */
> +	if (ret == 0)
> +		return -EINVAL;
> +
> +	return ret;

This looks good.

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix
  2022-10-25 15:12 ` [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix Matti Vaittinen
@ 2022-10-25 16:30   ` Andy Shevchenko
  2022-10-27  5:40     ` Vaittinen, Matti
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2022-10-25 16:30 UTC (permalink / raw)
  To: Matti Vaittinen
  Cc: Matti Vaittinen, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Wolfram Sang, Akhil R,
	linux-acpi, linux-kernel, linux-i2c

On Tue, Oct 25, 2022 at 06:12:11PM +0300, Matti Vaittinen wrote:
> The 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.

The entire error handling there looks suspicious.

The 'struct i2c_smbus_alert_setup' description says:

 "If irq is not specified, the smbus_alert driver doesn't take care of
  interrupt handling. In that case it is up to the I2C bus driver to
  either handle the interrupts or to poll for alerts."

So, the question is, shouldn't we just drop the check completely?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix
  2022-10-25 16:30   ` Andy Shevchenko
@ 2022-10-27  5:40     ` Vaittinen, Matti
  2022-11-15  9:09       ` Vaittinen, Matti
  0 siblings, 1 reply; 7+ messages in thread
From: Vaittinen, Matti @ 2022-10-27  5:40 UTC (permalink / raw)
  To: Andy Shevchenko, Matti Vaittinen
  Cc: Daniel Scally, Heikki Krogerus, Sakari Ailus, Greg Kroah-Hartman,
	Rafael J. Wysocki, Wolfram Sang, Akhil R, linux-acpi,
	linux-kernel, linux-i2c, jdelvare

On 10/25/22 19:30, Andy Shevchenko wrote:
> On Tue, Oct 25, 2022 at 06:12:11PM +0300, Matti Vaittinen wrote:
>> The 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.
> 
> The entire error handling there looks suspicious.
> 
> The 'struct i2c_smbus_alert_setup' description says:
> 
>   "If irq is not specified, the smbus_alert driver doesn't take care of
>    interrupt handling. In that case it is up to the I2C bus driver to
>    either handle the interrupts or to poll for alerts."
> 
> So, the question is, shouldn't we just drop the check completely?

I don't really know what this means. Does it mean that if IRQ is not 
provided, the driver needs to take care of alerts (in which case the 
check here is very valid because IRQ is required for smbus_alert 
driver). Or does it mean that only the IRQ handling is omitted while the 
smbus_alert driver should do all the other stuff (what ever that is) as 
usual. In this case this check indeed feels wrong.

I would appreciate someone with more insight to this driver to take a 
look at it.

Yours
	-- Matti

-- 
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~


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

* Re: [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix
  2022-10-27  5:40     ` Vaittinen, Matti
@ 2022-11-15  9:09       ` Vaittinen, Matti
  0 siblings, 0 replies; 7+ messages in thread
From: Vaittinen, Matti @ 2022-11-15  9:09 UTC (permalink / raw)
  To: Andy Shevchenko, Matti Vaittinen
  Cc: Daniel Scally, Heikki Krogerus, Sakari Ailus, Greg Kroah-Hartman,
	Rafael J. Wysocki, Wolfram Sang, Akhil R, linux-acpi,
	linux-kernel, linux-i2c, jdelvare, Mark Brown

On 10/27/22 08:40, Matti Vaittinen wrote:
> On 10/25/22 19:30, Andy Shevchenko wrote:
>> On Tue, Oct 25, 2022 at 06:12:11PM +0300, Matti Vaittinen wrote:
>>> The 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.
>>
>> The entire error handling there looks suspicious.
>>
>> The 'struct i2c_smbus_alert_setup' description says:
>>
>>   "If irq is not specified, the smbus_alert driver doesn't take care of
>>    interrupt handling. In that case it is up to the I2C bus driver to
>>    either handle the interrupts or to poll for alerts."
>>
>> So, the question is, shouldn't we just drop the check completely?
> 
> I don't really know what this means. Does it mean that if IRQ is not 
> provided, the driver needs to take care of alerts (in which case the 
> check here is very valid because IRQ is required for smbus_alert 
> driver). Or does it mean that only the IRQ handling is omitted while the 
> smbus_alert driver should do all the other stuff (what ever that is) as 
> usual. In this case this check indeed feels wrong.
> 
> I would appreciate someone with more insight to this driver to take a 
> look at it.

Wolfram, do you have the required insight?

What would be the best way to proceed? I see 3 options:

1. fix the return value as is done by this series.
https://lore.kernel.org/all/cover.1666710197.git.mazziesaccount@gmail.com/
	=> Will cause the i2c-smbus probe to return failure also if IRQ
	   mapping fails.

2. apply the 1/1 from the series "as is" - but drop the return value 
check for fwnode_irq_get_byname() altogether as was suggested by Andy

3. drop this series and apply the documentation fix suggested in:
https://lore.kernel.org/all/Y1dzCCMCDswQFVvO@dc75zzyyyyyyyyyyyyyby-3.rev.dnainternet.fi/

Thoughts anyone?

Yours
	-- Matti


-- 
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~


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

end of thread, other threads:[~2022-11-15  9:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-25 15:11 [PATCH v2 0/2] fix fwnode_irq_get_byname() returnvalue Matti Vaittinen
2022-10-25 15:11 ` [PATCH v2 1/2] drivers: fwnode: fix fwnode_irq_get_byname() Matti Vaittinen
2022-10-25 16:21   ` Andy Shevchenko
2022-10-25 15:12 ` [PATCH v2 2/2] i2c: i2c-smbus: fwnode_irq_get_byname() return value fix Matti Vaittinen
2022-10-25 16:30   ` Andy Shevchenko
2022-10-27  5:40     ` Vaittinen, Matti
2022-11-15  9:09       ` Vaittinen, Matti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).