Skip to contentSkip to conversation

i2c: exynos5: Init data before registering interrupt handler

PATCH Applied Jesper Nilsson 5 participants 7 messages Mar 5, 2024 – Mar 7, 2024

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

PATCH v3 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.

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

---
Changes in v3:
- Avoid multiple assignment
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Use dev_err_probe() instead of open coding it
- Dropped the return failure if we can't find a match in devicetree
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/i2c/busses/i2c-exynos5.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-exynos5.c +15 -14
index 385ef9d9e4d4..8458e22313a7 100644
@@ -906,23 +906,9 @@ 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);
 
 	ret = exynos5_hsi2c_clock_setup(i2c);
@@ -940,6 +926,21 @@ 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);
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		goto err_clk;
+	i2c->irq = ret;
+
+	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 Applied
#1 lore raw

Hi

Quoted 9 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.

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

[...]

Applied to i2c/i2c-host-fixes on

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

Thank you,
Andi

Patches applied
===============
[1/1] i2c: exynos5: Init data before registering interrupt handler

      commit: 51130d52b84c473f5da5378aa7e7633611f79313

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#2 lore raw
Quoted 8 lines · Andi Shyti

Hi

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
Quoted 4 lines

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

[...]

Applied to i2c/i2c-host-fixes on

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

If for any reason this is going to fixes, which marks its importance as fix, then it should go to stable as well. Please always cc-stable for such cases.

Best regards,
Krzysztof

maintainer
#3 lore raw

Hi Krzysztof,

Quoted 14 lines · Krzysztof Kozlowski
Quoted 13 lines · Andi Shyti

Hi

Quoted 9 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.

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

[...]

Quoted 7 lines
Quoted 3 lines

Applied to i2c/i2c-host-fixes on

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

If for any reason this is going to fixes, which marks its importance as fix, then it should go to stable as well. Please always cc-stable for such cases.

yes, thank you! I should have checked that, as well.

Andi

#4 lore raw
In reply to Jesper Nilsson
Quoted 59 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.

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.

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

---
Changes in v3:
- Avoid multiple assignment
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Use dev_err_probe() instead of open coding it
- Dropped the return failure if we can't find a match in devicetree
- Link to v1: https://lore.kernel.org/r/[email protected]

---
  drivers/i2c/busses/i2c-exynos5.c | 29 +++++++++++++++--------------
  1 file changed, 15 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-exynos5.c +0 -14
index 385ef9d9e4d4..8458e22313a7 100644
@@ -906,23 +906,9 @@ 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);
 
  	ret = exynos5_hsi2c_clock_setup(i2c);

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

Quoted 5 lines
drivers/i2c/busses/i2c-exynos5.c +2 -0
@@ -940,6 +926,21 @@ 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);

Just above this call the clocks have been disabled, so any access to the i2c host registers will result in freeze or external abort (depending on the soc/cpu).

To make things worse, this patch moved registering the interrupt handler after the i2c_add_adapter() call. This means that all i2c devices that will be probbed directly from i2c_add_adapter() won't be able to access the i2c bus, as the host controller is still not fully functional that time yet.

This breaks today's linux-next on all Exynos5+ platforms. Has anyone tested this change?

Quoted 7 lines
drivers/i2c/busses/i2c-exynos5.c +6 -0
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		goto err_clk;
+	i2c->irq = ret;
+

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

Quoted 10 lines
drivers/i2c/busses/i2c-exynos5.c +7 -0
+	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:

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#5 lore raw
Quoted 70 lines · Marek Szyprowski
Quoted 59 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.

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.

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

---
Changes in v3:
- Avoid multiple assignment
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Use dev_err_probe() instead of open coding it
- Dropped the return failure if we can't find a match in devicetree
- Link to v1: https://lore.kernel.org/r/[email protected]

---
  drivers/i2c/busses/i2c-exynos5.c | 29 +++++++++++++++--------------
  1 file changed, 15 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-exynos5.c +0 -14
index 385ef9d9e4d4..8458e22313a7 100644
@@ -906,23 +906,9 @@ 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);
 
  	ret = exynos5_hsi2c_clock_setup(i2c);

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

Quoted 5 lines
drivers/i2c/busses/i2c-exynos5.c +2 -0
@@ -940,6 +926,21 @@ 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);

Just above this call the clocks have been disabled, so any access to the i2c host registers will result in freeze or external abort (depending on the soc/cpu).

Quoted 8 lines

To make things worse, this patch moved registering the interrupt handler after the i2c_add_adapter() call. This means that all i2c devices that will be probbed directly from i2c_add_adapter() won't be able to access the i2c bus, as the host controller is still not fully functional that time yet.

This breaks today's linux-next on all Exynos5+ platforms. Has anyone tested this change?

I don't think so. So that's the reason my boards fail on today's next/master and next/pending-fixes.

Untested code should not be send as fixes :/

Thanks for reporting Marek (and saving me some bisecting).

Best regards,
Krzysztof

#6 lore raw
In reply to Marek Szyprowski

Hi Marek,

Am 07.03.24 um 12:41 schrieb Marek Szyprowski:

Quoted 70 lines · Jesper Nilsson
Quoted 59 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.

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.

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

---
Changes in v3:
- Avoid multiple assignment
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Use dev_err_probe() instead of open coding it
- Dropped the return failure if we can't find a match in devicetree
- Link to v1: https://lore.kernel.org/r/[email protected]

---
  drivers/i2c/busses/i2c-exynos5.c | 29 +++++++++++++++--------------
  1 file changed, 15 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-exynos5.c +0 -14
index 385ef9d9e4d4..8458e22313a7 100644
@@ -906,23 +906,9 @@ 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);
 
  	ret = exynos5_hsi2c_clock_setup(i2c);

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

Quoted 5 lines
drivers/i2c/busses/i2c-exynos5.c +2 -0
@@ -940,6 +926,21 @@ 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);

Just above this call the clocks have been disabled, so any access to the i2c host registers will result in freeze or external abort (depending on the soc/cpu).

Quoted 8 lines

To make things worse, this patch moved registering the interrupt handler after the i2c_add_adapter() call. This means that all i2c devices that will be probbed directly from i2c_add_adapter() won't be able to access the i2c bus, as the host controller is still not fully functional that time yet.

This breaks today's linux-next on all Exynos5+ platforms. Has anyone tested this change?

Tested this patch on top of stable 6.7.9, breaks booting odroid-xu4.

Gruß,
--
Markus

Quoted 16 lines
Quoted 16 lines
drivers/i2c/busses/i2c-exynos5.c +13 -0
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		goto err_clk;
+	i2c->irq = ret;
+
+	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;
 
Quoted 3 lines

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

Quoted 2 lines
drivers/i2c/busses/i2c-exynos5.c +0 -0
   err_clk:

Best regards

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel