[RFC PATCH 6/6] PM: hibernate: predict image preallocation failure for compress-on-copy
From: Nhat-Trieu Huynh-Pham
Date: Sat Nov 28 2026 - 14:32:04 EST
Compress-on-copy runs the stock 1:1 copy-page allocation first and only
uses the pool after that allocation comes up short. On a system where the
allocation is certain to fail, that attempt spends time in direct reclaim
and in swap-out before the fallback starts.
After shrink_all_memory(), the stock allocation target is known
(alloc = count - max_size) and so is the amount of usable free memory.
Bound reclaimable anonymous pages by free swap (get_nr_swap_pages()) and,
if alloc exceeds usable free pages plus remaining reclaimable pages, skip
the 1:1 attempt and preallocate the pool directly with __GFP_NORETRY from
the pages that are actually available, capped at saveable.
If the prediction is inconclusive, keep the stock path unchanged: a
successful allocation is used as before and a short one still enters the
fallback path. The prediction is a heuristic; fragmentation and the
reclaim path are not modelled. A false negative still falls back cleanly,
and any pool that does not fit fails with -ENOMEM before anything is
written.
Signed-off-by: Nhat-Trieu Huynh-Pham <trieu2.huynh@xxxxxxx>
---
Documentation/ABI/testing/sysfs-power | 17 +++---
.../admin-guide/kernel-parameters.txt | 12 ++--
Documentation/power/swsusp.rst | 23 ++++----
kernel/power/snapshot.c | 56 +++++++++++++++++++
4 files changed, 86 insertions(+), 22 deletions(-)
diff --git a/Documentation/ABI/testing/sysfs-power b/Documentation/ABI/testing/sysfs-power
index a6287d5615d8..a4dab5eba22d 100644
--- a/Documentation/ABI/testing/sysfs-power
+++ b/Documentation/ABI/testing/sysfs-power
@@ -482,14 +482,17 @@ Description:
the default.
"on"
- Run the stock 1:1 copy-page allocation first. If it
+ Predict whether the stock 1:1 copy-page allocation
+ can fit after the hibernation shrink. If it cannot,
+ skip that allocation and use a pool of compressed
+ image pages instead. If the prediction is
+ inconclusive, run the stock allocation. If it
comes up short, keep the pages it did allocate and
- fill a pool of compressed image pages up to the
- usable free budget before activating it. If no pool
- page can be obtained, hibernation fails. The whole
- compressed image must fit in the pool, otherwise
- hibernation fails with -ENOMEM before anything is
- written.
+ fill the pool up to the usable free budget before
+ activating it. If no pool page can be obtained,
+ hibernation fails. The whole compressed image must
+ fit in the pool, otherwise hibernation fails with
+ -ENOMEM before anything is written.
The setting applies to the next hibernation and is
serialized with system sleep transitions. Available when
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index d06449d593c4..0809318c93b5 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1994,11 +1994,13 @@ Kernel parameters
nocompress Don't compress/decompress hibernation images.
compress_on_copy
[CONFIG_HIBERNATION_COMPRESS_ON_COPY]
- Use a pool of compressed image pages when the
- stock 1:1 copy-page allocation fails. The pool
- keeps the pages already allocated for the stock
- copy and fills up to the usable free budget; the
- whole compressed image must fit in it.
+ Predict whether the stock 1:1 copy-page
+ allocation can fit. If it cannot, skip it and
+ use a pool of compressed image pages; otherwise
+ keep the stock path. If the stock copy comes up
+ short, keep the pages it did allocate and fill
+ the pool up to the usable free budget. The whole
+ compressed image must fit in the pool.
no Disable hibernation and resume.
protect_image Turn on image protection during restoration
(that will set all pages holding image data
diff --git a/Documentation/power/swsusp.rst b/Documentation/power/swsusp.rst
index a17d0d3c1489..0c36cef98414 100644
--- a/Documentation/power/swsusp.rst
+++ b/Documentation/power/swsusp.rst
@@ -85,16 +85,19 @@ compressed while writing. The on-disk image, the resume path and the CRC
layout are unchanged.
Enable it with ``hibernate=compress_on_copy`` or by writing ``on`` to
-``/sys/power/compress_on_copy``. With ``on``, the stock
-copy-page allocation is tried first. If it completes, the stock image is
-used. If it fails, the pages it allocated are kept and the fallback fills
-the pool up to the usable free budget before activating it. That budget
-includes the pages already allocated in the pool; any pool pages above it are
-returned to the allocator before tasks are frozen. The I/O reserve,
-reserved_size and the zone watermarks therefore stay free for the freeze
-callbacks. If no pool page can be obtained, hibernation fails. The whole
-compressed image must fit in the pool; otherwise it fails with ``-ENOMEM``
-before anything is written. The pool is not a streaming writer.
+``/sys/power/compress_on_copy``. After the hibernation shrink, the kernel
+compares the stock allocation size with the usable free pages plus what is
+still reclaimable. If the stock allocation cannot fit, it is skipped and a
+pool is allocated from the pages that are actually available. If the
+prediction is inconclusive, the stock allocation runs unchanged; when it
+comes up short, the pages it allocated are kept and the fallback fills the
+pool up to the usable free budget before activating it. That budget includes
+the pages already allocated in the pool; any pool pages above it are returned
+to the allocator before tasks are frozen. The I/O reserve, reserved_size and
+the zone watermarks therefore stay free for the freeze callbacks. If no pool
+page can be obtained, hibernation fails. The whole compressed image must fit
+in the pool; otherwise it fails with ``-ENOMEM`` before anything is written.
+The pool is not a streaming writer.
The staging area (about 332 KiB) is reserved before the stock allocation so
the fallback does not have to vmalloc it after the stock allocation has
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index 45be309065f9..c162cd2620f2 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -34,6 +34,7 @@
#ifdef CONFIG_HIBERNATION_COMPRESS_ON_COPY
#include <linux/lzo.h>
#include <linux/lz4.h>
+#include <linux/swap.h>
#include <linux/vmalloc.h>
#endif
@@ -2377,6 +2378,32 @@ static unsigned long cocopy_usable_free(void)
return free;
}
+static unsigned long cocopy_reclaimable_pages(void)
+{
+ unsigned long anon;
+
+ anon = global_node_page_state(NR_ACTIVE_ANON) +
+ global_node_page_state(NR_INACTIVE_ANON);
+ anon = min_t(unsigned long, anon,
+ (unsigned long)max(0L, get_nr_swap_pages()));
+
+ return global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B) +
+ global_node_page_state(NR_ACTIVE_FILE) +
+ global_node_page_state(NR_INACTIVE_FILE) +
+ anon;
+}
+
+/*
+ * The stock allocation is certain to fail if even the reclaimable pages that
+ * are left cannot cover alloc. This is only a prediction: fragmentation and
+ * the reclaim path are not modelled, so a false negative just runs the stock
+ * allocation and can still fall back later.
+ */
+static bool cocopy_prealloc_fails(unsigned long alloc)
+{
+ return alloc > cocopy_usable_free() + cocopy_reclaimable_pages();
+}
+
/*
* The pool can reuse the image pages already allocated in copy_bm, so the
* target is computed from the current free pages plus alloc_normal. The
@@ -2427,6 +2454,11 @@ static unsigned int cocopy_pool_size_pages(void)
#else /* !CONFIG_HIBERNATION_COMPRESS_ON_COPY */
static inline unsigned long cocopy_usable_free(void) { return 0; }
+static inline bool cocopy_prealloc_fails(unsigned long alloc)
+{
+ return false;
+}
+
static inline unsigned long cocopy_pool_target(unsigned long saveable)
{
return 0;
@@ -2570,6 +2602,30 @@ int hibernate_preallocate_memory(void)
cocopy_usable_free(), count - max_size, saveable,
max_size);
+ /*
+ * If the stock 1:1 allocation cannot possibly fit, do not spend time in
+ * its reclaim path. Allocate a pool of the pages that are actually
+ * available and let copy_data_pages() fill it. The prediction is
+ * conservative: any doubt keeps the stock path unchanged.
+ */
+ if (snapshot_cocopy_enabled() && !cocopy_init_codec() &&
+ cocopy_prealloc_fails(count - max_size)) {
+ unsigned long target = cocopy_pool_target(saveable);
+
+ pages = (target && !cocopy_alloc_buffers()) ?
+ preallocate_image_pages(target,
+ GFP_IMAGE | __GFP_NORETRY) : 0;
+ if (pages && !cocopy_activate()) {
+ pr_info("Compress-on-copy: predicted short allocation, %lu-page pool\n",
+ pages);
+ goto out;
+ }
+ if (alloc_normal) {
+ cocopy_trim_pool(0);
+ pages = 0;
+ }
+ }
+
/*
* The number of saveable pages in memory was too high, so apply some
* pressure to decrease it. First, make room for the largest possible
--
2.43.0