blob: b91e4bc332a77c2e6ea1219e99f92c5ee924c418 [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
Arve Hjønnevåge5d79652012-01-13 22:21:25 +040058static unsigned long lowmem_deathpending_timeout;
San Mehat4755b722010-05-05 11:38:42 -070059
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090060#define lowmem_print(level, x...) \
61 do { \
62 if (lowmem_debug_level >= (level)) \
63 printk(x); \
64 } while (0)
65
Colin Crosscae9bf12011-06-22 16:05:47 -070066static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090067{
Anton Vorontsov95670002012-02-06 20:29:47 +040068 struct task_struct *tsk;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090069 struct task_struct *selected = NULL;
70 int rem = 0;
71 int tasksize;
72 int i;
David Rientjes940f77b2012-02-13 19:28:49 -080073 int min_score_adj = OOM_SCORE_ADJ_MAX + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090074 int selected_tasksize = 0;
David Rientjes940f77b2012-02-13 19:28:49 -080075 int selected_oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090076 int array_size = ARRAY_SIZE(lowmem_adj);
77 int other_free = global_page_state(NR_FREE_PAGES);
Arve Hjønnevåg71b2c822010-11-23 17:29:04 -080078 int other_file = global_page_state(NR_FILE_PAGES) -
79 global_page_state(NR_SHMEM);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090080
81 if (lowmem_adj_size < array_size)
82 array_size = lowmem_adj_size;
83 if (lowmem_minfree_size < array_size)
84 array_size = lowmem_minfree_size;
85 for (i = 0; i < array_size; i++) {
86 if (other_free < lowmem_minfree[i] &&
87 other_file < lowmem_minfree[i]) {
David Rientjes940f77b2012-02-13 19:28:49 -080088 min_score_adj = lowmem_adj[i];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090089 break;
90 }
91 }
Colin Crosscae9bf12011-06-22 16:05:47 -070092 if (sc->nr_to_scan > 0)
93 lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %d\n",
Marco Navarra3bf5d652011-12-22 13:28:23 +010094 sc->nr_to_scan, sc->gfp_mask, other_free,
David Rientjes940f77b2012-02-13 19:28:49 -080095 other_file, min_score_adj);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090096 rem = global_page_state(NR_ACTIVE_ANON) +
97 global_page_state(NR_ACTIVE_FILE) +
98 global_page_state(NR_INACTIVE_ANON) +
99 global_page_state(NR_INACTIVE_FILE);
David Rientjes940f77b2012-02-13 19:28:49 -0800100 if (sc->nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
Colin Crosscae9bf12011-06-22 16:05:47 -0700101 lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n",
102 sc->nr_to_scan, sc->gfp_mask, rem);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900103 return rem;
104 }
David Rientjes940f77b2012-02-13 19:28:49 -0800105 selected_oom_score_adj = min_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900106
Anton Vorontsov294b2712012-02-06 20:29:41 +0400107 rcu_read_lock();
Anton Vorontsov95670002012-02-06 20:29:47 +0400108 for_each_process(tsk) {
109 struct task_struct *p;
David Rientjes940f77b2012-02-13 19:28:49 -0800110 int oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900111
Anton Vorontsov9823ec92012-02-06 20:30:01 +0400112 if (tsk->flags & PF_KTHREAD)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900113 continue;
Anton Vorontsov9823ec92012-02-06 20:30:01 +0400114
Anton Vorontsov95670002012-02-06 20:29:47 +0400115 p = find_lock_task_mm(tsk);
116 if (!p)
117 continue;
118
David Rientjes83dbbdb2012-04-09 16:56:18 -0700119 if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
120 time_before_eq(jiffies, lowmem_deathpending_timeout)) {
121 task_unlock(p);
122 rcu_read_unlock();
123 return 0;
124 }
David Rientjes940f77b2012-02-13 19:28:49 -0800125 oom_score_adj = p->signal->oom_score_adj;
126 if (oom_score_adj < min_score_adj) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900127 task_unlock(p);
128 continue;
129 }
Anton Vorontsov95670002012-02-06 20:29:47 +0400130 tasksize = get_mm_rss(p->mm);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900131 task_unlock(p);
132 if (tasksize <= 0)
133 continue;
134 if (selected) {
David Rientjes940f77b2012-02-13 19:28:49 -0800135 if (oom_score_adj < selected_oom_score_adj)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900136 continue;
David Rientjes940f77b2012-02-13 19:28:49 -0800137 if (oom_score_adj == selected_oom_score_adj &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900138 tasksize <= selected_tasksize)
139 continue;
140 }
141 selected = p;
142 selected_tasksize = tasksize;
David Rientjes940f77b2012-02-13 19:28:49 -0800143 selected_oom_score_adj = oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900144 lowmem_print(2, "select %d (%s), adj %d, size %d, to kill\n",
David Rientjes940f77b2012-02-13 19:28:49 -0800145 p->pid, p->comm, oom_score_adj, tasksize);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900146 }
147 if (selected) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900148 lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n",
149 selected->pid, selected->comm,
David Rientjes940f77b2012-02-13 19:28:49 -0800150 selected_oom_score_adj, selected_tasksize);
Arve Hjønnevåge5d79652012-01-13 22:21:25 +0400151 lowmem_deathpending_timeout = jiffies + HZ;
Anton Vorontsov294b2712012-02-06 20:29:41 +0400152 send_sig(SIGKILL, selected, 0);
David Rientjes83dbbdb2012-04-09 16:56:18 -0700153 set_tsk_thread_flag(selected, TIF_MEMDIE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900154 rem -= selected_tasksize;
155 }
Colin Crosscae9bf12011-06-22 16:05:47 -0700156 lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
157 sc->nr_to_scan, sc->gfp_mask, rem);
Anton Vorontsov294b2712012-02-06 20:29:41 +0400158 rcu_read_unlock();
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900159 return rem;
160}
161
162static struct shrinker lowmem_shrinker = {
163 .shrink = lowmem_shrink,
164 .seeks = DEFAULT_SEEKS * 16
165};
166
167static int __init lowmem_init(void)
168{
169 register_shrinker(&lowmem_shrinker);
170 return 0;
171}
172
173static void __exit lowmem_exit(void)
174{
175 unregister_shrinker(&lowmem_shrinker);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900176}
177
178module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);
179module_param_array_named(adj, lowmem_adj, int, &lowmem_adj_size,
180 S_IRUGO | S_IWUSR);
181module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
182 S_IRUGO | S_IWUSR);
183module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);
184
185module_init(lowmem_init);
186module_exit(lowmem_exit);
187
188MODULE_LICENSE("GPL");
189