All of lore.kernel.org
 help / color / mirror / Atom feed
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>,
	stable@vger.kernel.org
Subject: [PATCH v6 04/10] mm/kmsan: fix the metadata cache flush ordering
Date: Thu,  3 Dec 2026 17:27:22 +0800	[thread overview]
Message-ID: <20261203092728.157806-5-hao.ge@linux.dev> (raw)
In-Reply-To: <20261203092728.157806-1-hao.ge@linux.dev>

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



  parent reply	other threads:[~2026-12-03  9:28 UTC|newest]

Thread overview: 15+ 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 19:44   ` sashiko-bot
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 19:44   ` sashiko-bot
2026-12-03  9:27 ` Hao Ge [this message]
2026-12-03  9:27 ` [PATCH v6 05/10] mm/vmalloc: reject an unaligned range in vmap_page_range() Hao Ge
2026-12-03 19:44   ` sashiko-bot
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 19:44   ` sashiko-bot
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 ` [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-5-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=sshegde@linux.ibm.com \
    --cc=stable@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.