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