[RFC PATCH 2/6] PM: hibernate: share compression record helpers
From: Nhat-Trieu Huynh-Pham
Date: Sat Nov 28 2026 - 14:31:51 EST
Move the hibernation compression algorithm names (COMPRESSION_ALGO_LZO,
COMPRESSION_ALGO_LZ4) and compressed record sizing macros (UNC_*, CMP_*,
bytes_worst_compress()) into kernel/power/power.h so snapshot.c and
swap.c can share them. Also factor out write_compressed_record() from
save_compressed_image() in kernel/power/swap.c so an upcoming direct
compressed writer can reuse the same bounce-page write loop.
No functional change.
Signed-off-by: Nhat-Trieu Huynh-Pham <trieu2.huynh@xxxxxxx>
---
kernel/power/hibernate.c | 3 ---
kernel/power/power.h | 20 ++++++++++++++++
kernel/power/swap.c | 52 +++++++++++++++++++---------------------
3 files changed, 45 insertions(+), 30 deletions(-)
diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index 2bbfd7ffafe5..535509804f57 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -769,9 +769,6 @@ static int load_image_and_restore(void)
return error;
}
-#define COMPRESSION_ALGO_LZO "lzo"
-#define COMPRESSION_ALGO_LZ4 "lz4"
-
/**
* hibernate - Carry out system hibernation, including saving the image.
*/
diff --git a/kernel/power/power.h b/kernel/power/power.h
index 75b63843886e..67b3a982950b 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -61,6 +61,8 @@ asmlinkage int swsusp_save(void);
/* kernel/power/hibernate.c */
extern bool freezer_test_done;
extern char hib_comp_algo[CRYPTO_MAX_ALG_NAME];
+#define COMPRESSION_ALGO_LZO "lzo"
+#define COMPRESSION_ALGO_LZ4 "lz4"
/* kernel/power/swap.c */
extern unsigned int swsusp_header_flags;
@@ -105,6 +107,24 @@ static struct kobj_attribute _name##_attr = { \
.show = _name##_show, \
}
+/*
+ * Bytes we need for compressed data in worst case. We assume(limitation)
+ * this is the worst of all the compression algorithms.
+ */
+#define bytes_worst_compress(x) ((x) + ((x) / 16) + 64 + 3 + 2)
+
+/* We need to remember how much compressed data we need to read. */
+#define CMP_HEADER sizeof(size_t)
+
+/* Number of pages/bytes we'll compress at one time. */
+#define UNC_PAGES 32
+#define UNC_SIZE (UNC_PAGES * PAGE_SIZE)
+
+/* Number of pages we need for compressed data (worst case). */
+#define CMP_PAGES DIV_ROUND_UP(bytes_worst_compress(UNC_SIZE) + \
+ CMP_HEADER, PAGE_SIZE)
+#define CMP_SIZE (CMP_PAGES * PAGE_SIZE)
+
/* Preferred image size in bytes (default 500 MB) */
extern unsigned long image_size;
/* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index 7e43731414e1..09eb3843dffe 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -530,24 +530,6 @@ static int swap_writer_finish(struct swap_map_handle *handle,
return error;
}
-/*
- * Bytes we need for compressed data in worst case. We assume(limitation)
- * this is the worst of all the compression algorithms.
- */
-#define bytes_worst_compress(x) ((x) + ((x) / 16) + 64 + 3 + 2)
-
-/* We need to remember how much compressed data we need to read. */
-#define CMP_HEADER sizeof(size_t)
-
-/* Number of pages/bytes we'll compress at one time. */
-#define UNC_PAGES 32
-#define UNC_SIZE (UNC_PAGES * PAGE_SIZE)
-
-/* Number of pages we need for compressed data (worst case). */
-#define CMP_PAGES DIV_ROUND_UP(bytes_worst_compress(UNC_SIZE) + \
- CMP_HEADER, PAGE_SIZE)
-#define CMP_SIZE (CMP_PAGES * PAGE_SIZE)
-
/* Default number of threads for compression/decompression. */
#define CMP_THREADS 3
static unsigned int hibernate_compression_threads = CMP_THREADS;
@@ -723,6 +705,26 @@ static int compress_threadfn(void *data)
return 0;
}
+/*
+ * @record must be backed by a PAGE_SIZE-padded buffer (CMP_SIZE); trailing
+ * bytes on the last page are ignored by the reader, which uses @len.
+ */
+static int write_compressed_record(struct swap_map_handle *handle,
+ void *bounce, const void *record, u32 len,
+ struct hib_bio_batch *hb)
+{
+ unsigned int off;
+ int ret;
+
+ for (off = 0; off < CMP_HEADER + len; off += PAGE_SIZE) {
+ memcpy(bounce, record + off, PAGE_SIZE);
+ ret = swap_write_page(handle, bounce, hb);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
static int save_compressed_image(struct swap_map_handle *handle,
struct snapshot_handle *snapshot,
unsigned int nr_to_write)
@@ -903,15 +905,11 @@ static int save_compressed_image(struct swap_map_handle *handle,
* any garbage at the end will be discarded when we
* read it.
*/
- for (off = 0;
- off < CMP_HEADER + data[thr].cmp_len;
- off += PAGE_SIZE) {
- memcpy(page, data[thr].cmp + off, PAGE_SIZE);
-
- ret = swap_write_page(handle, page, &hb);
- if (ret)
- goto out_finish;
- }
+ ret = write_compressed_record(handle, page,
+ data[thr].cmp,
+ data[thr].cmp_len, &hb);
+ if (ret)
+ goto out_finish;
}
wait_event(crc->done, atomic_read_acquire(&crc->stop));
--
2.43.0