* [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
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
2026-11-20 20:59 ` Brian Norris
2026-11-19 20:17 ` [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels Rafael J. Wysocki
` (2 subsequent siblings)
3 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki @ 2026-11-19 20:16 UTC (permalink / raw)
To: Linux PM
Cc: Brian Norris, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
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
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
2026-11-19 20:16 ` [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths Rafael J. Wysocki
@ 2026-11-20 20:59 ` Brian Norris
2026-11-20 21:29 ` Rafael J. Wysocki (Intel)
0 siblings, 1 reply; 16+ messages in thread
From: Brian Norris @ 2026-11-20 20:59 UTC (permalink / raw)
To: Rafael J. Wysocki
Cc: Linux PM, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Thu, Nov 19, 2026 at 09:16:30PM +0100, Rafael J. Wysocki wrote:
> 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().
Do consumer->supplier and child->parent refcount drops have the same
problem? e.g., in pm_runtime_release_supplier() and rpm_suspend() /
parent->power.child_count decrement?
> 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.
I suppose we don't care about: "what if .runtime_idle() is slow?" --
that makes a small delta in the expiration time.
> 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.
Semi-unrelated cleanup: pm_runtime_put_sync() some stale kerneldoc now,
saying "this does not update the last access time".
> ---
> 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);
This is somewhat of a pre-existing issue (e.g., in
pm_runtime_put_autosuspend()), but weakly ordered architectures don't
really enforce the dependencies you want.
rpm_drop_usage_count() uses an atomic decrement, which has a full
barrier. But this is supposed to influence the rpm_suspend() behavior of
rpm_check_suspend_allowed() (atomic_read) + READ_ONCE(last_busy), which
includes only a conditional dependency and no barrier.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0124r6.html
"Control dependencies can order prior loads against later dependent
stores, however, they do not order prior loads against later dependent
loads. (Use memory_order_consume or memory_order_acquire if you
require this behavior.)"
So, rpm_check_suspend_allowed() should probably use
atomic_read_acquire(&dev->power.usage_count).
(This is probably an extremely narrow, practically impossible issue.)
Brian
> if (retval < 0) {
> return retval;
> --
> 2.51.0
>
>
>
>
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
2026-11-20 20:59 ` Brian Norris
@ 2026-11-20 21:29 ` Rafael J. Wysocki (Intel)
2026-11-21 11:22 ` Rafael J. Wysocki (Intel)
0 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-11-20 21:29 UTC (permalink / raw)
To: Brian Norris
Cc: Rafael J. Wysocki, Linux PM, Alan Stern, Sakari Ailus,
Ulf Hansson, Linux Driver Core Development
On Fri, Nov 20, 2026 at 9:59 PM Brian Norris <briannorris@chromium.org> wrote:
>
> On Thu, Nov 19, 2026 at 09:16:30PM +0100, Rafael J. Wysocki wrote:
> > 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().
>
> Do consumer->supplier and child->parent refcount drops have the same
> problem? e.g., in pm_runtime_release_supplier() and rpm_suspend() /
> parent->power.child_count decrement?
I need to think about it.
I don't think it affects the $subject patch anyway though.
> > 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.
>
> I suppose we don't care about: "what if .runtime_idle() is slow?" --
> that makes a small delta in the expiration time.
No, I don't.
It is actually absent in the vast majority of cases. And if it is
present and it is slow, it may as well call
pm_runtime_mark_last_busy() itself.
> > 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.
>
> Semi-unrelated cleanup: pm_runtime_put_sync() some stale kerneldoc now,
> saying "this does not update the last access time".
Ah, good point.
> > ---
> > 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);
>
> This is somewhat of a pre-existing issue (e.g., in
> pm_runtime_put_autosuspend()), but weakly ordered architectures don't
> really enforce the dependencies you want.
>
> rpm_drop_usage_count() uses an atomic decrement, which has a full
> barrier.
Right, that's why the last_busy update is carried out before
rpm_drop_usage_count().
> But this is supposed to influence the rpm_suspend() behavior of
> rpm_check_suspend_allowed() (atomic_read) + READ_ONCE(last_busy), which
> includes only a conditional dependency and no barrier.
>
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0124r6.html
>
> "Control dependencies can order prior loads against later dependent
> stores, however, they do not order prior loads against later dependent
> loads. (Use memory_order_consume or memory_order_acquire if you
> require this behavior.)"
>
> So, rpm_check_suspend_allowed() should probably use
> atomic_read_acquire(&dev->power.usage_count).
>
> (This is probably an extremely narrow, practically impossible issue.)
Yes, this sounds nearly impossible in practice, given the number of
branches between the usage counter read in rpm_check_suspend_allowed()
and the last_busy check in pm_runtime_autosuspend_expiration().
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
2026-11-20 21:29 ` Rafael J. Wysocki (Intel)
@ 2026-11-21 11:22 ` Rafael J. Wysocki (Intel)
2026-11-22 11:53 ` Rafael J. Wysocki (Intel)
0 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-11-21 11:22 UTC (permalink / raw)
To: Brian Norris
Cc: Linux PM, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Fri, Nov 20, 2026 at 10:29 PM Rafael J. Wysocki (Intel)
<rafael@kernel.org> wrote:
>
> On Fri, Nov 20, 2026 at 9:59 PM Brian Norris <briannorris@chromium.org> wrote:
> >
> > On Thu, Nov 19, 2026 at 09:16:30PM +0100, Rafael J. Wysocki wrote:
> > > 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().
> >
> > Do consumer->supplier and child->parent refcount drops have the same
> > problem? e.g., in pm_runtime_release_supplier() and rpm_suspend() /
> > parent->power.child_count decrement?
>
> I need to think about it.
>
> I don't think it affects the $subject patch anyway though.
So generally speaking, yes, they do.
In the suppliers case, that can be addressed by doing something like
pm_runtime_mark_last_busy(supplier);
smp_mb__before_atomic();
pm_runtime_put_noidle(supplier);
in every step of the loop in pm_runtime_release_supplier().
In the parent case, a similar thing would need to be done in rpm_suspend():
parent = dev->parent;
pm_runtime_mark_last_busy(parent);
smp_mb__before_atomic();
atomic_add_unless(&parent->power.child_count, -1, 0);
with the addition of a read-side memory barrier (see below).
> > > 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.
> >
> > I suppose we don't care about: "what if .runtime_idle() is slow?" --
> > that makes a small delta in the expiration time.
>
> No, I don't.
>
> It is actually absent in the vast majority of cases. And if it is
> present and it is slow, it may as well call
> pm_runtime_mark_last_busy() itself.
>
> > > 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.
> >
> > Semi-unrelated cleanup: pm_runtime_put_sync() some stale kerneldoc now,
> > saying "this does not update the last access time".
>
> Ah, good point.
>
> > > ---
> > > 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);
> >
> > This is somewhat of a pre-existing issue (e.g., in
> > pm_runtime_put_autosuspend()), but weakly ordered architectures don't
> > really enforce the dependencies you want.
> >
> > rpm_drop_usage_count() uses an atomic decrement, which has a full
> > barrier.
>
> Right, that's why the last_busy update is carried out before
> rpm_drop_usage_count().
>
> > But this is supposed to influence the rpm_suspend() behavior of
> > rpm_check_suspend_allowed() (atomic_read) + READ_ONCE(last_busy), which
> > includes only a conditional dependency and no barrier.
> >
> > https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0124r6.html
> >
> > "Control dependencies can order prior loads against later dependent
> > stores, however, they do not order prior loads against later dependent
> > loads. (Use memory_order_consume or memory_order_acquire if you
> > require this behavior.)"
> >
> > So, rpm_check_suspend_allowed() should probably use
> > atomic_read_acquire(&dev->power.usage_count).
> >
> > (This is probably an extremely narrow, practically impossible issue.)
>
> Yes, this sounds nearly impossible in practice, given the number of
> branches between the usage counter read in rpm_check_suspend_allowed()
> and the last_busy check in pm_runtime_autosuspend_expiration().
It is not entirely impossible though AFAICS.
However, it is only really a concern if
pm_runtime_autosuspend_expiration() actually runs and if it actually
computes expires, so I'd rather add a read memory barrier to that
function:
smp_rmb();
expires = READ_ONCE(dev->power.last_busy);
That would get the ordering between the last_busy access above and the
initial usage counter and child counter checks always right.
Well, I guess I need to redo the patch series to take all of the above
into account.
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
2026-11-21 11:22 ` Rafael J. Wysocki (Intel)
@ 2026-11-22 11:53 ` Rafael J. Wysocki (Intel)
0 siblings, 0 replies; 16+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-11-22 11:53 UTC (permalink / raw)
To: Brian Norris
Cc: Linux PM, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Sat, Nov 21, 2026 at 12:22 PM Rafael J. Wysocki (Intel)
<rafael@kernel.org> wrote:
>
> On Fri, Nov 20, 2026 at 10:29 PM Rafael J. Wysocki (Intel)
> <rafael@kernel.org> wrote:
> >
> > On Fri, Nov 20, 2026 at 9:59 PM Brian Norris <briannorris@chromium.org> wrote:
> > >
> > > On Thu, Nov 19, 2026 at 09:16:30PM +0100, Rafael J. Wysocki wrote:
> > > > 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().
> > >
> > > Do consumer->supplier and child->parent refcount drops have the same
> > > problem? e.g., in pm_runtime_release_supplier() and rpm_suspend() /
> > > parent->power.child_count decrement?
> >
> > I need to think about it.
> >
> > I don't think it affects the $subject patch anyway though.
>
> So generally speaking, yes, they do.
>
> In the suppliers case, that can be addressed by doing something like
>
> pm_runtime_mark_last_busy(supplier);
> smp_mb__before_atomic();
> pm_runtime_put_noidle(supplier);
>
> in every step of the loop in pm_runtime_release_supplier().
That would be overkill because refcount_dec_not_one() provides release
semantics, as per the documented behavior of
atomic_try_cmpxchg_release(), so long as the refcount is actually
decremented by it which is expected to happen in
pm_runtime_release_supplier() at least once. So adding a
pm_runtime_mark_last_busy() call before the loop in
pm_runtime_release_supplier() should be sufficient.
> In the parent case, a similar thing would need to be done in rpm_suspend():
>
> parent = dev->parent;
> pm_runtime_mark_last_busy(parent);
> smp_mb__before_atomic();
> atomic_add_unless(&parent->power.child_count, -1, 0);
>
> with the addition of a read-side memory barrier (see below).
>
> > > > 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.
> > >
> > > I suppose we don't care about: "what if .runtime_idle() is slow?" --
> > > that makes a small delta in the expiration time.
> >
> > No, I don't.
> >
> > It is actually absent in the vast majority of cases. And if it is
> > present and it is slow, it may as well call
> > pm_runtime_mark_last_busy() itself.
> >
> > > > 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.
> > >
> > > Semi-unrelated cleanup: pm_runtime_put_sync() some stale kerneldoc now,
> > > saying "this does not update the last access time".
> >
> > Ah, good point.
> >
> > > > ---
> > > > 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);
> > >
> > > This is somewhat of a pre-existing issue (e.g., in
> > > pm_runtime_put_autosuspend()), but weakly ordered architectures don't
> > > really enforce the dependencies you want.
> > >
> > > rpm_drop_usage_count() uses an atomic decrement, which has a full
> > > barrier.
> >
> > Right, that's why the last_busy update is carried out before
> > rpm_drop_usage_count().
> >
> > > But this is supposed to influence the rpm_suspend() behavior of
> > > rpm_check_suspend_allowed() (atomic_read) + READ_ONCE(last_busy), which
> > > includes only a conditional dependency and no barrier.
> > >
> > > https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0124r6.html
> > >
> > > "Control dependencies can order prior loads against later dependent
> > > stores, however, they do not order prior loads against later dependent
> > > loads. (Use memory_order_consume or memory_order_acquire if you
> > > require this behavior.)"
> > >
> > > So, rpm_check_suspend_allowed() should probably use
> > > atomic_read_acquire(&dev->power.usage_count).
> > >
> > > (This is probably an extremely narrow, practically impossible issue.)
> >
> > Yes, this sounds nearly impossible in practice, given the number of
> > branches between the usage counter read in rpm_check_suspend_allowed()
> > and the last_busy check in pm_runtime_autosuspend_expiration().
>
> It is not entirely impossible though AFAICS.
>
> However, it is only really a concern if
> pm_runtime_autosuspend_expiration() actually runs and if it actually
> computes expires, so I'd rather add a read memory barrier to that
> function:
>
> smp_rmb();
> expires = READ_ONCE(dev->power.last_busy);
>
> That would get the ordering between the last_busy access above and the
> initial usage counter and child counter checks always right.
>
> Well, I guess I need to redo the patch series to take all of the above
> into account.
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
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 ` [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths Rafael J. Wysocki
@ 2026-11-19 20:17 ` Rafael J. Wysocki
2026-11-20 2:47 ` 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-19 20:18 ` [PATCH v1 4/4] PM: runtime: Drop direct last_busy updates from rpm_resume() Rafael J. Wysocki
3 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki @ 2026-11-19 20:17 UTC (permalink / raw)
To: Linux PM
Cc: Brian Norris, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
Drop space characters preceding labels from the core runtime PM code.
No functional impact.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
drivers/base/power/runtime.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
index 02e7b4953c38..5a0b4600c244 100644
--- a/drivers/base/power/runtime.c
+++ b/drivers/base/power/runtime.c
@@ -566,7 +566,7 @@ static int rpm_idle(struct device *dev, int rpmflags)
dev->power.idle_notification = false;
wake_up_all(&dev->power.wait_queue);
- out:
+out:
trace_rpm_return_int(dev, _THIS_IP_, retval);
if (retval)
return retval;
@@ -604,7 +604,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)
trace_rpm_suspend(dev, rpmflags);
- repeat:
+repeat:
retval = rpm_check_suspend_allowed(dev);
if (retval < 0)
goto out; /* Conditions are wrong. */
@@ -713,7 +713,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)
dev_pm_enable_wake_irq_complete(dev);
- no_callback:
+no_callback:
__update_runtime_status(dev, RPM_SUSPENDED);
pm_runtime_deactivate_timer(dev);
@@ -752,12 +752,12 @@ static int rpm_suspend(struct device *dev, int rpmflags)
spin_lock_irq(&dev->power.lock);
}
- out:
+out:
trace_rpm_return_int(dev, _THIS_IP_, retval);
return retval;
- fail:
+fail:
dev_pm_disable_wake_irq_check(dev, true);
__update_runtime_status(dev, RPM_ACTIVE);
dev->power.deferred_resume = false;
@@ -807,7 +807,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
trace_rpm_resume(dev, rpmflags);
- repeat:
+repeat:
if (dev->power.runtime_error) {
retval = -EINVAL;
} else if (dev->power.disable_depth > 0) {
@@ -947,7 +947,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
goto repeat;
}
- skip_parent:
+skip_parent:
if (dev->power.no_callbacks)
goto no_callback; /* Assume success. */
@@ -963,7 +963,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
pm_runtime_cancel_pending(dev);
dev_pm_enable_wake_irq_check(dev, false);
} else {
- no_callback:
+no_callback:
__update_runtime_status(dev, RPM_ACTIVE);
pm_runtime_mark_last_busy(dev);
if (parent)
@@ -974,7 +974,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
if (retval >= 0)
rpm_idle(dev, RPM_ASYNC);
- out:
+out:
if (parent && !dev->power.irq_safe) {
spin_unlock_irq(&dev->power.lock);
@@ -1026,7 +1026,7 @@ static void pm_runtime_work(struct work_struct *work)
break;
}
- out:
+out:
spin_unlock_irq(&dev->power.lock);
}
@@ -1095,7 +1095,7 @@ int pm_schedule_suspend(struct device *dev, unsigned int delay)
dev->power.timer_autosuspends = 0;
hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
- out:
+out:
spin_unlock_irqrestore(&dev->power.lock, flags);
return retval;
@@ -1419,12 +1419,12 @@ int __pm_runtime_set_status(struct device *dev, unsigned int status)
}
}
- out_set:
+out_set:
__update_runtime_status(dev, status);
if (!error)
dev->power.runtime_error = 0;
- out:
+out:
spin_unlock_irqrestore(&dev->power.lock, flags);
if (notify_parent)
@@ -1578,7 +1578,7 @@ void __pm_runtime_disable(struct device *dev, bool check_resume)
dev->power.last_status = dev->power.runtime_status;
}
- out:
+out:
spin_unlock_irq(&dev->power.lock);
}
EXPORT_SYMBOL_GPL(__pm_runtime_disable);
@@ -1710,7 +1710,7 @@ void pm_runtime_forbid(struct device *dev)
atomic_inc(&dev->power.usage_count);
rpm_resume(dev, 0);
- out:
+out:
spin_unlock_irq(&dev->power.lock);
}
EXPORT_SYMBOL_GPL(pm_runtime_forbid);
@@ -1742,7 +1742,7 @@ void pm_runtime_allow(struct device *dev)
else if (ret > 0)
trace_rpm_usage(dev, RPM_AUTO | RPM_ASYNC);
- out:
+out:
spin_unlock_irq(&dev->power.lock);
}
EXPORT_SYMBOL_GPL(pm_runtime_allow);
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
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)
0 siblings, 1 reply; 16+ messages in thread
From: Alan Stern @ 2026-11-20 2:47 UTC (permalink / raw)
To: Rafael J. Wysocki
Cc: Linux PM, Brian Norris, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Thu, Nov 19, 2026 at 09:17:08PM +0100, Rafael J. Wysocki wrote:
> From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
>
> Drop space characters preceding labels from the core runtime PM code.
>
> No functional impact.
>
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
There actually is a valid reason for keeping the spaces before
statement labels, although you may not agree with it. Namely, the space
prevents "diff -p" from thinking that the label is the start of a new
function definition. That's why I put a single space before statement
labels in the code I write.
Alan Stern
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
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)
0 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-11-20 12:11 UTC (permalink / raw)
To: Alan Stern
Cc: Rafael J. Wysocki, Linux PM, Brian Norris, Sakari Ailus,
Ulf Hansson, Linux Driver Core Development
On Fri, Nov 20, 2026 at 3:47 AM Alan Stern <stern@rowland.harvard.edu> wrote:
>
> On Thu, Nov 19, 2026 at 09:17:08PM +0100, Rafael J. Wysocki wrote:
> > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> >
> > Drop space characters preceding labels from the core runtime PM code.
> >
> > No functional impact.
> >
> > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > ---
>
> There actually is a valid reason for keeping the spaces before
> statement labels, although you may not agree with it. Namely, the space
> prevents "diff -p" from thinking that the label is the start of a new
> function definition. That's why I put a single space before statement
> labels in the code I write.
But this is not done elsewhere in the kernel and there is already some
inconsistency in that respect in runtime.c.
Since the next patch is adding a new label, I thought it would be
better to make it all consistent first and I chose to follow the
common kernel coding style.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
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
0 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-11-20 12:30 UTC (permalink / raw)
To: Alan Stern
Cc: Linux PM, Brian Norris, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Fri, Nov 20, 2026 at 1:11 PM Rafael J. Wysocki (Intel)
<rafael@kernel.org> wrote:
>
> On Fri, Nov 20, 2026 at 3:47 AM Alan Stern <stern@rowland.harvard.edu> wrote:
> >
> > On Thu, Nov 19, 2026 at 09:17:08PM +0100, Rafael J. Wysocki wrote:
> > > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> > >
> > > Drop space characters preceding labels from the core runtime PM code.
> > >
> > > No functional impact.
> > >
> > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > ---
> >
> > There actually is a valid reason for keeping the spaces before
> > statement labels, although you may not agree with it. Namely, the space
> > prevents "diff -p" from thinking that the label is the start of a new
> > function definition. That's why I put a single space before statement
> > labels in the code I write.
>
> But this is not done elsewhere in the kernel and there is already some
> inconsistency in that respect in runtime.c.
>
> Since the next patch is adding a new label, I thought it would be
> better to make it all consistent first and I chose to follow the
> common kernel coding style.
I can drop the $subject patch though if this is an issue and make the
next one put a space in front of the new label.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
2026-11-20 12:30 ` Rafael J. Wysocki (Intel)
@ 2026-11-20 16:11 ` Alan Stern
0 siblings, 0 replies; 16+ messages in thread
From: Alan Stern @ 2026-11-20 16:11 UTC (permalink / raw)
To: Rafael J. Wysocki (Intel)
Cc: Linux PM, Brian Norris, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Fri, Nov 20, 2026 at 01:30:14PM +0100, Rafael J. Wysocki (Intel) wrote:
> On Fri, Nov 20, 2026 at 1:11 PM Rafael J. Wysocki (Intel)
> <rafael@kernel.org> wrote:
> >
> > On Fri, Nov 20, 2026 at 3:47 AM Alan Stern <stern@rowland.harvard.edu> wrote:
> > >
> > > On Thu, Nov 19, 2026 at 09:17:08PM +0100, Rafael J. Wysocki wrote:
> > > > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> > > >
> > > > Drop space characters preceding labels from the core runtime PM code.
> > > >
> > > > No functional impact.
> > > >
> > > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > > ---
> > >
> > > There actually is a valid reason for keeping the spaces before
> > > statement labels, although you may not agree with it. Namely, the space
> > > prevents "diff -p" from thinking that the label is the start of a new
> > > function definition. That's why I put a single space before statement
> > > labels in the code I write.
> >
> > But this is not done elsewhere in the kernel and there is already some
> > inconsistency in that respect in runtime.c.
> >
> > Since the next patch is adding a new label, I thought it would be
> > better to make it all consistent first and I chose to follow the
> > common kernel coding style.
>
> I can drop the $subject patch though if this is an issue and make the
> next one put a space in front of the new label.
Oh, it's not a problem. I just wanted to point out that putting a space
before statement labels is not a completely senseless thing to do.
After you've read a few diff listings with statement labels where the
function names should be, so you have to match the diff against the
original source to know where a particular hunk belongs, you understand
why.
This is your subsystem; you can solve formatting issues however you
like.
Alan Stern
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v1 3/4] PM: runtime: Let devices suspend after an attempted resume
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 ` [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths Rafael J. Wysocki
2026-11-19 20:17 ` [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels Rafael J. Wysocki
@ 2026-11-19 20:17 ` 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
3 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki @ 2026-11-19 20:17 UTC (permalink / raw)
To: Linux PM
Cc: Brian Norris, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
On a successful runtime resume of a device, rpm_idle() is called by
rpm_resume(), but only if the runtime PM status of the device was not
RPM_ACTIVE when rpm_resume() was called, which is inconsistent. For
instance, if the given device uses autosuspend, an autosuspend timer
will most likely be started for it after it has been actually resumed
by rpm_resume(), which will not happen if it is already active when it
is passed to that function. This may lead to observable differences in
behavior going forward.
Address it by rearranging rpm_resume() to call rpm_idle() also if the
device has been active to start with.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
drivers/base/power/runtime.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
index 5a0b4600c244..1a3b32e299d8 100644
--- a/drivers/base/power/runtime.c
+++ b/drivers/base/power/runtime.c
@@ -794,7 +794,8 @@ static int rpm_suspend(struct device *dev, int rpmflags)
* suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
* flag is set then queue a resume request; otherwise run the
* ->runtime_resume() callback directly. Queue an idle notification for the
- * device if the resume succeeded.
+ * device if the resume succeeded or the device's runtime PM status was
+ * RPM_ACTIVE to start with.
*
* This function must be called under dev->power.lock with interrupts disabled.
*/
@@ -840,7 +841,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
*/
pm_runtime_mark_last_busy(dev);
retval = 1;
- goto out;
+ goto success;
}
if (dev->power.runtime_status == RPM_RESUMING ||
@@ -971,8 +972,11 @@ static int rpm_resume(struct device *dev, int rpmflags)
}
wake_up_all(&dev->power.wait_queue);
- if (retval >= 0)
- rpm_idle(dev, RPM_ASYNC);
+ if (retval < 0)
+ goto out;
+
+success:
+ rpm_idle(dev, RPM_ASYNC);
out:
if (parent && !dev->power.irq_safe) {
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH v1 3/4] PM: runtime: Let devices suspend after an attempted resume
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-24 16:23 ` Rafael J. Wysocki (Intel)
0 siblings, 1 reply; 16+ messages in thread
From: Brian Norris @ 2026-11-20 21:04 UTC (permalink / raw)
To: Rafael J. Wysocki
Cc: Linux PM, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Thu, Nov 19, 2026 at 09:17:51PM +0100, Rafael J. Wysocki wrote:
> From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
>
> On a successful runtime resume of a device, rpm_idle() is called by
> rpm_resume(), but only if the runtime PM status of the device was not
> RPM_ACTIVE when rpm_resume() was called, which is inconsistent. For
> instance, if the given device uses autosuspend, an autosuspend timer
> will most likely be started for it after it has been actually resumed
> by rpm_resume(), which will not happen if it is already active when it
> is passed to that function. This may lead to observable differences in
> behavior going forward.
>
> Address it by rearranging rpm_resume() to call rpm_idle() also if the
> device has been active to start with.
>
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
> drivers/base/power/runtime.c | 12 ++++++++----
> 1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
> index 5a0b4600c244..1a3b32e299d8 100644
> --- a/drivers/base/power/runtime.c
> +++ b/drivers/base/power/runtime.c
> @@ -794,7 +794,8 @@ static int rpm_suspend(struct device *dev, int rpmflags)
> * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
> * flag is set then queue a resume request; otherwise run the
> * ->runtime_resume() callback directly. Queue an idle notification for the
> - * device if the resume succeeded.
> + * device if the resume succeeded or the device's runtime PM status was
> + * RPM_ACTIVE to start with.
> *
> * This function must be called under dev->power.lock with interrupts disabled.
> */
> @@ -840,7 +841,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
> */
> pm_runtime_mark_last_busy(dev);
> retval = 1;
> - goto out;
> + goto success;
> }
>
> if (dev->power.runtime_status == RPM_RESUMING ||
> @@ -971,8 +972,11 @@ static int rpm_resume(struct device *dev, int rpmflags)
> }
> wake_up_all(&dev->power.wait_queue);
>
> - if (retval >= 0)
> - rpm_idle(dev, RPM_ASYNC);
> + if (retval < 0)
> + goto out;
> +
> +success:
> + rpm_idle(dev, RPM_ASYNC);
It's super common for rpm_resume() to be called with a non-zero
usage_count (e.g., all pm_runtime_get*(), pm_runtime_forbid()). Do we
really want them all to turn into rpm_idle() (and predictably fail)? For
one, it adds a lot more trace_event noise.
Maybe we could check RPM_GET_PUT before doing this? Or check
usage_count? I wouldn't want to go too far on re-implementing all the
rpm_idle() pre-conditions though...
Also: I'm not quite sure what's *really* expected here, but drivers like
drivers/net/ethernet/intel/e1000e/netdev.c (no autosuspend; they use
.runtime_idle() to re-implement autosuspend, more or less) might see a
change in behavior. For example, see its watchdog, which does:
/* Cancel scheduled suspend requests. */
pm_runtime_resume(netdev->dev.parent);
For cases where the device stayed RPM_ACTIVE but usage_count == 0 (very
common, I think), this does indeed cancel any pending suspend timers,
but then it immediately queues another idle request. (That idle request
will end up failing anyway, so no functional issue. It just means the
comments are a little confusing.)
Do we really want that? Or maybe these drivers are just outliers, and
should be rewritten anyway.
Brian
>
> out:
> if (parent && !dev->power.irq_safe) {
> --
> 2.51.0
>
>
>
>
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH v1 3/4] PM: runtime: Let devices suspend after an attempted resume
2026-11-20 21:04 ` Brian Norris
@ 2026-11-24 16:23 ` Rafael J. Wysocki (Intel)
2026-11-26 1:12 ` Brian Norris
0 siblings, 1 reply; 16+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-11-24 16:23 UTC (permalink / raw)
To: Brian Norris
Cc: Rafael J. Wysocki, Linux PM, Alan Stern, Sakari Ailus,
Ulf Hansson, Linux Driver Core Development
On Fri, Nov 20, 2026 at 10:04 PM Brian Norris <briannorris@chromium.org> wrote:
>
> On Thu, Nov 19, 2026 at 09:17:51PM +0100, Rafael J. Wysocki wrote:
> > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> >
> > On a successful runtime resume of a device, rpm_idle() is called by
> > rpm_resume(), but only if the runtime PM status of the device was not
> > RPM_ACTIVE when rpm_resume() was called, which is inconsistent. For
> > instance, if the given device uses autosuspend, an autosuspend timer
> > will most likely be started for it after it has been actually resumed
> > by rpm_resume(), which will not happen if it is already active when it
> > is passed to that function. This may lead to observable differences in
> > behavior going forward.
> >
> > Address it by rearranging rpm_resume() to call rpm_idle() also if the
> > device has been active to start with.
> >
> > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > ---
> > drivers/base/power/runtime.c | 12 ++++++++----
> > 1 file changed, 8 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
> > index 5a0b4600c244..1a3b32e299d8 100644
> > --- a/drivers/base/power/runtime.c
> > +++ b/drivers/base/power/runtime.c
> > @@ -794,7 +794,8 @@ static int rpm_suspend(struct device *dev, int rpmflags)
> > * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
> > * flag is set then queue a resume request; otherwise run the
> > * ->runtime_resume() callback directly. Queue an idle notification for the
> > - * device if the resume succeeded.
> > + * device if the resume succeeded or the device's runtime PM status was
> > + * RPM_ACTIVE to start with.
> > *
> > * This function must be called under dev->power.lock with interrupts disabled.
> > */
> > @@ -840,7 +841,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
> > */
> > pm_runtime_mark_last_busy(dev);
> > retval = 1;
> > - goto out;
> > + goto success;
> > }
> >
> > if (dev->power.runtime_status == RPM_RESUMING ||
> > @@ -971,8 +972,11 @@ static int rpm_resume(struct device *dev, int rpmflags)
> > }
> > wake_up_all(&dev->power.wait_queue);
> >
> > - if (retval >= 0)
> > - rpm_idle(dev, RPM_ASYNC);
> > + if (retval < 0)
> > + goto out;
> > +
> > +success:
> > + rpm_idle(dev, RPM_ASYNC);
>
> It's super common for rpm_resume() to be called with a non-zero
> usage_count (e.g., all pm_runtime_get*(), pm_runtime_forbid()). Do we
> really want them all to turn into rpm_idle() (and predictably fail)? For
> one, it adds a lot more trace_event noise.
>
> Maybe we could check RPM_GET_PUT before doing this? Or check
> usage_count? I wouldn't want to go too far on re-implementing all the
> rpm_idle() pre-conditions though...
I'm not sure why this is an issue if the device is already active when
rpm_resume() is called and not when the device is resumed by
rpm_resume(). It doesn't look like this should be an issue only in
one of those cases.
> Also: I'm not quite sure what's *really* expected here, but drivers like
> drivers/net/ethernet/intel/e1000e/netdev.c (no autosuspend; they use
> .runtime_idle() to re-implement autosuspend, more or less) might see a
> change in behavior.
Everybody may see a change in behavior, but it only applies to one of
the two cases which are handled differently now.
> For example, see its watchdog, which does:
>
> /* Cancel scheduled suspend requests. */
> pm_runtime_resume(netdev->dev.parent);
>
> For cases where the device stayed RPM_ACTIVE but usage_count == 0 (very
> common, I think), this does indeed cancel any pending suspend timers,
No, it doesn't cancel running autosuspend timers, for example.
> but then it immediately queues another idle request. (That idle request
> will end up failing anyway, so no functional issue. It just means the
> comments are a little confusing.)
So the comment is confusing regardless of the $subject patch.
> Do we really want that? Or maybe these drivers are just outliers, and
> should be rewritten anyway.
Probably.
If the device is RPM_ACTIVE to start with, all of the rpm_resume()
actions related to resuming it may be skipped, quite obviously, but
I'm not sure why any other rpm_resume() handling of the device should
be different in that case. This only causes the behavior to be less
consistent and less predictable IMV. In particular, if the device
uses autosuspend, an autosuspend timer should be started for it
regardless of whether or not it is RPM_ACTIVE to start with. Failing
to do so may cause it to never suspend going forward in theory.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v1 3/4] PM: runtime: Let devices suspend after an attempted resume
2026-11-24 16:23 ` Rafael J. Wysocki (Intel)
@ 2026-11-26 1:12 ` Brian Norris
0 siblings, 0 replies; 16+ messages in thread
From: Brian Norris @ 2026-11-26 1:12 UTC (permalink / raw)
To: Rafael J. Wysocki (Intel)
Cc: Linux PM, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
On Tue, Nov 24, 2026 at 05:23:52PM +0100, Rafael J. Wysocki (Intel) wrote:
> On Fri, Nov 20, 2026 at 10:04 PM Brian Norris <briannorris@chromium.org> wrote:
> >
> > On Thu, Nov 19, 2026 at 09:17:51PM +0100, Rafael J. Wysocki wrote:
> > > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> > >
> > > On a successful runtime resume of a device, rpm_idle() is called by
> > > rpm_resume(), but only if the runtime PM status of the device was not
> > > RPM_ACTIVE when rpm_resume() was called, which is inconsistent. For
> > > instance, if the given device uses autosuspend, an autosuspend timer
> > > will most likely be started for it after it has been actually resumed
> > > by rpm_resume(), which will not happen if it is already active when it
> > > is passed to that function. This may lead to observable differences in
> > > behavior going forward.
> > >
> > > Address it by rearranging rpm_resume() to call rpm_idle() also if the
> > > device has been active to start with.
> > >
> > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > ---
> > > drivers/base/power/runtime.c | 12 ++++++++----
> > > 1 file changed, 8 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
> > > index 5a0b4600c244..1a3b32e299d8 100644
> > > --- a/drivers/base/power/runtime.c
> > > +++ b/drivers/base/power/runtime.c
> > > @@ -794,7 +794,8 @@ static int rpm_suspend(struct device *dev, int rpmflags)
> > > * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
> > > * flag is set then queue a resume request; otherwise run the
> > > * ->runtime_resume() callback directly. Queue an idle notification for the
> > > - * device if the resume succeeded.
> > > + * device if the resume succeeded or the device's runtime PM status was
> > > + * RPM_ACTIVE to start with.
> > > *
> > > * This function must be called under dev->power.lock with interrupts disabled.
> > > */
> > > @@ -840,7 +841,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
> > > */
> > > pm_runtime_mark_last_busy(dev);
> > > retval = 1;
> > > - goto out;
> > > + goto success;
> > > }
> > >
> > > if (dev->power.runtime_status == RPM_RESUMING ||
> > > @@ -971,8 +972,11 @@ static int rpm_resume(struct device *dev, int rpmflags)
> > > }
> > > wake_up_all(&dev->power.wait_queue);
> > >
> > > - if (retval >= 0)
> > > - rpm_idle(dev, RPM_ASYNC);
> > > + if (retval < 0)
> > > + goto out;
> > > +
> > > +success:
> > > + rpm_idle(dev, RPM_ASYNC);
> >
> > It's super common for rpm_resume() to be called with a non-zero
> > usage_count (e.g., all pm_runtime_get*(), pm_runtime_forbid()). Do we
> > really want them all to turn into rpm_idle() (and predictably fail)? For
> > one, it adds a lot more trace_event noise.
> >
> > Maybe we could check RPM_GET_PUT before doing this? Or check
> > usage_count? I wouldn't want to go too far on re-implementing all the
> > rpm_idle() pre-conditions though...
>
> I'm not sure why this is an issue if the device is already active when
> rpm_resume() is called and not when the device is resumed by
> rpm_resume(). It doesn't look like this should be an issue only in
> one of those cases.
>
You're right, it's a bit spammy in existing cases too. (It's honestly
pretty hard to read rpm trace_event logs without being an expert.)
This comment was made by inspection; I think I'd have to get a feel for
how this looks on a real run to be sure. Feel free to ignore this if you
don't think it's concerning (and I don't come back with anything more
solid).
> > drivers/net/ethernet/intel/e1000e/netdev.c (no autosuspend; they use
> > .runtime_idle() to re-implement autosuspend, more or less) might see a
> > change in behavior.
>
> Everybody may see a change in behavior, but it only applies to one of
> the two cases which are handled differently now.
>
> > For example, see its watchdog, which does:
> >
> > /* Cancel scheduled suspend requests. */
> > pm_runtime_resume(netdev->dev.parent);
> >
> > For cases where the device stayed RPM_ACTIVE but usage_count == 0 (very
> > common, I think), this does indeed cancel any pending suspend timers,
>
> No, it doesn't cancel running autosuspend timers, for example.
Well, this driver doesn't use autosuspend (yet).
> > but then it immediately queues another idle request. (That idle request
> > will end up failing anyway, so no functional issue. It just means the
> > comments are a little confusing.)
>
> So the comment is confusing regardless of the $subject patch.
No, my point was that your patch changes that behavior from:
"cancel scheduled [non-auto]suspend requests" (an accurate comment)
to
"cancel scheduled [non-auto]suspend requests" and immediately run
.runtime_idle() again (and fail)
--> a somewhat misleading comment
Or perhaps I'm misunderstanding the before/after behavior here.
> > Do we really want that? Or maybe these drivers are just outliers, and
> > should be rewritten anyway.
>
> Probably.
>
> If the device is RPM_ACTIVE to start with, all of the rpm_resume()
> actions related to resuming it may be skipped, quite obviously, but
> I'm not sure why any other rpm_resume() handling of the device should
> be different in that case. This only causes the behavior to be less
> consistent and less predictable IMV. In particular, if the device
> uses autosuspend, an autosuspend timer should be started for it
> regardless of whether or not it is RPM_ACTIVE to start with. Failing
> to do so may cause it to never suspend going forward in theory.
Makes sense. Really, I was only pointing out changes (not necessarily
problems) with a few quirky non-autosuspend drivers [1], and if these
are expected, perhaps it's worth following up with the drivers in
question that were describing (if not fully depending on) corner cases
of behavior that we're changing here.
But on second look, maybe there's not much to fix for these drivers.
It's not really a "bug" to perform excess .runtime_idle() checks, even
if it's wasteful, and I don't see great alternatives.
Brian
[1] drivers/net/ethernet/intel/e1000e/netdev.c
drivers/net/ethernet/intel/igb/igb_main.c
drivers/net/ethernet/intel/igc/igc_main.c
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v1 4/4] PM: runtime: Drop direct last_busy updates from rpm_resume()
2026-11-19 20:13 [PATCH v1 0/4] PM: runtime: Close some gaps related to autosuspend Rafael J. Wysocki
` (2 preceding siblings ...)
2026-11-19 20:17 ` [PATCH v1 3/4] PM: runtime: Let devices suspend after an attempted resume Rafael J. Wysocki
@ 2026-11-19 20:18 ` Rafael J. Wysocki
3 siblings, 0 replies; 16+ messages in thread
From: Rafael J. Wysocki @ 2026-11-19 20:18 UTC (permalink / raw)
To: Linux PM
Cc: Brian Norris, Alan Stern, Sakari Ailus, Ulf Hansson,
Linux Driver Core Development
From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
Now that rpm_resume() calls rpm_idle() even if the device was active
to start with, both direct last_busy updates in rpm_resume() are on
paths that also call rpm_idle(), which updates last_busy itself.
Accordingly, drop the direct last_busy updates from rpm_resume().
No intentional functional impact.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
drivers/base/power/runtime.c | 7 -------
1 file changed, 7 deletions(-)
diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
index 1a3b32e299d8..19d44b258cd9 100644
--- a/drivers/base/power/runtime.c
+++ b/drivers/base/power/runtime.c
@@ -834,12 +834,6 @@ static int rpm_resume(struct device *dev, int rpmflags)
pm_runtime_deactivate_timer(dev);
if (dev->power.runtime_status == RPM_ACTIVE) {
- /*
- * The attempt to resume the device means that it is needed for
- * something, so update its last_busy timestamp as in the case
- * in which it actually resumes below.
- */
- pm_runtime_mark_last_busy(dev);
retval = 1;
goto success;
}
@@ -966,7 +960,6 @@ static int rpm_resume(struct device *dev, int rpmflags)
} else {
no_callback:
__update_runtime_status(dev, RPM_ACTIVE);
- pm_runtime_mark_last_busy(dev);
if (parent)
atomic_inc(&parent->power.child_count);
}
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread