Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Collaborative memory management interface. |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright IBM Corp 2003,2010 |
| 5 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/errno.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/sched.h> |
| 15 | #include <linux/sysctl.h> |
| 16 | #include <linux/ctype.h> |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 17 | #include <linux/swap.h> |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 18 | #include <linux/kthread.h> |
David Rientjes | 5a3135c2 | 2007-10-16 23:25:53 -0700 | [diff] [blame] | 19 | #include <linux/oom.h> |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 20 | #include <linux/suspend.h> |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/pgalloc.h> |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 24 | #include <asm/diag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 26 | static char *sender = "VMRMSVM"; |
Heiko Carstens | 447570c | 2005-06-21 17:16:29 -0700 | [diff] [blame] | 27 | module_param(sender, charp, 0400); |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 28 | MODULE_PARM_DESC(sender, |
| 29 | "Guest name that may send SMSG messages (default VMRMSVM)"); |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "../../../drivers/s390/net/smsgiucv.h" |
| 32 | |
| 33 | #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2) |
| 34 | |
| 35 | struct cmm_page_array { |
| 36 | struct cmm_page_array *next; |
| 37 | unsigned long index; |
| 38 | unsigned long pages[CMM_NR_PAGES]; |
| 39 | }; |
| 40 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 41 | static long cmm_pages; |
| 42 | static long cmm_timed_pages; |
| 43 | static volatile long cmm_pages_target; |
| 44 | static volatile long cmm_timed_pages_target; |
| 45 | static long cmm_timeout_pages; |
| 46 | static long cmm_timeout_seconds; |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 47 | static int cmm_suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 49 | static struct cmm_page_array *cmm_page_list; |
| 50 | static struct cmm_page_array *cmm_timed_page_list; |
| 51 | static DEFINE_SPINLOCK(cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 53 | static struct task_struct *cmm_thread_ptr; |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 54 | static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait); |
| 55 | static DEFINE_TIMER(cmm_timer, NULL, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | static void cmm_timer_fn(unsigned long); |
| 58 | static void cmm_set_timer(void); |
| 59 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 60 | static long cmm_alloc_pages(long nr, long *counter, |
| 61 | struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 63 | struct cmm_page_array *pa, *npa; |
| 64 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 66 | while (nr) { |
| 67 | addr = __get_free_page(GFP_NOIO); |
| 68 | if (!addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 70 | spin_lock(&cmm_lock); |
| 71 | pa = *list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 73 | /* Need a new page for the page list. */ |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 74 | spin_unlock(&cmm_lock); |
| 75 | npa = (struct cmm_page_array *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | __get_free_page(GFP_NOIO); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 77 | if (!npa) { |
| 78 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | break; |
| 80 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 81 | spin_lock(&cmm_lock); |
| 82 | pa = *list; |
| 83 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 84 | npa->next = pa; |
| 85 | npa->index = 0; |
| 86 | pa = npa; |
| 87 | *list = pa; |
| 88 | } else |
| 89 | free_page((unsigned long) npa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 91 | diag10(addr); |
| 92 | pa->pages[pa->index++] = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | (*counter)++; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 94 | spin_unlock(&cmm_lock); |
| 95 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 97 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 100 | static long cmm_free_pages(long nr, long *counter, struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | struct cmm_page_array *pa; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 103 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 105 | spin_lock(&cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | pa = *list; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 107 | while (nr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (!pa || pa->index <= 0) |
| 109 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 110 | addr = pa->pages[--pa->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (pa->index == 0) { |
| 112 | pa = pa->next; |
| 113 | free_page((unsigned long) *list); |
| 114 | *list = pa; |
| 115 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 116 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | (*counter)--; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 118 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 120 | spin_unlock(&cmm_lock); |
| 121 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 124 | static int cmm_oom_notify(struct notifier_block *self, |
| 125 | unsigned long dummy, void *parm) |
| 126 | { |
| 127 | unsigned long *freed = parm; |
| 128 | long nr = 256; |
| 129 | |
| 130 | nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list); |
| 131 | if (nr > 0) |
| 132 | nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list); |
| 133 | cmm_pages_target = cmm_pages; |
| 134 | cmm_timed_pages_target = cmm_timed_pages; |
| 135 | *freed += 256 - nr; |
| 136 | return NOTIFY_OK; |
| 137 | } |
| 138 | |
| 139 | static struct notifier_block cmm_oom_nb = { |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 140 | .notifier_call = cmm_oom_notify, |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 143 | static int cmm_thread(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | int rc; |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | while (1) { |
| 148 | rc = wait_event_interruptible(cmm_thread_wait, |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 149 | (!cmm_suspended && (cmm_pages != cmm_pages_target || |
| 150 | cmm_timed_pages != cmm_timed_pages_target)) || |
| 151 | kthread_should_stop()); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 152 | if (kthread_should_stop() || rc == -ERESTARTSYS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | cmm_pages_target = cmm_pages; |
| 154 | cmm_timed_pages_target = cmm_timed_pages; |
| 155 | break; |
| 156 | } |
| 157 | if (cmm_pages_target > cmm_pages) { |
| 158 | if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list)) |
| 159 | cmm_pages_target = cmm_pages; |
| 160 | } else if (cmm_pages_target < cmm_pages) { |
| 161 | cmm_free_pages(1, &cmm_pages, &cmm_page_list); |
| 162 | } |
| 163 | if (cmm_timed_pages_target > cmm_timed_pages) { |
| 164 | if (cmm_alloc_pages(1, &cmm_timed_pages, |
| 165 | &cmm_timed_page_list)) |
| 166 | cmm_timed_pages_target = cmm_timed_pages; |
| 167 | } else if (cmm_timed_pages_target < cmm_timed_pages) { |
| 168 | cmm_free_pages(1, &cmm_timed_pages, |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 169 | &cmm_timed_page_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer)) |
| 172 | cmm_set_timer(); |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 177 | static void cmm_kick_thread(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | wake_up(&cmm_thread_wait); |
| 180 | } |
| 181 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 182 | static void cmm_set_timer(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) { |
| 185 | if (timer_pending(&cmm_timer)) |
| 186 | del_timer(&cmm_timer); |
| 187 | return; |
| 188 | } |
| 189 | if (timer_pending(&cmm_timer)) { |
| 190 | if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ)) |
| 191 | return; |
| 192 | } |
| 193 | cmm_timer.function = cmm_timer_fn; |
| 194 | cmm_timer.data = 0; |
| 195 | cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ; |
| 196 | add_timer(&cmm_timer); |
| 197 | } |
| 198 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 199 | static void cmm_timer_fn(unsigned long ignored) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 201 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 203 | nr = cmm_timed_pages_target - cmm_timeout_pages; |
| 204 | if (nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | cmm_timed_pages_target = 0; |
| 206 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 207 | cmm_timed_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | cmm_kick_thread(); |
| 209 | cmm_set_timer(); |
| 210 | } |
| 211 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 212 | static void cmm_set_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 214 | cmm_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | cmm_kick_thread(); |
| 216 | } |
| 217 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 218 | static long cmm_get_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
| 220 | return cmm_pages; |
| 221 | } |
| 222 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 223 | static void cmm_add_timed_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 225 | cmm_timed_pages_target += nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | cmm_kick_thread(); |
| 227 | } |
| 228 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 229 | static long cmm_get_timed_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
| 231 | return cmm_timed_pages; |
| 232 | } |
| 233 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 234 | static void cmm_set_timeout(long nr, long seconds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 236 | cmm_timeout_pages = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | cmm_timeout_seconds = seconds; |
| 238 | cmm_set_timer(); |
| 239 | } |
| 240 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 241 | static int cmm_skip_blanks(char *cp, char **endp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | char *str; |
| 244 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 245 | for (str = cp; *str == ' ' || *str == '\t'; str++) |
| 246 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | *endp = str; |
| 248 | return str != cp; |
| 249 | } |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | static struct ctl_table cmm_table[]; |
| 252 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 253 | static int cmm_pages_handler(ctl_table *ctl, int write, void __user *buffer, |
| 254 | size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | char buf[16], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 257 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | int len; |
| 259 | |
| 260 | if (!*lenp || (*ppos && !write)) { |
| 261 | *lenp = 0; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | if (write) { |
| 266 | len = *lenp; |
| 267 | if (copy_from_user(buf, buffer, |
| 268 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 269 | return -EFAULT; |
| 270 | buf[sizeof(buf) - 1] = '\0'; |
| 271 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 272 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 274 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 276 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } else { |
| 278 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 279 | nr = cmm_get_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 281 | nr = cmm_get_timed_pages(); |
| 282 | len = sprintf(buf, "%ld\n", nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (len > *lenp) |
| 284 | len = *lenp; |
| 285 | if (copy_to_user(buffer, buf, len)) |
| 286 | return -EFAULT; |
| 287 | } |
| 288 | *lenp = len; |
| 289 | *ppos += len; |
| 290 | return 0; |
| 291 | } |
| 292 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 293 | static int cmm_timeout_handler(ctl_table *ctl, int write, void __user *buffer, |
| 294 | size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
| 296 | char buf[64], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 297 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | int len; |
| 299 | |
| 300 | if (!*lenp || (*ppos && !write)) { |
| 301 | *lenp = 0; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | if (write) { |
| 306 | len = *lenp; |
| 307 | if (copy_from_user(buf, buffer, |
| 308 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 309 | return -EFAULT; |
| 310 | buf[sizeof(buf) - 1] = '\0'; |
| 311 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 312 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | cmm_skip_blanks(p, &p); |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 314 | seconds = simple_strtoul(p, &p, 0); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 315 | cmm_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } else { |
| 317 | len = sprintf(buf, "%ld %ld\n", |
| 318 | cmm_timeout_pages, cmm_timeout_seconds); |
| 319 | if (len > *lenp) |
| 320 | len = *lenp; |
| 321 | if (copy_to_user(buffer, buf, len)) |
| 322 | return -EFAULT; |
| 323 | } |
| 324 | *lenp = len; |
| 325 | *ppos += len; |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static struct ctl_table cmm_table[] = { |
| 330 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | .procname = "cmm_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 332 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 333 | .proc_handler = cmm_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | }, |
| 335 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | .procname = "cmm_timed_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 337 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 338 | .proc_handler = cmm_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | }, |
| 340 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | .procname = "cmm_timeout", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 342 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 343 | .proc_handler = cmm_timeout_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | }, |
Eric W. Biederman | b05fd35 | 2009-04-03 04:36:34 -0700 | [diff] [blame] | 345 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | static struct ctl_table cmm_dir_table[] = { |
| 349 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | .procname = "vm", |
| 351 | .maxlen = 0, |
| 352 | .mode = 0555, |
| 353 | .child = cmm_table, |
| 354 | }, |
Eric W. Biederman | b05fd35 | 2009-04-03 04:36:34 -0700 | [diff] [blame] | 355 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | #ifdef CONFIG_CMM_IUCV |
| 359 | #define SMSG_PREFIX "CMM" |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 360 | static void cmm_smsg_target(const char *from, char *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 362 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 364 | if (strlen(sender) > 0 && strcmp(from, sender) != 0) |
| 365 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg)) |
| 367 | return; |
| 368 | if (strncmp(msg, "SHRINK", 6) == 0) { |
| 369 | if (!cmm_skip_blanks(msg + 6, &msg)) |
| 370 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 371 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | cmm_skip_blanks(msg, &msg); |
| 373 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 374 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } else if (strncmp(msg, "RELEASE", 7) == 0) { |
| 376 | if (!cmm_skip_blanks(msg + 7, &msg)) |
| 377 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 378 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | cmm_skip_blanks(msg, &msg); |
| 380 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 381 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } else if (strncmp(msg, "REUSE", 5) == 0) { |
| 383 | if (!cmm_skip_blanks(msg + 5, &msg)) |
| 384 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 385 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | if (!cmm_skip_blanks(msg, &msg)) |
| 387 | return; |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 388 | seconds = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | cmm_skip_blanks(msg, &msg); |
| 390 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 391 | cmm_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | #endif |
| 395 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 396 | static struct ctl_table_header *cmm_sysctl_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 398 | static int cmm_suspend(void) |
| 399 | { |
| 400 | cmm_suspended = 1; |
| 401 | cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); |
| 402 | cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int cmm_resume(void) |
| 407 | { |
| 408 | cmm_suspended = 0; |
| 409 | cmm_kick_thread(); |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int cmm_power_event(struct notifier_block *this, |
| 414 | unsigned long event, void *ptr) |
| 415 | { |
| 416 | switch (event) { |
| 417 | case PM_POST_HIBERNATION: |
| 418 | return cmm_resume(); |
| 419 | case PM_HIBERNATION_PREPARE: |
| 420 | return cmm_suspend(); |
| 421 | default: |
| 422 | return NOTIFY_DONE; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static struct notifier_block cmm_power_notifier = { |
| 427 | .notifier_call = cmm_power_event, |
| 428 | }; |
| 429 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 430 | static int cmm_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 432 | int rc = -ENOMEM; |
| 433 | |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 434 | cmm_sysctl_header = register_sysctl_table(cmm_dir_table); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 435 | if (!cmm_sysctl_header) |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 436 | goto out_sysctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | #ifdef CONFIG_CMM_IUCV |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 438 | rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); |
| 439 | if (rc < 0) |
| 440 | goto out_smsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 442 | rc = register_oom_notifier(&cmm_oom_nb); |
| 443 | if (rc < 0) |
| 444 | goto out_oom_notify; |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 445 | rc = register_pm_notifier(&cmm_power_notifier); |
| 446 | if (rc) |
| 447 | goto out_pm; |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 448 | cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); |
| 449 | rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0; |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 450 | if (rc) |
| 451 | goto out_kthread; |
| 452 | return 0; |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 453 | |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 454 | out_kthread: |
| 455 | unregister_pm_notifier(&cmm_power_notifier); |
| 456 | out_pm: |
| 457 | unregister_oom_notifier(&cmm_oom_nb); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 458 | out_oom_notify: |
| 459 | #ifdef CONFIG_CMM_IUCV |
| 460 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 461 | out_smsg: |
| 462 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 463 | unregister_sysctl_table(cmm_sysctl_header); |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 464 | out_sysctl: |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 465 | del_timer_sync(&cmm_timer); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 466 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 468 | module_init(cmm_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 470 | static void cmm_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | unregister_sysctl_table(cmm_sysctl_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | #ifdef CONFIG_CMM_IUCV |
| 474 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 475 | #endif |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 476 | unregister_pm_notifier(&cmm_power_notifier); |
| 477 | unregister_oom_notifier(&cmm_oom_nb); |
| 478 | kthread_stop(cmm_thread_ptr); |
| 479 | del_timer_sync(&cmm_timer); |
| 480 | cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); |
| 481 | cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | module_exit(cmm_exit); |
| 484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | MODULE_LICENSE("GPL"); |