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