clk: readd refcounting for struct clk instances
Subsystems (5)
PATCH v2 1/1 clk: readd refcounting for struct clk instances
With the split into struct clk and struct clk_core, clocks lost the ability for nested __clk_get clkdev calls. While it stays possible to call __clk_get, the first call to (__)clk_put will clear the struct clk, making subsequent clk_put calls run into a NULL pointer dereference.
One prime example of this sits in the generic power domain code, where it is possible to add the clocks both by name and by passing in a struct clk via pm_clk_add_clk(). __pm_clk_add() in turn then calls __clk_get to increase the refcount, so that the original code can put the clock again.
A possible call-path looks like
clk = of_clk_get();pm_clk_add_clk(dev, clk);clk_put(clk);
with pm_clk_add_clk() => __pm_clk_add() then calling __clk_get on the clk and later clk_put when the pm clock list gets destroyed, thus creating a NULL pointer deref, as the struct clk doesn't exist anymore.
So add a separate refcounting for struct clk instances and only clean up once the refcount reaches zero. This makes it possible again to hand off a clock reference to common code without needing to track it further.
---
While it may be nice to do the actual handling of the clock references only in the calling code, in this current use case it would create a big additional overhead.
It looks like this so called synchronous reset on power-domain state- changes, requiring device clocks to be turned on, is not that uncommon or rockchip-specific.
For this Kevin requested that we read the clocks from the actual consumer devices and not double-list them in the power-domain node as well.
So when expecting pm_clk_add_clk() to work, the current powerdomain code can simply do when adding a device to a domain in rockchip_pd_attach_dev():
while ((clk = of_clk_get(dev->of_node, i++)) && !IS_ERR(clk)) {
dev_dbg(dev, "adding clock '%pC' to list of PM clocks\n", clk);
error = pm_clk_add_clk(dev, clk);
clk_put(clk);
}The clock gets handed off to the generic pm clock handling and thus clk_put in there.
On the other hand when only the rockchip power-domain code is expected to get and put the clock, we would require a lot of new overhead, as now the code would also need to track which devices got added to what domain and also all clock-references until the device gets detached again. So this would essentially duplicate a big part of what the genpd-code does (per-domain device-list and per-device clock-list).
As this seems to be not uncommon, future powerdomain drivers might need that too and would also need to duplicate that handling.
When allowing multiple __clk_get and __clk_put calls on the other hand, the overhead for the regular case comes down to one atomic_inc, atomic_sub_and_test and the function call to the new separate release function ;-) .
changes in v2: removed double parentheses found by Stephen Boyd
drivers/clk/clk.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)
drivers/clk/clk.c +21 -10
@@ -85,6 +85,7 @@ struct clk {unsigned long min_rate;unsigned long max_rate;struct hlist_node clks_node; +struct kref ref; };/*** locking ***/
@@ -2590,7 +2591,7 @@ fail_out:EXPORT_SYMBOL_GPL (clk_register);/* Free memory allocated for a clock. */ -static void __clk_release (struct kref *ref) +static void __clk_core_release (struct kref *ref) { struct clk_core *core = container_of(ref, struct clk_core, ref);int i = core->num_parents;
@@ -2606,6 +2607,18 @@ static void __clk_release(struct kref *ref)kfree (core); } +static void __clk_release (struct kref *ref) +{ + struct clk *clk = container_of(ref, struct clk, ref); + +hlist_del (&clk->clks_node); +if (clk->min_rate > clk->core->req_rate || + clk->max_rate < clk->core->req_rate) +clk_core_set_rate_nolock (clk->core, clk->core->req_rate); + +kfree (clk); +} +/* * Empty clk_ops for unregistered clocks. These are used temporarily * after clk_unregister() was called on a clock and until last clock
@@ -2684,7 +2697,7 @@ void clk_unregister(struct clk *clk)if (clk->core->prepare_count)pr_warn ("%s: unregistering prepared clock: %s\n" , __func__, clk->core->name); -kref_put (&clk->core->ref, __clk_release); +kref_put (&clk->core->ref, __clk_core_release);clk_prepare_unlock (); }
@@ -2759,12 +2772,14 @@ int __clk_get(struct clk *clk)return 0 ;kref_get (&core->ref); +kref_get (&clk->ref); }return 1 ; }void __clk_put (struct clk *clk) { +struct clk_core *core;struct module *owner;if (!clk ||WARN_ON_ONCE (IS_ERR (clk)))
@@ -2772,19 +2787,15 @@ void __clk_put(struct clk *clk)clk_prepare_lock (); -hlist_del (&clk->clks_node); -if (clk->min_rate > clk->core->req_rate || - clk->max_rate < clk->core->req_rate) -clk_core_set_rate_nolock (clk->core, clk->core->req_rate); + core = clk->core; + owner = core->owner; - owner = clk->core->owner; -kref_put (&clk->core->ref, __clk_release); +kref_put (&clk->ref, __clk_release); +kref_put (&core->ref, __clk_core_release);clk_prepare_unlock ();module_put (owner); - -kfree (clk); }/*** clk rate change notifiers ***/
Signature · 1 line
2.5.1
Quoted 31 lines · Heiko Stübner
---
While it may be nice to do the actual handling of the clock references only in the calling code, in this current use case it would create a big additional overhead.
It looks like this so called synchronous reset on power-domain state- changes, requiring device clocks to be turned on, is not that uncommon or rockchip-specific.
For this Kevin requested that we read the clocks from the actual consumer devices and not double-list them in the power-domain node as well.So when expecting
pm_clk_add_clk()to work, the current powerdomain code can simply do when adding a device to a domain inrockchip_pd_attach_dev():while ((clk = of_clk_get(dev->of_node, i++)) && !IS_ERR(clk)) { dev_dbg(dev, "adding clock '%pC' to list of PM clocks\n", clk); error = pm_clk_add_clk(dev, clk); clk_put(clk); }The clock gets handed off to the generic pm clock handling and thus
clk_putin there.On the other hand when only the rockchip power-domain code is expected to get and put the clock, we would require a lot of new overhead, as now the code would also need to track which devices got added to what domain and also all clock-references until the device gets detached again. So this would essentially duplicate a big part of what the genpd-code does (per-domain device-list and per-device clock-list).
Quoted 8 lines
As this seems to be not uncommon, future powerdomain drivers might need that too and would also need to duplicate that handling.
When allowing multiple
__clk_getand__clk_putcalls on the other hand, the overhead for the regular case comes down to oneatomic_inc,atomic_sub_and_testand the function call to the new separate release function ;-) .
Why are we doing of_clk_get(), pm_clk_add_clk(), and then clk_put()? Just drop that clk_put() in the caller and remove the __clk_get() inside pm_clk_add_clk() and everything works the same. This patch does most of that, except it doesn't handle the error path where we would need to throw a clk_put().
Really, that snippet of code that loops over a device's clocks and adds them to a pm domain is an example of duplicate code that should go into some common layer like the PM clock stuff. Then we don't have any kind of situation where we're passing struct clk pointers off to other layers of code and this "problem" doesn't exist.
----8<----
drivers/base/power/clock_ops.c +2 -2
@@ -95,7 +95,7 @@ static int __pm_clk_add(struct device *dev, const char *con_id,return -ENOMEM ; } }else { -if (IS_ERR (clk) || !__clk_get (clk)) { +if (IS_ERR (clk)) {kfree (ce);return -ENOENT ; }
@@ -129,7 +129,7 @@ int pm_clk_add(struct device *dev, const char *con_id)* @clk: Clock pointer * * Add the clock to the list of clocks used for the power management of @dev. -* It will increment refcount on clock pointer, use clk_put() on it when done. +* Callers should not call clk_put() on @clk after calling this function. */ int pm_clk_add_clk (struct device *dev,struct clk *clk) {
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
Am Montag, 28. September 2015, 11:46:40 schrieb Stephen Boyd:
Quoted 4 lines · Heiko St=FCbner
Quoted 3 lines
---
=20
While it may be nice to do the actual handling of the clock referen=
ces
Quoted 4 lines
Quoted 4 lines
only in the calling code, in this current use case it would create a big additional overhead.
=20
It looks like this so called synchronous reset on power-domain stat=
e-
Quoted 1 line
Quoted 1 line
changes, requiring device clocks to be turned on, is not that uncom=
mon
Quoted 2 lines
Quoted 2 lines
or rockchip-specific.
For this Kevin requested that we read the clocks from the actual co=
nsumer
Quoted 3 lines
Quoted 3 lines
devices and not double-list them in the power-domain node as well. =20
So when expectingpm_clk_add_clk()to work, the current powerdomain=
code
Quoted 2 lines
Quoted 2 lines
=20
can simply do when adding a device to a domain in=20
rockchip_pd_attach_dev():
Quoted 1 line
Quoted 1 line
while ((clk =3D of_clk_get(dev->of_node, i++)) && !IS_ERR(clk))=
{
Quoted 9 lines
Quoted 8 lines
=09
=09dev_dbg(dev, "adding clock '%pC' to list of PM clocks\n", clk); =09error =3Dpm_clk_add_clk(dev, clk);
=09clk_put(clk);
=09}=20
The clock gets handed off to the generic pm clock handling and thus=
Quoted 4 lines
Quoted 4 lines
clk_putin there.
=20
=20
On the other hand when only the rockchip power-domain code is expec=
ted
Quoted 1 line
Quoted 1 line
to get and put the clock, we would require a lot of new overhead, a=
s now
Quoted 6 lines
Quoted 5 lines
the code would also need to track which devices got added to what domain and also all clock-references until the device gets detached=
again. So this would essentially duplicate a big part of what the genpd-code does (per-domain device-list and per-device clock-list).=
Quoted 7 lines
Quoted 7 lines
=20
As this seems to be not uncommon, future powerdomain drivers might need that too and would also need to duplicate that handling.==20
When allowing multiple__clk_getand__clk_putcalls on the other hand, the overhead for the regular case comes down to one atomic_in=
c,
Quoted 1 line
Quoted 1 line
atomic_sub_and_testand the function call to the new separate relea=
se
Quoted 7 lines
Quoted 1 line
function ;-) .
=20
Why are we doingof_clk_get(),pm_clk_add_clk(), and thenclk_put()? Just drop thatclk_put()in the caller and remove the__clk_get()insidepm_clk_add_clk()and everything works the same. This patch does most of that, except it doesn't handle the error path where we would need to throw aclk_put().
I guess that was my try to keep all the gets and puts together, but als=
o an=20
instance of not seeing the forest for the trees. Looking at your soluti= on=20
below does look actually really cool.
Would still be nice if the genpd people would speak up on their prefere= nce. In=20
the worst case I'll try hunting them down at ELCE next week :-)
I guess for the error path, one could just set the notion that if=20 pm_clk_add_clk returns sucessfully it has taken over the clock referenc= e=20
completely and in the error case the caller should clean up?
drivers/base/power/cloc=
Quoted 10 lines
=20
Really, that snippet of code that loops over a device's clocks and adds them to a pm domain is an example of duplicate code that should go into some common layer like the PM clock stuff. Then we don't have any kind of situation where we're passing struct clk pointers off to other layers of code and this "problem" doesn't exist.
=20
----8<----drivers/base/power/cloc= renamed from drivers/base/power/clock_ops.c +0
-0
k_ops.c
drivers/base/power/clock_ops.c static int __pm_clk_add(struct device *dev, const c=
Quoted 4 lines
index
acef9f9f759a..529a03e8282c100644drivers/base/power/clock_ops.c +0
-0@@ -95,7 +95,7 @@ static int __pm_clk_add(struct device *dev, const c=
har
drivers/base/power/clock_ops.c int pm_clk_add(struct device *dev, const char *co=
Quoted 9 lines
drivers/base/power/clock_ops.c +1
-1*con_id, return -ENOMEM ; } }else { -if (IS_ERR (clk) || !__clk_get (clk)) { +if (IS_ERR (clk)) {kfree (ce);return -ENOENT ; }@@ -129,7 +129,7 @@ int pm_clk_add(struct device *dev, const char *co=
n_id)
drivers/base/power/clock_ops.c int pm_clk_add(struct device *dev, const char *con_id)
Quoted 3 lines
drivers/base/power/clock_ops.c +0
-0* @clk: Clock pointer * * Add the clock to the list of clocks used for the power management=
of
Quoted 1 line
@dev. - * It will increment refcount on clock pointer, use
clk_put()=
on it
Quoted 1 line
when done. + * Callers should not call
clk_put()on @clk after callin=
g this
Quoted 3 lines
function. */
intpm_clk_add_clk(struct device *dev, struct clk *clk){