[PATCH v6 02/10] mm/kmsan: undo the shadow mapping when the origin mapping fails

From
Hao Ge <hao.ge@linux.dev>
Date
2026-12-03 09:28:00
Also in
linux-mm, stable
Reply to
Hao Ge, 2026-12-03
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: <redacted>
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

Thread (11 messages) 11 messages, 1 author, last reply 1d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help