xen-selfballoon.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. Note, that selfballooning should be disabled by default
  57. * if frontswap is not configured. Similarly selfballooning should be enabled
  58. * by default if frontswap is configured and can be disabled with the
  59. * "tmem.selfballooning=0" kernel boot option. Finally, when frontswap is
  60. * configured, frontswap-selfshrinking can be disabled with the
  61. * "tmem.selfshrink=0" kernel boot option.
  62. *
  63. * Selfballooning is disallowed in domain0 and force-disabled.
  64. *
  65. */
  66. #include <linux/kernel.h>
  67. #include <linux/bootmem.h>
  68. #include <linux/swap.h>
  69. #include <linux/mm.h>
  70. #include <linux/mman.h>
  71. #include <linux/module.h>
  72. #include <linux/workqueue.h>
  73. #include <linux/device.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. /*
  92. * Minimum usable RAM in MB for selfballooning target for balloon.
  93. * If non-zero, it is added to totalreserve_pages and self-ballooning
  94. * will not balloon below the sum. If zero, a piecewise linear function
  95. * is calculated as a minimum and added to totalreserve_pages. Note that
  96. * setting this value indiscriminately may cause OOMs and crashes.
  97. */
  98. static unsigned int selfballoon_min_usable_mb;
  99. /*
  100. * Amount of RAM in MB to add to the target number of pages.
  101. * Can be used to reserve some more room for caches and the like.
  102. */
  103. static unsigned int selfballoon_reserved_mb;
  104. static void selfballoon_process(struct work_struct *work);
  105. static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
  106. #ifdef CONFIG_FRONTSWAP
  107. #include <linux/frontswap.h>
  108. /* Enable/disable with sysfs. */
  109. static bool frontswap_selfshrinking __read_mostly;
  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. #endif /* CONFIG_FRONTSWAP */
  158. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  159. /*
  160. * Use current balloon size, the goal (vm_committed_as), and hysteresis
  161. * parameters to set a new target balloon size
  162. */
  163. static void selfballoon_process(struct work_struct *work)
  164. {
  165. unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
  166. unsigned long useful_pages;
  167. bool reset_timer = false;
  168. if (xen_selfballooning_enabled) {
  169. cur_pages = totalram_pages;
  170. tgt_pages = cur_pages; /* default is no change */
  171. goal_pages = vm_memory_committed() +
  172. totalreserve_pages +
  173. MB2PAGES(selfballoon_reserved_mb);
  174. #ifdef CONFIG_FRONTSWAP
  175. /* allow space for frontswap pages to be repatriated */
  176. if (frontswap_selfshrinking && frontswap_enabled)
  177. goal_pages += frontswap_curr_pages();
  178. #endif
  179. if (cur_pages > goal_pages)
  180. tgt_pages = cur_pages -
  181. ((cur_pages - goal_pages) /
  182. selfballoon_downhysteresis);
  183. else if (cur_pages < goal_pages)
  184. tgt_pages = cur_pages +
  185. ((goal_pages - cur_pages) /
  186. selfballoon_uphysteresis);
  187. /* else if cur_pages == goal_pages, no change */
  188. useful_pages = max_pfn - totalreserve_pages;
  189. if (selfballoon_min_usable_mb != 0)
  190. floor_pages = totalreserve_pages +
  191. MB2PAGES(selfballoon_min_usable_mb);
  192. /* piecewise linear function ending in ~3% slope */
  193. else if (useful_pages < MB2PAGES(16))
  194. floor_pages = max_pfn; /* not worth ballooning */
  195. else if (useful_pages < MB2PAGES(64))
  196. floor_pages = totalreserve_pages + MB2PAGES(16) +
  197. ((useful_pages - MB2PAGES(16)) >> 1);
  198. else if (useful_pages < MB2PAGES(512))
  199. floor_pages = totalreserve_pages + MB2PAGES(40) +
  200. ((useful_pages - MB2PAGES(40)) >> 3);
  201. else /* useful_pages >= MB2PAGES(512) */
  202. floor_pages = totalreserve_pages + MB2PAGES(99) +
  203. ((useful_pages - MB2PAGES(99)) >> 5);
  204. if (tgt_pages < floor_pages)
  205. tgt_pages = floor_pages;
  206. balloon_set_new_target(tgt_pages +
  207. balloon_stats.current_pages - totalram_pages);
  208. reset_timer = true;
  209. }
  210. #ifdef CONFIG_FRONTSWAP
  211. if (frontswap_selfshrinking && frontswap_enabled) {
  212. frontswap_selfshrink();
  213. reset_timer = true;
  214. }
  215. #endif
  216. if (reset_timer)
  217. schedule_delayed_work(&selfballoon_worker,
  218. selfballoon_interval * HZ);
  219. }
  220. #ifdef CONFIG_SYSFS
  221. #include <linux/capability.h>
  222. #define SELFBALLOON_SHOW(name, format, args...) \
  223. static ssize_t show_##name(struct device *dev, \
  224. struct device_attribute *attr, \
  225. char *buf) \
  226. { \
  227. return sprintf(buf, format, ##args); \
  228. }
  229. SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
  230. static ssize_t store_selfballooning(struct device *dev,
  231. struct device_attribute *attr,
  232. const char *buf,
  233. size_t count)
  234. {
  235. bool was_enabled = xen_selfballooning_enabled;
  236. unsigned long tmp;
  237. int err;
  238. if (!capable(CAP_SYS_ADMIN))
  239. return -EPERM;
  240. err = strict_strtoul(buf, 10, &tmp);
  241. if (err || ((tmp != 0) && (tmp != 1)))
  242. return -EINVAL;
  243. xen_selfballooning_enabled = !!tmp;
  244. if (!was_enabled && xen_selfballooning_enabled)
  245. schedule_delayed_work(&selfballoon_worker,
  246. selfballoon_interval * HZ);
  247. return count;
  248. }
  249. static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
  250. show_selfballooning, store_selfballooning);
  251. SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
  252. static ssize_t store_selfballoon_interval(struct device *dev,
  253. struct device_attribute *attr,
  254. const char *buf,
  255. size_t count)
  256. {
  257. unsigned long val;
  258. int err;
  259. if (!capable(CAP_SYS_ADMIN))
  260. return -EPERM;
  261. err = strict_strtoul(buf, 10, &val);
  262. if (err || val == 0)
  263. return -EINVAL;
  264. selfballoon_interval = val;
  265. return count;
  266. }
  267. static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
  268. show_selfballoon_interval, store_selfballoon_interval);
  269. SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
  270. static ssize_t store_selfballoon_downhys(struct device *dev,
  271. struct device_attribute *attr,
  272. const char *buf,
  273. size_t count)
  274. {
  275. unsigned long val;
  276. int err;
  277. if (!capable(CAP_SYS_ADMIN))
  278. return -EPERM;
  279. err = strict_strtoul(buf, 10, &val);
  280. if (err || val == 0)
  281. return -EINVAL;
  282. selfballoon_downhysteresis = val;
  283. return count;
  284. }
  285. static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
  286. show_selfballoon_downhys, store_selfballoon_downhys);
  287. SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
  288. static ssize_t store_selfballoon_uphys(struct device *dev,
  289. struct device_attribute *attr,
  290. const char *buf,
  291. size_t count)
  292. {
  293. unsigned long val;
  294. int err;
  295. if (!capable(CAP_SYS_ADMIN))
  296. return -EPERM;
  297. err = strict_strtoul(buf, 10, &val);
  298. if (err || val == 0)
  299. return -EINVAL;
  300. selfballoon_uphysteresis = val;
  301. return count;
  302. }
  303. static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
  304. show_selfballoon_uphys, store_selfballoon_uphys);
  305. SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
  306. selfballoon_min_usable_mb);
  307. static ssize_t store_selfballoon_min_usable_mb(struct device *dev,
  308. struct device_attribute *attr,
  309. const char *buf,
  310. size_t count)
  311. {
  312. unsigned long val;
  313. int err;
  314. if (!capable(CAP_SYS_ADMIN))
  315. return -EPERM;
  316. err = strict_strtoul(buf, 10, &val);
  317. if (err || val == 0)
  318. return -EINVAL;
  319. selfballoon_min_usable_mb = val;
  320. return count;
  321. }
  322. static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
  323. show_selfballoon_min_usable_mb,
  324. store_selfballoon_min_usable_mb);
  325. SELFBALLOON_SHOW(selfballoon_reserved_mb, "%d\n",
  326. selfballoon_reserved_mb);
  327. static ssize_t store_selfballoon_reserved_mb(struct device *dev,
  328. struct device_attribute *attr,
  329. const char *buf,
  330. size_t count)
  331. {
  332. unsigned long val;
  333. int err;
  334. if (!capable(CAP_SYS_ADMIN))
  335. return -EPERM;
  336. err = strict_strtoul(buf, 10, &val);
  337. if (err || val == 0)
  338. return -EINVAL;
  339. selfballoon_reserved_mb = val;
  340. return count;
  341. }
  342. static DEVICE_ATTR(selfballoon_reserved_mb, S_IRUGO | S_IWUSR,
  343. show_selfballoon_reserved_mb,
  344. store_selfballoon_reserved_mb);
  345. #ifdef CONFIG_FRONTSWAP
  346. SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
  347. static ssize_t store_frontswap_selfshrinking(struct device *dev,
  348. struct device_attribute *attr,
  349. const char *buf,
  350. size_t count)
  351. {
  352. bool was_enabled = frontswap_selfshrinking;
  353. unsigned long tmp;
  354. int err;
  355. if (!capable(CAP_SYS_ADMIN))
  356. return -EPERM;
  357. err = strict_strtoul(buf, 10, &tmp);
  358. if (err || ((tmp != 0) && (tmp != 1)))
  359. return -EINVAL;
  360. frontswap_selfshrinking = !!tmp;
  361. if (!was_enabled && !xen_selfballooning_enabled &&
  362. frontswap_selfshrinking)
  363. schedule_delayed_work(&selfballoon_worker,
  364. selfballoon_interval * HZ);
  365. return count;
  366. }
  367. static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
  368. show_frontswap_selfshrinking, store_frontswap_selfshrinking);
  369. SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
  370. static ssize_t store_frontswap_inertia(struct device *dev,
  371. struct device_attribute *attr,
  372. const char *buf,
  373. size_t count)
  374. {
  375. unsigned long val;
  376. int err;
  377. if (!capable(CAP_SYS_ADMIN))
  378. return -EPERM;
  379. err = strict_strtoul(buf, 10, &val);
  380. if (err || val == 0)
  381. return -EINVAL;
  382. frontswap_inertia = val;
  383. frontswap_inertia_counter = val;
  384. return count;
  385. }
  386. static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
  387. show_frontswap_inertia, store_frontswap_inertia);
  388. SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
  389. static ssize_t store_frontswap_hysteresis(struct device *dev,
  390. struct device_attribute *attr,
  391. const char *buf,
  392. size_t count)
  393. {
  394. unsigned long val;
  395. int err;
  396. if (!capable(CAP_SYS_ADMIN))
  397. return -EPERM;
  398. err = strict_strtoul(buf, 10, &val);
  399. if (err || val == 0)
  400. return -EINVAL;
  401. frontswap_hysteresis = val;
  402. return count;
  403. }
  404. static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
  405. show_frontswap_hysteresis, store_frontswap_hysteresis);
  406. #endif /* CONFIG_FRONTSWAP */
  407. static struct attribute *selfballoon_attrs[] = {
  408. &dev_attr_selfballooning.attr,
  409. &dev_attr_selfballoon_interval.attr,
  410. &dev_attr_selfballoon_downhysteresis.attr,
  411. &dev_attr_selfballoon_uphysteresis.attr,
  412. &dev_attr_selfballoon_min_usable_mb.attr,
  413. &dev_attr_selfballoon_reserved_mb.attr,
  414. #ifdef CONFIG_FRONTSWAP
  415. &dev_attr_frontswap_selfshrinking.attr,
  416. &dev_attr_frontswap_hysteresis.attr,
  417. &dev_attr_frontswap_inertia.attr,
  418. #endif
  419. NULL
  420. };
  421. static const struct attribute_group selfballoon_group = {
  422. .name = "selfballoon",
  423. .attrs = selfballoon_attrs
  424. };
  425. #endif
  426. int register_xen_selfballooning(struct device *dev)
  427. {
  428. int error = -1;
  429. #ifdef CONFIG_SYSFS
  430. error = sysfs_create_group(&dev->kobj, &selfballoon_group);
  431. #endif
  432. return error;
  433. }
  434. EXPORT_SYMBOL(register_xen_selfballooning);
  435. int xen_selfballoon_init(bool use_selfballooning, bool use_frontswap_selfshrink)
  436. {
  437. bool enable = false;
  438. if (!xen_domain())
  439. return -ENODEV;
  440. if (xen_initial_domain()) {
  441. pr_info("xen/balloon: Xen selfballooning driver "
  442. "disabled for domain0.\n");
  443. return -ENODEV;
  444. }
  445. xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
  446. if (xen_selfballooning_enabled) {
  447. pr_info("xen/balloon: Initializing Xen "
  448. "selfballooning driver.\n");
  449. enable = true;
  450. }
  451. #ifdef CONFIG_FRONTSWAP
  452. frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
  453. if (frontswap_selfshrinking) {
  454. pr_info("xen/balloon: Initializing frontswap "
  455. "selfshrinking driver.\n");
  456. enable = true;
  457. }
  458. #endif
  459. if (!enable)
  460. return -ENODEV;
  461. schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
  462. return 0;
  463. }
  464. EXPORT_SYMBOL(xen_selfballoon_init);