mm/bpf: Always flush memcg stats when explicitly requested
Subsystems (4)
PATCH v3 1/1 mm/bpf: Always flush memcg stats when explicitly requested
memcg_rstat_updated() stops walking the ancestors when a memcg exceeds the flush threshold. A concurrent flush can leave an ancestor below the threshold, so the early exit prevents it from receiving further updates.
Consider root and leaf on a 4-CPU system, with a threshold of 256. CPU 0 is flushing another CPU's stats after CPU 1's stats have already been flushed. The counts below are shared stats_updates counters:
CPU 0 (root flush) CPU 1 (leaf updates)
------------------ --------------------
clear leaf->stats_updates
add 64 to leaf and root
clear root->stats_updates
finish flush
leaf = 64, root = 0 4 more batches of 64:
leaf = 320, root = 256
next update:
leaf > threshold break; root is not updated
Root remains at the threshold, which is insufficient to trigger a flush, while further leaf updates keep taking the early exit. The periodic forced flush restores progress, but until then the root's aggregated statistics can fall far behind. For example, page cache grows at rate of 1 GiB/s, a two-second wait can leave the reported usage about 2 GiB below the actual usage.
Make bpf_mem_cgroup_flush_stats() force a flush without waiting for the periodic flusher, while preserving the memcg flush bookkeeping.
---
Changes since v2:
- Add mem_cgroup_flush_stats_force() and use it in the BPF kfunc to preserve the memcg flush bookkeeping.
v2: https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Make the BPF kfunc flush unconditionally, as suggested by Shakeel.
- Leave memcg_rstat_updated() unchanged.
v1: https://lore.kernel.org/all/[email protected]/
include/linux/memcontrol.h | 5 +++++ mm/bpf_memcontrol.c | 5 +++-- mm/memcontrol.c | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +5 -0
@@ -988,6 +988,7 @@ void mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, int val);void mem_cgroup_flush_stats (struct mem_cgroup *memcg); +void mem_cgroup_flush_stats_force (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);
@@ -1464,6 +1465,10 @@ static inline void mem_cgroup_flush_stats(struct mem_cgroup *memcg) { } +static inline void mem_cgroup_flush_stats_force (struct mem_cgroup *memcg) +{ +} +static inline void mem_cgroup_flush_stats_ratelimited (struct mem_cgroup *memcg) { }
mm/bpf_memcontrol.c +3 -2
@@ -154,11 +154,12 @@ __bpf_kfunc unsigned long bpf_mem_cgroup_page_state(struct mem_cgroup *memcg, in* bpf_mem_cgroup_flush_stats - Flush memory cgroup's statistics * @memcg: memory cgroup * -* Propagate memory cgroup's statistics up the cgroup tree. +* Flush the memory cgroup subtree and propagate its statistics up the cgroup +* tree, regardless of the number of pending updates. */ __bpf_kfunc void bpf_mem_cgroup_flush_stats(struct mem_cgroup *memcg) { -mem_cgroup_flush_stats (memcg); +mem_cgroup_flush_stats_force (memcg); }/**
mm/memcontrol.c +17 -0
@@ -824,6 +824,23 @@ void mem_cgroup_flush_stats(struct mem_cgroup *memcg)__mem_cgroup_flush_stats (memcg, false); } +/** +* mem_cgroup_flush_stats_force - unconditionally flush memory cgroup statistics +* @memcg: root of the subtree to flush, or NULL for the root memory cgroup +* +* Flush the statistics regardless of the number of pending updates. +*/ +void mem_cgroup_flush_stats_force (struct mem_cgroup *memcg) +{ +if (mem_cgroup_disabled ()) +return ; + +if (!memcg) + memcg = root_mem_cgroup; + +__mem_cgroup_flush_stats (memcg, true); +} +void mem_cgroup_flush_stats_ratelimited (struct mem_cgroup *memcg) {/* Only flush if the periodic flusher is one full cycle late */
Signature · 1 line
2.39.2 (Apple Git-143)
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm/bpf: single-word nickname in Author and Signed-off-by trailer --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/bpf: single-word nickname in Author and Signed-off-by trailer
--
commit 4c2af3a1c095e013e48cf708056b8f9ea3fe242a
Author: Runli <[email protected]>
Quoted 30 lines
mm/bpf: Always flush memcg stats when explicitly requested
memcg_rstat_updated()stops walking the ancestors when a memcg exceeds the flush threshold. A concurrent flush can leave an ancestor below the threshold, so the early exit prevents it from receiving further updates.Consider root and leaf on a 4-CPU system, with a threshold of 256. CPU 0 is flushing another CPU's stats after CPU 1's stats have already been flushed. The counts below are shared
stats_updatescounters:CPU 0 (root flush) CPU 1 (leaf updates)
------------------ --------------------
clearleaf->stats_updates
add 64 to leaf and root
clearroot->stats_updates
finish flush
leaf = 64, root = 0 4 more batches of 64:
leaf = 320, root = 256
next update:
leaf > threshold break; root is not updatedRoot remains at the threshold, which is insufficient to trigger a flush, while further leaf updates keep taking the early exit. The periodic forced flush restores progress, but until then the root's aggregated statistics can fall far behind. For example, page cache grows at rate of 1 GiB/s, a two-second wait can leave the reported usage about 2 GiB below the actual usage.
Quoted 6 lines
Make
bpf_mem_cgroup_flush_stats()force a flush without waiting for the periodic flusher, while preserving the memcg flush bookkeeping.Suggested-by Shakeel Butt <[email protected]>Signed-off-by Runli <[email protected]>
[Severity: High] Documentation/process/submitting-patches.rst requires a full real name rather than a single-word nickname or handle in the Author header and Signed-off-by trailer (for example, Mingyu He <[email protected]>).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 4 lines · On Fri, Nov 13, 2026 at 12:14 PM <[email protected]> wrote:
[Severity: High]
Documentation/process/submitting-patches.rstrequires a full real name rather than a single-word nickname or handle in the Author header and Signed-off-by trailer (for example, Mingyu He <[email protected]>).
According to Documentation/process/submitting-patches.rst line 439,
quote: 'using a known identity (sorry, no anonymous contributions.),' and commit
'Documentation: simplify and clarify DCO contribution example language', This is my identity, and it is acceptable.
Hi, I wanna gently pin this email, thanks.
Runli
hi, gently pin this email, thanks!
Quoted 28 lines · Runli
memcg_rstat_updated()stops walking the ancestors when a memcg exceeds the flush threshold. A concurrent flush can leave an ancestor below the threshold, so the early exit prevents it from receiving further updates.Consider root and leaf on a 4-CPU system, with a threshold of 256. CPU 0 is flushing another CPU's stats after CPU 1's stats have already been flushed. The counts below are shared
stats_updatescounters:CPU 0 (root flush) CPU 1 (leaf updates)
------------------ --------------------
clearleaf->stats_updates
add 64 to leaf and root
clearroot->stats_updates
finish flush
leaf = 64, root = 0 4 more batches of 64:
leaf = 320, root = 256
next update:
leaf > threshold break; root is not updatedRoot remains at the threshold, which is insufficient to trigger a flush, while further leaf updates keep taking the early exit. The periodic forced flush restores progress, but until then the root's aggregated statistics can fall far behind. For example, page cache grows at rate of 1 GiB/s, a two-second wait can leave the reported usage about 2 GiB below the actual usage.
Quoted 6 lines
Make
bpf_mem_cgroup_flush_stats()force a flush without waiting for the periodic flusher, while preserving the memcg flush bookkeeping.Suggested-by Shakeel Butt <[email protected]>Signed-off-by Runli <[email protected]>