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/config.h>
20 #include <linux/sched.h>
21 #include <linux/swap.h>
22 #include <linux/timex.h>
23 #include <linux/jiffies.h>
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
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.
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)
46 unsigned long badness(struct task_struct *p, unsigned long uptime)
48 unsigned long points, cpu_time, run_time, s;
49 struct list_head *tsk;
55 * The memory size of the process is the basis for the badness.
57 points = p->mm->total_vm;
58 /* FIXME add vserver badness ;) */
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
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;
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.
78 cpu_time = (cputime_to_jiffies(p->utime) + cputime_to_jiffies(p->stime))
81 if (uptime >= p->start_time.tv_sec)
82 run_time = (uptime - p->start_time.tv_sec) >> 10;
86 s = int_sqrt(cpu_time);
89 s = int_sqrt(int_sqrt(run_time));
94 * Niced processes are most likely less important, so double
95 * their badness points.
101 * Superuser processes are usually more important, so we make it
102 * less likely that we kill those.
104 if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
105 p->uid == 0 || p->euid == 0)
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
114 if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
118 * Adjust the score by oomkilladj.
121 if (p->oomkilladj > 0)
122 points <<= p->oomkilladj;
124 points >>= -(p->oomkilladj);
128 printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
129 p->pid, p->comm, points);
134 #if defined(CONFIG_OOM_PANIC) && defined(CONFIG_OOM_KILLER)
135 #warning Only define OOM_PANIC or OOM_KILLER; not both
138 #ifdef CONFIG_OOM_KILLER
140 * Simple selection loop. We chose the process with the highest
141 * number of 'points'. We expect the caller will lock the tasklist.
143 * (not docbooked, we don't want this one cluttering up the manual)
145 static struct task_struct * select_bad_process(void)
147 unsigned long maxpoints = 0;
148 struct task_struct *g, *p;
149 struct task_struct *chosen = NULL;
150 struct timespec uptime;
152 do_posix_clock_monotonic_gettime(&uptime);
154 /* skip the init task with pid == 1 */
155 if (p->pid > 1 && p->oomkilladj != OOM_DISABLE) {
156 unsigned long points;
159 * This is in the process of releasing memory so wait it
160 * to finish before killing some other task by mistake.
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)
168 points = badness(p, uptime.tv_sec);
169 if (points > maxpoints || !chosen) {
174 while_each_thread(g, p);
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).
183 static void __oom_kill_task(task_t *p)
187 printk(KERN_WARNING "tried to kill init!\n");
192 if (!p->mm || p->mm == &init_mm) {
194 printk(KERN_WARNING "tried to kill an mm-less task!\n");
199 printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n", p->pid, p->comm);
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...
207 set_tsk_thread_flag(p, TIF_MEMDIE);
209 force_sig(SIGKILL, p);
212 static struct mm_struct *oom_kill_task(task_t *p)
214 struct mm_struct *mm = get_task_mm(p);
219 if (mm == &init_mm) {
226 * kill all processes that share the ->mm (i.e. all threads),
227 * but are in a different thread group
230 if (q->mm == mm && q->tgid != p->tgid)
232 while_each_thread(g, q);
237 static struct mm_struct *oom_kill_process(struct task_struct *p)
239 struct mm_struct *mm;
240 struct task_struct *c;
241 struct list_head *tsk;
243 /* Try to kill a child first */
244 list_for_each(tsk, &p->children) {
245 c = list_entry(tsk, struct task_struct, sibling);
248 mm = oom_kill_task(c);
252 return oom_kill_task(p);
256 * oom_kill - kill the "best" process when we run out of memory
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.
263 void out_of_memory(unsigned int __nocast gfp_mask)
266 struct mm_struct *mm = NULL;
269 read_lock(&tasklist_lock);
271 p = select_bad_process();
273 if (PTR_ERR(p) == -1UL)
276 /* Found nothing?!?! Either we hang forever, or we panic. */
278 read_unlock(&tasklist_lock);
280 panic("Out of memory and no killable processes...\n");
283 printk("oom-killer: gfp_mask=0x%x\n", gfp_mask);
285 mm = oom_kill_process(p);
290 read_unlock(&tasklist_lock);
295 * Give "p" a good chance of killing itself before we
296 * retry to allocate memory.
298 __set_current_state(TASK_INTERRUPTIBLE);
301 #endif /* CONFIG_OOM_KILLER */
303 #ifdef CONFIG_OOM_PANIC
305 * out_of_memory - panic if the system out of memory?
307 void out_of_memory(unsigned int __nocast gfp_mask)
310 * oom_lock protects out_of_memory()'s static variables.
311 * It's a global lock; this is not performance-critical.
313 static spinlock_t oom_lock = SPIN_LOCK_UNLOCKED;
314 static unsigned long count;
316 spin_lock(&oom_lock);
319 * If we have gotten only a few failures,
320 * we're not really oom.
324 * Ok, really out of memory. Panic.
327 printk("oom-killer: gfp_mask=0x%x\n", gfp_mask);
330 panic("Out Of Memory");
332 spin_unlock(&oom_lock);
334 #endif /* CONFIG_OOM_PANIC */