[RFC PATCH 0/6] PM: hibernate: introduce compress-on-copy to support low memory-constrained devices
From: Nhat-Trieu Huynh-Pham
Date: Sat Nov 28 2026 - 14:31:52 EST
Motivation
==========
Hibernation is a useful feature: it saves power and allows a device to
resume its state quickly after wake-up. However, in most cases, it
requires roughly half of RAM to be free before the system can enter
hibernation successfully. This is because hibernation makes a 1:1
copy of every page to be saved prior to the atomic copy. On the save
side, the original pages and their copies must coexist, which is why
the stock path needs ~50% free RAM [1] and why a memory-constrained system
can fail to save an image that would easily fit on swap after
compression.
This is a practical problem on low-memory Android devices. For instance,
a 3 GiB SA4150P device running Android 12 hibernated reliably, but after
upgrading to Android 14, additional OS features increased the system
working set, causing hibernation to fail consistently. Proactive reclaim
before sleeping via cgroups or dropping caches does not create enough
headroom, as the remaining unreclaimable pages still prevent the 1:1
copy. The same failure is rarer on a 4 GiB SA6155P device, though not
eliminated. When it happens, the missing image forces a cold boot (or
security reboot), resulting in a noticeable boot delay (e.g., prolonged
black screen) in that such cases, degrading the user experience.
The 50% free-memory requirement is a property of the save-side copy
preallocation, not of resume. The resume kernel boots with very little
memory in use, and prepare_image() writes image pages directly to their
original page frames. The copy being replaced here is therefore only
the transient one on the suspend side. The goal is to preserve the
existing image and resume formats while optionally eliminating the
save-side 1:1 copy requirement.
Implementation
==============
The naive idea is straightforward as it is. We honor the stock's
preallocation when it can satisfy the image (either controlled by
image_size by user or by 2/5 by default [1]), if it comes up short, reuse
the pages it already allocated as a compressed pool and compress the image
into that pool. The save-side free-memory requirement then falls from
`S + R`, roughly 50% of RAM when `S` is about half of RAM, to `r * S + R`,
where `r` is the compression ratio and `R` is the I/O, driver and
watermark reserve. On QEMU the realistic ~2:1 mixed workload needs
~31-32% of RAM instead of ~59-63%; highly compressible text needs 2.6%,
and the 3 GiB hardware fallback needs 7.5%, instead of a 1:1 copy.
The series adds CONFIG_HIBERNATION_COMPRESS_ON_COPY (default n, depends
on HIBERNATION && !HIGHMEM) in six steps:
1. Factor out zone_usable_free_pages() from hibernate_preallocate_memory()
so the upcoming fallback shares the exact MIGRATE_HIGHATOMIC and free
CMA accounting [2].
2. Share the hibernation compression algorithm names and record sizing
macros in power.h, and factor out write_compressed_record() in swap.c.
3. Replace the save-side 1:1 copy with a compressed pool built from the
image pages already marked in copy_bm. copy_data_pages() compresses
page chunks into the pool, snapshot_cocopy_finish() returns unused
tail pages and logs exhaustion in process context, and
snapshot_read_next() decompresses records into its direct-map reader
buffer. The on-disk format, CRC and resume path are unchanged.
4. Write the already-compressed pool records directly in the existing image
format through a single direct-map bounce page, keeping
reqd_free_pages() and reporting compressed payload bytes in kbytes to
match the stock compressed writer.
5. Expose runtime control via /sys/power/compress_on_copy (in addition to
hibernate=compress_on_copy), serialized with system sleep transitions.
6. Skip the stock allocation when post-shrink free and reclaimable pages
(with anonymous pages bounded by get_nr_swap_pages()) cannot cover its
target; if inconclusive, keep stock and fall back only if short.
The whole compressed image must fit in the pool, otherwise hibernation
fails with -ENOMEM before anything is written. Compression runs with
interrupts disabled and secondary CPUs off, so its cost is part of the
atomic-copy interval and must fit the platform watchdog budget.
Testing
=======
(*) QEMU: x86_64/KVM (DEBUG_VM + DEBUG_PAGEALLOC), 4 GiB swap, based on
linux-pm/linux-next with 2 below configs of memory:
3 GiB guest (MemTotal 3,043,500 kB), and
4 GiB guest (MemTotal 4,009,380 kB).
case mem pol comp path result pool/copy used image req RAM hib
MiB MiB kB MiB % s
------------------ --- ---- ---- ------------ ------- --------- ------- --------- ------- ----- -----
mixedr 2048M 4G off LZO stock PASS 1,564.9 - 781,510 1,600.9 40.9 15.63
mixedr 2048M 4G on LZO stock PASS 1,568.4 - 793,009 1,597.6 40.8 7.27
mixedr 2200M mlock 4G off LZO stock FAIL 98,103pg - - ~2,448 ~62.5 0.30
mixedr 2200M mlock 4G on LZO pool-predict PASS 1,613.0 1,226.4 1,257,457 1,265.7 32.3 17.83
mixedr 2200M mlock 4G on LZ4 pool-predict PASS 1,618.3 1,148.6 1,178,406 1,187.9 30.3 3.86
mixedr 2200M mlock 4G on raw pool-predict PASS 1,618.8 1,231.2 2,391,188 1,270.5 32.4 6.52
mixedr 1600M mlock 3G off LZO stock FAIL 57,103pg - - ~1,755 ~59.1 0.25
mixedr 1600M mlock 3G on LZO pool-predict PASS 1,301.4 909.6 932,622 938.9 31.6 2.36
mixedr 2600M pc1G 3G off LZO stock FAIL 53,595pg - - ~1,760 ~59.2 0.44
mixedr 2600M pc1G 3G on LZO pool-predict PASS 1,297.7 878.0 900,253 907.3 30.5 2.17
text 2400M mlock 3G off LZO stock FAIL 256,571pg - - ~2,557 ~86.0 0.16
text 2400M mlock 3G on LZO pool-predict PASS 499.2 47.9 50,789 77.2 2.6 2.34
text 2400M mlock 3G on LZ4 pool-predict PASS 499.9 46.0 49,430 75.3 2.5 2.61
text 2400M mlock 3G on raw pool-predict PASS 503.8 47.7 2,579,132 77.0 2.6 7.52
text 2400M mlock 3G boot LZO pool-predict PASS 489.6 47.7 50,610 77.0 2.6 2.16
random 2300M mlock 3G on LZO pool-predict EXHAUST 586.3 - - ~2,457 ~82.7 1.41
zero 2400M mlock 3G on LZO pool-predict PASS 483.9 30.8 33,467 60.1 2.0 1.21
(*) Hardware: SA6155P, 6 GiB swap, based on AAOS 14 GKI 6.1.145,
with 2 below configs of memory:
3 GiB (MemTotal 2,854,756 kB), and
4 GiB (MemTotal 3,846,060 kB).
case mem pol comp path result pool/copy used image req RAM hib
MiB MiB kB MiB % s
------ --- ---- ---------- -------- ------- --------- ------- --------- ------- ----- -----
device 3G on nocompress fallback PASS 1,416.3 182.0 975,644 208.5 7.5 10.39
device 4G on nocompress stock PASS 1,646.4 - 1,112,588 1,646.4 43.8 13.50
device 3G off nocompress stock FAIL 31,818pg - - ~1,544 55.4 2.12
device 4G off nocompress stock PASS 1,619.5 - 1,128,812 1,619.5 43.1 13.52
(*) Notes:
mixedr = ~2:1 mixed anonymous workload (random + text pages)
pc1G = 1,600 MiB mlocked mixedr + 1,000 MiB clean ext4 page cache
mlock = listed anonymous MiB are mlocked (unreclaimable)
pol = compress-on-copy policy (off, on, boot =
hibernate=compress_on_copy)
comp = hibernation compressor (raw = hibernate=nocompress)
pool/copy = compress-on-copy pool capacity or stock 1:1 copy, MiB
used = compressed pool record bytes actually used, MiB
image kB = image size written to swap (compressed for LZO/LZ4,
uncompressed for raw/nocompress)
pg = pages short from stock 1:1 copy allocation
req/RAM% = estimated save-side free memory requirement (S+R for QEMU
stock and EXHAUST, with S = Need to copy pages slightly
below Allocated in pool/copy; stock 1:1 copy target for
hardware stock/FAIL; r*S+R = used + R for compressed pool)
/ MemTotal
hib s = kernel hibernation time (hibernation entry to PM: S| on
PASS, or to abort hibernation exit on FAIL/EXHAUST)
Limitations
===========
(*) Not a streaming writer: the whole compressed data image must fit in the
pool, otherwise hibernation fails cleanly with -ENOMEM before writing.
(*) Requires CONFIG_HIBERNATION_COMPRESS_ON_COPY (!HIGHMEM) and
supports LZO and LZ4.
The workloads used for testing this series may not cover every required
metric, and some edge cases or side effects introduced by this series may
not be fully covered. Any comments or feedback would be greatly
appreciated.
Tool-assistance
===============
AI coding assistants (LLM) were used during patch drafting, refactoring,
changelog editing and test log summarization; the author reviewed, tested
and takes full responsibility for the series.
References
==========
[1] Documentation/power/swsusp.rst: the stock copy design, image_size and
the 2/5 default.
[2] commit 7005136399e1 ("PM: hibernate: exclude unusable free pages from
image preallocation"): usable-free accounting base.
Nhat-Trieu Huynh-Pham (6):
PM: hibernate: factor out zone_usable_free_pages()
PM: hibernate: share compression record helpers
PM: hibernate: add compress-on-copy snapshot pool
PM: hibernate: write compress-on-copy records directly
PM: hibernate: add compress_on_copy sysfs policy
PM: hibernate: predict image preallocation failure for
compress-on-copy
Documentation/ABI/testing/sysfs-power | 32 +
.../admin-guide/kernel-parameters.txt | 9 +
Documentation/power/swsusp.rst | 48 ++
kernel/power/Kconfig | 19 +
kernel/power/hibernate.c | 46 +-
kernel/power/power.h | 69 ++
kernel/power/snapshot.c | 798 +++++++++++++++++-
kernel/power/swap.c | 204 ++++-
8 files changed, 1161 insertions(+), 64 deletions(-)
base-commit: 76bf07dbbc2513001a041c6d61f747e0a255cef4
--
2.43.0