[PATCH] rcuscale: Make kfree_scale_thread() exit through torture_kthread_stopping()

From: Zqiang

Date: Wed Dec 02 2026 - 04:21:01 EST


The kzalloc_objs() failed returning -ENOMEM lets the kthread exit on its
own, so it's task_struct is auto-reaped while kfree_reader_tasks[] still
holds a stale, unrefcounted pointer to it. the cleanup path then calls
kthread_stop() on that pointer, dereferencing freed memory.

This commit route the failure through torture_kthread_stopping() instead,
so the kthread only exits in response to kthread_stop().

Signed-off-by: Zqiang <qiang.zhang@xxxxxxxxx>
---
kernel/rcu/rcuscale.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/kernel/rcu/rcuscale.c b/kernel/rcu/rcuscale.c
index 687249d686a5..681f08e644cc 100644
--- a/kernel/rcu/rcuscale.c
+++ b/kernel/rcu/rcuscale.c
@@ -801,6 +801,7 @@ static int
kfree_scale_thread(void *arg)
{
int i, loop = 0;
+ int firsterr = 0;
long me = (long)arg;
struct kfree_obj *alloc_ptr;
u64 start_time, end_time;
@@ -831,8 +832,11 @@ kfree_scale_thread(void *arg)

for (i = 0; i < kfree_alloc_num; i++) {
alloc_ptr = kzalloc_objs(struct kfree_obj, kfree_mult);
- if (!alloc_ptr)
- return -ENOMEM;
+ if (!alloc_ptr) {
+ SCALEOUT_ERRSTRING("out of memory");
+ firsterr = -ENOMEM;
+ goto out;
+ }

if (kfree_by_call_rcu) {
call_rcu(&(alloc_ptr->rh), kfree_call_rcu);
@@ -874,8 +878,9 @@ kfree_scale_thread(void *arg)
}
}

+out:
torture_kthread_stopping("kfree_scale_thread");
- return 0;
+ return firsterr;
}

static void
--
2.17.1