xen-selfballoon.c 15 KB

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