93a1758b5c08a5897df73cb3729b3c98450855a1
1 /* drivers/misc/lowmemorykiller.c
3 * The lowmemorykiller driver lets user-space specify a set of memory thresholds
4 * where processes with a range of oom_adj values will get killed. Specify the
5 * minimum oom_adj values in /sys/module/lowmemorykiller/parameters/adj and the
6 * number of free pages in /sys/module/lowmemorykiller/parameters/minfree. Both
7 * files take a comma separated list of numbers in ascending order.
9 * For example, write "0,8" to /sys/module/lowmemorykiller/parameters/adj and
10 * "1024,4096" to /sys/module/lowmemorykiller/parameters/minfree to kill processes
11 * with a oom_adj value of 8 or higher when the free memory drops below 4096 pages
12 * and kill processes with a oom_adj value of 0 or higher when the free memory
13 * drops below 1024 pages.
15 * The driver considers memory used for caches to be free, but if a large
16 * percentage of the cached memory is locked this can be very inaccurate
17 * and processes may not get killed until the normal oom killer is triggered.
19 * Copyright (C) 2007-2008 Google, Inc.
21 * This software is licensed under the terms of the GNU General Public
22 * License version 2, as published by the Free Software Foundation, and
23 * may be copied, distributed, and modified under those terms.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
32 #include <linux/module.h>
33 #include <linux/kernel.h>
35 #include <linux/oom.h>
36 #include <linux/sched.h>
37 #include <linux/profile.h>
38 #include <linux/notifier.h>
40 static uint32_t lowmem_debug_level
= 2;
41 static int lowmem_adj
[6] = {
47 static int lowmem_adj_size
= 4;
48 static size_t lowmem_minfree
[6] = {
54 static int lowmem_minfree_size
= 4;
56 static struct task_struct
*lowmem_deathpending
;
58 #define lowmem_print(level, x...) \
60 if (lowmem_debug_level >= (level)) \
65 task_notify_func(struct notifier_block
*self
, unsigned long val
, void *data
);
67 static struct notifier_block task_nb
= {
68 .notifier_call
= task_notify_func
,
72 task_notify_func(struct notifier_block
*self
, unsigned long val
, void *data
)
74 struct task_struct
*task
= data
;
75 if (task
== lowmem_deathpending
) {
76 lowmem_deathpending
= NULL
;
77 task_handoff_unregister(&task_nb
);
82 static int lowmem_shrink(struct shrinker
*s
, int nr_to_scan
, gfp_t gfp_mask
)
84 struct task_struct
*p
;
85 struct task_struct
*selected
= NULL
;
89 int min_adj
= OOM_ADJUST_MAX
+ 1;
90 int selected_tasksize
= 0;
92 int array_size
= ARRAY_SIZE(lowmem_adj
);
93 int other_free
= global_page_state(NR_FREE_PAGES
);
94 int other_file
= global_page_state(NR_FILE_PAGES
);
97 * If we already have a death outstanding, then
98 * bail out right away; indicating to vmscan
99 * that we have nothing further to offer on
102 * Note: Currently you need CONFIG_PROFILING
103 * for this to work correctly.
105 if (lowmem_deathpending
)
108 if (lowmem_adj_size
< array_size
)
109 array_size
= lowmem_adj_size
;
110 if (lowmem_minfree_size
< array_size
)
111 array_size
= lowmem_minfree_size
;
112 for (i
= 0; i
< array_size
; i
++) {
113 if (other_free
< lowmem_minfree
[i
] &&
114 other_file
< lowmem_minfree
[i
]) {
115 min_adj
= lowmem_adj
[i
];
120 lowmem_print(3, "lowmem_shrink %d, %x, ofree %d %d, ma %d\n",
121 nr_to_scan
, gfp_mask
, other_free
, other_file
,
123 rem
= global_page_state(NR_ACTIVE_ANON
) +
124 global_page_state(NR_ACTIVE_FILE
) +
125 global_page_state(NR_INACTIVE_ANON
) +
126 global_page_state(NR_INACTIVE_FILE
);
127 if (nr_to_scan
<= 0 || min_adj
== OOM_ADJUST_MAX
+ 1) {
128 lowmem_print(5, "lowmem_shrink %d, %x, return %d\n",
129 nr_to_scan
, gfp_mask
, rem
);
132 selected_oom_adj
= min_adj
;
134 read_lock(&tasklist_lock
);
135 for_each_process(p
) {
136 struct mm_struct
*mm
;
137 struct signal_struct
*sig
;
147 oom_adj
= sig
->oom_adj
;
148 if (oom_adj
< min_adj
) {
152 tasksize
= get_mm_rss(mm
);
157 if (oom_adj
< selected_oom_adj
)
159 if (oom_adj
== selected_oom_adj
&&
160 tasksize
<= selected_tasksize
)
164 selected_tasksize
= tasksize
;
165 selected_oom_adj
= oom_adj
;
166 lowmem_print(2, "select %d (%s), adj %d, size %d, to kill\n",
167 p
->pid
, p
->comm
, oom_adj
, tasksize
);
170 lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n",
171 selected
->pid
, selected
->comm
,
172 selected_oom_adj
, selected_tasksize
);
174 * If CONFIG_PROFILING is off, then task_handoff_register()
175 * is a nop. In that case we don't want to stall the killer
176 * by setting lowmem_deathpending.
178 #ifdef CONFIG_PROFILING
179 lowmem_deathpending
= selected
;
180 task_handoff_register(&task_nb
);
182 force_sig(SIGKILL
, selected
);
183 rem
-= selected_tasksize
;
185 lowmem_print(4, "lowmem_shrink %d, %x, return %d\n",
186 nr_to_scan
, gfp_mask
, rem
);
187 read_unlock(&tasklist_lock
);
191 static struct shrinker lowmem_shrinker
= {
192 .shrink
= lowmem_shrink
,
193 .seeks
= DEFAULT_SEEKS
* 16
196 static int __init
lowmem_init(void)
198 register_shrinker(&lowmem_shrinker
);
202 static void __exit
lowmem_exit(void)
204 unregister_shrinker(&lowmem_shrinker
);
207 module_param_named(cost
, lowmem_shrinker
.seeks
, int, S_IRUGO
| S_IWUSR
);
208 module_param_array_named(adj
, lowmem_adj
, int, &lowmem_adj_size
,
210 module_param_array_named(minfree
, lowmem_minfree
, uint
, &lowmem_minfree_size
,
212 module_param_named(debug_level
, lowmem_debug_level
, uint
, S_IRUGO
| S_IWUSR
);
214 module_init(lowmem_init
);
215 module_exit(lowmem_exit
);
217 MODULE_LICENSE("GPL");
This page took 0.034126 seconds and 4 git commands to generate.