KMSAN: Move per-page metadata out of struct page
Subsystems (8)
v3 cover KMSAN: Move per-page metadata out of struct page
This series fixes a KMSAN metadata-loss bug triggered when boot-time gigantic HugeTLB pages reinitialize their page descriptors after kmsan_init_shadow(). KMSAN currently stores each page's shadow and origin pointers in struct page, so the later initialization drops the metadata mappings and leaves their backing pages unreachable.
Preserving the pointers around the HugeTLB initialization would add KMSAN-specific state handling to the memmap path. Moving kmsan_init_shadow() later is not viable either, because it allocates metadata from memblock while gigantic page descriptors are initialized after memblock teardown.
Move the metadata pointers to page_ext instead. page_ext storage is independent of struct page contents and is made available before kmsan_init_shadow(), so later page-descriptor initialization cannot erase the mappings.
The series first decouples KMSAN metadata access from its storage, then makes page_ext safe and available during early boot, and finally moves the metadata pointers. The move also shrinks struct page from 80 to 64 bytes on x86-64 KMSAN builds, allowing HugeTLB Vmemmap Optimization to be used with KMSAN.
v3:
- Fix page_ext client alignment on 32-bit architectures by using u64 alignment as the cap (reported by Sashiko).
- Open-code the lowest power-of-two factor and use a local for the u64 alignment (suggested by Andrew Morton and Mike Rapoport).
- Use DIV_ROUND_UP() for page_ext accounting instead of relying on the caller's size alignment (suggested by Mike Rapoport).
- Simplify the page_ext alignment commit message.
- Collect Reviewed-by and Tested-by tags from Chris Down for patches 1-4, 6, and 7, and Reviewed-by for patch 5.
- Collect Reviewed-by tags from Mike Rapoport for patches 3-5.
- Rebase onto mm/mm-new.
v2: https://lore.kernel.org/[email protected]/
- Fetch shadow and origin through one masked metadata resolver (suggested by Chris Down).
- Add a fix for pre-existing page_ext client misalignment.
- Use vmalloc for runtime SPARSEMEM page_ext backing
(suggested by Mike Rapoport).
- Unify page_ext allocation and initialization entry points and document the later FLATMEM callback timing (noted by Chris Down).
- Allocate all boot-time SPARSEMEM page_ext from memblock (suggested by Mike Rapoport).
- Keep memblock allocations above DMA and suppress per-section free messages (suggested by Chris Down).
- Inline the unchecked page_ext lookup while keeping page_ext.c instrumented (suggested by Chris Down).
- Update the KMSAN metadata documentation and describe HVO enablement (suggested by Chris Down).
- Expand the series from three to seven patches.
- Drop all v1 Reviewed-by tags because the affected patches were substantially reworked and split.
v1: https://lore.kernel.org/[email protected]/
Muchun Song (7):
kmsan: encapsulate per-page metadata access
mm/page_ext: fix client data alignment
mm/page_ext: always use vmalloc for section page extensions mm/page_ext: use common allocation and initialization entry points mm/page_ext: allocate page extensions early for SPARSEMEM mm/page_ext: inline unchecked page extension lookup
kmsan: fix metadata loss during HugeTLB struct page reinitialization
Documentation/dev-tools/kmsan.rst | 8 +- include/linux/kmsan.h | 2 + include/linux/mm.h | 12 +- include/linux/mm_types.h | 12 -- include/linux/page_ext.h | 61 +++++++-- lib/Kconfig.kmsan | 1 + mm/kmsan/shadow.c | 199 +++++++++++++++++++-------- mm/mm_init.c | 11 +- mm/page_ext.c | 221 ++++++++++++------------------ 9 files changed, 290 insertions(+), 237 deletions(-)
base-commit: 3dfc09a9493ddaa5e7722a29165d8450b55b4d3f
Signature · 1 line
2.54.0
PATCH v3 1/7 kmsan: encapsulate per-page metadata access
KMSAN directly accesses page->kmsan_shadow and page->kmsan_origin throughout the shadow implementation. This couples these users to storing the metadata pointers in struct page, requiring every access site to change when the storage moves elsewhere.
Introduce helpers that get and set both metadata page pointers as a pair and convert the existing users to them.
kmsan_get_shadow_origin_ptr() is the main metadata lookup path for compiler-instrumented memory accesses and requires both pointers. For page-backed addresses, resolve the backing page once and obtain the metadata pair through one lookup instead of calling the single-metadata path twice. This also avoids duplicate lookups after moving the metadata storage out of struct page.
No functional change is intended.
---
v3:
- Collect Reviewed-by and Tested-by from Chris Down
v2:
- Use one page metadata lookup for shadow and origin pointers (suggested by Chris Down)
--- mm/kmsan/shadow.c | 166 +++++++++++++++++++++++++++++++--------------- 1 file changed, 111 insertions(+), 55 deletions(-)
mm/kmsan/shadow.c +111 -55
@@ -9,6 +9,7 @@#include <asm/kmsan.h> #include <asm/tlbflush.h> +#include <linux/bits.h> #include <linux/cacheflush.h> #include <linux/memblock.h> #include <linux/mm_types.h>
@@ -20,29 +21,46 @@#include "../vmalloc.h" #include "kmsan.h" -#define shadow_page_for (page) ((page)->kmsan_shadow) +#define KMSAN_META_MASK_SHADOW BIT(0) +#define KMSAN_META_MASK_ORIGIN BIT(1) -#define origin_page_for (page) ((page)->kmsan_origin) +struct page_metadata { +struct page *shadow; +struct page *origin; +}; -static void *shadow_ptr_for (struct page *page) +static inline struct page_metadata get_page_metadata (struct page *page) { -return page_address (shadow_page_for (page)); +return (struct page_metadata ) { + .shadow = page->kmsan_shadow, + .origin = page->kmsan_origin, + }; } -static void *origin_ptr_for (struct page *page) +static inline void set_page_metadata (struct page *page,struct page_metadata meta) { -return page_address (origin_page_for (page)); + page->kmsan_shadow = meta.shadow; + page->kmsan_origin = meta.origin; } -static bool page_has_metadata (struct page *page) +static inline void *shadow_ptr_for (struct page_metadata meta) { -return shadow_page_for (page) &&origin_page_for (page); +return page_address (meta.shadow); +} + +static inline void *origin_ptr_for (struct page_metadata meta) +{ +return page_address (meta.origin); +} + +static inline bool page_has_metadata (struct page_metadata meta) +{ +return meta.shadow && meta.origin; }static void set_no_shadow_origin_page (struct page *page) { -shadow_page_for (page) =NULL ; -origin_page_for (page) =NULL ; +set_page_metadata (page, (struct page_metadata ) {}); }/*
@@ -79,11 +97,56 @@ static struct page *virt_to_page_or_null(void *vaddr)return NULL ; } +static __always_inline +struct shadow_origin_ptr __kmsan_get_metadata(void *address, unsigned int mask) +{ +struct shadow_origin_ptr ret = {}; +struct page_metadata meta; +struct page *page; +u64 addr = (u64 )address; +u64 origin_addr =ALIGN_DOWN (addr,KMSAN_ORIGIN_SIZE ); + +if (kmsan_internal_is_vmalloc_addr (address) || +kmsan_internal_is_module_addr (address)) { +if (mask &KMSAN_META_MASK_SHADOW ) + ret.shadow = (void *)vmalloc_meta (address,KMSAN_META_SHADOW ); +if (mask &KMSAN_META_MASK_ORIGIN ) + ret.origin = (void *)vmalloc_meta ((void *)origin_addr, +KMSAN_META_ORIGIN ); +return ret; + } + +if (mask &KMSAN_META_MASK_SHADOW ) { + ret.shadow =arch_kmsan_get_meta_or_null (address,KMSAN_META_SHADOW ); +if (!ret.shadow) + goto lookup_page; + } +if (mask &KMSAN_META_MASK_ORIGIN ) + ret.origin =arch_kmsan_get_meta_or_null ((void *)origin_addr, +KMSAN_META_ORIGIN ); +if (ret.shadow || ret.origin) +return ret; + +lookup_page: + page =virt_to_page_or_null (address); +if (!page) +return ret; + meta =get_page_metadata (page); +if (!page_has_metadata (meta)) +return ret; + +if (mask &KMSAN_META_MASK_SHADOW ) + ret.shadow =shadow_ptr_for (meta) +offset_in_page (addr); +if (mask &KMSAN_META_MASK_ORIGIN ) + ret.origin =origin_ptr_for (meta) +offset_in_page (origin_addr); + +return ret; +} +struct shadow_origin_ptr kmsan_get_shadow_origin_ptr (void *address,u64 size,bool store) {struct shadow_origin_ptr ret; -void *shadow;/* * Even if we redirect this memory access to the dummy page, it will
@@ -95,12 +158,10 @@ struct shadow_origin_ptr kmsan_get_shadow_origin_ptr(void *address, u64 size, goto return_dummy;KMSAN_WARN_ON (!kmsan_metadata_is_contiguous (address, size)); - shadow =kmsan_get_metadata (address,KMSAN_META_SHADOW ); -if (!shadow) + ret =__kmsan_get_metadata (address,KMSAN_META_MASK_SHADOW | +KMSAN_META_MASK_ORIGIN ); +if (!ret.shadow) goto return_dummy; - - ret.shadow = shadow; - ret.origin =kmsan_get_metadata (address,KMSAN_META_ORIGIN );return ret; return_dummy:
@@ -124,46 +185,36 @@ struct shadow_origin_ptr kmsan_get_shadow_origin_ptr(void *address, u64 size,*/ void *kmsan_get_metadata (void *address,bool is_origin) { -u64 addr = (u64 )address, off; -struct page *page; -void *ret; - -if (is_origin) - addr =ALIGN_DOWN (addr,KMSAN_ORIGIN_SIZE ); - address = (void *)addr; -if (kmsan_internal_is_vmalloc_addr (address) || -kmsan_internal_is_module_addr (address)) -return (void *)vmalloc_meta (address, is_origin); - - ret =arch_kmsan_get_meta_or_null (address, is_origin); -if (ret) -return ret; - - page =virt_to_page_or_null (address); -if (!page) -return NULL ; -if (!page_has_metadata (page)) -return NULL ; - off =offset_in_page (addr); +struct shadow_origin_ptr ret; +unsigned int mask; -return (is_origin ?origin_ptr_for (page) :shadow_ptr_for (page)) + off; + mask = is_origin ?KMSAN_META_MASK_ORIGIN :KMSAN_META_MASK_SHADOW ; + ret =__kmsan_get_metadata (address, mask); +return is_origin ? ret.origin : ret.shadow; }void kmsan_copy_page_meta (struct page *dst,struct page *src) { +struct page_metadata dst_meta, src_meta; +if (!kmsan_enabled ||kmsan_in_runtime ())return ; -if (!dst || !page_has_metadata (dst)) +if (!dst)return ; -if (!src || !page_has_metadata (src)) { + dst_meta =get_page_metadata (dst); +if (!page_has_metadata (dst_meta)) +return ; +if (src) + src_meta =get_page_metadata (src); +if (!src || !page_has_metadata (src_meta)) {kmsan_internal_unpoison_memory (page_address (dst),PAGE_SIZE ,/*checked*/ false);return ; }kmsan_enter_runtime (); -__memcpy (shadow_ptr_for (dst),shadow_ptr_for (src),PAGE_SIZE ); -__memcpy (origin_ptr_for (dst),origin_ptr_for (src),PAGE_SIZE ); +__memcpy (shadow_ptr_for (dst_meta),shadow_ptr_for (src_meta),PAGE_SIZE ); +__memcpy (origin_ptr_for (dst_meta),origin_ptr_for (src_meta),PAGE_SIZE );kmsan_leave_runtime (); }EXPORT_SYMBOL (kmsan_copy_page_meta);
@@ -171,19 +222,18 @@ EXPORT_SYMBOL(kmsan_copy_page_meta);void kmsan_alloc_page (struct page *page,unsigned int order,gfp_t flags) {bool initialized = (flags & __GFP_ZERO) || !kmsan_enabled; -struct page *shadow, *origin; +struct page_metadata meta;depot_stack_handle_t handle;int pages =1 << order;if (!page)return ; - shadow =shadow_page_for (page); - origin =origin_page_for (page); + meta =get_page_metadata (page);if (initialized) { -__memset (page_address (shadow),0 ,PAGE_SIZE * pages); -__memset (page_address (origin),0 ,PAGE_SIZE * pages); +__memset (shadow_ptr_for (meta),0 ,PAGE_SIZE * pages); +__memset (origin_ptr_for (meta),0 ,PAGE_SIZE * pages);return ; }
@@ -191,7 +241,7 @@ void kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags)if (kmsan_in_runtime ())return ; -__memset (page_address (shadow),-1 ,PAGE_SIZE * pages); +__memset (shadow_ptr_for (meta),-1 ,PAGE_SIZE * pages);kmsan_enter_runtime (); handle =kmsan_save_stack_with_flags (flags,/*extra_bits*/ 0 );kmsan_leave_runtime ();
@@ -200,7 +250,7 @@ void kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags)* to just fill the origin pages with @handle. */ for (int i =0 ; i <PAGE_SIZE * pages /sizeof (handle); i++) - ((depot_stack_handle_t *)page_address (origin))[i] = handle; + ((depot_stack_handle_t *)origin_ptr_for (meta))[i] = handle; }void kmsan_free_page (struct page *page,unsigned int order)
@@ -238,8 +288,10 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, goto ret; }for (int i =0 ; i < nr; i++) { - s_pages[i] =shadow_page_for (pages[i]); - o_pages[i] =origin_page_for (pages[i]); +struct page_metadata meta =get_page_metadata (pages[i]); + + s_pages[i] = meta.shadow; + o_pages[i] = meta.origin; } prot =PAGE_KERNEL ;
@@ -289,10 +341,12 @@ void __init kmsan_init_alloc_meta_for_range(void *start, void *end) page =virt_to_page_or_null ((char *)start + addr); shadow_p =virt_to_page ((char *)shadow + addr);set_no_shadow_origin_page (shadow_p); -shadow_page_for (page) = shadow_p; origin_p =virt_to_page ((char *)origin + addr);set_no_shadow_origin_page (origin_p); -origin_page_for (page) = origin_p; +set_page_metadata (page, (struct page_metadata ) { + .shadow = shadow_p, + .origin = origin_p, + }); } }
@@ -302,7 +356,9 @@ void kmsan_setup_meta(struct page *page, struct page *shadow,for (int i =0 ; i < (1 << order); i++) {set_no_shadow_origin_page (&shadow[i]);set_no_shadow_origin_page (&origin[i]); -shadow_page_for (&page[i]) = &shadow[i]; -origin_page_for (&page[i]) = &origin[i]; +set_page_metadata (&page[i], (struct page_metadata ) { + .shadow = &shadow[i], + .origin = &origin[i], + }); } }
Signature · 1 line
2.54.0
+ kmsan-encapsulate-per-page-metadata-access.patch added to mm-new branch
The patch titled Subject: kmsan: encapsulate per-page metadata access has been added to the -mm mm-new branch. Its filename is kmsan-encapsulate-per-page-metadata-access.patch
The patch titled
Subject: kmsan: encapsulate per-page metadata access
has been added to the -mm mm-new branch. Its filename is
kmsan-encapsulate-per-page-metadata-access.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/kmsan-encapsulate-per-page-metadata-access.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Muchun Song <[email protected]>
Subject: kmsan: encapsulate per-page metadata access
Date: Thu, 19 Nov 2026 14:58:06 +0800
Patch series "KMSAN: Move per-page metadata out of struct page", v3.
This series fixes a KMSAN metadata-loss bug triggered when boot-time gigantic HugeTLB pages reinitialize their page descriptors after kmsan_init_shadow(). KMSAN currently stores each page's shadow and origin pointers in struct page, so the later initialization drops the metadata mappings and leaves their backing pages unreachable.
Preserving the pointers around the HugeTLB initialization would add KMSAN-specific state handling to the memmap path. Moving kmsan_init_shadow() later is not viable either, because it allocates metadata from memblock while gigantic page descriptors are initialized after memblock teardown.
Move the metadata pointers to page_ext instead. page_ext storage is independent of struct page contents and is made available before kmsan_init_shadow(), so later page-descriptor initialization cannot erase the mappings.
The series first decouples KMSAN metadata access from its storage, then makes page_ext safe and available during early boot, and finally moves the metadata pointers. The move also shrinks struct page from 80 to 64 bytes on x86-64 KMSAN builds, allowing HugeTLB Vmemmap Optimization to be used with KMSAN.
This patch (of 7):
KMSAN directly accesses page->kmsan_shadow and page->kmsan_origin throughout the shadow implementation. This couples these users to storing the metadata pointers in struct page, requiring every access site to change when the storage moves elsewhere.
Introduce helpers that get and set both metadata page pointers as a pair and convert the existing users to them.
kmsan_get_shadow_origin_ptr() is the main metadata lookup path for compiler-instrumented memory accesses and requires both pointers. For page-backed addresses, resolve the backing page once and obtain the metadata pair through one lookup instead of calling the single-metadata path twice. This also avoids duplicate lookups after moving the metadata storage out of struct page.
No functional change is intended.
mm/kmsan/shadow.c | 166 +++++++++++++++++++++++++++++--------------- 1 file changed, 111 insertions(+), 55 deletions(-)
mm/kmsan/shadow.c renamed from mm/kmsan/shadow.c~kmsan-encapsulate-per-page-metadata-access +111 -55
@@ -9,6 +9,7 @@#include <asm/kmsan.h> #include <asm/tlbflush.h> +#include <linux/bits.h> #include <linux/cacheflush.h> #include <linux/memblock.h> #include <linux/mm_types.h>
@@ -20,29 +21,46 @@#include "../vmalloc.h" #include "kmsan.h" -#define shadow_page_for (page) ((page)->kmsan_shadow) +#define KMSAN_META_MASK_SHADOW BIT(0) +#define KMSAN_META_MASK_ORIGIN BIT(1) -#define origin_page_for (page) ((page)->kmsan_origin) +struct page_metadata { +struct page *shadow; +struct page *origin; +}; -static void *shadow_ptr_for (struct page *page) +static inline struct page_metadata get_page_metadata (struct page *page) { -return page_address (shadow_page_for (page)); +return (struct page_metadata ) { + .shadow = page->kmsan_shadow, + .origin = page->kmsan_origin, + }; } -static void *origin_ptr_for (struct page *page) +static inline void set_page_metadata (struct page *page,struct page_metadata meta) { -return page_address (origin_page_for (page)); + page->kmsan_shadow = meta.shadow; + page->kmsan_origin = meta.origin; } -static bool page_has_metadata (struct page *page) +static inline void *shadow_ptr_for (struct page_metadata meta) { -return shadow_page_for (page) &&origin_page_for (page); +return page_address (meta.shadow); +} + +static inline void *origin_ptr_for (struct page_metadata meta) +{ +return page_address (meta.origin); +} + +static inline bool page_has_metadata (struct page_metadata meta) +{ +return meta.shadow && meta.origin; }static void set_no_shadow_origin_page (struct page *page) { -shadow_page_for (page) =NULL ; -origin_page_for (page) =NULL ; +set_page_metadata (page, (struct page_metadata ) {}); }/*
@@ -79,11 +97,56 @@ static struct page *virt_to_page_or_nullreturn NULL ; } +static __always_inline +struct shadow_origin_ptr __kmsan_get_metadata(void *address, unsigned int mask) +{ +struct shadow_origin_ptr ret = {}; +struct page_metadata meta; +struct page *page; +u64 addr = (u64 )address; +u64 origin_addr =ALIGN_DOWN (addr,KMSAN_ORIGIN_SIZE ); + +if (kmsan_internal_is_vmalloc_addr (address) || +kmsan_internal_is_module_addr (address)) { +if (mask &KMSAN_META_MASK_SHADOW ) + ret.shadow = (void *)vmalloc_meta (address,KMSAN_META_SHADOW ); +if (mask &KMSAN_META_MASK_ORIGIN ) + ret.origin = (void *)vmalloc_meta ((void *)origin_addr, +KMSAN_META_ORIGIN ); +return ret; + } + +if (mask &KMSAN_META_MASK_SHADOW ) { + ret.shadow =arch_kmsan_get_meta_or_null (address,KMSAN_META_SHADOW ); +if (!ret.shadow) + goto lookup_page; + } +if (mask &KMSAN_META_MASK_ORIGIN ) + ret.origin =arch_kmsan_get_meta_or_null ((void *)origin_addr, +KMSAN_META_ORIGIN ); +if (ret.shadow || ret.origin) +return ret; + +lookup_page: + page =virt_to_page_or_null (address); +if (!page) +return ret; + meta =get_page_metadata (page); +if (!page_has_metadata (meta)) +return ret; + +if (mask &KMSAN_META_MASK_SHADOW ) + ret.shadow =shadow_ptr_for (meta) +offset_in_page (addr); +if (mask &KMSAN_META_MASK_ORIGIN ) + ret.origin =origin_ptr_for (meta) +offset_in_page (origin_addr); + +return ret; +} +struct shadow_origin_ptr kmsan_get_shadow_origin_ptr (void *address,u64 size,bool store) {struct shadow_origin_ptr ret; -void *shadow;/* * Even if we redirect this memory access to the dummy page, it will
@@ -95,12 +158,10 @@ struct shadow_origin_ptr kmsan_get_shado goto return_dummy;KMSAN_WARN_ON (!kmsan_metadata_is_contiguous (address, size)); - shadow =kmsan_get_metadata (address,KMSAN_META_SHADOW ); -if (!shadow) + ret =__kmsan_get_metadata (address,KMSAN_META_MASK_SHADOW | +KMSAN_META_MASK_ORIGIN ); +if (!ret.shadow) goto return_dummy; - - ret.shadow = shadow; - ret.origin =kmsan_get_metadata (address,KMSAN_META_ORIGIN );return ret; return_dummy:
@@ -124,46 +185,36 @@ return_dummy:*/ void *kmsan_get_metadata (void *address,bool is_origin) { -u64 addr = (u64 )address, off; -struct page *page; -void *ret; - -if (is_origin) - addr =ALIGN_DOWN (addr,KMSAN_ORIGIN_SIZE ); - address = (void *)addr; -if (kmsan_internal_is_vmalloc_addr (address) || -kmsan_internal_is_module_addr (address)) -return (void *)vmalloc_meta (address, is_origin); - - ret =arch_kmsan_get_meta_or_null (address, is_origin); -if (ret) -return ret; - - page =virt_to_page_or_null (address); -if (!page) -return NULL ; -if (!page_has_metadata (page)) -return NULL ; - off =offset_in_page (addr); +struct shadow_origin_ptr ret; +unsigned int mask; -return (is_origin ?origin_ptr_for (page) :shadow_ptr_for (page)) + off; + mask = is_origin ?KMSAN_META_MASK_ORIGIN :KMSAN_META_MASK_SHADOW ; + ret =__kmsan_get_metadata (address, mask); +return is_origin ? ret.origin : ret.shadow; }void kmsan_copy_page_meta (struct page *dst,struct page *src) { +struct page_metadata dst_meta, src_meta; +if (!kmsan_enabled ||kmsan_in_runtime ())return ; -if (!dst || !page_has_metadata (dst)) +if (!dst)return ; -if (!src || !page_has_metadata (src)) { + dst_meta =get_page_metadata (dst); +if (!page_has_metadata (dst_meta)) +return ; +if (src) + src_meta =get_page_metadata (src); +if (!src || !page_has_metadata (src_meta)) {kmsan_internal_unpoison_memory (page_address (dst),PAGE_SIZE ,/*checked*/ false);return ; }kmsan_enter_runtime (); -__memcpy (shadow_ptr_for (dst),shadow_ptr_for (src),PAGE_SIZE ); -__memcpy (origin_ptr_for (dst),origin_ptr_for (src),PAGE_SIZE ); +__memcpy (shadow_ptr_for (dst_meta),shadow_ptr_for (src_meta),PAGE_SIZE ); +__memcpy (origin_ptr_for (dst_meta),origin_ptr_for (src_meta),PAGE_SIZE );kmsan_leave_runtime (); }EXPORT_SYMBOL (kmsan_copy_page_meta);
@@ -171,19 +222,18 @@ EXPORT_SYMBOL(kmsan_copy_page_meta);void kmsan_alloc_page (struct page *page,unsigned int order,gfp_t flags) {bool initialized = (flags & __GFP_ZERO) || !kmsan_enabled; -struct page *shadow, *origin; +struct page_metadata meta;depot_stack_handle_t handle;int pages =1 << order;if (!page)return ; - shadow =shadow_page_for (page); - origin =origin_page_for (page); + meta =get_page_metadata (page);if (initialized) { -__memset (page_address (shadow),0 ,PAGE_SIZE * pages); -__memset (page_address (origin),0 ,PAGE_SIZE * pages); +__memset (shadow_ptr_for (meta),0 ,PAGE_SIZE * pages); +__memset (origin_ptr_for (meta),0 ,PAGE_SIZE * pages);return ; }
@@ -191,7 +241,7 @@ void kmsan_alloc_page(struct page *page,if (kmsan_in_runtime ())return ; -__memset (page_address (shadow),-1 ,PAGE_SIZE * pages); +__memset (shadow_ptr_for (meta),-1 ,PAGE_SIZE * pages);kmsan_enter_runtime (); handle =kmsan_save_stack_with_flags (flags,/*extra_bits*/ 0 );kmsan_leave_runtime ();
@@ -200,7 +250,7 @@ void kmsan_alloc_page(struct page *page,* to just fill the origin pages with @handle. */ for (int i =0 ; i <PAGE_SIZE * pages /sizeof (handle); i++) - ((depot_stack_handle_t *)page_address (origin))[i] = handle; + ((depot_stack_handle_t *)origin_ptr_for (meta))[i] = handle; }void kmsan_free_page (struct page *page,unsigned int order)
@@ -238,8 +288,10 @@ int kmsan_vmap_pages_range_noflush(unsig goto ret; }for (int i =0 ; i < nr; i++) { - s_pages[i] =shadow_page_for (pages[i]); - o_pages[i] =origin_page_for (pages[i]); +struct page_metadata meta =get_page_metadata (pages[i]); + + s_pages[i] = meta.shadow; + o_pages[i] = meta.origin; } prot =PAGE_KERNEL ;
@@ -289,10 +341,12 @@ void __init kmsan_init_alloc_meta_for_ra page =virt_to_page_or_null ((char *)start + addr); shadow_p =virt_to_page ((char *)shadow + addr);set_no_shadow_origin_page (shadow_p); -shadow_page_for (page) = shadow_p; origin_p =virt_to_page ((char *)origin + addr);set_no_shadow_origin_page (origin_p); -origin_page_for (page) = origin_p; +set_page_metadata (page, (struct page_metadata ) { + .shadow = shadow_p, + .origin = origin_p, + }); } }
@@ -302,7 +356,9 @@ void kmsan_setup_meta(struct page *page,for (int i =0 ; i < (1 << order); i++) {set_no_shadow_origin_page (&shadow[i]);set_no_shadow_origin_page (&origin[i]); -shadow_page_for (&page[i]) = &shadow[i]; -origin_page_for (&page[i]) = &origin[i]; +set_page_metadata (&page[i], (struct page_metadata ) { + .shadow = &shadow[i], + .origin = &origin[i], + }); } }
_
Patches currently in -mm which might be from [email protected] are
powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
PATCH v3 2/7 mm/page_ext: fix client data alignment
In reply to Muchun Songpage_ext lays out client payloads consecutively without aligning their offsets or the final entry size. This happened to work while the active payload sizes kept the stride naturally aligned, but IOMMU debug page allocation added a four-byte payload after data requiring eight-byte alignment.
When page owner and IOMMU debug page allocation are both enabled on x86-64, struct page_ext, struct page_owner, and the IOMMU payload use 8, 64, and 4 bytes respectively. This produces a 76-byte entry stride, leaving struct page_ext and page_owner data in every other entry only four-byte aligned instead of eight-byte aligned.
Align each client offset and round the final entry size to the largest active alignment so every page_ext entry preserves the alignment required by all active clients.
---
v3:
- Cap client alignment at u64 alignment to preserve 64-bit alignment on 32-bit architectures (reported by Sashiko)
- Open-code the lowest power-of-two factor for readability (suggested by Andrew Morton)
- Use a local for the u64 alignment (suggested by Mike Rapoport)
- Simplify the commit message to focus on the alignment guarantee
- Collect Reviewed-by and Tested-by from Chris Down
v2:
- New patch to fix the existing page_ext client alignment bug
--- mm/page_ext.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
mm/page_ext.c +26 -0
@@ -116,8 +116,27 @@ static int __init setup_early_page_ext(char *str) }early_param ("early_page_ext" , setup_early_page_ext); +static inline size_t page_ext_data_alignment (size_t size) +{ +const size_t align = __alignof__(u64 ); + +if (!size) +return 1 ; + +/* u64 has the strictest alignment required by page_ext clients. */ +if (IS_ALIGNED (size, align)) +return align; + +/* +* A type's size is a multiple of its natural alignment, so its +* lowest set bit is at least that alignment. +*/ +return size & -size; +} +static bool __init invoke_need_callbacks (void ) { +size_t max_align =1 ;int i;int entries =ARRAY_SIZE (page_ext_ops);bool need = false;
@@ -126,6 +145,7 @@ static bool __init invoke_need_callbacks(void)if (page_ext_ops[i]->need ()) {if (page_ext_ops[i]->need_shared_flags) { page_ext_size =sizeof (struct page_ext ); + max_align = __alignof__(struct page_ext );break ; } }
@@ -133,11 +153,17 @@ static bool __init invoke_need_callbacks(void)for (i =0 ; i < entries; i++) {if (page_ext_ops[i]->need ()) { +size_t align; + + align =page_ext_data_alignment (page_ext_ops[i]->size); + page_ext_size =ALIGN (page_ext_size, align); page_ext_ops[i]->offset = page_ext_size; page_ext_size += page_ext_ops[i]->size; + max_align =max (max_align, align); need = true; } } + page_ext_size =ALIGN (page_ext_size, max_align);return need; }
Signature · 1 line
2.54.0
Re: [PATCH v3 2/7] mm/page_ext: fix client data alignment
Quoted 12 lines · Muchun Song
page_extlays out client payloads consecutively without aligning their offsets or the final entry size. This happened to work while the active payload sizes kept the stride naturally aligned, but IOMMU debug page allocation added a four-byte payload after data requiring eight-byte alignment.When page owner and IOMMU debug page allocation are both enabled on x86-64, struct
page_ext, structpage_owner, and the IOMMU payload use 8, 64, and 4 bytes respectively. This produces a 76-byte entry stride, leaving structpage_extandpage_ownerdata in every other entry only four-byte aligned instead of eight-byte aligned.
Quoted 8 lines
Align each client offset and round the final entry size to the largest active alignment so every
page_extentry preserves the alignment required by all active clients.Fixes 466ae6978a5b ("iommu: Add page_ext for IOMMU_DEBUG_PAGEALLOC")Signed-off-by Muchun Song <[email protected]>Reviewed-by Chris Down <[email protected]>Tested-by Chris Down <[email protected]>
Quoted 15 lines
---
v3:
- Cap client alignment at u64 alignment to preserve 64-bit alignment on 32-bit architectures (reported by Sashiko)
- Open-code the lowest power-of-two factor for readability (suggested by Andrew Morton)
- Use a local for the u64 alignment (suggested by Mike Rapoport)
- Simplify the commit message to focus on the alignment guarantee
- Collect Reviewed-by and Tested-by from Chris Downv2:
- New patch to fix the existingpage_extclient alignment bug--- mm/page_ext.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
Signature · 2 lines
Sincerely yours, Mike.
+ mm-page_ext-fix-client-data-alignment.patch added to mm-new branch
In reply to Muchun SongThe patch titled Subject: mm/page_ext: fix client data alignment has been added to the -mm mm-new branch. Its filename is mm-page_ext-fix-client-data-alignment.patch
The patch titled
Subject: mm/page_ext: fix client data alignment
has been added to the -mm mm-new branch. Its filename is
mm-page_ext-fix-client-data-alignment.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-page_ext-fix-client-data-alignment.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Muchun Song <[email protected]>
Subject: mm/page_ext: fix client data alignment
Date: Thu, 19 Nov 2026 14:58:07 +0800
page_ext lays out client payloads consecutively without aligning their offsets or the final entry size. This happened to work while the active payload sizes kept the stride naturally aligned, but IOMMU debug page allocation added a four-byte payload after data requiring eight-byte alignment.
When page owner and IOMMU debug page allocation are both enabled on x86-64, struct page_ext, struct page_owner, and the IOMMU payload use 8, 64, and 4 bytes respectively. This produces a 76-byte entry stride, leaving struct page_ext and page_owner data in every other entry only four-byte aligned instead of eight-byte aligned.
Align each client offset and round the final entry size to the largest active alignment so every page_ext entry preserves the alignment required by all active clients.
mm/page_ext.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
mm/page_ext.c renamed from mm/page_ext.c~mm-page_ext-fix-client-data-alignment +26 -0
@@ -116,8 +116,27 @@ static int __init setup_early_page_ext(c }early_param ("early_page_ext" , setup_early_page_ext); +static inline size_t page_ext_data_alignment (size_t size) +{ +const size_t align = __alignof__(u64 ); + +if (!size) +return 1 ; + +/* u64 has the strictest alignment required by page_ext clients. */ +if (IS_ALIGNED (size, align)) +return align; + +/* +* A type's size is a multiple of its natural alignment, so its +* lowest set bit is at least that alignment. +*/ +return size & -size; +} +static bool __init invoke_need_callbacks (void ) { +size_t max_align =1 ;int i;int entries =ARRAY_SIZE (page_ext_ops);bool need = false;
@@ -126,6 +145,7 @@ static bool __init invoke_need_callbacksif (page_ext_ops[i]->need ()) {if (page_ext_ops[i]->need_shared_flags) { page_ext_size =sizeof (struct page_ext ); + max_align = __alignof__(struct page_ext );break ; } }
@@ -133,11 +153,17 @@ static bool __init invoke_need_callbacksfor (i =0 ; i < entries; i++) {if (page_ext_ops[i]->need ()) { +size_t align; + + align =page_ext_data_alignment (page_ext_ops[i]->size); + page_ext_size =ALIGN (page_ext_size, align); page_ext_ops[i]->offset = page_ext_size; page_ext_size += page_ext_ops[i]->size; + max_align =max (max_align, align); need = true; } } + page_ext_size =ALIGN (page_ext_size, max_align);return need; }
_
Patches currently in -mm which might be from [email protected] are
powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
PATCH v3 3/7 mm/page_ext: always use vmalloc for section page extensions
In reply to Muchun SongSPARSEMEM currently tries to allocate each section's page extensions from the buddy allocator before falling back to vmalloc. Boot-time section allocation happens after vmalloc is available, and memory hotplug allocations do not require physically contiguous backing.
Use vzalloc_node() directly and remove the buddy-specific allocation, free, and kmemleak bookkeeping. This leaves one runtime backing type and lets a later change add memblock as the distinct boot-time path to simplify the code.
---
v3:
- Collect Reviewed-by from Mike Rapoport and Chris Down
- Collect Tested-by from Chris Down
v2:
- Add a preparatory cleanup that always uses vmalloc for SPARSEMEM section page extensions (suggested by Mike Rapoport)
--- mm/page_ext.c | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-)
mm/page_ext.c +3 -18
@@ -290,15 +290,9 @@ static struct page_ext *lookup_page_ext(const struct page *page)static void *__meminit alloc_page_ext (size_t size,int nid) { -gfp_t flags =GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN; -void *addr =NULL ; - - addr =alloc_pages_exact_nid (nid, size, flags); -if (addr) -kmemleak_alloc (addr, size,1 , flags); -else - addr =vzalloc_node (size, nid); +void *addr; + addr =vzalloc_node (size, nid);if (addr)memmap_pages_add (DIV_ROUND_UP (size,PAGE_SIZE ));
@@ -344,19 +338,10 @@ static int __meminit init_section_page_ext(unsigned long pfn, int nid)static void free_page_ext (void *addr) {size_t table_size; -struct page *page; table_size = page_ext_size *PAGES_PER_SECTION ;memmap_pages_add (-1L * (DIV_ROUND_UP (table_size,PAGE_SIZE ))); - -if (is_vmalloc_addr (addr)) { -vfree (addr); - }else { - page =virt_to_page (addr); -BUG_ON (PageReserved (page)); -kmemleak_free (addr); -free_pages_exact (addr, table_size); - } +vfree (addr); }static void __free_page_ext (unsigned long pfn)
Signature · 1 line
2.54.0
+ mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch added to mm-new branch
The patch titled Subject: mm/page_ext: always use vmalloc for section page extensions has been added to the -mm mm-new branch. Its filename is…
The patch titled
Subject: mm/page_ext: always use vmalloc for section page extensions
has been added to the -mm mm-new branch. Its filename is
mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Muchun Song <[email protected]>
Subject: mm/page_ext: always use vmalloc for section page extensions
Date: Thu, 19 Nov 2026 14:58:08 +0800
SPARSEMEM currently tries to allocate each section's page extensions from the buddy allocator before falling back to vmalloc. Boot-time section allocation happens after vmalloc is available, and memory hotplug allocations do not require physically contiguous backing.
Use vzalloc_node() directly and remove the buddy-specific allocation, free, and kmemleak bookkeeping. This leaves one runtime backing type and lets a later change add memblock as the distinct boot-time path to simplify the code.
mm/page_ext.c | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-)
mm/page_ext.c renamed from mm/page_ext.c~mm-page_ext-always-use-vmalloc-for-section-page-extensions +3 -18
@@ -290,15 +290,9 @@ static struct page_ext *lookup_page_ext(static void *__meminit alloc_page_ext (size_t size,int nid) { -gfp_t flags =GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN; -void *addr =NULL ; - - addr =alloc_pages_exact_nid (nid, size, flags); -if (addr) -kmemleak_alloc (addr, size,1 , flags); -else - addr =vzalloc_node (size, nid); +void *addr; + addr =vzalloc_node (size, nid);if (addr)memmap_pages_add (DIV_ROUND_UP (size,PAGE_SIZE ));
@@ -344,19 +338,10 @@ static int __meminit init_section_page_estatic void free_page_ext (void *addr) {size_t table_size; -struct page *page; table_size = page_ext_size *PAGES_PER_SECTION ;memmap_pages_add (-1L * (DIV_ROUND_UP (table_size,PAGE_SIZE ))); - -if (is_vmalloc_addr (addr)) { -vfree (addr); - }else { - page =virt_to_page (addr); -BUG_ON (PageReserved (page)); -kmemleak_free (addr); -free_pages_exact (addr, table_size); - } +vfree (addr); }static void __free_page_ext (unsigned long pfn)
_
Patches currently in -mm which might be from [email protected] are
powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
PATCH v3 4/7 mm/page_ext: use common allocation and initialization entry points
In reply to Muchun SongFLATMEM uses separate page_ext_init_flatmem() and page_ext_init_flatmem_late() entry points, while SPARSEMEM performs its work through page_ext_init(). This exposes the memory model in the generic boot sequence and obscures the allocation and client initialization phases.
Rename the early entry point to page_ext_alloc() and use page_ext_init() for client callbacks in both memory models. SPARSEMEM keeps its existing late allocation behavior for now; its page_ext_alloc() remains an empty configuration stub.
This also moves FLATMEM client callbacks from immediately after kmem_cache_init() to after vmalloc_init(). The callbacks still run after the buddy and slab allocators are available, as required by page_owner.
---
v3:
- Collect Reviewed-by from Mike Rapoport and Chris Down
- Collect Tested-by from Chris Down
v2:
- Split the common allocation and initialization entry points into a preparatory patch
- Document the later FLATMEM client callback timing (noted by Chris Down)
--- include/linux/page_ext.h | 20 +++++--------------- mm/mm_init.c | 7 +------ mm/page_ext.c | 5 ++--- 3 files changed, 8 insertions(+), 24 deletions(-)
include/linux/page_ext.h +5 -15
@@ -62,13 +62,10 @@ static inline bool early_page_ext_enabled(void) }#ifdef CONFIG_SPARSEMEM -static inline void page_ext_init_flatmem (void ) -{ -} -extern void page_ext_init (void ); -static inline void page_ext_init_flatmem_late (void ) +static inline void page_ext_alloc (void ) { } +void page_ext_init (void );static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {
@@ -79,11 +76,8 @@ static inline bool page_ext_iter_next_fast_possible(unsigned long next_pfn) return next_pfn % PAGES_PER_SECTION; }#else -extern void page_ext_init_flatmem (void ); -extern void page_ext_init_flatmem_late (void ); -static inline void page_ext_init (void ) -{ -} +void page_ext_alloc (void ); +void page_ext_init (void );static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {
@@ -205,11 +199,7 @@ static inline void page_ext_init(void) { } -static inline void page_ext_init_flatmem_late (void ) -{ -} - -static inline void page_ext_init_flatmem (void ) +static inline void page_ext_alloc (void ) { }
mm/mm_init.c +1 -6
@@ -2709,7 +2709,7 @@ void __init mm_core_init(void)* page_ext requires contiguous pages, * bigger than MAX_PAGE_ORDER unless SPARSEMEM. */ -page_ext_init_flatmem (); +page_ext_alloc ();mem_debugging_and_hardening_init ();kfence_alloc_pool_and_metadata ();report_meminit ();
@@ -2725,11 +2725,6 @@ void __init mm_core_init(void)memblock_free_all ();mem_init ();kmem_cache_init (); -/* -* page_owner must be initialized after buddy is ready, and also after -* slab is ready so that stack_depot_init() works properly -*/ -page_ext_init_flatmem_late ();kmemleak_init ();ptlock_cache_init ();pgtable_cache_init ();
mm/page_ext.c +2 -3
@@ -185,7 +185,7 @@ static inline struct page_ext *get_entry(void *base, unsigned long index) }#ifndef CONFIG_SPARSEMEM -void __init page_ext_init_flatmem_late (void ) +void __init page_ext_init (void ) {invoke_init_callbacks (); }
@@ -243,9 +243,8 @@ static int __init alloc_node_page_ext(int nid)return 0 ; } -void __init page_ext_init_flatmem (void ) +void __init page_ext_alloc (void ) { -int nid, fail;if (!invoke_need_callbacks ())
Signature · 1 line
2.54.0
+ mm-page_ext-use-common-allocation-and-initialization-entry-points.patch added to mm-new branch
The patch titled Subject: mm/page_ext: use common allocation and initialization entry points has been added to the -mm mm-new branch. Its filename is…
The patch titled
Subject: mm/page_ext: use common allocation and initialization entry points
has been added to the -mm mm-new branch. Its filename is
mm-page_ext-use-common-allocation-and-initialization-entry-points.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-page_ext-use-common-allocation-and-initialization-entry-points.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Muchun Song <[email protected]>
Subject: mm/page_ext: use common allocation and initialization entry points
Date: Thu, 19 Nov 2026 14:58:09 +0800
FLATMEM uses separate page_ext_init_flatmem() and page_ext_init_flatmem_late() entry points, while SPARSEMEM performs its work through page_ext_init(). This exposes the memory model in the generic boot sequence and obscures the allocation and client initialization phases.
Rename the early entry point to page_ext_alloc() and use page_ext_init() for client callbacks in both memory models. SPARSEMEM keeps its existing late allocation behavior for now; its page_ext_alloc() remains an empty configuration stub.
This also moves FLATMEM client callbacks from immediately after kmem_cache_init() to after vmalloc_init(). The callbacks still run after the buddy and slab allocators are available, as required by page_owner.
include/linux/page_ext.h | 20 +++++--------------- mm/mm_init.c | 7 +------ mm/page_ext.c | 5 ++--- 3 files changed, 8 insertions(+), 24 deletions(-)
include/linux/page_ext.h renamed from include/linux/page_ext.h~mm-page_ext-use-common-allocation-and-initialization-entry-points +5 -15
@@ -62,13 +62,10 @@ static inline bool early_page_ext_enable }#ifdef CONFIG_SPARSEMEM -static inline void page_ext_init_flatmem (void ) -{ -} -extern void page_ext_init (void ); -static inline void page_ext_init_flatmem_late (void ) +static inline void page_ext_alloc (void ) { } +void page_ext_init (void );static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {
@@ -79,11 +76,8 @@ static inline bool page_ext_iter_next_fa return next_pfn % PAGES_PER_SECTION; }#else -extern void page_ext_init_flatmem (void ); -extern void page_ext_init_flatmem_late (void ); -static inline void page_ext_init (void ) -{ -} +void page_ext_alloc (void ); +void page_ext_init (void );static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {
@@ -205,11 +199,7 @@ static inline void page_ext_init(void) { } -static inline void page_ext_init_flatmem_late (void ) -{ -} - -static inline void page_ext_init_flatmem (void ) +static inline void page_ext_alloc (void ) { }
mm/mm_init.c renamed from mm/mm_init.c~mm-page_ext-use-common-allocation-and-initialization-entry-points +1 -6
@@ -2648,7 +2648,7 @@ void __init mm_core_init(void)* page_ext requires contiguous pages, * bigger than MAX_PAGE_ORDER unless SPARSEMEM. */ -page_ext_init_flatmem (); +page_ext_alloc ();mem_debugging_and_hardening_init ();kfence_alloc_pool_and_metadata ();report_meminit ();
@@ -2664,11 +2664,6 @@ void __init mm_core_init(void)memblock_free_all ();mem_init ();kmem_cache_init (); -/* -* page_owner must be initialized after buddy is ready, and also after -* slab is ready so that stack_depot_init() works properly -*/ -page_ext_init_flatmem_late ();kmemleak_init ();ptlock_cache_init ();pgtable_cache_init ();
mm/page_ext.c renamed from mm/page_ext.c~mm-page_ext-use-common-allocation-and-initialization-entry-points +2 -3
@@ -185,7 +185,7 @@ static inline struct page_ext *get_entry }#ifndef CONFIG_SPARSEMEM -void __init page_ext_init_flatmem_late (void ) +void __init page_ext_init (void ) {invoke_init_callbacks (); }
@@ -243,9 +243,8 @@ static int __init alloc_node_page_ext(inreturn 0 ; } -void __init page_ext_init_flatmem (void ) +void __init page_ext_alloc (void ) { -int nid, fail;if (!invoke_need_callbacks ())
_
Patches currently in -mm which might be from [email protected] are
powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
PATCH v3 5/7 mm/page_ext: allocate page extensions early for SPARSEMEM
In reply to Muchun SongKMSAN needs page extensions when kmsan_init_shadow() installs per-page metadata, before memblock_free_all() releases memory to the buddy allocator. FLATMEM makes page extensions available by this point, whereas SPARSEMEM initializes them later in boot.
Allocate boot-time SPARSEMEM page extensions from memblock to match FLATMEM's initialization timing. This gives early clients the same page_ext availability guarantee across both memory models, while runtime memory hotplug continues to use vmalloc.
---
v3:
- Use DIV_ROUND_UP() for page accounting instead of relying on the caller's size alignment (suggested by Mike Rapoport)
- Collect Reviewed-by from Chris Down and Mike Rapoport
v2:
- Allocate all boot-time SPARSEMEM page extensions from memblock (suggested by Mike Rapoport)
- Keep memblock allocations above DMA and suppress per-section free messages (suggested by Chris Down)
--- include/linux/page_ext.h | 9 +-- mm/mm_init.c | 4 -- mm/page_ext.c | 117 +++++++++++++++++++-------------------- 3 files changed, 59 insertions(+), 71 deletions(-)
include/linux/page_ext.h +2 -7
@@ -61,12 +61,10 @@ static inline bool early_page_ext_enabled(void)return early_page_ext; } -#ifdef CONFIG_SPARSEMEM -static inline void page_ext_alloc (void ) -{ -} +void page_ext_alloc (void );void page_ext_init (void ); +#ifdef CONFIG_SPARSEMEM static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {/*
@@ -76,9 +74,6 @@ static inline bool page_ext_iter_next_fast_possible(unsigned long next_pfn) return next_pfn % PAGES_PER_SECTION; }#else -void page_ext_alloc (void ); -void page_ext_init (void ); -static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {return true;
mm/mm_init.c +0 -4
@@ -2705,10 +2705,6 @@ void __init mm_core_init(void)build_all_zonelists (NULL );page_alloc_init_cpuhp ();alloc_tag_sec_init (); -/* -* page_ext requires contiguous pages, -* bigger than MAX_PAGE_ORDER unless SPARSEMEM. -*/ page_ext_alloc ();mem_debugging_and_hardening_init ();kfence_alloc_pool_and_metadata ();
mm/page_ext.c +57 -60
178 lines not on this page · raw message
Signature · 1 line
2.54.0
+ mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch added to mm-new branch
The patch titled Subject: mm/page_ext: allocate page extensions early for SPARSEMEM has been added to the -mm mm-new branch. Its filename is…
The patch titled
Subject: mm/page_ext: allocate page extensions early for SPARSEMEM
has been added to the -mm mm-new branch. Its filename is
mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Muchun Song <[email protected]>
Subject: mm/page_ext: allocate page extensions early for SPARSEMEM
Date: Thu, 19 Nov 2026 14:58:10 +0800
KMSAN needs page extensions when kmsan_init_shadow() installs per-page metadata, before memblock_free_all() releases memory to the buddy allocator. FLATMEM makes page extensions available by this point, whereas SPARSEMEM initializes them later in boot.
Allocate boot-time SPARSEMEM page extensions from memblock to match FLATMEM's initialization timing. This gives early clients the same page_ext availability guarantee across both memory models, while runtime memory hotplug continues to use vmalloc.
include/linux/page_ext.h | 9 -- mm/mm_init.c | 4 - mm/page_ext.c | 117 ++++++++++++++++++------------------- 3 files changed, 59 insertions(+), 71 deletions(-)
include/linux/page_ext.h renamed from include/linux/page_ext.h~mm-page_ext-allocate-page-extensions-early-for-sparsemem +2 -7
@@ -61,12 +61,10 @@ static inline bool early_page_ext_enablereturn early_page_ext; } -#ifdef CONFIG_SPARSEMEM -static inline void page_ext_alloc (void ) -{ -} +void page_ext_alloc (void );void page_ext_init (void ); +#ifdef CONFIG_SPARSEMEM static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {/*
@@ -76,9 +74,6 @@ static inline bool page_ext_iter_next_fa return next_pfn % PAGES_PER_SECTION; }#else -void page_ext_alloc (void ); -void page_ext_init (void ); -static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {return true;
mm/mm_init.c renamed from mm/mm_init.c~mm-page_ext-allocate-page-extensions-early-for-sparsemem +0 -4
@@ -2644,10 +2644,6 @@ void __init mm_core_init(void)build_all_zonelists (NULL );page_alloc_init_cpuhp ();alloc_tag_sec_init (); -/* -* page_ext requires contiguous pages, -* bigger than MAX_PAGE_ORDER unless SPARSEMEM. -*/ page_ext_alloc ();mem_debugging_and_hardening_init ();kfence_alloc_pool_and_metadata ();
mm/page_ext.c renamed from mm/page_ext.c~mm-page_ext-allocate-page-extensions-early-for-sparsemem +57 -60
178 lines not on this page · raw message
_
Patches currently in -mm which might be from [email protected] are
powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
PATCH v3 6/7 mm/page_ext: inline unchecked page extension lookup
In reply to Muchun SongThe KMSAN runtime needs an unchecked page extension lookup that can be inlined into its uninstrumented compilation unit while page_ext.c remains instrumented.
Provide an inline lookup for this use case while keeping the existing RCU checks for other callers.
---
v3:
- Collect Reviewed-by and Tested-by from Chris Down
v2:
- Add a preparatory patch that inlines the unchecked lookup (suggested by Chris Down)
- Use a PFN interface to avoid architecture header-order dependencies
--- include/linux/page_ext.h | 46 ++++++++++++++++++++++++++++++ mm/page_ext.c | 60 +++------------------------------------- 2 files changed, 50 insertions(+), 56 deletions(-)
include/linux/page_ext.h +46 -0
@@ -64,7 +64,14 @@ static inline bool early_page_ext_enabled(void)void page_ext_alloc (void );void page_ext_init (void ); +static __always_inline struct page_ext *page_ext_entry(void *base, unsigned long index) +{ +return base + page_ext_size * index; +} +#ifdef CONFIG_SPARSEMEM +#define PAGE_EXT_INVALID 0x1 +static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {/*
@@ -73,11 +80,50 @@ static inline bool page_ext_iter_next_fast_possible(unsigned long next_pfn)*/ return next_pfn %PAGES_PER_SECTION ; } + +static __always_inline bool page_ext_invalid(struct page_ext *page_ext) +{ +return !page_ext || (((unsigned long )page_ext &PAGE_EXT_INVALID ) ==PAGE_EXT_INVALID ); +} + +static __always_inline struct page_ext *__page_ext_lookup(unsigned long pfn) +{ +struct mem_section *section =__pfn_to_section (pfn); +struct page_ext *page_ext =READ_ONCE (section->page_ext); + +/* +* The sanity checks the page allocator does upon freeing a page can +* reach here before the page_ext arrays are allocated when feeding a +* range of pages to the allocator for the first time during bootup or +* memory hotplug. +*/ +if (page_ext_invalid (page_ext)) +return NULL ; +return page_ext_entry (page_ext, pfn); +}#else static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {return true; } + +static __always_inline struct page_ext *__page_ext_lookup(unsigned long pfn) +{ +int nid =pfn_to_nid (pfn); +struct page_ext *base =NODE_DATA (nid)->node_page_ext; +unsigned long index; + +/* +* The sanity checks the page allocator does upon freeing a page can +* reach here before the page_ext arrays are allocated when feeding a +* range of pages to the allocator for the first time during bootup or +* memory hotplug. +*/ +if (unlikely (!base)) +return NULL ; + index = pfn -round_down (node_start_pfn (nid),MAX_ORDER_NR_PAGES ); +return page_ext_entry (base, index); +}#endif extern struct page_ext *page_ext_get (const struct page *page);
mm/page_ext.c +4 -56
97 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH v3 6/7] mm/page_ext: inline unchecked page extension lookup
Quoted 10 lines · Muchun Song
The KMSAN runtime needs an unchecked page extension lookup that can be inlined into its uninstrumented compilation unit while
page_ext.cremains instrumented.Provide an inline lookup for this use case while keeping the existing RCU checks for other callers.
Signed-off-by Muchun Song <[email protected]>Reviewed-by Chris Down <[email protected]>Tested-by Chris Down <[email protected]>
Quoted 12 lines
---
v3:
- Collect Reviewed-by and Tested-by from Chris Downv2:
- Add a preparatory patch that inlines the unchecked lookup (suggested by Chris Down)
- Use a PFN interface to avoid architecture header-order dependencies--- include/linux/page_ext.h | 46 ++++++++++++++++++++++++++++++ mm/page_ext.c | 60 +++------------------------------------- 2 files changed, 50 insertions(+), 56 deletions(-)
Signature · 2 lines
Sincerely yours, Mike.
+ mm-page_ext-inline-unchecked-page-extension-lookup.patch added to mm-new branch
In reply to Muchun SongThe patch titled Subject: mm/page_ext: inline unchecked page extension lookup has been added to the -mm mm-new branch. Its filename is mm-page_ext-inline-unchecked-page-extension-lookup.patch
The patch titled
Subject: mm/page_ext: inline unchecked page extension lookup
has been added to the -mm mm-new branch. Its filename is
mm-page_ext-inline-unchecked-page-extension-lookup.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-page_ext-inline-unchecked-page-extension-lookup.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Muchun Song <[email protected]>
Subject: mm/page_ext: inline unchecked page extension lookup
Date: Thu, 19 Nov 2026 14:58:11 +0800
The KMSAN runtime needs an unchecked page extension lookup that can be inlined into its uninstrumented compilation unit while page_ext.c remains instrumented.
Provide an inline lookup for this use case while keeping the existing RCU checks for other callers.
include/linux/page_ext.h | 46 ++++++++++++++++++++++++++++ mm/page_ext.c | 60 ++----------------------------------- 2 files changed, 50 insertions(+), 56 deletions(-)
include/linux/page_ext.h renamed from include/linux/page_ext.h~mm-page_ext-inline-unchecked-page-extension-lookup +46 -0
@@ -64,7 +64,14 @@ static inline bool early_page_ext_enablevoid page_ext_alloc (void );void page_ext_init (void ); +static __always_inline struct page_ext *page_ext_entry(void *base, unsigned long index) +{ +return base + page_ext_size * index; +} +#ifdef CONFIG_SPARSEMEM +#define PAGE_EXT_INVALID 0x1 +static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {/*
@@ -73,11 +80,50 @@ static inline bool page_ext_iter_next_fa*/ return next_pfn %PAGES_PER_SECTION ; } + +static __always_inline bool page_ext_invalid(struct page_ext *page_ext) +{ +return !page_ext || (((unsigned long )page_ext &PAGE_EXT_INVALID ) ==PAGE_EXT_INVALID ); +} + +static __always_inline struct page_ext *__page_ext_lookup(unsigned long pfn) +{ +struct mem_section *section =__pfn_to_section (pfn); +struct page_ext *page_ext =READ_ONCE (section->page_ext); + +/* +* The sanity checks the page allocator does upon freeing a page can +* reach here before the page_ext arrays are allocated when feeding a +* range of pages to the allocator for the first time during bootup or +* memory hotplug. +*/ +if (page_ext_invalid (page_ext)) +return NULL ; +return page_ext_entry (page_ext, pfn); +}#else static inline bool page_ext_iter_next_fast_possible (unsigned long next_pfn) {return true; } + +static __always_inline struct page_ext *__page_ext_lookup(unsigned long pfn) +{ +int nid =pfn_to_nid (pfn); +struct page_ext *base =NODE_DATA (nid)->node_page_ext; +unsigned long index; + +/* +* The sanity checks the page allocator does upon freeing a page can +* reach here before the page_ext arrays are allocated when feeding a +* range of pages to the allocator for the first time during bootup or +* memory hotplug. +*/ +if (unlikely (!base)) +return NULL ; + index = pfn -round_down (node_start_pfn (nid),MAX_ORDER_NR_PAGES ); +return page_ext_entry (base, index); +}#endif extern struct page_ext *page_ext_get (const struct page *page);
mm/page_ext.c renamed from mm/page_ext.c~mm-page_ext-inline-unchecked-page-extension-lookup +4 -56
97 lines not on this page · raw message
_
Patches currently in -mm which might be from [email protected] are
powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
PATCH v3 7/7 kmsan: fix metadata loss during HugeTLB struct page reinitialization
In reply to Muchun SongKMSAN stores each page's shadow and origin pointers in struct page. Boot-time gigantic HugeTLB pages are reinitialized after kmsan_init_shadow() installs these pointers, dropping the mappings and leaving the allocated metadata pages unreachable.
Move the pointers to page_ext, whose boot-time storage is available before kmsan_init_shadow() and survives struct page reinitialization. Use the inline unchecked lookup under notrace preemption so metadata lookup cannot recurse through instrumented hooks.
This shrinks struct page from 80 to 64 bytes on x86-64 KMSAN builds, making its size a power of two and allowing HugeTLB Vmemmap Optimization with KMSAN.
---
v3:
- Collect Reviewed-by and Tested-by from Chris Down
v2:
- Update __mm_zero_struct_page() for the smaller struct page.
- Keep the unchecked lookup inline in KMSAN and leave page_ext.c instrumented (suggested by Chris Down)
- Document page_ext metadata storage and KMSAN HVO support (suggested by Chris Down)
- Use the common boot-time page_ext allocation instead of need_early
--- Documentation/dev-tools/kmsan.rst | 8 +++--- include/linux/kmsan.h | 2 ++ include/linux/mm.h | 12 +++------ include/linux/mm_types.h | 12 --------- lib/Kconfig.kmsan | 1 + mm/kmsan/shadow.c | 45 ++++++++++++++++++++++++++----- mm/page_ext.c | 4 +++ 7 files changed, 52 insertions(+), 32 deletions(-)
Documentation/dev-tools/kmsan.rst +3 -5
@@ -376,13 +376,11 @@ Metadata allocation There are several places in the kernel for which the metadata is stored. -1. Each ``struct page`` instance contains two pointers to its shadow and -origin pages:: +1. Each page frame has a corresponding ``struct page_metadata`` entry containing +two pointers to its shadow and origin pages:: - struct page { - ... + struct page_metadata { struct page *shadow, *origin; - ... }; At boot-time, the kernel allocates shadow and origin pages for every available
include/linux/kmsan.h +2 -0
@@ -15,12 +15,14 @@#include <linux/types.h> struct page ; +struct page_ext_operations ;struct kmem_cache ;struct task_struct ;struct scatterlist ;struct urb ;#ifdef CONFIG_KMSAN +extern struct page_ext_operations kmsan_ops;/** * kmsan_task_create() - Initialize KMSAN state for the task.
include/linux/mm.h +3 -9
@@ -134,7 +134,7 @@ extern int mmap_rnd_compat_bits __read_mostly;* define their own version of this macro in <asm/pgtable.h> */ #if BITS_PER_LONG ==64 -/* This function must be updated when the size of struct page grows above 96 +/* This function must be updated when the size of struct page grows above 80 * or reduces below 56. The idea that compiler optimizes out switch() * statement, and only leaves move/store instructions. Also the compiler can * combine write statements if they are both assignments and can be reordered,
@@ -145,18 +145,12 @@ static inline void __mm_zero_struct_page(struct page *page) {unsigned long *_pp = (void *)page; -/* Check that struct page is either 56, 64, 72, 80, 88 or 96 bytes */ +/* Check that struct page is either 56, 64, 72 or 80 bytes */ BUILD_BUG_ON (sizeof (struct page ) &7 );BUILD_BUG_ON (sizeof (struct page ) <56 ); -BUILD_BUG_ON (sizeof (struct page ) >96 ); +BUILD_BUG_ON (sizeof (struct page ) >80 );switch (sizeof (struct page )) { -case 96 : - _pp[11 ] =0 ; - fallthrough; -case 88 : - _pp[10 ] =0 ; - fallthrough;case 80 : _pp[9 ] =0 ; fallthrough;
include/linux/mm_types.h +0 -12
@@ -208,18 +208,6 @@ struct page {#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS int _last_cpupid;#endif - -#ifdef CONFIG_KMSAN -/* -* KMSAN metadata for this page: -* - shadow page: every bit indicates whether the corresponding -* bit of the original page is initialized (0) or not (1); -* - origin page: every 4 bytes contain an id of the stack trace -* where the uninitialized value was created. -*/ -struct page *kmsan_shadow; -struct page *kmsan_origin; -#endif } _struct_page_alignment;/*
lib/Kconfig.kmsan +1 -0
@@ -10,6 +10,7 @@ config KMSAN depends on HAVE_ARCH_KMSAN && HAVE_KMSAN_COMPILER depends on DEBUG_KERNEL && !KASAN && !KCSAN depends on !PREEMPT_RT + select PAGE_EXTENSION select STACKDEPOT select STACKDEPOT_ALWAYS_INIT help
mm/kmsan/shadow.c +39 -6
68 lines not on this page · raw message
mm/page_ext.c +4 -0
@@ -12,6 +12,7 @@#include <linux/rcupdate.h> #include <linux/pgalloc_tag.h> #include <linux/iommu-debug-pagealloc.h> +#include <linux/kmsan.h> #include <linux/slab.h> /*
@@ -87,6 +88,9 @@ static struct page_ext_operations *page_ext_ops[] __initdata = {#ifdef CONFIG_PAGE_TABLE_CHECK &page_table_check_ops,#endif +#ifdef CONFIG_KMSAN + &kmsan_ops, +#endif #ifdef CONFIG_IOMMU_DEBUG_PAGEALLOC &page_iommu_debug_ops,#endif
Signature · 1 line
2.54.0
+ kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch added to mm-new branch
The patch titled Subject: kmsan: fix metadata loss during HugeTLB struct page reinitialization has been added to the -mm mm-new branch. Its filename is…
The patch titled
Subject: kmsan: fix metadata loss during HugeTLB struct page reinitialization
has been added to the -mm mm-new branch. Its filename is
kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Muchun Song <[email protected]>
Subject: kmsan: fix metadata loss during HugeTLB struct page reinitialization
Date: Thu, 19 Nov 2026 14:58:12 +0800
KMSAN stores each page's shadow and origin pointers in struct page. Boot-time gigantic HugeTLB pages are reinitialized after kmsan_init_shadow() installs these pointers, dropping the mappings and leaving the allocated metadata pages unreachable.
Move the pointers to page_ext, whose boot-time storage is available before kmsan_init_shadow() and survives struct page reinitialization. Use the inline unchecked lookup under notrace preemption so metadata lookup cannot recurse through instrumented hooks.
This shrinks struct page from 80 to 64 bytes on x86-64 KMSAN builds, making its size a power of two and allowing HugeTLB Vmemmap Optimization with KMSAN.
Documentation/dev-tools/kmsan.rst | 8 +--- include/linux/kmsan.h | 2 + include/linux/mm.h | 12 +------ include/linux/mm_types.h | 12 ------- lib/Kconfig.kmsan | 1 mm/kmsan/shadow.c | 45 ++++++++++++++++++++++++---- mm/page_ext.c | 4 ++ 7 files changed, 52 insertions(+), 32 deletions(-)
Documentation/dev-tools/kmsan.rst renamed from Documentation/dev-tools/kmsan.rst~kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization +3 -5
@@ -376,13 +376,11 @@ Metadata allocation There are several places in the kernel for which the metadata is stored. -1. Each ``struct page`` instance contains two pointers to its shadow and -origin pages:: +1. Each page frame has a corresponding ``struct page_metadata`` entry containing +two pointers to its shadow and origin pages:: - struct page { - ... + struct page_metadata { struct page *shadow, *origin; - ... }; At boot-time, the kernel allocates shadow and origin pages for every available
include/linux/kmsan.h renamed from include/linux/kmsan.h~kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization +2 -0
@@ -15,12 +15,14 @@#include <linux/types.h> struct page ; +struct page_ext_operations ;struct kmem_cache ;struct task_struct ;struct scatterlist ;struct urb ;#ifdef CONFIG_KMSAN +extern struct page_ext_operations kmsan_ops;/** * kmsan_task_create() - Initialize KMSAN state for the task.
include/linux/mm.h renamed from include/linux/mm.h~kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization +3 -9
@@ -134,7 +134,7 @@ extern int mmap_rnd_compat_bits __read_m* define their own version of this macro in <asm/pgtable.h> */ #if BITS_PER_LONG ==64 -/* This function must be updated when the size of struct page grows above 96 +/* This function must be updated when the size of struct page grows above 80 * or reduces below 56. The idea that compiler optimizes out switch() * statement, and only leaves move/store instructions. Also the compiler can * combine write statements if they are both assignments and can be reordered,
@@ -145,18 +145,12 @@ static inline void __mm_zero_struct_page {unsigned long *_pp = (void *)page; -/* Check that struct page is either 56, 64, 72, 80, 88 or 96 bytes */ +/* Check that struct page is either 56, 64, 72 or 80 bytes */ BUILD_BUG_ON (sizeof (struct page ) &7 );BUILD_BUG_ON (sizeof (struct page ) <56 ); -BUILD_BUG_ON (sizeof (struct page ) >96 ); +BUILD_BUG_ON (sizeof (struct page ) >80 );switch (sizeof (struct page )) { -case 96 : - _pp[11 ] =0 ; - fallthrough; -case 88 : - _pp[10 ] =0 ; - fallthrough;case 80 : _pp[9 ] =0 ; fallthrough;
include/linux/mm_types.h renamed from include/linux/mm_types.h~kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization +0 -12
@@ -208,18 +208,6 @@ struct page {#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS int _last_cpupid;#endif - -#ifdef CONFIG_KMSAN -/* -* KMSAN metadata for this page: -* - shadow page: every bit indicates whether the corresponding -* bit of the original page is initialized (0) or not (1); -* - origin page: every 4 bytes contain an id of the stack trace -* where the uninitialized value was created. -*/ -struct page *kmsan_shadow; -struct page *kmsan_origin; -#endif } _struct_page_alignment;/*
lib/Kconfig.kmsan renamed from lib/Kconfig.kmsan~kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization +1 -0
@@ -10,6 +10,7 @@ config KMSAN depends on HAVE_ARCH_KMSAN && HAVE_KMSAN_COMPILER depends on DEBUG_KERNEL && !KASAN && !KCSAN depends on !PREEMPT_RT + select PAGE_EXTENSION select STACKDEPOT select STACKDEPOT_ALWAYS_INIT help
mm/kmsan/shadow.c renamed from mm/kmsan/shadow.c~kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization +39 -6
68 lines not on this page · raw message
mm/page_ext.c renamed from mm/page_ext.c~kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization +4 -0
@@ -12,6 +12,7 @@#include <linux/rcupdate.h> #include <linux/pgalloc_tag.h> #include <linux/iommu-debug-pagealloc.h> +#include <linux/kmsan.h> #include <linux/slab.h> /*
@@ -87,6 +88,9 @@ static struct page_ext_operations *page_#ifdef CONFIG_PAGE_TABLE_CHECK &page_table_check_ops,#endif +#ifdef CONFIG_KMSAN + &kmsan_ops, +#endif #ifdef CONFIG_IOMMU_DEBUG_PAGEALLOC &page_iommu_debug_ops,#endif
_
Patches currently in -mm which might be from [email protected] are
powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch