cmm.c 12 KB

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