Skip to contentSkip to conversation

clk/sprd: Hold reference returned by of_get_parent()

PATCH In review Liang He 2 participants 6 messages Jun 24, 2022 – Jun 27, 2022

Subsystems (2)
lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH 1/1 clk/sprd: Hold reference returned by of_get_parent()

We should hold the reference returned by of_get_parent() and use it to call of_node_out() for refcount balance.

Fixes f95e8c7923d1 ("clk: sprd: support to get regmap from parent node")

---
these bugs are found in 5.19rc2
these bugs are confirmed not fixed by lore.kernel.org these bugs are compiled tested in 5.19rc2 with at91_dt_defconfig

 drivers/clk/sprd/common.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/clk/sprd/common.c +11 -5
index d620bbbcdfc8..08c1d7a9ec8b 100644
@@ -41,35 +41,41 @@ int sprd_clk_regmap_init(struct platform_device *pdev,
 {
 	void __iomem *base;
 	struct device *dev = &pdev->dev;
-	struct device_node *node = dev->of_node;
+	struct device_node *node = dev->of_node, *np;
 	struct regmap *regmap;
 
+	np = of_get_parent(dev->of_node);
 	if (of_find_property(node, "sprd,syscon", NULL)) {
 		regmap = syscon_regmap_lookup_by_phandle(node, "sprd,syscon");
 		if (IS_ERR(regmap)) {
 			pr_err("%s: failed to get syscon regmap\n", __func__);
+			of_node_put(np);
 			return PTR_ERR(regmap);
 		}
-	} else if (of_device_is_compatible(of_get_parent(dev->of_node),
-			   "syscon")) {
-		regmap = device_node_to_regmap(of_get_parent(dev->of_node));
+	} else if (of_device_is_compatible(np, "syscon")) {
+		regmap = device_node_to_regmap(np);
 		if (IS_ERR(regmap)) {
+			of_node_put(np);
 			dev_err(dev, "failed to get regmap from its parent.\n");
 			return PTR_ERR(regmap);
 		}
 	} else {
 		base = devm_platform_ioremap_resource(pdev, 0);
-		if (IS_ERR(base))
+		if (IS_ERR(base)) {
+			of_node_put(np);
 			return PTR_ERR(base);
+		}
 
 		regmap = devm_regmap_init_mmio(&pdev->dev, base,
 					       &sprdclk_regmap_config);
 		if (IS_ERR(regmap)) {
+			of_node_put(np);
 			pr_err("failed to init regmap\n");
 			return PTR_ERR(regmap);
 		}
 	}
 
+	of_node_put(np);
 	sprd_clk_set_regmap(desc, regmap);
 
 	return 0;
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi Liang,

Quoted 19 lines · Liang He

We should hold the reference returned by of_get_parent() and use it to call of_node_out() for refcount balance.

Fixes f95e8c7923d1 ("clk: sprd: support to get regmap from parent node")

---
these bugs are found in 5.19rc2
these bugs are confirmed not fixed by lore.kernel.org these bugs are compiled tested in 5.19rc2 with at91_dt_defconfig

 drivers/clk/sprd/common.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/clk/sprd/common.c +0 -0
index d620bbbcdfc8..08c1d7a9ec8b 100644

drivers/clk/sprd/common.c int sprd_clk_regmap_init(struct platform_device *pdev, · line 49

Quoted 10 lines
drivers/clk/sprd/common.c +2 -1
@@ -41,35 +41,41 @@ int sprd_clk_regmap_init(struct platform_device *pdev,
 {
        void __iomem *base;
        struct device *dev = &pdev->dev;
-       struct device_node *node = dev->of_node;
+       struct device_node *node = dev->of_node, *np;
        struct regmap *regmap;
 
+       np = of_get_parent(dev->of_node);
        if (of_find_property(node, "sprd,syscon", NULL)) {
                regmap = syscon_regmap_lookup_by_phandle(node, "sprd,syscon");

Why not to call of_node_put() in this function?
He returns an error and it is his responsibility to clean up everything he used, right?

-Orson

Quoted 39 lines
drivers/clk/sprd/common.c +9 -5
                if (IS_ERR(regmap)) {
                        pr_err("%s: failed to get syscon regmap\n", __func__);
+                       of_node_put(np);
                        return PTR_ERR(regmap);
                }
-       } else if (of_device_is_compatible(of_get_parent(dev->of_node),
-                          "syscon")) {
-               regmap = device_node_to_regmap(of_get_parent(dev->of_node));
+       } else if (of_device_is_compatible(np, "syscon")) {
+               regmap = device_node_to_regmap(np);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        dev_err(dev, "failed to get regmap from its parent.\n");
                        return PTR_ERR(regmap);
                }
        } else {
                base = devm_platform_ioremap_resource(pdev, 0);
-               if (IS_ERR(base))
+               if (IS_ERR(base)) {
+                       of_node_put(np);
                        return PTR_ERR(base);
+               }
 
                regmap = devm_regmap_init_mmio(&pdev->dev, base,
                                               &sprdclk_regmap_config);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        pr_err("failed to init regmap\n");
                        return PTR_ERR(regmap);
                }
        }
 
+       of_node_put(np);
        sprd_clk_set_regmap(desc, regmap);
 
        return 0;
--

2.25.1

author
#2 lore raw
Quoted 37 lines · Orson Zhai

Hi Liang,

Quoted 19 lines · Liang He

We should hold the reference returned by of_get_parent() and use it to call of_node_out() for refcount balance.

Fixes f95e8c7923d1 ("clk: sprd: support to get regmap from parent node")

---
these bugs are found in 5.19rc2
these bugs are confirmed not fixed by lore.kernel.org these bugs are compiled tested in 5.19rc2 with at91_dt_defconfig

 drivers/clk/sprd/common.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/clk/sprd/common.c +0 -0
index d620bbbcdfc8..08c1d7a9ec8b 100644

drivers/clk/sprd/common.c int sprd_clk_regmap_init(struct platform_device *pdev, · line 49

Quoted 10 lines
drivers/clk/sprd/common.c +2 -1
@@ -41,35 +41,41 @@ int sprd_clk_regmap_init(struct platform_device *pdev,
 {
        void __iomem *base;
        struct device *dev = &pdev->dev;
-       struct device_node *node = dev->of_node;
+       struct device_node *node = dev->of_node, *np;
        struct regmap *regmap;
 
+       np = of_get_parent(dev->of_node);
        if (of_find_property(node, "sprd,syscon", NULL)) {
                regmap = syscon_regmap_lookup_by_phandle(node, "sprd,syscon");

Why not to call of_node_put() in this function?
He returns an error and it is his responsibility to clean up everything he used, right?

Quoted 2 lines

-Orson

Hi, Orson,

I only know we should use the pair of functions (of_get_parent(), of_node_put) to keep refcount balance.

However, as I do not really know the semantic of other functions, so I cannot give other kinds of patch.

By the way, if we chose to put of_node_put() in this function, I think it also need to put of_node_put() into 'device_node_to_regmap' as it also return error code in following path.

Anyway, thanks very much for your reply and review on my patch code.

Liang

Quoted 39 lines
Quoted 39 lines
drivers/clk/sprd/common.c +9 -5
                if (IS_ERR(regmap)) {
                        pr_err("%s: failed to get syscon regmap\n", __func__);
+                       of_node_put(np);
                        return PTR_ERR(regmap);
                }
-       } else if (of_device_is_compatible(of_get_parent(dev->of_node),
-                          "syscon")) {
-               regmap = device_node_to_regmap(of_get_parent(dev->of_node));
+       } else if (of_device_is_compatible(np, "syscon")) {
+               regmap = device_node_to_regmap(np);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        dev_err(dev, "failed to get regmap from its parent.\n");
                        return PTR_ERR(regmap);
                }
        } else {
                base = devm_platform_ioremap_resource(pdev, 0);
-               if (IS_ERR(base))
+               if (IS_ERR(base)) {
+                       of_node_put(np);
                        return PTR_ERR(base);
+               }
 
                regmap = devm_regmap_init_mmio(&pdev->dev, base,
                                               &sprdclk_regmap_config);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        pr_err("failed to init regmap\n");
                        return PTR_ERR(regmap);
                }
        }
 
+       of_node_put(np);
        sprd_clk_set_regmap(desc, regmap);
 
        return 0;
--

2.25.1

maintainer
#3 lore raw

Hi Liang,

Quoted 42 lines · Liang He
Quoted 37 lines · Orson Zhai

Hi Liang,

Quoted 19 lines · Liang He

We should hold the reference returned by of_get_parent() and use it to call of_node_out() for refcount balance.

Fixes f95e8c7923d1 ("clk: sprd: support to get regmap from parent node")

---
these bugs are found in 5.19rc2
these bugs are confirmed not fixed by lore.kernel.org these bugs are compiled tested in 5.19rc2 with at91_dt_defconfig

 drivers/clk/sprd/common.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/clk/sprd/common.c +0 -0
index d620bbbcdfc8..08c1d7a9ec8b 100644

drivers/clk/sprd/common.c int sprd_clk_regmap_init(struct platform_device *pdev, · line 49

Quoted 10 lines
drivers/clk/sprd/common.c +2 -1
@@ -41,35 +41,41 @@ int sprd_clk_regmap_init(struct platform_device *pdev,
 {
        void __iomem *base;
        struct device *dev = &pdev->dev;
-       struct device_node *node = dev->of_node;
+       struct device_node *node = dev->of_node, *np;
        struct regmap *regmap;
 
+       np = of_get_parent(dev->of_node);
        if (of_find_property(node, "sprd,syscon", NULL)) {
                regmap = syscon_regmap_lookup_by_phandle(node, "sprd,syscon");

Why not to call of_node_put() in this function?
He returns an error and it is his responsibility to clean up everything he used, right?

Quoted 2 lines

-Orson

Quoted 7 lines

Hi, Orson,

I only know we should use the pair of functions (of_get_parent(), of_node_put) to keep refcount balance.

However, as I do not really know the semantic of other functions, so I cannot give other kinds of patch.

Sorry, I didn't catch your point before.
You're right on this unbalance. But it has no direct relation to the function here.
I think it is better to minimize the effective range of of_get_parent()

Could you refer to Pantelis's answer to the last question at [1].

Thanks.
-Orson

[1] https://www.spinics.net/lists/devicetree/msg122555.html

Quoted 49 lines

By the way, if we chose to put of_node_put() in this function, I think it also need to put of_node_put() into 'device_node_to_regmap' as it also return error code in following path.

Anyway, thanks very much for your reply and review on my patch code.

Liang

Quoted 39 lines
Quoted 39 lines
drivers/clk/sprd/common.c +9 -5
                if (IS_ERR(regmap)) {
                        pr_err("%s: failed to get syscon regmap\n", __func__);
+                       of_node_put(np);
                        return PTR_ERR(regmap);
                }
-       } else if (of_device_is_compatible(of_get_parent(dev->of_node),
-                          "syscon")) {
-               regmap = device_node_to_regmap(of_get_parent(dev->of_node));
+       } else if (of_device_is_compatible(np, "syscon")) {
+               regmap = device_node_to_regmap(np);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        dev_err(dev, "failed to get regmap from its parent.\n");
                        return PTR_ERR(regmap);
                }
        } else {
                base = devm_platform_ioremap_resource(pdev, 0);
-               if (IS_ERR(base))
+               if (IS_ERR(base)) {
+                       of_node_put(np);
                        return PTR_ERR(base);
+               }
 
                regmap = devm_regmap_init_mmio(&pdev->dev, base,
                                               &sprdclk_regmap_config);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        pr_err("failed to init regmap\n");
                        return PTR_ERR(regmap);
                }
        }
 
+       of_node_put(np);
        sprd_clk_set_regmap(desc, regmap);
 
        return 0;
--

2.25.1

author
#4 lore raw
Quoted 50 lines · Orson Zhai

Hi Liang,

Quoted 47 lines · Liang He
Quoted 37 lines · Orson Zhai

Hi Liang,

Quoted 19 lines · Liang He

We should hold the reference returned by of_get_parent() and use it to call of_node_out() for refcount balance.

Fixes f95e8c7923d1 ("clk: sprd: support to get regmap from parent node")

---
these bugs are found in 5.19rc2
these bugs are confirmed not fixed by lore.kernel.org these bugs are compiled tested in 5.19rc2 with at91_dt_defconfig

 drivers/clk/sprd/common.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/clk/sprd/common.c +0 -0
index d620bbbcdfc8..08c1d7a9ec8b 100644

drivers/clk/sprd/common.c int sprd_clk_regmap_init(struct platform_device *pdev, · line 49

Quoted 10 lines
drivers/clk/sprd/common.c +2 -1
@@ -41,35 +41,41 @@ int sprd_clk_regmap_init(struct platform_device *pdev,
 {
        void __iomem *base;
        struct device *dev = &pdev->dev;
-       struct device_node *node = dev->of_node;
+       struct device_node *node = dev->of_node, *np;
        struct regmap *regmap;
 
+       np = of_get_parent(dev->of_node);
        if (of_find_property(node, "sprd,syscon", NULL)) {
                regmap = syscon_regmap_lookup_by_phandle(node, "sprd,syscon");

Why not to call of_node_put() in this function?
He returns an error and it is his responsibility to clean up everything he used, right?

Quoted 2 lines

-Orson

Hi, Orson,

I only know we should use the pair of functions (of_get_parent(), of_node_put) to keep refcount balance.

Quoted 6 lines
Quoted 2 lines

However, as I do not really know the semantic of other functions, so I cannot give other kinds of patch.

Sorry, I didn't catch your point before.
You're right on this unbalance. But it has no direct relation to the function here.

Thanks for your understanding. And yes, it has no direct relation
to the function here.

Quoted 9 lines

I think it is better to minimize the effective range of of_get_parent()

Could you refer to Pantelis's answer to the last question at [1].

Thanks.
-Orson

[1] https://www.spinics.net/lists/devicetree/msg122555.html

Thanks, Orson.

Yes, I also wanted to make a better patch solution as the bug is only caused by the 'else if' branch.

However, I cannot find a better solution to fix the bug in the middle of nested 'if-else if-else' brances unless we try to rewrite them, which is more difficult for me, now.

But if you think it is (semantic) safe to rewrite these branches, I can try it.

Thanks again for your relpy.

Liang

Quoted 49 lines
Quoted 49 lines

By the way, if we chose to put of_node_put() in this function, I think it also need to put of_node_put() into 'device_node_to_regmap' as it also return error code in following path.

Anyway, thanks very much for your reply and review on my patch code.

Liang

Quoted 39 lines
Quoted 39 lines
drivers/clk/sprd/common.c +9 -5
                if (IS_ERR(regmap)) {
                        pr_err("%s: failed to get syscon regmap\n", __func__);
+                       of_node_put(np);
                        return PTR_ERR(regmap);
                }
-       } else if (of_device_is_compatible(of_get_parent(dev->of_node),
-                          "syscon")) {
-               regmap = device_node_to_regmap(of_get_parent(dev->of_node));
+       } else if (of_device_is_compatible(np, "syscon")) {
+               regmap = device_node_to_regmap(np);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        dev_err(dev, "failed to get regmap from its parent.\n");
                        return PTR_ERR(regmap);
                }
        } else {
                base = devm_platform_ioremap_resource(pdev, 0);
-               if (IS_ERR(base))
+               if (IS_ERR(base)) {
+                       of_node_put(np);
                        return PTR_ERR(base);
+               }
 
                regmap = devm_regmap_init_mmio(&pdev->dev, base,
                                               &sprdclk_regmap_config);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        pr_err("failed to init regmap\n");
                        return PTR_ERR(regmap);
                }
        }
 
+       of_node_put(np);
        sprd_clk_set_regmap(desc, regmap);
 
        return 0;
--

2.25.1

maintainer
#5 lore raw
Quoted 76 lines · Liang He
Quoted 50 lines · Orson Zhai

Hi Liang,

Quoted 47 lines · Liang He
Quoted 37 lines · Orson Zhai

Hi Liang,

Quoted 19 lines · Liang He

We should hold the reference returned by of_get_parent() and use it to call of_node_out() for refcount balance.

Fixes f95e8c7923d1 ("clk: sprd: support to get regmap from parent node")

---
these bugs are found in 5.19rc2
these bugs are confirmed not fixed by lore.kernel.org these bugs are compiled tested in 5.19rc2 with at91_dt_defconfig

 drivers/clk/sprd/common.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/clk/sprd/common.c +0 -0
index d620bbbcdfc8..08c1d7a9ec8b 100644

drivers/clk/sprd/common.c int sprd_clk_regmap_init(struct platform_device *pdev, · line 49

Quoted 10 lines
drivers/clk/sprd/common.c +2 -1
@@ -41,35 +41,41 @@ int sprd_clk_regmap_init(struct platform_device *pdev,
 {
        void __iomem *base;
        struct device *dev = &pdev->dev;
-       struct device_node *node = dev->of_node;
+       struct device_node *node = dev->of_node, *np;
        struct regmap *regmap;
 
+       np = of_get_parent(dev->of_node);
        if (of_find_property(node, "sprd,syscon", NULL)) {
                regmap = syscon_regmap_lookup_by_phandle(node, "sprd,syscon");

Why not to call of_node_put() in this function?
He returns an error and it is his responsibility to clean up everything he used, right?

Quoted 2 lines

-Orson

Hi, Orson,

I only know we should use the pair of functions (of_get_parent(), of_node_put) to keep refcount balance.

Quoted 6 lines
Quoted 2 lines

However, as I do not really know the semantic of other functions, so I cannot give other kinds of patch.

Sorry, I didn't catch your point before.
You're right on this unbalance. But it has no direct relation to the function here.

Thanks for your understanding. And yes, it has no direct relation
to the function here.

Quoted 9 lines

I think it is better to minimize the effective range of of_get_parent()

Could you refer to Pantelis's answer to the last question at [1].

Thanks.
-Orson

[1] https://www.spinics.net/lists/devicetree/msg122555.html

Thanks, Orson.

Quoted 7 lines

Yes, I also wanted to make a better patch solution as the bug is only caused by the 'else if' branch.

However, I cannot find a better solution to fix the bug in the middle of nested 'if-else if-else' brances unless we try to rewrite them, which is more difficult for me, now.

But if you think it is (semantic) safe to rewrite these branches, I can try it.

No problem. Please do it as long as not to change its original logic.

Thanks.
-Orson

Quoted 54 lines

Thanks again for your relpy.

Liang

Quoted 49 lines
Quoted 49 lines

By the way, if we chose to put of_node_put() in this function, I think it also need to put of_node_put() into 'device_node_to_regmap' as it also return error code in following path.

Anyway, thanks very much for your reply and review on my patch code.

Liang

Quoted 39 lines
Quoted 39 lines
drivers/clk/sprd/common.c +9 -5
                if (IS_ERR(regmap)) {
                        pr_err("%s: failed to get syscon regmap\n", __func__);
+                       of_node_put(np);
                        return PTR_ERR(regmap);
                }
-       } else if (of_device_is_compatible(of_get_parent(dev->of_node),
-                          "syscon")) {
-               regmap = device_node_to_regmap(of_get_parent(dev->of_node));
+       } else if (of_device_is_compatible(np, "syscon")) {
+               regmap = device_node_to_regmap(np);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        dev_err(dev, "failed to get regmap from its parent.\n");
                        return PTR_ERR(regmap);
                }
        } else {
                base = devm_platform_ioremap_resource(pdev, 0);
-               if (IS_ERR(base))
+               if (IS_ERR(base)) {
+                       of_node_put(np);
                        return PTR_ERR(base);
+               }
 
                regmap = devm_regmap_init_mmio(&pdev->dev, base,
                                               &sprdclk_regmap_config);
                if (IS_ERR(regmap)) {
+                       of_node_put(np);
                        pr_err("failed to init regmap\n");
                        return PTR_ERR(regmap);
                }
        }
 
+       of_node_put(np);
        sprd_clk_set_regmap(desc, regmap);
 
        return 0;
--

2.25.1