blob: 24d2745e943778200c64996510814756854fa461 [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
Dmitry Voytik6b83f912013-03-16 11:37:26 +040033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Paul Gortmaker380b6542015-10-11 15:47:32 -040035#include <linux/init.h>
36#include <linux/moduleparam.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090037#include <linux/kernel.h>
38#include <linux/mm.h>
39#include <linux/oom.h>
40#include <linux/sched.h>
Arve Hjønnevågeeb0f4f2013-02-26 22:07:35 -080041#include <linux/swap.h>
Anton Vorontsov294b2712012-02-06 20:29:41 +040042#include <linux/rcupdate.h>
San Mehat4755b722010-05-05 11:38:42 -070043#include <linux/profile.h>
44#include <linux/notifier.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090045
Ioana Ciornei36f16ff2015-11-01 16:38:21 +020046static u32 lowmem_debug_level = 1;
David Rientjesa9c58b902012-12-11 16:02:54 -080047static short lowmem_adj[6] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090048 0,
49 1,
50 6,
51 12,
52};
Sandeep Jainb33061f2016-02-29 18:48:30 +053053
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054static int lowmem_adj_size = 4;
Greg Kroah-Hartman624b2252012-03-07 13:21:23 -080055static int lowmem_minfree[6] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090056 3 * 512, /* 6MB */
57 2 * 1024, /* 8MB */
58 4 * 1024, /* 16MB */
59 16 * 1024, /* 64MB */
60};
Sandeep Jainb33061f2016-02-29 18:48:30 +053061
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090062static int lowmem_minfree_size = 4;
63
Arve Hjønnevåge5d79652012-01-13 22:21:25 +040064static unsigned long lowmem_deathpending_timeout;
San Mehat4755b722010-05-05 11:38:42 -070065
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090066#define lowmem_print(level, x...) \
67 do { \
68 if (lowmem_debug_level >= (level)) \
Dmitry Voytik6b83f912013-03-16 11:37:26 +040069 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090070 } while (0)
71
Dave Chinner7dc19d52013-08-28 10:18:11 +100072static unsigned long lowmem_count(struct shrinker *s,
73 struct shrink_control *sc)
74{
75 return global_page_state(NR_ACTIVE_ANON) +
76 global_page_state(NR_ACTIVE_FILE) +
77 global_page_state(NR_INACTIVE_ANON) +
78 global_page_state(NR_INACTIVE_FILE);
79}
80
81static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090082{
Anton Vorontsov95670002012-02-06 20:29:47 +040083 struct task_struct *tsk;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090084 struct task_struct *selected = NULL;
Dave Chinner7dc19d52013-08-28 10:18:11 +100085 unsigned long rem = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086 int tasksize;
87 int i;
David Rientjesa9c58b902012-12-11 16:02:54 -080088 short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
Colin Crosscc635da2016-01-29 22:07:33 -080089 int minfree = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090090 int selected_tasksize = 0;
David Rientjesa9c58b902012-12-11 16:02:54 -080091 short selected_oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090092 int array_size = ARRAY_SIZE(lowmem_adj);
Arve Hjønnevågeeb0f4f2013-02-26 22:07:35 -080093 int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
Arve Hjønnevåg71b2c822010-11-23 17:29:04 -080094 int other_file = global_page_state(NR_FILE_PAGES) -
Vinayak Menon058dbde2014-02-27 00:36:22 +053095 global_page_state(NR_SHMEM) -
96 total_swapcache_pages();
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090097
98 if (lowmem_adj_size < array_size)
99 array_size = lowmem_adj_size;
100 if (lowmem_minfree_size < array_size)
101 array_size = lowmem_minfree_size;
102 for (i = 0; i < array_size; i++) {
Colin Crosscc635da2016-01-29 22:07:33 -0800103 minfree = lowmem_minfree[i];
104 if (other_free < minfree && other_file < minfree) {
David Rientjes940f77b2012-02-13 19:28:49 -0800105 min_score_adj = lowmem_adj[i];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900106 break;
107 }
108 }
Dave Chinner7dc19d52013-08-28 10:18:11 +1000109
110 lowmem_print(3, "lowmem_scan %lu, %x, ofree %d %d, ma %hd\n",
Ioana Ciorneif8b053e2015-11-01 16:38:22 +0200111 sc->nr_to_scan, sc->gfp_mask, other_free,
112 other_file, min_score_adj);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000113
114 if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
115 lowmem_print(5, "lowmem_scan %lu, %x, return 0\n",
116 sc->nr_to_scan, sc->gfp_mask);
117 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900118 }
Dave Chinner7dc19d52013-08-28 10:18:11 +1000119
David Rientjes940f77b2012-02-13 19:28:49 -0800120 selected_oom_score_adj = min_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900121
Anton Vorontsov294b2712012-02-06 20:29:41 +0400122 rcu_read_lock();
Anton Vorontsov95670002012-02-06 20:29:47 +0400123 for_each_process(tsk) {
124 struct task_struct *p;
David Rientjesa9c58b902012-12-11 16:02:54 -0800125 short oom_score_adj;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900126
Anton Vorontsov9823ec92012-02-06 20:30:01 +0400127 if (tsk->flags & PF_KTHREAD)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900128 continue;
Anton Vorontsov9823ec92012-02-06 20:30:01 +0400129
Anton Vorontsov95670002012-02-06 20:29:47 +0400130 p = find_lock_task_mm(tsk);
131 if (!p)
132 continue;
133
Dan Carpenterae25d432016-04-08 16:02:35 +0300134 if (task_lmk_waiting(p) &&
David Rientjes83dbbdb2012-04-09 16:56:18 -0700135 time_before_eq(jiffies, lowmem_deathpending_timeout)) {
136 task_unlock(p);
137 rcu_read_unlock();
138 return 0;
139 }
David Rientjes940f77b2012-02-13 19:28:49 -0800140 oom_score_adj = p->signal->oom_score_adj;
141 if (oom_score_adj < min_score_adj) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142 task_unlock(p);
143 continue;
144 }
Anton Vorontsov95670002012-02-06 20:29:47 +0400145 tasksize = get_mm_rss(p->mm);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900146 task_unlock(p);
147 if (tasksize <= 0)
148 continue;
149 if (selected) {
David Rientjes940f77b2012-02-13 19:28:49 -0800150 if (oom_score_adj < selected_oom_score_adj)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900151 continue;
David Rientjes940f77b2012-02-13 19:28:49 -0800152 if (oom_score_adj == selected_oom_score_adj &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900153 tasksize <= selected_tasksize)
154 continue;
155 }
156 selected = p;
157 selected_tasksize = tasksize;
David Rientjes940f77b2012-02-13 19:28:49 -0800158 selected_oom_score_adj = oom_score_adj;
Colin Crosscc635da2016-01-29 22:07:33 -0800159 lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n",
160 p->comm, p->pid, oom_score_adj, tasksize);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900161 }
162 if (selected) {
David Rientjese1099a62015-04-28 15:50:46 -0700163 task_lock(selected);
Tetsuo Handa3a81fc22015-09-06 14:25:35 +0900164 send_sig(SIGKILL, selected, 0);
Tetsuo Handa3a81fc22015-09-06 14:25:35 +0900165 if (selected->mm)
Tetsuo Handa77ed2c52016-03-08 20:01:32 +0900166 task_set_lmk_waiting(selected);
David Rientjese1099a62015-04-28 15:50:46 -0700167 task_unlock(selected);
Colin Crosscc635da2016-01-29 22:07:33 -0800168 lowmem_print(1, "Killing '%s' (%d), adj %hd,\n"
169 " to free %ldkB on behalf of '%s' (%d) because\n"
170 " cache %ldkB is below limit %ldkB for oom_score_adj %hd\n"
171 " Free memory is %ldkB above reserved\n",
172 selected->comm, selected->pid,
173 selected_oom_score_adj,
174 selected_tasksize * (long)(PAGE_SIZE / 1024),
175 current->comm, current->pid,
176 other_file * (long)(PAGE_SIZE / 1024),
177 minfree * (long)(PAGE_SIZE / 1024),
178 min_score_adj,
179 other_free * (long)(PAGE_SIZE / 1024));
David Rientjese1099a62015-04-28 15:50:46 -0700180 lowmem_deathpending_timeout = jiffies + HZ;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000181 rem += selected_tasksize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900182 }
Tetsuo Handa3a81fc22015-09-06 14:25:35 +0900183
Dave Chinner7dc19d52013-08-28 10:18:11 +1000184 lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
Colin Crosscae9bf12011-06-22 16:05:47 -0700185 sc->nr_to_scan, sc->gfp_mask, rem);
Anton Vorontsov294b2712012-02-06 20:29:41 +0400186 rcu_read_unlock();
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900187 return rem;
188}
189
190static struct shrinker lowmem_shrinker = {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000191 .scan_objects = lowmem_scan,
192 .count_objects = lowmem_count,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900193 .seeks = DEFAULT_SEEKS * 16
194};
195
196static int __init lowmem_init(void)
197{
198 register_shrinker(&lowmem_shrinker);
199 return 0;
200}
Paul Gortmaker380b6542015-10-11 15:47:32 -0400201device_initcall(lowmem_init);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900202
Paul Gortmaker380b6542015-10-11 15:47:32 -0400203/*
204 * not really modular, but the easiest way to keep compat with existing
205 * bootargs behaviour is to continue using module_param here.
206 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900207module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);
David Rientjesa9c58b902012-12-11 16:02:54 -0800208module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900209 S_IRUGO | S_IWUSR);
210module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
211 S_IRUGO | S_IWUSR);
212module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);
213