Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: Brian Norris <briannorris@chromium.org>
To: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Linux PM <linux-pm@vger.kernel.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: Re: [PATCH v2 2/4] PM: runtime: Avoid suspends without respecting autosuspend delay
Date: Mon, 30 Nov 2026 14:38:23 -0800	[thread overview]
Message-ID: <aw3635h4xXhLGlWF@google.com> (raw)
In-Reply-To: <3450859.44csPzL39Z@rafael.j.wysocki>

On Tue, Nov 24, 2026 at 04:35:08PM +0100, Rafael J. Wysocki wrote:
> 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);

pm_runtime_release_supplier() is already a bit of an odd function,
because it is designed to be called even in cases where the link in
question does *not* hold an active refcount on the supplier. (i.e.,
there are cases where
	refcount_dec_not_one(&link->rpm_active)
sees 1 (no refcount) and quits without doing anything.)

This means you are potentially adding spurious last_busy updates to
cases like:

* pm_runtime_set_suspended(dev), when dev is already suspended. This
  will still update last_busy for its suppliers

* some other odds and ends, like pm_runtime_drop_link()

* device_link_release_fn() -- even for non-DL_FLAG_PM_RUNTIME links.
  (Kind of unimportant.)

Perhaps this this last_busy update should go inside the 'if'? Or
otherwise refactor this function to be less subtle.

The other changes in this patch look good, so maybe this still qualifies
as:

Reviewed-by: Brian Norris <briannorris@chromium.org>

> +
>  	/*
>  	 * 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().
> +		 */

It feels like a bad sign that we have to scatter these comments
everywhere. It's not a criticism of the comment -- just the design^W
evolution of the code we have.

It feels like we'd be in a better place when these comments only go in
functions like rpm_drop_usage_count(), rpm_release_parent(),
pm_runtime_release_supplier(), as central places to manage refcounts.
And we are *almost* to the point that rpm_drop_usage_count() should
always call pm_runtime_mark_last_busy().

Anyway, I'm just musing. I think we're getting to a better place here.

Regards,
Brian

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

  reply	other threads:[~2026-11-30 22:38 UTC|newest]

Thread overview: 15+ 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-26  1:43   ` Brian Norris
2026-11-26 14:04     ` Rafael J. Wysocki (Intel)
2026-11-30 22:36       ` Brian Norris
2026-11-24 15:35 ` [PATCH v2 2/4] PM: runtime: Avoid suspends without respecting autosuspend delay Rafael J. Wysocki
2026-11-30 22:38   ` Brian Norris [this message]
2026-11-30 22:59     ` Brian Norris
2026-11-24 15:37 ` [PATCH v2 3/4] PM: runtime: Update last_busy in all rpm_idle() paths Rafael J. Wysocki
2026-11-30 22:31   ` Brian Norris
2026-11-24 15:39 ` [PATCH v2 4/4] PM: runtime: Update stale pm_runtime_put_sync() kerneldoc comment Rafael J. Wysocki
2026-11-30 22:39   ` Brian Norris
2026-11-26 11:51 ` [PATCH v2 0/4] PM: runtime: Fix issues related to autosuspend Ulf Hansson
2026-11-26 12:50   ` Rafael J. Wysocki (Intel)
2026-11-26 13:28     ` Ulf Hansson

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=aw3635h4xXhLGlWF@google.com \
    --to=briannorris@chromium.org \
    --cc=driver-core@lists.linux.dev \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@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