db571b8f0c8d961749e8599b2bcd55b96ee76b3d
[firefly-linux-kernel-4.4.55.git] / drivers / staging / android / lowmemorykiller.c
1 /* drivers/misc/lowmemorykiller.c
2  *
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.
9  *
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.
15  *
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/mm.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>
43
44 static uint32_t lowmem_debug_level = 1;
45 static short lowmem_adj[6] = {
46         0,
47         1,
48         6,
49         12,
50 };
51 static int lowmem_adj_size = 4;
52 static int lowmem_minfree[6] = {
53         3 * 512,        /* 6MB */
54         2 * 1024,       /* 8MB */
55         4 * 1024,       /* 16MB */
56         16 * 1024,      /* 64MB */
57 };
58 static int lowmem_minfree_size = 4;
59
60 static unsigned long lowmem_deathpending_timeout;
61
62 #define lowmem_print(level, x...)                       \
63         do {                                            \
64                 if (lowmem_debug_level >= (level))      \
65                         pr_info(x);                     \
66         } while (0)
67
68 static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
69 {
70         struct task_struct *tsk;
71         struct task_struct *selected = NULL;
72         int rem = 0;
73         int tasksize;
74         int i;
75         short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
76         int minfree = 0;
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);
84
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];
93                         break;
94                 }
95         }
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);
107                 return rem;
108         }
109         selected_oom_score_adj = min_score_adj;
110
111         rcu_read_lock();
112         for_each_process(tsk) {
113                 struct task_struct *p;
114                 short oom_score_adj;
115
116                 if (tsk->flags & PF_KTHREAD)
117                         continue;
118
119                 p = find_lock_task_mm(tsk);
120                 if (!p)
121                         continue;
122
123                 if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
124                     time_before_eq(jiffies, lowmem_deathpending_timeout)) {
125                         task_unlock(p);
126                         rcu_read_unlock();
127                         return 0;
128                 }
129                 oom_score_adj = p->signal->oom_score_adj;
130                 if (oom_score_adj < min_score_adj) {
131                         task_unlock(p);
132                         continue;
133                 }
134                 tasksize = get_mm_rss(p->mm);
135                 task_unlock(p);
136                 if (tasksize <= 0)
137                         continue;
138                 if (selected) {
139                         if (oom_score_adj < selected_oom_score_adj)
140                                 continue;
141                         if (oom_score_adj == selected_oom_score_adj &&
142                             tasksize <= selected_tasksize)
143                                 continue;
144                 }
145                 selected = p;
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);
150         }
151         if (selected) {
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),
162                              min_score_adj,
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;
168         }
169         lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
170                      sc->nr_to_scan, sc->gfp_mask, rem);
171         rcu_read_unlock();
172         return rem;
173 }
174
175 static struct shrinker lowmem_shrinker = {
176         .shrink = lowmem_shrink,
177         .seeks = DEFAULT_SEEKS * 16
178 };
179
180 static int __init lowmem_init(void)
181 {
182         register_shrinker(&lowmem_shrinker);
183         return 0;
184 }
185
186 static void __exit lowmem_exit(void)
187 {
188         unregister_shrinker(&lowmem_shrinker);
189 }
190
191 #ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES
192 static short lowmem_oom_adj_to_oom_score_adj(short oom_adj)
193 {
194         if (oom_adj == OOM_ADJUST_MAX)
195                 return OOM_SCORE_ADJ_MAX;
196         else
197                 return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
198 }
199
200 static void lowmem_autodetect_oom_adj_values(void)
201 {
202         int i;
203         short oom_adj;
204         short oom_score_adj;
205         int array_size = ARRAY_SIZE(lowmem_adj);
206
207         if (lowmem_adj_size < array_size)
208                 array_size = lowmem_adj_size;
209
210         if (array_size <= 0)
211                 return;
212
213         oom_adj = lowmem_adj[array_size - 1];
214         if (oom_adj > OOM_ADJUST_MAX)
215                 return;
216
217         oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj);
218         if (oom_score_adj <= OOM_ADJUST_MAX)
219                 return;
220
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);
228         }
229 }
230
231 static int lowmem_adj_array_set(const char *val, const struct kernel_param *kp)
232 {
233         int ret;
234
235         ret = param_array_ops.set(val, kp);
236
237         /* HACK: Autodetect oom_adj values in lowmem_adj array */
238         lowmem_autodetect_oom_adj_values();
239
240         return ret;
241 }
242
243 static int lowmem_adj_array_get(char *buffer, const struct kernel_param *kp)
244 {
245         return param_array_ops.get(buffer, kp);
246 }
247
248 static void lowmem_adj_array_free(void *arg)
249 {
250         param_array_ops.free(arg);
251 }
252
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,
257 };
258
259 static const struct kparam_array __param_arr_adj = {
260         .max = ARRAY_SIZE(lowmem_adj),
261         .num = &lowmem_adj_size,
262         .ops = &param_ops_short,
263         .elemsize = sizeof(lowmem_adj[0]),
264         .elem = lowmem_adj,
265 };
266 #endif
267
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");
275 #else
276 module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size,
277                          S_IRUGO | S_IWUSR);
278 #endif
279 module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
280                          S_IRUGO | S_IWUSR);
281 module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);
282
283 module_init(lowmem_init);
284 module_exit(lowmem_exit);
285
286 MODULE_LICENSE("GPL");
287