vmap_page_range(), __vmap_pages_range() and vmap_pages_range_batched()
call flush_cache_vmap() before they even look at the error code. A
failed mapping is never used, so the flush is pointless at best.

According to the Sashiko review it is worse than pointless on UML:
flush_cache_vmap() there is flush_tlb_kernel_range(), which syncs the
PTEs into the host OS. Flushing a half-installed mapping therefore
leaves host mappings behind for a range that was just freed again.

Only flush when the mapping, and for vmap_page_range() also the KMSAN
metadata, is fully set up.

Reported-by: Sashiko <[email protected]>
Closes: https://lore.kernel.org/all/[email protected]/
Signed-off-by: Hao Ge <[email protected]>
---
 mm/vmalloc.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index e2b63cb1592b..d99d7710e1ba 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -371,10 +371,12 @@ int vmap_page_range(unsigned long addr, unsigned long end,
 
        err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
                                 ioremap_max_page_shift);
-       flush_cache_vmap(addr, end);
        if (!err)
                err = kmsan_ioremap_page_range(addr, end, phys_addr, prot,
                                               ioremap_max_page_shift);
+       if (!err)
+               flush_cache_vmap(addr, end);
+
        return err;
 }
 
@@ -763,7 +765,9 @@ static int __vmap_pages_range(unsigned long addr, unsigned 
long end,
        int err;
 
        err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift, 
gfp_mask);
-       flush_cache_vmap(addr, end);
+       if (!err)
+               flush_cache_vmap(addr, end);
+
        return err;
 }
 
@@ -3729,7 +3733,8 @@ static int vmap_pages_range_batched(unsigned long addr, 
unsigned long end,
                                pages + batch_idx, prev_shift);
 
 out:
-       flush_cache_vmap(addr, end);
+       if (!err)
+               flush_cache_vmap(addr, end);
        return err;
 }
 
-- 
2.25.1


Reply via email to