[PATCH] Add a driver for the Technisat Skystar2 DVB card
[linux-2.6/history.git] / mm / oom_kill.c
blob34ac8e140479349ce638dca592c144bb1351d211
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...
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.
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.
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>
24 /* #define DEBUG */
26 /**
27 * int_sqrt - oom_kill.c internal function, rough approximation to sqrt
28 * @x: integer of which to calculate the sqrt
30 * A very rough approximation to the sqrt() function.
32 static unsigned int int_sqrt(unsigned int x)
34 unsigned int out = x;
35 while (x & ~(unsigned int)1) x >>=2, out >>=1;
36 if (x) out -= out >> 2;
37 return (out ? out : 1);
40 /**
41 * oom_badness - calculate a numeric value for how bad this task has been
42 * @p: task struct of which task we should calculate
44 * The formula used is relatively simple and documented inline in the
45 * function. The main rationale is that we want to select a good task
46 * to kill when we run out of memory.
48 * Good in this context means that:
49 * 1) we lose the minimum amount of work done
50 * 2) we recover a large amount of memory
51 * 3) we don't kill anything innocent of eating tons of memory
52 * 4) we want to kill the minimum amount of processes (one)
53 * 5) we try to kill the process the user expects us to kill, this
54 * algorithm has been meticulously tuned to meet the principle
55 * of least surprise ... (be careful when you change it)
58 static int badness(struct task_struct *p)
60 int points, cpu_time, run_time;
62 if (!p->mm)
63 return 0;
65 if (p->flags & PF_MEMDIE)
66 return 0;
68 * The memory size of the process is the basis for the badness.
70 points = p->mm->total_vm;
73 * CPU time is in seconds and run time is in minutes. There is no
74 * particular reason for this other than that it turned out to work
75 * very well in practice.
77 cpu_time = (p->utime + p->stime) >> (SHIFT_HZ + 3);
78 run_time = (get_jiffies_64() - p->start_time) >> (SHIFT_HZ + 10);
80 points /= int_sqrt(cpu_time);
81 points /= int_sqrt(int_sqrt(run_time));
84 * Niced processes are most likely less important, so double
85 * their badness points.
87 if (task_nice(p) > 0)
88 points *= 2;
91 * Superuser processes are usually more important, so we make it
92 * less likely that we kill those.
94 if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
95 p->uid == 0 || p->euid == 0)
96 points /= 4;
99 * We don't want to kill a process with direct hardware access.
100 * Not only could that mess up the hardware, but usually users
101 * tend to only have this flag set on applications they think
102 * of as important.
104 if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
105 points /= 4;
106 #ifdef DEBUG
107 printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
108 p->pid, p->comm, points);
109 #endif
110 return points;
114 * Simple selection loop. We chose the process with the highest
115 * number of 'points'. We expect the caller will lock the tasklist.
117 * (not docbooked, we don't want this one cluttering up the manual)
119 static struct task_struct * select_bad_process(void)
121 int maxpoints = 0;
122 struct task_struct *g, *p;
123 struct task_struct *chosen = NULL;
125 do_each_thread(g, p)
126 if (p->pid) {
127 int points = badness(p);
128 if (points > maxpoints) {
129 chosen = p;
130 maxpoints = points;
132 if (p->flags & PF_SWAPOFF)
133 return p;
135 while_each_thread(g, p);
136 return chosen;
140 * We must be careful though to never send SIGKILL a process with
141 * CAP_SYS_RAW_IO set, send SIGTERM instead (but it's unlikely that
142 * we select a process with CAP_SYS_RAW_IO set).
144 static void __oom_kill_task(task_t *p)
146 task_lock(p);
147 if (!p->mm || p->mm == &init_mm) {
148 WARN_ON(1);
149 printk(KERN_WARNING "tried to kill an mm-less task!\n");
150 task_unlock(p);
151 return;
153 task_unlock(p);
154 printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n", p->pid, p->comm);
157 * We give our sacrificial lamb high priority and access to
158 * all the memory it needs. That way it should be able to
159 * exit() and clear out its resources quickly...
161 p->time_slice = HZ;
162 p->flags |= PF_MEMALLOC | PF_MEMDIE;
164 /* This process has hardware access, be more careful. */
165 if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO)) {
166 force_sig(SIGTERM, p);
167 } else {
168 force_sig(SIGKILL, p);
172 static struct mm_struct *oom_kill_task(task_t *p)
174 struct mm_struct *mm = get_task_mm(p);
175 if (!mm || mm == &init_mm)
176 return NULL;
177 __oom_kill_task(p);
178 return mm;
183 * oom_kill - kill the "best" process when we run out of memory
185 * If we run out of memory, we have the choice between either
186 * killing a random task (bad), letting the system crash (worse)
187 * OR try to be smart about which process to kill. Note that we
188 * don't have to be perfect here, we just have to be good.
190 static void oom_kill(void)
192 struct mm_struct *mm;
193 struct task_struct *g, *p, *q;
195 read_lock(&tasklist_lock);
196 retry:
197 p = select_bad_process();
199 /* Found nothing?!?! Either we hang forever, or we panic. */
200 if (!p) {
201 show_free_areas();
202 panic("Out of memory and no killable processes...\n");
205 mm = oom_kill_task(p);
206 if (!mm)
207 goto retry;
209 * kill all processes that share the ->mm (i.e. all threads),
210 * but are in a different thread group
212 do_each_thread(g, q)
213 if (q->mm == mm && q->tgid != p->tgid)
214 __oom_kill_task(q);
215 while_each_thread(g, q);
216 if (!p->mm)
217 printk(KERN_INFO "Fixed up OOM kill of mm-less task\n");
218 read_unlock(&tasklist_lock);
219 mmput(mm);
222 * Make kswapd go out of the way, so "p" has a good chance of
223 * killing itself before someone else gets the chance to ask
224 * for more memory.
226 yield();
227 return;
231 * out_of_memory - is the system out of memory?
233 void out_of_memory(void)
236 * oom_lock protects out_of_memory()'s static variables.
237 * It's a global lock; this is not performance-critical.
239 static spinlock_t oom_lock = SPIN_LOCK_UNLOCKED;
240 static unsigned long first, last, count, lastkill;
241 unsigned long now, since;
244 * Enough swap space left? Not OOM.
246 if (nr_swap_pages > 0)
247 return;
249 spin_lock(&oom_lock);
250 now = jiffies;
251 since = now - last;
252 last = now;
255 * If it's been a long time since last failure,
256 * we're not oom.
258 last = now;
259 if (since > 5*HZ)
260 goto reset;
263 * If we haven't tried for at least one second,
264 * we're not really oom.
266 since = now - first;
267 if (since < HZ)
268 goto out_unlock;
271 * If we have gotten only a few failures,
272 * we're not really oom.
274 if (++count < 10)
275 goto out_unlock;
278 * If we just killed a process, wait a while
279 * to give that task a chance to exit. This
280 * avoids killing multiple processes needlessly.
282 since = now - lastkill;
283 if (since < HZ*5)
284 goto out_unlock;
287 * Ok, really out of memory. Kill something.
289 lastkill = now;
291 /* oom_kill() sleeps */
292 spin_unlock(&oom_lock);
293 oom_kill();
294 spin_lock(&oom_lock);
296 reset:
298 * We dropped the lock above, so check to be sure the variable
299 * first only ever increases to prevent false OOM's.
301 if (time_after(now, first))
302 first = now;
303 count = 0;
305 out_unlock:
306 spin_unlock(&oom_lock);