ARM Sunxi Platform Development
 help / color / mirror / Atom feed
* [PATCH 0/3] clk: sunxi: Pair platform clock setup with teardown
@ 2026-11-28  9:39 Guangshuo Li
  2026-11-28  9:39 ` [PATCH 1/3] clk: sunxi: Release the platform mod0 clock before unmapping it Guangshuo Li
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Guangshuo Li @ 2026-11-28  9:39 UTC (permalink / raw)
  To: emilio, sboyd, bmasney+clk, jbrunet+clk, wens, jernej.skrabec,
	samuel, mripard, hansg, bbrezillon
  Cc: linux-clk, linux-arm-kernel, linux-sunxi

Handle platform teardown for the mod0 and APB0 clocks and the APB0 gates
provider.

Guangshuo Li (3):
  clk: sunxi: Release the platform mod0 clock before unmapping it
  clk: sunxi: Unregister the A31 and A23 APB0 platform clocks
  clk: sunxi: Fix potential use-after-free of APB0 provider data

base-commit: d8f05ec001e838cdb94fc14efa7c411e676f9ab9

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] clk: sunxi: Release the platform mod0 clock before unmapping it
  2026-11-28  9:39 [PATCH 0/3] clk: sunxi: Pair platform clock setup with teardown Guangshuo Li
@ 2026-11-28  9:39 ` Guangshuo Li
  2026-11-28  9:39 ` [PATCH 2/3] clk: sunxi: Unregister the A31 and A23 APB0 platform clocks Guangshuo Li
  2026-11-28  9:39 ` [PATCH 3/3] clk: sunxi: Fix potential use-after-free of APB0 provider data Guangshuo Li
  2 siblings, 0 replies; 5+ messages in thread
From: Guangshuo Li @ 2026-11-28  9:39 UTC (permalink / raw)
  To: emilio, sboyd, bmasney+clk, jbrunet+clk, wens, jernej.skrabec,
	samuel, mripard, hansg, bbrezillon
  Cc: linux-clk, linux-arm-kernel, linux-sunxi

The mod0 platform driver leaves the composite clock and its OF provider
registered when devres unmaps the clock registers. Save the clock
created by the platform probe and unregister it on remove.

The unregister helper receives the composite clk_hw. Use its rate
component to recover the factors, then release the composite and its
parts.

This issue was found by our static analysis tool under development.

Fixes: 6ea3953da4e6 ("clk: sunxi: Make the mod0 clk driver also a platform driver")
Fixes: 4cbeaebb8af1 ("clk: sunxi: factors: Add unregister function")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/clk/sunxi/clk-factors.c |  5 ++---
 drivers/clk/sunxi/clk-mod0.c    | 12 ++++++++++--
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/clk/sunxi/clk-factors.c b/drivers/clk/sunxi/clk-factors.c
index 149187f189d6..14a84534011e 100644
--- a/drivers/clk/sunxi/clk-factors.c
+++ b/drivers/clk/sunxi/clk-factors.c
@@ -293,11 +293,10 @@ void sunxi_factors_unregister(struct device_node *node, struct clk *clk)
 	if (!hw)
 		return;
 
-	factors = to_clk_factors(hw);
+	factors = to_clk_factors(to_clk_composite(hw)->rate_hw);
 
 	of_clk_del_provider(node);
-	/* TODO: The composite clock stuff will leak a bit here. */
-	clk_unregister(clk);
+	clk_unregister_composite(clk);
 	kfree(factors->mux);
 	kfree(factors->gate);
 	kfree(factors);
diff --git a/drivers/clk/sunxi/clk-mod0.c b/drivers/clk/sunxi/clk-mod0.c
index b409a8d70962..e9ecdbe02fe8 100644
--- a/drivers/clk/sunxi/clk-mod0.c
+++ b/drivers/clk/sunxi/clk-mod0.c
@@ -89,6 +89,7 @@ static int sun4i_a10_mod0_clk_probe(struct platform_device *pdev)
 {
 	struct device_node *np = pdev->dev.of_node;
 	void __iomem *reg;
+	struct clk *clk;
 
 	if (!np)
 		return -ENODEV;
@@ -97,11 +98,17 @@ static int sun4i_a10_mod0_clk_probe(struct platform_device *pdev)
 	if (IS_ERR(reg))
 		return PTR_ERR(reg);
 
-	sunxi_factors_register(np, &sun4i_a10_mod0_data,
-			       &sun4i_a10_mod0_lock, reg);
+	clk = sunxi_factors_register(np, &sun4i_a10_mod0_data,
+				     &sun4i_a10_mod0_lock, reg);
+	platform_set_drvdata(pdev, clk);
 	return 0;
 }
 
+static void sun4i_a10_mod0_clk_remove(struct platform_device *pdev)
+{
+	sunxi_factors_unregister(pdev->dev.of_node, platform_get_drvdata(pdev));
+}
+
 static const struct of_device_id sun4i_a10_mod0_clk_dt_ids[] = {
 	{ .compatible = "allwinner,sun4i-a10-mod0-clk" },
 	{ /* sentinel */ }
@@ -113,6 +120,7 @@ static struct platform_driver sun4i_a10_mod0_clk_driver = {
 		.of_match_table = sun4i_a10_mod0_clk_dt_ids,
 	},
 	.probe = sun4i_a10_mod0_clk_probe,
+	.remove = sun4i_a10_mod0_clk_remove,
 };
 builtin_platform_driver(sun4i_a10_mod0_clk_driver);
 

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/3] clk: sunxi: Unregister the A31 and A23 APB0 platform clocks
  2026-11-28  9:39 [PATCH 0/3] clk: sunxi: Pair platform clock setup with teardown Guangshuo Li
  2026-11-28  9:39 ` [PATCH 1/3] clk: sunxi: Release the platform mod0 clock before unmapping it Guangshuo Li
@ 2026-11-28  9:39 ` Guangshuo Li
  2026-11-28  9:39 ` [PATCH 3/3] clk: sunxi: Fix potential use-after-free of APB0 provider data Guangshuo Li
  2 siblings, 0 replies; 5+ messages in thread
From: Guangshuo Li @ 2026-11-28  9:39 UTC (permalink / raw)
  To: emilio, sboyd, bmasney+clk, jbrunet+clk, wens, jernej.skrabec,
	samuel, mripard, hansg, bbrezillon
  Cc: linux-clk, linux-arm-kernel, linux-sunxi

The A31 and A23 APB0 platform drivers register a divider backed by a
device-managed mapping. The clock and its OF provider can remain live
after the mapping is released.

Manage both registrations on A31. On A23, pair the platform registration
with removal while keeping the early DT clock independent.

This issue was found by our static analysis tool under development.

Fixes: c8a76cac19ee ("clk: sunxi: add PRCM (Power/Reset/Clock Management) clks support")
Fixes: 57a1fbf28424 ("clk: sunxi: Add A23 APB0 divider clock support")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/clk/sunxi/clk-sun6i-apb0.c | 13 ++++++-------
 drivers/clk/sunxi/clk-sun8i-apb0.c | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/drivers/clk/sunxi/clk-sun6i-apb0.c b/drivers/clk/sunxi/clk-sun6i-apb0.c
index f80c67bafe38..a15a2319eee9 100644
--- a/drivers/clk/sunxi/clk-sun6i-apb0.c
+++ b/drivers/clk/sunxi/clk-sun6i-apb0.c
@@ -33,7 +33,7 @@ static int sun6i_a31_apb0_clk_probe(struct platform_device *pdev)
 	const char *clk_name = np->name;
 	const char *clk_parent;
 	void __iomem *reg;
-	struct clk *clk;
+	struct clk_hw *hw;
 
 	reg = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(reg))
@@ -45,13 +45,12 @@ static int sun6i_a31_apb0_clk_probe(struct platform_device *pdev)
 
 	of_property_read_string(np, "clock-output-names", &clk_name);
 
-	clk = clk_register_divider_table(&pdev->dev, clk_name, clk_parent,
-					 0, reg, 0, 2, 0, sun6i_a31_apb0_divs,
-					 NULL);
-	if (IS_ERR(clk))
-		return PTR_ERR(clk);
+	hw = devm_clk_hw_register_divider_table(&pdev->dev, clk_name, clk_parent,
+						0, reg, 0, 2, 0, sun6i_a31_apb0_divs, NULL);
+	if (IS_ERR(hw))
+		return PTR_ERR(hw);
 
-	return of_clk_add_provider(np, of_clk_src_simple_get, clk);
+	return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get, hw);
 }
 
 static const struct of_device_id sun6i_a31_apb0_clk_dt_ids[] = {
diff --git a/drivers/clk/sunxi/clk-sun8i-apb0.c b/drivers/clk/sunxi/clk-sun8i-apb0.c
index f605ecca879f..61d9b8a844af 100644
--- a/drivers/clk/sunxi/clk-sun8i-apb0.c
+++ b/drivers/clk/sunxi/clk-sun8i-apb0.c
@@ -95,7 +95,17 @@ static int sun8i_a23_apb0_clk_probe(struct platform_device *pdev)
 		return PTR_ERR(reg);
 
 	clk = sun8i_a23_apb0_register(np, reg);
-	return PTR_ERR_OR_ZERO(clk);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
+
+	platform_set_drvdata(pdev, clk);
+	return 0;
+}
+
+static void sun8i_a23_apb0_clk_remove(struct platform_device *pdev)
+{
+	of_clk_del_provider(pdev->dev.of_node);
+	clk_unregister_divider(platform_get_drvdata(pdev));
 }
 
 static const struct of_device_id sun8i_a23_apb0_clk_dt_ids[] = {
@@ -109,5 +119,6 @@ static struct platform_driver sun8i_a23_apb0_clk_driver = {
 		.of_match_table = sun8i_a23_apb0_clk_dt_ids,
 	},
 	.probe = sun8i_a23_apb0_clk_probe,
+	.remove = sun8i_a23_apb0_clk_remove,
 };
 builtin_platform_driver(sun8i_a23_apb0_clk_driver);

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/3] clk: sunxi: Fix potential use-after-free of APB0 provider data
  2026-11-28  9:39 [PATCH 0/3] clk: sunxi: Pair platform clock setup with teardown Guangshuo Li
  2026-11-28  9:39 ` [PATCH 1/3] clk: sunxi: Release the platform mod0 clock before unmapping it Guangshuo Li
  2026-11-28  9:39 ` [PATCH 2/3] clk: sunxi: Unregister the A31 and A23 APB0 platform clocks Guangshuo Li
@ 2026-11-28  9:39 ` Guangshuo Li
  2026-11-28  9:47   ` sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Guangshuo Li @ 2026-11-28  9:39 UTC (permalink / raw)
  To: emilio, sboyd, bmasney+clk, jbrunet+clk, wens, jernej.skrabec,
	samuel, mripard, hansg, bbrezillon
  Cc: linux-clk, linux-arm-kernel, linux-sunxi

Unbinding the APB0 gates driver releases its managed onecell data but
leaves the OF provider registered. Later clock lookups can dereference
the released data.

Remove the provider before the managed data is freed.

This issue was found by our static analysis tool under development.

Fixes: c8a76cac19ee ("clk: sunxi: add PRCM (Power/Reset/Clock Management) clks support")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/clk/sunxi/clk-sun6i-apb0-gates.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/clk/sunxi/clk-sun6i-apb0-gates.c b/drivers/clk/sunxi/clk-sun6i-apb0-gates.c
index a10ebd1998fb..9d1fef8c520e 100644
--- a/drivers/clk/sunxi/clk-sun6i-apb0-gates.c
+++ b/drivers/clk/sunxi/clk-sun6i-apb0-gates.c
@@ -87,11 +87,17 @@ static int sun6i_a31_apb0_gates_clk_probe(struct platform_device *pdev)
 	return of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
 }
 
+static void sun6i_a31_apb0_gates_clk_remove(struct platform_device *pdev)
+{
+	of_clk_del_provider(pdev->dev.of_node);
+}
+
 static struct platform_driver sun6i_a31_apb0_gates_clk_driver = {
 	.driver = {
 		.name = "sun6i-a31-apb0-gates-clk",
 		.of_match_table = sun6i_a31_apb0_gates_clk_dt_ids,
 	},
 	.probe = sun6i_a31_apb0_gates_clk_probe,
+	.remove = sun6i_a31_apb0_gates_clk_remove,
 };
 builtin_platform_driver(sun6i_a31_apb0_gates_clk_driver);

-- 
2.43.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 3/3] clk: sunxi: Fix potential use-after-free of APB0 provider data
  2026-11-28  9:39 ` [PATCH 3/3] clk: sunxi: Fix potential use-after-free of APB0 provider data Guangshuo Li
@ 2026-11-28  9:47   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-28  9:47 UTC (permalink / raw)
  To: Guangshuo Li; +Cc: linux-clk, linux-sunxi

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] clk: sunxi: unmapped MMIO use-after-free in
  sun6i_a31_apb0_gates_clk_remove()
--

commit dc78e25adc6746add0b39ad7479959e1a2f6206a
Author: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>

clk: sunxi: Fix potential use-after-free of APB0 provider data

This commit adds a remove callback to sun6i_a31_apb0_gates_clk_driver to
delete the OF clock provider before devres frees the managed onecell data.

> diff --git a/drivers/clk/sunxi/clk-sun6i-apb0-gates.c b/drivers/clk/sunxi/clk-sun6i-apb0-gates.c
> index a10ebd1998fbf..9d1fef8c520e9 100644
> --- a/drivers/clk/sunxi/clk-sun6i-apb0-gates.c
> +++ b/drivers/clk/sunxi/clk-sun6i-apb0-gates.c
> @@ -87,11 +87,17 @@ static int sun6i_a31_apb0_gates_clk_probe(struct platform_device *pdev)
>  	return of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
>  }
>  
> +static void sun6i_a31_apb0_gates_clk_remove(struct platform_device *pdev)
> +{
> +	of_clk_del_provider(pdev->dev.of_node);
> +}
> +
>  static struct platform_driver sun6i_a31_apb0_gates_clk_driver = {
>  	.driver = {
>  		.name = "sun6i-a31-apb0-gates-clk",
>  		.of_match_table = sun6i_a31_apb0_gates_clk_dt_ids,
>  	},
>  	.probe = sun6i_a31_apb0_gates_clk_probe,
> +	.remove = sun6i_a31_apb0_gates_clk_remove,
>  };
>  builtin_platform_driver(sun6i_a31_apb0_gates_clk_driver);

[Severity: High]
Does sun6i_a31_apb0_gates_clk_remove() leave the gate clocks registered
after reg is unmapped?

sun6i_a31_apb0_gates_clk_probe() maps reg with
devm_platform_ioremap_resource() and registers each gate clock with the
unmanaged clk_register_gate().  When the driver is unbound, devres unmaps
reg right after sun6i_a31_apb0_gates_clk_remove() returns, leaving the
registered struct clk_gate objects pointing at an unmapped MMIO address.
Any later access to those clocks (such as clk_enable() or reading
/sys/kernel/debug/clk/clk_summary) will fault on gate->reg, and rebinding
the driver will fail with -EEXIST.

Registering the gates with devm_clk_hw_register_gate() (or unregistering
them in remove) would release them before reg is unmapped.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-f81b700d6c-u17-0@mail.sdu.edu.cn?part=3

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-28  9:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28  9:39 [PATCH 0/3] clk: sunxi: Pair platform clock setup with teardown Guangshuo Li
2026-11-28  9:39 ` [PATCH 1/3] clk: sunxi: Release the platform mod0 clock before unmapping it Guangshuo Li
2026-11-28  9:39 ` [PATCH 2/3] clk: sunxi: Unregister the A31 and A23 APB0 platform clocks Guangshuo Li
2026-11-28  9:39 ` [PATCH 3/3] clk: sunxi: Fix potential use-after-free of APB0 provider data Guangshuo Li
2026-11-28  9:47   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox