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 * The priority level tries to ensure that this notifier is called as
43 * late as possible to reduce thrashing in the shared memory pool.
45 #define CMM_MEM_HOTPLUG_PRI 1
46 #define CMM_MEM_ISOLATE_PRI 15
48 static unsigned int delay = CMM_DEFAULT_DELAY;
49 static unsigned int hotplug_delay = CMM_HOTPLUG_DELAY;
50 static unsigned int oom_kb = CMM_OOM_KB;
51 static unsigned int cmm_debug = CMM_DEBUG;
52 static unsigned int cmm_disabled = CMM_DISABLE;
53 static unsigned long min_mem_mb = CMM_MIN_MEM_MB;
54 static struct device cmm_dev;
56 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
57 MODULE_DESCRIPTION("IBM System p Collaborative Memory Manager");
58 MODULE_LICENSE("GPL");
59 MODULE_VERSION(CMM_DRIVER_VERSION);
61 module_param_named(delay, delay, uint, 0644);
62 MODULE_PARM_DESC(delay, "Delay (in seconds) between polls to query hypervisor paging requests. "
63 "[Default=" __stringify(CMM_DEFAULT_DELAY) "]");
64 module_param_named(hotplug_delay, hotplug_delay, uint, 0644);
65 MODULE_PARM_DESC(hotplug_delay, "Delay (in seconds) after memory hotplug remove "
66 "before loaning resumes. "
67 "[Default=" __stringify(CMM_HOTPLUG_DELAY) "]");
68 module_param_named(oom_kb, oom_kb, uint, 0644);
69 MODULE_PARM_DESC(oom_kb, "Amount of memory in kb to free on OOM. "
70 "[Default=" __stringify(CMM_OOM_KB) "]");
71 module_param_named(min_mem_mb, min_mem_mb, ulong, 0644);
72 MODULE_PARM_DESC(min_mem_mb, "Minimum amount of memory (in MB) to not balloon. "
73 "[Default=" __stringify(CMM_MIN_MEM_MB) "]");
74 module_param_named(debug, cmm_debug, uint, 0644);
75 MODULE_PARM_DESC(debug, "Enable module debugging logging. Set to 1 to enable. "
76 "[Default=" __stringify(CMM_DEBUG) "]");
78 #define CMM_NR_PAGES ((PAGE_SIZE - sizeof(void *) - sizeof(unsigned long)) / sizeof(unsigned long))
80 #define cmm_dbg(...) if (cmm_debug) { printk(KERN_INFO "cmm: "__VA_ARGS__); }
82 struct cmm_page_array {
83 struct cmm_page_array *next;
85 unsigned long page[CMM_NR_PAGES];
88 static unsigned long loaned_pages;
89 static unsigned long loaned_pages_target;
90 static unsigned long oom_freed_pages;
92 static struct cmm_page_array *cmm_page_list;
93 static DEFINE_SPINLOCK(cmm_lock);
95 static DEFINE_MUTEX(hotplug_mutex);
96 static int hotplug_occurred; /* protected by the hotplug mutex */
98 static struct task_struct *cmm_thread_ptr;
100 static long plpar_page_set_loaned(unsigned long vpa)
102 unsigned long cmo_page_sz = cmo_get_page_size();
106 for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
107 rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED, vpa + i, 0);
109 for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
110 plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE,
111 vpa + i - cmo_page_sz, 0);
116 static long plpar_page_set_active(unsigned long vpa)
118 unsigned long cmo_page_sz = cmo_get_page_size();
122 for (i = 0; !rc && i < PAGE_SIZE; i += cmo_page_sz)
123 rc = plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_ACTIVE, vpa + i, 0);
125 for (i -= cmo_page_sz; rc && i != 0; i -= cmo_page_sz)
126 plpar_hcall_norets(H_PAGE_INIT, H_PAGE_SET_LOANED,
127 vpa + i - cmo_page_sz, 0);
133 * cmm_alloc_pages - Allocate pages and mark them as loaned
134 * @nr: number of pages to allocate
137 * number of pages requested to be allocated which were not
139 static long cmm_alloc_pages(long nr)
141 struct cmm_page_array *pa, *npa;
145 cmm_dbg("Begin request for %ld pages\n", nr);
148 /* Exit if a hotplug operation is in progress or occurred */
149 if (mutex_trylock(&hotplug_mutex)) {
150 if (hotplug_occurred) {
151 mutex_unlock(&hotplug_mutex);
154 mutex_unlock(&hotplug_mutex);
159 addr = __get_free_page(GFP_NOIO | __GFP_NOWARN |
160 __GFP_NORETRY | __GFP_NOMEMALLOC);
163 spin_lock(&cmm_lock);
165 if (!pa || pa->index >= CMM_NR_PAGES) {
166 /* Need a new page for the page list. */
167 spin_unlock(&cmm_lock);
168 npa = (struct cmm_page_array *)__get_free_page(
169 GFP_NOIO | __GFP_NOWARN |
170 __GFP_NORETRY | __GFP_NOMEMALLOC);
172 pr_info("%s: Can not allocate new page list\n", __func__);
176 spin_lock(&cmm_lock);
179 if (!pa || pa->index >= CMM_NR_PAGES) {
185 free_page((unsigned long) npa);
188 if ((rc = plpar_page_set_loaned(__pa(addr)))) {
189 pr_err("%s: Can not set page to loaned. rc=%ld\n", __func__, rc);
190 spin_unlock(&cmm_lock);
195 pa->page[pa->index++] = addr;
197 totalram_pages_dec();
198 spin_unlock(&cmm_lock);
202 cmm_dbg("End request with %ld pages unfulfilled\n", nr);
207 * cmm_free_pages - Free pages and mark them as active
208 * @nr: number of pages to free
211 * number of pages requested to be freed which were not
213 static long cmm_free_pages(long nr)
215 struct cmm_page_array *pa;
218 cmm_dbg("Begin free of %ld pages.\n", nr);
219 spin_lock(&cmm_lock);
222 if (!pa || pa->index <= 0)
224 addr = pa->page[--pa->index];
226 if (pa->index == 0) {
228 free_page((unsigned long) cmm_page_list);
232 plpar_page_set_active(__pa(addr));
236 totalram_pages_inc();
238 spin_unlock(&cmm_lock);
239 cmm_dbg("End request with %ld pages unfulfilled\n", nr);
244 * cmm_oom_notify - OOM notifier
245 * @self: notifier block struct
247 * @parm: returned - number of pages freed
252 static int cmm_oom_notify(struct notifier_block *self,
253 unsigned long dummy, void *parm)
255 unsigned long *freed = parm;
256 long nr = KB2PAGES(oom_kb);
258 cmm_dbg("OOM processing started\n");
259 nr = cmm_free_pages(nr);
260 loaned_pages_target = loaned_pages;
261 *freed += KB2PAGES(oom_kb) - nr;
262 oom_freed_pages += KB2PAGES(oom_kb) - nr;
263 cmm_dbg("OOM processing complete\n");
268 * cmm_get_mpp - Read memory performance parameters
270 * Makes hcall to query the current page loan request from the hypervisor.
275 static void cmm_get_mpp(void)
278 struct hvcall_mpp_data mpp_data;
279 signed long active_pages_target, page_loan_request, target;
280 signed long total_pages = totalram_pages() + loaned_pages;
281 signed long min_mem_pages = (min_mem_mb * 1024 * 1024) / PAGE_SIZE;
283 rc = h_get_mpp(&mpp_data);
288 page_loan_request = div_s64((s64)mpp_data.loan_request, PAGE_SIZE);
289 target = page_loan_request + (signed long)loaned_pages;
291 if (target < 0 || total_pages < min_mem_pages)
294 if (target > oom_freed_pages)
295 target -= oom_freed_pages;
299 active_pages_target = total_pages - target;
301 if (min_mem_pages > active_pages_target)
302 target = total_pages - min_mem_pages;
307 loaned_pages_target = target;
309 cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
310 page_loan_request, loaned_pages, loaned_pages_target,
311 oom_freed_pages, totalram_pages());
314 static struct notifier_block cmm_oom_nb = {
315 .notifier_call = cmm_oom_notify
319 * cmm_thread - CMM task thread
325 static int cmm_thread(void *dummy)
327 unsigned long timeleft;
330 timeleft = msleep_interruptible(delay * 1000);
332 if (kthread_should_stop() || timeleft)
335 if (mutex_trylock(&hotplug_mutex)) {
336 if (hotplug_occurred) {
337 hotplug_occurred = 0;
338 mutex_unlock(&hotplug_mutex);
339 cmm_dbg("Hotplug operation has occurred, "
340 "loaning activity suspended "
343 timeleft = msleep_interruptible(hotplug_delay *
345 if (kthread_should_stop() || timeleft)
349 mutex_unlock(&hotplug_mutex);
351 cmm_dbg("Hotplug operation in progress, activity "
358 if (loaned_pages_target > loaned_pages) {
359 if (cmm_alloc_pages(loaned_pages_target - loaned_pages))
360 loaned_pages_target = loaned_pages;
361 } else if (loaned_pages_target < loaned_pages)
362 cmm_free_pages(loaned_pages - loaned_pages_target);
367 #define CMM_SHOW(name, format, args...) \
368 static ssize_t show_##name(struct device *dev, \
369 struct device_attribute *attr, \
372 return sprintf(buf, format, ##args); \
374 static DEVICE_ATTR(name, 0444, show_##name, NULL)
376 CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(loaned_pages));
377 CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
379 static ssize_t show_oom_pages(struct device *dev,
380 struct device_attribute *attr, char *buf)
382 return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
385 static ssize_t store_oom_pages(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf, size_t count)
389 unsigned long val = simple_strtoul (buf, NULL, 10);
391 if (!capable(CAP_SYS_ADMIN))
400 static DEVICE_ATTR(oom_freed_kb, 0644,
401 show_oom_pages, store_oom_pages);
403 static struct device_attribute *cmm_attrs[] = {
405 &dev_attr_loaned_target_kb,
406 &dev_attr_oom_freed_kb,
409 static struct bus_type cmm_subsys = {
414 static void cmm_release_device(struct device *dev)
419 * cmm_sysfs_register - Register with sysfs
422 * 0 on success / other on failure
424 static int cmm_sysfs_register(struct device *dev)
428 if ((rc = subsys_system_register(&cmm_subsys, NULL)))
432 dev->bus = &cmm_subsys;
433 dev->release = cmm_release_device;
435 if ((rc = device_register(dev)))
436 goto subsys_unregister;
438 for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
439 if ((rc = device_create_file(dev, cmm_attrs[i])))
447 device_remove_file(dev, cmm_attrs[i]);
448 device_unregister(dev);
450 bus_unregister(&cmm_subsys);
455 * cmm_unregister_sysfs - Unregister from sysfs
458 static void cmm_unregister_sysfs(struct device *dev)
462 for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++)
463 device_remove_file(dev, cmm_attrs[i]);
464 device_unregister(dev);
465 bus_unregister(&cmm_subsys);
469 * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
472 static int cmm_reboot_notifier(struct notifier_block *nb,
473 unsigned long action, void *unused)
475 if (action == SYS_RESTART) {
477 kthread_stop(cmm_thread_ptr);
478 cmm_thread_ptr = NULL;
479 cmm_free_pages(loaned_pages);
484 static struct notifier_block cmm_reboot_nb = {
485 .notifier_call = cmm_reboot_notifier,
489 * cmm_count_pages - Count the number of pages loaned in a particular range.
491 * @arg: memory_isolate_notify structure with address range and count
496 static unsigned long cmm_count_pages(void *arg)
498 struct memory_isolate_notify *marg = arg;
499 struct cmm_page_array *pa;
500 unsigned long start = (unsigned long)pfn_to_kaddr(marg->start_pfn);
501 unsigned long end = start + (marg->nr_pages << PAGE_SHIFT);
504 spin_lock(&cmm_lock);
507 if ((unsigned long)pa >= start && (unsigned long)pa < end)
509 for (idx = 0; idx < pa->index; idx++)
510 if (pa->page[idx] >= start && pa->page[idx] < end)
514 spin_unlock(&cmm_lock);
519 * cmm_memory_isolate_cb - Handle memory isolation notifier calls
520 * @self: notifier block struct
521 * @action: action to take
522 * @arg: struct memory_isolate_notify data for handler
525 * NOTIFY_OK or notifier error based on subfunction return value
527 static int cmm_memory_isolate_cb(struct notifier_block *self,
528 unsigned long action, void *arg)
532 if (action == MEM_ISOLATE_COUNT)
533 ret = cmm_count_pages(arg);
535 return notifier_from_errno(ret);
538 static struct notifier_block cmm_mem_isolate_nb = {
539 .notifier_call = cmm_memory_isolate_cb,
540 .priority = CMM_MEM_ISOLATE_PRI
544 * cmm_mem_going_offline - Unloan pages where memory is to be removed
545 * @arg: memory_notify structure with page range to be offlined
550 static int cmm_mem_going_offline(void *arg)
552 struct memory_notify *marg = arg;
553 unsigned long start_page = (unsigned long)pfn_to_kaddr(marg->start_pfn);
554 unsigned long end_page = start_page + (marg->nr_pages << PAGE_SHIFT);
555 struct cmm_page_array *pa_curr, *pa_last, *npa;
557 unsigned long freed = 0;
559 cmm_dbg("Memory going offline, searching 0x%lx (%ld pages).\n",
560 start_page, marg->nr_pages);
561 spin_lock(&cmm_lock);
563 /* Search the page list for pages in the range to be offlined */
564 pa_last = pa_curr = cmm_page_list;
566 for (idx = (pa_curr->index - 1); (idx + 1) > 0; idx--) {
567 if ((pa_curr->page[idx] < start_page) ||
568 (pa_curr->page[idx] >= end_page))
571 plpar_page_set_active(__pa(pa_curr->page[idx]));
572 free_page(pa_curr->page[idx]);
575 totalram_pages_inc();
576 pa_curr->page[idx] = pa_last->page[--pa_last->index];
577 if (pa_last->index == 0) {
578 if (pa_curr == pa_last)
579 pa_curr = pa_last->next;
580 pa_last = pa_last->next;
581 free_page((unsigned long)cmm_page_list);
582 cmm_page_list = pa_last;
585 pa_curr = pa_curr->next;
588 /* Search for page list structures in the range to be offlined */
590 pa_curr = cmm_page_list;
592 if (((unsigned long)pa_curr >= start_page) &&
593 ((unsigned long)pa_curr < end_page)) {
594 npa = (struct cmm_page_array *)__get_free_page(
595 GFP_NOIO | __GFP_NOWARN |
596 __GFP_NORETRY | __GFP_NOMEMALLOC);
598 spin_unlock(&cmm_lock);
599 cmm_dbg("Failed to allocate memory for list "
600 "management. Memory hotplug "
604 memcpy(npa, pa_curr, PAGE_SIZE);
605 if (pa_curr == cmm_page_list)
609 free_page((unsigned long) pa_curr);
615 pa_curr = pa_curr->next;
618 spin_unlock(&cmm_lock);
619 cmm_dbg("Released %ld pages in the search range.\n", freed);
625 * cmm_memory_cb - Handle memory hotplug notifier calls
626 * @self: notifier block struct
627 * @action: action to take
628 * @arg: struct memory_notify data for handler
631 * NOTIFY_OK or notifier error based on subfunction return value
634 static int cmm_memory_cb(struct notifier_block *self,
635 unsigned long action, void *arg)
640 case MEM_GOING_OFFLINE:
641 mutex_lock(&hotplug_mutex);
642 hotplug_occurred = 1;
643 ret = cmm_mem_going_offline(arg);
646 case MEM_CANCEL_OFFLINE:
647 mutex_unlock(&hotplug_mutex);
648 cmm_dbg("Memory offline operation complete.\n");
650 case MEM_GOING_ONLINE:
652 case MEM_CANCEL_ONLINE:
656 return notifier_from_errno(ret);
659 static struct notifier_block cmm_mem_nb = {
660 .notifier_call = cmm_memory_cb,
661 .priority = CMM_MEM_HOTPLUG_PRI
665 * cmm_init - Module initialization
668 * 0 on success / other on failure
670 static int cmm_init(void)
674 if (!firmware_has_feature(FW_FEATURE_CMO))
677 if ((rc = register_oom_notifier(&cmm_oom_nb)) < 0)
680 if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
681 goto out_oom_notifier;
683 if ((rc = cmm_sysfs_register(&cmm_dev)))
684 goto out_reboot_notifier;
686 if (register_memory_notifier(&cmm_mem_nb) ||
687 register_memory_isolate_notifier(&cmm_mem_isolate_nb))
688 goto out_unregister_notifier;
693 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
694 if (IS_ERR(cmm_thread_ptr)) {
695 rc = PTR_ERR(cmm_thread_ptr);
696 goto out_unregister_notifier;
701 out_unregister_notifier:
702 unregister_memory_notifier(&cmm_mem_nb);
703 unregister_memory_isolate_notifier(&cmm_mem_isolate_nb);
704 cmm_unregister_sysfs(&cmm_dev);
706 unregister_reboot_notifier(&cmm_reboot_nb);
708 unregister_oom_notifier(&cmm_oom_nb);
713 * cmm_exit - Module exit
718 static void cmm_exit(void)
721 kthread_stop(cmm_thread_ptr);
722 unregister_oom_notifier(&cmm_oom_nb);
723 unregister_reboot_notifier(&cmm_reboot_nb);
724 unregister_memory_notifier(&cmm_mem_nb);
725 unregister_memory_isolate_notifier(&cmm_mem_isolate_nb);
726 cmm_free_pages(loaned_pages);
727 cmm_unregister_sysfs(&cmm_dev);
731 * cmm_set_disable - Disable/Enable CMM
734 * 0 on success / other on failure
736 static int cmm_set_disable(const char *val, const struct kernel_param *kp)
738 int disable = simple_strtoul(val, NULL, 10);
740 if (disable != 0 && disable != 1)
743 if (disable && !cmm_disabled) {
745 kthread_stop(cmm_thread_ptr);
746 cmm_thread_ptr = NULL;
747 cmm_free_pages(loaned_pages);
748 } else if (!disable && cmm_disabled) {
749 cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
750 if (IS_ERR(cmm_thread_ptr))
751 return PTR_ERR(cmm_thread_ptr);
754 cmm_disabled = disable;
758 module_param_call(disable, cmm_set_disable, param_get_uint,
759 &cmm_disabled, 0644);
760 MODULE_PARM_DESC(disable, "Disable CMM. Set to 1 to disable. "
761 "[Default=" __stringify(CMM_DISABLE) "]");
763 module_init(cmm_init);
764 module_exit(cmm_exit);