From patchwork Thu May 22 02:16:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zihuan Zhang X-Patchwork-Id: 891862 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 368F629CE6; Thu, 22 May 2025 02:35:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747881332; cv=none; b=qcij7MzBPamj+mwxRV/yuqDK5owF+ab7RutwFM/vcHzcmiwZqrj6a8eVW6k2741MdceiEEmue8QEi4EYWqxfFGT+/COOxap3uiwMU9ECsE8v5q801no9M2BScSdnjSde0K8PVX/5IsswiYuARKMLx789vhnnhKhxxEKVKVIvMOQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747881332; c=relaxed/simple; bh=O5WlLJx2+c4wwI4hD4aEpExVS165ZV3Rb0A7jWj6nqc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h1w41cFwvOfx7lhvVDal44mHEvpEsYf1nqZI7mN2XgzmTUcq81KSGub68Ui6SqJWL5AacC7oNLMxETSxS8OYXwArcgOubrbDSiT4eLcAHWkDgPDzqkYYgew/waSn4cmgzV3GokiV2oYWVYhDRNTPDv4lW0yWgw9HIpVil2vPu0g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 687d074e36b511f0b29709d653e92f7d-20250522 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.45, REQID:7d0dadb9-b786-4c96-a233-e70fc7d489b2, IP:0, U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:6493067, CLOUDID:162eb6d8f44e73553c797785cbcf1e9c, BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102,TC:nil,Content:0|50,EDM:-3,IP: nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,L ES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 687d074e36b511f0b29709d653e92f7d-20250522 Received: from mail.kylinos.cn [(10.44.16.175)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 261166824; Thu, 22 May 2025 10:35:21 +0800 Received: from mail.kylinos.cn (localhost [127.0.0.1]) by mail.kylinos.cn (NSMail) with SMTP id 2A346E00351B; Thu, 22 May 2025 10:17:05 +0800 (CST) X-ns-mid: postfix-682E8920-922448443 Received: from localhost.localdomain (unknown [172.25.120.24]) by mail.kylinos.cn (NSMail) with ESMTPA id D27B7E003519; Thu, 22 May 2025 10:17:01 +0800 (CST) From: Zihuan Zhang To: rafael@kernel.org, len.brown@intel.com, pavel@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, tzungbi@kernel.org, a.fatoum@pengutronix.de, jani.nikula@intel.com, joel.granados@kernel.org, paulmck@kernel.org, zhangguopeng@kylinos.cn, linux@weissschuh.net, Zihuan Zhang Subject: [PATCH v2 1/3] PM / Sleep: Replace mutex_[un]lock(&system_transition_mutex) with [un]lock_system_sleep() Date: Thu, 22 May 2025 10:16:47 +0800 Message-Id: <20250522021649.55228-2-zhangzihuan@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250522021649.55228-1-zhangzihuan@kylinos.cn> References: <20250522021649.55228-1-zhangzihuan@kylinos.cn> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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; } From patchwork Thu May 22 02:16:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zihuan Zhang X-Patchwork-Id: 891863 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 643572CCA5; Thu, 22 May 2025 02:35:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747881331; cv=none; b=ATq3xno2NM8ElC9Vgi5szqEtxPOMl1sMGRupk0ARnlL+sIEFPOhCVMyH7EvpvcNnFK84gITJTFNPHtrE/T4BWj0a5RJNocE5QcTNbpcx2ixbpUm8ytB1vEPnWeQjKMjCuudFZ9fbznuhsH1r0pSd/gtBoTzrnUt/dX7/ptHPJHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747881331; c=relaxed/simple; bh=X8/nxAlnKfmIilukc3uLUJEJN3++P9YxcsunTFAgtag=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BC00shq5IZZPMLjvEm1yzJi/We6LoOe9GjSL6sOKnNrPNTM/F9FNvXNAW9579mG43QoL1kli5yeraOhvAOytg7dZYkCDE/On7YZUtRbHW+zQVj57aVjb84Z2VWXbh4NJ+IUao154WO9iOvBLyDEQ7hipPyTNHWABdbH1ujvQsyM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 6893bc6e36b511f0b29709d653e92f7d-20250522 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.45, REQID:4803fc01-066d-448a-8340-36b1d7bdff29, IP:0, U RL:0,TC:0,Content:-25,EDM:-25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:-50 X-CID-META: VersionHash:6493067, CLOUDID:20bd7ec11e7dd67939d0da93bdc0c57b, BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102,TC:nil,Content:0|50,EDM:1,IP:n il,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LE S:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 6893bc6e36b511f0b29709d653e92f7d-20250522 Received: from mail.kylinos.cn [(10.44.16.175)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 1973197324; Thu, 22 May 2025 10:35:21 +0800 Received: from mail.kylinos.cn (localhost [127.0.0.1]) by mail.kylinos.cn (NSMail) with SMTP id 3B0C6E00351D; Thu, 22 May 2025 10:17:09 +0800 (CST) X-ns-mid: postfix-682E8925-85700445 Received: from localhost.localdomain (unknown [172.25.120.24]) by mail.kylinos.cn (NSMail) with ESMTPA id 17B3CE00351C; Thu, 22 May 2025 10:17:08 +0800 (CST) From: Zihuan Zhang To: rafael@kernel.org, len.brown@intel.com, pavel@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, tzungbi@kernel.org, a.fatoum@pengutronix.de, jani.nikula@intel.com, joel.granados@kernel.org, paulmck@kernel.org, zhangguopeng@kylinos.cn, linux@weissschuh.net, Zihuan Zhang Subject: [PATCH v2 3/3] PM / Sleep: Replace mutex_trylock(&system_transition_mutex) with try_lock_system_sleep() Date: Thu, 22 May 2025 10:16:49 +0800 Message-Id: <20250522021649.55228-4-zhangzihuan@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250522021649.55228-1-zhangzihuan@kylinos.cn> References: <20250522021649.55228-1-zhangzihuan@kylinos.cn> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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; }