Re: [PATCH] memcg: keep propagating stats updates to ancestors
From: Shakeel Butt
Date: Fri Sep 11 2026 - 15:37:07 EST
On Fri, Sep 11, 2026 at 11:29:05AM +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.
>
> Skip only the flushable node and continue walking its ancestors, so each
> ancestor can accumulate updates until it exceeds the flush threshold.
>
> Fixes: 60cada258dfe ("memcg: optimize memcg_rstat_updated")
> Signed-off-by: Runli <mingyu.he@xxxxxxxxxx>
> ---
> Testing:
> Based on commit 893e11787f78, with and without this patch,
> using 32 concurrent instances of:
> netperf -H 127.0.0.1 -p 12875 -t TCP_STREAM -l 60 \
> -T <client_cpu>,<server_cpu> -P 0 -v 0 -f m -- -m 1024
>
> Both netperf and netserver ran in the same leaf memcg, directly below
> root (root -> leaf), with memory bound to NUMA node 0.
> Each kernel was tested for five 60-second runs after a 10-second warmup.
>
> Median aggregate throughput decreased from 107.26 to 105.23 Gbit/s
> (1.9%) with this change.
>
> I think fixing the correctness issue is worth the roughly 2% throughput
> cost.
Do you have any real world or production data to show that this is an issue? I
would not change anything unless there is real data. Also we have periodic flush
which will fix this (I have not looked deeper into this to see if this is a real
issue). In addition we have bpf interface for the stats for users who really
care about accuracy and efficiency.