[v3,03/26] clk: clk-uclass: fix parent reference counting in clk_set_parent()

Message ID 20261202102716.572884-4-dario.binacchi@amarulasolutions.com
State New
Headers
Series Support display on the BSH SMM S2/PRO boards |

Commit Message

Dario Binacchi Dec. 2, 2026, 10:26 a.m. UTC
clk_set_parent() enables the new parent only if the clock is enabled,
but on failure it disables it unconditionally, dropping a reference it
never took. On success, it never releases the reference the clock holds
on its old parent, which then keeps running forever.

Disable the new parent on failure only if it was enabled, and release
the old parent once the clock has moved away from it, as Linux does in
__clk_set_parent_after().

Fixes: ac30d90f3367 ("clk: Ensure the parent clocks are enabled while reparenting")
Fixes: b493db39ea7f ("clk: Only enable the parent clock if the clock was enabled before reparenting")
Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---

(no changes since v2)

Changes in v2:
- Add patch

 drivers/clk/clk-uclass.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
  

Patch

diff --git a/drivers/clk/clk-uclass.c b/drivers/clk/clk-uclass.c
index 0069ce9a8875..c503f6733907 100644
--- a/drivers/clk/clk-uclass.c
+++ b/drivers/clk/clk-uclass.c
@@ -631,6 +631,7 @@  ulong clk_set_rate(struct clk *clk, ulong rate)
 int clk_set_parent(struct clk *clk, struct clk *parent)
 {
 	const struct clk_ops *ops;
+	struct clk *old_parent = NULL;
 	int ret;
 
 	debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
@@ -641,6 +642,12 @@  int clk_set_parent(struct clk *clk, struct clk *parent)
 	if (!ops->set_parent)
 		return -ENOSYS;
 
+	if (CONFIG_IS_ENABLED(CLK_CCF)) {
+		old_parent = clk_get_parent(clk);
+		if (IS_ERR(old_parent))
+			old_parent = NULL;
+	}
+
 	if (clk->enable_count) {
 		ret = clk_enable(parent);
 		if (ret && ret != -ENOSYS) {
@@ -650,18 +657,18 @@  int clk_set_parent(struct clk *clk, struct clk *parent)
 	}
 
 	ret = ops->set_parent(clk, parent);
+	if (!ret && CONFIG_IS_ENABLED(CLK_CCF))
+		ret = device_reparent(clk->dev, parent->dev);
+
 	if (ret) {
-		clk_disable(parent);
+		if (clk->enable_count)
+			clk_disable(parent);
 		return ret;
 	}
 
-	if (CONFIG_IS_ENABLED(CLK_CCF)) {
-		ret = device_reparent(clk->dev, parent->dev);
-		if (ret) {
-			clk_disable(parent);
-			return ret;
-		}
-	}
+	/* The clock no longer keeps its old parent running */
+	if (clk->enable_count && old_parent)
+		clk_disable(old_parent);
 
 	return 0;
 }