2023-04-03 10:45:05

by Kang Chen

[permalink] [raw]
Subject: [PATCH] drivers: thermal: mediatek: fix of_iomap leak in mtk_thermal_probe

Smatch reports:
1. mtk_thermal_probe() warn: 'apmixed_base' from of_iomap() not released.
2. mtk_thermal_probe() warn: 'auxadc_base' from of_iomap() not released.

The original code forgets to release iomap resource when handling errors,
fix it through unified error handling code at the end of mtk_thermal_probe.

Fixes: 89945047b166 ("thermal: mediatek: Add tsensor support for V2 thermal system")
Signed-off-by: Kang Chen <[email protected]>
---
I think the this should be released on the success path but I was too
scared of breaking things. Let me know and I will resend if people want
me to do that.

drivers/thermal/mediatek/auxadc_thermal.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/thermal/mediatek/auxadc_thermal.c b/drivers/thermal/mediatek/auxadc_thermal.c
index ab730f9552d0..741c7d8151bd 100644
--- a/drivers/thermal/mediatek/auxadc_thermal.c
+++ b/drivers/thermal/mediatek/auxadc_thermal.c
@@ -1149,13 +1149,15 @@ static int mtk_thermal_probe(struct platform_device *pdev)

if (auxadc_phys_base == OF_BAD_ADDR) {
dev_err(&pdev->dev, "Can't get auxadc phys address\n");
- return -EINVAL;
+ ret = -EINVAL;
+ goto err_iounmap_auxadc;
}

apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
if (!apmixedsys) {
dev_err(&pdev->dev, "missing apmixedsys node\n");
- return -ENODEV;
+ ret = -ENODEV;
+ goto err_iounmap_auxadc;
}

apmixed_base = of_iomap(apmixedsys, 0);
@@ -1165,17 +1167,18 @@ static int mtk_thermal_probe(struct platform_device *pdev)

if (apmixed_phys_base == OF_BAD_ADDR) {
dev_err(&pdev->dev, "Can't get auxadc phys address\n");
- return -EINVAL;
+ ret = -EINVAL;
+ goto err_iounmap_apmixed;
}

ret = device_reset_optional(&pdev->dev);
if (ret)
- return ret;
+ goto err_iounmap_apmixed;

ret = clk_prepare_enable(mt->clk_auxadc);
if (ret) {
dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
- return ret;
+ goto err_iounmap_apmixed;
}

ret = clk_prepare_enable(mt->clk_peri_therm);
@@ -1220,7 +1223,10 @@ static int mtk_thermal_probe(struct platform_device *pdev)
clk_disable_unprepare(mt->clk_peri_therm);
err_disable_clk_auxadc:
clk_disable_unprepare(mt->clk_auxadc);
-
+err_iounmap_apmixed:
+ iounmap(apmixed_base);
+err_iounmap_auxadc:
+ iounmap(auxadc_base);
return ret;
}

--
2.34.1


2023-04-03 11:39:41

by Daniel Lezcano

[permalink] [raw]
Subject: Re: [PATCH] drivers: thermal: mediatek: fix of_iomap leak in mtk_thermal_probe

On 03/04/2023 12:38, Kang Chen wrote:
> Smatch reports:
> 1. mtk_thermal_probe() warn: 'apmixed_base' from of_iomap() not released.
> 2. mtk_thermal_probe() warn: 'auxadc_base' from of_iomap() not released.
>
> The original code forgets to release iomap resource when handling errors,
> fix it through unified error handling code at the end of mtk_thermal_probe.
>
> Fixes: 89945047b166 ("thermal: mediatek: Add tsensor support for V2 thermal system")
> Signed-off-by: Kang Chen <[email protected]>
> ---
> I think the this should be released on the success path but I was too
> scared of breaking things. Let me know and I will resend if people want
> me to do that.
>
> drivers/thermal/mediatek/auxadc_thermal.c | 18 ++++++++++++------
> 1 file changed, 12 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/thermal/mediatek/auxadc_thermal.c b/drivers/thermal/mediatek/auxadc_thermal.c
> index ab730f9552d0..741c7d8151bd 100644
> --- a/drivers/thermal/mediatek/auxadc_thermal.c
> +++ b/drivers/thermal/mediatek/auxadc_thermal.c
> @@ -1149,13 +1149,15 @@ static int mtk_thermal_probe(struct platform_device *pdev)
>
> if (auxadc_phys_base == OF_BAD_ADDR) {
> dev_err(&pdev->dev, "Can't get auxadc phys address\n");
> - return -EINVAL;
> + ret = -EINVAL;
> + goto err_iounmap_auxadc;
> }

Better to use the devm_of_iomap() variant

And also take the opportunity to use devm_clk_get_enabled(), remove
clk_prepare_enable() and the rollbacking in the err_disable_clk_per*:
label sections.

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

2023-04-03 17:18:08

by Kang Chen

[permalink] [raw]
Subject: [PATCH v2 1/2] thermal: mediatek: use devm_of_iomap to avoid resource leak in mtk_thermal_probe

Smatch reports:
1. mtk_thermal_probe() warn: 'apmixed_base' from of_iomap() not released.
2. mtk_thermal_probe() warn: 'auxadc_base' from of_iomap() not released.

The original code forgets to release iomap resource when handling errors,
fix it by switch to devm_of_iomap.

Fixes: 89945047b166 ("thermal: mediatek: Add tsensor support for V2 thermal system")
Signed-off-by: Kang Chen <[email protected]>
---
v2 -> v1: use devm_of_iomap instead.

drivers/thermal/mediatek/auxadc_thermal.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/thermal/mediatek/auxadc_thermal.c b/drivers/thermal/mediatek/auxadc_thermal.c
index ab730f9552d0..df5e2e354408 100644
--- a/drivers/thermal/mediatek/auxadc_thermal.c
+++ b/drivers/thermal/mediatek/auxadc_thermal.c
@@ -1142,7 +1142,10 @@ static int mtk_thermal_probe(struct platform_device *pdev)
return -ENODEV;
}

- auxadc_base = of_iomap(auxadc, 0);
+ auxadc_base = devm_of_iomap(&pdev->dev, auxadc, 0, NULL);
+ if (IS_ERR(auxadc_base))
+ return PTR_ERR(auxadc_base);
+
auxadc_phys_base = of_get_phys_base(auxadc);

of_node_put(auxadc);
@@ -1158,7 +1161,10 @@ static int mtk_thermal_probe(struct platform_device *pdev)
return -ENODEV;
}

- apmixed_base = of_iomap(apmixedsys, 0);
+ apmixed_base = devm_of_iomap(&pdev->dev, apmixedsys, 0, NULL);
+ if (IS_ERR(apmixed_base)
+ return PTR_ERR(apmixed_base);
+
apmixed_phys_base = of_get_phys_base(apmixedsys);

of_node_put(apmixedsys);
--
2.34.1