Skip to contentSkip to conversation

clk: rp1: allow the clock provider to be probed again

PATCH In review Joshua Covington 3 participants 5 messages Nov 21 – Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 clk: rp1: allow the clock provider to be probed again

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")

---
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/[email protected]/

 drivers/clk/clk-rp1.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/clk/clk-rp1.c +13 -0
index 1dd276dd2b..c261306dcd 100644
@@ -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)
Signature · 1 line
2.56.0
#1 lore raw

Hi Joshua,

Quoted 16 lines

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.

Quoted 5 lines

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")

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

drivers/clk/clk.c +22 -0
index 673eac47450b..8e5b530d80b5 100644
@@ -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);
include/linux/clk-provider.h +3 -1
index a355d1ee5189..a4c60ea06361 100644
@@ -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;
Signature · 1 line
Brian Masney <[email protected]>
#2 lore raw

Hi Brian,

Quoted 5 lines · Brian Masney

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

#3 lore raw

Hi Joshua,

Quoted 21 lines · Joshua Covington
Quoted 5 lines · Brian Masney

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.

Quoted 3 lines

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!

Quoted 6 lines

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.

Quoted 3 lines

- 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...

Quoted 3 lines

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.

Quoted 7 lines

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...

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

#4 lore raw
Quoted 6 lines · Brian Masney
Quoted 4 lines · Joshua Covington

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.

Quoted 6 lines

Sure...

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