Re: [PATCH mm-new v2] mm/memcontrol: Flush stats when write stat file

From: Leon Huang Fu

Date: Wed Nov 05 2025 - 03:52:23 EST


On Wed, Nov 5, 2025 at 4:39 PM Lance Yang <lance.yang@xxxxxxxxx> wrote:
> On 2025/11/5 16:19, Michal Hocko wrote:
> > On Wed 05-11-25 15:49:16, Leon Huang Fu wrote:
> >> diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
> >> index 6eed14bff742..8cab6b52424b 100644
> >> --- a/mm/memcontrol-v1.c
> >> +++ b/mm/memcontrol-v1.c
> >> @@ -2040,6 +2040,7 @@ struct cftype mem_cgroup_legacy_files[] = {
> >> {
> >> .name = "stat",
> >> .seq_show = memory_stat_show,
> >> + .write_u64 = memory_stat_write,
> >> },
> >> {
> >> .name = "force_empty",
> >> @@ -2078,6 +2079,7 @@ struct cftype mem_cgroup_legacy_files[] = {
> >> {
> >> .name = "numa_stat",
> >> .seq_show = memcg_numa_stat_show,
> >> + .write_u64 = memory_stat_write,
> >> },
> >
> > Any reason you are not using .write like others? Also is there any
> > reason why a specific value is required. /proc/sys/vm/stat_refresh which does
> > something similar ignores the value. Also memcg.peak write handler which
> > resets the peak value ignores it. It is true that a specific value
> > allows for future extensions but I guess it would be better to be
> > consistent with others here.
> >
> > One last thing to consider is whether this should follow
> > /proc/sys/vm/stat_refresh path and have a single file to flush them all
> > or have a per file flushing. I do not have a strong preference but
> > considering both are doing the same thing it makes sense to go
> > stat_refresh path.
>
> +1
>
> IMHO, a dedicated file like memory.stat_refresh is a much better approach ;)
>
> It's cleaner, simpler to use, and much more intuitive for users.
>

Agreed. Thank you both for the feedback.

You're right that following the /proc/sys/vm/stat_refresh pattern makes
more sense here. A dedicated memory.stat_refresh file has several advantages:

1) It provides a clear, explicit interface for the refresh operation rather
than overloading existing stat files with write capability
2) It's more consistent with the existing kernel patterns - stat_refresh
ignores the written value, and memory.peak also ignores it for reset
3) It's more intuitive for users - the purpose is immediately clear from
the filename

For the next revision, I'll introduce a dedicated memory.stat_refresh file
that ignores the written value (similar to stat_refresh and memory.peak).
This will work for both cgroup v1 and v2.

Thanks,
Leon

[...]