public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] PM: hibernate: call preallocate_image after freeze prepare
@ 2026-03-26 11:36 Matthew Leach
  2026-03-26 13:09 ` Mario Limonciello
  2026-03-31 12:45 ` Rafael J. Wysocki
  0 siblings, 2 replies; 4+ messages in thread
From: Matthew Leach @ 2026-03-26 11:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Pavel Machek, Len Brown, Mario Limonciello
  Cc: linux-pm, linux-kernel, YoungJun Park, kernel, Matthew Leach

Certain drivers release resources (pinned pages, etc.) into system
memory during the prepare freeze PM op, making them swappable.
Currently, hibernate_preallocate_memory is called before prepare freeze,
so those drivers have no opportunity to release resources first. If a
driver is holding a large amount of unswappable system RAM, this can
cause hibernate_preallocate_memory to fail.

Move the call to hibernate_preallocate_memory after prepare freeze.
According to the documentation for the prepare callback, devices should
be left in a usable state, so storage drivers should still be able to
service I/O requests. This allows drivers to release unswappable
resources prior to preallocation, so they can be swapped out through
hibernate_preallocate_memory's reclaim path. Also remove
shrink_shmem_memory since hibernate_preallocate_memory will have
reclaimed enough memory for the hibernation image.

Signed-off-by: Matthew Leach <matthew.leach@collabora.com>
---
Changes in v2:
- Removed shrink_shmem_memory.
- Fixed missing call to dpm_prepare in error path.
- Link to v1: https://lore.kernel.org/r/20260321-hibernation-fixes-v1-1-5fe9637b6ff9@collabora.com
---
 kernel/power/hibernate.c | 46 +++++++++-------------------------------------
 1 file changed, 9 insertions(+), 37 deletions(-)

diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index af8d07bafe02..39b0a8ea4024 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -392,23 +392,6 @@ static int create_image(int platform_mode)
 	return error;
 }
 
-static void shrink_shmem_memory(void)
-{
-	struct sysinfo info;
-	unsigned long nr_shmem_pages, nr_freed_pages;
-
-	si_meminfo(&info);
-	nr_shmem_pages = info.sharedram; /* current page count used for shmem */
-	/*
-	 * The intent is to reclaim all shmem pages. Though shrink_all_memory() can
-	 * only reclaim about half of them, it's enough for creating the hibernation
-	 * image.
-	 */
-	nr_freed_pages = shrink_all_memory(nr_shmem_pages);
-	pr_debug("requested to reclaim %lu shmem pages, actually freed %lu pages\n",
-			nr_shmem_pages, nr_freed_pages);
-}
-
 /**
  * hibernation_snapshot - Quiesce devices and create a hibernation image.
  * @platform_mode: If set, use platform driver to prepare for the transition.
@@ -425,14 +408,9 @@ int hibernation_snapshot(int platform_mode)
 	if (error)
 		goto Close;
 
-	/* Preallocate image memory before shutting down devices. */
-	error = hibernate_preallocate_memory();
-	if (error)
-		goto Close;
-
 	error = freeze_kernel_threads();
 	if (error)
-		goto Cleanup;
+		goto Close;
 
 	if (hibernation_test(TEST_FREEZER)) {
 
@@ -441,23 +419,17 @@ int hibernation_snapshot(int platform_mode)
 		 * successful freezer test.
 		 */
 		freezer_test_done = true;
-		goto Thaw;
+		goto ThawKThreads;
 	}
 
 	error = dpm_prepare(PMSG_FREEZE);
-	if (error) {
-		dpm_complete(PMSG_RECOVER);
+	if (error)
 		goto Thaw;
-	}
 
-	/*
-	 * Device drivers may move lots of data to shmem in dpm_prepare(). The shmem
-	 * pages will use lots of system memory, causing hibernation image creation
-	 * fail due to insufficient free memory.
-	 * This call is to force flush the shmem pages to swap disk and reclaim
-	 * the system memory so that image creation can succeed.
-	 */
-	shrink_shmem_memory();
+	/* Preallocate image memory before shutting down devices. */
+	error = hibernate_preallocate_memory();
+	if (error)
+		goto Thaw;
 
 	console_suspend_all();
 	pm_restrict_gfp_mask();
@@ -493,9 +465,9 @@ int hibernation_snapshot(int platform_mode)
 	return error;
 
  Thaw:
+	dpm_complete(PMSG_RECOVER);
+ ThawKThreads:
 	thaw_kernel_threads();
- Cleanup:
-	swsusp_free();
 	goto Close;
 }
 

---
base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c
change-id: 20260321-hibernation-fixes-69bca2ee5b65

Best regards,
--  
Matt


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] PM: hibernate: call preallocate_image after freeze prepare
  2026-03-26 11:36 [PATCH v2] PM: hibernate: call preallocate_image after freeze prepare Matthew Leach
@ 2026-03-26 13:09 ` Mario Limonciello
  2026-03-31 12:45 ` Rafael J. Wysocki
  1 sibling, 0 replies; 4+ messages in thread
From: Mario Limonciello @ 2026-03-26 13:09 UTC (permalink / raw)
  To: Matthew Leach, Rafael J. Wysocki, Pavel Machek, Len Brown
  Cc: linux-pm, linux-kernel, YoungJun Park, kernel



On 3/26/26 06:36, Matthew Leach wrote:
> [You don't often get email from matthew.leach@collabora.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
> 
> Certain drivers release resources (pinned pages, etc.) into system
> memory during the prepare freeze PM op, making them swappable.
> Currently, hibernate_preallocate_memory is called before prepare freeze,
> so those drivers have no opportunity to release resources first. If a
> driver is holding a large amount of unswappable system RAM, this can
> cause hibernate_preallocate_memory to fail.
> 
> Move the call to hibernate_preallocate_memory after prepare freeze.
> According to the documentation for the prepare callback, devices should
> be left in a usable state, so storage drivers should still be able to
> service I/O requests. This allows drivers to release unswappable
> resources prior to preallocation, so they can be swapped out through
> hibernate_preallocate_memory's reclaim path. Also remove
> shrink_shmem_memory since hibernate_preallocate_memory will have
> reclaimed enough memory for the hibernation image.
> 
> Signed-off-by: Matthew Leach <matthew.leach@collabora.com>
Reviewed-by: Mario Limonciello (AMD) <superm1@kernel.org>
> ---
> Changes in v2:
> - Removed shrink_shmem_memory.
> - Fixed missing call to dpm_prepare in error path.
> - Link to v1: https://lore.kernel.org/r/20260321-hibernation-fixes-v1-1-5fe9637b6ff9@collabora.com
> ---
>   kernel/power/hibernate.c | 46 +++++++++-------------------------------------
>   1 file changed, 9 insertions(+), 37 deletions(-)
> 
> diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
> index af8d07bafe02..39b0a8ea4024 100644
> --- a/kernel/power/hibernate.c
> +++ b/kernel/power/hibernate.c
> @@ -392,23 +392,6 @@ static int create_image(int platform_mode)
>          return error;
>   }
> 
> -static void shrink_shmem_memory(void)
> -{
> -       struct sysinfo info;
> -       unsigned long nr_shmem_pages, nr_freed_pages;
> -
> -       si_meminfo(&info);
> -       nr_shmem_pages = info.sharedram; /* current page count used for shmem */
> -       /*
> -        * The intent is to reclaim all shmem pages. Though shrink_all_memory() can
> -        * only reclaim about half of them, it's enough for creating the hibernation
> -        * image.
> -        */
> -       nr_freed_pages = shrink_all_memory(nr_shmem_pages);
> -       pr_debug("requested to reclaim %lu shmem pages, actually freed %lu pages\n",
> -                       nr_shmem_pages, nr_freed_pages);
> -}
> -
>   /**
>    * hibernation_snapshot - Quiesce devices and create a hibernation image.
>    * @platform_mode: If set, use platform driver to prepare for the transition.
> @@ -425,14 +408,9 @@ int hibernation_snapshot(int platform_mode)
>          if (error)
>                  goto Close;
> 
> -       /* Preallocate image memory before shutting down devices. */
> -       error = hibernate_preallocate_memory();
> -       if (error)
> -               goto Close;
> -
>          error = freeze_kernel_threads();
>          if (error)
> -               goto Cleanup;
> +               goto Close;
> 
>          if (hibernation_test(TEST_FREEZER)) {
> 
> @@ -441,23 +419,17 @@ int hibernation_snapshot(int platform_mode)
>                   * successful freezer test.
>                   */
>                  freezer_test_done = true;
> -               goto Thaw;
> +               goto ThawKThreads;
>          }
> 
>          error = dpm_prepare(PMSG_FREEZE);
> -       if (error) {
> -               dpm_complete(PMSG_RECOVER);
> +       if (error)
>                  goto Thaw;
> -       }
> 
> -       /*
> -        * Device drivers may move lots of data to shmem in dpm_prepare(). The shmem
> -        * pages will use lots of system memory, causing hibernation image creation
> -        * fail due to insufficient free memory.
> -        * This call is to force flush the shmem pages to swap disk and reclaim
> -        * the system memory so that image creation can succeed.
> -        */
> -       shrink_shmem_memory();
> +       /* Preallocate image memory before shutting down devices. */
> +       error = hibernate_preallocate_memory();
> +       if (error)
> +               goto Thaw;
> 
>          console_suspend_all();
>          pm_restrict_gfp_mask();
> @@ -493,9 +465,9 @@ int hibernation_snapshot(int platform_mode)
>          return error;
> 
>    Thaw:
> +       dpm_complete(PMSG_RECOVER);
> + ThawKThreads:
>          thaw_kernel_threads();
> - Cleanup:
> -       swsusp_free();
>          goto Close;
>   }
> 
> 
> ---
> base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c
> change-id: 20260321-hibernation-fixes-69bca2ee5b65
> 
> Best regards,
> --
> Matt
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] PM: hibernate: call preallocate_image after freeze prepare
  2026-03-26 11:36 [PATCH v2] PM: hibernate: call preallocate_image after freeze prepare Matthew Leach
  2026-03-26 13:09 ` Mario Limonciello
@ 2026-03-31 12:45 ` Rafael J. Wysocki
  2026-04-01 10:13   ` Matthew Leach
  1 sibling, 1 reply; 4+ messages in thread
From: Rafael J. Wysocki @ 2026-03-31 12:45 UTC (permalink / raw)
  To: Matthew Leach
  Cc: Rafael J. Wysocki, Pavel Machek, Len Brown, Mario Limonciello,
	linux-pm, linux-kernel, YoungJun Park, kernel

On Thu, Mar 26, 2026 at 12:37 PM Matthew Leach
<matthew.leach@collabora.com> wrote:
>
> Certain drivers release resources (pinned pages, etc.) into system
> memory during the prepare freeze PM op, making them swappable.
> Currently, hibernate_preallocate_memory is called before prepare freeze,
> so those drivers have no opportunity to release resources first. If a
> driver is holding a large amount of unswappable system RAM, this can
> cause hibernate_preallocate_memory to fail.
>
> Move the call to hibernate_preallocate_memory after prepare freeze.
> According to the documentation for the prepare callback, devices should
> be left in a usable state, so storage drivers should still be able to
> service I/O requests. This allows drivers to release unswappable
> resources prior to preallocation, so they can be swapped out through
> hibernate_preallocate_memory's reclaim path. Also remove
> shrink_shmem_memory since hibernate_preallocate_memory will have
> reclaimed enough memory for the hibernation image.
>
> Signed-off-by: Matthew Leach <matthew.leach@collabora.com>
> ---
> Changes in v2:
> - Removed shrink_shmem_memory.
> - Fixed missing call to dpm_prepare in error path.
> - Link to v1: https://lore.kernel.org/r/20260321-hibernation-fixes-v1-1-5fe9637b6ff9@collabora.com
> ---
>  kernel/power/hibernate.c | 46 +++++++++-------------------------------------
>  1 file changed, 9 insertions(+), 37 deletions(-)
>
> diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
> index af8d07bafe02..39b0a8ea4024 100644
> --- a/kernel/power/hibernate.c
> +++ b/kernel/power/hibernate.c
> @@ -392,23 +392,6 @@ static int create_image(int platform_mode)
>         return error;
>  }
>
> -static void shrink_shmem_memory(void)
> -{
> -       struct sysinfo info;
> -       unsigned long nr_shmem_pages, nr_freed_pages;
> -
> -       si_meminfo(&info);
> -       nr_shmem_pages = info.sharedram; /* current page count used for shmem */
> -       /*
> -        * The intent is to reclaim all shmem pages. Though shrink_all_memory() can
> -        * only reclaim about half of them, it's enough for creating the hibernation
> -        * image.
> -        */
> -       nr_freed_pages = shrink_all_memory(nr_shmem_pages);
> -       pr_debug("requested to reclaim %lu shmem pages, actually freed %lu pages\n",
> -                       nr_shmem_pages, nr_freed_pages);
> -}
> -
>  /**
>   * hibernation_snapshot - Quiesce devices and create a hibernation image.
>   * @platform_mode: If set, use platform driver to prepare for the transition.
> @@ -425,14 +408,9 @@ int hibernation_snapshot(int platform_mode)
>         if (error)
>                 goto Close;
>
> -       /* Preallocate image memory before shutting down devices. */
> -       error = hibernate_preallocate_memory();
> -       if (error)
> -               goto Close;
> -
>         error = freeze_kernel_threads();
>         if (error)
> -               goto Cleanup;
> +               goto Close;
>
>         if (hibernation_test(TEST_FREEZER)) {
>
> @@ -441,23 +419,17 @@ int hibernation_snapshot(int platform_mode)
>                  * successful freezer test.
>                  */
>                 freezer_test_done = true;
> -               goto Thaw;
> +               goto ThawKThreads;

Do not change the name of this label.

>         }
>
>         error = dpm_prepare(PMSG_FREEZE);
> -       if (error) {
> -               dpm_complete(PMSG_RECOVER);
> +       if (error)
>                 goto Thaw;

But add a new label called "Complete" for this jump.

> -       }
>
> -       /*
> -        * Device drivers may move lots of data to shmem in dpm_prepare(). The shmem
> -        * pages will use lots of system memory, causing hibernation image creation
> -        * fail due to insufficient free memory.
> -        * This call is to force flush the shmem pages to swap disk and reclaim
> -        * the system memory so that image creation can succeed.
> -        */
> -       shrink_shmem_memory();
> +       /* Preallocate image memory before shutting down devices. */
> +       error = hibernate_preallocate_memory();
> +       if (error)
> +               goto Thaw;
>
>         console_suspend_all();
>         pm_restrict_gfp_mask();
> @@ -493,9 +465,9 @@ int hibernation_snapshot(int platform_mode)
>         return error;
>
>   Thaw:

swsusp_free() needs to be called here AFAICS or you'll see memory
allocation failures subsequently in some cases.

> +       dpm_complete(PMSG_RECOVER);
> + ThawKThreads:
>         thaw_kernel_threads();
> - Cleanup:
> -       swsusp_free();
>         goto Close;
>  }
>
>
> ---

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] PM: hibernate: call preallocate_image after freeze prepare
  2026-03-31 12:45 ` Rafael J. Wysocki
@ 2026-04-01 10:13   ` Matthew Leach
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Leach @ 2026-04-01 10:13 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Pavel Machek, Len Brown, Mario Limonciello, linux-pm,
	linux-kernel, YoungJun Park, kernel

Hi Rafael,

Thanks for the review.

"Rafael J. Wysocki" <rafael@kernel.org> writes:

> On Thu, Mar 26, 2026 at 12:37 PM Matthew Leach
> <matthew.leach@collabora.com> wrote:

[...]

>> @@ -493,9 +465,9 @@ int hibernation_snapshot(int platform_mode)
>>         return error;
>>
>>   Thaw:
>
> swsusp_free() needs to be called here AFAICS or you'll see memory
> allocation failures subsequently in some cases.

We only jump to this label in two places:

 - If dpm_prepare(PMSG_FREEZE) returns an error.
   hibernate_preallocate_memory() hasn't been called yet so there's no
   memory to free.

 - If hibernate_preallocate_memory() returns an error; in that instance
   hibernate_preallocate_memory() calls swsusp_free() to cleanup any
   memory that may have been allocated before returning the error.

AFAICS, I don't think we need an extra call to swsusp_free().  Happy to
change if I'm missing something though.

Regards,
-- 
Matt

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-04-01 10:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-26 11:36 [PATCH v2] PM: hibernate: call preallocate_image after freeze prepare Matthew Leach
2026-03-26 13:09 ` Mario Limonciello
2026-03-31 12:45 ` Rafael J. Wysocki
2026-04-01 10:13   ` Matthew Leach

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox