Re: [PATCH V3 2/6] PM: runtime: Add pm_runtime_if_active guard and conditional variant
"Rafael J. Wysocki (Intel)" <[email protected]>
| Newsgroups | org.kernel.vger.linux-pm,org.infradead.lists.linux-arm-kernel,org.kernel.vger.dmaengine,org.kernel.vger.linux-devicetree,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <CAJZ5v0gKwxBp=Ajm5_iJEP0GTY5J7SJnHJGCQjfKYgmOHqE0xg@mail.gmail.com> |
On Mon, Aug 10, 2026 at 12:11 PM Golla Nagendra <[email protected]> wrote: > > Add a pm_runtime_if_active base guard and its _try conditional variant > to pm_runtime.h for drivers that need to conditionally acquire a runtime > PM reference only when the device is already active. > > The base guard must not be used directly via guard()/scoped_guard() > because pm_runtime_get_if_active() only acquires a reference when it > returns 1; the destructor unconditionally calls pm_runtime_put(), which > would underflow usage_count on a suspended or RPM-disabled device. The > base guard exists solely to back the DEFINE_GUARD_COND _try variant. Which is not nice and confusing IMV. Is there anything like that anywhere else in the kernel? > The _try variant (used via PM_RUNTIME_ACQUIRE_IF_ACTIVE) checks the > return value and only runs the destructor when the reference was > actually acquired. This is useful in interrupt handlers where the > device may be runtime-suspended and MMIO accesses must be avoided. > > Signed-off-by: Golla Nagendra <[email protected]> > --- > Changes in V3: > - New patch: add PM_RUNTIME_ACQUIRE_IF_ACTIVE() guard and backing > infrastructure to pm_runtime.h so that drivers > can use a structured guard instead of an open-coded > pm_runtime_get_if_active()/pm_runtime_put() pair > --- > include/linux/pm_runtime.h | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/include/linux/pm_runtime.h b/include/linux/pm_runtime.h > index 64921b10ac74..bc6de97e6111 100644 > --- a/include/linux/pm_runtime.h > +++ b/include/linux/pm_runtime.h > @@ -609,6 +609,13 @@ DEFINE_GUARD(pm_runtime_active, struct device *, > pm_runtime_get_sync(_T), pm_runtime_put(_T)); > DEFINE_GUARD(pm_runtime_active_auto, struct device *, > pm_runtime_get_sync(_T), pm_runtime_put_autosuspend(_T)); > +/* > + * Do not use directly -- the destructor calls pm_runtime_put() > + * unconditionally, which underflows if no reference was acquired. > + * Use only via the _try variant below. > + */ > +DEFINE_GUARD(pm_runtime_if_active, struct device *, > + pm_runtime_get_if_active(_T), pm_runtime_put(_T)); > /* > * Use the following guards with ACQUIRE()/ACQUIRE_ERR(). > * > @@ -624,6 +631,8 @@ DEFINE_GUARD_COND(pm_runtime_active_auto, _try, > pm_runtime_get_active(_T, RPM_TRANSPARENT), _RET == 0) > DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled, > pm_runtime_resume_and_get(_T), _RET == 0) > +DEFINE_GUARD_COND(pm_runtime_if_active, _try, > + pm_runtime_get_if_active(_T) ?: -EAGAIN, _RET == 1) > > /* ACQUIRE() wrapper macros for the guards defined above. */ > > @@ -639,6 +648,9 @@ DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled, > #define PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(_dev, _var) \ > ACQUIRE(pm_runtime_active_auto_try_enabled, _var)(_dev) > > +#define PM_RUNTIME_ACQUIRE_IF_ACTIVE(_dev, _var) \ > + ACQUIRE(pm_runtime_if_active_try, _var)(_dev) > + > /* > * ACQUIRE_ERR() wrapper macro for guard pm_runtime_active. > * > -- > 2.43.7 >