public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module
@ 2023-04-13 21:39 Heiner Kallweit
  2023-04-13 22:29 ` Stephen Boyd
  0 siblings, 1 reply; 5+ messages in thread
From: Heiner Kallweit @ 2023-04-13 21:39 UTC (permalink / raw)
  To: Michael Turquette, Stephen Boyd; +Cc: linux-clk, Martin Blumenstingl

With clk_hw_create_clk() we have the problem that module unloading
is impossible if consumer and provider module owner are the same and
refcount is incremented. See also following comment in __clk_register().

/*
 * Don't call clk_hw_create_clk() here because that would pin the
 * provider module to itself and prevent it from ever being removed.
 */

I think this also affects any usage of clk_hw_get_clk(). To deal with
this let's increment the refcount only if owners are different.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/clk/clk.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 27c30a533..e9bf961d4 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -103,6 +103,7 @@ struct clk {
 	unsigned long max_rate;
 	unsigned int exclusive_count;
 	struct hlist_node clks_node;
+	bool put_core_owner;
 };
 
 /***           runtime pm          ***/
@@ -3969,6 +3970,7 @@ struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
 {
 	struct clk *clk;
 	struct clk_core *core;
+	struct module *owner;
 
 	/* This is to allow this function to be chained to others */
 	if (IS_ERR_OR_NULL(hw))
@@ -3980,9 +3982,18 @@ struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
 		return clk;
 	clk->dev = dev;
 
-	if (!try_module_get(core->owner)) {
-		free_clk(clk);
-		return ERR_PTR(-ENOENT);
+	owner = (dev && dev->driver) ? dev->driver->owner : NULL;
+	/*
+	 * Avoid being unable to remove module if consumer and
+	 * provider have the same owner.
+	 */
+	if (owner != core->owner) {
+		if (try_module_get(core->owner)) {
+			clk->put_core_owner = true;
+		} else {
+			free_clk(clk);
+			return ERR_PTR(-ENOENT);
+		}
 	}
 
 	kref_get(&core->ref);
@@ -4560,7 +4571,8 @@ void __clk_put(struct clk *clk)
 
 	clk_prepare_unlock();
 
-	module_put(owner);
+	if (clk->put_core_owner)
+		module_put(owner);
 
 	free_clk(clk);
 }
-- 
2.40.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module
  2023-04-13 21:39 [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module Heiner Kallweit
@ 2023-04-13 22:29 ` Stephen Boyd
  2023-04-14  6:01   ` Heiner Kallweit
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Boyd @ 2023-04-13 22:29 UTC (permalink / raw)
  To: Heiner Kallweit, Michael Turquette; +Cc: linux-clk, Martin Blumenstingl

Quoting Heiner Kallweit (2023-04-13 14:39:28)
> With clk_hw_create_clk() we have the problem that module unloading
> is impossible if consumer and provider module owner are the same and
> refcount is incremented. See also following comment in __clk_register().

Do you never call clk_put() on the clk that you get from
clk_hw_create_clk()?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module
  2023-04-13 22:29 ` Stephen Boyd
@ 2023-04-14  6:01   ` Heiner Kallweit
  2023-04-18  0:43     ` Stephen Boyd
  0 siblings, 1 reply; 5+ messages in thread
From: Heiner Kallweit @ 2023-04-14  6:01 UTC (permalink / raw)
  To: Stephen Boyd, Michael Turquette; +Cc: linux-clk, Martin Blumenstingl

On 14.04.2023 00:29, Stephen Boyd wrote:
> Quoting Heiner Kallweit (2023-04-13 14:39:28)
>> With clk_hw_create_clk() we have the problem that module unloading
>> is impossible if consumer and provider module owner are the same and
>> refcount is incremented. See also following comment in __clk_register().
> 
> Do you never call clk_put() on the clk that you get from
> clk_hw_create_clk()?

In my case clk_put() is called from a devm release hook. Same issue
we'd have if clk_put would be called from the drivers remove callback.
clk_put would be unreachable because the incremented module refcount
prevents module removal.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module
  2023-04-14  6:01   ` Heiner Kallweit
@ 2023-04-18  0:43     ` Stephen Boyd
  2023-04-18 21:03       ` Heiner Kallweit
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Boyd @ 2023-04-18  0:43 UTC (permalink / raw)
  To: Heiner Kallweit, Michael Turquette; +Cc: linux-clk, Martin Blumenstingl

Quoting Heiner Kallweit (2023-04-13 23:01:13)
> On 14.04.2023 00:29, Stephen Boyd wrote:
> > Quoting Heiner Kallweit (2023-04-13 14:39:28)
> >> With clk_hw_create_clk() we have the problem that module unloading
> >> is impossible if consumer and provider module owner are the same and
> >> refcount is incremented. See also following comment in __clk_register().
> > 
> > Do you never call clk_put() on the clk that you get from
> > clk_hw_create_clk()?
> 
> In my case clk_put() is called from a devm release hook. Same issue
> we'd have if clk_put would be called from the drivers remove callback.
> clk_put would be unreachable because the incremented module refcount
> prevents module removal.
> 

Ok. You could unbind the device in sysfs though, right?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module
  2023-04-18  0:43     ` Stephen Boyd
@ 2023-04-18 21:03       ` Heiner Kallweit
  0 siblings, 0 replies; 5+ messages in thread
From: Heiner Kallweit @ 2023-04-18 21:03 UTC (permalink / raw)
  To: Stephen Boyd, Michael Turquette; +Cc: linux-clk, Martin Blumenstingl

On 18.04.2023 02:43, Stephen Boyd wrote:
> Quoting Heiner Kallweit (2023-04-13 23:01:13)
>> On 14.04.2023 00:29, Stephen Boyd wrote:
>>> Quoting Heiner Kallweit (2023-04-13 14:39:28)
>>>> With clk_hw_create_clk() we have the problem that module unloading
>>>> is impossible if consumer and provider module owner are the same and
>>>> refcount is incremented. See also following comment in __clk_register().
>>>
>>> Do you never call clk_put() on the clk that you get from
>>> clk_hw_create_clk()?
>>
>> In my case clk_put() is called from a devm release hook. Same issue
>> we'd have if clk_put would be called from the drivers remove callback.
>> clk_put would be unreachable because the incremented module refcount
>> prevents module removal.
>>
> 
> Ok. You could unbind the device in sysfs though, right?

I *think* this should be possible, right.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-18 21:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-13 21:39 [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module Heiner Kallweit
2023-04-13 22:29 ` Stephen Boyd
2023-04-14  6:01   ` Heiner Kallweit
2023-04-18  0:43     ` Stephen Boyd
2023-04-18 21:03       ` Heiner Kallweit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox