From: Muchun Song <songmuchun@bytedance.com>
To: Chen Ridong <chenridong@huaweicloud.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
muchun.song@linux.dev, Andrew Morton <akpm@linux-foundation.org>,
Dave Chinner <david@fromorbit.com>,
Qi Zheng <zhengqi.arch@bytedance.com>,
yosry.ahmed@linux.dev, Nhat Pham <nphamcs@gmail.com>,
chengming.zhou@linux.dev, LKML <linux-kernel@vger.kernel.org>,
Cgroups <cgroups@vger.kernel.org>,
Linux Memory Management List <linux-mm@kvack.org>,
hamzamahfooz@linux.microsoft.com, apais@linux.microsoft.com
Subject: Re: Re: [PATCH RFC 10/28] mm: memcontrol: return root object cgroup for root memory cgroup
Date: Mon, 30 Jun 2025 15:16:18 +0800 [thread overview]
Message-ID: <CAMZfGtX_Ft=OpThZC0vp2TdXxymK-AV6HTyinVhRBJrk6ZkUfA@mail.gmail.com> (raw)
In-Reply-To: <d84092a7-fc3d-4c3c-98b3-341d63a21b18@huaweicloud.com>
On Sat, Jun 28, 2025 at 11:09 AM Chen Ridong <chenridong@huaweicloud.com> wrote:
>
>
>
> On 2025/4/15 10:45, Muchun Song wrote:
> > Memory cgroup functions such as get_mem_cgroup_from_folio() and
> > get_mem_cgroup_from_mm() return a valid memory cgroup pointer,
> > even for the root memory cgroup. In contrast, the situation for
> > object cgroups has been different.
> >
> > Previously, the root object cgroup couldn't be returned because
> > it didn't exist. Now that a valid root object cgroup exists, for
> > the sake of consistency, it's necessary to align the behavior of
> > object-cgroup-related operations with that of memory cgroup APIs.
> >
> > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > ---
> > include/linux/memcontrol.h | 29 ++++++++++++++++++-------
> > mm/memcontrol.c | 44 ++++++++++++++++++++------------------
> > mm/percpu.c | 2 +-
> > 3 files changed, 45 insertions(+), 30 deletions(-)
> >
> > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> > index bb4f203733f3..e74922d5755d 100644
> > --- a/include/linux/memcontrol.h
> > +++ b/include/linux/memcontrol.h
> > @@ -319,6 +319,7 @@ struct mem_cgroup {
> > #define MEMCG_CHARGE_BATCH 64U
> >
> > extern struct mem_cgroup *root_mem_cgroup;
> > +extern struct obj_cgroup *root_obj_cgroup;
> >
> > enum page_memcg_data_flags {
> > /* page->memcg_data is a pointer to an slabobj_ext vector */
> > @@ -528,6 +529,11 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
> > return (memcg == root_mem_cgroup);
> > }
> >
> > +static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
> > +{
> > + return objcg == root_obj_cgroup;
> > +}
> > +
> > static inline bool mem_cgroup_disabled(void)
> > {
> > return !cgroup_subsys_enabled(memory_cgrp_subsys);
> > @@ -752,23 +758,26 @@ struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
> >
> > static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
> > {
> > + if (obj_cgroup_is_root(objcg))
> > + return true;
> > return percpu_ref_tryget(&objcg->refcnt);
> > }
> >
> > -static inline void obj_cgroup_get(struct obj_cgroup *objcg)
> > +static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
> > + unsigned long nr)
> > {
> > - percpu_ref_get(&objcg->refcnt);
> > + if (!obj_cgroup_is_root(objcg))
> > + percpu_ref_get_many(&objcg->refcnt, nr);
> > }
> >
> > -static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
> > - unsigned long nr)
> > +static inline void obj_cgroup_get(struct obj_cgroup *objcg)
> > {
> > - percpu_ref_get_many(&objcg->refcnt, nr);
> > + obj_cgroup_get_many(objcg, 1);
> > }
> >
> > static inline void obj_cgroup_put(struct obj_cgroup *objcg)
> > {
> > - if (objcg)
> > + if (objcg && !obj_cgroup_is_root(objcg))
> > percpu_ref_put(&objcg->refcnt);
> > }
> >
> > @@ -1101,6 +1110,11 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
> > return true;
> > }
> >
> > +static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
> > +{
> > + return true;
> > +}
> > +
> > static inline bool mem_cgroup_disabled(void)
> > {
> > return true;
> > @@ -1684,8 +1698,7 @@ static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
> > {
> > struct obj_cgroup *objcg = current_obj_cgroup();
> >
> > - if (objcg)
> > - obj_cgroup_get(objcg);
> > + obj_cgroup_get(objcg);
> >
> > return objcg;
> > }
> > diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> > index a6362d11b46c..4aadc1b87db3 100644
> > --- a/mm/memcontrol.c
> > +++ b/mm/memcontrol.c
> > @@ -81,6 +81,7 @@ struct cgroup_subsys memory_cgrp_subsys __read_mostly;
> > EXPORT_SYMBOL(memory_cgrp_subsys);
> >
> > struct mem_cgroup *root_mem_cgroup __read_mostly;
> > +struct obj_cgroup *root_obj_cgroup __read_mostly;
> >
> > /* Active memory cgroup to use from an interrupt context */
> > DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
> > @@ -2525,15 +2526,14 @@ struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
> >
> > static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
> > {
> > - struct obj_cgroup *objcg = NULL;
> > + for (; memcg; memcg = parent_mem_cgroup(memcg)) {
> > + struct obj_cgroup *objcg = rcu_dereference(memcg->objcg);
> >
> > - for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
> > - objcg = rcu_dereference(memcg->objcg);
> > if (likely(objcg && obj_cgroup_tryget(objcg)))
> > - break;
> > - objcg = NULL;
> > + return objcg;
> > }
> > - return objcg;
> > +
> > + return NULL;
> > }
> >
>
> It appears that the return NULL statement might be dead code in this
> context. And would it be preferable to use return root_obj_cgroup instead?
I do not think so. The parameter of @memcg could be NULL passed from
current_objcg_update(). Returning NULL in this case makes sense to me.
It is not reasonable to return root_obj_cgroup for a NULL memcg for me.
Muchun,
Thanks.
>
> Best regards,
> Ridong
>
> > static struct obj_cgroup *current_objcg_update(void)
> > @@ -2604,18 +2604,17 @@ __always_inline struct obj_cgroup *current_obj_cgroup(void)
> > * Objcg reference is kept by the task, so it's safe
> > * to use the objcg by the current task.
> > */
> > - return objcg;
> > + return objcg ? : root_obj_cgroup;
> > }
> >
> > memcg = this_cpu_read(int_active_memcg);
> > if (unlikely(memcg))
> > goto from_memcg;
> >
> > - return NULL;
> > + return root_obj_cgroup;
> >
> > from_memcg:
> > - objcg = NULL;
> > - for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
> > + for (; memcg; memcg = parent_mem_cgroup(memcg)) {
> > /*
> > * Memcg pointer is protected by scope (see set_active_memcg())
> > * and is pinning the corresponding objcg, so objcg can't go
> > @@ -2624,10 +2623,10 @@ __always_inline struct obj_cgroup *current_obj_cgroup(void)
> > */
> > objcg = rcu_dereference_check(memcg->objcg, 1);
> > if (likely(objcg))
> > - break;
> > + return objcg;
> > }
> >
> > - return objcg;
> > + return root_obj_cgroup;
> > }
> >
> > struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
> > @@ -2641,14 +2640,8 @@ struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
> > objcg = __folio_objcg(folio);
> > obj_cgroup_get(objcg);
> > } else {
> > - struct mem_cgroup *memcg;
> > -
> > rcu_read_lock();
> > - memcg = __folio_memcg(folio);
> > - if (memcg)
> > - objcg = __get_obj_cgroup_from_memcg(memcg);
> > - else
> > - objcg = NULL;
> > + objcg = __get_obj_cgroup_from_memcg(__folio_memcg(folio));
> > rcu_read_unlock();
> > }
> > return objcg;
> > @@ -2733,7 +2726,7 @@ int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
> > int ret = 0;
> >
> > objcg = current_obj_cgroup();
> > - if (objcg) {
> > + if (!obj_cgroup_is_root(objcg)) {
> > ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order);
> > if (!ret) {
> > obj_cgroup_get(objcg);
> > @@ -3036,7 +3029,7 @@ bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,
> > * obj_cgroup_get() is used to get a permanent reference.
> > */
> > objcg = current_obj_cgroup();
> > - if (!objcg)
> > + if (obj_cgroup_is_root(objcg))
> > return true;
> >
> > /*
> > @@ -3708,6 +3701,9 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
> > if (!objcg)
> > goto free_shrinker;
> >
> > + if (unlikely(mem_cgroup_is_root(memcg)))
> > + root_obj_cgroup = objcg;
> > +
> > objcg->memcg = memcg;
> > rcu_assign_pointer(memcg->objcg, objcg);
> > obj_cgroup_get(objcg);
> > @@ -5302,6 +5298,9 @@ void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)
> > if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
> > return;
> >
> > + if (obj_cgroup_is_root(objcg))
> > + return;
> > +
> > VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC));
> >
> > /* PF_MEMALLOC context, charging must succeed */
> > @@ -5329,6 +5328,9 @@ void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size)
> > if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
> > return;
> >
> > + if (obj_cgroup_is_root(objcg))
> > + return;
> > +
> > obj_cgroup_uncharge(objcg, size);
> >
> > rcu_read_lock();
> > diff --git a/mm/percpu.c b/mm/percpu.c
> > index b35494c8ede2..3e54c6fca9bd 100644
> > --- a/mm/percpu.c
> > +++ b/mm/percpu.c
> > @@ -1616,7 +1616,7 @@ static bool pcpu_memcg_pre_alloc_hook(size_t size, gfp_t gfp,
> > return true;
> >
> > objcg = current_obj_cgroup();
> > - if (!objcg)
> > + if (obj_cgroup_is_root(objcg))
> > return true;
> >
> > if (obj_cgroup_charge(objcg, gfp, pcpu_obj_full_size(size)))
>
next prev parent reply other threads:[~2025-06-30 7:17 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-15 2:45 [PATCH RFC 00/28] Eliminate Dying Memory Cgroup Muchun Song
2025-04-15 2:45 ` [PATCH RFC 01/28] mm: memcontrol: remove dead code of checking parent memory cgroup Muchun Song
2025-04-17 14:35 ` Johannes Weiner
2025-04-15 2:45 ` [PATCH RFC 02/28] mm: memcontrol: use folio_memcg_charged() to avoid potential rcu lock holding Muchun Song
2025-04-17 14:48 ` Johannes Weiner
2025-04-18 2:38 ` Muchun Song
2025-04-15 2:45 ` [PATCH RFC 03/28] mm: workingset: use folio_lruvec() in workingset_refault() Muchun Song
2025-04-17 14:52 ` Johannes Weiner
2025-04-15 2:45 ` [PATCH RFC 04/28] mm: rename unlock_page_lruvec_irq and its variants Muchun Song
2025-04-17 14:53 ` Johannes Weiner
2025-04-15 2:45 ` [PATCH RFC 05/28] mm: thp: replace folio_memcg() with folio_memcg_charged() Muchun Song
2025-04-17 14:54 ` Johannes Weiner
2025-04-15 2:45 ` [PATCH RFC 06/28] mm: thp: introduce folio_split_queue_lock and its variants Muchun Song
2025-04-17 14:58 ` Johannes Weiner
2025-04-18 19:50 ` Johannes Weiner
2025-04-19 14:20 ` Muchun Song
2025-04-15 2:45 ` [PATCH RFC 07/28] mm: thp: use folio_batch to handle THP splitting in deferred_split_scan() Muchun Song
2025-04-30 14:37 ` Johannes Weiner
2025-05-06 6:44 ` Hugh Dickins
2025-05-06 21:44 ` Hugh Dickins
2025-05-07 3:30 ` Muchun Song
2025-04-15 2:45 ` [PATCH RFC 08/28] mm: vmscan: refactor move_folios_to_lru() Muchun Song
2025-04-30 14:49 ` Johannes Weiner
2025-04-15 2:45 ` [PATCH RFC 09/28] mm: memcontrol: allocate object cgroup for non-kmem case Muchun Song
2025-04-15 2:45 ` [PATCH RFC 10/28] mm: memcontrol: return root object cgroup for root memory cgroup Muchun Song
2025-06-28 3:09 ` Chen Ridong
2025-06-30 7:16 ` Muchun Song [this message]
2025-04-15 2:45 ` [PATCH RFC 11/28] mm: memcontrol: prevent memory cgroup release in get_mem_cgroup_from_folio() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 12/28] buffer: prevent memory cgroup release in folio_alloc_buffers() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 13/28] writeback: prevent memory cgroup release in writeback module Muchun Song
2025-04-15 2:45 ` [PATCH RFC 14/28] mm: memcontrol: prevent memory cgroup release in count_memcg_folio_events() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 15/28] mm: page_io: prevent memory cgroup release in page_io module Muchun Song
2025-04-15 2:45 ` [PATCH RFC 16/28] mm: migrate: prevent memory cgroup release in folio_migrate_mapping() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 17/28] mm: mglru: prevent memory cgroup release in mglru Muchun Song
2025-04-15 2:45 ` [PATCH RFC 18/28] mm: memcontrol: prevent memory cgroup release in mem_cgroup_swap_full() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 19/28] mm: workingset: prevent memory cgroup release in lru_gen_eviction() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 20/28] mm: workingset: prevent lruvec release in workingset_refault() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 21/28] mm: zswap: prevent lruvec release in zswap_folio_swapin() Muchun Song
2025-04-17 17:39 ` Nhat Pham
2025-04-18 2:36 ` Chengming Zhou
2025-04-15 2:45 ` [PATCH RFC 22/28] mm: swap: prevent lruvec release in swap module Muchun Song
2025-04-15 2:45 ` [PATCH RFC 23/28] mm: workingset: prevent lruvec release in workingset_activation() Muchun Song
2025-04-15 2:45 ` [PATCH RFC 24/28] mm: memcontrol: prepare for reparenting LRU pages for lruvec lock Muchun Song
2025-04-15 2:45 ` [PATCH RFC 25/28] mm: thp: prepare for reparenting LRU pages for split queue lock Muchun Song
2025-04-15 2:45 ` [PATCH RFC 26/28] mm: memcontrol: introduce memcg_reparent_ops Muchun Song
2025-06-30 12:47 ` Harry Yoo
2025-07-01 22:12 ` Harry Yoo
2025-07-07 9:29 ` [External] " Muchun Song
2025-07-09 0:14 ` Harry Yoo
2025-04-15 2:45 ` [PATCH RFC 27/28] mm: memcontrol: eliminate the problem of dying memory cgroup for LRU folios Muchun Song
2025-05-20 11:27 ` Harry Yoo
2025-05-22 2:31 ` Muchun Song
2025-05-23 1:24 ` Harry Yoo
2025-04-15 2:45 ` [PATCH RFC 28/28] mm: lru: add VM_WARN_ON_ONCE_FOLIO to lru maintenance helpers Muchun Song
2025-04-15 2:53 ` [PATCH RFC 00/28] Eliminate Dying Memory Cgroup Muchun Song
2025-04-15 6:19 ` Kairui Song
2025-04-15 8:01 ` Muchun Song
2025-04-17 18:22 ` Kairui Song
2025-04-17 19:04 ` Johannes Weiner
2025-06-27 8:50 ` Chen Ridong
2025-04-17 21:45 ` Roman Gushchin
2025-04-28 3:43 ` Kairui Song
2025-06-27 9:02 ` Chen Ridong
2025-06-27 18:54 ` Kairui Song
2025-06-27 19:14 ` Shakeel Butt
2025-06-28 9:21 ` Chen Ridong
2025-04-22 14:20 ` Yosry Ahmed
2025-05-23 1:23 ` Harry Yoo
2025-05-23 2:39 ` Muchun Song
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='CAMZfGtX_Ft=OpThZC0vp2TdXxymK-AV6HTyinVhRBJrk6ZkUfA@mail.gmail.com' \
--to=songmuchun@bytedance.com \
--cc=akpm@linux-foundation.org \
--cc=apais@linux.microsoft.com \
--cc=cgroups@vger.kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=chenridong@huaweicloud.com \
--cc=david@fromorbit.com \
--cc=hamzamahfooz@linux.microsoft.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=yosry.ahmed@linux.dev \
--cc=zhengqi.arch@bytedance.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