diff mbox series

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

Message ID 20250520032545.29558-3-zhangzihuan@kylinos.cn
State New
Headers show
Series PM / Sleep: Introduce and use system sleep lock helpers | expand

Commit Message

Zihuan Zhang May 20, 2025, 3:25 a.m. UTC
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@kylinos.cn>
---
 include/linux/suspend.h |  2 ++
 kernel/power/main.c     | 12 ++++++++++++
 2 files changed, 14 insertions(+)
diff mbox series

Patch

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))