lowmemorykiller.c revision 1eda5166c7640092f512138be6899d050c3d62ed
1/* drivers/misc/lowmemorykiller.c 2 * 3 * The lowmemorykiller driver lets user-space specify a set of memory thresholds 4 * where processes with a range of oom_score_adj values will get killed. Specify 5 * the minimum oom_score_adj values in 6 * /sys/module/lowmemorykiller/parameters/adj and the number of free pages in 7 * /sys/module/lowmemorykiller/parameters/minfree. Both files take a comma 8 * separated list of numbers in ascending order. 9 * 10 * For example, write "0,8" to /sys/module/lowmemorykiller/parameters/adj and 11 * "1024,4096" to /sys/module/lowmemorykiller/parameters/minfree to kill 12 * processes with a oom_score_adj value of 8 or higher when the free memory 13 * drops below 4096 pages and kill processes with a oom_score_adj value of 0 or 14 * higher when the free memory drops below 1024 pages. 15 * 16 * The driver considers memory used for caches to be free, but if a large 17 * percentage of the cached memory is locked this can be very inaccurate 18 * and processes may not get killed until the normal oom killer is triggered. 19 * 20 * Copyright (C) 2007-2008 Google, Inc. 21 * 22 * This software is licensed under the terms of the GNU General Public 23 * License version 2, as published by the Free Software Foundation, and 24 * may be copied, distributed, and modified under those terms. 25 * 26 * This program is distributed in the hope that it will be useful, 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 29 * GNU General Public License for more details. 30 * 31 */ 32 33#include <linux/module.h> 34#include <linux/kernel.h> 35#include <linux/mm.h> 36#include <linux/oom.h> 37#include <linux/sched.h> 38#include <linux/rcupdate.h> 39#include <linux/profile.h> 40#include <linux/notifier.h> 41 42static uint32_t lowmem_debug_level = 2; 43static int lowmem_adj[6] = { 44 0, 45 1, 46 6, 47 12, 48}; 49static int lowmem_adj_size = 4; 50static size_t lowmem_minfree[6] = { 51 3 * 512, /* 6MB */ 52 2 * 1024, /* 8MB */ 53 4 * 1024, /* 16MB */ 54 16 * 1024, /* 64MB */ 55}; 56static int lowmem_minfree_size = 4; 57 58static struct task_struct *lowmem_deathpending; 59static unsigned long lowmem_deathpending_timeout; 60 61#define lowmem_print(level, x...) \ 62 do { \ 63 if (lowmem_debug_level >= (level)) \ 64 printk(x); \ 65 } while (0) 66 67static int 68task_notify_func(struct notifier_block *self, unsigned long val, void *data); 69 70static struct notifier_block task_nb = { 71 .notifier_call = task_notify_func, 72}; 73 74static int 75task_notify_func(struct notifier_block *self, unsigned long val, void *data) 76{ 77 struct task_struct *task = data; 78 79 if (task == lowmem_deathpending) 80 lowmem_deathpending = NULL; 81 82 return NOTIFY_OK; 83} 84 85static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc) 86{ 87 struct task_struct *tsk; 88 struct task_struct *selected = NULL; 89 int rem = 0; 90 int tasksize; 91 int i; 92 int min_score_adj = OOM_SCORE_ADJ_MAX + 1; 93 int selected_tasksize = 0; 94 int selected_oom_score_adj; 95 int array_size = ARRAY_SIZE(lowmem_adj); 96 int other_free = global_page_state(NR_FREE_PAGES); 97 int other_file = global_page_state(NR_FILE_PAGES) - 98 global_page_state(NR_SHMEM); 99 100 /* 101 * If we already have a death outstanding, then 102 * bail out right away; indicating to vmscan 103 * that we have nothing further to offer on 104 * this pass. 105 * 106 * Note: Currently you need CONFIG_PROFILING 107 * for this to work correctly. 108 */ 109 if (lowmem_deathpending && 110 time_before_eq(jiffies, lowmem_deathpending_timeout)) 111 return 0; 112 113 if (lowmem_adj_size < array_size) 114 array_size = lowmem_adj_size; 115 if (lowmem_minfree_size < array_size) 116 array_size = lowmem_minfree_size; 117 for (i = 0; i < array_size; i++) { 118 if (other_free < lowmem_minfree[i] && 119 other_file < lowmem_minfree[i]) { 120 min_score_adj = lowmem_adj[i]; 121 break; 122 } 123 } 124 if (sc->nr_to_scan > 0) 125 lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %d\n", 126 sc->nr_to_scan, sc->gfp_mask, other_free, 127 other_file, min_score_adj); 128 rem = global_page_state(NR_ACTIVE_ANON) + 129 global_page_state(NR_ACTIVE_FILE) + 130 global_page_state(NR_INACTIVE_ANON) + 131 global_page_state(NR_INACTIVE_FILE); 132 if (sc->nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) { 133 lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n", 134 sc->nr_to_scan, sc->gfp_mask, rem); 135 return rem; 136 } 137 selected_oom_score_adj = min_score_adj; 138 139 rcu_read_lock(); 140 for_each_process(tsk) { 141 struct task_struct *p; 142 int oom_score_adj; 143 144 if (tsk->flags & PF_KTHREAD) 145 continue; 146 147 p = find_lock_task_mm(tsk); 148 if (!p) 149 continue; 150 151 oom_score_adj = p->signal->oom_score_adj; 152 if (oom_score_adj < min_score_adj) { 153 task_unlock(p); 154 continue; 155 } 156 tasksize = get_mm_rss(p->mm); 157 task_unlock(p); 158 if (tasksize <= 0) 159 continue; 160 if (selected) { 161 if (oom_score_adj < selected_oom_score_adj) 162 continue; 163 if (oom_score_adj == selected_oom_score_adj && 164 tasksize <= selected_tasksize) 165 continue; 166 } 167 selected = p; 168 selected_tasksize = tasksize; 169 selected_oom_score_adj = oom_score_adj; 170 lowmem_print(2, "select %d (%s), adj %d, size %d, to kill\n", 171 p->pid, p->comm, oom_score_adj, tasksize); 172 } 173 if (selected) { 174 lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n", 175 selected->pid, selected->comm, 176 selected_oom_score_adj, selected_tasksize); 177 /* 178 * If CONFIG_PROFILING is off, then we don't want to stall 179 * the killer by setting lowmem_deathpending. 180 */ 181#ifdef CONFIG_PROFILING 182 lowmem_deathpending = selected; 183 lowmem_deathpending_timeout = jiffies + HZ; 184#endif 185 send_sig(SIGKILL, selected, 0); 186 rem -= selected_tasksize; 187 } 188 lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n", 189 sc->nr_to_scan, sc->gfp_mask, rem); 190 rcu_read_unlock(); 191 return rem; 192} 193 194static struct shrinker lowmem_shrinker = { 195 .shrink = lowmem_shrink, 196 .seeks = DEFAULT_SEEKS * 16 197}; 198 199static int __init lowmem_init(void) 200{ 201 task_handoff_register(&task_nb); 202 register_shrinker(&lowmem_shrinker); 203 return 0; 204} 205 206static void __exit lowmem_exit(void) 207{ 208 unregister_shrinker(&lowmem_shrinker); 209 task_handoff_unregister(&task_nb); 210} 211 212module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR); 213module_param_array_named(adj, lowmem_adj, int, &lowmem_adj_size, 214 S_IRUGO | S_IWUSR); 215module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size, 216 S_IRUGO | S_IWUSR); 217module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR); 218 219module_init(lowmem_init); 220module_exit(lowmem_exit); 221 222MODULE_LICENSE("GPL"); 223 224