From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D12DC606A31A for ; Tue, 1 Dec 2026 02:04:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=XJ+MlQb4; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cyqZuYrg DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=GWY0S2gG17jmDZunPNZ868MideC54J2WxqALIikCPz8=; b=XJ+MlQb4ONBg+E ZEt4LzItRVFxMQI57WGAgCyHfakVa/uhVgqF3a46jxGAhvgg+iEZqs6yS7TO1k3+OtNQ5BFd1Ljfm 9V2fBhtEGNNJQ+5M18edb/4jmTwxcKhjUc5WBwgwixk08VpsDZ9rX2TCvX4yD75Mhf5VhToaZTnch eYR75HDLR9c/ovWmVY7d2+ngujBKSxNb9yd5TdhxuUhEicJSUjQ/Pmmw0hIGHFN8hqkeFRMKxJKcc JU16FJ25jAZ75ZW36FvDKjln2RTLvwf2LH8lnpwfASzhO5JAxLi033iWmXMPOh2my+Z/FJiPjB0x+ qZqcooxb2AHh+79o9SXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYD49-00000006E7G-1xGA; Tue, 01 Dec 2026 01:53:57 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYD3J-00000006DQg-1UXz for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 01:53:06 +0000 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-4ac08f4a0b2so7840931f8f.3 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=lists.infradead.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=cyqZuYrg8kZKj8PHoEBf98ONLin7DpqoI116/oFIQ96axFhIweTBF2+PjRe7UYVSAU R+lo+ORvFDwZTvayjqvIWUB6Q6bsB9X1o1DOI9MKs/TTwGP2J/Sb/oYJMCvpPgais9Co PbbYDKtMwtMRZf0LCWONmsa4e9CexgHUOSUO5OLdG/HItIYG1pdhVh8mYc1BTeNBFacT LzYp8zStvrgJvo0jezZ6v4w6wk1j378QDfAlkYK8RAuBhaR2rQX0Vct/UATxSoxlw7UI vpWMwF5ZVxBZO1BkYQL1CqwAMZq3nq9GYNnHYSryy4WoODQiOsFT4LlfHloIEDfrxLL5 k1sw== 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=c1b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TqcRIfaLhE1Eqd XlaulzsClDlb280rXHVWlgGnxO7Bw2pfZQF8zA== 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261130_175305_462095_DC16434D X-CRM114-Status: GOOD ( 27.42 ) X-Spam-Score: -1.9 (-) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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; }