Skip to contentSkip to conversation

refperf: Remove redundant kfree() after torture_stop_kthread()

PATCH Superseded Kaushlendra Kumar 2 participants 3 messages Sep 9, 2025 – Sep 10, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 refperf: Remove redundant kfree() after torture_stop_kthread()

Remove unnecessary kfree(main_task) call in ref_scale_cleanup() as torture_stop_kthread() already handles the memory cleanup for the task structure internally.

The torture_stop_kthread() function is designed to stop the kernel thread and free the associated task structure as part of its cleanup process. The additional kfree(main_task) call after torture_stop_kthread() results in a double-free condition.

This pattern is consistent with other torture test modules where torture_stop_kthread() is called without explicit kfree() of the task pointer, as the torture framework manages the task lifecycle internally.

---
 kernel/rcu/refscale.c | 1 -
 1 file changed, 1 deletion(-)
kernel/rcu/refscale.c +0 -1
index 2c2648a3ad30..2bfa987f4ba9 100644
@@ -1050,7 +1050,6 @@ ref_scale_cleanup(void)
 	kfree(reader_tasks);
 
 	torture_stop_kthread("main_task", main_task);
-	kfree(main_task);
 
 	// Do scale-type-specific cleanup operations.
 	if (cur_ops->cleanup != NULL)
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 3 lines · Kaushlendra Kumar

Remove unnecessary kfree(main_task) call in ref_scale_cleanup() as torture_stop_kthread() already handles the memory cleanup for the task structure internally.

Again, thank you for your interest in Linux-kernel RCU!

And you are right that the kfree(main_task) is redundant, especially given that the main_task pointer will be set to NULL in _torture_stop_kthread(), so that this kfree() is just a confusing no-op.

Quoted 4 lines

The torture_stop_kthread() function is designed to stop the kernel thread and free the associated task structure as part of its cleanup process. The additional kfree(main_task) call after torture_stop_kthread() results in a double-free condition.

But this last sentence needs adjustment. Because _torture_stop_kthread() NULLs the pointer, the second kfree() gets a NULL pointer, thus not doing a duplicate free, right?

Quoted 15 lines

This pattern is consistent with other torture test modules where torture_stop_kthread() is called without explicit kfree() of the task pointer, as the torture framework manages the task lifecycle internally.

---
 kernel/rcu/refscale.c | 1 -
 1 file changed, 1 deletion(-)
kernel/rcu/refscale.c +0 -0
index 2c2648a3ad30..2bfa987f4ba9 100644

kernel/rcu/refscale.c ref_scale_cleanup(void) · line 1050

Quoted 1 line
kernel/rcu/refscale.c +0 -0
@@ -1050,7 +1050,6 @@ ref_scale_cleanup(void)
 	kfree(reader_tasks);

For ease of debugging with kdgb, drgn, and other tools, it would also be good to set reader_tasks to NULL. Please feel free to do that, either as part of this patch or as a separate patch.

Thanx, Paul

Quoted 8 lines
kernel/rcu/refscale.c +0 -1
 	torture_stop_kthread("main_task", main_task);
-	kfree(main_task);
 
 	// Do scale-type-specific cleanup operations.
 	if (cur_ops->cleanup != NULL)
Signature · 1 line
2.34.1
#2 lore raw

Hi Paul,

Quoted 7 lines · Paul E. McKenney
Quoted 3 lines · Kaushlendra Kumar

Remove unnecessary kfree(main_task) call in ref_scale_cleanup() as torture_stop_kthread() already handles the memory cleanup for the task structure internally.

Again, thank you for your interest in Linux-kernel RCU!

Quoted 8 lines

And you are right that the kfree(main_task) is redundant, especially given that the main_task pointer will be set to NULL in _torture_stop_kthread(), so that this kfree() is just a confusing no-op.

Quoted 4 lines

The torture_stop_kthread() function is designed to stop the kernel thread and free the associated task structure as part of its cleanup process. The additional kfree(main_task) call after torture_stop_kthread() results in a double-free condition.

But this last sentence needs adjustment. Because _torture_stop_kthread() NULLs the pointer, the second kfree() gets a NULL pointer, thus not doing a duplicate free, right?

You're absolutely correct! Thank you for the clarification. .I have sent a v2 patch with the corrected commit message.

Quoted 1 line

For ease of debugging with kdgb, drgn, and other tools, it would also be good to set reader_tasks to NULL. Please feel free to do that, either as part of this patch or as a separate patch.

I'll handle the reader_tasks = NULL assignment as a separate patch to keep this one focused on removing the redundant kfree().

Thank you for the review!

Best regards,
Kaushlendra Kumar