Re: [PATCH v2] mm/kmemleak: avoid soft lockup when scanning task stacks
From: SeongJae Park
Date: Fri Jun 12 2026 - 20:53:33 EST
On Fri, 12 Jun 2026 08:16:07 -0700 Breno Leitao <leitao@xxxxxxxxxx> wrote:
> kmemleak_scan() walks every thread and scans its kernel stack under a
> single rcu_read_lock() with no reschedule point. On a host with very
> many threads -- amplified by KASAN/lockdep in debug builds -- this loop
> can hog a CPU long enough to trip the soft lockup watchdog:
>
> watchdog: BUG: soft lockup - CPU#35 stuck for 22s! [kmemleak:537]
> scan_block
> kmemleak_scan
> kmemleak_scan_thread
> kthread
>
> A cond_resched() cannot be added directly: the loop runs inside an RCU
> read-side critical section.
>
> Borrow the rcu_lock_break() pattern from kernel/hung_task.c: when a
> reschedule is needed, pin the two iteration cursors, drop the RCU read
> lock, cond_resched(), then re-acquire it and continue only if both
> cursors are still hashed.
>
> If a cursor was unhashed while the lock was dropped, the thread list
> cannot be walked further, so the round is aborted. Such a round scans
> only part of the task stacks, which would make live objects look
> unreferenced, so reuse the existing "scan interrupted" path to skip
> reporting; the next full scan reports the real leaks.
>
> Fixes: c4b28963fd79 ("mm/kmemleak: rely on rcu for task stack scanning")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Breno Leitao <leitao@xxxxxxxxxx>
Thank you for fixing this, Breno. Nothing stood out to me while reading the
patch, other than the below tiny and trivial nit. Regardless of that, please
feel free to add
Reviewed-by: SeongJae Park <sj@xxxxxxxxxx>
[...]
> @@ -1890,11 +1917,21 @@ static void kmemleak_scan(void)
> rcu_read_lock();
> for_each_process_thread(g, p) {
> void *stack = try_get_task_stack(p);
> +
> if (stack) {
> scan_block(stack, stack + THREAD_SIZE, NULL);
> put_task_stack(p);
> }
> + /*
> + * This is an expensive loop, we must to call the
> + * scheduler to avoid lockups
s/must to call/must call/ ?
I saw Lance also provided a suggestion for making this comment better. I think
that's also good and maybe even better than my suggestion. :)
Thanks,
SJ
[...]