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