Skip to content

Commit 8033b97

Browse files
Daisuke Nishimuratorvalds
authored andcommitted
memcg: avoid oom during moving charge
This move-charge-at-task-migration feature has extra charges on "to"(pre-charges) and "from"(left-over charges) during moving charge. This means unnecessary oom can happen. This patch tries to avoid such oom. Signed-off-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Cc: Balbir Singh <balbir@linux.vnet.ibm.com> Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Paul Menage <menage@google.com> Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 854ffa8 commit 8033b97

1 file changed

Lines changed: 51 additions & 2 deletions

File tree

‎mm/memcontrol.c‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ static struct move_charge_struct {
254254
struct mem_cgroup *to;
255255
unsigned long precharge;
256256
unsigned long moved_charge;
257-
} mc;
257+
struct task_struct *moving_task; /* a task moving charges */
258+
wait_queue_head_t waitq; /* a waitq for other context */
259+
} mc = {
260+
.waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
261+
};
258262

259263
/*
260264
* Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
@@ -1508,6 +1512,48 @@ static int __mem_cgroup_try_charge(struct mm_struct *mm,
15081512
if (mem_cgroup_check_under_limit(mem_over_limit))
15091513
continue;
15101514

1515+
/* try to avoid oom while someone is moving charge */
1516+
if (mc.moving_task && current != mc.moving_task) {
1517+
struct mem_cgroup *from, *to;
1518+
bool do_continue = false;
1519+
/*
1520+
* There is a small race that "from" or "to" can be
1521+
* freed by rmdir, so we use css_tryget().
1522+
*/
1523+
rcu_read_lock();
1524+
from = mc.from;
1525+
to = mc.to;
1526+
if (from && css_tryget(&from->css)) {
1527+
if (mem_over_limit->use_hierarchy)
1528+
do_continue = css_is_ancestor(
1529+
&from->css,
1530+
&mem_over_limit->css);
1531+
else
1532+
do_continue = (from == mem_over_limit);
1533+
css_put(&from->css);
1534+
}
1535+
if (!do_continue && to && css_tryget(&to->css)) {
1536+
if (mem_over_limit->use_hierarchy)
1537+
do_continue = css_is_ancestor(
1538+
&to->css,
1539+
&mem_over_limit->css);
1540+
else
1541+
do_continue = (to == mem_over_limit);
1542+
css_put(&to->css);
1543+
}
1544+
rcu_read_unlock();
1545+
if (do_continue) {
1546+
DEFINE_WAIT(wait);
1547+
prepare_to_wait(&mc.waitq, &wait,
1548+
TASK_INTERRUPTIBLE);
1549+
/* moving charge context might have finished. */
1550+
if (mc.moving_task)
1551+
schedule();
1552+
finish_wait(&mc.waitq, &wait);
1553+
continue;
1554+
}
1555+
}
1556+
15111557
if (!nr_retries--) {
15121558
if (oom) {
15131559
mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
@@ -3381,7 +3427,6 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
33813427
INIT_WORK(&stock->work, drain_local_stock);
33823428
}
33833429
hotcpu_notifier(memcg_stock_cpu_callback, 0);
3384-
33853430
} else {
33863431
parent = mem_cgroup_from_cont(cont->parent);
33873432
mem->use_hierarchy = parent->use_hierarchy;
@@ -3641,6 +3686,8 @@ static void mem_cgroup_clear_mc(void)
36413686
}
36423687
mc.from = NULL;
36433688
mc.to = NULL;
3689+
mc.moving_task = NULL;
3690+
wake_up_all(&mc.waitq);
36443691
}
36453692

36463693
static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
@@ -3666,10 +3713,12 @@ static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
36663713
VM_BUG_ON(mc.to);
36673714
VM_BUG_ON(mc.precharge);
36683715
VM_BUG_ON(mc.moved_charge);
3716+
VM_BUG_ON(mc.moving_task);
36693717
mc.from = from;
36703718
mc.to = mem;
36713719
mc.precharge = 0;
36723720
mc.moved_charge = 0;
3721+
mc.moving_task = current;
36733722

36743723
ret = mem_cgroup_precharge_mc(mm);
36753724
if (ret)

0 commit comments

Comments
 (0)