From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtpbgbr1.qq.com (smtpbgbr1.qq.com [54.207.19.206]) (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 74B96378D82 for ; Thu, 28 May 2026 08:19:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=54.207.19.206 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779956399; cv=none; b=n/7ommjcCdIV00i/gHEzZ/cvhkvdc0VOVAyY6doy6YDjvVq8hUOwA9YpbzRa9utjFzgoZ665SwuSqlROwYqZDdRsCLCPc8jp43vE6pDQEZEkvS6Vd1ZZ18aeubZxGOC/g2Z7DdHF8vlXG5XjqPr2T4YGYwM+6s9bo9U7fEK+CY4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779956399; c=relaxed/simple; bh=nAVAHsXso3C/0PsJj5P60FI82/FmYb+nqnvDIYEgvvo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hoyWUX6O6YGadcgEAugsAbycs8GCf0CGuEZrkDEEBxLK8vsBLFcOhGPDtHCvvp2LR/ZcTRl8EcUMwsZl8adH3UKRRqQStFNkj6hAPS1UYOkHibKBq/RPFcl5vY+WwMP7tloja6QVhiuklhSAlAfSgh2Xs1FJKozXKJnB9ma26g0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uniontech.com; spf=pass smtp.mailfrom=uniontech.com; dkim=pass (1024-bit key) header.d=uniontech.com header.i=@uniontech.com header.b=RQO1kUr4; arc=none smtp.client-ip=54.207.19.206 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uniontech.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=uniontech.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=uniontech.com header.i=@uniontech.com header.b="RQO1kUr4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uniontech.com; s=onoh2408; t=1779956358; bh=hTfKHFLpQr6Uxul1FFGZN32A4tJE6awvwOwRmFziUKE=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=RQO1kUr4l2CckPcdzHM4mlAIk2Pi4J67sawh8WX5ktO14RUEukygM0ePyGgtO9LhT AOEbK0uQcful/s2zAS4zclYWfH0OSFx36cIhJUBZb4AgkypEg/b936lE3YyFF1Xcxk ro+1Q3l6Ztl9tKKyF47ZAFDwxBL6Xv96ortsWXEw= X-QQ-mid: zesmtpgz3t1779956344tbbf05453 X-QQ-Originating-IP: 8iWOHWTPwmeusF4wCfttTXj2BiriBmDGHpk8HJ2MsI0= Received: from localhost.localdomain ( [113.57.152.160]) by bizesmtp.qq.com (ESMTP) with id ; Thu, 28 May 2026 16:18:55 +0800 (CST) X-QQ-SSF: 0000000000000000000000000000000 X-QQ-GoodBg: 1 X-BIZMAIL-ID: 17934321688194046005 EX-QQ-RecipientCnt: 15 From: Haowen Tu To: rafael@kernel.org Cc: lenb@kernel.org, pavel@kernel.org, linux-pm@vger.kernel.org, laurent.pinchart@ideasonboard.com, hansg@kernel.org, mchehab@kernel.org, linux-media@vger.kernel.org, gregkh@linuxfoundation.org, stern@rowland.harvard.edu, oneukum@suse.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@uniontech.com, Haowen Tu Subject: [PATCH v2 1/2] PM: hibernate: add pm_hibernation_snapshot_done() helper Date: Thu, 28 May 2026 16:18:39 +0800 Message-Id: <20260528081840.3528089-2-tuhaowen@uniontech.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260528081840.3528089-1-tuhaowen@uniontech.com> References: <20260428080513.1833515-1-tuhaowen@uniontech.com> <20260528081840.3528089-1-tuhaowen@uniontech.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: zesmtpgz:uniontech.com:qybglogicsvrsz:qybglogicsvrsz3b-0 X-QQ-XMAILINFO: M0IYNfr6MkC9JRNSETXnwWYtw3/WbSNoOco6Qy63t9G+I6+H8eWaFZJ+ Vl/CDl+PL8JjsyNET2+MnaZp14vOwPK0F9Hy+BsN9t78epR3ajN9iU90rRn4ArhO9p4Fzxx q5ng2CoBhtXQlzW5ZhfbiMfE1HcZIStup7STvokK5evYX9aEKVlOOicFPZnXWLOm08k6Lpz ZQyCvPiFCL8KVh3/2JFf+01w6VEd/fT0coCeRvgfx5sKAkYLvCvwIRB8c87JFQU5MdvoTzH RAyHl4/dK7rVlXI+0YPci/HEKklJtLL6vFrsWmVI3FZBZXxkRGIEwLPVHgcK5vAaeWd1yiz Bwx8WyPQmYyBI8M/cvH3Jcmahvmn1ktJ1PjoLDDGVhFHdATjcOes7nZuwl3UOU3eQn6+6vq YogSmEx7HfGV+hFuRiM8Xf8dcJLsH54xZ1AQq01zvY5V8IRlGTJEZI588vHvI7i4V2EGQ5G DzVODj0TIO2lTCLixBo7R9sFde/qNWY66gyWr71x//jWfhy8VSL5MdyBGW6tsuEijl3Lfk7 /AFRLOR5PQQ01ocb5ufwppDyfmgUBmnshxM4txBvvKhGDKUhb3HudH1Leu0hz1tYMDZSPnx i/PWSjOY3SymMPW/6edHsJUT4u3P/Wsvaihq7rIliA/XJiMpJlcmjAoenB5nHr73QsuoYqF +kCjRnTvXs2T23kxECdMbp1v3y7p6ip7x4oR/bXwFz+Lw4FJKskrA0QHf9OsNbxvpxwhurq kX9a+FPtqtTwZ2yjRThiLTL4gmdQawQ3kjraq6eb11CBOCgCKzd4UCF5NzlgvyWaj7FhV2T +rlJ+Yua+QfPuGDKQMOHkpsimPLE5T//HPnxG5FrG2zJro0G2jdCeBmlqQAHHOEL7RHFSdZ a2sSKm0LMRQPilqHLNkkHxPXBYF578DfmyB3V24Q+sYwdPLy9g9/gxguW6/DcczitVXo/kG TUzDGAWLRjnJ+aRf6Nqz5mjWFAQCJcPe4rBH+T+/OP3J9ySXFrV5L1hE0XcHhHkzvjjjINi verj+Xa7Gd19JVjGwTy0nxQEjdcRmC8VO62tbrB6u9M3Z07+iJJw9iQy1Z0UD7JfDlLUgTu 1f6ECCid6zCpdPp8h8LbJo= X-QQ-XMRINFO: NS+P29fieYNwqS3WCnRCOn9D1NpZuCnCRA== X-QQ-RECHKSPAM: 0 During hibernation, after create_image() saves the memory snapshot, the kernel resumes devices with PMSG_THAW solely to write the hibernation image to storage, then powers off. Drivers for hardware not involved in storage I/O have no reason to reinitialize during this transient phase. Some subsystems, such as USB, do not expose the hibernation PM message to driver resume callbacks, so drivers there need an explicit query to distinguish the image-write phase from the final restore path. Export pm_hibernation_snapshot_done() for this purpose. The implementation returns !!in_suspend, which is set to 1 in create_image() just before swsusp_arch_suspend(). Because in_suspend is marked __nosavedata, it is not saved into the hibernation image; on the restore path the variable remains 0, so the helper correctly returns false during PMSG_RESTORE device resume. Clear in_suspend before releasing snapshot memory on hibernation failure paths and after swsusp_write() returns, so the helper does not report a stale snapshot after the snapshot pages have been released. Signed-off-by: Haowen Tu --- Changes in v2: - Rename pm_hibernation_storing_image() to pm_hibernation_snapshot_done(). - Clear in_suspend before releasing snapshot memory on failure paths and after swsusp_write() returns. include/linux/suspend.h | 2 ++ kernel/power/hibernate.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/linux/suspend.h b/include/linux/suspend.h index b02876f1ae38..78e7e33c3d19 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -393,6 +393,7 @@ extern void hibernation_set_ops(const struct platform_hibernation_ops *ops); extern int hibernate(void); extern bool system_entering_hibernation(void); extern bool hibernation_available(void); +extern bool pm_hibernation_snapshot_done(void); asmlinkage int swsusp_save(void); extern struct pbe *restore_pblist; int pfn_is_nosave(unsigned long pfn); @@ -412,6 +413,7 @@ static inline void hibernation_set_ops(const struct platform_hibernation_ops *op static inline int hibernate(void) { return -ENOSYS; } static inline bool system_entering_hibernation(void) { return false; } static inline bool hibernation_available(void) { return false; } +static inline bool pm_hibernation_snapshot_done(void) { return false; } static inline int hibernate_quiet_exec(int (*func)(void *data), void *data) { return -ENOTSUPP; diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index af8d07bafe02..47047937e262 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -113,6 +113,25 @@ bool hibernation_available(void) !secretmem_active() && !cxl_mem_active(); } +/** + * pm_hibernation_snapshot_done - check if a hibernation snapshot is available + * + * After create_image() saves a memory snapshot, the kernel briefly resumes + * devices with PMSG_THAW to write the image to storage before final powerdown. + * Drivers that do not need to participate in image writing may call this + * helper from their resume callbacks to skip unnecessary hardware + * initialization during that transient phase. + * + * Context: May be called from device PM callbacks. + * Return: %true if a hibernation snapshot has been taken and has not been + * released yet. + */ +bool pm_hibernation_snapshot_done(void) +{ + return !!in_suspend; +} +EXPORT_SYMBOL_GPL(pm_hibernation_snapshot_done); + /** * hibernation_set_ops - Set the global hibernate operations. * @ops: Hibernation operations to use in subsequent hibernation transitions. @@ -418,6 +437,7 @@ static void shrink_shmem_memory(void) int hibernation_snapshot(int platform_mode) { pm_message_t msg; + bool snapshot_done; int error; pm_suspend_clear_flags(); @@ -474,15 +494,18 @@ int hibernation_snapshot(int platform_mode) * returns here (1) after the image has been created or the * image creation has failed and (2) after a successful restore. */ + snapshot_done = in_suspend; /* We may need to release the preallocated image pages here. */ - if (error || !in_suspend) + if (error || !snapshot_done) { + in_suspend = 0; swsusp_free(); + } - msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; + msg = snapshot_done ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; dpm_resume(msg); - if (error || !in_suspend) + if (error || !snapshot_done) pm_restore_gfp_mask(); console_resume_all(); @@ -865,6 +888,7 @@ int hibernate(void) pm_pr_dbg("Writing hibernation image.\n"); error = swsusp_write(flags); + in_suspend = 0; swsusp_free(); if (!error) { if (hibernation_mode == HIBERNATION_TEST_RESUME) @@ -872,7 +896,6 @@ int hibernate(void) else power_down(); } - in_suspend = 0; pm_restore_gfp_mask(); } else { pm_pr_dbg("Hibernation image restored successfully.\n"); -- 2.20.1