Linux USB
 help / color / mirror / Atom feed
From: Haowen Tu <tuhaowen@uniontech.com>
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 <tuhaowen@uniontech.com>
Subject: [PATCH v2 1/2] PM: hibernate: add pm_hibernation_snapshot_done() helper
Date: Thu, 28 May 2026 16:18:39 +0800	[thread overview]
Message-ID: <20260528081840.3528089-2-tuhaowen@uniontech.com> (raw)
In-Reply-To: <20260528081840.3528089-1-tuhaowen@uniontech.com>

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 <tuhaowen@uniontech.com>
---
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

  reply	other threads:[~2026-05-28  8:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260428080513.1833515-1-tuhaowen@uniontech.com>
2026-05-28  8:18 ` [PATCH v2 0/2] PM: hibernate: skip UVC resume after snapshot Haowen Tu
2026-05-28  8:18   ` Haowen Tu [this message]
2026-06-01 18:22     ` [PATCH v2 1/2] PM: hibernate: add pm_hibernation_snapshot_done() helper Rafael J. Wysocki
2026-06-02  3:24       ` Haowen Tu
2026-06-18  1:31         ` [PATCH v3 0/3] PM: hibernate: skip UVC streaming restart after snapshot Haowen Tu
2026-06-18  1:31           ` [PATCH v3 1/3] PM: hibernate: clear in_suspend before freeing the snapshot Haowen Tu
2026-06-18  1:31           ` [PATCH v3 2/3] PM: hibernate: add pm_hibernation_snapshot_done() helper Haowen Tu
2026-06-18  1:31           ` [PATCH v3 3/3] media: uvcvideo: skip streaming restart after hibernation snapshot Haowen Tu
2026-05-28  8:18   ` [PATCH v2 2/2] media: uvcvideo: skip resume " Haowen Tu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260528081840.3528089-2-tuhaowen@uniontech.com \
    --to=tuhaowen@uniontech.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hansg@kernel.org \
    --cc=kernel@uniontech.com \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=lenb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=oneukum@suse.com \
    --cc=pavel@kernel.org \
    --cc=rafael@kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox