Re: [PATCH V3 2/6] PM: runtime: Add pm_runtime_if_active guard and conditional variant
From: Golla, Nagendra
Date: Mon Aug 31 2026 - 08:11:56 EST
Hi Rafael,
On 8/24/2026 9:48 PM, Rafael J. Wysocki (Intel) wrote:
On Mon, Aug 10, 2026 at 12:11 PM Golla Nagendra <nagendra.golla@xxxxxxx> 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.
Thanks for the review
Which is not nice and confusing IMV.
Is there anything like that anywhere else in the kernel?
I reviewed the existing guards in pm_runtime.h and compared them with the if_active guard design. No existing base guard uses a conditional acquire model like this one, because all existing guards follow the "make the device active and then access" pattern - when they make the device active they hold the reference, and the destructor releases that reference, so it stays balanced.
But this guard's use case is different: it follows the "access only if already active" pattern. In the IRQ handler we must not resume the device, so we only proceed if it is already active. This requirement is precisely why a conditional acquire is unavoidable here - the reference can only be taken when the device is already active. The guard takes the reference in that case and the destructor releases it at the end of its scope. However, it does not take a reference when the device is suspended or when PM is disabled - and even in those cases the destructor still runs and releases a reference, which causes the underflow problem.
Thanks,
Nagendra
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 <nagendra.golla@xxxxxxx>
---
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