vserver 1.9.3
[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 static unsigned long badness(struct task_struct *p, unsigned long uptime)
46 {
47         unsigned long points, cpu_time, run_time, s;
48
49         if (!p->mm)
50                 return 0;
51
52         if (p->flags & PF_MEMDIE)
53                 return 0;
54         /*
55          * The memory size of the process is the basis for the badness.
56          */
57         points = p->mm->total_vm;
58         /* add vserver badness ;) */
59
60         /*
61          * CPU time is in tens of seconds and run time is in thousands
62          * of seconds. There is no particular reason for this other than
63          * that it turned out to work very well in practice.
64          */
65         cpu_time = (p->utime + p->stime) >> (SHIFT_HZ + 3);
66
67         if (uptime >= p->start_time.tv_sec)
68                 run_time = (uptime - p->start_time.tv_sec) >> 10;
69         else
70                 run_time = 0;
71
72         s = int_sqrt(cpu_time);
73         if (s)
74                 points /= s;
75         s = int_sqrt(int_sqrt(run_time));
76         if (s)
77                 points /= s;
78
79         /*
80          * Niced processes are most likely less important, so double
81          * their badness points.
82          */
83         if (task_nice(p) > 0)
84                 points *= 2;
85
86         /*
87          * Superuser processes are usually more important, so we make it
88          * less likely that we kill those.
89          */
90         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
91                                 p->uid == 0 || p->euid == 0)
92                 points /= 4;
93
94         /*
95          * We don't want to kill a process with direct hardware access.
96          * Not only could that mess up the hardware, but usually users
97          * tend to only have this flag set on applications they think
98          * of as important.
99          */
100         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
101                 points /= 4;
102 #ifdef DEBUG
103         printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
104         p->pid, p->comm, points);
105 #endif
106         return points;
107 }
108
109 /*
110  * Simple selection loop. We chose the process with the highest
111  * number of 'points'. We expect the caller will lock the tasklist.
112  *
113  * (not docbooked, we don't want this one cluttering up the manual)
114  */
115 static struct task_struct * select_bad_process(void)
116 {
117         unsigned long maxpoints = 0;
118         struct task_struct *g, *p;
119         struct task_struct *chosen = NULL;
120         struct timespec uptime;
121
122         do_posix_clock_monotonic_gettime(&uptime);
123         do_each_thread(g, p)
124                 if (p->pid) {
125                         unsigned long points = badness(p, uptime.tv_sec);
126                         if (points > maxpoints) {
127                                 chosen = p;
128                                 maxpoints = points;
129                         }
130                         if (p->flags & PF_SWAPOFF)
131                                 return p;
132                 }
133         while_each_thread(g, p);
134         return chosen;
135 }
136
137 /**
138  * We must be careful though to never send SIGKILL a process with
139  * CAP_SYS_RAW_IO set, send SIGTERM instead (but it's unlikely that
140  * we select a process with CAP_SYS_RAW_IO set).
141  */
142 static void __oom_kill_task(task_t *p)
143 {
144         task_lock(p);
145         if (!p->mm || p->mm == &init_mm) {
146                 WARN_ON(1);
147                 printk(KERN_WARNING "tried to kill an mm-less task!\n");
148                 task_unlock(p);
149                 return;
150         }
151         task_unlock(p);
152         printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n", p->pid, p->comm);
153
154         /*
155          * We give our sacrificial lamb high priority and access to
156          * all the memory it needs. That way it should be able to
157          * exit() and clear out its resources quickly...
158          */
159         p->time_slice = HZ;
160         p->flags |= PF_MEMALLOC | PF_MEMDIE;
161
162         /* This process has hardware access, be more careful. */
163         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO)) {
164                 force_sig(SIGTERM, p);
165         } else {
166                 force_sig(SIGKILL, p);
167         }
168 }
169
170 static struct mm_struct *oom_kill_task(task_t *p)
171 {
172         struct mm_struct *mm = get_task_mm(p);
173         if (!mm || mm == &init_mm)
174                 return NULL;
175         __oom_kill_task(p);
176         return mm;
177 }
178
179
180 /**
181  * oom_kill - kill the "best" process when we run out of memory
182  *
183  * If we run out of memory, we have the choice between either
184  * killing a random task (bad), letting the system crash (worse)
185  * OR try to be smart about which process to kill. Note that we
186  * don't have to be perfect here, we just have to be good.
187  */
188 static void oom_kill(void)
189 {
190         struct mm_struct *mm;
191         struct task_struct *g, *p, *q;
192         
193         read_lock(&tasklist_lock);
194 retry:
195         p = select_bad_process();
196
197         /* Found nothing?!?! Either we hang forever, or we panic. */
198         if (!p) {
199                 show_free_areas();
200                 panic("Out of memory and no killable processes...\n");
201         }
202
203         mm = oom_kill_task(p);
204         if (!mm)
205                 goto retry;
206         /*
207          * kill all processes that share the ->mm (i.e. all threads),
208          * but are in a different thread group
209          */
210         do_each_thread(g, q)
211                 if (q->mm == mm && q->tgid != p->tgid)
212                         __oom_kill_task(q);
213         while_each_thread(g, q);
214         if (!p->mm)
215                 printk(KERN_INFO "Fixed up OOM kill of mm-less task\n");
216         read_unlock(&tasklist_lock);
217         mmput(mm);
218
219         /*
220          * Make kswapd go out of the way, so "p" has a good chance of
221          * killing itself before someone else gets the chance to ask
222          * for more memory.
223          */
224         yield();
225         return;
226 }
227
228 /**
229  * out_of_memory - is the system out of memory?
230  */
231 void out_of_memory(int gfp_mask)
232 {
233         /*
234          * oom_lock protects out_of_memory()'s static variables.
235          * It's a global lock; this is not performance-critical.
236          */
237         static spinlock_t oom_lock = SPIN_LOCK_UNLOCKED;
238         static unsigned long first, last, count, lastkill;
239         unsigned long now, since;
240
241         spin_lock(&oom_lock);
242         now = jiffies;
243         since = now - last;
244         last = now;
245
246         /*
247          * If it's been a long time since last failure,
248          * we're not oom.
249          */
250         if (since > 5*HZ)
251                 goto reset;
252
253         /*
254          * If we haven't tried for at least one second,
255          * we're not really oom.
256          */
257         since = now - first;
258         if (since < HZ)
259                 goto out_unlock;
260
261         /*
262          * If we have gotten only a few failures,
263          * we're not really oom. 
264          */
265         if (++count < 10)
266                 goto out_unlock;
267
268         /*
269          * If we just killed a process, wait a while
270          * to give that task a chance to exit. This
271          * avoids killing multiple processes needlessly.
272          */
273         since = now - lastkill;
274         if (since < HZ*5)
275                 goto out_unlock;
276
277         /*
278          * Ok, really out of memory. Kill something.
279          */
280         lastkill = now;
281
282         printk("oom-killer: gfp_mask=0x%x\n", gfp_mask);
283         show_free_areas();
284
285         /* oom_kill() sleeps */
286         spin_unlock(&oom_lock);
287         oom_kill();
288         spin_lock(&oom_lock);
289
290 reset:
291         /*
292          * We dropped the lock above, so check to be sure the variable
293          * first only ever increases to prevent false OOM's.
294          */
295         if (time_after(now, first))
296                 first = now;
297         count = 0;
298
299 out_unlock:
300         spin_unlock(&oom_lock);
301 }