blob: 2dbdcd85b68f200762846ce0c59dc39438d0f9e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Collaborative memory management interface.
Heiko Carstensdb705e82010-05-26 23:27:10 +02003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp 2003, 2010
Heiko Carstensdb705e82010-05-26 23:27:10 +02005 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
10#include <linux/fs.h>
11#include <linux/init.h>
12#include <linux/module.h>
Paul Gortmakerff24b072017-02-09 15:20:24 -050013#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Heiko Carstensdb705e82010-05-26 23:27:10 +020022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/pgalloc.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020025#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Heiko Carstensa20852d2010-10-25 16:10:21 +020027#ifdef CONFIG_CMM_IUCV
28static char *cmm_default_sender = "VMRMSVM";
29#endif
30static char *sender;
Heiko Carstens447570c2005-06-21 17:16:29 -070031module_param(sender, charp, 0400);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070032MODULE_PARM_DESC(sender,
33 "Guest name that may send SMSG messages (default VMRMSVM)");
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "../../../drivers/s390/net/smsgiucv.h"
36
37#define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
38
39struct cmm_page_array {
40 struct cmm_page_array *next;
41 unsigned long index;
42 unsigned long pages[CMM_NR_PAGES];
43};
44
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070045static long cmm_pages;
46static long cmm_timed_pages;
47static volatile long cmm_pages_target;
48static volatile long cmm_timed_pages_target;
49static long cmm_timeout_pages;
50static long cmm_timeout_seconds;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +010051static int cmm_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070053static struct cmm_page_array *cmm_page_list;
54static struct cmm_page_array *cmm_timed_page_list;
55static DEFINE_SPINLOCK(cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Heiko Carstense8216de2006-09-25 23:33:11 -070057static struct task_struct *cmm_thread_ptr;
Heiko Carstens1ef6acf2010-05-26 23:26:17 +020058static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait);
Kees Cook1d27e3e2017-10-04 16:27:04 -070059static DEFINE_TIMER(cmm_timer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static void cmm_timer_fn(unsigned long);
62static void cmm_set_timer(void);
63
Heiko Carstensdb705e82010-05-26 23:27:10 +020064static long cmm_alloc_pages(long nr, long *counter,
65 struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070067 struct cmm_page_array *pa, *npa;
68 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070070 while (nr) {
71 addr = __get_free_page(GFP_NOIO);
72 if (!addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070074 spin_lock(&cmm_lock);
75 pa = *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!pa || pa->index >= CMM_NR_PAGES) {
77 /* Need a new page for the page list. */
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070078 spin_unlock(&cmm_lock);
79 npa = (struct cmm_page_array *)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 __get_free_page(GFP_NOIO);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070081 if (!npa) {
82 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 break;
84 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070085 spin_lock(&cmm_lock);
86 pa = *list;
87 if (!pa || pa->index >= CMM_NR_PAGES) {
88 npa->next = pa;
89 npa->index = 0;
90 pa = npa;
91 *list = pa;
92 } else
93 free_page((unsigned long) npa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Michael Holzheu83ace2702011-05-10 17:13:41 +020095 diag10_range(addr >> PAGE_SHIFT, 1);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070096 pa->pages[pa->index++] = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 (*counter)++;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070098 spin_unlock(&cmm_lock);
99 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700101 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Heiko Carstensdb705e82010-05-26 23:27:10 +0200104static long cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct cmm_page_array *pa;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700107 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700109 spin_lock(&cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 pa = *list;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700111 while (nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (!pa || pa->index <= 0)
113 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700114 addr = pa->pages[--pa->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (pa->index == 0) {
116 pa = pa->next;
117 free_page((unsigned long) *list);
118 *list = pa;
119 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700120 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 (*counter)--;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700122 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700124 spin_unlock(&cmm_lock);
125 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700128static int cmm_oom_notify(struct notifier_block *self,
129 unsigned long dummy, void *parm)
130{
131 unsigned long *freed = parm;
132 long nr = 256;
133
134 nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
135 if (nr > 0)
136 nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
137 cmm_pages_target = cmm_pages;
138 cmm_timed_pages_target = cmm_timed_pages;
139 *freed += 256 - nr;
140 return NOTIFY_OK;
141}
142
143static struct notifier_block cmm_oom_nb = {
Heiko Carstensdb705e82010-05-26 23:27:10 +0200144 .notifier_call = cmm_oom_notify,
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700145};
146
Heiko Carstensdb705e82010-05-26 23:27:10 +0200147static int cmm_thread(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
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,
Heiko Carstensdb705e82010-05-26 23:27:10 +0200173 &cmm_timed_page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
176 cmm_set_timer();
177 }
178 return 0;
179}
180
Heiko Carstensdb705e82010-05-26 23:27:10 +0200181static void cmm_kick_thread(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 wake_up(&cmm_thread_wait);
184}
185
Heiko Carstensdb705e82010-05-26 23:27:10 +0200186static void cmm_set_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
189 if (timer_pending(&cmm_timer))
190 del_timer(&cmm_timer);
191 return;
192 }
193 if (timer_pending(&cmm_timer)) {
194 if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
195 return;
196 }
197 cmm_timer.function = cmm_timer_fn;
198 cmm_timer.data = 0;
199 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
200 add_timer(&cmm_timer);
201}
202
Heiko Carstensdb705e82010-05-26 23:27:10 +0200203static void cmm_timer_fn(unsigned long ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700205 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700207 nr = cmm_timed_pages_target - cmm_timeout_pages;
208 if (nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 cmm_timed_pages_target = 0;
210 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700211 cmm_timed_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 cmm_kick_thread();
213 cmm_set_timer();
214}
215
Heiko Carstensdb705e82010-05-26 23:27:10 +0200216static void cmm_set_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700218 cmm_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 cmm_kick_thread();
220}
221
Heiko Carstensdb705e82010-05-26 23:27:10 +0200222static long cmm_get_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 return cmm_pages;
225}
226
Heiko Carstensdb705e82010-05-26 23:27:10 +0200227static void cmm_add_timed_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700229 cmm_timed_pages_target += nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 cmm_kick_thread();
231}
232
Heiko Carstensdb705e82010-05-26 23:27:10 +0200233static long cmm_get_timed_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 return cmm_timed_pages;
236}
237
Heiko Carstensdb705e82010-05-26 23:27:10 +0200238static void cmm_set_timeout(long nr, long seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700240 cmm_timeout_pages = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 cmm_timeout_seconds = seconds;
242 cmm_set_timer();
243}
244
Heiko Carstensdb705e82010-05-26 23:27:10 +0200245static int cmm_skip_blanks(char *cp, char **endp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 char *str;
248
Heiko Carstensdb705e82010-05-26 23:27:10 +0200249 for (str = cp; *str == ' ' || *str == '\t'; str++)
250 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *endp = str;
252 return str != cp;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static struct ctl_table cmm_table[];
256
Joe Perches302bfe22013-10-22 15:29:46 -0700257static int cmm_pages_handler(struct ctl_table *ctl, int write,
258 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 char buf[16], *p;
Heiko Carstens041058a2013-10-14 13:58:10 +0200261 unsigned int len;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700262 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (!*lenp || (*ppos && !write)) {
265 *lenp = 0;
266 return 0;
267 }
268
269 if (write) {
270 len = *lenp;
271 if (copy_from_user(buf, buffer,
272 len > sizeof(buf) ? sizeof(buf) : len))
273 return -EFAULT;
274 buf[sizeof(buf) - 1] = '\0';
275 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700276 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700278 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700280 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 } else {
282 if (ctl == &cmm_table[0])
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700283 nr = cmm_get_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700285 nr = cmm_get_timed_pages();
286 len = sprintf(buf, "%ld\n", nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (len > *lenp)
288 len = *lenp;
289 if (copy_to_user(buffer, buf, len))
290 return -EFAULT;
291 }
292 *lenp = len;
293 *ppos += len;
294 return 0;
295}
296
Joe Perches302bfe22013-10-22 15:29:46 -0700297static int cmm_timeout_handler(struct ctl_table *ctl, int write,
298 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 char buf[64], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700301 long nr, seconds;
Heiko Carstens041058a2013-10-14 13:58:10 +0200302 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (!*lenp || (*ppos && !write)) {
305 *lenp = 0;
306 return 0;
307 }
308
309 if (write) {
310 len = *lenp;
311 if (copy_from_user(buf, buffer,
312 len > sizeof(buf) ? sizeof(buf) : len))
313 return -EFAULT;
314 buf[sizeof(buf) - 1] = '\0';
315 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700316 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 cmm_skip_blanks(p, &p);
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200318 seconds = simple_strtoul(p, &p, 0);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700319 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 } else {
321 len = sprintf(buf, "%ld %ld\n",
322 cmm_timeout_pages, cmm_timeout_seconds);
323 if (len > *lenp)
324 len = *lenp;
325 if (copy_to_user(buffer, buf, len))
326 return -EFAULT;
327 }
328 *lenp = len;
329 *ppos += len;
330 return 0;
331}
332
333static struct ctl_table cmm_table[] = {
334 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 .procname = "cmm_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800336 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800337 .proc_handler = cmm_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 },
339 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .procname = "cmm_timed_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800341 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800342 .proc_handler = cmm_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 },
344 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 .procname = "cmm_timeout",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800346 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800347 .proc_handler = cmm_timeout_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700349 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
352static struct ctl_table cmm_dir_table[] = {
353 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .procname = "vm",
355 .maxlen = 0,
356 .mode = 0555,
357 .child = cmm_table,
358 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700359 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362#ifdef CONFIG_CMM_IUCV
363#define SMSG_PREFIX "CMM"
Heiko Carstensdb705e82010-05-26 23:27:10 +0200364static void cmm_smsg_target(const char *from, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700366 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Martin Schwidefsky15439d72005-05-01 08:58:58 -0700368 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
369 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
371 return;
372 if (strncmp(msg, "SHRINK", 6) == 0) {
373 if (!cmm_skip_blanks(msg + 6, &msg))
374 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700375 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 cmm_skip_blanks(msg, &msg);
377 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700378 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else if (strncmp(msg, "RELEASE", 7) == 0) {
380 if (!cmm_skip_blanks(msg + 7, &msg))
381 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700382 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 cmm_skip_blanks(msg, &msg);
384 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700385 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 } else if (strncmp(msg, "REUSE", 5) == 0) {
387 if (!cmm_skip_blanks(msg + 5, &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 if (!cmm_skip_blanks(msg, &msg))
391 return;
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200392 seconds = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 cmm_skip_blanks(msg, &msg);
394 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700395 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397}
398#endif
399
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100400static struct ctl_table_header *cmm_sysctl_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100402static int cmm_suspend(void)
403{
404 cmm_suspended = 1;
405 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
406 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
407 return 0;
408}
409
410static int cmm_resume(void)
411{
412 cmm_suspended = 0;
413 cmm_kick_thread();
414 return 0;
415}
416
417static int cmm_power_event(struct notifier_block *this,
418 unsigned long event, void *ptr)
419{
420 switch (event) {
421 case PM_POST_HIBERNATION:
422 return cmm_resume();
423 case PM_HIBERNATION_PREPARE:
424 return cmm_suspend();
425 default:
426 return NOTIFY_DONE;
427 }
428}
429
430static struct notifier_block cmm_power_notifier = {
431 .notifier_call = cmm_power_event,
432};
433
Heiko Carstens2e85ba52010-08-09 18:12:55 +0200434static int __init cmm_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Heiko Carstense8216de2006-09-25 23:33:11 -0700436 int rc = -ENOMEM;
437
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800438 cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
Heiko Carstense8216de2006-09-25 23:33:11 -0700439 if (!cmm_sysctl_header)
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100440 goto out_sysctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#ifdef CONFIG_CMM_IUCV
Hendrik Brueckner41b42872010-08-09 18:12:56 +0200442 /* convert sender to uppercase characters */
443 if (sender) {
444 int len = strlen(sender);
445 while (len--)
446 sender[len] = toupper(sender[len]);
Heiko Carstensa20852d2010-10-25 16:10:21 +0200447 } else {
448 sender = cmm_default_sender;
Hendrik Brueckner41b42872010-08-09 18:12:56 +0200449 }
450
Heiko Carstense8216de2006-09-25 23:33:11 -0700451 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
452 if (rc < 0)
453 goto out_smsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700455 rc = register_oom_notifier(&cmm_oom_nb);
456 if (rc < 0)
457 goto out_oom_notify;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100458 rc = register_pm_notifier(&cmm_power_notifier);
459 if (rc)
460 goto out_pm;
Heiko Carstense8216de2006-09-25 23:33:11 -0700461 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
Alexandru Gheorghiu8fe853f2013-03-13 21:46:08 +0200462 if (!IS_ERR(cmm_thread_ptr))
463 return 0;
Heiko Carstense8216de2006-09-25 23:33:11 -0700464
Alexandru Gheorghiu8fe853f2013-03-13 21:46:08 +0200465 rc = PTR_ERR(cmm_thread_ptr);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100466 unregister_pm_notifier(&cmm_power_notifier);
467out_pm:
468 unregister_oom_notifier(&cmm_oom_nb);
Heiko Carstense8216de2006-09-25 23:33:11 -0700469out_oom_notify:
470#ifdef CONFIG_CMM_IUCV
471 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
472out_smsg:
473#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700474 unregister_sysctl_table(cmm_sysctl_header);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100475out_sysctl:
Heiko Carstens1ef6acf2010-05-26 23:26:17 +0200476 del_timer_sync(&cmm_timer);
Heiko Carstense8216de2006-09-25 23:33:11 -0700477 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
Heiko Carstensdb705e82010-05-26 23:27:10 +0200479module_init(cmm_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Heiko Carstens2e85ba52010-08-09 18:12:55 +0200481static void __exit cmm_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unregister_sysctl_table(cmm_sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#ifdef CONFIG_CMM_IUCV
485 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
486#endif
Heiko Carstens1ef6acf2010-05-26 23:26:17 +0200487 unregister_pm_notifier(&cmm_power_notifier);
488 unregister_oom_notifier(&cmm_oom_nb);
489 kthread_stop(cmm_thread_ptr);
490 del_timer_sync(&cmm_timer);
491 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
492 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494module_exit(cmm_exit);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496MODULE_LICENSE("GPL");