Skip to content

Commit 3c11ecf

Browse files
hkamezawatorvalds
authored andcommitted
memcg: oom kill disable and oom status
This adds a feature to disable oom-killer for memcg, if disabled, of course, tasks under memcg will stop. But now, we have oom-notifier for memcg. And the world around memcg is not under out-of-memory. memcg's out-of-memory just shows memcg hits limit. Then, administrator or management daemon can recover the situation by - kill some process - enlarge limit, add more swap. - migrate some tasks - remove file cache on tmps (difficult ?) Unlike oom-killer, you can take enough information before killing tasks. (by gcore, or, ps etc.) [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Cc: Balbir Singh <balbir@in.ibm.com> Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Cc: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9490ff2 commit 3c11ecf

2 files changed

Lines changed: 117 additions & 19 deletions

File tree

‎Documentation/cgroups/memory.txt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ It's applicable for root and non-root cgroup.
493493

494494
10. OOM Control
495495

496+
memory.oom_control file is for OOM notification and other controls.
497+
496498
Memory controler implements oom notifier using cgroup notification
497499
API (See cgroups.txt). It allows to register multiple oom notification
498500
delivery and gets notification when oom happens.
@@ -505,6 +507,27 @@ To register a notifier, application need:
505507
Application will be notifier through eventfd when oom happens.
506508
OOM notification doesn't work for root cgroup.
507509

510+
You can disable oom-killer by writing "1" to memory.oom_control file.
511+
As.
512+
#echo 1 > memory.oom_control
513+
514+
This operation is only allowed to the top cgroup of subhierarchy.
515+
If oom-killer is disabled, tasks under cgroup will hang/sleep
516+
in memcg's oom-waitq when they request accountable memory.
517+
518+
For running them, you have to relax the memcg's oom sitaution by
519+
* enlarge limit or reduce usage.
520+
To reduce usage,
521+
* kill some tasks.
522+
* move some tasks to other group with account migration.
523+
* remove some files (on tmpfs?)
524+
525+
Then, stopped tasks will work again.
526+
527+
At reading, current status of OOM is shown.
528+
oom_kill_disable 0 or 1 (if 1, oom-killer is disabled)
529+
under_oom 0 or 1 (if 1, the memcg is under OOM,tasks may
530+
be stopped.)
508531

509532
11. TODO
510533

‎mm/memcontrol.c‎

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ struct mem_cgroup {
214214
atomic_t refcnt;
215215

216216
unsigned int swappiness;
217+
/* OOM-Killer disable */
218+
int oom_kill_disable;
217219

218220
/* set when res.limit == memsw.limit */
219221
bool memsw_is_minimum;
@@ -235,7 +237,6 @@ struct mem_cgroup {
235237
* mem_cgroup ? And what type of charges should we move ?
236238
*/
237239
unsigned long move_charge_at_immigrate;
238-
239240
/*
240241
* percpu counter.
241242
*/
@@ -1342,20 +1343,26 @@ static void memcg_wakeup_oom(struct mem_cgroup *mem)
13421343
__wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
13431344
}
13441345

1346+
static void memcg_oom_recover(struct mem_cgroup *mem)
1347+
{
1348+
if (mem->oom_kill_disable && atomic_read(&mem->oom_lock))
1349+
memcg_wakeup_oom(mem);
1350+
}
1351+
13451352
/*
13461353
* try to call OOM killer. returns false if we should exit memory-reclaim loop.
13471354
*/
13481355
bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
13491356
{
13501357
struct oom_wait_info owait;
1351-
bool locked;
1358+
bool locked, need_to_kill;
13521359

13531360
owait.mem = mem;
13541361
owait.wait.flags = 0;
13551362
owait.wait.func = memcg_oom_wake_function;
13561363
owait.wait.private = current;
13571364
INIT_LIST_HEAD(&owait.wait.task_list);
1358-
1365+
need_to_kill = true;
13591366
/* At first, try to OOM lock hierarchy under mem.*/
13601367
mutex_lock(&memcg_oom_mutex);
13611368
locked = mem_cgroup_oom_lock(mem);
@@ -1364,15 +1371,17 @@ bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
13641371
* accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
13651372
* under OOM is always welcomed, use TASK_KILLABLE here.
13661373
*/
1367-
if (!locked)
1368-
prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1369-
else
1374+
prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1375+
if (!locked || mem->oom_kill_disable)
1376+
need_to_kill = false;
1377+
if (locked)
13701378
mem_cgroup_oom_notify(mem);
13711379
mutex_unlock(&memcg_oom_mutex);
13721380

1373-
if (locked)
1381+
if (need_to_kill) {
1382+
finish_wait(&memcg_oom_waitq, &owait.wait);
13741383
mem_cgroup_out_of_memory(mem, mask);
1375-
else {
1384+
} else {
13761385
schedule();
13771386
finish_wait(&memcg_oom_waitq, &owait.wait);
13781387
}
@@ -2162,15 +2171,6 @@ __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
21622171
/* If swapout, usage of swap doesn't decrease */
21632172
if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
21642173
uncharge_memsw = false;
2165-
/*
2166-
* do_batch > 0 when unmapping pages or inode invalidate/truncate.
2167-
* In those cases, all pages freed continously can be expected to be in
2168-
* the same cgroup and we have chance to coalesce uncharges.
2169-
* But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2170-
* because we want to do uncharge as soon as possible.
2171-
*/
2172-
if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
2173-
goto direct_uncharge;
21742174

21752175
batch = &current->memcg_batch;
21762176
/*
@@ -2180,6 +2180,17 @@ __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
21802180
*/
21812181
if (!batch->memcg)
21822182
batch->memcg = mem;
2183+
/*
2184+
* do_batch > 0 when unmapping pages or inode invalidate/truncate.
2185+
* In those cases, all pages freed continously can be expected to be in
2186+
* the same cgroup and we have chance to coalesce uncharges.
2187+
* But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2188+
* because we want to do uncharge as soon as possible.
2189+
*/
2190+
2191+
if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2192+
goto direct_uncharge;
2193+
21832194
/*
21842195
* In typical case, batch->memcg == mem. This means we can
21852196
* merge a series of uncharges to an uncharge of res_counter.
@@ -2196,6 +2207,8 @@ __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
21962207
res_counter_uncharge(&mem->res, PAGE_SIZE);
21972208
if (uncharge_memsw)
21982209
res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2210+
if (unlikely(batch->memcg != mem))
2211+
memcg_oom_recover(mem);
21992212
return;
22002213
}
22012214

@@ -2332,6 +2345,7 @@ void mem_cgroup_uncharge_end(void)
23322345
res_counter_uncharge(&batch->memcg->res, batch->bytes);
23332346
if (batch->memsw_bytes)
23342347
res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2348+
memcg_oom_recover(batch->memcg);
23352349
/* forget this pointer (for sanity check) */
23362350
batch->memcg = NULL;
23372351
}
@@ -2568,10 +2582,11 @@ static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
25682582
unsigned long long val)
25692583
{
25702584
int retry_count;
2571-
u64 memswlimit;
2585+
u64 memswlimit, memlimit;
25722586
int ret = 0;
25732587
int children = mem_cgroup_count_children(memcg);
25742588
u64 curusage, oldusage;
2589+
int enlarge;
25752590

25762591
/*
25772592
* For keeping hierarchical_reclaim simple, how long we should retry
@@ -2582,6 +2597,7 @@ static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
25822597

25832598
oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
25842599

2600+
enlarge = 0;
25852601
while (retry_count) {
25862602
if (signal_pending(current)) {
25872603
ret = -EINTR;
@@ -2599,6 +2615,11 @@ static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
25992615
mutex_unlock(&set_limit_mutex);
26002616
break;
26012617
}
2618+
2619+
memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2620+
if (memlimit < val)
2621+
enlarge = 1;
2622+
26022623
ret = res_counter_set_limit(&memcg->res, val);
26032624
if (!ret) {
26042625
if (memswlimit == val)
@@ -2620,6 +2641,8 @@ static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
26202641
else
26212642
oldusage = curusage;
26222643
}
2644+
if (!ret && enlarge)
2645+
memcg_oom_recover(memcg);
26232646

26242647
return ret;
26252648
}
@@ -2628,9 +2651,10 @@ static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
26282651
unsigned long long val)
26292652
{
26302653
int retry_count;
2631-
u64 memlimit, oldusage, curusage;
2654+
u64 memlimit, memswlimit, oldusage, curusage;
26322655
int children = mem_cgroup_count_children(memcg);
26332656
int ret = -EBUSY;
2657+
int enlarge = 0;
26342658

26352659
/* see mem_cgroup_resize_res_limit */
26362660
retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
@@ -2652,6 +2676,9 @@ static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
26522676
mutex_unlock(&set_limit_mutex);
26532677
break;
26542678
}
2679+
memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2680+
if (memswlimit < val)
2681+
enlarge = 1;
26552682
ret = res_counter_set_limit(&memcg->memsw, val);
26562683
if (!ret) {
26572684
if (memlimit == val)
@@ -2674,6 +2701,8 @@ static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
26742701
else
26752702
oldusage = curusage;
26762703
}
2704+
if (!ret && enlarge)
2705+
memcg_oom_recover(memcg);
26772706
return ret;
26782707
}
26792708

@@ -2865,6 +2894,7 @@ static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
28652894
if (ret)
28662895
break;
28672896
}
2897+
memcg_oom_recover(mem);
28682898
/* it seems parent cgroup doesn't have enough mem */
28692899
if (ret == -ENOMEM)
28702900
goto try_to_free;
@@ -3645,6 +3675,46 @@ static int mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
36453675
return 0;
36463676
}
36473677

3678+
static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3679+
struct cftype *cft, struct cgroup_map_cb *cb)
3680+
{
3681+
struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3682+
3683+
cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
3684+
3685+
if (atomic_read(&mem->oom_lock))
3686+
cb->fill(cb, "under_oom", 1);
3687+
else
3688+
cb->fill(cb, "under_oom", 0);
3689+
return 0;
3690+
}
3691+
3692+
/*
3693+
*/
3694+
static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
3695+
struct cftype *cft, u64 val)
3696+
{
3697+
struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3698+
struct mem_cgroup *parent;
3699+
3700+
/* cannot set to root cgroup and only 0 and 1 are allowed */
3701+
if (!cgrp->parent || !((val == 0) || (val == 1)))
3702+
return -EINVAL;
3703+
3704+
parent = mem_cgroup_from_cont(cgrp->parent);
3705+
3706+
cgroup_lock();
3707+
/* oom-kill-disable is a flag for subhierarchy. */
3708+
if ((parent->use_hierarchy) ||
3709+
(mem->use_hierarchy && !list_empty(&cgrp->children))) {
3710+
cgroup_unlock();
3711+
return -EINVAL;
3712+
}
3713+
mem->oom_kill_disable = val;
3714+
cgroup_unlock();
3715+
return 0;
3716+
}
3717+
36483718
static struct cftype mem_cgroup_files[] = {
36493719
{
36503720
.name = "usage_in_bytes",
@@ -3702,6 +3772,8 @@ static struct cftype mem_cgroup_files[] = {
37023772
},
37033773
{
37043774
.name = "oom_control",
3775+
.read_map = mem_cgroup_oom_control_read,
3776+
.write_u64 = mem_cgroup_oom_control_write,
37053777
.register_event = mem_cgroup_oom_register_event,
37063778
.unregister_event = mem_cgroup_oom_unregister_event,
37073779
.private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
@@ -3943,6 +4015,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
39434015
} else {
39444016
parent = mem_cgroup_from_cont(cont->parent);
39454017
mem->use_hierarchy = parent->use_hierarchy;
4018+
mem->oom_kill_disable = parent->oom_kill_disable;
39464019
}
39474020

39484021
if (parent && parent->use_hierarchy) {
@@ -4215,6 +4288,7 @@ static void mem_cgroup_clear_mc(void)
42154288
if (mc.precharge) {
42164289
__mem_cgroup_cancel_charge(mc.to, mc.precharge);
42174290
mc.precharge = 0;
4291+
memcg_oom_recover(mc.to);
42184292
}
42194293
/*
42204294
* we didn't uncharge from mc.from at mem_cgroup_move_account(), so
@@ -4223,6 +4297,7 @@ static void mem_cgroup_clear_mc(void)
42234297
if (mc.moved_charge) {
42244298
__mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
42254299
mc.moved_charge = 0;
4300+
memcg_oom_recover(mc.from);
42264301
}
42274302
/* we must fixup refcnts and charges */
42284303
if (mc.moved_swap) {

0 commit comments

Comments
 (0)