Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / android / lowmemorykiller.c
blob4c26bcdda7aa8c0883e85494773e7c1966e90edb
1 /* drivers/misc/lowmemorykiller.c
3 * The lowmemorykiller driver lets user-space specify a set of memory thresholds
4 * where processes with a range of oom_adj values will get killed. Specify the
5 * minimum oom_adj values in /sys/module/lowmemorykiller/parameters/adj and the
6 * number of free pages in /sys/module/lowmemorykiller/parameters/minfree. Both
7 * files take a comma separated list of numbers in ascending order.
9 * For example, write "0,8" to /sys/module/lowmemorykiller/parameters/adj and
10 * "1024,4096" to /sys/module/lowmemorykiller/parameters/minfree to kill
11 * processes with a oom_adj value of 8 or higher when the free memory drops
12 * below 4096 pages and kill processes with a oom_adj value of 0 or higher
13 * when the free memory drops below 1024 pages.
15 * The driver considers memory used for caches to be free, but if a large
16 * percentage of the cached memory is locked this can be very inaccurate
17 * and processes may not get killed until the normal oom killer is triggered.
19 * Copyright (C) 2007-2008 Google, Inc.
21 * This software is licensed under the terms of the GNU General Public
22 * License version 2, as published by the Free Software Foundation, and
23 * may be copied, distributed, and modified under those terms.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/mm.h>
35 #include <linux/oom.h>
36 #include <linux/sched.h>
37 #include <linux/profile.h>
38 #include <linux/notifier.h>
40 static uint32_t lowmem_debug_level = 2;
41 static int lowmem_adj[6] = {
45 12,
47 static int lowmem_adj_size = 4;
48 static size_t lowmem_minfree[6] = {
49 3 * 512, /* 6MB */
50 2 * 1024, /* 8MB */
51 4 * 1024, /* 16MB */
52 16 * 1024, /* 64MB */
54 static int lowmem_minfree_size = 4;
56 static unsigned long lowmem_deathpending_timeout;
58 #define lowmem_print(level, x...) \
59 do { \
60 if (lowmem_debug_level >= (level)) \
61 printk(x); \
62 } while (0)
64 static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
66 struct task_struct *p;
67 struct task_struct *selected = NULL;
68 int rem = 0;
69 int tasksize;
70 int i;
71 int min_adj = OOM_ADJUST_MAX + 1;
72 int selected_tasksize = 0;
73 int selected_oom_adj;
74 int array_size = ARRAY_SIZE(lowmem_adj);
75 int other_free = global_page_state(NR_FREE_PAGES);
76 int other_file = global_page_state(NR_FILE_PAGES) -
77 global_page_state(NR_SHMEM);
79 if (lowmem_adj_size < array_size)
80 array_size = lowmem_adj_size;
81 if (lowmem_minfree_size < array_size)
82 array_size = lowmem_minfree_size;
83 for (i = 0; i < array_size; i++) {
84 if (other_free < lowmem_minfree[i] &&
85 other_file < lowmem_minfree[i]) {
86 min_adj = lowmem_adj[i];
87 break;
90 if (sc->nr_to_scan > 0)
91 lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %d\n",
92 sc->nr_to_scan, sc->gfp_mask, other_free,
93 other_file, min_adj);
94 rem = global_page_state(NR_ACTIVE_ANON) +
95 global_page_state(NR_ACTIVE_FILE) +
96 global_page_state(NR_INACTIVE_ANON) +
97 global_page_state(NR_INACTIVE_FILE);
98 if (sc->nr_to_scan <= 0 || min_adj == OOM_ADJUST_MAX + 1) {
99 lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n",
100 sc->nr_to_scan, sc->gfp_mask, rem);
101 return rem;
103 selected_oom_adj = min_adj;
105 read_lock(&tasklist_lock);
106 for_each_process(p) {
107 struct mm_struct *mm;
108 struct signal_struct *sig;
109 int oom_adj;
111 if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
112 time_before_eq(jiffies, lowmem_deathpending_timeout)) {
113 read_unlock(&tasklist_lock);
114 return 0;
116 task_lock(p);
117 mm = p->mm;
118 sig = p->signal;
119 if (!mm || !sig) {
120 task_unlock(p);
121 continue;
123 oom_adj = sig->oom_adj;
124 if (oom_adj < min_adj) {
125 task_unlock(p);
126 continue;
128 tasksize = get_mm_rss(mm);
129 task_unlock(p);
130 if (tasksize <= 0)
131 continue;
132 if (selected) {
133 if (oom_adj < selected_oom_adj)
134 continue;
135 if (oom_adj == selected_oom_adj &&
136 tasksize <= selected_tasksize)
137 continue;
139 selected = p;
140 selected_tasksize = tasksize;
141 selected_oom_adj = oom_adj;
142 lowmem_print(2, "select %d (%s), adj %d, size %d, to kill\n",
143 p->pid, p->comm, oom_adj, tasksize);
145 if (selected) {
146 lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n",
147 selected->pid, selected->comm,
148 selected_oom_adj, selected_tasksize);
149 lowmem_deathpending_timeout = jiffies + HZ;
150 force_sig(SIGKILL, selected);
151 set_tsk_thread_flag(selected, TIF_MEMDIE);
152 rem -= selected_tasksize;
154 lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
155 sc->nr_to_scan, sc->gfp_mask, rem);
156 read_unlock(&tasklist_lock);
157 return rem;
160 static struct shrinker lowmem_shrinker = {
161 .shrink = lowmem_shrink,
162 .seeks = DEFAULT_SEEKS * 16
165 static int __init lowmem_init(void)
167 register_shrinker(&lowmem_shrinker);
168 return 0;
171 static void __exit lowmem_exit(void)
173 unregister_shrinker(&lowmem_shrinker);
176 module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);
177 module_param_array_named(adj, lowmem_adj, int, &lowmem_adj_size,
178 S_IRUGO | S_IWUSR);
179 module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
180 S_IRUGO | S_IWUSR);
181 module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);
183 module_init(lowmem_init);
184 module_exit(lowmem_exit);
186 MODULE_LICENSE("GPL");