PM: runtime: Avoid device usage count underflows
Subsystems (4)
PATCH 1/1 PM: runtime: Avoid device usage count underflows
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
For this reason, (1) make rpm_check_suspend_allowed() return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introduce rpm_drop_usage_count() that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead of atomic_dec_and_test().
--- drivers/base/power/runtime.c | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-)
drivers/base/power/runtime.c +37 -7
@@ -263,7 +263,7 @@ static int rpm_check_suspend_allowed(str retval = -EINVAL ; else if (dev->power.disable_depth > 0) retval = -EACCES ; - else if (atomic_read(&dev->power.usage_count) > 0) + else if (atomic_read(&dev->power.usage_count)) retval = -EAGAIN ; else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count))
@@ -1039,13 +1039,33 @@ int pm_schedule_suspend(struct device *d }EXPORT_SYMBOL_GPL (pm_schedule_suspend); +static int rpm_drop_usage_count (struct device *dev) +{ +int ret; + + ret =atomic_sub_return (1 , &dev->power.usage_count); +if (ret >=0 ) +return ret; + +/* +* Because rpm_resume() does not check the usage counter, it will resume +* the device even if the usage counter is 0 or negative, so it is +* sufficient to increment the usage counter here to reverse the change +* made above. +*/ +atomic_inc (&dev->power.usage_count); +dev_warn (dev,"Runtime PM usage count underflow!\n" ); +return -EINVAL ; +} +/** * __pm_runtime_idle - Entry point for runtime idle operations. * @dev: Device to send idle notification for. * @rpmflags: Flag bits. * * If the RPM_GET_PUT flag is set, decrement the device's usage count and -* return immediately if it is larger than zero. Then carry out an idle +* return immediately if it is larger than zero (if it becomes negative, log a +* warning, increment it, and return an error). Then carry out an idle * notification, either synchronous or asynchronous. * * This routine may be called in atomic context if the RPM_ASYNC flag is set,
@@ -1057,9 +1077,12 @@ int __pm_runtime_idle(struct device *devint retval;if (rpmflags &RPM_GET_PUT ) { -if (!atomic_dec_and_test (&dev->power.usage_count)) { + retval =rpm_drop_usage_count (dev); +if (retval >0 ) {trace_rpm_usage_rcuidle (dev, rpmflags);return 0 ; + }else if (retval <0 ) { +return retval; } }
@@ -1079,7 +1102,8 @@ EXPORT_SYMBOL_GPL(__pm_runtime_idle);* @rpmflags: Flag bits. * * If the RPM_GET_PUT flag is set, decrement the device's usage count and -* return immediately if it is larger than zero. Then carry out a suspend, +* return immediately if it is larger than zero (if it becomes negative, log a +* warning, increment it, and return an error). Then carry out a suspend, * either synchronous or asynchronous. * * This routine may be called in atomic context if the RPM_ASYNC flag is set,
@@ -1091,9 +1115,12 @@ int __pm_runtime_suspend(struct device *int retval;if (rpmflags &RPM_GET_PUT ) { -if (!atomic_dec_and_test (&dev->power.usage_count)) { + retval =rpm_drop_usage_count (dev); +if (retval >0 ) {trace_rpm_usage_rcuidle (dev, rpmflags);return 0 ; + }else if (retval <0 ) { +return retval; } }
@@ -1527,14 +1554,17 @@ EXPORT_SYMBOL_GPL(pm_runtime_forbid);*/ void pm_runtime_allow (struct device *dev) { +int ret; +spin_lock_irq (&dev->power.lock);if (dev->power.runtime_auto) goto out; dev->power.runtime_auto = true; -if (atomic_dec_and_test (&dev->power.usage_count)) + ret =rpm_drop_usage_count (dev); +if (ret ==0 )rpm_idle (dev,RPM_AUTO |RPM_ASYNC ); -else +else if (ret >0 )trace_rpm_usage_rcuidle (dev,RPM_AUTO |RPM_ASYNC ); out:
Quoted 6 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
I get the point. Although, perhaps we should also state that it's a programming problem that we would like to catch and warn about?
Quoted 35 lines
For this reason, (1) make
rpm_check_suspend_allowed()return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introducerpm_drop_usage_count()that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead ofatomic_dec_and_test().Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/base/power/runtime.c | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-)Index:
linux-pm/drivers/base/power/runtime.c
===================================================================drivers/base/power/runtime.c +6
-1@@ -263,7 +263,7 @@ static int rpm_check_suspend_allowed(str retval = -EINVAL ; else if (dev->power.disable_depth > 0) retval = -EACCES ; - else if (atomic_read(&dev->power.usage_count) > 0) + else if (atomic_read(&dev->power.usage_count)) retval = -EAGAIN ; else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count))@@ -1039,13 +1039,33 @@ int pm_schedule_suspend(struct device *d }EXPORT_SYMBOL_GPL (pm_schedule_suspend); +static int rpm_drop_usage_count (struct device *dev) +{ +int ret; + + ret =atomic_sub_return (1 , &dev->power.usage_count);
drivers/base/power/runtime.c int pm_schedule_suspend(struct device *d · line 1056
Quoted 10 lines
drivers/base/power/runtime.c +10
-0+if (ret >=0 ) +return ret; + +/* +* Because rpm_resume() does not check the usage counter, it will resume +* the device even if the usage counter is 0 or negative, so it is +* sufficient to increment the usage counter here to reverse the change +* made above. +*/ +atomic_inc (&dev->power.usage_count);
Rather than this two-step process, couldn't we just do an "atomic_add_unless(&dev->power.usage_count, -1, 0)" - and check the return value?
drivers/base/power/runtime.c int pm_schedule_suspend(struct device *d · line 1060
Quoted 4 lines
drivers/base/power/runtime.c +4
-0+dev_warn (dev,"Runtime PM usage count underflow!\n" ); +return -EINVAL ; +} +
[...]
Kind regards
Uffe
Rafael J. Wysocki
maintainerQuoted 11 lines · Ulf Hansson
Quoted 6 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
I get the point. Although, perhaps we should also state that it's a programming problem that we would like to catch and warn about?
OK, I can add that to the changelog.
Quoted 41 lines
Quoted 41 lines
For this reason, (1) make
rpm_check_suspend_allowed()return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introducerpm_drop_usage_count()that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead ofatomic_dec_and_test().Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/base/power/runtime.c | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-)Index:
linux-pm/drivers/base/power/runtime.c
===================================================================drivers/base/power/runtime.c +12
-1@@ -263,7 +263,7 @@ static int rpm_check_suspend_allowed(str retval = -EINVAL ; else if (dev->power.disable_depth > 0) retval = -EACCES ; - else if (atomic_read(&dev->power.usage_count) > 0) + else if (atomic_read(&dev->power.usage_count)) retval = -EAGAIN ; else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count))@@ -1039,13 +1039,33 @@ int pm_schedule_suspend(struct device *d }EXPORT_SYMBOL_GPL (pm_schedule_suspend); +static int rpm_drop_usage_count (struct device *dev) +{ +int ret; + + ret =atomic_sub_return (1 , &dev->power.usage_count); +if (ret >=0 ) +return ret; + +/* +* Because rpm_resume() does not check the usage counter, it will resume +* the device even if the usage counter is 0 or negative, so it is
Quoted 8 lines
drivers/base/power/runtime.c int pm_schedule_suspend(struct device *d · line 1056
Quoted 4 lines
drivers/base/power/runtime.c +4
-0+* sufficient to increment the usage counter here to reverse the change +* made above. +*/ +atomic_inc (&dev->power.usage_count);Rather than this two-step process, couldn't we just do an "
atomic_add_unless(&dev->power.usage_count, -1, 0)" - and check the return value?
No, we couldn't, because atomic_add_unless() returns a bool and we need to know the new counter value (and in particular whether or not it is 0).
I thought that it would be better to do the extra access in the failing case only.
Quoted 6 lines
Quoted 4 lines
drivers/base/power/runtime.c +4
-0+dev_warn (dev,"Runtime PM usage count underflow!\n" ); +return -EINVAL ; +} +[...]
Quoted 66 lines · Rafael J. Wysocki
Quoted 11 lines · Ulf Hansson
Quoted 6 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
I get the point. Although, perhaps we should also state that it's a programming problem that we would like to catch and warn about?
OK, I can add that to the changelog.
Quoted 41 lines
Quoted 41 lines
For this reason, (1) make
rpm_check_suspend_allowed()return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introducerpm_drop_usage_count()that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead ofatomic_dec_and_test().Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/base/power/runtime.c | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-)Index:
linux-pm/drivers/base/power/runtime.c
===================================================================drivers/base/power/runtime.c +12
-1@@ -263,7 +263,7 @@ static int rpm_check_suspend_allowed(str retval = -EINVAL ; else if (dev->power.disable_depth > 0) retval = -EACCES ; - else if (atomic_read(&dev->power.usage_count) > 0) + else if (atomic_read(&dev->power.usage_count)) retval = -EAGAIN ; else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count))@@ -1039,13 +1039,33 @@ int pm_schedule_suspend(struct device *d }EXPORT_SYMBOL_GPL (pm_schedule_suspend); +static int rpm_drop_usage_count (struct device *dev) +{ +int ret; + + ret =atomic_sub_return (1 , &dev->power.usage_count); +if (ret >=0 ) +return ret; + +/* +* Because rpm_resume() does not check the usage counter, it will resume +* the device even if the usage counter is 0 or negative, so it is Quoted 8 lines
drivers/base/power/runtime.c int pm_schedule_suspend(struct device *d · line 1056
Quoted 4 lines
drivers/base/power/runtime.c +4
-0+* sufficient to increment the usage counter here to reverse the change +* made above. +*/ +atomic_inc (&dev->power.usage_count);Rather than this two-step process, couldn't we just do an "
atomic_add_unless(&dev->power.usage_count, -1, 0)" - and check the return value?
Quoted 3 lines
No, we couldn't, because
atomic_add_unless()returns a bool and we need to know the new counter value (and in particular whether or not it is 0).
atomic_add_unless(&dev->power.usage_count, -1, 0) would return true as long as the counter value is greater than 0.
If the counter has become 0, atomic_add_unless() would return false and not continue to decrease the value below zero. Isn't this exactly what we want?
Quoted 10 lines
I thought that it would be better to do the extra access in the failing case only.
Quoted 6 lines
Quoted 4 lines
drivers/base/power/runtime.c +4
-0+dev_warn (dev,"Runtime PM usage count underflow!\n" ); +return -EINVAL ; +} +[...]
Kind regards
Uffe
Rafael J. Wysocki
maintainerQuoted 68 lines · Ulf Hansson
Quoted 66 lines · Rafael J. Wysocki
Quoted 11 lines · Ulf Hansson
Quoted 6 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
I get the point. Although, perhaps we should also state that it's a programming problem that we would like to catch and warn about?
OK, I can add that to the changelog.
Quoted 49 lines
Quoted 35 lines
For this reason, (1) make
rpm_check_suspend_allowed()return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introducerpm_drop_usage_count()that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead ofatomic_dec_and_test().Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/base/power/runtime.c | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-)Index:
linux-pm/drivers/base/power/runtime.c
===================================================================drivers/base/power/runtime.c +6
-1@@ -263,7 +263,7 @@ static int rpm_check_suspend_allowed(str retval = -EINVAL ; else if (dev->power.disable_depth > 0) retval = -EACCES ; - else if (atomic_read(&dev->power.usage_count) > 0) + else if (atomic_read(&dev->power.usage_count)) retval = -EAGAIN ; else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count))@@ -1039,13 +1039,33 @@ int pm_schedule_suspend(struct device *d }EXPORT_SYMBOL_GPL (pm_schedule_suspend); +static int rpm_drop_usage_count (struct device *dev) +{ +int ret; + + ret =atomic_sub_return (1 , &dev->power.usage_count);drivers/base/power/runtime.c int pm_schedule_suspend(struct device *d · line 1056
Quoted 10 lines
drivers/base/power/runtime.c +10
-0+if (ret >=0 ) +return ret; + +/* +* Because rpm_resume() does not check the usage counter, it will resume +* the device even if the usage counter is 0 or negative, so it is +* sufficient to increment the usage counter here to reverse the change +* made above. +*/ +atomic_inc (&dev->power.usage_count);Rather than this two-step process, couldn't we just do an "
atomic_add_unless(&dev->power.usage_count, -1, 0)" - and check the return value?
Quoted 6 lines
Quoted 3 lines
No, we couldn't, because
atomic_add_unless()returns a bool and we need to know the new counter value (and in particular whether or not it is 0).
atomic_add_unless(&dev->power.usage_count, -1, 0)would return true as long as the counter value is greater than 0.
Yes, and it in particular, when the current value of the counter is 1 before the operation IIUC.
So after the operation it is 0 and true will be returned, won't it? But that's exactly the case we want to catch.
Quoted 3 lines
If the counter has become 0,
atomic_add_unless()would return false and not continue to decrease the value below zero. Isn't this exactly what we want?
Not really.
We want to detect transitions from 0 to 1 which is the case when the device can be suspended.
Ulf Hansson
Reviewed-byQuoted 83 lines · Rafael J. Wysocki
Quoted 68 lines · Ulf Hansson
Quoted 66 lines · Rafael J. Wysocki
Quoted 11 lines · Ulf Hansson
Quoted 6 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
I get the point. Although, perhaps we should also state that it's a programming problem that we would like to catch and warn about?
OK, I can add that to the changelog.
Quoted 49 lines
Quoted 35 lines
For this reason, (1) make
rpm_check_suspend_allowed()return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introducerpm_drop_usage_count()that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead ofatomic_dec_and_test().Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/base/power/runtime.c | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-)Index:
linux-pm/drivers/base/power/runtime.c
===================================================================drivers/base/power/runtime.c +6
-1@@ -263,7 +263,7 @@ static int rpm_check_suspend_allowed(str retval = -EINVAL ; else if (dev->power.disable_depth > 0) retval = -EACCES ; - else if (atomic_read(&dev->power.usage_count) > 0) + else if (atomic_read(&dev->power.usage_count)) retval = -EAGAIN ; else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count))@@ -1039,13 +1039,33 @@ int pm_schedule_suspend(struct device *d }EXPORT_SYMBOL_GPL (pm_schedule_suspend); +static int rpm_drop_usage_count (struct device *dev) +{ +int ret; + + ret =atomic_sub_return (1 , &dev->power.usage_count);drivers/base/power/runtime.c int pm_schedule_suspend(struct device *d · line 1056
Quoted 10 lines
drivers/base/power/runtime.c +10
-0+if (ret >=0 ) +return ret; + +/* +* Because rpm_resume() does not check the usage counter, it will resume +* the device even if the usage counter is 0 or negative, so it is +* sufficient to increment the usage counter here to reverse the change +* made above. +*/ +atomic_inc (&dev->power.usage_count);Rather than this two-step process, couldn't we just do an "
atomic_add_unless(&dev->power.usage_count, -1, 0)" - and check the return value?Quoted 6 lines
Quoted 3 lines
No, we couldn't, because
atomic_add_unless()returns a bool and we need to know the new counter value (and in particular whether or not it is 0).
atomic_add_unless(&dev->power.usage_count, -1, 0)would return true as long as the counter value is greater than 0.Yes, and it in particular, when the current value of the counter is 1 before the operation IIUC.
So after the operation it is 0 and true will be returned, won't it? But that's exactly the case we want to catch.
Quoted 8 lines
Quoted 3 lines
If the counter has become 0,
atomic_add_unless()would return false and not continue to decrease the value below zero. Isn't this exactly what we want?Not really.
We want to detect transitions from 0 to 1 which is the case when the device can be suspended.
I assume you mean from 1 to 0. In any case, I see what you mean by now, sorry for the noise.
Then feel free to add:
Kind regards
Uffe
Quoted 13 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
For this reason, (1) make
rpm_check_suspend_allowed()return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introducerpm_drop_usage_count()that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead ofatomic_dec_and_test().
...
drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev · line 1086
Quoted 7 lines
drivers/base/power/runtime.c +4
-0+ retval =rpm_drop_usage_count (dev); +if (retval >0 ) {trace_rpm_usage_rcuidle (dev, rpmflags);return 0 ; + }else if (retval <0 ) { +return retval; }
Can be written in a form
if (retval < 0)
return retval;
if (retval > 0) {
trace_rpm_usage_rcuidle(dev, rpmflags);
return 0;
}...
drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev · line 1086
Quoted 6 lines
drivers/base/power/runtime.c +3
-0+if (retval >0 ) {trace_rpm_usage_rcuidle (dev, rpmflags);return 0 ; + }else if (retval <0 ) { +return retval; }
Ditto.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Rafael J. Wysocki
maintainerQuoted 26 lines · Andy Shevchenko
Quoted 13 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
A PM-runtime device usage count underflow is potentially critical, because it may cause a device to be suspended when it is expected to be operational.
For this reason, (1) make
rpm_check_suspend_allowed()return an error when the device usage count is negative to prevent devices from being suspended in that case, (2) introducerpm_drop_usage_count()that will detect device usage count underflows, warn about them and fix them up, and (3) use it to drop the usage count in a few places instead ofatomic_dec_and_test()....
drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev · line 1086
Quoted 7 lines
drivers/base/power/runtime.c +4
-0+ retval =rpm_drop_usage_count (dev); +if (retval >0 ) {trace_rpm_usage_rcuidle (dev, rpmflags);return 0 ; + }else if (retval <0 ) { +return retval; }
Quoted 9 lines
Can be written in a form
if (retval < 0) return retval; if (retval > 0) { trace_rpm_usage_rcuidle(dev, rpmflags); return 0; }
I know.
And why would it be better?
Quoted 3 lines · Rafael J. Wysocki
Quoted 1 line · Andy Shevchenko
On Wed, Apr 6, 2022 at 11:49 PM Rafael J. Wysocki <[email protected]> wrote:
...
Quoted 18 lines
Quoted 8 lines
drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev · line 1086
Quoted 7 lines
drivers/base/power/runtime.c +4
-0+ retval =rpm_drop_usage_count (dev); +if (retval >0 ) {trace_rpm_usage_rcuidle (dev, rpmflags);return 0 ; + }else if (retval <0 ) { +return retval; }Quoted 9 lines
Can be written in a form
if (retval < 0) return retval; if (retval > 0) { trace_rpm_usage_rcuidle(dev, rpmflags); return 0; }
Quoted 3 lines
I know.
And why would it be better?
Depends on the perception:
a) less characters to parse (no 'else');
b) checking for errors first, which seems more or less standard pattern.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Rafael J. Wysocki
maintainerQuoted 26 lines · Andy Shevchenko
Quoted 3 lines · Rafael J. Wysocki
Quoted 1 line · Andy Shevchenko
On Wed, Apr 6, 2022 at 11:49 PM Rafael J. Wysocki <[email protected]> wrote:
...
Quoted 18 lines
Quoted 17 lines
drivers/base/power/runtime.c int __pm_runtime_idle(struct device *dev · line 1086
Quoted 7 lines
drivers/base/power/runtime.c +4
-0+ retval =rpm_drop_usage_count (dev); +if (retval >0 ) {trace_rpm_usage_rcuidle (dev, rpmflags);return 0 ; + }else if (retval <0 ) { +return retval; }Can be written in a form
if (retval < 0) return retval; if (retval > 0) { trace_rpm_usage_rcuidle(dev, rpmflags); return 0; }
Quoted 5 lines
Quoted 3 lines
I know.
And why would it be better?
Depends on the perception:
Well, exactly.
Quoted 1 line
a) less characters to parse (no 'else');
But to me, with the "else" it is clear that the conditionals are related to each other which is not so clear otherwise at first sight. YMMV
Quoted 1 line
b) checking for errors first, which seems more or less standard pattern.
So the checks can be reversed no problem, but this is such a minor point ,,,