Re: [PATCH v2 1/2] PM: hibernate: add pm_hibernation_snapshot_done() helper
From: Rafael J. Wysocki
Date: Mon Jun 01 2026 - 14:24:07 EST
On Thu, May 28, 2026 at 10:19 AM Haowen Tu <tuhaowen@xxxxxxxxxxxxx> wrote:
>
> 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.
They do have a reason for doing it.
Their poweroff (or shutdown) callbacks will be called while preparing
to power off the system subsequently and they need to be ready for
that. The most straightforward way to achieve this is to resume so
they can "suspend" again.
> 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.
This last piece needs to be split off into a separate patch.
> Signed-off-by: Haowen Tu <tuhaowen@xxxxxxxxxxxxx>
> ---
> 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