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 | |
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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 51 | static struct cmm_page_array *cmm_page_list; |
| 52 | static struct cmm_page_array *cmm_timed_page_list; |
| 53 | static DEFINE_SPINLOCK(cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 55 | static struct task_struct *cmm_thread_ptr; |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 56 | static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Kees Cook | 6cc73a0 | 2017-10-23 22:41:17 -0700 | [diff] [blame] | 58 | static void cmm_timer_fn(struct timer_list *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | static void cmm_set_timer(void); |
Kees Cook | 6cc73a0 | 2017-10-23 22:41:17 -0700 | [diff] [blame] | 60 | static DEFINE_TIMER(cmm_timer, cmm_timer_fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 62 | static long cmm_alloc_pages(long nr, long *counter, |
| 63 | struct cmm_page_array **list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 65 | struct cmm_page_array *pa, *npa; |
| 66 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 68 | while (nr) { |
| 69 | addr = __get_free_page(GFP_NOIO); |
| 70 | if (!addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 72 | spin_lock(&cmm_lock); |
| 73 | pa = *list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 75 | /* Need a new page for the page list. */ |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 76 | spin_unlock(&cmm_lock); |
| 77 | npa = (struct cmm_page_array *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | __get_free_page(GFP_NOIO); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 79 | if (!npa) { |
| 80 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | break; |
| 82 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 83 | spin_lock(&cmm_lock); |
| 84 | pa = *list; |
| 85 | if (!pa || pa->index >= CMM_NR_PAGES) { |
| 86 | npa->next = pa; |
| 87 | npa->index = 0; |
| 88 | pa = npa; |
| 89 | *list = pa; |
| 90 | } else |
| 91 | free_page((unsigned long) npa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
Michael Holzheu | 83ace270 | 2011-05-10 17:13:41 +0200 | [diff] [blame] | 93 | diag10_range(addr >> PAGE_SHIFT, 1); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 94 | pa->pages[pa->index++] = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | (*counter)++; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 96 | spin_unlock(&cmm_lock); |
| 97 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 99 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 102 | 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] | 103 | { |
| 104 | struct cmm_page_array *pa; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 105 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 107 | spin_lock(&cmm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | pa = *list; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 109 | while (nr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (!pa || pa->index <= 0) |
| 111 | break; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 112 | addr = pa->pages[--pa->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if (pa->index == 0) { |
| 114 | pa = pa->next; |
| 115 | free_page((unsigned long) *list); |
| 116 | *list = pa; |
| 117 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 118 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | (*counter)--; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 120 | nr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 122 | spin_unlock(&cmm_lock); |
| 123 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 126 | static int cmm_oom_notify(struct notifier_block *self, |
| 127 | unsigned long dummy, void *parm) |
| 128 | { |
| 129 | unsigned long *freed = parm; |
| 130 | long nr = 256; |
| 131 | |
| 132 | nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list); |
| 133 | if (nr > 0) |
| 134 | nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list); |
| 135 | cmm_pages_target = cmm_pages; |
| 136 | cmm_timed_pages_target = cmm_timed_pages; |
| 137 | *freed += 256 - nr; |
| 138 | return NOTIFY_OK; |
| 139 | } |
| 140 | |
| 141 | static struct notifier_block cmm_oom_nb = { |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 142 | .notifier_call = cmm_oom_notify, |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 145 | static int cmm_thread(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | int rc; |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | while (1) { |
| 150 | rc = wait_event_interruptible(cmm_thread_wait, |
Heiko Carstens | 3942162 | 2020-03-18 20:55:24 +0100 | [diff] [blame] | 151 | cmm_pages != cmm_pages_target || |
| 152 | cmm_timed_pages != cmm_timed_pages_target || |
| 153 | kthread_should_stop()); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 154 | if (kthread_should_stop() || rc == -ERESTARTSYS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | cmm_pages_target = cmm_pages; |
| 156 | cmm_timed_pages_target = cmm_timed_pages; |
| 157 | break; |
| 158 | } |
| 159 | if (cmm_pages_target > cmm_pages) { |
| 160 | if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list)) |
| 161 | cmm_pages_target = cmm_pages; |
| 162 | } else if (cmm_pages_target < cmm_pages) { |
| 163 | cmm_free_pages(1, &cmm_pages, &cmm_page_list); |
| 164 | } |
| 165 | if (cmm_timed_pages_target > cmm_timed_pages) { |
| 166 | if (cmm_alloc_pages(1, &cmm_timed_pages, |
| 167 | &cmm_timed_page_list)) |
| 168 | cmm_timed_pages_target = cmm_timed_pages; |
| 169 | } else if (cmm_timed_pages_target < cmm_timed_pages) { |
| 170 | cmm_free_pages(1, &cmm_timed_pages, |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 171 | &cmm_timed_page_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer)) |
| 174 | cmm_set_timer(); |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 179 | static void cmm_kick_thread(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | wake_up(&cmm_thread_wait); |
| 182 | } |
| 183 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 184 | static void cmm_set_timer(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Sven Schnelle | 0188d08 | 2020-06-12 13:06:07 +0200 | [diff] [blame] | 191 | mod_timer(&cmm_timer, jiffies + msecs_to_jiffies(cmm_timeout_seconds * MSEC_PER_SEC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Kees Cook | 6cc73a0 | 2017-10-23 22:41:17 -0700 | [diff] [blame] | 194 | static void cmm_timer_fn(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 196 | long nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 198 | nr = cmm_timed_pages_target - cmm_timeout_pages; |
| 199 | if (nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | cmm_timed_pages_target = 0; |
| 201 | else |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 202 | cmm_timed_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | cmm_kick_thread(); |
| 204 | cmm_set_timer(); |
| 205 | } |
| 206 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 207 | static void cmm_set_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 209 | cmm_pages_target = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | cmm_kick_thread(); |
| 211 | } |
| 212 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 213 | static long cmm_get_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
| 215 | return cmm_pages; |
| 216 | } |
| 217 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 218 | static void cmm_add_timed_pages(long nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 220 | cmm_timed_pages_target += nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | cmm_kick_thread(); |
| 222 | } |
| 223 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 224 | static long cmm_get_timed_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | return cmm_timed_pages; |
| 227 | } |
| 228 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 229 | static void cmm_set_timeout(long nr, long seconds) |
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_timeout_pages = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | cmm_timeout_seconds = seconds; |
| 233 | cmm_set_timer(); |
| 234 | } |
| 235 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 236 | static int cmm_skip_blanks(char *cp, char **endp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | char *str; |
| 239 | |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 240 | for (str = cp; *str == ' ' || *str == '\t'; str++) |
| 241 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | *endp = str; |
| 243 | return str != cp; |
| 244 | } |
| 245 | |
Joe Perches | 302bfe2 | 2013-10-22 15:29:46 -0700 | [diff] [blame] | 246 | static int cmm_pages_handler(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 247 | void *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 249 | long nr = cmm_get_pages(); |
| 250 | struct ctl_table ctl_entry = { |
| 251 | .procname = ctl->procname, |
| 252 | .data = &nr, |
| 253 | .maxlen = sizeof(long), |
| 254 | }; |
| 255 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 257 | rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos); |
| 258 | if (rc < 0 || !write) |
| 259 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 261 | cmm_set_pages(nr); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int cmm_timed_pages_handler(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 266 | void *buffer, size_t *lenp, |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 267 | loff_t *ppos) |
| 268 | { |
| 269 | long nr = cmm_get_timed_pages(); |
| 270 | struct ctl_table ctl_entry = { |
| 271 | .procname = ctl->procname, |
| 272 | .data = &nr, |
| 273 | .maxlen = sizeof(long), |
| 274 | }; |
| 275 | int rc; |
| 276 | |
| 277 | rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos); |
| 278 | if (rc < 0 || !write) |
| 279 | return rc; |
| 280 | |
| 281 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
Joe Perches | 302bfe2 | 2013-10-22 15:29:46 -0700 | [diff] [blame] | 285 | static int cmm_timeout_handler(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 286 | void *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
| 288 | char buf[64], *p; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 289 | long nr, seconds; |
Heiko Carstens | 041058a | 2013-10-14 13:58:10 +0200 | [diff] [blame] | 290 | unsigned int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | if (!*lenp || (*ppos && !write)) { |
| 293 | *lenp = 0; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | if (write) { |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 298 | len = min(*lenp, sizeof(buf)); |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 299 | memcpy(buf, buffer, len); |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 300 | buf[len - 1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | cmm_skip_blanks(buf, &p); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 302 | nr = simple_strtoul(p, &p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | cmm_skip_blanks(p, &p); |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 304 | seconds = simple_strtoul(p, &p, 0); |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 305 | cmm_set_timeout(nr, seconds); |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 306 | *ppos += *lenp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } else { |
| 308 | len = sprintf(buf, "%ld %ld\n", |
| 309 | cmm_timeout_pages, cmm_timeout_seconds); |
| 310 | if (len > *lenp) |
| 311 | len = *lenp; |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 312 | memcpy(buffer, buf, len); |
Yihui ZENG | b8e51a6 | 2019-10-25 12:31:48 +0300 | [diff] [blame] | 313 | *lenp = len; |
| 314 | *ppos += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static struct ctl_table cmm_table[] = { |
| 320 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | .procname = "cmm_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 322 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 323 | .proc_handler = cmm_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | }, |
| 325 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | .procname = "cmm_timed_pages", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 327 | .mode = 0644, |
Vasily Gorbik | 71e33a1 | 2018-07-01 15:56:28 +0200 | [diff] [blame] | 328 | .proc_handler = cmm_timed_pages_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | }, |
| 330 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | .procname = "cmm_timeout", |
Martin Schwidefsky | 5e8b1c4 | 2006-03-24 03:15:16 -0800 | [diff] [blame] | 332 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 333 | .proc_handler = cmm_timeout_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | }, |
Eric W. Biederman | b05fd35 | 2009-04-03 04:36:34 -0700 | [diff] [blame] | 335 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | static struct ctl_table cmm_dir_table[] = { |
| 339 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | .procname = "vm", |
| 341 | .maxlen = 0, |
| 342 | .mode = 0555, |
| 343 | .child = cmm_table, |
| 344 | }, |
Eric W. Biederman | b05fd35 | 2009-04-03 04:36:34 -0700 | [diff] [blame] | 345 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | #ifdef CONFIG_CMM_IUCV |
| 349 | #define SMSG_PREFIX "CMM" |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 350 | static void cmm_smsg_target(const char *from, char *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 352 | long nr, seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 354 | if (strlen(sender) > 0 && strcmp(from, sender) != 0) |
| 355 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg)) |
| 357 | return; |
| 358 | if (strncmp(msg, "SHRINK", 6) == 0) { |
| 359 | if (!cmm_skip_blanks(msg + 6, &msg)) |
| 360 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 361 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | cmm_skip_blanks(msg, &msg); |
| 363 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 364 | cmm_set_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } else if (strncmp(msg, "RELEASE", 7) == 0) { |
| 366 | if (!cmm_skip_blanks(msg + 7, &msg)) |
| 367 | return; |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 368 | nr = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | cmm_skip_blanks(msg, &msg); |
| 370 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 371 | cmm_add_timed_pages(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } else if (strncmp(msg, "REUSE", 5) == 0) { |
| 373 | if (!cmm_skip_blanks(msg + 5, &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 | if (!cmm_skip_blanks(msg, &msg)) |
| 377 | return; |
Heiko Carstens | 7d5d688 | 2006-09-20 15:59:00 +0200 | [diff] [blame] | 378 | seconds = simple_strtoul(msg, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | cmm_skip_blanks(msg, &msg); |
| 380 | if (*msg == '\0') |
Martin Schwidefsky | 8bc719d | 2006-09-25 23:31:20 -0700 | [diff] [blame] | 381 | cmm_set_timeout(nr, seconds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | #endif |
| 385 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 386 | static struct ctl_table_header *cmm_sysctl_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Heiko Carstens | 2e85ba5 | 2010-08-09 18:12:55 +0200 | [diff] [blame] | 388 | static int __init cmm_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 390 | int rc = -ENOMEM; |
| 391 | |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 392 | cmm_sysctl_header = register_sysctl_table(cmm_dir_table); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 393 | if (!cmm_sysctl_header) |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 394 | goto out_sysctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | #ifdef CONFIG_CMM_IUCV |
Hendrik Brueckner | 41b4287 | 2010-08-09 18:12:56 +0200 | [diff] [blame] | 396 | /* convert sender to uppercase characters */ |
| 397 | if (sender) { |
| 398 | int len = strlen(sender); |
| 399 | while (len--) |
| 400 | sender[len] = toupper(sender[len]); |
Heiko Carstens | a20852d | 2010-10-25 16:10:21 +0200 | [diff] [blame] | 401 | } else { |
| 402 | sender = cmm_default_sender; |
Hendrik Brueckner | 41b4287 | 2010-08-09 18:12:56 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 405 | rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); |
| 406 | if (rc < 0) |
| 407 | goto out_smsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 409 | rc = register_oom_notifier(&cmm_oom_nb); |
| 410 | if (rc < 0) |
| 411 | goto out_oom_notify; |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 412 | cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); |
Alexandru Gheorghiu | 8fe853f | 2013-03-13 21:46:08 +0200 | [diff] [blame] | 413 | if (!IS_ERR(cmm_thread_ptr)) |
| 414 | return 0; |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 415 | |
Alexandru Gheorghiu | 8fe853f | 2013-03-13 21:46:08 +0200 | [diff] [blame] | 416 | rc = PTR_ERR(cmm_thread_ptr); |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 417 | unregister_oom_notifier(&cmm_oom_nb); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 418 | out_oom_notify: |
| 419 | #ifdef CONFIG_CMM_IUCV |
| 420 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 421 | out_smsg: |
| 422 | #endif |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 423 | unregister_sysctl_table(cmm_sysctl_header); |
Martin Schwidefsky | 52b169c | 2009-12-07 12:52:06 +0100 | [diff] [blame] | 424 | out_sysctl: |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 425 | del_timer_sync(&cmm_timer); |
Heiko Carstens | e8216de | 2006-09-25 23:33:11 -0700 | [diff] [blame] | 426 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
Heiko Carstens | db705e8 | 2010-05-26 23:27:10 +0200 | [diff] [blame] | 428 | module_init(cmm_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Heiko Carstens | 2e85ba5 | 2010-08-09 18:12:55 +0200 | [diff] [blame] | 430 | static void __exit cmm_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | unregister_sysctl_table(cmm_sysctl_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | #ifdef CONFIG_CMM_IUCV |
| 434 | smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); |
| 435 | #endif |
Heiko Carstens | 1ef6acf | 2010-05-26 23:26:17 +0200 | [diff] [blame] | 436 | unregister_oom_notifier(&cmm_oom_nb); |
| 437 | kthread_stop(cmm_thread_ptr); |
| 438 | del_timer_sync(&cmm_timer); |
| 439 | cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); |
| 440 | 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] | 441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | module_exit(cmm_exit); |
| 443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | MODULE_LICENSE("GPL"); |