1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Collaborative memory management interface. 4 * 5 * Copyright IBM Corp 2003, 2010 6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, 7 * 8 */ 9 10 #include <linux/errno.h> 11 #include <linux/fs.h> 12 #include <linux/init.h> 13 #include <linux/module.h> 14 #include <linux/moduleparam.h> 15 #include <linux/gfp.h> 16 #include <linux/sched.h> 17 #include <linux/sysctl.h> 18 #include <linux/ctype.h> 19 #include <linux/swap.h> 20 #include <linux/kthread.h> 21 #include <linux/oom.h> 22 #include <linux/uaccess.h> 23 24 #include <asm/pgalloc.h> 25 #include <asm/diag.h> 26 27 #ifdef CONFIG_CMM_IUCV 28 static char *cmm_default_sender = "VMRMSVM"; 29 #endif 30 static char *sender; 31 module_param(sender, charp, 0400); 32 MODULE_PARM_DESC(sender, 33 "Guest name that may send SMSG messages (default VMRMSVM)"); 34 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 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; 51 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); 55 56 static struct task_struct *cmm_thread_ptr; 57 static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait); 58 59 static void cmm_timer_fn(struct timer_list *); 60 static void cmm_set_timer(void); 61 static DEFINE_TIMER(cmm_timer, cmm_timer_fn); 62 63 static long cmm_alloc_pages(long nr, long *counter, 64 struct cmm_page_array **list) 65 { 66 struct cmm_page_array *pa, *npa; 67 unsigned long addr; 68 69 while (nr) { 70 addr = __get_free_page(GFP_NOIO); 71 if (!addr) 72 break; 73 spin_lock(&cmm_lock); 74 pa = *list; 75 if (!pa || pa->index >= CMM_NR_PAGES) { 76 /* Need a new page for the page list. */ 77 spin_unlock(&cmm_lock); 78 npa = (struct cmm_page_array *) 79 __get_free_page(GFP_NOIO); 80 if (!npa) { 81 free_page(addr); 82 break; 83 } 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); 93 } 94 diag10_range(addr >> PAGE_SHIFT, 1); 95 pa->pages[pa->index++] = addr; 96 (*counter)++; 97 spin_unlock(&cmm_lock); 98 nr--; 99 } 100 return nr; 101 } 102 103 static long cmm_free_pages(long nr, long *counter, struct cmm_page_array **list) 104 { 105 struct cmm_page_array *pa; 106 unsigned long addr; 107 108 spin_lock(&cmm_lock); 109 pa = *list; 110 while (nr) { 111 if (!pa || pa->index <= 0) 112 break; 113 addr = pa->pages[--pa->index]; 114 if (pa->index == 0) { 115 pa = pa->next; 116 free_page((unsigned long) *list); 117 *list = pa; 118 } 119 free_page(addr); 120 (*counter)--; 121 nr--; 122 } 123 spin_unlock(&cmm_lock); 124 return nr; 125 } 126 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 = { 143 .notifier_call = cmm_oom_notify, 144 }; 145 146 static int cmm_thread(void *dummy) 147 { 148 int rc; 149 150 while (1) { 151 rc = wait_event_interruptible(cmm_thread_wait, 152 cmm_pages != cmm_pages_target || 153 cmm_timed_pages != cmm_timed_pages_target || 154 kthread_should_stop()); 155 if (kthread_should_stop() || rc == -ERESTARTSYS) { 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, 172 &cmm_timed_page_list); 173 } 174 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer)) 175 cmm_set_timer(); 176 } 177 return 0; 178 } 179 180 static void cmm_kick_thread(void) 181 { 182 wake_up(&cmm_thread_wait); 183 } 184 185 static void cmm_set_timer(void) 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 mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds * HZ); 193 } 194 195 static void cmm_timer_fn(struct timer_list *unused) 196 { 197 long nr; 198 199 nr = cmm_timed_pages_target - cmm_timeout_pages; 200 if (nr < 0) 201 cmm_timed_pages_target = 0; 202 else 203 cmm_timed_pages_target = nr; 204 cmm_kick_thread(); 205 cmm_set_timer(); 206 } 207 208 static void cmm_set_pages(long nr) 209 { 210 cmm_pages_target = nr; 211 cmm_kick_thread(); 212 } 213 214 static long cmm_get_pages(void) 215 { 216 return cmm_pages; 217 } 218 219 static void cmm_add_timed_pages(long nr) 220 { 221 cmm_timed_pages_target += nr; 222 cmm_kick_thread(); 223 } 224 225 static long cmm_get_timed_pages(void) 226 { 227 return cmm_timed_pages; 228 } 229 230 static void cmm_set_timeout(long nr, long seconds) 231 { 232 cmm_timeout_pages = nr; 233 cmm_timeout_seconds = seconds; 234 cmm_set_timer(); 235 } 236 237 static int cmm_skip_blanks(char *cp, char **endp) 238 { 239 char *str; 240 241 for (str = cp; *str == ' ' || *str == '\t'; str++) 242 ; 243 *endp = str; 244 return str != cp; 245 } 246 247 static int cmm_pages_handler(struct ctl_table *ctl, int write, 248 void __user *buffer, size_t *lenp, loff_t *ppos) 249 { 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; 257 258 rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos); 259 if (rc < 0 || !write) 260 return rc; 261 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); 283 return 0; 284 } 285 286 static int cmm_timeout_handler(struct ctl_table *ctl, int write, 287 void __user *buffer, size_t *lenp, loff_t *ppos) 288 { 289 char buf[64], *p; 290 long nr, seconds; 291 unsigned int len; 292 293 if (!*lenp || (*ppos && !write)) { 294 *lenp = 0; 295 return 0; 296 } 297 298 if (write) { 299 len = min(*lenp, sizeof(buf)); 300 if (copy_from_user(buf, buffer, len)) 301 return -EFAULT; 302 buf[len - 1] = '\0'; 303 cmm_skip_blanks(buf, &p); 304 nr = simple_strtoul(p, &p, 0); 305 cmm_skip_blanks(p, &p); 306 seconds = simple_strtoul(p, &p, 0); 307 cmm_set_timeout(nr, seconds); 308 *ppos += *lenp; 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; 316 *lenp = len; 317 *ppos += len; 318 } 319 return 0; 320 } 321 322 static struct ctl_table cmm_table[] = { 323 { 324 .procname = "cmm_pages", 325 .mode = 0644, 326 .proc_handler = cmm_pages_handler, 327 }, 328 { 329 .procname = "cmm_timed_pages", 330 .mode = 0644, 331 .proc_handler = cmm_timed_pages_handler, 332 }, 333 { 334 .procname = "cmm_timeout", 335 .mode = 0644, 336 .proc_handler = cmm_timeout_handler, 337 }, 338 { } 339 }; 340 341 static struct ctl_table cmm_dir_table[] = { 342 { 343 .procname = "vm", 344 .maxlen = 0, 345 .mode = 0555, 346 .child = cmm_table, 347 }, 348 { } 349 }; 350 351 #ifdef CONFIG_CMM_IUCV 352 #define SMSG_PREFIX "CMM" 353 static void cmm_smsg_target(const char *from, char *msg) 354 { 355 long nr, seconds; 356 357 if (strlen(sender) > 0 && strcmp(from, sender) != 0) 358 return; 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; 364 nr = simple_strtoul(msg, &msg, 0); 365 cmm_skip_blanks(msg, &msg); 366 if (*msg == '\0') 367 cmm_set_pages(nr); 368 } else if (strncmp(msg, "RELEASE", 7) == 0) { 369 if (!cmm_skip_blanks(msg + 7, &msg)) 370 return; 371 nr = simple_strtoul(msg, &msg, 0); 372 cmm_skip_blanks(msg, &msg); 373 if (*msg == '\0') 374 cmm_add_timed_pages(nr); 375 } else if (strncmp(msg, "REUSE", 5) == 0) { 376 if (!cmm_skip_blanks(msg + 5, &msg)) 377 return; 378 nr = simple_strtoul(msg, &msg, 0); 379 if (!cmm_skip_blanks(msg, &msg)) 380 return; 381 seconds = simple_strtoul(msg, &msg, 0); 382 cmm_skip_blanks(msg, &msg); 383 if (*msg == '\0') 384 cmm_set_timeout(nr, seconds); 385 } 386 } 387 #endif 388 389 static struct ctl_table_header *cmm_sysctl_header; 390 391 static int __init cmm_init(void) 392 { 393 int rc = -ENOMEM; 394 395 cmm_sysctl_header = register_sysctl_table(cmm_dir_table); 396 if (!cmm_sysctl_header) 397 goto out_sysctl; 398 #ifdef CONFIG_CMM_IUCV 399 /* convert sender to uppercase characters */ 400 if (sender) { 401 int len = strlen(sender); 402 while (len--) 403 sender[len] = toupper(sender[len]); 404 } else { 405 sender = cmm_default_sender; 406 } 407 408 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); 409 if (rc < 0) 410 goto out_smsg; 411 #endif 412 rc = register_oom_notifier(&cmm_oom_nb); 413 if (rc < 0) 414 goto out_oom_notify; 415 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); 416 if (!IS_ERR(cmm_thread_ptr)) 417 return 0; 418 419 rc = PTR_ERR(cmm_thread_ptr); 420 unregister_oom_notifier(&cmm_oom_nb); 421 out_oom_notify: 422 #ifdef CONFIG_CMM_IUCV 423 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); 424 out_smsg: 425 #endif 426 unregister_sysctl_table(cmm_sysctl_header); 427 out_sysctl: 428 del_timer_sync(&cmm_timer); 429 return rc; 430 } 431 module_init(cmm_init); 432 433 static void __exit cmm_exit(void) 434 { 435 unregister_sysctl_table(cmm_sysctl_header); 436 #ifdef CONFIG_CMM_IUCV 437 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); 438 #endif 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); 444 } 445 module_exit(cmm_exit); 446 447 MODULE_LICENSE("GPL"); 448