Greg Kroah-Hartman | ac41aae | 2017-11-24 15:00:35 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Collaborative memory management interface. |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 4 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp 2003, 2010 |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 6 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
Paul Gortmaker | ff24b07 | 2017-02-09 15:20:24 -0500 | [diff] [blame] | 14 | #include <linux/moduleparam.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/sched.h> |
| 17 | #include <linux/sysctl.h> |
| 18 | #include <linux/ctype.h> |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 19 | #include <linux/swap.h> |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 20 | #include <linux/kthread.h> |
David Rientjes | 5a3135c2 | 2007-10-16 23:25:53 -0700 | [diff] [blame] | 21 | #include <linux/oom.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; |
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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Kees Cook | 6cc73a0 | 2017-10-23 22:41:17 -0700 | [diff] [blame] | 59 | static void cmm_timer_fn(struct timer_list *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static void cmm_set_timer(void); |
Kees Cook | 6cc73a0 | 2017-10-23 22:41:17 -0700 | [diff] [blame] | 61 | static DEFINE_TIMER(cmm_timer, cmm_timer_fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Michael Holzheu | 83ace270 | 2011-05-10 17:13:41 +0200 | [diff] [blame] | 94 | diag10_range(addr >> PAGE_SHIFT, 1); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 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, |
Heiko Carstens | 3942162 | 2020-03-18 20:55:24 +0100 | [diff] [blame^] | 152 | 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 | } |
Vasily Gorbik | ac4c4fc | 2018-07-13 08:13:13 +0200 | [diff] [blame] | 192 | mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Kees Cook | 6cc73a0 | 2017-10-23 22:41:17 -0700 | [diff] [blame] | 195 | static void cmm_timer_fn(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 197 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 199 | nr = cmm_timed_pages_target - cmm_timeout_pages; |
| 200 | if (nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | cmm_timed_pages_target = 0; |
| 202 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 203 | cmm_timed_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | cmm_kick_thread(); |
| 205 | cmm_set_timer(); |
| 206 | } |
| 207 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 208 | static void cmm_set_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 210 | cmm_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | cmm_kick_thread(); |
| 212 | } |
| 213 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 214 | static long cmm_get_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | return cmm_pages; |
| 217 | } |
| 218 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 219 | static void cmm_add_timed_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 221 | cmm_timed_pages_target += nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | cmm_kick_thread(); |
| 223 | } |
| 224 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 225 | static long cmm_get_timed_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | return cmm_timed_pages; |
| 228 | } |
| 229 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 230 | static void cmm_set_timeout(long nr, long seconds) |
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_timeout_pages = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | cmm_timeout_seconds = seconds; |
| 234 | cmm_set_timer(); |
| 235 | } |
| 236 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 237 | static int cmm_skip_blanks(char *cp, char **endp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | char *str; |
| 240 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 241 | for (str = cp; *str == ' ' || *str == '\t'; str++) |
| 242 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | *endp = str; |
| 244 | return str != cp; |
| 245 | } |
| 246 | |
Joe Perches | 302bfe2 | 2013-10-22 15:29:46 -0700 | [diff] [blame] | 247 | static int cmm_pages_handler(struct ctl_table *ctl, int write, |
| 248 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 250 | long nr = cmm_get_pages(); |
| 251 | struct ctl_table ctl_entry = { |
| 252 | .procname = ctl->procname, |
| 253 | .data = &nr, |
| 254 | .maxlen = sizeof(long), |
| 255 | }; |
| 256 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 258 | rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos); |
| 259 | if (rc < 0 || !write) |
| 260 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 262 | cmm_set_pages(nr); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int cmm_timed_pages_handler(struct ctl_table *ctl, int write, |
| 267 | void __user *buffer, size_t *lenp, |
| 268 | loff_t *ppos) |
| 269 | { |
| 270 | long nr = cmm_get_timed_pages(); |
| 271 | struct ctl_table ctl_entry = { |
| 272 | .procname = ctl->procname, |
| 273 | .data = &nr, |
| 274 | .maxlen = sizeof(long), |
| 275 | }; |
| 276 | int rc; |
| 277 | |
| 278 | rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos); |
| 279 | if (rc < 0 || !write) |
| 280 | return rc; |
| 281 | |
| 282 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
Joe Perches | 302bfe2 | 2013-10-22 15:29:46 -0700 | [diff] [blame] | 286 | static int cmm_timeout_handler(struct ctl_table *ctl, int write, |
| 287 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | char buf[64], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 290 | long nr, seconds; |
Heiko Carstens | 041058a | 2013-10-14 13:58:10 +0200 | [diff] [blame] | 291 | unsigned int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | if (!*lenp || (*ppos && !write)) { |
| 294 | *lenp = 0; |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | if (write) { |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 299 | len = min(*lenp, sizeof(buf)); |
| 300 | if (copy_from_user(buf, buffer, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return -EFAULT; |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 302 | buf[len - 1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 304 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | cmm_skip_blanks(p, &p); |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 306 | seconds = simple_strtoul(p, &p, 0); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 307 | cmm_set_timeout(nr, seconds); |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 308 | *ppos += *lenp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } else { |
| 310 | len = sprintf(buf, "%ld %ld\n", |
| 311 | cmm_timeout_pages, cmm_timeout_seconds); |
| 312 | if (len > *lenp) |
| 313 | len = *lenp; |
| 314 | if (copy_to_user(buffer, buf, len)) |
| 315 | return -EFAULT; |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 316 | *lenp = len; |
| 317 | *ppos += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static struct ctl_table cmm_table[] = { |
| 323 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | .procname = "cmm_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 325 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 326 | .proc_handler = cmm_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | }, |
| 328 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | .procname = "cmm_timed_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 330 | .mode = 0644, |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 331 | .proc_handler = cmm_timed_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | }, |
| 333 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | .procname = "cmm_timeout", |
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_timeout_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | }, |
Eric W. Biederman | b05fd35 | 2009-04-03 04:36:34 -0700 | [diff] [blame] | 338 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | static struct ctl_table cmm_dir_table[] = { |
| 342 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | .procname = "vm", |
| 344 | .maxlen = 0, |
| 345 | .mode = 0555, |
| 346 | .child = cmm_table, |
| 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 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | #ifdef CONFIG_CMM_IUCV |
| 352 | #define SMSG_PREFIX "CMM" |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 353 | static void cmm_smsg_target(const char *from, char *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 355 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 357 | if (strlen(sender) > 0 && strcmp(from, sender) != 0) |
| 358 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg)) |
| 360 | return; |
| 361 | if (strncmp(msg, "SHRINK", 6) == 0) { |
| 362 | if (!cmm_skip_blanks(msg + 6, &msg)) |
| 363 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 364 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | cmm_skip_blanks(msg, &msg); |
| 366 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 367 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } else if (strncmp(msg, "RELEASE", 7) == 0) { |
| 369 | if (!cmm_skip_blanks(msg + 7, &msg)) |
| 370 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 371 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | cmm_skip_blanks(msg, &msg); |
| 373 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 374 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } else if (strncmp(msg, "REUSE", 5) == 0) { |
| 376 | if (!cmm_skip_blanks(msg + 5, &msg)) |
| 377 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 378 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (!cmm_skip_blanks(msg, &msg)) |
| 380 | return; |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 381 | seconds = 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_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | #endif |
| 388 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 389 | static struct ctl_table_header *cmm_sysctl_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Heiko Carstens | 2e85ba5 | 2010-08-09 18:12:55 +0200 | [diff] [blame] | 391 | static int __init cmm_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 393 | int rc = -ENOMEM; |
| 394 | |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 395 | cmm_sysctl_header = register_sysctl_table(cmm_dir_table); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 396 | if (!cmm_sysctl_header) |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 397 | goto out_sysctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | #ifdef CONFIG_CMM_IUCV |
Hendrik Brueckner | 41b4287 | 2010-08-09 18:12:56 +0200 | [diff] [blame] | 399 | /* convert sender to uppercase characters */ |
| 400 | if (sender) { |
| 401 | int len = strlen(sender); |
| 402 | while (len--) |
| 403 | sender[len] = toupper(sender[len]); |
Heiko Carstens | a20852d | 2010-10-25 16:10:21 +0200 | [diff] [blame] | 404 | } else { |
| 405 | sender = cmm_default_sender; |
Hendrik Brueckner | 41b4287 | 2010-08-09 18:12:56 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 408 | rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); |
| 409 | if (rc < 0) |
| 410 | goto out_smsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 412 | rc = register_oom_notifier(&cmm_oom_nb); |
| 413 | if (rc < 0) |
| 414 | goto out_oom_notify; |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 415 | cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); |
Alexandru Gheorghiu | 8fe853f | 2013-03-13 21:46:08 +0200 | [diff] [blame] | 416 | if (!IS_ERR(cmm_thread_ptr)) |
| 417 | return 0; |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 418 | |
Alexandru Gheorghiu | 8fe853f | 2013-03-13 21:46:08 +0200 | [diff] [blame] | 419 | rc = PTR_ERR(cmm_thread_ptr); |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 420 | unregister_oom_notifier(&cmm_oom_nb); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 421 | out_oom_notify: |
| 422 | #ifdef CONFIG_CMM_IUCV |
| 423 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 424 | out_smsg: |
| 425 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 426 | unregister_sysctl_table(cmm_sysctl_header); |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 427 | out_sysctl: |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 428 | del_timer_sync(&cmm_timer); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 429 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 431 | module_init(cmm_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Heiko Carstens | 2e85ba5 | 2010-08-09 18:12:55 +0200 | [diff] [blame] | 433 | static void __exit cmm_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | unregister_sysctl_table(cmm_sysctl_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | #ifdef CONFIG_CMM_IUCV |
| 437 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 438 | #endif |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 439 | unregister_oom_notifier(&cmm_oom_nb); |
| 440 | kthread_stop(cmm_thread_ptr); |
| 441 | del_timer_sync(&cmm_timer); |
| 442 | cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); |
| 443 | 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] | 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | module_exit(cmm_exit); |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | MODULE_LICENSE("GPL"); |