Skip to contentSkip to conversation

clk: renesas: clk-mstp: Free memory and Unmap region obtained by kzalloc and of_iomap

PATCH In review Arvind Yadav 2 participants 2 messages Sep 20, 2016

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

PATCH 1/1 clk: renesas: clk-mstp: Free memory and Unmap region obtained by kzalloc and of_iomap

From: Arvind Yadav <[email protected]>

Free memory and memory mapping , if cpg_mstp_clocks_init is not successful.

---
 drivers/clk/renesas/clk-mstp.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/clk/renesas/clk-mstp.c +12 -2
index 5093a25..19e73c8 100644
@@ -179,14 +179,20 @@ static void __init cpg_mstp_clocks_init(struct device_node *np)
 	group->data.clks = clks;
 
 	group->smstpcr = of_iomap(np, 0);
-	group->mstpsr = of_iomap(np, 1);
-
 	if (group->smstpcr == NULL) {
 		pr_err("%s: failed to remap SMSTPCR\n", __func__);
 		kfree(group);
 		kfree(clks);
 		return;
 	}
+	group->mstpsr = of_iomap(np, 1);
+	if (group->mstpsr == NULL) {
+		pr_err("%s: failed to remap MSTPCR\n", __func__);
+		iounmap(group->smstpcr);
+		kfree(group);
+		kfree(clks);
+		return;
+	}
 
 	for (i = 0; i < MSTP_MAX_CLOCKS; ++i)
 		clks[i] = ERR_PTR(-ENOENT);
@@ -236,6 +242,10 @@ static void __init cpg_mstp_clocks_init(struct device_node *np)
 		} else {
 			pr_err("%s: failed to register %s %s clock (%ld)\n",
 			       __func__, np->name, name, PTR_ERR(clks[clkidx]));
+			iounmap(group->smstpcr);
+			iounmap(group->mstpsr);
+			kfree(group);
+			kfree(clks);
 		}
 	}
 
Signature · 1 line
1.7.9.5
#1 lore raw

HI Arvind,

Thank you for the patch.

Quoted 22 lines · On Tuesday 20 Sep 2016 18:09:19 Arvind Yadav wrote:

From: Arvind Yadav <[email protected]>

Free memory and memory mapping , if cpg_mstp_clocks_init is not successful.

---
 drivers/clk/renesas/clk-mstp.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/clk/renesas/clk-mstp.c +0 -2
index 5093a25..19e73c8 100644
@@ -179,14 +179,20 @@ static void __init cpg_mstp_clocks_init(struct
 device_node *np) group->data.clks = clks;
 
 	group->smstpcr = of_iomap(np, 0);
-	group->mstpsr = of_iomap(np, 1);
-
 	if (group->smstpcr == NULL) {
 		pr_err("%s: failed to remap SMSTPCR\n", __func__);
 		kfree(group);

drivers/clk/renesas/clk-mstp.c static void __init cpg_mstp_clocks_init(struct · line 194

Quoted 10 lines
drivers/clk/renesas/clk-mstp.c +7 -0
 		kfree(clks);
 		return;
 	}
+	group->mstpsr = of_iomap(np, 1);
+	if (group->mstpsr == NULL) {
+		pr_err("%s: failed to remap MSTPCR\n", __func__);
+		iounmap(group->smstpcr);
+		kfree(group);
+		kfree(clks);
+		return;

If you really want to do this (and I don't think that's worth it, an error here will render the system completely unbootable anyway, I'd rather remove the kfree() calls), you can test both group->smstpcr and group->mstpsr in a single go, with a single error path. You can then call iounmap() on both variables in the error path, either unconditionally if iounmap() is safe to be called on NULL pointers (to be checked, I haven't verified that personally), or conditionally based on the pointer value.

drivers/clk/renesas/clk-mstp.c static void __init cpg_mstp_clocks_init(struct · line 244

Quoted 8 lines
drivers/clk/renesas/clk-mstp.c +1 -0
+	}
 
 	for (i = 0; i < MSTP_MAX_CLOCKS; ++i)
 		clks[i] = ERR_PTR(-ENOENT);
@@ -236,6 +242,10 @@ static void __init cpg_mstp_clocks_init(struct
 device_node *np) } else {
 			pr_err("%s: failed to register %s %s clock (%ld)\n",
 			       __func__, np->name, name, 

PTR_ERR(clks[clkidx]));

drivers/clk/renesas/clk-mstp.c static void __init cpg_mstp_clocks_init(struct device_node *np) · line 248

Quoted 4 lines
drivers/clk/renesas/clk-mstp.c +4 -0
+			iounmap(group->smstpcr);
+			iounmap(group->mstpsr);
+			kfree(group);
+			kfree(clks);

This is wrong, a single clock failing to be registered is not a fatal error, and should not free resources used by all other clocks in the group.

Quoted 2 lines
drivers/clk/renesas/clk-mstp.c +0 -0
 		}
 	}
Signature · 3 lines
Regards,

Laurent Pinchart