All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Curtin <ericcurtin17@gmail.com>
To: linux-arm-kernel@lists.infradead.org
Cc: Ryan Roberts <ryan.roberts@arm.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Len Brown <lenb@kernel.org>, Pavel Machek <pavel@kernel.org>,
	linux-pm@vger.kernel.org
Subject: [RFC PATCH v2 10/43] PM: hibernate: Do not size swap structures to exactly PAGE_SIZE
Date: Tue,  1 Dec 2026 01:51:42 +0000	[thread overview]
Message-ID: <20261201015240.1017259-11-ericcurtin17@gmail.com> (raw)
In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com>

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;
 }
 

  parent reply	other threads:[~2026-12-01  1:53 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
2026-12-01  1:51 ` Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` [RFC PATCH v2 03/43] mm: page_frag: Use PAGE_SIZE_MIN/MAX limits in page_frag_cache Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 04/43] mm: page_frag: Add _MIN/_MAX variants of PAGE_FRAG_CACHE_MAX_SIZE Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 05/43] mm: Avoid split pmd ptl if pmd level is run-time folded Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 06/43] mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 08/43] cgroup: Use DEFINE_GLOBAL_PAGE_SIZE_VAR for the kernfs_ops tables Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 09/43] bpf: Do not embed page-aligned members in struct bpf_ringbuf Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` Eric Curtin [this message]
2026-12-01  1:51 ` [RFC PATCH v2 11/43] perf/core: Handle a boot-time PAGE_SIZE in format attrs and mlock Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 12/43] KVM: Check struct kvm_run against PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 13/43] crash: Check crash_notes size " Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 14/43] arm64: Make INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE usable from C Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 15/43] arm64: pi: Pass the early pgtable allocation limit to map_range() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 16/43] arm64: Introduce macros required for boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 17/43] arm64: Parameterise early pgtable size macros by page shift Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 18/43] arm64: Add arm64.pagesize= command line override Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 19/43] arm64: pi: Select the page size at runtime during early boot Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 20/43] arm64: pi: Clear the FDT ptes on every remap in map_fdt() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 21/43] arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 22/43] arm64: Align image sections to PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 23/43] arm64: Map the trampoline rodata at its aligned offset Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 24/43] arm64: fixmap: Size slots and page tables for PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 25/43] arm64: Size and align the zero page and FDT ptes for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 26/43] arm64: Convert KVM and hugetlb size switches to if/else Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` [RFC PATCH v2 27/43] arm64: mm: Make page size dependent BUILD_BUG_ON() checks runtime Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 28/43] arm64: cpufeature: Add capabilities for the page size in use Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 29/43] arm64: Look up the page size at runtime in assembly code Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:52 ` [RFC PATCH v2 31/43] arm64: Support runtime folding in idmap_kpti_install_ng_mappings Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 32/43] arm64: Patch TRAMP_VALIAS into instructions at boot Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern at boot Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 35/43] mm/zsmalloc: Size static limits for the worst-case page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 36/43] arm64: Convert remaining page-size switches to if/else Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:52 ` [RFC PATCH v2 37/43] vdso: Lay out the data pages in VDSO_PAGE_SIZE units Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 38/43] mm: Use fixed sizes for shmem str_dup and swap cache PFN bits Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 39/43] crash: Size the vmcoreinfo page array for the smallest page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 40/43] arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 41/43] exit: Use THREAD_SIZE_MAX for lowest_to_date Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 42/43] tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page Eric Curtin
2026-12-01 13:15   ` Masami Hiramatsu (Google)
2026-12-01  1:52 ` [RFC PATCH v2 43/43] arm64: Enable boot-time page size selection Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01 10:41 ` [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Lorenzo Stoakes (ARM)
2026-12-01 10:41   ` Lorenzo Stoakes (ARM)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201015240.1017259-11-ericcurtin17@gmail.com \
    --to=ericcurtin17@gmail.com \
    --cc=lenb@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pavel@kernel.org \
    --cc=rafael@kernel.org \
    --cc=ryan.roberts@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.