Skip to contentSkip to conversation

i2c: hisi: Only handle the interrupt of the driver's transfer

PATCH Applied Yicong Yang 4 participants 9 messages Aug 1, 2023 – Aug 15, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: hisi: Only handle the interrupt of the driver's transfer

From: Yicong Yang <[email protected]>

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

---
 drivers/i2c/busses/i2c-hisi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/i2c/busses/i2c-hisi.c +8 -0
index e067671b3ce2..8328da4bc3ec 100644
@@ -330,6 +330,14 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
 	struct hisi_i2c_controller *ctlr = context;
 	u32 int_stat;
 
+	/*
+	 * Don't handle the interrupt if cltr->completion is NULL. We may
+	 * reach here because the interrupt is spurious or the transfer is
+	 * started by another port rather than us.
+	 */
+	if (!ctlr->completion)
+		return IRQ_NONE;
+
 	int_stat = readl(ctlr->iobase + HISI_I2C_INT_MSTAT);
 	hisi_i2c_clear_int(ctlr, int_stat);
 	if (!(int_stat & HISI_I2C_INT_ALL))
Signature · 1 line
2.24.0
maintainer
#1 lore raw

Hi Yicong,

Quoted 8 lines · Yicong Yang

From: Yicong Yang <[email protected]>

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

Is this a fix? Then, could you please add:

Fixes d62fbdb99a85 ("i2c: add support for HiSilicon I2C controller")
Cc <[email protected]> # v5.13+

What kind of crash is this? Is it a NULL pointer dereference?

Quoted 9 lines
---
 drivers/i2c/busses/i2c-hisi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/i2c/busses/i2c-hisi.c +0 -0
index e067671b3ce2..8328da4bc3ec 100644

drivers/i2c/busses/i2c-hisi.c static irqreturn_t hisi_i2c_irq(int irq, void *context) · line 339

Quoted 10 lines
drivers/i2c/busses/i2c-hisi.c +7 -0
@@ -330,6 +330,14 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
 	struct hisi_i2c_controller *ctlr = context;
 	u32 int_stat;
 
+	/*
+	 * Don't handle the interrupt if cltr->completion is NULL. We may
+	 * reach here because the interrupt is spurious or the transfer is
+	 * started by another port rather than us.
+	 */
+	if (!ctlr->completion)
+		return IRQ_NONE;

Is this the place you should really check for completion being NULL? By reading the code I don't exclude that completion at this stage might be NULL.

Can it be that the real fix is this one instead:

@@ -352,7 +352,7 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
* Only use TRANS_CPLT to indicate the completion. On error cases we'll
* get two interrupts, INT_ERR first then TRANS_CPLT.
*/
- if (int_stat & HISI_I2C_INT_TRANS_CPLT) {

+       if (ctrl->completion && (int_stat & HISI_I2C_INT_TRANS_CPLT)) {
                hisi_i2c_disable_int(ctlr, HISI_I2C_INT_ALL);
                hisi_i2c_clear_int(ctlr, HISI_I2C_INT_ALL);
                complete(ctlr->completion);

Anyway, this whole completion management smells a bit racy to me.

Andi

Quoted 6 lines
drivers/i2c/busses/i2c-hisi.c +0 -0
 	int_stat = readl(ctlr->iobase + HISI_I2C_INT_MSTAT);
 	hisi_i2c_clear_int(ctlr, int_stat);
 	if (!(int_stat & HISI_I2C_INT_ALL))
Signature · 1 line
2.24.0
#2 lore raw
Quoted 10 lines · Andi Shyti

Hi Yicong,

Quoted 7 lines · Yicong Yang

From: Yicong Yang <[email protected]>

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

Quoted 8 lines
Quoted 1 line

Is this a fix? Then, could you please add:

Fixes d62fbdb99a85 ("i2c: add support for HiSilicon I2C controller")
Cc <[email protected]> # v5.13+

What kind of crash is this? Is it a NULL pointer dereference?

I not quite sure this is a fix of the driver. On some use case the controller is shared between the firmware and the OS and we have no synchronization method from the hardware. A transfer started by the firmware cause the interrupt handled by the driver and cause a NULL pointer dereference.

Quoted 25 lines
Quoted 10 lines
---
 drivers/i2c/busses/i2c-hisi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/i2c/busses/i2c-hisi.c +0 -0
index e067671b3ce2..8328da4bc3ec 100644

drivers/i2c/busses/i2c-hisi.c static irqreturn_t hisi_i2c_irq(int irq, void *context) · line 339

Quoted 10 lines
drivers/i2c/busses/i2c-hisi.c +7 -0
@@ -330,6 +330,14 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
 	struct hisi_i2c_controller *ctlr = context;
 	u32 int_stat;
 
+	/*
+	 * Don't handle the interrupt if cltr->completion is NULL. We may
+	 * reach here because the interrupt is spurious or the transfer is
+	 * started by another port rather than us.
+	 */
+	if (!ctlr->completion)
+		return IRQ_NONE;

Is this the place you should really check for completion being NULL? By reading the code I don't exclude that completion at this stage might be NULL.

Quoted 1 line

Can it be that the real fix is this one instead:

Maybe not. If we handle the case as late as below, we'll operate the hardware which should be handled by the firmware which start the transfer. So we check it as early as possible.

Quoted 23 lines
(quoted diff) +1 -1
@@ -352,7 +352,7 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
         * Only use TRANS_CPLT to indicate the completion. On error cases we'll
         * get two interrupts, INT_ERR first then TRANS_CPLT.
         */
-       if (int_stat & HISI_I2C_INT_TRANS_CPLT) {
+       if (ctrl->completion && (int_stat & HISI_I2C_INT_TRANS_CPLT)) {
                hisi_i2c_disable_int(ctlr, HISI_I2C_INT_ALL);
                hisi_i2c_clear_int(ctlr, HISI_I2C_INT_ALL);
                complete(ctlr->completion);

Anyway, this whole completion management smells a bit racy to me.

Andi

Quoted 6 lines
drivers/i2c/busses/i2c-hisi.c +0 -0
 	int_stat = readl(ctlr->iobase + HISI_I2C_INT_MSTAT);
 	hisi_i2c_clear_int(ctlr, int_stat);
 	if (!(int_stat & HISI_I2C_INT_ALL))
Signature · 1 line
2.24.0

.

maintainer
#3 lore raw

Hi Yicong,

Quoted 18 lines · Yicong Yang
Quoted 17 lines · Andi Shyti

Hi Yicong,

Quoted 8 lines · Yicong Yang

From: Yicong Yang <[email protected]>

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

Is this a fix? Then, could you please add:

Fixes d62fbdb99a85 ("i2c: add support for HiSilicon I2C controller")
Cc <[email protected]> # v5.13+
Quoted 6 lines
Quoted 1 line

What kind of crash is this? Is it a NULL pointer dereference?

I not quite sure this is a fix of the driver. On some use case the controller is shared between the firmware and the OS and we have no synchronization method from the hardware. A transfer started by the firmware cause the interrupt handled by the driver and cause a NULL pointer dereference.

So that the firmware is running on a controller and memory, concurrently to the main CPU; which means that you are having some kind of bus arbitration issue with two masters on the bus.

Anyway, if we are talking about avoiding a NULL pointer dereference then this is a fix and you need to add the tags above.

(No need to resend for this, I can do it for you if you convince me on the part below.)

Quoted 24 lines
Quoted 24 lines
Quoted 9 lines
---
 drivers/i2c/busses/i2c-hisi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/i2c/busses/i2c-hisi.c +0 -0
index e067671b3ce2..8328da4bc3ec 100644

drivers/i2c/busses/i2c-hisi.c static irqreturn_t hisi_i2c_irq(int irq, void *context) · line 339

Quoted 10 lines
drivers/i2c/busses/i2c-hisi.c +7 -0
@@ -330,6 +330,14 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
 	struct hisi_i2c_controller *ctlr = context;
 	u32 int_stat;
 
+	/*
+	 * Don't handle the interrupt if cltr->completion is NULL. We may
+	 * reach here because the interrupt is spurious or the transfer is
+	 * started by another port rather than us.
+	 */
+	if (!ctlr->completion)
+		return IRQ_NONE;

Is this the place you should really check for completion being NULL? By reading the code I don't exclude that completion at this stage might be NULL.

Quoted 5 lines
Quoted 1 line

Can it be that the real fix is this one instead:

Maybe not. If we handle the case as late as below, we'll operate the hardware which should be handled by the firmware which start the transfer. So we check it as early as possible.

But if i2c_master_xfer() is not called and we receive an irq, most probably ctrl->completion is NULL. Right? Can this happen?

I can't really tell the sequence for enabling/disabling the interrupt in this device. They might happen in hisi_i2c_start_xfer() for enabling and in hisi_i2c_xfer_msg() for desabling at the last message; which makes the scenario above a bit difficult, indeed.

Thanks for the explanation,
Andi

Quoted 22 lines
Quoted 22 lines
(quoted diff) +1 -1
@@ -352,7 +352,7 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
         * Only use TRANS_CPLT to indicate the completion. On error cases we'll
         * get two interrupts, INT_ERR first then TRANS_CPLT.
         */
-       if (int_stat & HISI_I2C_INT_TRANS_CPLT) {
+       if (ctrl->completion && (int_stat & HISI_I2C_INT_TRANS_CPLT)) {
                hisi_i2c_disable_int(ctlr, HISI_I2C_INT_ALL);
                hisi_i2c_clear_int(ctlr, HISI_I2C_INT_ALL);
                complete(ctlr->completion);

Anyway, this whole completion management smells a bit racy to me.

Andi

Quoted 6 lines
drivers/i2c/busses/i2c-hisi.c +0 -0
 	int_stat = readl(ctlr->iobase + HISI_I2C_INT_MSTAT);
 	hisi_i2c_clear_int(ctlr, int_stat);
 	if (!(int_stat & HISI_I2C_INT_ALL))
Signature · 1 line
2.24.0

.

#4 lore raw
Quoted 23 lines · Andi Shyti

Hi Yicong,

Quoted 20 lines · Yicong Yang
Quoted 18 lines · Andi Shyti

Hi Yicong,

Quoted 8 lines · Yicong Yang

From: Yicong Yang <[email protected]>

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

Is this a fix? Then, could you please add:

Fixes d62fbdb99a85 ("i2c: add support for HiSilicon I2C controller")
Cc <[email protected]> # v5.13+

What kind of crash is this? Is it a NULL pointer dereference?

Quoted 9 lines
Quoted 4 lines

I not quite sure this is a fix of the driver. On some use case the controller is shared between the firmware and the OS and we have no synchronization method from the hardware. A transfer started by the firmware cause the interrupt handled by the driver and cause a NULL pointer dereference.

So that the firmware is running on a controller and memory, concurrently to the main CPU; which means that you are having some kind of bus arbitration issue with two masters on the bus.

That is one case. Another case maybe the i2c rtc also locates on the same bus and the efi-rtc will access the i2c bus.

Quoted 6 lines

Anyway, if we are talking about avoiding a NULL pointer dereference then this is a fix and you need to add the tags above.

(No need to resend for this, I can do it for you if you convince me on the part below.)

Thanks!

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 10 lines
---
 drivers/i2c/busses/i2c-hisi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/i2c/busses/i2c-hisi.c +0 -0
index e067671b3ce2..8328da4bc3ec 100644

drivers/i2c/busses/i2c-hisi.c static irqreturn_t hisi_i2c_irq(int irq, void *context) · line 339

Quoted 10 lines
drivers/i2c/busses/i2c-hisi.c +7 -0
@@ -330,6 +330,14 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
 	struct hisi_i2c_controller *ctlr = context;
 	u32 int_stat;
 
+	/*
+	 * Don't handle the interrupt if cltr->completion is NULL. We may
+	 * reach here because the interrupt is spurious or the transfer is
+	 * started by another port rather than us.
+	 */
+	if (!ctlr->completion)
+		return IRQ_NONE;

Is this the place you should really check for completion being NULL? By reading the code I don't exclude that completion at this stage might be NULL.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Can it be that the real fix is this one instead:

Maybe not. If we handle the case as late as below, we'll operate the hardware which should be handled by the firmware which start the transfer. So we check it as early as possible.

But if i2c_master_xfer() is not called and we receive an irq, most probably ctrl->completion is NULL. Right? Can this happen?

Yes, this is the case.

Quoted 6 lines

I can't really tell the sequence for enabling/disabling the interrupt in this device. They might happen in hisi_i2c_start_xfer() for enabling and in hisi_i2c_xfer_msg() for desabling at the last message; which makes the scenario above a bit difficult, indeed.

The driver will keep the interrupt disabled if no transfer in progress. But since the transfer is driven by the interrupt so if the firmware start the transfer it will enable the interrupt. In such case the driver will receive an interrupt on the Tx FIFO empty, etc and since the transfer is not started by the driver ctlr->completion is not initialized.

Thanks,
Yicong

Quoted 27 lines

Thanks for the explanation,
Andi

Quoted 14 lines
Quoted 14 lines
(quoted diff) +1 -1
@@ -352,7 +352,7 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
         * Only use TRANS_CPLT to indicate the completion. On error cases we'll
         * get two interrupts, INT_ERR first then TRANS_CPLT.
         */
-       if (int_stat & HISI_I2C_INT_TRANS_CPLT) {
+       if (ctrl->completion && (int_stat & HISI_I2C_INT_TRANS_CPLT)) {
                hisi_i2c_disable_int(ctlr, HISI_I2C_INT_ALL);
                hisi_i2c_clear_int(ctlr, HISI_I2C_INT_ALL);
                complete(ctlr->completion);

Anyway, this whole completion management smells a bit racy to me.

Andi

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
drivers/i2c/busses/i2c-hisi.c +0 -0
 	int_stat = readl(ctlr->iobase + HISI_I2C_INT_MSTAT);
 	hisi_i2c_clear_int(ctlr, int_stat);
 	if (!(int_stat & HISI_I2C_INT_ALL))
Signature · 1 line
2.24.0

.

.

maintainer Reviewed-by
#5 lore raw

Hi Yicong,

[...]

Quoted 35 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

drivers/i2c/busses/i2c-hisi.c static irqreturn_t hisi_i2c_irq(int irq, void *context) · line 339

Quoted 11 lines
drivers/i2c/busses/i2c-hisi.c +7 -0
@@ -330,6 +330,14 @@ static irqreturn_t hisi_i2c_irq(int irq, void *context)
 	struct hisi_i2c_controller *ctlr = context;
 	u32 int_stat;
 
+	/*
+	 * Don't handle the interrupt if cltr->completion is NULL. We may
+	 * reach here because the interrupt is spurious or the transfer is
+	 * started by another port rather than us.
+	 */
+	if (!ctlr->completion)
+		return IRQ_NONE;

Is this the place you should really check for completion being NULL? By reading the code I don't exclude that completion at this stage might be NULL.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Can it be that the real fix is this one instead:

Maybe not. If we handle the case as late as below, we'll operate the hardware which should be handled by the firmware which start the transfer. So we check it as early as possible.

But if i2c_master_xfer() is not called and we receive an irq, most probably ctrl->completion is NULL. Right? Can this happen?

Yes, this is the case.

Quoted 6 lines

I can't really tell the sequence for enabling/disabling the interrupt in this device. They might happen in hisi_i2c_start_xfer() for enabling and in hisi_i2c_xfer_msg() for desabling at the last message; which makes the scenario above a bit difficult, indeed.

Quoted 6 lines

The driver will keep the interrupt disabled if no transfer in progress. But since the transfer is driven by the interrupt so if the firmware start the transfer it will enable the interrupt. In such case the driver will receive an interrupt on the Tx FIFO empty, etc and since the transfer is not started by the driver ctlr->completion is not initialized.

OK... makes sense...

Thanks!
Andi

maintainer
#6 lore raw
In reply to Yicong Yang

Hi

Quoted 6 lines · Yicong Yang

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

With the Fixes tag added, applied to i2c/andi-for-current on

https://git.kernel.org/pub/scm/linux/kernel/git/andi.shyti/linux.git

Please note that this patch may still undergo further evaluation and the final decision will be made in collaboration with Wolfram.

Thank you,
Andi

Patches applied
===============
[1/1] i2c: hisi: Only handle the interrupt of the driver's transfer

      commit: 9a5adaf694f5ae8ba8f8e6178d01b5950fc7222a
Applied
#7 lore raw
In reply to Yicong Yang
Quoted 8 lines · Yicong Yang

From: Yicong Yang <[email protected]>

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

Applied to for-current, thanks! I updated the comment to mention that another port is likely the firmware. Similar like in the above text.

#8 lore raw

Hi Wolfram, Andi,

Quoted 8 lines · Wolfram Sang
Quoted 7 lines · Yicong Yang

From: Yicong Yang <[email protected]>

The controller may be shared with other port, for example the firmware. Handle the interrupt from other sources will cause crash since some data are not initialized. So only handle the interrupt of the driver's transfer and discard others.

Quoted 4 lines
Quoted 1 line

Applied to for-current, thanks! I updated the comment to mention that another port is likely the firmware. Similar like in the above text.

Today's renesas-drivers merge of i2c-host/i2c/andi-for-current got a conflict in:

drivers/i2c/busses/i2c-hisi.c

between commit fff67c1b17ee0939 ("i2c: hisi: Only handle the interrupt of the driver's transfer") in i2c/i2c/for-next and commit 9a5adaf694f5ae8b ("i2c: hisi: Only handle the interrupt of the driver's transfer") in i2c-host/i2c/andi-for-current.

I took the version from i2c/i2c/for-next, as that contained the extra comment.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds