vmw_balloon.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * VMware Balloon driver.
  3. *
  4. * Copyright (C) 2000-2010, VMware, Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; version 2 of the License and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13. * NON INFRINGEMENT. See the GNU General Public License for more
  14. * details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Maintained by: Xavier Deguillard <xdeguillard@vmware.com>
  21. * Philip Moltmann <moltmann@vmware.com>
  22. */
  23. /*
  24. * This is VMware physical memory management driver for Linux. The driver
  25. * acts like a "balloon" that can be inflated to reclaim physical pages by
  26. * reserving them in the guest and invalidating them in the monitor,
  27. * freeing up the underlying machine pages so they can be allocated to
  28. * other guests. The balloon can also be deflated to allow the guest to
  29. * use more physical memory. Higher level policies can control the sizes
  30. * of balloons in VMs in order to manage physical memory resources.
  31. */
  32. //#define DEBUG
  33. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/types.h>
  35. #include <linux/kernel.h>
  36. #include <linux/mm.h>
  37. #include <linux/sched.h>
  38. #include <linux/module.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/seq_file.h>
  42. #include <asm/hypervisor.h>
  43. MODULE_AUTHOR("VMware, Inc.");
  44. MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
  45. MODULE_VERSION("1.3.0.0-k");
  46. MODULE_ALIAS("dmi:*:svnVMware*:*");
  47. MODULE_ALIAS("vmware_vmmemctl");
  48. MODULE_LICENSE("GPL");
  49. /*
  50. * Various constants controlling rate of inflaint/deflating balloon,
  51. * measured in pages.
  52. */
  53. /*
  54. * Rate of allocating memory when there is no memory pressure
  55. * (driver performs non-sleeping allocations).
  56. */
  57. #define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U
  58. /*
  59. * Rates of memory allocaton when guest experiences memory pressure
  60. * (driver performs sleeping allocations).
  61. */
  62. #define VMW_BALLOON_RATE_ALLOC_MIN 512U
  63. #define VMW_BALLOON_RATE_ALLOC_MAX 2048U
  64. #define VMW_BALLOON_RATE_ALLOC_INC 16U
  65. /*
  66. * Rates for releasing pages while deflating balloon.
  67. */
  68. #define VMW_BALLOON_RATE_FREE_MIN 512U
  69. #define VMW_BALLOON_RATE_FREE_MAX 16384U
  70. #define VMW_BALLOON_RATE_FREE_INC 16U
  71. /*
  72. * When guest is under memory pressure, use a reduced page allocation
  73. * rate for next several cycles.
  74. */
  75. #define VMW_BALLOON_SLOW_CYCLES 4
  76. /*
  77. * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't
  78. * allow wait (__GFP_WAIT) for NOSLEEP page allocations. Use
  79. * __GFP_NOWARN, to suppress page allocation failure warnings.
  80. */
  81. #define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
  82. /*
  83. * Use GFP_HIGHUSER when executing in a separate kernel thread
  84. * context and allocation can sleep. This is less stressful to
  85. * the guest memory system, since it allows the thread to block
  86. * while memory is reclaimed, and won't take pages from emergency
  87. * low-memory pools.
  88. */
  89. #define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
  90. /* Maximum number of page allocations without yielding processor */
  91. #define VMW_BALLOON_YIELD_THRESHOLD 1024
  92. /* Maximum number of refused pages we accumulate during inflation cycle */
  93. #define VMW_BALLOON_MAX_REFUSED 16
  94. /*
  95. * Hypervisor communication port definitions.
  96. */
  97. #define VMW_BALLOON_HV_PORT 0x5670
  98. #define VMW_BALLOON_HV_MAGIC 0x456c6d6f
  99. #define VMW_BALLOON_GUEST_ID 1 /* Linux */
  100. enum vmwballoon_capabilities {
  101. /*
  102. * Bit 0 is reserved and not associated to any capability.
  103. */
  104. VMW_BALLOON_BASIC_CMDS = (1 << 1),
  105. VMW_BALLOON_BATCHED_CMDS = (1 << 2)
  106. };
  107. #define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_BASIC_CMDS)
  108. #define VMW_BALLOON_CMD_START 0
  109. #define VMW_BALLOON_CMD_GET_TARGET 1
  110. #define VMW_BALLOON_CMD_LOCK 2
  111. #define VMW_BALLOON_CMD_UNLOCK 3
  112. #define VMW_BALLOON_CMD_GUEST_ID 4
  113. /* error codes */
  114. #define VMW_BALLOON_SUCCESS 0
  115. #define VMW_BALLOON_FAILURE -1
  116. #define VMW_BALLOON_ERROR_CMD_INVALID 1
  117. #define VMW_BALLOON_ERROR_PPN_INVALID 2
  118. #define VMW_BALLOON_ERROR_PPN_LOCKED 3
  119. #define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
  120. #define VMW_BALLOON_ERROR_PPN_PINNED 5
  121. #define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
  122. #define VMW_BALLOON_ERROR_RESET 7
  123. #define VMW_BALLOON_ERROR_BUSY 8
  124. #define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
  125. #define VMWARE_BALLOON_CMD(cmd, data, result) \
  126. ({ \
  127. unsigned long __status, __dummy1, __dummy2; \
  128. __asm__ __volatile__ ("inl %%dx" : \
  129. "=a"(__status), \
  130. "=c"(__dummy1), \
  131. "=d"(__dummy2), \
  132. "=b"(result) : \
  133. "0"(VMW_BALLOON_HV_MAGIC), \
  134. "1"(VMW_BALLOON_CMD_##cmd), \
  135. "2"(VMW_BALLOON_HV_PORT), \
  136. "3"(data) : \
  137. "memory"); \
  138. if (VMW_BALLOON_CMD_##cmd == VMW_BALLOON_CMD_START) \
  139. result = __dummy1; \
  140. result &= -1UL; \
  141. __status & -1UL; \
  142. })
  143. #ifdef CONFIG_DEBUG_FS
  144. struct vmballoon_stats {
  145. unsigned int timer;
  146. /* allocation statistics */
  147. unsigned int alloc;
  148. unsigned int alloc_fail;
  149. unsigned int sleep_alloc;
  150. unsigned int sleep_alloc_fail;
  151. unsigned int refused_alloc;
  152. unsigned int refused_free;
  153. unsigned int free;
  154. /* monitor operations */
  155. unsigned int lock;
  156. unsigned int lock_fail;
  157. unsigned int unlock;
  158. unsigned int unlock_fail;
  159. unsigned int target;
  160. unsigned int target_fail;
  161. unsigned int start;
  162. unsigned int start_fail;
  163. unsigned int guest_type;
  164. unsigned int guest_type_fail;
  165. };
  166. #define STATS_INC(stat) (stat)++
  167. #else
  168. #define STATS_INC(stat)
  169. #endif
  170. struct vmballoon {
  171. /* list of reserved physical pages */
  172. struct list_head pages;
  173. /* transient list of non-balloonable pages */
  174. struct list_head refused_pages;
  175. unsigned int n_refused_pages;
  176. /* balloon size in pages */
  177. unsigned int size;
  178. unsigned int target;
  179. /* reset flag */
  180. bool reset_required;
  181. /* adjustment rates (pages per second) */
  182. unsigned int rate_alloc;
  183. unsigned int rate_free;
  184. /* slowdown page allocations for next few cycles */
  185. unsigned int slow_allocation_cycles;
  186. #ifdef CONFIG_DEBUG_FS
  187. /* statistics */
  188. struct vmballoon_stats stats;
  189. /* debugfs file exporting statistics */
  190. struct dentry *dbg_entry;
  191. #endif
  192. struct sysinfo sysinfo;
  193. struct delayed_work dwork;
  194. };
  195. static struct vmballoon balloon;
  196. /*
  197. * Send "start" command to the host, communicating supported version
  198. * of the protocol.
  199. */
  200. static bool vmballoon_send_start(struct vmballoon *b)
  201. {
  202. unsigned long status, capabilities;
  203. STATS_INC(b->stats.start);
  204. status = VMWARE_BALLOON_CMD(START, VMW_BALLOON_CAPABILITIES,
  205. capabilities);
  206. if (status == VMW_BALLOON_SUCCESS)
  207. return true;
  208. pr_debug("%s - failed, hv returns %ld\n", __func__, status);
  209. STATS_INC(b->stats.start_fail);
  210. return false;
  211. }
  212. static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
  213. {
  214. switch (status) {
  215. case VMW_BALLOON_SUCCESS:
  216. return true;
  217. case VMW_BALLOON_ERROR_RESET:
  218. b->reset_required = true;
  219. /* fall through */
  220. default:
  221. return false;
  222. }
  223. }
  224. /*
  225. * Communicate guest type to the host so that it can adjust ballooning
  226. * algorithm to the one most appropriate for the guest. This command
  227. * is normally issued after sending "start" command and is part of
  228. * standard reset sequence.
  229. */
  230. static bool vmballoon_send_guest_id(struct vmballoon *b)
  231. {
  232. unsigned long status, dummy;
  233. status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy);
  234. STATS_INC(b->stats.guest_type);
  235. if (vmballoon_check_status(b, status))
  236. return true;
  237. pr_debug("%s - failed, hv returns %ld\n", __func__, status);
  238. STATS_INC(b->stats.guest_type_fail);
  239. return false;
  240. }
  241. /*
  242. * Retrieve desired balloon size from the host.
  243. */
  244. static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
  245. {
  246. unsigned long status;
  247. unsigned long target;
  248. unsigned long limit;
  249. u32 limit32;
  250. /*
  251. * si_meminfo() is cheap. Moreover, we want to provide dynamic
  252. * max balloon size later. So let us call si_meminfo() every
  253. * iteration.
  254. */
  255. si_meminfo(&b->sysinfo);
  256. limit = b->sysinfo.totalram;
  257. /* Ensure limit fits in 32-bits */
  258. limit32 = (u32)limit;
  259. if (limit != limit32)
  260. return false;
  261. /* update stats */
  262. STATS_INC(b->stats.target);
  263. status = VMWARE_BALLOON_CMD(GET_TARGET, limit, target);
  264. if (vmballoon_check_status(b, status)) {
  265. *new_target = target;
  266. return true;
  267. }
  268. pr_debug("%s - failed, hv returns %ld\n", __func__, status);
  269. STATS_INC(b->stats.target_fail);
  270. return false;
  271. }
  272. /*
  273. * Notify the host about allocated page so that host can use it without
  274. * fear that guest will need it. Host may reject some pages, we need to
  275. * check the return value and maybe submit a different page.
  276. */
  277. static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
  278. unsigned int *hv_status)
  279. {
  280. unsigned long status, dummy;
  281. u32 pfn32;
  282. pfn32 = (u32)pfn;
  283. if (pfn32 != pfn)
  284. return -1;
  285. STATS_INC(b->stats.lock);
  286. *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy);
  287. if (vmballoon_check_status(b, status))
  288. return 0;
  289. pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
  290. STATS_INC(b->stats.lock_fail);
  291. return 1;
  292. }
  293. /*
  294. * Notify the host that guest intends to release given page back into
  295. * the pool of available (to the guest) pages.
  296. */
  297. static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn)
  298. {
  299. unsigned long status, dummy;
  300. u32 pfn32;
  301. pfn32 = (u32)pfn;
  302. if (pfn32 != pfn)
  303. return false;
  304. STATS_INC(b->stats.unlock);
  305. status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy);
  306. if (vmballoon_check_status(b, status))
  307. return true;
  308. pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
  309. STATS_INC(b->stats.unlock_fail);
  310. return false;
  311. }
  312. /*
  313. * Quickly release all pages allocated for the balloon. This function is
  314. * called when host decides to "reset" balloon for one reason or another.
  315. * Unlike normal "deflate" we do not (shall not) notify host of the pages
  316. * being released.
  317. */
  318. static void vmballoon_pop(struct vmballoon *b)
  319. {
  320. struct page *page, *next;
  321. unsigned int count = 0;
  322. list_for_each_entry_safe(page, next, &b->pages, lru) {
  323. list_del(&page->lru);
  324. __free_page(page);
  325. STATS_INC(b->stats.free);
  326. b->size--;
  327. if (++count >= b->rate_free) {
  328. count = 0;
  329. cond_resched();
  330. }
  331. }
  332. }
  333. /*
  334. * Perform standard reset sequence by popping the balloon (in case it
  335. * is not empty) and then restarting protocol. This operation normally
  336. * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
  337. */
  338. static void vmballoon_reset(struct vmballoon *b)
  339. {
  340. /* free all pages, skipping monitor unlock */
  341. vmballoon_pop(b);
  342. if (vmballoon_send_start(b)) {
  343. b->reset_required = false;
  344. if (!vmballoon_send_guest_id(b))
  345. pr_err("failed to send guest ID to the host\n");
  346. }
  347. }
  348. /*
  349. * Notify the host of a ballooned page. If host rejects the page put it on the
  350. * refuse list, those refused page are then released at the end of the
  351. * inflation cycle.
  352. */
  353. static int vmballoon_lock_page(struct vmballoon *b, struct page *page)
  354. {
  355. int locked, hv_status;
  356. locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status);
  357. if (locked > 0) {
  358. STATS_INC(b->stats.refused_alloc);
  359. if (hv_status == VMW_BALLOON_ERROR_RESET ||
  360. hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) {
  361. __free_page(page);
  362. return -EIO;
  363. }
  364. /*
  365. * Place page on the list of non-balloonable pages
  366. * and retry allocation, unless we already accumulated
  367. * too many of them, in which case take a breather.
  368. */
  369. if (b->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
  370. b->n_refused_pages++;
  371. list_add(&page->lru, &b->refused_pages);
  372. } else {
  373. __free_page(page);
  374. }
  375. return -EIO;
  376. }
  377. /* track allocated page */
  378. list_add(&page->lru, &b->pages);
  379. /* update balloon size */
  380. b->size++;
  381. return 0;
  382. }
  383. /*
  384. * Release the page allocated for the balloon. Note that we first notify
  385. * the host so it can make sure the page will be available for the guest
  386. * to use, if needed.
  387. */
  388. static int vmballoon_release_page(struct vmballoon *b, struct page *page)
  389. {
  390. if (!vmballoon_send_unlock_page(b, page_to_pfn(page)))
  391. return -EIO;
  392. list_del(&page->lru);
  393. /* deallocate page */
  394. __free_page(page);
  395. STATS_INC(b->stats.free);
  396. /* update balloon size */
  397. b->size--;
  398. return 0;
  399. }
  400. /*
  401. * Release pages that were allocated while attempting to inflate the
  402. * balloon but were refused by the host for one reason or another.
  403. */
  404. static void vmballoon_release_refused_pages(struct vmballoon *b)
  405. {
  406. struct page *page, *next;
  407. list_for_each_entry_safe(page, next, &b->refused_pages, lru) {
  408. list_del(&page->lru);
  409. __free_page(page);
  410. STATS_INC(b->stats.refused_free);
  411. }
  412. b->n_refused_pages = 0;
  413. }
  414. /*
  415. * Inflate the balloon towards its target size. Note that we try to limit
  416. * the rate of allocation to make sure we are not choking the rest of the
  417. * system.
  418. */
  419. static void vmballoon_inflate(struct vmballoon *b)
  420. {
  421. unsigned int goal;
  422. unsigned int rate;
  423. unsigned int i;
  424. unsigned int allocations = 0;
  425. int error = 0;
  426. gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
  427. pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
  428. /*
  429. * First try NOSLEEP page allocations to inflate balloon.
  430. *
  431. * If we do not throttle nosleep allocations, we can drain all
  432. * free pages in the guest quickly (if the balloon target is high).
  433. * As a side-effect, draining free pages helps to inform (force)
  434. * the guest to start swapping if balloon target is not met yet,
  435. * which is a desired behavior. However, balloon driver can consume
  436. * all available CPU cycles if too many pages are allocated in a
  437. * second. Therefore, we throttle nosleep allocations even when
  438. * the guest is not under memory pressure. OTOH, if we have already
  439. * predicted that the guest is under memory pressure, then we
  440. * slowdown page allocations considerably.
  441. */
  442. goal = b->target - b->size;
  443. /*
  444. * Start with no sleep allocation rate which may be higher
  445. * than sleeping allocation rate.
  446. */
  447. rate = b->slow_allocation_cycles ?
  448. b->rate_alloc : VMW_BALLOON_NOSLEEP_ALLOC_MAX;
  449. pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n",
  450. __func__, goal, rate, b->rate_alloc);
  451. for (i = 0; i < goal; i++) {
  452. struct page *page;
  453. if (flags == VMW_PAGE_ALLOC_NOSLEEP)
  454. STATS_INC(b->stats.alloc);
  455. else
  456. STATS_INC(b->stats.sleep_alloc);
  457. page = alloc_page(flags);
  458. if (!page) {
  459. if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
  460. /*
  461. * CANSLEEP page allocation failed, so guest
  462. * is under severe memory pressure. Quickly
  463. * decrease allocation rate.
  464. */
  465. b->rate_alloc = max(b->rate_alloc / 2,
  466. VMW_BALLOON_RATE_ALLOC_MIN);
  467. STATS_INC(b->stats.sleep_alloc_fail);
  468. break;
  469. }
  470. STATS_INC(b->stats.alloc_fail);
  471. /*
  472. * NOSLEEP page allocation failed, so the guest is
  473. * under memory pressure. Let us slow down page
  474. * allocations for next few cycles so that the guest
  475. * gets out of memory pressure. Also, if we already
  476. * allocated b->rate_alloc pages, let's pause,
  477. * otherwise switch to sleeping allocations.
  478. */
  479. b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
  480. if (i >= b->rate_alloc)
  481. break;
  482. flags = VMW_PAGE_ALLOC_CANSLEEP;
  483. /* Lower rate for sleeping allocations. */
  484. rate = b->rate_alloc;
  485. continue;
  486. }
  487. error = vmballoon_lock_page(b, page);
  488. if (error)
  489. break;
  490. if (++allocations > VMW_BALLOON_YIELD_THRESHOLD) {
  491. cond_resched();
  492. allocations = 0;
  493. }
  494. if (i >= rate) {
  495. /* We allocated enough pages, let's take a break. */
  496. break;
  497. }
  498. }
  499. /*
  500. * We reached our goal without failures so try increasing
  501. * allocation rate.
  502. */
  503. if (error == 0 && i >= b->rate_alloc) {
  504. unsigned int mult = i / b->rate_alloc;
  505. b->rate_alloc =
  506. min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
  507. VMW_BALLOON_RATE_ALLOC_MAX);
  508. }
  509. vmballoon_release_refused_pages(b);
  510. }
  511. /*
  512. * Decrease the size of the balloon allowing guest to use more memory.
  513. */
  514. static void vmballoon_deflate(struct vmballoon *b)
  515. {
  516. struct page *page, *next;
  517. unsigned int i = 0;
  518. unsigned int goal;
  519. int error;
  520. pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
  521. /* limit deallocation rate */
  522. goal = min(b->size - b->target, b->rate_free);
  523. pr_debug("%s - goal: %d, rate: %d\n", __func__, goal, b->rate_free);
  524. /* free pages to reach target */
  525. list_for_each_entry_safe(page, next, &b->pages, lru) {
  526. error = vmballoon_release_page(b, page);
  527. if (error) {
  528. /* quickly decrease rate in case of error */
  529. b->rate_free = max(b->rate_free / 2,
  530. VMW_BALLOON_RATE_FREE_MIN);
  531. return;
  532. }
  533. if (++i >= goal)
  534. break;
  535. }
  536. /* slowly increase rate if there were no errors */
  537. b->rate_free = min(b->rate_free + VMW_BALLOON_RATE_FREE_INC,
  538. VMW_BALLOON_RATE_FREE_MAX);
  539. }
  540. /*
  541. * Balloon work function: reset protocol, if needed, get the new size and
  542. * adjust balloon as needed. Repeat in 1 sec.
  543. */
  544. static void vmballoon_work(struct work_struct *work)
  545. {
  546. struct delayed_work *dwork = to_delayed_work(work);
  547. struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
  548. unsigned int target;
  549. STATS_INC(b->stats.timer);
  550. if (b->reset_required)
  551. vmballoon_reset(b);
  552. if (b->slow_allocation_cycles > 0)
  553. b->slow_allocation_cycles--;
  554. if (vmballoon_send_get_target(b, &target)) {
  555. /* update target, adjust size */
  556. b->target = target;
  557. if (b->size < target)
  558. vmballoon_inflate(b);
  559. else if (b->size > target)
  560. vmballoon_deflate(b);
  561. }
  562. /*
  563. * We are using a freezable workqueue so that balloon operations are
  564. * stopped while the system transitions to/from sleep/hibernation.
  565. */
  566. queue_delayed_work(system_freezable_wq,
  567. dwork, round_jiffies_relative(HZ));
  568. }
  569. /*
  570. * DEBUGFS Interface
  571. */
  572. #ifdef CONFIG_DEBUG_FS
  573. static int vmballoon_debug_show(struct seq_file *f, void *offset)
  574. {
  575. struct vmballoon *b = f->private;
  576. struct vmballoon_stats *stats = &b->stats;
  577. /* format size info */
  578. seq_printf(f,
  579. "target: %8d pages\n"
  580. "current: %8d pages\n",
  581. b->target, b->size);
  582. /* format rate info */
  583. seq_printf(f,
  584. "rateNoSleepAlloc: %8d pages/sec\n"
  585. "rateSleepAlloc: %8d pages/sec\n"
  586. "rateFree: %8d pages/sec\n",
  587. VMW_BALLOON_NOSLEEP_ALLOC_MAX,
  588. b->rate_alloc, b->rate_free);
  589. seq_printf(f,
  590. "\n"
  591. "timer: %8u\n"
  592. "start: %8u (%4u failed)\n"
  593. "guestType: %8u (%4u failed)\n"
  594. "lock: %8u (%4u failed)\n"
  595. "unlock: %8u (%4u failed)\n"
  596. "target: %8u (%4u failed)\n"
  597. "primNoSleepAlloc: %8u (%4u failed)\n"
  598. "primCanSleepAlloc: %8u (%4u failed)\n"
  599. "primFree: %8u\n"
  600. "errAlloc: %8u\n"
  601. "errFree: %8u\n",
  602. stats->timer,
  603. stats->start, stats->start_fail,
  604. stats->guest_type, stats->guest_type_fail,
  605. stats->lock, stats->lock_fail,
  606. stats->unlock, stats->unlock_fail,
  607. stats->target, stats->target_fail,
  608. stats->alloc, stats->alloc_fail,
  609. stats->sleep_alloc, stats->sleep_alloc_fail,
  610. stats->free,
  611. stats->refused_alloc, stats->refused_free);
  612. return 0;
  613. }
  614. static int vmballoon_debug_open(struct inode *inode, struct file *file)
  615. {
  616. return single_open(file, vmballoon_debug_show, inode->i_private);
  617. }
  618. static const struct file_operations vmballoon_debug_fops = {
  619. .owner = THIS_MODULE,
  620. .open = vmballoon_debug_open,
  621. .read = seq_read,
  622. .llseek = seq_lseek,
  623. .release = single_release,
  624. };
  625. static int __init vmballoon_debugfs_init(struct vmballoon *b)
  626. {
  627. int error;
  628. b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
  629. &vmballoon_debug_fops);
  630. if (IS_ERR(b->dbg_entry)) {
  631. error = PTR_ERR(b->dbg_entry);
  632. pr_err("failed to create debugfs entry, error: %d\n", error);
  633. return error;
  634. }
  635. return 0;
  636. }
  637. static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
  638. {
  639. debugfs_remove(b->dbg_entry);
  640. }
  641. #else
  642. static inline int vmballoon_debugfs_init(struct vmballoon *b)
  643. {
  644. return 0;
  645. }
  646. static inline void vmballoon_debugfs_exit(struct vmballoon *b)
  647. {
  648. }
  649. #endif /* CONFIG_DEBUG_FS */
  650. static int __init vmballoon_init(void)
  651. {
  652. int error;
  653. /*
  654. * Check if we are running on VMware's hypervisor and bail out
  655. * if we are not.
  656. */
  657. if (x86_hyper != &x86_hyper_vmware)
  658. return -ENODEV;
  659. INIT_LIST_HEAD(&balloon.pages);
  660. INIT_LIST_HEAD(&balloon.refused_pages);
  661. /* initialize rates */
  662. balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
  663. balloon.rate_free = VMW_BALLOON_RATE_FREE_MAX;
  664. INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
  665. /*
  666. * Start balloon.
  667. */
  668. if (!vmballoon_send_start(&balloon)) {
  669. pr_err("failed to send start command to the host\n");
  670. return -EIO;
  671. }
  672. if (!vmballoon_send_guest_id(&balloon)) {
  673. pr_err("failed to send guest ID to the host\n");
  674. return -EIO;
  675. }
  676. error = vmballoon_debugfs_init(&balloon);
  677. if (error)
  678. return error;
  679. queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
  680. return 0;
  681. }
  682. module_init(vmballoon_init);
  683. static void __exit vmballoon_exit(void)
  684. {
  685. cancel_delayed_work_sync(&balloon.dwork);
  686. vmballoon_debugfs_exit(&balloon);
  687. /*
  688. * Deallocate all reserved memory, and reset connection with monitor.
  689. * Reset connection before deallocating memory to avoid potential for
  690. * additional spurious resets from guest touching deallocated pages.
  691. */
  692. vmballoon_send_start(&balloon);
  693. vmballoon_pop(&balloon);
  694. }
  695. module_exit(vmballoon_exit);