mm: kmsan: fix iounmap metadata teardown
Subsystems (3)
PATCH v2 1/2 mm: kmsan: fix iounmap metadata teardown
kmsan_vmalloc_to_page_or_null() rejects addresses outside the regular vmalloc and module ranges, including the shadow and origin addresses passed by its callers. As a result, iounmap does not free the metadata backing pages. Also, the first iteration of the teardown loop unmaps the entire metadata range, preventing subsequent iterations from finding their pages even if the address lookup is fixed.
Factor the page-table walk out of vmalloc_to_page() into __vmalloc_to_page(), keeping the address check in the public wrapper. Use the unchecked helper in kmsan_vmalloc_to_page_or_null() and check for NULL before converting the returned page to a PFN. Mark __vmalloc_to_page() as __always_inline to avoid an additional function call in vmalloc_to_page().
Move metadata teardown into kmsan_iounmap_pages(). Free the backing blocks while their mappings are still available, then unmap each metadata range once and flush its TLB entries.
---
Changes in v2:
- Share the vmalloc page-table walk instead of adding a KMSAN PTE walker.
- Keep kmsan_vmalloc_to_page_or_null() and handle absent mappings.
mm/kmsan/core.c | 8 +++----- mm/kmsan/hooks.c | 51 +++++++++++++++++++++++++++++++----------------- mm/vmalloc.c | 21 ++++++++++++-------- mm/vmalloc.h | 1 + 4 files changed, 50 insertions(+), 31 deletions(-)
mm/kmsan/core.c +3 -5
@@ -27,6 +27,7 @@#include <linux/vmalloc.h> #include "../slab.h" +#include "../vmalloc.h" #include "kmsan.h" bool kmsan_enabled__read_mostly ;
@@ -240,11 +241,8 @@ struct page *kmsan_vmalloc_to_page_or_null(void *vaddr) {struct page *page; -if (!kmsan_internal_is_vmalloc_addr (vaddr) && - !kmsan_internal_is_module_addr (vaddr)) -return NULL ; - page =vmalloc_to_page (vaddr); -if (pfn_valid (page_to_pfn (page))) + page =__vmalloc_to_page (vaddr); +if (page &&pfn_valid (page_to_pfn (page)))return page;else return NULL ;
mm/kmsan/hooks.c +33 -18
73 lines not on this page · raw message
mm/vmalloc.c +13 -8
@@ -817,9 +817,10 @@ EXPORT_SYMBOL_GPL(is_vmalloc_or_module_addr);/* * Walk a vmap address to the struct page it maps. Huge vmap mappings will * return the tail page that corresponds to the base page address, which -* matches small vmap mappings. +* matches small vmap mappings. Unlike vmalloc_to_page(), this also accepts +* addresses outside the vmalloc and module ranges, such as KMSAN metadata. */ -struct page *vmalloc_to_page (const void *vmalloc_addr) +__always_inline struct page *__vmalloc_to_page(const void *vmalloc_addr) {unsigned long addr = (unsigned long ) vmalloc_addr;struct page *page =NULL ;
@@ -829,12 +830,6 @@ struct page *vmalloc_to_page(const void *vmalloc_addr)pmd_t *pmd;pte_t *ptep, pte; -/* -* XXX we might need to change this if we add VIRTUAL_BUG_ON for -* architectures that do not vmalloc module space -*/ -VIRTUAL_BUG_ON (!is_vmalloc_or_module_addr (vmalloc_addr)); -if (pgd_none (*pgd))return NULL ;if (WARN_ON_ONCE (pgd_leaf (*pgd)))
@@ -873,6 +868,16 @@ struct page *vmalloc_to_page(const void *vmalloc_addr)return page; } + +struct page *vmalloc_to_page (const void *vmalloc_addr) +{ +/* +* XXX we might need to change this if we add VIRTUAL_BUG_ON for +* architectures that do not vmalloc module space +*/ +VIRTUAL_BUG_ON (!is_vmalloc_or_module_addr (vmalloc_addr)); +return __vmalloc_to_page (vmalloc_addr); +}EXPORT_SYMBOL (vmalloc_to_page);/*
mm/vmalloc.h +1 -0
@@ -8,6 +8,7 @@#include <linux/vmalloc.h> #ifdef CONFIG_MMU +struct page *__vmalloc_to_page (const void *vmalloc_addr);void __init vmalloc_init (void ); int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages,
base-commit: 587858367581b9c55c3690f4e63382ad622719d4
PATCH v2 2/2 mm: kmsan: fix ioremap error cleanup
Looking further at kmsan_ioremap_page_range(), I found three cases where error cleanup leaks metadata blocks.
1. If the first iteration fails, clean is zero and cleanup is skipped, leaking any allocations that succeeded in that iteration.
2. If shadow mapping succeeds but origin mapping fails, the shadow pointer has already been cleared. Removing its mapping loses the backing block.
3. On failures after completed iterations, cleanup removes the earlier metadata mappings without freeing their backing blocks.
The cleanup needed here is the same as for iounmap, so it makes sense to reuse kmsan_iounmap_pages(). Track the end of installed mappings with mapped_end and advance it after each successful shadow mapping. This includes the current shadow block if origin mapping subsequently fails, while the helper skips the missing origin mapping.
Run cleanup whenever err is non-zero. Free allocations that have not been mapped directly, and use the shared helper to unmap and free the installed metadata, including blocks from completed iterations.
---
Changes in v2:
- No code changes.
- Add Reviewed-by Alexander Potapenko from v1.
I tested this series on Linux 7.3-rc3 under QEMU, using ioremap()/iounmap() calls on the QEMU VGA BAR0. All tested mappings were torn down without metadata leaks.
mm/kmsan/hooks.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-)
mm/kmsan/hooks.c +12 -23
@@ -186,16 +186,17 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end,gfp_t gfp_mask =GFP_KERNEL | __GFP_ZERO;struct page *shadow, *origin;unsigned long off =0 ; -int nr, err =0 , clean =0 , mapped; +unsigned long mapped_end = start; +int nr, err =0 , mapped;if (!kmsan_enabled ||kmsan_in_runtime ())return 0 ; nr = (end - start) /PAGE_SIZE ;kmsan_enter_runtime (); -for (int i =0 ; i < nr; i++, off +=PAGE_SIZE , clean = i) { - shadow =alloc_pages (gfp_mask,1 ); - origin =alloc_pages (gfp_mask,1 ); +for (int i =0 ; i < nr; i++, off +=PAGE_SIZE ) { + shadow =alloc_pages (gfp_mask,KMSAN_IOREMAP_META_ORDER ); + origin =alloc_pages (gfp_mask,KMSAN_IOREMAP_META_ORDER );if (!shadow || !origin) { err = -ENOMEM ; goto ret;
@@ -209,39 +210,27 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end, goto ret; } shadow =NULL ; + mapped_end = start + off +PAGE_SIZE ; mapped =__vmap_pages_range_noflush (vmalloc_origin (start + off),vmalloc_origin (start + off +PAGE_SIZE ), prot, &origin,PAGE_SHIFT );if (mapped) { -__vunmap_range_noflush ( -vmalloc_shadow (start + off), -vmalloc_shadow (start + off +PAGE_SIZE )); err = mapped; goto ret; } origin =NULL ; } -/* Page mapping loop finished normally, nothing to clean up. */ - clean =0 ; ret: -if (clean >0 ) { -/* -* Something went wrong. Clean up shadow/origin pages allocated -* on the last loop iteration, then delete mappings created -* during the previous iterations. -*/ +if (err) {if (shadow) -__free_pages (shadow,1 ); +__free_pages (shadow,KMSAN_IOREMAP_META_ORDER );if (origin) -__free_pages (origin,1 ); -__vunmap_range_noflush ( -vmalloc_shadow (start), -vmalloc_shadow (start + clean *PAGE_SIZE )); -__vunmap_range_noflush ( -vmalloc_origin (start), -vmalloc_origin (start + clean *PAGE_SIZE )); +__free_pages (origin,KMSAN_IOREMAP_META_ORDER ); + +if (mapped_end > start) +kmsan_iounmap_pages (start, mapped_end); }flush_cache_vmap (vmalloc_shadow (start),vmalloc_shadow (end));flush_cache_vmap (vmalloc_origin (start),vmalloc_origin (end));
Andrew Morton
maintainerQuoted 14 lines · On Fri, 2 Oct 2026 23:05:07 +0300 Dima Koziuk <[email protected]> wrote:
kmsan_vmalloc_to_page_or_null()rejects addresses outside the regular vmalloc and module ranges, including the shadow and origin addresses passed by its callers. As a result, iounmap does not free the metadata backing pages. Also, the first iteration of the teardown loop unmaps the entire metadata range, preventing subsequent iterations from finding their pages even if the address lookup is fixed.Factor the page-table walk out of
vmalloc_to_page()into__vmalloc_to_page(), keeping the address check in the public wrapper. Use the unchecked helper inkmsan_vmalloc_to_page_or_null()and check for NULL before converting the returned page to a PFN. Mark__vmalloc_to_page()as__always_inlineto avoid an additional function call invmalloc_to_page().
Quoted 3 lines
Move metadata teardown into
kmsan_iounmap_pages(). Free the backing blocks while their mappings are still available, then unmap each metadata range once and flush its TLB entries.
Thanks, I'll queue these for test and further review.
Could people please offer opinions on whether we should backport one/both into -stable kernels?
Alexander Potapenko
maintainerQuoted 17 lines · Andrew Morton
Quoted 14 lines · On Fri, 2 Oct 2026 23:05:07 +0300 Dima Koziuk <[email protected]> wrote:
kmsan_vmalloc_to_page_or_null()rejects addresses outside the regular vmalloc and module ranges, including the shadow and origin addresses passed by its callers. As a result, iounmap does not free the metadata backing pages. Also, the first iteration of the teardown loop unmaps the entire metadata range, preventing subsequent iterations from finding their pages even if the address lookup is fixed.Factor the page-table walk out of
vmalloc_to_page()into__vmalloc_to_page(), keeping the address check in the public wrapper. Use the unchecked helper inkmsan_vmalloc_to_page_or_null()and check for NULL before converting the returned page to a PFN. Mark__vmalloc_to_page()as__always_inlineto avoid an additional function call invmalloc_to_page().
Quoted 9 lines
Quoted 3 lines
Move metadata teardown into
kmsan_iounmap_pages(). Free the backing blocks while their mappings are still available, then unmap each metadata range once and flush its TLB entries.Thanks, I'll queue these for test and further review.
Could people please offer opinions on whether we should backport one/both into -stable kernels?
This bug is quite old, and it only causes leaks in a KMSAN build. So stable backports are probably not needed.