diff mbox series

[RFC,06/10] clk: Move runtime PM calls out of the prepare_lock in clk_prepare()

Message ID 20250326-cross-lock-dep-v1-6-3199e49e8652@bootlin.com (mailing list archive)
State New
Headers show
Series Fix the ABBA locking situation between clk and runtime PM | expand

Commit Message

Miquel Raynal March 26, 2025, 6:26 p.m. UTC
In order to fix the ABBA locking situation between clock and power
domains, let's disimburse these two locks by preventing any runtime PM
call to happen with the clk prepare_lock mutex acquired.

The prepare routine shall preferably call the runtime PM functions
before acquiring the prepare lock. In practice the prepare helper
requires the current clock and all its parents to be runtime
resumed.

There is no need to perform individual calls as we get inner and inner
in recursive calls (ie. following the "clock parents") because, as of
today, runtime PM works by already resuming automatically all the
suppliers which in this case already include the clock parents.

Hence, doing a single runtime PM call at the beginning of the function
is enough. One side effect though is that we now need to check for the
validity of core clk pointers in the runtime PM clk helpers.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/clk/clk.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index b6decfaf8aa6bd50c2173d19f1ece8f08a95afd8..95f53bc427d8980287bfe668d1c993023e0e078b 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -113,7 +113,7 @@  struct clk {
 /***           runtime pm          ***/
 static int clk_pm_runtime_get(struct clk_core *core)
 {
-	if (!core->rpm_enabled)
+	if (!core || !core->rpm_enabled)
 		return 0;
 
 	return pm_runtime_resume_and_get(core->dev);
@@ -135,7 +135,7 @@  static int clk_pm_runtime_get_if_active(struct clk_core *core)
 
 static void clk_pm_runtime_put(struct clk_core *core)
 {
-	if (!core->rpm_enabled)
+	if (!core || !core->rpm_enabled)
 		return;
 
 	pm_runtime_put_sync(core->dev);
@@ -1081,7 +1081,6 @@  static void clk_core_unprepare(struct clk_core *core)
 
 	trace_clk_unprepare_complete(core);
 	clk_core_unprepare(core->parent);
-	clk_pm_runtime_put(core);
 }
 
 static void clk_core_unprepare_lock(struct clk_core *core)
@@ -1089,6 +1088,8 @@  static void clk_core_unprepare_lock(struct clk_core *core)
 	clk_prepare_lock();
 	clk_core_unprepare(core);
 	clk_prepare_unlock();
+
+	clk_pm_runtime_put(core);
 }
 
 /**
@@ -1121,13 +1122,9 @@  static int clk_core_prepare(struct clk_core *core)
 		return 0;
 
 	if (core->prepare_count == 0) {
-		ret = clk_pm_runtime_get(core);
-		if (ret)
-			return ret;
-
 		ret = clk_core_prepare(core->parent);
 		if (ret)
-			goto runtime_put;
+			return ret;
 
 		trace_clk_prepare(core);
 
@@ -1155,8 +1152,7 @@  static int clk_core_prepare(struct clk_core *core)
 	return 0;
 unprepare:
 	clk_core_unprepare(core->parent);
-runtime_put:
-	clk_pm_runtime_put(core);
+
 	return ret;
 }
 
@@ -1164,10 +1160,17 @@  static int clk_core_prepare_lock(struct clk_core *core)
 {
 	int ret;
 
+	ret = clk_pm_runtime_get(core);
+	if (ret)
+		return ret;
+
 	clk_prepare_lock();
 	ret = clk_core_prepare(core);
 	clk_prepare_unlock();
 
+	if (ret)
+		clk_pm_runtime_put(core);
+
 	return ret;
 }