mm/kmemleak: avoid soft lockup when scanning task stacks
Subsystems (3)
v3 cover mm/kmemleak: avoid soft lockup when scanning task stacks
kmemleak_scan() scans every task stack under one rcu_read_lock() with no reschedule point, which can trip the soft lockup watchdog on hosts with very many threads.
That prints the following message, depending on the workload+host configuration:
watchdog: BUG: soft lockup - CPU#35 stuck for 22s! [kmemleak:537]
scan_block
kmemleak_scan
kmemleak_scan_thread
kthreadPatch 1 walks the tasks with find_ge_pid() so the scan reschedules between tasks
Patches 2-3 let the scan loops stop early once a scan is interrupted.
---
Changes in v3:
- Rework the task stack walk to use find_ge_pid() instead of v1's array
and v2's rcu_lock_break() helper (Catalin).
- Add two follow-up patches letting scan_block() report an interrupted
scan so the scan loops stop early.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Do not create the nasty array, but use the same pattern as
kernel/hung_task.c.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Breno Leitao (3):
mm/kmemleak: avoid soft lockup when scanning task stacks mm/kmemleak: stop the task stack scan early when interrupted mm/kmemleak: stop the per-cpu and struct page scans early too
mm/kmemleak.c | 88 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 23 deletions(-)
---
base-commit: abe651837cb394f76d738a7a747322fca3bf17ba
change-id: 20260611-kmemleak-stack-resched-01ed72858a7f
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
PATCH v3 1/3 mm/kmemleak: avoid soft lockup when scanning task stacks
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.
Walk the tasks one PID at a time with find_ge_pid(), taking the RCU read lock only to look up and pin each task. The stack is then scanned with no lock held, so cond_resched() runs between tasks and the scan stops early on scan_should_stop(). This follows the next_tgid()/task_seq_get_next() iteration pattern and keeps each RCU critical section short.
--- mm/kmemleak.c | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-)
mm/kmemleak.c +38 -13
@@ -1695,6 +1695,42 @@ static void kmemleak_cond_resched(struct kmemleak_object *object)put_object (object); } +/* +* Scan all task kernel stacks, rescheduling between tasks. Each task is looked +* up and pinned within its own RCU read-side section, so no lock is held across +* the scan and the walk cannot trip the soft lockup watchdog. +*/ +static void kmemleak_scan_task_stacks (void ) +{ +struct pid *pid; +int nr =1 ; + +do { +struct task_struct *p =NULL ; + +rcu_read_lock (); + pid =find_ge_pid (nr, &init_pid_ns); +if (pid) { + nr =pid_nr (pid) +1 ; + p =pid_task (pid,PIDTYPE_PID ); +if (p) +get_task_struct (p); + } +rcu_read_unlock (); + +if (p) { +void *stack =try_get_task_stack (p); + +if (stack) { +scan_block (stack, stack +THREAD_SIZE ,NULL ); +put_task_stack (p); + } +put_task_struct (p); + } +cond_resched (); + }while (pid && !scan_should_stop ()); +} +/* * Print one leak inline. The hex dump is gated on OBJECT_ALLOCATED so it * does not touch user memory that was freed concurrently; the rest of the
@@ -1884,19 +1920,8 @@ static void kmemleak_scan(void)/* * Scanning the task stacks (may introduce false negatives). */ -if (kmemleak_stack_scan) { -struct task_struct *p, *g; - -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); - } - } -rcu_read_unlock (); - } +if (kmemleak_stack_scan) +kmemleak_scan_task_stacks ();/* * Scan the objects already referenced from the sections scanned
Signature · 1 line
2.53.0-Meta
Quoted 20 lines · Breno Leitao
kmemleak_scan()walks every thread and scans its kernel stack under a singlercu_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 kthreadA
cond_resched()cannot be added directly: the loop runs inside an RCU read-side critical section.Walk the tasks one PID at a time with
find_ge_pid(), taking the RCU read lock only to look up and pin each task. The stack is then scanned with no lock held, socond_resched()runs between tasks and the scan stops early onscan_should_stop(). This follows the next_tgid()/task_seq_get_next() iteration pattern and keeps each RCU critical section short.
Quoted 3 lines
Fixes c4b28963fd79 ("mm/kmemleak: rely on rcu for task stack scanning")Signed-off-by Breno Leitao <[email protected]>
I think the Fixes is just a marker to tell how far back to go. Before the above commit, we used a read_lock(&tasklist_lock) which probably had similar issues.
Thanks.
Signature · 1 line
Catalin
Quoted 20 lines · Breno Leitao
kmemleak_scan()walks every thread and scans its kernel stack under a singlercu_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 kthreadA
cond_resched()cannot be added directly: the loop runs inside an RCU read-side critical section.Walk the tasks one PID at a time with
find_ge_pid(), taking the RCU read lock only to look up and pin each task. The stack is then scanned with no lock held, socond_resched()runs between tasks and the scan stops early onscan_should_stop(). This follows the next_tgid()/task_seq_get_next() iteration pattern and keeps each RCU critical section short.
Quoted 3 lines
Fixes c4b28963fd79 ("mm/kmemleak: rely on rcu for task stack scanning")Signed-off-by Breno Leitao <[email protected]>
LGTM
Quoted 40 lines · Breno Leitao
kmemleak_scan()walks every thread and scans its kernel stack under a singlercu_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 kthreadA
cond_resched()cannot be added directly: the loop runs inside an RCU read-side critical section.Walk the tasks one PID at a time with
find_ge_pid(), taking the RCU read lock only to look up and pin each task. The stack is then scanned with no lock held, socond_resched()runs between tasks and the scan stops early onscan_should_stop(). This follows the next_tgid()/task_seq_get_next() iteration pattern and keeps each RCU critical section short.Fixes c4b28963fd79 ("mm/kmemleak: rely on rcu for task stack scanning")Signed-off-by Breno Leitao <[email protected]>---
mm/kmemleak.c| 51 ++++++++++++++++++++++++++++++++++++++-------------1 file changed, 38 insertions(+), 13 deletions(-)mm/kmemleak.c +5
-0@@ -1695,6 +1695,42 @@ static void kmemleak_cond_resched(struct kmemleak_object *object)put_object (object); } +/* +* Scan all task kernel stacks, rescheduling between tasks. Each task is looked +* up and pinned within its own RCU read-side section, so no lock is held across +* the scan and the walk cannot trip the soft lockup watchdog. +*/
mm/kmemleak.c static void kmemleak_cond_resched(struct kmemleak_object *object) · line 1712
Quoted 10 lines
mm/kmemleak.c +10
-0+static void kmemleak_scan_task_stacks (void ) +{ +struct pid *pid; +int nr =1 ; + + do { + struct task_struct *p = NULL; + + rcu_read_lock(); + pid = find_ge_pid(nr, &init_pid_ns);
I wasn't aware of find_ge_pid() before. It walks the pid IDR, not every possible pid number :) LGTM.
Quoted 6 lines · Breno Leitao
Walk the tasks one PID at a time with
find_ge_pid(), taking the RCU read lock only to look up and pin each task. The stack is then scanned with no lock held, socond_resched()runs between tasks and the scan stops early onscan_should_stop(). This follows the next_tgid()/task_seq_get_next() iteration pattern and keeps each RCU critical section short.
PATCH v3 2/3 mm/kmemleak: stop the task stack scan early when interrupted
In reply to Breno Leitaoscan_block() already checks scan_should_stop() for every pointer and bails out of the current block, but the task stack walk cannot tell and keeps issuing a separate scan_should_stop() between every task.
Return that status from scan_block() and use it as the task stack loop condition, so the walk stops as soon as a scan is interrupted.
--- mm/kmemleak.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
mm/kmemleak.c +12 -6
@@ -1524,22 +1524,25 @@ static int scan_should_stop(void)/* * Scan a memory block (exclusive range) for valid pointers and add those -* found to the gray list. +* found to the gray list. Return non-zero if the scan was interrupted. */ -static void scan_block (void *_start,void *_end, -struct kmemleak_object *scanned) +static int scan_block (void *_start,void *_end, +struct kmemleak_object *scanned) {unsigned long *ptr;unsigned long *start =PTR_ALIGN (_start,BYTES_PER_POINTER );unsigned long *end = _end - (BYTES_PER_POINTER -1 );unsigned long flags; +int stop =0 ;raw_spin_lock_irqsave (&kmemleak_lock, flags);for (ptr = start; ptr < end; ptr++) {unsigned long pointer; -if (scan_should_stop ()) +if (scan_should_stop ()) { + stop =1 ;break ; + }kasan_disable_current (); pointer = *(unsigned long *)kasan_reset_tag ((void *)ptr);
@@ -1549,6 +1552,8 @@ static void scan_block(void *_start, void *_end,pointer_update_refs (scanned, pointer,OBJECT_PERCPU ); }raw_spin_unlock_irqrestore (&kmemleak_lock, flags); + +return stop; }/*
@@ -1704,6 +1709,7 @@ static void kmemleak_scan_task_stacks(void) {struct pid *pid;int nr =1 ; +int stop =0 ; do { struct task_struct *p = NULL;
@@ -1722,13 +1728,13 @@ static void kmemleak_scan_task_stacks(void)void *stack =try_get_task_stack (p);if (stack) { -scan_block (stack, stack +THREAD_SIZE ,NULL ); + stop =scan_block (stack, stack +THREAD_SIZE ,NULL );put_task_stack (p); }put_task_struct (p); }cond_resched (); - }while (pid && !scan_should_stop ()); + }while (pid && !stop); }/*
Signature · 1 line
2.53.0-Meta
Re: [PATCH v3 2/3] mm/kmemleak: stop the task stack scan early when interrupted
Quoted 9 lines · Breno Leitao
scan_block()already checksscan_should_stop()for every pointer and bails out of the current block, but the task stack walk cannot tell and keeps issuing a separatescan_should_stop()between every task.Return that status from
scan_block()and use it as the task stack loop condition, so the walk stops as soon as a scan is interrupted.Suggested-by Catalin Marinas <[email protected]>Signed-off-by Breno Leitao <[email protected]>
Re: [PATCH v3 2/3] mm/kmemleak: stop the task stack scan early when interrupted
In reply to Breno LeitaoQuoted 10 lines · Breno Leitao
scan_block()already checksscan_should_stop()for every pointer and bails out of the current block, but the task stack walk cannot tell and keeps issuing a separatescan_should_stop()between every task.Return that status from
scan_block()and use it as the task stack loop condition, so the walk stops as soon as a scan is interrupted.Suggested-by Catalin Marinas <[email protected]>Signed-off-by Breno Leitao <[email protected]>
PATCH v3 3/3 mm/kmemleak: stop the per-cpu and struct page scans early too
In reply to Breno LeitaoThe per-cpu and struct page scan loops have no reschedule-stop check of their own: once a scan is interrupted they keep calling scan_block() for every remaining block, which scans nothing useful.
Propagate scan_block()'s interrupted status through scan_large_block() and break both loops as soon as it is set.
--- mm/kmemleak.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
mm/kmemleak.c +17 -6
@@ -1558,18 +1558,22 @@ static int scan_block(void *_start, void *_end,/* * Scan a large memory block in MAX_SCAN_SIZE chunks to reduce the latency. +* Return non-zero if the scan was interrupted. */ #ifdef CONFIG_SMP -static void scan_large_block (void *start,void *end) +static int scan_large_block (void *start,void *end) {void *next;while (start < end) { next =min (start +MAX_SCAN_SIZE , end); -scan_block (start, next,NULL ); +if (scan_block (start, next,NULL )) +return 1 ; start = next;cond_resched (); } + +return 0 ; }#endif
@@ -1889,9 +1893,11 @@ static void kmemleak_scan(void)#ifdef CONFIG_SMP /* per-cpu sections scanning */ -for_each_possible_cpu (i) -scan_large_block (__per_cpu_start +per_cpu_offset (i), - __per_cpu_end +per_cpu_offset (i)); +for_each_possible_cpu (i) { +if (scan_large_block (__per_cpu_start +per_cpu_offset (i), + __per_cpu_end +per_cpu_offset (i))) +break ; + }#endif /*
@@ -1902,6 +1908,7 @@ static void kmemleak_scan(void)unsigned long start_pfn = zone->zone_start_pfn;unsigned long end_pfn =zone_end_pfn (zone);unsigned long pfn; +int stop =0 ;for (pfn = start_pfn; pfn < end_pfn; pfn++) {struct page *page =pfn_to_online_page (pfn);
@@ -1918,8 +1925,12 @@ static void kmemleak_scan(void)/* only scan if page is in use */ if (page_count (page) ==0 )continue ; -scan_block (page, page +1 ,NULL ); + stop =scan_block (page, page +1 ,NULL ); +if (stop) +break ; } +if (stop) +break ; }put_online_mems ();
Signature · 1 line
2.53.0-Meta
Re: [PATCH v3 3/3] mm/kmemleak: stop the per-cpu and struct page scans early too
Quoted 9 lines · Breno Leitao
The per-cpu and struct page scan loops have no reschedule-stop check of their own: once a scan is interrupted they keep calling
scan_block()for every remaining block, which scans nothing useful.Propagate scan_block()'s interrupted status through
scan_large_block()and break both loops as soon as it is set.Suggested-by Catalin Marinas <[email protected]>Signed-off-by Breno Leitao <[email protected]>
Re: [PATCH v3 3/3] mm/kmemleak: stop the per-cpu and struct page scans early too
In reply to Breno Leitaomm/kmemleak.c static void kmemleak_scan(void) · line 1901
Quoted 12 lines · Breno Leitao
#ifdef
CONFIG_SMPmm/kmemleak.c +5
-3/* per-cpu sections scanning */ -for_each_possible_cpu (i) -scan_large_block (__per_cpu_start +per_cpu_offset (i), - __per_cpu_end +per_cpu_offset (i)); +for_each_possible_cpu (i) { +if (scan_large_block (__per_cpu_start +per_cpu_offset (i), + __per_cpu_end +per_cpu_offset (i))) +break ; + }#endif
The patch looks correct, but...
if scan_large_block() returns true, then
Quoted 9 lines
mm/kmemleak.c +1
-0@@ -1902,6 +1908,7 @@ static void kmemleak_scan(void)unsigned long start_pfn = zone->zone_start_pfn;unsigned long end_pfn =zone_end_pfn (zone);unsigned long pfn; +int stop =0 ;for (pfn = start_pfn; pfn < end_pfn; pfn++) {struct page *page =pfn_to_online_page (pfn);
mm/kmemleak.c static void kmemleak_scan(void) · line 1928
Quoted 5 lines
mm/kmemleak.c +1
-1@@ -1918,8 +1925,12 @@ static void kmemleak_scan(void)/* only scan if page is in use */ if (page_count (page) ==0 )continue ; -scan_block (page, page +1 ,NULL ); + stop =scan_block (page, page +1 ,NULL );
it is pointless to enter this for_each_populated_zone() loop and call kmemleak_scan_task_stacks() after that?
Not that I think this is bad, just a question...
Oleg.
Re: [PATCH v3 3/3] mm/kmemleak: stop the per-cpu and struct page scans early too
Hello Oleg,
Quoted 24 lines · Oleg Nesterov
mm/kmemleak.c static void kmemleak_scan(void) · line 1901
Quoted 12 lines · Breno Leitao
#ifdef
CONFIG_SMPmm/kmemleak.c +5
-3/* per-cpu sections scanning */ -for_each_possible_cpu (i) -scan_large_block (__per_cpu_start +per_cpu_offset (i), - __per_cpu_end +per_cpu_offset (i)); +for_each_possible_cpu (i) { +if (scan_large_block (__per_cpu_start +per_cpu_offset (i), + __per_cpu_end +per_cpu_offset (i))) +break ; + }#endif The patch looks correct, but...
if
scan_large_block()returns true, thenQuoted 6 lines
mm/kmemleak.c +1
-0@@ -1902,6 +1908,7 @@ static void kmemleak_scan(void)unsigned long start_pfn = zone->zone_start_pfn;unsigned long end_pfn =zone_end_pfn (zone);unsigned long pfn; +int stop =0 ;
Quoted 11 lines
mm/kmemleak.c static void kmemleak_scan(void) · line 1928
Quoted 8 lines
mm/kmemleak.c +1
-1for (pfn = start_pfn; pfn < end_pfn; pfn++) {struct page *page =pfn_to_online_page (pfn);@@ -1918,8 +1925,12 @@ static void kmemleak_scan(void)/* only scan if page is in use */ if (page_count (page) ==0 )continue ; -scan_block (page, page +1 ,NULL ); + stop =scan_block (page, page +1 ,NULL );it is pointless to enter this
for_each_populated_zone()loop and callkmemleak_scan_task_stacks()after that?
Upon further review, your semll is right, once a phase trips scan_should_stop() the later phases are still entered and each just bails on its first scan_block(). That can be optimized away, e.g. by lifting 'stop' to function scope and guarding the remaining phases so they aren't entered at all.
It is not a regression, though -- the pointless work is pre-existing.
Before this series none of these loops broke out on scan_should_stop(): the scan ground through every CPU, every pfn and every thread, relying only on scan_block() returning early on each call. This series already removes the bulk of that; the bit you spotted (entering the next phase only to bail) is the small leftover.
So I'd rather keep this series as-is and do that optimization as a follow-up once it lands, instead of growing a patch that's already been reviewed. Are you OK with that?
Thanks,
--breno
Oleg Nesterov
Reviewed-byRe: [PATCH v3 3/3] mm/kmemleak: stop the per-cpu and struct page scans early too
Hi Breno,
Quoted 4 lines · Breno Leitao
So I'd rather keep this series as-is and do that optimization as a follow-up once it lands, instead of growing a patch that's already been reviewed. Are you OK with that?
OK, agreed.
I don't think you need my ack, but FWIW