cmm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Collaborative memory management interface.
  3. *
  4. * Copyright IBM Corp 2003,2010
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  6. *
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/fs.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/gfp.h>
  13. #include <linux/sched.h>
  14. #include <linux/sysctl.h>
  15. #include <linux/ctype.h>
  16. #include <linux/swap.h>
  17. #include <linux/kthread.h>
  18. #include <linux/oom.h>
  19. #include <linux/suspend.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/diag.h>
  23. #ifdef CONFIG_CMM_IUCV
  24. static char *cmm_default_sender = "VMRMSVM";
  25. #endif
  26. static char *sender;
  27. module_param(sender, charp, 0400);
  28. MODULE_PARM_DESC(sender,
  29. "Guest name that may send SMSG messages (default VMRMSVM)");
  30. #include "../../../drivers/s390/net/smsgiucv.h"
  31. #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
  32. struct cmm_page_array {
  33. struct cmm_page_array *next;
  34. unsigned long index;
  35. unsigned long pages[CMM_NR_PAGES];
  36. };
  37. static long cmm_pages;
  38. static long cmm_timed_pages;
  39. static volatile long cmm_pages_target;
  40. static volatile long cmm_timed_pages_target;
  41. static long cmm_timeout_pages;
  42. static long cmm_timeout_seconds;
  43. static int cmm_suspended;
  44. static struct cmm_page_array *cmm_page_list;
  45. static struct cmm_page_array *cmm_timed_page_list;
  46. static DEFINE_SPINLOCK(cmm_lock);
  47. static struct task_struct *cmm_thread_ptr;
  48. static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait);
  49. static DEFINE_TIMER(cmm_timer, NULL, 0, 0);
  50. static void cmm_timer_fn(unsigned long);
  51. static void cmm_set_timer(void);
  52. static long cmm_alloc_pages(long nr, long *counter,
  53. struct cmm_page_array **list)
  54. {
  55. struct cmm_page_array *pa, *npa;
  56. unsigned long addr;
  57. while (nr) {
  58. addr = __get_free_page(GFP_NOIO);
  59. if (!addr)
  60. break;
  61. spin_lock(&cmm_lock);
  62. pa = *list;
  63. if (!pa || pa->index >= CMM_NR_PAGES) {
  64. /* Need a new page for the page list. */
  65. spin_unlock(&cmm_lock);
  66. npa = (struct cmm_page_array *)
  67. __get_free_page(GFP_NOIO);
  68. if (!npa) {
  69. free_page(addr);
  70. break;
  71. }
  72. spin_lock(&cmm_lock);
  73. pa = *list;
  74. if (!pa || pa->index >= CMM_NR_PAGES) {
  75. npa->next = pa;
  76. npa->index = 0;
  77. pa = npa;
  78. *list = pa;
  79. } else
  80. free_page((unsigned long) npa);
  81. }
  82. diag10_range(addr >> PAGE_SHIFT, 1);
  83. pa->pages[pa->index++] = addr;
  84. (*counter)++;
  85. spin_unlock(&cmm_lock);
  86. nr--;
  87. }
  88. return nr;
  89. }
  90. static long cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
  91. {
  92. struct cmm_page_array *pa;
  93. unsigned long addr;
  94. spin_lock(&cmm_lock);
  95. pa = *list;
  96. while (nr) {
  97. if (!pa || pa->index <= 0)
  98. break;
  99. addr = pa->pages[--pa->index];
  100. if (pa->index == 0) {
  101. pa = pa->next;
  102. free_page((unsigned long) *list);
  103. *list = pa;
  104. }
  105. free_page(addr);
  106. (*counter)--;
  107. nr--;
  108. }
  109. spin_unlock(&cmm_lock);
  110. return nr;
  111. }
  112. static int cmm_oom_notify(struct notifier_block *self,
  113. unsigned long dummy, void *parm)
  114. {
  115. unsigned long *freed = parm;
  116. long nr = 256;
  117. nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
  118. if (nr > 0)
  119. nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
  120. cmm_pages_target = cmm_pages;
  121. cmm_timed_pages_target = cmm_timed_pages;
  122. *freed += 256 - nr;
  123. return NOTIFY_OK;
  124. }
  125. static struct notifier_block cmm_oom_nb = {
  126. .notifier_call = cmm_oom_notify,
  127. };
  128. static int cmm_thread(void *dummy)
  129. {
  130. int rc;
  131. while (1) {
  132. rc = wait_event_interruptible(cmm_thread_wait,
  133. (!cmm_suspended && (cmm_pages != cmm_pages_target ||
  134. cmm_timed_pages != cmm_timed_pages_target)) ||
  135. kthread_should_stop());
  136. if (kthread_should_stop() || rc == -ERESTARTSYS) {
  137. cmm_pages_target = cmm_pages;
  138. cmm_timed_pages_target = cmm_timed_pages;
  139. break;
  140. }
  141. if (cmm_pages_target > cmm_pages) {
  142. if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
  143. cmm_pages_target = cmm_pages;
  144. } else if (cmm_pages_target < cmm_pages) {
  145. cmm_free_pages(1, &cmm_pages, &cmm_page_list);
  146. }
  147. if (cmm_timed_pages_target > cmm_timed_pages) {
  148. if (cmm_alloc_pages(1, &cmm_timed_pages,
  149. &cmm_timed_page_list))
  150. cmm_timed_pages_target = cmm_timed_pages;
  151. } else if (cmm_timed_pages_target < cmm_timed_pages) {
  152. cmm_free_pages(1, &cmm_timed_pages,
  153. &cmm_timed_page_list);
  154. }
  155. if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
  156. cmm_set_timer();
  157. }
  158. return 0;
  159. }
  160. static void cmm_kick_thread(void)
  161. {
  162. wake_up(&cmm_thread_wait);
  163. }
  164. static void cmm_set_timer(void)
  165. {
  166. if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
  167. if (timer_pending(&cmm_timer))
  168. del_timer(&cmm_timer);
  169. return;
  170. }
  171. if (timer_pending(&cmm_timer)) {
  172. if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
  173. return;
  174. }
  175. cmm_timer.function = cmm_timer_fn;
  176. cmm_timer.data = 0;
  177. cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
  178. add_timer(&cmm_timer);
  179. }
  180. static void cmm_timer_fn(unsigned long ignored)
  181. {
  182. long nr;
  183. nr = cmm_timed_pages_target - cmm_timeout_pages;
  184. if (nr < 0)
  185. cmm_timed_pages_target = 0;
  186. else
  187. cmm_timed_pages_target = nr;
  188. cmm_kick_thread();
  189. cmm_set_timer();
  190. }
  191. static void cmm_set_pages(long nr)
  192. {
  193. cmm_pages_target = nr;
  194. cmm_kick_thread();
  195. }
  196. static long cmm_get_pages(void)
  197. {
  198. return cmm_pages;
  199. }
  200. static void cmm_add_timed_pages(long nr)
  201. {
  202. cmm_timed_pages_target += nr;
  203. cmm_kick_thread();
  204. }
  205. static long cmm_get_timed_pages(void)
  206. {
  207. return cmm_timed_pages;
  208. }
  209. static void cmm_set_timeout(long nr, long seconds)
  210. {
  211. cmm_timeout_pages = nr;
  212. cmm_timeout_seconds = seconds;
  213. cmm_set_timer();
  214. }
  215. static int cmm_skip_blanks(char *cp, char **endp)
  216. {
  217. char *str;
  218. for (str = cp; *str == ' ' || *str == '\t'; str++)
  219. ;
  220. *endp = str;
  221. return str != cp;
  222. }
  223. static struct ctl_table cmm_table[];
  224. static int cmm_pages_handler(ctl_table *ctl, int write, void __user *buffer,
  225. size_t *lenp, loff_t *ppos)
  226. {
  227. char buf[16], *p;
  228. long nr;
  229. int len;
  230. if (!*lenp || (*ppos && !write)) {
  231. *lenp = 0;
  232. return 0;
  233. }
  234. if (write) {
  235. len = *lenp;
  236. if (copy_from_user(buf, buffer,
  237. len > sizeof(buf) ? sizeof(buf) : len))
  238. return -EFAULT;
  239. buf[sizeof(buf) - 1] = '\0';
  240. cmm_skip_blanks(buf, &p);
  241. nr = simple_strtoul(p, &p, 0);
  242. if (ctl == &cmm_table[0])
  243. cmm_set_pages(nr);
  244. else
  245. cmm_add_timed_pages(nr);
  246. } else {
  247. if (ctl == &cmm_table[0])
  248. nr = cmm_get_pages();
  249. else
  250. nr = cmm_get_timed_pages();
  251. len = sprintf(buf, "%ld\n", nr);
  252. if (len > *lenp)
  253. len = *lenp;
  254. if (copy_to_user(buffer, buf, len))
  255. return -EFAULT;
  256. }
  257. *lenp = len;
  258. *ppos += len;
  259. return 0;
  260. }
  261. static int cmm_timeout_handler(ctl_table *ctl, int write, void __user *buffer,
  262. size_t *lenp, loff_t *ppos)
  263. {
  264. char buf[64], *p;
  265. long nr, seconds;
  266. int len;
  267. if (!*lenp || (*ppos && !write)) {
  268. *lenp = 0;
  269. return 0;
  270. }
  271. if (write) {
  272. len = *lenp;
  273. if (copy_from_user(buf, buffer,
  274. len > sizeof(buf) ? sizeof(buf) : len))
  275. return -EFAULT;
  276. buf[sizeof(buf) - 1] = '\0';
  277. cmm_skip_blanks(buf, &p);
  278. nr = simple_strtoul(p, &p, 0);
  279. cmm_skip_blanks(p, &p);
  280. seconds = simple_strtoul(p, &p, 0);
  281. cmm_set_timeout(nr, seconds);
  282. } else {
  283. len = sprintf(buf, "%ld %ld\n",
  284. cmm_timeout_pages, cmm_timeout_seconds);
  285. if (len > *lenp)
  286. len = *lenp;
  287. if (copy_to_user(buffer, buf, len))
  288. return -EFAULT;
  289. }
  290. *lenp = len;
  291. *ppos += len;
  292. return 0;
  293. }
  294. static struct ctl_table cmm_table[] = {
  295. {
  296. .procname = "cmm_pages",
  297. .mode = 0644,
  298. .proc_handler = cmm_pages_handler,
  299. },
  300. {
  301. .procname = "cmm_timed_pages",
  302. .mode = 0644,
  303. .proc_handler = cmm_pages_handler,
  304. },
  305. {
  306. .procname = "cmm_timeout",
  307. .mode = 0644,
  308. .proc_handler = cmm_timeout_handler,
  309. },
  310. { }
  311. };
  312. static struct ctl_table cmm_dir_table[] = {
  313. {
  314. .procname = "vm",
  315. .maxlen = 0,
  316. .mode = 0555,
  317. .child = cmm_table,
  318. },
  319. { }
  320. };
  321. #ifdef CONFIG_CMM_IUCV
  322. #define SMSG_PREFIX "CMM"
  323. static void cmm_smsg_target(const char *from, char *msg)
  324. {
  325. long nr, seconds;
  326. if (strlen(sender) > 0 && strcmp(from, sender) != 0)
  327. return;
  328. if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
  329. return;
  330. if (strncmp(msg, "SHRINK", 6) == 0) {
  331. if (!cmm_skip_blanks(msg + 6, &msg))
  332. return;
  333. nr = simple_strtoul(msg, &msg, 0);
  334. cmm_skip_blanks(msg, &msg);
  335. if (*msg == '\0')
  336. cmm_set_pages(nr);
  337. } else if (strncmp(msg, "RELEASE", 7) == 0) {
  338. if (!cmm_skip_blanks(msg + 7, &msg))
  339. return;
  340. nr = simple_strtoul(msg, &msg, 0);
  341. cmm_skip_blanks(msg, &msg);
  342. if (*msg == '\0')
  343. cmm_add_timed_pages(nr);
  344. } else if (strncmp(msg, "REUSE", 5) == 0) {
  345. if (!cmm_skip_blanks(msg + 5, &msg))
  346. return;
  347. nr = simple_strtoul(msg, &msg, 0);
  348. if (!cmm_skip_blanks(msg, &msg))
  349. return;
  350. seconds = simple_strtoul(msg, &msg, 0);
  351. cmm_skip_blanks(msg, &msg);
  352. if (*msg == '\0')
  353. cmm_set_timeout(nr, seconds);
  354. }
  355. }
  356. #endif
  357. static struct ctl_table_header *cmm_sysctl_header;
  358. static int cmm_suspend(void)
  359. {
  360. cmm_suspended = 1;
  361. cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
  362. cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
  363. return 0;
  364. }
  365. static int cmm_resume(void)
  366. {
  367. cmm_suspended = 0;
  368. cmm_kick_thread();
  369. return 0;
  370. }
  371. static int cmm_power_event(struct notifier_block *this,
  372. unsigned long event, void *ptr)
  373. {
  374. switch (event) {
  375. case PM_POST_HIBERNATION:
  376. return cmm_resume();
  377. case PM_HIBERNATION_PREPARE:
  378. return cmm_suspend();
  379. default:
  380. return NOTIFY_DONE;
  381. }
  382. }
  383. static struct notifier_block cmm_power_notifier = {
  384. .notifier_call = cmm_power_event,
  385. };
  386. static int __init cmm_init(void)
  387. {
  388. int rc = -ENOMEM;
  389. cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
  390. if (!cmm_sysctl_header)
  391. goto out_sysctl;
  392. #ifdef CONFIG_CMM_IUCV
  393. /* convert sender to uppercase characters */
  394. if (sender) {
  395. int len = strlen(sender);
  396. while (len--)
  397. sender[len] = toupper(sender[len]);
  398. } else {
  399. sender = cmm_default_sender;
  400. }
  401. rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
  402. if (rc < 0)
  403. goto out_smsg;
  404. #endif
  405. rc = register_oom_notifier(&cmm_oom_nb);
  406. if (rc < 0)
  407. goto out_oom_notify;
  408. rc = register_pm_notifier(&cmm_power_notifier);
  409. if (rc)
  410. goto out_pm;
  411. cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
  412. rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0;
  413. if (rc)
  414. goto out_kthread;
  415. return 0;
  416. out_kthread:
  417. unregister_pm_notifier(&cmm_power_notifier);
  418. out_pm:
  419. unregister_oom_notifier(&cmm_oom_nb);
  420. out_oom_notify:
  421. #ifdef CONFIG_CMM_IUCV
  422. smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
  423. out_smsg:
  424. #endif
  425. unregister_sysctl_table(cmm_sysctl_header);
  426. out_sysctl:
  427. del_timer_sync(&cmm_timer);
  428. return rc;
  429. }
  430. module_init(cmm_init);
  431. static void __exit cmm_exit(void)
  432. {
  433. unregister_sysctl_table(cmm_sysctl_header);
  434. #ifdef CONFIG_CMM_IUCV
  435. smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
  436. #endif
  437. unregister_pm_notifier(&cmm_power_notifier);
  438. unregister_oom_notifier(&cmm_oom_nb);
  439. kthread_stop(cmm_thread_ptr);
  440. del_timer_sync(&cmm_timer);
  441. cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
  442. cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
  443. }
  444. module_exit(cmm_exit);
  445. MODULE_LICENSE("GPL");