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/config.h>
12 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/sysctl.h>
18 #include <linux/ctype.h>
20 #include <asm/pgalloc.h>
21 #include <asm/uaccess.h>
23 static char *sender = "VMRMSVM";
24 module_param(sender, charp, 0400);
25 MODULE_PARM_DESC(sender,
26 "Guest name that may send SMSG messages (default VMRMSVM)");
28 #include "../../../drivers/s390/net/smsgiucv.h"
30 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
32 struct cmm_page_array {
33 struct cmm_page_array *next;
35 unsigned long pages[CMM_NR_PAGES];
38 static long cmm_pages = 0;
39 static long cmm_timed_pages = 0;
40 static volatile long cmm_pages_target = 0;
41 static volatile long cmm_timed_pages_target = 0;
42 static long cmm_timeout_pages = 0;
43 static long cmm_timeout_seconds = 0;
45 static struct cmm_page_array *cmm_page_list = 0;
46 static struct cmm_page_array *cmm_timed_page_list = 0;
48 static unsigned long cmm_thread_active = 0;
49 static struct work_struct cmm_thread_starter;
50 static wait_queue_head_t cmm_thread_wait;
51 static struct timer_list cmm_timer;
53 static void cmm_timer_fn(unsigned long);
54 static void cmm_set_timer(void);
57 cmm_strtoul(const char *cp, char **endp)
59 unsigned int base = 10;
64 if ((*cp == 'x' || *cp == 'X') && isxdigit(cp[1])) {
69 return simple_strtoul(cp, endp, base);
73 cmm_alloc_pages(long pages, long *counter, struct cmm_page_array **list)
75 struct cmm_page_array *pa;
80 page = __get_free_page(GFP_NOIO);
83 if (!pa || pa->index >= CMM_NR_PAGES) {
84 /* Need a new page for the page list. */
85 pa = (struct cmm_page_array *)
86 __get_free_page(GFP_NOIO);
96 pa->pages[pa->index++] = page;
104 cmm_free_pages(long pages, long *counter, struct cmm_page_array **list)
106 struct cmm_page_array *pa;
111 if (!pa || pa->index <= 0)
113 page = pa->pages[--pa->index];
114 if (pa->index == 0) {
116 free_page((unsigned long) *list);
126 cmm_thread(void *dummy)
130 daemonize("cmmthread");
132 rc = wait_event_interruptible(cmm_thread_wait,
133 (cmm_pages != cmm_pages_target ||
134 cmm_timed_pages != cmm_timed_pages_target));
135 if (rc == -ERESTARTSYS) {
136 /* Got kill signal. End thread. */
137 clear_bit(0, &cmm_thread_active);
138 cmm_pages_target = cmm_pages;
139 cmm_timed_pages_target = cmm_timed_pages;
142 if (cmm_pages_target > cmm_pages) {
143 if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
144 cmm_pages_target = cmm_pages;
145 } else if (cmm_pages_target < cmm_pages) {
146 cmm_free_pages(1, &cmm_pages, &cmm_page_list);
148 if (cmm_timed_pages_target > cmm_timed_pages) {
149 if (cmm_alloc_pages(1, &cmm_timed_pages,
150 &cmm_timed_page_list))
151 cmm_timed_pages_target = cmm_timed_pages;
152 } else if (cmm_timed_pages_target < cmm_timed_pages) {
153 cmm_free_pages(1, &cmm_timed_pages,
154 &cmm_timed_page_list);
156 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
163 cmm_start_thread(void)
165 kernel_thread(cmm_thread, 0, 0);
169 cmm_kick_thread(void)
171 if (!test_and_set_bit(0, &cmm_thread_active))
172 schedule_work(&cmm_thread_starter);
173 wake_up(&cmm_thread_wait);
179 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
180 if (timer_pending(&cmm_timer))
181 del_timer(&cmm_timer);
184 if (timer_pending(&cmm_timer)) {
185 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
188 cmm_timer.function = cmm_timer_fn;
190 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
191 add_timer(&cmm_timer);
195 cmm_timer_fn(unsigned long ignored)
199 pages = cmm_timed_pages_target - cmm_timeout_pages;
201 cmm_timed_pages_target = 0;
203 cmm_timed_pages_target = pages;
209 cmm_set_pages(long pages)
211 cmm_pages_target = pages;
222 cmm_add_timed_pages(long pages)
224 cmm_timed_pages_target += pages;
229 cmm_get_timed_pages(void)
231 return cmm_timed_pages;
235 cmm_set_timeout(long pages, long seconds)
237 cmm_timeout_pages = pages;
238 cmm_timeout_seconds = seconds;
243 cmm_skip_blanks(char *cp, char **endp)
247 for (str = cp; *str == ' ' || *str == '\t'; str++);
252 #ifdef CONFIG_CMM_PROC
253 /* These will someday get removed. */
254 #define VM_CMM_PAGES 1111
255 #define VM_CMM_TIMED_PAGES 1112
256 #define VM_CMM_TIMEOUT 1113
258 static struct ctl_table cmm_table[];
261 cmm_pages_handler(ctl_table *ctl, int write, struct file *filp,
262 void *buffer, size_t *lenp, loff_t *ppos)
268 if (!*lenp || (*ppos && !write)) {
275 if (copy_from_user(buf, buffer,
276 len > sizeof(buf) ? sizeof(buf) : len))
278 buf[sizeof(buf) - 1] = '\0';
279 cmm_skip_blanks(buf, &p);
280 pages = cmm_strtoul(p, &p);
281 if (ctl == &cmm_table[0])
282 cmm_set_pages(pages);
284 cmm_add_timed_pages(pages);
286 if (ctl == &cmm_table[0])
287 pages = cmm_get_pages();
289 pages = cmm_get_timed_pages();
290 len = sprintf(buf, "%ld\n", pages);
293 if (copy_to_user(buffer, buf, len))
302 cmm_timeout_handler(ctl_table *ctl, int write, struct file *filp,
303 void *buffer, size_t *lenp, loff_t *ppos)
309 if (!*lenp || (*ppos && !write)) {
316 if (copy_from_user(buf, buffer,
317 len > sizeof(buf) ? sizeof(buf) : len))
319 buf[sizeof(buf) - 1] = '\0';
320 cmm_skip_blanks(buf, &p);
321 pages = cmm_strtoul(p, &p);
322 cmm_skip_blanks(p, &p);
323 seconds = cmm_strtoul(p, &p);
324 cmm_set_timeout(pages, seconds);
326 len = sprintf(buf, "%ld %ld\n",
327 cmm_timeout_pages, cmm_timeout_seconds);
330 if (copy_to_user(buffer, buf, len))
338 static struct ctl_table cmm_table[] = {
340 .ctl_name = VM_CMM_PAGES,
341 .procname = "cmm_pages",
343 .proc_handler = &cmm_pages_handler,
346 .ctl_name = VM_CMM_TIMED_PAGES,
347 .procname = "cmm_timed_pages",
349 .proc_handler = &cmm_pages_handler,
352 .ctl_name = VM_CMM_TIMEOUT,
353 .procname = "cmm_timeout",
355 .proc_handler = &cmm_timeout_handler,
360 static struct ctl_table cmm_dir_table[] = {
372 #ifdef CONFIG_CMM_IUCV
373 #define SMSG_PREFIX "CMM"
375 cmm_smsg_target(char *from, char *msg)
379 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
381 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
383 if (strncmp(msg, "SHRINK", 6) == 0) {
384 if (!cmm_skip_blanks(msg + 6, &msg))
386 pages = cmm_strtoul(msg, &msg);
387 cmm_skip_blanks(msg, &msg);
389 cmm_set_pages(pages);
390 } else if (strncmp(msg, "RELEASE", 7) == 0) {
391 if (!cmm_skip_blanks(msg + 7, &msg))
393 pages = cmm_strtoul(msg, &msg);
394 cmm_skip_blanks(msg, &msg);
396 cmm_add_timed_pages(pages);
397 } else if (strncmp(msg, "REUSE", 5) == 0) {
398 if (!cmm_skip_blanks(msg + 5, &msg))
400 pages = cmm_strtoul(msg, &msg);
401 if (!cmm_skip_blanks(msg, &msg))
403 seconds = cmm_strtoul(msg, &msg);
404 cmm_skip_blanks(msg, &msg);
406 cmm_set_timeout(pages, seconds);
411 struct ctl_table_header *cmm_sysctl_header;
416 #ifdef CONFIG_CMM_PROC
417 cmm_sysctl_header = register_sysctl_table(cmm_dir_table, 1);
419 #ifdef CONFIG_CMM_IUCV
420 smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
422 INIT_WORK(&cmm_thread_starter, (void *) cmm_start_thread, 0);
423 init_waitqueue_head(&cmm_thread_wait);
424 init_timer(&cmm_timer);
431 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
432 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
433 #ifdef CONFIG_CMM_PROC
434 unregister_sysctl_table(cmm_sysctl_header);
436 #ifdef CONFIG_CMM_IUCV
437 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
441 module_init(cmm_init);
442 module_exit(cmm_exit);
444 EXPORT_SYMBOL(cmm_set_pages);
445 EXPORT_SYMBOL(cmm_get_pages);
446 EXPORT_SYMBOL(cmm_add_timed_pages);
447 EXPORT_SYMBOL(cmm_get_timed_pages);
448 EXPORT_SYMBOL(cmm_set_timeout);
450 MODULE_LICENSE("GPL");