lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250520032545.29558-4-zhangzihuan@kylinos.cn>
Date: Tue, 20 May 2025 11:25:45 +0800
From: Zihuan Zhang <zhangzihuan@...inos.cn>
To: rafael@...nel.org,
	len.brown@...el.com,
	pavel@...nel.org
Cc: linux-pm@...r.kernel.org,
	linux-kernel@...r.kernel.org,
	akpm@...ux-foundation.org,
	tzungbi@...nel.org,
	a.fatoum@...gutronix.de,
	jani.nikula@...el.com,
	joel.granados@...nel.org,
	paulmck@...nel.org,
	zhangguopeng@...inos.cn,
	linux@...ssschuh.net,
	Zihuan Zhang <zhangzihuan@...inos.cn>
Subject: [PATCH 3/3] PM / Sleep: Replace mutex_trylock(&system_transition_mutex) with try_lock_system_sleep()

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@...inos.cn>
---
 kernel/power/hibernate.c | 6 ++++--
 kernel/power/suspend.c   | 7 +++++--
 kernel/power/user.c      | 6 ++++--
 3 files changed, 13 insertions(+), 6 deletions(-)

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;
 }
-- 
2.25.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ