Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 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_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. |
| 8 | * |
| 9 | * For example, write "0,8" to /sys/module/lowmemorykiller/parameters/adj and |
Marco Navarra | 3bf5d65 | 2011-12-22 13:28:23 +0100 | [diff] [blame] | 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. |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 14 | * |
| 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. |
| 18 | * |
| 19 | * Copyright (C) 2007-2008 Google, Inc. |
| 20 | * |
| 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. |
| 24 | * |
| 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. |
| 29 | * |
| 30 | */ |
| 31 | |
| 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> |
Anton Vorontsov | 294b271 | 2012-02-06 20:29:41 +0400 | [diff] [blame] | 37 | #include <linux/rcupdate.h> |
San Mehat | 4755b72 | 2010-05-05 11:38:42 -0700 | [diff] [blame] | 38 | #include <linux/profile.h> |
| 39 | #include <linux/notifier.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 40 | |
| 41 | static uint32_t lowmem_debug_level = 2; |
| 42 | static int lowmem_adj[6] = { |
| 43 | 0, |
| 44 | 1, |
| 45 | 6, |
| 46 | 12, |
| 47 | }; |
| 48 | static int lowmem_adj_size = 4; |
| 49 | static size_t lowmem_minfree[6] = { |
| 50 | 3 * 512, /* 6MB */ |
| 51 | 2 * 1024, /* 8MB */ |
| 52 | 4 * 1024, /* 16MB */ |
| 53 | 16 * 1024, /* 64MB */ |
| 54 | }; |
| 55 | static int lowmem_minfree_size = 4; |
| 56 | |
San Mehat | 4755b72 | 2010-05-05 11:38:42 -0700 | [diff] [blame] | 57 | static struct task_struct *lowmem_deathpending; |
| 58 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 59 | #define lowmem_print(level, x...) \ |
| 60 | do { \ |
| 61 | if (lowmem_debug_level >= (level)) \ |
| 62 | printk(x); \ |
| 63 | } while (0) |
| 64 | |
San Mehat | 4755b72 | 2010-05-05 11:38:42 -0700 | [diff] [blame] | 65 | static int |
| 66 | task_notify_func(struct notifier_block *self, unsigned long val, void *data); |
| 67 | |
| 68 | static struct notifier_block task_nb = { |
| 69 | .notifier_call = task_notify_func, |
| 70 | }; |
| 71 | |
| 72 | static int |
| 73 | task_notify_func(struct notifier_block *self, unsigned long val, void *data) |
| 74 | { |
| 75 | struct task_struct *task = data; |
| 76 | if (task == lowmem_deathpending) { |
| 77 | lowmem_deathpending = NULL; |
| 78 | task_handoff_unregister(&task_nb); |
| 79 | } |
| 80 | return NOTIFY_OK; |
| 81 | } |
| 82 | |
Colin Cross | cae9bf1 | 2011-06-22 16:05:47 -0700 | [diff] [blame] | 83 | static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 84 | { |
Anton Vorontsov | 9567000 | 2012-02-06 20:29:47 +0400 | [diff] [blame^] | 85 | struct task_struct *tsk; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 86 | struct task_struct *selected = NULL; |
| 87 | int rem = 0; |
| 88 | int tasksize; |
| 89 | int i; |
| 90 | int min_adj = OOM_ADJUST_MAX + 1; |
| 91 | int selected_tasksize = 0; |
| 92 | int selected_oom_adj; |
| 93 | int array_size = ARRAY_SIZE(lowmem_adj); |
| 94 | int other_free = global_page_state(NR_FREE_PAGES); |
Arve Hjønnevåg | 71b2c82 | 2010-11-23 17:29:04 -0800 | [diff] [blame] | 95 | int other_file = global_page_state(NR_FILE_PAGES) - |
| 96 | global_page_state(NR_SHMEM); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 97 | |
San Mehat | 4755b72 | 2010-05-05 11:38:42 -0700 | [diff] [blame] | 98 | /* |
| 99 | * If we already have a death outstanding, then |
| 100 | * bail out right away; indicating to vmscan |
| 101 | * that we have nothing further to offer on |
| 102 | * this pass. |
| 103 | * |
| 104 | * Note: Currently you need CONFIG_PROFILING |
| 105 | * for this to work correctly. |
| 106 | */ |
| 107 | if (lowmem_deathpending) |
| 108 | return 0; |
| 109 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 110 | if (lowmem_adj_size < array_size) |
| 111 | array_size = lowmem_adj_size; |
| 112 | if (lowmem_minfree_size < array_size) |
| 113 | array_size = lowmem_minfree_size; |
| 114 | for (i = 0; i < array_size; i++) { |
| 115 | if (other_free < lowmem_minfree[i] && |
| 116 | other_file < lowmem_minfree[i]) { |
| 117 | min_adj = lowmem_adj[i]; |
| 118 | break; |
| 119 | } |
| 120 | } |
Colin Cross | cae9bf1 | 2011-06-22 16:05:47 -0700 | [diff] [blame] | 121 | if (sc->nr_to_scan > 0) |
| 122 | lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %d\n", |
Marco Navarra | 3bf5d65 | 2011-12-22 13:28:23 +0100 | [diff] [blame] | 123 | sc->nr_to_scan, sc->gfp_mask, other_free, |
| 124 | other_file, min_adj); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 125 | rem = global_page_state(NR_ACTIVE_ANON) + |
| 126 | global_page_state(NR_ACTIVE_FILE) + |
| 127 | global_page_state(NR_INACTIVE_ANON) + |
| 128 | global_page_state(NR_INACTIVE_FILE); |
Colin Cross | cae9bf1 | 2011-06-22 16:05:47 -0700 | [diff] [blame] | 129 | if (sc->nr_to_scan <= 0 || min_adj == OOM_ADJUST_MAX + 1) { |
| 130 | lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n", |
| 131 | sc->nr_to_scan, sc->gfp_mask, rem); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 132 | return rem; |
| 133 | } |
| 134 | selected_oom_adj = min_adj; |
| 135 | |
Anton Vorontsov | 294b271 | 2012-02-06 20:29:41 +0400 | [diff] [blame] | 136 | rcu_read_lock(); |
Anton Vorontsov | 9567000 | 2012-02-06 20:29:47 +0400 | [diff] [blame^] | 137 | for_each_process(tsk) { |
| 138 | struct task_struct *p; |
Corentin Chary | 23687af | 2009-11-28 09:45:14 +0100 | [diff] [blame] | 139 | struct signal_struct *sig; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 140 | int oom_adj; |
| 141 | |
Anton Vorontsov | 9567000 | 2012-02-06 20:29:47 +0400 | [diff] [blame^] | 142 | p = find_lock_task_mm(tsk); |
| 143 | if (!p) |
| 144 | continue; |
| 145 | |
Corentin Chary | 23687af | 2009-11-28 09:45:14 +0100 | [diff] [blame] | 146 | sig = p->signal; |
Anton Vorontsov | 9567000 | 2012-02-06 20:29:47 +0400 | [diff] [blame^] | 147 | if (!sig) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 148 | task_unlock(p); |
| 149 | continue; |
| 150 | } |
Corentin Chary | 23687af | 2009-11-28 09:45:14 +0100 | [diff] [blame] | 151 | oom_adj = sig->oom_adj; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 152 | if (oom_adj < min_adj) { |
| 153 | task_unlock(p); |
| 154 | continue; |
| 155 | } |
Anton Vorontsov | 9567000 | 2012-02-06 20:29:47 +0400 | [diff] [blame^] | 156 | tasksize = get_mm_rss(p->mm); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 157 | task_unlock(p); |
| 158 | if (tasksize <= 0) |
| 159 | continue; |
| 160 | if (selected) { |
| 161 | if (oom_adj < selected_oom_adj) |
| 162 | continue; |
| 163 | if (oom_adj == selected_oom_adj && |
| 164 | tasksize <= selected_tasksize) |
| 165 | continue; |
| 166 | } |
| 167 | selected = p; |
| 168 | selected_tasksize = tasksize; |
| 169 | selected_oom_adj = oom_adj; |
| 170 | lowmem_print(2, "select %d (%s), adj %d, size %d, to kill\n", |
| 171 | p->pid, p->comm, oom_adj, tasksize); |
| 172 | } |
| 173 | if (selected) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 174 | lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n", |
| 175 | selected->pid, selected->comm, |
| 176 | selected_oom_adj, selected_tasksize); |
San Mehat | 4755b72 | 2010-05-05 11:38:42 -0700 | [diff] [blame] | 177 | /* |
| 178 | * If CONFIG_PROFILING is off, then task_handoff_register() |
| 179 | * is a nop. In that case we don't want to stall the killer |
| 180 | * by setting lowmem_deathpending. |
| 181 | */ |
| 182 | #ifdef CONFIG_PROFILING |
| 183 | lowmem_deathpending = selected; |
| 184 | task_handoff_register(&task_nb); |
| 185 | #endif |
Anton Vorontsov | 294b271 | 2012-02-06 20:29:41 +0400 | [diff] [blame] | 186 | send_sig(SIGKILL, selected, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 187 | rem -= selected_tasksize; |
| 188 | } |
Colin Cross | cae9bf1 | 2011-06-22 16:05:47 -0700 | [diff] [blame] | 189 | lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n", |
| 190 | sc->nr_to_scan, sc->gfp_mask, rem); |
Anton Vorontsov | 294b271 | 2012-02-06 20:29:41 +0400 | [diff] [blame] | 191 | rcu_read_unlock(); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 192 | return rem; |
| 193 | } |
| 194 | |
| 195 | static struct shrinker lowmem_shrinker = { |
| 196 | .shrink = lowmem_shrink, |
| 197 | .seeks = DEFAULT_SEEKS * 16 |
| 198 | }; |
| 199 | |
| 200 | static int __init lowmem_init(void) |
| 201 | { |
| 202 | register_shrinker(&lowmem_shrinker); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static void __exit lowmem_exit(void) |
| 207 | { |
| 208 | unregister_shrinker(&lowmem_shrinker); |
| 209 | } |
| 210 | |
| 211 | module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR); |
| 212 | module_param_array_named(adj, lowmem_adj, int, &lowmem_adj_size, |
| 213 | S_IRUGO | S_IWUSR); |
| 214 | module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size, |
| 215 | S_IRUGO | S_IWUSR); |
| 216 | module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR); |
| 217 | |
| 218 | module_init(lowmem_init); |
| 219 | module_exit(lowmem_exit); |
| 220 | |
| 221 | MODULE_LICENSE("GPL"); |
| 222 | |