mbox series

[v5,0/8] fix fwnode_irq_get[_byname()] returnvalue

Message ID cover.1684493615.git.mazziesaccount@gmail.com
Headers show
Series fix fwnode_irq_get[_byname()] returnvalue | expand

Message

Matti Vaittinen May 19, 2023, 11 a.m. UTC
The fwnode_irq_get() and the fwnode_irq_get_byname() may have returned
zero if mapping the IRQ fails. This contradicts the
fwnode_irq_get_byname() 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() and the fwnode_irq_get_byname() to
always return a negative errno upon failure.

I have audited following callers (v6.4-rc2):

fwnode_irq_get_byname():
drivers/i2c/i2c-smbus.c
drivers/iio/accel/adxl355_core.c
drivers/iio/accel/kionix-kx022a.c
drivers/iio/adc/ad4130.c
drivers/iio/adc/max11410.c
drivers/iio/addac/ad74115.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

fwnode_irq_get():
drivers/gpio/gpio-dwapb.c
drivers/iio/chemical/scd30_serial.c
drivers/iio/proximity/mb1232.c
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c
drivers/net/mdio/fwnode_mdio.c
drivers/pinctrl/pinctrl-ingenic.c
drivers/pinctrl/pinctrl-microchip-sgpio.c
drivers/pinctrl/pinctrl-pistachio.c

and it seems to me these calls will be Ok after the change. The
i2c-smbus.c and kionix-kx022a.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.

NOTES:

Changes are compile-tested only.

drivers/pinctrl/nuvoton/pinctrl-wpcm450.c
will also gain a functional change. The pinctrl-wpcm450.c change is easy
to see - after this series the device-tree mapping failures will be
handled as any other errors - probe will be aborted with -EINVAL. Other
feasible option could be treating other errors in IRQ getting same way
as the DT mapping failures - just silently skip the IRQ. Please see
comment in the respective patch.

drivers/iio/cdc/ad7150.c
will gain functional change as well. Here the logic is less
straightforward but boils down to the same question as with the
pinctrl-wpcm450.c. Should all the IRQ getting errors jump to same
'no-IRQ' branch as the DT mapping error, or should the DT mapping error
abort the probe with error same way as other IRQ getting failures do?

Revision history:
v4 => v5:
 - Fix subject lines for mvpp2 and wpcm450
 - drop unnecessary irqno assignment from mb1232
 - add back the drivers/i2c/i2c-smbus.c change which was accidentally
   dropped during v3 => v4 work
v3 => v4:
 - Change also the fwnode_irq_get() as was suggested by Jonathan.
Changelog v2 => v3:
 - rebase/resend/add kx022a fix.
Changelog v1 => v2:
 - minor styling

---

Matti Vaittinen (8):
  drivers: fwnode: fix fwnode_irq_get[_byname]()
  iio: mb1232: relax return value check for IRQ get
  net-next: mvpp2: relax return value check for IRQ get
  pinctrl: wpcm450: relax return value check for IRQ get
  pinctrl: ingenic: relax return value check for IRQ get
  pinctrl: pistachio: relax return value check for IRQ get
  iio: cdc: ad7150: Functional change
  i2c: i2c-smbus: fwnode_irq_get_byname() return value fix

 drivers/base/property.c                         | 12 +++++++++---
 drivers/i2c/i2c-smbus.c                         |  2 +-
 drivers/iio/cdc/ad7150.c                        |  3 +--
 drivers/iio/proximity/mb1232.c                  |  7 ++-----
 drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c |  4 ++--
 drivers/pinctrl/nuvoton/pinctrl-wpcm450.c       |  2 --
 drivers/pinctrl/pinctrl-ingenic.c               |  2 --
 drivers/pinctrl/pinctrl-pistachio.c             |  6 ------
 8 files changed, 15 insertions(+), 23 deletions(-)


base-commit: f1fcbaa18b28dec10281551dfe6ed3a3ed80e3d6

Comments

Jonathan Cameron May 20, 2023, 3:30 p.m. UTC | #1
On Fri, 19 May 2023 14:04:32 +0300
Matti Vaittinen <mazziesaccount@gmail.com> wrote:

> fwnode_irq_get[_byname]() were changed to not return 0 anymore. The
> special error case where device-tree based IRQ mapping fails can't no
> longer be reliably detected from this return value. This yields a
> functional change in the driver where the mapping failure is treated as
> an error.
> 
> The mapping failure can occur for example when the device-tree IRQ
> information translation call-back(s) (xlate) fail, IRQ domain is not
> found, IRQ type conflicts, etc. In most cases this indicates an error in
> the device-tree and special handling is not really required.
> 
> One more thing to note is that ACPI APIs do not return zero for any
> failures so this special handling did only apply on device-tree based
> systems.
> 
> Drop the special handling for DT mapping failures as these can no longer
> be separated from other errors at driver side.
> 
> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
> 
> ---
> 
> Please note that I don't have the hardware to test this change.
> Furthermore, testing this type of device-tree error cases is not
> trivial, as the question we probably dive in is "what happens with the
> existing users who have errors in the device-tree". Answering to this
> question is not simple.
> 
> I did this patch with minimal code changes - but a question is if we
> should really jump into the else branch below on all IRQ getting errors?
> 
>         } else {
>                 indio_dev->info = &ad7150_info_no_irq;
>                 switch (id->driver_data) {
>                 case AD7150:
>                         indio_dev->channels = ad7150_channels_no_irq;
>                         indio_dev->num_channels =
>                                 ARRAY_SIZE(ad7150_channels_no_irq);
>                         break;
>                 case AD7151:
>                         indio_dev->channels = ad7151_channels_no_irq;
>                         indio_dev->num_channels =
>                                 ARRAY_SIZE(ad7151_channels_no_irq);
>                         break;
>                 default:
>                         return -EINVAL;
>                 }
> 
> Why do we have special handling for !chip->interrupts[0] while other
> errors on getting the fwnode_irq_get(dev_fwnode(&client->dev), 0); will
> abort the probe?

Gut feeling is that this was a rework of board file code where 0 meant not
provided. We should look to do the same here.  I'm not sure we have a consistent
return for no irq though across the various fw types.

The driver looks like it should support either no interrupts or all the
ones for a given device.

Currrently it definitely doesn't handle the no irqs provided case right.
Its not elegant, but if we have to have all failures to get irqs result
in carrying on without them then that's better than now.

Jonathan


> 
> The first patch of the series changes the fwnode_irq_get() so this depends
> on the first patch of the series and should not be applied alone.
> ---
>  drivers/iio/cdc/ad7150.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/cdc/ad7150.c b/drivers/iio/cdc/ad7150.c
> index 79aeb0aaea67..d7ba50b9780d 100644
> --- a/drivers/iio/cdc/ad7150.c
> +++ b/drivers/iio/cdc/ad7150.c
> @@ -567,8 +567,7 @@ static int ad7150_probe(struct i2c_client *client)
>  		if (chip->interrupts[1] < 0)
>  			return chip->interrupts[1];
>  	}
> -	if (chip->interrupts[0] &&
> -	    (id->driver_data == AD7151 || chip->interrupts[1])) {
> +	if (id->driver_data == AD7151 || chip->interrupts[1]) {
>  		irq_set_status_flags(chip->interrupts[0], IRQ_NOAUTOEN);
>  		ret = devm_request_threaded_irq(&client->dev,
>  						chip->interrupts[0],
Andy Shevchenko May 21, 2023, 5:19 p.m. UTC | #2
Fri, May 19, 2023 at 02:01:47PM +0300, Matti Vaittinen kirjoitti:
> fwnode_irq_get[_byname]() were changed to not return 0 anymore.
> 
> Drop check for return value 0.

...

> -		if (v->irq <= 0) {
> +		if (v->irq < 0) {
>  			ret = -EINVAL;

			ret = v->irq;

?

>  			goto err;
>  		}
Matti Vaittinen May 22, 2023, 5:15 a.m. UTC | #3
Hi Andy,

On 5/21/23 20:19, andy.shevchenko@gmail.com wrote:
> Fri, May 19, 2023 at 02:01:47PM +0300, Matti Vaittinen kirjoitti:
>> fwnode_irq_get[_byname]() were changed to not return 0 anymore.
>>
>> Drop check for return value 0.
> 
> ...
> 
>> -		if (v->irq <= 0) {
>> +		if (v->irq < 0) {
>>   			ret = -EINVAL;
> 
> 			ret = v->irq;
> 
> ?

For me that seems to be correct, yes. This, however, would be a 
functional change and in my opinion it should be done separately from 
this API change.

> 
>>   			goto err;
>>   		}
>
Jonathan Cameron May 28, 2023, 7:14 p.m. UTC | #4
On Mon, 22 May 2023 08:15:01 +0300
Matti Vaittinen <mazziesaccount@gmail.com> wrote:

> Hi Andy,
> 
> On 5/21/23 20:19, andy.shevchenko@gmail.com wrote:
> > Fri, May 19, 2023 at 02:01:47PM +0300, Matti Vaittinen kirjoitti:  
> >> fwnode_irq_get[_byname]() were changed to not return 0 anymore.
> >>
> >> Drop check for return value 0.  
> > 
> > ...
> >   
> >> -		if (v->irq <= 0) {
> >> +		if (v->irq < 0) {
> >>   			ret = -EINVAL;  
> > 
> > 			ret = v->irq;
> > 
> > ?  
> 
> For me that seems to be correct, yes. This, however, would be a 
> functional change and in my opinion it should be done separately from 
> this API change.
Ah. I commented on this as well in v6.  Roll us that separate patch
and I expect we'll both be happy ;)

Jonathan

> 
> >   
> >>   			goto err;
> >>   		}  
> >   
>