Re: [PATCH v11 2/6] arm64: kvm: Introduce MTE VM feature

From: Catalin Marinas
Date: Thu May 13 2021 - 11:08:54 EST


On Thu, May 13, 2021 at 11:57:39AM +0100, Steven Price wrote:
> On 12/05/2021 18:45, Catalin Marinas wrote:
> > On Wed, May 12, 2021 at 04:46:48PM +0100, Steven Price wrote:
> >>>>>> On Thu, Apr 29, 2021 at 05:06:41PM +0100, Steven Price wrote:
> >>>>>>> Given the changes to set_pte_at() which means that tags are restored from
> >>>>>>> swap even if !PROT_MTE, the only race I can see remaining is the creation of
> >>>>>>> new PROT_MTE mappings. As you mention an attempt to change mappings in the
> >>>>>>> VMM memory space should involve a mmu notifier call which I think serialises
> >>>>>>> this. So the remaining issue is doing this in a separate address space.
> >>>>>>>
> >>>>>>> So I guess the potential problem is:
> >>>>>>>
> >>>>>>> * allocate memory MAP_SHARED but !PROT_MTE
> >>>>>>> * fork()
> >>>>>>> * VM causes a fault in parent address space
> >>>>>>> * child does a mprotect(PROT_MTE)
> >>>>>>>
> >>>>>>> With the last two potentially racing. Sadly I can't see a good way of
> >>>>>>> handling that.
[...]
> >> 4. Sledgehammer locking in mte_sync_page_tags(), add a spinlock only for
> >> the MTE case where we have to sync tags (see below). What the actual
> >> performance impact of this is I've no idea. It could certainly be bad
> >> if there are a lot of pages with MTE enabled, which sadly is exactly
> >> the case if KVM is used with MTE :(
> >>
> >> --->8----
> >> diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
> >> index 0d320c060ebe..389ad40256f6 100644
> >> --- a/arch/arm64/kernel/mte.c
> >> +++ b/arch/arm64/kernel/mte.c
> >> @@ -25,23 +25,33 @@
> >> u64 gcr_kernel_excl __ro_after_init;
> >> static bool report_fault_once = true;
> >> +static spinlock_t tag_sync_lock;
> >> static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap,
> >> bool pte_is_tagged)
> >> {
> >> pte_t old_pte = READ_ONCE(*ptep);
> >> + if (!is_swap_pte(old_pte) && !pte_is_tagged)
> >> + return;
> >> +
> >> + spin_lock_irqsave(&tag_sync_lock, flags);
> >> +
> >> + /* Recheck with the lock held */
> >> + if (test_bit(PG_mte_tagged, &page->flags))
> >> + goto out;
> >
> > Can we skip the lock if the page already has the PG_mte_tagged set?
> > That's assuming that we set the flag only after clearing the tags. The
> > normal case where mprotect() is called on a page already mapped with
> > PROT_MTE would not be affected.
>
> It was missing from the diff context (sorry, should have double checked
> that), but I was keeping the check in mte_sync_tags():
>
> void mte_sync_tags(pte_t *ptep, pte_t pte)
> {
> struct page *page = pte_page(pte);
> long i, nr_pages = compound_nr(page);
> bool check_swap = nr_pages == 1;
> bool pte_is_tagged = pte_tagged(pte);
> unsigned long flags;
>
> /* Early out if there's nothing to do */
> if (!check_swap && !pte_is_tagged)
> return;
>
> /* if PG_mte_tagged is set, tags have already been initialised */
> for (i = 0; i < nr_pages; i++, page++) {
> if (!test_bit(PG_mte_tagged, &page->flags))
> mte_sync_page_tags(page, ptep, check_swap,
> pte_is_tagged);
> }
> }
>
> So the hit is only taken if !PG_mte_tagged - hence the "recheck" comment
> in mte_sync_page_tags() once the lock is held. I guess if I'm going this
> route it would make sense to refactor this to be a bit clearer.

I think we can go with this for now but we should still raise it with
the mm folk, maybe they have a better idea on how to avoid the race in
the core code. There are other architectures affected, those that use
PG_arch_1.

As the kernel stands currently, we'd take the lock on any set_pte_at()
for a tagged page when first mapped. With Peter's patches to use DC
GZVA, the tag zeroing is done on allocation. Until those are merged, we
could do something similar in the arch code but without the DC GZVA
optimisation (useful if we need to cc this fix to stable):

----------8<--------------------------