Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/list_lru: code clean up for reparenting

No feature change, just change of code structure and fix comment.

The list lrus are not empty until memcg_reparent_list_lru_node() calls are
all done, so the comments in memcg_offline_kmem were slightly inaccurate.

Link: https://lkml.kernel.org/r/20241104175257.60853-4-ryncsn@gmail.com
Signed-off-by: Kairui Song <kasong@tencent.com>
Reviewed-by: Muchun Song <muchun.song@linux.dev>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Chengming Zhou <zhouchengming@bytedance.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Qi Zheng <zhengqi.arch@bytedance.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Waiman Long <longman@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kairui Song and committed by
Andrew Morton
8d42abbf 78c0ed09

+17 -29
+17 -22
mm/list_lru.c
··· 421 421 spin_unlock_irq(&nlru->lock); 422 422 } 423 423 424 - static void memcg_reparent_list_lru(struct list_lru *lru, 425 - int src_idx, struct mem_cgroup *dst_memcg) 426 - { 427 - int i; 428 - 429 - for_each_node(i) 430 - memcg_reparent_list_lru_node(lru, i, src_idx, dst_memcg); 431 - 432 - memcg_list_lru_free(lru, src_idx); 433 - } 434 - 435 424 void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent) 436 425 { 437 426 struct cgroup_subsys_state *css; 438 427 struct list_lru *lru; 439 - int src_idx = memcg->kmemcg_id; 428 + int src_idx = memcg->kmemcg_id, i; 440 429 441 430 /* 442 431 * Change kmemcg_id of this cgroup and all its descendants to the 443 432 * parent's id, and then move all entries from this cgroup's list_lrus 444 433 * to ones of the parent. 445 - * 446 - * After we have finished, all list_lrus corresponding to this cgroup 447 - * are guaranteed to remain empty. So we can safely free this cgroup's 448 - * list lrus in memcg_list_lru_free(). 449 - * 450 - * Changing ->kmemcg_id to the parent can prevent memcg_list_lru_alloc() 451 - * from allocating list lrus for this cgroup after memcg_list_lru_free() 452 - * call. 453 434 */ 454 435 rcu_read_lock(); 455 436 css_for_each_descendant_pre(css, &memcg->css) { ··· 441 460 } 442 461 rcu_read_unlock(); 443 462 463 + /* 464 + * With kmemcg_id set to parent, holding the lock of each list_lru_node 465 + * below can prevent list_lru_{add,del,isolate} from touching the lru, 466 + * safe to reparent. 467 + */ 444 468 mutex_lock(&list_lrus_mutex); 445 - list_for_each_entry(lru, &memcg_list_lrus, list) 446 - memcg_reparent_list_lru(lru, src_idx, parent); 469 + list_for_each_entry(lru, &memcg_list_lrus, list) { 470 + for_each_node(i) 471 + memcg_reparent_list_lru_node(lru, i, src_idx, parent); 472 + 473 + /* 474 + * Here all list_lrus corresponding to the cgroup are guaranteed 475 + * to remain empty, we can safely free this lru, any further 476 + * memcg_list_lru_alloc() call will simply bail out. 477 + */ 478 + memcg_list_lru_free(lru, src_idx); 479 + } 447 480 mutex_unlock(&list_lrus_mutex); 448 481 } 449 482
-7
mm/memcontrol.c
··· 3111 3111 parent = root_mem_cgroup; 3112 3112 3113 3113 memcg_reparent_objcgs(memcg, parent); 3114 - 3115 - /* 3116 - * After we have finished memcg_reparent_objcgs(), all list_lrus 3117 - * corresponding to this cgroup are guaranteed to remain empty. 3118 - * The ordering is imposed by list_lru_node->lock taken by 3119 - * memcg_reparent_list_lrus(). 3120 - */ 3121 3114 memcg_reparent_list_lrus(memcg, parent); 3122 3115 } 3123 3116