[RFC PATCH 4/6] PM: hibernate: write compress-on-copy records directly

From: Nhat-Trieu Huynh-Pham

Date: Sat Nov 28 2026 - 14:32:08 EST


The compress-on-copy pool already holds compressed data records. The stock
compressed writer, however, reads the decompressed stream from
snapshot_read_next() and compresses it again. That second pass wastes CPU
in swap-out and does not use the pool layout.

Write the pool records directly in the existing compressed image format
using a single direct-map bounce page so swap_write_page() never receives a
vmalloc staging address in its synchronous I/O fallback path. The header
and metadata pages still go through snapshot_read_next(); the metadata
chunks are compressed while writing. The data records are then emitted
from the pool as a size_t compressed length followed by the compressed
bytes and page padding. The writer sets reqd_free_pages(), reports the
total compressed payload bytes in kbytes to match the stock compressed
writer, and reuses swap_write_page(), hib_bio_batch and CMP_*/UNC_*.

The CRC still covers the uncompressed metadata and data stream, as required
by the on-disk format. It is updated from the metadata pages and from each
record as it is decompressed for validation. This keeps the resume path
and the image header unchanged.

hibernate=nocompress continues to use the stock raw writer and decompresses
the pool through snapshot_read_next().

Signed-off-by: Nhat-Trieu Huynh-Pham <trieu2.huynh@xxxxxxx>
---
Documentation/power/swsusp.rst | 6 +-
kernel/power/power.h | 29 +++++++
kernel/power/snapshot.c | 42 +++++++--
kernel/power/swap.c | 152 ++++++++++++++++++++++++++++++++-
4 files changed, 216 insertions(+), 13 deletions(-)

diff --git a/Documentation/power/swsusp.rst b/Documentation/power/swsusp.rst
index b6fcf0448f68..0ab7bc6173de 100644
--- a/Documentation/power/swsusp.rst
+++ b/Documentation/power/swsusp.rst
@@ -79,8 +79,10 @@ that holds 32-page chunks compressed with the hibernation codec. The pool is
taken from the copy pages already marked in ``copy_bm``; no pointer array and
no separate pool allocator are used. ``copy_data_pages()`` compresses each
chunk while creating the image and ``snapshot_read_next()`` decompresses it
-for the writer. The on-disk image, the resume path and the CRC layout are
-unchanged.
+for the writer. For compressed images the pool records are written directly
+in the existing length-prefixed format; only the metadata pages are
+compressed while writing. The on-disk image, the resume path and the CRC
+layout are unchanged.

Enable it with ``hibernate=compress_on_copy``. With ``on``, the stock
copy-page allocation is tried first. If it completes, the stock image is
diff --git a/kernel/power/power.h b/kernel/power/power.h
index a361a8eaa9af..436e097b8675 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -181,6 +181,7 @@ struct snapshot_handle {
extern unsigned int snapshot_additional_pages(struct zone *zone);
extern unsigned long snapshot_get_image_size(void);
extern int snapshot_read_next(struct snapshot_handle *handle);
+unsigned int snapshot_nr_meta_pages(void);

/* kernel/power/snapshot.c: compress-on-copy pool */
#ifdef CONFIG_HIBERNATION_COMPRESS_ON_COPY
@@ -188,6 +189,13 @@ bool snapshot_cocopy_enabled(void);
int snapshot_cocopy_set_enabled(bool enable);
bool snapshot_cocopy_active(void);
void snapshot_cocopy_finish(int error);
+void snapshot_cocopy_read_start(void);
+int snapshot_cocopy_next_record(const void **record, u32 *len);
+void *snapshot_cocopy_unc(void);
+int snapshot_cocopy_compress(const void *src, unsigned int len,
+ const void **record, u32 *dst_len);
+int snapshot_cocopy_decompress(const void *src, unsigned int len, void *dst,
+ unsigned int *dst_len);
void snapshot_cocopy_release(void);
#else
static inline bool snapshot_cocopy_enabled(void) { return false; }
@@ -199,6 +207,27 @@ static inline int snapshot_cocopy_set_enabled(bool enable)

static inline bool snapshot_cocopy_active(void) { return false; }
static inline void snapshot_cocopy_finish(int error) {}
+static inline void snapshot_cocopy_read_start(void) {}
+
+static inline int snapshot_cocopy_next_record(const void **record, u32 *len)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void *snapshot_cocopy_unc(void) { return NULL; }
+
+static inline int snapshot_cocopy_compress(const void *src, unsigned int len,
+ const void **record, u32 *dst_len)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int snapshot_cocopy_decompress(const void *src, unsigned int len,
+ void *dst, unsigned int *dst_len)
+{
+ return -EOPNOTSUPP;
+}
+
static inline void snapshot_cocopy_release(void) {}
#endif
extern int snapshot_write_next(struct snapshot_handle *handle);
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index 92ba3706366d..45be309065f9 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -1664,6 +1664,11 @@ bool snapshot_cocopy_active(void)
return READ_ONCE(cocopy.active);
}

+void *snapshot_cocopy_unc(void)
+{
+ return cocopy.unc;
+}
+
static int cocopy_init_codec(void)
{
const char *name = hib_comp_algo[0] ? hib_comp_algo :
@@ -1706,8 +1711,23 @@ static int cocopy_compress(const void *src, unsigned int len, u32 *dst_len)
return 0;
}

-static int cocopy_decompress(const void *src, unsigned int len, void *dst,
- unsigned int *dst_len)
+/* *record points into cocopy.cmp until the next compress or record read. */
+int snapshot_cocopy_compress(const void *src, unsigned int len,
+ const void **record, u32 *dst_len)
+{
+ int ret;
+
+ ret = cocopy_compress(src, len, dst_len);
+ if (ret)
+ return ret;
+
+ *(size_t *)cocopy.cmp = *dst_len;
+ *record = cocopy.cmp;
+ return 0;
+}
+
+int snapshot_cocopy_decompress(const void *src, unsigned int len, void *dst,
+ unsigned int *dst_len)
{
if (cocopy.codec == COCOPY_LZO) {
size_t out = UNC_SIZE;
@@ -2004,7 +2024,7 @@ static void cocopy_read_reset(void)
cocopy.read_started = false;
}

-static void cocopy_read_start(void)
+void snapshot_cocopy_read_start(void)
{
memory_bm_position_reset(&copy_bm);
cocopy.read_pfn = memory_bm_next_pfn(&copy_bm);
@@ -2015,7 +2035,8 @@ static void cocopy_read_start(void)
cocopy.read_started = true;
}

-static int cocopy_next_record(const void **record, u32 *len)
+/* *record points into cocopy.cmp until the next compress or record read. */
+int snapshot_cocopy_next_record(const void **record, u32 *len)
{
size_t record_len;
u32 cmp_len;
@@ -2050,14 +2071,14 @@ static int cocopy_read_next_page(void *dst)
int error;

if (!cocopy.read_started)
- cocopy_read_start();
+ snapshot_cocopy_read_start();

if (cocopy.read_chunk_pos >= cocopy.read_chunk_pages) {
- error = cocopy_next_record(&record, &len);
+ error = snapshot_cocopy_next_record(&record, &len);
if (error)
return error;
- error = cocopy_decompress(record + CMP_HEADER, len,
- cocopy.unc, &unc_len);
+ error = snapshot_cocopy_decompress(record + CMP_HEADER, len,
+ cocopy.unc, &unc_len);
if (error)
return error;
if (!unc_len || unc_len > UNC_SIZE ||
@@ -2848,6 +2869,11 @@ unsigned long snapshot_get_image_size(void)
return nr_copy_pages + nr_meta_pages + 1;
}

+unsigned int snapshot_nr_meta_pages(void)
+{
+ return nr_meta_pages;
+}
+
static int init_header(struct swsusp_info *info)
{
memset(info, 0, sizeof(struct swsusp_info));
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index 09eb3843dffe..9a1c60e7f7da 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -951,6 +951,148 @@ static int save_compressed_image(struct swap_map_handle *handle,
return ret;
}

+/*
+ * Write a compress-on-copy image. The metadata pages are compressed here;
+ * the data records were compressed in copy_data_pages() and are written from
+ * the pool without another compression pass. The CRC still covers the
+ * uncompressed metadata and data stream, so it is computed while the records
+ * are decompressed for validation.
+ */
+#ifdef CONFIG_HIBERNATION_COMPRESS_ON_COPY
+static int save_cocopy_image(struct swap_map_handle *handle,
+ struct snapshot_handle *snapshot,
+ unsigned int nr_to_write)
+{
+ unsigned int meta_pages = snapshot_nr_meta_pages();
+ unsigned int done = 0, data_pages = 0, nr_pages = 0;
+ unsigned int i, expected, m = max(1U, nr_to_write / 10);
+ char *unc = snapshot_cocopy_unc();
+ struct hib_bio_batch hb;
+ u64 compressed_bytes = 0;
+ unsigned char *page;
+ const void *record;
+ ktime_t start, stop;
+ u32 len, crc = 0;
+ int ret = 0, err2;
+
+ if (WARN_ON_ONCE(nr_to_write < meta_pages))
+ return -EINVAL;
+ expected = nr_to_write - meta_pages;
+
+ page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
+ if (!page) {
+ pr_err("Failed to allocate bounce page\n");
+ return -ENOMEM;
+ }
+
+ hib_init_batch(&hb);
+ handle->crc32 = 0;
+ handle->reqd_free_pages = reqd_free_pages();
+ pr_info("Saving compress-on-copy image data (%u pages)...\n",
+ nr_to_write);
+ start = ktime_get();
+
+ while (done < meta_pages) {
+ unsigned int n = min_t(unsigned int, UNC_PAGES,
+ meta_pages - done);
+
+ for (i = 0; i < n; i++) {
+ ret = snapshot_read_next(snapshot);
+ if (ret <= 0) {
+ if (!ret)
+ ret = -ENODATA;
+ goto out_finish;
+ }
+ memcpy(unc + i * PAGE_SIZE, data_of(*snapshot),
+ PAGE_SIZE);
+ crc = crc32_le(crc, data_of(*snapshot), PAGE_SIZE);
+ if (!(nr_pages % m))
+ pr_info("Image saving progress: %3d%%\n",
+ nr_pages / m * 10);
+ nr_pages++;
+ }
+
+ ret = snapshot_cocopy_compress(unc, n * PAGE_SIZE,
+ &record, &len);
+ if (ret) {
+ pr_err("Compress-on-copy: metadata compression failed\n");
+ goto out_finish;
+ }
+ compressed_bytes += len;
+ ret = write_compressed_record(handle, page, record, len, &hb);
+ if (ret)
+ goto out_finish;
+ done += n;
+ }
+
+ /* Read compressed data records directly from the pool. */
+ snapshot_cocopy_read_start();
+ while ((ret = snapshot_cocopy_next_record(&record, &len)) == 0) {
+ unsigned int unc_len, chunk_pages;
+
+ ret = snapshot_cocopy_decompress(record + CMP_HEADER, len,
+ unc, &unc_len);
+ if (ret || !unc_len || (unc_len & (PAGE_SIZE - 1))) {
+ ret = -EINVAL;
+ goto out_finish;
+ }
+ chunk_pages = unc_len / PAGE_SIZE;
+ if (data_pages + chunk_pages > expected) {
+ ret = -EINVAL;
+ goto out_finish;
+ }
+
+ compressed_bytes += len;
+ ret = write_compressed_record(handle, page, record, len, &hb);
+ if (ret)
+ goto out_finish;
+
+ for (i = 0; i < chunk_pages; i++) {
+ if (!(nr_pages % m))
+ pr_info("Image saving progress: %3d%%\n",
+ nr_pages / m * 10);
+ nr_pages++;
+ }
+
+ crc = crc32_le(crc, unc, unc_len);
+ data_pages += chunk_pages;
+ }
+ if (ret != -ENODATA)
+ goto out_finish;
+ if (data_pages != expected) {
+ ret = -ENODATA;
+ goto out_finish;
+ }
+
+ handle->crc32 = crc;
+ ret = 0;
+
+out_finish:
+ err2 = hib_wait_io(&hb);
+ hib_finish_batch(&hb);
+ free_page((unsigned long)page);
+ stop = ktime_get();
+ if (!ret)
+ ret = err2;
+ if (!ret) {
+ swsusp_show_speed(start, stop, nr_to_write, "Wrote");
+ pr_info("Image size after compression: %llu kbytes\n",
+ compressed_bytes >> 10);
+ pr_info("Image saving done\n");
+ } else {
+ pr_err("Image saving failed: %d\n", ret);
+ }
+ return ret;
+}
+#else /* !CONFIG_HIBERNATION_COMPRESS_ON_COPY */
+static inline int save_cocopy_image(struct swap_map_handle *handle,
+ struct snapshot_handle *snapshot,
+ unsigned int nr_to_write)
+{
+ return -EOPNOTSUPP;
+}
+#endif /* !CONFIG_HIBERNATION_COMPRESS_ON_COPY */
+
static int enough_swap(unsigned int nr_pages)
{
unsigned int free_swap = count_swap_pages(root_swap, 1);
@@ -1005,9 +1147,13 @@ int swsusp_write(unsigned int flags)
header = (struct swsusp_info *)data_of(snapshot);
error = swap_write_page(&handle, header, NULL);
if (!error) {
- error = (flags & SF_NOCOMPRESS_MODE) ?
- save_image(&handle, &snapshot, pages - 1) :
- save_compressed_image(&handle, &snapshot, pages - 1);
+ if (flags & SF_NOCOMPRESS_MODE)
+ error = save_image(&handle, &snapshot, pages - 1);
+ else if (snapshot_cocopy_active())
+ error = save_cocopy_image(&handle, &snapshot, pages - 1);
+ else
+ error = save_compressed_image(&handle, &snapshot,
+ pages - 1);
}
out_finish:
error = swap_writer_finish(&handle, flags, error);
--
2.43.0