[PATCH] clk: mmp: pxa168: fix potential memory leaks in pxa168_clk_init()

From: xkernel . wang
Date: Thu Apr 07 2022 - 05:02:08 EST


From: Xiaoke Wang <xkernel.wang@xxxxxxxxxxx>

In pxa168_clk_init(), except for the first error path, the other error
paths directly return without releasing the allocated resources, which
can lead to memory leaks.

This patch unifies the error handling code and each error will jump to
the corresponding tag to release the resources.

Signed-off-by: Xiaoke Wang <xkernel.wang@xxxxxxxxxxx>
---
drivers/clk/mmp/clk-of-pxa168.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/mmp/clk-of-pxa168.c b/drivers/clk/mmp/clk-of-pxa168.c
index f110c02..05b96cf 100644
--- a/drivers/clk/mmp/clk-of-pxa168.c
+++ b/drivers/clk/mmp/clk-of-pxa168.c
@@ -258,19 +258,19 @@ static void __init pxa168_clk_init(struct device_node *np)
pxa_unit->mpmu_base = of_iomap(np, 0);
if (!pxa_unit->mpmu_base) {
pr_err("failed to map mpmu registers\n");
- return;
+ goto free_memory;
}

pxa_unit->apmu_base = of_iomap(np, 1);
if (!pxa_unit->apmu_base) {
pr_err("failed to map apmu registers\n");
- return;
+ goto unmap_mpmu_region;
}

pxa_unit->apbc_base = of_iomap(np, 2);
if (!pxa_unit->apbc_base) {
pr_err("failed to map apbc registers\n");
- return;
+ goto unmap_apmu_region;
}

mmp_clk_init(np, &pxa_unit->unit, PXA168_NR_CLKS);
@@ -282,6 +282,15 @@ static void __init pxa168_clk_init(struct device_node *np)
pxa168_axi_periph_clk_init(pxa_unit);

pxa168_clk_reset_init(np, pxa_unit);
+
+ return;
+
+unmap_apmu_region:
+ iounmap(pxa_unit->apmu_base);
+unmap_mpmu_region:
+ iounmap(pxa_unit->mpmu_base);
+free_memory:
+ kfree(pxa_unit);
}

CLK_OF_DECLARE(pxa168_clk, "marvell,pxa168-clock", pxa168_clk_init);
--