* [PATCH v6 01/10] mm/vmalloc: allow the vmap unmap paths to run in atomic context
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 02/10] mm/kmsan: undo the shadow mapping when the origin mapping fails Hao Ge
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge, Sashiko
The vmap failure paths can be reached with GFP_ATOMIC or GFP_NOWAIT
since commit b186a94227b7 ("kmsan: remove hard-coded GFP_KERNEL
flags") and commit 8da89ba18ed4 ("mm/vmalloc: handle non-blocking
GFP in __vmalloc_area_node()"). An undo of a partially installed
mapping has to run in atomic context there, and the cond_resched()
in vunmap_pmd_range() cannot schedule.
Give __vunmap_range_noflush() and kmsan_vunmap_range_noflush() a
can_resched argument and skip the cond_resched() when it is false.
The existing callers are on sleepable paths and pass true, so nothing
changes for them.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-178439@kernel.org/
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
include/linux/kmsan.h | 8 ++++++--
mm/kmsan/hooks.c | 14 +++++++++-----
mm/vmalloc.c | 24 ++++++++++++++----------
mm/vmalloc.h | 3 ++-
4 files changed, 31 insertions(+), 18 deletions(-)
diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h
index 08fb79566235..e071c33abded 100644
--- a/include/linux/kmsan.h
+++ b/include/linux/kmsan.h
@@ -152,11 +152,14 @@ int __must_check kmsan_vmap_pages_range_noflush(unsigned long start,
* kmsan_vunmap_kernel_range_noflush() - Notify KMSAN about a vunmap.
* @start: start of vunmapped range.
* @end: end of vunmapped range.
+ * @can_resched: whether the unmap walk may call cond_resched(); pass
+ * false from atomic context.
*
* KMSAN unmaps the contiguous metadata ranges created by
* kmsan_map_kernel_range_noflush().
*/
-void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end);
+void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end,
+ bool can_resched);
/**
* kmsan_ioremap_page_range() - Notify KMSAN about a ioremap_page_range() call.
@@ -357,7 +360,8 @@ static inline int __must_check kmsan_vmap_pages_range_noflush(
}
static inline void kmsan_vunmap_range_noflush(unsigned long start,
- unsigned long end)
+ unsigned long end,
+ bool can_resched)
{
}
diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c
index a706666db097..69008b085658 100644
--- a/mm/kmsan/hooks.c
+++ b/mm/kmsan/hooks.c
@@ -136,10 +136,13 @@ static unsigned long vmalloc_origin(unsigned long addr)
KMSAN_META_ORIGIN);
}
-void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end)
+void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end,
+ bool can_resched)
{
- __vunmap_range_noflush(vmalloc_shadow(start), vmalloc_shadow(end));
- __vunmap_range_noflush(vmalloc_origin(start), vmalloc_origin(end));
+ __vunmap_range_noflush(vmalloc_shadow(start), vmalloc_shadow(end),
+ can_resched);
+ __vunmap_range_noflush(vmalloc_origin(start), vmalloc_origin(end),
+ can_resched);
flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
}
@@ -168,8 +171,9 @@ static void kmsan_iounmap_pages(unsigned long start, unsigned long end)
if (origin)
__free_pages(origin, KMSAN_IOREMAP_META_ORDER);
}
- __vunmap_range_noflush(shadow_start, shadow_end);
- __vunmap_range_noflush(origin_start, origin_end);
+ /* ioremap metadata mappings are always GFP_KERNEL, so resched is safe. */
+ __vunmap_range_noflush(shadow_start, shadow_end, true);
+ __vunmap_range_noflush(origin_start, origin_end, true);
flush_tlb_kernel_range(shadow_start, shadow_end);
flush_tlb_kernel_range(origin_start, origin_end);
}
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index ee722162da7c..1fe81a7e617b 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -429,7 +429,7 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
}
static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
- pgtbl_mod_mask *mask)
+ pgtbl_mod_mask *mask, bool can_resched)
{
pmd_t *pmd;
unsigned long next;
@@ -451,12 +451,14 @@ static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
continue;
vunmap_pte_range(pmd, addr, next, mask);
- cond_resched();
+ /* The undo path may run in atomic context. */
+ if (can_resched)
+ cond_resched();
} while (pmd++, addr = next, addr != end);
}
static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
- pgtbl_mod_mask *mask)
+ pgtbl_mod_mask *mask, bool can_resched)
{
pud_t *pud;
unsigned long next;
@@ -476,12 +478,12 @@ static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
}
if (pud_none_or_clear_bad(pud))
continue;
- vunmap_pmd_range(pud, addr, next, mask);
+ vunmap_pmd_range(pud, addr, next, mask, can_resched);
} while (pud++, addr = next, addr != end);
}
static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
- pgtbl_mod_mask *mask)
+ pgtbl_mod_mask *mask, bool can_resched)
{
p4d_t *p4d;
unsigned long next;
@@ -496,7 +498,7 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
if (p4d_none_or_clear_bad(p4d))
continue;
- vunmap_pud_range(p4d, addr, next, mask);
+ vunmap_pud_range(p4d, addr, next, mask, can_resched);
} while (p4d++, addr = next, addr != end);
}
@@ -512,7 +514,8 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
*
* This is an internal function only. Do not use outside mm/.
*/
-void __vunmap_range_noflush(unsigned long start, unsigned long end)
+void __vunmap_range_noflush(unsigned long start, unsigned long end,
+ bool can_resched)
{
unsigned long next;
pgd_t *pgd;
@@ -527,7 +530,7 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end)
mask |= PGTBL_PGD_MODIFIED;
if (pgd_none_or_clear_bad(pgd))
continue;
- vunmap_p4d_range(pgd, addr, next, &mask);
+ vunmap_p4d_range(pgd, addr, next, &mask, can_resched);
} while (pgd++, addr = next, addr != end);
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
@@ -536,8 +539,9 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end)
void vunmap_range_noflush(unsigned long start, unsigned long end)
{
- kmsan_vunmap_range_noflush(start, end);
- __vunmap_range_noflush(start, end);
+ /* The unmap paths are sleepable, keep the resched point. */
+ kmsan_vunmap_range_noflush(start, end, true);
+ __vunmap_range_noflush(start, end, true);
}
/**
diff --git a/mm/vmalloc.h b/mm/vmalloc.h
index d8372a4f65c4..4ec114a75f04 100644
--- a/mm/vmalloc.h
+++ b/mm/vmalloc.h
@@ -44,6 +44,7 @@ int __must_check __vmap_pages_range_noflush(unsigned long addr,
void vunmap_range_noflush(unsigned long start, unsigned long end);
-void __vunmap_range_noflush(unsigned long start, unsigned long end);
+void __vunmap_range_noflush(unsigned long start, unsigned long end,
+ bool can_resched);
#endif /* __MM_VMALLOC_H */
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 02/10] mm/kmsan: undo the shadow mapping when the origin mapping fails
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
2026-12-03 9:27 ` [PATCH v6 01/10] mm/vmalloc: allow the vmap unmap paths to run in atomic context Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 03/10] mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment Hao Ge
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge, stable
kmsan_vmap_pages_range_noflush() first maps the shadow pages and then
the origin pages. If the second mapping fails, the first one is left
mapped, and the callers do not roll it back. The next vmap of the
same metadata range hits the stale PTEs again and fails with a
WARN_ON() and -EBUSY in vmap_pages_pte_range().
Undo the shadow mapping on that error path and flush the cache and the
TLB for the shadow range. The mapping may still be cached even though
nothing accessed it, and Documentation/core-api/cachetlb.rst requires
the cache flush to happen before the page table change and the TLB
flush after it.
Fixes: 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")
Cc: stable@vger.kernel.org
Suggested-by: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
mm/kmsan/shadow.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c
index fa2a403d87c2..1af03f83d2ea 100644
--- a/mm/kmsan/shadow.c
+++ b/mm/kmsan/shadow.c
@@ -343,6 +343,13 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end,
o_pages, page_shift);
kmsan_leave_runtime();
if (mapped) {
+ /* Undo the shadow mapping set up above. */
+ flush_cache_vunmap(shadow_start, shadow_end);
+ kmsan_enter_runtime();
+ __vunmap_range_noflush(shadow_start, shadow_end,
+ gfpflags_allow_blocking(gfp_mask));
+ kmsan_leave_runtime();
+ flush_tlb_kernel_range(shadow_start, shadow_end);
err = mapped;
goto ret;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 03/10] mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
2026-12-03 9:27 ` [PATCH v6 01/10] mm/vmalloc: allow the vmap unmap paths to run in atomic context Hao Ge
2026-12-03 9:27 ` [PATCH v6 02/10] mm/kmsan: undo the shadow mapping when the origin mapping fails Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 04/10] mm/kmsan: fix the metadata cache flush ordering Hao Ge
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge, stable
The comment asks the caller to call flush_cache_vmap() before calling
this function. flush_cache_vunmap() is meant here, the old comment that
commit 4ad0ae8c64ac ("mm/vmalloc: remove unmap_kernel_range") replaced
said flush_cache_vunmap(), and Documentation/core-api/cachetlb.rst and
vunmap_range() both use flush_cache_vunmap() before the page tables are
torn down.
Fixes: 4ad0ae8c64ac ("mm/vmalloc: remove unmap_kernel_range")
Cc: stable@vger.kernel.org
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
mm/vmalloc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 1fe81a7e617b..1580b2c72727 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -506,7 +506,7 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
* vunmap_range_noflush is similar to vunmap_range, but does not
* flush caches or TLBs.
*
- * The caller is responsible for calling flush_cache_vmap() before calling
+ * The caller is responsible for calling flush_cache_vunmap() before calling
* this function, and flush_tlb_kernel_range after it has returned
* successfully (and before the addresses are expected to cause a page fault
* or be re-mapped for something else, if TLB flushes are being delayed or
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 04/10] mm/kmsan: fix the metadata cache flush ordering
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
` (2 preceding siblings ...)
2026-12-03 9:27 ` [PATCH v6 03/10] mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 05/10] mm/vmalloc: reject an unaligned range in vmap_page_range() Hao Ge
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge, stable
kmsan_vunmap_range_noflush(), kmsan_iounmap_page_range() and the
error cleanup of kmsan_ioremap_page_range() all call
flush_cache_vmap() after the page tables were cleared.
Documentation/core-api/cachetlb.rst is explicit about the order.
6) void flush_cache_vmap(unsigned long start, unsigned long end)
void flush_cache_vunmap(unsigned long start, unsigned long end)
The first of these two routines is invoked after vmap_range()
has installed the page table entries. The second is invoked
before vunmap_range() deletes the page table entries.
Switch the teardown paths to flush_cache_vunmap() before clearing
the PTEs, and before the metadata pages are freed: a dirty cache line of
a freed page may be written back after the page has been handed out to
somebody else. The iounmap side does the flush inside
kmsan_iounmap_pages(). The successful install path of
kmsan_ioremap_page_range() keeps its flush_cache_vmap(), as it runs
after the install.
Fixes: b073d7f8aee4 ("mm: kmsan: maintain KMSAN metadata for page operations")
Cc: stable@vger.kernel.org
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
mm/kmsan/hooks.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c
index 69008b085658..f2d6a598ad3c 100644
--- a/mm/kmsan/hooks.c
+++ b/mm/kmsan/hooks.c
@@ -139,12 +139,12 @@ static unsigned long vmalloc_origin(unsigned long addr)
void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end,
bool can_resched)
{
+ flush_cache_vunmap(vmalloc_shadow(start), vmalloc_shadow(end));
+ flush_cache_vunmap(vmalloc_origin(start), vmalloc_origin(end));
__vunmap_range_noflush(vmalloc_shadow(start), vmalloc_shadow(end),
can_resched);
__vunmap_range_noflush(vmalloc_origin(start), vmalloc_origin(end),
can_resched);
- flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
- flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
}
#define KMSAN_IOREMAP_META_ORDER 1
@@ -162,6 +162,10 @@ static void kmsan_iounmap_pages(unsigned long start, unsigned long end)
nr = (end - start) / PAGE_SIZE;
v_shadow = shadow_start;
v_origin = origin_start;
+
+ flush_cache_vunmap(shadow_start, shadow_end);
+ flush_cache_vunmap(origin_start, origin_end);
+
for (int i = 0; i < nr;
i++, v_shadow += PAGE_SIZE, v_origin += PAGE_SIZE) {
shadow = kmsan_vmalloc_to_page_or_null((void *)v_shadow);
@@ -171,6 +175,7 @@ static void kmsan_iounmap_pages(unsigned long start, unsigned long end)
if (origin)
__free_pages(origin, KMSAN_IOREMAP_META_ORDER);
}
+
/* ioremap metadata mappings are always GFP_KERNEL, so resched is safe. */
__vunmap_range_noflush(shadow_start, shadow_end, true);
__vunmap_range_noflush(origin_start, origin_end, true);
@@ -235,9 +240,10 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end,
if (mapped_end > start)
kmsan_iounmap_pages(start, mapped_end);
+ } else {
+ flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
+ flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
}
- flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
- flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
kmsan_leave_runtime();
return err;
}
@@ -249,8 +255,6 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end)
kmsan_enter_runtime();
kmsan_iounmap_pages(start, end);
- flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
- flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
kmsan_leave_runtime();
}
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 05/10] mm/vmalloc: reject an unaligned range in vmap_page_range()
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
` (3 preceding siblings ...)
2026-12-03 9:27 ` [PATCH v6 04/10] mm/kmsan: fix the metadata cache flush ordering Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 06/10] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush() Hao Ge
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge
commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not
PAGE_SIZE aligned") put the check in vmap_pte_range(), which is too
late, because the upper levels may already have installed page tables
for a range that is rejected anyway.
The pte loop maps whole pages and stops only when addr lands exactly
on end. An unaligned end lets addr jump over it and the loop keeps
mapping past the range. An unaligned start maps the page below the
range and usually misses end too. Such a range is never a legitimate
request.
vmap_range_noflush() has a single caller, vmap_page_range(). Warn on
addr and end there and bail out with -EINVAL before any page table is
installed, and drop the check from vmap_pte_range().
Suggested-by: Suren Baghdasaryan <surenb@google.com>
Suggested-by: Dev Jain <dev.jain@arm.com>
Reviewed-by: Dev Jain <dev.jain@arm.com>
Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
mm/vmalloc.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 1580b2c72727..081c672f63ee 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -125,9 +125,6 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
unsigned long size;
unsigned int nr_pages;
- if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
- return -EINVAL;
-
pfn = phys_addr >> PAGE_SHIFT;
pte = pte_alloc_kernel_track(pmd, addr, mask);
if (!pte)
@@ -369,6 +366,11 @@ int vmap_page_range(unsigned long addr, unsigned long end,
{
int err;
+ if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
+ return -EINVAL;
+ if (WARN_ON_ONCE(!PAGE_ALIGNED(end)))
+ return -EINVAL;
+
err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
ioremap_max_page_shift);
flush_cache_vmap(addr, end);
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 06/10] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush()
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
` (4 preceding siblings ...)
2026-12-03 9:27 ` [PATCH v6 05/10] mm/vmalloc: reject an unaligned range in vmap_page_range() Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 07/10] mm/vmalloc: undo what a failed vmap_page_range() installed Hao Ge
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge, Sashiko
The PTE loop stops only at addr == end. An unaligned end is never
reached, so the loop keeps clearing past the range. An unaligned
start clears the page below the range first. Both are caller bugs,
and the damage is hard to attribute afterwards.
vmap only installs page aligned mappings, so an unaligned unmap
request is never a legitimate teardown. Warn and bail out at the
entry of __vunmap_range_noflush(), before any PTE is cleared.
The adjacent BUG_ON(addr >= end) is converted to the same
WARN_ON_ONCE() + return, since an empty range is a caller bug too.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-178439@kernel.org/
Reviewed-by: Dev Jain <dev.jain@arm.com>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
mm/vmalloc.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 081c672f63ee..dcf3ddc0c6c0 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -524,7 +524,13 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end,
unsigned long addr = start;
pgtbl_mod_mask mask = 0;
- BUG_ON(addr >= end);
+ if (WARN_ON_ONCE(addr >= end))
+ return;
+ if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
+ return;
+ if (WARN_ON_ONCE(!PAGE_ALIGNED(end)))
+ return;
+
pgd = pgd_offset_k(addr);
do {
next = pgd_addr_end(addr, end);
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 07/10] mm/vmalloc: undo what a failed vmap_page_range() installed
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
` (5 preceding siblings ...)
2026-12-03 9:27 ` [PATCH v6 06/10] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush() Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 08/10] mm/vmalloc: undo what a failed __vmap_pages_range_noflush() installed Hao Ge
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge, Sashiko
vmap_page_range() maps a physical range for ioremap and arch setup
code. The walk may fail halfway, or the KMSAN metadata may fail
after the walk, and the PTEs installed so far stay behind. The
callers do not agree on who cleans it up. The ioremap
implementations free the vm_area, which unmaps the whole range,
and powerpc calls vunmap_range() itself. The arch setup code that
maps a fixed PCI_IOBASE range either ignores the return value or
has no vm_area to free, and leaves the PTEs in place.
vmap_range_noflush() now takes a mapped_end argument that tracks
how far the walk got, and the error path undoes [addr, mapped_end),
never past the failure point. The PTEs above it were not installed
by this call and may be in use by somebody else, clearing them is
not up to the undo. If the walk completed and only the KMSAN
metadata failed, the whole range was installed by this call, and
all of it is undone.
The flush_cache_vmap() also moves after the error check. Flushing
a half-installed mapping is pointless, and on UML it is worse,
flush_cache_vmap() there is flush_tlb_kernel_range(), which syncs
the PTEs into the host OS and would leave host mappings behind
for a range that was just freed again.
The undo paths use the new vunmap_range_nokmsan() helper, which
flushes the cache, clears the PTEs and flushes the TLB, and leaves
the KMSAN metadata to the caller.
All callers are sleepable, the teardown passes can_resched = true.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-178439@kernel.org/
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
mm/vmalloc.c | 72 ++++++++++++++++++++++++++++++++++++++++++++--------
mm/vmalloc.h | 3 +++
2 files changed, 64 insertions(+), 11 deletions(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index dcf3ddc0c6c0..0f2b50ad1a7d 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -188,7 +188,8 @@ static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,
static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
phys_addr_t phys_addr, pgprot_t prot,
- unsigned int max_page_shift, pgtbl_mod_mask *mask)
+ unsigned int max_page_shift, pgtbl_mod_mask *mask,
+ unsigned long *mapped_end)
{
pmd_t *pmd;
unsigned long next;
@@ -203,12 +204,15 @@ static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot,
max_page_shift)) {
*mask |= PGTBL_PMD_MODIFIED;
+ *mapped_end = next;
continue;
}
- err = vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask);
+ err = vmap_pte_range(pmd, addr, next, phys_addr, prot,
+ max_page_shift, mask);
if (err)
break;
+ *mapped_end = next;
} while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
return err;
}
@@ -245,7 +249,8 @@ static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,
static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
phys_addr_t phys_addr, pgprot_t prot,
- unsigned int max_page_shift, pgtbl_mod_mask *mask)
+ unsigned int max_page_shift, pgtbl_mod_mask *mask,
+ unsigned long *mapped_end)
{
pud_t *pud;
unsigned long next;
@@ -260,10 +265,12 @@ static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot,
max_page_shift)) {
*mask |= PGTBL_PUD_MODIFIED;
+ *mapped_end = next;
continue;
}
- err = vmap_pmd_range(pud, addr, next, phys_addr, prot, max_page_shift, mask);
+ err = vmap_pmd_range(pud, addr, next, phys_addr, prot,
+ max_page_shift, mask, mapped_end);
if (err)
break;
} while (pud++, phys_addr += (next - addr), addr = next, addr != end);
@@ -302,7 +309,8 @@ static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,
static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
phys_addr_t phys_addr, pgprot_t prot,
- unsigned int max_page_shift, pgtbl_mod_mask *mask)
+ unsigned int max_page_shift, pgtbl_mod_mask *mask,
+ unsigned long *mapped_end)
{
p4d_t *p4d;
unsigned long next;
@@ -317,10 +325,12 @@ static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot,
max_page_shift)) {
*mask |= PGTBL_P4D_MODIFIED;
+ *mapped_end = next;
continue;
}
- err = vmap_pud_range(p4d, addr, next, phys_addr, prot, max_page_shift, mask);
+ err = vmap_pud_range(p4d, addr, next, phys_addr, prot,
+ max_page_shift, mask, mapped_end);
if (err)
break;
} while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
@@ -329,7 +339,7 @@ static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
static int vmap_range_noflush(unsigned long addr, unsigned long end,
phys_addr_t phys_addr, pgprot_t prot,
- unsigned int max_page_shift)
+ unsigned int max_page_shift, unsigned long *mapped_end)
{
pgd_t *pgd;
unsigned long start;
@@ -346,11 +356,12 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,
BUG_ON(addr >= end);
start = addr;
+ *mapped_end = addr;
pgd = pgd_offset_k(addr);
do {
next = pgd_addr_end(addr, end);
err = vmap_p4d_range(pgd, addr, next, phys_addr, prot,
- max_page_shift, &mask);
+ max_page_shift, &mask, mapped_end);
if (err)
break;
} while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
@@ -364,6 +375,7 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,
int vmap_page_range(unsigned long addr, unsigned long end,
phys_addr_t phys_addr, pgprot_t prot)
{
+ unsigned long mapped_end = addr;
int err;
if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
@@ -372,11 +384,26 @@ int vmap_page_range(unsigned long addr, unsigned long end,
return -EINVAL;
err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
- ioremap_max_page_shift);
- flush_cache_vmap(addr, end);
- if (!err)
+ ioremap_max_page_shift, &mapped_end);
+ if (err) {
+ /* Undo the part the walk installed before it failed. */
+ if (mapped_end > addr)
+ vunmap_range_nokmsan(addr, mapped_end, true);
+ } else {
err = kmsan_ioremap_page_range(addr, end, phys_addr, prot,
ioremap_max_page_shift);
+ if (err) {
+ /*
+ * Only the KMSAN metadata failed, the walk mapped
+ * the whole range. Undo it all. The callers are
+ * ioremap and arch setup code, always sleepable.
+ */
+ vunmap_range_nokmsan(addr, end, true);
+ } else {
+ flush_cache_vmap(addr, end);
+ }
+ }
+
return err;
}
@@ -552,6 +579,29 @@ void vunmap_range_noflush(unsigned long start, unsigned long end)
__vunmap_range_noflush(start, end, true);
}
+/**
+ * vunmap_range_nokmsan - unmap kernel virtual addresses without the
+ * KMSAN part
+ * @addr: start of the VM area to unmap
+ * @end: end of the VM area to unmap (non-inclusive)
+ * @can_resched: whether the unmap walk may call cond_resched(); pass
+ * false from atomic context.
+ *
+ * Clears any present PTEs in the virtual address range, flushes TLBs and
+ * caches, but leaves the KMSAN metadata to the caller. The undo paths
+ * use this for a range that is not going back to vmalloc, which would
+ * flush the TLB lazily, so nobody else flushes it.
+ *
+ * This is an internal function only. Do not use outside mm/.
+ */
+void vunmap_range_nokmsan(unsigned long addr, unsigned long end,
+ bool can_resched)
+{
+ flush_cache_vunmap(addr, end);
+ __vunmap_range_noflush(addr, end, can_resched);
+ flush_tlb_kernel_range(addr, end);
+}
+
/**
* vunmap_range - unmap kernel virtual addresses
* @addr: start of the VM area to unmap
diff --git a/mm/vmalloc.h b/mm/vmalloc.h
index 4ec114a75f04..4406a1c5b1e9 100644
--- a/mm/vmalloc.h
+++ b/mm/vmalloc.h
@@ -47,4 +47,7 @@ void vunmap_range_noflush(unsigned long start, unsigned long end);
void __vunmap_range_noflush(unsigned long start, unsigned long end,
bool can_resched);
+void vunmap_range_nokmsan(unsigned long start, unsigned long end,
+ bool can_resched);
+
#endif /* __MM_VMALLOC_H */
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 08/10] mm/vmalloc: undo what a failed __vmap_pages_range_noflush() installed
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
` (6 preceding siblings ...)
2026-12-03 9:27 ` [PATCH v6 07/10] mm/vmalloc: undo what a failed vmap_page_range() installed Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 09/10] powerpc: drop redundant unmaps of failed vmap mappings Hao Ge
2026-12-03 9:27 ` [PATCH v6 10/10] mm/percpu: stop unmapping the CPU that failed to map Hao Ge
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge, Sashiko
A page array is mapped into a contiguous virtual range by
__vmap_pages_range_noflush(), which serves vmap_pages_range() and
the vmalloc paths, and by vmap_pages_range_batched(), which serves
vmap(). The walk may fail halfway, and the PTEs installed so far
stay behind.
The callers do not agree on who cleans it up. pcpu_map_pages() and
kmsan_ioremap_page_range() roll back what they mapped, while
vm_module_tags_populate() and the __GFP_NOFAIL retry loop in
__vmalloc_area_node() expect a clean range from the mapping function.
For them the leftovers stay mapped, and the next attempt to map the
same range trips the WARN_ON(!pte_none()) in vmap_pages_pte_range().
Both __vmap_pages_range_noflush() and vmap_pages_range_batched()
now undo what they installed, up to the point where the walk
failed. vmap_pages_range_noflush_walk() reports how many pages it
mapped through its index into the pages array, and the batched path
uses the end of the last installed batch.
Going further would clear PTEs that are not ours.
vmap_pages_pte_range() fails with -EBUSY instead of installing over
a PTE that is already set, so a PTE below the failure point can
only be ours, and a PTE above it belongs to somebody else, for
example when the caller's range overlaps an existing mapping.
The flush_cache_vmap() now only runs when the mapping succeeded,
as flushing a half-installed mapping is pointless. The undo flushes
the cache and the TLB itself with vunmap_range_nokmsan(), which
leaves the KMSAN metadata alone.
The KMSAN metadata is set up for the whole range before the mapping
starts, so the undo tears it down as a whole and flushes its TLB
right away, since the undo paths do not go through the lazy purge
and vmap() may retry the range right after. The undo may run with
a non-blocking gfp mask, so it passes gfpflags_allow_blocking() as
can_resched.
The undo calls a new kmsan_vunmap_range(), which tears the
metadata down and flushes its TLB in one go.
Suggested-by: Suren Baghdasaryan <surenb@google.com>
Suggested-by: Uladzislau Rezki <urezki@gmail.com>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-a99dfd25-ba6c-40a4-94db-2046a2d97d70
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
include/linux/kmsan.h | 19 ++++++++++++++
mm/kmsan/hooks.c | 12 +++++++--
mm/kmsan/shadow.c | 4 +--
mm/vmalloc.c | 61 +++++++++++++++++++++++++++++++++++--------
mm/vmalloc.h | 3 ++-
5 files changed, 83 insertions(+), 16 deletions(-)
diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h
index e071c33abded..02b46e44385a 100644
--- a/include/linux/kmsan.h
+++ b/include/linux/kmsan.h
@@ -161,6 +161,19 @@ int __must_check kmsan_vmap_pages_range_noflush(unsigned long start,
void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end,
bool can_resched);
+/**
+ * kmsan_vunmap_range() - Notify KMSAN about a vunmap.
+ * @start: start of vunmapped range.
+ * @end: end of vunmapped range.
+ * @can_resched: whether the unmap walk may call cond_resched(); pass
+ * false from atomic context.
+ *
+ * KMSAN clears any present PTEs of the shadow and the origin of the range,
+ * flushes TLBs and caches.
+ */
+void kmsan_vunmap_range(unsigned long start, unsigned long end,
+ bool can_resched);
+
/**
* kmsan_ioremap_page_range() - Notify KMSAN about a ioremap_page_range() call.
* @addr: range start.
@@ -365,6 +378,12 @@ static inline void kmsan_vunmap_range_noflush(unsigned long start,
{
}
+static inline void kmsan_vunmap_range(unsigned long start,
+ unsigned long end,
+ bool can_resched)
+{
+}
+
static inline int __must_check kmsan_ioremap_page_range(unsigned long start,
unsigned long end,
phys_addr_t phys_addr,
diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c
index f2d6a598ad3c..674a89196d49 100644
--- a/mm/kmsan/hooks.c
+++ b/mm/kmsan/hooks.c
@@ -147,6 +147,14 @@ void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end,
can_resched);
}
+void kmsan_vunmap_range(unsigned long start, unsigned long end,
+ bool can_resched)
+{
+ kmsan_vunmap_range_noflush(start, end, can_resched);
+ flush_tlb_kernel_range(vmalloc_shadow(start), vmalloc_shadow(end));
+ flush_tlb_kernel_range(vmalloc_origin(start), vmalloc_origin(end));
+}
+
#define KMSAN_IOREMAP_META_ORDER 1
static void kmsan_iounmap_pages(unsigned long start, unsigned long end)
@@ -213,7 +221,7 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end,
mapped = __vmap_pages_range_noflush(
vmalloc_shadow(start + off),
vmalloc_shadow(start + off + PAGE_SIZE), prot, &shadow,
- PAGE_SHIFT);
+ PAGE_SHIFT, gfp_mask);
if (mapped) {
err = mapped;
goto ret;
@@ -223,7 +231,7 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end,
mapped = __vmap_pages_range_noflush(
vmalloc_origin(start + off),
vmalloc_origin(start + off + PAGE_SIZE), prot, &origin,
- PAGE_SHIFT);
+ PAGE_SHIFT, gfp_mask);
if (mapped) {
err = mapped;
goto ret;
diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c
index 1af03f83d2ea..fb65178426be 100644
--- a/mm/kmsan/shadow.c
+++ b/mm/kmsan/shadow.c
@@ -332,7 +332,7 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end,
origin_end = vmalloc_meta((void *)end, KMSAN_META_ORIGIN);
kmsan_enter_runtime();
mapped = __vmap_pages_range_noflush(shadow_start, shadow_end, prot,
- s_pages, page_shift);
+ s_pages, page_shift, gfp_mask);
kmsan_leave_runtime();
if (mapped) {
err = mapped;
@@ -340,7 +340,7 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end,
}
kmsan_enter_runtime();
mapped = __vmap_pages_range_noflush(origin_start, origin_end, prot,
- o_pages, page_shift);
+ o_pages, page_shift, gfp_mask);
kmsan_leave_runtime();
if (mapped) {
/* Undo the shadow mapping set up above. */
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 0f2b50ad1a7d..1ba543b039af 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -752,7 +752,8 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
* may have contiguous chunks, as hinted by @shift.
*/
static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,
- pgprot_t prot, struct page **pages, unsigned int shift)
+ pgprot_t prot, struct page **pages, unsigned int shift,
+ unsigned long *mapped_pages)
{
unsigned long start = addr;
pgd_t *pgd;
@@ -775,6 +776,7 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
arch_sync_kernel_mappings(start, end);
+ *mapped_pages = nr;
return err;
}
@@ -785,18 +787,35 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,
* The caller is responsible for calling flush_cache_vmap() after this
* function returns successfully and before the addresses are accessed.
*
+ * On failure the PTEs installed by this call are torn down before
+ * returning, so a retry sees a clean range; PTEs that were already
+ * there when the walk failed are left alone. When @gfp_mask cannot
+ * block, the teardown skips cond_resched(), it may run in atomic
+ * context.
+ *
* This is an internal function only. Do not use outside mm/.
*/
int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
- pgprot_t prot, struct page **pages, unsigned int page_shift)
+ pgprot_t prot, struct page **pages, unsigned int page_shift,
+ gfp_t gfp_mask)
{
+ int err;
+ unsigned long mapped_pages = 0;
+
if (WARN_ON_ONCE(page_shift < PAGE_SHIFT))
return -EINVAL;
if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC))
page_shift = PAGE_SHIFT;
- return vmap_pages_range_noflush_walk(addr, end, prot, pages, page_shift);
+ err = vmap_pages_range_noflush_walk(addr, end, prot, pages, page_shift,
+ &mapped_pages);
+
+ if (err && mapped_pages)
+ vunmap_range_nokmsan(addr, addr + (mapped_pages * PAGE_SIZE),
+ gfpflags_allow_blocking(gfp_mask));
+
+ return err;
}
int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
@@ -808,7 +827,13 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
if (ret)
return ret;
- return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
+
+ ret = __vmap_pages_range_noflush(addr, end, prot, pages, page_shift,
+ gfp_mask);
+ if (ret)
+ kmsan_vunmap_range(addr, end, gfpflags_allow_blocking(gfp_mask));
+
+ return ret;
}
static int __vmap_pages_range(unsigned long addr, unsigned long end,
@@ -818,7 +843,9 @@ static int __vmap_pages_range(unsigned long addr, unsigned long end,
int err;
err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift, gfp_mask);
- flush_cache_vmap(addr, end);
+ if (!err)
+ flush_cache_vmap(addr, end);
+
return err;
}
@@ -3742,13 +3769,15 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
const unsigned int nr_pages = (end - addr) >> PAGE_SHIFT;
unsigned int prev_shift = 0, batch_idx = 0;
unsigned long batch_start = addr, batch_end = addr;
+ unsigned long nr_mapped;
+ unsigned long mapped_end = addr;
int err;
err = kmsan_vmap_pages_range_noflush(addr, end, prot, pages,
PAGE_SHIFT, GFP_KERNEL);
if (err)
- goto out;
+ return err;
for (unsigned int i = 0; i < nr_pages; ) {
unsigned int shift = PAGE_SHIFT +
@@ -3758,8 +3787,10 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
prev_shift = shift;
if (shift != prev_shift) {
- err = vmap_pages_range_noflush_walk(batch_start, batch_end,
- prot, pages + batch_idx, prev_shift);
+ err = vmap_pages_range_noflush_walk(batch_start,
+ batch_end, prot, pages + batch_idx,
+ prev_shift, &nr_mapped);
+ mapped_end = batch_start + (nr_mapped * PAGE_SIZE);
if (err)
goto out;
prev_shift = shift;
@@ -3779,12 +3810,20 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
}
/* Remaining */
- if (batch_start < end)
+ if (batch_start < end) {
err = vmap_pages_range_noflush_walk(batch_start, end, prot,
- pages + batch_idx, prev_shift);
+ pages + batch_idx, prev_shift, &nr_mapped);
+ mapped_end = batch_start + (nr_mapped * PAGE_SIZE);
+ }
out:
- flush_cache_vmap(addr, end);
+ if (err) {
+ if (mapped_end > addr)
+ vunmap_range_nokmsan(addr, mapped_end, true);
+ kmsan_vunmap_range(addr, end, true);
+ } else {
+ flush_cache_vmap(addr, end);
+ }
return err;
}
diff --git a/mm/vmalloc.h b/mm/vmalloc.h
index 4406a1c5b1e9..fe6314ce55e6 100644
--- a/mm/vmalloc.h
+++ b/mm/vmalloc.h
@@ -40,7 +40,8 @@ void clear_vm_uninitialized_flag(struct vm_struct *vm);
int __must_check __vmap_pages_range_noflush(unsigned long addr,
unsigned long end, pgprot_t prot,
- struct page **pages, unsigned int page_shift);
+ struct page **pages, unsigned int page_shift,
+ gfp_t gfp_mask);
void vunmap_range_noflush(unsigned long start, unsigned long end);
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 09/10] powerpc: drop redundant unmaps of failed vmap mappings
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
` (7 preceding siblings ...)
2026-12-03 9:27 ` [PATCH v6 08/10] mm/vmalloc: undo what a failed __vmap_pages_range_noflush() installed Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
2026-12-03 9:27 ` [PATCH v6 10/10] mm/percpu: stop unmapping the CPU that failed to map Hao Ge
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge
vmap_page_range() and ioremap_page_range() clear the PTEs they
installed when they fail, so the vunmap_range() in remap_isa_base()
and ioremap_phb() has nothing left to remove.
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
arch/powerpc/kernel/isa-bridge.c | 5 ++---
arch/powerpc/kernel/pci_64.c | 4 +---
2 files changed, 3 insertions(+), 6 deletions(-)
diff --git a/arch/powerpc/kernel/isa-bridge.c b/arch/powerpc/kernel/isa-bridge.c
index 5c064485197a..93029d85e6ea 100644
--- a/arch/powerpc/kernel/isa-bridge.c
+++ b/arch/powerpc/kernel/isa-bridge.c
@@ -46,9 +46,8 @@ static void remap_isa_base(phys_addr_t pa, unsigned long size)
WARN_ON_ONCE(size & ~PAGE_MASK);
if (slab_is_available()) {
- if (vmap_page_range(ISA_IO_BASE, ISA_IO_BASE + size, pa,
- pgprot_noncached(PAGE_KERNEL)))
- vunmap_range(ISA_IO_BASE, ISA_IO_BASE + size);
+ vmap_page_range(ISA_IO_BASE, ISA_IO_BASE + size, pa,
+ pgprot_noncached(PAGE_KERNEL));
} else {
early_ioremap_range(ISA_IO_BASE, pa, size,
pgprot_noncached(PAGE_KERNEL));
diff --git a/arch/powerpc/kernel/pci_64.c b/arch/powerpc/kernel/pci_64.c
index e27342ef128b..f74269636d5e 100644
--- a/arch/powerpc/kernel/pci_64.c
+++ b/arch/powerpc/kernel/pci_64.c
@@ -139,10 +139,8 @@ void __iomem *ioremap_phb(phys_addr_t paddr, unsigned long size)
addr = (unsigned long)area->addr;
if (ioremap_page_range(addr, addr + size, paddr,
- pgprot_noncached(PAGE_KERNEL))) {
- vunmap_range(addr, addr + size);
+ pgprot_noncached(PAGE_KERNEL)))
return NULL;
- }
return (void __iomem *)addr;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 10/10] mm/percpu: stop unmapping the CPU that failed to map
2026-12-03 9:27 [PATCH v6 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
` (8 preceding siblings ...)
2026-12-03 9:27 ` [PATCH v6 09/10] powerpc: drop redundant unmaps of failed vmap mappings Hao Ge
@ 2026-12-03 9:27 ` Hao Ge
9 siblings, 0 replies; 11+ messages in thread
From: Hao Ge @ 2026-12-03 9:27 UTC (permalink / raw)
To: Suren Baghdasaryan, Uladzislau Rezki, Dev Jain, Andrew Morton,
David Hildenbrand (Arm), Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Alexander Potapenko,
Marco Elver, Dmitry Vyukov, Dennis Zhou, Tejun Heo,
Christoph Lameter
Cc: linuxppc-dev, kasan-dev, linux-mm, Hao Ge
__pcpu_map_pages() clears the PTEs it installed when it fails, so
pcpu_map_pages() only has to tear down the CPUs it mapped before
the failing one.
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
Signed-off-by: Hao Ge <hao.ge@linux.dev>
---
mm/percpu-vm.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/mm/percpu-vm.c b/mm/percpu-vm.c
index 509d8901835c..59d3a336a6bd 100644
--- a/mm/percpu-vm.c
+++ b/mm/percpu-vm.c
@@ -252,10 +252,11 @@ static int pcpu_map_pages(struct pcpu_chunk *chunk, struct page **pages,
return 0;
err:
for_each_possible_cpu(tcpu) {
- __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start),
- page_end - page_start);
+ /* The failing CPU's partial mapping undoes itself */
if (tcpu == cpu)
break;
+ __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start),
+ page_end - page_start);
}
pcpu_post_unmap_tlb_flush(chunk, page_start, page_end);
return err;
--
2.25.1
^ permalink raw reply related [flat|nested] 11+ messages in thread