lowmemorykiller.c revision 99150f6a1a015de275a83ca95aa5f4054b409f40
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/swap.h> 39#include <linux/rcupdate.h> 40#include <linux/profile.h> 41#include <linux/notifier.h> 42 43static uint32_t lowmem_debug_level = 1; 44static short lowmem_adj[6] = { 45 0, 46 1, 47 6, 48 12, 49}; 50static int lowmem_adj_size = 4; 51static int lowmem_minfree[6] = { 52 3 * 512, /* 6MB */ 53 2 * 1024, /* 8MB */ 54 4 * 1024, /* 16MB */ 55 16 * 1024, /* 64MB */ 56}; 57static int lowmem_minfree_size = 4; 58 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 lowmem_shrink(struct shrinker *s, struct shrink_control *sc) 68{ 69 struct task_struct *tsk; 70 struct task_struct *selected = NULL; 71 int rem = 0; 72 int tasksize; 73 int i; 74 short min_score_adj = OOM_SCORE_ADJ_MAX + 1; 75 int selected_tasksize = 0; 76 short selected_oom_score_adj; 77 int array_size = ARRAY_SIZE(lowmem_adj); 78 int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages; 79 int other_file = global_page_state(NR_FILE_PAGES) - 80 global_page_state(NR_SHMEM); 81 82 if (lowmem_adj_size < array_size) 83 array_size = lowmem_adj_size; 84 if (lowmem_minfree_size < array_size) 85 array_size = lowmem_minfree_size; 86 for (i = 0; i < array_size; i++) { 87 if (other_free < lowmem_minfree[i] && 88 other_file < lowmem_minfree[i]) { 89 min_score_adj = lowmem_adj[i]; 90 break; 91 } 92 } 93 if (sc->nr_to_scan > 0) 94 lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %hd\n", 95 sc->nr_to_scan, sc->gfp_mask, other_free, 96 other_file, min_score_adj); 97 rem = global_page_state(NR_ACTIVE_ANON) + 98 global_page_state(NR_ACTIVE_FILE) + 99 global_page_state(NR_INACTIVE_ANON) + 100 global_page_state(NR_INACTIVE_FILE); 101 if (sc->nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) { 102 lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n", 103 sc->nr_to_scan, sc->gfp_mask, rem); 104 return rem; 105 } 106 selected_oom_score_adj = min_score_adj; 107 108 rcu_read_lock(); 109 for_each_process(tsk) { 110 struct task_struct *p; 111 short oom_score_adj; 112 113 if (tsk->flags & PF_KTHREAD) 114 continue; 115 116 p = find_lock_task_mm(tsk); 117 if (!p) 118 continue; 119 120 if (test_tsk_thread_flag(p, TIF_MEMDIE) && 121 time_before_eq(jiffies, lowmem_deathpending_timeout)) { 122 task_unlock(p); 123 rcu_read_unlock(); 124 return 0; 125 } 126 oom_score_adj = p->signal->oom_score_adj; 127 if (oom_score_adj < min_score_adj) { 128 task_unlock(p); 129 continue; 130 } 131 tasksize = get_mm_rss(p->mm); 132 task_unlock(p); 133 if (tasksize <= 0) 134 continue; 135 if (selected) { 136 if (oom_score_adj < selected_oom_score_adj) 137 continue; 138 if (oom_score_adj == selected_oom_score_adj && 139 tasksize <= selected_tasksize) 140 continue; 141 } 142 selected = p; 143 selected_tasksize = tasksize; 144 selected_oom_score_adj = oom_score_adj; 145 lowmem_print(2, "select %d (%s), adj %hd, size %d, to kill\n", 146 p->pid, p->comm, oom_score_adj, tasksize); 147 } 148 if (selected) { 149 lowmem_print(1, "send sigkill to %d (%s), adj %hd, size %d\n", 150 selected->pid, selected->comm, 151 selected_oom_score_adj, selected_tasksize); 152 lowmem_deathpending_timeout = jiffies + HZ; 153 send_sig(SIGKILL, selected, 0); 154 set_tsk_thread_flag(selected, TIF_MEMDIE); 155 rem -= selected_tasksize; 156 } 157 lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n", 158 sc->nr_to_scan, sc->gfp_mask, rem); 159 rcu_read_unlock(); 160 return rem; 161} 162 163static struct shrinker lowmem_shrinker = { 164 .shrink = lowmem_shrink, 165 .seeks = DEFAULT_SEEKS * 16 166}; 167 168static int __init lowmem_init(void) 169{ 170 register_shrinker(&lowmem_shrinker); 171 return 0; 172} 173 174static void __exit lowmem_exit(void) 175{ 176 unregister_shrinker(&lowmem_shrinker); 177} 178 179module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR); 180module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size, 181 S_IRUGO | S_IWUSR); 182module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size, 183 S_IRUGO | S_IWUSR); 184module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR); 185 186module_init(lowmem_init); 187module_exit(lowmem_exit); 188 189MODULE_LICENSE("GPL"); 190 191