Re: [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails

From: Suren Baghdasaryan

Date: Thu Dec 03 2026 - 15:31:44 EST


On Thu, Dec 3, 2026 at 5:00 AM Hao Ge <hao.ge@xxxxxxxxx> wrote:
>
> Hi Suren
>
> On 2026/11/25 16:20, Hao Ge wrote:
> > Hi Suren
> >
> >
> > On 2026/11/23 18:15, Hao Ge wrote:
> >>
> >>
> >> On 2026/11/23 16:12, Hao Ge wrote:
> >>> Hi Suren
> >>>
> >>> On 2026/11/21 05:49, Suren Baghdasaryan wrote:
> >>>> On Fri, Nov 20, 2026 at 9:14 PM Suren Baghdasaryan <surenb@xxxxxxxxxx> wrote:
> >>>>>
> >>>>> On Mon, Nov 16, 2026 at 7:13 AM Hao Ge <hao.ge@xxxxxxxxx> wrote:
> >>>>>>
> >>>>>> 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, the same way
> >>>>>> kmsan_ioremap_page_range() cleans up after a partial failure.
> >>>>>> __vunmap_range_noflush() only clears the PTEs; nothing has touched
> >>>>>> the shadow mapping, so no TLB flush is needed.
> >>>>
> >>>> After a second thought, kmsan_ioremap_page_range() does not skip TLB
> >>>> flushing. __vunmap_range_noflush() comment at
> >>>> https://elixir.bootlin.com/linux/v7.4-rc1/source/mm/vmalloc.c#L497
> >>>> says:
> >>>>
> >>>> The caller is responsible for calling flush_cache_vmap() before
> >>>> calling this function, and flush_tlb_kernel_range after it has
> >>>> returned successfully ...". There are no mentions of guarantees that
> >>>> mapped PTEs require explicit access to be cached. Please double-check
> >>>> if this is safe.
> >>>
> >>> That's a subtle detail. I've looked up the relevant documentation.
> >>> You are right, and my "no TLB flush is needed" was wrong.
> >>>
> >>> The Arm "Learn the architecture" AArch64 memory management guide says it
> >>> in "TLBs and Changing translations":
> >>>
> >>> https://support.arm.com/documentation/101811/0106/TLBs-and-Changing-translations
> >>>
> >>> If you change a translation table entry, or the controls that affect how entries
> >>> are interpreted, then you need to invalidate the affected entries in the TLB. If
> >>> you do not invalidate those entries, then the processor might continue to use the
> >>> old translation.
> >>>
> >>> The undo changes the shadow PTEs, so the TLB has to be invalidated.
> >>>
> >>> I also checked the Arm Architecture Reference Manual:
> >>> https://support.arm.com/documentation/ddi0487/md/-Part-D-The-AArch64-System-Level-Architecture/-Chapter-D8-The-AArch64-Virtual-Memory-System-Architecture/-D8-16-Translation-Lookaside-Buffers
> >>> rule R SQBCS:
> >>>
> >>> If a stage of translation is enabled for a translation regime that is not out-of-context,
> >>> any translation table entry for that stage that does not cause a Translation fault,
> >>> an Address size fault, or an Access flag fault is permitted to be cached in a TLB or
> >>> intermediate TLB caching structure as the result of an explicit or speculative access.
> >>>
> >>> So it may already be cached by speculative access even if there was no explicit access.
> >>>
> >>> For the kernel side, Documentation/core-api/cachetlb.rst
> >>> https://elixir.bootlin.com/linux/v7.4-rc1/source/Documentation/core-api/cachetlb.rst
> >>> says that The "TLB" is abstracted under Linux as something the cpu uses to cache
> >>> virtual-->physical address translations obtained from the software page tables.
> >>> Meaning that if the software page tables change, it is possible for stale translations
> >>> to exist in this "TLB" cache. Therefore when software page table changes occur, the kernel will
> >>> invoke one of the following flush methods _after_ the page table changes occur:
> >>>
> >>> The condition there is the page table change, not whether the range was accessed.
> >>>
> >>> Likewise, I believe flush_cache_vmap() is also required.
> >>> The sequence in cachetlb.rst is
> >>> flush_cache_range()
> >>> change_range_of_page_tables()
> >>> flush_tlb_range()
> >>> so the cache flush has to happen before the unmap.
> >>> This is exactly the contract that the __vunmap_range_noflush() comment you quoted spells out,
> >>> the caller is responsible for calling flush_cache_vmap() before the call and flush_tlb_kernel_range()
> >>> after it.
> >>>
> >>> The code will do:
> >>>
> >>> --- a/mm/kmsan/shadow.c
> >>> +++ b/mm/kmsan/shadow.c
> >>> @@ -258,6 +258,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_vmap(shadow_start, shadow_end);
> >>
> >> Sorry, this should be flush_cache_vunmap.
> >>
> >>> + 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;
> >>> }
> >>>
> >>> Thanks a lot for catching this, I learned quite a bit about the TLB
> >>> along the way.
> >>>
> >
> > Let's revisit this. Unless I'm missing something, the unmap counterpart,
> > kmsan_vunmap_range_noflush(), appears to be missing its TLB flush.
> >
> > Taking vmalloc as the example:
> >
> > vmalloc
> > __vmalloc_area_node()
> > vmap_pages_range()
> > vmap_pages_range_noflush()
> > kmsan_vmap_pages_range_noflush()
> > installs the shadow/origin and flushes
> > their TLB at the tail /* flushed */
> >
> > vfree
> > remove_vm_area
> > free_unmap_vmap_area
> > vunmap_range_noflush /*shadow/origin PTEs cleared,
> > no TLB flush */
> > free_vmap_area_noflush() /* area -> lazy list
> >
> > ... later, when the lazy list is settled:
> > __purge_vmap_area_lazy
> > flush_tlb_kernel_range(start, end);
> > /* no shadow/origin tlb flush */
> >
> > So unlike the normal paths, KMSAN performs the TLB flush when the range
> > is reused and re-mapped, rather than flushing it immediately after unmap.
> >
> > I'm not sure this is safe. Reading the documentation, this should not be safe,
> > perhaps I have misinterpreted the documentation.
> > If this was intentional, maybe a TLB flush is not required here, but if so I would
> > expect it in the purge instead, together with the main range. After all, vmalloc is
> > frequently called, and lazy TLB flushing is an existing optimization for vmalloc.
>
> Update this point. While the logic is shown here, this change is not present in v6.
> I originally expected this to be relatively straightforward, so I've spent the past
> few days working on it. The code change is not large.
>
> However, I've noticed one subtle detail about the x86 TLB.
>
> static void init_flush_tlb_info(struct flush_tlb_info *info,
> struct mm_struct *mm,
> unsigned long start, unsigned long end,
> unsigned int stride_shift, bool freed_tables,
> u64 new_tlb_gen)
> {
> /*
> * If the number of flushes is so large that a full flush
> * would be faster, do a full flush.
> */
> if ((end - start) >> stride_shift > tlb_single_page_flush_ceiling) {
> start = 0;
> end = TLB_FLUSH_ALL;
> }
>
> info->start = start;
> info->end = end;
> info->mm = mm;
> info->stride_shift = stride_shift;
> info->freed_tables = freed_tables;
> info->new_tlb_gen = new_tlb_gen;
> info->initiating_cpu = smp_processor_id();
> info->trim_cpumask = 0;
> }
>
> On x86, when we call flush_tlb_kernel_range, a full TLB flush will be performed
> if the range exceeds tlb_single_page_flush_ceiling (which defaults to 33).
>
> void kmsan_flush_tlb_range(unsigned long start, unsigned long end)
> {
>
> flush_tlb_kernel_range(vmalloc_shadow(start), vmalloc_shadow(end));
> flush_tlb_kernel_range(vmalloc_origin(start), vmalloc_origin(end));
> }
>
> @@ -2447,6 +2448,7 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end,
>
> if (nr_purge_nodes > 0) {
> flush_tlb_kernel_range(start, end);
> kmsan_flush_tlb_range(start, end);
>
> If the lazy region being purged actually exceeds 33 pages, we will end up issuing three consecutive
> full TLB flushes, two of which are unnecessary. I still need to think about how to optimize this,
> so I will defer this part for now.

Hi Hao,
Sorry for not replying sooner. I myself is not an expert in this, so
had to do my own research a bit.
I think KMSAN gets away with this by flushing TLBs during the next
allocation inside kmsan_vmap_pages_range_noflush() (even though it's
called _noflush it actually executed TLB flushes at [1]). I think this
works because KMSAN shadow/origin backing pages are never returned to
the buddy allocator; they are effectively carved out. So, I think a
stale TLB entry sitting around after vfree() won't cause aliasing
issues and in a normal vmalloc -> vfree -> vmalloc cycle all CPUs see
the new shadow/origin PTEs before the new VAs are accessed. Again,
this is based on my limited research and understanding. I could not
find any discussion about this on the mailing list to confirm.
Thanks,
Suren.

[1] https://elixir.bootlin.com/linux/v7.4-rc2/source/mm/kmsan/shadow.c#L264


>
> That said, it has been a really good learning process for me.
>
> Thanks
> Best Regards
> Hao
>
> >
> > Would you mind taking a look at this concern, or letting me know who I could ask?
> > This would be a good opportunity for me to brush up on my TLB knowledge, and I would
> > greatly appreciate your input.
> >
> > Thanks
> > Best Regards
> > Hao
> >
> >>> Thanks
> >>> Best Regards
> >>> Hao
> >>>>
> >>>>>>
> >>>>>> Fixes: 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")
> >>>>>> Cc: stable@xxxxxxxxxxxxxxx
> >>>>>> Suggested-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>
> >>>>>> Signed-off-by: Hao Ge <hao.ge@xxxxxxxxx>
> >>>>>
> >>>>> Reviewed-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>
> >>>>>
> >>>>>> ---
> >>>>>> mm/kmsan/shadow.c | 5 +++++
> >>>>>> 1 file changed, 5 insertions(+)
> >>>>>>
> >>>>>> diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c
> >>>>>> index 0c88d89bf0d6..212449db34a1 100644
> >>>>>> --- a/mm/kmsan/shadow.c
> >>>>>> +++ b/mm/kmsan/shadow.c
> >>>>>> @@ -258,6 +258,11 @@ 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. */
> >>>>>> + kmsan_enter_runtime();
> >>>>>> + __vunmap_range_noflush(shadow_start, shadow_end,
> >>>>>> + gfpflags_allow_blocking(gfp_mask));
> >>>>>> + kmsan_leave_runtime();
> >>>>>> err = mapped;
> >>>>>> goto ret;
> >>>>>> }
> >>>>>> --
> >>>>>> 2.25.1
> >>>>>>