xref: /openbmc/linux/arch/powerpc/platforms/pseries/cmm.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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>
22fe030c9bSDavid Hildenbrand #include <linux/balloon_compaction.h>
2384af458bSBrian King #include <asm/firmware.h>
2484af458bSBrian King #include <asm/hvcall.h>
2584af458bSBrian King #include <asm/mmu.h>
267c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
2714b8a76bSRobert Jennings #include <linux/memory.h>
28212bebb4SDeepthi Dharwar #include <asm/plpar_wrappers.h>
2984af458bSBrian King 
308f272a5dSMichael Ellerman #include "pseries.h"
318f272a5dSMichael Ellerman 
3284af458bSBrian King #define CMM_DRIVER_VERSION	"1.0.0"
3384af458bSBrian King #define CMM_DEFAULT_DELAY	1
3414b8a76bSRobert Jennings #define CMM_HOTPLUG_DELAY	5
3584af458bSBrian King #define CMM_DEBUG			0
3684af458bSBrian King #define CMM_DISABLE		0
3784af458bSBrian King #define CMM_OOM_KB		1024
3884af458bSBrian King #define CMM_MIN_MEM_MB		256
3984af458bSBrian King #define KB2PAGES(_p)		((_p)>>(PAGE_SHIFT-10))
4084af458bSBrian King #define PAGES2KB(_p)		((_p)<<(PAGE_SHIFT-10))
417659f5d6SDavid Hildenbrand 
4214b8a76bSRobert Jennings #define CMM_MEM_HOTPLUG_PRI	1
4384af458bSBrian King 
4484af458bSBrian King static unsigned int delay = CMM_DEFAULT_DELAY;
4514b8a76bSRobert Jennings static unsigned int hotplug_delay = CMM_HOTPLUG_DELAY;
4684af458bSBrian King static unsigned int oom_kb = CMM_OOM_KB;
4784af458bSBrian King static unsigned int cmm_debug = CMM_DEBUG;
4884af458bSBrian King static unsigned int cmm_disabled = CMM_DISABLE;
4984af458bSBrian King static unsigned long min_mem_mb = CMM_MIN_MEM_MB;
50b1713975SDavid Hildenbrand static bool __read_mostly simulate;
51b1713975SDavid Hildenbrand static unsigned long simulate_loan_target_kb;
526c9d2909SKay Sievers static struct device cmm_dev;
5384af458bSBrian King 
5484af458bSBrian King MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
5584af458bSBrian King MODULE_DESCRIPTION("IBM System p Collaborative Memory Manager");
5684af458bSBrian King MODULE_LICENSE("GPL");
5784af458bSBrian King MODULE_VERSION(CMM_DRIVER_VERSION);
5884af458bSBrian King 
5957ad583fSRussell Currey module_param_named(delay, delay, uint, 0644);
6084af458bSBrian King MODULE_PARM_DESC(delay, "Delay (in seconds) between polls to query hypervisor paging requests. "
6184af458bSBrian King 		 "[Default=" __stringify(CMM_DEFAULT_DELAY) "]");
6257ad583fSRussell Currey module_param_named(hotplug_delay, hotplug_delay, uint, 0644);
63b0b5a765SWei Yongjun MODULE_PARM_DESC(hotplug_delay, "Delay (in seconds) after memory hotplug remove "
6414b8a76bSRobert Jennings 		 "before loaning resumes. "
6514b8a76bSRobert Jennings 		 "[Default=" __stringify(CMM_HOTPLUG_DELAY) "]");
6657ad583fSRussell Currey module_param_named(oom_kb, oom_kb, uint, 0644);
6784af458bSBrian King MODULE_PARM_DESC(oom_kb, "Amount of memory in kb to free on OOM. "
6884af458bSBrian King 		 "[Default=" __stringify(CMM_OOM_KB) "]");
6957ad583fSRussell Currey module_param_named(min_mem_mb, min_mem_mb, ulong, 0644);
7084af458bSBrian King MODULE_PARM_DESC(min_mem_mb, "Minimum amount of memory (in MB) to not balloon. "
7184af458bSBrian King 		 "[Default=" __stringify(CMM_MIN_MEM_MB) "]");
7257ad583fSRussell Currey module_param_named(debug, cmm_debug, uint, 0644);
7384af458bSBrian King MODULE_PARM_DESC(debug, "Enable module debugging logging. Set to 1 to enable. "
7484af458bSBrian King 		 "[Default=" __stringify(CMM_DEBUG) "]");
75b1713975SDavid Hildenbrand module_param_named(simulate, simulate, bool, 0444);
76b1713975SDavid Hildenbrand MODULE_PARM_DESC(simulate, "Enable simulation mode (no communication with hw).");
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;
88fe030c9bSDavid Hildenbrand static struct balloon_dev_info b_dev_info;
8984af458bSBrian King 
plpar_page_set_loaned(struct page * page)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 
97b1713975SDavid Hildenbrand 	if (unlikely(simulate))
98b1713975SDavid Hildenbrand 		return 0;
99b1713975SDavid Hildenbrand 
1008f272a5dSMichael Ellerman 	for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
1018f272a5dSMichael Ellerman 		rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED, vpa + i, 0);
1028f272a5dSMichael Ellerman 
1038f272a5dSMichael Ellerman 	for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
1048f272a5dSMichael Ellerman 		plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE,
1058f272a5dSMichael Ellerman 				   vpa + i - cmo_page_sz, 0);
1068f272a5dSMichael Ellerman 
1078f272a5dSMichael Ellerman 	return rc;
1088f272a5dSMichael Ellerman }
1098f272a5dSMichael Ellerman 
plpar_page_set_active(struct page * page)1104a1745c5SDavid Hildenbrand static long plpar_page_set_active(struct page *page)
1118f272a5dSMichael Ellerman {
1124a1745c5SDavid Hildenbrand 	const unsigned long vpa = page_to_phys(page);
1138f272a5dSMichael Ellerman 	unsigned long cmo_page_sz = cmo_get_page_size();
1148f272a5dSMichael Ellerman 	long rc = 0;
1158f272a5dSMichael Ellerman 	int i;
1168f272a5dSMichael Ellerman 
117b1713975SDavid Hildenbrand 	if (unlikely(simulate))
118b1713975SDavid Hildenbrand 		return 0;
119b1713975SDavid Hildenbrand 
1208f272a5dSMichael Ellerman 	for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
1218f272a5dSMichael Ellerman 		rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE, vpa + i, 0);
1228f272a5dSMichael Ellerman 
1238f272a5dSMichael Ellerman 	for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
1248f272a5dSMichael Ellerman 		plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED,
1258f272a5dSMichael Ellerman 				   vpa + i - cmo_page_sz, 0);
1268f272a5dSMichael Ellerman 
1278f272a5dSMichael Ellerman 	return rc;
1288f272a5dSMichael Ellerman }
1298f272a5dSMichael Ellerman 
13084af458bSBrian King /**
13184af458bSBrian King  * cmm_alloc_pages - Allocate pages and mark them as loaned
13284af458bSBrian King  * @nr:	number of pages to allocate
13384af458bSBrian King  *
13484af458bSBrian King  * Return value:
13584af458bSBrian King  * 	number of pages requested to be allocated which were not
13684af458bSBrian King  **/
cmm_alloc_pages(long nr)13784af458bSBrian King static long cmm_alloc_pages(long nr)
13884af458bSBrian King {
1394a1745c5SDavid Hildenbrand 	struct page *page;
14084af458bSBrian King 	long rc;
14184af458bSBrian King 
14284af458bSBrian King 	cmm_dbg("Begin request for %ld pages\n", nr);
14384af458bSBrian King 
14484af458bSBrian King 	while (nr) {
14514b8a76bSRobert Jennings 		/* Exit if a hotplug operation is in progress or occurred */
14614b8a76bSRobert Jennings 		if (mutex_trylock(&hotplug_mutex)) {
14714b8a76bSRobert Jennings 			if (hotplug_occurred) {
14814b8a76bSRobert Jennings 				mutex_unlock(&hotplug_mutex);
14914b8a76bSRobert Jennings 				break;
15014b8a76bSRobert Jennings 			}
15114b8a76bSRobert Jennings 			mutex_unlock(&hotplug_mutex);
15214b8a76bSRobert Jennings 		} else {
15314b8a76bSRobert Jennings 			break;
15414b8a76bSRobert Jennings 		}
15514b8a76bSRobert Jennings 
156e8decafeSDavid Hildenbrand 		page = balloon_page_alloc();
1574a1745c5SDavid Hildenbrand 		if (!page)
15884af458bSBrian King 			break;
1594a1745c5SDavid Hildenbrand 		rc = plpar_page_set_loaned(page);
1604a1745c5SDavid Hildenbrand 		if (rc) {
1615df72bf3SHarvey Harrison 			pr_err("%s: Can not set page to loaned. rc=%ld\n", __func__, rc);
1624a1745c5SDavid Hildenbrand 			__free_page(page);
16384af458bSBrian King 			break;
16484af458bSBrian King 		}
16584af458bSBrian King 
166fe030c9bSDavid Hildenbrand 		balloon_page_enqueue(&b_dev_info, page);
1671ef2f06bSDavid Hildenbrand 		atomic_long_inc(&loaned_pages);
168287b8977SDavid Hildenbrand 		adjust_managed_page_count(page, -1);
16984af458bSBrian King 		nr--;
17084af458bSBrian King 	}
17184af458bSBrian King 
17284af458bSBrian King 	cmm_dbg("End request with %ld pages unfulfilled\n", nr);
17384af458bSBrian King 	return nr;
17484af458bSBrian King }
17584af458bSBrian King 
17684af458bSBrian King /**
17784af458bSBrian King  * cmm_free_pages - Free pages and mark them as active
17884af458bSBrian King  * @nr:	number of pages to free
17984af458bSBrian King  *
18084af458bSBrian King  * Return value:
18184af458bSBrian King  * 	number of pages requested to be freed which were not
18284af458bSBrian King  **/
cmm_free_pages(long nr)18384af458bSBrian King static long cmm_free_pages(long nr)
18484af458bSBrian King {
185fe030c9bSDavid Hildenbrand 	struct page *page;
18684af458bSBrian King 
18784af458bSBrian King 	cmm_dbg("Begin free of %ld pages.\n", nr);
188fe030c9bSDavid Hildenbrand 	while (nr) {
189fe030c9bSDavid Hildenbrand 		page = balloon_page_dequeue(&b_dev_info);
190fe030c9bSDavid Hildenbrand 		if (!page)
19184af458bSBrian King 			break;
1924a1745c5SDavid Hildenbrand 		plpar_page_set_active(page);
193287b8977SDavid Hildenbrand 		adjust_managed_page_count(page, 1);
1944a1745c5SDavid Hildenbrand 		__free_page(page);
1951ef2f06bSDavid Hildenbrand 		atomic_long_dec(&loaned_pages);
19684af458bSBrian King 		nr--;
19784af458bSBrian King 	}
19884af458bSBrian King 	cmm_dbg("End request with %ld pages unfulfilled\n", nr);
19984af458bSBrian King 	return nr;
20084af458bSBrian King }
20184af458bSBrian King 
20284af458bSBrian King /**
20384af458bSBrian King  * cmm_oom_notify - OOM notifier
20484af458bSBrian King  * @self:	notifier block struct
20584af458bSBrian King  * @dummy:	not used
20684af458bSBrian King  * @parm:	returned - number of pages freed
20784af458bSBrian King  *
20884af458bSBrian King  * Return value:
20984af458bSBrian King  * 	NOTIFY_OK
21084af458bSBrian King  **/
cmm_oom_notify(struct notifier_block * self,unsigned long dummy,void * parm)21184af458bSBrian King static int cmm_oom_notify(struct notifier_block *self,
21284af458bSBrian King 			  unsigned long dummy, void *parm)
21384af458bSBrian King {
21484af458bSBrian King 	unsigned long *freed = parm;
21584af458bSBrian King 	long nr = KB2PAGES(oom_kb);
21684af458bSBrian King 
21784af458bSBrian King 	cmm_dbg("OOM processing started\n");
21884af458bSBrian King 	nr = cmm_free_pages(nr);
2191ef2f06bSDavid Hildenbrand 	loaned_pages_target = atomic_long_read(&loaned_pages);
22084af458bSBrian King 	*freed += KB2PAGES(oom_kb) - nr;
22184af458bSBrian King 	oom_freed_pages += KB2PAGES(oom_kb) - nr;
22284af458bSBrian King 	cmm_dbg("OOM processing complete\n");
22384af458bSBrian King 	return NOTIFY_OK;
22484af458bSBrian King }
22584af458bSBrian King 
22684af458bSBrian King /**
22784af458bSBrian King  * cmm_get_mpp - Read memory performance parameters
22884af458bSBrian King  *
22984af458bSBrian King  * Makes hcall to query the current page loan request from the hypervisor.
23084af458bSBrian King  *
23184af458bSBrian King  * Return value:
23284af458bSBrian King  * 	nothing
23384af458bSBrian King  **/
cmm_get_mpp(void)23484af458bSBrian King static void cmm_get_mpp(void)
23584af458bSBrian King {
2361ef2f06bSDavid Hildenbrand 	const long __loaned_pages = atomic_long_read(&loaned_pages);
2371ef2f06bSDavid Hildenbrand 	const long total_pages = totalram_pages() + __loaned_pages;
23884af458bSBrian King 	int rc;
23984af458bSBrian King 	struct hvcall_mpp_data mpp_data;
2408be8cf5bSBrian King 	signed long active_pages_target, page_loan_request, target;
2418be8cf5bSBrian King 	signed long min_mem_pages = (min_mem_mb * 1024 * 1024) / PAGE_SIZE;
24284af458bSBrian King 
243b1713975SDavid Hildenbrand 	if (likely(!simulate)) {
24484af458bSBrian King 		rc = h_get_mpp(&mpp_data);
24584af458bSBrian King 		if (rc != H_SUCCESS)
24684af458bSBrian King 			return;
247b1713975SDavid Hildenbrand 		page_loan_request = div_s64((s64)mpp_data.loan_request,
248b1713975SDavid Hildenbrand 					    PAGE_SIZE);
2491ef2f06bSDavid Hildenbrand 		target = page_loan_request + __loaned_pages;
250b1713975SDavid Hildenbrand 	} else {
251b1713975SDavid Hildenbrand 		target = KB2PAGES(simulate_loan_target_kb);
252b1713975SDavid Hildenbrand 		page_loan_request = target - __loaned_pages;
253b1713975SDavid Hildenbrand 	}
2548be8cf5bSBrian King 
2558be8cf5bSBrian King 	if (target < 0 || total_pages < min_mem_pages)
2568be8cf5bSBrian King 		target = 0;
2578be8cf5bSBrian King 
2588be8cf5bSBrian King 	if (target > oom_freed_pages)
2598be8cf5bSBrian King 		target -= oom_freed_pages;
26084af458bSBrian King 	else
2618be8cf5bSBrian King 		target = 0;
26284af458bSBrian King 
2638be8cf5bSBrian King 	active_pages_target = total_pages - target;
26484af458bSBrian King 
2658be8cf5bSBrian King 	if (min_mem_pages > active_pages_target)
2668be8cf5bSBrian King 		target = total_pages - min_mem_pages;
2678be8cf5bSBrian King 
2688be8cf5bSBrian King 	if (target < 0)
2698be8cf5bSBrian King 		target = 0;
2708be8cf5bSBrian King 
2718be8cf5bSBrian King 	loaned_pages_target = target;
27284af458bSBrian King 
27384af458bSBrian King 	cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
2741ef2f06bSDavid Hildenbrand 		page_loan_request, __loaned_pages, loaned_pages_target,
275ca79b0c2SArun KS 		oom_freed_pages, totalram_pages());
27684af458bSBrian King }
27784af458bSBrian King 
27884af458bSBrian King static struct notifier_block cmm_oom_nb = {
27984af458bSBrian King 	.notifier_call = cmm_oom_notify
28084af458bSBrian King };
28184af458bSBrian King 
28284af458bSBrian King /**
28384af458bSBrian King  * cmm_thread - CMM task thread
28484af458bSBrian King  * @dummy:	not used
28584af458bSBrian King  *
28684af458bSBrian King  * Return value:
28784af458bSBrian King  * 	0
28884af458bSBrian King  **/
cmm_thread(void * dummy)28984af458bSBrian King static int cmm_thread(void *dummy)
29084af458bSBrian King {
29184af458bSBrian King 	unsigned long timeleft;
2921ef2f06bSDavid Hildenbrand 	long __loaned_pages;
29384af458bSBrian King 
29484af458bSBrian King 	while (1) {
29584af458bSBrian King 		timeleft = msleep_interruptible(delay * 1000);
29684af458bSBrian King 
29714b8a76bSRobert Jennings 		if (kthread_should_stop() || timeleft)
29884af458bSBrian King 			break;
29914b8a76bSRobert Jennings 
30014b8a76bSRobert Jennings 		if (mutex_trylock(&hotplug_mutex)) {
30114b8a76bSRobert Jennings 			if (hotplug_occurred) {
30214b8a76bSRobert Jennings 				hotplug_occurred = 0;
30314b8a76bSRobert Jennings 				mutex_unlock(&hotplug_mutex);
30414b8a76bSRobert Jennings 				cmm_dbg("Hotplug operation has occurred, "
30514b8a76bSRobert Jennings 						"loaning activity suspended "
30614b8a76bSRobert Jennings 						"for %d seconds.\n",
30714b8a76bSRobert Jennings 						hotplug_delay);
30814b8a76bSRobert Jennings 				timeleft = msleep_interruptible(hotplug_delay *
30914b8a76bSRobert Jennings 						1000);
31014b8a76bSRobert Jennings 				if (kthread_should_stop() || timeleft)
31114b8a76bSRobert Jennings 					break;
31214b8a76bSRobert Jennings 				continue;
31314b8a76bSRobert Jennings 			}
31414b8a76bSRobert Jennings 			mutex_unlock(&hotplug_mutex);
31514b8a76bSRobert Jennings 		} else {
31614b8a76bSRobert Jennings 			cmm_dbg("Hotplug operation in progress, activity "
31714b8a76bSRobert Jennings 					"suspended\n");
31814b8a76bSRobert Jennings 			continue;
31984af458bSBrian King 		}
32084af458bSBrian King 
32184af458bSBrian King 		cmm_get_mpp();
32284af458bSBrian King 
3231ef2f06bSDavid Hildenbrand 		__loaned_pages = atomic_long_read(&loaned_pages);
3241ef2f06bSDavid Hildenbrand 		if (loaned_pages_target > __loaned_pages) {
3251ef2f06bSDavid Hildenbrand 			if (cmm_alloc_pages(loaned_pages_target - __loaned_pages))
3261ef2f06bSDavid Hildenbrand 				loaned_pages_target = __loaned_pages;
3271ef2f06bSDavid Hildenbrand 		} else if (loaned_pages_target < __loaned_pages)
3281ef2f06bSDavid Hildenbrand 			cmm_free_pages(__loaned_pages - loaned_pages_target);
32984af458bSBrian King 	}
33084af458bSBrian King 	return 0;
33184af458bSBrian King }
33284af458bSBrian King 
33384af458bSBrian King #define CMM_SHOW(name, format, args...)			\
3346c9d2909SKay Sievers 	static ssize_t show_##name(struct device *dev,	\
3356c9d2909SKay Sievers 				   struct device_attribute *attr,	\
3363cee67f7SStephen Rothwell 				   char *buf)			\
33784af458bSBrian King 	{							\
33884af458bSBrian King 		return sprintf(buf, format, ##args);		\
33984af458bSBrian King 	}							\
34057ad583fSRussell Currey 	static DEVICE_ATTR(name, 0444, show_##name, NULL)
34184af458bSBrian King 
3421ef2f06bSDavid Hildenbrand CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(atomic_long_read(&loaned_pages)));
34384af458bSBrian King CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
34484af458bSBrian King 
show_oom_pages(struct device * dev,struct device_attribute * attr,char * buf)3456c9d2909SKay Sievers static ssize_t show_oom_pages(struct device *dev,
3466c9d2909SKay Sievers 			      struct device_attribute *attr, char *buf)
34784af458bSBrian King {
34884af458bSBrian King 	return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
34984af458bSBrian King }
35084af458bSBrian King 
store_oom_pages(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3516c9d2909SKay Sievers static ssize_t store_oom_pages(struct device *dev,
3526c9d2909SKay Sievers 			       struct device_attribute *attr,
35384af458bSBrian King 			       const char *buf, size_t count)
35484af458bSBrian King {
35584af458bSBrian King 	unsigned long val = simple_strtoul (buf, NULL, 10);
35684af458bSBrian King 
35784af458bSBrian King 	if (!capable(CAP_SYS_ADMIN))
35884af458bSBrian King 		return -EPERM;
35984af458bSBrian King 	if (val != 0)
36084af458bSBrian King 		return -EBADMSG;
36184af458bSBrian King 
36284af458bSBrian King 	oom_freed_pages = 0;
36384af458bSBrian King 	return count;
36484af458bSBrian King }
36584af458bSBrian King 
36657ad583fSRussell Currey static DEVICE_ATTR(oom_freed_kb, 0644,
36784af458bSBrian King 		   show_oom_pages, store_oom_pages);
36884af458bSBrian King 
3696c9d2909SKay Sievers static struct device_attribute *cmm_attrs[] = {
3706c9d2909SKay Sievers 	&dev_attr_loaned_kb,
3716c9d2909SKay Sievers 	&dev_attr_loaned_target_kb,
3726c9d2909SKay Sievers 	&dev_attr_oom_freed_kb,
37384af458bSBrian King };
37484af458bSBrian King 
375b1713975SDavid Hildenbrand static DEVICE_ULONG_ATTR(simulate_loan_target_kb, 0644,
376b1713975SDavid Hildenbrand 			 simulate_loan_target_kb);
377b1713975SDavid Hildenbrand 
3786c9d2909SKay Sievers static struct bus_type cmm_subsys = {
37984af458bSBrian King 	.name = "cmm",
3806c9d2909SKay Sievers 	.dev_name = "cmm",
38184af458bSBrian King };
38284af458bSBrian King 
cmm_release_device(struct device * dev)3837d821274SDavid Hildenbrand static void cmm_release_device(struct device *dev)
3847d821274SDavid Hildenbrand {
3857d821274SDavid Hildenbrand }
3867d821274SDavid Hildenbrand 
38784af458bSBrian King /**
38884af458bSBrian King  * cmm_sysfs_register - Register with sysfs
38984af458bSBrian King  *
39084af458bSBrian King  * Return value:
39184af458bSBrian King  * 	0 on success / other on failure
39284af458bSBrian King  **/
cmm_sysfs_register(struct device * dev)3936c9d2909SKay Sievers static int cmm_sysfs_register(struct device *dev)
39484af458bSBrian King {
39584af458bSBrian King 	int i, rc;
39684af458bSBrian King 
3976c9d2909SKay Sievers 	if ((rc = subsys_system_register(&cmm_subsys, NULL)))
39884af458bSBrian King 		return rc;
39984af458bSBrian King 
4006c9d2909SKay Sievers 	dev->id = 0;
4016c9d2909SKay Sievers 	dev->bus = &cmm_subsys;
4027d821274SDavid Hildenbrand 	dev->release = cmm_release_device;
40384af458bSBrian King 
4046c9d2909SKay Sievers 	if ((rc = device_register(dev)))
4056c9d2909SKay Sievers 		goto subsys_unregister;
40684af458bSBrian King 
40784af458bSBrian King 	for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
4086c9d2909SKay Sievers 		if ((rc = device_create_file(dev, cmm_attrs[i])))
40984af458bSBrian King 			goto fail;
41084af458bSBrian King 	}
41184af458bSBrian King 
412b1713975SDavid Hildenbrand 	if (!simulate)
413b1713975SDavid Hildenbrand 		return 0;
414b1713975SDavid Hildenbrand 	rc = device_create_file(dev, &dev_attr_simulate_loan_target_kb.attr);
415b1713975SDavid Hildenbrand 	if (rc)
416b1713975SDavid Hildenbrand 		goto fail;
41784af458bSBrian King 	return 0;
41884af458bSBrian King 
41984af458bSBrian King fail:
42084af458bSBrian King 	while (--i >= 0)
4216c9d2909SKay Sievers 		device_remove_file(dev, cmm_attrs[i]);
4226c9d2909SKay Sievers 	device_unregister(dev);
4236c9d2909SKay Sievers subsys_unregister:
4246c9d2909SKay Sievers 	bus_unregister(&cmm_subsys);
42584af458bSBrian King 	return rc;
42684af458bSBrian King }
42784af458bSBrian King 
42884af458bSBrian King /**
42984af458bSBrian King  * cmm_unregister_sysfs - Unregister from sysfs
43084af458bSBrian King  *
43184af458bSBrian King  **/
cmm_unregister_sysfs(struct device * dev)4326c9d2909SKay Sievers static void cmm_unregister_sysfs(struct device *dev)
43384af458bSBrian King {
43484af458bSBrian King 	int i;
43584af458bSBrian King 
43684af458bSBrian King 	for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++)
4376c9d2909SKay Sievers 		device_remove_file(dev, cmm_attrs[i]);
4386c9d2909SKay Sievers 	device_unregister(dev);
4396c9d2909SKay Sievers 	bus_unregister(&cmm_subsys);
44084af458bSBrian King }
44184af458bSBrian King 
44284af458bSBrian King /**
443fecba962SBrian King  * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
444fecba962SBrian King  *
445fecba962SBrian King  **/
cmm_reboot_notifier(struct notifier_block * nb,unsigned long action,void * unused)446fecba962SBrian King static int cmm_reboot_notifier(struct notifier_block *nb,
447fecba962SBrian King 			       unsigned long action, void *unused)
448fecba962SBrian King {
449fecba962SBrian King 	if (action == SYS_RESTART) {
450fecba962SBrian King 		if (cmm_thread_ptr)
451fecba962SBrian King 			kthread_stop(cmm_thread_ptr);
452fecba962SBrian King 		cmm_thread_ptr = NULL;
4531ef2f06bSDavid Hildenbrand 		cmm_free_pages(atomic_long_read(&loaned_pages));
454fecba962SBrian King 	}
455fecba962SBrian King 	return NOTIFY_DONE;
456fecba962SBrian King }
457fecba962SBrian King 
458fecba962SBrian King static struct notifier_block cmm_reboot_nb = {
459fecba962SBrian King 	.notifier_call = cmm_reboot_notifier,
460fecba962SBrian King };
461fecba962SBrian King 
462fecba962SBrian King /**
46314b8a76bSRobert Jennings  * cmm_memory_cb - Handle memory hotplug notifier calls
46414b8a76bSRobert Jennings  * @self:	notifier block struct
46514b8a76bSRobert Jennings  * @action:	action to take
46614b8a76bSRobert Jennings  * @arg:	struct memory_notify data for handler
46714b8a76bSRobert Jennings  *
46814b8a76bSRobert Jennings  * Return value:
46914b8a76bSRobert Jennings  *	NOTIFY_OK or notifier error based on subfunction return value
47014b8a76bSRobert Jennings  *
47114b8a76bSRobert Jennings  **/
cmm_memory_cb(struct notifier_block * self,unsigned long action,void * arg)47214b8a76bSRobert Jennings static int cmm_memory_cb(struct notifier_block *self,
47314b8a76bSRobert Jennings 			unsigned long action, void *arg)
47414b8a76bSRobert Jennings {
47514b8a76bSRobert Jennings 	switch (action) {
47614b8a76bSRobert Jennings 	case MEM_GOING_OFFLINE:
47714b8a76bSRobert Jennings 		mutex_lock(&hotplug_mutex);
47814b8a76bSRobert Jennings 		hotplug_occurred = 1;
47914b8a76bSRobert Jennings 		break;
48014b8a76bSRobert Jennings 	case MEM_OFFLINE:
48114b8a76bSRobert Jennings 	case MEM_CANCEL_OFFLINE:
48214b8a76bSRobert Jennings 		mutex_unlock(&hotplug_mutex);
48314b8a76bSRobert Jennings 		cmm_dbg("Memory offline operation complete.\n");
48414b8a76bSRobert Jennings 		break;
48514b8a76bSRobert Jennings 	case MEM_GOING_ONLINE:
48614b8a76bSRobert Jennings 	case MEM_ONLINE:
48714b8a76bSRobert Jennings 	case MEM_CANCEL_ONLINE:
48814b8a76bSRobert Jennings 		break;
48914b8a76bSRobert Jennings 	}
49014b8a76bSRobert Jennings 
491701c3167Szuoqilin 	return NOTIFY_OK;
49214b8a76bSRobert Jennings }
49314b8a76bSRobert Jennings 
49414b8a76bSRobert Jennings static struct notifier_block cmm_mem_nb = {
49514b8a76bSRobert Jennings 	.notifier_call = cmm_memory_cb,
49614b8a76bSRobert Jennings 	.priority = CMM_MEM_HOTPLUG_PRI
49714b8a76bSRobert Jennings };
49814b8a76bSRobert Jennings 
499fe030c9bSDavid Hildenbrand #ifdef CONFIG_BALLOON_COMPACTION
cmm_migratepage(struct balloon_dev_info * b_dev_info,struct page * newpage,struct page * page,enum migrate_mode mode)500fe030c9bSDavid Hildenbrand static int cmm_migratepage(struct balloon_dev_info *b_dev_info,
501fe030c9bSDavid Hildenbrand 			   struct page *newpage, struct page *page,
502fe030c9bSDavid Hildenbrand 			   enum migrate_mode mode)
503fe030c9bSDavid Hildenbrand {
504fe030c9bSDavid Hildenbrand 	unsigned long flags;
505fe030c9bSDavid Hildenbrand 
506fe030c9bSDavid Hildenbrand 	/*
507fe030c9bSDavid Hildenbrand 	 * loan/"inflate" the newpage first.
508fe030c9bSDavid Hildenbrand 	 *
509fe030c9bSDavid Hildenbrand 	 * We might race against the cmm_thread who might discover after our
510fe030c9bSDavid Hildenbrand 	 * loan request that another page is to be unloaned. However, once
511fe030c9bSDavid Hildenbrand 	 * the cmm_thread runs again later, this error will automatically
512fe030c9bSDavid Hildenbrand 	 * be corrected.
513fe030c9bSDavid Hildenbrand 	 */
514fe030c9bSDavid Hildenbrand 	if (plpar_page_set_loaned(newpage)) {
515fe030c9bSDavid Hildenbrand 		/* Unlikely, but possible. Tell the caller not to retry now. */
516fe030c9bSDavid Hildenbrand 		pr_err_ratelimited("%s: Cannot set page to loaned.", __func__);
517fe030c9bSDavid Hildenbrand 		return -EBUSY;
518fe030c9bSDavid Hildenbrand 	}
519fe030c9bSDavid Hildenbrand 
520fe030c9bSDavid Hildenbrand 	/* balloon page list reference */
521fe030c9bSDavid Hildenbrand 	get_page(newpage);
522fe030c9bSDavid Hildenbrand 
523e352f576SDavid Hildenbrand 	/*
524e352f576SDavid Hildenbrand 	 * When we migrate a page to a different zone, we have to fixup the
525e352f576SDavid Hildenbrand 	 * count of both involved zones as we adjusted the managed page count
526e352f576SDavid Hildenbrand 	 * when inflating.
527e352f576SDavid Hildenbrand 	 */
528e352f576SDavid Hildenbrand 	if (page_zone(page) != page_zone(newpage)) {
529e352f576SDavid Hildenbrand 		adjust_managed_page_count(page, 1);
530e352f576SDavid Hildenbrand 		adjust_managed_page_count(newpage, -1);
531e352f576SDavid Hildenbrand 	}
532e352f576SDavid Hildenbrand 
533fe030c9bSDavid Hildenbrand 	spin_lock_irqsave(&b_dev_info->pages_lock, flags);
534fe030c9bSDavid Hildenbrand 	balloon_page_insert(b_dev_info, newpage);
535fe030c9bSDavid Hildenbrand 	balloon_page_delete(page);
536fe030c9bSDavid Hildenbrand 	b_dev_info->isolated_pages--;
537fe030c9bSDavid Hildenbrand 	spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
538fe030c9bSDavid Hildenbrand 
539fe030c9bSDavid Hildenbrand 	/*
540fe030c9bSDavid Hildenbrand 	 * activate/"deflate" the old page. We ignore any errors just like the
541fe030c9bSDavid Hildenbrand 	 * other callers.
542fe030c9bSDavid Hildenbrand 	 */
543fe030c9bSDavid Hildenbrand 	plpar_page_set_active(page);
544fe030c9bSDavid Hildenbrand 
545fe030c9bSDavid Hildenbrand 	/* balloon page list reference */
546fe030c9bSDavid Hildenbrand 	put_page(page);
547fe030c9bSDavid Hildenbrand 
548fe030c9bSDavid Hildenbrand 	return MIGRATEPAGE_SUCCESS;
549fe030c9bSDavid Hildenbrand }
550fe030c9bSDavid Hildenbrand 
cmm_balloon_compaction_init(void)551*68f2736aSMatthew Wilcox (Oracle) static void cmm_balloon_compaction_init(void)
552fe030c9bSDavid Hildenbrand {
553fe030c9bSDavid Hildenbrand 	balloon_devinfo_init(&b_dev_info);
554fe030c9bSDavid Hildenbrand 	b_dev_info.migratepage = cmm_migratepage;
555fe030c9bSDavid Hildenbrand }
556fe030c9bSDavid Hildenbrand #else /* CONFIG_BALLOON_COMPACTION */
cmm_balloon_compaction_init(void)557*68f2736aSMatthew Wilcox (Oracle) static void cmm_balloon_compaction_init(void)
558fe030c9bSDavid Hildenbrand {
559fe030c9bSDavid Hildenbrand }
560fe030c9bSDavid Hildenbrand #endif /* CONFIG_BALLOON_COMPACTION */
561fe030c9bSDavid Hildenbrand 
56214b8a76bSRobert Jennings /**
56384af458bSBrian King  * cmm_init - Module initialization
56484af458bSBrian King  *
56584af458bSBrian King  * Return value:
56684af458bSBrian King  * 	0 on success / other on failure
56784af458bSBrian King  **/
cmm_init(void)56884af458bSBrian King static int cmm_init(void)
56984af458bSBrian King {
57068f7a049SDavid Hildenbrand 	int rc;
57184af458bSBrian King 
572b1713975SDavid Hildenbrand 	if (!firmware_has_feature(FW_FEATURE_CMO) && !simulate)
57384af458bSBrian King 		return -EOPNOTSUPP;
57484af458bSBrian King 
575*68f2736aSMatthew Wilcox (Oracle) 	cmm_balloon_compaction_init();
57684af458bSBrian King 
577fe030c9bSDavid Hildenbrand 	rc = register_oom_notifier(&cmm_oom_nb);
578fe030c9bSDavid Hildenbrand 	if (rc < 0)
579fe030c9bSDavid Hildenbrand 		goto out_balloon_compaction;
580fe030c9bSDavid Hildenbrand 
581fecba962SBrian King 	if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
58284af458bSBrian King 		goto out_oom_notifier;
58384af458bSBrian King 
5846c9d2909SKay Sievers 	if ((rc = cmm_sysfs_register(&cmm_dev)))
585fecba962SBrian King 		goto out_reboot_notifier;
586fecba962SBrian King 
587022da223SDavid Hildenbrand 	rc = register_memory_notifier(&cmm_mem_nb);
588022da223SDavid Hildenbrand 	if (rc)
589022da223SDavid Hildenbrand 		goto out_unregister_notifier;
590022da223SDavid Hildenbrand 
59184af458bSBrian King 	if (cmm_disabled)
59268f7a049SDavid Hildenbrand 		return 0;
59384af458bSBrian King 
59484af458bSBrian King 	cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
59584af458bSBrian King 	if (IS_ERR(cmm_thread_ptr)) {
59684af458bSBrian King 		rc = PTR_ERR(cmm_thread_ptr);
59714b8a76bSRobert Jennings 		goto out_unregister_notifier;
59884af458bSBrian King 	}
59984af458bSBrian King 
60068f7a049SDavid Hildenbrand 	return 0;
60114b8a76bSRobert Jennings out_unregister_notifier:
60214b8a76bSRobert Jennings 	unregister_memory_notifier(&cmm_mem_nb);
6036c9d2909SKay Sievers 	cmm_unregister_sysfs(&cmm_dev);
604fecba962SBrian King out_reboot_notifier:
605fecba962SBrian King 	unregister_reboot_notifier(&cmm_reboot_nb);
60684af458bSBrian King out_oom_notifier:
60784af458bSBrian King 	unregister_oom_notifier(&cmm_oom_nb);
608fe030c9bSDavid Hildenbrand out_balloon_compaction:
60984af458bSBrian King 	return rc;
61084af458bSBrian King }
61184af458bSBrian King 
61284af458bSBrian King /**
61384af458bSBrian King  * cmm_exit - Module exit
61484af458bSBrian King  *
61584af458bSBrian King  * Return value:
61684af458bSBrian King  * 	nothing
61784af458bSBrian King  **/
cmm_exit(void)61884af458bSBrian King static void cmm_exit(void)
61984af458bSBrian King {
62084af458bSBrian King 	if (cmm_thread_ptr)
62184af458bSBrian King 		kthread_stop(cmm_thread_ptr);
62284af458bSBrian King 	unregister_oom_notifier(&cmm_oom_nb);
623fecba962SBrian King 	unregister_reboot_notifier(&cmm_reboot_nb);
62414b8a76bSRobert Jennings 	unregister_memory_notifier(&cmm_mem_nb);
6251ef2f06bSDavid Hildenbrand 	cmm_free_pages(atomic_long_read(&loaned_pages));
6266c9d2909SKay Sievers 	cmm_unregister_sysfs(&cmm_dev);
62784af458bSBrian King }
62884af458bSBrian King 
62984af458bSBrian King /**
63084af458bSBrian King  * cmm_set_disable - Disable/Enable CMM
63184af458bSBrian King  *
63284af458bSBrian King  * Return value:
63384af458bSBrian King  * 	0 on success / other on failure
63484af458bSBrian King  **/
cmm_set_disable(const char * val,const struct kernel_param * kp)635e4dca7b7SKees Cook static int cmm_set_disable(const char *val, const struct kernel_param *kp)
63684af458bSBrian King {
63784af458bSBrian King 	int disable = simple_strtoul(val, NULL, 10);
63884af458bSBrian King 
63984af458bSBrian King 	if (disable != 0 && disable != 1)
64084af458bSBrian King 		return -EINVAL;
64184af458bSBrian King 
64284af458bSBrian King 	if (disable && !cmm_disabled) {
64384af458bSBrian King 		if (cmm_thread_ptr)
64484af458bSBrian King 			kthread_stop(cmm_thread_ptr);
64584af458bSBrian King 		cmm_thread_ptr = NULL;
6461ef2f06bSDavid Hildenbrand 		cmm_free_pages(atomic_long_read(&loaned_pages));
64784af458bSBrian King 	} else if (!disable && cmm_disabled) {
64884af458bSBrian King 		cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
64984af458bSBrian King 		if (IS_ERR(cmm_thread_ptr))
65084af458bSBrian King 			return PTR_ERR(cmm_thread_ptr);
65184af458bSBrian King 	}
65284af458bSBrian King 
65384af458bSBrian King 	cmm_disabled = disable;
65484af458bSBrian King 	return 0;
65584af458bSBrian King }
65684af458bSBrian King 
65784af458bSBrian King module_param_call(disable, cmm_set_disable, param_get_uint,
65857ad583fSRussell Currey 		  &cmm_disabled, 0644);
65984af458bSBrian King MODULE_PARM_DESC(disable, "Disable CMM. Set to 1 to disable. "
66084af458bSBrian King 		 "[Default=" __stringify(CMM_DISABLE) "]");
66184af458bSBrian King 
66284af458bSBrian King module_init(cmm_init);
66384af458bSBrian King module_exit(cmm_exit);
664