[RFC PATCH v2 38/43] mm: Use fixed sizes for shmem str_dup and swap cache PFN bits

Eric Curtin ericcurtin17 at gmail.com
Mon Nov 30 17:52:10 PST 2026


From: Eric Curtin <eric.curtin at docker.com>

str_dup in mm/shmem.c is a static buffer sized with PAGE_SIZE, and
SWAP_CACHE_PFN_BITS in mm/swap.h is computed from PAGE_SHIFT. Neither is
a compile-time constant with arm64 boot-time page size selection.

Size str_dup as SZ_4K, along with the matching length check in
setup_thp_shmem(), which is large enough for the command line option
that it copies. Compute SWAP_CACHE_PFN_BITS with PAGE_SHIFT_MIN, as the
smallest page size needs the most PFN bits.

Signed-off-by: Eric Curtin <eric.curtin at docker.com>
(cherry picked from commit 54043747407a2dc56dcd60bc2b1f29ce193ee804)
---
 mm/shmem.c | 4 ++--
 mm/swap.h  | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index a5713109c843..757b0906caae 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -5942,7 +5942,7 @@ static int __init setup_transparent_hugepage_tmpfs(char *str)
 }
 __setup("transparent_hugepage_tmpfs=", setup_transparent_hugepage_tmpfs);
 
-static char str_dup[PAGE_SIZE] __initdata;
+static char str_dup[SZ_4K] __initdata;
 static int __init setup_thp_shmem(char *str)
 {
 	char *token, *range, *policy, *subtoken;
@@ -5951,7 +5951,7 @@ static int __init setup_thp_shmem(char *str)
 	int start, end, nr;
 	char *p;
 
-	if (!str || strlen(str) + 1 > PAGE_SIZE)
+	if (!str || strlen(str) + 1 > SZ_4K)
 		goto err;
 	strscpy(str_dup, str);
 
diff --git a/mm/swap.h b/mm/swap.h
index d5bf21f517dc..6dab19c1fac9 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -12,12 +12,13 @@ struct swap_iocb;
 struct swap_memcg_table;
 struct swap_io_ctx;
 
+/* Worst case (most PFN bits) for boot-time page size. */
 #if defined(MAX_POSSIBLE_PHYSMEM_BITS)
-#define SWAP_CACHE_PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
+#define SWAP_CACHE_PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT_MIN)
 #elif defined(MAX_PHYSMEM_BITS)
-#define SWAP_CACHE_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
+#define SWAP_CACHE_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT_MIN)
 #else
-#define SWAP_CACHE_PFN_BITS (BITS_PER_LONG - PAGE_SHIFT)
+#define SWAP_CACHE_PFN_BITS (BITS_PER_LONG - PAGE_SHIFT_MIN)
 #endif
 
 /* Swap table marker, 0x1 means shadow, 0x2 means PFN (SWP_TB_PFN_MARK) */



More information about the linux-arm-kernel mailing list