cmm.c 19 KB

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