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

From: Jeff Xu

Date: Tue Nov 24 2026 - 14:55:27 EST


On Tue, Nov 24, 2026 at 09:17:45AM +0100, David Hildenbrand (Arm) wrote:
> On 11/23/26 04:31, Jeff Xu wrote:
> > +static void page_alloc_skip_kasan_helper(struct kunit *test, gfp_t gfp)
> > +{
> > + struct page *pages;
> > + size_t order = 2;
>
> Why magic value two?
>
> Should be const.

Just a small non-zero order so the loop checks tag resets on sub-pages
as well as the head page. I'll change it to "const unsigned int order = 2;"
and add a brief comment explaining that in v3.

> > + 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++) {
>
> "1 << order" is sufficient.

Will change to "1 << order" in v3.

> > + /*
> > + * __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);
>
> Squeeze this into a single line.

Will do in v3.

> > +
> > + 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);
>
> Why do we care about checking HW behavior? (match-all working)

Good point, match_all_ptr_tag() already tests match-all behavior in
hardware. I'll drop the kasan_poison() and KUNIT_EXPECT_KASAN_RESULT()
checks in v3. That also lets me drop the local ptr variable.

> > @@ -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),
>
> Don't we have to limit this case to having actual MTE-capable HW etc?

No extra gating is needed here: on non-MTE hardware (or with kasan=off),
kasan_init_hw_tags() returns early without calling kasan_enable(), so
kasan_suite_init() sees !kasan_enabled() and aborts the suite before any
test case runs. For Generic and SW_TAGS builds,
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_HW_TAGS) skips the test,
matching the rest of kasan_kunit_test_cases[].

I confirmed this by booting the HW_TAGS kernel under QEMU with mte=off
and with kasan=off: in both cases the suite fails to initialize and no
test case runs.

I'll add a short comment in page_alloc_skip_kasan() noting that in v3.

Thanks,
Jeff