[PATCH 2/2] mm: kmemleak: reschedule while reading reports
From: Weilin Tong
Date: Thu Nov 05 2026 - 04:59:18 EST
Reading a kmemleak report walks all tracked objects under RCU, even
when the report is empty. On large debug kernels this can stall RCU.
Use kmemleak_cond_resched() in both seq traversal loops. Request a break
when need_resched() is set and periodically every 256 positions, since
preemption can clear need_resched() without ending the RCU read-side
critical section.
The helper pins a linked cursor and defers its removal across RCU breaks.
Already removed cursors remain under RCU to protect their forward links.
This allows RCU grace periods to progress during long report reads.
Fixes: f5886c7f96f2 ("kmemleak: Protect the seq start/next/stop sequence by rcu_read_lock()")
Assisted-by: LLM
Signed-off-by: Weilin Tong <tongweilin@xxxxxxxxxxxxxxxxx>
---
mm/kmemleak.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index d959d11f6..0fe7d336c 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -2216,6 +2216,9 @@ static void stop_scan_thread(void)
}
}
+/* Also break RCU periodically when preemption clears need_resched(). */
+#define KMEMLEAK_SEQ_RESCHED_INTERVAL 256
+
/*
* Iterate over the object_list and return the first valid object at or after
* the required position with its use_count incremented. The function triggers
@@ -2233,6 +2236,9 @@ static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
rcu_read_lock();
list_for_each_entry_rcu(object, &object_list, object_list) {
+ if (need_resched() || !(n % KMEMLEAK_SEQ_RESCHED_INTERVAL))
+ kmemleak_cond_resched(object);
+
if (n-- > 0)
continue;
if (get_object(object))
@@ -2256,6 +2262,9 @@ static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos)
++(*pos);
list_for_each_entry_continue_rcu(obj, &object_list, object_list) {
+ if (need_resched() || !(*pos % KMEMLEAK_SEQ_RESCHED_INTERVAL))
+ kmemleak_cond_resched(obj);
+
if (get_object(obj)) {
next_obj = obj;
break;