xen-selfballoon.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 "tmem.selfshrink=0" 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. /*
  103. * Amount of RAM in MB to add to the target number of pages.
  104. * Can be used to reserve some more room for caches and the like.
  105. */
  106. static unsigned int selfballoon_reserved_mb;
  107. static void selfballoon_process(struct work_struct *work);
  108. static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
  109. #ifdef CONFIG_FRONTSWAP
  110. #include <linux/frontswap.h>
  111. /* Enable/disable with sysfs. */
  112. static bool frontswap_selfshrinking __read_mostly;
  113. /*
  114. * The default values for the following parameters were deemed reasonable
  115. * by experimentation, may be workload-dependent, and can all be
  116. * adjusted via sysfs.
  117. */
  118. /* Control rate for frontswap shrinking. Higher hysteresis is slower. */
  119. static unsigned int frontswap_hysteresis __read_mostly = 20;
  120. /*
  121. * Number of selfballoon worker invocations to wait before observing that
  122. * frontswap selfshrinking should commence. Note that selfshrinking does
  123. * not use a separate worker thread.
  124. */
  125. static unsigned int frontswap_inertia __read_mostly = 3;
  126. /* Countdown to next invocation of frontswap_shrink() */
  127. static unsigned long frontswap_inertia_counter;
  128. /*
  129. * Invoked by the selfballoon worker thread, uses current number of pages
  130. * in frontswap (frontswap_curr_pages()), previous status, and control
  131. * values (hysteresis and inertia) to determine if frontswap should be
  132. * shrunk and what the new frontswap size should be. Note that
  133. * frontswap_shrink is essentially a partial swapoff that immediately
  134. * transfers pages from the "swap device" (frontswap) back into kernel
  135. * RAM; despite the name, frontswap "shrinking" is very different from
  136. * the "shrinker" interface used by the kernel MM subsystem to reclaim
  137. * memory.
  138. */
  139. static void frontswap_selfshrink(void)
  140. {
  141. static unsigned long cur_frontswap_pages;
  142. static unsigned long last_frontswap_pages;
  143. static unsigned long tgt_frontswap_pages;
  144. last_frontswap_pages = cur_frontswap_pages;
  145. cur_frontswap_pages = frontswap_curr_pages();
  146. if (!cur_frontswap_pages ||
  147. (cur_frontswap_pages > last_frontswap_pages)) {
  148. frontswap_inertia_counter = frontswap_inertia;
  149. return;
  150. }
  151. if (frontswap_inertia_counter && --frontswap_inertia_counter)
  152. return;
  153. if (cur_frontswap_pages <= frontswap_hysteresis)
  154. tgt_frontswap_pages = 0;
  155. else
  156. tgt_frontswap_pages = cur_frontswap_pages -
  157. (cur_frontswap_pages / frontswap_hysteresis);
  158. frontswap_shrink(tgt_frontswap_pages);
  159. }
  160. /* Disable with kernel boot option. */
  161. static bool use_selfballooning = true;
  162. static int __init xen_noselfballooning_setup(char *s)
  163. {
  164. use_selfballooning = false;
  165. return 1;
  166. }
  167. __setup("noselfballooning", xen_noselfballooning_setup);
  168. #else /* !CONFIG_FRONTSWAP */
  169. /* Enable with kernel boot option. */
  170. static bool use_selfballooning;
  171. static int __init xen_selfballooning_setup(char *s)
  172. {
  173. use_selfballooning = true;
  174. return 1;
  175. }
  176. __setup("selfballooning", xen_selfballooning_setup);
  177. #endif /* CONFIG_FRONTSWAP */
  178. #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
  179. /*
  180. * Use current balloon size, the goal (vm_committed_as), and hysteresis
  181. * parameters to set a new target balloon size
  182. */
  183. static void selfballoon_process(struct work_struct *work)
  184. {
  185. unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
  186. unsigned long useful_pages;
  187. bool reset_timer = false;
  188. if (xen_selfballooning_enabled) {
  189. cur_pages = totalram_pages;
  190. tgt_pages = cur_pages; /* default is no change */
  191. goal_pages = vm_memory_committed() +
  192. totalreserve_pages +
  193. MB2PAGES(selfballoon_reserved_mb);
  194. #ifdef CONFIG_FRONTSWAP
  195. /* allow space for frontswap pages to be repatriated */
  196. if (frontswap_selfshrinking && frontswap_enabled)
  197. goal_pages += frontswap_curr_pages();
  198. #endif
  199. if (cur_pages > goal_pages)
  200. tgt_pages = cur_pages -
  201. ((cur_pages - goal_pages) /
  202. selfballoon_downhysteresis);
  203. else if (cur_pages < goal_pages)
  204. tgt_pages = cur_pages +
  205. ((goal_pages - cur_pages) /
  206. selfballoon_uphysteresis);
  207. /* else if cur_pages == goal_pages, no change */
  208. useful_pages = max_pfn - totalreserve_pages;
  209. if (selfballoon_min_usable_mb != 0)
  210. floor_pages = totalreserve_pages +
  211. MB2PAGES(selfballoon_min_usable_mb);
  212. /* piecewise linear function ending in ~3% slope */
  213. else if (useful_pages < MB2PAGES(16))
  214. floor_pages = max_pfn; /* not worth ballooning */
  215. else if (useful_pages < MB2PAGES(64))
  216. floor_pages = totalreserve_pages + MB2PAGES(16) +
  217. ((useful_pages - MB2PAGES(16)) >> 1);
  218. else if (useful_pages < MB2PAGES(512))
  219. floor_pages = totalreserve_pages + MB2PAGES(40) +
  220. ((useful_pages - MB2PAGES(40)) >> 3);
  221. else /* useful_pages >= MB2PAGES(512) */
  222. floor_pages = totalreserve_pages + MB2PAGES(99) +
  223. ((useful_pages - MB2PAGES(99)) >> 5);
  224. if (tgt_pages < floor_pages)
  225. tgt_pages = floor_pages;
  226. balloon_set_new_target(tgt_pages +
  227. balloon_stats.current_pages - totalram_pages);
  228. reset_timer = true;
  229. }
  230. #ifdef CONFIG_FRONTSWAP
  231. if (frontswap_selfshrinking && frontswap_enabled) {
  232. frontswap_selfshrink();
  233. reset_timer = true;
  234. }
  235. #endif
  236. if (reset_timer)
  237. schedule_delayed_work(&selfballoon_worker,
  238. selfballoon_interval * HZ);
  239. }
  240. #ifdef CONFIG_SYSFS
  241. #include <linux/capability.h>
  242. #define SELFBALLOON_SHOW(name, format, args...) \
  243. static ssize_t show_##name(struct device *dev, \
  244. struct device_attribute *attr, \
  245. char *buf) \
  246. { \
  247. return sprintf(buf, format, ##args); \
  248. }
  249. SELFBALLOON_SHOW(selfballooning, "%d\n", xen_selfballooning_enabled);
  250. static ssize_t store_selfballooning(struct device *dev,
  251. struct device_attribute *attr,
  252. const char *buf,
  253. size_t count)
  254. {
  255. bool was_enabled = xen_selfballooning_enabled;
  256. unsigned long tmp;
  257. int err;
  258. if (!capable(CAP_SYS_ADMIN))
  259. return -EPERM;
  260. err = strict_strtoul(buf, 10, &tmp);
  261. if (err || ((tmp != 0) && (tmp != 1)))
  262. return -EINVAL;
  263. xen_selfballooning_enabled = !!tmp;
  264. if (!was_enabled && xen_selfballooning_enabled)
  265. schedule_delayed_work(&selfballoon_worker,
  266. selfballoon_interval * HZ);
  267. return count;
  268. }
  269. static DEVICE_ATTR(selfballooning, S_IRUGO | S_IWUSR,
  270. show_selfballooning, store_selfballooning);
  271. SELFBALLOON_SHOW(selfballoon_interval, "%d\n", selfballoon_interval);
  272. static ssize_t store_selfballoon_interval(struct device *dev,
  273. struct device_attribute *attr,
  274. const char *buf,
  275. size_t count)
  276. {
  277. unsigned long val;
  278. int err;
  279. if (!capable(CAP_SYS_ADMIN))
  280. return -EPERM;
  281. err = strict_strtoul(buf, 10, &val);
  282. if (err || val == 0)
  283. return -EINVAL;
  284. selfballoon_interval = val;
  285. return count;
  286. }
  287. static DEVICE_ATTR(selfballoon_interval, S_IRUGO | S_IWUSR,
  288. show_selfballoon_interval, store_selfballoon_interval);
  289. SELFBALLOON_SHOW(selfballoon_downhys, "%d\n", selfballoon_downhysteresis);
  290. static ssize_t store_selfballoon_downhys(struct device *dev,
  291. struct device_attribute *attr,
  292. const char *buf,
  293. size_t count)
  294. {
  295. unsigned long val;
  296. int err;
  297. if (!capable(CAP_SYS_ADMIN))
  298. return -EPERM;
  299. err = strict_strtoul(buf, 10, &val);
  300. if (err || val == 0)
  301. return -EINVAL;
  302. selfballoon_downhysteresis = val;
  303. return count;
  304. }
  305. static DEVICE_ATTR(selfballoon_downhysteresis, S_IRUGO | S_IWUSR,
  306. show_selfballoon_downhys, store_selfballoon_downhys);
  307. SELFBALLOON_SHOW(selfballoon_uphys, "%d\n", selfballoon_uphysteresis);
  308. static ssize_t store_selfballoon_uphys(struct device *dev,
  309. struct device_attribute *attr,
  310. const char *buf,
  311. size_t count)
  312. {
  313. unsigned long val;
  314. int err;
  315. if (!capable(CAP_SYS_ADMIN))
  316. return -EPERM;
  317. err = strict_strtoul(buf, 10, &val);
  318. if (err || val == 0)
  319. return -EINVAL;
  320. selfballoon_uphysteresis = val;
  321. return count;
  322. }
  323. static DEVICE_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
  324. show_selfballoon_uphys, store_selfballoon_uphys);
  325. SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
  326. selfballoon_min_usable_mb);
  327. static ssize_t store_selfballoon_min_usable_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_min_usable_mb = val;
  340. return count;
  341. }
  342. static DEVICE_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
  343. show_selfballoon_min_usable_mb,
  344. store_selfballoon_min_usable_mb);
  345. SELFBALLOON_SHOW(selfballoon_reserved_mb, "%d\n",
  346. selfballoon_reserved_mb);
  347. static ssize_t store_selfballoon_reserved_mb(struct device *dev,
  348. struct device_attribute *attr,
  349. const char *buf,
  350. size_t count)
  351. {
  352. unsigned long val;
  353. int err;
  354. if (!capable(CAP_SYS_ADMIN))
  355. return -EPERM;
  356. err = strict_strtoul(buf, 10, &val);
  357. if (err || val == 0)
  358. return -EINVAL;
  359. selfballoon_reserved_mb = val;
  360. return count;
  361. }
  362. static DEVICE_ATTR(selfballoon_reserved_mb, S_IRUGO | S_IWUSR,
  363. show_selfballoon_reserved_mb,
  364. store_selfballoon_reserved_mb);
  365. #ifdef CONFIG_FRONTSWAP
  366. SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
  367. static ssize_t store_frontswap_selfshrinking(struct device *dev,
  368. struct device_attribute *attr,
  369. const char *buf,
  370. size_t count)
  371. {
  372. bool was_enabled = frontswap_selfshrinking;
  373. unsigned long tmp;
  374. int err;
  375. if (!capable(CAP_SYS_ADMIN))
  376. return -EPERM;
  377. err = strict_strtoul(buf, 10, &tmp);
  378. if (err || ((tmp != 0) && (tmp != 1)))
  379. return -EINVAL;
  380. frontswap_selfshrinking = !!tmp;
  381. if (!was_enabled && !xen_selfballooning_enabled &&
  382. frontswap_selfshrinking)
  383. schedule_delayed_work(&selfballoon_worker,
  384. selfballoon_interval * HZ);
  385. return count;
  386. }
  387. static DEVICE_ATTR(frontswap_selfshrinking, S_IRUGO | S_IWUSR,
  388. show_frontswap_selfshrinking, store_frontswap_selfshrinking);
  389. SELFBALLOON_SHOW(frontswap_inertia, "%d\n", frontswap_inertia);
  390. static ssize_t store_frontswap_inertia(struct device *dev,
  391. struct device_attribute *attr,
  392. const char *buf,
  393. size_t count)
  394. {
  395. unsigned long val;
  396. int err;
  397. if (!capable(CAP_SYS_ADMIN))
  398. return -EPERM;
  399. err = strict_strtoul(buf, 10, &val);
  400. if (err || val == 0)
  401. return -EINVAL;
  402. frontswap_inertia = val;
  403. frontswap_inertia_counter = val;
  404. return count;
  405. }
  406. static DEVICE_ATTR(frontswap_inertia, S_IRUGO | S_IWUSR,
  407. show_frontswap_inertia, store_frontswap_inertia);
  408. SELFBALLOON_SHOW(frontswap_hysteresis, "%d\n", frontswap_hysteresis);
  409. static ssize_t store_frontswap_hysteresis(struct device *dev,
  410. struct device_attribute *attr,
  411. const char *buf,
  412. size_t count)
  413. {
  414. unsigned long val;
  415. int err;
  416. if (!capable(CAP_SYS_ADMIN))
  417. return -EPERM;
  418. err = strict_strtoul(buf, 10, &val);
  419. if (err || val == 0)
  420. return -EINVAL;
  421. frontswap_hysteresis = val;
  422. return count;
  423. }
  424. static DEVICE_ATTR(frontswap_hysteresis, S_IRUGO | S_IWUSR,
  425. show_frontswap_hysteresis, store_frontswap_hysteresis);
  426. #endif /* CONFIG_FRONTSWAP */
  427. static struct attribute *selfballoon_attrs[] = {
  428. &dev_attr_selfballooning.attr,
  429. &dev_attr_selfballoon_interval.attr,
  430. &dev_attr_selfballoon_downhysteresis.attr,
  431. &dev_attr_selfballoon_uphysteresis.attr,
  432. &dev_attr_selfballoon_min_usable_mb.attr,
  433. &dev_attr_selfballoon_reserved_mb.attr,
  434. #ifdef CONFIG_FRONTSWAP
  435. &dev_attr_frontswap_selfshrinking.attr,
  436. &dev_attr_frontswap_hysteresis.attr,
  437. &dev_attr_frontswap_inertia.attr,
  438. #endif
  439. NULL
  440. };
  441. static const struct attribute_group selfballoon_group = {
  442. .name = "selfballoon",
  443. .attrs = selfballoon_attrs
  444. };
  445. #endif
  446. int register_xen_selfballooning(struct device *dev)
  447. {
  448. int error = -1;
  449. #ifdef CONFIG_SYSFS
  450. error = sysfs_create_group(&dev->kobj, &selfballoon_group);
  451. #endif
  452. return error;
  453. }
  454. EXPORT_SYMBOL(register_xen_selfballooning);
  455. int xen_selfballoon_init(bool use_selfballooning, bool use_frontswap_selfshrink)
  456. {
  457. bool enable = false;
  458. if (!xen_domain())
  459. return -ENODEV;
  460. if (xen_initial_domain()) {
  461. pr_info("xen/balloon: Xen selfballooning driver "
  462. "disabled for domain0.\n");
  463. return -ENODEV;
  464. }
  465. xen_selfballooning_enabled = tmem_enabled && use_selfballooning;
  466. if (xen_selfballooning_enabled) {
  467. pr_info("xen/balloon: Initializing Xen "
  468. "selfballooning driver.\n");
  469. enable = true;
  470. }
  471. #ifdef CONFIG_FRONTSWAP
  472. frontswap_selfshrinking = tmem_enabled && use_frontswap_selfshrink;
  473. if (frontswap_selfshrinking) {
  474. pr_info("xen/balloon: Initializing frontswap "
  475. "selfshrinking driver.\n");
  476. enable = true;
  477. }
  478. #endif
  479. if (!enable)
  480. return -ENODEV;
  481. schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
  482. return 0;
  483. }
  484. EXPORT_SYMBOL(xen_selfballoon_init);