From: Joshua Covington <joshuacov@gmail.com>
To: Stephen Boyd <sboyd@kernel.org>, Brian Masney <masneyb@kernel.org>
Cc: Brian Masney <bmasney+clk@redhat.com>,
Jerome Brunet <jbrunet+clk@baylibre.com>,
Andrea della Porta <andrea.porta@suse.com>,
Florian Fainelli <florian.fainelli@broadcom.com>,
Sylwester Nawrocki <s.nawrocki@samsung.com>,
linux-clk@vger.kernel.org, linux-rpi-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org,
Joshua Covington <joshuacov@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it
Date: Tue, 24 Nov 2026 23:17:49 +0000 [thread overview]
Message-ID: <20261124231751.83938-2-joshuacov@gmail.com> (raw)
In-Reply-To: <20261124231751.83938-1-joshuacov@gmail.com>
__clk_register() sets hw->init to NULL, and clk_unregister() leaves
hw->core and hw->clk pointing to the clk_core and clk it is about to
release. A driver with statically allocated clk_hw structures can
therefore not register them again, e.g. when its device is unbound and
bound again. The second registration has no init data and oopses. A
clk_parent_data.hw reference to such a clk_hw also resolves to the
freed clk_core of the previous registration.
Keep the init pointer in the clk_core, and put it back into the clk_hw
when the registration fails and when the clock is unregistered. Init
data on the stack of the registering function is gone by the time the
clock is unregistered, so leave hw->init NULL in that case. This has to
be checked at registration, as object_is_on_stack() only looks at the
stack of the current task. Also clear hw->core and hw->clk on
unregistration, so that a reference to an unregistered clk_hw is
treated as an orphan.
Fixes: 0214f33c4e0e ("clk: Overwrite clk_hw::init with NULL during clk_register()")
Link: https://lore.kernel.org/all/179545567027.246915.947299820828330388.b4-review@b4/
Cc: stable@vger.kernel.org
Co-developed-by: Brian Masney <masneyb@kernel.org>
Signed-off-by: Brian Masney <masneyb@kernel.org>
Signed-off-by: Joshua Covington <joshuacov@gmail.com>
---
drivers/clk/clk.c | 23 +++++++++++++++++++++++
include/linux/clk-provider.h | 5 ++++-
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 4d2cc86bfb..c19ae1812c 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -20,6 +20,7 @@
#include <linux/of.h>
#include <linux/pm_runtime.h>
#include <linux/sched.h>
+#include <linux/sched/task_stack.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
@@ -70,6 +71,9 @@ struct clk_parent_map {
* @ops: Pointer to hardware-specific operations for this clk.
* @hw: Pointer for traversing from a struct clk to its
* corresponding hardware-specific structure.
+ * @hw_init: Init data of @hw, put back into @hw when the
+ * registration fails or the clk is unregistered. NULL
+ * if the init data was on the stack.
* @owner: Kernel module owning this clk (for reference counting).
* @dev: Device associated with this clk (optional)
* @rpm_node: Node for runtime power management list management.
@@ -119,6 +123,7 @@ struct clk_core {
const char *name;
const struct clk_ops *ops;
struct clk_hw *hw;
+ const struct clk_init_data *hw_init;
struct module *owner;
struct device *dev;
struct hlist_node rpm_node;
@@ -4457,6 +4462,8 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
if (dev && dev->driver)
core->owner = dev->driver->owner;
core->hw = hw;
+ /* Stack init data is gone once the registering function returns. */
+ core->hw_init = object_is_on_stack(init) ? NULL : init;
core->flags = init->flags;
core->num_parents = init->num_parents;
core->min_rate = 0;
@@ -4505,6 +4512,9 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
pr_err("%pOF: error %pe: failed to register clk '%s' (%pS)\n",
np, ERR_PTR(ret), init->name, hw);
}
+
+ hw->init = init;
+
return ERR_PTR(ret);
}
@@ -4656,11 +4666,16 @@ static void clk_core_evict_parent_cache(struct clk_core *core)
/**
* clk_unregister - unregister a currently registered clock
* @clk: clock to unregister
+ *
+ * The clk_hw of @clk is returned to its state before registration, so
+ * that it can be registered again. Until then, a clk_parent_data.hw
+ * reference to it is treated as an orphan.
*/
void clk_unregister(struct clk *clk)
{
unsigned long flags;
const struct clk_ops *ops;
+ struct clk_hw *hw;
if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
return;
@@ -4709,6 +4724,14 @@ void clk_unregister(struct clk *clk)
if (clk->core->protect_count)
pr_warn("%s: unregistering protected clock: %s\n",
__func__, clk->core->name);
+
+ hw = clk->core->hw;
+ if (!WARN_ON(hw->core != clk->core || hw->clk != clk)) {
+ hw->core = NULL;
+ hw->init = clk->core->hw_init;
+ hw->clk = NULL;
+ }
+
clk_prepare_unlock();
kref_put(&clk->core->ref, __clk_release);
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index a355d1ee51..026efed876 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -359,7 +359,10 @@ struct clk_init_data {
*
* @init: pointer to struct clk_init_data that contains the init data shared
* with the common clock framework. This pointer will be set to NULL once
- * a clk_register() variant is called on this clk_hw pointer.
+ * a clk_register() variant is called on this clk_hw pointer. It is restored
+ * if registration fails, and when the clock is unregistered unless the init
+ * data was on the stack of the registering function. The clk_hw can then be
+ * registered again as long as the init data it points to is still valid.
*/
struct clk_hw {
struct clk_core *core;
--
2.56.0
next prev parent reply other threads:[~2026-11-24 23:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 23:17 [PATCH v3 0/3] " Joshua Covington
2026-11-24 23:17 ` Joshua Covington [this message]
2026-11-24 23:17 ` [PATCH v3 2/3] clk: tests: add tests for registering a clk_hw again Joshua Covington
2026-11-24 23:17 ` [PATCH v3 3/3] clk: rp1: reset cached rates on probe Joshua Covington
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=20261124231751.83938-2-joshuacov@gmail.com \
--to=joshuacov@gmail.com \
--cc=andrea.porta@suse.com \
--cc=bmasney+clk@redhat.com \
--cc=florian.fainelli@broadcom.com \
--cc=jbrunet+clk@baylibre.com \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rpi-kernel@lists.infradead.org \
--cc=masneyb@kernel.org \
--cc=s.nawrocki@samsung.com \
--cc=sboyd@kernel.org \
--cc=stable@vger.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
all inboxes | Powered by JetHome®