From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794559915; b=zju3FYIjpkZ6BK/Oyv3smANrCBwr8Eah0D3CFWCJJg7SPGseAvErBVWzOfOUrx5YyC/L KQPwQs2+vmxV25M+1YSTVZYKk8w5h/8sx41X3lpeh0IYPGQU2bD1f6D+loaYUKzXjsIaV ppVaSdywczqdtPW7tjrXN4Gh0UZ8ymvN8AvoWya1kSWo1LyrbEELfr5G3qHm+bzLQl25c ysCjiJ7m7yw6a+QexiOmCrDllmrH1FkEwayykLEknODst2jHcjJvljGWcADmSz03F5+vJ f7SdOK1A9lK8G0QFVj0KHUj7X89C+4zkmyy5qfjF8acNmiJvI0iqtzdeue6JRG+Y9Dg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794559915; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:Message-ID:In-Reply-To:References:From:Subject:To:Cc: MIME-Version:Content-Type:Content-Disposition:User-Agent; bh=+SHVtZshJgR7EfVrq7tFY+459hSuxkLyCsM2otXIkPk=; b=U1eQXCz5cx4zZov/moI7nkHUDAlLu46BlvEyZoe5ugqj2we6nh3ncXZXeLARjaM7fRzQ C7fgbTpoFkVwgIOpMlIAP5LEmk2ShHSLEQ3LQiKwC9vkx/EBAWWaZ50boDKIXS7IDETfF VmAgG5eZtnr675eD49d09ppsSq3eduHQu32vOYKPXf/UjDYjXXc2GfukR1I8YlWXf7qnB uwrdzF9lQ9bPX99JoC9oauZFiq9MD8uMfRgn09/6d+jX1MrAMYtcOGOMnzGUt8jo6ikpC 7pe+eGly3WATxtlR6pnNg8zloTw4d4de98yjcgwJV99RGGJqVp1Gx1R8R/9HxPduw/Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass header.d=chrisdown.name header.i=@chrisdown.name header.a=rsa-sha256 header.s=google header.b=UZ6Lxa1P; arc=none smtp.remote-ip=209.85.216.48 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1044606A333 for ; Fri, 13 Nov 2026 08:51:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E1044606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.a=rsa-sha256 header.s=google header.b=UZ6Lxa1P Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-3b0b3438540so754054a91.0 for ; Fri, 13 Nov 2026 00:51:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1794559914; x=1795164714; darn=vger.kernel.org; h=user-agent:content-disposition:content-type:mime-version:cc:to :subject:from:references:in-reply-to:message-id:date:from:to:cc :subject:date:message-id:reply-to:content-type; bh=+SHVtZshJgR7EfVrq7tFY+459hSuxkLyCsM2otXIkPk=; b=UZ6Lxa1PUPFlQVfHsq8c4m+2pmWrQpf5qKptnm6fWXQtGzJ1GxA+h4RU1RifcD8L+F 3o0oYyP6cZbGmcMPFFUXPBM191qbaSnV2nR4Hd8iU0y/zl367LGLUboXN1ozRQM7gDCn ONv0EgoF0C36SeCQU2kBjmyW3a8AwTaLcjI9s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794559914; x=1795164714; h=user-agent:content-disposition:content-type:mime-version:cc:to :subject:from:references:in-reply-to:message-id:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=+SHVtZshJgR7EfVrq7tFY+459hSuxkLyCsM2otXIkPk=; b=V3joJiWnpY8ijpt8jATikI9kMQd71zW7o4MM4FIOcuT5Qu6ugyVDAVq5mh3SCDaTq1 y792ZxF8NmODXQ1zm+GliKWYg/MSZbV4HKXyl0OXvofLZzXsPWiaaLRosW5EMMDTCxxW JTOx3rd0l7xgfYsYlnJDpox184G24Bm3gJziHOvOHW71MWSVZdq8v1dzsv2QuQr7hBpo t5WSm6zg7oCfonS0a5wwggmy/B5glQY2uBm/1aIUuFvKhOHMi78EPBkDri2Mgq17QG1d kBOATOlqnMDhfnPWSoC84ejURZYRF2aZMt6IAQx2tck0jcesc0LezoG14sdeAnftwqkc XjbQ== X-Forwarded-Encrypted: i=1; AL+hErGCwvM6N4KskS6wXDac8P6JZwClFJbQHvJTRVXCD+1O3hGYQ4CO5Rp89ZO7w+scfsWdccZUGSMuE62p8/Y=@vger.kernel.org X-Gm-Message-State: APuU4Z+u+uQi1i6xoJFZk1EnZ0nYBB1dA32X8Qd+XaDDSRXHRZH/ltH8 Og6/BviZZeyXjaJuGZiRP62TLhIHMllpxEXp/WUkwPfXEBg/4wRkJkkPhfFYqgI0464= X-Gm-Gg: AYBFou3BZ1dzIyBzbhTbe31PvOUnffUcpNT+KlaSE4joVnmYJvavgEMfyk1NK9A59Bo SPGDHRbbnj+0shg0cOpd/4fFHGtYEKZh9r37x6GWXrCJawfgfKunJgFhcMc4M4ofhf9wnselHLS wEMKZyi9WeBp/ThTLkf4RsXwvl6I7KD5/rLVM9xOSJy2b4iBFCPL/nvURpXzgrRzyrucp6eI8lO VAOlYE47E+odfkF0yI4wOskJRGRuK2qCBvAjQHYF5g7FSkvsFTzPFSw8ljPCcJWlJn4CtFPYHTY RtqrqOdJVHBnv0koBZ7MCnckCaIgWusazU9v4w0hGw1W8a3apz4IzRh3Dw6m4S3q4m62QQxDLsv GLn8XapponrvAlaAKEPgcp8vh78jGxT103kXIFo8zJW3HtUhQ0u/f5mcMNEaeu5EOw9gchqXggr VXXQLu9QrmLkRHeeVu5n2UTliquJ+8MkvhgmY7a4VzLvRdurseWtKSqi3cYCaOl9jldFHqO74Hz pQ2skeKKrORvbGivG7FB1Vrv6/Y6HSQPcs+iE0wtDU5F7SLVNoLw9D266vuEgXSOiTy2xRn5tqx fR4CzG2N4vOhGTPLl3iD+Wk7iA9tXL3G7/X8vZI05rtWShquPjxfVzNvcqyOqeyUpY8kUbmGw4V CG0VBEWUHWTOeX0q877Se9iH3UdAMsQDetXtID+OwdBJcLUdh2BltI85R59CQ75My+4t5NOoq7D oKrmM59tMuP3a0gs+Uiwz+ZD0OG+wA4NC0FKWHdVoZNu0UoEkZWpgbqQm6388= X-Received: by 2002:a17:90b:37cf:b0:3ac:9e76:aa76 with SMTP id 98e67ed59e1d1-3b1b09f3b85mr1586733a91.13.1794559914380; Fri, 13 Nov 2026 00:51:54 -0800 (PST) Received: from [172.22.0.2] ([50.175.87.158]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b1b3b708adsm1115279a91.4.2026.11.13.00.51.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2026 00:51:53 -0800 (PST) Date: Fri, 13 Nov 2026 00:51:47 -0800 Message-ID: In-Reply-To: References: From: Chris Down Subject: [PATCH v3 6/6] slub_kunit: Add a test for the kmalloc_partition boot parameter To: Vlastimil Babka , Harry Yoo , Andrew Morton Cc: Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , Kees Cook , Marco Elver , GONG Ruiqi , Jonathan Corbet , linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/2.4.3 (cc9e3943) (2026-10-09) Check that the kmalloc partitions are separate caches by default, and alias the normal kmalloc caches when they're turned off with kmalloc_partition=0. The test is skipped on kernels built without CONFIG_KMALLOC_PARTITION_CACHES. Make kmalloc_partition_enabled global and export it to KUnit so that the test can see it. Signed-off-by: Chris Down --- lib/tests/slub_kunit.c | 45 ++++++++++++++++++++++++++++++++++++++++++ mm/slab.h | 1 + mm/slab_common.c | 3 ++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c index 1af3f087134f..61227c242ae2 100644 --- a/lib/tests/slub_kunit.c +++ b/lib/tests/slub_kunit.c @@ -334,6 +334,50 @@ static void test_slab_buckets(struct kunit *test) kfree(p); } +/* + * Each partition copy is a separate cache, unless the copies were disabled + * with kmalloc_partition=0: then they alias the KMALLOC_NORMAL caches. + */ +static void test_kmalloc_partition(struct kunit *test) +{ + enum kmalloc_cache_type type; + struct kmem_cache *s; + unsigned int idx; + bool found; + void *p; + + if (!IS_ENABLED(CONFIG_KMALLOC_PARTITION_CACHES)) + kunit_skip(test, "requires CONFIG_KMALLOC_PARTITION_CACHES"); + + for (type = KMALLOC_PARTITION_START + 1; type <= KMALLOC_PARTITION_END; type++) { + for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[type]); idx++) { + struct kmem_cache *normal = kmalloc_caches[KMALLOC_NORMAL][idx]; + struct kmem_cache *part = kmalloc_caches[type][idx]; + + if (!kmalloc_partition_enabled) { + KUNIT_EXPECT_PTR_EQ(test, part, normal); + } else if (normal) { + KUNIT_EXPECT_NOT_NULL(test, part); + KUNIT_EXPECT_PTR_NE(test, part, normal); + } + } + } + + p = kmalloc(64, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, p); + s = virt_to_slab(p)->slab_cache; + idx = kmalloc_index(64); + if (!kmalloc_partition_enabled) { + KUNIT_EXPECT_PTR_EQ(test, s, kmalloc_caches[KMALLOC_NORMAL][idx]); + } else { + found = false; + for (type = KMALLOC_PARTITION_START; type <= KMALLOC_PARTITION_END; type++) + found |= s == kmalloc_caches[type][idx]; + KUNIT_EXPECT_TRUE_MSG(test, found, "allocated from %s", s->name); + } + kfree(p); +} + #if defined(CONFIG_PERF_EVENTS) || (defined(CONFIG_KPROBES) && defined(CONFIG_SMP)) #define NR_ITERATIONS 1000 #define NR_OBJECTS 1000 @@ -528,6 +572,7 @@ static struct kunit_case test_cases[] = { KUNIT_CASE(test_leak_destroy), KUNIT_CASE(test_krealloc_redzone_zeroing), KUNIT_CASE(test_slab_buckets), + KUNIT_CASE(test_kmalloc_partition), #ifdef CONFIG_PERF_EVENTS KUNIT_CASE_SLOW(test_kmalloc_nolock_and_friends_perf), #endif diff --git a/mm/slab.h b/mm/slab.h index 6a28951f6d5d..2a9d6a9a9948 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -391,6 +391,7 @@ extern struct list_head slab_caches; extern struct kmem_cache *kmem_cache; extern bool slab_buckets_enabled; +extern bool kmalloc_partition_enabled; /* A table of kmalloc cache names and sizes */ extern const struct kmalloc_info_struct { diff --git a/mm/slab_common.c b/mm/slab_common.c index 20b066d61703..d0695e4d2ab6 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -781,8 +781,9 @@ unsigned long random_kmalloc_seed __ro_after_init; EXPORT_SYMBOL(random_kmalloc_seed); #endif -static bool kmalloc_partition_enabled __ro_after_init = +bool kmalloc_partition_enabled __ro_after_init = IS_ENABLED(CONFIG_KMALLOC_PARTITION_CACHES); +EXPORT_SYMBOL_IF_KUNIT(kmalloc_partition_enabled); static int __init setup_kmalloc_partition(char *str) { -- 2.56.1