[RFC PATCH v2 13/13] mm: Use PAGE_SIZE_MIN/MAX where a compile-time constant is required

From: Eric Curtin

Date: Tue Dec 01 2026 - 22:18:07 EST


From: Ryan Roberts <ryan.roberts@xxxxxxx>

With arm64 boot-time page size selection, PAGE_SIZE and PAGE_SHIFT are
not compile-time constants. Convert the mm code that needs a constant,
using the PAGE_{SIZE,SHIFT}_{MIN,MAX} limits.

- struct vmap_block: replace the used_map bitmap with a flexible array
and size the allocation in new_vmap_block() with struct_size().
- union swap_header: drop the page-sized "magic" member and locate the
signature at SWAP_HEADER_MAGIC, i.e. PAGE_SIZE - 10, using pointer
arithmetic. Update MAX_SWAP_BADPAGES, read_swap_header() and mtdswap.
- BUILD_BUG_ON(), static_assert() and #if checks in memcontrol, mmzone,
slub, sparse, leafops and ptep_lockptr() now compare against the
appropriate limit. Add PFN_SECTION_SHIFT_MIN and SWP_PFN_BITS_MAX for
this. struct page_frag uses PAGE_SIZE_MAX to choose its field widths.
- stack_guard_gap, fault_around_pages and dirty_bytes_min are defined
with DEFINE_GLOBAL_PAGE_SIZE_VAR*() so that they can be initialised
at boot.
- str_dup[] for thp_anon= is a fixed SZ_4K instead of PAGE_SIZE; a
valid string fits easily for any page size.
- kmem_buckets is sized with KMALLOC_SHIFT_HIGH_MAX, and
__assume_page_alignment uses PAGE_SIZE_MIN. Add KMALLOC_SHIFT_HIGH_MIN
and _MAX, and KMALLOC_MAX_CACHE_SIZE_MIN and _MAX, for this.
- _kmalloc_noprof() and _kmalloc_node_noprof() keep the compile-time
large-versus-cache choice for sizes outside the MIN/MAX cache size
range. Constant sizes in between go to __kmalloc_noprof() and
__kmalloc_node_noprof(), so the decision is made at run time.

The ND_MIN_NAMESPACE_SIZE format in nvdimm is changed to %#lx with a
cast, as it is no longer an int constant.

No functional change for compile-time page sizes.

Signed-off-by: Ryan Roberts <ryan.roberts@xxxxxxx>
Signed-off-by: Eric Curtin <eric.curtin@xxxxxxxxxx>
---
drivers/mtd/mtdswap.c | 4 ++--
drivers/nvdimm/namespace_devs.c | 4 ++--
include/linux/leafops.h | 2 +-
include/linux/mm.h | 2 +-
include/linux/mm_types_task.h | 2 +-
include/linux/mmzone.h | 3 ++-
include/linux/slab.h | 30 ++++++++++++++++++++----------
include/linux/swap.h | 16 +++++++++++-----
include/linux/swapops.h | 4 ++++
mm/huge_memory.c | 4 ++--
mm/memcontrol.c | 4 ++--
mm/memory.c | 4 ++--
mm/mmap.c | 2 +-
mm/page-writeback.c | 2 +-
mm/slub.c | 2 +-
mm/sparse.h | 2 +-
mm/swapfile.c | 2 +-
mm/vmalloc.c | 6 ++++--
18 files changed, 59 insertions(+), 36 deletions(-)

diff --git a/drivers/mtd/mtdswap.c b/drivers/mtd/mtdswap.c
index 92e38ece8931..7b20e49d5a25 100644
--- a/drivers/mtd/mtdswap.c
+++ b/drivers/mtd/mtdswap.c
@@ -1063,13 +1063,13 @@ static int mtdswap_auto_header(struct mtdswap_dev *d, char *buf)
{
union swap_header *hd = (union swap_header *)(buf);

- memset(buf, 0, PAGE_SIZE - 10);
+ memset(buf, 0, SWAP_HEADER_MAGIC);

hd->info.version = 1;
hd->info.last_page = d->mbd_dev->size - 1;
hd->info.nr_badpages = 0;

- memcpy(buf + PAGE_SIZE - 10, "SWAPSPACE2", 10);
+ memcpy(buf + SWAP_HEADER_MAGIC, "SWAPSPACE2", 10);

return 0;
}
diff --git a/drivers/nvdimm/namespace_devs.c b/drivers/nvdimm/namespace_devs.c
index 3aa0a9026efa..10d5ce31eb4f 100644
--- a/drivers/nvdimm/namespace_devs.c
+++ b/drivers/nvdimm/namespace_devs.c
@@ -1478,8 +1478,8 @@ struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)

size = nvdimm_namespace_capacity(ndns);
if (size < ND_MIN_NAMESPACE_SIZE) {
- dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
- &size, ND_MIN_NAMESPACE_SIZE);
+ dev_dbg(&ndns->dev, "%pa, too small must be at least %#lx\n",
+ &size, (unsigned long)ND_MIN_NAMESPACE_SIZE);
return ERR_PTR(-ENODEV);
}

diff --git a/include/linux/leafops.h b/include/linux/leafops.h
index 4c1476ae3234..414da42ffc4c 100644
--- a/include/linux/leafops.h
+++ b/include/linux/leafops.h
@@ -355,7 +355,7 @@ static inline pte_marker softleaf_to_marker(softleaf_t entry)
static inline bool softleaf_has_pfn(softleaf_t entry)
{
/* Make sure the swp offset can always store the needed fields. */
- BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS);
+ BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS_MAX);

if (softleaf_is_migration(entry))
return true;
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 14a35440c806..caca5fcff031 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -4070,7 +4070,7 @@ static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
static inline spinlock_t *ptep_lockptr(struct mm_struct *mm, pte_t *pte)
{
BUILD_BUG_ON(IS_ENABLED(CONFIG_HIGHPTE));
- BUILD_BUG_ON(MAX_PTRS_PER_PTE * sizeof(pte_t) > PAGE_SIZE);
+ BUILD_BUG_ON(MAX_PTRS_PER_PTE * sizeof(pte_t) > PAGE_SIZE_MAX);
return ptlock_ptr(virt_to_ptdesc(pte));
}

diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h
index 2f760f472e19..2fe24a31e24d 100644
--- a/include/linux/mm_types_task.h
+++ b/include/linux/mm_types_task.h
@@ -35,7 +35,7 @@ struct page;

struct page_frag {
struct page *page;
-#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
+#if (BITS_PER_LONG > 32) || (PAGE_SIZE_MAX >= 65536)
__u32 offset;
__u32 size;
#else
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 72ffb2959c4f..bf317cc1a83a 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -1990,6 +1990,7 @@ static inline bool movable_only_nodes(nodemask_t *nodes)
*/
#define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
#define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
+#define PFN_SECTION_SHIFT_MIN (SECTION_SIZE_BITS - PAGE_SHIFT_MAX)

#define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)

@@ -1999,7 +2000,7 @@ static inline bool movable_only_nodes(nodemask_t *nodes)
#define SECTION_BLOCKFLAGS_BITS \
((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)

-#if (MAX_PAGE_ORDER + PAGE_SHIFT) > SECTION_SIZE_BITS
+#if (MAX_PAGE_ORDER + PAGE_SHIFT_MAX) > SECTION_SIZE_BITS
#error Allocator MAX_PAGE_ORDER exceeds SECTION_SIZE
#endif

diff --git a/include/linux/slab.h b/include/linux/slab.h
index cda126def67a..8078eb3eb0f5 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -653,7 +653,7 @@ static inline unsigned int arch_slab_minalign(void)
*/
#define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
#define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
-#define __assume_page_alignment __assume_aligned(PAGE_SIZE)
+#define __assume_page_alignment __assume_aligned(PAGE_SIZE_MIN)

/*
* Kmalloc array related definitions
@@ -664,6 +664,8 @@ static inline unsigned int arch_slab_minalign(void)
* (PAGE_SIZE*2). Larger requests are passed to the page allocator.
*/
#define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
+#define KMALLOC_SHIFT_HIGH_MIN (PAGE_SHIFT_MIN + 1)
+#define KMALLOC_SHIFT_HIGH_MAX (PAGE_SHIFT_MAX + 1)
#define KMALLOC_SHIFT_MAX (MAX_PAGE_ORDER + PAGE_SHIFT)
#ifndef KMALLOC_SHIFT_LOW
#define KMALLOC_SHIFT_LOW 3
@@ -671,8 +673,12 @@ static inline unsigned int arch_slab_minalign(void)

/* Maximum allocatable size */
#define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX)
-/* Maximum size for which we actually use a slab cache */
+/* Maximum size for which we actually use slab cache (may not be const) */
#define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH)
+/* Smallest maximum size for which we actually use slab cache (always const) */
+#define KMALLOC_MAX_CACHE_SIZE_MIN (1UL << KMALLOC_SHIFT_HIGH_MIN)
+/* Largest maximum size for which we actually use slab cache (always const) */
+#define KMALLOC_MAX_CACHE_SIZE_MAX (1UL << KMALLOC_SHIFT_HIGH_MAX)
/* Maximum order allocatable via the slab allocator */
#define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT)

@@ -738,7 +744,7 @@ enum kmalloc_cache_type {
NR_KMALLOC_TYPES
};

-typedef struct kmem_cache * kmem_buckets[KMALLOC_SHIFT_HIGH + 1];
+typedef struct kmem_cache * kmem_buckets[KMALLOC_SHIFT_HIGH_MAX + 1];

extern kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES];

@@ -836,7 +842,7 @@ static __always_inline unsigned int __kmalloc_index(size_t size,
/* Will never be reached. Needed because the compiler may complain */
return -1;
}
-static_assert(PAGE_SHIFT <= 20);
+static_assert(PAGE_SHIFT_MAX <= 20);
#define kmalloc_index(s) __kmalloc_index(s, true)

#include <linux/alloc_tag.h>
@@ -984,13 +990,15 @@ static __always_inline __alloc_size(1) void *_kmalloc_noprof(size_t size, gfp_t
if (__builtin_constant_p(size) && size) {
unsigned int index;

- if (size > KMALLOC_MAX_CACHE_SIZE)
+ if (size > KMALLOC_MAX_CACHE_SIZE_MAX)
return __kmalloc_large_noprof(size, flags);

- index = kmalloc_index(size);
- return __kmalloc_cache_noprof(
+ if (size <= KMALLOC_MAX_CACHE_SIZE_MIN) {
+ index = kmalloc_index(size);
+ return __kmalloc_cache_noprof(
kmalloc_caches[kmalloc_type(flags, token)][index],
flags, size);
+ }
}
return __kmalloc_noprof(PASS_TOKEN_PARAMS(size, token), flags);
}
@@ -1189,13 +1197,15 @@ static __always_inline __alloc_size(1) void *_kmalloc_node_noprof(size_t size, g
if (__builtin_constant_p(size) && size) {
unsigned int index;

- if (size > KMALLOC_MAX_CACHE_SIZE)
+ if (size > KMALLOC_MAX_CACHE_SIZE_MAX)
return __kmalloc_large_node_noprof(size, flags, node);

- index = kmalloc_index(size);
- return __kmalloc_cache_node_noprof(
+ if (size <= KMALLOC_MAX_CACHE_SIZE_MIN) {
+ index = kmalloc_index(size);
+ return __kmalloc_cache_node_noprof(
kmalloc_caches[kmalloc_type(flags, token)][index],
flags, node, size);
+ }
}
return __kmalloc_node_noprof(PASS_KMALLOC_PARAMS(size, NULL, token), flags, node);
}
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 4f686709d71f..4d6f00c2b7ac 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -117,10 +117,16 @@
* bootbits...
*/
union swap_header {
- struct {
- char reserved[PAGE_SIZE - 10];
- char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */
- } magic;
+ /*
+ * Exists conceptually, but since PAGE_SIZE may not be known at compile
+ * time, we must access through pointer arithmetic at run time.
+ *
+ * struct {
+ * char reserved[PAGE_SIZE - 10];
+ * char magic[10]; SWAP-SPACE or SWAPSPACE2
+ * } magic;
+ */
+#define SWAP_HEADER_MAGIC (PAGE_SIZE - 10)
struct {
char bootbits[1024]; /* Space for disklabel etc. */
__u32 version;
@@ -185,7 +191,7 @@ struct swap_extent {
* Max bad pages in the new format..
*/
#define MAX_SWAP_BADPAGES \
- ((offsetof(union swap_header, magic.magic) - \
+ ((SWAP_HEADER_MAGIC - \
offsetof(union swap_header, info.badpages)) / sizeof(int))

enum {
diff --git a/include/linux/swapops.h b/include/linux/swapops.h
index 603f9e3f909c..a5cdb7baa830 100644
--- a/include/linux/swapops.h
+++ b/include/linux/swapops.h
@@ -35,10 +35,14 @@
*/
#ifdef MAX_PHYSMEM_BITS
#define SWP_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
+#define SWP_PFN_BITS_MAX (MAX_PHYSMEM_BITS - PAGE_SHIFT_MIN)
#else /* MAX_PHYSMEM_BITS */
#define SWP_PFN_BITS min_t(int, \
sizeof(phys_addr_t) * 8 - PAGE_SHIFT, \
SWP_TYPE_SHIFT)
+#define SWP_PFN_BITS_MAX min_t(int, \
+ sizeof(phys_addr_t) * 8 - PAGE_SHIFT_MIN, \
+ SWP_TYPE_SHIFT)
#endif /* MAX_PHYSMEM_BITS */
#define SWP_PFN_MASK (BIT(SWP_PFN_BITS) - 1)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index d065b26a94df..5d60c15af51f 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1169,7 +1169,7 @@ static int __init setup_transparent_hugepage(char *str)
}
__setup("transparent_hugepage=", setup_transparent_hugepage);

-static char str_dup[PAGE_SIZE] __initdata;
+static char str_dup[SZ_4K] __initdata;
static int __init setup_thp_anon(char *str)
{
char *token, *range, *policy, *subtoken;
@@ -1178,7 +1178,7 @@ static int __init setup_thp_anon(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/memcontrol.c b/mm/memcontrol.c
index afbb638e7e1c..7dd9405ea50e 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2115,7 +2115,7 @@ struct obj_stock_pcp {
int16_t node_id;
int16_t nr_slab_reclaimable_b;
int16_t nr_slab_unreclaimable_b;
-#if PAGE_SHIFT > 16
+#if PAGE_SHIFT_MAX > 16
/*
* On rare archs with 256KiB base page size (hexagon and powerpc 44x)
* keep nr_bytes to unsigned int as uint16_t cannot represent the full
@@ -5973,7 +5973,7 @@ int __init mem_cgroup_init(void)
* to work fine, we should make sure that the overfill threshold can't
* exceed S32_MAX / PAGE_SIZE.
*/
- BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE);
+ BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE_MAX);

memcg_struct_check();

diff --git a/mm/memory.c b/mm/memory.c
index 05d350efc84b..9c149ba3b492 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -6015,8 +6015,8 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
return ret;
}

-static unsigned long fault_around_pages __read_mostly =
- 65536 >> PAGE_SHIFT;
+static DEFINE_GLOBAL_PAGE_SIZE_VAR_ATTR(unsigned long, fault_around_pages,
+ __read_mostly, 65536 >> PAGE_SHIFT);

#ifdef CONFIG_DEBUG_FS
static int fault_around_bytes_get(void *data, u64 *val)
diff --git a/mm/mmap.c b/mm/mmap.c
index 7618e72766a5..39663beeb2b1 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -952,7 +952,7 @@ find_vma_prev(struct mm_struct *mm, unsigned long addr,
}

/* enforced gap between the expanding stack and other mappings. */
-unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
+DEFINE_GLOBAL_PAGE_SIZE_VAR(unsigned long, stack_guard_gap, 256UL<<PAGE_SHIFT);

static int __init cmdline_parse_stack_guard_gap(char *p)
{
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 0694ec4098f6..562b26710b9c 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -2228,7 +2228,7 @@ static int laptop_mode_handler(const struct ctl_table *table, int write,
}

/* this is needed for the proc_doulongvec_minmax of vm_dirty_bytes */
-static const unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
+static DEFINE_GLOBAL_PAGE_SIZE_VAR_CONST(unsigned long, dirty_bytes_min, 2 * PAGE_SIZE);

static const struct ctl_table vm_page_writeback_sysctls[] = {
{
diff --git a/mm/slub.c b/mm/slub.c
index 5c33d1247060..0ab0532b9255 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -7821,7 +7821,7 @@ init_kmem_cache_node(struct kmem_cache_node *n)
static inline int alloc_kmem_cache_stats(struct kmem_cache *s)
{
BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
- NR_KMALLOC_TYPES * KMALLOC_SHIFT_HIGH *
+ NR_KMALLOC_TYPES * KMALLOC_SHIFT_HIGH_MAX *
sizeof(struct kmem_cache_stats));

s->cpu_stats = alloc_percpu(struct kmem_cache_stats);
diff --git a/mm/sparse.h b/mm/sparse.h
index fdbc4cacf91d..ff243ea4d71c 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -25,7 +25,7 @@ static inline void sparse_init_one_section(struct mem_section *ms,
{
unsigned long coded_mem_map;

- BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT);
+ BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT_MIN);
VM_WARN_ON_ONCE(section_nr > __highest_used_section_nr);

/*
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 226230dda0aa..b46867a0c294 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3639,7 +3639,7 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
unsigned long swapfilepages;
unsigned long last_page;

- if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
+ if (memcmp("SWAPSPACE2", (char *)swap_header + SWAP_HEADER_MAGIC, 10)) {
pr_err("Unable to find swap-space signature\n");
return 0;
}
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index a4c22c569b1d..79985cfdf884 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2655,12 +2655,12 @@ struct vmap_block {
spinlock_t lock;
struct vmap_area *va;
unsigned long free, dirty;
- DECLARE_BITMAP(used_map, VMAP_BBMAP_BITS);
unsigned long dirty_min, dirty_max; /*< dirty range */
struct list_head free_list;
struct rcu_head rcu_head;
struct list_head purge;
unsigned int cpu;
+ unsigned long used_map[];
};

/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
@@ -2758,10 +2758,12 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
unsigned long vb_idx;
int node, err;
void *vaddr;
+ size_t size;

node = numa_node_id();

- vb = kmalloc_node(sizeof(struct vmap_block), gfp_mask, node);
+ size = struct_size(vb, used_map, BITS_TO_LONGS(VMAP_BBMAP_BITS));
+ vb = kmalloc_node(size, gfp_mask, node);
if (unlikely(!vb))
return ERR_PTR(-ENOMEM);