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: [email protected]
Signed-off-by: Hao Ge <[email protected]>
---
 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


Reply via email to