Re: [PATCH v2 1/2] memcg: introduce kfuncs for fetching memcg stats

From: Song Liu

Date: Wed Oct 15 2025 - 19:12:20 EST


On Wed, Oct 15, 2025 at 12:08 PM JP Kobryn <inwardvessel@xxxxxxxxx> wrote:
>
[...]
> ---
> mm/memcontrol.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 67 insertions(+)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 4deda33625f4..6547c27d4430 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -871,6 +871,73 @@ unsigned long memcg_events_local(struct mem_cgroup *memcg, int event)
> }
> #endif
>
> +static inline struct mem_cgroup *memcg_from_cgroup(struct cgroup *cgrp)
> +{
> + return cgrp ? mem_cgroup_from_css(cgrp->subsys[memory_cgrp_id]) : NULL;
> +}
> +

We should add __bpf_kfunc_start_defs() here, and __bpf_kfunc_end_defs()
after all the kfuncs.

> +__bpf_kfunc static void memcg_flush_stats(struct cgroup *cgrp)

We mostly do not make kfunc static, but it seems to also work.

> +{
> + struct mem_cgroup *memcg = memcg_from_cgroup(cgrp);
> +
> + if (!memcg)
> + return;

Maybe we can let memcg_flush_stats return int, and return -EINVAL
on memcg == NULL cases?

> +
> + mem_cgroup_flush_stats(memcg);
> +}
> +
[...]