Re: [PATCH v1] PM: runtime: Avoid device usage count underflows
From: Andy Shevchenko
Date: Mon Apr 11 2022 - 11:09:57 EST
On Wed, Apr 6, 2022 at 11:49 PM Rafael J. Wysocki <rjw@xxxxxxxxxxxxx> wrote:
>
> From: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>
>
> 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().
...
> + 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;
}
...
> + if (retval > 0) {
> trace_rpm_usage_rcuidle(dev, rpmflags);
> return 0;
> + } else if (retval < 0) {
> + return retval;
> }
Ditto.
--
With Best Regards,
Andy Shevchenko