Linux Power Management development
 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 v2 2/4] PM: runtime: Avoid suspends without respecting autosuspend delay
Date: Tue, 24 Nov 2026 16:35:08 +0100	[thread overview]
Message-ID: <3450859.44csPzL39Z@rafael.j.wysocki> (raw)
In-Reply-To: <4779892.LvFx2qVVIh@rafael.j.wysocki>

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

Commit 26236a3e8803 ("PM: runtime: Update last_busy before calling
rpm_suspend() in rpm_idle()") overlooked some cases in which the given
device may be suspended without respecting its autosuspend delay right
after the last runtime PM reference to it has been dropped.  They are all
scenarios in which a leftover autosuspend timer is running when the last
runtime PM reference to the given device is dropped, and dropping that
reference allows the timer to suspend the device right away.

For example, pm_runtime_put() may be called while an autosuspend timer
is running.  Then, dropping the device's runtime PM usage counter to 0
in __pm_runtime_idle() allows rpm_suspend() triggered by the timer to
suspend the device right away before __pm_runtime_idle() is able to
grab its PM spinlock.

Analogous situations may occur when child_count of a device's parent
is dropped in rpm_suspend() and __pm_runtime_set_status(), and in the
callers of pm_runtime_release_supplier().

Prevent them all from occurring by adding last_busy updates in a few
places where rpm_idle() calls follow dropping runtime PM references to
devices.

It is not necessary to add memory barriers to enforce the requisite
ordering of updates which is naturally enforced by the existing code in
all of those cases.  However, add comments explaining the rationale for
updating last_busy and the pairing of memory barriers, and accordingly
update the memory barrier comment in pm_runtime_autosuspend_expiration().

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>
---

v1 -> v2:
   * New patch addressing one of the cases addressed by the v1 and a few
     more analogous cases (Brian)

---
 drivers/base/power/runtime.c | 53 ++++++++++++++++++++++++++++++++----
 1 file changed, 47 insertions(+), 6 deletions(-)

diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
index c295acf6a718..a71afaea7707 100644
--- a/drivers/base/power/runtime.c
+++ b/drivers/base/power/runtime.c
@@ -187,10 +187,13 @@ u64 pm_runtime_autosuspend_expiration(struct device *dev)
 		return 0;
 
 	/*
-	 * Prevent last_busy updates occurring before the rpm_drop_usage_count()
-	 * invocations in __pm_runtime_idle() and __pm_runtime_suspend() from
-	 * being missed if the last_busy access below gets reordered before the
-	 * initial usage counter check in rpm_suspend().
+	 * If the last_busy access below gets reordered before the initial usage
+	 * counter check in rpm_suspend(), last_busy updates made before any of
+	 * the following may be missed:
+	 * - rpm_drop_usage_count() in __pm_runtime_idle() and
+	 *   __pm_runtime_suspend(),
+	 * - the usage count update in pm_runtime_release_supplier(),
+	 * - the child_count update in rpm_release_parent().
 	 */
 	smp_rmb();
 	expires  = READ_ONCE(dev->power.last_busy);
@@ -333,6 +336,16 @@ void pm_runtime_release_supplier(struct device_link *link)
 {
 	struct device *supplier = link->supplier;
 
+	/*
+	 * Since rpm_suspend() may run as a whole right after the loop below,
+	 * prevent it from suspending the supplier without respecting its
+	 * autosuspend delay.  The last_busy update cannot be reordered with
+	 * the usage counter update in pm_runtime_put_noidle(), which is fully
+	 * ordered and pairs with the read memory barrier in
+	 * pm_runtime_autosuspend_expiration().
+	 */
+	pm_runtime_mark_last_busy(supplier);
+
 	/*
 	 * The additional power.usage_count check is a safety net in case
 	 * the rpm_active refcount becomes saturated, in which case
@@ -574,6 +587,25 @@ static int rpm_idle(struct device *dev, int rpmflags)
 	return rpm_suspend(dev, rpmflags | RPM_AUTO);
 }
 
+static void rpm_release_parent(struct device *parent)
+{
+	/*
+	 * Since rpm_suspend() may run as a whole for parent right after its
+	 * child_count update below, prevent it from suspending parent without
+	 * respecting its autosuspend delay.
+	 */
+	if (!parent->power.ignore_children)
+		pm_runtime_mark_last_busy(parent);
+
+	/*
+	 * A successful atomic_add_unless() call is a memory barrier which is
+	 * sufficient to enforce the requisite ordering with respect to the
+	 * last_busy update above and it pairs with the read memory barrier in
+	 * pm_runtime_autosuspend_expiration().
+	 */
+	atomic_add_unless(&parent->power.child_count, -1, 0);
+}
+
 /**
  * rpm_suspend - Carry out runtime suspend of given device.
  * @dev: Device to suspend.
@@ -719,7 +751,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)
 
 	if (dev->parent) {
 		parent = dev->parent;
-		atomic_add_unless(&parent->power.child_count, -1, 0);
+		rpm_release_parent(parent);
 	}
 	wake_up_all(&dev->power.wait_queue);
 
@@ -1139,6 +1171,15 @@ int __pm_runtime_idle(struct device *dev, int rpmflags)
 	int retval;
 
 	if (rpmflags & RPM_GET_PUT) {
+		/*
+		 * Since rpm_suspend() may run as a whole right after the usage
+		 * counter decrementation below, prevent it from suspending the
+		 * device without respecting its autosuspend delay.  The
+		 * requisite ordering is enforced by rpm_drop_usage_count(),
+		 * which is an implicit memory barrier and pairs with the
+		 * explicit one in pm_runtime_autosuspend_expiration().
+		 */
+		pm_runtime_mark_last_busy(dev);
 		retval = rpm_drop_usage_count(dev);
 		if (retval < 0) {
 			return retval;
@@ -1383,7 +1424,7 @@ int __pm_runtime_set_status(struct device *dev, unsigned int status)
 		goto out_set;
 
 	if (status == RPM_SUSPENDED) {
-		atomic_add_unless(&parent->power.child_count, -1, 0);
+		rpm_release_parent(parent);
 		notify_parent = !parent->power.ignore_children;
 	} else {
 		spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
-- 
2.51.0





  parent reply	other threads:[~2026-11-24 15:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 15:29 [PATCH v2 0/4] PM: runtime: Fix issues related to autosuspend Rafael J. Wysocki
2026-11-24 15:31 ` [PATCH v2 1/4] PM: runtime: Add memory barrier before reading last_busy Rafael J. Wysocki
2026-11-24 15:35 ` Rafael J. Wysocki [this message]
2026-11-24 15:37 ` [PATCH v2 3/4] PM: runtime: Update last_busy in all rpm_idle() paths Rafael J. Wysocki
2026-11-24 15:39 ` [PATCH v2 4/4] PM: runtime: Update stale pm_runtime_put_sync() kerneldoc comment 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=3450859.44csPzL39Z@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