linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Peter Collingbourne <pcc@google.com>
To: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	 Linux Memory Management List <linux-mm@kvack.org>,
	LKML <linux-kernel@vger.kernel.org>,
	 "# 3.4.x" <stable@vger.kernel.org>
Subject: Re: [PATCH] mm: use compare-exchange operation to set KASAN page tag
Date: Tue, 18 Jan 2022 14:38:22 -0800	[thread overview]
Message-ID: <CAMn1gO7zBjaAU9f=qnKKhVBxuujmqzHDpuEdHQxtePDZVBgWMg@mail.gmail.com> (raw)
In-Reply-To: <CA+fCnZeckbw+Vv9TQaCNfaMBA9DkMGXeiQwcoHnwxoO6fCrzcA@mail.gmail.com>

On Fri, Jan 14, 2022 at 1:58 PM Andrey Konovalov <andreyknvl@gmail.com> wrote:
>
>  On Thu, Jan 13, 2022 at 6:14 AM Peter Collingbourne <pcc@google.com> wrote:
> >
> > It has been reported that the tag setting operation on newly-allocated
> > pages can cause the page flags to be corrupted when performed
> > concurrently with other flag updates as a result of the use of
> > non-atomic operations.
>
> Is it know how exactly this race happens? Why are flags for a newly
> allocated page being accessed concurrently?

In the report that we received, the race resulted in a crash in
kswapd. This may just be a symptom of the problem though.

I haven't closely audited all of the callers to page_kasan_tag_set()
to check whether they may be operating on already-visible pages, but
at least it doesn't appear to be unanticipated that there may be other
threads accessing the page flags concurrently with a call to
page_kasan_tag_set() (see the calls to smp_wmb() in
arch/arm64/kernel/mte.c, arch/arm64/mm/copypage.c and
arch/arm64/mm/mteswap.c).

> > Fix the problem by using a compare-exchange
> > loop to update the tag.
> >
> > Signed-off-by: Peter Collingbourne <pcc@google.com>
> > Link: https://linux-review.googlesource.com/id/I456b24a2b9067d93968d43b4bb3351c0cec63101
> > Fixes: 2813b9c02962 ("kasan, mm, arm64: tag non slab memory allocated via pagealloc")
> > Cc: stable@vger.kernel.org
> > ---
> >  include/linux/mm.h | 16 +++++++++++-----
> >  1 file changed, 11 insertions(+), 5 deletions(-)
> >
> > diff --git a/include/linux/mm.h b/include/linux/mm.h
> > index c768a7c81b0b..b544b0a9f537 100644
> > --- a/include/linux/mm.h
> > +++ b/include/linux/mm.h
> > @@ -1531,11 +1531,17 @@ static inline u8 page_kasan_tag(const struct page *page)
> >
> >  static inline void page_kasan_tag_set(struct page *page, u8 tag)
> >  {
> > -       if (kasan_enabled()) {
> > -               tag ^= 0xff;
> > -               page->flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
> > -               page->flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
> > -       }
> > +       unsigned long old_flags, flags;
> > +
> > +       if (!kasan_enabled())
> > +               return;
> > +
> > +       tag ^= 0xff;
> > +       do {
> > +               old_flags = flags = page->flags;
>
> I guess this should be at least READ_ONCE(page->flags) if we care
> about concurrency.

Makes sense. I copied this code from page_cpupid_xchg_last() in
mm/mmzone.c which has the same problem. I'll send a patch to fix that
one as well.

Peter


      reply	other threads:[~2022-01-18 22:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-13  3:14 Peter Collingbourne
2022-01-13  3:32 ` Matthew Wilcox
2022-01-14 21:58 ` Andrey Konovalov
2022-01-18 22:38   ` Peter Collingbourne [this message]

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='CAMn1gO7zBjaAU9f=qnKKhVBxuujmqzHDpuEdHQxtePDZVBgWMg@mail.gmail.com' \
    --to=pcc@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=stable@vger.kernel.org \
    /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