1 /*
2  * Collaborative memory management interface.
3  *
4  * Copyright (C) 2008 IBM Corporation
5  * Author(s): Brian King (brking@linux.vnet.ibm.com),
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 
23 #include <linux/ctype.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/fs.h>
27 #include <linux/init.h>
28 #include <linux/kthread.h>
29 #include <linux/module.h>
30 #include <linux/oom.h>
31 #include <linux/reboot.h>
32 #include <linux/sched.h>
33 #include <linux/stringify.h>
34 #include <linux/swap.h>
35 #include <linux/sysdev.h>
36 #include <asm/firmware.h>
37 #include <asm/hvcall.h>
38 #include <asm/mmu.h>
39 #include <asm/pgalloc.h>
40 #include <asm/uaccess.h>
41 #include <linux/memory.h>
42 
43 #include "plpar_wrappers.h"
44 
45 #define CMM_DRIVER_VERSION	"1.0.0"
46 #define CMM_DEFAULT_DELAY	1
47 #define CMM_HOTPLUG_DELAY	5
48 #define CMM_DEBUG			0
49 #define CMM_DISABLE		0
50 #define CMM_OOM_KB		1024
51 #define CMM_MIN_MEM_MB		256
52 #define KB2PAGES(_p)		((_p)>>(PAGE_SHIFT-10))
53 #define PAGES2KB(_p)		((_p)<<(PAGE_SHIFT-10))
54 /*
55  * The priority level tries to ensure that this notifier is called as
56  * late as possible to reduce thrashing in the shared memory pool.
57  */
58 #define CMM_MEM_HOTPLUG_PRI	1
59 #define CMM_MEM_ISOLATE_PRI	15
60 
61 static unsigned int delay = CMM_DEFAULT_DELAY;
62 static unsigned int hotplug_delay = CMM_HOTPLUG_DELAY;
63 static unsigned int oom_kb = CMM_OOM_KB;
64 static unsigned int cmm_debug = CMM_DEBUG;
65 static unsigned int cmm_disabled = CMM_DISABLE;
66 static unsigned long min_mem_mb = CMM_MIN_MEM_MB;
67 static struct sys_device cmm_sysdev;
68 
69 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
70 MODULE_DESCRIPTION("IBM System p Collaborative Memory Manager");
71 MODULE_LICENSE("GPL");
72 MODULE_VERSION(CMM_DRIVER_VERSION);
73 
74 module_param_named(delay, delay, uint, S_IRUGO | S_IWUSR);
75 MODULE_PARM_DESC(delay, "Delay (in seconds) between polls to query hypervisor paging requests. "
76 		 "[Default=" __stringify(CMM_DEFAULT_DELAY) "]");
77 module_param_named(hotplug_delay, hotplug_delay, uint, S_IRUGO | S_IWUSR);
78 MODULE_PARM_DESC(delay, "Delay (in seconds) after memory hotplug remove "
79 		 "before loaning resumes. "
80 		 "[Default=" __stringify(CMM_HOTPLUG_DELAY) "]");
81 module_param_named(oom_kb, oom_kb, uint, S_IRUGO | S_IWUSR);
82 MODULE_PARM_DESC(oom_kb, "Amount of memory in kb to free on OOM. "
83 		 "[Default=" __stringify(CMM_OOM_KB) "]");
84 module_param_named(min_mem_mb, min_mem_mb, ulong, S_IRUGO | S_IWUSR);
85 MODULE_PARM_DESC(min_mem_mb, "Minimum amount of memory (in MB) to not balloon. "
86 		 "[Default=" __stringify(CMM_MIN_MEM_MB) "]");
87 module_param_named(debug, cmm_debug, uint, S_IRUGO | S_IWUSR);
88 MODULE_PARM_DESC(debug, "Enable module debugging logging. Set to 1 to enable. "
89 		 "[Default=" __stringify(CMM_DEBUG) "]");
90 
91 #define CMM_NR_PAGES ((PAGE_SIZE - sizeof(void *) - sizeof(unsigned long)) / sizeof(unsigned long))
92 
93 #define cmm_dbg(...) if (cmm_debug) { printk(KERN_INFO "cmm: "__VA_ARGS__); }
94 
95 struct cmm_page_array {
96 	struct cmm_page_array *next;
97 	unsigned long index;
98 	unsigned long page[CMM_NR_PAGES];
99 };
100 
101 static unsigned long loaned_pages;
102 static unsigned long loaned_pages_target;
103 static unsigned long oom_freed_pages;
104 
105 static struct cmm_page_array *cmm_page_list;
106 static DEFINE_SPINLOCK(cmm_lock);
107 
108 static DEFINE_MUTEX(hotplug_mutex);
109 static int hotplug_occurred; /* protected by the hotplug mutex */
110 
111 static struct task_struct *cmm_thread_ptr;
112 
113 /**
114  * cmm_alloc_pages - Allocate pages and mark them as loaned
115  * @nr:	number of pages to allocate
116  *
117  * Return value:
118  * 	number of pages requested to be allocated which were not
119  **/
120 static long cmm_alloc_pages(long nr)
121 {
122 	struct cmm_page_array *pa, *npa;
123 	unsigned long addr;
124 	long rc;
125 
126 	cmm_dbg("Begin request for %ld pages\n", nr);
127 
128 	while (nr) {
129 		/* Exit if a hotplug operation is in progress or occurred */
130 		if (mutex_trylock(&hotplug_mutex)) {
131 			if (hotplug_occurred) {
132 				mutex_unlock(&hotplug_mutex);
133 				break;
134 			}
135 			mutex_unlock(&hotplug_mutex);
136 		} else {
137 			break;
138 		}
139 
140 		addr = __get_free_page(GFP_NOIO | __GFP_NOWARN |
141 				       __GFP_NORETRY | __GFP_NOMEMALLOC);
142 		if (!addr)
143 			break;
144 		spin_lock(&cmm_lock);
145 		pa = cmm_page_list;
146 		if (!pa || pa->index >= CMM_NR_PAGES) {
147 			/* Need a new page for the page list. */
148 			spin_unlock(&cmm_lock);
149 			npa = (struct cmm_page_array *)__get_free_page(
150 					GFP_NOIO | __GFP_NOWARN |
151 					__GFP_NORETRY | __GFP_NOMEMALLOC);
152 			if (!npa) {
153 				pr_info("%s: Can not allocate new page list\n", __func__);
154 				free_page(addr);
155 				break;
156 			}
157 			spin_lock(&cmm_lock);
158 			pa = cmm_page_list;
159 
160 			if (!pa || pa->index >= CMM_NR_PAGES) {
161 				npa->next = pa;
162 				npa->index = 0;
163 				pa = npa;
164 				cmm_page_list = pa;
165 			} else
166 				free_page((unsigned long) npa);
167 		}
168 
169 		if ((rc = plpar_page_set_loaned(__pa(addr)))) {
170 			pr_err("%s: Can not set page to loaned. rc=%ld\n", __func__, rc);
171 			spin_unlock(&cmm_lock);
172 			free_page(addr);
173 			break;
174 		}
175 
176 		pa->page[pa->index++] = addr;
177 		loaned_pages++;
178 		totalram_pages--;
179 		spin_unlock(&cmm_lock);
180 		nr--;
181 	}
182 
183 	cmm_dbg("End request with %ld pages unfulfilled\n", nr);
184 	return nr;
185 }
186 
187 /**
188  * cmm_free_pages - Free pages and mark them as active
189  * @nr:	number of pages to free
190  *
191  * Return value:
192  * 	number of pages requested to be freed which were not
193  **/
194 static long cmm_free_pages(long nr)
195 {
196 	struct cmm_page_array *pa;
197 	unsigned long addr;
198 
199 	cmm_dbg("Begin free of %ld pages.\n", nr);
200 	spin_lock(&cmm_lock);
201 	pa = cmm_page_list;
202 	while (nr) {
203 		if (!pa || pa->index <= 0)
204 			break;
205 		addr = pa->page[--pa->index];
206 
207 		if (pa->index == 0) {
208 			pa = pa->next;
209 			free_page((unsigned long) cmm_page_list);
210 			cmm_page_list = pa;
211 		}
212 
213 		plpar_page_set_active(__pa(addr));
214 		free_page(addr);
215 		loaned_pages--;
216 		nr--;
217 		totalram_pages++;
218 	}
219 	spin_unlock(&cmm_lock);
220 	cmm_dbg("End request with %ld pages unfulfilled\n", nr);
221 	return nr;
222 }
223 
224 /**
225  * cmm_oom_notify - OOM notifier
226  * @self:	notifier block struct
227  * @dummy:	not used
228  * @parm:	returned - number of pages freed
229  *
230  * Return value:
231  * 	NOTIFY_OK
232  **/
233 static int cmm_oom_notify(struct notifier_block *self,
234 			  unsigned long dummy, void *parm)
235 {
236 	unsigned long *freed = parm;
237 	long nr = KB2PAGES(oom_kb);
238 
239 	cmm_dbg("OOM processing started\n");
240 	nr = cmm_free_pages(nr);
241 	loaned_pages_target = loaned_pages;
242 	*freed += KB2PAGES(oom_kb) - nr;
243 	oom_freed_pages += KB2PAGES(oom_kb) - nr;
244 	cmm_dbg("OOM processing complete\n");
245 	return NOTIFY_OK;
246 }
247 
248 /**
249  * cmm_get_mpp - Read memory performance parameters
250  *
251  * Makes hcall to query the current page loan request from the hypervisor.
252  *
253  * Return value:
254  * 	nothing
255  **/
256 static void cmm_get_mpp(void)
257 {
258 	int rc;
259 	struct hvcall_mpp_data mpp_data;
260 	signed long active_pages_target, page_loan_request, target;
261 	signed long total_pages = totalram_pages + loaned_pages;
262 	signed long min_mem_pages = (min_mem_mb * 1024 * 1024) / PAGE_SIZE;
263 
264 	rc = h_get_mpp(&mpp_data);
265 
266 	if (rc != H_SUCCESS)
267 		return;
268 
269 	page_loan_request = div_s64((s64)mpp_data.loan_request, PAGE_SIZE);
270 	target = page_loan_request + (signed long)loaned_pages;
271 
272 	if (target < 0 || total_pages < min_mem_pages)
273 		target = 0;
274 
275 	if (target > oom_freed_pages)
276 		target -= oom_freed_pages;
277 	else
278 		target = 0;
279 
280 	active_pages_target = total_pages - target;
281 
282 	if (min_mem_pages > active_pages_target)
283 		target = total_pages - min_mem_pages;
284 
285 	if (target < 0)
286 		target = 0;
287 
288 	loaned_pages_target = target;
289 
290 	cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
291 		page_loan_request, loaned_pages, loaned_pages_target,
292 		oom_freed_pages, totalram_pages);
293 }
294 
295 static struct notifier_block cmm_oom_nb = {
296 	.notifier_call = cmm_oom_notify
297 };
298 
299 /**
300  * cmm_thread - CMM task thread
301  * @dummy:	not used
302  *
303  * Return value:
304  * 	0
305  **/
306 static int cmm_thread(void *dummy)
307 {
308 	unsigned long timeleft;
309 
310 	while (1) {
311 		timeleft = msleep_interruptible(delay * 1000);
312 
313 		if (kthread_should_stop() || timeleft)
314 			break;
315 
316 		if (mutex_trylock(&hotplug_mutex)) {
317 			if (hotplug_occurred) {
318 				hotplug_occurred = 0;
319 				mutex_unlock(&hotplug_mutex);
320 				cmm_dbg("Hotplug operation has occurred, "
321 						"loaning activity suspended "
322 						"for %d seconds.\n",
323 						hotplug_delay);
324 				timeleft = msleep_interruptible(hotplug_delay *
325 						1000);
326 				if (kthread_should_stop() || timeleft)
327 					break;
328 				continue;
329 			}
330 			mutex_unlock(&hotplug_mutex);
331 		} else {
332 			cmm_dbg("Hotplug operation in progress, activity "
333 					"suspended\n");
334 			continue;
335 		}
336 
337 		cmm_get_mpp();
338 
339 		if (loaned_pages_target > loaned_pages) {
340 			if (cmm_alloc_pages(loaned_pages_target - loaned_pages))
341 				loaned_pages_target = loaned_pages;
342 		} else if (loaned_pages_target < loaned_pages)
343 			cmm_free_pages(loaned_pages - loaned_pages_target);
344 	}
345 	return 0;
346 }
347 
348 #define CMM_SHOW(name, format, args...)			\
349 	static ssize_t show_##name(struct sys_device *dev,	\
350 				   struct sysdev_attribute *attr,	\
351 				   char *buf)			\
352 	{							\
353 		return sprintf(buf, format, ##args);		\
354 	}							\
355 	static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
356 
357 CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(loaned_pages));
358 CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
359 
360 static ssize_t show_oom_pages(struct sys_device *dev,
361 			      struct sysdev_attribute *attr, char *buf)
362 {
363 	return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
364 }
365 
366 static ssize_t store_oom_pages(struct sys_device *dev,
367 			       struct sysdev_attribute *attr,
368 			       const char *buf, size_t count)
369 {
370 	unsigned long val = simple_strtoul (buf, NULL, 10);
371 
372 	if (!capable(CAP_SYS_ADMIN))
373 		return -EPERM;
374 	if (val != 0)
375 		return -EBADMSG;
376 
377 	oom_freed_pages = 0;
378 	return count;
379 }
380 
381 static SYSDEV_ATTR(oom_freed_kb, S_IWUSR| S_IRUGO,
382 		   show_oom_pages, store_oom_pages);
383 
384 static struct sysdev_attribute *cmm_attrs[] = {
385 	&attr_loaned_kb,
386 	&attr_loaned_target_kb,
387 	&attr_oom_freed_kb,
388 };
389 
390 static struct sysdev_class cmm_sysdev_class = {
391 	.name = "cmm",
392 };
393 
394 /**
395  * cmm_sysfs_register - Register with sysfs
396  *
397  * Return value:
398  * 	0 on success / other on failure
399  **/
400 static int cmm_sysfs_register(struct sys_device *sysdev)
401 {
402 	int i, rc;
403 
404 	if ((rc = sysdev_class_register(&cmm_sysdev_class)))
405 		return rc;
406 
407 	sysdev->id = 0;
408 	sysdev->cls = &cmm_sysdev_class;
409 
410 	if ((rc = sysdev_register(sysdev)))
411 		goto class_unregister;
412 
413 	for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
414 		if ((rc = sysdev_create_file(sysdev, cmm_attrs[i])))
415 			goto fail;
416 	}
417 
418 	return 0;
419 
420 fail:
421 	while (--i >= 0)
422 		sysdev_remove_file(sysdev, cmm_attrs[i]);
423 	sysdev_unregister(sysdev);
424 class_unregister:
425 	sysdev_class_unregister(&cmm_sysdev_class);
426 	return rc;
427 }
428 
429 /**
430  * cmm_unregister_sysfs - Unregister from sysfs
431  *
432  **/
433 static void cmm_unregister_sysfs(struct sys_device *sysdev)
434 {
435 	int i;
436 
437 	for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++)
438 		sysdev_remove_file(sysdev, cmm_attrs[i]);
439 	sysdev_unregister(sysdev);
440 	sysdev_class_unregister(&cmm_sysdev_class);
441 }
442 
443 /**
444  * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
445  *
446  **/
447 static int cmm_reboot_notifier(struct notifier_block *nb,
448 			       unsigned long action, void *unused)
449 {
450 	if (action == SYS_RESTART) {
451 		if (cmm_thread_ptr)
452 			kthread_stop(cmm_thread_ptr);
453 		cmm_thread_ptr = NULL;
454 		cmm_free_pages(loaned_pages);
455 	}
456 	return NOTIFY_DONE;
457 }
458 
459 static struct notifier_block cmm_reboot_nb = {
460 	.notifier_call = cmm_reboot_notifier,
461 };
462 
463 /**
464  * cmm_count_pages - Count the number of pages loaned in a particular range.
465  *
466  * @arg: memory_isolate_notify structure with address range and count
467  *
468  * Return value:
469  *      0 on success
470  **/
471 static unsigned long cmm_count_pages(void *arg)
472 {
473 	struct memory_isolate_notify *marg = arg;
474 	struct cmm_page_array *pa;
475 	unsigned long start = (unsigned long)pfn_to_kaddr(marg->start_pfn);
476 	unsigned long end = start + (marg->nr_pages << PAGE_SHIFT);
477 	unsigned long idx;
478 
479 	spin_lock(&cmm_lock);
480 	pa = cmm_page_list;
481 	while (pa) {
482 		if ((unsigned long)pa >= start && (unsigned long)pa < end)
483 			marg->pages_found++;
484 		for (idx = 0; idx < pa->index; idx++)
485 			if (pa->page[idx] >= start && pa->page[idx] < end)
486 				marg->pages_found++;
487 		pa = pa->next;
488 	}
489 	spin_unlock(&cmm_lock);
490 	return 0;
491 }
492 
493 /**
494  * cmm_memory_isolate_cb - Handle memory isolation notifier calls
495  * @self:	notifier block struct
496  * @action:	action to take
497  * @arg:	struct memory_isolate_notify data for handler
498  *
499  * Return value:
500  *	NOTIFY_OK or notifier error based on subfunction return value
501  **/
502 static int cmm_memory_isolate_cb(struct notifier_block *self,
503 				 unsigned long action, void *arg)
504 {
505 	int ret = 0;
506 
507 	if (action == MEM_ISOLATE_COUNT)
508 		ret = cmm_count_pages(arg);
509 
510 	if (ret)
511 		ret = notifier_from_errno(ret);
512 	else
513 		ret = NOTIFY_OK;
514 
515 	return ret;
516 }
517 
518 static struct notifier_block cmm_mem_isolate_nb = {
519 	.notifier_call = cmm_memory_isolate_cb,
520 	.priority = CMM_MEM_ISOLATE_PRI
521 };
522 
523 /**
524  * cmm_mem_going_offline - Unloan pages where memory is to be removed
525  * @arg: memory_notify structure with page range to be offlined
526  *
527  * Return value:
528  *	0 on success
529  **/
530 static int cmm_mem_going_offline(void *arg)
531 {
532 	struct memory_notify *marg = arg;
533 	unsigned long start_page = (unsigned long)pfn_to_kaddr(marg->start_pfn);
534 	unsigned long end_page = start_page + (marg->nr_pages << PAGE_SHIFT);
535 	struct cmm_page_array *pa_curr, *pa_last, *npa;
536 	unsigned long idx;
537 	unsigned long freed = 0;
538 
539 	cmm_dbg("Memory going offline, searching 0x%lx (%ld pages).\n",
540 			start_page, marg->nr_pages);
541 	spin_lock(&cmm_lock);
542 
543 	/* Search the page list for pages in the range to be offlined */
544 	pa_last = pa_curr = cmm_page_list;
545 	while (pa_curr) {
546 		for (idx = (pa_curr->index - 1); (idx + 1) > 0; idx--) {
547 			if ((pa_curr->page[idx] < start_page) ||
548 			    (pa_curr->page[idx] >= end_page))
549 				continue;
550 
551 			plpar_page_set_active(__pa(pa_curr->page[idx]));
552 			free_page(pa_curr->page[idx]);
553 			freed++;
554 			loaned_pages--;
555 			totalram_pages++;
556 			pa_curr->page[idx] = pa_last->page[--pa_last->index];
557 			if (pa_last->index == 0) {
558 				if (pa_curr == pa_last)
559 					pa_curr = pa_last->next;
560 				pa_last = pa_last->next;
561 				free_page((unsigned long)cmm_page_list);
562 				cmm_page_list = pa_last;
563 				continue;
564 			}
565 		}
566 		pa_curr = pa_curr->next;
567 	}
568 
569 	/* Search for page list structures in the range to be offlined */
570 	pa_last = NULL;
571 	pa_curr = cmm_page_list;
572 	while (pa_curr) {
573 		if (((unsigned long)pa_curr >= start_page) &&
574 				((unsigned long)pa_curr < end_page)) {
575 			npa = (struct cmm_page_array *)__get_free_page(
576 					GFP_NOIO | __GFP_NOWARN |
577 					__GFP_NORETRY | __GFP_NOMEMALLOC);
578 			if (!npa) {
579 				spin_unlock(&cmm_lock);
580 				cmm_dbg("Failed to allocate memory for list "
581 						"management. Memory hotplug "
582 						"failed.\n");
583 				return ENOMEM;
584 			}
585 			memcpy(npa, pa_curr, PAGE_SIZE);
586 			if (pa_curr == cmm_page_list)
587 				cmm_page_list = npa;
588 			if (pa_last)
589 				pa_last->next = npa;
590 			free_page((unsigned long) pa_curr);
591 			freed++;
592 			pa_curr = npa;
593 		}
594 
595 		pa_last = pa_curr;
596 		pa_curr = pa_curr->next;
597 	}
598 
599 	spin_unlock(&cmm_lock);
600 	cmm_dbg("Released %ld pages in the search range.\n", freed);
601 
602 	return 0;
603 }
604 
605 /**
606  * cmm_memory_cb - Handle memory hotplug notifier calls
607  * @self:	notifier block struct
608  * @action:	action to take
609  * @arg:	struct memory_notify data for handler
610  *
611  * Return value:
612  *	NOTIFY_OK or notifier error based on subfunction return value
613  *
614  **/
615 static int cmm_memory_cb(struct notifier_block *self,
616 			unsigned long action, void *arg)
617 {
618 	int ret = 0;
619 
620 	switch (action) {
621 	case MEM_GOING_OFFLINE:
622 		mutex_lock(&hotplug_mutex);
623 		hotplug_occurred = 1;
624 		ret = cmm_mem_going_offline(arg);
625 		break;
626 	case MEM_OFFLINE:
627 	case MEM_CANCEL_OFFLINE:
628 		mutex_unlock(&hotplug_mutex);
629 		cmm_dbg("Memory offline operation complete.\n");
630 		break;
631 	case MEM_GOING_ONLINE:
632 	case MEM_ONLINE:
633 	case MEM_CANCEL_ONLINE:
634 		break;
635 	}
636 
637 	if (ret)
638 		ret = notifier_from_errno(ret);
639 	else
640 		ret = NOTIFY_OK;
641 
642 	return ret;
643 }
644 
645 static struct notifier_block cmm_mem_nb = {
646 	.notifier_call = cmm_memory_cb,
647 	.priority = CMM_MEM_HOTPLUG_PRI
648 };
649 
650 /**
651  * cmm_init - Module initialization
652  *
653  * Return value:
654  * 	0 on success / other on failure
655  **/
656 static int cmm_init(void)
657 {
658 	int rc = -ENOMEM;
659 
660 	if (!firmware_has_feature(FW_FEATURE_CMO))
661 		return -EOPNOTSUPP;
662 
663 	if ((rc = register_oom_notifier(&cmm_oom_nb)) < 0)
664 		return rc;
665 
666 	if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
667 		goto out_oom_notifier;
668 
669 	if ((rc = cmm_sysfs_register(&cmm_sysdev)))
670 		goto out_reboot_notifier;
671 
672 	if (register_memory_notifier(&cmm_mem_nb) ||
673 	    register_memory_isolate_notifier(&cmm_mem_isolate_nb))
674 		goto out_unregister_notifier;
675 
676 	if (cmm_disabled)
677 		return rc;
678 
679 	cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
680 	if (IS_ERR(cmm_thread_ptr)) {
681 		rc = PTR_ERR(cmm_thread_ptr);
682 		goto out_unregister_notifier;
683 	}
684 
685 	return rc;
686 
687 out_unregister_notifier:
688 	unregister_memory_notifier(&cmm_mem_nb);
689 	unregister_memory_isolate_notifier(&cmm_mem_isolate_nb);
690 	cmm_unregister_sysfs(&cmm_sysdev);
691 out_reboot_notifier:
692 	unregister_reboot_notifier(&cmm_reboot_nb);
693 out_oom_notifier:
694 	unregister_oom_notifier(&cmm_oom_nb);
695 	return rc;
696 }
697 
698 /**
699  * cmm_exit - Module exit
700  *
701  * Return value:
702  * 	nothing
703  **/
704 static void cmm_exit(void)
705 {
706 	if (cmm_thread_ptr)
707 		kthread_stop(cmm_thread_ptr);
708 	unregister_oom_notifier(&cmm_oom_nb);
709 	unregister_reboot_notifier(&cmm_reboot_nb);
710 	unregister_memory_notifier(&cmm_mem_nb);
711 	unregister_memory_isolate_notifier(&cmm_mem_isolate_nb);
712 	cmm_free_pages(loaned_pages);
713 	cmm_unregister_sysfs(&cmm_sysdev);
714 }
715 
716 /**
717  * cmm_set_disable - Disable/Enable CMM
718  *
719  * Return value:
720  * 	0 on success / other on failure
721  **/
722 static int cmm_set_disable(const char *val, struct kernel_param *kp)
723 {
724 	int disable = simple_strtoul(val, NULL, 10);
725 
726 	if (disable != 0 && disable != 1)
727 		return -EINVAL;
728 
729 	if (disable && !cmm_disabled) {
730 		if (cmm_thread_ptr)
731 			kthread_stop(cmm_thread_ptr);
732 		cmm_thread_ptr = NULL;
733 		cmm_free_pages(loaned_pages);
734 	} else if (!disable && cmm_disabled) {
735 		cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
736 		if (IS_ERR(cmm_thread_ptr))
737 			return PTR_ERR(cmm_thread_ptr);
738 	}
739 
740 	cmm_disabled = disable;
741 	return 0;
742 }
743 
744 module_param_call(disable, cmm_set_disable, param_get_uint,
745 		  &cmm_disabled, S_IRUGO | S_IWUSR);
746 MODULE_PARM_DESC(disable, "Disable CMM. Set to 1 to disable. "
747 		 "[Default=" __stringify(CMM_DISABLE) "]");
748 
749 module_init(cmm_init);
750 module_exit(cmm_exit);
751