Re: [PATCH mm-new v2] mm/memcontrol: Flush stats when write stat file

From: JP Kobryn

Date: Thu Nov 06 2025 - 12:02:45 EST


On 11/4/25 11:49 PM, Leon Huang Fu wrote:
On high-core count systems, memory cgroup statistics can become stale
due to per-CPU caching and deferred aggregation. Monitoring tools and
management applications sometimes need guaranteed up-to-date statistics
at specific points in time to make accurate decisions.

This patch adds write handlers to both memory.stat and memory.numa_stat
files to allow userspace to explicitly force an immediate flush of
memory statistics. When "1" is written to either file, it triggers
__mem_cgroup_flush_stats(memcg, true), which unconditionally flushes
all pending statistics for the cgroup and its descendants.

The write operation validates the input and only accepts the value "1",
returning -EINVAL for any other input.

Usage example:
# Force immediate flush before reading critical statistics
echo 1 > /sys/fs/cgroup/mygroup/memory.stat
cat /sys/fs/cgroup/mygroup/memory.stat

This provides several benefits:

1. On-demand accuracy: Tools can flush only when needed, avoiding
continuous overhead

2. Targeted flushing: Allows flushing specific cgroups when precision
is required for particular workloads

I'm curious about your use case. Since you mention required precision,
are you planning on manually flushing before every read?


3. Integration flexibility: Monitoring scripts can decide when to pay
the flush cost based on their specific accuracy requirements

[...]
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index c34029e92bab..d6a5d872fbcb 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4531,6 +4531,17 @@ int memory_stat_show(struct seq_file *m, void *v)
return 0;
}

+int memory_stat_write(struct cgroup_subsys_state *css, struct cftype *cft, u64 val)
+{
+ if (val != 1)
+ return -EINVAL;
+
+ if (css)
+ css_rstat_flush(css);

This is a kfunc. You can do this right now from a bpf program without
any kernel changes.