Skip to contentSkip to conversation

mm/memcontrol: Introduce sysctl vm.memcg_stats_flush_threshold

PATCH In review Leon Huang Fu 2 participants 3 messages Nov 4, 2025 – Nov 5, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/memcontrol: Introduce sysctl vm.memcg_stats_flush_threshold

The current implementation uses a flush threshold calculated as MEMCG_CHARGE_BATCH * num_online_cpus() for determining when to aggregate per-CPU memory cgroup statistics. On systems with high core counts, this threshold can become very large (e.g., 64 * 256 = 16,384 on a 256-core system), leading to stale statistics when userspace reads memory.stat files.

This is particularly problematic for monitoring and management tools that rely on reasonably fresh statistics, as they may observe data that is thousands of updates out of date.

Introduce a new sysctl, vm.memcg_stats_flush_threshold, that allows administrators to override the flush threshold specifically for userspace reads of memory.stat. When set to 0 (default), the behavior remains unchanged, using the automatic calculation. When set to a non-zero value, userspace reads will use the custom threshold for more frequent flushing.

Importantly, this change only affects userspace paths. Internal kernel paths continue to use the default threshold (or ratelimited flushing) to maintain optimal performance. This is achieved by:

- Introducing mem_cgroup_flush_stats_user() for userspace reads
- Keeping mem_cgroup_flush_stats() unchanged for kernel internal paths
- Updating memory.stat read paths to use mem_cgroup_flush_stats_user()

The implementation adds comprehensive documentation in Documentation/admin-guide/sysctl/vm.rst explaining the use cases, examples for different system configurations, and the distinction between userspace and kernel flush behaviors.

---
 Documentation/admin-guide/sysctl/vm.rst | 48 ++++++++++++++
 include/linux/memcontrol.h              |  1 +
 mm/memcontrol-v1.c                      |  4 +-
 mm/memcontrol.c                         | 86 +++++++++++++++++++++----
 4 files changed, 124 insertions(+), 15 deletions(-)
Documentation/admin-guide/sysctl/vm.rst +48 -0
index ace73480eb9d..f40c629413ea 100644
@@ -46,6 +46,7 @@ Currently, these files are in /proc/sys/vm:
 - lowmem_reserve_ratio
 - max_map_count
 - mem_profiling         (only if CONFIG_MEM_ALLOC_PROFILING=y)
+- memcg_stats_flush_threshold
 - memory_failure_early_kill
 - memory_failure_recovery
 - min_free_kbytes
@@ -515,6 +516,53 @@ memory allocations.
 The default value depends on CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT.
 
 
+memcg_stats_flush_threshold
+============================
+
+Control the threshold for flushing memory cgroup statistics when reading
+memory.stat from userspace. Memory cgroup stats are updated frequently in
+per-CPU counters, but these updates need to be periodically aggregated
+(flushed) to provide accurate statistics.
+
+**Important**: This setting ONLY affects userspace reads of memory.stat files.
+Internal kernel paths continue to use the default threshold (or ratelimited
+flushing) to maintain optimal performance in latency-sensitive code paths.
+
+When set to 0 (default), userspace reads use the automatic threshold:
+MEMCG_CHARGE_BATCH * num_online_cpus()
+
+This means on systems with many CPU cores, the threshold can become very high
+(e.g., 64 * 256 = 16,384 updates on a 256-core system), potentially resulting
+in stale statistics when reading memory.stat.
+
+Setting this to a non-zero value overrides the automatic calculation for
+userspace reads only. Lower values result in fresher statistics when reading
+memory.stat but may increase overhead due to more frequent flushing.
+
+Examples:
+
+- On a 256-core system with default (0):
+  Userspace reads use threshold = 64 * 256 = 16,384 updates
+  Internal kernel paths use default thresholds (unaffected)
+
+- Setting to 2048:
+  Userspace reads use threshold = 2,048 updates (much fresher stats)
+  Internal kernel paths use default thresholds (performance maintained)
+
+- Setting to 1024:
+  Userspace reads use threshold = 1,024 updates (even fresher stats)
+  Internal kernel paths use default thresholds (performance maintained)
+
+Note: Memory cgroup statistics are also flushed automatically every 2 seconds
+regardless of this threshold.
+
+Recommended for systems with high core counts where the default threshold
+results in statistics that are too stale for monitoring or management tools,
+while keeping internal kernel operations performant.
+
+Default: 0 (auto-calculate based on CPU count)
+
+
 memory_failure_early_kill
 =========================
 
include/linux/memcontrol.h +1 -0
index 8d2e250535a8..208895e6cf14 100644
@@ -955,6 +955,7 @@ unsigned long lruvec_page_state_local(struct lruvec *lruvec,
 				      enum node_stat_item idx);
 
 void mem_cgroup_flush_stats(struct mem_cgroup *memcg);
+void mem_cgroup_flush_stats_user(struct mem_cgroup *memcg);
 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg);
 
 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
mm/memcontrol-v1.c +2 -2
index 6eed14bff742..3eeb20f6c5ad 100644
@@ -1792,7 +1792,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
 	int nid;
 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
 
-	mem_cgroup_flush_stats(memcg);
+	mem_cgroup_flush_stats_user(memcg);
 
 	for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
 		seq_printf(m, "%s=%lu", stat->name,
@@ -1873,7 +1873,7 @@ void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
 
 	BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
 
-	mem_cgroup_flush_stats(memcg);
+	mem_cgroup_flush_stats_user(memcg);
 
 	for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
 		unsigned long nr;
mm/memcontrol.c +73 -13

144 lines not on this page · raw message

Signature · 1 line
2.51.2
reviewer
#1 lore raw
Quoted 11 lines · Leon Huang Fu

The current implementation uses a flush threshold calculated as MEMCG_CHARGE_BATCH * num_online_cpus() for determining when to aggregate per-CPU memory cgroup statistics. On systems with high core counts, this threshold can become very large (e.g., 64 * 256 = 16,384 on a 256-core system), leading to stale statistics when userspace reads memory.stat files.

This is particularly problematic for monitoring and management tools that rely on reasonably fresh statistics, as they may observe data that is thousands of updates out of date.

Quoted 6 lines

Introduce a new sysctl, vm.memcg_stats_flush_threshold, that allows administrators to override the flush threshold specifically for userspace reads of memory.stat. When set to 0 (default), the behavior remains unchanged, using the automatic calculation. When set to a non-zero value, userspace reads will use the custom threshold for more frequent flushing.

How are admins supposed to know how to tune this? Wouldn't it make more sense to allow explicit flushing on write to the file? That would allow admins to implement their preferred accuracy tuning by writing to the file when the precision is required.

Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 20 lines · Michal Hocko
Quoted 11 lines · Leon Huang Fu

The current implementation uses a flush threshold calculated as MEMCG_CHARGE_BATCH * num_online_cpus() for determining when to aggregate per-CPU memory cgroup statistics. On systems with high core counts, this threshold can become very large (e.g., 64 * 256 = 16,384 on a 256-core system), leading to stale statistics when userspace reads memory.stat files.

This is particularly problematic for monitoring and management tools that rely on reasonably fresh statistics, as they may observe data that is thousands of updates out of date.

Quoted 6 lines

Introduce a new sysctl, vm.memcg_stats_flush_threshold, that allows administrators to override the flush threshold specifically for userspace reads of memory.stat. When set to 0 (default), the behavior remains unchanged, using the automatic calculation. When set to a non-zero value, userspace reads will use the custom threshold for more frequent flushing.

Quoted 4 lines

How are admins supposed to know how to tune this? Wouldn't it make more sense to allow explicit flushing on write to the file? That would allow admins to implement their preferred accuracy tuning by writing to the file when the precision is required.

Thank you for the feedback. Let me clarify the use case and design rationale.

The threshold approach is intended for scenarios where administrators want to improve accuracy for existing monitoring tools on high core-count systems. On such systems, the default threshold (MEMCG_CHARGE_BATCH * num_cpus) can reach 16K+ updates, causing monitoring dashboards to display stale data.

Regarding tunability: while the exact threshold value requires some understanding, the principle is straightforward - lower values mean fresher stats but higher overhead. Administrators can start conservatively (e.g., 1/4 of the default: num_cpus * 16) and adjust based on observed overhead.

Your suggestion about allowing writes to memory.stat to trigger explicit flushing is interesting. Comparing the two approaches:

- Threshold (this patch):
- Administrator sets once system-wide via sysctl
- Affects all memory.stat reads automatically
- Tradeoff: harder to tune, always-on overhead

- Write-to-flush (your suggestion):
- Tools write to memory.stat before reading: echo 1 > memory.stat
- Per-cgroup, on-demand control
- Tradeoff: requires tool modifications, but more precise control

Actually, your approach may be more elegant - tools pay the flush cost only when they need accuracy, rather than imposing a system-wide policy. The write-to-flush pattern is also more discoverable and self-documenting.

Let me try your approach in the next revision.

Thanks,
Leon

Quoted 4 lines

--
Michal Hocko
SUSE Labs