Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Hao Ge <hao.ge@linux.dev>
To: Suren Baghdasaryan <surenb@google.com>
Cc: Uladzislau Rezki <urezki@gmail.com>, Dev Jain <dev.jain@arm.com>,
	Andrew Morton <akpm@linux-foundation.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>,
	linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	kasan-dev@googlegroups.com, linux-mm@kvack.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails
Date: Mon, 23 Nov 2026 18:15:29 +0800	[thread overview]
Message-ID: <81b05a10-1b20-4132-8cc6-c71b88f21414@linux.dev> (raw)
In-Reply-To: <793d63ff-c48b-431d-8a2a-4044feb45721@linux.dev>



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@google.com> wrote:
>>>
>>> On Mon, Nov 16, 2026 at 7:13 AM Hao Ge <hao.ge@linux.dev> 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. 
> 
> Thanks
> Best Regards
> Hao
>>
>>>>
>>>> Fixes: 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")
>>>> Cc: stable@vger.kernel.org
>>>> Suggested-by: Suren Baghdasaryan <surenb@google.com>
>>>> Signed-off-by: Hao Ge <hao.ge@linux.dev>
>>>
>>> Reviewed-by: Suren Baghdasaryan <surenb@google.com>
>>>
>>>> ---
>>>>  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
>>>>


  reply	other threads:[~2026-11-23 10:15 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  7:13 [PATCH v5 00/11] mm/vmalloc: fix up vmap error handling Hao Ge
2026-11-16  7:13 ` [PATCH v5 01/11] mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic Hao Ge
2026-11-18 19:00   ` Uladzislau Rezki
2026-11-19  5:02   ` Dev Jain
2026-11-20 21:07     ` Suren Baghdasaryan
2026-11-23  6:17       ` Hao Ge
2026-11-16  7:13 ` [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails Hao Ge
2026-11-20 21:14   ` Suren Baghdasaryan
2026-11-20 21:49     ` Suren Baghdasaryan
2026-11-23  8:12       ` Hao Ge
2026-11-23 10:15         ` Hao Ge [this message]
2026-11-16  7:13 ` [PATCH v5 03/11] mm/vmalloc: reject an unaligned range in vmap_page_range() Hao Ge
2026-11-17  5:53   ` Dev Jain
2026-11-18 19:10   ` Uladzislau Rezki
2026-11-20 22:06     ` Suren Baghdasaryan
2026-11-16  7:13 ` [PATCH v5 04/11] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush() Hao Ge
2026-11-17  6:05   ` Dev Jain
2026-11-18 19:16     ` Uladzislau Rezki
2026-11-20 22:13     ` Suren Baghdasaryan
2026-11-23  8:24       ` Hao Ge
2026-11-16  7:13 ` [PATCH v5 05/11] mm/vmalloc: only flush the cache after the vmap succeeded Hao Ge
2026-11-18 19:31   ` Uladzislau Rezki
2026-11-20 22:24     ` Suren Baghdasaryan
2026-11-16  7:13 ` [PATCH v5 06/11] mm/vmalloc: undo what vmap_page_range() mapped on failure Hao Ge
2026-11-18 19:50   ` Uladzislau Rezki
2026-11-23  9:39     ` Hao Ge
2026-11-20 22:29   ` Suren Baghdasaryan
2026-11-16  7:13 ` [PATCH v5 07/11] mm/vmalloc: undo what __vmap_pages_range_noflush() " Hao Ge
2026-11-17  7:15   ` sashiko-bot
2026-11-17  7:27     ` Hao Ge
2026-11-22  3:58   ` Suren Baghdasaryan
2026-11-23 10:50     ` Hao Ge
2026-11-16  7:13 ` [PATCH v5 08/11] mm/vmalloc: drop the KMSAN metadata in vmap_pages_range_noflush() " Hao Ge
2026-11-22  4:04   ` Suren Baghdasaryan
2026-11-16  7:13 ` [PATCH v5 09/11] mm/vmalloc: undo what vmap_pages_range_batched() mapped " Hao Ge
2026-11-22  4:13   ` Suren Baghdasaryan
2026-11-16  7:13 ` [PATCH v5 10/11] powerpc: drop redundant unmaps of failed vmap mappings Hao Ge
2026-11-16  7:13 ` [PATCH v5 11/11] 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=81b05a10-1b20-4132-8cc6-c71b88f21414@linux.dev \
    --to=hao.ge@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=chleroy@kernel.org \
    --cc=cl@gentwo.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-kernel@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox