When a task moves to another memcg, MGLRU should move the mm it owns to the new memcg's mm list. mem_cgroup_lru_gen_attach() only checks the first thread-group leader in the taskset, though. A taskset can have several processes, e.g., when +memory is enabled on a parent, or no leader at all if the leader has exited. An mm missed this way stays on the old list, so the new memcg's aging doesn't walk it, and it keeps the old memcg alive after its memory controller is disabled. Check every task in the taskset instead. Skip this when MGLRU doesn't walk page tables, since lru_gen_migrate_mm() does nothing then. This takes task_lock() for every task being moved. Moving a 256-thread process via cgroup.procs got about 2.8 us (26%) slower in a QEMU/KVM microbenchmark. Fixes: bd74fdaea146 ("mm: multi-gen LRU: support page table walks") Signed-off-by: Aveline Noir --- Reproducers for both cases pass on x86_64. --- mm/memcontrol.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 84f4a8fd4d16..4590a9bfd9ff 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4762,27 +4762,22 @@ static void mem_cgroup_exit(struct task_struct *task) task->objcg = NULL; } -#ifdef CONFIG_LRU_GEN +#ifdef CONFIG_LRU_GEN_WALKS_MMU static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) { struct task_struct *task; struct cgroup_subsys_state *css; - /* find the first leader if there is any */ - cgroup_taskset_for_each_leader(task, css, tset) - break; - - if (!task) - return; - - task_lock(task); - if (task->mm && READ_ONCE(task->mm->owner) == task) - lru_gen_migrate_mm(task->mm); - task_unlock(task); + cgroup_taskset_for_each(task, css, tset) { + task_lock(task); + if (task->mm && READ_ONCE(task->mm->owner) == task) + lru_gen_migrate_mm(task->mm); + task_unlock(task); + } } #else static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) {} -#endif /* CONFIG_LRU_GEN */ +#endif /* CONFIG_LRU_GEN_WALKS_MMU */ static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset) { --- base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620 change-id: 20261112-mglru-memcg-attach-fix-20261112-7dc49d84708c Best regards, -- Aveline Noir