xen-selfballoon.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /******************************************************************************
  2. * Xen selfballoon driver (and optional frontswap self-shrinking driver)
  3. *
  4. * Copyright (c) 2009-2011, Dan Magenheimer, Oracle Corp.
  5. *
  6. * This code complements the cleancache and frontswap patchsets to optimize
  7. * support for Xen Transcendent Memory ("tmem"). The policy it implements
  8. * is rudimentary and will likely improve over time, but it does work well
  9. * enough today.
  10. *
  11. * Two functionalities are implemented here which both use "control theory"
  12. * (feedback) to optimize memory utilization. In a virtualized environment
  13. * such as Xen, RAM is often a scarce resource and we would like to ensure
  14. * that each of a possibly large number of virtual machines is using RAM
  15. * efficiently, i.e. using as little as possible when under light load
  16. * and obtaining as much as possible when memory demands are high.
  17. * Since RAM needs vary highly dynamically and sometimes dramatically,
  18. * "hysteresis" is used, that is, memory target is determined not just
  19. * on current data but also on past data stored in the system.
  20. *
  21. * "Selfballooning" creates memory pressure by managing the Xen balloon
  22. * driver to decrease and increase available kernel memory, driven
  23. * largely by the target value of "Committed_AS" (see /proc/meminfo).
  24. * Since Committed_AS does not account for clean mapped pages (i.e. pages
  25. * in RAM that are identical to pages on disk), selfballooning has the
  26. * affect of pushing less frequently used clean pagecache pages out of
  27. * kernel RAM and, presumably using cleancache, into Xen tmem where
  28. * Xen can more efficiently optimize RAM utilization for such pages.
  29. *
  30. * When kernel memory demand unexpectedly increases faster than Xen, via
  31. * the selfballoon driver, is able to (or chooses to) provide usable RAM,
  32. * the kernel may invoke swapping. In most cases, frontswap is able
  33. * to absorb this swapping into Xen tmem. However, due to the fact
  34. * that the kernel swap subsystem assumes swapping occurs to a disk,
  35. * swapped pages may sit on the disk for a very long time; even if
  36. * the kernel knows the page will never be used again. This is because
  37. * the disk space costs very little and can be overwritten when
  38. * necessary. When such stale pages are in frontswap, however, they
  39. * are taking up valuable real estate. "Frontswap selfshrinking" works
  40. * to resolve this: When frontswap activity is otherwise stable
  41. * and the guest kernel is not under memory pressure, the "frontswap
  42. * selfshrinking" accounts for this by providing pressure to remove some
  43. * pages from frontswap and return them to kernel memory.
  44. *
  45. * For both "selfballooning" and "frontswap-selfshrinking", a worker
  46. * thread is used and sysfs tunables are provided to adjust the frequency
  47. * and rate of adjustments to achieve the goal, as well as to disable one
  48. * or both functions independently.
  49. *
  50. * While some argue that this functionality can and should be implemented
  51. * in userspace, it has been observed that bad things happen (e.g. OOMs).
  52. *
  53. * System configuration note: Selfballooning should not be enabled on
  54. * systems without a sufficiently large swap device configured; for best
  55. * results, it is recommended that total swap be increased by the size
  56. * of the guest memory. Also, while technically not required to be
  57. * configured, it is highly recommended that frontswap also be configured
  58. * and enabled when selfballooning is running. So, selfballooning
  59. * is disabled by default if frontswap is not configured and can only
  60. * be enabled with the "selfballooning" kernel boot option; similarly
  61. * selfballooning is enabled by default if frontswap is configured and
  62. * can be disabled with the "noselfballooning" kernel boot option. Finally,
  63. * when frontswap is configured, frontswap-selfshrinking can be disabled
  64. * with the "noselfshrink" kernel boot option.
  65. *
  66. * Selfballooning is disallowed in domain0 and force-disabled.
  67. *
  68. */
  69. #include <linux/kernel.h>
  70. #include <linux/bootmem.h>
  71. #include <linux/swap.h>
  72. #include <linux/mm.h>
  73. #include <linux/mman.h>
  74. #include <linux/module.h>
  75. #include <linux/workqueue.h>
  76. #include <linux/device.h>
  77. #include <xen/balloon.h>
  78. #include <xen/tmem.h>
  79. #include <xen/xen.h>
  80. /* Enable/disable with sysfs. */
  81. static int xen_selfballooning_enabled __read_mostly;
  82. /*
  83. * Controls rate at which memory target (this iteration) approaches
  84. * ultimate goal when memory need is increasing (up-hysteresis) or
  85. * decreasing (down-hysteresis). Higher values of hysteresis cause
  86. * slower increases/decreases. The default values for the various
  87. * parameters were deemed reasonable by experimentation, may be
  88. * workload-dependent, and can all be adjusted via sysfs.
  89. */
  90. static unsigned int selfballoon_downhysteresis __read_mostly = 8;
  91. static unsigned int selfballoon_uphysteresis __read_mostly = 1;
  92. /* In HZ, controls frequency of worker invocation. */
  93. static unsigned int selfballoon_interval __read_mostly = 5;
  94. /*
  95. * Minimum usable RAM in MB for selfballooning target for balloon.
  96. * If non-zero, it is added to totalreserve_pages and self-ballooning
  97. * will not balloon below the sum. If zero, a piecewise linear function
  98. * is calculated as a minimum and added to totalreserve_pages. Note that
  99. * setting this value indiscriminately may cause OOMs and crashes.
  100. */
  101. static unsigned int selfballoon_min_usable_mb;
  102. static void selfballoon_process(struct work_struct *work);
  103. static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
  104. #ifdef CONFIG_FRONTSWAP
  105. #include <linux/frontswap.h>
  106. /* Enable/disable with sysfs. */
  107. static bool frontswap_selfshrinking __read_mostly;
  108. /* Enable/disable with kernel boot option. */
  109. static bool use_frontswap_selfshrink __initdata = true;
  110. /*
  111. * The default values for the following parameters were deemed reasonable
  112. * by experimentation, may be workload-dependent, and can all be
  113. * adjusted via sysfs.
  114. */
  115. /* Control rate for frontswap shrinking. Higher hysteresis is slower. */
  116. static unsigned int frontswap_hysteresis __read_mostly = 20;
  117. /*
  118. * Number of selfballoon worker invocations to wait before observing that
  119. * frontswap selfshrinking should commence. Note that selfshrinking does
  120. * not use a separate worker thread.
  121. */
  122. static unsigned int frontswap_inertia __read_mostly = 3;
  123. /* Countdown to next invocation of frontswap_shrink() */
  124. static unsigned long frontswap_inertia_counter;
  125. /*
  126. * Invoked by the selfballoon worker thread, uses current number of pages
  127. * in frontswap (frontswap_curr_pages()), previous status, and control
  128. * values (hysteresis and inertia) to determine if frontswap should be
  129. * shrunk and what the new frontswap size should be. Note that
  130. * frontswap_shrink is essentially a partial swapoff that immediately
  131. * transfers pages from the "swap device" (frontswap) back into kernel
  132. * RAM; despite the name, frontswap "shrinking" is very different from
  133. * the "shrinker" interface used by the kernel MM subsystem to reclaim
  134. * memory.
  135. */
  136. static void frontswap_selfshrink(void)
  137. {
  138. static unsigned long cur_frontswap_pages;
  139. static unsigned long last_frontswap_pages;
  140. static unsigned long tgt_frontswap_pages;
  141. last_frontswap_pages = cur_frontswap_pages;
  142. cur_frontswap_pages = frontswap_curr_pages();
  143. if (!cur_frontswap_pages ||
  144. (cur_frontswap_pages > last_frontswap_pages)) {
  145. frontswap_inertia_counter = frontswap_inertia;
  146. return;
  147. }
  148. if (frontswap_inertia_counter && --frontswap_inertia_counter)
  149. return;
  150. if (cur_frontswap_pages <= frontswap_hysteresis)
  151. tgt_frontswap_pages = 0;
  152. else
  153. tgt_frontswap_pages = cur_frontswap_pages -
  154. (cur_frontswap_pages / frontswap_hysteresis);
  155. frontswap_shrink(tgt_frontswap_pages);
  156. }
  157. static int __init xen_nofrontswap_selfshrink_setup(char *s)
  158. {
  159. use_frontswap_selfshrink = false;
  160. return 1;
  161. }
  162. __setup("noselfshrink", xen_nofrontswap_selfshrink_setup);
  163. /* Disable with kernel boot option. */
  164. static bool use_selfballooning __initdata = true;
  165. static int __init xen_noselfballooning_setup(char *s)
  166. {
  167. use_selfballooning = false;
  168. return 1;
  169. }
  170. __setup("noselfballooning", xen_noselfballooning_setup);
  171. #else /* !CONFIG_FRONTSWAP */
  172. /* Enable with kernel boot option. */
  173. static bool use_selfballooning __initdata = false;
  174. static int __init xen_selfballooning_setup(char *s)
  175. {
  176. use_selfballooning = true;
  177. return 1;
  178. }
  179. __setup("selfballooning", xen_selfballooning_setup);
  180. #endif /* CONFIG_FRONTSWAP */
  181. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  182. /*
  183. * Use current balloon size, the goal (vm_committed_as), and hysteresis
  184. * parameters to set a new target balloon size
  185. */
  186. static void selfballoon_process(struct work_struct *work)
  187. {
  188. unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
  189. unsigned long useful_pages;
  190. bool reset_timer = false;
  191. if (xen_selfballooning_enabled) {
  192. cur_pages = totalram_pages;
  193. tgt_pages = cur_pages; /* default is no change */
  194. goal_pages = percpu_counter_read_positive(&vm_committed_as) +
  195. totalreserve_pages;
  196. #ifdef CONFIG_FRONTSWAP
  197. /* allow space for frontswap pages to be repatriated */
  198. if (frontswap_selfshrinking && frontswap_enabled)
  199. goal_pages += frontswap_curr_pages();
  200. #endif
  201. if (cur_pages > goal_pages)
  202. tgt_pages = cur_pages -
  203. ((cur_pages - goal_pages) /
  204. selfballoon_downhysteresis);
  205. else if (cur_pages < goal_pages)
  206. tgt_pages = cur_pages +
  207. ((goal_pages - cur_pages) /
  208. selfballoon_uphysteresis);
  209. /* else if cur_pages == goal_pages, no change */
  210. useful_pages = max_pfn - totalreserve_pages;
  211. if (selfballoon_min_usable_mb != 0)
  212. floor_pages = totalreserve_pages +
  213. MB2PAGES(selfballoon_min_usable_mb);
  214. /* piecewise linear function ending in ~3% slope */
  215. else if (useful_pages < MB2PAGES(16))
  216. floor_pages = max_pfn; /* not worth ballooning */
  217. else if (useful_pages < MB2PAGES(64))
  218. floor_pages = totalreserve_pages + MB2PAGES(16) +
  219. ((useful_pages - MB2PAGES(16)) >> 1);
  220. else if (useful_pages < MB2PAGES(512))
  221. floor_pages = totalreserve_pages + MB2PAGES(40) +
  222. ((useful_pages - MB2PAGES(40)) >> 3);
  223. else /* useful_pages >= MB2PAGES(512) */
  224. floor_pages = totalreserve_pages + MB2PAGES(99) +
  225. ((useful_pages - MB2PAGES(99)) >> 5);
  226. if (tgt_pages < floor_pages)
  227. tgt_pages = floor_pages;
  228. balloon_set_new_target(tgt_pages +
  229. balloon_stats.current_pages - totalram_pages);
  230. reset_timer = true;
  231. }
  232. #ifdef CONFIG_FRONTSWAP
  233. if (frontswap_selfshrinking && frontswap_enabled) {
  234. frontswap_selfshrink();
  235. reset_timer = true;
  236. }
  237. #endif
  238. if (reset_timer)
  239. schedule_delayed_work(&selfballoon_worker,
  240. selfballoon_interval * HZ);
  241. }
  242. #ifdef CONFIG_SYSFS
  243. #include <linux/capability.h>
  244. #define SELFBALLOON_SHOW(name, format, args...) \
  245. static ssize_t show_##name(struct device *dev, \
  246. struct device_attribute *attr, \
  247. char *buf) \
  248. { \
  249. return sprintf(buf, format, ##args); \
  250. }
  251. SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
  252. static ssize_t store_selfballooning(struct device *dev,
  253. struct device_attribute *attr,
  254. const char *buf,
  255. size_t count)
  256. {
  257. bool was_enabled = xen_selfballooning_enabled;
  258. unsigned long tmp;
  259. int err;
  260. if (!capable(CAP_SYS_ADMIN))
  261. return -EPERM;
  262. err = strict_strtoul(buf, 10, &tmp);
  263. if (err || ((tmp != 0) && (tmp != 1)))
  264. return -EINVAL;
  265. xen_selfballooning_enabled = !!tmp;
  266. if (!was_enabled && xen_selfballooning_enabled)
  267. schedule_delayed_work(&selfballoon_worker,
  268. selfballoon_interval * HZ);
  269. return count;
  270. }
  271. static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
  272. show_selfballooning, store_selfballooning);
  273. SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
  274. static ssize_t store_selfballoon_interval(struct device *dev,
  275. struct device_attribute *attr,
  276. const char *buf,
  277. size_t count)
  278. {
  279. unsigned long val;
  280. int err;
  281. if (!capable(CAP_SYS_ADMIN))
  282. return -EPERM;
  283. err = strict_strtoul(buf, 10, &val);
  284. if (err || val == 0)
  285. return -EINVAL;
  286. selfballoon_interval = val;
  287. return count;
  288. }
  289. static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
  290. show_selfballoon_interval, store_selfballoon_interval);
  291. SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
  292. static ssize_t store_selfballoon_downhys(struct device *dev,
  293. struct device_attribute *attr,
  294. const char *buf,
  295. size_t count)
  296. {
  297. unsigned long val;
  298. int err;
  299. if (!capable(CAP_SYS_ADMIN))
  300. return -EPERM;
  301. err = strict_strtoul(buf, 10, &val);
  302. if (err || val == 0)
  303. return -EINVAL;
  304. selfballoon_downhysteresis = val;
  305. return count;
  306. }
  307. static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
  308. show_selfballoon_downhys, store_selfballoon_downhys);
  309. SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
  310. static ssize_t store_selfballoon_uphys(struct device *dev,
  311. struct device_attribute *attr,
  312. const char *buf,
  313. size_t count)
  314. {
  315. unsigned long val;
  316. int err;
  317. if (!capable(CAP_SYS_ADMIN))
  318. return -EPERM;
  319. err = strict_strtoul(buf, 10, &val);
  320. if (err || val == 0)
  321. return -EINVAL;
  322. selfballoon_uphysteresis = val;
  323. return count;
  324. }
  325. static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
  326. show_selfballoon_uphys, store_selfballoon_uphys);
  327. SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
  328. selfballoon_min_usable_mb);
  329. static ssize_t store_selfballoon_min_usable_mb(struct device *dev,
  330. struct device_attribute *attr,
  331. const char *buf,
  332. size_t count)
  333. {
  334. unsigned long val;
  335. int err;
  336. if (!capable(CAP_SYS_ADMIN))
  337. return -EPERM;
  338. err = strict_strtoul(buf, 10, &val);
  339. if (err || val == 0)
  340. return -EINVAL;
  341. selfballoon_min_usable_mb = val;
  342. return count;
  343. }
  344. static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
  345. show_selfballoon_min_usable_mb,
  346. store_selfballoon_min_usable_mb);
  347. #ifdef CONFIG_FRONTSWAP
  348. SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
  349. static ssize_t store_frontswap_selfshrinking(struct device *dev,
  350. struct device_attribute *attr,
  351. const char *buf,
  352. size_t count)
  353. {
  354. bool was_enabled = frontswap_selfshrinking;
  355. unsigned long tmp;
  356. int err;
  357. if (!capable(CAP_SYS_ADMIN))
  358. return -EPERM;
  359. err = strict_strtoul(buf, 10, &tmp);
  360. if (err || ((tmp != 0) && (tmp != 1)))
  361. return -EINVAL;
  362. frontswap_selfshrinking = !!tmp;
  363. if (!was_enabled && !xen_selfballooning_enabled &&
  364. frontswap_selfshrinking)
  365. schedule_delayed_work(&selfballoon_worker,
  366. selfballoon_interval * HZ);
  367. return count;
  368. }
  369. static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
  370. show_frontswap_selfshrinking, store_frontswap_selfshrinking);
  371. SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
  372. static ssize_t store_frontswap_inertia(struct device *dev,
  373. struct device_attribute *attr,
  374. const char *buf,
  375. size_t count)
  376. {
  377. unsigned long val;
  378. int err;
  379. if (!capable(CAP_SYS_ADMIN))
  380. return -EPERM;
  381. err = strict_strtoul(buf, 10, &val);
  382. if (err || val == 0)
  383. return -EINVAL;
  384. frontswap_inertia = val;
  385. frontswap_inertia_counter = val;
  386. return count;
  387. }
  388. static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
  389. show_frontswap_inertia, store_frontswap_inertia);
  390. SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
  391. static ssize_t store_frontswap_hysteresis(struct device *dev,
  392. struct device_attribute *attr,
  393. const char *buf,
  394. size_t count)
  395. {
  396. unsigned long val;
  397. int err;
  398. if (!capable(CAP_SYS_ADMIN))
  399. return -EPERM;
  400. err = strict_strtoul(buf, 10, &val);
  401. if (err || val == 0)
  402. return -EINVAL;
  403. frontswap_hysteresis = val;
  404. return count;
  405. }
  406. static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
  407. show_frontswap_hysteresis, store_frontswap_hysteresis);
  408. #endif /* CONFIG_FRONTSWAP */
  409. static struct attribute *selfballoon_attrs[] = {
  410. &dev_attr_selfballooning.attr,
  411. &dev_attr_selfballoon_interval.attr,
  412. &dev_attr_selfballoon_downhysteresis.attr,
  413. &dev_attr_selfballoon_uphysteresis.attr,
  414. &dev_attr_selfballoon_min_usable_mb.attr,
  415. #ifdef CONFIG_FRONTSWAP
  416. &dev_attr_frontswap_selfshrinking.attr,
  417. &dev_attr_frontswap_hysteresis.attr,
  418. &dev_attr_frontswap_inertia.attr,
  419. #endif
  420. NULL
  421. };
  422. static const struct attribute_group selfballoon_group = {
  423. .name = "selfballoon",
  424. .attrs = selfballoon_attrs
  425. };
  426. #endif
  427. int register_xen_selfballooning(struct device *dev)
  428. {
  429. int error = -1;
  430. #ifdef CONFIG_SYSFS
  431. error = sysfs_create_group(&dev->kobj, &selfballoon_group);
  432. #endif
  433. return error;
  434. }
  435. EXPORT_SYMBOL(register_xen_selfballooning);
  436. static int __init xen_selfballoon_init(void)
  437. {
  438. bool enable = false;
  439. if (!xen_domain())
  440. return -ENODEV;
  441. if (xen_initial_domain()) {
  442. pr_info("xen/balloon: Xen selfballooning driver "
  443. "disabled for domain0.\n");
  444. return -ENODEV;
  445. }
  446. xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
  447. if (xen_selfballooning_enabled) {
  448. pr_info("xen/balloon: Initializing Xen "
  449. "selfballooning driver.\n");
  450. enable = true;
  451. }
  452. #ifdef CONFIG_FRONTSWAP
  453. frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
  454. if (frontswap_selfshrinking) {
  455. pr_info("xen/balloon: Initializing frontswap "
  456. "selfshrinking driver.\n");
  457. enable = true;
  458. }
  459. #endif
  460. if (!enable)
  461. return -ENODEV;
  462. schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
  463. return 0;
  464. }
  465. subsys_initcall(xen_selfballoon_init);
  466. MODULE_LICENSE("GPL");