Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rafael@kernel.org>
To: Linux PM <linux-pm@vger.kernel.org>
Cc: Brian Norris <briannorris@chromium.org>,
	Alan Stern <stern@rowland.harvard.edu>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Ulf Hansson <ulf.hansson@oss.qualcomm.com>,
	Linux Driver Core Development <driver-core@lists.linux.dev>
Subject: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
Date: Thu, 19 Nov 2026 21:16:30 +0100	[thread overview]
Message-ID: <3076901.e9J7NaK4W3@rafael.j.wysocki> (raw)
In-Reply-To: <1978814.tdWV9SEqCh@rafael.j.wysocki>

From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>

The changelog of commit 26236a3e8803 ("PM: runtime: Update last_busy
before calling rpm_suspend() in rpm_idle()") stated that rpm_idle() only
needed to update last_busy when it was about to call rpm_suspend(), but
that is incorrect.  There are still cases in which a device may be
autosuspended earlier than expected.

The first one is when pm_runtime_put() is called while a leftover
autosuspend timer is running.  Then, dropping the device's runtime PM
usage counter to 0 allows rpm_suspend() triggered by the timer to
suspend the device immediately before __pm_runtime_idle() is able to
grab the spinlock.  Prevent it from occurring by adding a last_busy
update before the rpm_drop_usage_count() call in __pm_runtime_idle().

Another one is when rpm_idle() returns without calling rpm_suspend(),
for example because the callback has returned a transient error, while
an autosuspend timer is running.  The timer may then suspend the device
without respecting the autosuspend delay.  Moreover, the timer may fire
while the callback is running and it will not see the new last_busy
value in that case.  To address these cases, move the existing
pm_runtime_mark_last_busy() call in rpm_idle() before the first return
value check.

In both places mentioned above add comments explaining the rationale for
updating last_busy.

Fixes: 26236a3e8803 ("PM: runtime: Update last_busy before calling rpm_suspend() in rpm_idle()")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---

Commit 26236a3e8803 is present in linux-next.

---
 drivers/base/power/runtime.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
index 0b15c718c1bb..02e7b4953c38 100644
--- a/drivers/base/power/runtime.c
+++ b/drivers/base/power/runtime.c
@@ -518,6 +518,14 @@ static int rpm_idle(struct device *dev, int rpmflags)
 	else if (dev->power.idle_notification)
 		retval = -EINPROGRESS;
 
+	/*
+	 * Update last_busy to defer autosuspend by the full autosuspend delay.
+	 * Since an autosuspend timer may be running and the new last_busy value
+	 * needs to be seen by it, update last_busy before returning an error or
+	 * dropping the spinlock and allowing the timer function to grab it.
+	 */
+	pm_runtime_mark_last_busy(dev);
+
 	if (retval)
 		goto out;
 
@@ -563,7 +571,6 @@ static int rpm_idle(struct device *dev, int rpmflags)
 	if (retval)
 		return retval;
 
-	pm_runtime_mark_last_busy(dev);
 	return rpm_suspend(dev, rpmflags | RPM_AUTO);
 }
 
@@ -1132,6 +1139,13 @@ int __pm_runtime_idle(struct device *dev, int rpmflags)
 	int retval;
 
 	if (rpmflags & RPM_GET_PUT) {
+		/*
+		 * Once the usage counter drops to 0, a leftover autosuspend
+		 * timer may fire before the spinlock below is acquired.  Update
+		 * last_busy first so it does not find the expiration time in
+		 * the past and suspend the device immediately.
+		 */
+		pm_runtime_mark_last_busy(dev);
 		retval = rpm_drop_usage_count(dev);
 		if (retval < 0) {
 			return retval;
-- 
2.51.0





  reply	other threads:[~2026-11-19 20:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:13 [PATCH v1 0/4] PM: runtime: Close some gaps related to autosuspend Rafael J. Wysocki
2026-11-19 20:16 ` Rafael J. Wysocki [this message]
2026-11-20 20:59   ` [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths Brian Norris
2026-11-20 21:29     ` Rafael J. Wysocki (Intel)
2026-11-19 20:17 ` [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels Rafael J. Wysocki
2026-11-20  2:47   ` Alan Stern
2026-11-20 12:11     ` Rafael J. Wysocki (Intel)
2026-11-20 12:30       ` Rafael J. Wysocki (Intel)
2026-11-20 16:11         ` Alan Stern
2026-11-19 20:17 ` [PATCH v1 3/4] PM: runtime: Let devices suspend after an attempted resume Rafael J. Wysocki
2026-11-20 21:04   ` Brian Norris
2026-11-19 20:18 ` [PATCH v1 4/4] PM: runtime: Drop direct last_busy updates from rpm_resume() Rafael J. Wysocki

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=3076901.e9J7NaK4W3@rafael.j.wysocki \
    --to=rafael@kernel.org \
    --cc=briannorris@chromium.org \
    --cc=driver-core@lists.linux.dev \
    --cc=linux-pm@vger.kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=stern@rowland.harvard.edu \
    --cc=ulf.hansson@oss.qualcomm.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