From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7C473803F2 for ; Thu, 21 May 2026 12:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779367890; cv=none; b=MAnRonSrvfIzwiC4k56Nczuxy9BlzVEaNwZoQ5WzZCaP/DfwboBE9YRRJ7n/aGtx/9KgOVLOugg3CxzkClkx8sD4O5BP1Wn1CSJr2cWT4G4YvyaC4z7M2PaLvNFk4QoZlF5QCJT2GMBg4dmxBQFbz2rA5IMd1VDNZo0BmowPhiM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779367890; c=relaxed/simple; bh=SPpXdHoAbyGjLgjiY6g5WkDtYIIQKRkvRNDb6qGimJU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=gq937ZiE3cm/AdN+GEt1wWXn+Kpm96gsHk+rrzaS2X5uW2N7XqVBAevlE0Pf9DM3FNzYv60t4Lxf/WCNvXjGv3oAPBfFgs8jomSowFkpVBS8/lZ98gzQv8Q12cv7AKZigrxGJ45DCWA38EG9jtTIfDCCFA1HyNrQl7efEnQvI88= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=ON2tsNC5; arc=none smtp.client-ip=209.85.221.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="ON2tsNC5" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-45297094718so4875255f8f.3 for ; Thu, 21 May 2026 05:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1779367887; x=1779972687; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=9/tbvrxVpOQ/EMas3NioYF/low44OLJ27sYgV6LSzI8=; b=ON2tsNC508v+GNK6NLhEbv1R7miF/QqlUDCZ8FV5DDT1Ea213lR5VuvuPYUfG7w4WO gX3Z3bPJbkC9l3+sYtfilVTAnjYDsHQJG51X8w5ZkTSGdBNkEW7A4/WPL26YOdVnOxS7 xAQV2Z/KjYxkP0TMcJxXrGpRY5t9/bqOEN6Yz3lI3w2kszwHPhLU2TdIaPlWYoufAl4N DN6xKBlDN3DbU8WrYLlDnWtBoihMxyJJjKgdWu2q3UYrrT4LgTss5eq/2SfzhUpp5CeK 2Gn3JlevqIZIYkukuPlsrDLsC/eicyAolRv9uFNQLucWKxzm+wuLpciJNBZFrqgdy5HL hdKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779367887; x=1779972687; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9/tbvrxVpOQ/EMas3NioYF/low44OLJ27sYgV6LSzI8=; b=iiywICUGMMAXBkmxjGb8iv494KmGJvdTnvBP6AfCLsmBQ2QHrAieowVqDbI1yTx2T/ E58kfMJ/j361szD4itWUdTZ2KcoBHQHd6NylAy93nmxKNZEEbyhUXpe2XQwOSqV+q8s4 +L2ufh93003n6qUpfCD0gvHreLZX4tofu9D2nVmVHxy6qNRLlr0exXGpVSJKeXWUJTBB rlvVgZXVMzrurry2EjdU4CkS3P7GuyGSWDGYSttXbYTvRX0jkrEAhL2oNgMW1vb7TzIk iQ5OYYEcZImHR+PRlzb/vpZGFJ1JOOr6d8aF4Ddst7re5N06qVNrOYa+XLyY+vHMZHNH 3bYg== X-Forwarded-Encrypted: i=1; AFNElJ85uShZtbw9S5dMNOWz28jI14v2ZAYiCD/PDOpMolpNLeWS0f48TiuzdtAw+dhwurH57EIKD2lcAp+8+l0=@vger.kernel.org X-Gm-Message-State: AOJu0YwiXW8kb5OkO2YpT+mdlVCQzm4kRAPPpNlpS6vCFnyOQHjmsYPD c3xeTRH0ZiIWEz8nr4IRPyWQd8LSlCcQkoXJI4yL+YxwiwR3eGaxbTSKqXNB3u+0GLg= X-Gm-Gg: Acq92OFcLsS7bymg9Izl2khAYTLAoGiQ05LtWgzV7WT5LS6O1vGZXNQQerd3qruxGnQ kxvNCK4seac4/YDqWV9Gfe3n8BZCng8uXDYGAIT5JNEPAQ18D/U39GXD35caTD0XI8ulGrI+zMD DcZZj3WDxd8gCJZLuIJvOqakcL9LrYT0JvCGoOwimTMBtxziSWKEy0AKjVgT/iEQw7qmomeWumr U9nM9OIFNrt0/nmHZlTya4QqH7eu9MkkLUFp4TKA9KxsMM0CA3ekrhy2FZLeTyVJv8dW09zul73 LhH8esSO6WBBzodE5TEKQ6xBKV4PUcpzsyRFoSseGnc+95NTHgTlmBKSBXdfpHGLbxwTCXOqiJZ VuO0dbqid/1Iu2FJ6MaRfVBn15Ua9fqWXbzTmXrdS8+HSa8GZSL2QICNx/ipnQmRzMuzwGeAYL/ g8H4DjY/BBJBNfhbD6yHUjya72Dw== X-Received: by 2002:a05:6000:41fc:b0:45e:739b:3e43 with SMTP id ffacd0b85a97d-45ea344aedbmr4526939f8f.0.1779367887187; Thu, 21 May 2026 05:51:27 -0700 (PDT) Received: from pathway.suse.cz ([176.114.240.130]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45eaa93630asm2473854f8f.25.2026.05.21.05.51.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 May 2026 05:51:26 -0700 (PDT) Date: Thu, 21 May 2026 14:51:25 +0200 From: Petr Mladek To: Jia He Cc: "Paul E. McKenney" , Kees Cook , linux-kernel@vger.kernel.org Subject: Re: [PATCHv2 1/1] lib/tests: test_ratelimit: fix stress test thread lifecycle and leak Message-ID: References: <20260519093745.1062843-1-justin.he@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > --- > 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