Skip to content

Commit 81a6d74

Browse files
committed
PM: hibernate: shrink shmem pages after dev_pm_ops.prepare()
JIRA: https://issues.redhat.com/browse/RHEL-116879 commit 2640e81 Author: Samuel Zhang <guoqing.zhang@amd.com> Date: Thu Jul 10 14:23:11 2025 +0800 PM: hibernate: shrink shmem pages after dev_pm_ops.prepare() When hibernate with data center dGPUs, huge number of VRAM data will be moved to shmem during dev_pm_ops.prepare(). These shmem pages take a lot of system memory so that there's no enough free memory for creating the hibernation image. This will cause hibernation fail and abort. After dev_pm_ops.prepare(), call shrink_all_memory() to force move shmem pages to swap disk and reclaim the pages, so that there's enough system memory for hibernation image and less pages needed to copy to the image. This patch can only flush and free about half shmem pages. It will be better to flush and free more pages, even all of shmem pages, so that there're less pages to be copied to the hibernation image and the overall hibernation time can be reduced. Signed-off-by: Samuel Zhang <guoqing.zhang@amd.com> Acked-by: Rafael J. Wysocki <rafael@kernel.org> Link: https://lore.kernel.org/r/20250710062313.3226149-4-guoqing.zhang@amd.com Signed-off-by: Mario Limonciello <mario.limonciello@amd.com> Signed-off-by: Anusha Srivatsa <asrivats@redhat.com>
1 parent bc588fb commit 81a6d74

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

kernel/power/hibernate.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,23 @@ static int create_image(int platform_mode)
375375
return error;
376376
}
377377

378+
static void shrink_shmem_memory(void)
379+
{
380+
struct sysinfo info;
381+
unsigned long nr_shmem_pages, nr_freed_pages;
382+
383+
si_meminfo(&info);
384+
nr_shmem_pages = info.sharedram; /* current page count used for shmem */
385+
/*
386+
* The intent is to reclaim all shmem pages. Though shrink_all_memory() can
387+
* only reclaim about half of them, it's enough for creating the hibernation
388+
* image.
389+
*/
390+
nr_freed_pages = shrink_all_memory(nr_shmem_pages);
391+
pr_debug("requested to reclaim %lu shmem pages, actually freed %lu pages\n",
392+
nr_shmem_pages, nr_freed_pages);
393+
}
394+
378395
/**
379396
* hibernation_snapshot - Quiesce devices and create a hibernation image.
380397
* @platform_mode: If set, use platform driver to prepare for the transition.
@@ -416,6 +433,15 @@ int hibernation_snapshot(int platform_mode)
416433
goto Thaw;
417434
}
418435

436+
/*
437+
* Device drivers may move lots of data to shmem in dpm_prepare(). The shmem
438+
* pages will use lots of system memory, causing hibernation image creation
439+
* fail due to insufficient free memory.
440+
* This call is to force flush the shmem pages to swap disk and reclaim
441+
* the system memory so that image creation can succeed.
442+
*/
443+
shrink_shmem_memory();
444+
419445
console_suspend_all();
420446
pm_restrict_gfp_mask();
421447

0 commit comments

Comments
 (0)