Re: [RFC PATCH bpf-next 03/10] mm, memcg: Add new helper obj_cgroup_from_current()

Roman Gushchin
2022-06-23T04:41:27Z

On Sun, Jun 19, 2022 at 03:50:25PM +0000, Yafang Shao wrote:
> The difference between get_obj_cgroup_from_current() and obj_cgroup_from_current()
> is that the later one doesn't add objcg's refcnt.
>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
> include/linux/memcontrol.h | 1 +
> mm/memcontrol.c | 24 ++++++++++++++++++++++++
> 2 files changed, 25 insertions(+)
>
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index cf074156c6ac..402b42670bcd 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -1703,6 +1703,7 @@ bool mem_cgroup_kmem_disabled(void);
> int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
> void __memcg_kmem_uncharge_page(struct page *page, int order);
>
> +struct obj_cgroup *obj_cgroup_from_current(void);
> struct obj_cgroup *get_obj_cgroup_from_current(void);
> struct obj_cgroup *get_obj_cgroup_from_page(struct page *page);
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index abec50f31fe6..350a7849dac3 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2950,6 +2950,30 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page)
> return objcg;
> }
>
> +__always_inline struct obj_cgroup *obj_cgroup_from_current(void)
> +{
> + struct obj_cgroup *objcg = NULL;
> + struct mem_cgroup *memcg;
> +
> + if (memcg_kmem_bypass())
> + return NULL;
> +
> + rcu_read_lock();
> + if (unlikely(active_memcg()))
> + memcg = active_memcg();
> + else
> + memcg = mem_cgroup_from_task(current);
> +
> + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
> + objcg = rcu_dereference(memcg->objcg);
> + if (objcg)
> + break;
> + }
> + rcu_read_unlock();

Hm, what prevents the objcg from being released here? Under which conditions
it's safe to call it?