[PATCH] clk: Allow handing out a clk_hw before registration is complete
From: Brian Masney
Date: Mon Nov 30 2026 - 19:02:27 EST
A clk provider can register one or more clocks early using
CLK_OF_DECLARE_DRIVER() for clocks needed before the driver model is up,
add its OF provider there, and register the remaining clocks later from a
regular platform driver. Assigning those clk_hws into the already
registered provider's table (e.g. clk_hw_onecell_data) before or after
registering the clocks causes separate issues:
- If the clk_hw is assigned before the clock is registered, a consumer
can look it up before __clk_core_init() has set hw->core.
clk_hw_create_clk() reads hw->core without a lock and dereferences
core->owner, so the consumer oopses on a NULL pointer. If
__clk_core_init() then fails, the core can be freed while a consumer
is using it.
- If the clk_hw is assigned after the clock is registered, the orphans
waiting on it are not reparented until the next time that a new clk
is registered.
Address the first issue by having clk_hw_create_clk() read hw->core
under the clk prepare lock, where it is either NULL or fully
initialized. If it's NULL, return -EPROBE_DEFER. Also move the
kref_get() inside the prepare lock so that the clk_core is not freed
prematurely.
Since such a provider writes to its table without holding of_clk_mutex,
add a READ_ONCE() to of_clk_hw_onecell_get() that providers can pair
with a WRITE_ONCE().
Providers that fill their table before adding their provider are not
affected.
Signed-off-by: Brian Masney <masneyb@xxxxxxxxxx>
---
drivers/clk/clk.c | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 673eac47450b..5cea1b6ad665 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -4246,6 +4246,8 @@ static void free_clk(struct clk *clk)
kfree(clk);
}
+static void __clk_release(struct kref *ref);
+
/**
* clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
* a clk_hw
@@ -4258,6 +4260,10 @@ static void free_clk(struct clk *clk)
* This is the main function used to create a clk pointer for use by clk
* consumers. It connects a consumer to the clk_core and clk_hw structures
* used by the framework and clk provider respectively.
+ *
+ * A provider may hand out a clk_hw before it has finished registering it, so
+ * that clocks registering at the same time can find it as their parent. Such
+ * a clk_hw has no clk_core yet, and the consumer gets -EPROBE_DEFER.
*/
struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
const char *dev_id, const char *con_id,
@@ -4270,10 +4276,18 @@ struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
if (IS_ERR_OR_NULL(hw))
return ERR_CAST(hw);
+ clk_prepare_lock();
core = hw->core;
+ if (!core) {
+ clk_prepare_unlock();
+ return ERR_PTR(-EPROBE_DEFER);
+ }
+ kref_get(&core->ref);
+ clk_prepare_unlock();
+
clk = alloc_clk(core, dev_id, con_id);
if (IS_ERR(clk))
- return clk;
+ goto err_put_core;
clk->dev = dev;
/*
@@ -4284,12 +4298,17 @@ struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
clk->pinning = owner != core->owner;
if (clk->pinning && !try_module_get(core->owner)) {
free_clk(clk);
- return ERR_PTR(-ENOENT);
+ clk = ERR_PTR(-ENOENT);
+ goto err_put_core;
}
- kref_get(&core->ref);
clk_core_link_consumer(core, clk);
+ return clk;
+
+err_put_core:
+ kref_put(&core->ref, __clk_release);
+
return clk;
}
@@ -5084,7 +5103,7 @@ of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
return ERR_PTR(-EINVAL);
}
- return hw_data->hws[idx];
+ return READ_ONCE(hw_data->hws[idx]);
}
EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
--
2.56.1