[PATCH 2/3] PM / Sleep: Introduce try_lock_system_sleep()

From: Zihuan Zhang
Date: Mon May 19 2025 - 23:26:52 EST


The suspend subsystem uses system_transition_mutex to serialize
suspend and hibernate transitions. The existing lock_system_sleep()
wrapper both acquires the mutex and sets PF_NOFREEZE on the current
task to avoid it being frozen during the suspend process.

However, in some places such as enter_state(), mutex_trylock() is
used instead. This path currently lacks PF_NOFREEZE protection.

This patch introduces a new wrapper:

try_lock_system_sleep()

It sets PF_NOFREEZE and then performs mutex_trylock(), mirroring the
existing lock_system_sleep() implementation. This improves consistency
and enables future cleanup of raw trylock calls.

Signed-off-by: Zihuan Zhang <zhangzihuan@xxxxxxxxxx>
---
include/linux/suspend.h | 2 ++
kernel/power/main.c | 12 ++++++++++++
2 files changed, 14 insertions(+)

diff --git a/include/linux/suspend.h b/include/linux/suspend.h
index da6ebca3ff77..6c9e8fe0c446 100644
--- a/include/linux/suspend.h
+++ b/include/linux/suspend.h
@@ -468,6 +468,7 @@ extern void pm_wakep_autosleep_enabled(bool set);
extern void pm_print_active_wakeup_sources(void);

extern unsigned int lock_system_sleep(void);
+extern unsigned int try_lock_system_sleep(void);
extern void unlock_system_sleep(unsigned int);

#else /* !CONFIG_PM_SLEEP */
@@ -496,6 +497,7 @@ static inline void pm_wakeup_clear(bool reset) {}
static inline void pm_system_irq_wakeup(unsigned int irq_number) {}

static inline unsigned int lock_system_sleep(void) { return 0; }
+static inline unsigned int try_lock_system_sleep(void) { return 0; }
static inline void unlock_system_sleep(unsigned int flags) {}

#endif /* !CONFIG_PM_SLEEP */
diff --git a/kernel/power/main.c b/kernel/power/main.c
index 6254814d4817..e5b64f0dda2d 100644
--- a/kernel/power/main.c
+++ b/kernel/power/main.c
@@ -58,6 +58,18 @@ unsigned int lock_system_sleep(void)
}
EXPORT_SYMBOL_GPL(lock_system_sleep);

+unsigned int try_lock_system_sleep(void)
+{
+ unsigned int flags = current->flags;
+ current->flags |= PF_NOFREEZE;
+
+ if (!mutex_trylock(&system_transition_mutex))
+ return 0;
+
+ return flags;
+}
+EXPORT_SYMBOL_GPL(try_lock_system_sleep);
+
void unlock_system_sleep(unsigned int flags)
{
if (!(flags & PF_NOFREEZE))
--
2.25.1