Re: [PATCH v2] clk: rp1: allow the clock provider to be probed again
From: Brian Masney
Date: Mon Nov 23 2026 - 12:41:18 EST
Hi Joshua,
> The clock descriptors are static. The clock core clears hw.init when a
> clock is registered. When RP1 is unbound and bound again, the second
> probe registers the clocks without init data and oopses:
>
> pc : __clk_register+0x5c/0x9dc
> Call trace:
> __clk_register+0x5c/0x9dc (P)
> devm_clk_hw_register+0x5c/0xe0
> rp1_register_pll+0x24/0x40
> rp1_clk_probe+0xc0/0x168
>
> The clock core also leaves hw.core pointing to the freed clk_core on
> unregister. Some clocks refer to a parent that is registered after
> them, e.g. clk_mipi0_dpi to clksrc_mipi0_dsi_byteclk. On the second
> probe, their parent lookup can use the stale clk_core.
>
> Restore hw.init and clear the stale state of all descriptors before
> registering them.
>
> Fixes: 6486341721a2 ("clk: rp1: Add support for clocks provided by RP1")
> Signed-off-by: Joshua Covington <joshuacov@xxxxxxxxx>
There's presumably other drivers that will have the same issue as well.
Rather than having drivers all try to fix this themselves, let's see if
we can fix this in the core for everyone. Can you try this completely
untested patch? If that works, then you can take it over. It will need a
kunit test.
Brian
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 673eac47450b..8e5b530d80b5 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -119,6 +119,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;
@@ -4450,6 +4451,7 @@ __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;
+ core->hw_init = init;
core->flags = init->flags;
core->num_parents = init->num_parents;
core->min_rate = 0;
@@ -4498,6 +4500,10 @@ __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);
}
+
+ /* Leave @hw as it was handed to us so that it can be registered again */
+ hw->init = init;
+
return ERR_PTR(ret);
}
@@ -4654,6 +4660,7 @@ 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;
@@ -4702,6 +4709,21 @@ void clk_unregister(struct clk *clk)
if (clk->core->protect_count)
pr_warn("%s: unregistering protected clock: %s\n",
__func__, clk->core->name);
+
+ /*
+ * Return the clk_hw to the state it had before registration. Drivers
+ * with statically allocated clk_hw structures can then register them
+ * again, e.g. when the device is unbound and bound again. This also
+ * makes sure that a clk_parent_data.hw reference to this clk_hw is
+ * treated as an orphan rather than resolved to the stale clk_core.
+ */
+ 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 a355d1ee5189..a4c60ea06361 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -359,7 +359,9 @@ 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 or when the clock is unregistered, so the clk_hw can
+ * be registered again as long as the init data it points to is still valid.
*/
struct clk_hw {
struct clk_core *core;
--
Brian Masney <masneyb@xxxxxxxxxx>