Swap has no way to ask how big a hibernation image gets. Add hibernate=reserve[:] and hibernation_reserve_pages(), which swap can call at swapon to learn how much space to hold back. The answer is , or image_size when no size is given. It is zero without hibernate=reserve and for any area other than the one named by resume=. The image can still outgrow it. Assisted-by: Claude:claude-opus-5 Signed-off-by: Youngjun Park --- .../admin-guide/kernel-parameters.txt | 8 ++++++ include/linux/suspend.h | 3 +++ kernel/power/hibernate.c | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 68647ff4bdd2..eb2454b73f4b 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1980,6 +1980,14 @@ Kernel parameters protect_image Turn on image protection during restoration (that will set all pages holding image data during restoration read-only). + reserve[:] + Hold of swap back for the image when the + area named by resume= and resume_offset= is + swapped on. Swap cannot use that space. + Without , /sys/power/image_size is used. + takes K, M or G. More than half of the + area is refused. Later changes take effect at + the next swapon. hibernate.compressor= [HIBERNATION] Compression algorithm to be used with hibernation. diff --git a/include/linux/suspend.h b/include/linux/suspend.h index b02876f1ae38..1fb1f5d6ce6a 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); +unsigned long hibernation_reserve_pages(dev_t dev, sector_t offset); asmlinkage int swsusp_save(void); extern struct pbe *restore_pblist; int pfn_is_nosave(unsigned long pfn); @@ -412,6 +413,8 @@ 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 unsigned long hibernation_reserve_pages(dev_t dev, sector_t offset) +{ return 0; } 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 907d791b85ad..225fe55ee703 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -41,6 +41,8 @@ static int nocompress; static int noresume; static int nohibernate; +static int reserve_swap; +static unsigned long reserve_size; static int resume_wait; static unsigned int resume_delay; static char resume_file[256] = CONFIG_PM_STD_PARTITION; @@ -113,6 +115,26 @@ bool hibernation_available(void) !secretmem_active() && !cxl_mem_active(); } +/** + * hibernation_reserve_pages - How much swap to hold back for an image. + * @dev: the block device the swap area lives on. + * @offset: the first block of the swap area on that device. + * + * Only the area named by resume= and resume_offset= holds anything back. + * The image can still grow past this amount. + * + * Return: the number of pages to hold back, or zero when nothing should be. + */ +unsigned long hibernation_reserve_pages(dev_t dev, sector_t offset) +{ + if (!reserve_swap || !hibernation_available()) + return 0; + if (!dev || dev != swsusp_resume_device || offset != swsusp_resume_block) + return 0; + + return DIV_ROUND_UP(reserve_size ? reserve_size : image_size, PAGE_SIZE); +} + /** * hibernation_set_ops - Set the global hibernate operations. * @ops: Hibernation operations to use in subsequent hibernation transitions. @@ -1437,6 +1459,10 @@ static int __init hibernate_setup(char *str) } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && !strncmp(str, "protect_image", 13)) { enable_restore_image_protection(); + } else if (!strncmp(str, "reserve", 7)) { + reserve_swap = 1; + if (str[7] == ':') + reserve_size = memparse(str + 8, NULL); } return 1; } -- 2.48.1