blob: f87b34731e1d9e377de37c30adfcbc50dec18477 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sched.h>
17#include <linux/sysctl.h>
18#include <linux/ctype.h>
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070019#include <linux/swap.h>
Heiko Carstense8216de2006-09-25 23:33:11 -070020#include <linux/kthread.h>
David Rientjes5a3135c2007-10-16 23:25:53 -070021#include <linux/oom.h>
Martin Schwidefsky52b169c2009-12-07 12:52:06 +010022#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/pgalloc.h>
25#include <asm/uaccess.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020026#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Martin Schwidefsky15439d72005-05-01 08:58:58 -070028static char *sender = "VMRMSVM";
Heiko Carstens447570c2005-06-21 17:16:29 -070029module_param(sender, charp, 0400);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070030MODULE_PARM_DESC(sender,
31 "Guest name that may send SMSG messages (default VMRMSVM)");
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "../../../drivers/s390/net/smsgiucv.h"
34
35#define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
36
37struct cmm_page_array {
38 struct cmm_page_array *next;
39 unsigned long index;
40 unsigned long pages[CMM_NR_PAGES];
41};
42
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070043static long cmm_pages;
44static long cmm_timed_pages;
45static volatile long cmm_pages_target;
46static volatile long cmm_timed_pages_target;
47static long cmm_timeout_pages;
48static long cmm_timeout_seconds;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +010049static int cmm_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070051static struct cmm_page_array *cmm_page_list;
52static struct cmm_page_array *cmm_timed_page_list;
53static DEFINE_SPINLOCK(cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Heiko Carstense8216de2006-09-25 23:33:11 -070055static struct task_struct *cmm_thread_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static wait_queue_head_t cmm_thread_wait;
57static struct timer_list cmm_timer;
58
59static void cmm_timer_fn(unsigned long);
60static void cmm_set_timer(void);
61
62static long
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070063cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070065 struct cmm_page_array *pa, *npa;
66 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070068 while (nr) {
69 addr = __get_free_page(GFP_NOIO);
70 if (!addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070072 spin_lock(&cmm_lock);
73 pa = *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (!pa || pa->index >= CMM_NR_PAGES) {
75 /* Need a new page for the page list. */
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070076 spin_unlock(&cmm_lock);
77 npa = (struct cmm_page_array *)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 __get_free_page(GFP_NOIO);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070079 if (!npa) {
80 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 break;
82 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070083 spin_lock(&cmm_lock);
84 pa = *list;
85 if (!pa || pa->index >= CMM_NR_PAGES) {
86 npa->next = pa;
87 npa->index = 0;
88 pa = npa;
89 *list = pa;
90 } else
91 free_page((unsigned long) npa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070093 diag10(addr);
94 pa->pages[pa->index++] = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 (*counter)++;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070096 spin_unlock(&cmm_lock);
97 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070099 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700102static long
103cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct cmm_page_array *pa;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700106 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700108 spin_lock(&cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 pa = *list;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700110 while (nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (!pa || pa->index <= 0)
112 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700113 addr = pa->pages[--pa->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (pa->index == 0) {
115 pa = pa->next;
116 free_page((unsigned long) *list);
117 *list = pa;
118 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700119 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 (*counter)--;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700121 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700123 spin_unlock(&cmm_lock);
124 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700127static int cmm_oom_notify(struct notifier_block *self,
128 unsigned long dummy, void *parm)
129{
130 unsigned long *freed = parm;
131 long nr = 256;
132
133 nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
134 if (nr > 0)
135 nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
136 cmm_pages_target = cmm_pages;
137 cmm_timed_pages_target = cmm_timed_pages;
138 *freed += 256 - nr;
139 return NOTIFY_OK;
140}
141
142static struct notifier_block cmm_oom_nb = {
143 .notifier_call = cmm_oom_notify
144};
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static int
147cmm_thread(void *dummy)
148{
149 int rc;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 while (1) {
152 rc = wait_event_interruptible(cmm_thread_wait,
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100153 (!cmm_suspended && (cmm_pages != cmm_pages_target ||
154 cmm_timed_pages != cmm_timed_pages_target)) ||
155 kthread_should_stop());
Heiko Carstense8216de2006-09-25 23:33:11 -0700156 if (kthread_should_stop() || rc == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 cmm_pages_target = cmm_pages;
158 cmm_timed_pages_target = cmm_timed_pages;
159 break;
160 }
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);
166 }
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);
174 }
175 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
176 cmm_set_timer();
177 }
178 return 0;
179}
180
181static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182cmm_kick_thread(void)
183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 wake_up(&cmm_thread_wait);
185}
186
187static void
188cmm_set_timer(void)
189{
190 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
191 if (timer_pending(&cmm_timer))
192 del_timer(&cmm_timer);
193 return;
194 }
195 if (timer_pending(&cmm_timer)) {
196 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
197 return;
198 }
199 cmm_timer.function = cmm_timer_fn;
200 cmm_timer.data = 0;
201 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
202 add_timer(&cmm_timer);
203}
204
205static void
206cmm_timer_fn(unsigned long ignored)
207{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700208 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700210 nr = cmm_timed_pages_target - cmm_timeout_pages;
211 if (nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 cmm_timed_pages_target = 0;
213 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700214 cmm_timed_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 cmm_kick_thread();
216 cmm_set_timer();
217}
218
219void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700220cmm_set_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700222 cmm_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 cmm_kick_thread();
224}
225
226long
227cmm_get_pages(void)
228{
229 return cmm_pages;
230}
231
232void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700233cmm_add_timed_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700235 cmm_timed_pages_target += nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 cmm_kick_thread();
237}
238
239long
240cmm_get_timed_pages(void)
241{
242 return cmm_timed_pages;
243}
244
245void
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700246cmm_set_timeout(long nr, long seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700248 cmm_timeout_pages = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 cmm_timeout_seconds = seconds;
250 cmm_set_timer();
251}
252
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100253static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254cmm_skip_blanks(char *cp, char **endp)
255{
256 char *str;
257
258 for (str = cp; *str == ' ' || *str == '\t'; str++);
259 *endp = str;
260 return str != cp;
261}
262
263#ifdef CONFIG_CMM_PROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265static struct ctl_table cmm_table[];
266
267static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700268cmm_pages_handler(ctl_table *ctl, int write,
Al Viroaaedd942006-02-01 06:29:14 -0500269 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 char buf[16], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700272 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int len;
274
275 if (!*lenp || (*ppos && !write)) {
276 *lenp = 0;
277 return 0;
278 }
279
280 if (write) {
281 len = *lenp;
282 if (copy_from_user(buf, buffer,
283 len > sizeof(buf) ? sizeof(buf) : len))
284 return -EFAULT;
285 buf[sizeof(buf) - 1] = '\0';
286 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700287 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700289 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700291 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 } else {
293 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700294 nr = cmm_get_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700296 nr = cmm_get_timed_pages();
297 len = sprintf(buf, "%ld\n", nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (len > *lenp)
299 len = *lenp;
300 if (copy_to_user(buffer, buf, len))
301 return -EFAULT;
302 }
303 *lenp = len;
304 *ppos += len;
305 return 0;
306}
307
308static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700309cmm_timeout_handler(ctl_table *ctl, int write,
Al Viroaaedd942006-02-01 06:29:14 -0500310 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 char buf[64], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700313 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int len;
315
316 if (!*lenp || (*ppos && !write)) {
317 *lenp = 0;
318 return 0;
319 }
320
321 if (write) {
322 len = *lenp;
323 if (copy_from_user(buf, buffer,
324 len > sizeof(buf) ? sizeof(buf) : len))
325 return -EFAULT;
326 buf[sizeof(buf) - 1] = '\0';
327 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700328 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 cmm_skip_blanks(p, &p);
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200330 seconds = simple_strtoul(p, &p, 0);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700331 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 } else {
333 len = sprintf(buf, "%ld %ld\n",
334 cmm_timeout_pages, cmm_timeout_seconds);
335 if (len > *lenp)
336 len = *lenp;
337 if (copy_to_user(buffer, buf, len))
338 return -EFAULT;
339 }
340 *lenp = len;
341 *ppos += len;
342 return 0;
343}
344
345static struct ctl_table cmm_table[] = {
346 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .procname = "cmm_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800348 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800349 .proc_handler = cmm_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 },
351 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 .procname = "cmm_timed_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800353 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800354 .proc_handler = cmm_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 },
356 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .procname = "cmm_timeout",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800358 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800359 .proc_handler = cmm_timeout_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700361 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364static struct ctl_table cmm_dir_table[] = {
365 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 .procname = "vm",
367 .maxlen = 0,
368 .mode = 0555,
369 .child = cmm_table,
370 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700371 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373#endif
374
375#ifdef CONFIG_CMM_IUCV
376#define SMSG_PREFIX "CMM"
377static void
Hendrik Brueckner09003ed2010-03-08 12:26:25 +0100378cmm_smsg_target(const 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");