linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] mm: use compare-exchange operation to set KASAN page tag
@ 2022-01-20  2:01 Peter Collingbourne
  2022-01-24 17:22 ` Andrey Konovalov
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Collingbourne @ 2022-01-20  2:01 UTC (permalink / raw)
  To: Andrey Konovalov, Andrew Morton
  Cc: Peter Collingbourne, linux-mm, linux-kernel, Peter Zijlstra, stable

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. 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
---
v3:
- use try_cmpxchg() as suggested by Peter Zijlstra on another
  patch

v2:
- use READ_ONCE()

 include/linux/mm.h | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index c768a7c81b0b..87473fe52c3f 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1531,11 +1531,18 @@ 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;
+	old_flags = READ_ONCE(page->flags);
+	do {
+		flags = old_flags;
+		flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
+		flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
+	} while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags)));
 }
 
 static inline void page_kasan_tag_reset(struct page *page)
-- 
2.34.1.703.g22d0c6ccf7-goog



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] mm: use compare-exchange operation to set KASAN page tag
  2022-01-20  2:01 [PATCH v3] mm: use compare-exchange operation to set KASAN page tag Peter Collingbourne
@ 2022-01-24 17:22 ` Andrey Konovalov
  2022-01-24 17:25   ` Andrey Konovalov
  0 siblings, 1 reply; 3+ messages in thread
From: Andrey Konovalov @ 2022-01-24 17:22 UTC (permalink / raw)
  To: Peter Collingbourne
  Cc: Andrew Morton, Linux Memory Management List, LKML,
	Peter Zijlstra, stable

On Thu, Jan 20, 2022 at 3:02 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. 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
> ---
> v3:
> - use try_cmpxchg() as suggested by Peter Zijlstra on another
>   patch
>
> v2:
> - use READ_ONCE()
>
>  include/linux/mm.h | 17 ++++++++++++-----
>  1 file changed, 12 insertions(+), 5 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index c768a7c81b0b..87473fe52c3f 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1531,11 +1531,18 @@ 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;
> +       old_flags = READ_ONCE(page->flags);
> +       do {
> +               flags = old_flags;
> +               flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
> +               flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
> +       } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags)));
>  }
>
>  static inline void page_kasan_tag_reset(struct page *page)
> --
> 2.34.1.703.g22d0c6ccf7-goog
>

Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>

FWIW, try_cmpxchg() doesn't seem to be doing annotated atomic accesses
when accessing old_flags, so using READ_ONCE() in page_kasan_tag_set()
seems pointless after all.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] mm: use compare-exchange operation to set KASAN page tag
  2022-01-24 17:22 ` Andrey Konovalov
@ 2022-01-24 17:25   ` Andrey Konovalov
  0 siblings, 0 replies; 3+ messages in thread
From: Andrey Konovalov @ 2022-01-24 17:25 UTC (permalink / raw)
  To: Peter Collingbourne
  Cc: Andrew Morton, Linux Memory Management List, LKML,
	Peter Zijlstra, stable

On Mon, Jan 24, 2022 at 6:22 PM Andrey Konovalov <andreyknvl@gmail.com> wrote:
>
> On Thu, Jan 20, 2022 at 3:02 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. 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
> > ---
> > v3:
> > - use try_cmpxchg() as suggested by Peter Zijlstra on another
> >   patch
> >
> > v2:
> > - use READ_ONCE()
> >
> >  include/linux/mm.h | 17 ++++++++++++-----
> >  1 file changed, 12 insertions(+), 5 deletions(-)
> >
> > diff --git a/include/linux/mm.h b/include/linux/mm.h
> > index c768a7c81b0b..87473fe52c3f 100644
> > --- a/include/linux/mm.h
> > +++ b/include/linux/mm.h
> > @@ -1531,11 +1531,18 @@ 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;
> > +       old_flags = READ_ONCE(page->flags);
> > +       do {
> > +               flags = old_flags;
> > +               flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
> > +               flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
> > +       } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags)));
> >  }
> >
> >  static inline void page_kasan_tag_reset(struct page *page)
> > --
> > 2.34.1.703.g22d0c6ccf7-goog
> >
>
> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
>
> FWIW, try_cmpxchg() doesn't seem to be doing annotated atomic accesses
> when accessing old_flags, so using READ_ONCE() in page_kasan_tag_set()
> seems pointless after all.

Ah, nevermind. For a second I thought that try_cmpxchg() is actually
accessing page->flags through its second argument.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-24 17:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-20  2:01 [PATCH v3] mm: use compare-exchange operation to set KASAN page tag Peter Collingbourne
2022-01-24 17:22 ` Andrey Konovalov
2022-01-24 17:25   ` Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).