Re: [PATCH v2] mm/bpf: Always flush memcg stats when explicitly requested

From: Shakeel Butt

Date: Wed Nov 11 2026 - 11:07:11 EST


On Wed, Nov 11, 2026 at 03:14:34PM +0800, Runli wrote:
> 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() bypass the update threshold so BPF
> callers can explicitly flush stale statistics without waiting for the
> periodic flush.
>
> Suggested-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
> Link: https://lore.kernel.org/all/avKy9oMwDRjKjfzm@xxxxxxxxx/
> Signed-off-by: Runli <mingyu.he@xxxxxxxxxx>
> ---
> Changes since v1:
> - Make the BPF kfunc flush unconditionally, as suggested by Shakeel.
> - Leave memcg_rstat_updated() unchanged.
>
> v1: https://lore.kernel.org/all/20260911032905.63683-1-mingyu.he@xxxxxxxxxx/
>
> mm/bpf_memcontrol.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/mm/bpf_memcontrol.c b/mm/bpf_memcontrol.c
> index d8f579c28560..f8552651b6d6 100644
> --- a/mm/bpf_memcontrol.c
> +++ b/mm/bpf_memcontrol.c
> @@ -7,6 +7,7 @@
>
> #include <linux/memcontrol.h>
> #include <linux/bpf.h>
> +#include <linux/cgroup.h>
>
> #include "internal.h"
>
> @@ -154,11 +155,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);
> + css_rstat_flush(&memcg->css);

Please expose mem_cgroup_flush_stats_force(memcg) which call
__mem_cgroup_flush_stats(memcg, true) or something like that. We have some
bookkeeping in __mem_cgroup_flush_stats() which we should not skip.

> }
>
> /**
> --
> 2.39.2 (Apple Git-143)
>