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_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.
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.
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.
20 * Copyright (C) 2007-2008 Google, Inc.
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.
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.
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/module.h>
36 #include <linux/kernel.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>
44 static uint32_t lowmem_debug_level = 1;
45 static short lowmem_adj[6] = {
51 static int lowmem_adj_size = 4;
52 static int lowmem_minfree[6] = {
58 static int lowmem_minfree_size = 4;
60 static unsigned long lowmem_deathpending_timeout;
62 #define lowmem_print(level, x...) \
64 if (lowmem_debug_level >= (level)) \
68 static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
70 struct task_struct *tsk;
71 struct task_struct *selected = NULL;
75 short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
77 int selected_tasksize = 0;
78 short selected_oom_score_adj;
79 int array_size = ARRAY_SIZE(lowmem_adj);
80 int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages -
81 global_page_state(NR_FREE_CMA_PAGES);
82 int other_file = global_page_state(NR_FILE_PAGES) -
83 global_page_state(NR_SHMEM);
85 if (lowmem_adj_size < array_size)
86 array_size = lowmem_adj_size;
87 if (lowmem_minfree_size < array_size)
88 array_size = lowmem_minfree_size;
89 for (i = 0; i < array_size; i++) {
90 minfree = lowmem_minfree[i];
91 if (other_free < minfree && other_file < minfree) {
92 min_score_adj = lowmem_adj[i];
96 if (sc->nr_to_scan > 0)
97 lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %hd\n",
98 sc->nr_to_scan, sc->gfp_mask, other_free,
99 other_file, min_score_adj);
100 rem = global_page_state(NR_ACTIVE_ANON) +
101 global_page_state(NR_ACTIVE_FILE) +
102 global_page_state(NR_INACTIVE_ANON) +
103 global_page_state(NR_INACTIVE_FILE);
104 if (sc->nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
105 lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n",
106 sc->nr_to_scan, sc->gfp_mask, rem);
109 selected_oom_score_adj = min_score_adj;
112 for_each_process(tsk) {
113 struct task_struct *p;
116 if (tsk->flags & PF_KTHREAD)
119 p = find_lock_task_mm(tsk);
123 if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
124 time_before_eq(jiffies, lowmem_deathpending_timeout)) {
129 oom_score_adj = p->signal->oom_score_adj;
130 if (oom_score_adj < min_score_adj) {
134 tasksize = get_mm_rss(p->mm);
139 if (oom_score_adj < selected_oom_score_adj)
141 if (oom_score_adj == selected_oom_score_adj &&
142 tasksize <= selected_tasksize)
146 selected_tasksize = tasksize;
147 selected_oom_score_adj = oom_score_adj;
148 lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n",
149 p->comm, p->pid, oom_score_adj, tasksize);
152 lowmem_print(1, "Killing '%s' (%d), adj %hd,\n" \
153 " to free %ldkB on behalf of '%s' (%d) because\n" \
154 " cache %ldkB is below limit %ldkB for oom_score_adj %hd\n" \
155 " Free memory is %ldkB above reserved\n",
156 selected->comm, selected->pid,
157 selected_oom_score_adj,
158 selected_tasksize * (long)(PAGE_SIZE / 1024),
159 current->comm, current->pid,
160 other_file * (long)(PAGE_SIZE / 1024),
161 minfree * (long)(PAGE_SIZE / 1024),
163 other_free * (long)(PAGE_SIZE / 1024));
164 lowmem_deathpending_timeout = jiffies + HZ;
165 send_sig(SIGKILL, selected, 0);
166 set_tsk_thread_flag(selected, TIF_MEMDIE);
167 rem -= selected_tasksize;
169 lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
170 sc->nr_to_scan, sc->gfp_mask, rem);
175 static struct shrinker lowmem_shrinker = {
176 .shrink = lowmem_shrink,
177 .seeks = DEFAULT_SEEKS * 16
180 static int __init lowmem_init(void)
182 register_shrinker(&lowmem_shrinker);
186 static void __exit lowmem_exit(void)
188 unregister_shrinker(&lowmem_shrinker);
191 #ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES
192 static short lowmem_oom_adj_to_oom_score_adj(short oom_adj)
194 if (oom_adj == OOM_ADJUST_MAX)
195 return OOM_SCORE_ADJ_MAX;
197 return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
200 static void lowmem_autodetect_oom_adj_values(void)
205 int array_size = ARRAY_SIZE(lowmem_adj);
207 if (lowmem_adj_size < array_size)
208 array_size = lowmem_adj_size;
213 oom_adj = lowmem_adj[array_size - 1];
214 if (oom_adj > OOM_ADJUST_MAX)
217 oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj);
218 if (oom_score_adj <= OOM_ADJUST_MAX)
221 lowmem_print(1, "lowmem_shrink: convert oom_adj to oom_score_adj:\n");
222 for (i = 0; i < array_size; i++) {
223 oom_adj = lowmem_adj[i];
224 oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj);
225 lowmem_adj[i] = oom_score_adj;
226 lowmem_print(1, "oom_adj %d => oom_score_adj %d\n",
227 oom_adj, oom_score_adj);
231 static int lowmem_adj_array_set(const char *val, const struct kernel_param *kp)
235 ret = param_array_ops.set(val, kp);
237 /* HACK: Autodetect oom_adj values in lowmem_adj array */
238 lowmem_autodetect_oom_adj_values();
243 static int lowmem_adj_array_get(char *buffer, const struct kernel_param *kp)
245 return param_array_ops.get(buffer, kp);
248 static void lowmem_adj_array_free(void *arg)
250 param_array_ops.free(arg);
253 static struct kernel_param_ops lowmem_adj_array_ops = {
254 .set = lowmem_adj_array_set,
255 .get = lowmem_adj_array_get,
256 .free = lowmem_adj_array_free,
259 static const struct kparam_array __param_arr_adj = {
260 .max = ARRAY_SIZE(lowmem_adj),
261 .num = &lowmem_adj_size,
262 .ops = ¶m_ops_short,
263 .elemsize = sizeof(lowmem_adj[0]),
268 module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);
269 #ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES
270 __module_param_call(MODULE_PARAM_PREFIX, adj,
271 &lowmem_adj_array_ops,
272 .arr = &__param_arr_adj,
273 S_IRUGO | S_IWUSR, -1);
274 __MODULE_PARM_TYPE(adj, "array of short");
276 module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size,
279 module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
281 module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);
283 module_init(lowmem_init);
284 module_exit(lowmem_exit);
286 MODULE_LICENSE("GPL");