Skip to content

Commit 2640e81

Browse files
guoqzhansuperm1
authored andcommitted
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>
1 parent 924dda0 commit 2640e81

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
@@ -381,6 +381,23 @@ static int create_image(int platform_mode)
381381
return error;
382382
}
383383

384+
static void shrink_shmem_memory(void)
385+
{
386+
struct sysinfo info;
387+
unsigned long nr_shmem_pages, nr_freed_pages;
388+
389+
si_meminfo(&info);
390+
nr_shmem_pages = info.sharedram; /* current page count used for shmem */
391+
/*
392+
* The intent is to reclaim all shmem pages. Though shrink_all_memory() can
393+
* only reclaim about half of them, it's enough for creating the hibernation
394+
* image.
395+
*/
396+
nr_freed_pages = shrink_all_memory(nr_shmem_pages);
397+
pr_debug("requested to reclaim %lu shmem pages, actually freed %lu pages\n",
398+
nr_shmem_pages, nr_freed_pages);
399+
}
400+
384401
/**
385402
* hibernation_snapshot - Quiesce devices and create a hibernation image.
386403
* @platform_mode: If set, use platform driver to prepare for the transition.
@@ -422,6 +439,15 @@ int hibernation_snapshot(int platform_mode)
422439
goto Thaw;
423440
}
424441

442+
/*
443+
* Device drivers may move lots of data to shmem in dpm_prepare(). The shmem
444+
* pages will use lots of system memory, causing hibernation image creation
445+
* fail due to insufficient free memory.
446+
* This call is to force flush the shmem pages to swap disk and reclaim
447+
* the system memory so that image creation can succeed.
448+
*/
449+
shrink_shmem_memory();
450+
425451
console_suspend_all();
426452
pm_restrict_gfp_mask();
427453

0 commit comments

Comments
 (0)