@@ -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 */
@@ -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))
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(+)