blob: f2e00df99bae6f10ff0c535deb00b9733ebbe1e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/mm/cmm.c
3 *
4 * S390 version
5 * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 *
8 * Collaborative memory management interface.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/fs.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>
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070018#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/pgalloc.h>
21#include <asm/uaccess.h>
22
Martin Schwidefsky15439d72005-05-01 08:58:58 -070023static char *sender = "VMRMSVM";
Heiko Carstens447570c2005-06-21 17:16:29 -070024module_param(sender, charp, 0400);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070025MODULE_PARM_DESC(sender,
26 "Guest name that may send SMSG messages (default VMRMSVM)");
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "../../../drivers/s390/net/smsgiucv.h"
29
30#define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
31
32struct cmm_page_array {
33 struct cmm_page_array *next;
34 unsigned long index;
35 unsigned long pages[CMM_NR_PAGES];
36};
37
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070038static long cmm_pages;
39static long cmm_timed_pages;
40static volatile long cmm_pages_target;
41static volatile long cmm_timed_pages_target;
42static long cmm_timeout_pages;
43static long cmm_timeout_seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070045static struct cmm_page_array *cmm_page_list;
46static struct cmm_page_array *cmm_timed_page_list;
47static DEFINE_SPINLOCK(cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070049static unsigned long cmm_thread_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static struct work_struct cmm_thread_starter;
51static wait_queue_head_t cmm_thread_wait;
52static struct timer_list cmm_timer;
53
54static void cmm_timer_fn(unsigned long);
55static void cmm_set_timer(void);
56
57static long
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070058cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070060 struct cmm_page_array *pa, *npa;
61 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070063 while (nr) {
64 addr = __get_free_page(GFP_NOIO);
65 if (!addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070067 spin_lock(&cmm_lock);
68 pa = *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!pa || pa->index >= CMM_NR_PAGES) {
70 /* Need a new page for the page list. */
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070071 spin_unlock(&cmm_lock);
72 npa = (struct cmm_page_array *)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 __get_free_page(GFP_NOIO);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070074 if (!npa) {
75 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 break;
77 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070078 spin_lock(&cmm_lock);
79 pa = *list;
80 if (!pa || pa->index >= CMM_NR_PAGES) {
81 npa->next = pa;
82 npa->index = 0;
83 pa = npa;
84 *list = pa;
85 } else
86 free_page((unsigned long) npa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070088 diag10(addr);
89 pa->pages[pa->index++] = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 (*counter)++;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070091 spin_unlock(&cmm_lock);
92 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070094 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070097static long
98cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct cmm_page_array *pa;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700101 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700103 spin_lock(&cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 pa = *list;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700105 while (nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (!pa || pa->index <= 0)
107 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700108 addr = pa->pages[--pa->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (pa->index == 0) {
110 pa = pa->next;
111 free_page((unsigned long) *list);
112 *list = pa;
113 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700114 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 (*counter)--;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700116 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700118 spin_unlock(&cmm_lock);
119 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700122static int cmm_oom_notify(struct notifier_block *self,
123 unsigned long dummy, void *parm)
124{
125 unsigned long *freed = parm;
126 long nr = 256;
127
128 nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
129 if (nr > 0)
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;
133 *freed += 256 - nr;
134 return NOTIFY_OK;
135}
136
137static struct notifier_block cmm_oom_nb = {
138 .notifier_call = cmm_oom_notify
139};
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int
142cmm_thread(void *dummy)
143{
144 int rc;
145
146 daemonize("cmmthread");
147 while (1) {
148 rc = wait_event_interruptible(cmm_thread_wait,
149 (cmm_pages != cmm_pages_target ||
150 cmm_timed_pages != cmm_timed_pages_target));
151 if (rc == -ERESTARTSYS) {
152 /* Got kill signal. End thread. */
153 clear_bit(0, &cmm_thread_active);
154 cmm_pages_target = cmm_pages;
155 cmm_timed_pages_target = cmm_timed_pages;
156 break;
157 }
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);
163 }
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);
171 }
172 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
173 cmm_set_timer();
174 }
175 return 0;
176}
177
178static void
179cmm_start_thread(void)
180{
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200181 kernel_thread(cmm_thread, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static void
185cmm_kick_thread(void)
186{
187 if (!test_and_set_bit(0, &cmm_thread_active))
188 schedule_work(&cmm_thread_starter);
189 wake_up(&cmm_thread_wait);
190}
191
192static void
193cmm_set_timer(void)
194{
195 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
196 if (timer_pending(&cmm_timer))
197 del_timer(&cmm_timer);
198 return;
199 }
200 if (timer_pending(&cmm_timer)) {
201 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
202 return;
203 }
204 cmm_timer.function = cmm_timer_fn;
205 cmm_timer.data = 0;
206 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
207 add_timer(&cmm_timer);
208}
209
210static void
211cmm_timer_fn(unsigned long ignored)
212{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700213 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700215 nr = cmm_timed_pages_target - cmm_timeout_pages;
216 if (nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 cmm_timed_pages_target = 0;
218 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700219 cmm_timed_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 cmm_kick_thread();
221 cmm_set_timer();
222}
223
224void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700225cmm_set_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700227 cmm_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 cmm_kick_thread();
229}
230
231long
232cmm_get_pages(void)
233{
234 return cmm_pages;
235}
236
237void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700238cmm_add_timed_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700240 cmm_timed_pages_target += nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 cmm_kick_thread();
242}
243
244long
245cmm_get_timed_pages(void)
246{
247 return cmm_timed_pages;
248}
249
250void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700251cmm_set_timeout(long nr, long seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700253 cmm_timeout_pages = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 cmm_timeout_seconds = seconds;
255 cmm_set_timer();
256}
257
258static inline int
259cmm_skip_blanks(char *cp, char **endp)
260{
261 char *str;
262
263 for (str = cp; *str == ' ' || *str == '\t'; str++);
264 *endp = str;
265 return str != cp;
266}
267
268#ifdef CONFIG_CMM_PROC
269/* These will someday get removed. */
270#define VM_CMM_PAGES 1111
271#define VM_CMM_TIMED_PAGES 1112
272#define VM_CMM_TIMEOUT 1113
273
274static struct ctl_table cmm_table[];
275
276static int
277cmm_pages_handler(ctl_table *ctl, int write, struct file *filp,
Al Viroaaedd942006-02-01 06:29:14 -0500278 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 char buf[16], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700281 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int len;
283
284 if (!*lenp || (*ppos && !write)) {
285 *lenp = 0;
286 return 0;
287 }
288
289 if (write) {
290 len = *lenp;
291 if (copy_from_user(buf, buffer,
292 len > sizeof(buf) ? sizeof(buf) : len))
293 return -EFAULT;
294 buf[sizeof(buf) - 1] = '\0';
295 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700296 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700298 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700300 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 } else {
302 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700303 nr = cmm_get_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700305 nr = cmm_get_timed_pages();
306 len = sprintf(buf, "%ld\n", nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (len > *lenp)
308 len = *lenp;
309 if (copy_to_user(buffer, buf, len))
310 return -EFAULT;
311 }
312 *lenp = len;
313 *ppos += len;
314 return 0;
315}
316
317static int
318cmm_timeout_handler(ctl_table *ctl, int write, struct file *filp,
Al Viroaaedd942006-02-01 06:29:14 -0500319 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 char buf[64], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700322 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int len;
324
325 if (!*lenp || (*ppos && !write)) {
326 *lenp = 0;
327 return 0;
328 }
329
330 if (write) {
331 len = *lenp;
332 if (copy_from_user(buf, buffer,
333 len > sizeof(buf) ? sizeof(buf) : len))
334 return -EFAULT;
335 buf[sizeof(buf) - 1] = '\0';
336 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700337 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 cmm_skip_blanks(p, &p);
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200339 seconds = simple_strtoul(p, &p, 0);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700340 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 } else {
342 len = sprintf(buf, "%ld %ld\n",
343 cmm_timeout_pages, cmm_timeout_seconds);
344 if (len > *lenp)
345 len = *lenp;
346 if (copy_to_user(buffer, buf, len))
347 return -EFAULT;
348 }
349 *lenp = len;
350 *ppos += len;
351 return 0;
352}
353
354static struct ctl_table cmm_table[] = {
355 {
356 .ctl_name = VM_CMM_PAGES,
357 .procname = "cmm_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800358 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .proc_handler = &cmm_pages_handler,
360 },
361 {
362 .ctl_name = VM_CMM_TIMED_PAGES,
363 .procname = "cmm_timed_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800364 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .proc_handler = &cmm_pages_handler,
366 },
367 {
368 .ctl_name = VM_CMM_TIMEOUT,
369 .procname = "cmm_timeout",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800370 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .proc_handler = &cmm_timeout_handler,
372 },
373 { .ctl_name = 0 }
374};
375
376static struct ctl_table cmm_dir_table[] = {
377 {
378 .ctl_name = CTL_VM,
379 .procname = "vm",
380 .maxlen = 0,
381 .mode = 0555,
382 .child = cmm_table,
383 },
384 { .ctl_name = 0 }
385};
386#endif
387
388#ifdef CONFIG_CMM_IUCV
389#define SMSG_PREFIX "CMM"
390static void
Martin Schwidefsky15439d72005-05-01 08:58:58 -0700391cmm_smsg_target(char *from, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700393 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Martin Schwidefsky15439d72005-05-01 08:58:58 -0700395 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
396 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
398 return;
399 if (strncmp(msg, "SHRINK", 6) == 0) {
400 if (!cmm_skip_blanks(msg + 6, &msg))
401 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700402 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 cmm_skip_blanks(msg, &msg);
404 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700405 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else if (strncmp(msg, "RELEASE", 7) == 0) {
407 if (!cmm_skip_blanks(msg + 7, &msg))
408 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700409 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 cmm_skip_blanks(msg, &msg);
411 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700412 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 } else if (strncmp(msg, "REUSE", 5) == 0) {
414 if (!cmm_skip_blanks(msg + 5, &msg))
415 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700416 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!cmm_skip_blanks(msg, &msg))
418 return;
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200419 seconds = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 cmm_skip_blanks(msg, &msg);
421 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700422 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424}
425#endif
426
427struct ctl_table_header *cmm_sysctl_header;
428
429static int
430cmm_init (void)
431{
432#ifdef CONFIG_CMM_PROC
433 cmm_sysctl_header = register_sysctl_table(cmm_dir_table, 1);
434#endif
435#ifdef CONFIG_CMM_IUCV
436 smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
437#endif
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700438 register_oom_notifier(&cmm_oom_nb);
Al Viroaaedd942006-02-01 06:29:14 -0500439 INIT_WORK(&cmm_thread_starter, (void *) cmm_start_thread, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 init_waitqueue_head(&cmm_thread_wait);
441 init_timer(&cmm_timer);
442 return 0;
443}
444
445static void
446cmm_exit(void)
447{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700448 unregister_oom_notifier(&cmm_oom_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
450 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
451#ifdef CONFIG_CMM_PROC
452 unregister_sysctl_table(cmm_sysctl_header);
453#endif
454#ifdef CONFIG_CMM_IUCV
455 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
456#endif
457}
458
459module_init(cmm_init);
460module_exit(cmm_exit);
461
462EXPORT_SYMBOL(cmm_set_pages);
463EXPORT_SYMBOL(cmm_get_pages);
464EXPORT_SYMBOL(cmm_add_timed_pages);
465EXPORT_SYMBOL(cmm_get_timed_pages);
466EXPORT_SYMBOL(cmm_set_timeout);
467
468MODULE_LICENSE("GPL");