1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Collaborative memory management interface.
5 * Copyright (C) 2008 IBM Corporation
6 * Author(s): Brian King (brking@linux.vnet.ibm.com),
9 #include <linux/ctype.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
13 #include <linux/gfp.h>
14 #include <linux/kthread.h>
15 #include <linux/module.h>
16 #include <linux/oom.h>
17 #include <linux/reboot.h>
18 #include <linux/sched.h>
19 #include <linux/stringify.h>
20 #include <linux/swap.h>
21 #include <linux/device.h>
22 #include <asm/firmware.h>
23 #include <asm/hvcall.h>
25 #include <asm/pgalloc.h>
26 #include <linux/uaccess.h>
27 #include <linux/memory.h>
28 #include <asm/plpar_wrappers.h>
32 #define CMM_DRIVER_VERSION "1.0.0"
33 #define CMM_DEFAULT_DELAY 1
34 #define CMM_HOTPLUG_DELAY 5
37 #define CMM_OOM_KB 1024
38 #define CMM_MIN_MEM_MB 256
39 #define KB2PAGES(_p) ((_p)>>(PAGE_SHIFT-10))
40 #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
42 #define CMM_MEM_HOTPLUG_PRI 1
44 static unsigned int delay = CMM_DEFAULT_DELAY;
45 static unsigned int hotplug_delay = CMM_HOTPLUG_DELAY;
46 static unsigned int oom_kb = CMM_OOM_KB;
47 static unsigned int cmm_debug = CMM_DEBUG;
48 static unsigned int cmm_disabled = CMM_DISABLE;
49 static unsigned long min_mem_mb = CMM_MIN_MEM_MB;
50 static struct device cmm_dev;
52 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
53 MODULE_DESCRIPTION("IBM System p Collaborative Memory Manager");
54 MODULE_LICENSE("GPL");
55 MODULE_VERSION(CMM_DRIVER_VERSION);
57 module_param_named(delay, delay, uint, 0644);
58 MODULE_PARM_DESC(delay, "Delay (in seconds) between polls to query hypervisor paging requests. "
59 "[Default=" __stringify(CMM_DEFAULT_DELAY) "]");
60 module_param_named(hotplug_delay, hotplug_delay, uint, 0644);
61 MODULE_PARM_DESC(hotplug_delay, "Delay (in seconds) after memory hotplug remove "
62 "before loaning resumes. "
63 "[Default=" __stringify(CMM_HOTPLUG_DELAY) "]");
64 module_param_named(oom_kb, oom_kb, uint, 0644);
65 MODULE_PARM_DESC(oom_kb, "Amount of memory in kb to free on OOM. "
66 "[Default=" __stringify(CMM_OOM_KB) "]");
67 module_param_named(min_mem_mb, min_mem_mb, ulong, 0644);
68 MODULE_PARM_DESC(min_mem_mb, "Minimum amount of memory (in MB) to not balloon. "
69 "[Default=" __stringify(CMM_MIN_MEM_MB) "]");
70 module_param_named(debug, cmm_debug, uint, 0644);
71 MODULE_PARM_DESC(debug, "Enable module debugging logging. Set to 1 to enable. "
72 "[Default=" __stringify(CMM_DEBUG) "]");
74 #define cmm_dbg(...) if (cmm_debug) { printk(KERN_INFO "cmm: "__VA_ARGS__); }
76 static atomic_long_t loaned_pages;
77 static unsigned long loaned_pages_target;
78 static unsigned long oom_freed_pages;
80 static LIST_HEAD(cmm_page_list);
81 static DEFINE_SPINLOCK(cmm_lock);
83 static DEFINE_MUTEX(hotplug_mutex);
84 static int hotplug_occurred; /* protected by the hotplug mutex */
86 static struct task_struct *cmm_thread_ptr;
88 static long plpar_page_set_loaned(struct page *page)
90 const unsigned long vpa = page_to_phys(page);
91 unsigned long cmo_page_sz = cmo_get_page_size();
95 for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
96 rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED, vpa + i, 0);
98 for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
99 plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE,
100 vpa + i - cmo_page_sz, 0);
105 static long plpar_page_set_active(struct page *page)
107 const unsigned long vpa = page_to_phys(page);
108 unsigned long cmo_page_sz = cmo_get_page_size();
112 for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
113 rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE, vpa + i, 0);
115 for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
116 plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED,
117 vpa + i - cmo_page_sz, 0);
123 * cmm_alloc_pages - Allocate pages and mark them as loaned
124 * @nr: number of pages to allocate
127 * number of pages requested to be allocated which were not
129 static long cmm_alloc_pages(long nr)
134 cmm_dbg("Begin request for %ld pages\n", nr);
137 /* Exit if a hotplug operation is in progress or occurred */
138 if (mutex_trylock(&hotplug_mutex)) {
139 if (hotplug_occurred) {
140 mutex_unlock(&hotplug_mutex);
143 mutex_unlock(&hotplug_mutex);
148 page = alloc_page(GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY |
152 spin_lock(&cmm_lock);
153 rc = plpar_page_set_loaned(page);
155 pr_err("%s: Can not set page to loaned. rc=%ld\n", __func__, rc);
156 spin_unlock(&cmm_lock);
161 list_add(&page->lru, &cmm_page_list);
162 atomic_long_inc(&loaned_pages);
163 adjust_managed_page_count(page, -1);
164 spin_unlock(&cmm_lock);
168 cmm_dbg("End request with %ld pages unfulfilled\n", nr);
173 * cmm_free_pages - Free pages and mark them as active
174 * @nr: number of pages to free
177 * number of pages requested to be freed which were not
179 static long cmm_free_pages(long nr)
181 struct page *page, *tmp;
183 cmm_dbg("Begin free of %ld pages.\n", nr);
184 spin_lock(&cmm_lock);
185 list_for_each_entry_safe(page, tmp, &cmm_page_list, lru) {
188 plpar_page_set_active(page);
189 list_del(&page->lru);
190 adjust_managed_page_count(page, 1);
192 atomic_long_dec(&loaned_pages);
195 spin_unlock(&cmm_lock);
196 cmm_dbg("End request with %ld pages unfulfilled\n", nr);
201 * cmm_oom_notify - OOM notifier
202 * @self: notifier block struct
204 * @parm: returned - number of pages freed
209 static int cmm_oom_notify(struct notifier_block *self,
210 unsigned long dummy, void *parm)
212 unsigned long *freed = parm;
213 long nr = KB2PAGES(oom_kb);
215 cmm_dbg("OOM processing started\n");
216 nr = cmm_free_pages(nr);
217 loaned_pages_target = atomic_long_read(&loaned_pages);
218 *freed += KB2PAGES(oom_kb) - nr;
219 oom_freed_pages += KB2PAGES(oom_kb) - nr;
220 cmm_dbg("OOM processing complete\n");
225 * cmm_get_mpp - Read memory performance parameters
227 * Makes hcall to query the current page loan request from the hypervisor.
232 static void cmm_get_mpp(void)
234 const long __loaned_pages = atomic_long_read(&loaned_pages);
235 const long total_pages = totalram_pages() + __loaned_pages;
237 struct hvcall_mpp_data mpp_data;
238 signed long active_pages_target, page_loan_request, target;
239 signed long min_mem_pages = (min_mem_mb * 1024 * 1024) / PAGE_SIZE;
241 rc = h_get_mpp(&mpp_data);
246 page_loan_request = div_s64((s64)mpp_data.loan_request, PAGE_SIZE);
247 target = page_loan_request + __loaned_pages;
249 if (target < 0 || total_pages < min_mem_pages)
252 if (target > oom_freed_pages)
253 target -= oom_freed_pages;
257 active_pages_target = total_pages - target;
259 if (min_mem_pages > active_pages_target)
260 target = total_pages - min_mem_pages;
265 loaned_pages_target = target;
267 cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
268 page_loan_request, __loaned_pages, loaned_pages_target,
269 oom_freed_pages, totalram_pages());
272 static struct notifier_block cmm_oom_nb = {
273 .notifier_call = cmm_oom_notify
277 * cmm_thread - CMM task thread
283 static int cmm_thread(void *dummy)
285 unsigned long timeleft;
289 timeleft = msleep_interruptible(delay * 1000);
291 if (kthread_should_stop() || timeleft)
294 if (mutex_trylock(&hotplug_mutex)) {
295 if (hotplug_occurred) {
296 hotplug_occurred = 0;
297 mutex_unlock(&hotplug_mutex);
298 cmm_dbg("Hotplug operation has occurred, "
299 "loaning activity suspended "
302 timeleft = msleep_interruptible(hotplug_delay *
304 if (kthread_should_stop() || timeleft)
308 mutex_unlock(&hotplug_mutex);
310 cmm_dbg("Hotplug operation in progress, activity "
317 __loaned_pages = atomic_long_read(&loaned_pages);
318 if (loaned_pages_target > __loaned_pages) {
319 if (cmm_alloc_pages(loaned_pages_target - __loaned_pages))
320 loaned_pages_target = __loaned_pages;
321 } else if (loaned_pages_target < __loaned_pages)
322 cmm_free_pages(__loaned_pages - loaned_pages_target);
327 #define CMM_SHOW(name, format, args...) \
328 static ssize_t show_##name(struct device *dev, \
329 struct device_attribute *attr, \
332 return sprintf(buf, format, ##args); \
334 static DEVICE_ATTR(name, 0444, show_##name, NULL)
336 CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(atomic_long_read(&loaned_pages)));
337 CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
339 static ssize_t show_oom_pages(struct device *dev,
340 struct device_attribute *attr, char *buf)
342 return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
345 static ssize_t store_oom_pages(struct device *dev,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
349 unsigned long val = simple_strtoul (buf, NULL, 10);
351 if (!capable(CAP_SYS_ADMIN))
360 static DEVICE_ATTR(oom_freed_kb, 0644,
361 show_oom_pages, store_oom_pages);
363 static struct device_attribute *cmm_attrs[] = {
365 &dev_attr_loaned_target_kb,
366 &dev_attr_oom_freed_kb,
369 static struct bus_type cmm_subsys = {
374 static void cmm_release_device(struct device *dev)
379 * cmm_sysfs_register - Register with sysfs
382 * 0 on success / other on failure
384 static int cmm_sysfs_register(struct device *dev)
388 if ((rc = subsys_system_register(&cmm_subsys, NULL)))
392 dev->bus = &cmm_subsys;
393 dev->release = cmm_release_device;
395 if ((rc = device_register(dev)))
396 goto subsys_unregister;
398 for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
399 if ((rc = device_create_file(dev, cmm_attrs[i])))
407 device_remove_file(dev, cmm_attrs[i]);
408 device_unregister(dev);
410 bus_unregister(&cmm_subsys);
415 * cmm_unregister_sysfs - Unregister from sysfs
418 static void cmm_unregister_sysfs(struct device *dev)
422 for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++)
423 device_remove_file(dev, cmm_attrs[i]);
424 device_unregister(dev);
425 bus_unregister(&cmm_subsys);
429 * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
432 static int cmm_reboot_notifier(struct notifier_block *nb,
433 unsigned long action, void *unused)
435 if (action == SYS_RESTART) {
437 kthread_stop(cmm_thread_ptr);
438 cmm_thread_ptr = NULL;
439 cmm_free_pages(atomic_long_read(&loaned_pages));
444 static struct notifier_block cmm_reboot_nb = {
445 .notifier_call = cmm_reboot_notifier,
449 * cmm_memory_cb - Handle memory hotplug notifier calls
450 * @self: notifier block struct
451 * @action: action to take
452 * @arg: struct memory_notify data for handler
455 * NOTIFY_OK or notifier error based on subfunction return value
458 static int cmm_memory_cb(struct notifier_block *self,
459 unsigned long action, void *arg)
464 case MEM_GOING_OFFLINE:
465 mutex_lock(&hotplug_mutex);
466 hotplug_occurred = 1;
469 case MEM_CANCEL_OFFLINE:
470 mutex_unlock(&hotplug_mutex);
471 cmm_dbg("Memory offline operation complete.\n");
473 case MEM_GOING_ONLINE:
475 case MEM_CANCEL_ONLINE:
479 return notifier_from_errno(ret);
482 static struct notifier_block cmm_mem_nb = {
483 .notifier_call = cmm_memory_cb,
484 .priority = CMM_MEM_HOTPLUG_PRI
488 * cmm_init - Module initialization
491 * 0 on success / other on failure
493 static int cmm_init(void)
497 if (!firmware_has_feature(FW_FEATURE_CMO))
500 if ((rc = register_oom_notifier(&cmm_oom_nb)) < 0)
503 if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
504 goto out_oom_notifier;
506 if ((rc = cmm_sysfs_register(&cmm_dev)))
507 goto out_reboot_notifier;
509 rc = register_memory_notifier(&cmm_mem_nb);
511 goto out_unregister_notifier;
516 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
517 if (IS_ERR(cmm_thread_ptr)) {
518 rc = PTR_ERR(cmm_thread_ptr);
519 goto out_unregister_notifier;
523 out_unregister_notifier:
524 unregister_memory_notifier(&cmm_mem_nb);
525 cmm_unregister_sysfs(&cmm_dev);
527 unregister_reboot_notifier(&cmm_reboot_nb);
529 unregister_oom_notifier(&cmm_oom_nb);
534 * cmm_exit - Module exit
539 static void cmm_exit(void)
542 kthread_stop(cmm_thread_ptr);
543 unregister_oom_notifier(&cmm_oom_nb);
544 unregister_reboot_notifier(&cmm_reboot_nb);
545 unregister_memory_notifier(&cmm_mem_nb);
546 cmm_free_pages(atomic_long_read(&loaned_pages));
547 cmm_unregister_sysfs(&cmm_dev);
551 * cmm_set_disable - Disable/Enable CMM
554 * 0 on success / other on failure
556 static int cmm_set_disable(const char *val, const struct kernel_param *kp)
558 int disable = simple_strtoul(val, NULL, 10);
560 if (disable != 0 && disable != 1)
563 if (disable && !cmm_disabled) {
565 kthread_stop(cmm_thread_ptr);
566 cmm_thread_ptr = NULL;
567 cmm_free_pages(atomic_long_read(&loaned_pages));
568 } else if (!disable && cmm_disabled) {
569 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
570 if (IS_ERR(cmm_thread_ptr))
571 return PTR_ERR(cmm_thread_ptr);
574 cmm_disabled = disable;
578 module_param_call(disable, cmm_set_disable, param_get_uint,
579 &cmm_disabled, 0644);
580 MODULE_PARM_DESC(disable, "Disable CMM. Set to 1 to disable. "
581 "[Default=" __stringify(CMM_DISABLE) "]");
583 module_init(cmm_init);
584 module_exit(cmm_exit);