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>
20 #include <linux/oom.h>
22 #include <asm/pgalloc.h>
23 #include <asm/uaccess.h>
26 static char *sender
= "VMRMSVM";
27 module_param(sender
, charp
, 0400);
28 MODULE_PARM_DESC(sender
,
29 "Guest name that may send SMSG messages (default VMRMSVM)");
31 #include "../../../drivers/s390/net/smsgiucv.h"
33 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
35 struct cmm_page_array
{
36 struct cmm_page_array
*next
;
38 unsigned long pages
[CMM_NR_PAGES
];
41 static long cmm_pages
;
42 static long cmm_timed_pages
;
43 static volatile long cmm_pages_target
;
44 static volatile long cmm_timed_pages_target
;
45 static long cmm_timeout_pages
;
46 static long cmm_timeout_seconds
;
48 static struct cmm_page_array
*cmm_page_list
;
49 static struct cmm_page_array
*cmm_timed_page_list
;
50 static DEFINE_SPINLOCK(cmm_lock
);
52 static struct task_struct
*cmm_thread_ptr
;
53 static wait_queue_head_t cmm_thread_wait
;
54 static struct timer_list cmm_timer
;
56 static void cmm_timer_fn(unsigned long);
57 static void cmm_set_timer(void);
60 cmm_alloc_pages(long nr
, long *counter
, struct cmm_page_array
**list
)
62 struct cmm_page_array
*pa
, *npa
;
66 addr
= __get_free_page(GFP_NOIO
);
71 if (!pa
|| pa
->index
>= CMM_NR_PAGES
) {
72 /* Need a new page for the page list. */
73 spin_unlock(&cmm_lock
);
74 npa
= (struct cmm_page_array
*)
75 __get_free_page(GFP_NOIO
);
82 if (!pa
|| pa
->index
>= CMM_NR_PAGES
) {
88 free_page((unsigned long) npa
);
91 pa
->pages
[pa
->index
++] = addr
;
93 spin_unlock(&cmm_lock
);
100 cmm_free_pages(long nr
, long *counter
, struct cmm_page_array
**list
)
102 struct cmm_page_array
*pa
;
105 spin_lock(&cmm_lock
);
108 if (!pa
|| pa
->index
<= 0)
110 addr
= pa
->pages
[--pa
->index
];
111 if (pa
->index
== 0) {
113 free_page((unsigned long) *list
);
120 spin_unlock(&cmm_lock
);
124 static int cmm_oom_notify(struct notifier_block
*self
,
125 unsigned long dummy
, void *parm
)
127 unsigned long *freed
= parm
;
130 nr
= cmm_free_pages(nr
, &cmm_timed_pages
, &cmm_timed_page_list
);
132 nr
= cmm_free_pages(nr
, &cmm_pages
, &cmm_page_list
);
133 cmm_pages_target
= cmm_pages
;
134 cmm_timed_pages_target
= cmm_timed_pages
;
139 static struct notifier_block cmm_oom_nb
= {
140 .notifier_call
= cmm_oom_notify
144 cmm_thread(void *dummy
)
149 rc
= wait_event_interruptible(cmm_thread_wait
,
150 (cmm_pages
!= cmm_pages_target
||
151 cmm_timed_pages
!= cmm_timed_pages_target
||
152 kthread_should_stop()));
153 if (kthread_should_stop() || rc
== -ERESTARTSYS
) {
154 cmm_pages_target
= cmm_pages
;
155 cmm_timed_pages_target
= cmm_timed_pages
;
158 if (cmm_pages_target
> cmm_pages
) {
159 if (cmm_alloc_pages(1, &cmm_pages
, &cmm_page_list
))
160 cmm_pages_target
= cmm_pages
;
161 } else if (cmm_pages_target
< cmm_pages
) {
162 cmm_free_pages(1, &cmm_pages
, &cmm_page_list
);
164 if (cmm_timed_pages_target
> cmm_timed_pages
) {
165 if (cmm_alloc_pages(1, &cmm_timed_pages
,
166 &cmm_timed_page_list
))
167 cmm_timed_pages_target
= cmm_timed_pages
;
168 } else if (cmm_timed_pages_target
< cmm_timed_pages
) {
169 cmm_free_pages(1, &cmm_timed_pages
,
170 &cmm_timed_page_list
);
172 if (cmm_timed_pages
> 0 && !timer_pending(&cmm_timer
))
179 cmm_kick_thread(void)
181 wake_up(&cmm_thread_wait
);
187 if (cmm_timed_pages_target
<= 0 || cmm_timeout_seconds
<= 0) {
188 if (timer_pending(&cmm_timer
))
189 del_timer(&cmm_timer
);
192 if (timer_pending(&cmm_timer
)) {
193 if (mod_timer(&cmm_timer
, jiffies
+ cmm_timeout_seconds
*HZ
))
196 cmm_timer
.function
= cmm_timer_fn
;
198 cmm_timer
.expires
= jiffies
+ cmm_timeout_seconds
*HZ
;
199 add_timer(&cmm_timer
);
203 cmm_timer_fn(unsigned long ignored
)
207 nr
= cmm_timed_pages_target
- cmm_timeout_pages
;
209 cmm_timed_pages_target
= 0;
211 cmm_timed_pages_target
= nr
;
217 cmm_set_pages(long nr
)
219 cmm_pages_target
= nr
;
230 cmm_add_timed_pages(long nr
)
232 cmm_timed_pages_target
+= nr
;
237 cmm_get_timed_pages(void)
239 return cmm_timed_pages
;
243 cmm_set_timeout(long nr
, long seconds
)
245 cmm_timeout_pages
= nr
;
246 cmm_timeout_seconds
= seconds
;
251 cmm_skip_blanks(char *cp
, char **endp
)
255 for (str
= cp
; *str
== ' ' || *str
== '\t'; str
++);
260 #ifdef CONFIG_CMM_PROC
262 static struct ctl_table cmm_table
[];
265 cmm_pages_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
266 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
272 if (!*lenp
|| (*ppos
&& !write
)) {
279 if (copy_from_user(buf
, buffer
,
280 len
> sizeof(buf
) ? sizeof(buf
) : len
))
282 buf
[sizeof(buf
) - 1] = '\0';
283 cmm_skip_blanks(buf
, &p
);
284 nr
= simple_strtoul(p
, &p
, 0);
285 if (ctl
== &cmm_table
[0])
288 cmm_add_timed_pages(nr
);
290 if (ctl
== &cmm_table
[0])
291 nr
= cmm_get_pages();
293 nr
= cmm_get_timed_pages();
294 len
= sprintf(buf
, "%ld\n", nr
);
297 if (copy_to_user(buffer
, buf
, len
))
306 cmm_timeout_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
307 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
313 if (!*lenp
|| (*ppos
&& !write
)) {
320 if (copy_from_user(buf
, buffer
,
321 len
> sizeof(buf
) ? sizeof(buf
) : len
))
323 buf
[sizeof(buf
) - 1] = '\0';
324 cmm_skip_blanks(buf
, &p
);
325 nr
= simple_strtoul(p
, &p
, 0);
326 cmm_skip_blanks(p
, &p
);
327 seconds
= simple_strtoul(p
, &p
, 0);
328 cmm_set_timeout(nr
, seconds
);
330 len
= sprintf(buf
, "%ld %ld\n",
331 cmm_timeout_pages
, cmm_timeout_seconds
);
334 if (copy_to_user(buffer
, buf
, len
))
342 static struct ctl_table cmm_table
[] = {
344 .procname
= "cmm_pages",
346 .proc_handler
= &cmm_pages_handler
,
349 .procname
= "cmm_timed_pages",
351 .proc_handler
= &cmm_pages_handler
,
354 .procname
= "cmm_timeout",
356 .proc_handler
= &cmm_timeout_handler
,
361 static struct ctl_table cmm_dir_table
[] = {
373 #ifdef CONFIG_CMM_IUCV
374 #define SMSG_PREFIX "CMM"
376 cmm_smsg_target(char *from
, char *msg
)
380 if (strlen(sender
) > 0 && strcmp(from
, sender
) != 0)
382 if (!cmm_skip_blanks(msg
+ strlen(SMSG_PREFIX
), &msg
))
384 if (strncmp(msg
, "SHRINK", 6) == 0) {
385 if (!cmm_skip_blanks(msg
+ 6, &msg
))
387 nr
= simple_strtoul(msg
, &msg
, 0);
388 cmm_skip_blanks(msg
, &msg
);
391 } else if (strncmp(msg
, "RELEASE", 7) == 0) {
392 if (!cmm_skip_blanks(msg
+ 7, &msg
))
394 nr
= simple_strtoul(msg
, &msg
, 0);
395 cmm_skip_blanks(msg
, &msg
);
397 cmm_add_timed_pages(nr
);
398 } else if (strncmp(msg
, "REUSE", 5) == 0) {
399 if (!cmm_skip_blanks(msg
+ 5, &msg
))
401 nr
= simple_strtoul(msg
, &msg
, 0);
402 if (!cmm_skip_blanks(msg
, &msg
))
404 seconds
= simple_strtoul(msg
, &msg
, 0);
405 cmm_skip_blanks(msg
, &msg
);
407 cmm_set_timeout(nr
, seconds
);
412 static struct ctl_table_header
*cmm_sysctl_header
;
419 #ifdef CONFIG_CMM_PROC
420 cmm_sysctl_header
= register_sysctl_table(cmm_dir_table
);
421 if (!cmm_sysctl_header
)
424 #ifdef CONFIG_CMM_IUCV
425 rc
= smsg_register_callback(SMSG_PREFIX
, cmm_smsg_target
);
429 rc
= register_oom_notifier(&cmm_oom_nb
);
432 init_waitqueue_head(&cmm_thread_wait
);
433 init_timer(&cmm_timer
);
434 cmm_thread_ptr
= kthread_run(cmm_thread
, NULL
, "cmmthread");
435 rc
= IS_ERR(cmm_thread_ptr
) ? PTR_ERR(cmm_thread_ptr
) : 0;
439 * kthread_create failed. undo all the stuff from above again.
441 unregister_oom_notifier(&cmm_oom_nb
);
444 #ifdef CONFIG_CMM_IUCV
445 smsg_unregister_callback(SMSG_PREFIX
, cmm_smsg_target
);
448 #ifdef CONFIG_CMM_PROC
449 unregister_sysctl_table(cmm_sysctl_header
);
458 kthread_stop(cmm_thread_ptr
);
459 unregister_oom_notifier(&cmm_oom_nb
);
460 cmm_free_pages(cmm_pages
, &cmm_pages
, &cmm_page_list
);
461 cmm_free_pages(cmm_timed_pages
, &cmm_timed_pages
, &cmm_timed_page_list
);
462 #ifdef CONFIG_CMM_PROC
463 unregister_sysctl_table(cmm_sysctl_header
);
465 #ifdef CONFIG_CMM_IUCV
466 smsg_unregister_callback(SMSG_PREFIX
, cmm_smsg_target
);
470 module_init(cmm_init
);
471 module_exit(cmm_exit
);
473 EXPORT_SYMBOL(cmm_set_pages
);
474 EXPORT_SYMBOL(cmm_get_pages
);
475 EXPORT_SYMBOL(cmm_add_timed_pages
);
476 EXPORT_SYMBOL(cmm_get_timed_pages
);
477 EXPORT_SYMBOL(cmm_set_timeout
);
479 MODULE_LICENSE("GPL");