Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796089985; b=uh0YwzsaJez4DkXT8xvRNIj2GmkAbp11pfLTr8kDAqDNndRq7sVbH88cG5kP0E16Ft0s Ms1wsWKNPuKLlBon1ZM2O8gUkTd7u2ZEnbQGbIYKwxfLL9Dd1smSzquRV2Hgzd63CnK83 GrYobMwwKaItCAokoNkI7FWaq+HpjypdOVjwdjNyXpQC7iGV2IOTon2yi9CCelaVYrPFB 57+/069qZj0IuHB8T2VsW9m6P1kn+7MwAUdV6v2BbUmEsz7zXn4vu2Yf91v+605oh0GxR /wFqQ/zHfZ93Qwh5eqInEPWk/glCe0i0+J9nGHK3GcJPFRUcbl1Fxe0JJFUypFaiwFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796089985; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=GWY0S2gG17jmDZunPNZ868MideC54J2WxqALIikCPz8=; b=2VkfNCt+EZaVO/jOTURjT7iI2qyRtuHspBaGxiNgM/xI9aX4mpRQyHPIwwSkDYm/0aUH 6k2EusDYyYosD4n85PZuAxh5Xg1gyVdTbm9HbS5QMGLOqv9rPVNEJVGZtAqIyHA2s+zl0 emKELSF20Bn6yESdzsnYve2hPR2rqRrkNZW82JLtSP279b7IwxW7blY+BzpsUpKHXDc1K CjF5FVfI/+CDan93CjA3L28TohB0Z9kHYhVhmGQQJZjQKxZ7noWYyJuMgFWVVedpHoqp1 5a6LMQ6P0kRRcUbBwwY+ANTMcyOiURpM9rUPTivls9RWTvHQB0yQlGhji5Hsx8ncaXQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=IFecxxxn; arc=none smtp.remote-ip=209.85.128.54 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F347F606A31A for ; Tue, 1 Dec 2026 01:53:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F347F606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=IFecxxxn Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4bb85e07aadso137774895e9.1 for ; Mon, 30 Nov 2026 17:53:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796089983; x=1796694783; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=GWY0S2gG17jmDZunPNZ868MideC54J2WxqALIikCPz8=; b=IFecxxxnbt8fWRLlTiZoqs9GU+eC8d47GZA3nmLqlJ7XZsezPAKQg0F3/M47I4Nagy Y1nvYKVUH0761hBGFRoOfBmwSYlpx7rqmVGOk2eH+u107raTzQtG+WukJ5csmk80KB3Z cVFzEIPULLuw5OeQBHJKPSfxE+o7FtGuASJ19G4+ZxV0aoY7Cao69Ocktzywbqma0/0W OZAGbhaPHGBJSZSBvXemyfykXKpAkvnbiwfDVuXOUvzPbOUuE1VethZeRt4Z0QUYfA5J uEXuRlOfBzISjJmrgclOwXuyOAGCOb3yFqxp3+sE4l1zB4FyFIoslTcUHFO07WyVGpzz h8dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796089983; x=1796694783; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=GWY0S2gG17jmDZunPNZ868MideC54J2WxqALIikCPz8=; b=heinr1bQk7kPnkx0q1Aq11ZBfDKI79goifoO0JnlN73nONft9lfrVF2/tHqaU0AYru 79LMo3Kvd+dZq2x+LNV7OdZ4FwGJ2JNRAp7T6ItyCDP+Q57jgOrMMRtkpRfsd9buKEs+ CPnueUZysulBGIspj3r1klC1snmFVdMU7BNOCzCVOUX9lhDs3oGUmteGG3dV3yzS36Jd aSXV319zTDnog8xkwHhRRFyTI6wXLQvEeBmHh25z/ZmnwnftS6L1p9ibWUGz2crExzKV nZpjnObr/U3P/uucnmdSo4BrMCA69iNwZFiuQZakonxtQBhb44DKv6wLbPf5tK+QPXVv UCdg== X-Forwarded-Encrypted: i=1; AL+hErH8LNBgOFssZUkxPHdPt6nXdrd6IiKf5kKpR5UDxXprvhx8jNwnXDg1muKbUrBowZiIQS/g7DCXwg==@vger.kernel.org X-Gm-Message-State: APuU4Z9zlWnEOtCFp6tkT94s8V1cO/hzfsLHVNK77sW502s5jNF8z4SI tJTw8/QgGtjuIzZbPrZs88Dq4p2pjQy3F1hdGMqSqdwhG8OTzIsOmusf X-Gm-Gg: AZFdTZcAseRdBlxHa4O6WUHZn1sKXIkGcf83Tk+IY/hp6tcKEW+GyMGD+HBOnaWwjfG tJntHQYlEVLOJnQTfpqrcj19F18Dn4oRo8KXiveHsHNdJ2AB4l6PjCA4h+cPWSwAAzrEtLIApmR vCUCh5kENRpMdG6EKkhiWatpi2stj7Sj4OjHHWmG/8KHquZc7MSZXedJdJ00SxtfrDp7YMTR5L5 oYC/nYYX0K4ATrpdNS9tnd8WHMbmfDhhbilUCr0lF2xfGxgDs93k3vOTBeIaTjqVaLJ4/oQf1l2 yeRI0+0laZjsA+zsh5tkmCDp7sQRREUoz43FkAqcrO7KubsTnNECIaH7GlXw+xe7oUH/D9IpMKB pcsF2xt31373qRlgodPAgoYD4y7XgtDcDkoyDJzjCljT7mrYENZ/sQ/l/CHYdbsST+64Z88Emh0 O5c/NrkNtq5aXRjLPEo+eU7gigPH6zbVQS0aq+fH/UAv0hcn+GC+uiYZTOVykNFqH2rCbs413Mb 1x/6177wrG7A+xAMUB9FTB7vi5yObwQzLYgCikEAmTmbutZ/clqpD/BpXshZ6wc0B2nA+bBjNZS 7eqwEfCLmaQQ1Gu1The1y5FKbaUjcAX+kxNgFw== X-Received: by 2002:a05:600c:154c:b0:4a2:1364:252a with SMTP id 5b1f17b1804b1-4bb887c28a8mr693363075e9.3.1796089983194; Mon, 30 Nov 2026 17:53:03 -0800 (PST) Received: from NULL.Home.net ([2001:8a0:7280:4000:12d:c3b9:cecc:8966]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884911e3sm1199037055e9.6.2026.11.30.17.53.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 17:53:02 -0800 (PST) From: Eric Curtin To: linux-arm-kernel@lists.infradead.org Cc: Ryan Roberts , "Rafael J. Wysocki" , Len Brown , Pavel Machek , 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 Message-ID: <20261201015240.1017259-11-ericcurtin17@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com> References: <20261201015240.1017259-1-ericcurtin17@gmail.com> X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ryan Roberts 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 Signed-off-by: Eric Curtin (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; }