mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Jia He <justin.he@arm.com>
Cc: "Paul E. McKenney" <paulmck@kernel.org>,
	Kees Cook <kees@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCHv2 1/1] lib/tests: test_ratelimit: fix stress test thread lifecycle and leak
Date: Thu, 21 May 2026 14:51:25 +0200	[thread overview]
Message-ID: <ag7_zY5jMHHtWPVI@pathway.suse.cz> (raw)
In-Reply-To: <20260519093745.1062843-1-justin.he@arm.com>

On Tue 2026-05-19 09:37:45, Jia He wrote:
> 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.

True.

> 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.

IMHO, this does not guarantee that the task pointer has been assigned.
AFAIK, there might be spurious wakeups, for example, by the
suspend/resume or livepatching code.

I have another idea, see below.

> 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.

This part looks good to me. 

> Signed-off-by: Jia He <justin.he@arm.com>
> ---
> 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);

It seem that "sktp->tp" is not really used in this function.
I am not sure why the WARN_ON_ONCE() is there.

It would simply remove this check and keep the original kthread_run().

>  
> -	while (!READ_ONCE(doneflag)) {
> +	while (!kthread_should_stop()) {
>  		sktp->nattempts++;
>  		if (___ratelimit(&stressrl, __func__))
>  			sktp->nunlimited++;

Best Regards,
Petr

  reply	other threads:[~2026-05-21 12:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-19  9:37 Jia He
2026-05-21 12:51 ` Petr Mladek [this message]
2026-05-29  8:17   ` Justin He

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=ag7_zY5jMHHtWPVI@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=justin.he@arm.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®