Skip to contentSkip to conversation

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

PATCH Superseded Joshua Covington 2 participants 3 messages Nov 21

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

The clock descriptors are static and are registered directly. The clock framework clears hw.init once a clock has been registered, so when the RP1 device is unbound and bound again, the second probe registers 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

Keep the init data of each descriptor and restore it before the clock is registered.

Fixes 6486341721a2 ("clk: rp1: Add support for clocks provided by RP1")

---
Tested on a Raspberry Pi CM5 with 6.18.64 and the Raspberry Pi downstream patches, where binding rp1_pci again after unbinding it hit this. Applies unchanged to v7.4-rc3 and builds there without warnings.

 drivers/clk/clk-rp1.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/clk/clk-rp1.c +10 -2
index 1dd276dd2..5c3929e93 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;
@@ -2418,8 +2419,15 @@ static int rp1_clk_probe(struct platform_device *pdev)
 
 	for (i = 0; i < asize; i++) {
 		desc = clk_desc_array[i];
-		if (desc && desc->clk_register && desc->data)
-			hws[i] = desc->clk_register(clockman, desc);
+		if (!desc || !desc->clk_register || !desc->data)
+			continue;
+
+		/* The clock core clears hw.init of these static descriptors */
+		if (!desc->init)
+			desc->init = desc->hw.init;
+		desc->hw.init = desc->init;
+
+		hws[i] = desc->clk_register(clockman, desc);
 	}
 
 	clk_audio_core = &pll_audio_core_desc;
Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] clk: rp1: use-after-free in rp1_clk_probe() from stale hw.core on re-probe --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] clk: rp1: use-after-free in rp1_clk_probe() from stale hw.core on re-probe
--

commit 0c4e0577acfd5e28d80d2ef3e1cc9693b2d23c01
Author: Joshua Covington <[email protected]>

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

This commit saves and restores the clk_init_data pointer on each static rp1_clk_desc descriptor in rp1_clk_probe() so the driver can register its clocks again after an unbind and rebind.

Quoted 18 lines
drivers/clk/clk-rp1.c +1 -2
index 1dd276dd2bb3e..5c3929e935fce 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;
@@ -2418,8 +2419,15 @@ static int rp1_clk_probe(struct platform_device *pdev)
 
 	for (i = 0; i < asize; i++) {
 		desc = clk_desc_array[i];
-		if (desc && desc->clk_register && desc->data)
-			hws[i] = desc->clk_register(clockman, desc);

drivers/clk/clk-rp1.c static int rp1_clk_probe(struct platform_device *pdev) · line 2431

Quoted 10 lines
drivers/clk/clk-rp1.c +9 -0
+		if (!desc || !desc->clk_register || !desc->data)
+			continue;
+
+		/* The clock core clears hw.init of these static descriptors */
+		if (!desc->init)
+			desc->init = desc->hw.init;
+		desc->hw.init = desc->init;
+
+		hws[i] = desc->clk_register(clockman, desc);
 	}

[Severity: High]
Can re-probing cause a use-after-free on a freed struct clk_core when a clock references a parent at a higher index in clk_desc_array?

When the device is unbound, clk_unregister() frees each clock's struct clk_core without clearing hw->core on the static descriptors. In clk_core_fill_parent_index(), the clock core checks entry->hw->core and assumes a non-NULL pointer is an already-registered parent.

Several descriptors in clk_desc_array reference parents via .hw that sit at higher array indices, such as clk_mipi0_dpi_desc (RP1_CLK_MIPI0_DPI) referencing clksrc_mipi0_dsi_byteclk_desc (RP1_CLK_MIPI0_DSI_BYTECLOCK). Because rp1_clk_probe() registers clocks in a single pass without first clearing desc->hw.core (and desc->div.hw.core) across clk_desc_array, if the hardware mux selects one of those higher-indexed parents during re-probe, __clk_init_parent() -> clk_core_fill_parent_index() returns the dangling clk_core pointer from the previous bind and __clk_core_init() links into parent->children on freed memory.

Clearing desc->hw.core and desc->div.hw.core for all descriptors before the registration loop would avoid this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1