* [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it [not found] <20261124231751.83938-1-joshuacov@gmail.com> @ 2026-11-24 23:17 ` Joshua Covington 2026-12-03 10:26 ` Jerome Brunet 0 siblings, 1 reply; 6+ messages in thread From: Joshua Covington @ 2026-11-24 23:17 UTC (permalink / raw) To: Stephen Boyd, Brian Masney Cc: Brian Masney, Jerome Brunet, Andrea della Porta, Florian Fainelli, Sylwester Nawrocki, linux-clk, linux-rpi-kernel, linux-kernel, Joshua Covington, stable __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 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it 2026-11-24 23:17 ` [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it Joshua Covington @ 2026-12-03 10:26 ` Jerome Brunet 2026-12-03 15:19 ` Joshua Covington 0 siblings, 1 reply; 6+ messages in thread From: Jerome Brunet @ 2026-12-03 10:26 UTC (permalink / raw) To: Joshua Covington Cc: Stephen Boyd, Brian Masney, Brian Masney, Jerome Brunet, Andrea della Porta, Florian Fainelli, Sylwester Nawrocki, linux-clk, linux-rpi-kernel, linux-kernel, stable, Jerome Brunet > __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> I understand what you are fixing here but it feels like we are not addressing the actual problem here, more hiding it under more complexity. It also eats more memory for every single clock on every platforms, for something we could still be storing elsewhere. The problem is clk_hw stores data it really should not. Because it should not, CCF poison it to make sure nobody does uses it afterward. IMO, the short term solution to the problem explained in this series is to stop poisoning the init data in clk_hw. The framework did not allocate the data, arguably it should not be wrecking it on purpose or otherwise touch it. Just leave it be. We should however put clear comment/warning that provider driver should NOT be using that or count on it being valid. We could maybe teach sashiko to pick up on this in reviews The longer term solution would be stop it storing altogther. I think that would mean adding a new registration API that takes the init data as parameter and provide the clk_hw in return ... them slowly convert everything, then drop it. -- Jerome ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it 2026-12-03 10:26 ` Jerome Brunet @ 2026-12-03 15:19 ` Joshua Covington 2026-12-03 16:36 ` Jerome Brunet 0 siblings, 1 reply; 6+ messages in thread From: Joshua Covington @ 2026-12-03 15:19 UTC (permalink / raw) To: Jerome Brunet Cc: Joshua Covington, Stephen Boyd, Brian Masney, Brian Masney, Jerome Brunet, Andrea della Porta, Florian Fainelli, Sylwester Nawrocki, linux-clk, linux-rpi-kernel, linux-kernel, stable Hi Jerome, Thanks for the review. On Thu, Dec 03, 2026 at 11:26:39AM +0100, Jerome Brunet wrote: > IMO, the short term solution to the problem explained in this series is to > stop poisoning the init data in clk_hw. The framework did not allocate the > data, arguably it should not be wrecking it on purpose or otherwise touch it. > Just leave it be. > > We should however put clear comment/warning that provider driver should NOT > be using that or count on it being valid. That works for me, and it will remove the new clk_core member and the object_is_on_stack() check. So far I have tried it on top of v3, with __clk_register() no longer clearing hw->init. The tests for registering again after unregistration and after a failed registration pass. In v4 I will also add to the clk_hw kernel-doc that providers must not use @init after registration, as it may point to init data that is gone. The other half of the patch still needs to stay, though. clk_unregister() leaves hw->core and hw->clk pointing to the clk_core and clk it releases. Without clearing them, a clk_parent_data.hw reference to an unregistered clk_hw resolves to the freed clk_core, and KASAN reports a slab-use-after-free in __clk_register() in the parent test. That part costs no memory, so I would keep it. Stephen, the hw->init poisoning was added by 0214f33c4e0e. Are you fine with dropping it? If so, I will send a v4 that does the above, with the test for init data on the stack removed. > The longer term solution would be stop it storing altogther. I think that > would mean adding a new registration API that takes the init data as parameter > and provide the clk_hw in return ... them slowly convert everything, then drop it. Agreed. That is beyond the scope of this fix, and the decision on it is for you and Stephen. Thanks, Joshua ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it 2026-12-03 15:19 ` Joshua Covington @ 2026-12-03 16:36 ` Jerome Brunet 2026-12-03 17:42 ` Joshua Covington 0 siblings, 1 reply; 6+ messages in thread From: Jerome Brunet @ 2026-12-03 16:36 UTC (permalink / raw) To: Joshua Covington Cc: Jerome Brunet, Stephen Boyd, Brian Masney, Brian Masney, Jerome Brunet, Andrea della Porta, Florian Fainelli, Sylwester Nawrocki, linux-clk, linux-rpi-kernel, linux-kernel, stable On 2026-12-03 15:19 +0000, Joshua Covington wrote: > Hi Jerome, > > Thanks for the review. > > On Thu, Dec 03, 2026 at 11:26:39AM +0100, Jerome Brunet wrote: > > IMO, the short term solution to the problem explained in this series is to > > stop poisoning the init data in clk_hw. The framework did not allocate the > > data, arguably it should not be wrecking it on purpose or otherwise touch it. > > Just leave it be. > > > > We should however put clear comment/warning that provider driver should NOT > > be using that or count on it being valid. > > That works for me, and it will remove the new clk_core member and the > object_is_on_stack() check. So far I have tried it on top of v3, with > __clk_register() no longer clearing hw->init. The tests for registering > again after unregistration and after a failed registration pass. In v4 > I will also add to the clk_hw kernel-doc that providers must not use > @init after registration, as it may point to init data that is gone. > > The other half of the patch still needs to stay, though. clk_unregister() > leaves hw->core and hw->clk pointing to the clk_core and clk it > releases. Without clearing them, a clk_parent_data.hw reference to an > unregistered clk_hw resolves to the freed clk_core, and KASAN reports a > slab-use-after-free in __clk_register() in the parent test. That part > costs no memory, so I would keep it. I'm not too the sure the WARN() condition is needed though. It is very late for such a consistency check and what it is checking should be guaranteed by the registration code, no ? Can't we just skip the check ? -- Jerome ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it 2026-12-03 16:36 ` Jerome Brunet @ 2026-12-03 17:42 ` Joshua Covington 2026-12-04 0:48 ` Brian Masney 0 siblings, 1 reply; 6+ messages in thread From: Joshua Covington @ 2026-12-03 17:42 UTC (permalink / raw) To: Jerome Brunet Cc: Joshua Covington, Stephen Boyd, Brian Masney, Brian Masney, Jerome Brunet, Andrea della Porta, Florian Fainelli, Sylwester Nawrocki, linux-clk, linux-rpi-kernel, linux-kernel, stable On Thu, Dec 03, 2026 at 05:36:41PM +0100, Jerome Brunet wrote: > I'm not too the sure the WARN() condition is needed though. > It is very late for such a consistency check and what it is checking should > be guaranteed by the registration code, no ? Can't we just skip the check ? Right. I took the check over from Brian's proposal, and it never triggered in my testing, neither in the KUnit runs nor on the CM5. __clk_register() sets both pointers and clears them again if the registration fails, so for the clk returned by the registration the check always passes. The only case it would catch is if clk_unregister() is being called with some other clk of the same clock, e.g. one from clk_hw_get_clk(), which clk_unregister() has never checked for either. Brian, unless you had a specific case in mind for it, I will drop it in v4 and clear hw->core and hw->clk unconditionally. Thanks, Joshua ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it 2026-12-03 17:42 ` Joshua Covington @ 2026-12-04 0:48 ` Brian Masney 0 siblings, 0 replies; 6+ messages in thread From: Brian Masney @ 2026-12-04 0:48 UTC (permalink / raw) To: Joshua Covington Cc: Jerome Brunet, Stephen Boyd, Brian Masney, Jerome Brunet, Andrea della Porta, Florian Fainelli, Sylwester Nawrocki, linux-clk, linux-rpi-kernel, linux-kernel, stable On Thu, Dec 03, 2026 at 05:42:18PM +0000, Joshua Covington wrote: > On Thu, Dec 03, 2026 at 05:36:41PM +0100, Jerome Brunet wrote: > > I'm not too the sure the WARN() condition is needed though. > > It is very late for such a consistency check and what it is checking should > > be guaranteed by the registration code, no ? Can't we just skip the check ? > > Right. I took the check over from Brian's proposal, and it never > triggered in my testing, neither in the KUnit runs nor on the CM5. > > __clk_register() sets both pointers and clears them again if the > registration fails, so for the clk returned by the registration the > check always passes. The only case it would catch is if > clk_unregister() is being called with some other clk of the same clock, > e.g. one from clk_hw_get_clk(), which clk_unregister() has never checked > for either. > > Brian, unless you had a specific case in mind for it, I will drop it in > v4 and clear hw->core and hw->clk unconditionally. Yes that sounds good. Brian ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-04 0:48 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261124231751.83938-1-joshuacov@gmail.com>
2026-11-24 23:17 ` [PATCH v3 1/3] clk: allow a clk_hw to be registered again after unregistering it Joshua Covington
2026-12-03 10:26 ` Jerome Brunet
2026-12-03 15:19 ` Joshua Covington
2026-12-03 16:36 ` Jerome Brunet
2026-12-03 17:42 ` Joshua Covington
2026-12-04 0:48 ` Brian Masney
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox