Re: [PATCH v1] PM: runtime: Avoid device usage count underflows

From: Rafael J. Wysocki
Date: Mon Apr 11 2022 - 11:18:03 EST


On Mon, Apr 11, 2022 at 5:09 PM Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
>
> 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;
> }
>

I know.

And why would it be better?