San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 1 | /* drivers/misc/lowmemorykiller.c |
| 2 | * |
| 3 | * Copyright (C) 2007-2008 Google, Inc. |
| 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/oom.h> |
| 20 | #include <linux/sched.h> |
| 21 | |
| 22 | static int lowmem_shrink(int nr_to_scan, gfp_t gfp_mask); |
| 23 | |
| 24 | static struct shrinker lowmem_shrinker = { |
| 25 | .shrink = lowmem_shrink, |
| 26 | .seeks = DEFAULT_SEEKS * 16 |
| 27 | }; |
| 28 | static uint32_t lowmem_debug_level = 2; |
| 29 | static int lowmem_adj[6] = { |
| 30 | 0, |
| 31 | 1, |
| 32 | 6, |
| 33 | 12, |
| 34 | }; |
| 35 | static int lowmem_adj_size = 4; |
| 36 | static size_t lowmem_minfree[6] = { |
Greg Kroah-Hartman | 1dbb576 | 2009-05-11 17:00:41 -0700 | [diff] [blame] | 37 | 3 * 512, /* 6MB */ |
| 38 | 2 * 1024, /* 8MB */ |
| 39 | 4 * 1024, /* 16MB */ |
| 40 | 16 * 1024, /* 64MB */ |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 41 | }; |
| 42 | static int lowmem_minfree_size = 4; |
| 43 | |
Greg Kroah-Hartman | 1dbb576 | 2009-05-11 17:00:41 -0700 | [diff] [blame] | 44 | #define lowmem_print(level, x...) \ |
| 45 | do { \ |
| 46 | if (lowmem_debug_level >= (level)) \ |
| 47 | printk(x); \ |
| 48 | } while (0) |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 49 | |
| 50 | module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR); |
Greg Kroah-Hartman | 1dbb576 | 2009-05-11 17:00:41 -0700 | [diff] [blame] | 51 | module_param_array_named(adj, lowmem_adj, int, &lowmem_adj_size, |
| 52 | S_IRUGO | S_IWUSR); |
| 53 | module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size, |
| 54 | S_IRUGO | S_IWUSR); |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 55 | module_param_named(debug_level, lowmem_debug_level, uint, S_IRUGO | S_IWUSR); |
| 56 | |
| 57 | static int lowmem_shrink(int nr_to_scan, gfp_t gfp_mask) |
| 58 | { |
| 59 | struct task_struct *p; |
| 60 | struct task_struct *selected = NULL; |
| 61 | int rem = 0; |
| 62 | int tasksize; |
| 63 | int i; |
| 64 | int min_adj = OOM_ADJUST_MAX + 1; |
| 65 | int selected_tasksize = 0; |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 66 | int selected_oom_adj; |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 67 | int array_size = ARRAY_SIZE(lowmem_adj); |
Arve Hjønnevåg | 31d59a4 | 2009-05-11 15:45:10 -0700 | [diff] [blame] | 68 | int other_free = global_page_state(NR_FREE_PAGES); |
| 69 | int other_file = global_page_state(NR_FILE_PAGES); |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 70 | |
| 71 | if (lowmem_adj_size < array_size) |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 72 | array_size = lowmem_adj_size; |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 73 | if (lowmem_minfree_size < array_size) |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 74 | array_size = lowmem_minfree_size; |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 75 | for (i = 0; i < array_size; i++) { |
Arve Hjønnevåg | 31d59a4 | 2009-05-11 15:45:10 -0700 | [diff] [blame] | 76 | if (other_free < lowmem_minfree[i] && |
| 77 | other_file < lowmem_minfree[i]) { |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 78 | min_adj = lowmem_adj[i]; |
| 79 | break; |
| 80 | } |
| 81 | } |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 82 | if (nr_to_scan > 0) |
| 83 | lowmem_print(3, "lowmem_shrink %d, %x, ofree %d %d, ma %d\n", |
| 84 | nr_to_scan, gfp_mask, other_free, other_file, |
| 85 | min_adj); |
Arve Hjønnevåg | 31d59a4 | 2009-05-11 15:45:10 -0700 | [diff] [blame] | 86 | rem = global_page_state(NR_ACTIVE_ANON) + |
| 87 | global_page_state(NR_ACTIVE_FILE) + |
| 88 | global_page_state(NR_INACTIVE_ANON) + |
| 89 | global_page_state(NR_INACTIVE_FILE); |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 90 | if (nr_to_scan <= 0 || min_adj == OOM_ADJUST_MAX + 1) { |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 91 | lowmem_print(5, "lowmem_shrink %d, %x, return %d\n", |
| 92 | nr_to_scan, gfp_mask, rem); |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 93 | return rem; |
| 94 | } |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 95 | selected_oom_adj = min_adj; |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 96 | |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 97 | read_lock(&tasklist_lock); |
| 98 | for_each_process(p) { |
David Rientjes | a6a9f81 | 2009-06-16 16:42:53 -0700 | [diff] [blame] | 99 | struct mm_struct *mm; |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 100 | int oom_adj; |
| 101 | |
David Rientjes | 5d14a57 | 2009-05-11 15:45:14 -0700 | [diff] [blame] | 102 | task_lock(p); |
David Rientjes | a6a9f81 | 2009-06-16 16:42:53 -0700 | [diff] [blame] | 103 | mm = p->mm; |
| 104 | if (!mm) { |
David Rientjes | 5d14a57 | 2009-05-11 15:45:14 -0700 | [diff] [blame] | 105 | task_unlock(p); |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 106 | continue; |
David Rientjes | 5d14a57 | 2009-05-11 15:45:14 -0700 | [diff] [blame] | 107 | } |
David Rientjes | a6a9f81 | 2009-06-16 16:42:53 -0700 | [diff] [blame] | 108 | oom_adj = mm->oom_adj; |
David Rientjes | 5d14a57 | 2009-05-11 15:45:14 -0700 | [diff] [blame] | 109 | if (oom_adj < min_adj) { |
| 110 | task_unlock(p); |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 111 | continue; |
David Rientjes | 5d14a57 | 2009-05-11 15:45:14 -0700 | [diff] [blame] | 112 | } |
David Rientjes | a6a9f81 | 2009-06-16 16:42:53 -0700 | [diff] [blame] | 113 | tasksize = get_mm_rss(mm); |
David Rientjes | 5d14a57 | 2009-05-11 15:45:14 -0700 | [diff] [blame] | 114 | task_unlock(p); |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 115 | if (tasksize <= 0) |
| 116 | continue; |
| 117 | if (selected) { |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 118 | if (oom_adj < selected_oom_adj) |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 119 | continue; |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 120 | if (oom_adj == selected_oom_adj && |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 121 | tasksize <= selected_tasksize) |
| 122 | continue; |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 123 | } |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 124 | selected = p; |
| 125 | selected_tasksize = tasksize; |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 126 | selected_oom_adj = oom_adj; |
Arve Hjønnevåg | f501d00 | 2009-05-11 15:45:09 -0700 | [diff] [blame] | 127 | lowmem_print(2, "select %d (%s), adj %d, size %d, to kill\n", |
Greg Kroah-Hartman | 1dbb576 | 2009-05-11 17:00:41 -0700 | [diff] [blame] | 128 | p->pid, p->comm, oom_adj, tasksize); |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 129 | } |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 130 | if (selected) { |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 131 | lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n", |
Greg Kroah-Hartman | 1dbb576 | 2009-05-11 17:00:41 -0700 | [diff] [blame] | 132 | selected->pid, selected->comm, |
| 133 | selected_oom_adj, selected_tasksize); |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 134 | force_sig(SIGKILL, selected); |
| 135 | rem -= selected_tasksize; |
| 136 | } |
David Rientjes | 34006e1 | 2009-05-11 15:45:12 -0700 | [diff] [blame] | 137 | lowmem_print(4, "lowmem_shrink %d, %x, return %d\n", |
| 138 | nr_to_scan, gfp_mask, rem); |
San Mehat | 08b88cc | 2008-12-20 00:48:38 -0800 | [diff] [blame] | 139 | read_unlock(&tasklist_lock); |
| 140 | return rem; |
| 141 | } |
| 142 | |
| 143 | static int __init lowmem_init(void) |
| 144 | { |
| 145 | register_shrinker(&lowmem_shrinker); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void __exit lowmem_exit(void) |
| 150 | { |
| 151 | unregister_shrinker(&lowmem_shrinker); |
| 152 | } |
| 153 | |
| 154 | module_init(lowmem_init); |
| 155 | module_exit(lowmem_exit); |
| 156 | |
| 157 | MODULE_LICENSE("GPL"); |
| 158 | |