root/arch/powerpc/platforms/pseries/cmm.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. plpar_page_set_loaned
  2. plpar_page_set_active
  3. cmm_alloc_pages
  4. cmm_free_pages
  5. cmm_oom_notify
  6. cmm_get_mpp
  7. cmm_thread
  8. show_oom_pages
  9. store_oom_pages
  10. cmm_release_device
  11. cmm_sysfs_register
  12. cmm_unregister_sysfs
  13. cmm_reboot_notifier
  14. cmm_count_pages
  15. cmm_memory_isolate_cb
  16. cmm_mem_going_offline
  17. cmm_memory_cb
  18. cmm_init
  19. cmm_exit
  20. cmm_set_disable

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * Collaborative memory management interface.
   4  *
   5  * Copyright (C) 2008 IBM Corporation
   6  * Author(s): Brian King (brking@linux.vnet.ibm.com),
   7  */
   8 
   9 #include <linux/ctype.h>
  10 #include <linux/delay.h>
  11 #include <linux/errno.h>
  12 #include <linux/fs.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>
  24 #include <asm/mmu.h>
  25 #include <asm/pgalloc.h>
  26 #include <linux/uaccess.h>
  27 #include <linux/memory.h>
  28 #include <asm/plpar_wrappers.h>
  29 
  30 #include "pseries.h"
  31 
  32 #define CMM_DRIVER_VERSION      "1.0.0"
  33 #define CMM_DEFAULT_DELAY       1
  34 #define CMM_HOTPLUG_DELAY       5
  35 #define CMM_DEBUG                       0
  36 #define CMM_DISABLE             0
  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))
  41 /*
  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.
  44  */
  45 #define CMM_MEM_HOTPLUG_PRI     1
  46 #define CMM_MEM_ISOLATE_PRI     15
  47 
  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;
  55 
  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);
  60 
  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) "]");
  77 
  78 #define CMM_NR_PAGES ((PAGE_SIZE - sizeof(void *) - sizeof(unsigned long)) / sizeof(unsigned long))
  79 
  80 #define cmm_dbg(...) if (cmm_debug) { printk(KERN_INFO "cmm: "__VA_ARGS__); }
  81 
  82 struct cmm_page_array {
  83         struct cmm_page_array *next;
  84         unsigned long index;
  85         unsigned long page[CMM_NR_PAGES];
  86 };
  87 
  88 static unsigned long loaned_pages;
  89 static unsigned long loaned_pages_target;
  90 static unsigned long oom_freed_pages;
  91 
  92 static struct cmm_page_array *cmm_page_list;
  93 static DEFINE_SPINLOCK(cmm_lock);
  94 
  95 static DEFINE_MUTEX(hotplug_mutex);
  96 static int hotplug_occurred; /* protected by the hotplug mutex */
  97 
  98 static struct task_struct *cmm_thread_ptr;
  99 
 100 static long plpar_page_set_loaned(unsigned long vpa)
 101 {
 102         unsigned long cmo_page_sz = cmo_get_page_size();
 103         long rc = 0;
 104         int i;
 105 
 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);
 108 
 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);
 112 
 113         return rc;
 114 }
 115 
 116 static long plpar_page_set_active(unsigned long vpa)
 117 {
 118         unsigned long cmo_page_sz = cmo_get_page_size();
 119         long rc = 0;
 120         int i;
 121 
 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);
 124 
 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);
 128 
 129         return rc;
 130 }
 131 
 132 /**
 133  * cmm_alloc_pages - Allocate pages and mark them as loaned
 134  * @nr: number of pages to allocate
 135  *
 136  * Return value:
 137  *      number of pages requested to be allocated which were not
 138  **/
 139 static long cmm_alloc_pages(long nr)
 140 {
 141         struct cmm_page_array *pa, *npa;
 142         unsigned long addr;
 143         long rc;
 144 
 145         cmm_dbg("Begin request for %ld pages\n", nr);
 146 
 147         while (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);
 152                                 break;
 153                         }
 154                         mutex_unlock(&hotplug_mutex);
 155                 } else {
 156                         break;
 157                 }
 158 
 159                 addr = __get_free_page(GFP_NOIO | __GFP_NOWARN |
 160                                        __GFP_NORETRY | __GFP_NOMEMALLOC);
 161                 if (!addr)
 162                         break;
 163                 spin_lock(&cmm_lock);
 164                 pa = cmm_page_list;
 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);
 171                         if (!npa) {
 172                                 pr_info("%s: Can not allocate new page list\n", __func__);
 173                                 free_page(addr);
 174                                 break;
 175                         }
 176                         spin_lock(&cmm_lock);
 177                         pa = cmm_page_list;
 178 
 179                         if (!pa || pa->index >= CMM_NR_PAGES) {
 180                                 npa->next = pa;
 181                                 npa->index = 0;
 182                                 pa = npa;
 183                                 cmm_page_list = pa;
 184                         } else
 185                                 free_page((unsigned long) npa);
 186                 }
 187 
 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);
 191                         free_page(addr);
 192                         break;
 193                 }
 194 
 195                 pa->page[pa->index++] = addr;
 196                 loaned_pages++;
 197                 totalram_pages_dec();
 198                 spin_unlock(&cmm_lock);
 199                 nr--;
 200         }
 201 
 202         cmm_dbg("End request with %ld pages unfulfilled\n", nr);
 203         return nr;
 204 }
 205 
 206 /**
 207  * cmm_free_pages - Free pages and mark them as active
 208  * @nr: number of pages to free
 209  *
 210  * Return value:
 211  *      number of pages requested to be freed which were not
 212  **/
 213 static long cmm_free_pages(long nr)
 214 {
 215         struct cmm_page_array *pa;
 216         unsigned long addr;
 217 
 218         cmm_dbg("Begin free of %ld pages.\n", nr);
 219         spin_lock(&cmm_lock);
 220         pa = cmm_page_list;
 221         while (nr) {
 222                 if (!pa || pa->index <= 0)
 223                         break;
 224                 addr = pa->page[--pa->index];
 225 
 226                 if (pa->index == 0) {
 227                         pa = pa->next;
 228                         free_page((unsigned long) cmm_page_list);
 229                         cmm_page_list = pa;
 230                 }
 231 
 232                 plpar_page_set_active(__pa(addr));
 233                 free_page(addr);
 234                 loaned_pages--;
 235                 nr--;
 236                 totalram_pages_inc();
 237         }
 238         spin_unlock(&cmm_lock);
 239         cmm_dbg("End request with %ld pages unfulfilled\n", nr);
 240         return nr;
 241 }
 242 
 243 /**
 244  * cmm_oom_notify - OOM notifier
 245  * @self:       notifier block struct
 246  * @dummy:      not used
 247  * @parm:       returned - number of pages freed
 248  *
 249  * Return value:
 250  *      NOTIFY_OK
 251  **/
 252 static int cmm_oom_notify(struct notifier_block *self,
 253                           unsigned long dummy, void *parm)
 254 {
 255         unsigned long *freed = parm;
 256         long nr = KB2PAGES(oom_kb);
 257 
 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");
 264         return NOTIFY_OK;
 265 }
 266 
 267 /**
 268  * cmm_get_mpp - Read memory performance parameters
 269  *
 270  * Makes hcall to query the current page loan request from the hypervisor.
 271  *
 272  * Return value:
 273  *      nothing
 274  **/
 275 static void cmm_get_mpp(void)
 276 {
 277         int rc;
 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;
 282 
 283         rc = h_get_mpp(&mpp_data);
 284 
 285         if (rc != H_SUCCESS)
 286                 return;
 287 
 288         page_loan_request = div_s64((s64)mpp_data.loan_request, PAGE_SIZE);
 289         target = page_loan_request + (signed long)loaned_pages;
 290 
 291         if (target < 0 || total_pages < min_mem_pages)
 292                 target = 0;
 293 
 294         if (target > oom_freed_pages)
 295                 target -= oom_freed_pages;
 296         else
 297                 target = 0;
 298 
 299         active_pages_target = total_pages - target;
 300 
 301         if (min_mem_pages > active_pages_target)
 302                 target = total_pages - min_mem_pages;
 303 
 304         if (target < 0)
 305                 target = 0;
 306 
 307         loaned_pages_target = target;
 308 
 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());
 312 }
 313 
 314 static struct notifier_block cmm_oom_nb = {
 315         .notifier_call = cmm_oom_notify
 316 };
 317 
 318 /**
 319  * cmm_thread - CMM task thread
 320  * @dummy:      not used
 321  *
 322  * Return value:
 323  *      0
 324  **/
 325 static int cmm_thread(void *dummy)
 326 {
 327         unsigned long timeleft;
 328 
 329         while (1) {
 330                 timeleft = msleep_interruptible(delay * 1000);
 331 
 332                 if (kthread_should_stop() || timeleft)
 333                         break;
 334 
 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 "
 341                                                 "for %d seconds.\n",
 342                                                 hotplug_delay);
 343                                 timeleft = msleep_interruptible(hotplug_delay *
 344                                                 1000);
 345                                 if (kthread_should_stop() || timeleft)
 346                                         break;
 347                                 continue;
 348                         }
 349                         mutex_unlock(&hotplug_mutex);
 350                 } else {
 351                         cmm_dbg("Hotplug operation in progress, activity "
 352                                         "suspended\n");
 353                         continue;
 354                 }
 355 
 356                 cmm_get_mpp();
 357 
 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);
 363         }
 364         return 0;
 365 }
 366 
 367 #define CMM_SHOW(name, format, args...)                 \
 368         static ssize_t show_##name(struct device *dev,  \
 369                                    struct device_attribute *attr,       \
 370                                    char *buf)                   \
 371         {                                                       \
 372                 return sprintf(buf, format, ##args);            \
 373         }                                                       \
 374         static DEVICE_ATTR(name, 0444, show_##name, NULL)
 375 
 376 CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(loaned_pages));
 377 CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
 378 
 379 static ssize_t show_oom_pages(struct device *dev,
 380                               struct device_attribute *attr, char *buf)
 381 {
 382         return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
 383 }
 384 
 385 static ssize_t store_oom_pages(struct device *dev,
 386                                struct device_attribute *attr,
 387                                const char *buf, size_t count)
 388 {
 389         unsigned long val = simple_strtoul (buf, NULL, 10);
 390 
 391         if (!capable(CAP_SYS_ADMIN))
 392                 return -EPERM;
 393         if (val != 0)
 394                 return -EBADMSG;
 395 
 396         oom_freed_pages = 0;
 397         return count;
 398 }
 399 
 400 static DEVICE_ATTR(oom_freed_kb, 0644,
 401                    show_oom_pages, store_oom_pages);
 402 
 403 static struct device_attribute *cmm_attrs[] = {
 404         &dev_attr_loaned_kb,
 405         &dev_attr_loaned_target_kb,
 406         &dev_attr_oom_freed_kb,
 407 };
 408 
 409 static struct bus_type cmm_subsys = {
 410         .name = "cmm",
 411         .dev_name = "cmm",
 412 };
 413 
 414 static void cmm_release_device(struct device *dev)
 415 {
 416 }
 417 
 418 /**
 419  * cmm_sysfs_register - Register with sysfs
 420  *
 421  * Return value:
 422  *      0 on success / other on failure
 423  **/
 424 static int cmm_sysfs_register(struct device *dev)
 425 {
 426         int i, rc;
 427 
 428         if ((rc = subsys_system_register(&cmm_subsys, NULL)))
 429                 return rc;
 430 
 431         dev->id = 0;
 432         dev->bus = &cmm_subsys;
 433         dev->release = cmm_release_device;
 434 
 435         if ((rc = device_register(dev)))
 436                 goto subsys_unregister;
 437 
 438         for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
 439                 if ((rc = device_create_file(dev, cmm_attrs[i])))
 440                         goto fail;
 441         }
 442 
 443         return 0;
 444 
 445 fail:
 446         while (--i >= 0)
 447                 device_remove_file(dev, cmm_attrs[i]);
 448         device_unregister(dev);
 449 subsys_unregister:
 450         bus_unregister(&cmm_subsys);
 451         return rc;
 452 }
 453 
 454 /**
 455  * cmm_unregister_sysfs - Unregister from sysfs
 456  *
 457  **/
 458 static void cmm_unregister_sysfs(struct device *dev)
 459 {
 460         int i;
 461 
 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);
 466 }
 467 
 468 /**
 469  * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
 470  *
 471  **/
 472 static int cmm_reboot_notifier(struct notifier_block *nb,
 473                                unsigned long action, void *unused)
 474 {
 475         if (action == SYS_RESTART) {
 476                 if (cmm_thread_ptr)
 477                         kthread_stop(cmm_thread_ptr);
 478                 cmm_thread_ptr = NULL;
 479                 cmm_free_pages(loaned_pages);
 480         }
 481         return NOTIFY_DONE;
 482 }
 483 
 484 static struct notifier_block cmm_reboot_nb = {
 485         .notifier_call = cmm_reboot_notifier,
 486 };
 487 
 488 /**
 489  * cmm_count_pages - Count the number of pages loaned in a particular range.
 490  *
 491  * @arg: memory_isolate_notify structure with address range and count
 492  *
 493  * Return value:
 494  *      0 on success
 495  **/
 496 static unsigned long cmm_count_pages(void *arg)
 497 {
 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);
 502         unsigned long idx;
 503 
 504         spin_lock(&cmm_lock);
 505         pa = cmm_page_list;
 506         while (pa) {
 507                 if ((unsigned long)pa >= start && (unsigned long)pa < end)
 508                         marg->pages_found++;
 509                 for (idx = 0; idx < pa->index; idx++)
 510                         if (pa->page[idx] >= start && pa->page[idx] < end)
 511                                 marg->pages_found++;
 512                 pa = pa->next;
 513         }
 514         spin_unlock(&cmm_lock);
 515         return 0;
 516 }
 517 
 518 /**
 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
 523  *
 524  * Return value:
 525  *      NOTIFY_OK or notifier error based on subfunction return value
 526  **/
 527 static int cmm_memory_isolate_cb(struct notifier_block *self,
 528                                  unsigned long action, void *arg)
 529 {
 530         int ret = 0;
 531 
 532         if (action == MEM_ISOLATE_COUNT)
 533                 ret = cmm_count_pages(arg);
 534 
 535         return notifier_from_errno(ret);
 536 }
 537 
 538 static struct notifier_block cmm_mem_isolate_nb = {
 539         .notifier_call = cmm_memory_isolate_cb,
 540         .priority = CMM_MEM_ISOLATE_PRI
 541 };
 542 
 543 /**
 544  * cmm_mem_going_offline - Unloan pages where memory is to be removed
 545  * @arg: memory_notify structure with page range to be offlined
 546  *
 547  * Return value:
 548  *      0 on success
 549  **/
 550 static int cmm_mem_going_offline(void *arg)
 551 {
 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;
 556         unsigned long idx;
 557         unsigned long freed = 0;
 558 
 559         cmm_dbg("Memory going offline, searching 0x%lx (%ld pages).\n",
 560                         start_page, marg->nr_pages);
 561         spin_lock(&cmm_lock);
 562 
 563         /* Search the page list for pages in the range to be offlined */
 564         pa_last = pa_curr = cmm_page_list;
 565         while (pa_curr) {
 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))
 569                                 continue;
 570 
 571                         plpar_page_set_active(__pa(pa_curr->page[idx]));
 572                         free_page(pa_curr->page[idx]);
 573                         freed++;
 574                         loaned_pages--;
 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;
 583                         }
 584                 }
 585                 pa_curr = pa_curr->next;
 586         }
 587 
 588         /* Search for page list structures in the range to be offlined */
 589         pa_last = NULL;
 590         pa_curr = cmm_page_list;
 591         while (pa_curr) {
 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);
 597                         if (!npa) {
 598                                 spin_unlock(&cmm_lock);
 599                                 cmm_dbg("Failed to allocate memory for list "
 600                                                 "management. Memory hotplug "
 601                                                 "failed.\n");
 602                                 return -ENOMEM;
 603                         }
 604                         memcpy(npa, pa_curr, PAGE_SIZE);
 605                         if (pa_curr == cmm_page_list)
 606                                 cmm_page_list = npa;
 607                         if (pa_last)
 608                                 pa_last->next = npa;
 609                         free_page((unsigned long) pa_curr);
 610                         freed++;
 611                         pa_curr = npa;
 612                 }
 613 
 614                 pa_last = pa_curr;
 615                 pa_curr = pa_curr->next;
 616         }
 617 
 618         spin_unlock(&cmm_lock);
 619         cmm_dbg("Released %ld pages in the search range.\n", freed);
 620 
 621         return 0;
 622 }
 623 
 624 /**
 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
 629  *
 630  * Return value:
 631  *      NOTIFY_OK or notifier error based on subfunction return value
 632  *
 633  **/
 634 static int cmm_memory_cb(struct notifier_block *self,
 635                         unsigned long action, void *arg)
 636 {
 637         int ret = 0;
 638 
 639         switch (action) {
 640         case MEM_GOING_OFFLINE:
 641                 mutex_lock(&hotplug_mutex);
 642                 hotplug_occurred = 1;
 643                 ret = cmm_mem_going_offline(arg);
 644                 break;
 645         case MEM_OFFLINE:
 646         case MEM_CANCEL_OFFLINE:
 647                 mutex_unlock(&hotplug_mutex);
 648                 cmm_dbg("Memory offline operation complete.\n");
 649                 break;
 650         case MEM_GOING_ONLINE:
 651         case MEM_ONLINE:
 652         case MEM_CANCEL_ONLINE:
 653                 break;
 654         }
 655 
 656         return notifier_from_errno(ret);
 657 }
 658 
 659 static struct notifier_block cmm_mem_nb = {
 660         .notifier_call = cmm_memory_cb,
 661         .priority = CMM_MEM_HOTPLUG_PRI
 662 };
 663 
 664 /**
 665  * cmm_init - Module initialization
 666  *
 667  * Return value:
 668  *      0 on success / other on failure
 669  **/
 670 static int cmm_init(void)
 671 {
 672         int rc = -ENOMEM;
 673 
 674         if (!firmware_has_feature(FW_FEATURE_CMO))
 675                 return -EOPNOTSUPP;
 676 
 677         if ((rc = register_oom_notifier(&cmm_oom_nb)) < 0)
 678                 return rc;
 679 
 680         if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
 681                 goto out_oom_notifier;
 682 
 683         if ((rc = cmm_sysfs_register(&cmm_dev)))
 684                 goto out_reboot_notifier;
 685 
 686         if (register_memory_notifier(&cmm_mem_nb) ||
 687             register_memory_isolate_notifier(&cmm_mem_isolate_nb))
 688                 goto out_unregister_notifier;
 689 
 690         if (cmm_disabled)
 691                 return rc;
 692 
 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;
 697         }
 698 
 699         return rc;
 700 
 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);
 705 out_reboot_notifier:
 706         unregister_reboot_notifier(&cmm_reboot_nb);
 707 out_oom_notifier:
 708         unregister_oom_notifier(&cmm_oom_nb);
 709         return rc;
 710 }
 711 
 712 /**
 713  * cmm_exit - Module exit
 714  *
 715  * Return value:
 716  *      nothing
 717  **/
 718 static void cmm_exit(void)
 719 {
 720         if (cmm_thread_ptr)
 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);
 728 }
 729 
 730 /**
 731  * cmm_set_disable - Disable/Enable CMM
 732  *
 733  * Return value:
 734  *      0 on success / other on failure
 735  **/
 736 static int cmm_set_disable(const char *val, const struct kernel_param *kp)
 737 {
 738         int disable = simple_strtoul(val, NULL, 10);
 739 
 740         if (disable != 0 && disable != 1)
 741                 return -EINVAL;
 742 
 743         if (disable && !cmm_disabled) {
 744                 if (cmm_thread_ptr)
 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);
 752         }
 753 
 754         cmm_disabled = disable;
 755         return 0;
 756 }
 757 
 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) "]");
 762 
 763 module_init(cmm_init);
 764 module_exit(cmm_exit);

/* [<][>][^][v][top][bottom][index][help] */