diff mbox series

[RFC,v2,1/4] clk: clk-conf: properly release of nodes

Message ID 20220531124554.275682-2-nuno.sa@analog.com (mailing list archive)
State Superseded, archived
Headers show
Series Dynamic OF and use after free related fixes | expand

Commit Message

Nuno Sa May 31, 2022, 12:45 p.m. UTC
We need to call 'of_node_put()' when we are done with the node or on
error paths. Otherwise this can leak memory in DYNAMIC_OF setups.

In order to make things easier to follow, an helper function was added
to set each parent in it's own function.

Fixes: 86be408bfbd8 ("clk: Support for clock parents and rates assigned from device tree")
Signed-off-by: Nuno Sá <nuno.sa@analog.com>
---
 drivers/clk/clk-conf.c | 126 +++++++++++++++++++++++++----------------
 1 file changed, 78 insertions(+), 48 deletions(-)
diff mbox series

Patch

diff --git a/drivers/clk/clk-conf.c b/drivers/clk/clk-conf.c
index 2ef819606c41..b8ea4f93f35e 100644
--- a/drivers/clk/clk-conf.c
+++ b/drivers/clk/clk-conf.c
@@ -11,11 +11,78 @@ 
 #include <linux/of.h>
 #include <linux/printk.h>
 
-static int __set_clk_parents(struct device_node *node, bool clk_supplier)
+static int __set_clk_parent(struct device_node *node, bool clk_supplier,
+			    int index, bool *stop)
 {
 	struct of_phandle_args clkspec;
-	int index, rc, num_parents;
 	struct clk *clk, *pclk;
+	int rc;
+
+	rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
+					"#clock-cells",	index, &clkspec);
+	if (rc) {
+		/* skip empty (null) phandles */
+		if (rc == -ENOENT)
+			return 0;
+
+		return rc;
+	}
+
+	if (clkspec.np == node && !clk_supplier) {
+		*stop = true;
+		goto out_of_put;
+	}
+
+	pclk = of_clk_get_from_provider(&clkspec);
+	of_node_put(clkspec.np);
+	if (IS_ERR(pclk)) {
+		if (PTR_ERR(pclk) != -EPROBE_DEFER)
+			pr_warn("clk: couldn't get parent clock %d for %pOF\n",
+				index, node);
+
+		return PTR_ERR(pclk);
+	}
+
+	rc = of_parse_phandle_with_args(node, "assigned-clocks",
+					"#clock-cells", index, &clkspec);
+	if (rc) {
+		clk_put(pclk);
+		return rc;
+	}
+
+	if (clkspec.np == node && !clk_supplier) {
+		*stop = true;
+		goto out_clk_put;
+	}
+
+	clk = of_clk_get_from_provider(&clkspec);
+	if (IS_ERR(clk)) {
+		if (PTR_ERR(clk) != -EPROBE_DEFER)
+			pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
+				index, node);
+
+		rc = PTR_ERR(clk);
+		goto out_clk_put;
+	}
+
+	rc = clk_set_parent(clk, pclk);
+	if (rc)
+		pr_err("clk: failed to reparent %s to %s: %d\n",
+		       __clk_get_name(clk), __clk_get_name(pclk), rc);
+
+	clk_put(clk);
+
+out_clk_put:
+	clk_put(pclk);
+out_of_put:
+	of_node_put(clkspec.np);
+	return rc;
+}
+
+static int __set_clk_parents(struct device_node *node, bool clk_supplier)
+{
+	int index, rc, num_parents;
+	bool stop = false;
 
 	num_parents = of_count_phandle_with_args(node, "assigned-clock-parents",
 						 "#clock-cells");
@@ -24,53 +91,12 @@  static int __set_clk_parents(struct device_node *node, bool clk_supplier)
 		       node);
 
 	for (index = 0; index < num_parents; index++) {
-		rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
-					"#clock-cells",	index, &clkspec);
-		if (rc < 0) {
-			/* skip empty (null) phandles */
-			if (rc == -ENOENT)
-				continue;
-			else
-				return rc;
-		}
-		if (clkspec.np == node && !clk_supplier)
-			return 0;
-		pclk = of_clk_get_from_provider(&clkspec);
-		if (IS_ERR(pclk)) {
-			if (PTR_ERR(pclk) != -EPROBE_DEFER)
-				pr_warn("clk: couldn't get parent clock %d for %pOF\n",
-					index, node);
-			return PTR_ERR(pclk);
-		}
-
-		rc = of_parse_phandle_with_args(node, "assigned-clocks",
-					"#clock-cells", index, &clkspec);
-		if (rc < 0)
-			goto err;
-		if (clkspec.np == node && !clk_supplier) {
-			rc = 0;
-			goto err;
-		}
-		clk = of_clk_get_from_provider(&clkspec);
-		if (IS_ERR(clk)) {
-			if (PTR_ERR(clk) != -EPROBE_DEFER)
-				pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
-					index, node);
-			rc = PTR_ERR(clk);
-			goto err;
-		}
-
-		rc = clk_set_parent(clk, pclk);
-		if (rc < 0)
-			pr_err("clk: failed to reparent %s to %s: %d\n",
-			       __clk_get_name(clk), __clk_get_name(pclk), rc);
-		clk_put(clk);
-		clk_put(pclk);
+		rc = __set_clk_parent(node, clk_supplier, index, &stop);
+		if (rc || stop)
+			return rc;
 	}
+
 	return 0;
-err:
-	clk_put(pclk);
-	return rc;
 }
 
 static int __set_clk_rates(struct device_node *node, bool clk_supplier)
@@ -93,14 +119,17 @@  static int __set_clk_rates(struct device_node *node, bool clk_supplier)
 				else
 					return rc;
 			}
-			if (clkspec.np == node && !clk_supplier)
+			if (clkspec.np == node && !clk_supplier) {
+				of_node_put(clkspec.np);
 				return 0;
+			}
 
 			clk = of_clk_get_from_provider(&clkspec);
 			if (IS_ERR(clk)) {
 				if (PTR_ERR(clk) != -EPROBE_DEFER)
 					pr_warn("clk: couldn't get clock %d for %pOF\n",
 						index, node);
+				of_node_put(clkspec.np);
 				return PTR_ERR(clk);
 			}
 
@@ -110,6 +139,7 @@  static int __set_clk_rates(struct device_node *node, bool clk_supplier)
 				       __clk_get_name(clk), rate, rc,
 				       clk_get_rate(clk));
 			clk_put(clk);
+			of_node_put(clkspec.np);
 		}
 		index++;
 	}