1 // SPDX-License-Identifier: GPL-2.0
3 * Collaborative memory management interface.
5 * Copyright IBM Corp 2003, 2010
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
10 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/gfp.h>
16 #include <linux/sched.h>
17 #include <linux/sysctl.h>
18 #include <linux/ctype.h>
19 #include <linux/swap.h>
20 #include <linux/kthread.h>
21 #include <linux/oom.h>
22 #include <linux/uaccess.h>
26 #ifdef CONFIG_CMM_IUCV
27 static char *cmm_default_sender
= "VMRMSVM";
30 module_param(sender
, charp
, 0400);
31 MODULE_PARM_DESC(sender
,
32 "Guest name that may send SMSG messages (default VMRMSVM)");
34 #include "../../../drivers/s390/net/smsgiucv.h"
36 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
38 struct cmm_page_array
{
39 struct cmm_page_array
*next
;
41 unsigned long pages
[CMM_NR_PAGES
];
44 static long cmm_pages
;
45 static long cmm_timed_pages
;
46 static volatile long cmm_pages_target
;
47 static volatile long cmm_timed_pages_target
;
48 static long cmm_timeout_pages
;
49 static long cmm_timeout_seconds
;
51 static struct cmm_page_array
*cmm_page_list
;
52 static struct cmm_page_array
*cmm_timed_page_list
;
53 static DEFINE_SPINLOCK(cmm_lock
);
55 static struct task_struct
*cmm_thread_ptr
;
56 static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait
);
58 static void cmm_timer_fn(struct timer_list
*);
59 static void cmm_set_timer(void);
60 static DEFINE_TIMER(cmm_timer
, cmm_timer_fn
);
62 static long cmm_alloc_pages(long nr
, long *counter
,
63 struct cmm_page_array
**list
)
65 struct cmm_page_array
*pa
, *npa
;
69 addr
= __get_free_page(GFP_NOIO
);
74 if (!pa
|| pa
->index
>= CMM_NR_PAGES
) {
75 /* Need a new page for the page list. */
76 spin_unlock(&cmm_lock
);
77 npa
= (struct cmm_page_array
*)
78 __get_free_page(GFP_NOIO
);
85 if (!pa
|| pa
->index
>= CMM_NR_PAGES
) {
91 free_page((unsigned long) npa
);
93 diag10_range(addr
>> PAGE_SHIFT
, 1);
94 pa
->pages
[pa
->index
++] = addr
;
96 spin_unlock(&cmm_lock
);
102 static long cmm_free_pages(long nr
, long *counter
, struct cmm_page_array
**list
)
104 struct cmm_page_array
*pa
;
107 spin_lock(&cmm_lock
);
110 if (!pa
|| pa
->index
<= 0)
112 addr
= pa
->pages
[--pa
->index
];
113 if (pa
->index
== 0) {
115 free_page((unsigned long) *list
);
122 spin_unlock(&cmm_lock
);
126 static int cmm_oom_notify(struct notifier_block
*self
,
127 unsigned long dummy
, void *parm
)
129 unsigned long *freed
= parm
;
132 nr
= cmm_free_pages(nr
, &cmm_timed_pages
, &cmm_timed_page_list
);
134 nr
= cmm_free_pages(nr
, &cmm_pages
, &cmm_page_list
);
135 cmm_pages_target
= cmm_pages
;
136 cmm_timed_pages_target
= cmm_timed_pages
;
141 static struct notifier_block cmm_oom_nb
= {
142 .notifier_call
= cmm_oom_notify
,
145 static int cmm_thread(void *dummy
)
150 rc
= wait_event_interruptible(cmm_thread_wait
,
151 cmm_pages
!= cmm_pages_target
||
152 cmm_timed_pages
!= cmm_timed_pages_target
||
153 kthread_should_stop());
154 if (kthread_should_stop() || rc
== -ERESTARTSYS
) {
155 cmm_pages_target
= cmm_pages
;
156 cmm_timed_pages_target
= cmm_timed_pages
;
159 if (cmm_pages_target
> cmm_pages
) {
160 if (cmm_alloc_pages(1, &cmm_pages
, &cmm_page_list
))
161 cmm_pages_target
= cmm_pages
;
162 } else if (cmm_pages_target
< cmm_pages
) {
163 cmm_free_pages(1, &cmm_pages
, &cmm_page_list
);
165 if (cmm_timed_pages_target
> cmm_timed_pages
) {
166 if (cmm_alloc_pages(1, &cmm_timed_pages
,
167 &cmm_timed_page_list
))
168 cmm_timed_pages_target
= cmm_timed_pages
;
169 } else if (cmm_timed_pages_target
< cmm_timed_pages
) {
170 cmm_free_pages(1, &cmm_timed_pages
,
171 &cmm_timed_page_list
);
173 if (cmm_timed_pages
> 0 && !timer_pending(&cmm_timer
))
179 static void cmm_kick_thread(void)
181 wake_up(&cmm_thread_wait
);
184 static void cmm_set_timer(void)
186 if (cmm_timed_pages_target
<= 0 || cmm_timeout_seconds
<= 0) {
187 if (timer_pending(&cmm_timer
))
188 del_timer(&cmm_timer
);
191 mod_timer(&cmm_timer
, jiffies
+ msecs_to_jiffies(cmm_timeout_seconds
* MSEC_PER_SEC
));
194 static void cmm_timer_fn(struct timer_list
*unused
)
198 nr
= cmm_timed_pages_target
- cmm_timeout_pages
;
200 cmm_timed_pages_target
= 0;
202 cmm_timed_pages_target
= nr
;
207 static void cmm_set_pages(long nr
)
209 cmm_pages_target
= nr
;
213 static long cmm_get_pages(void)
218 static void cmm_add_timed_pages(long nr
)
220 cmm_timed_pages_target
+= nr
;
224 static long cmm_get_timed_pages(void)
226 return cmm_timed_pages
;
229 static void cmm_set_timeout(long nr
, long seconds
)
231 cmm_timeout_pages
= nr
;
232 cmm_timeout_seconds
= seconds
;
236 static int cmm_skip_blanks(char *cp
, char **endp
)
240 for (str
= cp
; *str
== ' ' || *str
== '\t'; str
++)
246 static int cmm_pages_handler(struct ctl_table
*ctl
, int write
,
247 void *buffer
, size_t *lenp
, loff_t
*ppos
)
249 long nr
= cmm_get_pages();
250 struct ctl_table ctl_entry
= {
251 .procname
= ctl
->procname
,
253 .maxlen
= sizeof(long),
257 rc
= proc_doulongvec_minmax(&ctl_entry
, write
, buffer
, lenp
, ppos
);
258 if (rc
< 0 || !write
)
265 static int cmm_timed_pages_handler(struct ctl_table
*ctl
, int write
,
266 void *buffer
, size_t *lenp
,
269 long nr
= cmm_get_timed_pages();
270 struct ctl_table ctl_entry
= {
271 .procname
= ctl
->procname
,
273 .maxlen
= sizeof(long),
277 rc
= proc_doulongvec_minmax(&ctl_entry
, write
, buffer
, lenp
, ppos
);
278 if (rc
< 0 || !write
)
281 cmm_add_timed_pages(nr
);
285 static int cmm_timeout_handler(struct ctl_table
*ctl
, int write
,
286 void *buffer
, size_t *lenp
, loff_t
*ppos
)
292 if (!*lenp
|| (*ppos
&& !write
)) {
298 len
= min(*lenp
, sizeof(buf
));
299 memcpy(buf
, buffer
, len
);
301 cmm_skip_blanks(buf
, &p
);
302 nr
= simple_strtoul(p
, &p
, 0);
303 cmm_skip_blanks(p
, &p
);
304 seconds
= simple_strtoul(p
, &p
, 0);
305 cmm_set_timeout(nr
, seconds
);
308 len
= sprintf(buf
, "%ld %ld\n",
309 cmm_timeout_pages
, cmm_timeout_seconds
);
312 memcpy(buffer
, buf
, len
);
319 static struct ctl_table cmm_table
[] = {
321 .procname
= "cmm_pages",
323 .proc_handler
= cmm_pages_handler
,
326 .procname
= "cmm_timed_pages",
328 .proc_handler
= cmm_timed_pages_handler
,
331 .procname
= "cmm_timeout",
333 .proc_handler
= cmm_timeout_handler
,
338 static struct ctl_table cmm_dir_table
[] = {
348 #ifdef CONFIG_CMM_IUCV
349 #define SMSG_PREFIX "CMM"
350 static void cmm_smsg_target(const char *from
, char *msg
)
354 if (strlen(sender
) > 0 && strcmp(from
, sender
) != 0)
356 if (!cmm_skip_blanks(msg
+ strlen(SMSG_PREFIX
), &msg
))
358 if (strncmp(msg
, "SHRINK", 6) == 0) {
359 if (!cmm_skip_blanks(msg
+ 6, &msg
))
361 nr
= simple_strtoul(msg
, &msg
, 0);
362 cmm_skip_blanks(msg
, &msg
);
365 } else if (strncmp(msg
, "RELEASE", 7) == 0) {
366 if (!cmm_skip_blanks(msg
+ 7, &msg
))
368 nr
= simple_strtoul(msg
, &msg
, 0);
369 cmm_skip_blanks(msg
, &msg
);
371 cmm_add_timed_pages(nr
);
372 } else if (strncmp(msg
, "REUSE", 5) == 0) {
373 if (!cmm_skip_blanks(msg
+ 5, &msg
))
375 nr
= simple_strtoul(msg
, &msg
, 0);
376 if (!cmm_skip_blanks(msg
, &msg
))
378 seconds
= simple_strtoul(msg
, &msg
, 0);
379 cmm_skip_blanks(msg
, &msg
);
381 cmm_set_timeout(nr
, seconds
);
386 static struct ctl_table_header
*cmm_sysctl_header
;
388 static int __init
cmm_init(void)
392 cmm_sysctl_header
= register_sysctl_table(cmm_dir_table
);
393 if (!cmm_sysctl_header
)
395 #ifdef CONFIG_CMM_IUCV
396 /* convert sender to uppercase characters */
398 int len
= strlen(sender
);
400 sender
[len
] = toupper(sender
[len
]);
402 sender
= cmm_default_sender
;
405 rc
= smsg_register_callback(SMSG_PREFIX
, cmm_smsg_target
);
409 rc
= register_oom_notifier(&cmm_oom_nb
);
412 cmm_thread_ptr
= kthread_run(cmm_thread
, NULL
, "cmmthread");
413 if (!IS_ERR(cmm_thread_ptr
))
416 rc
= PTR_ERR(cmm_thread_ptr
);
417 unregister_oom_notifier(&cmm_oom_nb
);
419 #ifdef CONFIG_CMM_IUCV
420 smsg_unregister_callback(SMSG_PREFIX
, cmm_smsg_target
);
423 unregister_sysctl_table(cmm_sysctl_header
);
425 del_timer_sync(&cmm_timer
);
428 module_init(cmm_init
);
430 static void __exit
cmm_exit(void)
432 unregister_sysctl_table(cmm_sysctl_header
);
433 #ifdef CONFIG_CMM_IUCV
434 smsg_unregister_callback(SMSG_PREFIX
, cmm_smsg_target
);
436 unregister_oom_notifier(&cmm_oom_nb
);
437 kthread_stop(cmm_thread_ptr
);
438 del_timer_sync(&cmm_timer
);
439 cmm_free_pages(cmm_pages
, &cmm_pages
, &cmm_page_list
);
440 cmm_free_pages(cmm_timed_pages
, &cmm_timed_pages
, &cmm_timed_page_list
);
442 module_exit(cmm_exit
);
444 MODULE_LICENSE("GPL");