2 * Collaborative memory management interface.
4 * Copyright IBM Corp 2003, 2010
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
9 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/gfp.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>
21 #include <linux/suspend.h>
22 #include <linux/uaccess.h>
24 #include <asm/pgalloc.h>
27 #ifdef CONFIG_CMM_IUCV
28 static char *cmm_default_sender
= "VMRMSVM";
31 module_param(sender
, charp
, 0400);
32 MODULE_PARM_DESC(sender
,
33 "Guest name that may send SMSG messages (default VMRMSVM)");
35 #include "../../../drivers/s390/net/smsgiucv.h"
37 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
39 struct cmm_page_array
{
40 struct cmm_page_array
*next
;
42 unsigned long pages
[CMM_NR_PAGES
];
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 static int cmm_suspended
;
53 static struct cmm_page_array
*cmm_page_list
;
54 static struct cmm_page_array
*cmm_timed_page_list
;
55 static DEFINE_SPINLOCK(cmm_lock
);
57 static struct task_struct
*cmm_thread_ptr
;
58 static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait
);
59 static DEFINE_TIMER(cmm_timer
, NULL
, 0, 0);
61 static void cmm_timer_fn(unsigned long);
62 static void cmm_set_timer(void);
64 static long cmm_alloc_pages(long nr
, long *counter
,
65 struct cmm_page_array
**list
)
67 struct cmm_page_array
*pa
, *npa
;
71 addr
= __get_free_page(GFP_NOIO
);
76 if (!pa
|| pa
->index
>= CMM_NR_PAGES
) {
77 /* Need a new page for the page list. */
78 spin_unlock(&cmm_lock
);
79 npa
= (struct cmm_page_array
*)
80 __get_free_page(GFP_NOIO
);
87 if (!pa
|| pa
->index
>= CMM_NR_PAGES
) {
93 free_page((unsigned long) npa
);
95 diag10_range(addr
>> PAGE_SHIFT
, 1);
96 pa
->pages
[pa
->index
++] = addr
;
98 spin_unlock(&cmm_lock
);
104 static long cmm_free_pages(long nr
, long *counter
, struct cmm_page_array
**list
)
106 struct cmm_page_array
*pa
;
109 spin_lock(&cmm_lock
);
112 if (!pa
|| pa
->index
<= 0)
114 addr
= pa
->pages
[--pa
->index
];
115 if (pa
->index
== 0) {
117 free_page((unsigned long) *list
);
124 spin_unlock(&cmm_lock
);
128 static int cmm_oom_notify(struct notifier_block
*self
,
129 unsigned long dummy
, void *parm
)
131 unsigned long *freed
= parm
;
134 nr
= cmm_free_pages(nr
, &cmm_timed_pages
, &cmm_timed_page_list
);
136 nr
= cmm_free_pages(nr
, &cmm_pages
, &cmm_page_list
);
137 cmm_pages_target
= cmm_pages
;
138 cmm_timed_pages_target
= cmm_timed_pages
;
143 static struct notifier_block cmm_oom_nb
= {
144 .notifier_call
= cmm_oom_notify
,
147 static int cmm_thread(void *dummy
)
152 rc
= wait_event_interruptible(cmm_thread_wait
,
153 (!cmm_suspended
&& (cmm_pages
!= cmm_pages_target
||
154 cmm_timed_pages
!= cmm_timed_pages_target
)) ||
155 kthread_should_stop());
156 if (kthread_should_stop() || rc
== -ERESTARTSYS
) {
157 cmm_pages_target
= cmm_pages
;
158 cmm_timed_pages_target
= cmm_timed_pages
;
161 if (cmm_pages_target
> cmm_pages
) {
162 if (cmm_alloc_pages(1, &cmm_pages
, &cmm_page_list
))
163 cmm_pages_target
= cmm_pages
;
164 } else if (cmm_pages_target
< cmm_pages
) {
165 cmm_free_pages(1, &cmm_pages
, &cmm_page_list
);
167 if (cmm_timed_pages_target
> cmm_timed_pages
) {
168 if (cmm_alloc_pages(1, &cmm_timed_pages
,
169 &cmm_timed_page_list
))
170 cmm_timed_pages_target
= cmm_timed_pages
;
171 } else if (cmm_timed_pages_target
< cmm_timed_pages
) {
172 cmm_free_pages(1, &cmm_timed_pages
,
173 &cmm_timed_page_list
);
175 if (cmm_timed_pages
> 0 && !timer_pending(&cmm_timer
))
181 static void cmm_kick_thread(void)
183 wake_up(&cmm_thread_wait
);
186 static void cmm_set_timer(void)
188 if (cmm_timed_pages_target
<= 0 || cmm_timeout_seconds
<= 0) {
189 if (timer_pending(&cmm_timer
))
190 del_timer(&cmm_timer
);
193 if (timer_pending(&cmm_timer
)) {
194 if (mod_timer(&cmm_timer
, jiffies
+ cmm_timeout_seconds
*HZ
))
197 cmm_timer
.function
= cmm_timer_fn
;
199 cmm_timer
.expires
= jiffies
+ cmm_timeout_seconds
*HZ
;
200 add_timer(&cmm_timer
);
203 static void 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
;
216 static void cmm_set_pages(long nr
)
218 cmm_pages_target
= nr
;
222 static long cmm_get_pages(void)
227 static void cmm_add_timed_pages(long nr
)
229 cmm_timed_pages_target
+= nr
;
233 static long cmm_get_timed_pages(void)
235 return cmm_timed_pages
;
238 static void cmm_set_timeout(long nr
, long seconds
)
240 cmm_timeout_pages
= nr
;
241 cmm_timeout_seconds
= seconds
;
245 static int cmm_skip_blanks(char *cp
, char **endp
)
249 for (str
= cp
; *str
== ' ' || *str
== '\t'; str
++)
255 static struct ctl_table cmm_table
[];
257 static int cmm_pages_handler(struct ctl_table
*ctl
, int write
,
258 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
264 if (!*lenp
|| (*ppos
&& !write
)) {
271 if (copy_from_user(buf
, buffer
,
272 len
> sizeof(buf
) ? sizeof(buf
) : len
))
274 buf
[sizeof(buf
) - 1] = '\0';
275 cmm_skip_blanks(buf
, &p
);
276 nr
= simple_strtoul(p
, &p
, 0);
277 if (ctl
== &cmm_table
[0])
280 cmm_add_timed_pages(nr
);
282 if (ctl
== &cmm_table
[0])
283 nr
= cmm_get_pages();
285 nr
= cmm_get_timed_pages();
286 len
= sprintf(buf
, "%ld\n", nr
);
289 if (copy_to_user(buffer
, buf
, len
))
297 static int cmm_timeout_handler(struct ctl_table
*ctl
, int write
,
298 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
304 if (!*lenp
|| (*ppos
&& !write
)) {
311 if (copy_from_user(buf
, buffer
,
312 len
> sizeof(buf
) ? sizeof(buf
) : len
))
314 buf
[sizeof(buf
) - 1] = '\0';
315 cmm_skip_blanks(buf
, &p
);
316 nr
= simple_strtoul(p
, &p
, 0);
317 cmm_skip_blanks(p
, &p
);
318 seconds
= simple_strtoul(p
, &p
, 0);
319 cmm_set_timeout(nr
, seconds
);
321 len
= sprintf(buf
, "%ld %ld\n",
322 cmm_timeout_pages
, cmm_timeout_seconds
);
325 if (copy_to_user(buffer
, buf
, len
))
333 static struct ctl_table cmm_table
[] = {
335 .procname
= "cmm_pages",
337 .proc_handler
= cmm_pages_handler
,
340 .procname
= "cmm_timed_pages",
342 .proc_handler
= cmm_pages_handler
,
345 .procname
= "cmm_timeout",
347 .proc_handler
= cmm_timeout_handler
,
352 static struct ctl_table cmm_dir_table
[] = {
362 #ifdef CONFIG_CMM_IUCV
363 #define SMSG_PREFIX "CMM"
364 static void cmm_smsg_target(const char *from
, char *msg
)
368 if (strlen(sender
) > 0 && strcmp(from
, sender
) != 0)
370 if (!cmm_skip_blanks(msg
+ strlen(SMSG_PREFIX
), &msg
))
372 if (strncmp(msg
, "SHRINK", 6) == 0) {
373 if (!cmm_skip_blanks(msg
+ 6, &msg
))
375 nr
= simple_strtoul(msg
, &msg
, 0);
376 cmm_skip_blanks(msg
, &msg
);
379 } else if (strncmp(msg
, "RELEASE", 7) == 0) {
380 if (!cmm_skip_blanks(msg
+ 7, &msg
))
382 nr
= simple_strtoul(msg
, &msg
, 0);
383 cmm_skip_blanks(msg
, &msg
);
385 cmm_add_timed_pages(nr
);
386 } else if (strncmp(msg
, "REUSE", 5) == 0) {
387 if (!cmm_skip_blanks(msg
+ 5, &msg
))
389 nr
= simple_strtoul(msg
, &msg
, 0);
390 if (!cmm_skip_blanks(msg
, &msg
))
392 seconds
= simple_strtoul(msg
, &msg
, 0);
393 cmm_skip_blanks(msg
, &msg
);
395 cmm_set_timeout(nr
, seconds
);
400 static struct ctl_table_header
*cmm_sysctl_header
;
402 static int cmm_suspend(void)
405 cmm_free_pages(cmm_pages
, &cmm_pages
, &cmm_page_list
);
406 cmm_free_pages(cmm_timed_pages
, &cmm_timed_pages
, &cmm_timed_page_list
);
410 static int cmm_resume(void)
417 static int cmm_power_event(struct notifier_block
*this,
418 unsigned long event
, void *ptr
)
421 case PM_POST_HIBERNATION
:
423 case PM_HIBERNATION_PREPARE
:
424 return cmm_suspend();
430 static struct notifier_block cmm_power_notifier
= {
431 .notifier_call
= cmm_power_event
,
434 static int __init
cmm_init(void)
438 cmm_sysctl_header
= register_sysctl_table(cmm_dir_table
);
439 if (!cmm_sysctl_header
)
441 #ifdef CONFIG_CMM_IUCV
442 /* convert sender to uppercase characters */
444 int len
= strlen(sender
);
446 sender
[len
] = toupper(sender
[len
]);
448 sender
= cmm_default_sender
;
451 rc
= smsg_register_callback(SMSG_PREFIX
, cmm_smsg_target
);
455 rc
= register_oom_notifier(&cmm_oom_nb
);
458 rc
= register_pm_notifier(&cmm_power_notifier
);
461 cmm_thread_ptr
= kthread_run(cmm_thread
, NULL
, "cmmthread");
462 if (!IS_ERR(cmm_thread_ptr
))
465 rc
= PTR_ERR(cmm_thread_ptr
);
466 unregister_pm_notifier(&cmm_power_notifier
);
468 unregister_oom_notifier(&cmm_oom_nb
);
470 #ifdef CONFIG_CMM_IUCV
471 smsg_unregister_callback(SMSG_PREFIX
, cmm_smsg_target
);
474 unregister_sysctl_table(cmm_sysctl_header
);
476 del_timer_sync(&cmm_timer
);
479 module_init(cmm_init
);
481 static void __exit
cmm_exit(void)
483 unregister_sysctl_table(cmm_sysctl_header
);
484 #ifdef CONFIG_CMM_IUCV
485 smsg_unregister_callback(SMSG_PREFIX
, cmm_smsg_target
);
487 unregister_pm_notifier(&cmm_power_notifier
);
488 unregister_oom_notifier(&cmm_oom_nb
);
489 kthread_stop(cmm_thread_ptr
);
490 del_timer_sync(&cmm_timer
);
491 cmm_free_pages(cmm_pages
, &cmm_pages
, &cmm_page_list
);
492 cmm_free_pages(cmm_timed_pages
, &cmm_timed_pages
, &cmm_timed_page_list
);
494 module_exit(cmm_exit
);
496 MODULE_LICENSE("GPL");