Skip to contentSkip to conversation

clk: Free struct clk allocated during clk_hw_register()

PATCH In review Rajendra Nayak 2 participants 4 messages Nov 8, 2016 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: Free struct clk allocated during clk_hw_register()

With clk_hw_register() API we hide the struct clk from the caller and return an int error code instead, so the caller (clk provider) is not expected to use hw->clk on return.
Free the memory, and mark hw->clk as NULL before returning.

---
 drivers/clk/clk.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/clk/clk.c +9 -1
index 0fb39fe..f81e4aa 100644
@@ -2628,7 +2628,15 @@ struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  */
 int clk_hw_register(struct device *dev, struct clk_hw *hw)
 {
-	return PTR_ERR_OR_ZERO(clk_register(dev, hw));
+	struct clk *c;
+
+	c = clk_register(dev, hw);
+	if (IS_ERR(c))
+		return PTR_ERR(c);
+
+	__clk_free_clk(c);
+	hw->clk = NULL;
+	return 0;
 }
 EXPORT_SYMBOL_GPL(clk_hw_register);
 
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation
#1 lore raw

Hi Rajendra,

Quoted 3 lines · Rajendra Nayak

With clk_hw_register() API we hide the struct clk from the caller and return an int error code instead, so the caller (clk provider) is not expected to use hw->clk on return.

That's correct, in case of failure.

Quoted 14 lines

Free the memory, and mark hw->clk as NULL before returning.

---
 drivers/clk/clk.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/clk/clk.c +0 -0
index 0fb39fe..f81e4aa 100644
@@ -2628,7 +2628,15 @@ struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  */
 int clk_hw_register(struct device *dev, struct clk_hw *hw)

drivers/clk/clk.c struct clk *clk_register(struct device *dev, struct clk_hw *hw) · line 2638

Quoted 10 lines
drivers/clk/clk.c +8 -1
 {
-       return PTR_ERR_OR_ZERO(clk_register(dev, hw));
+       struct clk *c;
+
+       c = clk_register(dev, hw);
+       if (IS_ERR(c))
+               return PTR_ERR(c);
+
+       __clk_free_clk(c);
+       hw->clk = NULL;

This is the success path, not the failure path (on failure, clk_register() has already freed the struct clk).
Why do you free the struct clk in case of success?

What am I missing?

drivers/clk/clk.c struct clk *clk_register(struct device *dev, struct clk_hw *hw) · line 2641

Quoted 3 lines
drivers/clk/clk.c +1 -0
+       return 0;
 }
 EXPORT_SYMBOL_GPL(clk_hw_register);

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#2 lore raw
Quoted 8 lines · Geert Uytterhoeven

Hi Rajendra,

Quoted 3 lines · Rajendra Nayak

With clk_hw_register() API we hide the struct clk from the caller and return an int error code instead, so the caller (clk provider) is not expected to use hw->clk on return.

That's correct, in case of failure.

sorry, maybe the commit text needs to be reworded. I meant 'clk_hw_register() returns an int (not a struct clk pointer), 0 on success or an error code in case of a failure.

Quoted 30 lines
Quoted 15 lines

Free the memory, and mark hw->clk as NULL before returning.

---
 drivers/clk/clk.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/clk/clk.c +0 -0
index 0fb39fe..f81e4aa 100644
@@ -2628,7 +2628,15 @@ struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  */
 int clk_hw_register(struct device *dev, struct clk_hw *hw)

drivers/clk/clk.c struct clk *clk_register(struct device *dev, struct clk_hw *hw) · line 2638

Quoted 10 lines
drivers/clk/clk.c +8 -1
 {
-       return PTR_ERR_OR_ZERO(clk_register(dev, hw));
+       struct clk *c;
+
+       c = clk_register(dev, hw);
+       if (IS_ERR(c))
+               return PTR_ERR(c);
+
+       __clk_free_clk(c);
+       hw->clk = NULL;

This is the success path, not the failure path (on failure, clk_register() has already freed the struct clk).
Why do you free the struct clk in case of success?

Quoted 1 line

What am I missing?

so with 'per-user' clks, I thought we now have one struct clk per user, allocated when the user does a clk_get() and freed with a clk_put(), so we shouldn't ideally need one during clk registration?
The one allocated in clk_register() is for legacy users who need to get a struct clk * back. For users of clk_hw_register() this should not be needed, no?

Quoted 16 lines

drivers/clk/clk.c struct clk *clk_register(struct device *dev, struct clk_hw *hw) · line 2641

Quoted 4 lines
drivers/clk/clk.c +1 -0
+       return 0;
 }
 EXPORT_SYMBOL_GPL(clk_hw_register);

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation
#3 lore raw
Quoted 45 lines · Rajendra Nayak
Quoted 8 lines · Geert Uytterhoeven

Hi Rajendra,

Quoted 3 lines · Rajendra Nayak

With clk_hw_register() API we hide the struct clk from the caller and return an int error code instead, so the caller (clk provider) is not expected to use hw->clk on return.

That's correct, in case of failure.

sorry, maybe the commit text needs to be reworded. I meant 'clk_hw_register() returns an int (not a struct clk pointer), 0 on success or an error code in case of a failure.

Quoted 30 lines
Quoted 15 lines

Free the memory, and mark hw->clk as NULL before returning.

---
 drivers/clk/clk.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/clk/clk.c +0 -0
index 0fb39fe..f81e4aa 100644
@@ -2628,7 +2628,15 @@ struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  */
 int clk_hw_register(struct device *dev, struct clk_hw *hw)

drivers/clk/clk.c struct clk *clk_register(struct device *dev, struct clk_hw *hw) · line 2638

Quoted 10 lines
drivers/clk/clk.c +8 -1
 {
-       return PTR_ERR_OR_ZERO(clk_register(dev, hw));
+       struct clk *c;
+
+       c = clk_register(dev, hw);
+       if (IS_ERR(c))
+               return PTR_ERR(c);
+
+       __clk_free_clk(c);
+       hw->clk = NULL;

This is the success path, not the failure path (on failure, clk_register() has already freed the struct clk).
Why do you free the struct clk in case of success?

Quoted 7 lines
Quoted 1 line

What am I missing?

so with 'per-user' clks, I thought we now have one struct clk per user, allocated when the user does a clk_get() and freed with a clk_put(), so we shouldn't ideally need one during clk registration?
The one allocated in clk_register() is for legacy users who need to get a struct clk * back. For users of clk_hw_register() this should not be needed, no?

Looking through this a little more, I don't think we can get rid of the 'struct clk' allocations at registration time as yet.
It seems to be used by clk_get_parent() at least, which does not yet do a __clk_create_clk() and relies on hw->clk

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation