blob: 50d8d1c887a4ef6651a0a051e3598aa5a360543a [file] [log] [blame]
Greg Kroah-Hartmanac41aae2017-11-24 15:00:35 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Collaborative memory management interface.
Heiko Carstensdb705e82010-05-26 23:27:10 +02004 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp 2003, 2010
Heiko Carstensdb705e82010-05-26 23:27:10 +02006 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/errno.h>
11#include <linux/fs.h>
12#include <linux/init.h>
13#include <linux/module.h>
Paul Gortmakerff24b072017-02-09 15:20:24 -050014#include <linux/moduleparam.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 Rientjes5a3135c22007-10-16 23:25:53 -070021#include <linux/oom.h>
Martin Schwidefsky52b169c2009-12-07 12:52:06 +010022#include <linux/suspend.h>
Heiko Carstensdb705e82010-05-26 23:27:10 +020023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/pgalloc.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020026#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Heiko Carstensa20852d2010-10-25 16:10:21 +020028#ifdef CONFIG_CMM_IUCV
29static char *cmm_default_sender = "VMRMSVM";
30#endif
31static char *sender;
Heiko Carstens447570c2005-06-21 17:16:29 -070032module_param(sender, charp, 0400);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070033MODULE_PARM_DESC(sender,
34 "Guest name that may send SMSG messages (default VMRMSVM)");
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "../../../drivers/s390/net/smsgiucv.h"
37
38#define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
39
40struct cmm_page_array {
41 struct cmm_page_array *next;
42 unsigned long index;
43 unsigned long pages[CMM_NR_PAGES];
44};
45
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070046static long cmm_pages;
47static long cmm_timed_pages;
48static volatile long cmm_pages_target;
49static volatile long cmm_timed_pages_target;
50static long cmm_timeout_pages;
51static long cmm_timeout_seconds;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +010052static int cmm_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070054static struct cmm_page_array *cmm_page_list;
55static struct cmm_page_array *cmm_timed_page_list;
56static DEFINE_SPINLOCK(cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Heiko Carstense8216de2006-09-25 23:33:11 -070058static struct task_struct *cmm_thread_ptr;
Heiko Carstens1ef6acf2010-05-26 23:26:17 +020059static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Kees Cook6cc73a02017-10-23 22:41:17 -070061static void cmm_timer_fn(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void cmm_set_timer(void);
Kees Cook6cc73a02017-10-23 22:41:17 -070063static DEFINE_TIMER(cmm_timer, cmm_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Heiko Carstensdb705e82010-05-26 23:27:10 +020065static long cmm_alloc_pages(long nr, long *counter,
66 struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070068 struct cmm_page_array *pa, *npa;
69 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070071 while (nr) {
72 addr = __get_free_page(GFP_NOIO);
73 if (!addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070075 spin_lock(&cmm_lock);
76 pa = *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!pa || pa->index >= CMM_NR_PAGES) {
78 /* Need a new page for the page list. */
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070079 spin_unlock(&cmm_lock);
80 npa = (struct cmm_page_array *)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 __get_free_page(GFP_NOIO);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070082 if (!npa) {
83 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 break;
85 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070086 spin_lock(&cmm_lock);
87 pa = *list;
88 if (!pa || pa->index >= CMM_NR_PAGES) {
89 npa->next = pa;
90 npa->index = 0;
91 pa = npa;
92 *list = pa;
93 } else
94 free_page((unsigned long) npa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Michael Holzheu83ace2702011-05-10 17:13:41 +020096 diag10_range(addr >> PAGE_SHIFT, 1);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070097 pa->pages[pa->index++] = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 (*counter)++;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070099 spin_unlock(&cmm_lock);
100 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700102 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Heiko Carstensdb705e82010-05-26 23:27:10 +0200105static long cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct cmm_page_array *pa;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700108 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700110 spin_lock(&cmm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 pa = *list;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700112 while (nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (!pa || pa->index <= 0)
114 break;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700115 addr = pa->pages[--pa->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (pa->index == 0) {
117 pa = pa->next;
118 free_page((unsigned long) *list);
119 *list = pa;
120 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700121 free_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 (*counter)--;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700123 nr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700125 spin_unlock(&cmm_lock);
126 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700129static int cmm_oom_notify(struct notifier_block *self,
130 unsigned long dummy, void *parm)
131{
132 unsigned long *freed = parm;
133 long nr = 256;
134
135 nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
136 if (nr > 0)
137 nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
138 cmm_pages_target = cmm_pages;
139 cmm_timed_pages_target = cmm_timed_pages;
140 *freed += 256 - nr;
141 return NOTIFY_OK;
142}
143
144static struct notifier_block cmm_oom_nb = {
Heiko Carstensdb705e82010-05-26 23:27:10 +0200145 .notifier_call = cmm_oom_notify,
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700146};
147
Heiko Carstensdb705e82010-05-26 23:27:10 +0200148static int cmm_thread(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int rc;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 while (1) {
153 rc = wait_event_interruptible(cmm_thread_wait,
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100154 (!cmm_suspended && (cmm_pages != cmm_pages_target ||
155 cmm_timed_pages != cmm_timed_pages_target)) ||
156 kthread_should_stop());
Heiko Carstense8216de2006-09-25 23:33:11 -0700157 if (kthread_should_stop() || rc == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 cmm_pages_target = cmm_pages;
159 cmm_timed_pages_target = cmm_timed_pages;
160 break;
161 }
162 if (cmm_pages_target > cmm_pages) {
163 if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
164 cmm_pages_target = cmm_pages;
165 } else if (cmm_pages_target < cmm_pages) {
166 cmm_free_pages(1, &cmm_pages, &cmm_page_list);
167 }
168 if (cmm_timed_pages_target > cmm_timed_pages) {
169 if (cmm_alloc_pages(1, &cmm_timed_pages,
170 &cmm_timed_page_list))
171 cmm_timed_pages_target = cmm_timed_pages;
172 } else if (cmm_timed_pages_target < cmm_timed_pages) {
173 cmm_free_pages(1, &cmm_timed_pages,
Heiko Carstensdb705e82010-05-26 23:27:10 +0200174 &cmm_timed_page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
177 cmm_set_timer();
178 }
179 return 0;
180}
181
Heiko Carstensdb705e82010-05-26 23:27:10 +0200182static void cmm_kick_thread(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 wake_up(&cmm_thread_wait);
185}
186
Heiko Carstensdb705e82010-05-26 23:27:10 +0200187static void cmm_set_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
199 add_timer(&cmm_timer);
200}
201
Kees Cook6cc73a02017-10-23 22:41:17 -0700202static void cmm_timer_fn(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700204 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700206 nr = cmm_timed_pages_target - cmm_timeout_pages;
207 if (nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 cmm_timed_pages_target = 0;
209 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700210 cmm_timed_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 cmm_kick_thread();
212 cmm_set_timer();
213}
214
Heiko Carstensdb705e82010-05-26 23:27:10 +0200215static void cmm_set_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700217 cmm_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 cmm_kick_thread();
219}
220
Heiko Carstensdb705e82010-05-26 23:27:10 +0200221static long cmm_get_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 return cmm_pages;
224}
225
Heiko Carstensdb705e82010-05-26 23:27:10 +0200226static void cmm_add_timed_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700228 cmm_timed_pages_target += nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 cmm_kick_thread();
230}
231
Heiko Carstensdb705e82010-05-26 23:27:10 +0200232static long cmm_get_timed_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 return cmm_timed_pages;
235}
236
Heiko Carstensdb705e82010-05-26 23:27:10 +0200237static void cmm_set_timeout(long nr, long seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700239 cmm_timeout_pages = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 cmm_timeout_seconds = seconds;
241 cmm_set_timer();
242}
243
Heiko Carstensdb705e82010-05-26 23:27:10 +0200244static int cmm_skip_blanks(char *cp, char **endp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 char *str;
247
Heiko Carstensdb705e82010-05-26 23:27:10 +0200248 for (str = cp; *str == ' ' || *str == '\t'; str++)
249 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *endp = str;
251 return str != cp;
252}
253
Joe Perches302bfe22013-10-22 15:29:46 -0700254static int cmm_pages_handler(struct ctl_table *ctl, int write,
255 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200257 long nr = cmm_get_pages();
258 struct ctl_table ctl_entry = {
259 .procname = ctl->procname,
260 .data = &nr,
261 .maxlen = sizeof(long),
262 };
263 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200265 rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
266 if (rc < 0 || !write)
267 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200269 cmm_set_pages(nr);
270 return 0;
271}
272
273static int cmm_timed_pages_handler(struct ctl_table *ctl, int write,
274 void __user *buffer, size_t *lenp,
275 loff_t *ppos)
276{
277 long nr = cmm_get_timed_pages();
278 struct ctl_table ctl_entry = {
279 .procname = ctl->procname,
280 .data = &nr,
281 .maxlen = sizeof(long),
282 };
283 int rc;
284
285 rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
286 if (rc < 0 || !write)
287 return rc;
288
289 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 0;
291}
292
Joe Perches302bfe22013-10-22 15:29:46 -0700293static int cmm_timeout_handler(struct ctl_table *ctl, int write,
294 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 char buf[64], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700297 long nr, seconds;
Heiko Carstens041058a2013-10-14 13:58:10 +0200298 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (!*lenp || (*ppos && !write)) {
301 *lenp = 0;
302 return 0;
303 }
304
305 if (write) {
306 len = *lenp;
307 if (copy_from_user(buf, buffer,
308 len > sizeof(buf) ? sizeof(buf) : len))
309 return -EFAULT;
310 buf[sizeof(buf) - 1] = '\0';
311 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700312 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 cmm_skip_blanks(p, &p);
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200314 seconds = simple_strtoul(p, &p, 0);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700315 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 } else {
317 len = sprintf(buf, "%ld %ld\n",
318 cmm_timeout_pages, cmm_timeout_seconds);
319 if (len > *lenp)
320 len = *lenp;
321 if (copy_to_user(buffer, buf, len))
322 return -EFAULT;
323 }
324 *lenp = len;
325 *ppos += len;
326 return 0;
327}
328
329static struct ctl_table cmm_table[] = {
330 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 .procname = "cmm_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800332 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800333 .proc_handler = cmm_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 },
335 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .procname = "cmm_timed_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800337 .mode = 0644,
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200338 .proc_handler = cmm_timed_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 },
340 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 .procname = "cmm_timeout",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800342 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800343 .proc_handler = cmm_timeout_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700345 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
348static struct ctl_table cmm_dir_table[] = {
349 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .procname = "vm",
351 .maxlen = 0,
352 .mode = 0555,
353 .child = cmm_table,
354 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700355 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358#ifdef CONFIG_CMM_IUCV
359#define SMSG_PREFIX "CMM"
Heiko Carstensdb705e82010-05-26 23:27:10 +0200360static void cmm_smsg_target(const char *from, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700362 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Martin Schwidefsky15439d72005-05-01 08:58:58 -0700364 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
365 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
367 return;
368 if (strncmp(msg, "SHRINK", 6) == 0) {
369 if (!cmm_skip_blanks(msg + 6, &msg))
370 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700371 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 cmm_skip_blanks(msg, &msg);
373 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700374 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else if (strncmp(msg, "RELEASE", 7) == 0) {
376 if (!cmm_skip_blanks(msg + 7, &msg))
377 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700378 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 cmm_skip_blanks(msg, &msg);
380 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700381 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 } else if (strncmp(msg, "REUSE", 5) == 0) {
383 if (!cmm_skip_blanks(msg + 5, &msg))
384 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700385 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!cmm_skip_blanks(msg, &msg))
387 return;
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200388 seconds = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 cmm_skip_blanks(msg, &msg);
390 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700391 cmm_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393}
394#endif
395
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100396static struct ctl_table_header *cmm_sysctl_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100398static int cmm_suspend(void)
399{
400 cmm_suspended = 1;
401 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
402 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
403 return 0;
404}
405
406static int cmm_resume(void)
407{
408 cmm_suspended = 0;
409 cmm_kick_thread();
410 return 0;
411}
412
413static int cmm_power_event(struct notifier_block *this,
414 unsigned long event, void *ptr)
415{
416 switch (event) {
417 case PM_POST_HIBERNATION:
418 return cmm_resume();
419 case PM_HIBERNATION_PREPARE:
420 return cmm_suspend();
421 default:
422 return NOTIFY_DONE;
423 }
424}
425
426static struct notifier_block cmm_power_notifier = {
427 .notifier_call = cmm_power_event,
428};
429
Heiko Carstens2e85ba52010-08-09 18:12:55 +0200430static int __init cmm_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Heiko Carstense8216de2006-09-25 23:33:11 -0700432 int rc = -ENOMEM;
433
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800434 cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
Heiko Carstense8216de2006-09-25 23:33:11 -0700435 if (!cmm_sysctl_header)
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100436 goto out_sysctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#ifdef CONFIG_CMM_IUCV
Hendrik Brueckner41b42872010-08-09 18:12:56 +0200438 /* convert sender to uppercase characters */
439 if (sender) {
440 int len = strlen(sender);
441 while (len--)
442 sender[len] = toupper(sender[len]);
Heiko Carstensa20852d2010-10-25 16:10:21 +0200443 } else {
444 sender = cmm_default_sender;
Hendrik Brueckner41b42872010-08-09 18:12:56 +0200445 }
446
Heiko Carstense8216de2006-09-25 23:33:11 -0700447 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
448 if (rc < 0)
449 goto out_smsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700451 rc = register_oom_notifier(&cmm_oom_nb);
452 if (rc < 0)
453 goto out_oom_notify;
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100454 rc = register_pm_notifier(&cmm_power_notifier);
455 if (rc)
456 goto out_pm;
Heiko Carstense8216de2006-09-25 23:33:11 -0700457 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
Alexandru Gheorghiu8fe853f2013-03-13 21:46:08 +0200458 if (!IS_ERR(cmm_thread_ptr))
459 return 0;
Heiko Carstense8216de2006-09-25 23:33:11 -0700460
Alexandru Gheorghiu8fe853f2013-03-13 21:46:08 +0200461 rc = PTR_ERR(cmm_thread_ptr);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100462 unregister_pm_notifier(&cmm_power_notifier);
463out_pm:
464 unregister_oom_notifier(&cmm_oom_nb);
Heiko Carstense8216de2006-09-25 23:33:11 -0700465out_oom_notify:
466#ifdef CONFIG_CMM_IUCV
467 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
468out_smsg:
469#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700470 unregister_sysctl_table(cmm_sysctl_header);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100471out_sysctl:
Heiko Carstens1ef6acf2010-05-26 23:26:17 +0200472 del_timer_sync(&cmm_timer);
Heiko Carstense8216de2006-09-25 23:33:11 -0700473 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
Heiko Carstensdb705e82010-05-26 23:27:10 +0200475module_init(cmm_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Heiko Carstens2e85ba52010-08-09 18:12:55 +0200477static void __exit cmm_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unregister_sysctl_table(cmm_sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480#ifdef CONFIG_CMM_IUCV
481 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
482#endif
Heiko Carstens1ef6acf2010-05-26 23:26:17 +0200483 unregister_pm_notifier(&cmm_power_notifier);
484 unregister_oom_notifier(&cmm_oom_nb);
485 kthread_stop(cmm_thread_ptr);
486 del_timer_sync(&cmm_timer);
487 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
488 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490module_exit(cmm_exit);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492MODULE_LICENSE("GPL");