Re: [PATCH v2] clk: rp1: allow the clock provider to be probed again

From: Brian Masney

Date: Mon Nov 23 2026 - 19:44:15 EST


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@xxxxxxxxxx>

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