From: Hao Ge <hao.ge@linux.dev>
To: Suren Baghdasaryan <surenb@google.com>,
Uladzislau Rezki <urezki@gmail.com>, Dev Jain <dev.jain@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
"David Hildenbrand (Arm)" <david@kernel.org>,
Madhavan Srinivasan <maddy@linux.ibm.com>,
Michael Ellerman <mpe@ellerman.id.au>,
Nicholas Piggin <npiggin@gmail.com>,
"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
Shrikanth Hegde <sshegde@linux.ibm.com>,
Alexander Potapenko <glider@google.com>,
Marco Elver <elver@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>,
Christoph Lameter <cl@gentwo.org>
Cc: linuxppc-dev@lists.ozlabs.org, kasan-dev@googlegroups.com,
linux-mm@kvack.org, Hao Ge <hao.ge@linux.dev>,
Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH v6 08/10] mm/vmalloc: undo what a failed __vmap_pages_range_noflush() installed
Date: Thu, 3 Dec 2026 17:27:26 +0800 [thread overview]
Message-ID: <20261203092728.157806-9-hao.ge@linux.dev> (raw)
In-Reply-To: <20261203092728.157806-1-hao.ge@linux.dev>
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
next prev parent reply other threads:[~2026-12-03 9:28 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` [PATCH v6 03/10] mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment Hao Ge
2026-12-03 9:27 ` [PATCH v6 04/10] mm/kmsan: fix the metadata cache flush ordering Hao Ge
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 ` [PATCH v6 06/10] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush() Hao Ge
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203092728.157806-9-hao.ge@linux.dev \
--to=hao.ge@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=chleroy@kernel.org \
--cc=cl@gentwo.org \
--cc=david@kernel.org \
--cc=dennis@kernel.org \
--cc=dev.jain@arm.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-mm@kvack.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=mpe@ellerman.id.au \
--cc=npiggin@gmail.com \
--cc=ritesh.list@gmail.com \
--cc=sashiko-bot@kernel.org \
--cc=sshegde@linux.ibm.com \
--cc=surenb@google.com \
--cc=tj@kernel.org \
--cc=urezki@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox