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/init.h>
36 #include <linux/moduleparam.h>
37 #include <linux/kernel.h>
39 #include <linux/oom.h>
40 #include <linux/sched.h>
41 #include <linux/swap.h>
42 #include <linux/rcupdate.h>
43 #include <linux/profile.h>
44 #include <linux/notifier.h>
46 #define CREATE_TRACE_POINTS
47 #include "trace/lowmemorykiller.h"
49 static uint32_t lowmem_debug_level = 1;
50 static short lowmem_adj[6] = {
56 static int lowmem_adj_size = 4;
57 static int lowmem_minfree[6] = {
63 static int lowmem_minfree_size = 4;
65 static unsigned long lowmem_deathpending_timeout;
67 #define lowmem_print(level, x...) \
69 if (lowmem_debug_level >= (level)) \
73 static unsigned long lowmem_count(struct shrinker *s,
74 struct shrink_control *sc)
76 return global_page_state(NR_ACTIVE_ANON) +
77 global_page_state(NR_ACTIVE_FILE) +
78 global_page_state(NR_INACTIVE_ANON) +
79 global_page_state(NR_INACTIVE_FILE);
82 static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
84 struct task_struct *tsk;
85 struct task_struct *selected = NULL;
86 unsigned long rem = 0;
89 short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
91 int selected_tasksize = 0;
92 short selected_oom_score_adj;
93 int array_size = ARRAY_SIZE(lowmem_adj);
94 int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
95 int other_file = global_page_state(NR_FILE_PAGES) -
96 global_page_state(NR_SHMEM) -
97 total_swapcache_pages();
99 if (lowmem_adj_size < array_size)
100 array_size = lowmem_adj_size;
101 if (lowmem_minfree_size < array_size)
102 array_size = lowmem_minfree_size;
103 for (i = 0; i < array_size; i++) {
104 minfree = lowmem_minfree[i];
105 if (other_free < minfree && other_file < minfree) {
106 min_score_adj = lowmem_adj[i];
111 lowmem_print(3, "lowmem_scan %lu, %x, ofree %d %d, ma %hd\n",
112 sc->nr_to_scan, sc->gfp_mask, other_free,
113 other_file, min_score_adj);
115 if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
116 lowmem_print(5, "lowmem_scan %lu, %x, return 0\n",
117 sc->nr_to_scan, sc->gfp_mask);
121 selected_oom_score_adj = min_score_adj;
124 for_each_process(tsk) {
125 struct task_struct *p;
128 if (tsk->flags & PF_KTHREAD)
131 p = find_lock_task_mm(tsk);
135 if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
136 time_before_eq(jiffies, lowmem_deathpending_timeout)) {
141 oom_score_adj = p->signal->oom_score_adj;
142 if (oom_score_adj < min_score_adj) {
146 tasksize = get_mm_rss(p->mm);
151 if (oom_score_adj < selected_oom_score_adj)
153 if (oom_score_adj == selected_oom_score_adj &&
154 tasksize <= selected_tasksize)
158 selected_tasksize = tasksize;
159 selected_oom_score_adj = oom_score_adj;
160 lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n",
161 p->comm, p->pid, oom_score_adj, tasksize);
165 send_sig(SIGKILL, selected, 0);
167 * FIXME: lowmemorykiller shouldn't abuse global OOM killer
168 * infrastructure. There is no real reason why the selected
169 * task should have access to the memory reserves.
172 mark_oom_victim(selected);
173 task_unlock(selected);
174 long cache_size = other_file * (long)(PAGE_SIZE / 1024);
175 long cache_limit = minfree * (long)(PAGE_SIZE / 1024);
176 long free = other_free * (long)(PAGE_SIZE / 1024);
177 trace_lowmemory_kill(selected, cache_size, cache_limit, free);
178 lowmem_print(1, "Killing '%s' (%d), adj %hd,\n" \
179 " to free %ldkB on behalf of '%s' (%d) because\n" \
180 " cache %ldkB is below limit %ldkB for oom_score_adj %hd\n" \
181 " Free memory is %ldkB above reserved\n",
182 selected->comm, selected->pid,
183 selected_oom_score_adj,
184 selected_tasksize * (long)(PAGE_SIZE / 1024),
185 current->comm, current->pid,
186 cache_size, cache_limit,
189 lowmem_deathpending_timeout = jiffies + HZ;
190 rem += selected_tasksize;
193 lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
194 sc->nr_to_scan, sc->gfp_mask, rem);
199 static struct shrinker lowmem_shrinker = {
200 .scan_objects = lowmem_scan,
201 .count_objects = lowmem_count,
202 .seeks = DEFAULT_SEEKS * 16
205 static int __init lowmem_init(void)
207 register_shrinker(&lowmem_shrinker);
210 device_initcall(lowmem_init);
212 #ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES
213 static short lowmem_oom_adj_to_oom_score_adj(short oom_adj)
215 if (oom_adj == OOM_ADJUST_MAX)
216 return OOM_SCORE_ADJ_MAX;
218 return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
221 static void lowmem_autodetect_oom_adj_values(void)
226 int array_size = ARRAY_SIZE(lowmem_adj);
228 if (lowmem_adj_size < array_size)
229 array_size = lowmem_adj_size;
234 oom_adj = lowmem_adj[array_size - 1];
235 if (oom_adj > OOM_ADJUST_MAX)
238 oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj);
239 if (oom_score_adj <= OOM_ADJUST_MAX)
242 lowmem_print(1, "lowmem_shrink: convert oom_adj to oom_score_adj:\n");
243 for (i = 0; i < array_size; i++) {
244 oom_adj = lowmem_adj[i];
245 oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj);
246 lowmem_adj[i] = oom_score_adj;
247 lowmem_print(1, "oom_adj %d => oom_score_adj %d\n",
248 oom_adj, oom_score_adj);
252 static int lowmem_adj_array_set(const char *val, const struct kernel_param *kp)
256 ret = param_array_ops.set(val, kp);
258 /* HACK: Autodetect oom_adj values in lowmem_adj array */
259 lowmem_autodetect_oom_adj_values();
264 static int lowmem_adj_array_get(char *buffer, const struct kernel_param *kp)
266 return param_array_ops.get(buffer, kp);
269 static void lowmem_adj_array_free(void *arg)
271 param_array_ops.free(arg);
274 static struct kernel_param_ops lowmem_adj_array_ops = {
275 .set = lowmem_adj_array_set,
276 .get = lowmem_adj_array_get,
277 .free = lowmem_adj_array_free,
280 static const struct kparam_array __param_arr_adj = {
281 .max = ARRAY_SIZE(lowmem_adj),
282 .num = &lowmem_adj_size,
283 .ops = ¶m_ops_short,
284 .elemsize = sizeof(lowmem_adj[0]),
290 * not really modular, but the easiest way to keep compat with existing
291 * bootargs behaviour is to continue using module_param here.
293 module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);
294 #ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES
295 __module_param_call(MODULE_PARAM_PREFIX, adj,
296 &lowmem_adj_array_ops,
297 .arr = &__param_arr_adj,
298 S_IRUGO | S_IWUSR, -1);
299 __MODULE_PARM_TYPE(adj, "array of short");
301 module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size,
304 module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
306 module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);