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


Reply via email to