Skip to contentSkip to conversation

i2c: qcom-cci: Fix NULL pointer dereference in cci_remove()

PATCH Applied Vladimir Zapolskiy 4 participants 13 messages May 15 – Jun 8

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

v2 cover i2c: qcom-cci: Fix NULL pointer dereference in cci_remove()

On all modern platforms Qualcomm CCI controller provides two I2C masters, and on particular boards only one I2C master may be initialized, and in such cases the device unbinding or driver removal causes a NULL pointer dereference, because cci_halt() is called for all I2C masters regardless of its initialized state, the bug is present in the driver since its inclusion to the Linux kernel.

Along with this fix a number of driver code simplifications are added into the series.

Changes from v1 to v2:
* remove junk accidentally added to one of the commit messages.

Link to v1:
* https://lore.kernel.org/linux-i2c/[email protected]

Vladimir Zapolskiy (4):
i2c: qcom-cci: Fix NULL pointer dereference in cci_remove()
i2c: qcom-cci: Do not check return value of cci_init()
i2c: qcom-cci: Move cci_init() under cci_reset() function
i2c: qcom-cci: Remove overcautious disable_irq() calls

 drivers/i2c/busses/i2c-qcom-cci.c | 50 +++++++++++++------------------
 1 file changed, 21 insertions(+), 29 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/4 i2c: qcom-cci: Fix NULL pointer dereference in cci_remove()

On all modern platforms Qualcomm CCI controller provides two I2C masters, and on particular boards only one I2C master may be initialized, and in such cases the device unbinding or driver removal causes a NULL pointer dereference, because cci_halt() is called for all two I2C masters, but a completion is initialized only for the single enabled master:

    % rmmod i2c-qcom-cci
    Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
    <snip>
    Call trace:
    __wait_for_common+0x194/0x1a8 (P)
    wait_for_completion_timeout+0x20/0x2c
    cci_remove+0xc4/0x138 [i2c_qcom_cci]
    platform_remove+0x20/0x30
    device_remove+0x4c/0x80
    device_release_driver_internal+0x1c8/0x224
    driver_detach+0x50/0x98
    bus_remove_driver+0x6c/0xbc
    driver_unregister+0x30/0x60
    platform_driver_unregister+0x14/0x20
    qcom_cci_driver_exit+0x18/0x1008 [i2c_qcom_cci]
    ....
Fixes e517526195de ("i2c: Add Qualcomm CCI I2C driver")
---
 drivers/i2c/busses/i2c-qcom-cci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-qcom-cci.c +1 -1
index f3ccfbbc4bea..01e440b6585d 100644
@@ -660,8 +660,8 @@ static void cci_remove(struct platform_device *pdev)
 		if (cci->master[i].cci) {
 			i2c_del_adapter(&cci->master[i].adap);
 			of_node_put(cci->master[i].adap.dev.of_node);
+			cci_halt(cci, i);
 		}
-		cci_halt(cci, i);
 	}
 
 	disable_irq(cci->irq);
Signature · 1 line
2.49.0
Reviewed-by on v2 1/4
#2 lore raw
Quoted 23 lines · Vladimir Zapolskiy

On all modern platforms Qualcomm CCI controller provides two I2C masters, and on particular boards only one I2C master may be initialized, and in such cases the device unbinding or driver removal causes a NULL pointer dereference, because cci_halt() is called for all two I2C masters, but a completion is initialized only for the single enabled master:

    % rmmod i2c-qcom-cci
    Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
    <snip>
    Call trace:
    __wait_for_common+0x194/0x1a8 (P)
    wait_for_completion_timeout+0x20/0x2c
    cci_remove+0xc4/0x138 [i2c_qcom_cci]
    platform_remove+0x20/0x30
    device_remove+0x4c/0x80
    device_release_driver_internal+0x1c8/0x224
    driver_detach+0x50/0x98
    bus_remove_driver+0x6c/0xbc
    driver_unregister+0x30/0x60
    platform_driver_unregister+0x14/0x20
    qcom_cci_driver_exit+0x18/0x1008 [i2c_qcom_cci]
    ....
Quoted 3 lines
Fixes e517526195de ("i2c: Add Qualcomm CCI I2C driver")

---

FYI I think the driver currently can't really cope with master0 being not registered

Konrad

#3 lore raw

PATCH v2 2/4 i2c: qcom-cci: Do not check return value of cci_init()

In reply to Vladimir Zapolskiy

The cci_init() function is not supposed to fail, and it never returns a non-zero, so it'd make sense to convert its signature to void.

---
 drivers/i2c/busses/i2c-qcom-cci.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-qcom-cci.c +2 -6
index 01e440b6585d..be41a53e30c6 100644
@@ -243,7 +243,7 @@ static int cci_reset(struct cci *cci)
 	return 0;
 }
 
-static int cci_init(struct cci *cci)
+static void cci_init(struct cci *cci)
 {
 	u32 val = CCI_IRQ_MASK_0_I2C_M0_RD_DONE |
 			CCI_IRQ_MASK_0_I2C_M0_Q0_REPORT |
@@ -284,8 +284,6 @@ static int cci_init(struct cci *cci)
 		val = hw->scl_stretch_en << 8 | hw->trdhld << 4 | hw->tsp;
 		writel(val, cci->base + CCI_I2C_Mm_MISC_CTL(i));
 	}
-
-	return 0;
 }
 
 static int cci_run_queue(struct cci *cci, u8 master, u8 queue)
@@ -611,9 +609,7 @@ static int cci_probe(struct platform_device *pdev)
 	if (ret < 0)
 		goto error;
 
-	ret = cci_init(cci);
-	if (ret < 0)
-		goto error;
+	cci_init(cci);
 
 	pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
 	pm_runtime_use_autosuspend(dev);
Signature · 1 line
2.49.0
maintainer Reviewed-by on v2 2/4
#5 lore raw

Re: [PATCH v2 2/4] i2c: qcom-cci: Do not check return value of cci_init()

In reply to Vladimir Zapolskiy
Quoted 5 lines · Vladimir Zapolskiy

The cci_init() function is not supposed to fail, and it never returns a non-zero, so it'd make sense to convert its signature to void.

Quoted 40 lines
---
 drivers/i2c/busses/i2c-qcom-cci.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-qcom-cci.c +2 -7
index 01e440b6585d..be41a53e30c6 100644
@@ -243,7 +243,7 @@ static int cci_reset(struct cci *cci)
        return 0;
 }
 
-static int cci_init(struct cci *cci)
+static void cci_init(struct cci *cci)
 {
        u32 val = CCI_IRQ_MASK_0_I2C_M0_RD_DONE |
                        CCI_IRQ_MASK_0_I2C_M0_Q0_REPORT |
@@ -284,8 +284,6 @@ static int cci_init(struct cci *cci)
                val = hw->scl_stretch_en << 8 | hw->trdhld << 4 | hw->tsp;
                writel(val, cci->base + CCI_I2C_Mm_MISC_CTL(i));
        }
-
-       return 0;
 }
 
 static int cci_run_queue(struct cci *cci, u8 master, u8 queue)
@@ -611,9 +609,7 @@ static int cci_probe(struct platform_device *pdev)
        if (ret < 0)
                goto error;
 
-       ret = cci_init(cci);
-       if (ret < 0)
-               goto error;
+       cci_init(cci);
 
        pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
        pm_runtime_use_autosuspend(dev);
--

2.49.0

#6 lore raw

PATCH v2 3/4 i2c: qcom-cci: Move cci_init() under cci_reset() function

In reply to Vladimir Zapolskiy

On probe or runtime errors cci_reset() is called and it should be coupled with cci_init(), instead of doing this on caller's side, embed cci_init() directly into the cci_reset() function.

This is a non-functional change, cci_reset() and cci_init() function bodies are reordered.

---
 drivers/i2c/busses/i2c-qcom-cci.c | 41 +++++++++++++++----------------
 1 file changed, 20 insertions(+), 21 deletions(-)
drivers/i2c/busses/i2c-qcom-cci.c +20 -21
index be41a53e30c6..2d73903f14d3 100644
@@ -225,24 +225,6 @@ static int cci_halt(struct cci *cci, u8 master_num)
 	return 0;
 }
 
-static int cci_reset(struct cci *cci)
-{
-	/*
-	 * we reset the whole controller, here and for implicity use
-	 * master[0].xxx for waiting on it.
-	 */
-	reinit_completion(&cci->master[0].irq_complete);
-	writel(CCI_RESET_CMD_MASK, cci->base + CCI_RESET_CMD);
-
-	if (!wait_for_completion_timeout(&cci->master[0].irq_complete,
-					 CCI_TIMEOUT)) {
-		dev_err(cci->dev, "CCI reset timeout\n");
-		return -ETIMEDOUT;
-	}
-
-	return 0;
-}
-
 static void cci_init(struct cci *cci)
 {
 	u32 val = CCI_IRQ_MASK_0_I2C_M0_RD_DONE |
@@ -286,6 +268,26 @@ static void cci_init(struct cci *cci)
 	}
 }
 
+static int cci_reset(struct cci *cci)
+{
+	/*
+	 * we reset the whole controller, here and for implicity use
+	 * master[0].xxx for waiting on it.
+	 */
+	reinit_completion(&cci->master[0].irq_complete);
+	writel(CCI_RESET_CMD_MASK, cci->base + CCI_RESET_CMD);
+
+	if (!wait_for_completion_timeout(&cci->master[0].irq_complete,
+					 CCI_TIMEOUT)) {
+		dev_err(cci->dev, "CCI reset timeout\n");
+		return -ETIMEDOUT;
+	}
+
+	cci_init(cci);
+
+	return 0;
+}
+
 static int cci_run_queue(struct cci *cci, u8 master, u8 queue)
 {
 	u32 val;
@@ -302,7 +304,6 @@ static int cci_run_queue(struct cci *cci, u8 master, u8 queue)
 		dev_err(cci->dev, "master %d queue %d timeout\n",
 			master, queue);
 		cci_reset(cci);
-		cci_init(cci);
 		return -ETIMEDOUT;
 	}
 
@@ -609,8 +610,6 @@ static int cci_probe(struct platform_device *pdev)
 	if (ret < 0)
 		goto error;
 
-	cci_init(cci);
-
 	pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_set_active(dev);
Signature · 1 line
2.49.0
Reviewed-by on v2 3/4
#7 lore raw

Re: [PATCH v2 3/4] i2c: qcom-cci: Move cci_init() under cci_reset() function

Quoted 9 lines · Vladimir Zapolskiy

On probe or runtime errors cci_reset() is called and it should be coupled with cci_init(), instead of doing this on caller's side, embed cci_init() directly into the cci_reset() function.

This is a non-functional change, cci_reset() and cci_init() function bodies are reordered.

---

Konrad

maintainer Reviewed-by on v2 3/4
#8 lore raw

Re: [PATCH v2 3/4] i2c: qcom-cci: Move cci_init() under cci_reset() function

In reply to Vladimir Zapolskiy
Quoted 9 lines · Vladimir Zapolskiy

On probe or runtime errors cci_reset() is called and it should be coupled with cci_init(), instead of doing this on caller's side, embed cci_init() directly into the cci_reset() function.

This is a non-functional change, cci_reset() and cci_init() function bodies are reordered.

Quoted 80 lines
---
 drivers/i2c/busses/i2c-qcom-cci.c | 41 +++++++++++++++----------------
 1 file changed, 20 insertions(+), 21 deletions(-)
drivers/i2c/busses/i2c-qcom-cci.c +20 -22
index be41a53e30c6..2d73903f14d3 100644
@@ -225,24 +225,6 @@ static int cci_halt(struct cci *cci, u8 master_num)
        return 0;
 }
 
-static int cci_reset(struct cci *cci)
-{
-       /*
-        * we reset the whole controller, here and for implicity use
-        * master[0].xxx for waiting on it.
-        */
-       reinit_completion(&cci->master[0].irq_complete);
-       writel(CCI_RESET_CMD_MASK, cci->base + CCI_RESET_CMD);
-
-       if (!wait_for_completion_timeout(&cci->master[0].irq_complete,
-                                        CCI_TIMEOUT)) {
-               dev_err(cci->dev, "CCI reset timeout\n");
-               return -ETIMEDOUT;
-       }
-
-       return 0;
-}
-
 static void cci_init(struct cci *cci)
 {
        u32 val = CCI_IRQ_MASK_0_I2C_M0_RD_DONE |
@@ -286,6 +268,26 @@ static void cci_init(struct cci *cci)
        }
 }
 
+static int cci_reset(struct cci *cci)
+{
+       /*
+        * we reset the whole controller, here and for implicity use
+        * master[0].xxx for waiting on it.
+        */
+       reinit_completion(&cci->master[0].irq_complete);
+       writel(CCI_RESET_CMD_MASK, cci->base + CCI_RESET_CMD);
+
+       if (!wait_for_completion_timeout(&cci->master[0].irq_complete,
+                                        CCI_TIMEOUT)) {
+               dev_err(cci->dev, "CCI reset timeout\n");
+               return -ETIMEDOUT;
+       }
+
+       cci_init(cci);
+
+       return 0;
+}
+
 static int cci_run_queue(struct cci *cci, u8 master, u8 queue)
 {
        u32 val;
@@ -302,7 +304,6 @@ static int cci_run_queue(struct cci *cci, u8 master, u8 queue)
                dev_err(cci->dev, "master %d queue %d timeout\n",
                        master, queue);
                cci_reset(cci);
-               cci_init(cci);
                return -ETIMEDOUT;
        }
 
@@ -609,8 +610,6 @@ static int cci_probe(struct platform_device *pdev)
        if (ret < 0)
                goto error;
 
-       cci_init(cci);
-
        pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
        pm_runtime_use_autosuspend(dev);
        pm_runtime_set_active(dev);
--

2.49.0

#9 lore raw

PATCH v2 4/4 i2c: qcom-cci: Remove overcautious disable_irq() calls

In reply to Vladimir Zapolskiy

In cci_probe() the controller's interrupt is requested using a devres managed API, and in cci_probe() error path and cci_remove() it'd be safe to rely on devres mechanism to free and shutdown the interrupt, thus explicit disable_irq() calls can be removed as unnecessary ones.

---
 drivers/i2c/busses/i2c-qcom-cci.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/i2c/busses/i2c-qcom-cci.c +1 -4
index 2d73903f14d3..4d64895a9e9e 100644
@@ -608,7 +608,7 @@ static int cci_probe(struct platform_device *pdev)
 
 	ret = cci_reset(cci);
 	if (ret < 0)
-		goto error;
+		goto disable_clocks;
 
 	pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
 	pm_runtime_use_autosuspend(dev);
@@ -638,8 +638,6 @@ static int cci_probe(struct platform_device *pdev)
 			of_node_put(cci->master[i].adap.dev.of_node);
 		}
 	}
-error:
-	disable_irq(cci->irq);
 disable_clocks:
 	cci_disable_clocks(cci);
 
@@ -659,7 +657,6 @@ static void cci_remove(struct platform_device *pdev)
 		}
 	}
 
-	disable_irq(cci->irq);
 	pm_runtime_disable(&pdev->dev);
 	pm_runtime_set_suspended(&pdev->dev);
 }
Signature · 1 line
2.49.0
Reviewed-by on v2 4/4
#10 lore raw

Re: [PATCH v2 4/4] i2c: qcom-cci: Remove overcautious disable_irq() calls

Quoted 7 lines · Vladimir Zapolskiy

In cci_probe() the controller's interrupt is requested using a devres managed API, and in cci_probe() error path and cci_remove() it'd be safe to rely on devres mechanism to free and shutdown the interrupt, thus explicit disable_irq() calls can be removed as unnecessary ones.

---

I think disbling happens for all IRQs if this was the only/last user, regardless of whether its requested with a devm_ helper

Konrad

maintainer Reviewed-by on v2 4/4
#11 lore raw

Re: [PATCH v2 4/4] i2c: qcom-cci: Remove overcautious disable_irq() calls

In reply to Vladimir Zapolskiy
Quoted 7 lines · Vladimir Zapolskiy

In cci_probe() the controller's interrupt is requested using a devres managed API, and in cci_probe() error path and cci_remove() it'd be safe to rely on devres mechanism to free and shutdown the interrupt, thus explicit disable_irq() calls can be removed as unnecessary ones.

Quoted 37 lines
---
 drivers/i2c/busses/i2c-qcom-cci.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/i2c/busses/i2c-qcom-cci.c +1 -5
index 2d73903f14d3..4d64895a9e9e 100644
@@ -608,7 +608,7 @@ static int cci_probe(struct platform_device *pdev)
 
        ret = cci_reset(cci);
        if (ret < 0)
-               goto error;
+               goto disable_clocks;
 
        pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
        pm_runtime_use_autosuspend(dev);
@@ -638,8 +638,6 @@ static int cci_probe(struct platform_device *pdev)
                        of_node_put(cci->master[i].adap.dev.of_node);
                }
        }
-error:
-       disable_irq(cci->irq);
 disable_clocks:
        cci_disable_clocks(cci);
 
@@ -659,7 +657,6 @@ static void cci_remove(struct platform_device *pdev)
                }
        }
 
-       disable_irq(cci->irq);
        pm_runtime_disable(&pdev->dev);
        pm_runtime_set_suspended(&pdev->dev);
 }
--

2.49.0

maintainer Applied
#12 lore raw
In reply to Vladimir Zapolskiy

Hi Vladimir,

Quoted 10 lines · Vladimir Zapolskiy

On all modern platforms Qualcomm CCI controller provides two I2C masters, and on particular boards only one I2C master may be initialized, and in such cases the device unbinding or driver removal causes a NULL pointer dereference, because cci_halt() is called for all I2C masters regardless of its initialized state, the bug is present in the driver since its inclusion to the Linux kernel.

Along with this fix a number of driver code simplifications are added into the series.

Quoted 8 lines

Changes from v1 to v2:
* remove junk accidentally added to one of the commit messages.

Link to v1:
* https://lore.kernel.org/linux-i2c/[email protected]

Vladimir Zapolskiy (4):
i2c: qcom-cci: Fix NULL pointer dereference in cci_remove()

merged to i2c/i2c-host-fixes.

Quoted 3 lines

i2c: qcom-cci: Do not check return value of cci_init()
i2c: qcom-cci: Move cci_init() under cci_reset() function
i2c: qcom-cci: Remove overcautious disable_irq() calls

merged to i2c/i2c-host-next.

Thanks,
Andi