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> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/sysctl.h> |
| 17 | #include <linux/ctype.h> |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 18 | #include <linux/swap.h> |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 19 | #include <linux/kthread.h> |
David Rientjes | 5a3135c2 | 2007-10-16 23:25:53 -0700 | [diff] [blame] | 20 | #include <linux/oom.h> |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame^] | 21 | #include <linux/suspend.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/pgalloc.h> |
| 24 | #include <asm/uaccess.h> |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 25 | #include <asm/diag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 27 | static char *sender = "VMRMSVM"; |
Heiko Carstens | 447570c | 2005-06-21 17:16:29 -0700 | [diff] [blame] | 28 | module_param(sender, charp, 0400); |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 29 | MODULE_PARM_DESC(sender, |
| 30 | "Guest name that may send SMSG messages (default VMRMSVM)"); |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "../../../drivers/s390/net/smsgiucv.h" |
| 33 | |
| 34 | #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2) |
| 35 | |
| 36 | struct cmm_page_array { |
| 37 | struct cmm_page_array *next; |
| 38 | unsigned long index; |
| 39 | unsigned long pages[CMM_NR_PAGES]; |
| 40 | }; |
| 41 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 42 | static long cmm_pages; |
| 43 | static long cmm_timed_pages; |
| 44 | static volatile long cmm_pages_target; |
| 45 | static volatile long cmm_timed_pages_target; |
| 46 | static long cmm_timeout_pages; |
| 47 | static long cmm_timeout_seconds; |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame^] | 48 | static int cmm_suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 50 | static struct cmm_page_array *cmm_page_list; |
| 51 | static struct cmm_page_array *cmm_timed_page_list; |
| 52 | static DEFINE_SPINLOCK(cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 54 | static struct task_struct *cmm_thread_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static wait_queue_head_t cmm_thread_wait; |
| 56 | static struct timer_list cmm_timer; |
| 57 | |
| 58 | static void cmm_timer_fn(unsigned long); |
| 59 | static void cmm_set_timer(void); |
| 60 | |
| 61 | static long |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 62 | cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 64 | struct cmm_page_array *pa, *npa; |
| 65 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 67 | while (nr) { |
| 68 | addr = __get_free_page(GFP_NOIO); |
| 69 | if (!addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 71 | spin_lock(&cmm_lock); |
| 72 | pa = *list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 74 | /* Need a new page for the page list. */ |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 75 | spin_unlock(&cmm_lock); |
| 76 | npa = (struct cmm_page_array *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | __get_free_page(GFP_NOIO); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 78 | if (!npa) { |
| 79 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | break; |
| 81 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 82 | spin_lock(&cmm_lock); |
| 83 | pa = *list; |
| 84 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 85 | npa->next = pa; |
| 86 | npa->index = 0; |
| 87 | pa = npa; |
| 88 | *list = pa; |
| 89 | } else |
| 90 | free_page((unsigned long) npa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 92 | diag10(addr); |
| 93 | pa->pages[pa->index++] = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | (*counter)++; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 95 | spin_unlock(&cmm_lock); |
| 96 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 98 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 101 | static long |
| 102 | cmm_free_pages(long nr, long *counter, struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | struct cmm_page_array *pa; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 105 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 107 | spin_lock(&cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | pa = *list; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 109 | while (nr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (!pa || pa->index <= 0) |
| 111 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 112 | addr = pa->pages[--pa->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if (pa->index == 0) { |
| 114 | pa = pa->next; |
| 115 | free_page((unsigned long) *list); |
| 116 | *list = pa; |
| 117 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 118 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | (*counter)--; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 120 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 122 | spin_unlock(&cmm_lock); |
| 123 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 126 | static int cmm_oom_notify(struct notifier_block *self, |
| 127 | unsigned long dummy, void *parm) |
| 128 | { |
| 129 | unsigned long *freed = parm; |
| 130 | long nr = 256; |
| 131 | |
| 132 | nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list); |
| 133 | if (nr > 0) |
| 134 | nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list); |
| 135 | cmm_pages_target = cmm_pages; |
| 136 | cmm_timed_pages_target = cmm_timed_pages; |
| 137 | *freed += 256 - nr; |
| 138 | return NOTIFY_OK; |
| 139 | } |
| 140 | |
| 141 | static struct notifier_block cmm_oom_nb = { |
| 142 | .notifier_call = cmm_oom_notify |
| 143 | }; |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | static int |
| 146 | cmm_thread(void *dummy) |
| 147 | { |
| 148 | int rc; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | while (1) { |
| 151 | rc = wait_event_interruptible(cmm_thread_wait, |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame^] | 152 | (!cmm_suspended && (cmm_pages != cmm_pages_target || |
| 153 | cmm_timed_pages != cmm_timed_pages_target)) || |
| 154 | kthread_should_stop()); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 155 | if (kthread_should_stop() || rc == -ERESTARTSYS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | cmm_pages_target = cmm_pages; |
| 157 | cmm_timed_pages_target = cmm_timed_pages; |
| 158 | break; |
| 159 | } |
| 160 | if (cmm_pages_target > cmm_pages) { |
| 161 | if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list)) |
| 162 | cmm_pages_target = cmm_pages; |
| 163 | } else if (cmm_pages_target < cmm_pages) { |
| 164 | cmm_free_pages(1, &cmm_pages, &cmm_page_list); |
| 165 | } |
| 166 | if (cmm_timed_pages_target > cmm_timed_pages) { |
| 167 | if (cmm_alloc_pages(1, &cmm_timed_pages, |
| 168 | &cmm_timed_page_list)) |
| 169 | cmm_timed_pages_target = cmm_timed_pages; |
| 170 | } else if (cmm_timed_pages_target < cmm_timed_pages) { |
| 171 | cmm_free_pages(1, &cmm_timed_pages, |
| 172 | &cmm_timed_page_list); |
| 173 | } |
| 174 | if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer)) |
| 175 | cmm_set_timer(); |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | cmm_kick_thread(void) |
| 182 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | wake_up(&cmm_thread_wait); |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | cmm_set_timer(void) |
| 188 | { |
| 189 | if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) { |
| 190 | if (timer_pending(&cmm_timer)) |
| 191 | del_timer(&cmm_timer); |
| 192 | return; |
| 193 | } |
| 194 | if (timer_pending(&cmm_timer)) { |
| 195 | if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ)) |
| 196 | return; |
| 197 | } |
| 198 | cmm_timer.function = cmm_timer_fn; |
| 199 | cmm_timer.data = 0; |
| 200 | cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ; |
| 201 | add_timer(&cmm_timer); |
| 202 | } |
| 203 | |
| 204 | static void |
| 205 | cmm_timer_fn(unsigned long ignored) |
| 206 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 207 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 209 | nr = cmm_timed_pages_target - cmm_timeout_pages; |
| 210 | if (nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | cmm_timed_pages_target = 0; |
| 212 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 213 | cmm_timed_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | cmm_kick_thread(); |
| 215 | cmm_set_timer(); |
| 216 | } |
| 217 | |
| 218 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 219 | cmm_set_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 221 | cmm_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | cmm_kick_thread(); |
| 223 | } |
| 224 | |
| 225 | long |
| 226 | cmm_get_pages(void) |
| 227 | { |
| 228 | return cmm_pages; |
| 229 | } |
| 230 | |
| 231 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 232 | cmm_add_timed_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 234 | cmm_timed_pages_target += nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | cmm_kick_thread(); |
| 236 | } |
| 237 | |
| 238 | long |
| 239 | cmm_get_timed_pages(void) |
| 240 | { |
| 241 | return cmm_timed_pages; |
| 242 | } |
| 243 | |
| 244 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 245 | cmm_set_timeout(long nr, long seconds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 247 | cmm_timeout_pages = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | cmm_timeout_seconds = seconds; |
| 249 | cmm_set_timer(); |
| 250 | } |
| 251 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 252 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | cmm_skip_blanks(char *cp, char **endp) |
| 254 | { |
| 255 | char *str; |
| 256 | |
| 257 | for (str = cp; *str == ' ' || *str == '\t'; str++); |
| 258 | *endp = str; |
| 259 | return str != cp; |
| 260 | } |
| 261 | |
| 262 | #ifdef CONFIG_CMM_PROC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | static struct ctl_table cmm_table[]; |
| 265 | |
| 266 | static int |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 267 | cmm_pages_handler(ctl_table *ctl, int write, |
Al Viro | aaedd94 | 2006-02-01 06:29:14 -0500 | [diff] [blame] | 268 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
| 270 | char buf[16], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 271 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | int len; |
| 273 | |
| 274 | if (!*lenp || (*ppos && !write)) { |
| 275 | *lenp = 0; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | if (write) { |
| 280 | len = *lenp; |
| 281 | if (copy_from_user(buf, buffer, |
| 282 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 283 | return -EFAULT; |
| 284 | buf[sizeof(buf) - 1] = '\0'; |
| 285 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 286 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 288 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 290 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } else { |
| 292 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 293 | nr = cmm_get_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 295 | nr = cmm_get_timed_pages(); |
| 296 | len = sprintf(buf, "%ld\n", nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | if (len > *lenp) |
| 298 | len = *lenp; |
| 299 | if (copy_to_user(buffer, buf, len)) |
| 300 | return -EFAULT; |
| 301 | } |
| 302 | *lenp = len; |
| 303 | *ppos += len; |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 308 | cmm_timeout_handler(ctl_table *ctl, int write, |
Al Viro | aaedd94 | 2006-02-01 06:29:14 -0500 | [diff] [blame] | 309 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
| 311 | char buf[64], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 312 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | int len; |
| 314 | |
| 315 | if (!*lenp || (*ppos && !write)) { |
| 316 | *lenp = 0; |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | if (write) { |
| 321 | len = *lenp; |
| 322 | if (copy_from_user(buf, buffer, |
| 323 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 324 | return -EFAULT; |
| 325 | buf[sizeof(buf) - 1] = '\0'; |
| 326 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 327 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | cmm_skip_blanks(p, &p); |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 329 | seconds = simple_strtoul(p, &p, 0); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 330 | cmm_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } else { |
| 332 | len = sprintf(buf, "%ld %ld\n", |
| 333 | cmm_timeout_pages, cmm_timeout_seconds); |
| 334 | if (len > *lenp) |
| 335 | len = *lenp; |
| 336 | if (copy_to_user(buffer, buf, len)) |
| 337 | return -EFAULT; |
| 338 | } |
| 339 | *lenp = len; |
| 340 | *ppos += len; |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static struct ctl_table cmm_table[] = { |
| 345 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | .procname = "cmm_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 347 | .mode = 0644, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | .proc_handler = &cmm_pages_handler, |
| 349 | }, |
| 350 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | .procname = "cmm_timed_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 352 | .mode = 0644, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | .proc_handler = &cmm_pages_handler, |
| 354 | }, |
| 355 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | .procname = "cmm_timeout", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 357 | .mode = 0644, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | .proc_handler = &cmm_timeout_handler, |
| 359 | }, |
| 360 | { .ctl_name = 0 } |
| 361 | }; |
| 362 | |
| 363 | static struct ctl_table cmm_dir_table[] = { |
| 364 | { |
| 365 | .ctl_name = CTL_VM, |
| 366 | .procname = "vm", |
| 367 | .maxlen = 0, |
| 368 | .mode = 0555, |
| 369 | .child = cmm_table, |
| 370 | }, |
| 371 | { .ctl_name = 0 } |
| 372 | }; |
| 373 | #endif |
| 374 | |
| 375 | #ifdef CONFIG_CMM_IUCV |
| 376 | #define SMSG_PREFIX "CMM" |
| 377 | static void |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 378 | cmm_smsg_target(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"); |