From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 307763FB7F1 for ; Tue, 19 May 2026 09:37:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779183481; cv=none; b=dAVEet70Ns1bvXmlzRFh4SiXyq/3emq+8v0vfxM/AMwf8kMrh/7CiMn8CrmEd02nBjZEEmaXhyTxq3A1NyB2PV4jwUuWRY7IFIne7OrmH+YK4Fa/EBB73HvUqN33ln6vLO33wU5xnSRegc3RmcTHbs9OgFnB4Q1ZuRUFTrB5bAE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779183481; c=relaxed/simple; bh=sCSQPZkTzGivh8YtHVGYj/jpBIy9bGpSQArwmUcvdJU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=gm7Y9QmRBZ/KBAytl5/Vje/vyop9Dp2Zy5ZhwQd4/lrM7+GDzEVIRXiSfGBSGWOM702pPDSqUbGXrtwdC3b8+zru/umzSRcSXH1Wto0qo3u9vB4Cqyw1J4tZCPTLw9hpuHgUtjTPe6/6nhDR9/lOKj0WX6ik8R79w7OMP2YzZgs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=UE8lmV0m; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="UE8lmV0m" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4CE7C1CE0; Tue, 19 May 2026 02:37:53 -0700 (PDT) Received: from entos-yitian-01.Arm.com (unknown [10.168.197.69]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8C2E43F632; Tue, 19 May 2026 02:37:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1779183478; bh=sCSQPZkTzGivh8YtHVGYj/jpBIy9bGpSQArwmUcvdJU=; h=From:To:Cc:Subject:Date:From; b=UE8lmV0mb0fpelb26D0LXteXnWfZ96MkPRwt2z5iwHgUe8XopDyl/j9eIqpkAolyr Epl3pkTFBhvjyvN5R2IUrGBb98O926LJnZ/esxVucOQtah/7nklEYchwAWpjC55lkr CX0a0oGHTjzzQNfSXOfYWt7nbwF4turhJPWwjuDw= From: Jia He To: "Paul E. McKenney" , Petr Mladek , Kees Cook Cc: linux-kernel@vger.kernel.org, Jia He Subject: [PATCHv2 1/1] lib/tests: test_ratelimit: fix stress test thread lifecycle and leak Date: Tue, 19 May 2026 09:37:45 +0000 Message-Id: <20260519093745.1062843-1-justin.he@arm.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The stress test checks the kthread pointer in the child thread, but the thread was created with kthread_run(). Since kthread_run() wakes the thread before returning, the child can run before sktp[i].tp is assigned, triggering the WARN_ON_ONCE() check on an arm64 N2 server with 128 cores. Use kthread_create() instead, assign sktp[i].tp after successful creation, and then wake the thread explicitly. This guarantees that the child observes an initialized task pointer. Also add a common cleanup path for thread creation failures. If creating one of the later threads fails, stop all threads that were already started and free the allocated array instead of leaving orphan kthreads and leaked memory behind. Finally, replace the module-static doneflag with kthread_should_stop(). With the doneflag, child threads may exit before the parent calls kthread_stop(), so the task lifetime is no longer guaranteed when the parent later tries to stop them. Using kthread_should_stop() keeps each child alive until kthread_stop() synchronously terminates it. Signed-off-by: Jia He --- v2: replace the module-static doneflag with kthread_should_stop() to avoid UAF (sashiko-bot) lib/tests/test_ratelimit.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/tests/test_ratelimit.c b/lib/tests/test_ratelimit.c index 33cea5f3d28b..d95c49e914c7 100644 --- a/lib/tests/test_ratelimit.c +++ b/lib/tests/test_ratelimit.c @@ -68,7 +68,6 @@ static void test_ratelimit_smoke(struct kunit *test) static struct ratelimit_state stressrl = RATELIMIT_STATE_INIT_FLAGS("stressrl", HZ / 10, 3, RATELIMIT_MSG_ON_RELEASE); -static int doneflag; static const int stress_duration = 2 * HZ; struct stress_kthread { @@ -86,7 +85,7 @@ static int test_ratelimit_stress_child(void *arg) set_user_nice(current, MAX_NICE); WARN_ON_ONCE(!sktp->tp); - while (!READ_ONCE(doneflag)) { + while (!kthread_should_stop()) { sktp->nattempts++; if (___ratelimit(&stressrl, __func__)) sktp->nunlimited++; @@ -105,26 +104,42 @@ static void test_ratelimit_stress(struct kunit *test) const int n_stress_kthread = cpumask_weight(cpu_online_mask); struct stress_kthread skt = { 0 }; struct stress_kthread *sktp = kzalloc_objs(*sktp, n_stress_kthread); + int n_started = 0; - KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "Memory allocation failure"); + KUNIT_ASSERT_NOT_NULL_MSG(test, sktp, "Memory allocation failure"); for (i = 0; i < n_stress_kthread; i++) { - sktp[i].tp = kthread_run(test_ratelimit_stress_child, &sktp[i], "%s/%i", - "test_ratelimit_stress_child", i); - KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "kthread creation failure"); + struct task_struct *tp; + + tp = kthread_create(test_ratelimit_stress_child, &sktp[i], + "%s/%i", "test_ratelimit_stress_child", i); + if (IS_ERR(tp)) { + KUNIT_FAIL(test, "kthread_create failed: %ld", PTR_ERR(tp)); + goto out_stop; + } + + sktp[i].tp = tp; + wake_up_process(tp); + n_started++; pr_alert("Spawned test_ratelimit_stress_child %d\n", i); } schedule_timeout_idle(stress_duration); - WRITE_ONCE(doneflag, 1); - for (i = 0; i < n_stress_kthread; i++) { + +out_stop: + for (i = 0; i < n_started; i++) { kthread_stop(sktp[i].tp); skt.nattempts += sktp[i].nattempts; skt.nunlimited += sktp[i].nunlimited; skt.nlimited += sktp[i].nlimited; skt.nmissed += sktp[i].nmissed; } - KUNIT_ASSERT_EQ_MSG(test, skt.nunlimited + skt.nlimited, skt.nattempts, - "Outcomes not equal to attempts"); - KUNIT_ASSERT_EQ_MSG(test, skt.nlimited, skt.nmissed, "Misses not equal to limits"); + if (n_started == n_stress_kthread) { + KUNIT_ASSERT_EQ_MSG(test, skt.nunlimited + skt.nlimited, skt.nattempts, + "Outcomes not equal to attempts"); + KUNIT_ASSERT_EQ_MSG(test, skt.nlimited, skt.nmissed, + "Misses not equal to limits"); + } + + kfree(sktp); } static struct kunit_case ratelimit_test_cases[] = { -- 2.34.1