PM: runtime: Fix issues related to autosuspend
Subsystems (4)
Rafael J. Wysocki
maintainerv2 cover PM: runtime: Fix issues related to autosuspend
Hi All,
This is a follow-up series for
which is in linux-next and it replaces
https://lore.kernel.org/linux-pm/[email protected]/
(hence v2).
First of all, there is a potential issue with respecting autosuspend delays in pm_runtime_put_autosuspend() and pm_runtime_put_sync_autosuspend() that existed before the above commit, which is addressed in the first patch, and that fix is relevant for the other patches in the series.
Second, the commit above overlooked some cases in which a running autosuspend timer may trigger between dropping a runtime PM reference to a device and updating its last_busy timestamp, which may cause the device to suspend without respecting its autosuspend delay. Those cases are addressed by the second patch.
Next, I was mistaken when I wrote this in the changelog of the above commit:
Updating the last_busy timestamp when rpm_idle() returns an error without calling rpm_suspend() is not useful because the device is then going to remain active for an unknown amount of time and its autosuspend delay needs to take effect later.
There actually is a reason for doing this which is that an autosuspend timer (left behind by an intermediate runtime resume) may be running when rpm_idle() runs and that autosuspend timer needs to be prevented from using a soon-to-be-stale last_busy timestamp value. That is addressed by the 3rd patch.
Finally, the last patch updates a kerneldoc comment that has become outdated after the commit above.
For details, please see the patch changelogs.
Thanks!
Rafael J. Wysocki
maintainerPATCH v2 1/4 PM: runtime: Add memory barrier before reading last_busy
From: "Rafael J. Wysocki" <[email protected]>
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.
---
New patch in v2, based on
--- drivers/base/power/runtime.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/base/power/runtime.c +7 -0
@@ -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(). +*/ +smp_rmb (); expires =READ_ONCE (dev->power.last_busy); expires += (u64 )autosuspend_delay *NSEC_PER_MSEC ;if (expires >ktime_get_mono_fast_ns ())
Signature · 1 line
2.51.0
Re: [PATCH v2 1/4] PM: runtime: Add memory barrier before reading last_busy
Quoted 16 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
The users of
pm_runtime_put_sync_autosuspend()andpm_runtime_put_autosuspend()can expect that the autosuspend delay of the device will be respected after calling one of them because they both invokepm_runtime_mark_last_busy()upfront. However, that may not happen due to a leftover autosuspend timer running when thelast_busyupdate takes place, in which caserpm_suspend()triggered by the timer may run in parallel with the locklesslast_busyupdate.Namely, the
last_busyaccess inpm_runtime_autosuspend_expiration()called byrpm_suspend()may be reordered before the initial read of the device's runtime PM usage counter inrpm_suspend(), and iflast_busygets updated between those two reads, the update will be missed and the device may be suspended right away.
Quoted 7 lines
To prevent that from occurring, add a read memory barrier before the
last_busyread inpm_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 <[email protected]>
If you're into this sort of thing:
and
Quoted 15 lines
---
New patch in v2, based on
--- drivers/base/power/runtime.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/base/power/runtime.c +0
-0
drivers/base/power/runtime.c u64 pm_runtime_autosuspend_expiration(struct device *dev) · line 193
Quoted 8 lines
drivers/base/power/runtime.c +5
-0@@ -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.
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.
Thanks,
Brian
Quoted 11 lines
drivers/base/power/runtime.c +2
-0+*/ +smp_rmb (); expires =READ_ONCE (dev->power.last_busy); expires += (u64 )autosuspend_delay *NSEC_PER_MSEC ;if (expires >ktime_get_mono_fast_ns ())Signature · 1 line
2.51.0
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v2 1/4] PM: runtime: Add memory barrier before reading last_busy
Quoted 22 lines · Brian Norris
Quoted 20 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
The users of
pm_runtime_put_sync_autosuspend()andpm_runtime_put_autosuspend()can expect that the autosuspend delay of the device will be respected after calling one of them because they both invokepm_runtime_mark_last_busy()upfront. However, that may not happen due to a leftover autosuspend timer running when thelast_busyupdate takes place, in which caserpm_suspend()triggered by the timer may run in parallel with the locklesslast_busyupdate.Namely, the
last_busyaccess inpm_runtime_autosuspend_expiration()called byrpm_suspend()may be reordered before the initial read of the device's runtime PM usage counter inrpm_suspend(), and iflast_busygets 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_busyread inpm_runtime_autosuspend_expiration()along with a comment explaining the reason for adding it.
Quoted 8 lines
Quoted 3 lines
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 <[email protected]>If you're into this sort of thing:
OK
Quoted 3 lines
and
Reviewed-by Brian Norris <[email protected]>
Thanks!
Quoted 18 lines
Quoted 18 lines
---
New patch in v2, based on
--- drivers/base/power/runtime.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/base/power/runtime.c +0
-0@@ -186,6 +186,13 @@ u64 pm_runtime_autosuspend_expiration(struct device *dev)if (autosuspend_delay <0 )return 0 ;
Quoted 9 lines
drivers/base/power/runtime.c u64 pm_runtime_autosuspend_expiration(struct device *dev) · line 193
Quoted 5 lines
drivers/base/power/runtime.c +5
-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.
Quoted 15 lines
I think it might make for a better long-lasting comment if we just write in terms of "
last_busyupdate" 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 thislast_busyaccess, to avoid missing
* anylast_busyupdate performed before the last lockless usage
* count decrement.
*/
Quoted 3 lines
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. ;-)
Quoted 6 lines
Quoted 6 lines
drivers/base/power/runtime.c +2
-1+*/ +smp_rmb (); expires =READ_ONCE (dev->power.last_busy); expires += (u64 )autosuspend_delay *NSEC_PER_MSEC ;if (expires >ktime_get_mono_fast_ns ()) --
Re: [PATCH v2 1/4] PM: runtime: Add memory barrier before reading last_busy
Quoted 68 lines · Rafael J. Wysocki (Intel)
Quoted 22 lines · Brian Norris
Quoted 20 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
The users of
pm_runtime_put_sync_autosuspend()andpm_runtime_put_autosuspend()can expect that the autosuspend delay of the device will be respected after calling one of them because they both invokepm_runtime_mark_last_busy()upfront. However, that may not happen due to a leftover autosuspend timer running when thelast_busyupdate takes place, in which caserpm_suspend()triggered by the timer may run in parallel with the locklesslast_busyupdate.Namely, the
last_busyaccess inpm_runtime_autosuspend_expiration()called byrpm_suspend()may be reordered before the initial read of the device's runtime PM usage counter inrpm_suspend(), and iflast_busygets 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_busyread inpm_runtime_autosuspend_expiration()along with a comment explaining the reason for adding it.Quoted 8 lines
Quoted 3 lines
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 <[email protected]>If you're into this sort of thing:
OK
Quoted 3 lines
and
Reviewed-by Brian Norris <[email protected]>Thanks!
Quoted 18 lines
Quoted 18 lines
---
New patch in v2, based on
--- drivers/base/power/runtime.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/base/power/runtime.c +0
-0@@ -186,6 +186,13 @@ u64 pm_runtime_autosuspend_expiration(struct device *dev)if (autosuspend_delay <0 )return 0 ;Quoted 9 lines
drivers/base/power/runtime.c u64 pm_runtime_autosuspend_expiration(struct device *dev) · line 193
Quoted 5 lines
drivers/base/power/runtime.c +5
-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.
Quoted 4 lines
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.
Quoted 17 lines
Quoted 17 lines
I think it might make for a better long-lasting comment if we just write in terms of "
last_busyupdate" 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 thislast_busyaccess, to avoid missing
* anylast_busyupdate performed before the last lockless usage
* count decrement.
*/But again, that's a bit nitpicky.
Quoted 7 lines
Quoted 1 line
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
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/base/power/runtime.c +2
-1+*/ +smp_rmb (); expires =READ_ONCE (dev->power.last_busy); expires += (u64 )autosuspend_delay *NSEC_PER_MSEC ;if (expires >ktime_get_mono_fast_ns ()) --
Rafael J. Wysocki
maintainerPATCH v2 2/4 PM: runtime: Avoid suspends without respecting autosuspend delay
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
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().
---
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(-)
drivers/base/power/runtime.c +47 -6
@@ -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 );
Signature · 1 line
2.51.0
Re: [PATCH v2 2/4] PM: runtime: Avoid suspends without respecting autosuspend delay
Quoted 67 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
Commit
26236a3e8803("PM: runtime: Updatelast_busybefore callingrpm_suspend()inrpm_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()allowsrpm_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_countof a device's parent is dropped inrpm_suspend()and__pm_runtime_set_status(), and in the callers ofpm_runtime_release_supplier().Prevent them all from occurring by adding
last_busyupdates in a few places whererpm_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_busyand the pairing of memory barriers, and accordingly update the memory barrier comment inpm_runtime_autosuspend_expiration().Fixes 26236a3e8803 ("PM: runtime: Update last_busy before calling rpm_suspend() in rpm_idle()")Signed-off-by Rafael J. Wysocki <[email protected]>---
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(-)drivers/base/power/runtime.c +7
-4@@ -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;
drivers/base/power/runtime.c void pm_runtime_release_supplier(struct device_link *link) · line 347
Quoted 10 lines
drivers/base/power/runtime.c +9
-0+/* +* 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:
Quoted 41 lines
drivers/base/power/runtime.c +21
-1+/* * 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;
drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev, int rpmflags) · line 1181
Quoted 10 lines
drivers/base/power/runtime.c +8
-0if (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
Quoted 19 lines
drivers/base/power/runtime.c +2
-1+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 );Signature · 1 line
2.51.0
Re: [PATCH v2 2/4] PM: runtime: Avoid suspends without respecting autosuspend delay
Quoted 14 lines · Brian Norris
Quoted 13 lines · Rafael J. Wysocki
drivers/base/power/runtime.c +9
-0@@ -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);
[...]
Quoted 2 lines
Perhaps this this
last_busyupdate should go inside the 'if'? Or otherwise refactor this function to be less subtle.
Sorry, there's no 'if' in pm_runtime_release_supplier() -- it's a 'while' loop.
We could move it into the 'while', although that'd probably induce redundant mark_last_busy updates. So perhaps the "otherwise refactor" option would be more attractive?
Brian
P.S. My redundant "this this" is a bit ironic :)
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v2 2/4] PM: runtime: Avoid suspends without respecting autosuspend delay
Quoted 22 lines · Brian Norris
Quoted 14 lines · Brian Norris
Quoted 13 lines · Rafael J. Wysocki
drivers/base/power/runtime.c +9
-0@@ -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);[...]
Quoted 2 lines
Perhaps this this
last_busyupdate should go inside the 'if'? Or otherwise refactor this function to be less subtle.
Quoted 6 lines
Sorry, there's no 'if' in
pm_runtime_release_supplier()-- it's a 'while' loop.We could move it into the 'while', although that'd probably induce redundant
mark_last_busyupdates. So perhaps the "otherwise refactor" option would be more attractive?
A rather obvious thing would be to move the pm_runtime_mark_last_busy() call right before pm_runtime_put_noidle() which is fully ordered and only called when the supplier's usage count actually goes down.
I'd prefer to cut a separate patch for that though.
Rafael J. Wysocki
maintainerPATCH v2 3/4 PM: runtime: Update last_busy in all rpm_idle() paths
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
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.
Namely, 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 suspend the device without respecting the autosuspend delay. Moreover, an autosuspend timer may fire while the callback is running in rpm_idle() and it will not see the new last_busy value in that case.
To address these cases, add one more pm_runtime_mark_last_busy() call to rpm_idle(), before the first retval check, and add a comment next to it to explain the rationale for updating last_busy at that point.
---
v1 -> v2:
* Address only one case addressed by the v1 (out of two)
* Add a new last_busy update instead of moving the existing one in case the runtime idle callback waits for the device to become idle or similar
--- drivers/base/power/runtime.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/base/power/runtime.c +7 -0
@@ -538,6 +538,13 @@ static int rpm_idle(struct device *dev, int rpmflags)else if (dev->power.idle_notification) retval = -EINPROGRESS ; +/* +* 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;
Signature · 1 line
2.51.0
Re: [PATCH v2 3/4] PM: runtime: Update last_busy in all rpm_idle() paths
Quoted 19 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
The changelog of commit
26236a3e8803("PM: runtime: Updatelast_busybefore callingrpm_suspend()inrpm_idle()") stated thatrpm_idle()only needed to updatelast_busywhen it was about to callrpm_suspend(), but that is incorrect. There are still cases in which a device may be autosuspended earlier than expected.Namely, when
rpm_idle()returns without callingrpm_suspend(), for example because the callback has returned a transient error, while an autosuspend timer is running, the timer may suspend the device without respecting the autosuspend delay. Moreover, an autosuspend timer may fire while the callback is running inrpm_idle()and it will not see the newlast_busyvalue in that case.To address these cases, add one more
pm_runtime_mark_last_busy()call torpm_idle(), before the first retval check, and add a comment next to it to explain the rationale for updatinglast_busyat that point.
Quoted 8 lines
Fixes 26236a3e8803 ("PM: runtime: Update last_busy before calling rpm_suspend() in rpm_idle()")Signed-off-by Rafael J. Wysocki <[email protected]>---
v1 -> v2:
* Address only one case addressed by the v1 (out of two)
* Add a newlast_busyupdate instead of moving the existing one in case the runtime idle callback waits for the device to become idle or similar
I think there's a lot of noise in here about potentially-redundant last_busy updates, but I suppose that's a better problem to have than inconsistent/missing updates. I think we'd be in a happier world once rpm_idle() is gone.
Rafael J. Wysocki
maintainerPATCH v2 4/4 PM: runtime: Update stale pm_runtime_put_sync() kerneldoc comment
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
The pm_runtime_put_sync() kerneldoc comment still claims that it does not update the last access time which is not correct any more. Also the suggestion to use pm_runtime_put_sync_autosuspend() or instead of pm_runtime_put_sync() for devices with autosuspend enabled is outdated.
Make that kerneldoc comment more up to date.
---
New patch in v2.
--- include/linux/pm_runtime.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/pm_runtime.h +3 -3
@@ -692,9 +692,9 @@ DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled,* Decrement the runtime PM usage counter of @dev. If the counter drops to zero, * synchronously evaluate and trigger idle/suspend handling. * -* Note that this does not update the last access time, but it does respect -* existing autosuspend timers. If @dev uses autosuspend, consider using -* pm_runtime_put_sync_autosuspend() or pm_runtime_put_sync_suspend() instead. +* This updates the last access time and respects existing autosuspend timers. +* If @dev uses autosuspend and respecting its autosuspend delay is not +* desirable, consider using pm_runtime_put_sync_suspend() instead. * * The runtime PM usage counter of @dev remains decremented in all cases, even * if it returns an error code.
Signature · 1 line
2.51.0
Re: [PATCH v2 4/4] PM: runtime: Update stale pm_runtime_put_sync() kerneldoc comment
Quoted 7 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
The
pm_runtime_put_sync()kerneldoc comment still claims that it does not update the last access time which is not correct any more. Also the suggestion to usepm_runtime_put_sync_autosuspend()or instead ofpm_runtime_put_sync()for devices with autosuspend enabled is outdated.
Quoted 6 lines
Make that kerneldoc comment more up to date.
Fixes 26236a3e8803 ("PM: runtime: Update last_busy before calling rpm_suspend() in rpm_idle()")Reported-by Brian Norris <[email protected]>Signed-off-by Rafael J. Wysocki <[email protected]>---
We're halfway between recommending using the autosuspend() variants, and not. e.g., pm_runtime_put_sync_autosuspend() still says, "Prefer this API over pm_runtime_put_sync() for devices that use autosuspend", while you're (rightfully) changing pm_runtime_put_sync()'s recommendations to be a bit weaker. And there's even more in Documentation/power/runtime_pm.rst that suggests using the autosuspend() variation.
But that's a larger thing to rework in the docs. We could either wait until we remove .runtime_idle() (so there are zero differences), or we could start changing recommendations to the rpm_idle()-based helpers (since they are a superset of rpm_suspend()-based / *_autosuspend() helpers) right now.
This is more a request for input, rather than a request for you to change things. Perhaps I can take another pass at the docs, pending your input. In any case, this is a fine start IMO:
Quoted 29 lines
New patch in v2.
--- include/linux/pm_runtime.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)include/linux/pm_runtime.h +3
-3@@ -692,9 +692,9 @@ DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled,* Decrement the runtime PM usage counter of @dev. If the counter drops to zero, * synchronously evaluate and trigger idle/suspend handling. * -* Note that this does not update the last access time, but it does respect -* existing autosuspend timers. If @dev uses autosuspend, consider using -* pm_runtime_put_sync_autosuspend() or pm_runtime_put_sync_suspend() instead. +* This updates the last access time and respects existing autosuspend timers. +* If @dev uses autosuspend and respecting its autosuspend delay is not +* desirable, consider using pm_runtime_put_sync_suspend() instead. * * The runtime PM usage counter of @dev remains decremented in all cases, even * if it returns an error code. Signature · 1 line
2.51.0
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v2 4/4] PM: runtime: Update stale pm_runtime_put_sync() kerneldoc comment
Quoted 30 lines · Brian Norris
Quoted 7 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
The
pm_runtime_put_sync()kerneldoc comment still claims that it does not update the last access time which is not correct any more. Also the suggestion to usepm_runtime_put_sync_autosuspend()or instead ofpm_runtime_put_sync()for devices with autosuspend enabled is outdated.Quoted 6 lines
Make that kerneldoc comment more up to date.
Fixes 26236a3e8803 ("PM: runtime: Update last_busy before calling rpm_suspend() in rpm_idle()")Reported-by Brian Norris <[email protected]>Signed-off-by Rafael J. Wysocki <[email protected]>---
We're halfway between recommending using the
autosuspend()variants, and not. e.g.,pm_runtime_put_sync_autosuspend()still says, "Prefer this API overpm_runtime_put_sync()for devices that use autosuspend", while you're (rightfully) changing pm_runtime_put_sync()'s recommendations to be a bit weaker. And there's even more inDocumentation/power/runtime_pm.rstthat suggests using theautosuspend()variation.But that's a larger thing to rework in the docs. We could either wait until we remove .runtime_idle() (so there are zero differences), or we could start changing recommendations to the rpm_idle()-based helpers (since they are a superset of rpm_suspend()-based /
*_autosuspend()helpers) right now.
Quoted 3 lines
This is more a request for input, rather than a request for you to change things. Perhaps I can take another pass at the docs, pending your input. In any case, this is a fine start IMO:
The documentation needs to be updated regardless of the plans to remove .runtime_idle() and it need not be updated all at once.
I'd say let's start updating it and if we end up making more changes to the code in the meantime, they can be taken into account as we go.
Quoted 36 lines · Rafael J. Wysocki
Hi All,
This is a follow-up series for
which is in linux-next and it replaces
https://lore.kernel.org/linux-pm/[email protected]/
(hence v2).
First of all, there is a potential issue with respecting autosuspend delays in
pm_runtime_put_autosuspend()andpm_runtime_put_sync_autosuspend()that existed before the above commit, which is addressed in the first patch, and that fix is relevant for the other patches in the series.Second, the commit above overlooked some cases in which a running autosuspend timer may trigger between dropping a runtime PM reference to a device and updating its
last_busytimestamp, which may cause the device to suspend without respecting its autosuspend delay. Those cases are addressed by the second patch.Next, I was mistaken when I wrote this in the changelog of the above commit:
Updating the
last_busytimestamp whenrpm_idle()returns an error without callingrpm_suspend()is not useful because the device is then going to remain active for an unknown amount of time and its autosuspend delay needs to take effect later.There actually is a reason for doing this which is that an autosuspend timer (left behind by an intermediate runtime resume) may be running when
rpm_idle()runs and that autosuspend timer needs to be prevented from using a soon-to-be-stalelast_busytimestamp value. That is addressed by the 3rd patch.
Quoted 9 lines
Finally, the last patch updates a kerneldoc comment that has become outdated after the commit above.
For details, please see the patch changelogs.
Thanks!
Again, thanks for working on this! For the series, please add:
As somewhat also proven by the subject $series, there are some churns that we need to deal with in regards to the intermediate rpm_idle() phase. I am wondering if it perhaps may be the time to consider removing it altogether, unless we still think it's useful of course.
In the past we have discussed introducing support for *multiple* (a dynamical amount based on what the device supports) low power states to runtime PM. When I started looking into how to implement it, I stumbled over the rpm_idle() thing, which kind of prevented me from moving forward at that point.
What do you think?
Kind regards
Uffe
Rafael J. Wysocki (Intel)
maintainerQuoted 43 lines · Ulf Hansson
Quoted 41 lines · Rafael J. Wysocki
Hi All,
This is a follow-up series for
which is in linux-next and it replaces
https://lore.kernel.org/linux-pm/[email protected]/
(hence v2).
First of all, there is a potential issue with respecting autosuspend delays in
pm_runtime_put_autosuspend()andpm_runtime_put_sync_autosuspend()that existed before the above commit, which is addressed in the first patch, and that fix is relevant for the other patches in the series.Second, the commit above overlooked some cases in which a running autosuspend timer may trigger between dropping a runtime PM reference to a device and updating its
last_busytimestamp, which may cause the device to suspend without respecting its autosuspend delay. Those cases are addressed by the second patch.Next, I was mistaken when I wrote this in the changelog of the above commit:
Updating the
last_busytimestamp whenrpm_idle()returns an error without callingrpm_suspend()is not useful because the device is then going to remain active for an unknown amount of time and its autosuspend delay needs to take effect later.There actually is a reason for doing this which is that an autosuspend timer (left behind by an intermediate runtime resume) may be running when
rpm_idle()runs and that autosuspend timer needs to be prevented from using a soon-to-be-stalelast_busytimestamp value. That is addressed by the 3rd patch.Finally, the last patch updates a kerneldoc comment that has become outdated after the commit above.
For details, please see the patch changelogs.
Quoted 8 lines
Quoted 4 lines
Thanks!
Again, thanks for working on this! For the series, please add:
Reviewed-by Ulf Hansson <[email protected]>
Thank you!
Quoted 4 lines
As somewhat also proven by the subject $series, there are some churns that we need to deal with in regards to the intermediate
rpm_idle()phase. I am wondering if it perhaps may be the time to consider removing it altogether, unless we still think it's useful of course.
I don't think that the rpm_idle() phase is super-useful.
Of course, eliminating it would require getting rid of the runtime idle callbacks in the first place, but we all seem to agree that this should be doable and probably worth doing.
Quoted 7 lines
In the past we have discussed introducing support for *multiple* (a dynamical amount based on what the device supports) low power states to runtime PM. When I started looking into how to implement it, I stumbled over the
rpm_idle()thing, which kind of prevented me from moving forward at that point.What do you think?
Here I have some doubts.
I'm not sure if there is a single model covering multiple idle states that can be applied to every device in every system.
There are different semantics and different requirements. For example, you may consider "clock gate" and "power gate" states for some devices, such that a device needs to go to "clock gate" first and then to "power gate". On the way back, it would need to go to "clock gate" before reaching full power. However, this is not how PCI or ACPI PM works: There you can go from shallower to deeper, but to go the other way around you need to transition straight to full power.
Quoted 62 lines · Rafael J. Wysocki (Intel)
Quoted 43 lines · Ulf Hansson
Quoted 41 lines · Rafael J. Wysocki
Hi All,
This is a follow-up series for
which is in linux-next and it replaces
https://lore.kernel.org/linux-pm/[email protected]/
(hence v2).
First of all, there is a potential issue with respecting autosuspend delays in
pm_runtime_put_autosuspend()andpm_runtime_put_sync_autosuspend()that existed before the above commit, which is addressed in the first patch, and that fix is relevant for the other patches in the series.Second, the commit above overlooked some cases in which a running autosuspend timer may trigger between dropping a runtime PM reference to a device and updating its
last_busytimestamp, which may cause the device to suspend without respecting its autosuspend delay. Those cases are addressed by the second patch.Next, I was mistaken when I wrote this in the changelog of the above commit:
Updating the
last_busytimestamp whenrpm_idle()returns an error without callingrpm_suspend()is not useful because the device is then going to remain active for an unknown amount of time and its autosuspend delay needs to take effect later.There actually is a reason for doing this which is that an autosuspend timer (left behind by an intermediate runtime resume) may be running when
rpm_idle()runs and that autosuspend timer needs to be prevented from using a soon-to-be-stalelast_busytimestamp value. That is addressed by the 3rd patch.Finally, the last patch updates a kerneldoc comment that has become outdated after the commit above.
For details, please see the patch changelogs.
Quoted 8 lines
Quoted 4 lines
Thanks!
Again, thanks for working on this! For the series, please add:
Reviewed-by Ulf Hansson <[email protected]>Thank you!
Quoted 4 lines
As somewhat also proven by the subject $series, there are some churns that we need to deal with in regards to the intermediate
rpm_idle()phase. I am wondering if it perhaps may be the time to consider removing it altogether, unless we still think it's useful of course.
Quoted 5 lines
I don't think that the
rpm_idle()phase is super-useful.Of course, eliminating it would require getting rid of the runtime idle callbacks in the first place, but we all seem to agree that this should be doable and probably worth doing.
Okay, agreed!
Quoted 14 lines
Quoted 8 lines
In the past we have discussed introducing support for *multiple* (a dynamical amount based on what the device supports) low power states to runtime PM. When I started looking into how to implement it, I stumbled over the
rpm_idle()thing, which kind of prevented me from moving forward at that point.What do you think?
Here I have some doubts.
I'm not sure if there is a single model covering multiple idle states that can be applied to every device in every system.
Quoted 7 lines
There are different semantics and different requirements. For example, you may consider "clock gate" and "power gate" states for some devices, such that a device needs to go to "clock gate" first and then to "power gate". On the way back, it would need to go to "clock gate" before reaching full power. However, this is not how PCI or ACPI PM works: There you can go from shallower to deeper, but to go the other way around you need to transition straight to full power.
I fully agree and honestly it's been a while since I thought about this.
In principle we would need something that is flexible enough to allow us to support all the different devices and use cases. It's not a straightforward thing, but removing rpm_idle() is probably going to make it a bit easier, I think.
Kind regards
Uffe