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