public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Caleb Connolly <caleb.connolly@linaro.org>
To: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
	Sebastian Reichel <sre@kernel.org>,
	Viresh Kumar <viresh.kumar@linaro.org>
Cc: Caleb Connolly <caleb.connolly@linaro.org>, linux-pm@vger.kernel.org
Subject: [RFC PATCH] power: supply: fix circular dependency with cooling device
Date: Sun,  1 Jan 2023 17:43:40 +0000	[thread overview]
Message-ID: <20230101174342.58351-1-caleb.connolly@linaro.org> (raw)

A recent change in thermal/core means it now calls the cooling device
->get_max_state() callback during __thermal_cooling_device_register().
This creates a circular dependency as it attempts to fetch a power
supply property before the psy is initialised. Move this call later to
break the dependency.

Fixes: c408b3d1d9bb ("thermal: Validate new state in cur_state_store()")
Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
---
 drivers/power/supply/power_supply_core.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index 7c790c41e2fe..c921111ff26a 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -1354,10 +1354,6 @@ __power_supply_register(struct device *parent,
 	if (rc)
 		goto register_thermal_failed;
 
-	rc = psy_register_cooler(psy);
-	if (rc)
-		goto register_cooler_failed;
-
 	rc = power_supply_create_triggers(psy);
 	if (rc)
 		goto create_triggers_failed;
@@ -1378,17 +1374,27 @@ __power_supply_register(struct device *parent,
 	atomic_inc(&psy->use_cnt);
 	psy->initialized = true;
 
+	/* This has to be done after updating use_cnt and initialized
+	 * otherwise when __thermal_cooling_device_register calls back
+	 * to ->get_max_state() the psy core will return -EAGAIN..
+	 */
+	rc = psy_register_cooler(psy);
+	if (rc)
+		goto register_cooler_failed;
+
 	queue_delayed_work(system_power_efficient_wq,
 			   &psy->deferred_register_work,
 			   POWER_SUPPLY_DEFERRED_REGISTER_TIME);
 
 	return psy;
 
+register_cooler_failed:
+	power_supply_remove_hwmon_sysfs(psy);
+	psy->initialized = false;
+	atomic_dec(&psy->use_cnt);
 add_hwmon_sysfs_failed:
 	power_supply_remove_triggers(psy);
 create_triggers_failed:
-	psy_unregister_cooler(psy);
-register_cooler_failed:
 	psy_unregister_thermal(psy);
 register_thermal_failed:
 wakeup_init_failed:
-- 
2.39.0


             reply	other threads:[~2023-01-01 17:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-01 17:43 Caleb Connolly [this message]
2023-01-12 21:47 ` [RFC PATCH] power: supply: fix circular dependency with cooling device Luca Weiss
2023-02-03 12:40   ` Sebastian Reichel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230101174342.58351-1-caleb.connolly@linaro.org \
    --to=caleb.connolly@linaro.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael.j.wysocki@intel.com \
    --cc=sre@kernel.org \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox