linux/arch/powerpc/platforms/pseries/cmm.c
<<
>>
Prefs
   1/*
   2 * Collaborative memory management interface.
   3 *
   4 * Copyright (C) 2008 IBM Corporation
   5 * Author(s): Brian King (brking@linux.vnet.ibm.com),
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20 *
  21 */
  22
  23#include <linux/ctype.h>
  24#include <linux/delay.h>
  25#include <linux/errno.h>
  26#include <linux/fs.h>
  27#include <linux/gfp.h>
  28#include <linux/kthread.h>
  29#include <linux/module.h>
  30#include <linux/oom.h>
  31#include <linux/reboot.h>
  32#include <linux/sched.h>
  33#include <linux/stringify.h>
  34#include <linux/swap.h>
  35#include <linux/device.h>
  36#include <asm/firmware.h>
  37#include <asm/hvcall.h>
  38#include <asm/mmu.h>
  39#include <asm/pgalloc.h>
  40#include <asm/uaccess.h>
  41#include <linux/memory.h>
  42#include <asm/plpar_wrappers.h>
  43
  44#define CMM_DRIVER_VERSION      "1.0.0"
  45#define CMM_DEFAULT_DELAY       1
  46#define CMM_HOTPLUG_DELAY       5
  47#define CMM_DEBUG                       0
  48#define CMM_DISABLE             0
  49#define CMM_OOM_KB              1024
  50#define CMM_MIN_MEM_MB          256
  51#define KB2PAGES(_p)            ((_p)>>(PAGE_SHIFT-10))
  52#define PAGES2KB(_p)            ((_p)<<(PAGE_SHIFT-10))
  53/*
  54 * The priority level tries to ensure that this notifier is called as
  55 * late as possible to reduce thrashing in the shared memory pool.
  56 */
  57#define CMM_MEM_HOTPLUG_PRI     1
  58#define CMM_MEM_ISOLATE_PRI     15
  59
  60static unsigned int delay = CMM_DEFAULT_DELAY;
  61static unsigned int hotplug_delay = CMM_HOTPLUG_DELAY;
  62static unsigned int oom_kb = CMM_OOM_KB;
  63static unsigned int cmm_debug = CMM_DEBUG;
  64static unsigned int cmm_disabled = CMM_DISABLE;
  65static unsigned long min_mem_mb = CMM_MIN_MEM_MB;
  66static struct device cmm_dev;
  67
  68MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  69MODULE_DESCRIPTION("IBM System p Collaborative Memory Manager");
  70MODULE_LICENSE("GPL");
  71MODULE_VERSION(CMM_DRIVER_VERSION);
  72
  73module_param_named(delay, delay, uint, S_IRUGO | S_IWUSR);
  74MODULE_PARM_DESC(delay, "Delay (in seconds) between polls to query hypervisor paging requests. "
  75                 "[Default=" __stringify(CMM_DEFAULT_DELAY) "]");
  76module_param_named(hotplug_delay, hotplug_delay, uint, S_IRUGO | S_IWUSR);
  77MODULE_PARM_DESC(delay, "Delay (in seconds) after memory hotplug remove "
  78                 "before loaning resumes. "
  79                 "[Default=" __stringify(CMM_HOTPLUG_DELAY) "]");
  80module_param_named(oom_kb, oom_kb, uint, S_IRUGO | S_IWUSR);
  81MODULE_PARM_DESC(oom_kb, "Amount of memory in kb to free on OOM. "
  82                 "[Default=" __stringify(CMM_OOM_KB) "]");
  83module_param_named(min_mem_mb, min_mem_mb, ulong, S_IRUGO | S_IWUSR);
  84MODULE_PARM_DESC(min_mem_mb, "Minimum amount of memory (in MB) to not balloon. "
  85                 "[Default=" __stringify(CMM_MIN_MEM_MB) "]");
  86module_param_named(debug, cmm_debug, uint, S_IRUGO | S_IWUSR);
  87MODULE_PARM_DESC(debug, "Enable module debugging logging. Set to 1 to enable. "
  88                 "[Default=" __stringify(CMM_DEBUG) "]");
  89
  90#define CMM_NR_PAGES ((PAGE_SIZE - sizeof(void *) - sizeof(unsigned long)) / sizeof(unsigned long))
  91
  92#define cmm_dbg(...) if (cmm_debug) { printk(KERN_INFO "cmm: "__VA_ARGS__); }
  93
  94struct cmm_page_array {
  95        struct cmm_page_array *next;
  96        unsigned long index;
  97        unsigned long page[CMM_NR_PAGES];
  98};
  99
 100static unsigned long loaned_pages;
 101static unsigned long loaned_pages_target;
 102static unsigned long oom_freed_pages;
 103
 104static struct cmm_page_array *cmm_page_list;
 105static DEFINE_SPINLOCK(cmm_lock);
 106
 107static DEFINE_MUTEX(hotplug_mutex);
 108static int hotplug_occurred; /* protected by the hotplug mutex */
 109
 110static struct task_struct *cmm_thread_ptr;
 111
 112/**
 113 * cmm_alloc_pages - Allocate pages and mark them as loaned
 114 * @nr: number of pages to allocate
 115 *
 116 * Return value:
 117 *      number of pages requested to be allocated which were not
 118 **/
 119static long cmm_alloc_pages(long nr)
 120{
 121        struct cmm_page_array *pa, *npa;
 122        unsigned long addr;
 123        long rc;
 124
 125        cmm_dbg("Begin request for %ld pages\n", nr);
 126
 127        while (nr) {
 128                /* Exit if a hotplug operation is in progress or occurred */
 129                if (mutex_trylock(&hotplug_mutex)) {
 130                        if (hotplug_occurred) {
 131                                mutex_unlock(&hotplug_mutex);
 132                                break;
 133                        }
 134                        mutex_unlock(&hotplug_mutex);
 135                } else {
 136                        break;
 137                }
 138
 139                addr = __get_free_page(GFP_NOIO | __GFP_NOWARN |
 140                                       __GFP_NORETRY | __GFP_NOMEMALLOC);
 141                if (!addr)
 142                        break;
 143                spin_lock(&cmm_lock);
 144                pa = cmm_page_list;
 145                if (!pa || pa->index >= CMM_NR_PAGES) {
 146                        /* Need a new page for the page list. */
 147                        spin_unlock(&cmm_lock);
 148                        npa = (struct cmm_page_array *)__get_free_page(
 149                                        GFP_NOIO | __GFP_NOWARN |
 150                                        __GFP_NORETRY | __GFP_NOMEMALLOC);
 151                        if (!npa) {
 152                                pr_info("%s: Can not allocate new page list\n", __func__);
 153                                free_page(addr);
 154                                break;
 155                        }
 156                        spin_lock(&cmm_lock);
 157                        pa = cmm_page_list;
 158
 159                        if (!pa || pa->index >= CMM_NR_PAGES) {
 160                                npa->next = pa;
 161                                npa->index = 0;
 162                                pa = npa;
 163                                cmm_page_list = pa;
 164                        } else
 165                                free_page((unsigned long) npa);
 166                }
 167
 168                if ((rc = plpar_page_set_loaned(__pa(addr)))) {
 169                        pr_err("%s: Can not set page to loaned. rc=%ld\n", __func__, rc);
 170                        spin_unlock(&cmm_lock);
 171                        free_page(addr);
 172                        break;
 173                }
 174
 175                pa->page[pa->index++] = addr;
 176                loaned_pages++;
 177                totalram_pages--;
 178                spin_unlock(&cmm_lock);
 179                nr--;
 180        }
 181
 182        cmm_dbg("End request with %ld pages unfulfilled\n", nr);
 183        return nr;
 184}
 185
 186/**
 187 * cmm_free_pages - Free pages and mark them as active
 188 * @nr: number of pages to free
 189 *
 190 * Return value:
 191 *      number of pages requested to be freed which were not
 192 **/
 193static long cmm_free_pages(long nr)
 194{
 195        struct cmm_page_array *pa;
 196        unsigned long addr;
 197
 198        cmm_dbg("Begin free of %ld pages.\n", nr);
 199        spin_lock(&cmm_lock);
 200        pa = cmm_page_list;
 201        while (nr) {
 202                if (!pa || pa->index <= 0)
 203                        break;
 204                addr = pa->page[--pa->index];
 205
 206                if (pa->index == 0) {
 207                        pa = pa->next;
 208                        free_page((unsigned long) cmm_page_list);
 209                        cmm_page_list = pa;
 210                }
 211
 212                plpar_page_set_active(__pa(addr));
 213                free_page(addr);
 214                loaned_pages--;
 215                nr--;
 216                totalram_pages++;
 217        }
 218        spin_unlock(&cmm_lock);
 219        cmm_dbg("End request with %ld pages unfulfilled\n", nr);
 220        return nr;
 221}
 222
 223/**
 224 * cmm_oom_notify - OOM notifier
 225 * @self:       notifier block struct
 226 * @dummy:      not used
 227 * @parm:       returned - number of pages freed
 228 *
 229 * Return value:
 230 *      NOTIFY_OK
 231 **/
 232static int cmm_oom_notify(struct notifier_block *self,
 233                          unsigned long dummy, void *parm)
 234{
 235        unsigned long *freed = parm;
 236        long nr = KB2PAGES(oom_kb);
 237
 238        cmm_dbg("OOM processing started\n");
 239        nr = cmm_free_pages(nr);
 240        loaned_pages_target = loaned_pages;
 241        *freed += KB2PAGES(oom_kb) - nr;
 242        oom_freed_pages += KB2PAGES(oom_kb) - nr;
 243        cmm_dbg("OOM processing complete\n");
 244        return NOTIFY_OK;
 245}
 246
 247/**
 248 * cmm_get_mpp - Read memory performance parameters
 249 *
 250 * Makes hcall to query the current page loan request from the hypervisor.
 251 *
 252 * Return value:
 253 *      nothing
 254 **/
 255static void cmm_get_mpp(void)
 256{
 257        int rc;
 258        struct hvcall_mpp_data mpp_data;
 259        signed long active_pages_target, page_loan_request, target;
 260        signed long total_pages = totalram_pages + loaned_pages;
 261        signed long min_mem_pages = (min_mem_mb * 1024 * 1024) / PAGE_SIZE;
 262
 263        rc = h_get_mpp(&mpp_data);
 264
 265        if (rc != H_SUCCESS)
 266                return;
 267
 268        page_loan_request = div_s64((s64)mpp_data.loan_request, PAGE_SIZE);
 269        target = page_loan_request + (signed long)loaned_pages;
 270
 271        if (target < 0 || total_pages < min_mem_pages)
 272                target = 0;
 273
 274        if (target > oom_freed_pages)
 275                target -= oom_freed_pages;
 276        else
 277                target = 0;
 278
 279        active_pages_target = total_pages - target;
 280
 281        if (min_mem_pages > active_pages_target)
 282                target = total_pages - min_mem_pages;
 283
 284        if (target < 0)
 285                target = 0;
 286
 287        loaned_pages_target = target;
 288
 289        cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
 290                page_loan_request, loaned_pages, loaned_pages_target,
 291                oom_freed_pages, totalram_pages);
 292}
 293
 294static struct notifier_block cmm_oom_nb = {
 295        .notifier_call = cmm_oom_notify
 296};
 297
 298/**
 299 * cmm_thread - CMM task thread
 300 * @dummy:      not used
 301 *
 302 * Return value:
 303 *      0
 304 **/
 305static int cmm_thread(void *dummy)
 306{
 307        unsigned long timeleft;
 308
 309        while (1) {
 310                timeleft = msleep_interruptible(delay * 1000);
 311
 312                if (kthread_should_stop() || timeleft)
 313                        break;
 314
 315                if (mutex_trylock(&hotplug_mutex)) {
 316                        if (hotplug_occurred) {
 317                                hotplug_occurred = 0;
 318                                mutex_unlock(&hotplug_mutex);
 319                                cmm_dbg("Hotplug operation has occurred, "
 320                                                "loaning activity suspended "
 321                                                "for %d seconds.\n",
 322                                                hotplug_delay);
 323                                timeleft = msleep_interruptible(hotplug_delay *
 324                                                1000);
 325                                if (kthread_should_stop() || timeleft)
 326                                        break;
 327                                continue;
 328                        }
 329                        mutex_unlock(&hotplug_mutex);
 330                } else {
 331                        cmm_dbg("Hotplug operation in progress, activity "
 332                                        "suspended\n");
 333                        continue;
 334                }
 335
 336                cmm_get_mpp();
 337
 338                if (loaned_pages_target > loaned_pages) {
 339                        if (cmm_alloc_pages(loaned_pages_target - loaned_pages))
 340                                loaned_pages_target = loaned_pages;
 341                } else if (loaned_pages_target < loaned_pages)
 342                        cmm_free_pages(loaned_pages - loaned_pages_target);
 343        }
 344        return 0;
 345}
 346
 347#define CMM_SHOW(name, format, args...)                 \
 348        static ssize_t show_##name(struct device *dev,  \
 349                                   struct device_attribute *attr,       \
 350                                   char *buf)                   \
 351        {                                                       \
 352                return sprintf(buf, format, ##args);            \
 353        }                                                       \
 354        static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
 355
 356CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(loaned_pages));
 357CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
 358
 359static ssize_t show_oom_pages(struct device *dev,
 360                              struct device_attribute *attr, char *buf)
 361{
 362        return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
 363}
 364
 365static ssize_t store_oom_pages(struct device *dev,
 366                               struct device_attribute *attr,
 367                               const char *buf, size_t count)
 368{
 369        unsigned long val = simple_strtoul (buf, NULL, 10);
 370
 371        if (!capable(CAP_SYS_ADMIN))
 372                return -EPERM;
 373        if (val != 0)
 374                return -EBADMSG;
 375
 376        oom_freed_pages = 0;
 377        return count;
 378}
 379
 380static DEVICE_ATTR(oom_freed_kb, S_IWUSR | S_IRUGO,
 381                   show_oom_pages, store_oom_pages);
 382
 383static struct device_attribute *cmm_attrs[] = {
 384        &dev_attr_loaned_kb,
 385        &dev_attr_loaned_target_kb,
 386        &dev_attr_oom_freed_kb,
 387};
 388
 389static struct bus_type cmm_subsys = {
 390        .name = "cmm",
 391        .dev_name = "cmm",
 392};
 393
 394/**
 395 * cmm_sysfs_register - Register with sysfs
 396 *
 397 * Return value:
 398 *      0 on success / other on failure
 399 **/
 400static int cmm_sysfs_register(struct device *dev)
 401{
 402        int i, rc;
 403
 404        if ((rc = subsys_system_register(&cmm_subsys, NULL)))
 405                return rc;
 406
 407        dev->id = 0;
 408        dev->bus = &cmm_subsys;
 409
 410        if ((rc = device_register(dev)))
 411                goto subsys_unregister;
 412
 413        for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
 414                if ((rc = device_create_file(dev, cmm_attrs[i])))
 415                        goto fail;
 416        }
 417
 418        return 0;
 419
 420fail:
 421        while (--i >= 0)
 422                device_remove_file(dev, cmm_attrs[i]);
 423        device_unregister(dev);
 424subsys_unregister:
 425        bus_unregister(&cmm_subsys);
 426        return rc;
 427}
 428
 429/**
 430 * cmm_unregister_sysfs - Unregister from sysfs
 431 *
 432 **/
 433static void cmm_unregister_sysfs(struct device *dev)
 434{
 435        int i;
 436
 437        for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++)
 438                device_remove_file(dev, cmm_attrs[i]);
 439        device_unregister(dev);
 440        bus_unregister(&cmm_subsys);
 441}
 442
 443/**
 444 * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
 445 *
 446 **/
 447static int cmm_reboot_notifier(struct notifier_block *nb,
 448                               unsigned long action, void *unused)
 449{
 450        if (action == SYS_RESTART) {
 451                if (cmm_thread_ptr)
 452                        kthread_stop(cmm_thread_ptr);
 453                cmm_thread_ptr = NULL;
 454                cmm_free_pages(loaned_pages);
 455        }
 456        return NOTIFY_DONE;
 457}
 458
 459static struct notifier_block cmm_reboot_nb = {
 460        .notifier_call = cmm_reboot_notifier,
 461};
 462
 463/**
 464 * cmm_count_pages - Count the number of pages loaned in a particular range.
 465 *
 466 * @arg: memory_isolate_notify structure with address range and count
 467 *
 468 * Return value:
 469 *      0 on success
 470 **/
 471static unsigned long cmm_count_pages(void *arg)
 472{
 473        struct memory_isolate_notify *marg = arg;
 474        struct cmm_page_array *pa;
 475        unsigned long start = (unsigned long)pfn_to_kaddr(marg->start_pfn);
 476        unsigned long end = start + (marg->nr_pages << PAGE_SHIFT);
 477        unsigned long idx;
 478
 479        spin_lock(&cmm_lock);
 480        pa = cmm_page_list;
 481        while (pa) {
 482                if ((unsigned long)pa >= start && (unsigned long)pa < end)
 483                        marg->pages_found++;
 484                for (idx = 0; idx < pa->index; idx++)
 485                        if (pa->page[idx] >= start && pa->page[idx] < end)
 486                                marg->pages_found++;
 487                pa = pa->next;
 488        }
 489        spin_unlock(&cmm_lock);
 490        return 0;
 491}
 492
 493/**
 494 * cmm_memory_isolate_cb - Handle memory isolation notifier calls
 495 * @self:       notifier block struct
 496 * @action:     action to take
 497 * @arg:        struct memory_isolate_notify data for handler
 498 *
 499 * Return value:
 500 *      NOTIFY_OK or notifier error based on subfunction return value
 501 **/
 502static int cmm_memory_isolate_cb(struct notifier_block *self,
 503                                 unsigned long action, void *arg)
 504{
 505        int ret = 0;
 506
 507        if (action == MEM_ISOLATE_COUNT)
 508                ret = cmm_count_pages(arg);
 509
 510        return notifier_from_errno(ret);
 511}
 512
 513static struct notifier_block cmm_mem_isolate_nb = {
 514        .notifier_call = cmm_memory_isolate_cb,
 515        .priority = CMM_MEM_ISOLATE_PRI
 516};
 517
 518/**
 519 * cmm_mem_going_offline - Unloan pages where memory is to be removed
 520 * @arg: memory_notify structure with page range to be offlined
 521 *
 522 * Return value:
 523 *      0 on success
 524 **/
 525static int cmm_mem_going_offline(void *arg)
 526{
 527        struct memory_notify *marg = arg;
 528        unsigned long start_page = (unsigned long)pfn_to_kaddr(marg->start_pfn);
 529        unsigned long end_page = start_page + (marg->nr_pages << PAGE_SHIFT);
 530        struct cmm_page_array *pa_curr, *pa_last, *npa;
 531        unsigned long idx;
 532        unsigned long freed = 0;
 533
 534        cmm_dbg("Memory going offline, searching 0x%lx (%ld pages).\n",
 535                        start_page, marg->nr_pages);
 536        spin_lock(&cmm_lock);
 537
 538        /* Search the page list for pages in the range to be offlined */
 539        pa_last = pa_curr = cmm_page_list;
 540        while (pa_curr) {
 541                for (idx = (pa_curr->index - 1); (idx + 1) > 0; idx--) {
 542                        if ((pa_curr->page[idx] < start_page) ||
 543                            (pa_curr->page[idx] >= end_page))
 544                                continue;
 545
 546                        plpar_page_set_active(__pa(pa_curr->page[idx]));
 547                        free_page(pa_curr->page[idx]);
 548                        freed++;
 549                        loaned_pages--;
 550                        totalram_pages++;
 551                        pa_curr->page[idx] = pa_last->page[--pa_last->index];
 552                        if (pa_last->index == 0) {
 553                                if (pa_curr == pa_last)
 554                                        pa_curr = pa_last->next;
 555                                pa_last = pa_last->next;
 556                                free_page((unsigned long)cmm_page_list);
 557                                cmm_page_list = pa_last;
 558                        }
 559                }
 560                pa_curr = pa_curr->next;
 561        }
 562
 563        /* Search for page list structures in the range to be offlined */
 564        pa_last = NULL;
 565        pa_curr = cmm_page_list;
 566        while (pa_curr) {
 567                if (((unsigned long)pa_curr >= start_page) &&
 568                                ((unsigned long)pa_curr < end_page)) {
 569                        npa = (struct cmm_page_array *)__get_free_page(
 570                                        GFP_NOIO | __GFP_NOWARN |
 571                                        __GFP_NORETRY | __GFP_NOMEMALLOC);
 572                        if (!npa) {
 573                                spin_unlock(&cmm_lock);
 574                                cmm_dbg("Failed to allocate memory for list "
 575                                                "management. Memory hotplug "
 576                                                "failed.\n");
 577                                return -ENOMEM;
 578                        }
 579                        memcpy(npa, pa_curr, PAGE_SIZE);
 580                        if (pa_curr == cmm_page_list)
 581                                cmm_page_list = npa;
 582                        if (pa_last)
 583                                pa_last->next = npa;
 584                        free_page((unsigned long) pa_curr);
 585                        freed++;
 586                        pa_curr = npa;
 587                }
 588
 589                pa_last = pa_curr;
 590                pa_curr = pa_curr->next;
 591        }
 592
 593        spin_unlock(&cmm_lock);
 594        cmm_dbg("Released %ld pages in the search range.\n", freed);
 595
 596        return 0;
 597}
 598
 599/**
 600 * cmm_memory_cb - Handle memory hotplug notifier calls
 601 * @self:       notifier block struct
 602 * @action:     action to take
 603 * @arg:        struct memory_notify data for handler
 604 *
 605 * Return value:
 606 *      NOTIFY_OK or notifier error based on subfunction return value
 607 *
 608 **/
 609static int cmm_memory_cb(struct notifier_block *self,
 610                        unsigned long action, void *arg)
 611{
 612        int ret = 0;
 613
 614        switch (action) {
 615        case MEM_GOING_OFFLINE:
 616                mutex_lock(&hotplug_mutex);
 617                hotplug_occurred = 1;
 618                ret = cmm_mem_going_offline(arg);
 619                break;
 620        case MEM_OFFLINE:
 621        case MEM_CANCEL_OFFLINE:
 622                mutex_unlock(&hotplug_mutex);
 623                cmm_dbg("Memory offline operation complete.\n");
 624                break;
 625        case MEM_GOING_ONLINE:
 626        case MEM_ONLINE:
 627        case MEM_CANCEL_ONLINE:
 628                break;
 629        }
 630
 631        return notifier_from_errno(ret);
 632}
 633
 634static struct notifier_block cmm_mem_nb = {
 635        .notifier_call = cmm_memory_cb,
 636        .priority = CMM_MEM_HOTPLUG_PRI
 637};
 638
 639/**
 640 * cmm_init - Module initialization
 641 *
 642 * Return value:
 643 *      0 on success / other on failure
 644 **/
 645static int cmm_init(void)
 646{
 647        int rc = -ENOMEM;
 648
 649        if (!firmware_has_feature(FW_FEATURE_CMO))
 650                return -EOPNOTSUPP;
 651
 652        if ((rc = register_oom_notifier(&cmm_oom_nb)) < 0)
 653                return rc;
 654
 655        if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
 656                goto out_oom_notifier;
 657
 658        if ((rc = cmm_sysfs_register(&cmm_dev)))
 659                goto out_reboot_notifier;
 660
 661        if (register_memory_notifier(&cmm_mem_nb) ||
 662            register_memory_isolate_notifier(&cmm_mem_isolate_nb))
 663                goto out_unregister_notifier;
 664
 665        if (cmm_disabled)
 666                return rc;
 667
 668        cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
 669        if (IS_ERR(cmm_thread_ptr)) {
 670                rc = PTR_ERR(cmm_thread_ptr);
 671                goto out_unregister_notifier;
 672        }
 673
 674        return rc;
 675
 676out_unregister_notifier:
 677        unregister_memory_notifier(&cmm_mem_nb);
 678        unregister_memory_isolate_notifier(&cmm_mem_isolate_nb);
 679        cmm_unregister_sysfs(&cmm_dev);
 680out_reboot_notifier:
 681        unregister_reboot_notifier(&cmm_reboot_nb);
 682out_oom_notifier:
 683        unregister_oom_notifier(&cmm_oom_nb);
 684        return rc;
 685}
 686
 687/**
 688 * cmm_exit - Module exit
 689 *
 690 * Return value:
 691 *      nothing
 692 **/
 693static void cmm_exit(void)
 694{
 695        if (cmm_thread_ptr)
 696                kthread_stop(cmm_thread_ptr);
 697        unregister_oom_notifier(&cmm_oom_nb);
 698        unregister_reboot_notifier(&cmm_reboot_nb);
 699        unregister_memory_notifier(&cmm_mem_nb);
 700        unregister_memory_isolate_notifier(&cmm_mem_isolate_nb);
 701        cmm_free_pages(loaned_pages);
 702        cmm_unregister_sysfs(&cmm_dev);
 703}
 704
 705/**
 706 * cmm_set_disable - Disable/Enable CMM
 707 *
 708 * Return value:
 709 *      0 on success / other on failure
 710 **/
 711static int cmm_set_disable(const char *val, struct kernel_param *kp)
 712{
 713        int disable = simple_strtoul(val, NULL, 10);
 714
 715        if (disable != 0 && disable != 1)
 716                return -EINVAL;
 717
 718        if (disable && !cmm_disabled) {
 719                if (cmm_thread_ptr)
 720                        kthread_stop(cmm_thread_ptr);
 721                cmm_thread_ptr = NULL;
 722                cmm_free_pages(loaned_pages);
 723        } else if (!disable && cmm_disabled) {
 724                cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
 725                if (IS_ERR(cmm_thread_ptr))
 726                        return PTR_ERR(cmm_thread_ptr);
 727        }
 728
 729        cmm_disabled = disable;
 730        return 0;
 731}
 732
 733module_param_call(disable, cmm_set_disable, param_get_uint,
 734                  &cmm_disabled, S_IRUGO | S_IWUSR);
 735MODULE_PARM_DESC(disable, "Disable CMM. Set to 1 to disable. "
 736                 "[Default=" __stringify(CMM_DISABLE) "]");
 737
 738module_init(cmm_init);
 739module_exit(cmm_exit);
 740