public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
From: Heiner Kallweit <hkallweit1@gmail.com>
To: Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>
Cc: linux-clk@vger.kernel.org,
	Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Subject: [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module
Date: Thu, 13 Apr 2023 23:39:28 +0200	[thread overview]
Message-ID: <4eb4755b-7a06-6cd9-7c9d-6d088d05ab19@gmail.com> (raw)

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


             reply	other threads:[~2023-04-13 21:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13 21:39 Heiner Kallweit [this message]
2023-04-13 22:29 ` [PATCH] clk: change clk_hw_create_clk() to avoid being unable to remove module Stephen Boyd
2023-04-14  6:01   ` Heiner Kallweit
2023-04-18  0:43     ` Stephen Boyd
2023-04-18 21:03       ` Heiner Kallweit

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4eb4755b-7a06-6cd9-7c9d-6d088d05ab19@gmail.com \
    --to=hkallweit1@gmail.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=mturquette@baylibre.com \
    --cc=sboyd@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox