PM: runtime: Close some gaps related to autosuspend
Subsystems (4)
Rafael J. Wysocki
maintainercover PM: runtime: Close some gaps related to autosuspend
Hi All,
This is a follow-up series for
which is in linux-next.
First of all, I was mistaken when I wrote this in the changelog of the above:
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. There is one more issue related to that in __pm_runtime_idle() and both are addressed by patch [1/4].
Patch [2/4] simply removes spaces preceding labels as a matter of cleanup.
Patch [3/4] removes one more difference in the handling of devices that are resumed by rpm_resume() and those that are already active when they are passed to that function.
Patch [4/4] drops the direct pm_runtime_mark_last_busy() calls from rpm_resume() that are not needed any more after the previous changes.
For details, please see the patch changelogs.
Thanks!
Rafael J. Wysocki
maintainerPATCH 1/4 PM: runtime: Update last_busy in all rpm_idle() paths
From: "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.
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.
---
Commit 26236a3e8803 is present in linux-next.
--- drivers/base/power/runtime.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/base/power/runtime.c +15 -1
@@ -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;
Signature · 1 line
2.51.0
Re: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
Quoted 8 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.
Quoted 6 lines
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 allowsrpm_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 alast_busyupdate before therpm_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?
Quoted 8 lines
Another one is 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 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 newlast_busyvalue in that case. To address these cases, move the existingpm_runtime_mark_last_busy()call inrpm_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.
Quoted 8 lines
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 <[email protected]>---
Commit
26236a3e8803is present in linux-next.
Semi-unrelated cleanup: pm_runtime_put_sync() some stale kerneldoc now, saying "this does not update the last access time".
Quoted 33 lines
--- drivers/base/power/runtime.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)drivers/base/power/runtime.c +8
-1@@ -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;
drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev, int rpmflags) · line 1149
Quoted 10 lines
drivers/base/power/runtime.c +7
-0if (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
Quoted 8 lines
drivers/base/power/runtime.c +0
-0if (retval <0 ) {return retval;Signature · 1 line
2.51.0
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
Quoted 17 lines · Brian Norris
Quoted 8 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.Quoted 6 lines
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 allowsrpm_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 alast_busyupdate before therpm_drop_usage_count()call in__pm_runtime_idle().
Quoted 3 lines
Do
consumer->supplierandchild->parentrefcount drops have the same problem? e.g., inpm_runtime_release_supplier()andrpm_suspend()/parent->power.child_countdecrement?
I need to think about it.
I don't think it affects the $subject patch anyway though.
Quoted 11 lines
Quoted 8 lines
Another one is 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 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 newlast_busyvalue in that case. To address these cases, move the existingpm_runtime_mark_last_busy()call inrpm_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.
Quoted 11 lines
Quoted 8 lines
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 <[email protected]>---
Commit
26236a3e8803is 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.
Quoted 48 lines
Quoted 33 lines
--- drivers/base/power/runtime.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)drivers/base/power/runtime.c +8
-1@@ -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;drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev, int rpmflags) · line 1149
Quoted 10 lines
drivers/base/power/runtime.c +7
-0if (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.
Quoted 2 lines
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().
Quoted 11 lines
But this is supposed to influence the
rpm_suspend()behavior ofrpm_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_consumeormemory_order_acquireif you require this behavior.)"
Quoted 4 lines
So,
rpm_check_suspend_allowed()should probably useatomic_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().
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
Quoted 19 lines · Rafael J. Wysocki (Intel)
Quoted 17 lines · Brian Norris
Quoted 14 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.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 allowsrpm_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 alast_busyupdate before therpm_drop_usage_count()call in__pm_runtime_idle().
Quoted 7 lines
Quoted 3 lines
Do
consumer->supplierandchild->parentrefcount drops have the same problem? e.g., inpm_runtime_release_supplier()andrpm_suspend()/parent->power.child_countdecrement?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).
Quoted 97 lines
Quoted 11 lines
Quoted 8 lines
Another one is 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 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 newlast_busyvalue in that case. To address these cases, move the existingpm_runtime_mark_last_busy()call inrpm_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.Quoted 11 lines
Quoted 8 lines
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 <[email protected]>---
Commit
26236a3e8803is 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.
Quoted 48 lines
Quoted 33 lines
--- drivers/base/power/runtime.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)drivers/base/power/runtime.c +8
-1@@ -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;drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev, int rpmflags) · line 1149
Quoted 10 lines
drivers/base/power/runtime.c +7
-0if (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.Quoted 2 lines
rpm_drop_usage_count()uses an atomic decrement, which has a full barrier.Right, that's why the
last_busyupdate is carried out beforerpm_drop_usage_count().Quoted 11 lines
But this is supposed to influence the
rpm_suspend()behavior ofrpm_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_consumeormemory_order_acquireif you require this behavior.)"
Quoted 8 lines
Quoted 4 lines
So,
rpm_check_suspend_allowed()should probably useatomic_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 thelast_busycheck inpm_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.
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v1 1/4] PM: runtime: Update last_busy in all rpm_idle() paths
Quoted 32 lines · Rafael J. Wysocki (Intel)
Quoted 19 lines · Rafael J. Wysocki (Intel)
Quoted 17 lines · Brian Norris
Quoted 14 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.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 allowsrpm_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 alast_busyupdate before therpm_drop_usage_count()call in__pm_runtime_idle().Quoted 7 lines
Quoted 3 lines
Do
consumer->supplierandchild->parentrefcount drops have the same problem? e.g., inpm_runtime_release_supplier()andrpm_suspend()/parent->power.child_countdecrement?I need to think about it.
I don't think it affects the $subject patch anyway though.
So generally speaking, yes, they do.
Quoted 7 lines
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.
Quoted 130 lines
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).
Quoted 97 lines
Quoted 11 lines
Quoted 8 lines
Another one is 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 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 newlast_busyvalue in that case. To address these cases, move the existingpm_runtime_mark_last_busy()call inrpm_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.Quoted 11 lines
Quoted 8 lines
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 <[email protected]>---
Commit
26236a3e8803is 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.
Quoted 48 lines
Quoted 33 lines
--- drivers/base/power/runtime.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)drivers/base/power/runtime.c +8
-1@@ -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;drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev, int rpmflags) · line 1149
Quoted 10 lines
drivers/base/power/runtime.c +7
-0if (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.Quoted 2 lines
rpm_drop_usage_count()uses an atomic decrement, which has a full barrier.Right, that's why the
last_busyupdate is carried out beforerpm_drop_usage_count().Quoted 11 lines
But this is supposed to influence the
rpm_suspend()behavior ofrpm_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_consumeormemory_order_acquireif you require this behavior.)"Quoted 8 lines
Quoted 4 lines
So,
rpm_check_suspend_allowed()should probably useatomic_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 thelast_busycheck inpm_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_busyaccess 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.
Rafael J. Wysocki
maintainerPATCH 2/4 PM: runtime: Drop space characters preceding labels
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Drop space characters preceding labels from the core runtime PM code.
No functional impact.
--- drivers/base/power/runtime.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
drivers/base/power/runtime.c +16 -16
@@ -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);
Signature · 1 line
2.51.0
Re: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
Quoted 8 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
Drop space characters preceding labels from the core runtime PM code.
No functional impact.
Signed-off-by Rafael J. Wysocki <[email protected]>---
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
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
Quoted 8 lines · Alan Stern
Quoted 6 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
Drop space characters preceding labels from the core runtime PM code.
No functional impact.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Rafael J. Wysocki <[email protected]>---
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.
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
Quoted 19 lines · Rafael J. Wysocki (Intel)
Quoted 8 lines · Alan Stern
Quoted 6 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
Drop space characters preceding labels from the core runtime PM code.
No functional impact.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Rafael J. Wysocki <[email protected]>---
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.
Quoted 6 lines
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.
Re: [PATCH v1 2/4] PM: runtime: Drop space characters preceding labels
Quoted 24 lines · Rafael J. Wysocki (Intel)
Quoted 22 lines · Rafael J. Wysocki (Intel)
Quoted 8 lines · Alan Stern
Quoted 6 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
Drop space characters preceding labels from the core runtime PM code.
No functional impact.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Rafael J. Wysocki <[email protected]>---
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.
Quoted 6 lines
Quoted 3 lines
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
Rafael J. Wysocki
maintainerPATCH 3/4 PM: runtime: Let devices suspend after an attempted resume
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
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.
--- drivers/base/power/runtime.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/base/power/runtime.c +8 -4
@@ -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) {
Signature · 1 line
2.51.0
Re: [PATCH v1 3/4] PM: runtime: Let devices suspend after an attempted resume
Quoted 43 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
On a successful runtime resume of a device,
rpm_idle()is called byrpm_resume(), but only if the runtime PM status of the device was notRPM_ACTIVEwhenrpm_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 byrpm_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 callrpm_idle()also if the device has been active to start with.Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/base/power/runtime.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)drivers/base/power/runtime.c +3
-2@@ -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 ||
drivers/base/power/runtime.c static int rpm_resume(struct device *dev, int rpmflags) · line 979
Quoted 10 lines
drivers/base/power/runtime.c +5
-2@@ -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
Quoted 9 lines
drivers/base/power/runtime.c +0
-0out:if (parent && !dev->power.irq_safe) {Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 4/4 PM: runtime: Drop direct last_busy updates from rpm_resume()
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
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.
--- drivers/base/power/runtime.c | 7 ------- 1 file changed, 7 deletions(-)
drivers/base/power/runtime.c +0 -7
@@ -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); }
Signature · 1 line
2.51.0