All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chris Down" <chris@chrisdown.name>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Harry Yoo" <harry@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Hao Li" <hao.li@linux.dev>, "Christoph Lameter" <cl@gentwo.org>,
	"David Rientjes" <rientjes@google.com>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Kees Cook" <kees@kernel.org>, "Marco Elver" <elver@google.com>,
	"GONG Ruiqi" <gongruiqi@huaweicloud.com>,
	"Jan Kiszka" <jan.kiszka@siemens.com>,
	"Kieran Bingham" <kbingham@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	linux-mm@kvack.org, kernel-team@meta.com
Subject: Re: [PATCH 4/8] slub_kunit: Add a test for slab_freelist_random=
Date: Wed, 28 Oct 2026 07:36:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-187584@kernel.org> (raw)
In-Reply-To: <0af4fc45ca8e1d7027ceaad079f8f9eaac663c76.1793086056.git.chris@chrisdown.name>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] slub_kunit: missing kasan_reset_tag() in test_freelist_random()
--

commit a6a4a9e9d2827c11abe21a80276d4c98393a0acc
Author: Chris Down <chris@chrisdown.name>

slub_kunit: Add a test for slab_freelist_random=

This commit exposes slab_freelist_random to KUnit and adds a test case in
slub_kunit to verify whether slab objects are allocated in linear address
order or randomized order based on slab_freelist_random.

> diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c
> index 5ace733685231..49b9942bc564e 100644
> --- a/lib/tests/slub_kunit.c
> +++ b/lib/tests/slub_kunit.c
> @@ -190,6 +190,56 @@ static void test_clobber_redzone_free(struct kunit *test)
>  	kmem_cache_destroy(s);
>  }
>  
> +static void test_freelist_random(struct kunit *test)
> +{
> +	struct kmem_cache *s = test_kmem_cache_create("TestSlub_freelist_random",
> +					64, SLAB_CONSISTENCY_CHECKS);
> +	bool random = false;
> +	unsigned int i, nr, nr_linear = 0;
> +	void **objs, *first;
> +
> +#ifdef CONFIG_SLAB_FREELIST_RANDOM
> +	random = slab_freelist_random;
> +	KUNIT_EXPECT_EQ(test, !!s->random_seq, random);
> +#endif
> +	kunit_info(test, "freelist randomisation is %s\n", str_on_off(random));
> +
> +	/*
> +	 * Caches with debug flags have no sheaves, so objects come straight
> +	 * off the slab freelist.
> +	 */
> +	first = kmem_cache_alloc(s, GFP_KERNEL);
> +	KUNIT_ASSERT_NOT_NULL(test, first);
> +	nr = virt_to_slab(first)->objects;
> +	objs = kunit_kcalloc(test, nr, sizeof(*objs), GFP_KERNEL);
> +	KUNIT_ASSERT_NOT_NULL(test, objs);
> +	objs[0] = first;
> +
> +	for (i = 1; i < nr; i++) {
> +		objs[i] = kmem_cache_alloc(s, GFP_KERNEL);
> +		KUNIT_ASSERT_NOT_NULL(test, objs[i]);
> +	}
> +
> +	for (i = 1; i < nr; i++) {
> +		if (virt_to_slab(objs[i]) != virt_to_slab(objs[0]))
> +			break;
> +		if (objs[i] == objs[i - 1] + s->size)
> +			nr_linear++;
> +	}
> +
> +	if (i == nr) {
> +		/* Without randomisation, objects are handed out in address order. */
> +		KUNIT_EXPECT_EQ(test, nr_linear == nr - 1, !random);
> +	} else {

[Severity: Medium]
Can this pointer comparison fail under tag-based KASAN modes
(CONFIG_KASAN_SW_TAGS or CONFIG_KASAN_HW_TAGS) when freelist randomization is
disabled?

Because test_freelist_random is registered in test_cases[] outside the
#ifndef CONFIG_KASAN block, it runs under tag-based KASAN configurations. The
cache s is created without a constructor or SLAB_TYPESAFE_BY_RCU, so each
kmem_cache_alloc() call passes through:

  slab_post_alloc_hook()
    -> kasan_slab_alloc()
      -> __kasan_slab_alloc()
        -> assign_tag()
        -> set_tag()

which embeds a random tag in the top byte of each returned pointer in objs[].

While virt_to_slab() strips the tag via virt_to_page() so the loop reaches
i == nr, the direct comparison:

  if (objs[i] == objs[i - 1] + s->size)

compares the tagged pointers without calling kasan_reset_tag(). When
consecutive allocations receive different tags, nr_linear will not reach
nr - 1 even though the untagged object addresses are contiguous, causing
KUNIT_EXPECT_EQ(test, nr_linear == nr - 1, !random) to fail when !random.

Should objs[i] and objs[i - 1] be untagged with kasan_reset_tag() before
comparing their addresses?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1793086056.git.chris@chrisdown.name?part=4


  reply	other threads:[~2026-10-28  7:36 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27  7:35 [PATCH 0/8] mm/slab: Support selecting slab hardening features at boot Chris Down
2026-10-27  7:35 ` [PATCH 1/8] mm/slub: Support disabling freelist hardening " Chris Down
2026-10-27  7:35 ` [PATCH 2/8] slub_kunit: Add a test for slab_freelist_hardened= Chris Down
2026-10-27  7:35 ` [PATCH 3/8] mm/slub: Support disabling freelist randomisation at boot Chris Down
2026-10-27  7:35 ` [PATCH 4/8] slub_kunit: Add a test for slab_freelist_random= Chris Down
2026-10-28  7:36   ` sashiko-bot [this message]
2026-10-27  7:35 ` [PATCH 5/8] mm/slab: Support disabling SLAB_BUCKETS at boot Chris Down
2026-10-27  7:36 ` [PATCH 6/8] slub_kunit: Add a test for the slab_buckets boot parameter Chris Down
2026-10-28  7:36   ` sashiko-bot
2026-10-27  7:36 ` [PATCH 7/8] mm/slab: Support disabling KMALLOC_PARTITION_CACHES at boot Chris Down
2026-10-27  7:36 ` [PATCH 8/8] slub_kunit: Add a test for the kmalloc_partition boot parameter Chris Down
2026-10-27  8:10 ` [PATCH 0/8] mm/slab: Support selecting slab hardening features at boot Vlastimil Babka (SUSE)
2026-10-27 18:31   ` Harry Yoo
2026-10-28 15:23   ` Chris Down
2026-10-28 15:06 ` Harry Yoo
2026-10-28 15:27   ` Chris Down
2026-10-28 15:37     ` Harry Yoo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-187584@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=chris@chrisdown.name \
    --cc=cl@gentwo.org \
    --cc=corbet@lwn.net \
    --cc=elver@google.com \
    --cc=gongruiqi@huaweicloud.com \
    --cc=hao.li@linux.dev \
    --cc=harry@kernel.org \
    --cc=jan.kiszka@siemens.com \
    --cc=kbingham@kernel.org \
    --cc=kees@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.