* [RFC PATCH v2 10/43] PM: hibernate: Do not size swap structures to exactly PAGE_SIZE
[not found] <20261201015240.1017259-1-ericcurtin17@gmail.com>
@ 2026-12-01 1:51 ` Eric Curtin
0 siblings, 0 replies; only message in thread
From: Eric Curtin @ 2026-12-01 1:51 UTC (permalink / raw)
To: linux-arm-kernel
Cc: Ryan Roberts, Rafael J. Wysocki, Len Brown, Pavel Machek,
linux-pm
From: Ryan Roberts <ryan.roberts@arm.com>
struct linked_page, struct swap_map_page, struct swsusp_header and
struct swsusp_info are sized to exactly PAGE_SIZE, and struct cmp_data
and struct dec_data embed buffers sized from PAGE_SIZE. With boot-time
page size selection PAGE_SIZE is not a compile-time constant, so none of
this can be expressed in the type definitions.
Remove the padding and superimpose the structures on a page at run time:
- linked_page.data becomes a flexible array.
- swap_map_page.entries is sized with PAGE_SIZE_MAX, and the next_swap
link lives in entries[NEXT_SWAP_INDEX], the last slot of the page.
- swsusp_header loses its reserved[] member and is placed at the end of
the page held in swsusp_header_pg.
- swsusp_info loses __aligned(PAGE_SIZE); init_header() clears a full
page instead.
- cmp_data and dec_data hold pointers to unc and cmp buffers, with new
alloc/free helpers for each.
Because an image is only valid for the page size it was written with,
record page_shift in swsusp_info and make check_header() reject an image
whose page size does not match.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit e1d1db39b074b85e3d043190f3c59d67ffcf1136)
---
kernel/power/power.h | 3 +-
kernel/power/snapshot.c | 10 +++-
kernel/power/swap.c | 129 +++++++++++++++++++++++++++++++++-------
3 files changed, 115 insertions(+), 27 deletions(-)
diff --git a/kernel/power/power.h b/kernel/power/power.h
index 75b63843886e..e7bbf94fe00d 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -16,7 +16,8 @@ struct swsusp_info {
unsigned long image_pages;
unsigned long pages;
unsigned long size;
-} __aligned(PAGE_SIZE);
+ unsigned int page_shift;
+};
#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
extern int pm_sleep_fs_sync(void);
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index d5dba0e50b2e..af49d749aa66 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -155,7 +155,7 @@ struct pbe *restore_pblist;
struct linked_page {
struct linked_page *next;
- char data[LINKED_PAGE_DATA_SIZE];
+ char data[];
} __packed;
/*
@@ -2179,12 +2179,13 @@ unsigned long snapshot_get_image_size(void)
static int init_header(struct swsusp_info *info)
{
- memset(info, 0, sizeof(struct swsusp_info));
+ memset(info, 0, PAGE_SIZE);
info->num_physpages = get_num_physpages();
info->image_pages = nr_copy_pages;
info->pages = snapshot_get_image_size();
info->size = info->pages;
info->size <<= PAGE_SHIFT;
+ info->page_shift = PAGE_SHIFT;
return init_header_complete(info);
}
@@ -2320,7 +2321,10 @@ static int check_header(struct swsusp_info *info)
{
const char *reason;
- reason = check_image_kernel(info);
+ if (info->page_shift != PAGE_SHIFT)
+ reason = "page size";
+ else
+ reason = check_image_kernel(info);
if (!reason && info->num_physpages != get_num_physpages())
reason = "memory size";
if (reason) {
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index c78f1593600b..4ae766593081 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -59,6 +59,7 @@ static bool clean_pages_on_decompress;
* During resume we pick up all swap_map_page structures into a list.
*/
#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
+#define NEXT_SWAP_INDEX MAP_PAGE_ENTRIES
/*
* Number of free pages that are not high.
@@ -78,8 +79,11 @@ static inline unsigned long reqd_free_pages(void)
}
struct swap_map_page {
- sector_t entries[MAP_PAGE_ENTRIES];
- sector_t next_swap;
+ /*
+ * Only the first PAGE_SIZE bytes (PAGE_SIZE / sizeof(sector_t) entries)
+ * are used. The last one, [NEXT_SWAP_INDEX], is `.next_swap`.
+ */
+ sector_t entries[PAGE_SIZE_MAX / sizeof(sector_t)];
};
struct swap_map_page_list {
@@ -101,8 +105,6 @@ struct swap_map_handle {
};
struct swsusp_header {
- char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
- sizeof(u32) - sizeof(u32)];
u32 hw_sig;
u32 crc32;
sector_t image;
@@ -111,6 +113,7 @@ struct swsusp_header {
char sig[10];
} __packed;
+static char *swsusp_header_pg;
static struct swsusp_header *swsusp_header;
/*
@@ -303,7 +306,7 @@ static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
{
int error;
- hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header);
+ hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header_pg);
if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
!memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
@@ -317,7 +320,7 @@ static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
if (flags & SF_CRC32_MODE)
swsusp_header->crc32 = handle->crc32;
error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
- swsusp_resume_block, swsusp_header);
+ swsusp_resume_block, swsusp_header_pg);
} else {
pr_err("Swap header not found!\n");
error = -ENODEV;
@@ -441,7 +444,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
if (!offset)
return -ENOSPC;
- handle->cur->next_swap = offset;
+ handle->cur->entries[NEXT_SWAP_INDEX] = offset;
error = write_page(handle->cur, handle->cur_swap, hb);
if (error)
return error;
@@ -653,8 +656,8 @@ struct cmp_data {
wait_queue_head_t done; /* compression done */
size_t unc_len; /* uncompressed length */
size_t cmp_len; /* compressed length */
- unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
- unsigned char cmp[CMP_SIZE]; /* compressed buffer */
+ unsigned char *unc; /* uncompressed buffer */
+ unsigned char *cmp; /* compressed buffer */
};
/* Indicates the image size after compression */
@@ -691,6 +694,45 @@ static int compress_threadfn(void *data)
return 0;
}
+static void free_cmp_data(struct cmp_data *data, unsigned nr_threads)
+{
+ int i;
+
+ if (!data)
+ return;
+
+ for (i = 0; i < nr_threads; i++) {
+ vfree(data[i].unc);
+ vfree(data[i].cmp);
+ }
+
+ vfree(data);
+}
+
+static struct cmp_data *alloc_cmp_data(unsigned nr_threads)
+{
+ struct cmp_data *data = NULL;
+ int i = -1;
+
+ data = vcalloc(nr_threads, sizeof(*data));
+ if (!data)
+ goto fail;
+
+ for (i = 0; i < nr_threads; i++) {
+ data[i].unc = vzalloc(UNC_SIZE);
+ if (!data[i].unc)
+ goto fail;
+ data[i].cmp = vzalloc(CMP_SIZE);
+ if (!data[i].cmp)
+ goto fail;
+ }
+
+ return data;
+fail:
+ free_cmp_data(data, nr_threads);
+ return NULL;
+}
+
static int save_compressed_image(struct swap_map_handle *handle,
struct snapshot_handle *snapshot,
unsigned int nr_to_write)
@@ -726,7 +768,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
goto out_clean;
}
- data = vcalloc(nr_threads, sizeof(*data));
+ data = alloc_cmp_data(nr_threads);
if (!data) {
pr_err("Failed to allocate %s data\n", hib_comp_algo);
ret = -ENOMEM;
@@ -913,7 +955,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
if (!IS_ERR_OR_NULL(data[thr].cc))
crypto_free_acomp(data[thr].cc);
}
- vfree(data);
+ free_cmp_data(data, nr_threads);
}
if (page)
free_page((unsigned long)page);
@@ -1042,7 +1084,7 @@ static int get_swap_reader(struct swap_map_handle *handle,
release_swap_reader(handle);
return error;
}
- offset = tmp->map->next_swap;
+ offset = tmp->map->entries[NEXT_SWAP_INDEX];
}
handle->k = 0;
handle->cur = handle->maps->map;
@@ -1154,8 +1196,8 @@ struct dec_data {
wait_queue_head_t done; /* decompression done */
size_t unc_len; /* uncompressed length */
size_t cmp_len; /* compressed length */
- unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
- unsigned char cmp[CMP_SIZE]; /* compressed buffer */
+ unsigned char *unc; /* uncompressed buffer */
+ unsigned char *cmp; /* compressed buffer */
};
static int decompress_threadfn(void *data)
@@ -1192,6 +1234,45 @@ static int decompress_threadfn(void *data)
return 0;
}
+static void free_dec_data(struct dec_data *data, unsigned nr_threads)
+{
+ int i;
+
+ if (!data)
+ return;
+
+ for (i = 0; i < nr_threads; i++) {
+ vfree(data[i].unc);
+ vfree(data[i].cmp);
+ }
+
+ vfree(data);
+}
+
+static struct dec_data *alloc_dec_data(unsigned nr_threads)
+{
+ struct dec_data *data = NULL;
+ int i = -1;
+
+ data = vcalloc(nr_threads, sizeof(*data));
+ if (!data)
+ goto fail;
+
+ for (i = 0; i < nr_threads; i++) {
+ data[i].unc = vzalloc(UNC_SIZE);
+ if (!data[i].unc)
+ goto fail;
+ data[i].cmp = vzalloc(CMP_SIZE);
+ if (!data[i].cmp)
+ goto fail;
+ }
+
+ return data;
+fail:
+ free_dec_data(data, nr_threads);
+ return NULL;
+}
+
static int load_compressed_image(struct swap_map_handle *handle,
struct snapshot_handle *snapshot,
unsigned int nr_to_read)
@@ -1228,7 +1309,7 @@ static int load_compressed_image(struct swap_map_handle *handle,
goto out_clean;
}
- data = vcalloc(nr_threads, sizeof(*data));
+ data = alloc_dec_data(nr_threads);
if (!data) {
pr_err("Failed to allocate %s data\n", hib_comp_algo);
ret = -ENOMEM;
@@ -1513,7 +1594,7 @@ static int load_compressed_image(struct swap_map_handle *handle,
if (!IS_ERR_OR_NULL(data[thr].cc))
crypto_free_acomp(data[thr].cc);
}
- vfree(data);
+ free_dec_data(data, nr_threads);
}
vfree(page);
@@ -1574,9 +1655,9 @@ int swsusp_check(bool exclusive)
hib_resume_bdev_file = bdev_file_open_by_dev(swsusp_resume_device,
BLK_OPEN_READ, holder, NULL);
if (!IS_ERR(hib_resume_bdev_file)) {
- clear_page(swsusp_header);
+ clear_page(swsusp_header_pg);
error = hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block,
- swsusp_header);
+ swsusp_header_pg);
if (error)
goto put;
@@ -1586,7 +1667,7 @@ int swsusp_check(bool exclusive)
/* Reset swap signature now */
error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
swsusp_resume_block,
- swsusp_header);
+ swsusp_header_pg);
} else {
error = -EINVAL;
}
@@ -1635,12 +1716,12 @@ int swsusp_unmark(void)
{
int error;
- hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header);
+ hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header_pg);
if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
swsusp_resume_block,
- swsusp_header);
+ swsusp_header_pg);
} else {
pr_err("Cannot find swsusp signature!\n");
error = -ENODEV;
@@ -1695,9 +1776,11 @@ static int __init swsusp_header_init(void)
if (error)
return -ENOMEM;
- swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
- if (!swsusp_header)
+ swsusp_header_pg = (char *)__get_free_page(GFP_KERNEL);
+ if (!swsusp_header_pg)
panic("Could not allocate memory for swsusp_header\n");
+ swsusp_header = (struct swsusp_header *)(swsusp_header_pg +
+ PAGE_SIZE - sizeof(struct swsusp_header));
return 0;
}
^ permalink raw reply related [flat|nested] only message in thread