Skip to content

Commit da0efe3

Browse files
Muchun Songtorvalds
authored andcommitted
mm: memcontrol: move memcg_online_kmem() to mem_cgroup_css_online()
It will simplify the code if moving memcg_online_kmem() to mem_cgroup_css_online() and do not need to set ->kmemcg_id to -1 to indicate the memcg is offline. In the next patch, ->kmemcg_id will be used to sync list lru reparenting which requires not to change ->kmemcg_id. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Muchun Song <[email protected]> Acked-by: Roman Gushchin <[email protected]> Cc: Alex Shi <[email protected]> Cc: Anna Schumaker <[email protected]> Cc: Chao Yu <[email protected]> Cc: Dave Chinner <[email protected]> Cc: Fam Zheng <[email protected]> Cc: Jaegeuk Kim <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Kari Argillander <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Qi Zheng <[email protected]> Cc: Shakeel Butt <[email protected]> Cc: Theodore Ts'o <[email protected]> Cc: Trond Myklebust <[email protected]> Cc: Vladimir Davydov <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Wei Yang <[email protected]> Cc: Xiongchun Duan <[email protected]> Cc: Yang Shi <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 9bbdc0f commit da0efe3

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

mm/memcontrol.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,7 +3670,8 @@ static int memcg_online_kmem(struct mem_cgroup *memcg)
36703670
if (cgroup_memory_nokmem)
36713671
return 0;
36723672

3673-
BUG_ON(memcg->kmemcg_id >= 0);
3673+
if (unlikely(mem_cgroup_is_root(memcg)))
3674+
return 0;
36743675

36753676
memcg_id = memcg_alloc_cache_id();
36763677
if (memcg_id < 0)
@@ -3696,7 +3697,10 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg)
36963697
struct mem_cgroup *parent;
36973698
int kmemcg_id;
36983699

3699-
if (memcg->kmemcg_id == -1)
3700+
if (cgroup_memory_nokmem)
3701+
return;
3702+
3703+
if (unlikely(mem_cgroup_is_root(memcg)))
37003704
return;
37013705

37023706
parent = parent_mem_cgroup(memcg);
@@ -3706,7 +3710,6 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg)
37063710
memcg_reparent_objcgs(memcg, parent);
37073711

37083712
kmemcg_id = memcg->kmemcg_id;
3709-
BUG_ON(kmemcg_id < 0);
37103713

37113714
/*
37123715
* After we have finished memcg_reparent_objcgs(), all list_lrus
@@ -3717,7 +3720,6 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg)
37173720
memcg_drain_all_list_lrus(kmemcg_id, parent);
37183721

37193722
memcg_free_cache_id(kmemcg_id);
3720-
memcg->kmemcg_id = -1;
37213723
}
37223724
#else
37233725
static int memcg_online_kmem(struct mem_cgroup *memcg)
@@ -5237,7 +5239,6 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
52375239
{
52385240
struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
52395241
struct mem_cgroup *memcg, *old_memcg;
5240-
long error = -ENOMEM;
52415242

52425243
old_memcg = set_active_memcg(parent);
52435244
memcg = mem_cgroup_alloc();
@@ -5266,34 +5267,26 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
52665267
return &memcg->css;
52675268
}
52685269

5269-
/* The following stuff does not apply to the root */
5270-
error = memcg_online_kmem(memcg);
5271-
if (error)
5272-
goto fail;
5273-
52745270
if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
52755271
static_branch_inc(&memcg_sockets_enabled_key);
52765272

52775273
return &memcg->css;
5278-
fail:
5279-
mem_cgroup_id_remove(memcg);
5280-
mem_cgroup_free(memcg);
5281-
return ERR_PTR(error);
52825274
}
52835275

52845276
static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
52855277
{
52865278
struct mem_cgroup *memcg = mem_cgroup_from_css(css);
52875279

5280+
if (memcg_online_kmem(memcg))
5281+
goto remove_id;
5282+
52885283
/*
52895284
* A memcg must be visible for expand_shrinker_info()
52905285
* by the time the maps are allocated. So, we allocate maps
52915286
* here, when for_each_mem_cgroup() can't skip it.
52925287
*/
5293-
if (alloc_shrinker_info(memcg)) {
5294-
mem_cgroup_id_remove(memcg);
5295-
return -ENOMEM;
5296-
}
5288+
if (alloc_shrinker_info(memcg))
5289+
goto offline_kmem;
52975290

52985291
/* Online state pins memcg ID, memcg ID pins CSS */
52995292
refcount_set(&memcg->id.ref, 1);
@@ -5303,6 +5296,11 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
53035296
queue_delayed_work(system_unbound_wq, &stats_flush_dwork,
53045297
2UL*HZ);
53055298
return 0;
5299+
offline_kmem:
5300+
memcg_offline_kmem(memcg);
5301+
remove_id:
5302+
mem_cgroup_id_remove(memcg);
5303+
return -ENOMEM;
53065304
}
53075305

53085306
static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
@@ -5360,9 +5358,6 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
53605358
cancel_work_sync(&memcg->high_work);
53615359
mem_cgroup_remove_from_trees(memcg);
53625360
free_shrinker_info(memcg);
5363-
5364-
/* Need to offline kmem if online_css() fails */
5365-
memcg_offline_kmem(memcg);
53665361
mem_cgroup_free(memcg);
53675362
}
53685363

0 commit comments

Comments
 (0)