memcg: manipulate memcg private ID references by ID

This is a preparatory work for moving memcgid from memcg to objcg.

Swap entries retain a private ID rather than a memcg pointer.  Once
private ID references are moved to objcgs, the ID can also outlive the
memcg to which it was originally assigned.  So it's better to make the get
and put functions accept the ID itself instead of the memcg.

Rename mem_cgroup_private_id_get_online() to mem_cgroup_private_id_get(),
and make it return the ID only.  If the memcg is already dying, the dying
memcg will still be used for charging and stats accounting in v2 swap
charging path.  But they are hierarchical and will be reparented after
offlining so it doesn't matter.

Make mem_cgroup_private_id_put() take the ID and resolve the reference
holder internally.  Convert swap uncharge and charge rollback to release
the reference using that ID.  This introduces an extra xarray lookup for
now, which will be removed in the final patch.

Separate the online-state reference release into
mem_cgroup_private_id_kill().  The offline path already has the memcg
pointer and can call the underlying put helper directly.

Link: https://lore.kernel.org/20260921-bingfangguo-memcgid-rework-v2-3-6c0637dc0edb@tencent.com
Signed-off-by: Bingfang Guo <bingfangguo@tencent.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Muchun Song <muchun.song@linux.dev>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Qi Zheng <qi.zheng@linux.dev>
Cc: Kairui Song <kasong@tencent.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Yuanchu Xie <yuanchu@google.com>
Cc: Wei Xu <weixugc@google.com>
Cc: David Hildenbrand <david@kernel.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>
Cc: Bingfang Guo <bfguo@icloud.com>
This commit is contained in:
Bingfang Guo 2026-09-21 14:16:26 +08:00 committed by Andrew Morton
parent ea74172be2
commit 0262a5ee84
3 changed files with 27 additions and 15 deletions

View File

@ -268,7 +268,7 @@ void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg)
*/
void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
{
struct mem_cgroup *memcg, *swap_memcg;
struct mem_cgroup *memcg;
struct obj_cgroup *objcg;
unsigned int nr_entries;
unsigned short private_id;
@ -298,9 +298,8 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
* if the ID refers to the root memcg.
*/
nr_entries = folio_nr_pages(folio);
swap_memcg = mem_cgroup_private_id_get_online(memcg, nr_entries);
private_id = mem_cgroup_private_id(swap_memcg);
mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
private_id = mem_cgroup_private_id_get(memcg, nr_entries);
mod_memcg_state(memcg, MEMCG_SWAP, nr_entries);
__swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_entries, private_id);

View File

@ -27,8 +27,7 @@ static inline bool mem_cgroup_private_id_is_root(unsigned short id)
return id == mem_cgroup_private_id(root_mem_cgroup);
}
struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *memcg,
unsigned int n);
unsigned short mem_cgroup_private_id_get(struct mem_cgroup *memcg, unsigned int n);
void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg,
struct mem_cgroup *parent, int idx);

View File

@ -4082,7 +4082,7 @@ static void mem_cgroup_private_id_remove(struct mem_cgroup *memcg)
}
}
static inline void mem_cgroup_private_id_put(struct mem_cgroup *memcg, unsigned int n)
static void __mem_cgroup_private_id_put(struct mem_cgroup *memcg, unsigned int n)
{
if (refcount_sub_and_test(n, &memcg->private_id_ref)) {
mem_cgroup_private_id_remove(memcg);
@ -4092,7 +4092,22 @@ static inline void mem_cgroup_private_id_put(struct mem_cgroup *memcg, unsigned
}
}
struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *memcg, unsigned int n)
static inline void mem_cgroup_private_id_put(unsigned short id, unsigned int n)
{
struct mem_cgroup *memcg;
lockdep_assert_in_rcu_read_lock();
memcg = mem_cgroup_from_private_id(id);
__mem_cgroup_private_id_put(memcg, n);
}
static void mem_cgroup_private_id_kill(struct mem_cgroup *memcg)
{
__mem_cgroup_private_id_put(memcg, 1);
}
unsigned short mem_cgroup_private_id_get(struct mem_cgroup *memcg, unsigned int n)
{
while (!refcount_add_not_zero(n, &memcg->private_id_ref)) {
/*
@ -4105,7 +4120,8 @@ struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *memcg, un
}
memcg = parent_mem_cgroup(memcg);
}
return memcg;
return mem_cgroup_private_id(memcg);
}
/**
@ -4430,7 +4446,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
drain_all_stock(memcg);
mem_cgroup_private_id_put(memcg, 1);
mem_cgroup_private_id_kill(memcg);
}
static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
@ -5971,15 +5987,13 @@ int __mem_cgroup_try_charge_swap(struct folio *folio)
return 0;
}
memcg = mem_cgroup_private_id_get_online(memcg, nr_pages);
/* memcg is pined by memcg ID. */
private_id = mem_cgroup_private_id(memcg);
private_id = mem_cgroup_private_id_get(memcg, nr_pages);
if (!mem_cgroup_private_id_is_root(private_id) &&
!page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
memcg_memory_event(memcg, MEMCG_SWAP_MAX);
memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
mem_cgroup_private_id_put(memcg, nr_pages);
mem_cgroup_private_id_put(private_id, nr_pages);
return -ENOMEM;
}
mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
@ -6011,7 +6025,7 @@ 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(memcg, nr_pages);
mem_cgroup_private_id_put(id, nr_pages);
}
rcu_read_unlock();
}