From nobody Mon Apr 6 11:53:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56CB631D730 for ; Mon, 6 Apr 2026 09:09:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775466553; cv=none; b=d0OgWX5S7K3ii72hx9yd+zRcaqKF6peyp9dD2DTmh5sIjVeH/btWfxmN+VLJFmZmc4wAMGeIELHr6jrOdU0/cSEZAq7kRy/TZuyUAYahISz3KPV4JQlR9AuFYd8Cdz3eBy3PmevdjL3mfnXw5buiUnUTlz4JurYj9QNwdAwE/XE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775466553; c=relaxed/simple; bh=G7Il7/mKFzoeV4Tz39MRaOQuT1VjBgajncJeVtLZGak=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O+9gCuodDjKSbVNFwU1laBw36LtnoXYbrjL4rmtBUhYlwUj8hR+Z7fq4Ed+9f04UKXqPd4gNnrr+nbkyxhviW3TiiFNsX/PXFVAOWCyFV2/Sh+jQuwiXRuIH8euLwIV5SoleU14lRHrgBkvFI4P73XIolxQmr6BFzgfta920/qw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JvLRVcEw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JvLRVcEw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7EDC0C2BC9E; Mon, 6 Apr 2026 09:09:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775466553; bh=G7Il7/mKFzoeV4Tz39MRaOQuT1VjBgajncJeVtLZGak=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JvLRVcEw7NVelGrpfAYeykd+hge9Xt3lweLjfyls/etqVow+h0lV5k0m0bjpyXojY b/axaFmJkyiAVkRzz3IzbL5MVs7HUfLGKaLqBEnaZnUroPvY1B1Iqdl8zpcGVXZxt5 3sy7lcrRSwohEFIqn4BKY3xghHjSPBF4lJUXIMpWfqZrWC3igtKJryOhkuG3X/Naxa PW/i5N/NRrYc0Wq9LBOe7HLd2wZbi8E63jjlDv7/8RUAxr22LQX33GWoECecbdnHPL 8ESPRG89JFOuxjF3W0v8qNZ3xA/Fzkg5LzoToKqMv8tobW8uBmtT8snojC/6IZ4Rd/ VfEaH5X4W25Lw== From: "Harry Yoo (Oracle)" To: Vlastimil Babka , Andrew Morton Cc: Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , Alexei Starovoitov , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH V2 1/2] MAINTAINERS: add lib/tests/slub_kunit.c to SLAB ALLOCATOR section Date: Mon, 6 Apr 2026 18:09:06 +0900 Message-ID: <20260406090907.11710-2-harry@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260406090907.11710-1-harry@kernel.org> References: <20260406090907.11710-1-harry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The slub_kunit module has been maintained by SLAB ALLOCATOR folks, but is missing in the MAINTAINERS file. Add the missing entry. Acked-by: David Rientjes Signed-off-by: Harry Yoo (Oracle) --- MAINTAINERS | 1 + 1 file changed, 1 insertion(+) diff --git a/MAINTAINERS b/MAINTAINERS index 96ea84948d76..5f5add4f72a0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -24354,6 +24354,7 @@ F: Documentation/admin-guide/mm/slab.rst F: Documentation/mm/slab.rst F: include/linux/mempool.h F: include/linux/slab.h +F: lib/tests/slub_kunit.c F: mm/failslab.c F: mm/mempool.c F: mm/slab.h --=20 2.43.0 From nobody Mon Apr 6 11:53:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2343F31B823 for ; Mon, 6 Apr 2026 09:09:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775466555; cv=none; b=XX0F43DX39OLABaHXXz/qhBn0JJciwbF8rm3vee9J5xBWT+oEzgKcSQyHcylfcZLLXEc0ah3CP7TlOqt2YUNxCQ9PvZGEJezpsZSxm6z8qTRPZubqHp2FoxCYK0VdezOicq+a+5/MoanGTy4r4BWpuqmKUif8bYTn1i19Bz7apM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775466555; c=relaxed/simple; bh=+68CRKEHG6u9hyAJFuc4nQLarNYDPrsDi1eLDIKNsuQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Dy/S8tL8G48cAF/yr043FTliOFI7yzsfAt0dDJGJHpBfASx9FJR4soNFf2WUuzFGtVvorRGm/UXhXABtmcBwjCA8WK8kLDtQO6A4EwPJHo8b9yo3PaQ5kphxFXP1csPm0wYm+zfrpieg5qAWAbWv3zd0vd1nNIIVAPjixDtKMo8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UftitZ1R; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UftitZ1R" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CB5BC2BCB0; Mon, 6 Apr 2026 09:09:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775466554; bh=+68CRKEHG6u9hyAJFuc4nQLarNYDPrsDi1eLDIKNsuQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UftitZ1R+MbUtt7YHFOlyqSgkadiccGJBtvvAWSc+yh9VoDNaWjExtLtlq/8txg13 h04D0mP1XHvGngk9G3KfEtVZ+HrXa7eF68hEsazic/bjqGv4Cn0BhHozChIki+huQa tjfhJjrFDDECkJ541aLXY4GnJEdf5WK1kZJSR8oGWwCjvuWfN2+unzeHgIXjVeFf/4 gR8xRsQDZLdcW3RwTMiutZ2nNVf30lxuNJGejAKl2698EvTnoA4xYaJYMNJA9T/NCq d+rUaG5sRHuhoWu49JYvOrxwUPoFA8mFuTkbOSQHw7KifR8rjEQBo7uakK0aEaS+Ah lWVSViuop6dHA== From: "Harry Yoo (Oracle)" To: Vlastimil Babka , Andrew Morton Cc: Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , Alexei Starovoitov , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH V2 2/2] lib/tests/slub_kunit: add a test case for {kmalloc,kfree}_nolock Date: Mon, 6 Apr 2026 18:09:07 +0900 Message-ID: <20260406090907.11710-3-harry@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260406090907.11710-1-harry@kernel.org> References: <20260406090907.11710-1-harry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Testing invocation of {kmalloc,kfree}_nolock() during kmalloc() or kfree() is tricky, and it is even harder to ensure that slowpaths are properly tested. Lack of such testing has led to late discovery of the bug fixed by commit a1e244a9f177 ("mm/slab: use prandom if !allow_spin"). Add a slub_kunit test that allocates and frees objects in a tight loop while a perf event triggers interrupts (NMI or hardirq depending on the arch) on the same task, invoking {kmalloc,kfree}_nolock() from the overflow handler. Acked-by: David Rientjes Signed-off-by: Harry Yoo (Oracle) --- lib/tests/slub_kunit.c | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c index 848b682a2d70..fa6d31dbca16 100644 --- a/lib/tests/slub_kunit.c +++ b/lib/tests/slub_kunit.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "../mm/slab.h" =20 static struct kunit_resource resource; @@ -291,6 +292,94 @@ static void test_krealloc_redzone_zeroing(struct kunit= *test) kmem_cache_destroy(s); } =20 +#ifdef CONFIG_PERF_EVENTS +#define NR_ITERATIONS 1000 +#define NR_OBJECTS 1000 +static void *objects[NR_OBJECTS]; + +struct test_nolock_context { + struct kunit *test; + int callback_count; + int alloc_ok; + int alloc_fail; + struct perf_event *event; +}; + +static struct perf_event_attr hw_attr =3D { + .type =3D PERF_TYPE_HARDWARE, + .config =3D PERF_COUNT_HW_CPU_CYCLES, + .size =3D sizeof(struct perf_event_attr), + .pinned =3D 1, + .disabled =3D 1, + .freq =3D 1, + .sample_freq =3D 100000, +}; + +static void overflow_handler_test_kmalloc_kfree_nolock(struct perf_event *= event, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + void *objp; + gfp_t gfp; + struct test_nolock_context *ctx =3D event->overflow_handler_context; + + /* __GFP_ACCOUNT to test kmalloc_nolock() in alloc_slab_obj_exts() */ + gfp =3D (ctx->callback_count % 2) ? 0 : __GFP_ACCOUNT; + objp =3D kmalloc_nolock(64, gfp, NUMA_NO_NODE); + + if (objp) + ctx->alloc_ok++; + else + ctx->alloc_fail++; + + kfree_nolock(objp); + ctx->callback_count++; +} + +static void test_kmalloc_kfree_nolock(struct kunit *test) +{ + int i, j; + struct test_nolock_context ctx =3D { .test =3D test }; + struct perf_event *event; + bool alloc_fail =3D false; + + event =3D perf_event_create_kernel_counter(&hw_attr, -1, current, + overflow_handler_test_kmalloc_kfree_nolock, + &ctx); + if (IS_ERR(event)) + kunit_skip(test, "Failed to create perf event"); + ctx.event =3D event; + perf_event_enable(ctx.event); + for (i =3D 0; i < NR_ITERATIONS; i++) { + for (j =3D 0; j < NR_OBJECTS; j++) { + gfp_t gfp =3D (i % 2) ? GFP_KERNEL : GFP_KERNEL_ACCOUNT; + + objects[j] =3D kmalloc(64, gfp); + if (!objects[j]) { + j--; + while (j >=3D 0) + kfree(objects[j--]); + alloc_fail =3D true; + goto cleanup; + } + } + for (j =3D 0; j < NR_OBJECTS; j++) + kfree(objects[j]); + } + +cleanup: + perf_event_disable(ctx.event); + perf_event_release_kernel(ctx.event); + + kunit_info(test, "callback_count: %d, alloc_ok: %d, alloc_fail: %d\n", + ctx.callback_count, ctx.alloc_ok, ctx.alloc_fail); + + if (alloc_fail) + kunit_skip(test, "Allocation failed"); + KUNIT_EXPECT_EQ(test, 0, slab_errors); +} +#endif + static int test_init(struct kunit *test) { slab_errors =3D 0; @@ -315,6 +404,9 @@ static struct kunit_case test_cases[] =3D { KUNIT_CASE(test_kfree_rcu_wq_destroy), KUNIT_CASE(test_leak_destroy), KUNIT_CASE(test_krealloc_redzone_zeroing), +#ifdef CONFIG_PERF_EVENTS + KUNIT_CASE_SLOW(test_kmalloc_kfree_nolock), +#endif {} }; =20 --=20 2.43.0