blob: 5e5f3849660f4373dddbaef3b1daae2396438205 [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>
Heiko Carstense8216de2006-09-25 23:33:11 -070019#include <linux/kthread.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070020#include <linux/oom.h>
Martin Schwidefsky52b169c2009-12-07 12:52:06 +010021#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/pgalloc.h>
24#include <asm/uaccess.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020025#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Martin Schwidefsky15439d72005-05-01 08:58:58 -070027static char *sender = "VMRMSVM";
Heiko Carstens447570c2005-06-21 17:16:29 -070028module_param(sender, charp, 0400);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070029MODULE_PARM_DESC(sender,
30 "Guest name that may send SMSG messages (default VMRMSVM)");
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "../../../drivers/s390/net/smsgiucv.h"
33
34#define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
35
36struct cmm_page_array {
37 struct cmm_page_array *next;
38 unsigned long index;
39 unsigned long pages[CMM_NR_PAGES];
40};
41
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070042static long cmm_pages;
43static long cmm_timed_pages;
44static volatile long cmm_pages_target;
45static volatile long cmm_timed_pages_target;
46static long cmm_timeout_pages;
47static long cmm_timeout_seconds;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +010048static int cmm_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070050static struct cmm_page_array *cmm_page_list;
51static struct cmm_page_array *cmm_timed_page_list;
52static DEFINE_SPINLOCK(cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Heiko Carstense8216de2006-09-25 23:33:11 -070054static struct task_struct *cmm_thread_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static wait_queue_head_t cmm_thread_wait;
56static struct timer_list cmm_timer;
57
58static void cmm_timer_fn(unsigned long);
59static void cmm_set_timer(void);
60
61static long
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070062cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070064 struct cmm_page_array *pa, *npa;
65 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070067 while (nr) {
68 addr = __get_free_page(GFP_NOIO);
69 if (!addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070071 spin_lock(&cmm_lock);
72 pa = *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (!pa || pa->index >= CMM_NR_PAGES) {
74 /* Need a new page for the page list. */
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070075 spin_unlock(&cmm_lock);
76 npa = (struct cmm_page_array *)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 __get_free_page(GFP_NOIO);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070078 if (!npa) {
79 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 break;
81 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070082 spin_lock(&cmm_lock);
83 pa = *list;
84 if (!pa || pa->index >= CMM_NR_PAGES) {
85 npa->next = pa;
86 npa->index = 0;
87 pa = npa;
88 *list = pa;
89 } else
90 free_page((unsigned long) npa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070092 diag10(addr);
93 pa->pages[pa->index++] = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 (*counter)++;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070095 spin_unlock(&cmm_lock);
96 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070098 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700101static long
102cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct cmm_page_array *pa;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700105 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700107 spin_lock(&cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 pa = *list;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700109 while (nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (!pa || pa->index <= 0)
111 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700112 addr = pa->pages[--pa->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (pa->index == 0) {
114 pa = pa->next;
115 free_page((unsigned long) *list);
116 *list = pa;
117 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700118 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 (*counter)--;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700120 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700122 spin_unlock(&cmm_lock);
123 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700126static int cmm_oom_notify(struct notifier_block *self,
127 unsigned long dummy, void *parm)
128{
129 unsigned long *freed = parm;
130 long nr = 256;
131
132 nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
133 if (nr > 0)
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;
137 *freed += 256 - nr;
138 return NOTIFY_OK;
139}
140
141static struct notifier_block cmm_oom_nb = {
142 .notifier_call = cmm_oom_notify
143};
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static int
146cmm_thread(void *dummy)
147{
148 int rc;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 while (1) {
151 rc = wait_event_interruptible(cmm_thread_wait,
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100152 (!cmm_suspended && (cmm_pages != cmm_pages_target ||
153 cmm_timed_pages != cmm_timed_pages_target)) ||
154 kthread_should_stop());
Heiko Carstense8216de2006-09-25 23:33:11 -0700155 if (kthread_should_stop() || rc == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 cmm_pages_target = cmm_pages;
157 cmm_timed_pages_target = cmm_timed_pages;
158 break;
159 }
160 if (cmm_pages_target > cmm_pages) {
161 if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
162 cmm_pages_target = cmm_pages;
163 } else if (cmm_pages_target < cmm_pages) {
164 cmm_free_pages(1, &cmm_pages, &cmm_page_list);
165 }
166 if (cmm_timed_pages_target > cmm_timed_pages) {
167 if (cmm_alloc_pages(1, &cmm_timed_pages,
168 &cmm_timed_page_list))
169 cmm_timed_pages_target = cmm_timed_pages;
170 } else if (cmm_timed_pages_target < cmm_timed_pages) {
171 cmm_free_pages(1, &cmm_timed_pages,
172 &cmm_timed_page_list);
173 }
174 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
175 cmm_set_timer();
176 }
177 return 0;
178}
179
180static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181cmm_kick_thread(void)
182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 wake_up(&cmm_thread_wait);
184}
185
186static void
187cmm_set_timer(void)
188{
189 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
190 if (timer_pending(&cmm_timer))
191 del_timer(&cmm_timer);
192 return;
193 }
194 if (timer_pending(&cmm_timer)) {
195 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
196 return;
197 }
198 cmm_timer.function = cmm_timer_fn;
199 cmm_timer.data = 0;
200 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
201 add_timer(&cmm_timer);
202}
203
204static void
205cmm_timer_fn(unsigned long ignored)
206{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700207 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700209 nr = cmm_timed_pages_target - cmm_timeout_pages;
210 if (nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 cmm_timed_pages_target = 0;
212 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700213 cmm_timed_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 cmm_kick_thread();
215 cmm_set_timer();
216}
217
218void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700219cmm_set_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700221 cmm_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 cmm_kick_thread();
223}
224
225long
226cmm_get_pages(void)
227{
228 return cmm_pages;
229}
230
231void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700232cmm_add_timed_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700234 cmm_timed_pages_target += nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 cmm_kick_thread();
236}
237
238long
239cmm_get_timed_pages(void)
240{
241 return cmm_timed_pages;
242}
243
244void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700245cmm_set_timeout(long nr, long seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700247 cmm_timeout_pages = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 cmm_timeout_seconds = seconds;
249 cmm_set_timer();
250}
251
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100252static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253cmm_skip_blanks(char *cp, char **endp)
254{
255 char *str;
256
257 for (str = cp; *str == ' ' || *str == '\t'; str++);
258 *endp = str;
259 return str != cp;
260}
261
262#ifdef CONFIG_CMM_PROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264static struct ctl_table cmm_table[];
265
266static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700267cmm_pages_handler(ctl_table *ctl, int write,
Al Viroaaedd942006-02-01 06:29:14 -0500268 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 char buf[16], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700271 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 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);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700286 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700288 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700290 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 } else {
292 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700293 nr = cmm_get_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700295 nr = cmm_get_timed_pages();
296 len = sprintf(buf, "%ld\n", nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 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
307static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700308cmm_timeout_handler(ctl_table *ctl, int write,
Al Viroaaedd942006-02-01 06:29:14 -0500309 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 char buf[64], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700312 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 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);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700327 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 cmm_skip_blanks(p, &p);
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200329 seconds = simple_strtoul(p, &p, 0);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700330 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } 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
344static struct ctl_table cmm_table[] = {
345 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .procname = "cmm_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800347 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .proc_handler = &cmm_pages_handler,
349 },
350 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 .procname = "cmm_timed_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800352 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .proc_handler = &cmm_pages_handler,
354 },
355 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .procname = "cmm_timeout",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800357 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .proc_handler = &cmm_timeout_handler,
359 },
360 { .ctl_name = 0 }
361};
362
363static struct ctl_table cmm_dir_table[] = {
364 {
365 .ctl_name = CTL_VM,
366 .procname = "vm",
367 .maxlen = 0,
368 .mode = 0555,
369 .child = cmm_table,
370 },
371 { .ctl_name = 0 }
372};
373#endif
374
375#ifdef CONFIG_CMM_IUCV
376#define SMSG_PREFIX "CMM"
377static void
Martin Schwidefsky15439d72005-05-01 08:58:58 -0700378cmm_smsg_target(char *from, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700380 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Martin Schwidefsky15439d72005-05-01 08:58:58 -0700382 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
383 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
385 return;
386 if (strncmp(msg, "SHRINK", 6) == 0) {
387 if (!cmm_skip_blanks(msg + 6, &msg))
388 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700389 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 cmm_skip_blanks(msg, &msg);
391 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700392 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 } else if (strncmp(msg, "RELEASE", 7) == 0) {
394 if (!cmm_skip_blanks(msg + 7, &msg))
395 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700396 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 cmm_skip_blanks(msg, &msg);
398 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700399 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 } else if (strncmp(msg, "REUSE", 5) == 0) {
401 if (!cmm_skip_blanks(msg + 5, &msg))
402 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700403 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!cmm_skip_blanks(msg, &msg))
405 return;
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200406 seconds = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 cmm_skip_blanks(msg, &msg);
408 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700409 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411}
412#endif
413
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100414static struct ctl_table_header *cmm_sysctl_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100416static int cmm_suspend(void)
417{
418 cmm_suspended = 1;
419 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
420 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
421 return 0;
422}
423
424static int cmm_resume(void)
425{
426 cmm_suspended = 0;
427 cmm_kick_thread();
428 return 0;
429}
430
431static int cmm_power_event(struct notifier_block *this,
432 unsigned long event, void *ptr)
433{
434 switch (event) {
435 case PM_POST_HIBERNATION:
436 return cmm_resume();
437 case PM_HIBERNATION_PREPARE:
438 return cmm_suspend();
439 default:
440 return NOTIFY_DONE;
441 }
442}
443
444static struct notifier_block cmm_power_notifier = {
445 .notifier_call = cmm_power_event,
446};
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int
449cmm_init (void)
450{
Heiko Carstense8216de2006-09-25 23:33:11 -0700451 int rc = -ENOMEM;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453#ifdef CONFIG_CMM_PROC
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800454 cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
Heiko Carstense8216de2006-09-25 23:33:11 -0700455 if (!cmm_sysctl_header)
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100456 goto out_sysctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif
458#ifdef CONFIG_CMM_IUCV
Heiko Carstense8216de2006-09-25 23:33:11 -0700459 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
460 if (rc < 0)
461 goto out_smsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700463 rc = register_oom_notifier(&cmm_oom_nb);
464 if (rc < 0)
465 goto out_oom_notify;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100466 rc = register_pm_notifier(&cmm_power_notifier);
467 if (rc)
468 goto out_pm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 init_waitqueue_head(&cmm_thread_wait);
470 init_timer(&cmm_timer);
Heiko Carstense8216de2006-09-25 23:33:11 -0700471 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
472 rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100473 if (rc)
474 goto out_kthread;
475 return 0;
Heiko Carstense8216de2006-09-25 23:33:11 -0700476
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100477out_kthread:
478 unregister_pm_notifier(&cmm_power_notifier);
479out_pm:
480 unregister_oom_notifier(&cmm_oom_nb);
Heiko Carstense8216de2006-09-25 23:33:11 -0700481out_oom_notify:
482#ifdef CONFIG_CMM_IUCV
483 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
484out_smsg:
485#endif
486#ifdef CONFIG_CMM_PROC
487 unregister_sysctl_table(cmm_sysctl_header);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100488out_sysctl:
Heiko Carstense8216de2006-09-25 23:33:11 -0700489#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700490 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static void
494cmm_exit(void)
495{
Heiko Carstense8216de2006-09-25 23:33:11 -0700496 kthread_stop(cmm_thread_ptr);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100497 unregister_pm_notifier(&cmm_power_notifier);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700498 unregister_oom_notifier(&cmm_oom_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
500 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
501#ifdef CONFIG_CMM_PROC
502 unregister_sysctl_table(cmm_sysctl_header);
503#endif
504#ifdef CONFIG_CMM_IUCV
505 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
506#endif
507}
508
509module_init(cmm_init);
510module_exit(cmm_exit);
511
512EXPORT_SYMBOL(cmm_set_pages);
513EXPORT_SYMBOL(cmm_get_pages);
514EXPORT_SYMBOL(cmm_add_timed_pages);
515EXPORT_SYMBOL(cmm_get_timed_pages);
516EXPORT_SYMBOL(cmm_set_timeout);
517
518MODULE_LICENSE("GPL");