xen-selfballoon.c 17 KB

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