- build UP config last (hack! we have to figure out a way to build vnet
[linux-2.6.git] / mm / oom_kill.c
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
9  *  we're seriously out of memory. This gets called from kswapd()
10  *  in linux/mm/vmscan.c when we really run out of memory.
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/config.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
25 /* #define DEBUG */
26
27 /**
28  * oom_badness - calculate a numeric value for how bad this task has been
29  * @p: task struct of which task we should calculate
30  * @p: current uptime in seconds
31  *
32  * The formula used is relatively simple and documented inline in the
33  * function. The main rationale is that we want to select a good task
34  * to kill when we run out of memory.
35  *
36  * Good in this context means that:
37  * 1) we lose the minimum amount of work done
38  * 2) we recover a large amount of memory
39  * 3) we don't kill anything innocent of eating tons of memory
40  * 4) we want to kill the minimum amount of processes (one)
41  * 5) we try to kill the process the user expects us to kill, this
42  *    algorithm has been meticulously tuned to meet the principle
43  *    of least surprise ... (be careful when you change it)
44  */
45
46 unsigned long badness(struct task_struct *p, unsigned long uptime)
47 {
48         unsigned long points, cpu_time, run_time, s;
49         struct list_head *tsk;
50
51         if (!p->mm)
52                 return 0;
53
54         /*
55          * The memory size of the process is the basis for the badness.
56          */
57         points = p->mm->total_vm;
58         /* FIXME add vserver badness ;) */
59
60         /*
61          * Processes which fork a lot of child processes are likely
62          * a good choice. We add the vmsize of the childs if they
63          * have an own mm. This prevents forking servers to flood the
64          * machine with an endless amount of childs
65          */
66         list_for_each(tsk, &p->children) {
67                 struct task_struct *chld;
68                 chld = list_entry(tsk, struct task_struct, sibling);
69                 if (chld->mm != p->mm && chld->mm)
70                         points += chld->mm->total_vm;
71         }
72
73         /*
74          * CPU time is in tens of seconds and run time is in thousands
75          * of seconds. There is no particular reason for this other than
76          * that it turned out to work very well in practice.
77          */
78         cpu_time = (cputime_to_jiffies(p->utime) + cputime_to_jiffies(p->stime))
79                 >> (SHIFT_HZ + 3);
80
81         if (uptime >= p->start_time.tv_sec)
82                 run_time = (uptime - p->start_time.tv_sec) >> 10;
83         else
84                 run_time = 0;
85
86         s = int_sqrt(cpu_time);
87         if (s)
88                 points /= s;
89         s = int_sqrt(int_sqrt(run_time));
90         if (s)
91                 points /= s;
92
93         /*
94          * Niced processes are most likely less important, so double
95          * their badness points.
96          */
97         if (task_nice(p) > 0)
98                 points *= 2;
99
100         /*
101          * Superuser processes are usually more important, so we make it
102          * less likely that we kill those.
103          */
104         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
105                                 p->uid == 0 || p->euid == 0)
106                 points /= 4;
107
108         /*
109          * We don't want to kill a process with direct hardware access.
110          * Not only could that mess up the hardware, but usually users
111          * tend to only have this flag set on applications they think
112          * of as important.
113          */
114         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
115                 points /= 4;
116
117         /*
118          * Adjust the score by oomkilladj.
119          */
120         if (p->oomkilladj) {
121                 if (p->oomkilladj > 0)
122                         points <<= p->oomkilladj;
123                 else
124                         points >>= -(p->oomkilladj);
125         }
126
127 #ifdef DEBUG
128         printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
129         p->pid, p->comm, points);
130 #endif
131         return points;
132 }
133
134 #if defined(CONFIG_OOM_PANIC) && defined(CONFIG_OOM_KILLER)
135 #warning Only define OOM_PANIC or OOM_KILLER; not both
136 #endif
137
138 #ifdef CONFIG_OOM_KILLER
139 /*
140  * Simple selection loop. We chose the process with the highest
141  * number of 'points'. We expect the caller will lock the tasklist.
142  *
143  * (not docbooked, we don't want this one cluttering up the manual)
144  */
145 static struct task_struct * select_bad_process(void)
146 {
147         unsigned long maxpoints = 0;
148         struct task_struct *g, *p;
149         struct task_struct *chosen = NULL;
150         struct timespec uptime;
151
152         do_posix_clock_monotonic_gettime(&uptime);
153         do_each_thread(g, p)
154                 /* skip the init task with pid == 1 */
155                 if (p->pid > 1 && p->oomkilladj != OOM_DISABLE) {
156                         unsigned long points;
157
158                         /*
159                          * This is in the process of releasing memory so wait it
160                          * to finish before killing some other task by mistake.
161                          */
162                         if ((unlikely(test_tsk_thread_flag(p, TIF_MEMDIE)) || (p->flags & PF_EXITING)) &&
163                             !(p->flags & PF_DEAD))
164                                 return ERR_PTR(-1UL);
165                         if (p->flags & PF_SWAPOFF)
166                                 return p;
167
168                         points = badness(p, uptime.tv_sec);
169                         if (points > maxpoints || !chosen) {
170                                 chosen = p;
171                                 maxpoints = points;
172                         }
173                 }
174         while_each_thread(g, p);
175         return chosen;
176 }
177
178 /**
179  * We must be careful though to never send SIGKILL a process with
180  * CAP_SYS_RAW_IO set, send SIGTERM instead (but it's unlikely that
181  * we select a process with CAP_SYS_RAW_IO set).
182  */
183 static void __oom_kill_task(task_t *p)
184 {
185         if (p->pid == 1) {
186                 WARN_ON(1);
187                 printk(KERN_WARNING "tried to kill init!\n");
188                 return;
189         }
190
191         task_lock(p);
192         if (!p->mm || p->mm == &init_mm) {
193                 WARN_ON(1);
194                 printk(KERN_WARNING "tried to kill an mm-less task!\n");
195                 task_unlock(p);
196                 return;
197         }
198         task_unlock(p);
199         printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n", p->pid, p->comm);
200
201         /*
202          * We give our sacrificial lamb high priority and access to
203          * all the memory it needs. That way it should be able to
204          * exit() and clear out its resources quickly...
205          */
206         p->time_slice = HZ;
207         set_tsk_thread_flag(p, TIF_MEMDIE);
208
209         force_sig(SIGKILL, p);
210 }
211
212 static struct mm_struct *oom_kill_task(task_t *p)
213 {
214         struct mm_struct *mm = get_task_mm(p);
215         task_t * g, * q;
216
217         if (!mm)
218                 return NULL;
219         if (mm == &init_mm) {
220                 mmput(mm);
221                 return NULL;
222         }
223
224         __oom_kill_task(p);
225         /*
226          * kill all processes that share the ->mm (i.e. all threads),
227          * but are in a different thread group
228          */
229         do_each_thread(g, q)
230                 if (q->mm == mm && q->tgid != p->tgid)
231                         __oom_kill_task(q);
232         while_each_thread(g, q);
233
234         return mm;
235 }
236
237 static struct mm_struct *oom_kill_process(struct task_struct *p)
238 {
239         struct mm_struct *mm;
240         struct task_struct *c;
241         struct list_head *tsk;
242
243         /* Try to kill a child first */
244         list_for_each(tsk, &p->children) {
245                 c = list_entry(tsk, struct task_struct, sibling);
246                 if (c->mm == p->mm)
247                         continue;
248                 mm = oom_kill_task(c);
249                 if (mm)
250                         return mm;
251         }
252         return oom_kill_task(p);
253 }
254
255 /**
256  * oom_kill - kill the "best" process when we run out of memory
257  *
258  * If we run out of memory, we have the choice between either
259  * killing a random task (bad), letting the system crash (worse)
260  * OR try to be smart about which process to kill. Note that we
261  * don't have to be perfect here, we just have to be good.
262  */
263 void out_of_memory(unsigned int __nocast gfp_mask)
264 {
265
266         struct mm_struct *mm = NULL;
267         task_t * p;
268
269         read_lock(&tasklist_lock);
270 retry:
271         p = select_bad_process();
272
273         if (PTR_ERR(p) == -1UL)
274                 goto out;
275
276         /* Found nothing?!?! Either we hang forever, or we panic. */
277         if (!p) {
278                 read_unlock(&tasklist_lock);
279                 show_mem();
280                 panic("Out of memory and no killable processes...\n");
281         }
282
283         printk("oom-killer: gfp_mask=0x%x\n", gfp_mask);
284         show_mem();
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 }
301 #endif /* CONFIG_OOM_KILLER */
302
303 #ifdef CONFIG_OOM_PANIC
304 /**
305  * out_of_memory - panic if the system out of memory?
306  */
307 void out_of_memory(unsigned int __nocast gfp_mask)
308 {
309         /*
310          * oom_lock protects out_of_memory()'s static variables.
311          * It's a global lock; this is not performance-critical.
312          */
313         static spinlock_t oom_lock = SPIN_LOCK_UNLOCKED;
314         static unsigned long count;
315
316         spin_lock(&oom_lock);
317
318         /*
319          * If we have gotten only a few failures,
320          * we're not really oom. 
321          */
322         if (++count >= 10) {
323                 /*
324                  * Ok, really out of memory. Panic.
325                  */
326
327                 printk("oom-killer: gfp_mask=0x%x\n", gfp_mask);
328                 show_free_areas();
329
330                 panic("Out Of Memory");
331         }
332         spin_unlock(&oom_lock);
333 }
334 #endif /*  CONFIG_OOM_PANIC */