i2c: qcom-cci: Fix NULL pointer dereference in cci_remove()
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
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]
....--- drivers/i2c/busses/i2c-qcom-cci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-qcom-cci.c +1 -1
@@ -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
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")Signed-off-by Vladimir Zapolskiy <[email protected]>---
FYI I think the driver currently can't really cope with master0 being not registered
Konrad
PATCH v2 2/4 i2c: qcom-cci: Do not check return value of cci_init()
In reply to Vladimir ZapolskiyThe 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
@@ -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
Re: [PATCH v2 2/4] i2c: qcom-cci: Do not check return value of cci_init()
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.Signed-off-by Vladimir Zapolskiy <[email protected]>---
Konrad
Re: [PATCH v2 2/4] i2c: qcom-cci: Do not check return value of cci_init()
In reply to Vladimir ZapolskiyQuoted 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.Signed-off-by Vladimir Zapolskiy <[email protected]>
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@@ -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
PATCH v2 3/4 i2c: qcom-cci: Move cci_init() under cci_reset() function
In reply to Vladimir ZapolskiyOn 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
@@ -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
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 withcci_init(), instead of doing this on caller's side, embedcci_init()directly into thecci_reset()function.This is a non-functional change,
cci_reset()andcci_init()function bodies are reordered.Signed-off-by Vladimir Zapolskiy <[email protected]>---
Konrad
Re: [PATCH v2 3/4] i2c: qcom-cci: Move cci_init() under cci_reset() function
In reply to Vladimir ZapolskiyQuoted 9 lines · Vladimir Zapolskiy
On probe or runtime errors
cci_reset()is called and it should be coupled withcci_init(), instead of doing this on caller's side, embedcci_init()directly into thecci_reset()function.This is a non-functional change,
cci_reset()andcci_init()function bodies are reordered.Signed-off-by Vladimir Zapolskiy <[email protected]>
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@@ -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
PATCH v2 4/4 i2c: qcom-cci: Remove overcautious disable_irq() calls
In reply to Vladimir ZapolskiyIn 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
@@ -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
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 incci_probe()error path andcci_remove()it'd be safe to rely on devres mechanism to free and shutdown the interrupt, thus explicitdisable_irq()calls can be removed as unnecessary ones.Signed-off-by Vladimir Zapolskiy <[email protected]>---
I think disbling happens for all IRQs if this was the only/last user, regardless of whether its requested with a devm_ helper
Konrad
Re: [PATCH v2 4/4] i2c: qcom-cci: Remove overcautious disable_irq() calls
In reply to Vladimir ZapolskiyQuoted 7 lines · Vladimir Zapolskiy
In
cci_probe()the controller's interrupt is requested using a devres managed API, and incci_probe()error path andcci_remove()it'd be safe to rely on devres mechanism to free and shutdown the interrupt, thus explicitdisable_irq()calls can be removed as unnecessary ones.Signed-off-by Vladimir Zapolskiy <[email protected]>
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@@ -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
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 incci_remove()
merged to i2c/i2c-host-fixes.
Quoted 3 lines
i2c: qcom-cci: Do not check return value of
cci_init()
i2c: qcom-cci: Movecci_init()undercci_reset()function
i2c: qcom-cci: Remove overcautiousdisable_irq()calls
merged to i2c/i2c-host-next.
Thanks,
Andi