Linux cgroups development
 help / color / mirror / Atom feed
From: Muchun Song <muchun.song@linux.dev>
To: bingfangguo@tencent.com
Cc: cgroups@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	Andrew Morton <akpm@linux-foundation.org>,
	Dave Chinner <david@fromorbit.com>, Qi Zheng <qi.zheng@linux.dev>,
	Kairui Song <kasong@tencent.com>, Barry Song <baohua@kernel.org>,
	Axel Rasmussen <axelrasmussen@google.com>,
	Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
	David Hildenbrand <david@kernel.org>,
	Lorenzo Stoakes <ljs@kernel.org>, Bingfang Guo <bfguo@icloud.com>
Subject: Re: [PATCH RFC v2 4/6] mm/memcg: return the memcg when putting memcgid
Date: Sat, 5 Sep 2026 15:28:50 +0800	[thread overview]
Message-ID: <c3bec318-ef9d-4d96-8a98-a68717c17201@linux.dev> (raw)
In-Reply-To: <20260901-bingfangguo-memcgid-rework-v2-4-8edd7f7a7251@tencent.com>



On 2026/9/1 16:58, Bingfang Guo via B4 Relay wrote:
> From: Bingfang Guo <bingfangguo@tencent.com>
>
> __mem_cgroup_uncharge_swap() needs both the memcg and the id refcount
> drop.  Right now it looks the memcg up by id, uncharges it, then looks
> it up again inside mem_cgroup_private_id_put() to drop the reference.
>
> Make mem_cgroup_private_id_put() resolve the id once, drop the
> reference, and return the nearest online memcg with a reference held for
> the caller.  __mem_cgroup_uncharge_swap() then uses that memcg directly
> and drops the reference after uncharging, avoiding the second xarray
> lookup.
>
> Signed-off-by: Bingfang Guo <bingfangguo@tencent.com>
> ---
>   mm/memcontrol.c | 20 +++++++++++++++++---
>   1 file changed, 17 insertions(+), 3 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 048c9bb0fad79..f0503a1e5492d 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4048,14 +4048,28 @@ static void __mem_cgroup_private_id_put(struct mem_cgroup *memcg, unsigned int n
>   	}
>   }
>   
> -static void mem_cgroup_private_id_put(unsigned short id, unsigned int n)
> +/**
> + * mem_cgroup_private_id_put - put memcgid and get the nearest online memcg
> + * @id: the memcg private id got from mem_cgroup_id_get_online
> + * @n: count of references to put
> + */
> +static struct mem_cgroup *mem_cgroup_private_id_put(unsigned short id, unsigned int n)

Having an API that put reference-counted resources return a struct pointer
is a very strange design. Please don't do that.

>   {
>   	struct mem_cgroup *memcg;
>   
>   	rcu_read_lock();
>   	memcg = mem_cgroup_from_private_id(id);
> +	if (!memcg)
> +		goto out;
> +
>   	__mem_cgroup_private_id_put(memcg, n);
> +
> +	while (memcg_is_dying(memcg) || !mem_cgroup_tryget(memcg))
> +		memcg = parent_mem_cgroup(memcg);
> +
> +out:
>   	rcu_read_unlock();
> +	return memcg;
>   }
>   
>   static void mem_cgroup_private_id_kill(struct mem_cgroup *memcg)
> @@ -5816,7 +5830,7 @@ void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages)
>   	struct mem_cgroup *memcg;
>   
>   	rcu_read_lock();
> -	memcg = mem_cgroup_from_private_id(id);

I think we can introduce a new helper like obj_cgroup_from_private_id(),
We can use the ID to get the corresponding obj_cgroup, and then get the
mem_cgroup.

Muhcun,
Thanks.
> +	memcg = mem_cgroup_private_id_put(id, nr_pages);
>   	if (memcg) {
>   		if (!mem_cgroup_is_root(memcg)) {
>   			if (do_memsw_account())
> @@ -5825,10 +5839,10 @@ void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages)
>   				page_counter_uncharge(&memcg->swap, nr_pages);
>   		}
>   		mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
> -		mem_cgroup_private_id_put(id, nr_pages);
>   	}
>   	rcu_read_unlock();
>   
> +	mem_cgroup_put(memcg);
>   }
>   
>   long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
>


  parent reply	other threads:[~2026-09-05  7:29 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-01  8:57 [PATCH RFC v2 0/6] mm/memcg: move memcgid refcount to objcg to unpin dying memcgs Bingfang Guo via B4 Relay
2026-09-01  8:58 ` [PATCH RFC v2 1/6] mm/memcg: add a helper to kill the memcgid on offlining Bingfang Guo via B4 Relay
2026-09-01  8:58 ` [PATCH RFC v2 2/6] mm/memcg: get memcgid reference only after swap charging success Bingfang Guo via B4 Relay
2026-09-01 16:15   ` Bingfang Guo
2026-09-05  7:12   ` Muchun Song
2026-09-05 19:35     ` Bingfang Guo
2026-09-01  8:58 ` [PATCH RFC v2 3/6] mm/memcg: pass the id itself instead of memcg for putting ID Bingfang Guo via B4 Relay
2026-09-05 10:01   ` Muchun Song
2026-09-05 19:48     ` Bingfang Guo
2026-09-01  8:58 ` [PATCH RFC v2 4/6] mm/memcg: return the memcg when putting memcgid Bingfang Guo via B4 Relay
2026-09-01 15:58   ` Bingfang Guo
2026-09-05  7:28   ` Muchun Song [this message]
2026-09-05 19:36     ` Bingfang Guo
2026-09-01  8:58 ` [PATCH RFC v2 5/6] mm/memcg: move memcg private ID refcount to objcg Bingfang Guo via B4 Relay
2026-09-05  8:46   ` Muchun Song
2026-09-05 19:44     ` Bingfang Guo
2026-09-01  8:58 ` [PATCH RFC v2 6/6] mm/memcg: filter out reparented memcgs got from memcgid Bingfang Guo via B4 Relay
2026-09-02  3:53   ` Bingfang Guo
2026-09-05 12:18   ` Muchun Song
2026-09-05 19:45     ` Bingfang Guo

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=c3bec318-ef9d-4d96-8a98-a68717c17201@linux.dev \
    --to=muchun.song@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=bfguo@icloud.com \
    --cc=bingfangguo@tencent.com \
    --cc=cgroups@vger.kernel.org \
    --cc=david@fromorbit.com \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kasong@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=qi.zheng@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=weixugc@google.com \
    --cc=yuanchu@google.com \
    /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