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

From: Qi Zheng

Date: Sat Nov 21 2026 - 06:10:52 EST




On 11/20/26 7:05 PM, Xuewen Wang wrote:
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.
Leave the overflow state intact and return zero so seq_file can retry.

Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
In a single-node KVM guest with 1024 cgroups each caching 16 ordinary
files on ext4, the 8865-byte output required three show attempts. Count
callbacks fell from 3075 to 2472 (19.6%). With probes disabled, six fresh
guests per variant each performed 101 full reads. The median of guest
medians fell from 128.2 us to 112.2 us (12.4%, about 16 us per read).
Small and sparse outputs had unchanged callback counts. Complete output,
small reads and seek-to-zero rereads matched; concurrent memcg deletion
and signal interruption checks also passed.

The successful pass still traverses the full list. Early exit skips
count queries for subsequent memcgs in overflowing passes.

When overflow coincides with a pending signal, the original code can
return -EINTR in the current pass. With early exit, seq_file attempts to
grow the buffer first. The retry can return -EINTR, or buffer allocation
can fail first with -ENOMEM.

Would something like the following work instead?

if (!memcg_aware) {
mem_cgroup_iter_break(NULL, memcg);
break;
}

if (signal_pending(current)) {
mem_cgroup_iter_break(NULL, memcg);
ret = -EINTR;
break;
}

+ if (seq_has_overflowed(m)) {
+ mem_cgroup_iter_break(NULL, memcg);
+ break;
+ }
} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);

Thanks,
Qi


mm/shrinker_debug.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/shrinker_debug.c b/mm/shrinker_debug.c
index cafb566301326..3022d103b0b1e 100644
--- a/mm/shrinker_debug.c
+++ b/mm/shrinker_debug.c
@@ -74,7 +74,7 @@ static int shrinker_debugfs_count_show(struct seq_file *m, void *v)
seq_putc(m, '\n');
}
- if (!memcg_aware) {
+ if (!memcg_aware || seq_has_overflowed(m)) {
mem_cgroup_iter_break(NULL, memcg);
break;
}