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>
24 static char *sender = "VMRMSVM";
25 module_param(sender, charp, 0400);
26 MODULE_PARM_DESC(sender,
27 "Guest name that may send SMSG messages (default VMRMSVM)");
29 #include "../../../drivers/s390/net/smsgiucv.h"
31 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
33 struct cmm_page_array {
34 struct cmm_page_array *next;
36 unsigned long pages[CMM_NR_PAGES];
39 static long cmm_pages;
40 static long cmm_timed_pages;
41 static volatile long cmm_pages_target;
42 static volatile long cmm_timed_pages_target;
43 static long cmm_timeout_pages;
44 static long cmm_timeout_seconds;
46 static struct cmm_page_array *cmm_page_list;
47 static struct cmm_page_array *cmm_timed_page_list;
48 static DEFINE_SPINLOCK(cmm_lock);
50 static struct task_struct *cmm_thread_ptr;
51 static wait_queue_head_t cmm_thread_wait;
52 static struct timer_list cmm_timer;
54 static void cmm_timer_fn(unsigned long);
55 static void cmm_set_timer(void);
58 cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list)
60 struct cmm_page_array *pa, *npa;
64 addr = __get_free_page(GFP_NOIO);
69 if (!pa || pa->index >= CMM_NR_PAGES) {
70 /* Need a new page for the page list. */
71 spin_unlock(&cmm_lock);
72 npa = (struct cmm_page_array *)
73 __get_free_page(GFP_NOIO);
80 if (!pa || pa->index >= CMM_NR_PAGES) {
86 free_page((unsigned long) npa);
89 pa->pages[pa->index++] = addr;
91 spin_unlock(&cmm_lock);
98 cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
100 struct cmm_page_array *pa;
103 spin_lock(&cmm_lock);
106 if (!pa || pa->index <= 0)
108 addr = pa->pages[--pa->index];
109 if (pa->index == 0) {
111 free_page((unsigned long) *list);
118 spin_unlock(&cmm_lock);
122 static int cmm_oom_notify(struct notifier_block *self,
123 unsigned long dummy, void *parm)
125 unsigned long *freed = parm;
128 nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
130 nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
131 cmm_pages_target = cmm_pages;
132 cmm_timed_pages_target = cmm_timed_pages;
137 static struct notifier_block cmm_oom_nb = {
138 .notifier_call = cmm_oom_notify
142 cmm_thread(void *dummy)
147 rc = wait_event_interruptible(cmm_thread_wait,
148 (cmm_pages != cmm_pages_target ||
149 cmm_timed_pages != cmm_timed_pages_target ||
150 kthread_should_stop()));
151 if (kthread_should_stop() || rc == -ERESTARTSYS) {
152 cmm_pages_target = cmm_pages;
153 cmm_timed_pages_target = cmm_timed_pages;
156 if (cmm_pages_target > cmm_pages) {
157 if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
158 cmm_pages_target = cmm_pages;
159 } else if (cmm_pages_target < cmm_pages) {
160 cmm_free_pages(1, &cmm_pages, &cmm_page_list);
162 if (cmm_timed_pages_target > cmm_timed_pages) {
163 if (cmm_alloc_pages(1, &cmm_timed_pages,
164 &cmm_timed_page_list))
165 cmm_timed_pages_target = cmm_timed_pages;
166 } else if (cmm_timed_pages_target < cmm_timed_pages) {
167 cmm_free_pages(1, &cmm_timed_pages,
168 &cmm_timed_page_list);
170 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
177 cmm_kick_thread(void)
179 wake_up(&cmm_thread_wait);
185 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
186 if (timer_pending(&cmm_timer))
187 del_timer(&cmm_timer);
190 if (timer_pending(&cmm_timer)) {
191 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
194 cmm_timer.function = cmm_timer_fn;
196 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
197 add_timer(&cmm_timer);
201 cmm_timer_fn(unsigned long ignored)
205 nr = cmm_timed_pages_target - cmm_timeout_pages;
207 cmm_timed_pages_target = 0;
209 cmm_timed_pages_target = nr;
215 cmm_set_pages(long nr)
217 cmm_pages_target = nr;
228 cmm_add_timed_pages(long nr)
230 cmm_timed_pages_target += nr;
235 cmm_get_timed_pages(void)
237 return cmm_timed_pages;
241 cmm_set_timeout(long nr, long seconds)
243 cmm_timeout_pages = nr;
244 cmm_timeout_seconds = seconds;
249 cmm_skip_blanks(char *cp, char **endp)
253 for (str = cp; *str == ' ' || *str == '\t'; str++);
258 #ifdef CONFIG_CMM_PROC
260 static struct ctl_table cmm_table[];
263 cmm_pages_handler(ctl_table *ctl, int write, struct file *filp,
264 void __user *buffer, size_t *lenp, loff_t *ppos)
270 if (!*lenp || (*ppos && !write)) {
277 if (copy_from_user(buf, buffer,
278 len > sizeof(buf) ? sizeof(buf) : len))
280 buf[sizeof(buf) - 1] = '\0';
281 cmm_skip_blanks(buf, &p);
282 nr = simple_strtoul(p, &p, 0);
283 if (ctl == &cmm_table[0])
286 cmm_add_timed_pages(nr);
288 if (ctl == &cmm_table[0])
289 nr = cmm_get_pages();
291 nr = cmm_get_timed_pages();
292 len = sprintf(buf, "%ld\n", nr);
295 if (copy_to_user(buffer, buf, len))
304 cmm_timeout_handler(ctl_table *ctl, int write, struct file *filp,
305 void __user *buffer, size_t *lenp, loff_t *ppos)
311 if (!*lenp || (*ppos && !write)) {
318 if (copy_from_user(buf, buffer,
319 len > sizeof(buf) ? sizeof(buf) : len))
321 buf[sizeof(buf) - 1] = '\0';
322 cmm_skip_blanks(buf, &p);
323 nr = simple_strtoul(p, &p, 0);
324 cmm_skip_blanks(p, &p);
325 seconds = simple_strtoul(p, &p, 0);
326 cmm_set_timeout(nr, seconds);
328 len = sprintf(buf, "%ld %ld\n",
329 cmm_timeout_pages, cmm_timeout_seconds);
332 if (copy_to_user(buffer, buf, len))
340 static struct ctl_table cmm_table[] = {
342 .ctl_name = VM_CMM_PAGES,
343 .procname = "cmm_pages",
345 .proc_handler = &cmm_pages_handler,
348 .ctl_name = VM_CMM_TIMED_PAGES,
349 .procname = "cmm_timed_pages",
351 .proc_handler = &cmm_pages_handler,
354 .ctl_name = VM_CMM_TIMEOUT,
355 .procname = "cmm_timeout",
357 .proc_handler = &cmm_timeout_handler,
362 static struct ctl_table cmm_dir_table[] = {
374 #ifdef CONFIG_CMM_IUCV
375 #define SMSG_PREFIX "CMM"
377 cmm_smsg_target(char *from, char *msg)
381 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
383 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
385 if (strncmp(msg, "SHRINK", 6) == 0) {
386 if (!cmm_skip_blanks(msg + 6, &msg))
388 nr = simple_strtoul(msg, &msg, 0);
389 cmm_skip_blanks(msg, &msg);
392 } else if (strncmp(msg, "RELEASE", 7) == 0) {
393 if (!cmm_skip_blanks(msg + 7, &msg))
395 nr = simple_strtoul(msg, &msg, 0);
396 cmm_skip_blanks(msg, &msg);
398 cmm_add_timed_pages(nr);
399 } else if (strncmp(msg, "REUSE", 5) == 0) {
400 if (!cmm_skip_blanks(msg + 5, &msg))
402 nr = simple_strtoul(msg, &msg, 0);
403 if (!cmm_skip_blanks(msg, &msg))
405 seconds = simple_strtoul(msg, &msg, 0);
406 cmm_skip_blanks(msg, &msg);
408 cmm_set_timeout(nr, seconds);
413 static struct ctl_table_header *cmm_sysctl_header;
420 #ifdef CONFIG_CMM_PROC
421 cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
422 if (!cmm_sysctl_header)
425 #ifdef CONFIG_CMM_IUCV
426 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
430 rc = register_oom_notifier(&cmm_oom_nb);
433 init_waitqueue_head(&cmm_thread_wait);
434 init_timer(&cmm_timer);
435 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
436 rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0;
440 * kthread_create failed. undo all the stuff from above again.
442 unregister_oom_notifier(&cmm_oom_nb);
445 #ifdef CONFIG_CMM_IUCV
446 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
449 #ifdef CONFIG_CMM_PROC
450 unregister_sysctl_table(cmm_sysctl_header);
459 kthread_stop(cmm_thread_ptr);
460 unregister_oom_notifier(&cmm_oom_nb);
461 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
462 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
463 #ifdef CONFIG_CMM_PROC
464 unregister_sysctl_table(cmm_sysctl_header);
466 #ifdef CONFIG_CMM_IUCV
467 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
471 module_init(cmm_init);
472 module_exit(cmm_exit);
474 EXPORT_SYMBOL(cmm_set_pages);
475 EXPORT_SYMBOL(cmm_get_pages);
476 EXPORT_SYMBOL(cmm_add_timed_pages);
477 EXPORT_SYMBOL(cmm_get_timed_pages);
478 EXPORT_SYMBOL(cmm_set_timeout);
480 MODULE_LICENSE("GPL");