From: Waiman Long <llong@redhat.com>
To: Leon Huang Fu <leon.huangfu@shopee.com>, linux-mm@kvack.org
Cc: tj@kernel.org, mkoutny@suse.com, hannes@cmpxchg.org,
mhocko@kernel.org, roman.gushchin@linux.dev,
shakeel.butt@linux.dev, muchun.song@linux.dev,
akpm@linux-foundation.org, joel.granados@kernel.org,
jack@suse.cz, laoar.shao@gmail.com, mclapinski@google.com,
kyle.meyer@hpe.com, corbet@lwn.net, lance.yang@linux.dev,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
cgroups@vger.kernel.org
Subject: Re: [PATCH mm-new v3] mm/memcontrol: Add memory.stat_refresh for on-demand stats flushing
Date: Tue, 11 Nov 2025 14:10:28 -0500 [thread overview]
Message-ID: <9a9a2ede-af6e-413a-97a0-800993072b22@redhat.com> (raw)
In-Reply-To: <20251110101948.19277-1-leon.huangfu@shopee.com>
On 11/10/25 5:19 AM, Leon Huang Fu wrote:
> Memory cgroup statistics are updated asynchronously with periodic
> flushing to reduce overhead. 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 write-only file, memory.stat_refresh, that allows
> userspace to explicitly trigger an immediate flush of memory statistics.
> Writing any value to this file forces a synchronous flush via
> __mem_cgroup_flush_stats(memcg, true) for the cgroup and all its
> descendants, ensuring that subsequent reads of memory.stat and
> memory.numa_stat reflect current data.
>
> This approach follows the pattern established by /proc/sys/vm/stat_refresh
> and memory.peak, where the written value is ignored, keeping the
> interface simple and consistent with existing kernel APIs.
>
> Usage example:
> echo 1 > /sys/fs/cgroup/mygroup/memory.stat_refresh
> cat /sys/fs/cgroup/mygroup/memory.stat
>
> The feature is available in both cgroup v1 and v2 for consistency.
>
> Signed-off-by: Leon Huang Fu <leon.huangfu@shopee.com>
> ---
> v2 -> v3:
> - Flush stats by memory.stat_refresh (per Michal)
> - https://lore.kernel.org/linux-mm/20251105074917.94531-1-leon.huangfu@shopee.com/
>
> v1 -> v2:
> - Flush stats when write the file (per Michal).
> - https://lore.kernel.org/linux-mm/20251104031908.77313-1-leon.huangfu@shopee.com/
>
> Documentation/admin-guide/cgroup-v2.rst | 21 +++++++++++++++++--
> mm/memcontrol-v1.c | 4 ++++
> mm/memcontrol-v1.h | 2 ++
> mm/memcontrol.c | 27 ++++++++++++++++++-------
> 4 files changed, 45 insertions(+), 9 deletions(-)
>
> diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
> index 3345961c30ac..ca079932f957 100644
> --- a/Documentation/admin-guide/cgroup-v2.rst
> +++ b/Documentation/admin-guide/cgroup-v2.rst
> @@ -1337,7 +1337,7 @@ PAGE_SIZE multiple when read back.
> cgroup is within its effective low boundary, the cgroup's
> memory won't be reclaimed unless there is no reclaimable
> memory available in unprotected cgroups.
> - Above the effective low boundary (or
> + Above the effective low boundary (or
> effective min boundary if it is higher), pages are reclaimed
> proportionally to the overage, reducing reclaim pressure for
> smaller overages.
> @@ -1785,6 +1785,23 @@ The following nested keys are defined.
> up if hugetlb usage is accounted for in memory.current (i.e.
> cgroup is mounted with the memory_hugetlb_accounting option).
>
> + memory.stat_refresh
> + A write-only file which exists on non-root cgroups.
> +
> + Writing any value to this file forces an immediate flush of
> + memory statistics for this cgroup and its descendants. This
> + ensures subsequent reads of memory.stat and memory.numa_stat
> + reflect the most current data.
> +
> + This is useful on high-core count systems where per-CPU caching
> + can lead to stale statistics, or when precise memory usage
> + information is needed for monitoring or debugging purposes.
> +
> + Example::
> +
> + echo 1 > memory.stat_refresh
> + cat memory.stat
> +
> memory.numa_stat
> A read-only nested-keyed file which exists on non-root cgroups.
>
> @@ -2173,7 +2190,7 @@ of the two is enforced.
>
> cgroup writeback requires explicit support from the underlying
> filesystem. Currently, cgroup writeback is implemented on ext2, ext4,
> -btrfs, f2fs, and xfs. On other filesystems, all writeback IOs are
> +btrfs, f2fs, and xfs. On other filesystems, all writeback IOs are
> attributed to the root cgroup.
>
> There are inherent differences in memory and writeback management
> diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
> index 6eed14bff742..c3eac9b1f1be 100644
> --- a/mm/memcontrol-v1.c
> +++ b/mm/memcontrol-v1.c
> @@ -2041,6 +2041,10 @@ struct cftype mem_cgroup_legacy_files[] = {
> .name = "stat",
> .seq_show = memory_stat_show,
> },
> + {
> + .name = "stat_refresh",
> + .write = memory_stat_refresh_write,
> + },
> {
> .name = "force_empty",
> .write = mem_cgroup_force_empty_write,
> diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h
> index 6358464bb416..a14d4d74c9aa 100644
> --- a/mm/memcontrol-v1.h
> +++ b/mm/memcontrol-v1.h
> @@ -29,6 +29,8 @@ void drain_all_stock(struct mem_cgroup *root_memcg);
> unsigned long memcg_events(struct mem_cgroup *memcg, int event);
> unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item);
> int memory_stat_show(struct seq_file *m, void *v);
> +ssize_t memory_stat_refresh_write(struct kernfs_open_file *of, char *buf,
> + size_t nbytes, loff_t off);
>
> void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n);
> struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg);
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index bfc986da3289..19ef4b971d8d 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -610,6 +610,15 @@ static void __mem_cgroup_flush_stats(struct mem_cgroup *memcg, bool force)
> css_rstat_flush(&memcg->css);
> }
>
> +static void memcg_flush_stats(struct mem_cgroup *memcg, bool force)
> +{
> + if (mem_cgroup_disabled())
> + return;
> +
> + memcg = memcg ?: root_mem_cgroup;
> + __mem_cgroup_flush_stats(memcg, force);
> +}
Shouldn't we impose a limit in term of how frequently this
memcg_flush_stats() function can be called like at most a few times per
second to prevent abuse from user space as stat flushing is expensive?
We should prevent some kind of user space DoS attack by using this new
API if we decide to implement it.
Cheers,
Longman
next prev parent reply other threads:[~2025-11-11 19:10 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-10 10:19 [PATCH mm-new v3] mm/memcontrol: Add memory.stat_refresh for on-demand stats flushing Leon Huang Fu
2025-11-10 11:28 ` Michal Hocko
2025-11-11 6:12 ` Leon Huang Fu
2025-11-10 11:52 ` Harry Yoo
2025-11-11 6:12 ` Leon Huang Fu
2025-11-10 13:50 ` Michal Koutný
2025-11-10 16:04 ` Tejun Heo
2025-11-11 6:27 ` Leon Huang Fu
2025-11-11 1:00 ` Chen Ridong
2025-11-11 6:44 ` Leon Huang Fu
2025-11-12 0:56 ` Chen Ridong
2025-11-12 14:02 ` Michal Koutný
2025-11-11 6:13 ` Leon Huang Fu
2025-11-11 18:52 ` Tejun Heo
2025-11-11 19:01 ` Michal Koutný
2025-11-11 8:10 ` Michal Hocko
2025-11-11 19:10 ` Waiman Long [this message]
2025-11-11 19:47 ` Michal Hocko
2025-11-11 20:44 ` Waiman Long
2025-11-11 21:01 ` Michal Hocko
2025-11-12 14:02 ` Michal Koutný
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9a9a2ede-af6e-413a-97a0-800993072b22@redhat.com \
--to=llong@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=hannes@cmpxchg.org \
--cc=jack@suse.cz \
--cc=joel.granados@kernel.org \
--cc=kyle.meyer@hpe.com \
--cc=lance.yang@linux.dev \
--cc=laoar.shao@gmail.com \
--cc=leon.huangfu@shopee.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mclapinski@google.com \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).