Re: [PATCH v3 3/3] arm64: mte: Simplify swap tag restoration logic

From: Peter Collingbourne
Date: Mon May 22 2023 - 20:25:15 EST


Hi Catalin,

On Fri, May 19, 2023 at 9:54 AM Catalin Marinas <catalin.marinas@xxxxxxx> wrote:
>
> On Tue, May 16, 2023 at 07:21:13PM -0700, Peter Collingbourne wrote:
> > As a result of the previous two patches, there are no circumstances
> > in which a swapped-in page is installed in a page table without first
> > having arch_swap_restore() called on it. Therefore, we no longer need
> > the logic in set_pte_at() that restores the tags, so remove it.
> >
> > Because we can now rely on the page being locked, we no longer need to
> > handle the case where a page is having its tags restored by multiple tasks
> > concurrently, so we can slightly simplify the logic in mte_restore_tags().
> [...]
> > diff --git a/arch/arm64/mm/mteswap.c b/arch/arm64/mm/mteswap.c
> > index cd508ba80ab1..3a78bf1b1364 100644
> > --- a/arch/arm64/mm/mteswap.c
> > +++ b/arch/arm64/mm/mteswap.c
> > @@ -53,10 +53,9 @@ void mte_restore_tags(swp_entry_t entry, struct page *page)
> > if (!tags)
> > return;
> >
> > - if (try_page_mte_tagging(page)) {
> > - mte_restore_page_tags(page_address(page), tags);
> > - set_page_mte_tagged(page);
> > - }
> > + WARN_ON_ONCE(!try_page_mte_tagging(page));
> > + mte_restore_page_tags(page_address(page), tags);
> > + set_page_mte_tagged(page);
> > }
>
> Can we have a situation where two processes share the same swap pte
> (CoW) and they both enter the do_swap_page() or the unuse_pte() paths
> triggering this warning?

Having examined the code more closely, I realized that this is
possible with two do_swap_page() calls on CoW shared pages (or
do_swap_page() followed by unuse_pte()), because the swapcache page
will be shared between the tasks and so they will both call
arch_swap_restore() on the same page. I was able to provoke the
warning with the following program:

#include <sys/mman.h>
#include <unistd.h>

int main() {
char *p = mmap(0, 4096, PROT_READ|PROT_WRITE|PROT_MTE,
MAP_ANON|MAP_PRIVATE, -1, 0);
p[0] = 1;
madvise(p, 4096, MADV_PAGEOUT);
fork();
return p[0];
}

I will send a v4 with this hunk removed.

> Other than that, the looks nice, it simplifies the logic and probably
> saves a few cycles as well on the set_pte_at() path.
>
> Reviewed-by: Catalin Marinas <catalin.marinas@xxxxxxx>

Thanks for the review!

Peter