Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[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         int other_file = global_page_state(NR_FILE_PAGES) -
82                                                 global_page_state(NR_SHMEM);
83
84         if (lowmem_adj_size < array_size)
85                 array_size = lowmem_adj_size;
86         if (lowmem_minfree_size < array_size)
87                 array_size = lowmem_minfree_size;
88         for (i = 0; i < array_size; i++) {
89                 minfree = lowmem_minfree[i];
90                 if (other_free < minfree && other_file < minfree) {
91                         min_score_adj = lowmem_adj[i];
92                         break;
93                 }
94         }
95         if (sc->nr_to_scan > 0)
96                 lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %hd\n",
97                                 sc->nr_to_scan, sc->gfp_mask, other_free,
98                                 other_file, min_score_adj);
99         rem = global_page_state(NR_ACTIVE_ANON) +
100                 global_page_state(NR_ACTIVE_FILE) +
101                 global_page_state(NR_INACTIVE_ANON) +
102                 global_page_state(NR_INACTIVE_FILE);
103         if (sc->nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
104                 lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n",
105                              sc->nr_to_scan, sc->gfp_mask, rem);
106                 return rem;
107         }
108         selected_oom_score_adj = min_score_adj;
109
110         rcu_read_lock();
111         for_each_process(tsk) {
112                 struct task_struct *p;
113                 short oom_score_adj;
114
115                 if (tsk->flags & PF_KTHREAD)
116                         continue;
117
118                 p = find_lock_task_mm(tsk);
119                 if (!p)
120                         continue;
121
122                 if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
123                     time_before_eq(jiffies, lowmem_deathpending_timeout)) {
124                         task_unlock(p);
125                         rcu_read_unlock();
126                         return 0;
127                 }
128                 oom_score_adj = p->signal->oom_score_adj;
129                 if (oom_score_adj < min_score_adj) {
130                         task_unlock(p);
131                         continue;
132                 }
133                 tasksize = get_mm_rss(p->mm);
134                 task_unlock(p);
135                 if (tasksize <= 0)
136                         continue;
137                 if (selected) {
138                         if (oom_score_adj < selected_oom_score_adj)
139                                 continue;
140                         if (oom_score_adj == selected_oom_score_adj &&
141                             tasksize <= selected_tasksize)
142                                 continue;
143                 }
144                 selected = p;
145                 selected_tasksize = tasksize;
146                 selected_oom_score_adj = oom_score_adj;
147                 lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n",
148                              p->comm, p->pid, oom_score_adj, tasksize);
149         }
150         if (selected) {
151                 lowmem_print(1, "Killing '%s' (%d), adj %hd,\n" \
152                                 "   to free %ldkB on behalf of '%s' (%d) because\n" \
153                                 "   cache %ldkB is below limit %ldkB for oom_score_adj %hd\n" \
154                                 "   Free memory is %ldkB above reserved\n",
155                              selected->comm, selected->pid,
156                              selected_oom_score_adj,
157                              selected_tasksize * (long)(PAGE_SIZE / 1024),
158                              current->comm, current->pid,
159                              other_file * (long)(PAGE_SIZE / 1024),
160                              minfree * (long)(PAGE_SIZE / 1024),
161                              min_score_adj,
162                              other_free * (long)(PAGE_SIZE / 1024));
163                 lowmem_deathpending_timeout = jiffies + HZ;
164                 send_sig(SIGKILL, selected, 0);
165                 set_tsk_thread_flag(selected, TIF_MEMDIE);
166                 rem -= selected_tasksize;
167         }
168         lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
169                      sc->nr_to_scan, sc->gfp_mask, rem);
170         rcu_read_unlock();
171         return rem;
172 }
173
174 static struct shrinker lowmem_shrinker = {
175         .shrink = lowmem_shrink,
176         .seeks = DEFAULT_SEEKS * 16
177 };
178
179 static int __init lowmem_init(void)
180 {
181         register_shrinker(&lowmem_shrinker);
182         return 0;
183 }
184
185 static void __exit lowmem_exit(void)
186 {
187         unregister_shrinker(&lowmem_shrinker);
188 }
189
190 #ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES
191 static short lowmem_oom_adj_to_oom_score_adj(short oom_adj)
192 {
193         if (oom_adj == OOM_ADJUST_MAX)
194                 return OOM_SCORE_ADJ_MAX;
195         else
196                 return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
197 }
198
199 static void lowmem_autodetect_oom_adj_values(void)
200 {
201         int i;
202         short oom_adj;
203         short oom_score_adj;
204         int array_size = ARRAY_SIZE(lowmem_adj);
205
206         if (lowmem_adj_size < array_size)
207                 array_size = lowmem_adj_size;
208
209         if (array_size <= 0)
210                 return;
211
212         oom_adj = lowmem_adj[array_size - 1];
213         if (oom_adj > OOM_ADJUST_MAX)
214                 return;
215
216         oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj);
217         if (oom_score_adj <= OOM_ADJUST_MAX)
218                 return;
219
220         lowmem_print(1, "lowmem_shrink: convert oom_adj to oom_score_adj:\n");
221         for (i = 0; i < array_size; i++) {
222                 oom_adj = lowmem_adj[i];
223                 oom_score_adj = lowmem_oom_adj_to_oom_score_adj(oom_adj);
224                 lowmem_adj[i] = oom_score_adj;
225                 lowmem_print(1, "oom_adj %d => oom_score_adj %d\n",
226                              oom_adj, oom_score_adj);
227         }
228 }
229
230 static int lowmem_adj_array_set(const char *val, const struct kernel_param *kp)
231 {
232         int ret;
233
234         ret = param_array_ops.set(val, kp);
235
236         /* HACK: Autodetect oom_adj values in lowmem_adj array */
237         lowmem_autodetect_oom_adj_values();
238
239         return ret;
240 }
241
242 static int lowmem_adj_array_get(char *buffer, const struct kernel_param *kp)
243 {
244         return param_array_ops.get(buffer, kp);
245 }
246
247 static void lowmem_adj_array_free(void *arg)
248 {
249         param_array_ops.free(arg);
250 }
251
252 static struct kernel_param_ops lowmem_adj_array_ops = {
253         .set = lowmem_adj_array_set,
254         .get = lowmem_adj_array_get,
255         .free = lowmem_adj_array_free,
256 };
257
258 static const struct kparam_array __param_arr_adj = {
259         .max = ARRAY_SIZE(lowmem_adj),
260         .num = &lowmem_adj_size,
261         .ops = &param_ops_short,
262         .elemsize = sizeof(lowmem_adj[0]),
263         .elem = lowmem_adj,
264 };
265 #endif
266
267 module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);
268 #ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES
269 __module_param_call(MODULE_PARAM_PREFIX, adj,
270                     &lowmem_adj_array_ops,
271                     .arr = &__param_arr_adj,
272                     S_IRUGO | S_IWUSR, -1);
273 __MODULE_PARM_TYPE(adj, "array of short");
274 #else
275 module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size,
276                          S_IRUGO | S_IWUSR);
277 #endif
278 module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
279                          S_IRUGO | S_IWUSR);
280 module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR);
281
282 module_init(lowmem_init);
283 module_exit(lowmem_exit);
284
285 MODULE_LICENSE("GPL");
286