5 * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8 * Collaborative memory management interface.
11 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/sysctl.h>
17 #include <linux/ctype.h>
18 #include <linux/swap.h>
19 #include <linux/kthread.h>
21 #include <asm/pgalloc.h>
22 #include <asm/uaccess.h>
25 static char *sender = "VMRMSVM";
26 module_param(sender, charp, 0400);
27 MODULE_PARM_DESC(sender,
28 "Guest name that may send SMSG messages (default VMRMSVM)");
30 #include "../../../drivers/s390/net/smsgiucv.h"
32 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
34 struct cmm_page_array {
35 struct cmm_page_array *next;
37 unsigned long pages[CMM_NR_PAGES];
40 static long cmm_pages;
41 static long cmm_timed_pages;
42 static volatile long cmm_pages_target;
43 static volatile long cmm_timed_pages_target;
44 static long cmm_timeout_pages;
45 static long cmm_timeout_seconds;
47 static struct cmm_page_array *cmm_page_list;
48 static struct cmm_page_array *cmm_timed_page_list;
49 static DEFINE_SPINLOCK(cmm_lock);
51 static struct task_struct *cmm_thread_ptr;
52 static wait_queue_head_t cmm_thread_wait;
53 static struct timer_list cmm_timer;
55 static void cmm_timer_fn(unsigned long);
56 static void cmm_set_timer(void);
59 cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list)
61 struct cmm_page_array *pa, *npa;
65 addr = __get_free_page(GFP_NOIO);
70 if (!pa || pa->index >= CMM_NR_PAGES) {
71 /* Need a new page for the page list. */
72 spin_unlock(&cmm_lock);
73 npa = (struct cmm_page_array *)
74 __get_free_page(GFP_NOIO);
81 if (!pa || pa->index >= CMM_NR_PAGES) {
87 free_page((unsigned long) npa);
90 pa->pages[pa->index++] = addr;
92 spin_unlock(&cmm_lock);
99 cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
101 struct cmm_page_array *pa;
104 spin_lock(&cmm_lock);
107 if (!pa || pa->index <= 0)
109 addr = pa->pages[--pa->index];
110 if (pa->index == 0) {
112 free_page((unsigned long) *list);
119 spin_unlock(&cmm_lock);
123 static int cmm_oom_notify(struct notifier_block *self,
124 unsigned long dummy, void *parm)
126 unsigned long *freed = parm;
129 nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
131 nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
132 cmm_pages_target = cmm_pages;
133 cmm_timed_pages_target = cmm_timed_pages;
138 static struct notifier_block cmm_oom_nb = {
139 .notifier_call = cmm_oom_notify
143 cmm_thread(void *dummy)
148 rc = wait_event_interruptible(cmm_thread_wait,
149 (cmm_pages != cmm_pages_target ||
150 cmm_timed_pages != cmm_timed_pages_target ||
151 kthread_should_stop()));
152 if (kthread_should_stop() || rc == -ERESTARTSYS) {
153 cmm_pages_target = cmm_pages;
154 cmm_timed_pages_target = cmm_timed_pages;
157 if (cmm_pages_target > cmm_pages) {
158 if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
159 cmm_pages_target = cmm_pages;
160 } else if (cmm_pages_target < cmm_pages) {
161 cmm_free_pages(1, &cmm_pages, &cmm_page_list);
163 if (cmm_timed_pages_target > cmm_timed_pages) {
164 if (cmm_alloc_pages(1, &cmm_timed_pages,
165 &cmm_timed_page_list))
166 cmm_timed_pages_target = cmm_timed_pages;
167 } else if (cmm_timed_pages_target < cmm_timed_pages) {
168 cmm_free_pages(1, &cmm_timed_pages,
169 &cmm_timed_page_list);
171 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
178 cmm_kick_thread(void)
180 wake_up(&cmm_thread_wait);
186 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
187 if (timer_pending(&cmm_timer))
188 del_timer(&cmm_timer);
191 if (timer_pending(&cmm_timer)) {
192 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
195 cmm_timer.function = cmm_timer_fn;
197 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
198 add_timer(&cmm_timer);
202 cmm_timer_fn(unsigned long ignored)
206 nr = cmm_timed_pages_target - cmm_timeout_pages;
208 cmm_timed_pages_target = 0;
210 cmm_timed_pages_target = nr;
216 cmm_set_pages(long nr)
218 cmm_pages_target = nr;
229 cmm_add_timed_pages(long nr)
231 cmm_timed_pages_target += nr;
236 cmm_get_timed_pages(void)
238 return cmm_timed_pages;
242 cmm_set_timeout(long nr, long seconds)
244 cmm_timeout_pages = nr;
245 cmm_timeout_seconds = seconds;
250 cmm_skip_blanks(char *cp, char **endp)
254 for (str = cp; *str == ' ' || *str == '\t'; str++);
259 #ifdef CONFIG_CMM_PROC
261 static struct ctl_table cmm_table[];
264 cmm_pages_handler(ctl_table *ctl, int write, struct file *filp,
265 void __user *buffer, size_t *lenp, loff_t *ppos)
271 if (!*lenp || (*ppos && !write)) {
278 if (copy_from_user(buf, buffer,
279 len > sizeof(buf) ? sizeof(buf) : len))
281 buf[sizeof(buf) - 1] = '\0';
282 cmm_skip_blanks(buf, &p);
283 nr = simple_strtoul(p, &p, 0);
284 if (ctl == &cmm_table[0])
287 cmm_add_timed_pages(nr);
289 if (ctl == &cmm_table[0])
290 nr = cmm_get_pages();
292 nr = cmm_get_timed_pages();
293 len = sprintf(buf, "%ld\n", nr);
296 if (copy_to_user(buffer, buf, len))
305 cmm_timeout_handler(ctl_table *ctl, int write, struct file *filp,
306 void __user *buffer, size_t *lenp, loff_t *ppos)
312 if (!*lenp || (*ppos && !write)) {
319 if (copy_from_user(buf, buffer,
320 len > sizeof(buf) ? sizeof(buf) : len))
322 buf[sizeof(buf) - 1] = '\0';
323 cmm_skip_blanks(buf, &p);
324 nr = simple_strtoul(p, &p, 0);
325 cmm_skip_blanks(p, &p);
326 seconds = simple_strtoul(p, &p, 0);
327 cmm_set_timeout(nr, seconds);
329 len = sprintf(buf, "%ld %ld\n",
330 cmm_timeout_pages, cmm_timeout_seconds);
333 if (copy_to_user(buffer, buf, len))
341 static struct ctl_table cmm_table[] = {
343 .ctl_name = VM_CMM_PAGES,
344 .procname = "cmm_pages",
346 .proc_handler = &cmm_pages_handler,
349 .ctl_name = VM_CMM_TIMED_PAGES,
350 .procname = "cmm_timed_pages",
352 .proc_handler = &cmm_pages_handler,
355 .ctl_name = VM_CMM_TIMEOUT,
356 .procname = "cmm_timeout",
358 .proc_handler = &cmm_timeout_handler,
363 static struct ctl_table cmm_dir_table[] = {
375 #ifdef CONFIG_CMM_IUCV
376 #define SMSG_PREFIX "CMM"
378 cmm_smsg_target(char *from, char *msg)
382 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
384 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
386 if (strncmp(msg, "SHRINK", 6) == 0) {
387 if (!cmm_skip_blanks(msg + 6, &msg))
389 nr = simple_strtoul(msg, &msg, 0);
390 cmm_skip_blanks(msg, &msg);
393 } else if (strncmp(msg, "RELEASE", 7) == 0) {
394 if (!cmm_skip_blanks(msg + 7, &msg))
396 nr = simple_strtoul(msg, &msg, 0);
397 cmm_skip_blanks(msg, &msg);
399 cmm_add_timed_pages(nr);
400 } else if (strncmp(msg, "REUSE", 5) == 0) {
401 if (!cmm_skip_blanks(msg + 5, &msg))
403 nr = simple_strtoul(msg, &msg, 0);
404 if (!cmm_skip_blanks(msg, &msg))
406 seconds = simple_strtoul(msg, &msg, 0);
407 cmm_skip_blanks(msg, &msg);
409 cmm_set_timeout(nr, seconds);
414 static struct ctl_table_header *cmm_sysctl_header;
421 #ifdef CONFIG_CMM_PROC
422 cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
423 if (!cmm_sysctl_header)
426 #ifdef CONFIG_CMM_IUCV
427 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
431 rc = register_oom_notifier(&cmm_oom_nb);
434 init_waitqueue_head(&cmm_thread_wait);
435 init_timer(&cmm_timer);
436 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
437 rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0;
441 * kthread_create failed. undo all the stuff from above again.
443 unregister_oom_notifier(&cmm_oom_nb);
446 #ifdef CONFIG_CMM_IUCV
447 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
450 #ifdef CONFIG_CMM_PROC
451 unregister_sysctl_table(cmm_sysctl_header);
460 kthread_stop(cmm_thread_ptr);
461 unregister_oom_notifier(&cmm_oom_nb);
462 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
463 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
464 #ifdef CONFIG_CMM_PROC
465 unregister_sysctl_table(cmm_sysctl_header);
467 #ifdef CONFIG_CMM_IUCV
468 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
472 module_init(cmm_init);
473 module_exit(cmm_exit);
475 EXPORT_SYMBOL(cmm_set_pages);
476 EXPORT_SYMBOL(cmm_get_pages);
477 EXPORT_SYMBOL(cmm_add_timed_pages);
478 EXPORT_SYMBOL(cmm_get_timed_pages);
479 EXPORT_SYMBOL(cmm_set_timeout);
481 MODULE_LICENSE("GPL");