Skip to contentSkip to conversation

kasan: Fix ordering between MTE tag colouring and page->flags

PATCH Applied Catalin Marinas 7 participants 28 messages Jun 10, 2022 – Feb 14, 2023

Subsystems (6)
lore
Share & tools
lore

Catalin Marinas

author maintainer
#0 lore raw

v2 cover kasan: Fix ordering between MTE tag colouring and page->flags

Hi,

That's a second attempt on fixing the race race between setting the allocation (in-memory) tags in a page and the corresponding logical tag in page->flags. Initial version here:

https://lore.kernel.org/r/[email protected]

This new series does not introduce any new GFP flags but instead always skips unpoisoning of the user pages (we already skip the poisoning on free). Any unpoisoned page will have the page->flags tag reset.

For the background:

On a system with MTE and KASAN_HW_TAGS enabled, when a page is allocated kasan_unpoison_pages() sets a random tag and saves it in page->flags so that page_to_virt() re-creates the correct tagged pointer. We need to ensure that the in-memory tags are visible before setting the page->flags:

P0 (__kasan_unpoison_range): P1 (access via virt_to_page):
Wtags=x Rflags=x

    |                               |
    | DMB                           | address dependency
    V                               V
  Wflags=x                        Rtags=x

The first patch changes the order of page unpoisoning with the tag storing in page->flags. page_kasan_tag_set() has the right barriers through try_cmpxchg().

If a page is mapped in user-space with PROT_MTE, the architecture code will set the allocation tag to 0 and a subsequent page_to_virt() dereference will fault. We currently try to fix this by resetting the tag in page->flags so that it is 0xff (match-all, not faulting). However, setting the tags and flags can race with another CPU reading the flags (page_to_virt()) and barriers can't help, e.g.:

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

                                  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since clearing the flags in the arch code doesn't work, to do this at page allocation time when __GFP_SKIP_KASAN_UNPOISON is passed.

Thanks.

Catalin Marinas (4):
mm: kasan: Ensure the tags are visible before the tag in page->flags
mm: kasan: Skip unpoisoning of user pages
mm: kasan: Skip page unpoisoning only if __GFP_SKIP_KASAN_UNPOISON
arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags"

 arch/arm64/kernel/hibernate.c |  5 -----
 arch/arm64/kernel/mte.c       |  9 ---------
 arch/arm64/mm/copypage.c      |  9 ---------
 arch/arm64/mm/fault.c         |  1 -
 arch/arm64/mm/mteswap.c       |  9 ---------
 include/linux/gfp.h           |  2 +-
 mm/kasan/common.c             |  3 ++-
 mm/page_alloc.c               | 19 ++++++++++---------
 8 files changed, 13 insertions(+), 44 deletions(-)
author maintainer
#1 lore raw

PATCH v2 1/4 mm: kasan: Ensure the tags are visible before the tag in page->flags

__kasan_unpoison_pages() colours the memory with a random tag and stores it in page->flags in order to re-create the tagged pointer via page_to_virt() later. When the tag from the page->flags is read, ensure that the in-memory tags are already visible by re-ordering the page_kasan_tag_set() after kasan_unpoison(). The former already has barriers in place through try_cmpxchg(). On the reader side, the order is ensured by the address dependency between page->flags and the memory access.

---
 mm/kasan/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kasan/common.c +2 -1
index c40c0e7b3b5f..78be2beb7453 100644
@@ -108,9 +108,10 @@ void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
 		return;
 
 	tag = kasan_random_tag();
+	kasan_unpoison(set_tag(page_address(page), tag),
+		       PAGE_SIZE << order, init);
 	for (i = 0; i < (1 << order); i++)
 		page_kasan_tag_set(page + i, tag);
-	kasan_unpoison(page_address(page), PAGE_SIZE << order, init);
 }
 
 void __kasan_poison_pages(struct page *page, unsigned int order, bool init)
reviewer Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] mm: kasan: Ensure the tags are visible before the tag in page->flags

Quoted 9 lines · Catalin Marinas

__kasan_unpoison_pages() colours the memory with a random tag and stores it in page->flags in order to re-create the tagged pointer via page_to_virt() later. When the tag from the page->flags is read, ensure that the in-memory tags are already visible by re-ordering the page_kasan_tag_set() after kasan_unpoison(). The former already has barriers in place through try_cmpxchg(). On the reader side, the order is ensured by the address dependency between page->flags and the memory access.

Quoted 4 lines
Quoted 20 lines
---
 mm/kasan/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kasan/common.c +2 -1
index c40c0e7b3b5f..78be2beb7453 100644
@@ -108,9 +108,10 @@ void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
 		return;
 
 	tag = kasan_random_tag();
+	kasan_unpoison(set_tag(page_address(page), tag),
+		       PAGE_SIZE << order, init);
 	for (i = 0; i < (1 << order); i++)
 		page_kasan_tag_set(page + i, tag);
-	kasan_unpoison(page_address(page), PAGE_SIZE << order, init);
 }
 
 void __kasan_poison_pages(struct page *page, unsigned int order, bool init)
Signature · 2 lines
Regards,
Vincenzo
maintainer
#3 lore raw

Re: [PATCH v2 1/4] mm: kasan: Ensure the tags are visible before the tag in page->flags

In reply to Catalin Marinas
Quoted 22 lines · Catalin Marinas

__kasan_unpoison_pages() colours the memory with a random tag and stores it in page->flags in order to re-create the tagged pointer via page_to_virt() later. When the tag from the page->flags is read, ensure that the in-memory tags are already visible by re-ordering the page_kasan_tag_set() after kasan_unpoison(). The former already has barriers in place through try_cmpxchg(). On the reader side, the order is ensured by the address dependency between page->flags and the memory access.

---
 mm/kasan/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kasan/common.c +0 -0
index c40c0e7b3b5f..78be2beb7453 100644

mm/kasan/common.c void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) · line 113

Quoted 8 lines
mm/kasan/common.c +2 -1
@@ -108,9 +108,10 @@ void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
 		return;
 
 	tag = kasan_random_tag();
+	kasan_unpoison(set_tag(page_address(page), tag),
+		       PAGE_SIZE << order, init);
 	for (i = 0; i < (1 << order); i++)
 		page_kasan_tag_set(page + i, tag);
-	kasan_unpoison(page_address(page), PAGE_SIZE << order, init);

This looks good to me, but after reading the cover letter I'm wondering whether the try_cmpxchg() in page_kasan_tag_set() could be relaxed to try_cmpxchg_release() as a separate optimisation?

Will

author maintainer
#4 lore raw

Re: [PATCH v2 1/4] mm: kasan: Ensure the tags are visible before the tag in page->flags

Quoted 25 lines · Will Deacon
Quoted 24 lines · Catalin Marinas

__kasan_unpoison_pages() colours the memory with a random tag and stores it in page->flags in order to re-create the tagged pointer via page_to_virt() later. When the tag from the page->flags is read, ensure that the in-memory tags are already visible by re-ordering the page_kasan_tag_set() after kasan_unpoison(). The former already has barriers in place through try_cmpxchg(). On the reader side, the order is ensured by the address dependency between page->flags and the memory access.

---
 mm/kasan/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kasan/common.c +0 -0
index c40c0e7b3b5f..78be2beb7453 100644
@@ -108,9 +108,10 @@ void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
 		return;
 
Quoted 10 lines

mm/kasan/common.c void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) · line 113

Quoted 6 lines
mm/kasan/common.c +2 -1
 	tag = kasan_random_tag();
+	kasan_unpoison(set_tag(page_address(page), tag),
+		       PAGE_SIZE << order, init);
 	for (i = 0; i < (1 << order); i++)
 		page_kasan_tag_set(page + i, tag);
-	kasan_unpoison(page_address(page), PAGE_SIZE << order, init);

This looks good to me, but after reading the cover letter I'm wondering whether the try_cmpxchg() in page_kasan_tag_set() could be relaxed to try_cmpxchg_release() as a separate optimisation?

I think it can be a try_cmpxchg_release() (I did not realise we have one). I'll post a patch later today.

Signature · 1 line
Catalin
author maintainer
#5 lore raw

PATCH v2 2/4 mm: kasan: Skip unpoisoning of user pages

In reply to Catalin Marinas

Commit c275c5c6d50a ("kasan: disable freed user page poisoning with HW tags") added __GFP_SKIP_KASAN_POISON to GFP_HIGHUSER_MOVABLE. A similar argument can be made about unpoisoning, so also add __GFP_SKIP_KASAN_UNPOISON to user pages. To ensure the user page is still accessible via page_address() without a kasan fault, reset the page->flags tag.

With the above changes, there is no need for the arm64 tag_clear_highpage() to reset the page->flags tag.

Cc Andrey Ryabinin <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
Cc Vincenzo Frascino <[email protected]>
---
 arch/arm64/mm/fault.c | 1 -
 include/linux/gfp.h   | 2 +-
 mm/page_alloc.c       | 7 +++++--
 3 files changed, 6 insertions(+), 4 deletions(-)
arch/arm64/mm/fault.c +0 -1
index c5e11768e5c1..cdf3ffa0c223 100644
@@ -927,6 +927,5 @@ struct page *alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
 void tag_clear_highpage(struct page *page)
 {
 	mte_zero_clear_page_tags(page_address(page));
-	page_kasan_tag_reset(page);
 	set_bit(PG_mte_tagged, &page->flags);
 }
include/linux/gfp.h +1 -1
index 2d2ccae933c2..0ace7759acd2 100644
@@ -348,7 +348,7 @@ struct vm_area_struct;
 #define GFP_DMA32	__GFP_DMA32
 #define GFP_HIGHUSER	(GFP_USER | __GFP_HIGHMEM)
 #define GFP_HIGHUSER_MOVABLE	(GFP_HIGHUSER | __GFP_MOVABLE | \
-			 __GFP_SKIP_KASAN_POISON)
+			 __GFP_SKIP_KASAN_POISON | __GFP_SKIP_KASAN_UNPOISON)
 #define GFP_TRANSHUGE_LIGHT	((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
 			 __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
 #define GFP_TRANSHUGE	(GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
mm/page_alloc.c +5 -2
index e008a3df0485..f6ed240870bc 100644
@@ -2397,6 +2397,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
 			!should_skip_init(gfp_flags);
 	bool init_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+	int i;
 
 	set_page_private(page, 0);
 	set_page_refcounted(page);
@@ -2422,8 +2423,6 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	 * should be initialized as well).
 	 */
 	if (init_tags) {
-		int i;
-
 		/* Initialize both memory and tags. */
 		for (i = 0; i != 1 << order; ++i)
 			tag_clear_highpage(page + i);
@@ -2438,6 +2437,10 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 		/* Note that memory is already initialized by KASAN. */
 		if (kasan_has_integrated_init())
 			init = false;
+	} else {
+		/* Ensure page_address() dereferencing does not fault. */
+		for (i = 0; i != 1 << order; ++i)
+			page_kasan_tag_reset(page + i);
 	}
 	/* If memory is still not initialized, do it now. */
 	if (init)
reviewer Reviewed-by on v2 2/4
#6 lore raw

Re: [PATCH v2 2/4] mm: kasan: Skip unpoisoning of user pages

Quoted 68 lines · Catalin Marinas

Commit c275c5c6d50a ("kasan: disable freed user page poisoning with HW tags") added __GFP_SKIP_KASAN_POISON to GFP_HIGHUSER_MOVABLE. A similar argument can be made about unpoisoning, so also add __GFP_SKIP_KASAN_UNPOISON to user pages. To ensure the user page is still accessible via page_address() without a kasan fault, reset the page->flags tag.

With the above changes, there is no need for the arm64 tag_clear_highpage() to reset the page->flags tag.

Cc Andrey Ryabinin <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
Cc Vincenzo Frascino <[email protected]>
---
 arch/arm64/mm/fault.c | 1 -
 include/linux/gfp.h   | 2 +-
 mm/page_alloc.c       | 7 +++++--
 3 files changed, 6 insertions(+), 4 deletions(-)
arch/arm64/mm/fault.c +0 -1
index c5e11768e5c1..cdf3ffa0c223 100644
@@ -927,6 +927,5 @@ struct page *alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
 void tag_clear_highpage(struct page *page)
 {
        mte_zero_clear_page_tags(page_address(page));
-       page_kasan_tag_reset(page);
        set_bit(PG_mte_tagged, &page->flags);
 }
include/linux/gfp.h +1 -1
index 2d2ccae933c2..0ace7759acd2 100644
@@ -348,7 +348,7 @@ struct vm_area_struct;
 #define GFP_DMA32      __GFP_DMA32
 #define GFP_HIGHUSER   (GFP_USER | __GFP_HIGHMEM)
 #define GFP_HIGHUSER_MOVABLE   (GFP_HIGHUSER | __GFP_MOVABLE | \
-                        __GFP_SKIP_KASAN_POISON)
+                        __GFP_SKIP_KASAN_POISON | __GFP_SKIP_KASAN_UNPOISON)
 #define GFP_TRANSHUGE_LIGHT    ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
                         __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
 #define GFP_TRANSHUGE  (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
mm/page_alloc.c +1 -2
index e008a3df0485..f6ed240870bc 100644
@@ -2397,6 +2397,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
        bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
                        !should_skip_init(gfp_flags);
        bool init_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+       int i;
 
        set_page_private(page, 0);
        set_page_refcounted(page);
@@ -2422,8 +2423,6 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
         * should be initialized as well).
         */
        if (init_tags) {
-               int i;
-
                /* Initialize both memory and tags. */
                for (i = 0; i != 1 << order; ++i)
                        tag_clear_highpage(page + i);

mm/page_alloc.c inline void post_alloc_hook(struct page *page, unsigned int order, · line 2446

Quoted 10 lines
mm/page_alloc.c +4 -0
@@ -2438,6 +2437,10 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
                /* Note that memory is already initialized by KASAN. */
                if (kasan_has_integrated_init())
                        init = false;
+       } else {
+               /* Ensure page_address() dereferencing does not fault. */
+               for (i = 0; i != 1 << order; ++i)
+                       page_kasan_tag_reset(page + i);
        }
        /* If memory is still not initialized, do it now. */
        if (init)
reviewer Reviewed-by on v2 2/4
#7 lore raw

Re: [PATCH v2 2/4] mm: kasan: Skip unpoisoning of user pages

In reply to Catalin Marinas
Quoted 50 lines · Catalin Marinas

Commit c275c5c6d50a ("kasan: disable freed user page poisoning with HW tags") added __GFP_SKIP_KASAN_POISON to GFP_HIGHUSER_MOVABLE. A similar argument can be made about unpoisoning, so also add __GFP_SKIP_KASAN_UNPOISON to user pages. To ensure the user page is still accessible via page_address() without a kasan fault, reset the page->flags tag.

With the above changes, there is no need for the arm64 tag_clear_highpage() to reset the page->flags tag.

Cc Andrey Ryabinin <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
Cc Vincenzo Frascino <[email protected]>
---
 arch/arm64/mm/fault.c | 1 -
 include/linux/gfp.h   | 2 +-
 mm/page_alloc.c       | 7 +++++--
 3 files changed, 6 insertions(+), 4 deletions(-)
arch/arm64/mm/fault.c +0 -1
index c5e11768e5c1..cdf3ffa0c223 100644
@@ -927,6 +927,5 @@ struct page *alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
 void tag_clear_highpage(struct page *page)
 {
 	mte_zero_clear_page_tags(page_address(page));
-	page_kasan_tag_reset(page);
 	set_bit(PG_mte_tagged, &page->flags);
 }
include/linux/gfp.h +1 -1
index 2d2ccae933c2..0ace7759acd2 100644
@@ -348,7 +348,7 @@ struct vm_area_struct;
 #define GFP_DMA32	__GFP_DMA32
 #define GFP_HIGHUSER	(GFP_USER | __GFP_HIGHMEM)
 #define GFP_HIGHUSER_MOVABLE	(GFP_HIGHUSER | __GFP_MOVABLE | \
-			 __GFP_SKIP_KASAN_POISON)
+			 __GFP_SKIP_KASAN_POISON | __GFP_SKIP_KASAN_UNPOISON)
 #define GFP_TRANSHUGE_LIGHT	((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
 			 __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
 #define GFP_TRANSHUGE	(GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
mm/page_alloc.c +0 -0
index e008a3df0485..f6ed240870bc 100644

mm/page_alloc.c inline void post_alloc_hook(struct page *page, unsigned int order, · line 2401

Quoted 5 lines
mm/page_alloc.c +1 -0
@@ -2397,6 +2397,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
 			!should_skip_init(gfp_flags);
 	bool init_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+	int i;
 

Nit: Since "i" is not used outside of the for loop context we could use the contract form "for (int i = 0; ..." which is allowed by C11.

Quoted 12 lines
mm/page_alloc.c +0 -2
 	set_page_private(page, 0);
 	set_page_refcounted(page);
@@ -2422,8 +2423,6 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	 * should be initialized as well).
 	 */
 	if (init_tags) {
-		int i;
-
 		/* Initialize both memory and tags. */
 		for (i = 0; i != 1 << order; ++i)
 			tag_clear_highpage(page + i);

mm/page_alloc.c inline void post_alloc_hook(struct page *page, unsigned int order, · line 2446

Quoted 10 lines
mm/page_alloc.c +4 -0
@@ -2438,6 +2437,10 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 		/* Note that memory is already initialized by KASAN. */
 		if (kasan_has_integrated_init())
 			init = false;
+	} else {
+		/* Ensure page_address() dereferencing does not fault. */
+		for (i = 0; i != 1 << order; ++i)
+			page_kasan_tag_reset(page + i);
 	}
 	/* If memory is still not initialized, do it now. */
 	if (init)

Either way:

Signature · 2 lines
Regards,
Vincenzo
author maintainer
#8 lore raw

Re: [PATCH v2 2/4] mm: kasan: Skip unpoisoning of user pages

Quoted 14 lines · Vincenzo Frascino

mm/page_alloc.c inline void post_alloc_hook(struct page *page, unsigned int order, · line 2401

Quoted 10 lines · Catalin Marinas
mm/page_alloc.c +1 -0
index e008a3df0485..f6ed240870bc 100644
@@ -2397,6 +2397,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
 			!should_skip_init(gfp_flags);
 	bool init_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+	int i;
 

Nit: Since "i" is not used outside of the for loop context we could use the contract form "for (int i = 0; ..." which is allowed by C11.

Oh yeah, Linux moved to C11 in 5.18. But it looks better to be consistent with the other for loop in this file.

Quoted 1 line

Thanks.

Signature · 1 line
Catalin
author maintainer
#9 lore raw

PATCH v2 3/4 mm: kasan: Skip page unpoisoning only if __GFP_SKIP_KASAN_UNPOISON

In reply to Catalin Marinas

Currently post_alloc_hook() skips the kasan unpoisoning if the tags will be zeroed (__GFP_ZEROTAGS) or __GFP_SKIP_KASAN_UNPOISON is passed. Since __GFP_ZEROTAGS is now accompanied by __GFP_SKIP_KASAN_UNPOISON, remove the extra check.

Cc Andrey Ryabinin <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
Cc Vincenzo Frascino <[email protected]>
---
 mm/page_alloc.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mm/page_alloc.c +5 -7
index f6ed240870bc..bf45a6aa407a 100644
@@ -2361,7 +2361,7 @@ static inline bool check_new_pcp(struct page *page, unsigned int order)
 }
 #endif /* CONFIG_DEBUG_VM */
 
-static inline bool should_skip_kasan_unpoison(gfp_t flags, bool init_tags)
+static inline bool should_skip_kasan_unpoison(gfp_t flags)
 {
 	/* Don't skip if a software KASAN mode is enabled. */
 	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
@@ -2373,12 +2373,10 @@ static inline bool should_skip_kasan_unpoison(gfp_t flags, bool init_tags)
 		return true;
 
 	/*
-	 * With hardware tag-based KASAN enabled, skip if either:
-	 *
-	 * 1. Memory tags have already been cleared via tag_clear_highpage().
-	 * 2. Skipping has been requested via __GFP_SKIP_KASAN_UNPOISON.
+	 * With hardware tag-based KASAN enabled, skip if this has been
+	 * requested via __GFP_SKIP_KASAN_UNPOISON.
 	 */
-	return init_tags || (flags & __GFP_SKIP_KASAN_UNPOISON);
+	return flags & __GFP_SKIP_KASAN_UNPOISON;
 }
 
 static inline bool should_skip_init(gfp_t flags)
@@ -2430,7 +2428,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 		/* Note that memory is already initialized by the loop above. */
 		init = false;
 	}
-	if (!should_skip_kasan_unpoison(gfp_flags, init_tags)) {
+	if (!should_skip_kasan_unpoison(gfp_flags)) {
 		/* Unpoison shadow memory or set memory tags. */
 		kasan_unpoison_pages(page, order, init);
 
reviewer Reviewed-by on v2 3/4
#10 lore raw

Re: [PATCH v2 3/4] mm: kasan: Skip page unpoisoning only if __GFP_SKIP_KASAN_UNPOISON

Quoted 45 lines · Catalin Marinas

Currently post_alloc_hook() skips the kasan unpoisoning if the tags will be zeroed (__GFP_ZEROTAGS) or __GFP_SKIP_KASAN_UNPOISON is passed. Since __GFP_ZEROTAGS is now accompanied by __GFP_SKIP_KASAN_UNPOISON, remove the extra check.

Cc Andrey Ryabinin <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
Cc Vincenzo Frascino <[email protected]>
---
 mm/page_alloc.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mm/page_alloc.c +4 -6
index f6ed240870bc..bf45a6aa407a 100644
@@ -2361,7 +2361,7 @@ static inline bool check_new_pcp(struct page *page, unsigned int order)
 }
 #endif /* CONFIG_DEBUG_VM */
 
-static inline bool should_skip_kasan_unpoison(gfp_t flags, bool init_tags)
+static inline bool should_skip_kasan_unpoison(gfp_t flags)
 {
        /* Don't skip if a software KASAN mode is enabled. */
        if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
@@ -2373,12 +2373,10 @@ static inline bool should_skip_kasan_unpoison(gfp_t flags, bool init_tags)
                return true;
 
        /*
-        * With hardware tag-based KASAN enabled, skip if either:
-        *
-        * 1. Memory tags have already been cleared via tag_clear_highpage().
-        * 2. Skipping has been requested via __GFP_SKIP_KASAN_UNPOISON.
+        * With hardware tag-based KASAN enabled, skip if this has been
+        * requested via __GFP_SKIP_KASAN_UNPOISON.
         */
-       return init_tags || (flags & __GFP_SKIP_KASAN_UNPOISON);
+       return flags & __GFP_SKIP_KASAN_UNPOISON;
 }
 
 static inline bool should_skip_init(gfp_t flags)

mm/page_alloc.c inline void post_alloc_hook(struct page *page, unsigned int order, · line 2434

Quoted 8 lines
mm/page_alloc.c +1 -1
@@ -2430,7 +2428,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
                /* Note that memory is already initialized by the loop above. */
                init = false;
        }
-       if (!should_skip_kasan_unpoison(gfp_flags, init_tags)) {
+       if (!should_skip_kasan_unpoison(gfp_flags)) {
                /* Unpoison shadow memory or set memory tags. */
                kasan_unpoison_pages(page, order, init);
 
reviewer Reviewed-by on v2 3/4
#11 lore raw

Re: [PATCH v2 3/4] mm: kasan: Skip page unpoisoning only if __GFP_SKIP_KASAN_UNPOISON

In reply to Catalin Marinas
Quoted 10 lines · Catalin Marinas

Currently post_alloc_hook() skips the kasan unpoisoning if the tags will be zeroed (__GFP_ZEROTAGS) or __GFP_SKIP_KASAN_UNPOISON is passed. Since __GFP_ZEROTAGS is now accompanied by __GFP_SKIP_KASAN_UNPOISON, remove the extra check.

Cc Andrey Ryabinin <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
Cc Vincenzo Frascino <[email protected]>
Quoted 42 lines
---
 mm/page_alloc.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mm/page_alloc.c +5 -7
index f6ed240870bc..bf45a6aa407a 100644
@@ -2361,7 +2361,7 @@ static inline bool check_new_pcp(struct page *page, unsigned int order)
 }
 #endif /* CONFIG_DEBUG_VM */
 
-static inline bool should_skip_kasan_unpoison(gfp_t flags, bool init_tags)
+static inline bool should_skip_kasan_unpoison(gfp_t flags)
 {
 	/* Don't skip if a software KASAN mode is enabled. */
 	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
@@ -2373,12 +2373,10 @@ static inline bool should_skip_kasan_unpoison(gfp_t flags, bool init_tags)
 		return true;
 
 	/*
-	 * With hardware tag-based KASAN enabled, skip if either:
-	 *
-	 * 1. Memory tags have already been cleared via tag_clear_highpage().
-	 * 2. Skipping has been requested via __GFP_SKIP_KASAN_UNPOISON.
+	 * With hardware tag-based KASAN enabled, skip if this has been
+	 * requested via __GFP_SKIP_KASAN_UNPOISON.
 	 */
-	return init_tags || (flags & __GFP_SKIP_KASAN_UNPOISON);
+	return flags & __GFP_SKIP_KASAN_UNPOISON;
 }
 
 static inline bool should_skip_init(gfp_t flags)
@@ -2430,7 +2428,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 		/* Note that memory is already initialized by the loop above. */
 		init = false;
 	}
-	if (!should_skip_kasan_unpoison(gfp_flags, init_tags)) {
+	if (!should_skip_kasan_unpoison(gfp_flags)) {
 		/* Unpoison shadow memory or set memory tags. */
 		kasan_unpoison_pages(page, order, init);
 
Signature · 2 lines
Regards,
Vincenzo
author maintainer
#12 lore raw

PATCH v2 4/4 arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags"

In reply to Catalin Marinas

This reverts commit e5b8d9218951e59df986f627ec93569a0d22149b.

Pages mapped in user-space with PROT_MTE have the allocation tags either zeroed or copied/restored to some user values. In order for the kernel to access such pages via page_address(), resetting the tag in page->flags was necessary. This tag resetting was deferred to set_pte_at() -> mte_sync_page_tags() but it can race with another CPU reading the flags (via page_to_virt()):

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

				  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since now the post_alloc_hook() function resets the page->flags tag when unpoisoning is skipped for user pages (including the __GFP_ZEROTAGS case), revert the arm64 commit calling page_kasan_tag_reset().

Cc Will Deacon <[email protected]>
Cc Vincenzo Frascino <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
---
 arch/arm64/kernel/hibernate.c | 5 -----
 arch/arm64/kernel/mte.c       | 9 ---------
 arch/arm64/mm/copypage.c      | 9 ---------
 arch/arm64/mm/mteswap.c       | 9 ---------
 4 files changed, 32 deletions(-)
arch/arm64/kernel/hibernate.c +0 -5
index 2e248342476e..af5df48ba915 100644
@@ -300,11 +300,6 @@ static void swsusp_mte_restore_tags(void)
 		unsigned long pfn = xa_state.xa_index;
 		struct page *page = pfn_to_online_page(pfn);
 
-		/*
-		 * It is not required to invoke page_kasan_tag_reset(page)
-		 * at this point since the tags stored in page->flags are
-		 * already restored.
-		 */
 		mte_restore_page_tags(page_address(page), tags);
 
 		mte_free_tag_storage(tags);
arch/arm64/kernel/mte.c +0 -9
index 57b30bcf9f21..7ba4d6fd1f72 100644
@@ -48,15 +48,6 @@ static void mte_sync_page_tags(struct page *page, pte_t old_pte,
 	if (!pte_is_tagged)
 		return;
 
-	page_kasan_tag_reset(page);
-	/*
-	 * We need smp_wmb() in between setting the flags and clearing the
-	 * tags because if another thread reads page->flags and builds a
-	 * tagged address out of it, there is an actual dependency to the
-	 * memory access, but on the current thread we do not guarantee that
-	 * the new page->flags are visible before the tags were updated.
-	 */
-	smp_wmb();
 	mte_clear_page_tags(page_address(page));
 }
 
arch/arm64/mm/copypage.c +0 -9
index 0dea80bf6de4..24913271e898 100644
@@ -23,15 +23,6 @@ void copy_highpage(struct page *to, struct page *from)
 
 	if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
 		set_bit(PG_mte_tagged, &to->flags);
-		page_kasan_tag_reset(to);
-		/*
-		 * We need smp_wmb() in between setting the flags and clearing the
-		 * tags because if another thread reads page->flags and builds a
-		 * tagged address out of it, there is an actual dependency to the
-		 * memory access, but on the current thread we do not guarantee that
-		 * the new page->flags are visible before the tags were updated.
-		 */
-		smp_wmb();
 		mte_copy_page_tags(kto, kfrom);
 	}
 }
arch/arm64/mm/mteswap.c +0 -9
index a9e50e930484..4334dec93bd4 100644
@@ -53,15 +53,6 @@ bool mte_restore_tags(swp_entry_t entry, struct page *page)
 	if (!tags)
 		return false;
 
-	page_kasan_tag_reset(page);
-	/*
-	 * We need smp_wmb() in between setting the flags and clearing the
-	 * tags because if another thread reads page->flags and builds a
-	 * tagged address out of it, there is an actual dependency to the
-	 * memory access, but on the current thread we do not guarantee that
-	 * the new page->flags are visible before the tags were updated.
-	 */
-	smp_wmb();
 	mte_restore_page_tags(page_address(page), tags);
 
 	return true;
reviewer Acked-by on v2 4/4
#13 lore raw

Re: [PATCH v2 4/4] arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags"

Quoted 99 lines · Catalin Marinas

This reverts commit e5b8d9218951e59df986f627ec93569a0d22149b.

Pages mapped in user-space with PROT_MTE have the allocation tags either zeroed or copied/restored to some user values. In order for the kernel to access such pages via page_address(), resetting the tag in page->flags was necessary. This tag resetting was deferred to set_pte_at() -> mte_sync_page_tags() but it can race with another CPU reading the flags (via page_to_virt()):

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

                                  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since now the post_alloc_hook() function resets the page->flags tag when unpoisoning is skipped for user pages (including the __GFP_ZEROTAGS case), revert the arm64 commit calling page_kasan_tag_reset().

Cc Will Deacon <[email protected]>
Cc Vincenzo Frascino <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
---
 arch/arm64/kernel/hibernate.c | 5 -----
 arch/arm64/kernel/mte.c       | 9 ---------
 arch/arm64/mm/copypage.c      | 9 ---------
 arch/arm64/mm/mteswap.c       | 9 ---------
 4 files changed, 32 deletions(-)
arch/arm64/kernel/hibernate.c +0 -5
index 2e248342476e..af5df48ba915 100644
@@ -300,11 +300,6 @@ static void swsusp_mte_restore_tags(void)
                unsigned long pfn = xa_state.xa_index;
                struct page *page = pfn_to_online_page(pfn);
 
-               /*
-                * It is not required to invoke page_kasan_tag_reset(page)
-                * at this point since the tags stored in page->flags are
-                * already restored.
-                */
                mte_restore_page_tags(page_address(page), tags);
 
                mte_free_tag_storage(tags);
arch/arm64/kernel/mte.c +0 -9
index 57b30bcf9f21..7ba4d6fd1f72 100644
@@ -48,15 +48,6 @@ static void mte_sync_page_tags(struct page *page, pte_t old_pte,
        if (!pte_is_tagged)
                return;
 
-       page_kasan_tag_reset(page);
-       /*
-        * We need smp_wmb() in between setting the flags and clearing the
-        * tags because if another thread reads page->flags and builds a
-        * tagged address out of it, there is an actual dependency to the
-        * memory access, but on the current thread we do not guarantee that
-        * the new page->flags are visible before the tags were updated.
-        */
-       smp_wmb();
        mte_clear_page_tags(page_address(page));
 }
 
arch/arm64/mm/copypage.c +0 -9
index 0dea80bf6de4..24913271e898 100644
@@ -23,15 +23,6 @@ void copy_highpage(struct page *to, struct page *from)
 
        if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
                set_bit(PG_mte_tagged, &to->flags);
-               page_kasan_tag_reset(to);
-               /*
-                * We need smp_wmb() in between setting the flags and clearing the
-                * tags because if another thread reads page->flags and builds a
-                * tagged address out of it, there is an actual dependency to the
-                * memory access, but on the current thread we do not guarantee that
-                * the new page->flags are visible before the tags were updated.
-                */
-               smp_wmb();
                mte_copy_page_tags(kto, kfrom);
        }
 }
arch/arm64/mm/mteswap.c +0 -2
index a9e50e930484..4334dec93bd4 100644
@@ -53,15 +53,6 @@ bool mte_restore_tags(swp_entry_t entry, struct page *page)
        if (!tags)
                return false;
 
-       page_kasan_tag_reset(page);
-       /*

arch/arm64/mm/mteswap.c bool mte_restore_tags(swp_entry_t entry, struct page *page) · line 58

Quoted 10 lines
arch/arm64/mm/mteswap.c +0 -7
-        * We need smp_wmb() in between setting the flags and clearing the
-        * tags because if another thread reads page->flags and builds a
-        * tagged address out of it, there is an actual dependency to the
-        * memory access, but on the current thread we do not guarantee that
-        * the new page->flags are visible before the tags were updated.
-        */
-       smp_wmb();
        mte_restore_page_tags(page_address(page), tags);
 
        return true;
reviewer Reviewed-by on v2 4/4
#14 lore raw

Re: [PATCH v2 4/4] arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags"

In reply to Catalin Marinas
Quoted 20 lines · Catalin Marinas

This reverts commit e5b8d9218951e59df986f627ec93569a0d22149b.

Pages mapped in user-space with PROT_MTE have the allocation tags either zeroed or copied/restored to some user values. In order for the kernel to access such pages via page_address(), resetting the tag in page->flags was necessary. This tag resetting was deferred to set_pte_at() -> mte_sync_page_tags() but it can race with another CPU reading the flags (via page_to_virt()):

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

				  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since now the post_alloc_hook() function resets the page->flags tag when unpoisoning is skipped for user pages (including the __GFP_ZEROTAGS case), revert the arm64 commit calling page_kasan_tag_reset().

Quoted 5 lines
Cc Will Deacon <[email protected]>
Cc Vincenzo Frascino <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Peter Collingbourne <[email protected]>
Quoted 83 lines
---
 arch/arm64/kernel/hibernate.c | 5 -----
 arch/arm64/kernel/mte.c       | 9 ---------
 arch/arm64/mm/copypage.c      | 9 ---------
 arch/arm64/mm/mteswap.c       | 9 ---------
 4 files changed, 32 deletions(-)
arch/arm64/kernel/hibernate.c +0 -5
index 2e248342476e..af5df48ba915 100644
@@ -300,11 +300,6 @@ static void swsusp_mte_restore_tags(void)
 		unsigned long pfn = xa_state.xa_index;
 		struct page *page = pfn_to_online_page(pfn);
 
-		/*
-		 * It is not required to invoke page_kasan_tag_reset(page)
-		 * at this point since the tags stored in page->flags are
-		 * already restored.
-		 */
 		mte_restore_page_tags(page_address(page), tags);
 
 		mte_free_tag_storage(tags);
arch/arm64/kernel/mte.c +0 -9
index 57b30bcf9f21..7ba4d6fd1f72 100644
@@ -48,15 +48,6 @@ static void mte_sync_page_tags(struct page *page, pte_t old_pte,
 	if (!pte_is_tagged)
 		return;
 
-	page_kasan_tag_reset(page);
-	/*
-	 * We need smp_wmb() in between setting the flags and clearing the
-	 * tags because if another thread reads page->flags and builds a
-	 * tagged address out of it, there is an actual dependency to the
-	 * memory access, but on the current thread we do not guarantee that
-	 * the new page->flags are visible before the tags were updated.
-	 */
-	smp_wmb();
 	mte_clear_page_tags(page_address(page));
 }
 
arch/arm64/mm/copypage.c +0 -9
index 0dea80bf6de4..24913271e898 100644
@@ -23,15 +23,6 @@ void copy_highpage(struct page *to, struct page *from)
 
 	if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
 		set_bit(PG_mte_tagged, &to->flags);
-		page_kasan_tag_reset(to);
-		/*
-		 * We need smp_wmb() in between setting the flags and clearing the
-		 * tags because if another thread reads page->flags and builds a
-		 * tagged address out of it, there is an actual dependency to the
-		 * memory access, but on the current thread we do not guarantee that
-		 * the new page->flags are visible before the tags were updated.
-		 */
-		smp_wmb();
 		mte_copy_page_tags(kto, kfrom);
 	}
 }
arch/arm64/mm/mteswap.c +0 -9
index a9e50e930484..4334dec93bd4 100644
@@ -53,15 +53,6 @@ bool mte_restore_tags(swp_entry_t entry, struct page *page)
 	if (!tags)
 		return false;
 
-	page_kasan_tag_reset(page);
-	/*
-	 * We need smp_wmb() in between setting the flags and clearing the
-	 * tags because if another thread reads page->flags and builds a
-	 * tagged address out of it, there is an actual dependency to the
-	 * memory access, but on the current thread we do not guarantee that
-	 * the new page->flags are visible before the tags were updated.
-	 */
-	smp_wmb();
 	mte_restore_page_tags(page_address(page), tags);
 
 	return true;
Signature · 2 lines
Regards,
Vincenzo
maintainer Applied
#15 lore raw
In reply to Catalin Marinas
Quoted 11 lines · Catalin Marinas

That's a second attempt on fixing the race race between setting the allocation (in-memory) tags in a page and the corresponding logical tag in page->flags. Initial version here:

https://lore.kernel.org/r/[email protected]

This new series does not introduce any new GFP flags but instead always skips unpoisoning of the user pages (we already skip the poisoning on free). Any unpoisoned page will have the page->flags tag reset.

[...]

Applied to arm64 (for-next/mte), thanks!

[1/4] mm: kasan: Ensure the tags are visible before the tag in page->flags

      https://git.kernel.org/arm64/c/ed0a6d1d973e
[2/4] mm: kasan: Skip unpoisoning of user pages
      https://git.kernel.org/arm64/c/70c248aca9e7
[3/4] mm: kasan: Skip page unpoisoning only if __GFP_SKIP_KASAN_UNPOISON
      https://git.kernel.org/arm64/c/6d05141a3930
[4/4] arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags"
      https://git.kernel.org/arm64/c/20794545c146

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev
maintainer Applied
#16 lore raw
In reply to Catalin Marinas
Quoted 39 lines · Catalin Marinas

Hi,

That's a second attempt on fixing the race race between setting the allocation (in-memory) tags in a page and the corresponding logical tag in page->flags. Initial version here:

https://lore.kernel.org/r/[email protected]

This new series does not introduce any new GFP flags but instead always skips unpoisoning of the user pages (we already skip the poisoning on free). Any unpoisoned page will have the page->flags tag reset.

For the background:

On a system with MTE and KASAN_HW_TAGS enabled, when a page is allocated kasan_unpoison_pages() sets a random tag and saves it in page->flags so that page_to_virt() re-creates the correct tagged pointer. We need to ensure that the in-memory tags are visible before setting the page->flags:

P0 (__kasan_unpoison_range): P1 (access via virt_to_page):
Wtags=x Rflags=x

    |                               |
    | DMB                           | address dependency
    V                               V
  Wflags=x                        Rtags=x

The first patch changes the order of page unpoisoning with the tag storing in page->flags. page_kasan_tag_set() has the right barriers through try_cmpxchg().

If a page is mapped in user-space with PROT_MTE, the architecture code will set the allocation tag to 0 and a subsequent page_to_virt() dereference will fault. We currently try to fix this by resetting the tag in page->flags so that it is 0xff (match-all, not faulting). However, setting the tags and flags can race with another CPU reading the flags (page_to_virt()) and barriers can't help, e.g.:

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

Quoted 8 lines
                                  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since clearing the flags in the arch code doesn't work, to do this at page allocation time when __GFP_SKIP_KASAN_UNPOISON is passed.

I've picked this up, thanks.

An alternative solution might be to use a seqlock (if you can find somewhere to put it) so that virt_to_page() spins briefly while the tags and flags are being updated.

Will

#17 lore raw
In reply to Catalin Marinas
Quoted 8 lines · Catalin Marinas

Hi,

That's a second attempt on fixing the race race between setting the allocation (in-memory) tags in a page and the corresponding logical tag
in page->flags. Initial version here:

https://lore.kernel.org/r/[email protected]

Quoted 55 lines

This new series does not introduce any new GFP flags but instead always
skips unpoisoning of the user pages (we already skip the poisoning on free). Any unpoisoned page will have the page->flags tag reset.

For the background:

On a system with MTE and KASAN_HW_TAGS enabled, when a page is allocated
kasan_unpoison_pages() sets a random tag and saves it in page->flags so
that page_to_virt() re-creates the correct tagged pointer. We need to ensure that the in-memory tags are visible before setting the page->flags:

P0 (__kasan_unpoison_range): P1 (access via virt_to_page):
Wtags=x Rflags=x

    |                               |
    | DMB                           | address dependency
    V                               V
  Wflags=x                        Rtags=x

The first patch changes the order of page unpoisoning with the tag storing in page->flags. page_kasan_tag_set() has the right barriers through try_cmpxchg().

If a page is mapped in user-space with PROT_MTE, the architecture code
will set the allocation tag to 0 and a subsequent page_to_virt() dereference will fault. We currently try to fix this by resetting the tag in page->flags so that it is 0xff (match-all, not faulting). However, setting the tags and flags can race with another CPU reading the flags (page_to_virt()) and barriers can't help, e.g.:

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

                                  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since clearing the flags in the arch code doesn't work, to do this at page allocation time when __GFP_SKIP_KASAN_UNPOISON is passed.

Thanks.

Catalin Marinas (4):
mm: kasan: Ensure the tags are visible before the tag in page-

Quoted 1 line

flags

mm: kasan: Skip unpoisoning of user pages
mm: kasan: Skip page unpoisoning only if __GFP_SKIP_KASAN_UNPOISON
arm64: kasan: Revert "arm64: mte: reset the page tag in page-

Quoted 1 line

flags"

Quoted 10 lines
 arch/arm64/kernel/hibernate.c |  5 -----
 arch/arm64/kernel/mte.c       |  9 ---------
 arch/arm64/mm/copypage.c      |  9 ---------
 arch/arm64/mm/fault.c         |  1 -
 arch/arm64/mm/mteswap.c       |  9 ---------
 include/linux/gfp.h           |  2 +-
 mm/kasan/common.c             |  3 ++-
 mm/page_alloc.c               | 19 ++++++++++---------
 8 files changed, 13 insertions(+), 44 deletions(-)

Hi kasan maintainers,

We hit the following issue on the android-6.1 devices with MTE and HW tag kasan enabled.

I observe that the anon flag doesn't have skip_kasan_poison and skip_kasan_unpoison flag and kasantag is weird.

AFAIK, kasantag of anon flag needs to be 0x0.

[   71.953938] [T1403598] FramePolicy:
[name:report&]=========================================================
=========
[   71.955305] [T1403598] FramePolicy: [name:report&]BUG: KASAN:
invalid-access in copy_page+0x10/0xd0
[   71.956476] [T1403598] FramePolicy: [name:report&]Read at addr
f0ffff81332a8000 by task FramePolicy/3598
[   71.957673] [T1403598] FramePolicy: [name:report_hw_tags&]Pointer
tag: [f0], memory tag: [ff]
[   71.958746] [T1403598] FramePolicy: [name:report&]
[   71.959354] [T1403598] FramePolicy: CPU: 4 PID: 3598 Comm:
FramePolicy Tainted: G S      W  OE      6.1.0-mainline-android14-0-
ga8a53f83b9e4 #1
[   71.960978] [T1403598] FramePolicy: Hardware name: MT6985(ENG) (DT)
[   71.961767] [T1403598] FramePolicy: Call trace:
[   71.962338] [T1403598] FramePolicy:  dump_backtrace+0x108/0x158
[   71.963097] [T1403598] FramePolicy:  show_stack+0x20/0x48
[   71.963782] [T1403598] FramePolicy:  dump_stack_lvl+0x6c/0x88
[   71.964512] [T1403598] FramePolicy:  print_report+0x2cc/0xa64
[   71.965263] [T1403598] FramePolicy:  kasan_report+0xb8/0x138
[   71.965986] [T1403598] FramePolicy:  __do_kernel_fault+0xd4/0x248
[   71.966782] [T1403598] FramePolicy:  do_bad_area+0x38/0xe8
[   71.967484] [T1403598] FramePolicy:  do_tag_check_fault+0x24/0x38
[   71.968261] [T1403598] FramePolicy:  do_mem_abort+0x48/0xb0
[   71.968973] [T1403598] FramePolicy:  el1_abort+0x44/0x68
[   71.969646] [T1403598] FramePolicy:  el1h_64_sync_handler+0x68/0xb8
[   71.970440] [T1403598] FramePolicy:  el1h_64_sync+0x68/0x6c
[   71.971146] [T1403598] FramePolicy:  copy_page+0x10/0xd0
[   71.971824] [T1403598] FramePolicy:  copy_user_highpage+0x20/0x40
[   71.972603] [T1403598] FramePolicy:  wp_page_copy+0xd0/0x9f8
[   71.973344] [T1403598] FramePolicy:  do_wp_page+0x374/0x3b0
[   71.974056] [T1403598] FramePolicy:  handle_mm_fault+0x3ec/0x119c
[   71.974833] [T1403598] FramePolicy:  do_page_fault+0x344/0x4ac
[   71.975583] [T1403598] FramePolicy:  do_mem_abort+0x48/0xb0
[   71.976294] [T1403598] FramePolicy:  el0_da+0x4c/0xe0
[   71.976934] [T1403598] FramePolicy:  el0t_64_sync_handler+0xd4/0xfc
[   71.977725] [T1403598] FramePolicy:  el0t_64_sync+0x1a0/0x1a4
[   71.978451] [T1403598] FramePolicy: [name:report&]
[   71.979057] [T1403598] FramePolicy: [name:report&]The buggy address
belongs to the physical page:
[   71.980173] [T1403598] FramePolicy:
[name:debug&]page:fffffffe04ccaa00 refcount:14 mapcount:13
mapping:0000000000000000 index:0x7884c74 pfn:0x1732a8
[   71.981849] [T1403598] FramePolicy:
[name:debug&]memcg:faffff80c0241000
[   71.982680] [T1403598] FramePolicy: [name:debug&]anon flags:
0x43c000000048003e(referenced|uptodate|dirty|lru|active|swapbacked|arch
_2|zone=1|kasantag=0xf)
[   71.984446] [T1403598] FramePolicy: raw: 43c000000048003e
fffffffe04b99648 fffffffe04cca308 f2ffff8103390831
[   71.985684] [T1403598] FramePolicy: raw: 0000000007884c74
0000000000000000 0000000e0000000c faffff80c0241000
[   71.986919] [T1403598] FramePolicy: [name:debug&]page dumped
because: kasan: bad access detected
[   71.988022] [T1403598] FramePolicy: [name:report&]
[   71.988624] [T1403598] FramePolicy: [name:report&]Memory state
around the buggy address:
[   71.989641] [T1403598] FramePolicy:  ffffff81332a7e00: fe fe fe fe
fe fe fe fe fe fe fe fe fe fe fe fe
[   71.990811] [T1403598] FramePolicy:  ffffff81332a7f00: fe fe fe fe
fe fe fe fe fe fe fe fe fe fe fe fe
[   71.991982] [T1403598] FramePolicy: >ffffff81332a8000: ff ff ff ff
f0 f0 fc fc fc fc fc fc fc f0 f0 f3
[   71.993149] [T1403598] FramePolicy:
[name:report&]                   ^
[   71.993972] [T1403598] FramePolicy:  ffffff81332a8100: f3 f3 f3 f3
f3 f3 f0 f0 f8 f8 f8 f8 f8 f8 f8 f0
[   71.995141] [T1403598] FramePolicy:  ffffff81332a8200: f0 fb fb fb
fb fb fb fb f0 f0 fe fe fe fe fe fe
[   71.996332] [T1403598] FramePolicy:

[name:report&]=========================================================
=========

Originally, I suspect that some userspace pages have been migrated so the page->flags will be lost and page->flags is re-generated by alloc_pages().

I try the following diff, but it didn't help.

mm/migrate.c +1 -0
index dff333593a8a..ed2065908418 100644
@@ -51,6 +51,7 @@
 #include <linux/random.h>
 #include <linux/sched/sysctl.h>
 #include <linux/memory-tiers.h>
+#include <linux/kasan.h>
 
 #include <asm/tlbflush.h>
 
@@ -611,6 +612,14 @@ void folio_migrate_flags(struct folio *newfolio,

struct folio *folio)

if (!folio_test_hugetlb(folio))
mem_cgroup_migrate(folio, newfolio);
+
+#ifdef CONFIG_KASAN_HW_TAGS
+ if (kasan_hw_tags_enabled()) {
+ if (folio_test_skip_kasan_poison(folio))
+ folio_set_skip_kasan_poison(newfolio);
+ page_kasan_tag_set(&newfolio->page,
page_kasan_tag(&folio->page));
+ }
+#endif
}
EXPORT_SYMBOL(folio_migrate_flags);

After I revert this patchset (4 patches), this issue disappear.

Quoted 1 line
#18 lore raw
Quoted 157 lines · Kuan-Ying Lee (李冠穎)
Quoted 8 lines · Catalin Marinas

Hi,

That's a second attempt on fixing the race race between setting the allocation (in-memory) tags in a page and the corresponding logical tag
in page->flags. Initial version here:

https://lore.kernel.org/r/[email protected]

Quoted 55 lines

This new series does not introduce any new GFP flags but instead always
skips unpoisoning of the user pages (we already skip the poisoning on free). Any unpoisoned page will have the page->flags tag reset.

For the background:

On a system with MTE and KASAN_HW_TAGS enabled, when a page is allocated
kasan_unpoison_pages() sets a random tag and saves it in page->flags so
that page_to_virt() re-creates the correct tagged pointer. We need to ensure that the in-memory tags are visible before setting the page->flags:

P0 (__kasan_unpoison_range): P1 (access via virt_to_page):
Wtags=x Rflags=x

    |                               |
    | DMB                           | address dependency
    V                               V
  Wflags=x                        Rtags=x

The first patch changes the order of page unpoisoning with the tag storing in page->flags. page_kasan_tag_set() has the right barriers through try_cmpxchg().

If a page is mapped in user-space with PROT_MTE, the architecture code
will set the allocation tag to 0 and a subsequent page_to_virt() dereference will fault. We currently try to fix this by resetting the tag in page->flags so that it is 0xff (match-all, not faulting). However, setting the tags and flags can race with another CPU reading the flags (page_to_virt()) and barriers can't help, e.g.:

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

                                  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since clearing the flags in the arch code doesn't work, to do this at page allocation time when __GFP_SKIP_KASAN_UNPOISON is passed.

Thanks.

Catalin Marinas (4):
mm: kasan: Ensure the tags are visible before the tag in page-

Quoted 1 line

flags

mm: kasan: Skip unpoisoning of user pages
mm: kasan: Skip page unpoisoning only if __GFP_SKIP_KASAN_UNPOISON
arm64: kasan: Revert "arm64: mte: reset the page tag in page-

Quoted 1 line

flags"

Quoted 10 lines
 arch/arm64/kernel/hibernate.c |  5 -----
 arch/arm64/kernel/mte.c       |  9 ---------
 arch/arm64/mm/copypage.c      |  9 ---------
 arch/arm64/mm/fault.c         |  1 -
 arch/arm64/mm/mteswap.c       |  9 ---------
 include/linux/gfp.h           |  2 +-
 mm/kasan/common.c             |  3 ++-
 mm/page_alloc.c               | 19 ++++++++++---------
 8 files changed, 13 insertions(+), 44 deletions(-)

Hi kasan maintainers,

We hit the following issue on the android-6.1 devices with MTE and HW tag kasan enabled.

I observe that the anon flag doesn't have skip_kasan_poison and skip_kasan_unpoison flag and kasantag is weird.

AFAIK, kasantag of anon flag needs to be 0x0.

[   71.953938] [T1403598] FramePolicy:
[name:report&]=========================================================
=========
[   71.955305] [T1403598] FramePolicy: [name:report&]BUG: KASAN:
invalid-access in copy_page+0x10/0xd0
[   71.956476] [T1403598] FramePolicy: [name:report&]Read at addr
f0ffff81332a8000 by task FramePolicy/3598
[   71.957673] [T1403598] FramePolicy: [name:report_hw_tags&]Pointer
tag: [f0], memory tag: [ff]
[   71.958746] [T1403598] FramePolicy: [name:report&]
[   71.959354] [T1403598] FramePolicy: CPU: 4 PID: 3598 Comm:
FramePolicy Tainted: G S      W  OE      6.1.0-mainline-android14-0-
ga8a53f83b9e4 #1
[   71.960978] [T1403598] FramePolicy: Hardware name: MT6985(ENG) (DT)
[   71.961767] [T1403598] FramePolicy: Call trace:
[   71.962338] [T1403598] FramePolicy:  dump_backtrace+0x108/0x158
[   71.963097] [T1403598] FramePolicy:  show_stack+0x20/0x48
[   71.963782] [T1403598] FramePolicy:  dump_stack_lvl+0x6c/0x88
[   71.964512] [T1403598] FramePolicy:  print_report+0x2cc/0xa64
[   71.965263] [T1403598] FramePolicy:  kasan_report+0xb8/0x138
[   71.965986] [T1403598] FramePolicy:  __do_kernel_fault+0xd4/0x248
[   71.966782] [T1403598] FramePolicy:  do_bad_area+0x38/0xe8
[   71.967484] [T1403598] FramePolicy:  do_tag_check_fault+0x24/0x38
[   71.968261] [T1403598] FramePolicy:  do_mem_abort+0x48/0xb0
[   71.968973] [T1403598] FramePolicy:  el1_abort+0x44/0x68
[   71.969646] [T1403598] FramePolicy:  el1h_64_sync_handler+0x68/0xb8
[   71.970440] [T1403598] FramePolicy:  el1h_64_sync+0x68/0x6c
[   71.971146] [T1403598] FramePolicy:  copy_page+0x10/0xd0
[   71.971824] [T1403598] FramePolicy:  copy_user_highpage+0x20/0x40
[   71.972603] [T1403598] FramePolicy:  wp_page_copy+0xd0/0x9f8
[   71.973344] [T1403598] FramePolicy:  do_wp_page+0x374/0x3b0
[   71.974056] [T1403598] FramePolicy:  handle_mm_fault+0x3ec/0x119c
[   71.974833] [T1403598] FramePolicy:  do_page_fault+0x344/0x4ac
[   71.975583] [T1403598] FramePolicy:  do_mem_abort+0x48/0xb0
[   71.976294] [T1403598] FramePolicy:  el0_da+0x4c/0xe0
[   71.976934] [T1403598] FramePolicy:  el0t_64_sync_handler+0xd4/0xfc
[   71.977725] [T1403598] FramePolicy:  el0t_64_sync+0x1a0/0x1a4
[   71.978451] [T1403598] FramePolicy: [name:report&]
[   71.979057] [T1403598] FramePolicy: [name:report&]The buggy address
belongs to the physical page:
[   71.980173] [T1403598] FramePolicy:
[name:debug&]page:fffffffe04ccaa00 refcount:14 mapcount:13
mapping:0000000000000000 index:0x7884c74 pfn:0x1732a8
[   71.981849] [T1403598] FramePolicy:
[name:debug&]memcg:faffff80c0241000
[   71.982680] [T1403598] FramePolicy: [name:debug&]anon flags:
0x43c000000048003e(referenced|uptodate|dirty|lru|active|swapbacked|arch
_2|zone=1|kasantag=0xf)
[   71.984446] [T1403598] FramePolicy: raw: 43c000000048003e
fffffffe04b99648 fffffffe04cca308 f2ffff8103390831
[   71.985684] [T1403598] FramePolicy: raw: 0000000007884c74
0000000000000000 0000000e0000000c faffff80c0241000
[   71.986919] [T1403598] FramePolicy: [name:debug&]page dumped
because: kasan: bad access detected
[   71.988022] [T1403598] FramePolicy: [name:report&]
[   71.988624] [T1403598] FramePolicy: [name:report&]Memory state
around the buggy address:
[   71.989641] [T1403598] FramePolicy:  ffffff81332a7e00: fe fe fe fe
fe fe fe fe fe fe fe fe fe fe fe fe
[   71.990811] [T1403598] FramePolicy:  ffffff81332a7f00: fe fe fe fe
fe fe fe fe fe fe fe fe fe fe fe fe
[   71.991982] [T1403598] FramePolicy: >ffffff81332a8000: ff ff ff ff
f0 f0 fc fc fc fc fc fc fc f0 f0 f3
[   71.993149] [T1403598] FramePolicy:
[name:report&]                   ^
[   71.993972] [T1403598] FramePolicy:  ffffff81332a8100: f3 f3 f3 f3
f3 f3 f0 f0 f8 f8 f8 f8 f8 f8 f8 f0
[   71.995141] [T1403598] FramePolicy:  ffffff81332a8200: f0 fb fb fb
fb fb fb fb f0 f0 fe fe fe fe fe fe
[   71.996332] [T1403598] FramePolicy:
Quoted 6 lines

[name:report&]=========================================================
=========

Originally, I suspect that some userspace pages have been migrated so the page->flags will be lost and page->flags is re-generated by alloc_pages().

Hi Kuan-Ying,

There recently was a similar crash due to incorrectly implemented sampling.

Do you have the following patch in your tree?

https://android.googlesource.com/kernel/common/+/9f7f5a25f335e6e1484695da9180281a728db7e2

If not, please sync your 6.1 tree with the Android common kernel. Hopefully this will fix the issue.

Thanks!

#19 lore raw
Quoted 6 lines · Andrey Konovalov
Quoted 4 lines · Kuan-Ying Lee (李冠穎)
Quoted 2 lines · Catalin Marinas

Hi,

Quoted 10 lines
Quoted 10 lines
Quoted 8 lines

That's a second attempt on fixing the race race between setting the
allocation (in-memory) tags in a page and the corresponding logical
tag
in page->flags. Initial version here:

https://lore.kernel.org/r/[email protected]

Quoted 183 lines
Quoted 174 lines
Quoted 65 lines

This new series does not introduce any new GFP flags but instead always
skips unpoisoning of the user pages (we already skip the poisoning on
free). Any unpoisoned page will have the page->flags tag reset.

For the background:

On a system with MTE and KASAN_HW_TAGS enabled, when a page is allocated
kasan_unpoison_pages() sets a random tag and saves it in page-

Quoted 1 line

flags

so
that page_to_virt() re-creates the correct tagged pointer. We need to
ensure that the in-memory tags are visible before setting the page->flags:

P0 (__kasan_unpoison_range): P1 (access via virt_to_page):
Wtags=x Rflags=x

    |                               |
    | DMB                           | address dependency
    V                               V
  Wflags=x                        Rtags=x

The first patch changes the order of page unpoisoning with the tag
storing in page->flags. page_kasan_tag_set() has the right barriers
through try_cmpxchg().

If a page is mapped in user-space with PROT_MTE, the architecture code
will set the allocation tag to 0 and a subsequent page_to_virt() dereference will fault. We currently try to fix this by resetting the
tag in page->flags so that it is 0xff (match-all, not faulting). However, setting the tags and flags can race with another CPU reading
the flags (page_to_virt()) and barriers can't help, e.g.:

P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page):

                                  Rflags!=0xff

Wflags=0xff
DMB (doesn't help)
Wtags=0
Rtags=0 // fault

Since clearing the flags in the arch code doesn't work, to do this at
page allocation time when __GFP_SKIP_KASAN_UNPOISON is passed.

Thanks.

Catalin Marinas (4):
mm: kasan: Ensure the tags are visible before the tag in page-

Quoted 1 line

flags

mm: kasan: Skip unpoisoning of user pages
mm: kasan: Skip page unpoisoning only if
__GFP_SKIP_KASAN_UNPOISON
arm64: kasan: Revert "arm64: mte: reset the page tag in page-

Quoted 1 line

flags"

Quoted 10 lines
 arch/arm64/kernel/hibernate.c |  5 -----
 arch/arm64/kernel/mte.c       |  9 ---------
 arch/arm64/mm/copypage.c      |  9 ---------
 arch/arm64/mm/fault.c         |  1 -
 arch/arm64/mm/mteswap.c       |  9 ---------
 include/linux/gfp.h           |  2 +-
 mm/kasan/common.c             |  3 ++-
 mm/page_alloc.c               | 19 ++++++++++---------
 8 files changed, 13 insertions(+), 44 deletions(-)

Hi kasan maintainers,

We hit the following issue on the android-6.1 devices with MTE and HW
tag kasan enabled.

I observe that the anon flag doesn't have skip_kasan_poison and skip_kasan_unpoison flag and kasantag is weird.

AFAIK, kasantag of anon flag needs to be 0x0.

[   71.953938] [T1403598] FramePolicy:
[name:report&]=====================================================
====
=========
[   71.955305] [T1403598] FramePolicy: [name:report&]BUG: KASAN:
invalid-access in copy_page+0x10/0xd0
[   71.956476] [T1403598] FramePolicy: [name:report&]Read at addr
f0ffff81332a8000 by task FramePolicy/3598
[   71.957673] [T1403598] FramePolicy:
[name:report_hw_tags&]Pointer
tag: [f0], memory tag: [ff]
[   71.958746] [T1403598] FramePolicy: [name:report&]
[   71.959354] [T1403598] FramePolicy: CPU: 4 PID: 3598 Comm:
FramePolicy Tainted: G S      W  OE      6.1.0-mainline-android14-
0-
ga8a53f83b9e4 #1
[   71.960978] [T1403598] FramePolicy: Hardware name: MT6985(ENG)
(DT)
[   71.961767] [T1403598] FramePolicy: Call trace:
[   71.962338] [T1403598] FramePolicy:  dump_backtrace+0x108/0x158
[   71.963097] [T1403598] FramePolicy:  show_stack+0x20/0x48
[   71.963782] [T1403598] FramePolicy:  dump_stack_lvl+0x6c/0x88
[   71.964512] [T1403598] FramePolicy:  print_report+0x2cc/0xa64
[   71.965263] [T1403598] FramePolicy:  kasan_report+0xb8/0x138
[   71.965986] [T1403598]
FramePolicy:  __do_kernel_fault+0xd4/0x248
[   71.966782] [T1403598] FramePolicy:  do_bad_area+0x38/0xe8
[   71.967484] [T1403598]
FramePolicy:  do_tag_check_fault+0x24/0x38
[   71.968261] [T1403598] FramePolicy:  do_mem_abort+0x48/0xb0
[   71.968973] [T1403598] FramePolicy:  el1_abort+0x44/0x68
[   71.969646] [T1403598]
FramePolicy:  el1h_64_sync_handler+0x68/0xb8
[   71.970440] [T1403598] FramePolicy:  el1h_64_sync+0x68/0x6c
[   71.971146] [T1403598] FramePolicy:  copy_page+0x10/0xd0
[   71.971824] [T1403598]
FramePolicy:  copy_user_highpage+0x20/0x40
[   71.972603] [T1403598] FramePolicy:  wp_page_copy+0xd0/0x9f8
[   71.973344] [T1403598] FramePolicy:  do_wp_page+0x374/0x3b0
[   71.974056] [T1403598]
FramePolicy:  handle_mm_fault+0x3ec/0x119c
[   71.974833] [T1403598] FramePolicy:  do_page_fault+0x344/0x4ac
[   71.975583] [T1403598] FramePolicy:  do_mem_abort+0x48/0xb0
[   71.976294] [T1403598] FramePolicy:  el0_da+0x4c/0xe0
[   71.976934] [T1403598]
FramePolicy:  el0t_64_sync_handler+0xd4/0xfc
[   71.977725] [T1403598] FramePolicy:  el0t_64_sync+0x1a0/0x1a4
[   71.978451] [T1403598] FramePolicy: [name:report&]
[   71.979057] [T1403598] FramePolicy: [name:report&]The buggy
address
belongs to the physical page:
[   71.980173] [T1403598] FramePolicy:
[name:debug&]page:fffffffe04ccaa00 refcount:14 mapcount:13
mapping:0000000000000000 index:0x7884c74 pfn:0x1732a8
[   71.981849] [T1403598] FramePolicy:
[name:debug&]memcg:faffff80c0241000
[   71.982680] [T1403598] FramePolicy: [name:debug&]anon flags:
0x43c000000048003e(referenced|uptodate|dirty|lru|active|swapbacked|
arch
_2|zone=1|kasantag=0xf)
[   71.984446] [T1403598] FramePolicy: raw: 43c000000048003e
fffffffe04b99648 fffffffe04cca308 f2ffff8103390831
[   71.985684] [T1403598] FramePolicy: raw: 0000000007884c74
0000000000000000 0000000e0000000c faffff80c0241000
[   71.986919] [T1403598] FramePolicy: [name:debug&]page dumped
because: kasan: bad access detected
[   71.988022] [T1403598] FramePolicy: [name:report&]
[   71.988624] [T1403598] FramePolicy: [name:report&]Memory state
around the buggy address:
[   71.989641] [T1403598] FramePolicy:  ffffff81332a7e00: fe fe fe
fe
fe fe fe fe fe fe fe fe fe fe fe fe
[   71.990811] [T1403598] FramePolicy:  ffffff81332a7f00: fe fe fe
fe
fe fe fe fe fe fe fe fe fe fe fe fe
[   71.991982] [T1403598] FramePolicy: >ffffff81332a8000: ff ff ff
ff
f0 f0 fc fc fc fc fc fc fc f0 f0 f3
[   71.993149] [T1403598] FramePolicy:
[name:report&]                   ^
[   71.993972] [T1403598] FramePolicy:  ffffff81332a8100: f3 f3 f3
f3
f3 f3 f0 f0 f8 f8 f8 f8 f8 f8 f8 f0
[   71.995141] [T1403598] FramePolicy:  ffffff81332a8200: f0 fb fb
fb
fb fb fb fb f0 f0 fe fe fe fe fe fe
[   71.996332] [T1403598] FramePolicy:
Quoted 8 lines

[name:report&]=====================================================
====
=========

Originally, I suspect that some userspace pages have been migrated so
the page->flags will be lost and page->flags is re-generated by alloc_pages().

Quoted 8 lines

Hi Kuan-Ying,

There recently was a similar crash due to incorrectly implemented sampling.

Do you have the following patch in your tree?

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/9f7f5a25f335e6e1484695da9180281a728db7e2__;Kw!!CTRNKA9wMg0ARbw!hUjRlXirPMSusdIWe0RIPt0PNqIHYDCJyd7GSd4o-TgLMP0CKRUkjElH-jcvtaz42-sgE2U58964rCCbuNTJE5Jx$

Quoted 6 lines

If not, please sync your 6.1 tree with the Android common kernel. Hopefully this will fix the issue.

Thanks!

Hi Andrey,

Thanks for your advice.

I saw this patch is to fix ("kasan: allow sampling page_alloc allocations for HW_TAGS").

But our 6.1 tree doesn't have following two commits now. ("FROMGIT: kasan: allow sampling page_alloc allocations for HW_TAGS") (FROMLIST: kasan: reset page tags properly with sampling)

#20 lore raw
Quoted 19 lines · Kuan-Ying Lee (李冠穎)
Quoted 9 lines

Hi Kuan-Ying,

There recently was a similar crash due to incorrectly implemented sampling.

Do you have the following patch in your tree?

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/9f7f5a25f335e6e1484695da9180281a728db7e2__;Kw!!CTRNKA9wMg0ARbw!hUjRlXirPMSusdIWe0RIPt0PNqIHYDCJyd7GSd4o-TgLMP0CKRUkjElH-jcvtaz42-sgE2U58964rCCbuNTJE5Jx$

Quoted 6 lines

If not, please sync your 6.1 tree with the Android common kernel. Hopefully this will fix the issue.

Thanks!

Hi Andrey,

Quoted 8 lines

Thanks for your advice.

I saw this patch is to fix ("kasan: allow sampling page_alloc allocations for HW_TAGS").

But our 6.1 tree doesn't have following two commits now. ("FROMGIT: kasan: allow sampling page_alloc allocations for HW_TAGS") (FROMLIST: kasan: reset page tags properly with sampling)

Hi Kuan-Ying,

Just to clarify: these two patches were applied twice: once here on Jan 13:

https://android.googlesource.com/kernel/common/+/a2a9e34d164e90fc08d35fd097a164b9101d72ef
https://android.googlesource.com/kernel/common/+/435e2a6a6c8ba8d0eb55f9aaade53e7a3957322b

but then reverted here on Jan 20:

https://android.googlesource.com/kernel/common/+/5503dbe454478fe54b9cac3fc52d4477f52efdc9
https://android.googlesource.com/kernel/common/+/4573a3cf7e18735a477845426238d46d96426bb6

And then once again via the link I sent before together with a fix on Jan 25.

It might be that you still have to former two patches in your tree if you synced it before the revert.

However, if this is not the case:

Which 6.1 commit is your tree based on?
Do you have any private MTE-related changes in the kernel? Do you have userspace MTE enabled?

Thanks!

#21 lore raw
Quoted 5 lines · Andrey Konovalov
Quoted 3 lines · Kuan-Ying Lee (李冠穎)
Quoted 3 lines

Hi Kuan-Ying,

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

There recently was a similar crash due to incorrectly implemented sampling.

Do you have the following patch in your tree?

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/9f7f5a25f335e6e1484695da9180281a728db7e2__;Kw!!CTRNKA9wMg0ARbw!hUjRlXirPMSusdIWe0RIPt0PNqIHYDCJyd7GSd4o-TgLMP0CKRUkjElH-jcvtaz42-sgE2U58964rCCbuNTJE5Jx$

Quoted 14 lines
Quoted 14 lines
Quoted 6 lines

If not, please sync your 6.1 tree with the Android common kernel. Hopefully this will fix the issue.

Thanks!

Hi Andrey,

Thanks for your advice.

I saw this patch is to fix ("kasan: allow sampling page_alloc allocations for HW_TAGS").

Quoted 7 lines
Quoted 4 lines

But our 6.1 tree doesn't have following two commits now. ("FROMGIT: kasan: allow sampling page_alloc allocations for HW_TAGS")
(FROMLIST: kasan: reset page tags properly with sampling)

Hi Kuan-Ying,

Hi Andrey,
I'll stand in for Kuan-Ying as he's out of office. Thanks for your help!

Quoted 4 lines

Just to clarify: these two patches were applied twice: once here on Jan 13:

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/a2a9e34d164e90fc08d35fd097a164b9101d72ef__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745_3oO-3k$

Quoted 2 lines

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/435e2a6a6c8ba8d0eb55f9aaade53e7a3957322b__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745sDEOYWY$

Quoted 2 lines

Our codebase does not contain these two patches.

Quoted 3 lines

but then reverted here on Jan 20:

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/5503dbe454478fe54b9cac3fc52d4477f52efdc9__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745Bl77dFY$

Quoted 2 lines

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/4573a3cf7e18735a477845426238d46d96426bb6__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745K-J8O-w$

Quoted 11 lines

And then once again via the link I sent before together with a fix on Jan 25.

It might be that you still have to former two patches in your tree if you synced it before the revert.

However, if this is not the case:

Which 6.1 commit is your tree based on?

https://android.googlesource.com/kernel/common/+/53b3a7721b7aec74d8fa2ee55c2480044cc7c1b8
(53b3a77 Merge 6.1.1 into android14-6.1) is the latest commit in our tree.

Quoted 1 line

Do you have any private MTE-related changes in the kernel?

No, all the MTE-related code is the same as Android Common Kernel.

Quoted 1 line

Do you have userspace MTE enabled?

Yes, we have enabled MTE for both EL1 and EL0.

Quoted 2 lines

Thanks!

#22 lore raw
Quoted 78 lines · Qun-wei Lin (林群崴)
Quoted 5 lines · Andrey Konovalov
Quoted 3 lines · Kuan-Ying Lee (李冠穎)
Quoted 3 lines

Hi Kuan-Ying,

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

There recently was a similar crash due to incorrectly implemented sampling.

Do you have the following patch in your tree?

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/9f7f5a25f335e6e1484695da9180281a728db7e2__;Kw!!CTRNKA9wMg0ARbw!hUjRlXirPMSusdIWe0RIPt0PNqIHYDCJyd7GSd4o-TgLMP0CKRUkjElH-jcvtaz42-sgE2U58964rCCbuNTJE5Jx$

Quoted 14 lines
Quoted 14 lines
Quoted 6 lines

If not, please sync your 6.1 tree with the Android common kernel. Hopefully this will fix the issue.

Thanks!

Hi Andrey,

Thanks for your advice.

I saw this patch is to fix ("kasan: allow sampling page_alloc allocations for HW_TAGS").

Quoted 7 lines
Quoted 4 lines

But our 6.1 tree doesn't have following two commits now. ("FROMGIT: kasan: allow sampling page_alloc allocations for HW_TAGS")
(FROMLIST: kasan: reset page tags properly with sampling)

Hi Kuan-Ying,

Hi Andrey,
I'll stand in for Kuan-Ying as he's out of office. Thanks for your help!

Quoted 4 lines

Just to clarify: these two patches were applied twice: once here on Jan 13:

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/a2a9e34d164e90fc08d35fd097a164b9101d72ef__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745_3oO-3k$

Quoted 2 lines

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/435e2a6a6c8ba8d0eb55f9aaade53e7a3957322b__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745sDEOYWY$

Quoted 2 lines

Our codebase does not contain these two patches.

Quoted 3 lines

but then reverted here on Jan 20:

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/5503dbe454478fe54b9cac3fc52d4477f52efdc9__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745Bl77dFY$

Quoted 2 lines

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/4573a3cf7e18735a477845426238d46d96426bb6__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745K-J8O-w$

Quoted 11 lines

And then once again via the link I sent before together with a fix on Jan 25.

It might be that you still have to former two patches in your tree if you synced it before the revert.

However, if this is not the case:

Which 6.1 commit is your tree based on?

https://android.googlesource.com/kernel/common/+/53b3a7721b7aec74d8fa2ee55c2480044cc7c1b8
(53b3a77 Merge 6.1.1 into android14-6.1) is the latest commit in our tree.

Quoted 7 lines
Quoted 1 line

Do you have any private MTE-related changes in the kernel?

No, all the MTE-related code is the same as Android Common Kernel.

Quoted 1 line

Do you have userspace MTE enabled?

Yes, we have enabled MTE for both EL1 and EL0.

Hi Qun-wei,

Thanks for the information. We encountered a similar issue internally with the Android 5.15 common kernel. We tracked it down to an issue with page migration, where the source page was a userspace page with MTE tags, and the target page was allocated using KASAN (i.e. having a non-zero KASAN tag). This caused tag check faults when the page was subsequently accessed by the kernel as a result of the mismatching tags from userspace. Given the number of different ways that page migration target pages can be allocated, the simplest fix that we could think of was to synchronize the KASAN tag in copy_highpage().

Can you try the patch below and let us know whether it fixes the issue?

arch/arm64/mm/copypage.c +2 -0
index 24913271e898c..87ed38e9747bd 100644
@@ -23,6 +23,8 @@ void copy_highpage(struct page *to, struct page *from)
 
 	if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
 		set_bit(PG_mte_tagged, &to->flags);
+		if (kasan_hw_tags_enabled())
+			page_kasan_tag_set(to, page_kasan_tag(from));
 		mte_copy_page_tags(kto, kfrom);
 	}
 }

Catalin, please let us know what you think of the patch above. It effectively partially undoes commit 20794545c146 ("arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags""), but this seems okay to me because the mentioned race condition shouldn't affect "new" pages such as those being used as migration targets. The smp_wmb() that was there before doesn't seem necessary for the same reason.

If the patch is okay, we should apply it to the 6.1 stable kernel. The problem appears to be "fixed" in the mainline kernel because of a bad merge conflict resolution on my part; when I rebased commit e059853d14ca ("arm64: mte: Fix/clarify the PG_mte_tagged semantics") past commit 20794545c146, it looks like I accidentally brought back the page_kasan_tag_reset() line removed in the latter. But we should align the mainline kernel with whatever we decide to do on 6.1.

Peter

author maintainer
#23 lore raw

Hi Peter,

Quoted 17 lines · Peter Collingbourne

Thanks for the information. We encountered a similar issue internally with the Android 5.15 common kernel. We tracked it down to an issue with page migration, where the source page was a userspace page with MTE tags, and the target page was allocated using KASAN (i.e. having a non-zero KASAN tag). This caused tag check faults when the page was subsequently accessed by the kernel as a result of the mismatching tags from userspace. Given the number of different ways that page migration target pages can be allocated, the simplest fix that we could think of was to synchronize the KASAN tag in copy_highpage().

Can you try the patch below and let us know whether it fixes the issue?

arch/arm64/mm/copypage.c +0 -0
index 24913271e898c..87ed38e9747bd 100644

arch/arm64/mm/copypage.c void copy_highpage(struct page *to, struct page *from) · line 28

Quoted 6 lines
arch/arm64/mm/copypage.c +2 -0
@@ -23,6 +23,8 @@ void copy_highpage(struct page *to, struct page *from)
 
 	if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
 		set_bit(PG_mte_tagged, &to->flags);
+		if (kasan_hw_tags_enabled())
+			page_kasan_tag_set(to, page_kasan_tag(from));
 		mte_copy_page_tags(kto, kfrom);

Why not just page_kasan_tag_reset(to)? If PG_mte_tagged is set on the 'from' page, the tags are random anyway and page_kasan_tag(from) should already be 0xff. It makes more sense to do the same for the 'to' page rather than copying the tag from the 'from' page. IOW, we are copying user-controlled tags into a page, the kernel should have a match-all tag in page->flags.

Quoted 7 lines

Catalin, please let us know what you think of the patch above. It effectively partially undoes commit 20794545c146 ("arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags""), but this seems okay to me because the mentioned race condition shouldn't affect "new" pages such as those being used as migration targets. The smp_wmb() that was there before doesn't seem necessary for the same reason.

Quoted 7 lines

If the patch is okay, we should apply it to the 6.1 stable kernel. The problem appears to be "fixed" in the mainline kernel because of a bad merge conflict resolution on my part; when I rebased commit e059853d14ca ("arm64: mte: Fix/clarify the PG_mte_tagged semantics") past commit 20794545c146, it looks like I accidentally brought back the page_kasan_tag_reset() line removed in the latter. But we should align the mainline kernel with whatever we decide to do on 6.1.

Happy accident ;). When I reverted such calls in commit 20794545c146, my assumption was that we always get a page that went through post_alloc_hook() and the tags were reset. But it seems that's not always the case (and probably wasteful anyway if we have to zero the tags and data on a page we know we are going to override via copy_highpage() anyway). The barrier doesn't help, so we shouldn't add it back.

So, I'm fine with a stable fix but I wonder whether we should backport the whole "Fix/clarify the PG_mte_tagged semantics" series instead.

Signature · 1 line
Catalin
#24 lore raw

Hi Catalin,

Quoted 22 lines · Catalin Marinas

Hi Peter,

Quoted 18 lines · Peter Collingbourne

Thanks for the information. We encountered a similar issue internally with the Android 5.15 common kernel. We tracked it down to an issue with page migration, where the source page was a userspace page with MTE tags, and the target page was allocated using KASAN (i.e. having a non-zero KASAN tag). This caused tag check faults when the page was subsequently accessed by the kernel as a result of the mismatching tags from userspace. Given the number of different ways that page migration target pages can be allocated, the simplest fix that we could think of was to synchronize the KASAN tag in copy_highpage().

Can you try the patch below and let us know whether it fixes the issue?

arch/arm64/mm/copypage.c +0 -0
index 24913271e898c..87ed38e9747bd 100644
@@ -23,6 +23,8 @@ void copy_highpage(struct page *to, struct page *from)
 
Quoted 12 lines

arch/arm64/mm/copypage.c void copy_highpage(struct page *to, struct page *from) · line 28

Quoted 5 lines
arch/arm64/mm/copypage.c +2 -0
      if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
              set_bit(PG_mte_tagged, &to->flags);
+             if (kasan_hw_tags_enabled())
+                     page_kasan_tag_set(to, page_kasan_tag(from));
              mte_copy_page_tags(kto, kfrom);

Why not just page_kasan_tag_reset(to)? If PG_mte_tagged is set on the 'from' page, the tags are random anyway and page_kasan_tag(from) should already be 0xff. It makes more sense to do the same for the 'to' page rather than copying the tag from the 'from' page. IOW, we are copying user-controlled tags into a page, the kernel should have a match-all tag in page->flags.

That would also work, but I was thinking that if copy_highpage() were being used to copy a KASAN page we should keep the original tag in order to maintain tag checks for page accesses.

Quoted 23 lines
Quoted 7 lines

Catalin, please let us know what you think of the patch above. It effectively partially undoes commit 20794545c146 ("arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags""), but this seems okay to me because the mentioned race condition shouldn't affect "new" pages such as those being used as migration targets. The smp_wmb() that was there before doesn't seem necessary for the same reason.

Quoted 7 lines

If the patch is okay, we should apply it to the 6.1 stable kernel. The problem appears to be "fixed" in the mainline kernel because of a bad merge conflict resolution on my part; when I rebased commit e059853d14ca ("arm64: mte: Fix/clarify the PG_mte_tagged semantics") past commit 20794545c146, it looks like I accidentally brought back the page_kasan_tag_reset() line removed in the latter. But we should align the mainline kernel with whatever we decide to do on 6.1.

Happy accident ;). When I reverted such calls in commit 20794545c146, my assumption was that we always get a page that went through post_alloc_hook() and the tags were reset. But it seems that's not always the case (and probably wasteful anyway if we have to zero the tags and data on a page we know we are going to override via copy_highpage() anyway). The barrier doesn't help, so we shouldn't add it back.

Quoted 2 lines

So, I'm fine with a stable fix but I wonder whether we should backport the whole "Fix/clarify the PG_mte_tagged semantics" series instead.

That seems fine to me (or as well as the above patch if we decide to copy the tag).

Peter

author maintainer
#25 lore raw
Quoted 34 lines · Peter Collingbourne
Quoted 19 lines · Catalin Marinas
Quoted 18 lines · Peter Collingbourne

Thanks for the information. We encountered a similar issue internally with the Android 5.15 common kernel. We tracked it down to an issue with page migration, where the source page was a userspace page with MTE tags, and the target page was allocated using KASAN (i.e. having a non-zero KASAN tag). This caused tag check faults when the page was subsequently accessed by the kernel as a result of the mismatching tags from userspace. Given the number of different ways that page migration target pages can be allocated, the simplest fix that we could think of was to synchronize the KASAN tag in copy_highpage().

Can you try the patch below and let us know whether it fixes the issue?

arch/arm64/mm/copypage.c +0 -0
index 24913271e898c..87ed38e9747bd 100644
@@ -23,6 +23,8 @@ void copy_highpage(struct page *to, struct page *from)
 
Quoted 12 lines

arch/arm64/mm/copypage.c void copy_highpage(struct page *to, struct page *from) · line 28

Quoted 5 lines
arch/arm64/mm/copypage.c +2 -0
      if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
              set_bit(PG_mte_tagged, &to->flags);
+             if (kasan_hw_tags_enabled())
+                     page_kasan_tag_set(to, page_kasan_tag(from));
              mte_copy_page_tags(kto, kfrom);

Why not just page_kasan_tag_reset(to)? If PG_mte_tagged is set on the 'from' page, the tags are random anyway and page_kasan_tag(from) should already be 0xff. It makes more sense to do the same for the 'to' page rather than copying the tag from the 'from' page. IOW, we are copying user-controlled tags into a page, the kernel should have a match-all tag in page->flags.

Quoted 3 lines

That would also work, but I was thinking that if copy_highpage() were being used to copy a KASAN page we should keep the original tag in order to maintain tag checks for page accesses.

If PG_mte_tagged is set on the source, it means that the tags are no longer trusted and we should reset to match-all. Otherwise if copy_highpage() is called on a page that was never mapped as PROT_MTE in user space, PG_mte_tagged would not be set on the source and no tags copied. In such case, we should keep the original KASAN tag in the destination. Unless I misunderstood what you meant.

Signature · 1 line
Catalin
#26 lore raw
Quoted 40 lines · Catalin Marinas
Quoted 34 lines · Peter Collingbourne
Quoted 19 lines · Catalin Marinas
Quoted 18 lines · Peter Collingbourne

Thanks for the information. We encountered a similar issue internally with the Android 5.15 common kernel. We tracked it down to an issue with page migration, where the source page was a userspace page with MTE tags, and the target page was allocated using KASAN (i.e. having a non-zero KASAN tag). This caused tag check faults when the page was subsequently accessed by the kernel as a result of the mismatching tags from userspace. Given the number of different ways that page migration target pages can be allocated, the simplest fix that we could think of was to synchronize the KASAN tag in copy_highpage().

Can you try the patch below and let us know whether it fixes the issue?

arch/arm64/mm/copypage.c +0 -0
index 24913271e898c..87ed38e9747bd 100644
@@ -23,6 +23,8 @@ void copy_highpage(struct page *to, struct page *from)
 
Quoted 12 lines

arch/arm64/mm/copypage.c void copy_highpage(struct page *to, struct page *from) · line 28

Quoted 5 lines
arch/arm64/mm/copypage.c +2 -0
      if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
              set_bit(PG_mte_tagged, &to->flags);
+             if (kasan_hw_tags_enabled())
+                     page_kasan_tag_set(to, page_kasan_tag(from));
              mte_copy_page_tags(kto, kfrom);

Why not just page_kasan_tag_reset(to)? If PG_mte_tagged is set on the 'from' page, the tags are random anyway and page_kasan_tag(from) should already be 0xff. It makes more sense to do the same for the 'to' page rather than copying the tag from the 'from' page. IOW, we are copying user-controlled tags into a page, the kernel should have a match-all tag in page->flags.

Quoted 3 lines

That would also work, but I was thinking that if copy_highpage() were being used to copy a KASAN page we should keep the original tag in order to maintain tag checks for page accesses.

Quoted 6 lines

If PG_mte_tagged is set on the source, it means that the tags are no longer trusted and we should reset to match-all. Otherwise if copy_highpage() is called on a page that was never mapped as PROT_MTE in user space, PG_mte_tagged would not be set on the source and no tags copied. In such case, we should keep the original KASAN tag in the destination. Unless I misunderstood what you meant.

I was thinking that it might be possible for PG_mte_tagged to be set on a page with KASAN tags. But as far as I can tell, this isn't actually possible (or not intended to be possible, at least). So I agree, we can just call page_kasan_tag_reset() here.

Patch sent (with stable backport instructions) here:
https://lore.kernel.org/all/[email protected]/

Peter

#27 lore raw
In reply to Peter Collingbourne
Quoted 11 lines · Peter Collingbourne
Quoted 10 lines · Qun-wei Lin (林群崴)
Quoted 9 lines · Andrey Konovalov
Quoted 7 lines · Kuan-Ying Lee (李冠穎)
Quoted 7 lines

Hi Kuan-Ying,

There recently was a similar crash due to incorrectly implemented
sampling.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

Do you have the following patch in your tree?

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/9f7f5a25f335e6e1484695da9180281a728db7e2__;Kw!!CTRNKA9wMg0ARbw!hUjRlXirPMSusdIWe0RIPt0PNqIHYDCJyd7GSd4o-TgLMP0CKRUkjElH-jcvtaz42-sgE2U58964rCCbuNTJE5Jx$

Quoted 27 lines
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

If not, please sync your 6.1 tree with the Android common kernel.
Hopefully this will fix the issue.

Thanks!

Hi Andrey,

Thanks for your advice.

I saw this patch is to fix ("kasan: allow sampling page_alloc allocations for HW_TAGS").

Quoted 7 lines
Quoted 4 lines

But our 6.1 tree doesn't have following two commits now. ("FROMGIT: kasan: allow sampling page_alloc allocations for HW_TAGS")
(FROMLIST: kasan: reset page tags properly with sampling)

Hi Kuan-Ying,

Hi Andrey,
I'll stand in for Kuan-Ying as he's out of office. Thanks for your help!

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Just to clarify: these two patches were applied twice: once here on
Jan 13:

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/a2a9e34d164e90fc08d35fd097a164b9101d72ef__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745_3oO-3k$

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/435e2a6a6c8ba8d0eb55f9aaade53e7a3957322b__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745sDEOYWY$

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines

Our codebase does not contain these two patches.

Quoted 3 lines

but then reverted here on Jan 20:

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/5503dbe454478fe54b9cac3fc52d4477f52efdc9__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745Bl77dFY$

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/4573a3cf7e18735a477845426238d46d96426bb6__;Kw!!CTRNKA9wMg0ARbw!kE1XiSmunRcQb9rTpKGkFc1EFJA57qr1cj7v9EZAjUBzXcSzMl-ofCI2mdtEQsxn3J4n7Lkgxb0_G745K-J8O-w$

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

And then once again via the link I sent before together with a fix on
Jan 25.

It might be that you still have to former two patches in your tree if
you synced it before the revert.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

However, if this is not the case:

Which 6.1 commit is your tree based on?

https://urldefense.com/v3/__https://android.googlesource.com/kernel/common/*/53b3a7721b7aec74d8fa2ee55c2480044cc7c1b8__;Kw!!CTRNKA9wMg0ARbw!iEzuh9LYXlwXkpcWaHjncfr6lNgTky7OEAEzQ7cIFjlTD__7lwXqAhPJwWJXEnD8THUS7jnBK7hjnHw$

Quoted 44 lines
Quoted 5 lines

(53b3a77 Merge 6.1.1 into android14-6.1) is the latest commit in our
tree.

Quoted 7 lines
Quoted 1 line

Do you have any private MTE-related changes in the kernel?

No, all the MTE-related code is the same as Android Common Kernel.

Quoted 1 line

Do you have userspace MTE enabled?

Yes, we have enabled MTE for both EL1 and EL0.

Hi Qun-wei,

Thanks for the information. We encountered a similar issue internally with the Android 5.15 common kernel. We tracked it down to an issue with page migration, where the source page was a userspace page with MTE tags, and the target page was allocated using KASAN (i.e. having a non-zero KASAN tag). This caused tag check faults when the page was subsequently accessed by the kernel as a result of the mismatching tags
from userspace. Given the number of different ways that page migration
target pages can be allocated, the simplest fix that we could think of
was to synchronize the KASAN tag in copy_highpage().

Can you try the patch below and let us know whether it fixes the issue?

arch/arm64/mm/copypage.c +2 -0
index 24913271e898c..87ed38e9747bd 100644
@@ -23,6 +23,8 @@ void copy_highpage(struct page *to, struct page
 *from)
 
 	if (system_supports_mte() && test_bit(PG_mte_tagged, &from-
 >flags)) {
 		set_bit(PG_mte_tagged, &to->flags);
+		if (kasan_hw_tags_enabled())
+			page_kasan_tag_set(to, page_kasan_tag(from));
 		mte_copy_page_tags(kto, kfrom);
Quoted 3 lines

}
}

Thank you so much, this patch has solved the problem.

Quoted 22 lines

Catalin, please let us know what you think of the patch above. It effectively partially undoes commit 20794545c146 ("arm64: kasan:
Revert
"arm64: mte: reset the page tag in page->flags""), but this seems okay
to me because the mentioned race condition shouldn't affect "new" pages
such as those being used as migration targets. The smp_wmb() that was there before doesn't seem necessary for the same reason.

If the patch is okay, we should apply it to the 6.1 stable kernel. The
problem appears to be "fixed" in the mainline kernel because of a bad merge conflict resolution on my part; when I rebased commit e059853d14ca ("arm64: mte: Fix/clarify the PG_mte_tagged semantics") past commit 20794545c146, it looks like I accidentally brought back the
page_kasan_tag_reset() line removed in the latter. But we should align
the mainline kernel with whatever we decide to do on 6.1.

Peter