Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/mm/oom_kill.c | |
3 | * | |
4 | * Copyright (C) 1998,2000 Rik van Riel | |
5 | * Thanks go out to Claus Fischer for some serious inspiration and | |
6 | * for goading me into coding this file... | |
7 | * | |
8 | * The routines in this file are used to kill a process when | |
a49335cc PJ |
9 | * we're seriously out of memory. This gets called from __alloc_pages() |
10 | * in mm/page_alloc.c when we really run out of memory. | |
1da177e4 LT |
11 | * |
12 | * Since we won't call these routines often (on a well-configured | |
13 | * machine) this file will double as a 'coding guide' and a signpost | |
14 | * for newbie kernel hackers. It features several pointers to major | |
15 | * kernel subsystems and hints as to where to find out what things do. | |
16 | */ | |
17 | ||
18 | #include <linux/mm.h> | |
19 | #include <linux/sched.h> | |
20 | #include <linux/swap.h> | |
21 | #include <linux/timex.h> | |
22 | #include <linux/jiffies.h> | |
23 | ||
24 | /* #define DEBUG */ | |
25 | ||
26 | /** | |
27 | * oom_badness - calculate a numeric value for how bad this task has been | |
28 | * @p: task struct of which task we should calculate | |
a49335cc | 29 | * @uptime: current uptime in seconds |
1da177e4 LT |
30 | * |
31 | * The formula used is relatively simple and documented inline in the | |
32 | * function. The main rationale is that we want to select a good task | |
33 | * to kill when we run out of memory. | |
34 | * | |
35 | * Good in this context means that: | |
36 | * 1) we lose the minimum amount of work done | |
37 | * 2) we recover a large amount of memory | |
38 | * 3) we don't kill anything innocent of eating tons of memory | |
39 | * 4) we want to kill the minimum amount of processes (one) | |
40 | * 5) we try to kill the process the user expects us to kill, this | |
41 | * algorithm has been meticulously tuned to meet the principle | |
42 | * of least surprise ... (be careful when you change it) | |
43 | */ | |
44 | ||
45 | unsigned long badness(struct task_struct *p, unsigned long uptime) | |
46 | { | |
47 | unsigned long points, cpu_time, run_time, s; | |
48 | struct list_head *tsk; | |
49 | ||
50 | if (!p->mm) | |
51 | return 0; | |
52 | ||
53 | /* | |
54 | * The memory size of the process is the basis for the badness. | |
55 | */ | |
56 | points = p->mm->total_vm; | |
57 | ||
58 | /* | |
59 | * Processes which fork a lot of child processes are likely | |
a49335cc | 60 | * a good choice. We add the vmsize of the children if they |
1da177e4 | 61 | * have an own mm. This prevents forking servers to flood the |
a49335cc | 62 | * machine with an endless amount of children |
1da177e4 LT |
63 | */ |
64 | list_for_each(tsk, &p->children) { | |
65 | struct task_struct *chld; | |
66 | chld = list_entry(tsk, struct task_struct, sibling); | |
67 | if (chld->mm != p->mm && chld->mm) | |
68 | points += chld->mm->total_vm; | |
69 | } | |
70 | ||
71 | /* | |
72 | * CPU time is in tens of seconds and run time is in thousands | |
73 | * of seconds. There is no particular reason for this other than | |
74 | * that it turned out to work very well in practice. | |
75 | */ | |
76 | cpu_time = (cputime_to_jiffies(p->utime) + cputime_to_jiffies(p->stime)) | |
77 | >> (SHIFT_HZ + 3); | |
78 | ||
79 | if (uptime >= p->start_time.tv_sec) | |
80 | run_time = (uptime - p->start_time.tv_sec) >> 10; | |
81 | else | |
82 | run_time = 0; | |
83 | ||
84 | s = int_sqrt(cpu_time); | |
85 | if (s) | |
86 | points /= s; | |
87 | s = int_sqrt(int_sqrt(run_time)); | |
88 | if (s) | |
89 | points /= s; | |
90 | ||
91 | /* | |
92 | * Niced processes are most likely less important, so double | |
93 | * their badness points. | |
94 | */ | |
95 | if (task_nice(p) > 0) | |
96 | points *= 2; | |
97 | ||
98 | /* | |
99 | * Superuser processes are usually more important, so we make it | |
100 | * less likely that we kill those. | |
101 | */ | |
102 | if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) || | |
103 | p->uid == 0 || p->euid == 0) | |
104 | points /= 4; | |
105 | ||
106 | /* | |
107 | * We don't want to kill a process with direct hardware access. | |
108 | * Not only could that mess up the hardware, but usually users | |
109 | * tend to only have this flag set on applications they think | |
110 | * of as important. | |
111 | */ | |
112 | if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO)) | |
113 | points /= 4; | |
114 | ||
115 | /* | |
116 | * Adjust the score by oomkilladj. | |
117 | */ | |
118 | if (p->oomkilladj) { | |
119 | if (p->oomkilladj > 0) | |
120 | points <<= p->oomkilladj; | |
121 | else | |
122 | points >>= -(p->oomkilladj); | |
123 | } | |
124 | ||
125 | #ifdef DEBUG | |
126 | printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n", | |
127 | p->pid, p->comm, points); | |
128 | #endif | |
129 | return points; | |
130 | } | |
131 | ||
132 | /* | |
133 | * Simple selection loop. We chose the process with the highest | |
134 | * number of 'points'. We expect the caller will lock the tasklist. | |
135 | * | |
136 | * (not docbooked, we don't want this one cluttering up the manual) | |
137 | */ | |
138 | static struct task_struct * select_bad_process(void) | |
139 | { | |
140 | unsigned long maxpoints = 0; | |
141 | struct task_struct *g, *p; | |
142 | struct task_struct *chosen = NULL; | |
143 | struct timespec uptime; | |
144 | ||
145 | do_posix_clock_monotonic_gettime(&uptime); | |
a49335cc PJ |
146 | do_each_thread(g, p) { |
147 | unsigned long points; | |
148 | int releasing; | |
149 | ||
1da177e4 | 150 | /* skip the init task with pid == 1 */ |
a49335cc PJ |
151 | if (p->pid == 1) |
152 | continue; | |
153 | if (p->oomkilladj == OOM_DISABLE) | |
154 | continue; | |
155 | /* | |
156 | * This is in the process of releasing memory so for wait it | |
157 | * to finish before killing some other task by mistake. | |
158 | */ | |
159 | releasing = test_tsk_thread_flag(p, TIF_MEMDIE) || | |
160 | p->flags & PF_EXITING; | |
161 | if (releasing && !(p->flags & PF_DEAD)) | |
162 | return ERR_PTR(-1UL); | |
163 | if (p->flags & PF_SWAPOFF) | |
164 | return p; | |
165 | ||
166 | points = badness(p, uptime.tv_sec); | |
167 | if (points > maxpoints || !chosen) { | |
168 | chosen = p; | |
169 | maxpoints = points; | |
1da177e4 | 170 | } |
a49335cc | 171 | } while_each_thread(g, p); |
1da177e4 LT |
172 | return chosen; |
173 | } | |
174 | ||
175 | /** | |
176 | * We must be careful though to never send SIGKILL a process with | |
177 | * CAP_SYS_RAW_IO set, send SIGTERM instead (but it's unlikely that | |
178 | * we select a process with CAP_SYS_RAW_IO set). | |
179 | */ | |
180 | static void __oom_kill_task(task_t *p) | |
181 | { | |
182 | if (p->pid == 1) { | |
183 | WARN_ON(1); | |
184 | printk(KERN_WARNING "tried to kill init!\n"); | |
185 | return; | |
186 | } | |
187 | ||
188 | task_lock(p); | |
189 | if (!p->mm || p->mm == &init_mm) { | |
190 | WARN_ON(1); | |
191 | printk(KERN_WARNING "tried to kill an mm-less task!\n"); | |
192 | task_unlock(p); | |
193 | return; | |
194 | } | |
195 | task_unlock(p); | |
a49335cc PJ |
196 | printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n", |
197 | p->pid, p->comm); | |
1da177e4 LT |
198 | |
199 | /* | |
200 | * We give our sacrificial lamb high priority and access to | |
201 | * all the memory it needs. That way it should be able to | |
202 | * exit() and clear out its resources quickly... | |
203 | */ | |
204 | p->time_slice = HZ; | |
205 | set_tsk_thread_flag(p, TIF_MEMDIE); | |
206 | ||
207 | force_sig(SIGKILL, p); | |
208 | } | |
209 | ||
210 | static struct mm_struct *oom_kill_task(task_t *p) | |
211 | { | |
212 | struct mm_struct *mm = get_task_mm(p); | |
213 | task_t * g, * q; | |
214 | ||
215 | if (!mm) | |
216 | return NULL; | |
217 | if (mm == &init_mm) { | |
218 | mmput(mm); | |
219 | return NULL; | |
220 | } | |
221 | ||
222 | __oom_kill_task(p); | |
223 | /* | |
224 | * kill all processes that share the ->mm (i.e. all threads), | |
225 | * but are in a different thread group | |
226 | */ | |
227 | do_each_thread(g, q) | |
228 | if (q->mm == mm && q->tgid != p->tgid) | |
229 | __oom_kill_task(q); | |
230 | while_each_thread(g, q); | |
231 | ||
232 | return mm; | |
233 | } | |
234 | ||
235 | static struct mm_struct *oom_kill_process(struct task_struct *p) | |
236 | { | |
237 | struct mm_struct *mm; | |
238 | struct task_struct *c; | |
239 | struct list_head *tsk; | |
240 | ||
241 | /* Try to kill a child first */ | |
242 | list_for_each(tsk, &p->children) { | |
243 | c = list_entry(tsk, struct task_struct, sibling); | |
244 | if (c->mm == p->mm) | |
245 | continue; | |
246 | mm = oom_kill_task(c); | |
247 | if (mm) | |
248 | return mm; | |
249 | } | |
250 | return oom_kill_task(p); | |
251 | } | |
252 | ||
253 | /** | |
254 | * oom_kill - kill the "best" process when we run out of memory | |
255 | * | |
256 | * If we run out of memory, we have the choice between either | |
257 | * killing a random task (bad), letting the system crash (worse) | |
258 | * OR try to be smart about which process to kill. Note that we | |
259 | * don't have to be perfect here, we just have to be good. | |
260 | */ | |
79b9ce31 | 261 | void out_of_memory(unsigned int __nocast gfp_mask, int order) |
1da177e4 LT |
262 | { |
263 | struct mm_struct *mm = NULL; | |
264 | task_t * p; | |
265 | ||
42639269 AB |
266 | if (printk_ratelimit()) { |
267 | printk("oom-killer: gfp_mask=0x%x, order=%d\n", | |
268 | gfp_mask, order); | |
269 | show_mem(); | |
270 | } | |
578c2fd6 | 271 | |
1da177e4 LT |
272 | read_lock(&tasklist_lock); |
273 | retry: | |
274 | p = select_bad_process(); | |
275 | ||
276 | if (PTR_ERR(p) == -1UL) | |
277 | goto out; | |
278 | ||
279 | /* Found nothing?!?! Either we hang forever, or we panic. */ | |
280 | if (!p) { | |
281 | read_unlock(&tasklist_lock); | |
1da177e4 LT |
282 | panic("Out of memory and no killable processes...\n"); |
283 | } | |
284 | ||
1da177e4 LT |
285 | mm = oom_kill_process(p); |
286 | if (!mm) | |
287 | goto retry; | |
288 | ||
289 | out: | |
290 | read_unlock(&tasklist_lock); | |
291 | if (mm) | |
292 | mmput(mm); | |
293 | ||
294 | /* | |
295 | * Give "p" a good chance of killing itself before we | |
296 | * retry to allocate memory. | |
297 | */ | |
298 | __set_current_state(TASK_INTERRUPTIBLE); | |
299 | schedule_timeout(1); | |
300 | } |