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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/pgalloc.h> |
| 22 | #include <asm/uaccess.h> |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 23 | #include <asm/diag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 25 | static char *sender = "VMRMSVM"; |
Heiko Carstens | 447570c | 2005-06-21 17:16:29 -0700 | [diff] [blame] | 26 | module_param(sender, charp, 0400); |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 27 | MODULE_PARM_DESC(sender, |
| 28 | "Guest name that may send SMSG messages (default VMRMSVM)"); |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "../../../drivers/s390/net/smsgiucv.h" |
| 31 | |
| 32 | #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2) |
| 33 | |
| 34 | struct cmm_page_array { |
| 35 | struct cmm_page_array *next; |
| 36 | unsigned long index; |
| 37 | unsigned long pages[CMM_NR_PAGES]; |
| 38 | }; |
| 39 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 40 | static long cmm_pages; |
| 41 | static long cmm_timed_pages; |
| 42 | static volatile long cmm_pages_target; |
| 43 | static volatile long cmm_timed_pages_target; |
| 44 | static long cmm_timeout_pages; |
| 45 | static long cmm_timeout_seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 47 | static struct cmm_page_array *cmm_page_list; |
| 48 | static struct cmm_page_array *cmm_timed_page_list; |
| 49 | static DEFINE_SPINLOCK(cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 51 | static struct task_struct *cmm_thread_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static wait_queue_head_t cmm_thread_wait; |
| 53 | static struct timer_list cmm_timer; |
| 54 | |
| 55 | static void cmm_timer_fn(unsigned long); |
| 56 | static void cmm_set_timer(void); |
| 57 | |
| 58 | static long |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 59 | cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 61 | struct cmm_page_array *pa, *npa; |
| 62 | unsigned long addr; |
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 | while (nr) { |
| 65 | addr = __get_free_page(GFP_NOIO); |
| 66 | if (!addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 68 | spin_lock(&cmm_lock); |
| 69 | pa = *list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 71 | /* Need a new page for the page list. */ |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 72 | spin_unlock(&cmm_lock); |
| 73 | npa = (struct cmm_page_array *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | __get_free_page(GFP_NOIO); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 75 | if (!npa) { |
| 76 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | break; |
| 78 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 79 | spin_lock(&cmm_lock); |
| 80 | pa = *list; |
| 81 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 82 | npa->next = pa; |
| 83 | npa->index = 0; |
| 84 | pa = npa; |
| 85 | *list = pa; |
| 86 | } else |
| 87 | free_page((unsigned long) npa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 89 | diag10(addr); |
| 90 | pa->pages[pa->index++] = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | (*counter)++; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 92 | spin_unlock(&cmm_lock); |
| 93 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 95 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 98 | static long |
| 99 | cmm_free_pages(long nr, long *counter, struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | struct cmm_page_array *pa; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 102 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 104 | spin_lock(&cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | pa = *list; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 106 | while (nr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | if (!pa || pa->index <= 0) |
| 108 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 109 | addr = pa->pages[--pa->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (pa->index == 0) { |
| 111 | pa = pa->next; |
| 112 | free_page((unsigned long) *list); |
| 113 | *list = pa; |
| 114 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 115 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | (*counter)--; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 117 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 119 | spin_unlock(&cmm_lock); |
| 120 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 123 | static int cmm_oom_notify(struct notifier_block *self, |
| 124 | unsigned long dummy, void *parm) |
| 125 | { |
| 126 | unsigned long *freed = parm; |
| 127 | long nr = 256; |
| 128 | |
| 129 | nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list); |
| 130 | if (nr > 0) |
| 131 | nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list); |
| 132 | cmm_pages_target = cmm_pages; |
| 133 | cmm_timed_pages_target = cmm_timed_pages; |
| 134 | *freed += 256 - nr; |
| 135 | return NOTIFY_OK; |
| 136 | } |
| 137 | |
| 138 | static struct notifier_block cmm_oom_nb = { |
| 139 | .notifier_call = cmm_oom_notify |
| 140 | }; |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static int |
| 143 | cmm_thread(void *dummy) |
| 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, |
| 149 | (cmm_pages != cmm_pages_target || |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 150 | cmm_timed_pages != cmm_timed_pages_target || |
| 151 | kthread_should_stop())); |
| 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, |
| 169 | &cmm_timed_page_list); |
| 170 | } |
| 171 | if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer)) |
| 172 | cmm_set_timer(); |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | cmm_kick_thread(void) |
| 179 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | wake_up(&cmm_thread_wait); |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | cmm_set_timer(void) |
| 185 | { |
| 186 | if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) { |
| 187 | if (timer_pending(&cmm_timer)) |
| 188 | del_timer(&cmm_timer); |
| 189 | return; |
| 190 | } |
| 191 | if (timer_pending(&cmm_timer)) { |
| 192 | if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ)) |
| 193 | return; |
| 194 | } |
| 195 | cmm_timer.function = cmm_timer_fn; |
| 196 | cmm_timer.data = 0; |
| 197 | cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ; |
| 198 | add_timer(&cmm_timer); |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | cmm_timer_fn(unsigned long ignored) |
| 203 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 204 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 206 | nr = cmm_timed_pages_target - cmm_timeout_pages; |
| 207 | if (nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | cmm_timed_pages_target = 0; |
| 209 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 210 | cmm_timed_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | cmm_kick_thread(); |
| 212 | cmm_set_timer(); |
| 213 | } |
| 214 | |
| 215 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 216 | cmm_set_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 218 | cmm_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | cmm_kick_thread(); |
| 220 | } |
| 221 | |
| 222 | long |
| 223 | cmm_get_pages(void) |
| 224 | { |
| 225 | return cmm_pages; |
| 226 | } |
| 227 | |
| 228 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 229 | cmm_add_timed_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 231 | cmm_timed_pages_target += nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | cmm_kick_thread(); |
| 233 | } |
| 234 | |
| 235 | long |
| 236 | cmm_get_timed_pages(void) |
| 237 | { |
| 238 | return cmm_timed_pages; |
| 239 | } |
| 240 | |
| 241 | void |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 242 | cmm_set_timeout(long nr, long seconds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 244 | cmm_timeout_pages = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | cmm_timeout_seconds = seconds; |
| 246 | cmm_set_timer(); |
| 247 | } |
| 248 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 249 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | cmm_skip_blanks(char *cp, char **endp) |
| 251 | { |
| 252 | char *str; |
| 253 | |
| 254 | for (str = cp; *str == ' ' || *str == '\t'; str++); |
| 255 | *endp = str; |
| 256 | return str != cp; |
| 257 | } |
| 258 | |
| 259 | #ifdef CONFIG_CMM_PROC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | static struct ctl_table cmm_table[]; |
| 262 | |
| 263 | static int |
| 264 | cmm_pages_handler(ctl_table *ctl, int write, struct file *filp, |
Al Viro | aaedd94 | 2006-02-01 06:29:14 -0500 | [diff] [blame] | 265 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
| 267 | char buf[16], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 268 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | int len; |
| 270 | |
| 271 | if (!*lenp || (*ppos && !write)) { |
| 272 | *lenp = 0; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | if (write) { |
| 277 | len = *lenp; |
| 278 | if (copy_from_user(buf, buffer, |
| 279 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 280 | return -EFAULT; |
| 281 | buf[sizeof(buf) - 1] = '\0'; |
| 282 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 283 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 285 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 287 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } else { |
| 289 | if (ctl == &cmm_table[0]) |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 290 | nr = cmm_get_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 292 | nr = cmm_get_timed_pages(); |
| 293 | len = sprintf(buf, "%ld\n", nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | if (len > *lenp) |
| 295 | len = *lenp; |
| 296 | if (copy_to_user(buffer, buf, len)) |
| 297 | return -EFAULT; |
| 298 | } |
| 299 | *lenp = len; |
| 300 | *ppos += len; |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int |
| 305 | cmm_timeout_handler(ctl_table *ctl, int write, struct file *filp, |
Al Viro | aaedd94 | 2006-02-01 06:29:14 -0500 | [diff] [blame] | 306 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
| 308 | char buf[64], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 309 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | int len; |
| 311 | |
| 312 | if (!*lenp || (*ppos && !write)) { |
| 313 | *lenp = 0; |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | if (write) { |
| 318 | len = *lenp; |
| 319 | if (copy_from_user(buf, buffer, |
| 320 | len > sizeof(buf) ? sizeof(buf) : len)) |
| 321 | return -EFAULT; |
| 322 | buf[sizeof(buf) - 1] = '\0'; |
| 323 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 324 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | cmm_skip_blanks(p, &p); |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 326 | seconds = simple_strtoul(p, &p, 0); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 327 | cmm_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } else { |
| 329 | len = sprintf(buf, "%ld %ld\n", |
| 330 | cmm_timeout_pages, cmm_timeout_seconds); |
| 331 | if (len > *lenp) |
| 332 | len = *lenp; |
| 333 | if (copy_to_user(buffer, buf, len)) |
| 334 | return -EFAULT; |
| 335 | } |
| 336 | *lenp = len; |
| 337 | *ppos += len; |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static struct ctl_table cmm_table[] = { |
| 342 | { |
| 343 | .ctl_name = VM_CMM_PAGES, |
| 344 | .procname = "cmm_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 345 | .mode = 0644, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | .proc_handler = &cmm_pages_handler, |
| 347 | }, |
| 348 | { |
| 349 | .ctl_name = VM_CMM_TIMED_PAGES, |
| 350 | .procname = "cmm_timed_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 351 | .mode = 0644, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | .proc_handler = &cmm_pages_handler, |
| 353 | }, |
| 354 | { |
| 355 | .ctl_name = VM_CMM_TIMEOUT, |
| 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 | |
| 416 | static int |
| 417 | cmm_init (void) |
| 418 | { |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 419 | int rc = -ENOMEM; |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | #ifdef CONFIG_CMM_PROC |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 422 | cmm_sysctl_header = register_sysctl_table(cmm_dir_table); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 423 | if (!cmm_sysctl_header) |
| 424 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | #endif |
| 426 | #ifdef CONFIG_CMM_IUCV |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 427 | rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); |
| 428 | if (rc < 0) |
| 429 | goto out_smsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 431 | rc = register_oom_notifier(&cmm_oom_nb); |
| 432 | if (rc < 0) |
| 433 | goto out_oom_notify; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | init_waitqueue_head(&cmm_thread_wait); |
| 435 | init_timer(&cmm_timer); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 436 | cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); |
| 437 | rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0; |
| 438 | if (!rc) |
| 439 | goto out; |
| 440 | /* |
| 441 | * kthread_create failed. undo all the stuff from above again. |
| 442 | */ |
| 443 | unregister_oom_notifier(&cmm_oom_nb); |
| 444 | |
| 445 | out_oom_notify: |
| 446 | #ifdef CONFIG_CMM_IUCV |
| 447 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 448 | out_smsg: |
| 449 | #endif |
| 450 | #ifdef CONFIG_CMM_PROC |
| 451 | unregister_sysctl_table(cmm_sysctl_header); |
| 452 | #endif |
| 453 | out: |
| 454 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static void |
| 458 | cmm_exit(void) |
| 459 | { |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 460 | kthread_stop(cmm_thread_ptr); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 461 | unregister_oom_notifier(&cmm_oom_nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); |
| 463 | cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list); |
| 464 | #ifdef CONFIG_CMM_PROC |
| 465 | unregister_sysctl_table(cmm_sysctl_header); |
| 466 | #endif |
| 467 | #ifdef CONFIG_CMM_IUCV |
| 468 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 469 | #endif |
| 470 | } |
| 471 | |
| 472 | module_init(cmm_init); |
| 473 | module_exit(cmm_exit); |
| 474 | |
| 475 | EXPORT_SYMBOL(cmm_set_pages); |
| 476 | EXPORT_SYMBOL(cmm_get_pages); |
| 477 | EXPORT_SYMBOL(cmm_add_timed_pages); |
| 478 | EXPORT_SYMBOL(cmm_get_timed_pages); |
| 479 | EXPORT_SYMBOL(cmm_set_timeout); |
| 480 | |
| 481 | MODULE_LICENSE("GPL"); |