* [PATCH v2] clk: rp1: allow the clock provider to be probed again
@ 2026-11-21 18:16 Joshua Covington
2026-11-23 17:41 ` Brian Masney
0 siblings, 1 reply; 5+ messages in thread
From: Joshua Covington @ 2026-11-21 18:16 UTC (permalink / raw)
To: Stephen Boyd
Cc: Brian Masney, Jerome Brunet, Andrea della Porta, linux-clk,
linux-rpi-kernel, linux-kernel, Joshua Covington
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@gmail.com>
---
v2:
- Also clear hw.core, div.hw.core and cached_rate of all descriptors
before registering them (Sashiko review)
v1: https://lore.kernel.org/all/20261121163915.86681-1-joshuacov@gmail.com/
drivers/clk/clk-rp1.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/drivers/clk/clk-rp1.c b/drivers/clk/clk-rp1.c
index 1dd276dd2b..c261306dcd 100644
--- a/drivers/clk/clk-rp1.c
+++ b/drivers/clk/clk-rp1.c
@@ -363,6 +363,7 @@ struct rp1_clk_desc {
struct rp1_clk_desc *desc);
const void *data;
struct clk_hw hw;
+ const struct clk_init_data *init;
struct rp1_clockman *clockman;
unsigned long cached_rate;
struct clk_divider div;
@@ -2416,6 +2417,18 @@ static int rp1_clk_probe(struct platform_device *pdev)
clockman->onecell.num = asize;
hws = clockman->onecell.hws;
+ for (i = 0; i < asize; i++) {
+ desc = clk_desc_array[i];
+ if (!desc)
+ continue;
+ if (!desc->init)
+ desc->init = desc->hw.init;
+ desc->hw.init = desc->init;
+ desc->hw.core = NULL;
+ desc->div.hw.core = NULL;
+ desc->cached_rate = 0;
+ }
+
for (i = 0; i < asize; i++) {
desc = clk_desc_array[i];
if (desc && desc->clk_register && desc->data)
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2] clk: rp1: allow the clock provider to be probed again 2026-11-21 18:16 [PATCH v2] clk: rp1: allow the clock provider to be probed again Joshua Covington @ 2026-11-23 17:41 ` Brian Masney 2026-11-23 23:38 ` Joshua Covington 0 siblings, 1 reply; 5+ messages in thread From: Brian Masney @ 2026-11-23 17:41 UTC (permalink / raw) To: Joshua Covington Cc: Stephen Boyd, Brian Masney, Jerome Brunet, Andrea della Porta, linux-clk, linux-rpi-kernel, linux-kernel, Brian Masney 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@gmail.com> 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@kernel.org> ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] clk: rp1: allow the clock provider to be probed again 2026-11-23 17:41 ` Brian Masney @ 2026-11-23 23:38 ` Joshua Covington 2026-11-24 0:44 ` Brian Masney 0 siblings, 1 reply; 5+ messages in thread From: Joshua Covington @ 2026-11-23 23:38 UTC (permalink / raw) To: masneyb Cc: sboyd, bmasney+clk, jbrunet+clk, andrea.porta, linux-clk, linux-rpi-kernel, linux-kernel Hi Brian, On Mon, Nov 23, 2026 at 12:41:10PM -0500, Brian Masney wrote: > 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. Thanks a lot, it works. I tested it on my Raspberry Pi CM5 (currently 6.18.64 with the Raspberry Pi downstream patches) with your patch instead of the clk-rp1 change. Unbinding and binding the RP1 PCI device again, which oopsed in __clk_register() before, now brings all RP1 peripherals back without any warning. I also wrote three KUnit tests for the following: - registering, unregistering and registering the same clk_hw again; - hw->init being restored when registration fails (-EEXIST on a duplicate name); - a parent referenced through clk_parent_data.hw being an orphan after it has been unregistered, and reparented once it is registered again. They pass on the CM5 and on QEMU x86_64 with KASAN. Without your patch, the first two oops on the NULL init data, and the third reports a slab-use-after-free in __clk_register(). I noticed two things while reading it: - clk_unregister() now writes to the clk_hw. So far it only touched it through ops->terminate, so a driver that frees its clk_hw before unregistering it may now write to freed memory. - hw->init is also restored when the clk_init_data was on the stack of the registering function, so after unregistering it can point to a dead stack frame. That only matters for a driver that registers the clk_hw again without setting hw->init first, as the updated comment in clk-provider.h says. On the CM5, clk-rp1 also keeps a cached rate per clock that survives a rebind, so I added a small clk-rp1 patch that resets it on probe. I'll rebase on clk-next and send a series with your change, the tests and the clk-rp1 patch. How would you like to be credited for the core change? If you add your Signed-off-by, I'll keep you as the author; otherwise I'll use Suggested-by. Thanks, Joshua ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] clk: rp1: allow the clock provider to be probed again 2026-11-23 23:38 ` Joshua Covington @ 2026-11-24 0:44 ` Brian Masney 2026-11-24 0:48 ` Brian Masney 0 siblings, 1 reply; 5+ messages in thread From: Brian Masney @ 2026-11-24 0:44 UTC (permalink / raw) To: Joshua Covington Cc: masneyb, sboyd, bmasney+clk, jbrunet+clk, andrea.porta, linux-clk, linux-rpi-kernel, linux-kernel Hi Joshua, On Mon, Nov 23, 2026 at 11:38:27PM +0000, Joshua Covington wrote: > On Mon, Nov 23, 2026 at 12:41:10PM -0500, Brian Masney wrote: > > 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. > > Thanks a lot, it works. I tested it on my Raspberry Pi CM5 (currently > 6.18.64 with the Raspberry Pi downstream patches) with your patch > instead of the clk-rp1 change. Unbinding and binding the RP1 PCI device > again, which oopsed in __clk_register() before, now brings all RP1 > peripherals back without any warning. > > I also wrote three KUnit tests for the following: > > - registering, unregistering and registering the same clk_hw again; > - hw->init being restored when registration fails (-EEXIST on a > duplicate name); > - a parent referenced through clk_parent_data.hw being an orphan after > it has been unregistered, and reparented once it is registered again. > > They pass on the CM5 and on QEMU x86_64 with KASAN. Without your patch, > the first two oops on the NULL init data, and the third reports a > slab-use-after-free in __clk_register(). That all sounds good! > > I noticed two things while reading it: > > - clk_unregister() now writes to the clk_hw. So far it only touched it > through ops->terminate, so a driver that frees its clk_hw before > unregistering it may now write to freed memory. As far as I know drivers shouldn't do that, and it's a bug if they do. > - hw->init is also restored when the clk_init_data was on the stack of > the registering function, so after unregistering it can point to a > dead stack frame. This could potentially be a problem, however... > That only matters for a driver that registers the > clk_hw again without setting hw->init first, as the updated comment > in clk-provider.h says. All of the cases that I've seen across the various clk drivers the hw->init will be initialized again with data from the stack, so it shouldn't be an issue. I found there is a function object_is_on_stack() that could be used here, and if true, we could just set hw->init to NULL to avoid the issue of it reading stale data the next time the driver is bound. This will still assume that the next time it probes again, the caller sets hw->init again. But honestly, if that crops up, then the driver already has an issue that needs to be fixed. > On the CM5, clk-rp1 also keeps a cached rate per clock that survives a > rebind, so I added a small clk-rp1 patch that resets it on probe. > > I'll rebase on clk-next and send a series with your change, the tests > and the clk-rp1 patch. How would you like to be credited for the core > change? If you add your Signed-off-by, I'll keep you as the author; > otherwise I'll use Suggested-by. Sure... Signed-off-by: Brian Masney <masneyb@kernel.org> If you end up changing the core patch more, then feel free to take over authorship of the patch, and just list me as Co-developed-by. Brian ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] clk: rp1: allow the clock provider to be probed again 2026-11-24 0:44 ` Brian Masney @ 2026-11-24 0:48 ` Brian Masney 0 siblings, 0 replies; 5+ messages in thread From: Brian Masney @ 2026-11-24 0:48 UTC (permalink / raw) To: Joshua Covington Cc: masneyb, sboyd, bmasney+clk, jbrunet+clk, andrea.porta, linux-clk, linux-rpi-kernel, linux-kernel On Mon, Nov 23, 2026 at 07:44:04PM -0500, Brian Masney wrote: > On Mon, Nov 23, 2026 at 11:38:27PM +0000, Joshua Covington wrote: > > I'll rebase on clk-next and send a series with your change, the tests > > and the clk-rp1 patch. How would you like to be credited for the core > > change? If you add your Signed-off-by, I'll keep you as the author; > > otherwise I'll use Suggested-by. > > Sure... > > Signed-off-by: Brian Masney <masneyb@kernel.org> > > If you end up changing the core patch more, then feel free to take over > authorship of the patch, and just list me as Co-developed-by. Also that's going to need a commit message, and some code changes, so feel free to add yourself as well with Co-developed-by if you end up listing me as the author. Brian ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 0:48 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-21 18:16 [PATCH v2] clk: rp1: allow the clock provider to be probed again Joshua Covington 2026-11-23 17:41 ` Brian Masney 2026-11-23 23:38 ` Joshua Covington 2026-11-24 0:44 ` Brian Masney 2026-11-24 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