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 | 5a3135c | 2007-10-16 23:25:53 -0700 | [diff] [blame] | 20 | #include <linux/oom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/pgalloc.h> |
| 23 | #include <asm/uaccess.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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 48 | static struct cmm_page_array *cmm_page_list; |
| 49 | static struct cmm_page_array *cmm_timed_page_list; |
| 50 | static DEFINE_SPINLOCK(cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 52 | static struct task_struct *cmm_thread_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static wait_queue_head_t cmm_thread_wait; |
| 54 | static struct timer_list cmm_timer; |
| 55 | |
| 56 | static void cmm_timer_fn(unsigned long); |
| 57 | static void cmm_set_timer(void); |
| 58 | |
| 59 | static long |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 60 | cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 62 | struct cmm_page_array *pa, *npa; |
| 63 | unsigned long addr; |
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 | while (nr) { |
| 66 | addr = __get_free_page(GFP_NOIO); |
| 67 | if (!addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 69 | spin_lock(&cmm_lock); |
| 70 | pa = *list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 72 | /* Need a new page for the page list. */ |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 73 | spin_unlock(&cmm_lock); |
| 74 | npa = (struct cmm_page_array *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | __get_free_page(GFP_NOIO); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 76 | if (!npa) { |
| 77 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
| 79 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 80 | spin_lock(&cmm_lock); |
| 81 | pa = *list; |
| 82 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 83 | npa->next = pa; |
| 84 | npa->index = 0; |
| 85 | pa = npa; |
| 86 | *list = pa; |
| 87 | } else |
| 88 | free_page((unsigned long) npa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 90 | diag10(addr); |
| 91 | pa->pages[pa->index++] = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | (*counter)++; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 93 | spin_unlock(&cmm_lock); |
| 94 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 96 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 99 | static long |
| 100 | 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 = { |
| 140 | .notifier_call = cmm_oom_notify |
| 141 | }; |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | static int |
| 144 | cmm_thread(void *dummy) |
| 145 | { |
| 146 | int rc; |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | while (1) { |
| 149 | rc = wait_event_interruptible(cmm_thread_wait, |
| 150 | (cmm_pages != cmm_pages_target || |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 151 | cmm_timed_pages != cmm_timed_pages_target || |
| 152 | kthread_should_stop())); |
| 153 | if (kthread_should_stop() || rc == -ERESTARTSYS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | cmm_pages_target = cmm_pages; |
| 155 | cmm_timed_pages_target = cmm_timed_pages; |
| 156 | break; |
| 157 | } |
| 158 | if (cmm_pages_target > cmm_pages) { |
| 159 | if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list)) |
| 160 | cmm_pages_target = cmm_pages; |
| 161 | } else if (cmm_pages_target < cmm_pages) { |
| 162 | cmm_free_pages(1, &cmm_pages, &cmm_page_list); |
| 163 | } |
| 164 | if (cmm_timed_pages_target > cmm_timed_pages) { |
| 165 | if (cmm_alloc_pages(1, &cmm_timed_pages, |
| 166 | &cmm_timed_page_list)) |
| 167 | cmm_timed_pages_target = cmm_timed_pages; |
| 168 | } else if (cmm_timed_pages_target < cmm_timed_pages) { |
| 169 | cmm_free_pages(1, &cmm_timed_pages, |
| 170 | &cmm_timed_page_list); |
| 171 | } |
| 172 | if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer)) |
| 173 | cmm_set_timer(); |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | cmm_kick_thread(void) |
| 180 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | wake_up(&cmm_thread_wait); |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | cmm_set_timer(void) |
| 186 | { |
| 187 | if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) { |
| 188 | if (timer_pending(&cmm_timer)) |
| 189 | del_timer(&cmm_timer); |
| 190 | return; |
| 191 | } |
| 192 | if (timer_pending(&cmm_timer)) { |
| 193 | if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ)) |
| 194 | return; |
| 195 | } |
| 196 | cmm_timer.function = cmm_timer_fn; |
| 197 | cmm_timer.data = 0; |
| 198 | cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ; |
| 199 | add_timer(&cmm_timer); |
| 200 | } |
| 201 | |
| 202 | static void |
| 203 | cmm_timer_fn(unsigned long ignored) |
| 204 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 205 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 207 | nr = cmm_timed_pages_target - cmm_timeout_pages; |
| 208 | if (nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | cmm_timed_pages_target = 0; |
| 210 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 211 | cmm_timed_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | cmm_kick_thread(); |
| 213 | cmm_set_timer(); |
| 214 | } |
| 215 | |
| 216 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 217 | cmm_set_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 219 | cmm_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | cmm_kick_thread(); |
| 221 | } |
| 222 | |
| 223 | long |
| 224 | cmm_get_pages(void) |
| 225 | { |
| 226 | return cmm_pages; |
| 227 | } |
| 228 | |
| 229 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 230 | cmm_add_timed_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 232 | cmm_timed_pages_target += nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | cmm_kick_thread(); |
| 234 | } |
| 235 | |
| 236 | long |
| 237 | cmm_get_timed_pages(void) |
| 238 | { |
| 239 | return cmm_timed_pages; |
| 240 | } |
| 241 | |
| 242 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 243 | cmm_set_timeout(long nr, long seconds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 245 | cmm_timeout_pages = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | cmm_timeout_seconds = seconds; |
| 247 | cmm_set_timer(); |
| 248 | } |
| 249 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 250 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | cmm_skip_blanks(char *cp, char **endp) |
| 252 | { |
| 253 | char *str; |
| 254 | |
| 255 | for (str = cp; *str == ' ' || *str == '\t'; str++); |
| 256 | *endp = str; |
| 257 | return str != cp; |
| 258 | } |
| 259 | |
| 260 | #ifdef CONFIG_CMM_PROC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | static struct ctl_table cmm_table[]; |
| 263 | |
| 264 | static int |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 265 | cmm_pages_handler(ctl_table *ctl, int write, |
Al Viro | aaedd94 | 2006-02-01 06:29:14 -0500 | [diff] [blame] | 266 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | char buf[16], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 269 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | int len; |
| 271 | |
| 272 | if (!*lenp || (*ppos && !write)) { |
| 273 | *lenp = 0; |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | if (write) { |
| 278 | len = *lenp; |
| 279 | if (copy_from_user(buf, buffer, |
| 280 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 281 | return -EFAULT; |
| 282 | buf[sizeof(buf) - 1] = '\0'; |
| 283 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 284 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 286 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 288 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } else { |
| 290 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 291 | nr = cmm_get_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 293 | nr = cmm_get_timed_pages(); |
| 294 | len = sprintf(buf, "%ld\n", nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (len > *lenp) |
| 296 | len = *lenp; |
| 297 | if (copy_to_user(buffer, buf, len)) |
| 298 | return -EFAULT; |
| 299 | } |
| 300 | *lenp = len; |
| 301 | *ppos += len; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 306 | cmm_timeout_handler(ctl_table *ctl, int write, |
Al Viro | aaedd94 | 2006-02-01 06:29:14 -0500 | [diff] [blame] | 307 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | char buf[64], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 310 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | int len; |
| 312 | |
| 313 | if (!*lenp || (*ppos && !write)) { |
| 314 | *lenp = 0; |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | if (write) { |
| 319 | len = *lenp; |
| 320 | if (copy_from_user(buf, buffer, |
| 321 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 322 | return -EFAULT; |
| 323 | buf[sizeof(buf) - 1] = '\0'; |
| 324 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 325 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | cmm_skip_blanks(p, &p); |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 327 | seconds = simple_strtoul(p, &p, 0); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 328 | cmm_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } else { |
| 330 | len = sprintf(buf, "%ld %ld\n", |
| 331 | cmm_timeout_pages, cmm_timeout_seconds); |
| 332 | if (len > *lenp) |
| 333 | len = *lenp; |
| 334 | if (copy_to_user(buffer, buf, len)) |
| 335 | return -EFAULT; |
| 336 | } |
| 337 | *lenp = len; |
| 338 | *ppos += len; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static struct ctl_table cmm_table[] = { |
| 343 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | .procname = "cmm_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 345 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 346 | .proc_handler = cmm_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | }, |
| 348 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | .procname = "cmm_timed_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 350 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 351 | .proc_handler = cmm_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | }, |
| 353 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | .procname = "cmm_timeout", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 355 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 356 | .proc_handler = cmm_timeout_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | }, |
Eric W. Biederman | b05fd35 | 2009-04-03 04:36:34 -0700 | [diff] [blame] | 358 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | static struct ctl_table cmm_dir_table[] = { |
| 362 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | .procname = "vm", |
| 364 | .maxlen = 0, |
| 365 | .mode = 0555, |
| 366 | .child = cmm_table, |
| 367 | }, |
Eric W. Biederman | b05fd35 | 2009-04-03 04:36:34 -0700 | [diff] [blame] | 368 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | }; |
| 370 | #endif |
| 371 | |
| 372 | #ifdef CONFIG_CMM_IUCV |
| 373 | #define SMSG_PREFIX "CMM" |
| 374 | static void |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 375 | cmm_smsg_target(char *from, char *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 377 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 379 | if (strlen(sender) > 0 && strcmp(from, sender) != 0) |
| 380 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg)) |
| 382 | return; |
| 383 | if (strncmp(msg, "SHRINK", 6) == 0) { |
| 384 | if (!cmm_skip_blanks(msg + 6, &msg)) |
| 385 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 386 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | cmm_skip_blanks(msg, &msg); |
| 388 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 389 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } else if (strncmp(msg, "RELEASE", 7) == 0) { |
| 391 | if (!cmm_skip_blanks(msg + 7, &msg)) |
| 392 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 393 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | cmm_skip_blanks(msg, &msg); |
| 395 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 396 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } else if (strncmp(msg, "REUSE", 5) == 0) { |
| 398 | if (!cmm_skip_blanks(msg + 5, &msg)) |
| 399 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 400 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | if (!cmm_skip_blanks(msg, &msg)) |
| 402 | return; |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 403 | seconds = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | cmm_skip_blanks(msg, &msg); |
| 405 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 406 | cmm_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | #endif |
| 410 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 411 | static struct ctl_table_header *cmm_sysctl_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | static int |
| 414 | cmm_init (void) |
| 415 | { |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 416 | int rc = -ENOMEM; |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | #ifdef CONFIG_CMM_PROC |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 419 | cmm_sysctl_header = register_sysctl_table(cmm_dir_table); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 420 | if (!cmm_sysctl_header) |
| 421 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | #endif |
| 423 | #ifdef CONFIG_CMM_IUCV |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 424 | rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); |
| 425 | if (rc < 0) |
| 426 | goto out_smsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 428 | rc = register_oom_notifier(&cmm_oom_nb); |
| 429 | if (rc < 0) |
| 430 | goto out_oom_notify; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | init_waitqueue_head(&cmm_thread_wait); |
| 432 | init_timer(&cmm_timer); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 433 | cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); |
| 434 | rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0; |
| 435 | if (!rc) |
| 436 | goto out; |
| 437 | /* |
| 438 | * kthread_create failed. undo all the stuff from above again. |
| 439 | */ |
| 440 | unregister_oom_notifier(&cmm_oom_nb); |
| 441 | |
| 442 | out_oom_notify: |
| 443 | #ifdef CONFIG_CMM_IUCV |
| 444 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 445 | out_smsg: |
| 446 | #endif |
| 447 | #ifdef CONFIG_CMM_PROC |
| 448 | unregister_sysctl_table(cmm_sysctl_header); |
| 449 | #endif |
| 450 | out: |
| 451 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void |
| 455 | cmm_exit(void) |
| 456 | { |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 457 | kthread_stop(cmm_thread_ptr); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 458 | unregister_oom_notifier(&cmm_oom_nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); |
| 460 | cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list); |
| 461 | #ifdef CONFIG_CMM_PROC |
| 462 | unregister_sysctl_table(cmm_sysctl_header); |
| 463 | #endif |
| 464 | #ifdef CONFIG_CMM_IUCV |
| 465 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 466 | #endif |
| 467 | } |
| 468 | |
| 469 | module_init(cmm_init); |
| 470 | module_exit(cmm_exit); |
| 471 | |
| 472 | EXPORT_SYMBOL(cmm_set_pages); |
| 473 | EXPORT_SYMBOL(cmm_get_pages); |
| 474 | EXPORT_SYMBOL(cmm_add_timed_pages); |
| 475 | EXPORT_SYMBOL(cmm_get_timed_pages); |
| 476 | EXPORT_SYMBOL(cmm_set_timeout); |
| 477 | |
| 478 | MODULE_LICENSE("GPL"); |