blob: 052b43e4e505148aa0da8b4da5b611da85b5dd3c [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* drivers/misc/lowmemorykiller.c
2 *
3 * The lowmemorykiller driver lets user-space specify a set of memory thresholds
David Rientjes940f77b2012-02-13 19:28:49 -08004 * 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.
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09009 *
10 * For example, write "0,8" to /sys/module/lowmemorykiller/parameters/adj and
Marco Navarra3bf5d652011-12-22 13:28:23 +010011 * "1024,4096" to /sys/module/lowmemorykiller/parameters/minfree to kill
David Rientjes940f77b2012-02-13 19:28:49 -080012 * 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.
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090015 *
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>
Anton Vorontsov294b2712012-02-06 20:29:41 +040038#include <linux/rcupdate.h>
San Mehat4755b722010-05-05 11:38:42 -070039#include <linux/profile.h>
40#include <linux/notifier.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090041
42static uint32_t lowmem_debug_level = 2;
43static int lowmem_adj[6] = {
44 0,
45 1,
46 6,
47 12,
48};
49static int lowmem_adj_size = 4;
Greg Kroah-Hartman624b2252012-03-07 13:21:23 -080050static int lowmem_minfree[6] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090051 3 * 512, /* 6MB */
52 2 * 1024, /* 8MB */
53 4 * 1024, /* 16MB */
54 16 * 1024, /* 64MB */
55};
56static int lowmem_minfree_size = 4;
57
San Mehat4755b722010-05-05 11:38:42 -070058static struct task_struct *lowmem_deathpending;
Arve Hjønnevåge5d79652012-01-13 22:21:25 +040059static unsigned long lowmem_deathpending_timeout;
San Mehat4755b722010-05-05 11:38:42 -070060
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061#define lowmem_print(level, x...) \
62 do { \
63 if (lowmem_debug_level >= (level)) \
64 printk(x); \
65 } while (0)
66
San Mehat4755b722010-05-05 11:38:42 -070067static int
68task_notify_func(struct notifier_block *self, unsigned long val, void *data);
69
70static struct notifier_block task_nb = {
71 .notifier_call = task_notify_func,
72};
73
74static int
75task_notify_func(struct notifier_block *self, unsigned long val, void *data)
76{
77 struct task_struct *task = data;
Paul E. McKenney1eda5162012-03-07 17:54:00 +040078
79 if (task == lowmem_deathpending)
San Mehat4755b722010-05-05 11:38:42 -070080 lowmem_deathpending = NULL;
Paul E. McKenney1eda5162012-03-07 17:54:00 +040081
San Mehat4755b722010-05-05 11:38:42 -070082 return NOTIFY_OK;
83}
84
Colin Crosscae9bf12011-06-22 16:05:47 -070085static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086{
Anton Vorontsov95670002012-02-06 20:29:47 +040087 struct task_struct *tsk;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090088 struct task_struct *selected = NULL;
89 int rem = 0;
90 int tasksize;
91 int i;
David Rientjes940f77b2012-02-13 19:28:49 -080092 int min_score_adj = OOM_SCORE_ADJ_MAX + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090093 int selected_tasksize = 0;
David Rientjes940f77b2012-02-13 19:28:49 -080094 int selected_oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090095 int array_size = ARRAY_SIZE(lowmem_adj);
96 int other_free = global_page_state(NR_FREE_PAGES);
Arve Hjønnevåg71b2c822010-11-23 17:29:04 -080097 int other_file = global_page_state(NR_FILE_PAGES) -
98 global_page_state(NR_SHMEM);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090099
San Mehat4755b722010-05-05 11:38:42 -0700100 /*
101 * If we already have a death outstanding, then
102 * bail out right away; indicating to vmscan
103 * that we have nothing further to offer on
104 * this pass.
105 *
106 * Note: Currently you need CONFIG_PROFILING
107 * for this to work correctly.
108 */
Arve Hjønnevåge5d79652012-01-13 22:21:25 +0400109 if (lowmem_deathpending &&
110 time_before_eq(jiffies, lowmem_deathpending_timeout))
San Mehat4755b722010-05-05 11:38:42 -0700111 return 0;
112
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900113 if (lowmem_adj_size < array_size)
114 array_size = lowmem_adj_size;
115 if (lowmem_minfree_size < array_size)
116 array_size = lowmem_minfree_size;
117 for (i = 0; i < array_size; i++) {
118 if (other_free < lowmem_minfree[i] &&
119 other_file < lowmem_minfree[i]) {
David Rientjes940f77b2012-02-13 19:28:49 -0800120 min_score_adj = lowmem_adj[i];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900121 break;
122 }
123 }
Colin Crosscae9bf12011-06-22 16:05:47 -0700124 if (sc->nr_to_scan > 0)
125 lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %d\n",
Marco Navarra3bf5d652011-12-22 13:28:23 +0100126 sc->nr_to_scan, sc->gfp_mask, other_free,
David Rientjes940f77b2012-02-13 19:28:49 -0800127 other_file, min_score_adj);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900128 rem = global_page_state(NR_ACTIVE_ANON) +
129 global_page_state(NR_ACTIVE_FILE) +
130 global_page_state(NR_INACTIVE_ANON) +
131 global_page_state(NR_INACTIVE_FILE);
David Rientjes940f77b2012-02-13 19:28:49 -0800132 if (sc->nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
Colin Crosscae9bf12011-06-22 16:05:47 -0700133 lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n",
134 sc->nr_to_scan, sc->gfp_mask, rem);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900135 return rem;
136 }
David Rientjes940f77b2012-02-13 19:28:49 -0800137 selected_oom_score_adj = min_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900138
Anton Vorontsov294b2712012-02-06 20:29:41 +0400139 rcu_read_lock();
Anton Vorontsov95670002012-02-06 20:29:47 +0400140 for_each_process(tsk) {
141 struct task_struct *p;
David Rientjes940f77b2012-02-13 19:28:49 -0800142 int oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900143
Anton Vorontsov9823ec92012-02-06 20:30:01 +0400144 if (tsk->flags & PF_KTHREAD)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900145 continue;
Anton Vorontsov9823ec92012-02-06 20:30:01 +0400146
Anton Vorontsov95670002012-02-06 20:29:47 +0400147 p = find_lock_task_mm(tsk);
148 if (!p)
149 continue;
150
David Rientjes940f77b2012-02-13 19:28:49 -0800151 oom_score_adj = p->signal->oom_score_adj;
152 if (oom_score_adj < min_score_adj) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900153 task_unlock(p);
154 continue;
155 }
Anton Vorontsov95670002012-02-06 20:29:47 +0400156 tasksize = get_mm_rss(p->mm);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900157 task_unlock(p);
158 if (tasksize <= 0)
159 continue;
160 if (selected) {
David Rientjes940f77b2012-02-13 19:28:49 -0800161 if (oom_score_adj < selected_oom_score_adj)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900162 continue;
David Rientjes940f77b2012-02-13 19:28:49 -0800163 if (oom_score_adj == selected_oom_score_adj &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900164 tasksize <= selected_tasksize)
165 continue;
166 }
167 selected = p;
168 selected_tasksize = tasksize;
David Rientjes940f77b2012-02-13 19:28:49 -0800169 selected_oom_score_adj = oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900170 lowmem_print(2, "select %d (%s), adj %d, size %d, to kill\n",
David Rientjes940f77b2012-02-13 19:28:49 -0800171 p->pid, p->comm, oom_score_adj, tasksize);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900172 }
173 if (selected) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900174 lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n",
175 selected->pid, selected->comm,
David Rientjes940f77b2012-02-13 19:28:49 -0800176 selected_oom_score_adj, selected_tasksize);
San Mehat4755b722010-05-05 11:38:42 -0700177 /*
Paul E. McKenney1eda5162012-03-07 17:54:00 +0400178 * If CONFIG_PROFILING is off, then we don't want to stall
179 * the killer by setting lowmem_deathpending.
San Mehat4755b722010-05-05 11:38:42 -0700180 */
181#ifdef CONFIG_PROFILING
182 lowmem_deathpending = selected;
Arve Hjønnevåge5d79652012-01-13 22:21:25 +0400183 lowmem_deathpending_timeout = jiffies + HZ;
San Mehat4755b722010-05-05 11:38:42 -0700184#endif
Anton Vorontsov294b2712012-02-06 20:29:41 +0400185 send_sig(SIGKILL, selected, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900186 rem -= selected_tasksize;
187 }
Colin Crosscae9bf12011-06-22 16:05:47 -0700188 lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
189 sc->nr_to_scan, sc->gfp_mask, rem);
Anton Vorontsov294b2712012-02-06 20:29:41 +0400190 rcu_read_unlock();
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900191 return rem;
192}
193
194static struct shrinker lowmem_shrinker = {
195 .shrink = lowmem_shrink,
196 .seeks = DEFAULT_SEEKS * 16
197};
198
199static int __init lowmem_init(void)
200{
Paul E. McKenney1eda5162012-03-07 17:54:00 +0400201 task_handoff_register(&task_nb);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900202 register_shrinker(&lowmem_shrinker);
203 return 0;
204}
205
206static void __exit lowmem_exit(void)
207{
208 unregister_shrinker(&lowmem_shrinker);
Paul E. McKenney1eda5162012-03-07 17:54:00 +0400209 task_handoff_unregister(&task_nb);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900210}
211
212module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);
213module_param_array_named(adj, lowmem_adj, int, &lowmem_adj_size,
214 S_IRUGO | S_IWUSR);
215module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
216 S_IRUGO | S_IWUSR);
217module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);
218
219module_init(lowmem_init);
220module_exit(lowmem_exit);
221
222MODULE_LICENSE("GPL");
223