xen-selfballoon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/mm.h>
  71. #include <linux/mman.h>
  72. #include <linux/workqueue.h>
  73. #include <xen/balloon.h>
  74. #include <xen/tmem.h>
  75. #include <xen/xen.h>
  76. /* Enable/disable with sysfs. */
  77. static int xen_selfballooning_enabled __read_mostly;
  78. /*
  79. * Controls rate at which memory target (this iteration) approaches
  80. * ultimate goal when memory need is increasing (up-hysteresis) or
  81. * decreasing (down-hysteresis). Higher values of hysteresis cause
  82. * slower increases/decreases. The default values for the various
  83. * parameters were deemed reasonable by experimentation, may be
  84. * workload-dependent, and can all be adjusted via sysfs.
  85. */
  86. static unsigned int selfballoon_downhysteresis __read_mostly = 8;
  87. static unsigned int selfballoon_uphysteresis __read_mostly = 1;
  88. /* In HZ, controls frequency of worker invocation. */
  89. static unsigned int selfballoon_interval __read_mostly = 5;
  90. static void selfballoon_process(struct work_struct *work);
  91. static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
  92. #ifdef CONFIG_FRONTSWAP
  93. #include <linux/frontswap.h>
  94. /* Enable/disable with sysfs. */
  95. static bool frontswap_selfshrinking __read_mostly;
  96. /* Enable/disable with kernel boot option. */
  97. static bool use_frontswap_selfshrink __initdata = true;
  98. /*
  99. * The default values for the following parameters were deemed reasonable
  100. * by experimentation, may be workload-dependent, and can all be
  101. * adjusted via sysfs.
  102. */
  103. /* Control rate for frontswap shrinking. Higher hysteresis is slower. */
  104. static unsigned int frontswap_hysteresis __read_mostly = 20;
  105. /*
  106. * Number of selfballoon worker invocations to wait before observing that
  107. * frontswap selfshrinking should commence. Note that selfshrinking does
  108. * not use a separate worker thread.
  109. */
  110. static unsigned int frontswap_inertia __read_mostly = 3;
  111. /* Countdown to next invocation of frontswap_shrink() */
  112. static unsigned long frontswap_inertia_counter;
  113. /*
  114. * Invoked by the selfballoon worker thread, uses current number of pages
  115. * in frontswap (frontswap_curr_pages()), previous status, and control
  116. * values (hysteresis and inertia) to determine if frontswap should be
  117. * shrunk and what the new frontswap size should be. Note that
  118. * frontswap_shrink is essentially a partial swapoff that immediately
  119. * transfers pages from the "swap device" (frontswap) back into kernel
  120. * RAM; despite the name, frontswap "shrinking" is very different from
  121. * the "shrinker" interface used by the kernel MM subsystem to reclaim
  122. * memory.
  123. */
  124. static void frontswap_selfshrink(void)
  125. {
  126. static unsigned long cur_frontswap_pages;
  127. static unsigned long last_frontswap_pages;
  128. static unsigned long tgt_frontswap_pages;
  129. last_frontswap_pages = cur_frontswap_pages;
  130. cur_frontswap_pages = frontswap_curr_pages();
  131. if (!cur_frontswap_pages ||
  132. (cur_frontswap_pages > last_frontswap_pages)) {
  133. frontswap_inertia_counter = frontswap_inertia;
  134. return;
  135. }
  136. if (frontswap_inertia_counter && --frontswap_inertia_counter)
  137. return;
  138. if (cur_frontswap_pages <= frontswap_hysteresis)
  139. tgt_frontswap_pages = 0;
  140. else
  141. tgt_frontswap_pages = cur_frontswap_pages -
  142. (cur_frontswap_pages / frontswap_hysteresis);
  143. frontswap_shrink(tgt_frontswap_pages);
  144. }
  145. static int __init xen_nofrontswap_selfshrink_setup(char *s)
  146. {
  147. use_frontswap_selfshrink = false;
  148. return 1;
  149. }
  150. __setup("noselfshrink", xen_nofrontswap_selfshrink_setup);
  151. /* Disable with kernel boot option. */
  152. static bool use_selfballooning __initdata = true;
  153. static int __init xen_noselfballooning_setup(char *s)
  154. {
  155. use_selfballooning = false;
  156. return 1;
  157. }
  158. __setup("noselfballooning", xen_noselfballooning_setup);
  159. #else /* !CONFIG_FRONTSWAP */
  160. /* Enable with kernel boot option. */
  161. static bool use_selfballooning __initdata = false;
  162. static int __init xen_selfballooning_setup(char *s)
  163. {
  164. use_selfballooning = true;
  165. return 1;
  166. }
  167. __setup("selfballooning", xen_selfballooning_setup);
  168. #endif /* CONFIG_FRONTSWAP */
  169. /*
  170. * Use current balloon size, the goal (vm_committed_as), and hysteresis
  171. * parameters to set a new target balloon size
  172. */
  173. static void selfballoon_process(struct work_struct *work)
  174. {
  175. unsigned long cur_pages, goal_pages, tgt_pages;
  176. bool reset_timer = false;
  177. if (xen_selfballooning_enabled) {
  178. cur_pages = balloon_stats.current_pages;
  179. tgt_pages = cur_pages; /* default is no change */
  180. goal_pages = percpu_counter_read_positive(&vm_committed_as) +
  181. balloon_stats.current_pages - totalram_pages;
  182. #ifdef CONFIG_FRONTSWAP
  183. /* allow space for frontswap pages to be repatriated */
  184. if (frontswap_selfshrinking && frontswap_enabled)
  185. goal_pages += frontswap_curr_pages();
  186. #endif
  187. if (cur_pages > goal_pages)
  188. tgt_pages = cur_pages -
  189. ((cur_pages - goal_pages) /
  190. selfballoon_downhysteresis);
  191. else if (cur_pages < goal_pages)
  192. tgt_pages = cur_pages +
  193. ((goal_pages - cur_pages) /
  194. selfballoon_uphysteresis);
  195. /* else if cur_pages == goal_pages, no change */
  196. balloon_set_new_target(tgt_pages);
  197. reset_timer = true;
  198. }
  199. #ifdef CONFIG_FRONTSWAP
  200. if (frontswap_selfshrinking && frontswap_enabled) {
  201. frontswap_selfshrink();
  202. reset_timer = true;
  203. }
  204. #endif
  205. if (reset_timer)
  206. schedule_delayed_work(&selfballoon_worker,
  207. selfballoon_interval * HZ);
  208. }
  209. #ifdef CONFIG_SYSFS
  210. #include <linux/sysdev.h>
  211. #include <linux/capability.h>
  212. #define SELFBALLOON_SHOW(name, format, args...) \
  213. static ssize_t show_##name(struct sys_device *dev, \
  214. struct sysdev_attribute *attr, \
  215. char *buf) \
  216. { \
  217. return sprintf(buf, format, ##args); \
  218. }
  219. SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
  220. static ssize_t store_selfballooning(struct sys_device *dev,
  221. struct sysdev_attribute *attr,
  222. const char *buf,
  223. size_t count)
  224. {
  225. bool was_enabled = xen_selfballooning_enabled;
  226. unsigned long tmp;
  227. int err;
  228. if (!capable(CAP_SYS_ADMIN))
  229. return -EPERM;
  230. err = strict_strtoul(buf, 10, &tmp);
  231. if (err || ((tmp != 0) && (tmp != 1)))
  232. return -EINVAL;
  233. xen_selfballooning_enabled = !!tmp;
  234. if (!was_enabled && xen_selfballooning_enabled)
  235. schedule_delayed_work(&selfballoon_worker,
  236. selfballoon_interval * HZ);
  237. return count;
  238. }
  239. static SYSDEV_ATTR(selfballooning, S_IRUGO | S_IWUSR,
  240. show_selfballooning, store_selfballooning);
  241. SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
  242. static ssize_t store_selfballoon_interval(struct sys_device *dev,
  243. struct sysdev_attribute *attr,
  244. const char *buf,
  245. size_t count)
  246. {
  247. unsigned long val;
  248. int err;
  249. if (!capable(CAP_SYS_ADMIN))
  250. return -EPERM;
  251. err = strict_strtoul(buf, 10, &val);
  252. if (err || val == 0)
  253. return -EINVAL;
  254. selfballoon_interval = val;
  255. return count;
  256. }
  257. static SYSDEV_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
  258. show_selfballoon_interval, store_selfballoon_interval);
  259. SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
  260. static ssize_t store_selfballoon_downhys(struct sys_device *dev,
  261. struct sysdev_attribute *attr,
  262. const char *buf,
  263. size_t count)
  264. {
  265. unsigned long val;
  266. int err;
  267. if (!capable(CAP_SYS_ADMIN))
  268. return -EPERM;
  269. err = strict_strtoul(buf, 10, &val);
  270. if (err || val == 0)
  271. return -EINVAL;
  272. selfballoon_downhysteresis = val;
  273. return count;
  274. }
  275. static SYSDEV_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
  276. show_selfballoon_downhys, store_selfballoon_downhys);
  277. SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
  278. static ssize_t store_selfballoon_uphys(struct sys_device *dev,
  279. struct sysdev_attribute *attr,
  280. const char *buf,
  281. size_t count)
  282. {
  283. unsigned long val;
  284. int err;
  285. if (!capable(CAP_SYS_ADMIN))
  286. return -EPERM;
  287. err = strict_strtoul(buf, 10, &val);
  288. if (err || val == 0)
  289. return -EINVAL;
  290. selfballoon_uphysteresis = val;
  291. return count;
  292. }
  293. static SYSDEV_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
  294. show_selfballoon_uphys, store_selfballoon_uphys);
  295. #ifdef CONFIG_FRONTSWAP
  296. SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
  297. static ssize_t store_frontswap_selfshrinking(struct sys_device *dev,
  298. struct sysdev_attribute *attr,
  299. const char *buf,
  300. size_t count)
  301. {
  302. bool was_enabled = frontswap_selfshrinking;
  303. unsigned long tmp;
  304. int err;
  305. if (!capable(CAP_SYS_ADMIN))
  306. return -EPERM;
  307. err = strict_strtoul(buf, 10, &tmp);
  308. if (err || ((tmp != 0) && (tmp != 1)))
  309. return -EINVAL;
  310. frontswap_selfshrinking = !!tmp;
  311. if (!was_enabled && !xen_selfballooning_enabled &&
  312. frontswap_selfshrinking)
  313. schedule_delayed_work(&selfballoon_worker,
  314. selfballoon_interval * HZ);
  315. return count;
  316. }
  317. static SYSDEV_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
  318. show_frontswap_selfshrinking, store_frontswap_selfshrinking);
  319. SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
  320. static ssize_t store_frontswap_inertia(struct sys_device *dev,
  321. struct sysdev_attribute *attr,
  322. const char *buf,
  323. size_t count)
  324. {
  325. unsigned long val;
  326. int err;
  327. if (!capable(CAP_SYS_ADMIN))
  328. return -EPERM;
  329. err = strict_strtoul(buf, 10, &val);
  330. if (err || val == 0)
  331. return -EINVAL;
  332. frontswap_inertia = val;
  333. frontswap_inertia_counter = val;
  334. return count;
  335. }
  336. static SYSDEV_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
  337. show_frontswap_inertia, store_frontswap_inertia);
  338. SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
  339. static ssize_t store_frontswap_hysteresis(struct sys_device *dev,
  340. struct sysdev_attribute *attr,
  341. const char *buf,
  342. size_t count)
  343. {
  344. unsigned long val;
  345. int err;
  346. if (!capable(CAP_SYS_ADMIN))
  347. return -EPERM;
  348. err = strict_strtoul(buf, 10, &val);
  349. if (err || val == 0)
  350. return -EINVAL;
  351. frontswap_hysteresis = val;
  352. return count;
  353. }
  354. static SYSDEV_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
  355. show_frontswap_hysteresis, store_frontswap_hysteresis);
  356. #endif /* CONFIG_FRONTSWAP */
  357. static struct attribute *selfballoon_attrs[] = {
  358. &attr_selfballooning.attr,
  359. &attr_selfballoon_interval.attr,
  360. &attr_selfballoon_downhysteresis.attr,
  361. &attr_selfballoon_uphysteresis.attr,
  362. #ifdef CONFIG_FRONTSWAP
  363. &attr_frontswap_selfshrinking.attr,
  364. &attr_frontswap_hysteresis.attr,
  365. &attr_frontswap_inertia.attr,
  366. #endif
  367. NULL
  368. };
  369. static struct attribute_group selfballoon_group = {
  370. .name = "selfballoon",
  371. .attrs = selfballoon_attrs
  372. };
  373. #endif
  374. int register_xen_selfballooning(struct sys_device *sysdev)
  375. {
  376. int error = -1;
  377. #ifdef CONFIG_SYSFS
  378. error = sysfs_create_group(&sysdev->kobj, &selfballoon_group);
  379. #endif
  380. return error;
  381. }
  382. EXPORT_SYMBOL(register_xen_selfballooning);
  383. static int __init xen_selfballoon_init(void)
  384. {
  385. bool enable = false;
  386. if (!xen_domain())
  387. return -ENODEV;
  388. if (xen_initial_domain()) {
  389. pr_info("xen/balloon: Xen selfballooning driver "
  390. "disabled for domain0.\n");
  391. return -ENODEV;
  392. }
  393. xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
  394. if (xen_selfballooning_enabled) {
  395. pr_info("xen/balloon: Initializing Xen "
  396. "selfballooning driver.\n");
  397. enable = true;
  398. }
  399. #ifdef CONFIG_FRONTSWAP
  400. frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
  401. if (frontswap_selfshrinking) {
  402. pr_info("xen/balloon: Initializing frontswap "
  403. "selfshrinking driver.\n");
  404. enable = true;
  405. }
  406. #endif
  407. if (!enable)
  408. return -ENODEV;
  409. schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
  410. return 0;
  411. }
  412. subsys_initcall(xen_selfballoon_init);
  413. MODULE_LICENSE("GPL");