Skip to contentSkip to conversation

clk: readd refcounting for struct clk instances

PATCH Superseded Heiko Stübner 2 participants 4 messages Sep 15, 2015 – Sep 20, 2015

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
I stumbled upon this while applying the new Rockchip power-domain driver, but I guess the underlying issue is universal and probably present since the original clk/clk_core split, so this probably counts as fix.

 drivers/clk/clk.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
drivers/clk/clk.c +21 -10
index 43e2c3a..7aab1a4 100644
@@ -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
#1 lore raw
Quoted 19 lines · Heiko Stübner

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.

Quoted 8 lines

So add a separate refcounting for struct clk instances and only clean up once the refcount reaches zero.

---
I stumbled upon this while applying the new Rockchip power-domain driver, but I guess the underlying issue is universal and probably present since the original clk/clk_core split, so this probably counts as fix.

Ok. The fix makes sense, but I wonder why we do this. Perhaps we should stop exporting __clk_get() and __clk_put() to everything that uses clkdev in the kernel. They're called per-user clks for a reason. There's one user. Now we have two users of the same struct clk instance, so we have to refcount it too? I hope the shared clk instance isn't being used to set rates in two pieces of code.

And this only affects pm_clk_add_clk() callers right? So the only caller in the kernel (drivers/clk/shmobile/clk-mstp.c) doesn't seem to have this problem right now because it never calls clk_put() on the pointer it passes to pm_clk_add_clk().

So what if we did the patch below? This would rely on callers not calling clk_put() before calling pm_clk_remove() or pm_clk_destroy(), and the life cycle would be clear, but the sharing is still there. Or we could say that after a clk is given to pm_clk_add_clk() that the caller shouldn't touch it anymore, like shmobile is doing right now. Then there's nothing to do besides remove the extra __clk_get() call in the pm layer.

Review static void __clk_release(struct kref *ref) · line 2616

Quoted 12 lines
(quoted diff) +7 -0
@@ -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))

Why did we grow a pair of parenthesis?

Review static void __clk_release(struct kref *ref) · line 2624

Quoted 8 lines
(quoted diff) +5 -0
+		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
drivers/base/power/clock_ops.c +7 -3
index 652b5a367c1f..ef62bb3d7b26 100644
@@ -31,6 +31,7 @@ struct pm_clock_entry {
 	char *con_id;
 	struct clk *clk;
 	enum pce_status status;
+	bool needs_clk_put;
 };
 
 /**
@@ -59,8 +60,10 @@ static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce
  */
 static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
 {
-	if (!ce->clk)
+	if (!ce->clk) {
 		ce->clk = clk_get(dev, ce->con_id);
+		ce->needs_clk_put = true;
+	}
 	if (IS_ERR(ce->clk)) {
 		ce->status = PCE_STATUS_ERROR;
 	} else {
@@ -93,7 +96,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;
 		}
@@ -149,7 +152,8 @@ static void __pm_clk_remove(struct pm_clock_entry *ce)
 
 		if (ce->status >= PCE_STATUS_ACQUIRED) {
 			clk_unprepare(ce->clk);
-			clk_put(ce->clk);
+			if (ce->needs_clk_put)
+				clk_put(ce->clk);
 		}
 	}
 
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

Re: [PATCH] clk: readd refcounting for struct clk instances [when used in pm_clk_add_clk(), genpd]

Hi Stephen,

Am Dienstag, 15. September 2015, 17:39:31 schrieb Stephen Boyd:

Quoted 4 lines · Heiko St=FCbner
Quoted 3 lines

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

Quoted 1 line
Quoted 1 line

call __clk_get, the first call to (__)clk_put will clear the struct=

clk,

Quoted 1 line
Quoted 1 line

making subsequent clk_put calls run into a NULL pointer dereference=

.

Quoted 2 lines
Quoted 2 lines

=20
One prime example of this sits in the generic power domain code, wh=

ere it

Quoted 1 line
Quoted 1 line

is possible to add the clocks both by name and by passing in a stru=

ct clk

Quoted 1 line
Quoted 1 line

via pm_clk_add_clk(). __pm_clk_add() in turn then calls __clk_get t=

o

Quoted 1 line
Quoted 1 line

increase the refcount, so that the original code can put the clock =

again.

Quoted 7 lines
Quoted 7 lines

=20
A possible call-path looks like
clk =3D of_clk_get();
pm_clk_add_clk(dev, clk);
clk_put(clk);
=20
with pm_clk_add_clk() =3D> __pm_clk_add() then calling __clk_get on=

the clk

Quoted 1 line
Quoted 1 line

and later clk_put when the pm clock list gets destroyed, thus creat=

ing

Quoted 3 lines
Quoted 3 lines

a NULL pointer deref, as the struct clk doesn't exist anymore. =20
So add a separate refcounting for struct clk instances and only cle=

an up

Quoted 5 lines
Quoted 5 lines

once the refcount reaches zero.
=20

---
I stumbled upon this while applying the new Rockchip power-domain d=

river,

Quoted 1 line
Quoted 1 line

but I guess the underlying issue is universal and probably present =

since

Quoted 14 lines
Quoted 1 line

the original clk/clk_core split, so this probably counts as fix.

=20
Ok. The fix makes sense, but I wonder why we do this. Perhaps we should stop exporting __clk_get() and __clk_put() to everything that uses clkdev in the kernel. They're called per-user clks for a reason. There's one user. Now we have two users of the same struct clk instance, so we have to refcount it too? I hope the shared clk instance isn't being used to set rates in two pieces of code.
=20
And this only affects pm_clk_add_clk() callers right? So the only caller in the kernel (drivers/clk/shmobile/clk-mstp.c) doesn't seem to have this problem right now because it never calls clk_put() on the pointer it passes to pm_clk_add_clk().

As written above, I stumbled upon this with the new Rockchip power-doma= in=20
driver [0] which calls pm_clk_add_clk in its rockchip_pd_attach_dev() f= unction

[0] http://www.spinics.net/lists/kernel/msg2070432.html

Quoted 7 lines

So what if we did the patch below? This would rely on callers not calling clk_put() before calling pm_clk_remove() or pm_clk_destroy(), and the life cycle would be clear, but the sharing is still there. Or we could say that after a clk is given to pm_clk_add_clk() that the caller shouldn't touch it anymore, like shmobile is doing right now. Then there's nothing to do besides remove the extra __clk_get() call in the pm layer.

I guess that is the call of the genpd people (Rafael and Pavel accordin= g to=20
get_maintainers.pl). I'm very much fine with adapting the Rockchip powe= r-
domain driver as needed to new handling paradigms.

Personally I'd prefer your solution of making the initial handler do al= l the=20
getting and putting, as doing clk_get in the power-domain driver and re= lying=20
on clk_put being done in the genpd core feels awkward. Although that so= lution=20
would mean, the calling driver would also need to keep track of clocks,=
while=20
the current rockchip power-domain driver can just call clk_put after ha= nding=20
the clock of to genpd.

Quoted 16 lines

=20

Review

Quoted 13 lines
(quoted diff) +0 -0
@@ -2606,6 +2607,18 @@ static void __clk_release(struct kref *ref)

=20
=09kfree(core);
=20
}
=20

(quoted diff) +7 -0
+static void __clk_release(struct kref *ref)
+{
+=09struct clk *clk =3D container_of(ref, struct clk, ref);
+
+=09hlist_del(&clk->clks_node);
+=09if ((clk->min_rate > clk->core->req_rate ||
+=09     clk->max_rate < clk->core->req_rate))

=20
Why did we grow a pair of parenthesis?

Remnant of me moving code around, sorry about that.
As it seems you prefer a different solution, I'll refrain from sending = a fixed=20
version, till we decide which way to go :-).

Quoted 10 lines

=20

Quoted 9 lines

+=09=09clk_core_set_rate_nolock(clk->core, clk->core->req_rate);

(quoted diff) +4 -0
+
+=09kfree(clk);
+}
+

=20
/*
=20
* Empty clk_ops for unregistered clocks. These are used temporari=

ly

Quoted 1 line
Quoted 1 line

* after clk_unregister() was called on a clock and until last clo=

ck

drivers/base/power/cloc=

Quoted 2 lines

=20

drivers/base/power/cloc= renamed from drivers/base/power/clock_ops.c +0 -0

k_ops.c

drivers/base/power/clock_ops.c static inline void __pm_clk_enable(struct device *=

Quoted 12 lines

index 652b5a367c1f..ef62bb3d7b26 100644

drivers/base/power/clock_ops.c +1 -0
@@ -31,6 +31,7 @@ struct pm_clock_entry {
 =09char *con_id;
 =09struct clk *clk;
 =09enum pce_status status;
+=09bool needs_clk_put;
 };

=20
/**

drivers/base/power/clock_ops.c +0 -0
@@ -59,8 +60,10 @@ static inline void __pm_clk_enable(struct device *=

dev,

Quoted 2 lines

struct pm_clock_entry *ce */
static void pm_clk_acquire(struct device *dev, struct pm_clock_entry=

*ce)

drivers/base/power/clock_ops.c static int __pm_clk_add(struct device *dev, const c=

Quoted 10 lines

{
-=09if (!ce->clk)
+=09if (!ce->clk) {
=09=09ce->clk =3D clk_get(dev, ce->con_id);

+=09=09ce->needs_clk_put =3D true;
+=09}

=09if (IS_ERR(ce->clk)) {
=09=09ce->status =3D PCE_STATUS_ERROR;
=09} else {

drivers/base/power/clock_ops.c +0 -0
@@ -93,7 +96,7 @@ static int __pm_clk_add(struct device *dev, const c=

har

drivers/base/power/clock_ops.c static void __pm_clk_remove(struct pm_clock_entry=

Quoted 9 lines
drivers/base/power/clock_ops.c +1 -1
 *con_id, return -ENOMEM;
 =09=09}
 =09} else {
-=09=09if (IS_ERR(clk) || !__clk_get(clk)) {
+=09=09if (IS_ERR(clk)) {
 =09=09=09kfree(ce);
 =09=09=09return -ENOENT;
 =09=09}
@@ -149,7 +152,8 @@ static void __pm_clk_remove(struct pm_clock_entry=

*ce)

Quoted 8 lines

=20
=09=09if (ce->status >=3D PCE_STATUS_ACQUIRED) {
=09=09=09clk_unprepare(ce->clk);
-=09=09=09clk_put(ce->clk);

+=09=09=09if (ce->needs_clk_put)
+=09=09=09=09clk_put(ce->clk);

=09=09}
=09}

#3 lore raw

Re: [PATCH] clk: readd refcounting for struct clk instances [when used in pm_clk_add_clk(), genpd]

Hi Stephen,

Am Mittwoch, 16. September 2015, 11:18:05 schrieb Heiko Stübner:

Quoted 67 lines

Am Dienstag, 15. September 2015, 17:39:31 schrieb Stephen Boyd:

Quoted 42 lines · Heiko Stübner
Quoted 26 lines

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.

Quoted 6 lines

---
I stumbled upon this while applying the new Rockchip power-domain driver,
but I guess the underlying issue is universal and probably present since the original clk/clk_core split, so this probably counts as fix.

Ok. The fix makes sense, but I wonder why we do this. Perhaps we should stop exporting __clk_get() and __clk_put() to everything that uses clkdev in the kernel. They're called per-user clks for a reason. There's one user. Now we have two users of the same struct clk instance, so we have to refcount it too? I hope the shared clk instance isn't being used to set rates in two pieces of code.

Quoted 4 lines

And this only affects pm_clk_add_clk() callers right? So the only caller in the kernel (drivers/clk/shmobile/clk-mstp.c) doesn't seem to have this problem right now because it never calls clk_put() on the pointer it passes to pm_clk_add_clk().

As written above, I stumbled upon this with the new Rockchip power-domain driver [0] which calls pm_clk_add_clk in its rockchip_pd_attach_dev() function

[0] http://www.spinics.net/lists/kernel/msg2070432.html

Quoted 7 lines

So what if we did the patch below? This would rely on callers not calling clk_put() before calling pm_clk_remove() or pm_clk_destroy(), and the life cycle would be clear, but the sharing is still there. Or we could say that after a clk is given to pm_clk_add_clk() that the caller shouldn't touch it anymore, like shmobile is doing right now. Then there's nothing to do besides remove the extra __clk_get() call in the pm layer.

I guess that is the call of the genpd people (Rafael and Pavel according to get_maintainers.pl). I'm very much fine with adapting the Rockchip power- domain driver as needed to new handling paradigms.

Quoted 6 lines

Personally I'd prefer your solution of making the initial handler do all the getting and putting, as doing clk_get in the power-domain driver and relying on clk_put being done in the genpd core feels awkward. Although that solution would mean, the calling driver would also need to keep track of clocks, while the current rockchip power-domain driver can just call clk_put after handing the clock of to genpd.

after looking more into the suggested change, I'd like to retract that statement :-) .

For reference the code in question is at [0] and currently rockchip_pd_attach_dev() can just do (when expecting refcounting works)

	while ((clk = of_clk_get(dev->of_node, i++)) && !IS_ERR(clk)) {
		error = pm_clk_add_clk(dev, clk);
		clk_put(clk);
	}

effectively handing the clock off to the power-domain code.

These clocks are necessary to be on for the synchronous reset of the device when powerdomain state changes and thus get read from the device nodes as discussed during the 18 revision the powerdomain code took.

Now if the caller really needs to keep track of that, this would require duplicating large parts of the pm-clock handling, like the keeping track of what devices actually got attached to what power-domain, and of course all the clock references of each device, as they would need to be "put" in rockchip_pd_detach_dev() then.

Heiko

[0] https://git.kernel.org/cgit/linux/kernel/git/mmind/linux-rockchip.git/commit/?h=v4.4-armsoc/drivers&id=e329c10ed7f244da48fa17ab85fb266bf5bbebcc