mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arun Iyengar via B4 Relay <devnull+aiyengar1990.gmail.com@kernel.org>
To: MyungJoo Ham <myungjoo.ham@samsung.com>,
	 Kyungmin Park <kyungmin.park@samsung.com>,
	 Chanwoo Choi <cw00.choi@samsung.com>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Boris Brezillon <boris.brezillon@collabora.com>,
	 Steven Price <steven.price@arm.com>,
	Liviu Dudau <liviu.dudau@arm.com>,
	 Jonas Karlman <jonas@kwiboo.se>,
	Dong Aisheng <aisheng.dong@nxp.com>,
	 Arun Iyengar <aiyengar1990@gmail.com>
Subject: [PATCH v4] PM / devfreq: Do not change the frequency of a suspended device
Date: Tue, 01 Dec 2026 04:17:30 +0000	[thread overview]
Message-ID: <20261201-devfreq-suspend-sync-v4-1-dd72d4b73f6c@gmail.com> (raw)

From: Arun Iyengar <aiyengar1990@gmail.com>

Once a driver has called devfreq_suspend_device(), its device may be
powered down, but PM QoS and sysfs limits, cooling, the userspace
governor's set_freq, OPP notifiers and the passive governor still reach
->target() through devfreq_update_target(). On RK3588 with the GPU clock
on SCMI_CLK_GPU (not in mainline yet), TF-A then accesses the GPU GRF
with its power domain off, which ends in an SError.

Drivers cannot filter these calls in ->target() without races, as the
suspend count changes outside devfreq->lock.

Change the suspend count under devfreq->lock. While it is positive,
devfreq_update_target() only records that an update is pending, except
for passive updates from the parent, which exynos-bus needs at suspend
and shutdown. devfreq_resume_device() runs the pending update. Polling
governors, whose load data the driver has just reset, and the passive
governor instead resume at the previous frequency within the current
limits. The userspace governor without set_freq keeps the frequency from
before suspend.

On error, devfreq_suspend_device() and devfreq_resume_device() leave
devfreq suspended, as drivers may power the device down regardless. The
driver's next devfreq_suspend_device() takes the count over and retries.
devfreq_suspend() and devfreq_resume() always pair: they nest, and a
failed system resume still resumes devfreq.

Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
Link: https://lore.kernel.org/r/20261110140232.061d6844@fedora1.home
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
Tested on an Orange Pi 5 (RK3588S) with the panthor side [1]: limits,
set_freq and cooling written while the GPU was runtime suspended take
effect on resume, with no SError, also after s2idle and deep suspend.
A KUnit test (not posted) covers the error paths changed in v4.

Developed with Claude (Anthropic) as a coding assistant; I reviewed and
tested it.

[1] https://lore.kernel.org/r/20261112-panthor-suspend-clk-v2-1-62e1b168052d@gmail.com
---
Changes in v4:
- Drop 1/2, applied to devfreq-next
- After a failed suspend, the driver's next suspend takes the count over
  and retries, instead of nesting: a driver that aborted its suspend
  kept devfreq suspended for good (Sashiko)
- devfreq_suspend() no longer takes over a driver's failed resume, which
  let devfreq_resume() call ->target() on a powered-down device; a
  failed system resume still resumes devfreq (Sashiko)
- Link to v3: https://patch.msgid.link/20261119-devfreq-suspend-sync-v3-0-ef19fbf94d83@gmail.com

Changes in v3:
- Run the pending update instead of restoring first, so resume makes
  one transition; the userspace governor without set_freq keeps the
  frequency from before suspend (Sashiko)
- Keep the passive governor out of the pending update, which evaluated
  it with freq 0 (Sashiko)
- Leave devfreq suspended when suspend or resume fails, instead of the
  v2 undo: callers may power the device down regardless. A caller that
  ignores a failed resume recovers on its next suspend and resume (Sashiko)
- Add [2] as 1/2: commit message corrected (lima on RK3528 is the
  mainline case, not panthor), comment shortened
- Rebase on devfreq-next
- Link to v2: https://patch.msgid.link/20261114-devfreq-suspend-sync-v2-1-10f0c3e14245@gmail.com

Changes in v2:
- Keep only the suspend count, now a plain int changed under
  devfreq->lock, instead of a separate suspended flag that could
  disagree with it when suspend and resume race (Sashiko)
- Resume at the resume frequency clamped to the current limits, then
  run the deferred update: the userspace governor without set_freq
  stayed at the suspend frequency (Sashiko)
- Don't defer the passive governor's parent updates: exynos-bus
  children must follow their parent to its suspend frequency, also at
  shutdown (Sashiko)
- Drop pending_freq: the deferred update runs like any update_devfreq()
  call. If it fails, resume logs the error and still sends
  DEVFREQ_GOV_RESUME (Sashiko)
- Undo a failed devfreq_suspend_device(), as rk3399_dmc and lima abort
  without resuming; an unbalanced devfreq_resume_device() is a no-op
  (Sashiko)
- Leave polling governors to their next poll after resume; the driver
  has just reset their load data (Sashiko)
- Link to v1: https://patch.msgid.link/20261112-devfreq-suspend-sync-v1-1-af64603ef43e@gmail.com
---
 drivers/devfreq/devfreq.c            | 150 ++++++++++++++++++++++++++---------
 drivers/devfreq/governor_userspace.c |   3 +-
 include/linux/devfreq.h              |  11 ++-
 3 files changed, 122 insertions(+), 42 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 7c01b588fefa..96adeb5cd4eb 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -409,6 +409,16 @@ int devfreq_update_target(struct devfreq *devfreq, unsigned long freq)
 	if (!devfreq->governor)
 		return -EINVAL;
 
+	/*
+	 * The device may be powered down while it is suspended. Leave it
+	 * alone, devfreq_resume_device() does the update. A passive device
+	 * (freq set) still follows its parent, as exynos-bus needs.
+	 */
+	if (devfreq->suspend_count > 0 && !freq) {
+		devfreq->update_pending = true;
+		return 0;
+	}
+
 	/* Reevaluate the proper frequency */
 	err = devfreq->governor->get_target_freq(devfreq, &freq);
 	if (err)
@@ -873,7 +883,7 @@ struct devfreq *devfreq_add_device(struct device *dev,
 	if (IS_ERR(devfreq->opp_table))
 		devfreq->opp_table = NULL;
 
-	atomic_set(&devfreq->suspend_count, 0);
+	devfreq->suspend_count = 0;
 
 	dev_set_name(&devfreq->dev, "%s", dev_name(dev));
 	err = device_register(&devfreq->dev);
@@ -1136,81 +1146,143 @@ void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
 }
 EXPORT_SYMBOL(devm_devfreq_remove_device);
 
-/**
- * devfreq_suspend_device() - Suspend devfreq of a device.
- * @devfreq: the devfreq instance to be suspended
- *
- * This function is intended to be called by the pm callbacks
- * (e.g., runtime_suspend, suspend) of the device driver that
- * holds the devfreq.
- */
-int devfreq_suspend_device(struct devfreq *devfreq)
+static int __devfreq_suspend_device(struct devfreq *devfreq, bool sys)
 {
 	unsigned long resume_freq;
-	int ret;
+	bool retry = false;
+	int ret, count;
 
 	if (!devfreq)
 		return -EINVAL;
 
-	if (atomic_inc_return(&devfreq->suspend_count) > 1)
+	mutex_lock(&devfreq->lock);
+	/* The driver's failed suspend or resume left the count raised */
+	if (!sys && devfreq->suspend_retry) {
+		devfreq->suspend_retry = false;
+		retry = true;
+	} else {
+		devfreq->suspend_count++;
+	}
+	count = devfreq->suspend_count;
+	mutex_unlock(&devfreq->lock);
+	if (count > 1)
 		return 0;
 
-	if (devfreq->governor) {
+	if (devfreq->governor && !retry) {
 		ret = devfreq->governor->event_handler(devfreq,
 					DEVFREQ_GOV_SUSPEND, NULL);
 		if (ret)
-			return ret;
+			goto err;
 	}
 
 	if (devfreq->suspend_freq) {
 		mutex_lock(&devfreq->lock);
 		/* devfreq_set_target() overwrites resume_freq; keep the pre-suspend one */
-		resume_freq = devfreq->previous_freq;
+		resume_freq = retry ? devfreq->resume_freq : devfreq->previous_freq;
 		ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
 		devfreq->resume_freq = resume_freq;
 		mutex_unlock(&devfreq->lock);
 		if (ret)
-			return ret;
+			goto err;
 	}
 
 	return 0;
+
+err:
+	if (!sys) {
+		mutex_lock(&devfreq->lock);
+		devfreq->suspend_retry = true;
+		mutex_unlock(&devfreq->lock);
+	}
+	return ret;
 }
-EXPORT_SYMBOL(devfreq_suspend_device);
 
-/**
- * devfreq_resume_device() - Resume devfreq of a device.
- * @devfreq: the devfreq instance to be resumed
- *
- * This function is intended to be called by the pm callbacks
- * (e.g., runtime_resume, resume) of the device driver that
- * holds the devfreq.
- */
-int devfreq_resume_device(struct devfreq *devfreq)
+static int __devfreq_resume_device(struct devfreq *devfreq, bool sys)
 {
-	int ret;
+	unsigned long min_freq, max_freq, freq;
+	int ret = 0, err;
+	u32 flags;
 
 	if (!devfreq)
 		return -EINVAL;
 
-	if (atomic_dec_return(&devfreq->suspend_count) >= 1)
+	mutex_lock(&devfreq->lock);
+	/* Count 0: no suspend to resume from */
+	if (devfreq->suspend_count <= 0 || --devfreq->suspend_count > 0) {
+		mutex_unlock(&devfreq->lock);
 		return 0;
+	}
 
-	if (devfreq->resume_freq) {
-		mutex_lock(&devfreq->lock);
-		ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
+	/*
+	 * Run the update deferred while suspended, except for polling
+	 * governors, which re-evaluate on fresh load data after
+	 * DEVFREQ_GOV_RESUME, and the passive governor, which follows its
+	 * parent. These resume within the current limits.
+	 */
+	if (devfreq->update_pending && devfreq->governor &&
+	    !IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL) &&
+	    strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
+		    DEVFREQ_NAME_LEN)) {
+		ret = devfreq_update_target(devfreq, 0);
+	} else {
+		freq = devfreq->resume_freq;
+		if (!freq && devfreq->update_pending)
+			freq = devfreq->previous_freq;
+		if (freq) {
+			devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
+			freq = clamp(freq, min_freq, max_freq);
+			flags = freq == max_freq ? DEVFREQ_FLAG_LEAST_UPPER_BOUND : 0;
+			ret = devfreq_set_target(devfreq, freq, flags);
+		}
+	}
+	if (ret && !sys) {
+		/* The driver may keep the device powered down */
+		devfreq->suspend_count++;
+		devfreq->suspend_retry = true;
 		mutex_unlock(&devfreq->lock);
-		if (ret)
-			return ret;
+		return ret;
 	}
+	devfreq->update_pending = false;
+	devfreq->suspend_retry = false;
+	mutex_unlock(&devfreq->lock);
 
 	if (devfreq->governor) {
-		ret = devfreq->governor->event_handler(devfreq,
+		err = devfreq->governor->event_handler(devfreq,
 					DEVFREQ_GOV_RESUME, NULL);
-		if (ret)
-			return ret;
+		ret = ret ?: err;
 	}
 
-	return 0;
+	return ret;
+}
+
+/**
+ * devfreq_suspend_device() - Suspend devfreq of a device.
+ * @devfreq: the devfreq instance to be suspended
+ *
+ * This function is intended to be called by the pm callbacks
+ * (e.g., runtime_suspend, suspend) of the device driver that
+ * holds the devfreq. On error, devfreq stays suspended until the next
+ * devfreq_suspend_device() and devfreq_resume_device() pair.
+ */
+int devfreq_suspend_device(struct devfreq *devfreq)
+{
+	return __devfreq_suspend_device(devfreq, false);
+}
+EXPORT_SYMBOL(devfreq_suspend_device);
+
+/**
+ * devfreq_resume_device() - Resume devfreq of a device.
+ * @devfreq: the devfreq instance to be resumed
+ *
+ * This function is intended to be called by the pm callbacks
+ * (e.g., runtime_resume, resume) of the device driver that
+ * holds the devfreq. If the frequency cannot be set, devfreq stays
+ * suspended until the next devfreq_suspend_device() and
+ * devfreq_resume_device() pair.
+ */
+int devfreq_resume_device(struct devfreq *devfreq)
+{
+	return __devfreq_resume_device(devfreq, false);
 }
 EXPORT_SYMBOL(devfreq_resume_device);
 
@@ -1229,7 +1301,7 @@ void devfreq_suspend(void)
 
 	mutex_lock(&devfreq_list_lock);
 	list_for_each_entry(devfreq, &devfreq_list, node) {
-		ret = devfreq_suspend_device(devfreq);
+		ret = __devfreq_suspend_device(devfreq, true);
 		if (ret)
 			dev_err(&devfreq->dev,
 				"failed to suspend devfreq device\n");
@@ -1250,7 +1322,7 @@ void devfreq_resume(void)
 
 	mutex_lock(&devfreq_list_lock);
 	list_for_each_entry(devfreq, &devfreq_list, node) {
-		ret = devfreq_resume_device(devfreq);
+		ret = __devfreq_resume_device(devfreq, true);
 		if (ret)
 			dev_warn(&devfreq->dev,
 				 "failed to resume devfreq device\n");
diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c
index 8211aadb81f0..eb2b2c7af01f 100644
--- a/drivers/devfreq/governor_userspace.c
+++ b/drivers/devfreq/governor_userspace.c
@@ -27,7 +27,8 @@ static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
 	if (data->valid)
 		*freq = data->user_frequency;
 	else
-		*freq = df->previous_freq; /* No user freq specified yet */
+		/* No user freq specified yet: keep the one from before suspend */
+		*freq = df->resume_freq ?: df->previous_freq;
 
 	return 0;
 }
diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
index 809ef29b9af5..a7b245ec8546 100644
--- a/include/linux/devfreq.h
+++ b/include/linux/devfreq.h
@@ -169,7 +169,12 @@ struct devfreq_stats {
  * @stop_polling:	 devfreq polling status of a device.
  * @suspend_freq:	 frequency of a device set during suspend phase.
  * @resume_freq:	 frequency of a device set in resume phase.
- * @suspend_count:	 suspend requests counter for a device.
+ * @suspend_count:	 suspend requests counter for a device, protected by
+ *		 @lock. While it is positive, frequency updates are deferred
+ *		 until resume, except the passive governor's parent updates.
+ * @update_pending:	 a frequency update was deferred while suspended.
+ * @suspend_retry:	 a failed devfreq_suspend_device() or devfreq_resume_device()
+ *		 left @suspend_count raised for the next devfreq_suspend_device().
  * @stats:	Statistics of devfreq device behavior
  * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
  * @cdev:	Cooling device pointer if the devfreq has cooling property
@@ -212,7 +217,9 @@ struct devfreq {
 
 	unsigned long suspend_freq;
 	unsigned long resume_freq;
-	atomic_t suspend_count;
+	int suspend_count;
+	bool update_pending;
+	bool suspend_retry;
 
 	/* information for device frequency transitions */
 	struct devfreq_stats stats;

---
base-commit: e7c25197980f6edcf38a8167996eb4626a0a5c0d
change-id: 20261112-devfreq-suspend-sync-7ca8fc51550a

Best regards,
--  
Arun Iyengar <aiyengar1990@gmail.com>



                 reply	other threads:[~2026-12-01  4:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261201-devfreq-suspend-sync-v4-1-dd72d4b73f6c@gmail.com \
    --to=devnull+aiyengar1990.gmail.com@kernel.org \
    --cc=aisheng.dong@nxp.com \
    --cc=aiyengar1990@gmail.com \
    --cc=boris.brezillon@collabora.com \
    --cc=cw00.choi@samsung.com \
    --cc=jonas@kwiboo.se \
    --cc=kyungmin.park@samsung.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=liviu.dudau@arm.com \
    --cc=myungjoo.ham@samsung.com \
    --cc=steven.price@arm.com \
    /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

all inboxes | Powered by JetHome®