Skip to content

Commit 1b28059

Browse files
MarkKai2000abelvesa
authored andcommitted
clk: imx: clk-imxrt1050: fix memory leak in imxrt1050_clocks_probe
Use devm_of_iomap() instead of of_iomap() to automatically handle the unused ioremap region. If any error occurs, regions allocated by kzalloc() will leak, but using devm_kzalloc() instead will automatically free the memory using devm_kfree(). Also, fix error handling of hws by adding unregister_hws label, which unregisters remaining hws when iomap failed. Fixes: 7154b04 ("clk: imx: Add initial support for i.MXRT1050 clock driver") Signed-off-by: Kai Ma <[email protected]> Reviewed-by: Peng Fan <[email protected]> Acked-by: Jesse Taube <[email protected]> Reviewed-by: Abel Vesa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Abel Vesa <[email protected]>
1 parent 8208181 commit 1b28059

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

drivers/clk/imx/clk-imxrt1050.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int imxrt1050_clocks_probe(struct platform_device *pdev)
4242
struct device_node *anp;
4343
int ret;
4444

45-
clk_hw_data = kzalloc(struct_size(clk_hw_data, hws,
45+
clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws,
4646
IMXRT1050_CLK_END), GFP_KERNEL);
4747
if (WARN_ON(!clk_hw_data))
4848
return -ENOMEM;
@@ -53,10 +53,12 @@ static int imxrt1050_clocks_probe(struct platform_device *pdev)
5353
hws[IMXRT1050_CLK_OSC] = imx_get_clk_hw_by_name(np, "osc");
5454

5555
anp = of_find_compatible_node(NULL, NULL, "fsl,imxrt-anatop");
56-
pll_base = of_iomap(anp, 0);
56+
pll_base = devm_of_iomap(dev, anp, 0, NULL);
5757
of_node_put(anp);
58-
if (WARN_ON(!pll_base))
59-
return -ENOMEM;
58+
if (WARN_ON(IS_ERR(pll_base))) {
59+
ret = PTR_ERR(pll_base);
60+
goto unregister_hws;
61+
}
6062

6163
/* Anatop clocks */
6264
hws[IMXRT1050_CLK_DUMMY] = imx_clk_hw_fixed("dummy", 0UL);
@@ -104,8 +106,10 @@ static int imxrt1050_clocks_probe(struct platform_device *pdev)
104106

105107
/* CCM clocks */
106108
ccm_base = devm_platform_ioremap_resource(pdev, 0);
107-
if (WARN_ON(IS_ERR(ccm_base)))
108-
return PTR_ERR(ccm_base);
109+
if (WARN_ON(IS_ERR(ccm_base))) {
110+
ret = PTR_ERR(ccm_base);
111+
goto unregister_hws;
112+
}
109113

110114
hws[IMXRT1050_CLK_ARM_PODF] = imx_clk_hw_divider("arm_podf", "pll1_arm", ccm_base + 0x10, 0, 3);
111115
hws[IMXRT1050_CLK_PRE_PERIPH_SEL] = imx_clk_hw_mux("pre_periph_sel", ccm_base + 0x18, 18, 2,
@@ -149,8 +153,12 @@ static int imxrt1050_clocks_probe(struct platform_device *pdev)
149153
ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
150154
if (ret < 0) {
151155
dev_err(dev, "Failed to register clks for i.MXRT1050.\n");
152-
imx_unregister_hw_clocks(hws, IMXRT1050_CLK_END);
156+
goto unregister_hws;
153157
}
158+
return 0;
159+
160+
unregister_hws:
161+
imx_unregister_hw_clocks(hws, IMXRT1050_CLK_END);
154162
return ret;
155163
}
156164
static const struct of_device_id imxrt1050_clk_of_match[] = {

0 commit comments

Comments
 (0)