From nobody Thu Apr 2 04:18: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 5FFC43CA488 for ; Mon, 30 Mar 2026 12:05:29 +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=1774872329; cv=none; b=ZdT2QCqPyXMkKfxl4/Gb8AAFPTz7ZPrPjZtE0xIa8Lc8EUEWeas/il1VfrNv1PbnMN4b0irF4uxyuV0fPDgRMHA877vq8J+hXObEKdvFN1O9IW4I4EIy3ES++K3vY4CNTb3aaQ+rcemZkTx+l/fBNS1YbVg0uPTC79zHoyjTHZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774872329; c=relaxed/simple; bh=3YAzMlMeOCrZ28GqQoCsD/ygZK0bAT3TmpmN5xJXIE0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pa1sqzoBykUhktedZ69M68oUetKmuDertt6Celzr6Rg+KDVaioHEPi6q8OfZVrYCqiqG6LUJXmECFpqbJMmxZ/SEWd/Vl+2+fSu1yDSHo+Sl5XBfsV1VcEZhn8YaPRA/QGTCXOC5Xgp/b1jJeTBFuygUDhOoj2CcUmpo8AAV+Ec= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fHp5i//h; 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="fHp5i//h" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B65AC2BCB0; Mon, 30 Mar 2026 12:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774872329; bh=3YAzMlMeOCrZ28GqQoCsD/ygZK0bAT3TmpmN5xJXIE0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fHp5i//hU2PYbop8C5stsJRnhWZ8wzJtYv4nGlIwbHGol9FEtgyveBDLFqan/3wup 7+uvb8JxqsRC7pbMns7pmKW2qGz86XEZar/UtikVE6CXc2zBmwh791gA/1CItMjASf xpBguOxJ9FMl7u1XWbr/jjAxhRbuNVvcEWkR8ayKUZ0Vj2ZKqpNmiOiw7RxnNHbAZ4 tt8Hm89UTBLAmatDRBvDnyN37Nwt0GUNMgrLq6EQ1J8j8Itlu8PZx6RJLjx0Jspgay vYblVsRJmLQ6Tq2mPNY1IZS0F4f7nMsOiRKh/++Oe2pBVasyxS06dmNJtESGv4udzj +Fi8mi2JPek9A== From: "Harry Yoo (Oracle)" To: Vlastimil Babka , Andrew Morton Cc: Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Harry Yoo (Oracle)" Subject: [PATCH V1 1/2] MAINTAINERS: add lib/tests/slub_kunit.c to SLAB ALLOCATOR section Date: Mon, 30 Mar 2026 21:05:15 +0900 Message-ID: <20260330120517.104743-2-harry@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260330120517.104743-1-harry@kernel.org> References: <20260330120517.104743-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. Signed-off-by: Harry Yoo (Oracle) Acked-by: David Rientjes --- 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 Thu Apr 2 04:18: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 0CF603CAE6B for ; Mon, 30 Mar 2026 12:05:31 +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=1774872331; cv=none; b=KWLGZ1nbp77A4vu9rbDiZP4AJli7a5eLrAFyvOaEr5nKfMUc2fBFrDPUz1TjqDWTB3acCNweHNgjpqztUQGN66hTE2ucnLKWz7cJlIUiW/xV7DUq0eE8/Wg6efYO/hlJce8G4+a3+DbHUlY8bNlVLoT6bSnyLydS1L43dW3BU6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774872331; c=relaxed/simple; bh=fckQad9uvkMGEVwpEwBUucO8q9GVwf03fWoMEZuJ4qg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mHaEG5HBzh744hpmvFcxxnqYG4vrYDJYXi9tvT3Id25RORMm0jeDIJE7za4IwyekqCAt5Knv+z8sNdgA/YzjIrMpFEpkSYfUgp3q5aJvlHpsgOoO2sAEN1e6Cg8CZZCfT9MupzJJOapeGTvc59jSORHvWANc0GH2gL/0wEN8FMw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jkF2qVAb; 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="jkF2qVAb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 692A4C4CEF7; Mon, 30 Mar 2026 12:05:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774872330; bh=fckQad9uvkMGEVwpEwBUucO8q9GVwf03fWoMEZuJ4qg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jkF2qVAby3PLCcJIBHq4PWILtfQMfwG08dYFVhw1GptuD9n+phFpXUGV94tU5Nrq5 SbTznRAbDuMicT15KDPSwYjGWMzaxz5UAYtGUgF6dWvY1/vzcibjdyCnWn/hh4VmkC BEcXI4ihirWdu/bmJDjF8YTUsXCfaBCQUIRTkn6JONO0crrxH40AydNsN6w4fHekjY RAkMQ6TZXof0e+FOPAq7GY7EemT3t5MdMr1Qbe4m5iQ99AXOVPTi+LfrYz28d5uojZ dShI5jczLuZ9ad2S/3prVgwOz8qxwEijqfSQBpv55Yq6BU7w+EfyHe8Zu1yo1iJkV5 I+mq9U994CyPA== From: "Harry Yoo (Oracle)" To: Vlastimil Babka , Andrew Morton Cc: Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Harry Yoo (Oracle)" Subject: [PATCH V1 2/2] lib/tests/slub_kunit: add a test case for {kmalloc,kfree}_nolock Date: Mon, 30 Mar 2026 21:05:16 +0900 Message-ID: <20260330120517.104743-3-harry@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260330120517.104743-1-harry@kernel.org> References: <20260330120517.104743-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 NMIs on the same task, invoking {kmalloc,kfree}_nolock() from the NMI handler. Signed-off-by: Harry Yoo (Oracle) Acked-by: David Rientjes --- lib/tests/slub_kunit.c | 88 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c index 848b682a2d70..2ed5f90a748a 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,92 @@ static void test_krealloc_redzone_zeroing(struct kunit= *test) kmem_cache_destroy(s); } =20 +#define REPEAT_TIMES 1000 +#define LOOP_SIZE 1000 +static void *objects[LOOP_SIZE]; + +struct nmi_context { + struct kunit *test; + int callback_count; + int alloc_ok; + int alloc_fail; + struct perf_event *event; +}; + +static struct perf_event_attr nmi_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 kmalloc_kfree_nolock_in_nmi(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + void *objp; + gfp_t gfp; + struct nmi_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 nmi_context ctx =3D { .test =3D test }; + struct perf_event *event; + bool alloc_fail =3D false; + + event =3D perf_event_create_kernel_counter(&nmi_hw_attr, -1, current, + kmalloc_kfree_nolock_in_nmi, + &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 < REPEAT_TIMES; i++) { + for (j =3D 0; j < LOOP_SIZE; j++) { + gfp_t gfp =3D (i % 2) ? 0 : __GFP_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 < LOOP_SIZE; 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); +} + static int test_init(struct kunit *test) { slab_errors =3D 0; @@ -315,6 +402,7 @@ 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), + KUNIT_CASE_SLOW(test_kmalloc_kfree_nolock), {} }; =20 --=20 2.43.0