blob: 1141c8d5c0d038a23ca3ecc2ced56e7b63a3c47a [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>
Heiko Carstensdb705e82010-05-26 23:27:10 +020022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020024#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Heiko Carstensa20852d2010-10-25 16:10:21 +020026#ifdef CONFIG_CMM_IUCV
27static char *cmm_default_sender = "VMRMSVM";
28#endif
29static char *sender;
Heiko Carstens447570c2005-06-21 17:16:29 -070030module_param(sender, charp, 0400);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070031MODULE_PARM_DESC(sender,
32 "Guest name that may send SMSG messages (default VMRMSVM)");
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "../../../drivers/s390/net/smsgiucv.h"
35
36#define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
37
38struct cmm_page_array {
39 struct cmm_page_array *next;
40 unsigned long index;
41 unsigned long pages[CMM_NR_PAGES];
42};
43
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070044static long cmm_pages;
45static long cmm_timed_pages;
46static volatile long cmm_pages_target;
47static volatile long cmm_timed_pages_target;
48static long cmm_timeout_pages;
49static long cmm_timeout_seconds;
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;
Heiko Carstens1ef6acf2010-05-26 23:26:17 +020056static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Kees Cook6cc73a02017-10-23 22:41:17 -070058static void cmm_timer_fn(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void cmm_set_timer(void);
Kees Cook6cc73a02017-10-23 22:41:17 -070060static DEFINE_TIMER(cmm_timer, cmm_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Heiko Carstensdb705e82010-05-26 23:27:10 +020062static long cmm_alloc_pages(long nr, long *counter,
63 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 }
Michael Holzheu83ace2702011-05-10 17:13:41 +020093 diag10_range(addr >> PAGE_SHIFT, 1);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -070094 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
Heiko Carstensdb705e82010-05-26 23:27:10 +0200102static long cmm_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 = {
Heiko Carstensdb705e82010-05-26 23:27:10 +0200142 .notifier_call = cmm_oom_notify,
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700143};
144
Heiko Carstensdb705e82010-05-26 23:27:10 +0200145static int cmm_thread(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 int rc;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 while (1) {
150 rc = wait_event_interruptible(cmm_thread_wait,
Heiko Carstens39421622020-03-18 20:55:24 +0100151 cmm_pages != cmm_pages_target ||
152 cmm_timed_pages != cmm_timed_pages_target ||
153 kthread_should_stop());
Heiko Carstense8216de2006-09-25 23:33:11 -0700154 if (kthread_should_stop() || rc == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 cmm_pages_target = cmm_pages;
156 cmm_timed_pages_target = cmm_timed_pages;
157 break;
158 }
159 if (cmm_pages_target > cmm_pages) {
160 if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
161 cmm_pages_target = cmm_pages;
162 } else if (cmm_pages_target < cmm_pages) {
163 cmm_free_pages(1, &cmm_pages, &cmm_page_list);
164 }
165 if (cmm_timed_pages_target > cmm_timed_pages) {
166 if (cmm_alloc_pages(1, &cmm_timed_pages,
167 &cmm_timed_page_list))
168 cmm_timed_pages_target = cmm_timed_pages;
169 } else if (cmm_timed_pages_target < cmm_timed_pages) {
170 cmm_free_pages(1, &cmm_timed_pages,
Heiko Carstensdb705e82010-05-26 23:27:10 +0200171 &cmm_timed_page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
174 cmm_set_timer();
175 }
176 return 0;
177}
178
Heiko Carstensdb705e82010-05-26 23:27:10 +0200179static void cmm_kick_thread(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 wake_up(&cmm_thread_wait);
182}
183
Heiko Carstensdb705e82010-05-26 23:27:10 +0200184static void cmm_set_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
187 if (timer_pending(&cmm_timer))
188 del_timer(&cmm_timer);
189 return;
190 }
Sven Schnelle0188d082020-06-12 13:06:07 +0200191 mod_timer(&cmm_timer, jiffies + msecs_to_jiffies(cmm_timeout_seconds * MSEC_PER_SEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Kees Cook6cc73a02017-10-23 22:41:17 -0700194static void cmm_timer_fn(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700196 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700198 nr = cmm_timed_pages_target - cmm_timeout_pages;
199 if (nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 cmm_timed_pages_target = 0;
201 else
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700202 cmm_timed_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 cmm_kick_thread();
204 cmm_set_timer();
205}
206
Heiko Carstensdb705e82010-05-26 23:27:10 +0200207static void cmm_set_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700209 cmm_pages_target = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 cmm_kick_thread();
211}
212
Heiko Carstensdb705e82010-05-26 23:27:10 +0200213static long cmm_get_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 return cmm_pages;
216}
217
Heiko Carstensdb705e82010-05-26 23:27:10 +0200218static void cmm_add_timed_pages(long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700220 cmm_timed_pages_target += nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 cmm_kick_thread();
222}
223
Heiko Carstensdb705e82010-05-26 23:27:10 +0200224static long cmm_get_timed_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 return cmm_timed_pages;
227}
228
Heiko Carstensdb705e82010-05-26 23:27:10 +0200229static void cmm_set_timeout(long nr, long seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700231 cmm_timeout_pages = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cmm_timeout_seconds = seconds;
233 cmm_set_timer();
234}
235
Heiko Carstensdb705e82010-05-26 23:27:10 +0200236static int cmm_skip_blanks(char *cp, char **endp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 char *str;
239
Heiko Carstensdb705e82010-05-26 23:27:10 +0200240 for (str = cp; *str == ' ' || *str == '\t'; str++)
241 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *endp = str;
243 return str != cp;
244}
245
Joe Perches302bfe22013-10-22 15:29:46 -0700246static int cmm_pages_handler(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200247 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200249 long nr = cmm_get_pages();
250 struct ctl_table ctl_entry = {
251 .procname = ctl->procname,
252 .data = &nr,
253 .maxlen = sizeof(long),
254 };
255 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200257 rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
258 if (rc < 0 || !write)
259 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200261 cmm_set_pages(nr);
262 return 0;
263}
264
265static int cmm_timed_pages_handler(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200266 void *buffer, size_t *lenp,
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200267 loff_t *ppos)
268{
269 long nr = cmm_get_timed_pages();
270 struct ctl_table ctl_entry = {
271 .procname = ctl->procname,
272 .data = &nr,
273 .maxlen = sizeof(long),
274 };
275 int rc;
276
277 rc = proc_doulongvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
278 if (rc < 0 || !write)
279 return rc;
280
281 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
284
Joe Perches302bfe22013-10-22 15:29:46 -0700285static int cmm_timeout_handler(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200286 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 char buf[64], *p;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700289 long nr, seconds;
Heiko Carstens041058a2013-10-14 13:58:10 +0200290 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (!*lenp || (*ppos && !write)) {
293 *lenp = 0;
294 return 0;
295 }
296
297 if (write) {
Yihui ZENGb8e51a62019-10-25 12:31:48 +0300298 len = min(*lenp, sizeof(buf));
Christoph Hellwig32927392020-04-24 08:43:38 +0200299 memcpy(buf, buffer, len);
Yihui ZENGb8e51a62019-10-25 12:31:48 +0300300 buf[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 cmm_skip_blanks(buf, &p);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700302 nr = simple_strtoul(p, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 cmm_skip_blanks(p, &p);
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200304 seconds = simple_strtoul(p, &p, 0);
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700305 cmm_set_timeout(nr, seconds);
Yihui ZENGb8e51a62019-10-25 12:31:48 +0300306 *ppos += *lenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 } else {
308 len = sprintf(buf, "%ld %ld\n",
309 cmm_timeout_pages, cmm_timeout_seconds);
310 if (len > *lenp)
311 len = *lenp;
Christoph Hellwig32927392020-04-24 08:43:38 +0200312 memcpy(buffer, buf, len);
Yihui ZENGb8e51a62019-10-25 12:31:48 +0300313 *lenp = len;
314 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319static struct ctl_table cmm_table[] = {
320 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 .procname = "cmm_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800322 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800323 .proc_handler = cmm_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 },
325 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .procname = "cmm_timed_pages",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800327 .mode = 0644,
Vasily Gorbik71e33a12018-07-01 15:56:28 +0200328 .proc_handler = cmm_timed_pages_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 },
330 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 .procname = "cmm_timeout",
Martin Schwidefsky5e8b1c42006-03-24 03:15:16 -0800332 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800333 .proc_handler = cmm_timeout_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700335 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336};
337
338static struct ctl_table cmm_dir_table[] = {
339 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .procname = "vm",
341 .maxlen = 0,
342 .mode = 0555,
343 .child = cmm_table,
344 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700345 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348#ifdef CONFIG_CMM_IUCV
349#define SMSG_PREFIX "CMM"
Heiko Carstensdb705e82010-05-26 23:27:10 +0200350static void cmm_smsg_target(const char *from, char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700352 long nr, seconds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Martin Schwidefsky15439d72005-05-01 08:58:58 -0700354 if (strlen(sender) > 0 && strcmp(from, sender) != 0)
355 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
357 return;
358 if (strncmp(msg, "SHRINK", 6) == 0) {
359 if (!cmm_skip_blanks(msg + 6, &msg))
360 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700361 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 cmm_skip_blanks(msg, &msg);
363 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700364 cmm_set_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } else if (strncmp(msg, "RELEASE", 7) == 0) {
366 if (!cmm_skip_blanks(msg + 7, &msg))
367 return;
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700368 nr = simple_strtoul(msg, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 cmm_skip_blanks(msg, &msg);
370 if (*msg == '\0')
Martin Schwidefsky8bc719d2006-09-25 23:31:20 -0700371 cmm_add_timed_pages(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 } else if (strncmp(msg, "REUSE", 5) == 0) {
373 if (!cmm_skip_blanks(msg + 5, &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 if (!cmm_skip_blanks(msg, &msg))
377 return;
Heiko Carstens7d5d6882006-09-20 15:59:00 +0200378 seconds = 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_set_timeout(nr, seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383}
384#endif
385
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100386static struct ctl_table_header *cmm_sysctl_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Heiko Carstens2e85ba52010-08-09 18:12:55 +0200388static int __init cmm_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Heiko Carstense8216de2006-09-25 23:33:11 -0700390 int rc = -ENOMEM;
391
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800392 cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
Heiko Carstense8216de2006-09-25 23:33:11 -0700393 if (!cmm_sysctl_header)
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100394 goto out_sysctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#ifdef CONFIG_CMM_IUCV
Hendrik Brueckner41b42872010-08-09 18:12:56 +0200396 /* convert sender to uppercase characters */
397 if (sender) {
398 int len = strlen(sender);
399 while (len--)
400 sender[len] = toupper(sender[len]);
Heiko Carstensa20852d2010-10-25 16:10:21 +0200401 } else {
402 sender = cmm_default_sender;
Hendrik Brueckner41b42872010-08-09 18:12:56 +0200403 }
404
Heiko Carstense8216de2006-09-25 23:33:11 -0700405 rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
406 if (rc < 0)
407 goto out_smsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700409 rc = register_oom_notifier(&cmm_oom_nb);
410 if (rc < 0)
411 goto out_oom_notify;
Heiko Carstense8216de2006-09-25 23:33:11 -0700412 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
Alexandru Gheorghiu8fe853f2013-03-13 21:46:08 +0200413 if (!IS_ERR(cmm_thread_ptr))
414 return 0;
Heiko Carstense8216de2006-09-25 23:33:11 -0700415
Alexandru Gheorghiu8fe853f2013-03-13 21:46:08 +0200416 rc = PTR_ERR(cmm_thread_ptr);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100417 unregister_oom_notifier(&cmm_oom_nb);
Heiko Carstense8216de2006-09-25 23:33:11 -0700418out_oom_notify:
419#ifdef CONFIG_CMM_IUCV
420 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
421out_smsg:
422#endif
Heiko Carstense8216de2006-09-25 23:33:11 -0700423 unregister_sysctl_table(cmm_sysctl_header);
Martin Schwidefsky52b169c2009-12-07 12:52:06 +0100424out_sysctl:
Heiko Carstens1ef6acf2010-05-26 23:26:17 +0200425 del_timer_sync(&cmm_timer);
Heiko Carstense8216de2006-09-25 23:33:11 -0700426 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
Heiko Carstensdb705e82010-05-26 23:27:10 +0200428module_init(cmm_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Heiko Carstens2e85ba52010-08-09 18:12:55 +0200430static void __exit cmm_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 unregister_sysctl_table(cmm_sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#ifdef CONFIG_CMM_IUCV
434 smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
435#endif
Heiko Carstens1ef6acf2010-05-26 23:26:17 +0200436 unregister_oom_notifier(&cmm_oom_nb);
437 kthread_stop(cmm_thread_ptr);
438 del_timer_sync(&cmm_timer);
439 cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
440 cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442module_exit(cmm_exit);
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444MODULE_LICENSE("GPL");