Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: Brian Norris <briannorris@chromium.org>
To: "Rafael J. Wysocki (Intel)" <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 1/4] PM: runtime: Add memory barrier before reading last_busy
Date: Mon, 30 Nov 2026 14:36:26 -0800	[thread overview]
Message-ID: <aw36amdmVguRnDxr@google.com> (raw)
In-Reply-To: <CAJZ5v0g05_8vJMRYSpN+y+rYB+Ou5dCq0EkPo3L+9um_6kkQgA@mail.gmail.com>

On Thu, Nov 26, 2026 at 03:04:04PM +0100, Rafael J. Wysocki (Intel) wrote:
> On Thu, Nov 26, 2026 at 2:44 AM Brian Norris <briannorris@chromium.org> wrote:
> >
> > On Tue, Nov 24, 2026 at 04:31:51PM +0100, Rafael J. Wysocki wrote:
> > > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> > >
> > > The users of pm_runtime_put_sync_autosuspend() and
> > > pm_runtime_put_autosuspend() can expect that the autosuspend delay of
> > > the device will be respected after calling one of them because they
> > > both invoke pm_runtime_mark_last_busy() upfront.  However, that may not
> > > happen due to a leftover autosuspend timer running when the last_busy
> > > update takes place, in which case rpm_suspend() triggered by the timer
> > > may run in parallel with the lockless last_busy update.
> > >
> > > Namely, the last_busy access in pm_runtime_autosuspend_expiration()
> > > called by rpm_suspend() may be reordered before the initial read of the
> > > device's runtime PM usage counter in rpm_suspend(), and if last_busy
> > > gets updated between those two reads, the update will be missed and the
> > > device may be suspended right away.
> > >
> > > To prevent that from occurring, add a read memory barrier before the
> > > last_busy read in pm_runtime_autosuspend_expiration() along with a
> > > comment explaining the reason for adding it.
> > >
> > > Fixes: b3db492e8335 ("PM: runtime: Mark last busy stamp in pm_runtime_put_autosuspend()")
> > > Fixes: e24e0630b5ba ("PM: runtime: Mark last busy stamp in pm_runtime_put_sync_autosuspend()")
> > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> >
> > If you're into this sort of thing:
> >
> > Reported-by: Brian Norris <briannorris@chromium.org>
> > Closes: https://lore.kernel.org/all/awC0tcqgq2KvCyxO@google.com/
> 
> OK
> 
> > and
> >
> > Reviewed-by: Brian Norris <briannorris@chromium.org>
> 
> Thanks!
> 
> > > ---
> > >
> > > New patch in v2, based on
> > >
> > > https://web.git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git/commit/?h=linux-next&id=26236a3e8803f7e1ace104020d7f006edb220116
> > >
> > > ---
> > >  drivers/base/power/runtime.c | 7 +++++++
> > >  1 file changed, 7 insertions(+)
> > >
> > > diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
> > > index 0b15c718c1bb..c295acf6a718 100644
> > > --- a/drivers/base/power/runtime.c
> > > +++ b/drivers/base/power/runtime.c
> > > @@ -186,6 +186,13 @@ u64 pm_runtime_autosuspend_expiration(struct device *dev)
> > >       if (autosuspend_delay < 0)
> > >               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().
> >
> > super-nit / feel free to ignore: it's a bit odd to write this comment
> > covering super-precise details of function calls, and then immediately
> > rewrite it in patch 2.
> 
> Well, say "stable" picks up this one without the next one (which is
> entirely possible).
> 
> And the time spent on writing it has already been sacrificed so to speak.

Part of my thought (that I failed to mention) was that depending on what
you really meant, the __pm_runtime_idle() part is a bit misleading, for
exactly the "stable" case you mention. __pm_runtime_idle() doesn't
actually update last_busy the way you mention until patch 2. If you
meant literal code like the following, then sure, you have a point:

	pm_runtime_mark_last_busy();
	pm_runtime_put(); // __pm_runtime_idle() is hidden under the hood here

But I don't expect anybody should have been expecting such code to do
sensible things, at least not until we recently decided to tweak
autosuspend and rpm_idle() behaviors.

In any case, I doubt anybody else is reading these code/comments that
closely for advice.

> > I think it might make for a better long-lasting comment if we just write
> > in terms of "last_busy update" and "lockless usage-count decrement".
> > Then we don't have to update it every time these things move around and
> > get patched.
> >
> > Sample:
> >
> >         /*
> >          * Ensure ordering between the initial usage counter check in
> >          * rpm_suspend() and this last_busy access, to avoid missing
> >          * any last_busy update performed before the last lockless usage
> >          * count decrement.
> >          */
> >
> >
> > But again, that's a bit nitpicky.
> >
> > I'll try to look at the rest next week.
> 
> Thank you!
> 
> I'll queue this series up in the meantime because it has been reviewed
> by Ulf and Sashiko has no concerns, but if you find anything needing a
> fix in it, please let me know.  Tags would be appreciated too. ;-)

Sounds good.

Brian

> > > +      */
> > > +     smp_rmb();
> > >       expires  = READ_ONCE(dev->power.last_busy);
> > >       expires += (u64)autosuspend_delay * NSEC_PER_MSEC;
> > >       if (expires > ktime_get_mono_fast_ns())
> > > --

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

Thread overview: 14+ 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 [this message]
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
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=aw36amdmVguRnDxr@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