xref: /openbmc/linux/arch/powerpc/platforms/pseries/cmm.c (revision fe030c9b85e6783bc52fe86449c0a4b8aa16c753)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
284af458bSBrian King /*
384af458bSBrian King  * Collaborative memory management interface.
484af458bSBrian King  *
584af458bSBrian King  * Copyright (C) 2008 IBM Corporation
684af458bSBrian King  * Author(s): Brian King (brking@linux.vnet.ibm.com),
784af458bSBrian King  */
884af458bSBrian King 
984af458bSBrian King #include <linux/ctype.h>
1084af458bSBrian King #include <linux/delay.h>
1184af458bSBrian King #include <linux/errno.h>
1284af458bSBrian King #include <linux/fs.h>
135a0e3ad6STejun Heo #include <linux/gfp.h>
1484af458bSBrian King #include <linux/kthread.h>
1584af458bSBrian King #include <linux/module.h>
1684af458bSBrian King #include <linux/oom.h>
17fecba962SBrian King #include <linux/reboot.h>
1884af458bSBrian King #include <linux/sched.h>
1984af458bSBrian King #include <linux/stringify.h>
2084af458bSBrian King #include <linux/swap.h>
216c9d2909SKay Sievers #include <linux/device.h>
22*fe030c9bSDavid Hildenbrand #include <linux/mount.h>
23*fe030c9bSDavid Hildenbrand #include <linux/pseudo_fs.h>
24*fe030c9bSDavid Hildenbrand #include <linux/magic.h>
25*fe030c9bSDavid Hildenbrand #include <linux/balloon_compaction.h>
2684af458bSBrian King #include <asm/firmware.h>
2784af458bSBrian King #include <asm/hvcall.h>
2884af458bSBrian King #include <asm/mmu.h>
2984af458bSBrian King #include <asm/pgalloc.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3114b8a76bSRobert Jennings #include <linux/memory.h>
32212bebb4SDeepthi Dharwar #include <asm/plpar_wrappers.h>
3384af458bSBrian King 
348f272a5dSMichael Ellerman #include "pseries.h"
358f272a5dSMichael Ellerman 
3684af458bSBrian King #define CMM_DRIVER_VERSION	"1.0.0"
3784af458bSBrian King #define CMM_DEFAULT_DELAY	1
3814b8a76bSRobert Jennings #define CMM_HOTPLUG_DELAY	5
3984af458bSBrian King #define CMM_DEBUG			0
4084af458bSBrian King #define CMM_DISABLE		0
4184af458bSBrian King #define CMM_OOM_KB		1024
4284af458bSBrian King #define CMM_MIN_MEM_MB		256
4384af458bSBrian King #define KB2PAGES(_p)		((_p)>>(PAGE_SHIFT-10))
4484af458bSBrian King #define PAGES2KB(_p)		((_p)<<(PAGE_SHIFT-10))
457659f5d6SDavid Hildenbrand 
4614b8a76bSRobert Jennings #define CMM_MEM_HOTPLUG_PRI	1
4784af458bSBrian King 
4884af458bSBrian King static unsigned int delay = CMM_DEFAULT_DELAY;
4914b8a76bSRobert Jennings static unsigned int hotplug_delay = CMM_HOTPLUG_DELAY;
5084af458bSBrian King static unsigned int oom_kb = CMM_OOM_KB;
5184af458bSBrian King static unsigned int cmm_debug = CMM_DEBUG;
5284af458bSBrian King static unsigned int cmm_disabled = CMM_DISABLE;
5384af458bSBrian King static unsigned long min_mem_mb = CMM_MIN_MEM_MB;
546c9d2909SKay Sievers static struct device cmm_dev;
5584af458bSBrian King 
5684af458bSBrian King MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
5784af458bSBrian King MODULE_DESCRIPTION("IBM System p Collaborative Memory Manager");
5884af458bSBrian King MODULE_LICENSE("GPL");
5984af458bSBrian King MODULE_VERSION(CMM_DRIVER_VERSION);
6084af458bSBrian King 
6157ad583fSRussell Currey module_param_named(delay, delay, uint, 0644);
6284af458bSBrian King MODULE_PARM_DESC(delay, "Delay (in seconds) between polls to query hypervisor paging requests. "
6384af458bSBrian King 		 "[Default=" __stringify(CMM_DEFAULT_DELAY) "]");
6457ad583fSRussell Currey module_param_named(hotplug_delay, hotplug_delay, uint, 0644);
65b0b5a765SWei Yongjun MODULE_PARM_DESC(hotplug_delay, "Delay (in seconds) after memory hotplug remove "
6614b8a76bSRobert Jennings 		 "before loaning resumes. "
6714b8a76bSRobert Jennings 		 "[Default=" __stringify(CMM_HOTPLUG_DELAY) "]");
6857ad583fSRussell Currey module_param_named(oom_kb, oom_kb, uint, 0644);
6984af458bSBrian King MODULE_PARM_DESC(oom_kb, "Amount of memory in kb to free on OOM. "
7084af458bSBrian King 		 "[Default=" __stringify(CMM_OOM_KB) "]");
7157ad583fSRussell Currey module_param_named(min_mem_mb, min_mem_mb, ulong, 0644);
7284af458bSBrian King MODULE_PARM_DESC(min_mem_mb, "Minimum amount of memory (in MB) to not balloon. "
7384af458bSBrian King 		 "[Default=" __stringify(CMM_MIN_MEM_MB) "]");
7457ad583fSRussell Currey module_param_named(debug, cmm_debug, uint, 0644);
7584af458bSBrian King MODULE_PARM_DESC(debug, "Enable module debugging logging. Set to 1 to enable. "
7684af458bSBrian King 		 "[Default=" __stringify(CMM_DEBUG) "]");
7784af458bSBrian King 
7884af458bSBrian King #define cmm_dbg(...) if (cmm_debug) { printk(KERN_INFO "cmm: "__VA_ARGS__); }
7984af458bSBrian King 
801ef2f06bSDavid Hildenbrand static atomic_long_t loaned_pages;
8184af458bSBrian King static unsigned long loaned_pages_target;
8284af458bSBrian King static unsigned long oom_freed_pages;
8384af458bSBrian King 
8414b8a76bSRobert Jennings static DEFINE_MUTEX(hotplug_mutex);
8514b8a76bSRobert Jennings static int hotplug_occurred; /* protected by the hotplug mutex */
8614b8a76bSRobert Jennings 
8784af458bSBrian King static struct task_struct *cmm_thread_ptr;
88*fe030c9bSDavid Hildenbrand static struct balloon_dev_info b_dev_info;
8984af458bSBrian King 
904a1745c5SDavid Hildenbrand static long plpar_page_set_loaned(struct page *page)
918f272a5dSMichael Ellerman {
924a1745c5SDavid Hildenbrand 	const unsigned long vpa = page_to_phys(page);
938f272a5dSMichael Ellerman 	unsigned long cmo_page_sz = cmo_get_page_size();
948f272a5dSMichael Ellerman 	long rc = 0;
958f272a5dSMichael Ellerman 	int i;
968f272a5dSMichael Ellerman 
978f272a5dSMichael Ellerman 	for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
988f272a5dSMichael Ellerman 		rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED, vpa + i, 0);
998f272a5dSMichael Ellerman 
1008f272a5dSMichael Ellerman 	for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
1018f272a5dSMichael Ellerman 		plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE,
1028f272a5dSMichael Ellerman 				   vpa + i - cmo_page_sz, 0);
1038f272a5dSMichael Ellerman 
1048f272a5dSMichael Ellerman 	return rc;
1058f272a5dSMichael Ellerman }
1068f272a5dSMichael Ellerman 
1074a1745c5SDavid Hildenbrand static long plpar_page_set_active(struct page *page)
1088f272a5dSMichael Ellerman {
1094a1745c5SDavid Hildenbrand 	const unsigned long vpa = page_to_phys(page);
1108f272a5dSMichael Ellerman 	unsigned long cmo_page_sz = cmo_get_page_size();
1118f272a5dSMichael Ellerman 	long rc = 0;
1128f272a5dSMichael Ellerman 	int i;
1138f272a5dSMichael Ellerman 
1148f272a5dSMichael Ellerman 	for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
1158f272a5dSMichael Ellerman 		rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE, vpa + i, 0);
1168f272a5dSMichael Ellerman 
1178f272a5dSMichael Ellerman 	for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
1188f272a5dSMichael Ellerman 		plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED,
1198f272a5dSMichael Ellerman 				   vpa + i - cmo_page_sz, 0);
1208f272a5dSMichael Ellerman 
1218f272a5dSMichael Ellerman 	return rc;
1228f272a5dSMichael Ellerman }
1238f272a5dSMichael Ellerman 
12484af458bSBrian King /**
12584af458bSBrian King  * cmm_alloc_pages - Allocate pages and mark them as loaned
12684af458bSBrian King  * @nr:	number of pages to allocate
12784af458bSBrian King  *
12884af458bSBrian King  * Return value:
12984af458bSBrian King  * 	number of pages requested to be allocated which were not
13084af458bSBrian King  **/
13184af458bSBrian King static long cmm_alloc_pages(long nr)
13284af458bSBrian King {
1334a1745c5SDavid Hildenbrand 	struct page *page;
13484af458bSBrian King 	long rc;
13584af458bSBrian King 
13684af458bSBrian King 	cmm_dbg("Begin request for %ld pages\n", nr);
13784af458bSBrian King 
13884af458bSBrian King 	while (nr) {
13914b8a76bSRobert Jennings 		/* Exit if a hotplug operation is in progress or occurred */
14014b8a76bSRobert Jennings 		if (mutex_trylock(&hotplug_mutex)) {
14114b8a76bSRobert Jennings 			if (hotplug_occurred) {
14214b8a76bSRobert Jennings 				mutex_unlock(&hotplug_mutex);
14314b8a76bSRobert Jennings 				break;
14414b8a76bSRobert Jennings 			}
14514b8a76bSRobert Jennings 			mutex_unlock(&hotplug_mutex);
14614b8a76bSRobert Jennings 		} else {
14714b8a76bSRobert Jennings 			break;
14814b8a76bSRobert Jennings 		}
14914b8a76bSRobert Jennings 
1504a1745c5SDavid Hildenbrand 		page = alloc_page(GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY |
1514a1745c5SDavid Hildenbrand 				  __GFP_NOMEMALLOC);
1524a1745c5SDavid Hildenbrand 		if (!page)
15384af458bSBrian King 			break;
1544a1745c5SDavid Hildenbrand 		rc = plpar_page_set_loaned(page);
1554a1745c5SDavid Hildenbrand 		if (rc) {
1565df72bf3SHarvey Harrison 			pr_err("%s: Can not set page to loaned. rc=%ld\n", __func__, rc);
1574a1745c5SDavid Hildenbrand 			__free_page(page);
15884af458bSBrian King 			break;
15984af458bSBrian King 		}
16084af458bSBrian King 
161*fe030c9bSDavid Hildenbrand 		balloon_page_enqueue(&b_dev_info, page);
1621ef2f06bSDavid Hildenbrand 		atomic_long_inc(&loaned_pages);
163287b8977SDavid Hildenbrand 		adjust_managed_page_count(page, -1);
16484af458bSBrian King 		nr--;
16584af458bSBrian King 	}
16684af458bSBrian King 
16784af458bSBrian King 	cmm_dbg("End request with %ld pages unfulfilled\n", nr);
16884af458bSBrian King 	return nr;
16984af458bSBrian King }
17084af458bSBrian King 
17184af458bSBrian King /**
17284af458bSBrian King  * cmm_free_pages - Free pages and mark them as active
17384af458bSBrian King  * @nr:	number of pages to free
17484af458bSBrian King  *
17584af458bSBrian King  * Return value:
17684af458bSBrian King  * 	number of pages requested to be freed which were not
17784af458bSBrian King  **/
17884af458bSBrian King static long cmm_free_pages(long nr)
17984af458bSBrian King {
180*fe030c9bSDavid Hildenbrand 	struct page *page;
18184af458bSBrian King 
18284af458bSBrian King 	cmm_dbg("Begin free of %ld pages.\n", nr);
183*fe030c9bSDavid Hildenbrand 	while (nr) {
184*fe030c9bSDavid Hildenbrand 		page = balloon_page_dequeue(&b_dev_info);
185*fe030c9bSDavid Hildenbrand 		if (!page)
18684af458bSBrian King 			break;
1874a1745c5SDavid Hildenbrand 		plpar_page_set_active(page);
188287b8977SDavid Hildenbrand 		adjust_managed_page_count(page, 1);
1894a1745c5SDavid Hildenbrand 		__free_page(page);
1901ef2f06bSDavid Hildenbrand 		atomic_long_dec(&loaned_pages);
19184af458bSBrian King 		nr--;
19284af458bSBrian King 	}
19384af458bSBrian King 	cmm_dbg("End request with %ld pages unfulfilled\n", nr);
19484af458bSBrian King 	return nr;
19584af458bSBrian King }
19684af458bSBrian King 
19784af458bSBrian King /**
19884af458bSBrian King  * cmm_oom_notify - OOM notifier
19984af458bSBrian King  * @self:	notifier block struct
20084af458bSBrian King  * @dummy:	not used
20184af458bSBrian King  * @parm:	returned - number of pages freed
20284af458bSBrian King  *
20384af458bSBrian King  * Return value:
20484af458bSBrian King  * 	NOTIFY_OK
20584af458bSBrian King  **/
20684af458bSBrian King static int cmm_oom_notify(struct notifier_block *self,
20784af458bSBrian King 			  unsigned long dummy, void *parm)
20884af458bSBrian King {
20984af458bSBrian King 	unsigned long *freed = parm;
21084af458bSBrian King 	long nr = KB2PAGES(oom_kb);
21184af458bSBrian King 
21284af458bSBrian King 	cmm_dbg("OOM processing started\n");
21384af458bSBrian King 	nr = cmm_free_pages(nr);
2141ef2f06bSDavid Hildenbrand 	loaned_pages_target = atomic_long_read(&loaned_pages);
21584af458bSBrian King 	*freed += KB2PAGES(oom_kb) - nr;
21684af458bSBrian King 	oom_freed_pages += KB2PAGES(oom_kb) - nr;
21784af458bSBrian King 	cmm_dbg("OOM processing complete\n");
21884af458bSBrian King 	return NOTIFY_OK;
21984af458bSBrian King }
22084af458bSBrian King 
22184af458bSBrian King /**
22284af458bSBrian King  * cmm_get_mpp - Read memory performance parameters
22384af458bSBrian King  *
22484af458bSBrian King  * Makes hcall to query the current page loan request from the hypervisor.
22584af458bSBrian King  *
22684af458bSBrian King  * Return value:
22784af458bSBrian King  * 	nothing
22884af458bSBrian King  **/
22984af458bSBrian King static void cmm_get_mpp(void)
23084af458bSBrian King {
2311ef2f06bSDavid Hildenbrand 	const long __loaned_pages = atomic_long_read(&loaned_pages);
2321ef2f06bSDavid Hildenbrand 	const long total_pages = totalram_pages() + __loaned_pages;
23384af458bSBrian King 	int rc;
23484af458bSBrian King 	struct hvcall_mpp_data mpp_data;
2358be8cf5bSBrian King 	signed long active_pages_target, page_loan_request, target;
2368be8cf5bSBrian King 	signed long min_mem_pages = (min_mem_mb * 1024 * 1024) / PAGE_SIZE;
23784af458bSBrian King 
23884af458bSBrian King 	rc = h_get_mpp(&mpp_data);
23984af458bSBrian King 
24084af458bSBrian King 	if (rc != H_SUCCESS)
24184af458bSBrian King 		return;
24284af458bSBrian King 
24384af458bSBrian King 	page_loan_request = div_s64((s64)mpp_data.loan_request, PAGE_SIZE);
2441ef2f06bSDavid Hildenbrand 	target = page_loan_request + __loaned_pages;
2458be8cf5bSBrian King 
2468be8cf5bSBrian King 	if (target < 0 || total_pages < min_mem_pages)
2478be8cf5bSBrian King 		target = 0;
2488be8cf5bSBrian King 
2498be8cf5bSBrian King 	if (target > oom_freed_pages)
2508be8cf5bSBrian King 		target -= oom_freed_pages;
25184af458bSBrian King 	else
2528be8cf5bSBrian King 		target = 0;
25384af458bSBrian King 
2548be8cf5bSBrian King 	active_pages_target = total_pages - target;
25584af458bSBrian King 
2568be8cf5bSBrian King 	if (min_mem_pages > active_pages_target)
2578be8cf5bSBrian King 		target = total_pages - min_mem_pages;
2588be8cf5bSBrian King 
2598be8cf5bSBrian King 	if (target < 0)
2608be8cf5bSBrian King 		target = 0;
2618be8cf5bSBrian King 
2628be8cf5bSBrian King 	loaned_pages_target = target;
26384af458bSBrian King 
26484af458bSBrian King 	cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
2651ef2f06bSDavid Hildenbrand 		page_loan_request, __loaned_pages, loaned_pages_target,
266ca79b0c2SArun KS 		oom_freed_pages, totalram_pages());
26784af458bSBrian King }
26884af458bSBrian King 
26984af458bSBrian King static struct notifier_block cmm_oom_nb = {
27084af458bSBrian King 	.notifier_call = cmm_oom_notify
27184af458bSBrian King };
27284af458bSBrian King 
27384af458bSBrian King /**
27484af458bSBrian King  * cmm_thread - CMM task thread
27584af458bSBrian King  * @dummy:	not used
27684af458bSBrian King  *
27784af458bSBrian King  * Return value:
27884af458bSBrian King  * 	0
27984af458bSBrian King  **/
28084af458bSBrian King static int cmm_thread(void *dummy)
28184af458bSBrian King {
28284af458bSBrian King 	unsigned long timeleft;
2831ef2f06bSDavid Hildenbrand 	long __loaned_pages;
28484af458bSBrian King 
28584af458bSBrian King 	while (1) {
28684af458bSBrian King 		timeleft = msleep_interruptible(delay * 1000);
28784af458bSBrian King 
28814b8a76bSRobert Jennings 		if (kthread_should_stop() || timeleft)
28984af458bSBrian King 			break;
29014b8a76bSRobert Jennings 
29114b8a76bSRobert Jennings 		if (mutex_trylock(&hotplug_mutex)) {
29214b8a76bSRobert Jennings 			if (hotplug_occurred) {
29314b8a76bSRobert Jennings 				hotplug_occurred = 0;
29414b8a76bSRobert Jennings 				mutex_unlock(&hotplug_mutex);
29514b8a76bSRobert Jennings 				cmm_dbg("Hotplug operation has occurred, "
29614b8a76bSRobert Jennings 						"loaning activity suspended "
29714b8a76bSRobert Jennings 						"for %d seconds.\n",
29814b8a76bSRobert Jennings 						hotplug_delay);
29914b8a76bSRobert Jennings 				timeleft = msleep_interruptible(hotplug_delay *
30014b8a76bSRobert Jennings 						1000);
30114b8a76bSRobert Jennings 				if (kthread_should_stop() || timeleft)
30214b8a76bSRobert Jennings 					break;
30314b8a76bSRobert Jennings 				continue;
30414b8a76bSRobert Jennings 			}
30514b8a76bSRobert Jennings 			mutex_unlock(&hotplug_mutex);
30614b8a76bSRobert Jennings 		} else {
30714b8a76bSRobert Jennings 			cmm_dbg("Hotplug operation in progress, activity "
30814b8a76bSRobert Jennings 					"suspended\n");
30914b8a76bSRobert Jennings 			continue;
31084af458bSBrian King 		}
31184af458bSBrian King 
31284af458bSBrian King 		cmm_get_mpp();
31384af458bSBrian King 
3141ef2f06bSDavid Hildenbrand 		__loaned_pages = atomic_long_read(&loaned_pages);
3151ef2f06bSDavid Hildenbrand 		if (loaned_pages_target > __loaned_pages) {
3161ef2f06bSDavid Hildenbrand 			if (cmm_alloc_pages(loaned_pages_target - __loaned_pages))
3171ef2f06bSDavid Hildenbrand 				loaned_pages_target = __loaned_pages;
3181ef2f06bSDavid Hildenbrand 		} else if (loaned_pages_target < __loaned_pages)
3191ef2f06bSDavid Hildenbrand 			cmm_free_pages(__loaned_pages - loaned_pages_target);
32084af458bSBrian King 	}
32184af458bSBrian King 	return 0;
32284af458bSBrian King }
32384af458bSBrian King 
32484af458bSBrian King #define CMM_SHOW(name, format, args...)			\
3256c9d2909SKay Sievers 	static ssize_t show_##name(struct device *dev,	\
3266c9d2909SKay Sievers 				   struct device_attribute *attr,	\
3273cee67f7SStephen Rothwell 				   char *buf)			\
32884af458bSBrian King 	{							\
32984af458bSBrian King 		return sprintf(buf, format, ##args);		\
33084af458bSBrian King 	}							\
33157ad583fSRussell Currey 	static DEVICE_ATTR(name, 0444, show_##name, NULL)
33284af458bSBrian King 
3331ef2f06bSDavid Hildenbrand CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(atomic_long_read(&loaned_pages)));
33484af458bSBrian King CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
33584af458bSBrian King 
3366c9d2909SKay Sievers static ssize_t show_oom_pages(struct device *dev,
3376c9d2909SKay Sievers 			      struct device_attribute *attr, char *buf)
33884af458bSBrian King {
33984af458bSBrian King 	return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
34084af458bSBrian King }
34184af458bSBrian King 
3426c9d2909SKay Sievers static ssize_t store_oom_pages(struct device *dev,
3436c9d2909SKay Sievers 			       struct device_attribute *attr,
34484af458bSBrian King 			       const char *buf, size_t count)
34584af458bSBrian King {
34684af458bSBrian King 	unsigned long val = simple_strtoul (buf, NULL, 10);
34784af458bSBrian King 
34884af458bSBrian King 	if (!capable(CAP_SYS_ADMIN))
34984af458bSBrian King 		return -EPERM;
35084af458bSBrian King 	if (val != 0)
35184af458bSBrian King 		return -EBADMSG;
35284af458bSBrian King 
35384af458bSBrian King 	oom_freed_pages = 0;
35484af458bSBrian King 	return count;
35584af458bSBrian King }
35684af458bSBrian King 
35757ad583fSRussell Currey static DEVICE_ATTR(oom_freed_kb, 0644,
35884af458bSBrian King 		   show_oom_pages, store_oom_pages);
35984af458bSBrian King 
3606c9d2909SKay Sievers static struct device_attribute *cmm_attrs[] = {
3616c9d2909SKay Sievers 	&dev_attr_loaned_kb,
3626c9d2909SKay Sievers 	&dev_attr_loaned_target_kb,
3636c9d2909SKay Sievers 	&dev_attr_oom_freed_kb,
36484af458bSBrian King };
36584af458bSBrian King 
3666c9d2909SKay Sievers static struct bus_type cmm_subsys = {
36784af458bSBrian King 	.name = "cmm",
3686c9d2909SKay Sievers 	.dev_name = "cmm",
36984af458bSBrian King };
37084af458bSBrian King 
3717d821274SDavid Hildenbrand static void cmm_release_device(struct device *dev)
3727d821274SDavid Hildenbrand {
3737d821274SDavid Hildenbrand }
3747d821274SDavid Hildenbrand 
37584af458bSBrian King /**
37684af458bSBrian King  * cmm_sysfs_register - Register with sysfs
37784af458bSBrian King  *
37884af458bSBrian King  * Return value:
37984af458bSBrian King  * 	0 on success / other on failure
38084af458bSBrian King  **/
3816c9d2909SKay Sievers static int cmm_sysfs_register(struct device *dev)
38284af458bSBrian King {
38384af458bSBrian King 	int i, rc;
38484af458bSBrian King 
3856c9d2909SKay Sievers 	if ((rc = subsys_system_register(&cmm_subsys, NULL)))
38684af458bSBrian King 		return rc;
38784af458bSBrian King 
3886c9d2909SKay Sievers 	dev->id = 0;
3896c9d2909SKay Sievers 	dev->bus = &cmm_subsys;
3907d821274SDavid Hildenbrand 	dev->release = cmm_release_device;
39184af458bSBrian King 
3926c9d2909SKay Sievers 	if ((rc = device_register(dev)))
3936c9d2909SKay Sievers 		goto subsys_unregister;
39484af458bSBrian King 
39584af458bSBrian King 	for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
3966c9d2909SKay Sievers 		if ((rc = device_create_file(dev, cmm_attrs[i])))
39784af458bSBrian King 			goto fail;
39884af458bSBrian King 	}
39984af458bSBrian King 
40084af458bSBrian King 	return 0;
40184af458bSBrian King 
40284af458bSBrian King fail:
40384af458bSBrian King 	while (--i >= 0)
4046c9d2909SKay Sievers 		device_remove_file(dev, cmm_attrs[i]);
4056c9d2909SKay Sievers 	device_unregister(dev);
4066c9d2909SKay Sievers subsys_unregister:
4076c9d2909SKay Sievers 	bus_unregister(&cmm_subsys);
40884af458bSBrian King 	return rc;
40984af458bSBrian King }
41084af458bSBrian King 
41184af458bSBrian King /**
41284af458bSBrian King  * cmm_unregister_sysfs - Unregister from sysfs
41384af458bSBrian King  *
41484af458bSBrian King  **/
4156c9d2909SKay Sievers static void cmm_unregister_sysfs(struct device *dev)
41684af458bSBrian King {
41784af458bSBrian King 	int i;
41884af458bSBrian King 
41984af458bSBrian King 	for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++)
4206c9d2909SKay Sievers 		device_remove_file(dev, cmm_attrs[i]);
4216c9d2909SKay Sievers 	device_unregister(dev);
4226c9d2909SKay Sievers 	bus_unregister(&cmm_subsys);
42384af458bSBrian King }
42484af458bSBrian King 
42584af458bSBrian King /**
426fecba962SBrian King  * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
427fecba962SBrian King  *
428fecba962SBrian King  **/
429fecba962SBrian King static int cmm_reboot_notifier(struct notifier_block *nb,
430fecba962SBrian King 			       unsigned long action, void *unused)
431fecba962SBrian King {
432fecba962SBrian King 	if (action == SYS_RESTART) {
433fecba962SBrian King 		if (cmm_thread_ptr)
434fecba962SBrian King 			kthread_stop(cmm_thread_ptr);
435fecba962SBrian King 		cmm_thread_ptr = NULL;
4361ef2f06bSDavid Hildenbrand 		cmm_free_pages(atomic_long_read(&loaned_pages));
437fecba962SBrian King 	}
438fecba962SBrian King 	return NOTIFY_DONE;
439fecba962SBrian King }
440fecba962SBrian King 
441fecba962SBrian King static struct notifier_block cmm_reboot_nb = {
442fecba962SBrian King 	.notifier_call = cmm_reboot_notifier,
443fecba962SBrian King };
444fecba962SBrian King 
445fecba962SBrian King /**
44614b8a76bSRobert Jennings  * cmm_memory_cb - Handle memory hotplug notifier calls
44714b8a76bSRobert Jennings  * @self:	notifier block struct
44814b8a76bSRobert Jennings  * @action:	action to take
44914b8a76bSRobert Jennings  * @arg:	struct memory_notify data for handler
45014b8a76bSRobert Jennings  *
45114b8a76bSRobert Jennings  * Return value:
45214b8a76bSRobert Jennings  *	NOTIFY_OK or notifier error based on subfunction return value
45314b8a76bSRobert Jennings  *
45414b8a76bSRobert Jennings  **/
45514b8a76bSRobert Jennings static int cmm_memory_cb(struct notifier_block *self,
45614b8a76bSRobert Jennings 			unsigned long action, void *arg)
45714b8a76bSRobert Jennings {
45814b8a76bSRobert Jennings 	int ret = 0;
45914b8a76bSRobert Jennings 
46014b8a76bSRobert Jennings 	switch (action) {
46114b8a76bSRobert Jennings 	case MEM_GOING_OFFLINE:
46214b8a76bSRobert Jennings 		mutex_lock(&hotplug_mutex);
46314b8a76bSRobert Jennings 		hotplug_occurred = 1;
46414b8a76bSRobert Jennings 		break;
46514b8a76bSRobert Jennings 	case MEM_OFFLINE:
46614b8a76bSRobert Jennings 	case MEM_CANCEL_OFFLINE:
46714b8a76bSRobert Jennings 		mutex_unlock(&hotplug_mutex);
46814b8a76bSRobert Jennings 		cmm_dbg("Memory offline operation complete.\n");
46914b8a76bSRobert Jennings 		break;
47014b8a76bSRobert Jennings 	case MEM_GOING_ONLINE:
47114b8a76bSRobert Jennings 	case MEM_ONLINE:
47214b8a76bSRobert Jennings 	case MEM_CANCEL_ONLINE:
47314b8a76bSRobert Jennings 		break;
47414b8a76bSRobert Jennings 	}
47514b8a76bSRobert Jennings 
4767e26065dSPrarit Bhargava 	return notifier_from_errno(ret);
47714b8a76bSRobert Jennings }
47814b8a76bSRobert Jennings 
47914b8a76bSRobert Jennings static struct notifier_block cmm_mem_nb = {
48014b8a76bSRobert Jennings 	.notifier_call = cmm_memory_cb,
48114b8a76bSRobert Jennings 	.priority = CMM_MEM_HOTPLUG_PRI
48214b8a76bSRobert Jennings };
48314b8a76bSRobert Jennings 
484*fe030c9bSDavid Hildenbrand #ifdef CONFIG_BALLOON_COMPACTION
485*fe030c9bSDavid Hildenbrand static struct vfsmount *balloon_mnt;
486*fe030c9bSDavid Hildenbrand 
487*fe030c9bSDavid Hildenbrand static int cmm_init_fs_context(struct fs_context *fc)
488*fe030c9bSDavid Hildenbrand {
489*fe030c9bSDavid Hildenbrand 	return init_pseudo(fc, PPC_CMM_MAGIC) ? 0 : -ENOMEM;
490*fe030c9bSDavid Hildenbrand }
491*fe030c9bSDavid Hildenbrand 
492*fe030c9bSDavid Hildenbrand static struct file_system_type balloon_fs = {
493*fe030c9bSDavid Hildenbrand 	.name = "ppc-cmm",
494*fe030c9bSDavid Hildenbrand 	.init_fs_context = cmm_init_fs_context,
495*fe030c9bSDavid Hildenbrand 	.kill_sb = kill_anon_super,
496*fe030c9bSDavid Hildenbrand };
497*fe030c9bSDavid Hildenbrand 
498*fe030c9bSDavid Hildenbrand static int cmm_migratepage(struct balloon_dev_info *b_dev_info,
499*fe030c9bSDavid Hildenbrand 			   struct page *newpage, struct page *page,
500*fe030c9bSDavid Hildenbrand 			   enum migrate_mode mode)
501*fe030c9bSDavid Hildenbrand {
502*fe030c9bSDavid Hildenbrand 	unsigned long flags;
503*fe030c9bSDavid Hildenbrand 
504*fe030c9bSDavid Hildenbrand 	/*
505*fe030c9bSDavid Hildenbrand 	 * loan/"inflate" the newpage first.
506*fe030c9bSDavid Hildenbrand 	 *
507*fe030c9bSDavid Hildenbrand 	 * We might race against the cmm_thread who might discover after our
508*fe030c9bSDavid Hildenbrand 	 * loan request that another page is to be unloaned. However, once
509*fe030c9bSDavid Hildenbrand 	 * the cmm_thread runs again later, this error will automatically
510*fe030c9bSDavid Hildenbrand 	 * be corrected.
511*fe030c9bSDavid Hildenbrand 	 */
512*fe030c9bSDavid Hildenbrand 	if (plpar_page_set_loaned(newpage)) {
513*fe030c9bSDavid Hildenbrand 		/* Unlikely, but possible. Tell the caller not to retry now. */
514*fe030c9bSDavid Hildenbrand 		pr_err_ratelimited("%s: Cannot set page to loaned.", __func__);
515*fe030c9bSDavid Hildenbrand 		return -EBUSY;
516*fe030c9bSDavid Hildenbrand 	}
517*fe030c9bSDavid Hildenbrand 
518*fe030c9bSDavid Hildenbrand 	/* balloon page list reference */
519*fe030c9bSDavid Hildenbrand 	get_page(newpage);
520*fe030c9bSDavid Hildenbrand 
521*fe030c9bSDavid Hildenbrand 	spin_lock_irqsave(&b_dev_info->pages_lock, flags);
522*fe030c9bSDavid Hildenbrand 	balloon_page_insert(b_dev_info, newpage);
523*fe030c9bSDavid Hildenbrand 	balloon_page_delete(page);
524*fe030c9bSDavid Hildenbrand 	b_dev_info->isolated_pages--;
525*fe030c9bSDavid Hildenbrand 	spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
526*fe030c9bSDavid Hildenbrand 
527*fe030c9bSDavid Hildenbrand 	/*
528*fe030c9bSDavid Hildenbrand 	 * activate/"deflate" the old page. We ignore any errors just like the
529*fe030c9bSDavid Hildenbrand 	 * other callers.
530*fe030c9bSDavid Hildenbrand 	 */
531*fe030c9bSDavid Hildenbrand 	plpar_page_set_active(page);
532*fe030c9bSDavid Hildenbrand 
533*fe030c9bSDavid Hildenbrand 	/* balloon page list reference */
534*fe030c9bSDavid Hildenbrand 	put_page(page);
535*fe030c9bSDavid Hildenbrand 
536*fe030c9bSDavid Hildenbrand 	return MIGRATEPAGE_SUCCESS;
537*fe030c9bSDavid Hildenbrand }
538*fe030c9bSDavid Hildenbrand 
539*fe030c9bSDavid Hildenbrand static int cmm_balloon_compaction_init(void)
540*fe030c9bSDavid Hildenbrand {
541*fe030c9bSDavid Hildenbrand 	int rc;
542*fe030c9bSDavid Hildenbrand 
543*fe030c9bSDavid Hildenbrand 	balloon_devinfo_init(&b_dev_info);
544*fe030c9bSDavid Hildenbrand 	b_dev_info.migratepage = cmm_migratepage;
545*fe030c9bSDavid Hildenbrand 
546*fe030c9bSDavid Hildenbrand 	balloon_mnt = kern_mount(&balloon_fs);
547*fe030c9bSDavid Hildenbrand 	if (IS_ERR(balloon_mnt)) {
548*fe030c9bSDavid Hildenbrand 		rc = PTR_ERR(balloon_mnt);
549*fe030c9bSDavid Hildenbrand 		balloon_mnt = NULL;
550*fe030c9bSDavid Hildenbrand 		return rc;
551*fe030c9bSDavid Hildenbrand 	}
552*fe030c9bSDavid Hildenbrand 
553*fe030c9bSDavid Hildenbrand 	b_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
554*fe030c9bSDavid Hildenbrand 	if (IS_ERR(b_dev_info.inode)) {
555*fe030c9bSDavid Hildenbrand 		rc = PTR_ERR(b_dev_info.inode);
556*fe030c9bSDavid Hildenbrand 		b_dev_info.inode = NULL;
557*fe030c9bSDavid Hildenbrand 		kern_unmount(balloon_mnt);
558*fe030c9bSDavid Hildenbrand 		balloon_mnt = NULL;
559*fe030c9bSDavid Hildenbrand 		return rc;
560*fe030c9bSDavid Hildenbrand 	}
561*fe030c9bSDavid Hildenbrand 
562*fe030c9bSDavid Hildenbrand 	b_dev_info.inode->i_mapping->a_ops = &balloon_aops;
563*fe030c9bSDavid Hildenbrand 	return 0;
564*fe030c9bSDavid Hildenbrand }
565*fe030c9bSDavid Hildenbrand static void cmm_balloon_compaction_deinit(void)
566*fe030c9bSDavid Hildenbrand {
567*fe030c9bSDavid Hildenbrand 	if (b_dev_info.inode)
568*fe030c9bSDavid Hildenbrand 		iput(b_dev_info.inode);
569*fe030c9bSDavid Hildenbrand 	b_dev_info.inode = NULL;
570*fe030c9bSDavid Hildenbrand 	kern_unmount(balloon_mnt);
571*fe030c9bSDavid Hildenbrand 	balloon_mnt = NULL;
572*fe030c9bSDavid Hildenbrand }
573*fe030c9bSDavid Hildenbrand #else /* CONFIG_BALLOON_COMPACTION */
574*fe030c9bSDavid Hildenbrand static int cmm_balloon_compaction_init(void)
575*fe030c9bSDavid Hildenbrand {
576*fe030c9bSDavid Hildenbrand 	return 0;
577*fe030c9bSDavid Hildenbrand }
578*fe030c9bSDavid Hildenbrand 
579*fe030c9bSDavid Hildenbrand static void cmm_balloon_compaction_deinit(void)
580*fe030c9bSDavid Hildenbrand {
581*fe030c9bSDavid Hildenbrand }
582*fe030c9bSDavid Hildenbrand #endif /* CONFIG_BALLOON_COMPACTION */
583*fe030c9bSDavid Hildenbrand 
58414b8a76bSRobert Jennings /**
58584af458bSBrian King  * cmm_init - Module initialization
58684af458bSBrian King  *
58784af458bSBrian King  * Return value:
58884af458bSBrian King  * 	0 on success / other on failure
58984af458bSBrian King  **/
59084af458bSBrian King static int cmm_init(void)
59184af458bSBrian King {
59268f7a049SDavid Hildenbrand 	int rc;
59384af458bSBrian King 
59484af458bSBrian King 	if (!firmware_has_feature(FW_FEATURE_CMO))
59584af458bSBrian King 		return -EOPNOTSUPP;
59684af458bSBrian King 
597*fe030c9bSDavid Hildenbrand 	rc = cmm_balloon_compaction_init();
598*fe030c9bSDavid Hildenbrand 	if (rc)
59984af458bSBrian King 		return rc;
60084af458bSBrian King 
601*fe030c9bSDavid Hildenbrand 	rc = register_oom_notifier(&cmm_oom_nb);
602*fe030c9bSDavid Hildenbrand 	if (rc < 0)
603*fe030c9bSDavid Hildenbrand 		goto out_balloon_compaction;
604*fe030c9bSDavid Hildenbrand 
605fecba962SBrian King 	if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
60684af458bSBrian King 		goto out_oom_notifier;
60784af458bSBrian King 
6086c9d2909SKay Sievers 	if ((rc = cmm_sysfs_register(&cmm_dev)))
609fecba962SBrian King 		goto out_reboot_notifier;
610fecba962SBrian King 
611022da223SDavid Hildenbrand 	rc = register_memory_notifier(&cmm_mem_nb);
612022da223SDavid Hildenbrand 	if (rc)
613022da223SDavid Hildenbrand 		goto out_unregister_notifier;
614022da223SDavid Hildenbrand 
61584af458bSBrian King 	if (cmm_disabled)
61668f7a049SDavid Hildenbrand 		return 0;
61784af458bSBrian King 
61884af458bSBrian King 	cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
61984af458bSBrian King 	if (IS_ERR(cmm_thread_ptr)) {
62084af458bSBrian King 		rc = PTR_ERR(cmm_thread_ptr);
62114b8a76bSRobert Jennings 		goto out_unregister_notifier;
62284af458bSBrian King 	}
62384af458bSBrian King 
62468f7a049SDavid Hildenbrand 	return 0;
62514b8a76bSRobert Jennings out_unregister_notifier:
62614b8a76bSRobert Jennings 	unregister_memory_notifier(&cmm_mem_nb);
6276c9d2909SKay Sievers 	cmm_unregister_sysfs(&cmm_dev);
628fecba962SBrian King out_reboot_notifier:
629fecba962SBrian King 	unregister_reboot_notifier(&cmm_reboot_nb);
63084af458bSBrian King out_oom_notifier:
63184af458bSBrian King 	unregister_oom_notifier(&cmm_oom_nb);
632*fe030c9bSDavid Hildenbrand out_balloon_compaction:
633*fe030c9bSDavid Hildenbrand 	cmm_balloon_compaction_deinit();
63484af458bSBrian King 	return rc;
63584af458bSBrian King }
63684af458bSBrian King 
63784af458bSBrian King /**
63884af458bSBrian King  * cmm_exit - Module exit
63984af458bSBrian King  *
64084af458bSBrian King  * Return value:
64184af458bSBrian King  * 	nothing
64284af458bSBrian King  **/
64384af458bSBrian King static void cmm_exit(void)
64484af458bSBrian King {
64584af458bSBrian King 	if (cmm_thread_ptr)
64684af458bSBrian King 		kthread_stop(cmm_thread_ptr);
64784af458bSBrian King 	unregister_oom_notifier(&cmm_oom_nb);
648fecba962SBrian King 	unregister_reboot_notifier(&cmm_reboot_nb);
64914b8a76bSRobert Jennings 	unregister_memory_notifier(&cmm_mem_nb);
6501ef2f06bSDavid Hildenbrand 	cmm_free_pages(atomic_long_read(&loaned_pages));
6516c9d2909SKay Sievers 	cmm_unregister_sysfs(&cmm_dev);
652*fe030c9bSDavid Hildenbrand 	cmm_balloon_compaction_deinit();
65384af458bSBrian King }
65484af458bSBrian King 
65584af458bSBrian King /**
65684af458bSBrian King  * cmm_set_disable - Disable/Enable CMM
65784af458bSBrian King  *
65884af458bSBrian King  * Return value:
65984af458bSBrian King  * 	0 on success / other on failure
66084af458bSBrian King  **/
661e4dca7b7SKees Cook static int cmm_set_disable(const char *val, const struct kernel_param *kp)
66284af458bSBrian King {
66384af458bSBrian King 	int disable = simple_strtoul(val, NULL, 10);
66484af458bSBrian King 
66584af458bSBrian King 	if (disable != 0 && disable != 1)
66684af458bSBrian King 		return -EINVAL;
66784af458bSBrian King 
66884af458bSBrian King 	if (disable && !cmm_disabled) {
66984af458bSBrian King 		if (cmm_thread_ptr)
67084af458bSBrian King 			kthread_stop(cmm_thread_ptr);
67184af458bSBrian King 		cmm_thread_ptr = NULL;
6721ef2f06bSDavid Hildenbrand 		cmm_free_pages(atomic_long_read(&loaned_pages));
67384af458bSBrian King 	} else if (!disable && cmm_disabled) {
67484af458bSBrian King 		cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
67584af458bSBrian King 		if (IS_ERR(cmm_thread_ptr))
67684af458bSBrian King 			return PTR_ERR(cmm_thread_ptr);
67784af458bSBrian King 	}
67884af458bSBrian King 
67984af458bSBrian King 	cmm_disabled = disable;
68084af458bSBrian King 	return 0;
68184af458bSBrian King }
68284af458bSBrian King 
68384af458bSBrian King module_param_call(disable, cmm_set_disable, param_get_uint,
68457ad583fSRussell Currey 		  &cmm_disabled, 0644);
68584af458bSBrian King MODULE_PARM_DESC(disable, "Disable CMM. Set to 1 to disable. "
68684af458bSBrian King 		 "[Default=" __stringify(CMM_DISABLE) "]");
68784af458bSBrian King 
68884af458bSBrian King module_init(cmm_init);
68984af458bSBrian King module_exit(cmm_exit);
690