[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <20250520032545.29558-2-zhangzihuan@kylinos.cn>
Date: Tue, 20 May 2025 11:25:43 +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 1/3] PM / Sleep: Replace mutex_[un]lock(&system_transition_mutex) with [un]lock_system_sleep()
These function currently calls mutex_lock(&system_transition_mutex) and
mutex_unlock() directly to protect the resume-from-disk operation from
concurrent suspend/resume transitions.
However, this is inconsistent with the rest of the power management
code, where lock_system_sleep() and unlock_system_sleep() are used to
wrap suspend/hibernate transitions. These wrapper functions not only
acquire system_transition_mutex, but also set PF_NOFREEZE for the
calling thread, ensuring that it is not subject to freezing during
suspend.
This change replaces the raw mutex_lock()/unlock() with the standard
lock_system_sleep()/unlock_system_sleep() wrapper pair, bringing it in
line with the locking pattern used by hibernate(), pm_suspend(), and
other similar entry points.
Benefits of this change:
- Ensures the thread performing software resume is marked PF_NOFREEZE,
which is important during early resume paths where freezing is
active.
- Improves code clarity by making the locking intent more explicit.
- Unifies suspend/hibernate locking style across the kernel power
subsystem.
- Reduces the risk of future maintenance issues due to inconsistent
locking.
No functional change is expected at runtime, since the lock order and
coverage remain the same. This is a straightforward cleanup and
consistency fix.
Signed-off-by: Zihuan Zhang <zhangzihuan@...inos.cn>
---
kernel/power/hibernate.c | 5 +++--
kernel/reboot.c | 5 +++--
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index 23c0f4e6cb2f..cfaa92f24857 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -988,6 +988,7 @@ static int __init find_resume_device(void)
static int software_resume(void)
{
+ unsigned int sleep_flags;
int error;
pm_pr_dbg("Hibernation image partition %d:%d present\n",
@@ -995,7 +996,7 @@ static int software_resume(void)
pm_pr_dbg("Looking for hibernation image.\n");
- mutex_lock(&system_transition_mutex);
+ sleep_flags = lock_system_sleep();
error = swsusp_check(true);
if (error)
goto Unlock;
@@ -1050,7 +1051,7 @@ static int software_resume(void)
hibernate_release();
/* For success case, the suspend path will release the lock */
Unlock:
- mutex_unlock(&system_transition_mutex);
+ unlock_system_sleep(sleep_flags);
pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
return error;
Close_Finish:
diff --git a/kernel/reboot.c b/kernel/reboot.c
index ec087827c85c..68ac7e377efb 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -729,6 +729,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
void __user *, arg)
{
struct pid_namespace *pid_ns = task_active_pid_ns(current);
+ unsigned int sleep_flags;
char buffer[256];
int ret = 0;
@@ -761,7 +762,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
cmd = LINUX_REBOOT_CMD_HALT;
}
- mutex_lock(&system_transition_mutex);
+ sleep_flags = lock_system_sleep();
switch (cmd) {
case LINUX_REBOOT_CMD_RESTART:
kernel_restart(NULL);
@@ -811,7 +812,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
ret = -EINVAL;
break;
}
- mutex_unlock(&system_transition_mutex);
+ unlock_system_sleep(sleep_flags);
return ret;
}
--
2.25.1
Powered by blists - more mailing lists