1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/oom.h>
19#include <linux/mm.h>
20#include <linux/err.h>
21#include <linux/sched.h>
22#include <linux/swap.h>
23#include <linux/timex.h>
24#include <linux/jiffies.h>
25#include <linux/cpuset.h>
26#include <linux/module.h>
27#include <linux/notifier.h>
28#include <linux/memcontrol.h>
29#include <linux/security.h>
30
31int sysctl_panic_on_oom;
32int sysctl_oom_kill_allocating_task;
33int sysctl_oom_dump_tasks;
34static DEFINE_SPINLOCK(zone_scan_lock);
35
36
37
38
39
40static int has_intersects_mems_allowed(struct task_struct *tsk)
41{
42 struct task_struct *t;
43
44 t = tsk;
45 do {
46 if (cpuset_mems_allowed_intersects(current, t))
47 return 1;
48 t = next_thread(t);
49 } while (t != tsk);
50
51 return 0;
52}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73unsigned long badness(struct task_struct *p, unsigned long uptime)
74{
75 unsigned long points, cpu_time, run_time;
76 struct mm_struct *mm;
77 struct task_struct *child;
78 int oom_adj = p->signal->oom_adj;
79 struct task_cputime task_time;
80 unsigned long utime;
81 unsigned long stime;
82
83 if (oom_adj == OOM_DISABLE)
84 return 0;
85
86 task_lock(p);
87 mm = p->mm;
88 if (!mm) {
89 task_unlock(p);
90 return 0;
91 }
92
93
94
95
96 points = mm->total_vm;
97
98
99
100
101 task_unlock(p);
102
103
104
105
106 if (p->flags & PF_OOM_ORIGIN)
107 return ULONG_MAX;
108
109
110
111
112
113
114
115
116
117 list_for_each_entry(child, &p->children, sibling) {
118 task_lock(child);
119 if (child->mm != mm && child->mm)
120 points += child->mm->total_vm/2 + 1;
121 task_unlock(child);
122 }
123
124
125
126
127
128
129 thread_group_cputime(p, &task_time);
130 utime = cputime_to_jiffies(task_time.utime);
131 stime = cputime_to_jiffies(task_time.stime);
132 cpu_time = (utime + stime) >> (SHIFT_HZ + 3);
133
134
135 if (uptime >= p->start_time.tv_sec)
136 run_time = (uptime - p->start_time.tv_sec) >> 10;
137 else
138 run_time = 0;
139
140 if (cpu_time)
141 points /= int_sqrt(cpu_time);
142 if (run_time)
143 points /= int_sqrt(int_sqrt(run_time));
144
145
146
147
148
149 if (task_nice(p) > 0)
150 points *= 2;
151
152
153
154
155
156 if (has_capability_noaudit(p, CAP_SYS_ADMIN) ||
157 has_capability_noaudit(p, CAP_SYS_RESOURCE))
158 points /= 4;
159
160
161
162
163
164
165
166 if (has_capability_noaudit(p, CAP_SYS_RAWIO))
167 points /= 4;
168
169
170
171
172
173
174 if (!has_intersects_mems_allowed(p))
175 points /= 8;
176
177
178
179
180 if (oom_adj) {
181 if (oom_adj > 0) {
182 if (!points)
183 points = 1;
184 points <<= oom_adj;
185 } else
186 points >>= -(oom_adj);
187 }
188
189#ifdef DEBUG
190 printk(KERN_DEBUG "OOMkill: task %d (%s) got %lu points\n",
191 p->pid, p->comm, points);
192#endif
193 return points;
194}
195
196
197
198
199static inline enum oom_constraint constrained_alloc(struct zonelist *zonelist,
200 gfp_t gfp_mask)
201{
202#ifdef CONFIG_NUMA
203 struct zone *zone;
204 struct zoneref *z;
205 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
206 nodemask_t nodes = node_states[N_HIGH_MEMORY];
207
208 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
209 if (cpuset_zone_allowed_softwall(zone, gfp_mask))
210 node_clear(zone_to_nid(zone), nodes);
211 else
212 return CONSTRAINT_CPUSET;
213
214 if (!nodes_empty(nodes))
215 return CONSTRAINT_MEMORY_POLICY;
216#endif
217
218 return CONSTRAINT_NONE;
219}
220
221
222
223
224
225
226
227static struct task_struct *select_bad_process(unsigned long *ppoints,
228 struct mem_cgroup *mem)
229{
230 struct task_struct *p;
231 struct task_struct *chosen = NULL;
232 struct timespec uptime;
233 *ppoints = 0;
234
235 do_posix_clock_monotonic_gettime(&uptime);
236 for_each_process(p) {
237 unsigned long points;
238
239
240
241
242
243 if (!p->mm)
244 continue;
245
246 if (is_global_init(p))
247 continue;
248 if (mem && !task_in_mem_cgroup(p, mem))
249 continue;
250
251
252
253
254
255
256
257
258
259
260 if (test_tsk_thread_flag(p, TIF_MEMDIE))
261 return ERR_PTR(-1UL);
262
263
264
265
266
267
268
269
270
271
272
273 if (p->flags & PF_EXITING) {
274 if (p != current)
275 return ERR_PTR(-1UL);
276
277 chosen = p;
278 *ppoints = ULONG_MAX;
279 }
280
281 if (p->signal->oom_adj == OOM_DISABLE)
282 continue;
283
284 points = badness(p, uptime.tv_sec);
285 if (points > *ppoints || !chosen) {
286 chosen = p;
287 *ppoints = points;
288 }
289 }
290
291 return chosen;
292}
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307static void dump_tasks(const struct mem_cgroup *mem)
308{
309 struct task_struct *g, *p;
310
311 printk(KERN_INFO "[ pid ] uid tgid total_vm rss cpu oom_adj "
312 "name\n");
313 do_each_thread(g, p) {
314 struct mm_struct *mm;
315
316 if (mem && !task_in_mem_cgroup(p, mem))
317 continue;
318 if (!thread_group_leader(p))
319 continue;
320
321 task_lock(p);
322 mm = p->mm;
323 if (!mm) {
324
325
326
327
328
329 task_unlock(p);
330 continue;
331 }
332 printk(KERN_INFO "[%5d] %5d %5d %8lu %8lu %3d %3d %s\n",
333 p->pid, __task_cred(p)->uid, p->tgid, mm->total_vm,
334 get_mm_rss(mm), (int)task_cpu(p), p->signal->oom_adj,
335 p->comm);
336 task_unlock(p);
337 } while_each_thread(g, p);
338}
339
340
341
342
343
344
345static void __oom_kill_task(struct task_struct *p, int verbose)
346{
347 if (is_global_init(p)) {
348 WARN_ON(1);
349 printk(KERN_WARNING "tried to kill init!\n");
350 return;
351 }
352
353 if (!p->mm) {
354 WARN_ON(1);
355 printk(KERN_WARNING "tried to kill an mm-less task!\n");
356 return;
357 }
358
359 if (verbose)
360 printk(KERN_ERR "Killed process %d (%s)\n",
361 task_pid_nr(p), p->comm);
362
363
364
365
366
367
368 p->rt.time_slice = HZ;
369 set_tsk_thread_flag(p, TIF_MEMDIE);
370
371 force_sig(SIGKILL, p);
372}
373
374static int oom_kill_task(struct task_struct *p)
375{
376
377
378
379
380
381
382
383
384 if (!p->mm || p->signal->oom_adj == OOM_DISABLE)
385 return 1;
386
387 __oom_kill_task(p, 1);
388
389 return 0;
390}
391
392static int oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,
393 unsigned long points, struct mem_cgroup *mem,
394 const char *message)
395{
396 struct task_struct *c;
397
398 if (printk_ratelimit()) {
399 printk(KERN_WARNING "%s invoked oom-killer: "
400 "gfp_mask=0x%x, order=%d, oom_adj=%d\n",
401 current->comm, gfp_mask, order,
402 current->signal->oom_adj);
403 task_lock(current);
404 cpuset_print_task_mems_allowed(current);
405 task_unlock(current);
406 dump_stack();
407 mem_cgroup_print_oom_info(mem, current);
408 show_mem();
409 if (sysctl_oom_dump_tasks)
410 dump_tasks(mem);
411 }
412
413
414
415
416
417 if (p->flags & PF_EXITING) {
418 __oom_kill_task(p, 0);
419 return 0;
420 }
421
422 printk(KERN_ERR "%s: kill process %d (%s) score %li or a child\n",
423 message, task_pid_nr(p), p->comm, points);
424
425
426 list_for_each_entry(c, &p->children, sibling) {
427 if (c->mm == p->mm)
428 continue;
429 if (!oom_kill_task(c))
430 return 0;
431 }
432 return oom_kill_task(p);
433}
434
435#ifdef CONFIG_CGROUP_MEM_RES_CTLR
436void mem_cgroup_out_of_memory(struct mem_cgroup *mem, gfp_t gfp_mask)
437{
438 unsigned long points = 0;
439 struct task_struct *p;
440
441 read_lock(&tasklist_lock);
442retry:
443 p = select_bad_process(&points, mem);
444 if (PTR_ERR(p) == -1UL)
445 goto out;
446
447 if (!p)
448 p = current;
449
450 if (oom_kill_process(p, gfp_mask, 0, points, mem,
451 "Memory cgroup out of memory"))
452 goto retry;
453out:
454 read_unlock(&tasklist_lock);
455}
456#endif
457
458static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
459
460int register_oom_notifier(struct notifier_block *nb)
461{
462 return blocking_notifier_chain_register(&oom_notify_list, nb);
463}
464EXPORT_SYMBOL_GPL(register_oom_notifier);
465
466int unregister_oom_notifier(struct notifier_block *nb)
467{
468 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
469}
470EXPORT_SYMBOL_GPL(unregister_oom_notifier);
471
472
473
474
475
476
477int try_set_zone_oom(struct zonelist *zonelist, gfp_t gfp_mask)
478{
479 struct zoneref *z;
480 struct zone *zone;
481 int ret = 1;
482
483 spin_lock(&zone_scan_lock);
484 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
485 if (zone_is_oom_locked(zone)) {
486 ret = 0;
487 goto out;
488 }
489 }
490
491 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
492
493
494
495
496
497 zone_set_flag(zone, ZONE_OOM_LOCKED);
498 }
499
500out:
501 spin_unlock(&zone_scan_lock);
502 return ret;
503}
504
505
506
507
508
509
510void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
511{
512 struct zoneref *z;
513 struct zone *zone;
514
515 spin_lock(&zone_scan_lock);
516 for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
517 zone_clear_flag(zone, ZONE_OOM_LOCKED);
518 }
519 spin_unlock(&zone_scan_lock);
520}
521
522
523
524
525static void __out_of_memory(gfp_t gfp_mask, int order)
526{
527 struct task_struct *p;
528 unsigned long points;
529
530 if (sysctl_oom_kill_allocating_task)
531 if (!oom_kill_process(current, gfp_mask, order, 0, NULL,
532 "Out of memory (oom_kill_allocating_task)"))
533 return;
534retry:
535
536
537
538
539 p = select_bad_process(&points, NULL);
540
541 if (PTR_ERR(p) == -1UL)
542 return;
543
544
545 if (!p) {
546 read_unlock(&tasklist_lock);
547 panic("Out of memory and no killable processes...\n");
548 }
549
550 if (oom_kill_process(p, gfp_mask, order, points, NULL,
551 "Out of memory"))
552 goto retry;
553}
554
555
556
557
558
559void pagefault_out_of_memory(void)
560{
561 unsigned long freed = 0;
562
563 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
564 if (freed > 0)
565
566 return;
567
568
569
570
571
572 if (mem_cgroup_oom_called(current))
573 goto rest_and_return;
574
575 if (sysctl_panic_on_oom)
576 panic("out of memory from page fault. panic_on_oom is selected.\n");
577
578 read_lock(&tasklist_lock);
579 __out_of_memory(0, 0);
580 read_unlock(&tasklist_lock);
581
582
583
584
585
586rest_and_return:
587 if (!test_thread_flag(TIF_MEMDIE))
588 schedule_timeout_uninterruptible(1);
589}
590
591
592
593
594
595
596
597
598
599
600
601
602void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order)
603{
604 unsigned long freed = 0;
605 enum oom_constraint constraint;
606
607 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
608 if (freed > 0)
609
610 return;
611
612 if (sysctl_panic_on_oom == 2)
613 panic("out of memory. Compulsory panic_on_oom is selected.\n");
614
615
616
617
618
619 constraint = constrained_alloc(zonelist, gfp_mask);
620 read_lock(&tasklist_lock);
621
622 switch (constraint) {
623 case CONSTRAINT_MEMORY_POLICY:
624 oom_kill_process(current, gfp_mask, order, 0, NULL,
625 "No available memory (MPOL_BIND)");
626 break;
627
628 case CONSTRAINT_NONE:
629 if (sysctl_panic_on_oom)
630 panic("out of memory. panic_on_oom is selected\n");
631
632 case CONSTRAINT_CPUSET:
633 __out_of_memory(gfp_mask, order);
634 break;
635 }
636
637 read_unlock(&tasklist_lock);
638
639
640
641
642
643 if (!test_thread_flag(TIF_MEMDIE))
644 schedule_timeout_uninterruptible(1);
645}
646