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/sched.h>
21#include <linux/swap.h>
22#include <linux/timex.h>
23#include <linux/jiffies.h>
24#include <linux/cpuset.h>
25#include <linux/module.h>
26#include <linux/notifier.h>
27
28int sysctl_panic_on_oom;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50unsigned long badness(struct task_struct *p, unsigned long uptime)
51{
52 unsigned long points, cpu_time, run_time, s;
53 struct mm_struct *mm;
54 struct task_struct *child;
55
56 task_lock(p);
57 mm = p->mm;
58 if (!mm) {
59 task_unlock(p);
60 return 0;
61 }
62
63
64
65
66 points = mm->total_vm;
67
68
69
70
71 task_unlock(p);
72
73
74
75
76 if (p->flags & PF_SWAPOFF)
77 return ULONG_MAX;
78
79
80
81
82
83
84
85
86
87 list_for_each_entry(child, &p->children, sibling) {
88 task_lock(child);
89 if (child->mm != mm && child->mm)
90 points += child->mm->total_vm/2 + 1;
91 task_unlock(child);
92 }
93
94
95
96
97
98
99 cpu_time = (cputime_to_jiffies(p->utime) + cputime_to_jiffies(p->stime))
100 >> (SHIFT_HZ + 3);
101
102 if (uptime >= p->start_time.tv_sec)
103 run_time = (uptime - p->start_time.tv_sec) >> 10;
104 else
105 run_time = 0;
106
107 s = int_sqrt(cpu_time);
108 if (s)
109 points /= s;
110 s = int_sqrt(int_sqrt(run_time));
111 if (s)
112 points /= s;
113
114
115
116
117
118 if (task_nice(p) > 0)
119 points *= 2;
120
121
122
123
124
125 if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
126 p->uid == 0 || p->euid == 0)
127 points /= 4;
128
129
130
131
132
133
134
135 if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
136 points /= 4;
137
138
139
140
141
142
143 if (!cpuset_excl_nodes_overlap(p))
144 points /= 8;
145
146
147
148
149 if (p->oomkilladj) {
150 if (p->oomkilladj > 0) {
151 if (!points)
152 points = 1;
153 points <<= p->oomkilladj;
154 } else
155 points >>= -(p->oomkilladj);
156 }
157
158#ifdef DEBUG
159 printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
160 p->pid, p->comm, points);
161#endif
162 return points;
163}
164
165
166
167
168#define CONSTRAINT_NONE 1
169#define CONSTRAINT_MEMORY_POLICY 2
170#define CONSTRAINT_CPUSET 3
171
172
173
174
175static inline int constrained_alloc(struct zonelist *zonelist, gfp_t gfp_mask)
176{
177#ifdef CONFIG_NUMA
178 struct zone **z;
179 nodemask_t nodes;
180 int node;
181
182 nodes_clear(nodes);
183
184 for_each_online_node(node)
185 if (NODE_DATA(node)->node_present_pages)
186 node_set(node, nodes);
187
188 for (z = zonelist->zones; *z; z++)
189 if (cpuset_zone_allowed_softwall(*z, gfp_mask))
190 node_clear(zone_to_nid(*z), nodes);
191 else
192 return CONSTRAINT_CPUSET;
193
194 if (!nodes_empty(nodes))
195 return CONSTRAINT_MEMORY_POLICY;
196#endif
197
198 return CONSTRAINT_NONE;
199}
200
201
202
203
204
205
206
207static struct task_struct *select_bad_process(unsigned long *ppoints)
208{
209 struct task_struct *g, *p;
210 struct task_struct *chosen = NULL;
211 struct timespec uptime;
212 *ppoints = 0;
213
214 do_posix_clock_monotonic_gettime(&uptime);
215 do_each_thread(g, p) {
216 unsigned long points;
217
218
219
220
221
222 if (!p->mm)
223 continue;
224
225 if (is_init(p))
226 continue;
227
228
229
230
231
232
233
234
235
236
237 if (test_tsk_thread_flag(p, TIF_MEMDIE))
238 return ERR_PTR(-1UL);
239
240
241
242
243
244
245
246
247
248
249
250 if (p->flags & PF_EXITING) {
251 if (p != current)
252 return ERR_PTR(-1UL);
253
254 chosen = p;
255 *ppoints = ULONG_MAX;
256 }
257
258 if (p->oomkilladj == OOM_DISABLE)
259 continue;
260
261 points = badness(p, uptime.tv_sec);
262 if (points > *ppoints || !chosen) {
263 chosen = p;
264 *ppoints = points;
265 }
266 } while_each_thread(g, p);
267
268 return chosen;
269}
270
271
272
273
274
275
276static void __oom_kill_task(struct task_struct *p, int verbose)
277{
278 if (is_init(p)) {
279 WARN_ON(1);
280 printk(KERN_WARNING "tried to kill init!\n");
281 return;
282 }
283
284 if (!p->mm) {
285 WARN_ON(1);
286 printk(KERN_WARNING "tried to kill an mm-less task!\n");
287 return;
288 }
289
290 if (verbose)
291 printk(KERN_ERR "Killed process %d (%s)\n", p->pid, p->comm);
292
293
294
295
296
297
298 p->time_slice = HZ;
299 set_tsk_thread_flag(p, TIF_MEMDIE);
300
301 force_sig(SIGKILL, p);
302}
303
304static int oom_kill_task(struct task_struct *p)
305{
306 struct mm_struct *mm;
307 struct task_struct *g, *q;
308
309 mm = p->mm;
310
311
312
313
314
315
316
317
318
319
320 if (mm == NULL)
321 return 1;
322
323
324
325
326 do_each_thread(g, q) {
327 if (q->mm == mm && q->oomkilladj == OOM_DISABLE)
328 return 1;
329 } while_each_thread(g, q);
330
331 __oom_kill_task(p, 1);
332
333
334
335
336
337
338 do_each_thread(g, q) {
339 if (q->mm == mm && q->tgid != p->tgid)
340 force_sig(SIGKILL, q);
341 } while_each_thread(g, q);
342
343 return 0;
344}
345
346static int oom_kill_process(struct task_struct *p, unsigned long points,
347 const char *message)
348{
349 struct task_struct *c;
350 struct list_head *tsk;
351
352
353
354
355
356 if (p->flags & PF_EXITING) {
357 __oom_kill_task(p, 0);
358 return 0;
359 }
360
361 printk(KERN_ERR "%s: kill process %d (%s) score %li or a child\n",
362 message, p->pid, p->comm, points);
363
364
365 list_for_each(tsk, &p->children) {
366 c = list_entry(tsk, struct task_struct, sibling);
367 if (c->mm == p->mm)
368 continue;
369 if (!oom_kill_task(c))
370 return 0;
371 }
372 return oom_kill_task(p);
373}
374
375static BLOCKING_NOTIFIER_HEAD(oom_notify_list);
376
377int register_oom_notifier(struct notifier_block *nb)
378{
379 return blocking_notifier_chain_register(&oom_notify_list, nb);
380}
381EXPORT_SYMBOL_GPL(register_oom_notifier);
382
383int unregister_oom_notifier(struct notifier_block *nb)
384{
385 return blocking_notifier_chain_unregister(&oom_notify_list, nb);
386}
387EXPORT_SYMBOL_GPL(unregister_oom_notifier);
388
389
390
391
392
393
394
395
396
397void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order)
398{
399 struct task_struct *p;
400 unsigned long points = 0;
401 unsigned long freed = 0;
402 int constraint;
403
404 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
405 if (freed > 0)
406
407 return;
408
409 if (printk_ratelimit()) {
410 printk(KERN_WARNING "%s invoked oom-killer: "
411 "gfp_mask=0x%x, order=%d, oomkilladj=%d\n",
412 current->comm, gfp_mask, order, current->oomkilladj);
413 dump_stack();
414 show_mem();
415 }
416
417 if (sysctl_panic_on_oom == 2)
418 panic("out of memory. Compulsory panic_on_oom is selected.\n");
419
420
421
422
423
424 constraint = constrained_alloc(zonelist, gfp_mask);
425 cpuset_lock();
426 read_lock(&tasklist_lock);
427
428 switch (constraint) {
429 case CONSTRAINT_MEMORY_POLICY:
430 oom_kill_process(current, points,
431 "No available memory (MPOL_BIND)");
432 break;
433
434 case CONSTRAINT_CPUSET:
435 oom_kill_process(current, points,
436 "No available memory in cpuset");
437 break;
438
439 case CONSTRAINT_NONE:
440 if (sysctl_panic_on_oom)
441 panic("out of memory. panic_on_oom is selected\n");
442retry:
443
444
445
446
447 p = select_bad_process(&points);
448
449 if (PTR_ERR(p) == -1UL)
450 goto out;
451
452
453 if (!p) {
454 read_unlock(&tasklist_lock);
455 cpuset_unlock();
456 panic("Out of memory and no killable processes...\n");
457 }
458
459 if (oom_kill_process(p, points, "Out of memory"))
460 goto retry;
461
462 break;
463 }
464
465out:
466 read_unlock(&tasklist_lock);
467 cpuset_unlock();
468
469
470
471
472
473 if (!test_thread_flag(TIF_MEMDIE))
474 schedule_timeout_uninterruptible(1);
475}
476