lowmemorykiller.c revision bad385c687f886ef7371628cb239aca6e408708c
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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 34 35#include <linux/module.h> 36#include <linux/kernel.h> 37#include <linux/mm.h> 38#include <linux/oom.h> 39#include <linux/sched.h> 40#include <linux/swap.h> 41#include <linux/rcupdate.h> 42#include <linux/notifier.h> 43 44static uint32_t lowmem_debug_level = 1; 45static short lowmem_adj[6] = { 46 0, 47 1, 48 6, 49 12, 50}; 51static int lowmem_adj_size = 4; 52static int lowmem_minfree[6] = { 53 3 * 512, /* 6MB */ 54 2 * 1024, /* 8MB */ 55 4 * 1024, /* 16MB */ 56 16 * 1024, /* 64MB */ 57}; 58static int lowmem_minfree_size = 4; 59 60static unsigned long lowmem_deathpending_timeout; 61 62#define lowmem_print(level, x...) \ 63 do { \ 64 if (lowmem_debug_level >= (level)) \ 65 pr_info(x); \ 66 } while (0) 67 68static unsigned long lowmem_count(struct shrinker *s, 69 struct shrink_control *sc) 70{ 71 return global_page_state(NR_ACTIVE_ANON) + 72 global_page_state(NR_ACTIVE_FILE) + 73 global_page_state(NR_INACTIVE_ANON) + 74 global_page_state(NR_INACTIVE_FILE); 75} 76 77static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc) 78{ 79 struct task_struct *tsk; 80 struct task_struct *selected = NULL; 81 unsigned long rem = 0; 82 int tasksize; 83 int i; 84 short min_score_adj = OOM_SCORE_ADJ_MAX + 1; 85 int minfree = 0; 86 int selected_tasksize = 0; 87 short selected_oom_score_adj; 88 int array_size = ARRAY_SIZE(lowmem_adj); 89 int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages; 90 int other_file = global_page_state(NR_FILE_PAGES) - 91 global_page_state(NR_SHMEM) - 92 total_swapcache_pages(); 93 94 if (lowmem_adj_size < array_size) 95 array_size = lowmem_adj_size; 96 if (lowmem_minfree_size < array_size) 97 array_size = lowmem_minfree_size; 98 for (i = 0; i < array_size; i++) { 99 minfree = lowmem_minfree[i]; 100 if (other_free < minfree && other_file < minfree) { 101 min_score_adj = lowmem_adj[i]; 102 break; 103 } 104 } 105 106 lowmem_print(3, "lowmem_scan %lu, %x, ofree %d %d, ma %hd\n", 107 sc->nr_to_scan, sc->gfp_mask, other_free, 108 other_file, min_score_adj); 109 110 if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) { 111 lowmem_print(5, "lowmem_scan %lu, %x, return 0\n", 112 sc->nr_to_scan, sc->gfp_mask); 113 return 0; 114 } 115 116 selected_oom_score_adj = min_score_adj; 117 118 rcu_read_lock(); 119 for_each_process(tsk) { 120 struct task_struct *p; 121 short oom_score_adj; 122 123 if (tsk->flags & PF_KTHREAD) 124 continue; 125 126 p = find_lock_task_mm(tsk); 127 if (!p) 128 continue; 129 130 if (test_tsk_thread_flag(p, TIF_MEMDIE) && 131 time_before_eq(jiffies, lowmem_deathpending_timeout)) { 132 task_unlock(p); 133 rcu_read_unlock(); 134 return 0; 135 } 136 oom_score_adj = p->signal->oom_score_adj; 137 if (oom_score_adj < min_score_adj) { 138 task_unlock(p); 139 continue; 140 } 141 tasksize = get_mm_rss(p->mm); 142 task_unlock(p); 143 if (tasksize <= 0) 144 continue; 145 if (selected) { 146 if (oom_score_adj < selected_oom_score_adj) 147 continue; 148 if (oom_score_adj == selected_oom_score_adj && 149 tasksize <= selected_tasksize) 150 continue; 151 } 152 selected = p; 153 selected_tasksize = tasksize; 154 selected_oom_score_adj = oom_score_adj; 155 lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n", 156 p->comm, p->pid, oom_score_adj, tasksize); 157 } 158 if (selected) { 159 lowmem_print(1, "Killing '%s' (%d), adj %hd,\n" \ 160 " to free %ldkB on behalf of '%s' (%d) because\n" \ 161 " cache %ldkB is below limit %ldkB for oom_score_adj %hd\n" \ 162 " Free memory is %ldkB above reserved\n", 163 selected->comm, selected->pid, 164 selected_oom_score_adj, 165 selected_tasksize * (long)(PAGE_SIZE / 1024), 166 current->comm, current->pid, 167 other_file * (long)(PAGE_SIZE / 1024), 168 minfree * (long)(PAGE_SIZE / 1024), 169 min_score_adj, 170 other_free * (long)(PAGE_SIZE / 1024)); 171 lowmem_deathpending_timeout = jiffies + HZ; 172 set_tsk_thread_flag(selected, TIF_MEMDIE); 173 send_sig(SIGKILL, selected, 0); 174 rem += selected_tasksize; 175 } 176 177 lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n", 178 sc->nr_to_scan, sc->gfp_mask, rem); 179 rcu_read_unlock(); 180 return rem; 181} 182 183static struct shrinker lowmem_shrinker = { 184 .scan_objects = lowmem_scan, 185 .count_objects = lowmem_count, 186 .seeks = DEFAULT_SEEKS * 16 187}; 188 189static int __init lowmem_init(void) 190{ 191 register_shrinker(&lowmem_shrinker); 192 return 0; 193} 194 195static void __exit lowmem_exit(void) 196{ 197 unregister_shrinker(&lowmem_shrinker); 198} 199 200#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES 201static short lowmem_oom_adj_to_oom_score_adj(short oom_adj) 202{ 203 if (oom_adj == OOM_ADJUST_MAX) 204 return OOM_SCORE_ADJ_MAX; 205 else 206 return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; 207} 208 209static void lowmem_autodetect_oom_adj_values(void) 210{ 211 int i; 212 short oom_adj; 213 short oom_score_adj; 214 int array_size = ARRAY_SIZE(lowmem_adj); 215 216 if (lowmem_adj_size < array_size) 217 array_size = lowmem_adj_size; 218 219 if (array_size <= 0) 220 return; 221 222 oom_adj = lowmem_adj[array_size - 1]; 223 if (oom_adj > OOM_ADJUST_MAX) 224 return; 225 226 oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj); 227 if (oom_score_adj <= OOM_ADJUST_MAX) 228 return; 229 230 lowmem_print(1, "lowmem_shrink: convert oom_adj to oom_score_adj:\n"); 231 for (i = 0; i < array_size; i++) { 232 oom_adj = lowmem_adj[i]; 233 oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj); 234 lowmem_adj[i] = oom_score_adj; 235 lowmem_print(1, "oom_adj %d => oom_score_adj %d\n", 236 oom_adj, oom_score_adj); 237 } 238} 239 240static int lowmem_adj_array_set(const char *val, const struct kernel_param *kp) 241{ 242 int ret; 243 244 ret = param_array_ops.set(val, kp); 245 246 /* HACK: Autodetect oom_adj values in lowmem_adj array */ 247 lowmem_autodetect_oom_adj_values(); 248 249 return ret; 250} 251 252static int lowmem_adj_array_get(char *buffer, const struct kernel_param *kp) 253{ 254 return param_array_ops.get(buffer, kp); 255} 256 257static void lowmem_adj_array_free(void *arg) 258{ 259 param_array_ops.free(arg); 260} 261 262static struct kernel_param_ops lowmem_adj_array_ops = { 263 .set = lowmem_adj_array_set, 264 .get = lowmem_adj_array_get, 265 .free = lowmem_adj_array_free, 266}; 267 268static const struct kparam_array __param_arr_adj = { 269 .max = ARRAY_SIZE(lowmem_adj), 270 .num = &lowmem_adj_size, 271 .ops = ¶m_ops_short, 272 .elemsize = sizeof(lowmem_adj[0]), 273 .elem = lowmem_adj, 274}; 275#endif 276 277module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR); 278#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES 279__module_param_call(MODULE_PARAM_PREFIX, adj, 280 &lowmem_adj_array_ops, 281 .arr = &__param_arr_adj, 282 S_IRUGO | S_IWUSR, -1); 283__MODULE_PARM_TYPE(adj, "array of short"); 284#else 285module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size, 286 S_IRUGO | S_IWUSR); 287#endif 288module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size, 289 S_IRUGO | S_IWUSR); 290module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR); 291 292module_init(lowmem_init); 293module_exit(lowmem_exit); 294 295MODULE_LICENSE("GPL"); 296 297