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 *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 *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 *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 memcpy(buf, buffer, len); 301 buf[len - 1] = '\0'; 302 cmm_skip_blanks(buf, &p); 303 nr = simple_strtoul(p, &p, 0); 304 cmm_skip_blanks(p, &p); 305 seconds = simple_strtoul(p, &p, 0); 306 cmm_set_timeout(nr, seconds); 307 *ppos += *lenp; 308 } else { 309 len = sprintf(buf, "%ld %ld\n", 310 cmm_timeout_pages, cmm_timeout_seconds); 311 if (len > *lenp) 312 len = *lenp; 313 memcpy(buffer, buf, len); 314 *lenp = len; 315 *ppos += len; 316 } 317 return 0; 318 } 319 320 static struct ctl_table cmm_table[] = { 321 { 322 .procname = "cmm_pages", 323 .mode = 0644, 324 .proc_handler = cmm_pages_handler, 325 }, 326 { 327 .procname = "cmm_timed_pages", 328 .mode = 0644, 329 .proc_handler = cmm_timed_pages_handler, 330 }, 331 { 332 .procname = "cmm_timeout", 333 .mode = 0644, 334 .proc_handler = cmm_timeout_handler, 335 }, 336 { } 337 }; 338 339 static struct ctl_table cmm_dir_table[] = { 340 { 341 .procname = "vm", 342 .maxlen = 0, 343 .mode = 0555, 344 .child = cmm_table, 345 }, 346 { } 347 }; 348 349 #ifdef CONFIG_CMM_IUCV 350 #define SMSG_PREFIX "CMM" 351 static void cmm_smsg_target(const char *from, char *msg) 352 { 353 long nr, seconds; 354 355 if (strlen(sender) > 0 && strcmp(from, sender) != 0) 356 return; 357 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg)) 358 return; 359 if (strncmp(msg, "SHRINK", 6) == 0) { 360 if (!cmm_skip_blanks(msg + 6, &msg)) 361 return; 362 nr = simple_strtoul(msg, &msg, 0); 363 cmm_skip_blanks(msg, &msg); 364 if (*msg == '\0') 365 cmm_set_pages(nr); 366 } else if (strncmp(msg, "RELEASE", 7) == 0) { 367 if (!cmm_skip_blanks(msg + 7, &msg)) 368 return; 369 nr = simple_strtoul(msg, &msg, 0); 370 cmm_skip_blanks(msg, &msg); 371 if (*msg == '\0') 372 cmm_add_timed_pages(nr); 373 } else if (strncmp(msg, "REUSE", 5) == 0) { 374 if (!cmm_skip_blanks(msg + 5, &msg)) 375 return; 376 nr = simple_strtoul(msg, &msg, 0); 377 if (!cmm_skip_blanks(msg, &msg)) 378 return; 379 seconds = simple_strtoul(msg, &msg, 0); 380 cmm_skip_blanks(msg, &msg); 381 if (*msg == '\0') 382 cmm_set_timeout(nr, seconds); 383 } 384 } 385 #endif 386 387 static struct ctl_table_header *cmm_sysctl_header; 388 389 static int __init cmm_init(void) 390 { 391 int rc = -ENOMEM; 392 393 cmm_sysctl_header = register_sysctl_table(cmm_dir_table); 394 if (!cmm_sysctl_header) 395 goto out_sysctl; 396 #ifdef CONFIG_CMM_IUCV 397 /* convert sender to uppercase characters */ 398 if (sender) { 399 int len = strlen(sender); 400 while (len--) 401 sender[len] = toupper(sender[len]); 402 } else { 403 sender = cmm_default_sender; 404 } 405 406 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); 407 if (rc < 0) 408 goto out_smsg; 409 #endif 410 rc = register_oom_notifier(&cmm_oom_nb); 411 if (rc < 0) 412 goto out_oom_notify; 413 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); 414 if (!IS_ERR(cmm_thread_ptr)) 415 return 0; 416 417 rc = PTR_ERR(cmm_thread_ptr); 418 unregister_oom_notifier(&cmm_oom_nb); 419 out_oom_notify: 420 #ifdef CONFIG_CMM_IUCV 421 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); 422 out_smsg: 423 #endif 424 unregister_sysctl_table(cmm_sysctl_header); 425 out_sysctl: 426 del_timer_sync(&cmm_timer); 427 return rc; 428 } 429 module_init(cmm_init); 430 431 static void __exit cmm_exit(void) 432 { 433 unregister_sysctl_table(cmm_sysctl_header); 434 #ifdef CONFIG_CMM_IUCV 435 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); 436 #endif 437 unregister_oom_notifier(&cmm_oom_nb); 438 kthread_stop(cmm_thread_ptr); 439 del_timer_sync(&cmm_timer); 440 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); 441 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list); 442 } 443 module_exit(cmm_exit); 444 445 MODULE_LICENSE("GPL"); 446