607f50ead1fd4d0c7077487bf39ca3392121effd
[sfrench/cifs-2.6.git] / arch / s390 / mm / cmm.c
1 /*
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
11 #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>
18 #include <linux/swap.h>
19 #include <linux/kthread.h>
20
21 #include <asm/pgalloc.h>
22 #include <asm/uaccess.h>
23
24 static char *sender = "VMRMSVM";
25 module_param(sender, charp, 0400);
26 MODULE_PARM_DESC(sender,
27                  "Guest name that may send SMSG messages (default VMRMSVM)");
28
29 #include "../../../drivers/s390/net/smsgiucv.h"
30
31 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
32
33 struct cmm_page_array {
34         struct cmm_page_array *next;
35         unsigned long index;
36         unsigned long pages[CMM_NR_PAGES];
37 };
38
39 static long cmm_pages;
40 static long cmm_timed_pages;
41 static volatile long cmm_pages_target;
42 static volatile long cmm_timed_pages_target;
43 static long cmm_timeout_pages;
44 static long cmm_timeout_seconds;
45
46 static struct cmm_page_array *cmm_page_list;
47 static struct cmm_page_array *cmm_timed_page_list;
48 static DEFINE_SPINLOCK(cmm_lock);
49
50 static struct task_struct *cmm_thread_ptr;
51 static wait_queue_head_t cmm_thread_wait;
52 static struct timer_list cmm_timer;
53
54 static void cmm_timer_fn(unsigned long);
55 static void cmm_set_timer(void);
56
57 static long
58 cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list)
59 {
60         struct cmm_page_array *pa, *npa;
61         unsigned long addr;
62
63         while (nr) {
64                 addr = __get_free_page(GFP_NOIO);
65                 if (!addr)
66                         break;
67                 spin_lock(&cmm_lock);
68                 pa = *list;
69                 if (!pa || pa->index >= CMM_NR_PAGES) {
70                         /* Need a new page for the page list. */
71                         spin_unlock(&cmm_lock);
72                         npa = (struct cmm_page_array *)
73                                 __get_free_page(GFP_NOIO);
74                         if (!npa) {
75                                 free_page(addr);
76                                 break;
77                         }
78                         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);
87                 }
88                 diag10(addr);
89                 pa->pages[pa->index++] = addr;
90                 (*counter)++;
91                 spin_unlock(&cmm_lock);
92                 nr--;
93         }
94         return nr;
95 }
96
97 static long
98 cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
99 {
100         struct cmm_page_array *pa;
101         unsigned long addr;
102
103         spin_lock(&cmm_lock);
104         pa = *list;
105         while (nr) {
106                 if (!pa || pa->index <= 0)
107                         break;
108                 addr = pa->pages[--pa->index];
109                 if (pa->index == 0) {
110                         pa = pa->next;
111                         free_page((unsigned long) *list);
112                         *list = pa;
113                 }
114                 free_page(addr);
115                 (*counter)--;
116                 nr--;
117         }
118         spin_unlock(&cmm_lock);
119         return nr;
120 }
121
122 static 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
137 static struct notifier_block cmm_oom_nb = {
138         .notifier_call = cmm_oom_notify
139 };
140
141 static int
142 cmm_thread(void *dummy)
143 {
144         int rc;
145
146         while (1) {
147                 rc = wait_event_interruptible(cmm_thread_wait,
148                         (cmm_pages != cmm_pages_target ||
149                          cmm_timed_pages != cmm_timed_pages_target ||
150                          kthread_should_stop()));
151                 if (kthread_should_stop() || rc == -ERESTARTSYS) {
152                         cmm_pages_target = cmm_pages;
153                         cmm_timed_pages_target = cmm_timed_pages;
154                         break;
155                 }
156                 if (cmm_pages_target > cmm_pages) {
157                         if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
158                                 cmm_pages_target = cmm_pages;
159                 } else if (cmm_pages_target < cmm_pages) {
160                         cmm_free_pages(1, &cmm_pages, &cmm_page_list);
161                 }
162                 if (cmm_timed_pages_target > cmm_timed_pages) {
163                         if (cmm_alloc_pages(1, &cmm_timed_pages,
164                                            &cmm_timed_page_list))
165                                 cmm_timed_pages_target = cmm_timed_pages;
166                 } else if (cmm_timed_pages_target < cmm_timed_pages) {
167                         cmm_free_pages(1, &cmm_timed_pages,
168                                        &cmm_timed_page_list);
169                 }
170                 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
171                         cmm_set_timer();
172         }
173         return 0;
174 }
175
176 static void
177 cmm_kick_thread(void)
178 {
179         wake_up(&cmm_thread_wait);
180 }
181
182 static void
183 cmm_set_timer(void)
184 {
185         if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
186                 if (timer_pending(&cmm_timer))
187                         del_timer(&cmm_timer);
188                 return;
189         }
190         if (timer_pending(&cmm_timer)) {
191                 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
192                         return;
193         }
194         cmm_timer.function = cmm_timer_fn;
195         cmm_timer.data = 0;
196         cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
197         add_timer(&cmm_timer);
198 }
199
200 static void
201 cmm_timer_fn(unsigned long ignored)
202 {
203         long nr;
204
205         nr = cmm_timed_pages_target - cmm_timeout_pages;
206         if (nr < 0)
207                 cmm_timed_pages_target = 0;
208         else
209                 cmm_timed_pages_target = nr;
210         cmm_kick_thread();
211         cmm_set_timer();
212 }
213
214 void
215 cmm_set_pages(long nr)
216 {
217         cmm_pages_target = nr;
218         cmm_kick_thread();
219 }
220
221 long
222 cmm_get_pages(void)
223 {
224         return cmm_pages;
225 }
226
227 void
228 cmm_add_timed_pages(long nr)
229 {
230         cmm_timed_pages_target += nr;
231         cmm_kick_thread();
232 }
233
234 long
235 cmm_get_timed_pages(void)
236 {
237         return cmm_timed_pages;
238 }
239
240 void
241 cmm_set_timeout(long nr, long seconds)
242 {
243         cmm_timeout_pages = nr;
244         cmm_timeout_seconds = seconds;
245         cmm_set_timer();
246 }
247
248 static inline int
249 cmm_skip_blanks(char *cp, char **endp)
250 {
251         char *str;
252
253         for (str = cp; *str == ' ' || *str == '\t'; str++);
254         *endp = str;
255         return str != cp;
256 }
257
258 #ifdef CONFIG_CMM_PROC
259 /* These will someday get removed. */
260 #define VM_CMM_PAGES            1111
261 #define VM_CMM_TIMED_PAGES      1112
262 #define VM_CMM_TIMEOUT          1113
263
264 static struct ctl_table cmm_table[];
265
266 static int
267 cmm_pages_handler(ctl_table *ctl, int write, struct file *filp,
268                   void __user *buffer, size_t *lenp, loff_t *ppos)
269 {
270         char buf[16], *p;
271         long nr;
272         int len;
273
274         if (!*lenp || (*ppos && !write)) {
275                 *lenp = 0;
276                 return 0;
277         }
278
279         if (write) {
280                 len = *lenp;
281                 if (copy_from_user(buf, buffer,
282                                    len > sizeof(buf) ? sizeof(buf) : len))
283                         return -EFAULT;
284                 buf[sizeof(buf) - 1] = '\0';
285                 cmm_skip_blanks(buf, &p);
286                 nr = simple_strtoul(p, &p, 0);
287                 if (ctl == &cmm_table[0])
288                         cmm_set_pages(nr);
289                 else
290                         cmm_add_timed_pages(nr);
291         } else {
292                 if (ctl == &cmm_table[0])
293                         nr = cmm_get_pages();
294                 else
295                         nr = cmm_get_timed_pages();
296                 len = sprintf(buf, "%ld\n", nr);
297                 if (len > *lenp)
298                         len = *lenp;
299                 if (copy_to_user(buffer, buf, len))
300                         return -EFAULT;
301         }
302         *lenp = len;
303         *ppos += len;
304         return 0;
305 }
306
307 static int
308 cmm_timeout_handler(ctl_table *ctl, int write, struct file *filp,
309                     void __user *buffer, size_t *lenp, loff_t *ppos)
310 {
311         char buf[64], *p;
312         long nr, seconds;
313         int len;
314
315         if (!*lenp || (*ppos && !write)) {
316                 *lenp = 0;
317                 return 0;
318         }
319
320         if (write) {
321                 len = *lenp;
322                 if (copy_from_user(buf, buffer,
323                                    len > sizeof(buf) ? sizeof(buf) : len))
324                         return -EFAULT;
325                 buf[sizeof(buf) - 1] = '\0';
326                 cmm_skip_blanks(buf, &p);
327                 nr = simple_strtoul(p, &p, 0);
328                 cmm_skip_blanks(p, &p);
329                 seconds = simple_strtoul(p, &p, 0);
330                 cmm_set_timeout(nr, seconds);
331         } else {
332                 len = sprintf(buf, "%ld %ld\n",
333                               cmm_timeout_pages, cmm_timeout_seconds);
334                 if (len > *lenp)
335                         len = *lenp;
336                 if (copy_to_user(buffer, buf, len))
337                         return -EFAULT;
338         }
339         *lenp = len;
340         *ppos += len;
341         return 0;
342 }
343
344 static struct ctl_table cmm_table[] = {
345         {
346                 .ctl_name       = VM_CMM_PAGES,
347                 .procname       = "cmm_pages",
348                 .mode           = 0644,
349                 .proc_handler   = &cmm_pages_handler,
350         },
351         {
352                 .ctl_name       = VM_CMM_TIMED_PAGES,
353                 .procname       = "cmm_timed_pages",
354                 .mode           = 0644,
355                 .proc_handler   = &cmm_pages_handler,
356         },
357         {
358                 .ctl_name       = VM_CMM_TIMEOUT,
359                 .procname       = "cmm_timeout",
360                 .mode           = 0644,
361                 .proc_handler   = &cmm_timeout_handler,
362         },
363         { .ctl_name = 0 }
364 };
365
366 static struct ctl_table cmm_dir_table[] = {
367         {
368                 .ctl_name       = CTL_VM,
369                 .procname       = "vm",
370                 .maxlen         = 0,
371                 .mode           = 0555,
372                 .child          = cmm_table,
373         },
374         { .ctl_name = 0 }
375 };
376 #endif
377
378 #ifdef CONFIG_CMM_IUCV
379 #define SMSG_PREFIX "CMM"
380 static void
381 cmm_smsg_target(char *from, char *msg)
382 {
383         long nr, seconds;
384
385         if (strlen(sender) > 0 && strcmp(from, sender) != 0)
386                 return;
387         if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
388                 return;
389         if (strncmp(msg, "SHRINK", 6) == 0) {
390                 if (!cmm_skip_blanks(msg + 6, &msg))
391                         return;
392                 nr = simple_strtoul(msg, &msg, 0);
393                 cmm_skip_blanks(msg, &msg);
394                 if (*msg == '\0')
395                         cmm_set_pages(nr);
396         } else if (strncmp(msg, "RELEASE", 7) == 0) {
397                 if (!cmm_skip_blanks(msg + 7, &msg))
398                         return;
399                 nr = simple_strtoul(msg, &msg, 0);
400                 cmm_skip_blanks(msg, &msg);
401                 if (*msg == '\0')
402                         cmm_add_timed_pages(nr);
403         } else if (strncmp(msg, "REUSE", 5) == 0) {
404                 if (!cmm_skip_blanks(msg + 5, &msg))
405                         return;
406                 nr = simple_strtoul(msg, &msg, 0);
407                 if (!cmm_skip_blanks(msg, &msg))
408                         return;
409                 seconds = simple_strtoul(msg, &msg, 0);
410                 cmm_skip_blanks(msg, &msg);
411                 if (*msg == '\0')
412                         cmm_set_timeout(nr, seconds);
413         }
414 }
415 #endif
416
417 struct ctl_table_header *cmm_sysctl_header;
418
419 static int
420 cmm_init (void)
421 {
422         int rc = -ENOMEM;
423
424 #ifdef CONFIG_CMM_PROC
425         cmm_sysctl_header = register_sysctl_table(cmm_dir_table, 1);
426         if (!cmm_sysctl_header)
427                 goto out;
428 #endif
429 #ifdef CONFIG_CMM_IUCV
430         rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
431         if (rc < 0)
432                 goto out_smsg;
433 #endif
434         rc = register_oom_notifier(&cmm_oom_nb);
435         if (rc < 0)
436                 goto out_oom_notify;
437         init_waitqueue_head(&cmm_thread_wait);
438         init_timer(&cmm_timer);
439         cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
440         rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0;
441         if (!rc)
442                 goto out;
443         /*
444          * kthread_create failed. undo all the stuff from above again.
445          */
446         unregister_oom_notifier(&cmm_oom_nb);
447
448 out_oom_notify:
449 #ifdef CONFIG_CMM_IUCV
450         smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
451 out_smsg:
452 #endif
453 #ifdef CONFIG_CMM_PROC
454         unregister_sysctl_table(cmm_sysctl_header);
455 #endif
456 out:
457         return rc;
458 }
459
460 static void
461 cmm_exit(void)
462 {
463         kthread_stop(cmm_thread_ptr);
464         unregister_oom_notifier(&cmm_oom_nb);
465         cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
466         cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
467 #ifdef CONFIG_CMM_PROC
468         unregister_sysctl_table(cmm_sysctl_header);
469 #endif
470 #ifdef CONFIG_CMM_IUCV
471         smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
472 #endif
473 }
474
475 module_init(cmm_init);
476 module_exit(cmm_exit);
477
478 EXPORT_SYMBOL(cmm_set_pages);
479 EXPORT_SYMBOL(cmm_get_pages);
480 EXPORT_SYMBOL(cmm_add_timed_pages);
481 EXPORT_SYMBOL(cmm_get_timed_pages);
482 EXPORT_SYMBOL(cmm_set_timeout);
483
484 MODULE_LICENSE("GPL");