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