Skip to contentSkip to conversation

i2c: exynos5: Init data before registering interrupt handler

PATCH Superseded Jesper Nilsson 3 participants 5 messages Mar 4, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: exynos5: Init data before registering interrupt handler

devm_request_irq() is called before we initialize the "variant" member variable from of_device_get_match_data(), so if an interrupt is triggered inbetween, we can end up following a NULL pointer in the interrupt handler.

This problem was exposed when the I2C controller in question was (mis)configured to be used in both secure world and Linux.

That this can happen is also reflected by the existing code that clears any pending interrupts from "u-boot or misc causes".

Move the clearing of pending interrupts and the call to devm_request_irq() to the end of probe.
Additionally, return failure if we can't find a match in devicetree.

---
 drivers/i2c/busses/i2c-exynos5.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-exynos5.c +18 -14
index 385ef9d9e4d4..eba717e5cad7 100644
@@ -906,24 +906,14 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
 	i2c->adap.algo_data = i2c;
 	i2c->adap.dev.parent = &pdev->dev;
 
-	/* Clear pending interrupts from u-boot or misc causes */
-	exynos5_i2c_clr_pend_irq(i2c);
-
 	spin_lock_init(&i2c->lock);
 	init_completion(&i2c->msg_complete);
 
-	i2c->irq = ret = platform_get_irq(pdev, 0);
-	if (ret < 0)
-		goto err_clk;
-
-	ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
-			       IRQF_NO_SUSPEND, dev_name(&pdev->dev), i2c);
-	if (ret != 0) {
-		dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
-		goto err_clk;
-	}
-
 	i2c->variant = of_device_get_match_data(&pdev->dev);
+	if (!i2c->variant) {
+		dev_err(&pdev->dev, "can't match device variant\n");
+		return -ENODEV;
+	}
 
 	ret = exynos5_hsi2c_clock_setup(i2c);
 	if (ret)
@@ -940,6 +930,20 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
 	clk_disable(i2c->clk);
 	clk_disable(i2c->pclk);
 
+	/* Clear pending interrupts from u-boot or misc causes */
+	exynos5_i2c_clr_pend_irq(i2c);
+
+	i2c->irq = ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		goto err_clk;
+
+	ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
+			       IRQF_NO_SUSPEND, dev_name(&pdev->dev), i2c);
+	if (ret != 0) {
+		dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
+		goto err_clk;
+	}
+
 	return 0;
 
  err_clk:

---
base-commit: 0dd3ee31125508cd67f7e7172247f05b7fd1753a
change-id: 20240228-i2c_exynos5-db13a72eec8b

Best regards,

Signature · 3 lines
/^JN - Jesper Nilsson
-- 
               Jesper Nilsson -- [email protected]
maintainer
#1 lore raw

Hi Jesper,

Quoted 5 lines · Jesper Nilsson

devm_request_irq() is called before we initialize the "variant" member variable from of_device_get_match_data(), so if an interrupt is triggered inbetween, we can end up following a NULL pointer in the interrupt handler.

Quoted 8 lines

This problem was exposed when the I2C controller in question was (mis)configured to be used in both secure world and Linux.

That this can happen is also reflected by the existing code that clears any pending interrupts from "u-boot or misc causes".

Move the clearing of pending interrupts and the call to devm_request_irq() to the end of probe.

I'm OK with moving the irq request at the end and I'm going to give my r-b anyway. There is still one comment below.

Quoted 3 lines

Additionally, return failure if we can't find a match in devicetree.

The way you are describing it you would need the Fixes tag here and this patch should be treated as a fix.

Nevertheless, I think that it's odd that the device is sending interrupts at this phase and the real fix should be preventing the controller to send interrupts here.

How have you tested this patch?

Quoted 23 lines
---
 drivers/i2c/busses/i2c-exynos5.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-exynos5.c +0 -8
index 385ef9d9e4d4..eba717e5cad7 100644
@@ -906,24 +906,14 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
 	i2c->adap.algo_data = i2c;
 	i2c->adap.dev.parent = &pdev->dev;
 
-	/* Clear pending interrupts from u-boot or misc causes */
-	exynos5_i2c_clr_pend_irq(i2c);
-
 	spin_lock_init(&i2c->lock);
 	init_completion(&i2c->msg_complete);
 
-	i2c->irq = ret = platform_get_irq(pdev, 0);
-	if (ret < 0)
-		goto err_clk;
-
-	ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,

drivers/i2c/busses/i2c-exynos5.c static int exynos5_i2c_probe(struct platform_device *pdev) · line 915

Quoted 10 lines
drivers/i2c/busses/i2c-exynos5.c +3 -6
-			       IRQF_NO_SUSPEND, dev_name(&pdev->dev), i2c);
-	if (ret != 0) {
-		dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
-		goto err_clk;
-	}
-
 	i2c->variant = of_device_get_match_data(&pdev->dev);
+	if (!i2c->variant) {
+		dev_err(&pdev->dev, "can't match device variant\n");
+		return -ENODEV;

return dev_err_probe(), please.

Andi

#2 lore raw
Quoted 7 lines · Andi Shyti

drivers/i2c/busses/i2c-exynos5.c static int exynos5_i2c_probe(struct platform_device *pdev) · line 915

Quoted 5 lines
drivers/i2c/busses/i2c-exynos5.c +3 -1
-
 	i2c->variant = of_device_get_match_data(&pdev->dev);
+	if (!i2c->variant) {
+		dev_err(&pdev->dev, "can't match device variant\n");
+		return -ENODEV;

return dev_err_probe(), please.

How this condition even possibly happen? And how is this related to the problem described here?

No, don't mix two different issues.

Best regards,
Krzysztof

#4 lore raw
In reply to Andi Shyti
Quoted 1 line · Andi Shyti

Hi Jesper,

Hi Andi,

Quoted 9 lines · Jesper Nilsson
Quoted 8 lines

devm_request_irq() is called before we initialize the "variant" member variable from of_device_get_match_data(), so if an interrupt is triggered inbetween, we can end up following a NULL pointer in the interrupt handler.

This problem was exposed when the I2C controller in question was (mis)configured to be used in both secure world and Linux.

Quoted 8 lines
Quoted 5 lines

That this can happen is also reflected by the existing code that clears any pending interrupts from "u-boot or misc causes".

Move the clearing of pending interrupts and the call to devm_request_irq() to the end of probe.

I'm OK with moving the irq request at the end and I'm going to give my r-b anyway. There is still one comment below.

Thanks.

Quoted 6 lines
Quoted 3 lines

Additionally, return failure if we can't find a match in devicetree.

The way you are describing it you would need the Fixes tag here and this patch should be treated as a fix.

Ok, the variant member was introduced in:

218e1496135e ("i2c: exynos5: add support for HSI2C on Exynos5260 SoC")

Before that, the ordering didn't matter.

Quoted 3 lines

Nevertheless, I think that it's odd that the device is sending interrupts at this phase and the real fix should be preventing the controller to send interrupts here.

I found this bug when we moved control of an I2C bus into secure world (OP-TEE) and hadn't removed it from devicetree config. Since we hadn't enabled the secure parts yet, the kernel could still get the interrupt, even when OP-TEE was performing I2C transfers.

Quoted 1 line

How have you tested this patch?

This was tested in our ARTPEC-8 SoC, which includes the same IP the exynos SoCs, both with and without the problematic configuration described above.

Quoted 27 lines
Quoted 27 lines
---
 drivers/i2c/busses/i2c-exynos5.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-exynos5.c +0 -12
index 385ef9d9e4d4..eba717e5cad7 100644
@@ -906,24 +906,14 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
 	i2c->adap.algo_data = i2c;
 	i2c->adap.dev.parent = &pdev->dev;
 
-	/* Clear pending interrupts from u-boot or misc causes */
-	exynos5_i2c_clr_pend_irq(i2c);
-
 	spin_lock_init(&i2c->lock);
 	init_completion(&i2c->msg_complete);
 
-	i2c->irq = ret = platform_get_irq(pdev, 0);
-	if (ret < 0)
-		goto err_clk;
-
-	ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
-			       IRQF_NO_SUSPEND, dev_name(&pdev->dev), i2c);
-	if (ret != 0) {
-		dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
-		goto err_clk;
Quoted 8 lines

drivers/i2c/busses/i2c-exynos5.c static int exynos5_i2c_probe(struct platform_device *pdev) · line 915

Quoted 6 lines
drivers/i2c/busses/i2c-exynos5.c +3 -2
-	}
-
 	i2c->variant = of_device_get_match_data(&pdev->dev);
+	if (!i2c->variant) {
+		dev_err(&pdev->dev, "can't match device variant\n");
+		return -ENODEV;

return dev_err_probe(), please.

Will do.

Quoted 1 line

Andi

/^JN - Jesper Nilsson

Signature · 1 line
               Jesper Nilsson -- [email protected]
#3 lore raw
In reply to Krzysztof Kozlowski
Quoted 11 lines · Krzysztof Kozlowski
Quoted 7 lines · Andi Shyti

drivers/i2c/busses/i2c-exynos5.c static int exynos5_i2c_probe(struct platform_device *pdev) · line 915

Quoted 5 lines
drivers/i2c/busses/i2c-exynos5.c +3 -1
-
 	i2c->variant = of_device_get_match_data(&pdev->dev);
+	if (!i2c->variant) {
+		dev_err(&pdev->dev, "can't match device variant\n");
+		return -ENODEV;

return dev_err_probe(), please.

How this condition even possibly happen? And how is this related to the problem described here?

No, it was not. That was part of the debugging before we understood the real problem.

Quoted 1 line

No, don't mix two different issues.

No problem, I can drop this part.

Quoted 2 lines

Best regards,
Krzysztof

/^JN - Jesper Nilsson

Signature · 1 line
               Jesper Nilsson -- [email protected]