Skip to content

Commit 40d5813

Browse files
Daisuke Nishimuratorvalds
authored andcommitted
memcg: fix error path of mem_cgroup_move_parent
There is a bug in error path of mem_cgroup_move_parent. Extra refcnt got from try_charge should be dropped, and usages incremented by try_charge should be decremented in both error paths: A: failure at get_page_unless_zero B: failure at isolate_lru_page This bug makes this parent directory unremovable. In case of A, rmdir doesn't return, because res.usage doesn't go down to 0 at mem_cgroup_force_empty even after all the pc in lru are removed. In case of B, rmdir fails and returns -EBUSY, because it has extra ref counts even after res.usage goes down to 0. Signed-off-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Acked-by: Balbir Singh <balbir@linux.vnet.ibm.com> Cc: Pavel Emelyanov <xemul@openvz.org> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Paul Menage <menage@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bd112db commit 40d5813

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎mm/memcontrol.c‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -994,14 +994,15 @@ static int mem_cgroup_move_account(struct page_cgroup *pc,
994994
if (pc->mem_cgroup != from)
995995
goto out;
996996

997-
css_put(&from->css);
998997
res_counter_uncharge(&from->res, PAGE_SIZE);
999998
mem_cgroup_charge_statistics(from, pc, false);
1000999
if (do_swap_account)
10011000
res_counter_uncharge(&from->memsw, PAGE_SIZE);
1001+
css_put(&from->css);
1002+
1003+
css_get(&to->css);
10021004
pc->mem_cgroup = to;
10031005
mem_cgroup_charge_statistics(to, pc, true);
1004-
css_get(&to->css);
10051006
ret = 0;
10061007
out:
10071008
unlock_page_cgroup(pc);
@@ -1034,8 +1035,10 @@ static int mem_cgroup_move_parent(struct page_cgroup *pc,
10341035
if (ret || !parent)
10351036
return ret;
10361037

1037-
if (!get_page_unless_zero(page))
1038-
return -EBUSY;
1038+
if (!get_page_unless_zero(page)) {
1039+
ret = -EBUSY;
1040+
goto uncharge;
1041+
}
10391042

10401043
ret = isolate_lru_page(page);
10411044

@@ -1044,19 +1047,23 @@ static int mem_cgroup_move_parent(struct page_cgroup *pc,
10441047

10451048
ret = mem_cgroup_move_account(pc, child, parent);
10461049

1047-
/* drop extra refcnt by try_charge() (move_account increment one) */
1048-
css_put(&parent->css);
10491050
putback_lru_page(page);
10501051
if (!ret) {
10511052
put_page(page);
1053+
/* drop extra refcnt by try_charge() */
1054+
css_put(&parent->css);
10521055
return 0;
10531056
}
1054-
/* uncharge if move fails */
1057+
10551058
cancel:
1059+
put_page(page);
1060+
uncharge:
1061+
/* drop extra refcnt by try_charge() */
1062+
css_put(&parent->css);
1063+
/* uncharge if move fails */
10561064
res_counter_uncharge(&parent->res, PAGE_SIZE);
10571065
if (do_swap_account)
10581066
res_counter_uncharge(&parent->memsw, PAGE_SIZE);
1059-
put_page(page);
10601067
return ret;
10611068
}
10621069

0 commit comments

Comments
 (0)