cmm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/init.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/sysdev.h>
  35. #include <asm/firmware.h>
  36. #include <asm/hvcall.h>
  37. #include <asm/mmu.h>
  38. #include <asm/pgalloc.h>
  39. #include <asm/uaccess.h>
  40. #include "plpar_wrappers.h"
  41. #define CMM_DRIVER_VERSION "1.0.0"
  42. #define CMM_DEFAULT_DELAY 1
  43. #define CMM_DEBUG 0
  44. #define CMM_DISABLE 0
  45. #define CMM_OOM_KB 1024
  46. #define CMM_MIN_MEM_MB 256
  47. #define KB2PAGES(_p) ((_p)>>(PAGE_SHIFT-10))
  48. #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
  49. static unsigned int delay = CMM_DEFAULT_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 sys_device cmm_sysdev;
  55. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  56. MODULE_DESCRIPTION("IBM System p Collaborative Memory Manager");
  57. MODULE_LICENSE("GPL");
  58. MODULE_VERSION(CMM_DRIVER_VERSION);
  59. module_param_named(delay, delay, uint, S_IRUGO | S_IWUSR);
  60. MODULE_PARM_DESC(delay, "Delay (in seconds) between polls to query hypervisor paging requests. "
  61. "[Default=" __stringify(CMM_DEFAULT_DELAY) "]");
  62. module_param_named(oom_kb, oom_kb, uint, S_IRUGO | S_IWUSR);
  63. MODULE_PARM_DESC(oom_kb, "Amount of memory in kb to free on OOM. "
  64. "[Default=" __stringify(CMM_OOM_KB) "]");
  65. module_param_named(min_mem_mb, min_mem_mb, ulong, S_IRUGO | S_IWUSR);
  66. MODULE_PARM_DESC(min_mem_mb, "Minimum amount of memory (in MB) to not balloon. "
  67. "[Default=" __stringify(CMM_MIN_MEM_MB) "]");
  68. module_param_named(debug, cmm_debug, uint, S_IRUGO | S_IWUSR);
  69. MODULE_PARM_DESC(debug, "Enable module debugging logging. Set to 1 to enable. "
  70. "[Default=" __stringify(CMM_DEBUG) "]");
  71. #define CMM_NR_PAGES ((PAGE_SIZE - sizeof(void *) - sizeof(unsigned long)) / sizeof(unsigned long))
  72. #define cmm_dbg(...) if (cmm_debug) { printk(KERN_INFO "cmm: "__VA_ARGS__); }
  73. struct cmm_page_array {
  74. struct cmm_page_array *next;
  75. unsigned long index;
  76. unsigned long page[CMM_NR_PAGES];
  77. };
  78. static unsigned long loaned_pages;
  79. static unsigned long loaned_pages_target;
  80. static unsigned long oom_freed_pages;
  81. static struct cmm_page_array *cmm_page_list;
  82. static DEFINE_SPINLOCK(cmm_lock);
  83. static struct task_struct *cmm_thread_ptr;
  84. /**
  85. * cmm_alloc_pages - Allocate pages and mark them as loaned
  86. * @nr: number of pages to allocate
  87. *
  88. * Return value:
  89. * number of pages requested to be allocated which were not
  90. **/
  91. static long cmm_alloc_pages(long nr)
  92. {
  93. struct cmm_page_array *pa, *npa;
  94. unsigned long addr;
  95. long rc;
  96. cmm_dbg("Begin request for %ld pages\n", nr);
  97. while (nr) {
  98. addr = __get_free_page(GFP_NOIO | __GFP_NOWARN |
  99. __GFP_NORETRY | __GFP_NOMEMALLOC);
  100. if (!addr)
  101. break;
  102. spin_lock(&cmm_lock);
  103. pa = cmm_page_list;
  104. if (!pa || pa->index >= CMM_NR_PAGES) {
  105. /* Need a new page for the page list. */
  106. spin_unlock(&cmm_lock);
  107. npa = (struct cmm_page_array *)__get_free_page(GFP_NOIO | __GFP_NOWARN |
  108. __GFP_NORETRY | __GFP_NOMEMALLOC);
  109. if (!npa) {
  110. pr_info("%s: Can not allocate new page list\n", __func__);
  111. free_page(addr);
  112. break;
  113. }
  114. spin_lock(&cmm_lock);
  115. pa = cmm_page_list;
  116. if (!pa || pa->index >= CMM_NR_PAGES) {
  117. npa->next = pa;
  118. npa->index = 0;
  119. pa = npa;
  120. cmm_page_list = pa;
  121. } else
  122. free_page((unsigned long) npa);
  123. }
  124. if ((rc = plpar_page_set_loaned(__pa(addr)))) {
  125. pr_err("%s: Can not set page to loaned. rc=%ld\n", __func__, rc);
  126. spin_unlock(&cmm_lock);
  127. free_page(addr);
  128. break;
  129. }
  130. pa->page[pa->index++] = addr;
  131. loaned_pages++;
  132. totalram_pages--;
  133. spin_unlock(&cmm_lock);
  134. nr--;
  135. }
  136. cmm_dbg("End request with %ld pages unfulfilled\n", nr);
  137. return nr;
  138. }
  139. /**
  140. * cmm_free_pages - Free pages and mark them as active
  141. * @nr: number of pages to free
  142. *
  143. * Return value:
  144. * number of pages requested to be freed which were not
  145. **/
  146. static long cmm_free_pages(long nr)
  147. {
  148. struct cmm_page_array *pa;
  149. unsigned long addr;
  150. cmm_dbg("Begin free of %ld pages.\n", nr);
  151. spin_lock(&cmm_lock);
  152. pa = cmm_page_list;
  153. while (nr) {
  154. if (!pa || pa->index <= 0)
  155. break;
  156. addr = pa->page[--pa->index];
  157. if (pa->index == 0) {
  158. pa = pa->next;
  159. free_page((unsigned long) cmm_page_list);
  160. cmm_page_list = pa;
  161. }
  162. plpar_page_set_active(__pa(addr));
  163. free_page(addr);
  164. loaned_pages--;
  165. nr--;
  166. totalram_pages++;
  167. }
  168. spin_unlock(&cmm_lock);
  169. cmm_dbg("End request with %ld pages unfulfilled\n", nr);
  170. return nr;
  171. }
  172. /**
  173. * cmm_oom_notify - OOM notifier
  174. * @self: notifier block struct
  175. * @dummy: not used
  176. * @parm: returned - number of pages freed
  177. *
  178. * Return value:
  179. * NOTIFY_OK
  180. **/
  181. static int cmm_oom_notify(struct notifier_block *self,
  182. unsigned long dummy, void *parm)
  183. {
  184. unsigned long *freed = parm;
  185. long nr = KB2PAGES(oom_kb);
  186. cmm_dbg("OOM processing started\n");
  187. nr = cmm_free_pages(nr);
  188. loaned_pages_target = loaned_pages;
  189. *freed += KB2PAGES(oom_kb) - nr;
  190. oom_freed_pages += KB2PAGES(oom_kb) - nr;
  191. cmm_dbg("OOM processing complete\n");
  192. return NOTIFY_OK;
  193. }
  194. /**
  195. * cmm_get_mpp - Read memory performance parameters
  196. *
  197. * Makes hcall to query the current page loan request from the hypervisor.
  198. *
  199. * Return value:
  200. * nothing
  201. **/
  202. static void cmm_get_mpp(void)
  203. {
  204. int rc;
  205. struct hvcall_mpp_data mpp_data;
  206. unsigned long active_pages_target;
  207. signed long page_loan_request;
  208. rc = h_get_mpp(&mpp_data);
  209. if (rc != H_SUCCESS)
  210. return;
  211. page_loan_request = div_s64((s64)mpp_data.loan_request, PAGE_SIZE);
  212. loaned_pages_target = page_loan_request + loaned_pages;
  213. if (loaned_pages_target > oom_freed_pages)
  214. loaned_pages_target -= oom_freed_pages;
  215. else
  216. loaned_pages_target = 0;
  217. active_pages_target = totalram_pages + loaned_pages - loaned_pages_target;
  218. if ((min_mem_mb * 1024 * 1024) > (active_pages_target * PAGE_SIZE))
  219. loaned_pages_target = totalram_pages + loaned_pages -
  220. ((min_mem_mb * 1024 * 1024) / PAGE_SIZE);
  221. cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
  222. page_loan_request, loaned_pages, loaned_pages_target,
  223. oom_freed_pages, totalram_pages);
  224. }
  225. static struct notifier_block cmm_oom_nb = {
  226. .notifier_call = cmm_oom_notify
  227. };
  228. /**
  229. * cmm_thread - CMM task thread
  230. * @dummy: not used
  231. *
  232. * Return value:
  233. * 0
  234. **/
  235. static int cmm_thread(void *dummy)
  236. {
  237. unsigned long timeleft;
  238. while (1) {
  239. timeleft = msleep_interruptible(delay * 1000);
  240. if (kthread_should_stop() || timeleft) {
  241. loaned_pages_target = loaned_pages;
  242. break;
  243. }
  244. cmm_get_mpp();
  245. if (loaned_pages_target > loaned_pages) {
  246. if (cmm_alloc_pages(loaned_pages_target - loaned_pages))
  247. loaned_pages_target = loaned_pages;
  248. } else if (loaned_pages_target < loaned_pages)
  249. cmm_free_pages(loaned_pages - loaned_pages_target);
  250. }
  251. return 0;
  252. }
  253. #define CMM_SHOW(name, format, args...) \
  254. static ssize_t show_##name(struct sys_device *dev, \
  255. struct sysdev_attribute *attr, \
  256. char *buf) \
  257. { \
  258. return sprintf(buf, format, ##args); \
  259. } \
  260. static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
  261. CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(loaned_pages));
  262. CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
  263. static ssize_t show_oom_pages(struct sys_device *dev,
  264. struct sysdev_attribute *attr, char *buf)
  265. {
  266. return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
  267. }
  268. static ssize_t store_oom_pages(struct sys_device *dev,
  269. struct sysdev_attribute *attr,
  270. const char *buf, size_t count)
  271. {
  272. unsigned long val = simple_strtoul (buf, NULL, 10);
  273. if (!capable(CAP_SYS_ADMIN))
  274. return -EPERM;
  275. if (val != 0)
  276. return -EBADMSG;
  277. oom_freed_pages = 0;
  278. return count;
  279. }
  280. static SYSDEV_ATTR(oom_freed_kb, S_IWUSR| S_IRUGO,
  281. show_oom_pages, store_oom_pages);
  282. static struct sysdev_attribute *cmm_attrs[] = {
  283. &attr_loaned_kb,
  284. &attr_loaned_target_kb,
  285. &attr_oom_freed_kb,
  286. };
  287. static struct sysdev_class cmm_sysdev_class = {
  288. .name = "cmm",
  289. };
  290. /**
  291. * cmm_sysfs_register - Register with sysfs
  292. *
  293. * Return value:
  294. * 0 on success / other on failure
  295. **/
  296. static int cmm_sysfs_register(struct sys_device *sysdev)
  297. {
  298. int i, rc;
  299. if ((rc = sysdev_class_register(&cmm_sysdev_class)))
  300. return rc;
  301. sysdev->id = 0;
  302. sysdev->cls = &cmm_sysdev_class;
  303. if ((rc = sysdev_register(sysdev)))
  304. goto class_unregister;
  305. for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++) {
  306. if ((rc = sysdev_create_file(sysdev, cmm_attrs[i])))
  307. goto fail;
  308. }
  309. return 0;
  310. fail:
  311. while (--i >= 0)
  312. sysdev_remove_file(sysdev, cmm_attrs[i]);
  313. sysdev_unregister(sysdev);
  314. class_unregister:
  315. sysdev_class_unregister(&cmm_sysdev_class);
  316. return rc;
  317. }
  318. /**
  319. * cmm_unregister_sysfs - Unregister from sysfs
  320. *
  321. **/
  322. static void cmm_unregister_sysfs(struct sys_device *sysdev)
  323. {
  324. int i;
  325. for (i = 0; i < ARRAY_SIZE(cmm_attrs); i++)
  326. sysdev_remove_file(sysdev, cmm_attrs[i]);
  327. sysdev_unregister(sysdev);
  328. sysdev_class_unregister(&cmm_sysdev_class);
  329. }
  330. /**
  331. * cmm_reboot_notifier - Make sure pages are not still marked as "loaned"
  332. *
  333. **/
  334. static int cmm_reboot_notifier(struct notifier_block *nb,
  335. unsigned long action, void *unused)
  336. {
  337. if (action == SYS_RESTART) {
  338. if (cmm_thread_ptr)
  339. kthread_stop(cmm_thread_ptr);
  340. cmm_thread_ptr = NULL;
  341. cmm_free_pages(loaned_pages);
  342. }
  343. return NOTIFY_DONE;
  344. }
  345. static struct notifier_block cmm_reboot_nb = {
  346. .notifier_call = cmm_reboot_notifier,
  347. };
  348. /**
  349. * cmm_init - Module initialization
  350. *
  351. * Return value:
  352. * 0 on success / other on failure
  353. **/
  354. static int cmm_init(void)
  355. {
  356. int rc = -ENOMEM;
  357. if (!firmware_has_feature(FW_FEATURE_CMO))
  358. return -EOPNOTSUPP;
  359. if ((rc = register_oom_notifier(&cmm_oom_nb)) < 0)
  360. return rc;
  361. if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
  362. goto out_oom_notifier;
  363. if ((rc = cmm_sysfs_register(&cmm_sysdev)))
  364. goto out_reboot_notifier;
  365. if (cmm_disabled)
  366. return rc;
  367. cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
  368. if (IS_ERR(cmm_thread_ptr)) {
  369. rc = PTR_ERR(cmm_thread_ptr);
  370. goto out_unregister_sysfs;
  371. }
  372. return rc;
  373. out_unregister_sysfs:
  374. cmm_unregister_sysfs(&cmm_sysdev);
  375. out_reboot_notifier:
  376. unregister_reboot_notifier(&cmm_reboot_nb);
  377. out_oom_notifier:
  378. unregister_oom_notifier(&cmm_oom_nb);
  379. return rc;
  380. }
  381. /**
  382. * cmm_exit - Module exit
  383. *
  384. * Return value:
  385. * nothing
  386. **/
  387. static void cmm_exit(void)
  388. {
  389. if (cmm_thread_ptr)
  390. kthread_stop(cmm_thread_ptr);
  391. unregister_oom_notifier(&cmm_oom_nb);
  392. unregister_reboot_notifier(&cmm_reboot_nb);
  393. cmm_free_pages(loaned_pages);
  394. cmm_unregister_sysfs(&cmm_sysdev);
  395. }
  396. /**
  397. * cmm_set_disable - Disable/Enable CMM
  398. *
  399. * Return value:
  400. * 0 on success / other on failure
  401. **/
  402. static int cmm_set_disable(const char *val, struct kernel_param *kp)
  403. {
  404. int disable = simple_strtoul(val, NULL, 10);
  405. if (disable != 0 && disable != 1)
  406. return -EINVAL;
  407. if (disable && !cmm_disabled) {
  408. if (cmm_thread_ptr)
  409. kthread_stop(cmm_thread_ptr);
  410. cmm_thread_ptr = NULL;
  411. cmm_free_pages(loaned_pages);
  412. } else if (!disable && cmm_disabled) {
  413. cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
  414. if (IS_ERR(cmm_thread_ptr))
  415. return PTR_ERR(cmm_thread_ptr);
  416. }
  417. cmm_disabled = disable;
  418. return 0;
  419. }
  420. module_param_call(disable, cmm_set_disable, param_get_uint,
  421. &cmm_disabled, S_IRUGO | S_IWUSR);
  422. MODULE_PARM_DESC(disable, "Disable CMM. Set to 1 to disable. "
  423. "[Default=" __stringify(CMM_DISABLE) "]");
  424. module_init(cmm_init);
  425. module_exit(cmm_exit);