Re: [PATCH v2 2/2] mm/kasan/kunit: add page_alloc_skip_kasan test

From: Ryan Roberts

Date: Mon Nov 23 2026 - 03:58:42 EST


On 23/11/2026 03:31, Jeff Xu wrote:
> Add a KUnit test (page_alloc_skip_kasan) to verify that page allocations
> using __GFP_SKIP_KASAN (GFP_KERNEL | __GFP_SKIP_KASAN, GFP_HIGHUSER, and
> GFP_HIGHUSER_MOVABLE) under CONFIG_KASAN_HW_TAGS:
>
> 1. Reset page_kasan_tag() to KASAN_TAG_KERNEL (0xff) across all sub-pages
> of a high-order allocation, and
> 2. Return a match-all (0xff) linear-map pointer from page_address() that
> does not trigger a KASAN fault even when the underlying physical
> granules carry a mismatched tag (such as KASAN_TAG_INVALID or
> restored userspace PROT_MTE tags).
>
> Tested on arm64 with CONFIG_KASAN_HW_TAGS under QEMU (virt,mte=on) with
> kasan.mode=sync, async and asymm.
>
> Signed-off-by: Jeff Xu <jeffxu@xxxxxxxxxxxx>

Reviewed-by: Ryan Roberts <ryan.roberts@xxxxxxx>

> ---
> mm/kasan/kasan_test_c.c | 44 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 44 insertions(+)
>
> diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c
> index f34163e19bd6..72342137bb18 100644
> --- a/mm/kasan/kasan_test_c.c
> +++ b/mm/kasan/kasan_test_c.c
> @@ -380,6 +380,49 @@ static void page_alloc_uaf(struct kunit *test)
> KUNIT_EXPECT_KASAN_FAIL_READ(test, ((volatile char *)ptr)[0]);
> }
>
> +static void page_alloc_skip_kasan_helper(struct kunit *test, gfp_t gfp)
> +{
> + struct page *pages;
> + size_t order = 2;
> + char *ptr;
> + size_t i;
> +
> + pages = alloc_pages(gfp, order);
> + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pages);
> +
> + for (i = 0; i < (1UL << order); i++) {
> + /*
> + * __GFP_SKIP_KASAN allocations must carry the match-all tag
> + * in page->flags, and so in page_address(), not a random tag.
> + */
> + KUNIT_EXPECT_EQ(test, page_kasan_tag(pages + i),
> + (u8)KASAN_TAG_KERNEL);
> +
> + ptr = page_address(pages + i);
> + KUNIT_EXPECT_EQ(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
> +
> + /*
> + * The memory tags of such pages are arbitrary: stale poison,
> + * or tags set by userspace through PROT_MTE. Accesses via
> + * page_address() must not fault regardless.
> + */
> + kasan_poison(ptr, PAGE_SIZE, KASAN_TAG_INVALID, false);
> + KUNIT_EXPECT_KASAN_RESULT(test, WRITE_ONCE(*ptr, 0),
> + "WRITE_ONCE(*ptr, 0)", false);
> + }
> +
> + __free_pages(pages, order);
> +}
> +
> +static void page_alloc_skip_kasan(struct kunit *test)
> +{
> + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_HW_TAGS);
> +
> + page_alloc_skip_kasan_helper(test, GFP_KERNEL | __GFP_SKIP_KASAN);
> + page_alloc_skip_kasan_helper(test, GFP_HIGHUSER);
> + page_alloc_skip_kasan_helper(test, GFP_HIGHUSER_MOVABLE);
> +}
> +
> static void krealloc_more_oob_helper(struct kunit *test,
> size_t size1, size_t size2)
> {
> @@ -2369,6 +2412,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {
> KUNIT_CASE(kmalloc_large_invalid_free),
> KUNIT_CASE(page_alloc_oob_right),
> KUNIT_CASE(page_alloc_uaf),
> + KUNIT_CASE(page_alloc_skip_kasan),
> KUNIT_CASE(krealloc_more_oob),
> KUNIT_CASE(krealloc_less_oob),
> KUNIT_CASE(krealloc_large_more_oob),