diff mbox series

[v2,3/3] PM / Sleep: Replace mutex_trylock(&system_transition_mutex) with try_lock_system_sleep()

Message ID 20250522021649.55228-4-zhangzihuan@kylinos.cn
State New
Headers show
Series [v2,1/3] PM / Sleep: Replace mutex_[un]lock(&system_transition_mutex) with [un]lock_system_sleep() | expand

Commit Message

Zihuan Zhang May 22, 2025, 2:16 a.m. UTC
This patch replaces the remaining instances of mutex_trylock(&system_transition_mutex)
that semantically intend to *try* acquiring the lock with the newly introduced
try_lock_system_sleep(), which provides a clearer abstraction and avoids direct
mutex operations in higher-level PM logic.

This improves code readability, keeps synchronization logic consistent across
all system sleep paths, and helps prepare for future enhancements or lock
substitutions (e.g., lockdep annotations or switching to a different lock
primitive).

Signed-off-by: Zihuan Zhang <zhangzihuan@kylinos.cn>
---
 kernel/power/hibernate.c | 6 ++++--
 kernel/power/suspend.c   | 7 +++++--
 kernel/power/user.c      | 6 ++++--
 3 files changed, 13 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index cfaa92f24857..c06af4008183 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -1448,9 +1448,11 @@  static const char * const comp_alg_enabled[] = {
 static int hibernate_compressor_param_set(const char *compressor,
 		const struct kernel_param *kp)
 {
+	unsigned int sleep_flags;
 	int index, ret;
 
-	if (!mutex_trylock(&system_transition_mutex))
+	sleep_flags = try_lock_system_sleep();
+	if (!sleep_flags)
 		return -EBUSY;
 
 	index = sysfs_match_string(comp_alg_enabled, compressor);
@@ -1463,7 +1465,7 @@  static int hibernate_compressor_param_set(const char *compressor,
 		ret = index;
 	}
 
-	mutex_unlock(&system_transition_mutex);
+	unlock_system_sleep(sleep_flags);
 
 	if (ret)
 		pr_debug("Cannot set specified compressor %s\n",
diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
index 8eaec4ab121d..7d39f1ae9711 100644
--- a/kernel/power/suspend.c
+++ b/kernel/power/suspend.c
@@ -564,6 +564,7 @@  static void suspend_finish(void)
  */
 static int enter_state(suspend_state_t state)
 {
+	unsigned int sleep_flags;
 	int error;
 
 	trace_suspend_resume(TPS("suspend_enter"), state, true);
@@ -577,7 +578,9 @@  static int enter_state(suspend_state_t state)
 	} else if (!valid_state(state)) {
 		return -EINVAL;
 	}
-	if (!mutex_trylock(&system_transition_mutex))
+
+	sleep_flags = try_lock_system_sleep();
+	if (!sleep_flags)
 		return -EBUSY;
 
 	if (state == PM_SUSPEND_TO_IDLE)
@@ -609,7 +612,7 @@  static int enter_state(suspend_state_t state)
 	pm_pr_dbg("Finishing wakeup.\n");
 	suspend_finish();
  Unlock:
-	mutex_unlock(&system_transition_mutex);
+	unlock_system_sleep(sleep_flags);
 	return error;
 }
 
diff --git a/kernel/power/user.c b/kernel/power/user.c
index 3f9e3efb9f6e..a41fb48b3f96 100644
--- a/kernel/power/user.c
+++ b/kernel/power/user.c
@@ -249,6 +249,7 @@  static int snapshot_set_swap_area(struct snapshot_data *data,
 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 							unsigned long arg)
 {
+	unsigned int sleep_flags;
 	int error = 0;
 	struct snapshot_data *data;
 	loff_t size;
@@ -266,7 +267,8 @@  static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (!mutex_trylock(&system_transition_mutex))
+	sleep_flags = try_lock_system_sleep();
+	if (!sleep_flags)
 		return -EBUSY;
 
 	lock_device_hotplug();
@@ -417,7 +419,7 @@  static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 	}
 
 	unlock_device_hotplug();
-	mutex_unlock(&system_transition_mutex);
+	unlock_system_sleep(sleep_flags);
 
 	return error;
 }