diff mbox series

[2/2] i2c: Clear client->irq in i2c_device_remove

Message ID 20181019085958.32694-2-ckeepax@opensource.cirrus.com
State Accepted
Headers show
Series [1/2] i2c: Remove unnecessary call to irq_find_mapping | expand

Commit Message

Charles Keepax Oct. 19, 2018, 8:59 a.m. UTC
The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
i2c_device_remove does not clear this. When rebinding an I2C device,
whos IRQ provider has also been rebound this means that an IRQ mapping
will never be created, causing the I2C device to fail to acquire its
IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
forcing i2c_device_probe to lookup the mapping again.

Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
---
 drivers/i2c/i2c-core-base.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Wolfram Sang Oct. 28, 2018, 10:31 p.m. UTC | #1
On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> i2c_device_remove does not clear this. When rebinding an I2C device,
> whos IRQ provider has also been rebound this means that an IRQ mapping
> will never be created, causing the I2C device to fail to acquire its
> IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> forcing i2c_device_probe to lookup the mapping again.
> 
> Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>

Adding Benjamin here again. Also, should there be a Fixes: tag?

> ---
>  drivers/i2c/i2c-core-base.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
> index 656f0a6fe3adf..28460f6a60cc1 100644
> --- a/drivers/i2c/i2c-core-base.c
> +++ b/drivers/i2c/i2c-core-base.c
> @@ -430,6 +430,8 @@ static int i2c_device_remove(struct device *dev)
>  	dev_pm_clear_wake_irq(&client->dev);
>  	device_init_wakeup(&client->dev, false);
>  
> +	client->irq = 0;
> +
>  	return status;
>  }
>  
> -- 
> 2.11.0
>
Benjamin Tissoires Oct. 29, 2018, 10:15 a.m. UTC | #2
On Sun, Oct 28, 2018 at 11:31 PM Wolfram Sang <wsa@the-dreams.de> wrote:
>
> On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> > The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> > i2c_device_remove does not clear this. When rebinding an I2C device,
> > whos IRQ provider has also been rebound this means that an IRQ mapping
> > will never be created, causing the I2C device to fail to acquire its
> > IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> > forcing i2c_device_probe to lookup the mapping again.
> >
> > Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
>
> Adding Benjamin here again. Also, should there be a Fixes: tag?

Not sure if the circumstances are preventing me to think straight, but
how can you reprobe the i2c_device?
And in all cases, for the Host notify part, having the IRQ already set
shouldn't be an issue.

So for the host notify case, this patch is fine, not entirely sure
about the acpi or OF part of it.

Cheers,
Benjamin

>
> > ---
> >  drivers/i2c/i2c-core-base.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
> > index 656f0a6fe3adf..28460f6a60cc1 100644
> > --- a/drivers/i2c/i2c-core-base.c
> > +++ b/drivers/i2c/i2c-core-base.c
> > @@ -430,6 +430,8 @@ static int i2c_device_remove(struct device *dev)
> >       dev_pm_clear_wake_irq(&client->dev);
> >       device_init_wakeup(&client->dev, false);
> >
> > +     client->irq = 0;
> > +
> >       return status;
> >  }
> >
> > --
> > 2.11.0
> >
Charles Keepax Oct. 30, 2018, 11:51 a.m. UTC | #3
On Mon, Oct 29, 2018 at 11:15:47AM +0100, Benjamin Tissoires wrote:
> On Sun, Oct 28, 2018 at 11:31 PM Wolfram Sang <wsa@the-dreams.de> wrote:
> >
> > On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> > > The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> > > i2c_device_remove does not clear this. When rebinding an I2C device,
> > > whos IRQ provider has also been rebound this means that an IRQ mapping
> > > will never be created, causing the I2C device to fail to acquire its
> > > IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> > > forcing i2c_device_probe to lookup the mapping again.
> > >
> > > Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
> >
> > Adding Benjamin here again. Also, should there be a Fixes: tag?
> 
> Not sure if the circumstances are preventing me to think straight, but
> how can you reprobe the i2c_device?

You just head into /sys/bus/i2c/drivers/<the_driver> and use the
unbind file to remove the driver. You can then probe the driver
again using the bind file.

> And in all cases, for the Host notify part, having the IRQ already set
> shouldn't be an issue.

To be clear this isn't a theoretical issue this is something I
can replicate very easily. The problem comes in when you unbind
both the I2C device and the device that is providing its IRQ. In
my case the I2C device is a speaker amp and the device providing
IRQs is an audio CODEC.

When the device providing the IRQ is unbound it will delete the
IRQ mapping. For the I2C device to acquire its IRQ something
needs to recreate that mapping, this would normally happen (in a
DT system) as a result of the of_irq_get/_by_name. But as
client->irq is already set this doesn't happen, causing the I2C
device to fail probe because it couldn't locate its IRQ.

I can provide some stack traces or something if that would help
to clarify the issue?

Thanks,
Charles
Benjamin Tissoires Oct. 30, 2018, 2:34 p.m. UTC | #4
On Tue, Oct 30, 2018 at 12:51 PM Charles Keepax
<ckeepax@opensource.cirrus.com> wrote:
>
> On Mon, Oct 29, 2018 at 11:15:47AM +0100, Benjamin Tissoires wrote:
> > On Sun, Oct 28, 2018 at 11:31 PM Wolfram Sang <wsa@the-dreams.de> wrote:
> > >
> > > On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> > > > The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> > > > i2c_device_remove does not clear this. When rebinding an I2C device,
> > > > whos IRQ provider has also been rebound this means that an IRQ mapping
> > > > will never be created, causing the I2C device to fail to acquire its
> > > > IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> > > > forcing i2c_device_probe to lookup the mapping again.
> > > >
> > > > Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
> > >
> > > Adding Benjamin here again. Also, should there be a Fixes: tag?
> >
> > Not sure if the circumstances are preventing me to think straight, but
> > how can you reprobe the i2c_device?
>
> You just head into /sys/bus/i2c/drivers/<the_driver> and use the
> unbind file to remove the driver. You can then probe the driver
> again using the bind file.

Right... not sure why I haven't seen that one yesterday

>
> > And in all cases, for the Host notify part, having the IRQ already set
> > shouldn't be an issue.
>
> To be clear this isn't a theoretical issue this is something I
> can replicate very easily. The problem comes in when you unbind
> both the I2C device and the device that is providing its IRQ. In
> my case the I2C device is a speaker amp and the device providing
> IRQs is an audio CODEC.
>
> When the device providing the IRQ is unbound it will delete the
> IRQ mapping. For the I2C device to acquire its IRQ something
> needs to recreate that mapping, this would normally happen (in a
> DT system) as a result of the of_irq_get/_by_name. But as
> client->irq is already set this doesn't happen, causing the I2C
> device to fail probe because it couldn't locate its IRQ.

Right. I completely missed the fact that the driver of the IRQ could
be reset during the period the device is unbound.
That's a pretty strong case for this patch.

>
> I can provide some stack traces or something if that would help
> to clarify the issue?

No, that's fine. Now I get this, and I totally agree with the approach:

Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

Cheers,
Benjamin
Wolfram Sang Oct. 30, 2018, 2:55 p.m. UTC | #5
> No, that's fine. Now I get this, and I totally agree with the approach:
> 
> Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

Thanks! If one of you could provide me with a Fixes tag (for this. or
both patches?), that would be most helpful.
Charles Keepax Oct. 31, 2018, 9:52 a.m. UTC | #6
On Tue, Oct 30, 2018 at 02:55:50PM +0000, Wolfram Sang wrote:
> 
> > No, that's fine. Now I get this, and I totally agree with the approach:
> > 
> > Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> 
> Thanks! If one of you could provide me with a Fixes tag (for this. or
> both patches?), that would be most helpful.
> 

It's a bit of a long chain and fairly tricky to say exactly when
the bug was introduced but I think this is probably the closest
match:

Fixes: 2fd36c552649 ("i2c: core: Map OF IRQ at probe time")

I suspect the process of unbinding/rebinding would have also
not worked before that commit but it would have not worked
for different reasons.

Thanks,
Charles
Wolfram Sang Oct. 31, 2018, 9:59 a.m. UTC | #7
> It's a bit of a long chain and fairly tricky to say exactly when
> the bug was introduced but I think this is probably the closest
> match:
> 
> Fixes: 2fd36c552649 ("i2c: core: Map OF IRQ at probe time")

Thank you for checking! Then, I'll just leave it and cc stable so it can
be applied as long as it fits.
Wolfram Sang Oct. 31, 2018, 11:35 p.m. UTC | #8
On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> i2c_device_remove does not clear this. When rebinding an I2C device,
> whos IRQ provider has also been rebound this means that an IRQ mapping
> will never be created, causing the I2C device to fail to acquire its
> IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> forcing i2c_device_probe to lookup the mapping again.
> 
> Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>

Applied to for-current, thanks!
Yauhen Kharuzhy Jan. 9, 2019, 9:47 p.m. UTC | #9
On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> i2c_device_remove does not clear this. When rebinding an I2C device,
> whos IRQ provider has also been rebound this means that an IRQ mapping
> will never be created, causing the I2C device to fail to acquire its
> IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> forcing i2c_device_probe to lookup the mapping again.

Hi.

I found  driver i2c/busses/i2c-cht-wc.c which instantiates I2C device
(battery charger) and passes IRQ to driver not using standard I2C IRQ mapping code.
So if we reprobe I2C device (by reloading I2C device driver module or by
manipulations with sysfs), we get invalid IRQ number for client:

 adap->client_irq = irq_create_mapping(adap->irq_domain, 0);
 ...
 irq_set_chip_data(adap->client_irq, adap);
 irq_set_chip_and_handler(adap->client_irq, &adap->irqchip, handle_simple_irq);
 ...
 board_info.irq = adap->client_irq;
 adap->client = i2c_new_device(&adap->adapter, &board_info);


adap->client->irq will be reset after device removing here.


Any advice to fix this? Maybe move initial i2c_client->irq value to new field
like client->init_irq and copy it to client->irq at probing, for example?

> 
> Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
> Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> ---
>  drivers/i2c/i2c-core-base.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
> index 656f0a6fe3adf..28460f6a60cc1 100644
> --- a/drivers/i2c/i2c-core-base.c
> +++ b/drivers/i2c/i2c-core-base.c
> @@ -430,6 +430,8 @@ static int i2c_device_remove(struct device *dev)
>  	dev_pm_clear_wake_irq(&client->dev);
>  	device_init_wakeup(&client->dev, false);
>  
> +	client->irq = 0;
> +
>  	return status;
>  }
>
Charles Keepax Jan. 10, 2019, 1:32 p.m. UTC | #10
On Thu, Jan 10, 2019 at 12:47:56AM +0300, Yauhen Kharuzhy wrote:
> On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> > The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> > i2c_device_remove does not clear this. When rebinding an I2C device,
> > whos IRQ provider has also been rebound this means that an IRQ mapping
> > will never be created, causing the I2C device to fail to acquire its
> > IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> > forcing i2c_device_probe to lookup the mapping again.
> 
> Hi.
> 
> I found  driver i2c/busses/i2c-cht-wc.c which instantiates I2C device
> (battery charger) and passes IRQ to driver not using standard I2C IRQ mapping code.
> So if we reprobe I2C device (by reloading I2C device driver module or by
> manipulations with sysfs), we get invalid IRQ number for client:
> 
>  adap->client_irq = irq_create_mapping(adap->irq_domain, 0);
>  ...
>  irq_set_chip_data(adap->client_irq, adap);
>  irq_set_chip_and_handler(adap->client_irq, &adap->irqchip, handle_simple_irq);
>  ...
>  board_info.irq = adap->client_irq;
>  adap->client = i2c_new_device(&adap->adapter, &board_info);
> 
> 
> adap->client->irq will be reset after device removing here.
> 
> 
> Any advice to fix this? Maybe move initial i2c_client->irq value to new field
> like client->init_irq and copy it to client->irq at probing, for example?
> 

Could you be a little more specific here, are you saying if you
reprobe the battery charger device or if you reprobe the I2C
controller itself?

Apologies but I am having a little difficulty working out the
path through which the IRQ is not reinitialised. As I would have
through the battery chargers probe would have reset up the IRQ
then the core would pick it up again from there.

Thanks,
Charles
Yauhen Kharuzhy Jan. 10, 2019, 8:35 p.m. UTC | #11
On Thu, Jan 10, 2019 at 01:32:36PM +0000, Charles Keepax wrote:
> On Thu, Jan 10, 2019 at 12:47:56AM +0300, Yauhen Kharuzhy wrote:
> > On Fri, Oct 19, 2018 at 09:59:58AM +0100, Charles Keepax wrote:
> > > The IRQ will be mapped in i2c_device_probe only if client->irq is zero and
> > > i2c_device_remove does not clear this. When rebinding an I2C device,
> > > whos IRQ provider has also been rebound this means that an IRQ mapping
> > > will never be created, causing the I2C device to fail to acquire its
> > > IRQ. Fix this issue by clearing client->irq in i2c_device_remove,
> > > forcing i2c_device_probe to lookup the mapping again.
> > 
> > Hi.
> > 
> > I found  driver i2c/busses/i2c-cht-wc.c which instantiates I2C device
> > (battery charger) and passes IRQ to driver not using standard I2C IRQ mapping code.
> > So if we reprobe I2C device (by reloading I2C device driver module or by
> > manipulations with sysfs), we get invalid IRQ number for client:
> > 
> >  adap->client_irq = irq_create_mapping(adap->irq_domain, 0);
> >  ...
> >  irq_set_chip_data(adap->client_irq, adap);
> >  irq_set_chip_and_handler(adap->client_irq, &adap->irqchip, handle_simple_irq);
> >  ...
> >  board_info.irq = adap->client_irq;
> >  adap->client = i2c_new_device(&adap->adapter, &board_info);
> > 
> > 
> > adap->client->irq will be reset after device removing here.
> > 
> > 
> > Any advice to fix this? Maybe move initial i2c_client->irq value to new field
> > like client->init_irq and copy it to client->irq at probing, for example?
> > 
> 
> Could you be a little more specific here, are you saying if you
> reprobe the battery charger device or if you reprobe the I2C
> controller itself?

If I reprobe the battery charger.

> 
> Apologies but I am having a little difficulty working out the
> path through which the IRQ is not reinitialised. As I would have
> through the battery chargers probe would have reset up the IRQ
> then the core would pick it up again from there.

No, this I2C bus driver(i2c-cht-wc) pass the IRQ to charger not throuth
ACPI of OF but by initializing of board_info.irq field, so I2C subsystem
cannot reinitialize irq automatically.

I am not sure if this is right path for IRQ initialization and maybe
should be changed for this driver.

> 
> Thanks,
> Charles
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 656f0a6fe3adf..28460f6a60cc1 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -430,6 +430,8 @@  static int i2c_device_remove(struct device *dev)
 	dev_pm_clear_wake_irq(&client->dev);
 	device_init_wakeup(&client->dev, false);
 
+	client->irq = 0;
+
 	return status;
 }