[PATCH v2] mm: shrinker: stop counting when the seq buffer overflows

From: Xuewen Wang

Date: Mon Nov 23 2026 - 00:52:05 EST


From: Xuewen Wang <wangxuewen@xxxxxxxxxx>

The shrinker debugfs count file uses single_open(), so all memcg rows
belong to one seq_file record. When the buffer overflows, seq_file grows
it and retries the entire record. Queries for subsequent memcgs in
an overflowing pass cannot contribute to the final output.

Stop the traversal once seq_has_overflowed() is true, using the existing
mem_cgroup_iter_break() path to release the current memcg reference.
Check for pending signals first to preserve the existing -EINTR
priority. Leave the overflow state intact and return zero on overflow
so seq_file can grow the buffer and retry.

Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Changes in v2:
- Move the overflow check after signal_pending(), as suggested by Qi
Zheng, preserving the original -EINTR priority.

Retested in a single-node KVM guest with 1024 cgroups each caching 16
ordinary files on ext4. Output remained 8865 bytes with three show
attempts. Count callbacks fell from 3075 to 2472 (19.6%). Six fresh
guests per variant each performed 101 full reads with no probes
registered. The median of guest medians fell from 127.8 us to 114.0 us
(10.7%, about 13.7 us per read).

Output matched byte-for-byte across the tested guests. Small reads,
seek-to-zero rereads, populated memcg teardown and signal stress passed.
Separate probe runs had no missed or dropped events. Small/sparse cases
had unchanged callbacks. The 64-cgroup baseline timings were bimodal,
so their apparent timing difference is not attributed to this change.

mm/shrinker_debug.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/mm/shrinker_debug.c b/mm/shrinker_debug.c
index cafb566301326..eabdb33353a45 100644
--- a/mm/shrinker_debug.c
+++ b/mm/shrinker_debug.c
@@ -84,6 +84,11 @@ static int shrinker_debugfs_count_show(struct seq_file *m, void *v)
ret = -EINTR;
break;
}
+
+ if (seq_has_overflowed(m)) {
+ mem_cgroup_iter_break(NULL, memcg);
+ break;
+ }
} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);

kfree(count_per_node);

--
2.53.0