~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/mm/oom_kill.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/swapctl.h>
 22 #include <linux/timex.h>
 23 
 24 /* #define DEBUG */
 25 
 26 /**
 27  * int_sqrt - oom_kill.c internal function, rough approximation to sqrt
 28  * @x: integer of which to calculate the sqrt
 29  * 
 30  * A very rough approximation to the sqrt() function.
 31  */
 32 static unsigned int int_sqrt(unsigned int x)
 33 {
 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);
 38 }       
 39 
 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
 43  *
 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.
 47  *
 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 priniciple
 55  *    of least surprise ... (be careful when you change it)
 56  */
 57 
 58 static int badness(struct task_struct *p)
 59 {
 60         int points, cpu_time, run_time;
 61 
 62         if (!p->mm)
 63                 return 0;
 64         /*
 65          * The memory size of the process is the basis for the badness.
 66          */
 67         points = p->mm->total_vm;
 68 
 69         /*
 70          * CPU time is in seconds and run time is in minutes. There is no
 71          * particular reason for this other than that it turned out to work
 72          * very well in practice. This is not safe against jiffie wraps
 73          * but we don't care _that_ much...
 74          */
 75         cpu_time = (p->times.tms_utime + p->times.tms_stime) >> (SHIFT_HZ + 3);
 76         run_time = (jiffies - p->start_time) >> (SHIFT_HZ + 10);
 77 
 78         points /= int_sqrt(cpu_time);
 79         points /= int_sqrt(int_sqrt(run_time));
 80 
 81         /*
 82          * Niced processes are most likely less important, so double
 83          * their badness points.
 84          */
 85         if (p->nice > 0)
 86                 points *= 2;
 87 
 88         /*
 89          * Superuser processes are usually more important, so we make it
 90          * less likely that we kill those.
 91          */
 92         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
 93                                 p->uid == 0 || p->euid == 0)
 94                 points /= 4;
 95 
 96         /*
 97          * We don't want to kill a process with direct hardware access.
 98          * Not only could that mess up the hardware, but usually users
 99          * tend to only have this flag set on applications they think
100          * of as important.
101          */
102         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
103                 points /= 4;
104 #ifdef DEBUG
105         printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n",
106         p->pid, p->comm, points);
107 #endif
108         return points;
109 }
110 
111 /*
112  * Simple selection loop. We chose the process with the highest
113  * number of 'points'. We need the locks to make sure that the
114  * list of task structs doesn't change while we look the other way.
115  *
116  * (not docbooked, we don't want this one cluttering up the manual)
117  */
118 static struct task_struct * select_bad_process(void)
119 {
120         int maxpoints = 0;
121         struct task_struct *p = NULL;
122         struct task_struct *chosen = NULL;
123 
124         read_lock(&tasklist_lock);
125         for_each_task(p) {
126                 if (p->pid) {
127                         int points = badness(p);
128                         if (points > maxpoints) {
129                                 chosen = p;
130                                 maxpoints = points;
131                         }
132                 }
133         }
134         read_unlock(&tasklist_lock);
135         return chosen;
136 }
137 
138 /**
139  * oom_kill - kill the "best" process when we run out of memory
140  *
141  * If we run out of memory, we have the choice between either
142  * killing a random task (bad), letting the system crash (worse)
143  * OR try to be smart about which process to kill. Note that we
144  * don't have to be perfect here, we just have to be good.
145  *
146  * We must be careful though to never send SIGKILL a process with
147  * CAP_SYS_RAW_IO set, send SIGTERM instead (but it's unlikely that
148  * we select a process with CAP_SYS_RAW_IO set).
149  */
150 void oom_kill(void)
151 {
152 
153         struct task_struct *p = select_bad_process();
154 
155         /* Found nothing?!?! Either we hang forever, or we panic. */
156         if (p == NULL)
157                 panic("Out of memory and no killable processes...\n");
158 
159         printk(KERN_ERR "Out of Memory: Killed process %d (%s).\n", p->pid, p->comm);
160 
161         /*
162          * We give our sacrificial lamb high priority and access to
163          * all the memory it needs. That way it should be able to
164          * exit() and clear out its resources quickly...
165          */
166         p->counter = 5 * HZ;
167         p->flags |= PF_MEMALLOC;
168 
169         /* This process has hardware access, be more careful. */
170         if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO)) {
171                 force_sig(SIGTERM, p);
172         } else {
173                 force_sig(SIGKILL, p);
174         }
175 
176         /*
177          * Make kswapd go out of the way, so "p" has a good chance of
178          * killing itself before someone else gets the chance to ask
179          * for more memory.
180          */
181         current->policy |= SCHED_YIELD;
182         schedule();
183         return;
184 }
185 
186 /**
187  * out_of_memory - is the system out of memory?
188  *
189  * Returns 0 if there is still enough memory left,
190  * 1 when we are out of memory (otherwise).
191  */
192 int out_of_memory(void)
193 {
194         struct sysinfo swp_info;
195 
196         /* Enough free memory?  Not OOM. */
197         if (nr_free_pages() > freepages.min)
198                 return 0;
199 
200         if (nr_free_pages() + nr_inactive_clean_pages() > freepages.low)
201                 return 0;
202 
203         /* Enough swap space left?  Not OOM. */
204         si_swapinfo(&swp_info);
205         if (swp_info.freeswap > 0)
206                 return 0;
207 
208         /* Else... */
209         return 1;
210 }
211 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.