vmware_balloon.c 21 KB

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