vserver 2.0 rc7
[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/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
29  * @p: current uptime in seconds
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         /* FIXME add vserver badness ;) */
58
59         /*
60          * Processes which fork a lot of child processes are likely
61          * a good choice. We add the vmsize of the childs if they
62          * have an own mm. This prevents forking servers to flood the
63          * machine with an endless amount of childs
64          */
65         list_for_each(tsk, &p->children) {
66                 struct task_struct *chld;
67                 chld = list_entry(tsk, struct task_struct, sibling);
68                 if (chld->mm != p->mm && chld->mm)
69                         points += chld->mm->total_vm;
70         }
71
72         /*
73          * CPU time is in tens of seconds and run time is in thousands
74          * of seconds. There is no particular reason for this other than
75          * that it turned out to work very well in practice.
76          */
77         cpu_time = (cputime_to_jiffies(p->utime) + cputime_to_jiffies(p->stime))
78                 >> (SHIFT_HZ + 3);
79
80         if (uptime >= p->start_time.tv_sec)
81                 run_time = (uptime - p->start_time.tv_sec) >> 10;
82         else
83                 run_time = 0;
84
85         s = int_sqrt(cpu_time);
86         if (s)
87                 points /= s;
88         s = int_sqrt(int_sqrt(run_time));
89         if (s)
90                 points /= s;
91
92         /*
93          * Niced processes are most likely less important, so double
94          * their badness points.
95          */
96         if (task_nice(p) > 0)
97                 points *= 2;
98
99         /*
100          * Superuser processes are usually more important, so we make it
101          * less likely that we kill those.
102          */
103         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
104                                 p->uid == 0 || p->euid == 0)
105                 points /= 4;
106
107         /*
108          * We don't want to kill a process with direct hardware access.
109          * Not only could that mess up the hardware, but usually users
110          * tend to only have this flag set on applications they think
111          * of as important.
112          */
113         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
114                 points /= 4;
115
116         /*
117          * Adjust the score by oomkilladj.
118          */
119         if (p->oomkilladj) {
120                 if (p->oomkilladj > 0)
121                         points <<= p->oomkilladj;
122                 else
123                         points >>= -(p->oomkilladj);
124         }
125
126 #ifdef DEBUG
127         printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
128         p->pid, p->comm, points);
129 #endif
130         return points;
131 }
132
133 /*
134  * Simple selection loop. We chose the process with the highest
135  * number of 'points'. We expect the caller will lock the tasklist.
136  *
137  * (not docbooked, we don't want this one cluttering up the manual)
138  */
139 static struct task_struct * select_bad_process(void)
140 {
141         unsigned long maxpoints = 0;
142         struct task_struct *g, *p;
143         struct task_struct *chosen = NULL;
144         struct timespec uptime;
145
146         do_posix_clock_monotonic_gettime(&uptime);
147         do_each_thread(g, p)
148                 /* skip the init task with pid == 1 */
149                 if (p->pid > 1 && p->oomkilladj != OOM_DISABLE) {
150                         unsigned long points;
151
152                         /*
153                          * This is in the process of releasing memory so wait it
154                          * to finish before killing some other task by mistake.
155                          */
156                         if ((unlikely(test_tsk_thread_flag(p, TIF_MEMDIE)) || (p->flags & PF_EXITING)) &&
157                             !(p->flags & PF_DEAD))
158                                 return ERR_PTR(-1UL);
159                         if (p->flags & PF_SWAPOFF)
160                                 return p;
161
162                         points = badness(p, uptime.tv_sec);
163                         if (points > maxpoints || !chosen) {
164                                 chosen = p;
165                                 maxpoints = points;
166                         }
167                 }
168         while_each_thread(g, p);
169         return chosen;
170 }
171
172 /**
173  * We must be careful though to never send SIGKILL a process with
174  * CAP_SYS_RAW_IO set, send SIGTERM instead (but it's unlikely that
175  * we select a process with CAP_SYS_RAW_IO set).
176  */
177 static void __oom_kill_task(task_t *p)
178 {
179         if (p->pid == 1) {
180                 WARN_ON(1);
181                 printk(KERN_WARNING "tried to kill init!\n");
182                 return;
183         }
184
185         task_lock(p);
186         if (!p->mm || p->mm == &init_mm) {
187                 WARN_ON(1);
188                 printk(KERN_WARNING "tried to kill an mm-less task!\n");
189                 task_unlock(p);
190                 return;
191         }
192         task_unlock(p);
193         printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n", p->pid, p->comm);
194
195         /*
196          * We give our sacrificial lamb high priority and access to
197          * all the memory it needs. That way it should be able to
198          * exit() and clear out its resources quickly...
199          */
200         p->time_slice = HZ;
201         set_tsk_thread_flag(p, TIF_MEMDIE);
202
203         force_sig(SIGKILL, p);
204 }
205
206 static struct mm_struct *oom_kill_task(task_t *p)
207 {
208         struct mm_struct *mm = get_task_mm(p);
209         task_t * g, * q;
210
211         if (!mm)
212                 return NULL;
213         if (mm == &init_mm) {
214                 mmput(mm);
215                 return NULL;
216         }
217
218         __oom_kill_task(p);
219         /*
220          * kill all processes that share the ->mm (i.e. all threads),
221          * but are in a different thread group
222          */
223         do_each_thread(g, q)
224                 if (q->mm == mm && q->tgid != p->tgid)
225                         __oom_kill_task(q);
226         while_each_thread(g, q);
227
228         return mm;
229 }
230
231 static struct mm_struct *oom_kill_process(struct task_struct *p)
232 {
233         struct mm_struct *mm;
234         struct task_struct *c;
235         struct list_head *tsk;
236
237         /* Try to kill a child first */
238         list_for_each(tsk, &p->children) {
239                 c = list_entry(tsk, struct task_struct, sibling);
240                 if (c->mm == p->mm)
241                         continue;
242                 mm = oom_kill_task(c);
243                 if (mm)
244                         return mm;
245         }
246         return oom_kill_task(p);
247 }
248
249 /**
250  * oom_kill - kill the "best" process when we run out of memory
251  *
252  * If we run out of memory, we have the choice between either
253  * killing a random task (bad), letting the system crash (worse)
254  * OR try to be smart about which process to kill. Note that we
255  * don't have to be perfect here, we just have to be good.
256  */
257 void out_of_memory(unsigned int __nocast gfp_mask)
258 {
259         struct mm_struct *mm = NULL;
260         task_t * p;
261
262         read_lock(&tasklist_lock);
263 retry:
264         p = select_bad_process();
265
266         if (PTR_ERR(p) == -1UL)
267                 goto out;
268
269         /* Found nothing?!?! Either we hang forever, or we panic. */
270         if (!p) {
271                 read_unlock(&tasklist_lock);
272                 show_free_areas();
273                 panic("Out of memory and no killable processes...\n");
274         }
275
276         printk("oom-killer: gfp_mask=0x%x\n", gfp_mask);
277         show_free_areas();
278         mm = oom_kill_process(p);
279         if (!mm)
280                 goto retry;
281
282  out:
283         read_unlock(&tasklist_lock);
284         if (mm)
285                 mmput(mm);
286
287         /*
288          * Give "p" a good chance of killing itself before we
289          * retry to allocate memory.
290          */
291         __set_current_state(TASK_INTERRUPTIBLE);
292         schedule_timeout(1);
293 }