vmw_balloon.c 21 KB

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