Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v6 02/10] mm/kmsan: undo the shadow mapping when the origin mapping fails
       [not found] <20261203092728.157806-1-hao.ge@linux.dev>
@ 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
  2026-12-03  9:27 ` [PATCH v6 04/10] mm/kmsan: fix the metadata cache flush ordering Hao Ge
  2 siblings, 0 replies; 3+ 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] 3+ messages in thread

* [PATCH v6 03/10] mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment
       [not found] <20261203092728.157806-1-hao.ge@linux.dev>
  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
  2 siblings, 0 replies; 3+ 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] 3+ messages in thread

* [PATCH v6 04/10] mm/kmsan: fix the metadata cache flush ordering
       [not found] <20261203092728.157806-1-hao.ge@linux.dev>
  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 ` Hao Ge
  2 siblings, 0 replies; 3+ 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] 3+ messages in thread

end of thread, other threads:[~2026-12-03  9:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261203092728.157806-1-hao.ge@linux.dev>
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox