vmw_balloon.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /*
  2. * VMware Balloon driver.
  3. *
  4. * Copyright (C) 2000-2014, 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/vmalloc.h>
  38. #include <linux/sched.h>
  39. #include <linux/module.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/debugfs.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/vmw_vmci_defs.h>
  44. #include <linux/vmw_vmci_api.h>
  45. #include <linux/io.h>
  46. #include <asm/hypervisor.h>
  47. MODULE_AUTHOR("VMware, Inc.");
  48. MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
  49. MODULE_VERSION("1.5.0.0-k");
  50. MODULE_ALIAS("dmi:*:svnVMware*:*");
  51. MODULE_ALIAS("vmware_vmmemctl");
  52. MODULE_LICENSE("GPL");
  53. /*
  54. * Various constants controlling rate of inflaint/deflating balloon,
  55. * measured in pages.
  56. */
  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. * When guest is under memory pressure, use a reduced page allocation
  66. * rate for next several cycles.
  67. */
  68. #define VMW_BALLOON_SLOW_CYCLES 4
  69. /*
  70. * Use __GFP_HIGHMEM to allow pages from HIGHMEM zone. We don't
  71. * allow wait (__GFP_RECLAIM) for NOSLEEP page allocations. Use
  72. * __GFP_NOWARN, to suppress page allocation failure warnings.
  73. */
  74. #define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
  75. /*
  76. * Use GFP_HIGHUSER when executing in a separate kernel thread
  77. * context and allocation can sleep. This is less stressful to
  78. * the guest memory system, since it allows the thread to block
  79. * while memory is reclaimed, and won't take pages from emergency
  80. * low-memory pools.
  81. */
  82. #define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
  83. /* Maximum number of refused pages we accumulate during inflation cycle */
  84. #define VMW_BALLOON_MAX_REFUSED 16
  85. /*
  86. * Hypervisor communication port definitions.
  87. */
  88. #define VMW_BALLOON_HV_PORT 0x5670
  89. #define VMW_BALLOON_HV_MAGIC 0x456c6d6f
  90. #define VMW_BALLOON_GUEST_ID 1 /* Linux */
  91. enum vmwballoon_capabilities {
  92. /*
  93. * Bit 0 is reserved and not associated to any capability.
  94. */
  95. VMW_BALLOON_BASIC_CMDS = (1 << 1),
  96. VMW_BALLOON_BATCHED_CMDS = (1 << 2),
  97. VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
  98. VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
  99. };
  100. #define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_BASIC_CMDS \
  101. | VMW_BALLOON_BATCHED_CMDS \
  102. | VMW_BALLOON_BATCHED_2M_CMDS \
  103. | VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
  104. #define VMW_BALLOON_2M_SHIFT (9)
  105. #define VMW_BALLOON_NUM_PAGE_SIZES (2)
  106. /*
  107. * Backdoor commands availability:
  108. *
  109. * START, GET_TARGET and GUEST_ID are always available,
  110. *
  111. * VMW_BALLOON_BASIC_CMDS:
  112. * LOCK and UNLOCK commands,
  113. * VMW_BALLOON_BATCHED_CMDS:
  114. * BATCHED_LOCK and BATCHED_UNLOCK commands.
  115. * VMW BALLOON_BATCHED_2M_CMDS:
  116. * BATCHED_2M_LOCK and BATCHED_2M_UNLOCK commands,
  117. * VMW VMW_BALLOON_SIGNALLED_WAKEUP_CMD:
  118. * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command.
  119. */
  120. #define VMW_BALLOON_CMD_START 0
  121. #define VMW_BALLOON_CMD_GET_TARGET 1
  122. #define VMW_BALLOON_CMD_LOCK 2
  123. #define VMW_BALLOON_CMD_UNLOCK 3
  124. #define VMW_BALLOON_CMD_GUEST_ID 4
  125. #define VMW_BALLOON_CMD_BATCHED_LOCK 6
  126. #define VMW_BALLOON_CMD_BATCHED_UNLOCK 7
  127. #define VMW_BALLOON_CMD_BATCHED_2M_LOCK 8
  128. #define VMW_BALLOON_CMD_BATCHED_2M_UNLOCK 9
  129. #define VMW_BALLOON_CMD_VMCI_DOORBELL_SET 10
  130. /* error codes */
  131. #define VMW_BALLOON_SUCCESS 0
  132. #define VMW_BALLOON_FAILURE -1
  133. #define VMW_BALLOON_ERROR_CMD_INVALID 1
  134. #define VMW_BALLOON_ERROR_PPN_INVALID 2
  135. #define VMW_BALLOON_ERROR_PPN_LOCKED 3
  136. #define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
  137. #define VMW_BALLOON_ERROR_PPN_PINNED 5
  138. #define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
  139. #define VMW_BALLOON_ERROR_RESET 7
  140. #define VMW_BALLOON_ERROR_BUSY 8
  141. #define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
  142. /* Batch page description */
  143. /*
  144. * Layout of a page in the batch page:
  145. *
  146. * +-------------+----------+--------+
  147. * | | | |
  148. * | Page number | Reserved | Status |
  149. * | | | |
  150. * +-------------+----------+--------+
  151. * 64 PAGE_SHIFT 6 0
  152. *
  153. * The reserved field should be set to 0.
  154. */
  155. #define VMW_BALLOON_BATCH_MAX_PAGES (PAGE_SIZE / sizeof(u64))
  156. #define VMW_BALLOON_BATCH_STATUS_MASK ((1UL << 5) - 1)
  157. #define VMW_BALLOON_BATCH_PAGE_MASK (~((1UL << PAGE_SHIFT) - 1))
  158. struct vmballoon_batch_page {
  159. u64 pages[VMW_BALLOON_BATCH_MAX_PAGES];
  160. };
  161. static u64 vmballoon_batch_get_pa(struct vmballoon_batch_page *batch, int idx)
  162. {
  163. return batch->pages[idx] & VMW_BALLOON_BATCH_PAGE_MASK;
  164. }
  165. static int vmballoon_batch_get_status(struct vmballoon_batch_page *batch,
  166. int idx)
  167. {
  168. return (int)(batch->pages[idx] & VMW_BALLOON_BATCH_STATUS_MASK);
  169. }
  170. static void vmballoon_batch_set_pa(struct vmballoon_batch_page *batch, int idx,
  171. u64 pa)
  172. {
  173. batch->pages[idx] = pa;
  174. }
  175. #define VMWARE_BALLOON_CMD(cmd, arg1, arg2, result) \
  176. ({ \
  177. unsigned long __status, __dummy1, __dummy2, __dummy3; \
  178. __asm__ __volatile__ ("inl %%dx" : \
  179. "=a"(__status), \
  180. "=c"(__dummy1), \
  181. "=d"(__dummy2), \
  182. "=b"(result), \
  183. "=S" (__dummy3) : \
  184. "0"(VMW_BALLOON_HV_MAGIC), \
  185. "1"(VMW_BALLOON_CMD_##cmd), \
  186. "2"(VMW_BALLOON_HV_PORT), \
  187. "3"(arg1), \
  188. "4" (arg2) : \
  189. "memory"); \
  190. if (VMW_BALLOON_CMD_##cmd == VMW_BALLOON_CMD_START) \
  191. result = __dummy1; \
  192. result &= -1UL; \
  193. __status & -1UL; \
  194. })
  195. #ifdef CONFIG_DEBUG_FS
  196. struct vmballoon_stats {
  197. unsigned int timer;
  198. unsigned int doorbell;
  199. /* allocation statistics */
  200. unsigned int alloc[VMW_BALLOON_NUM_PAGE_SIZES];
  201. unsigned int alloc_fail[VMW_BALLOON_NUM_PAGE_SIZES];
  202. unsigned int sleep_alloc;
  203. unsigned int sleep_alloc_fail;
  204. unsigned int refused_alloc[VMW_BALLOON_NUM_PAGE_SIZES];
  205. unsigned int refused_free[VMW_BALLOON_NUM_PAGE_SIZES];
  206. unsigned int free[VMW_BALLOON_NUM_PAGE_SIZES];
  207. /* monitor operations */
  208. unsigned int lock[VMW_BALLOON_NUM_PAGE_SIZES];
  209. unsigned int lock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
  210. unsigned int unlock[VMW_BALLOON_NUM_PAGE_SIZES];
  211. unsigned int unlock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
  212. unsigned int target;
  213. unsigned int target_fail;
  214. unsigned int start;
  215. unsigned int start_fail;
  216. unsigned int guest_type;
  217. unsigned int guest_type_fail;
  218. unsigned int doorbell_set;
  219. unsigned int doorbell_unset;
  220. };
  221. #define STATS_INC(stat) (stat)++
  222. #else
  223. #define STATS_INC(stat)
  224. #endif
  225. struct vmballoon;
  226. struct vmballoon_ops {
  227. void (*add_page)(struct vmballoon *b, int idx, struct page *p);
  228. int (*lock)(struct vmballoon *b, unsigned int num_pages,
  229. bool is_2m_pages, unsigned int *target);
  230. int (*unlock)(struct vmballoon *b, unsigned int num_pages,
  231. bool is_2m_pages, unsigned int *target);
  232. };
  233. struct vmballoon_page_size {
  234. /* list of reserved physical pages */
  235. struct list_head pages;
  236. /* transient list of non-balloonable pages */
  237. struct list_head refused_pages;
  238. unsigned int n_refused_pages;
  239. };
  240. struct vmballoon {
  241. struct vmballoon_page_size page_sizes[VMW_BALLOON_NUM_PAGE_SIZES];
  242. /* supported page sizes. 1 == 4k pages only, 2 == 4k and 2m pages */
  243. unsigned supported_page_sizes;
  244. /* balloon size in pages */
  245. unsigned int size;
  246. unsigned int target;
  247. /* reset flag */
  248. bool reset_required;
  249. /* adjustment rates (pages per second) */
  250. unsigned int rate_alloc;
  251. /* slowdown page allocations for next few cycles */
  252. unsigned int slow_allocation_cycles;
  253. unsigned long capabilities;
  254. struct vmballoon_batch_page *batch_page;
  255. unsigned int batch_max_pages;
  256. struct page *page;
  257. const struct vmballoon_ops *ops;
  258. #ifdef CONFIG_DEBUG_FS
  259. /* statistics */
  260. struct vmballoon_stats stats;
  261. /* debugfs file exporting statistics */
  262. struct dentry *dbg_entry;
  263. #endif
  264. struct sysinfo sysinfo;
  265. struct delayed_work dwork;
  266. struct vmci_handle vmci_doorbell;
  267. };
  268. static struct vmballoon balloon;
  269. /*
  270. * Send "start" command to the host, communicating supported version
  271. * of the protocol.
  272. */
  273. static bool vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
  274. {
  275. unsigned long status, capabilities, dummy = 0;
  276. bool success;
  277. STATS_INC(b->stats.start);
  278. status = VMWARE_BALLOON_CMD(START, req_caps, dummy, capabilities);
  279. switch (status) {
  280. case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
  281. b->capabilities = capabilities;
  282. success = true;
  283. break;
  284. case VMW_BALLOON_SUCCESS:
  285. b->capabilities = VMW_BALLOON_BASIC_CMDS;
  286. success = true;
  287. break;
  288. default:
  289. success = false;
  290. }
  291. /*
  292. * 2MB pages are only supported with batching. If batching is for some
  293. * reason disabled, do not use 2MB pages, since otherwise the legacy
  294. * mechanism is used with 2MB pages, causing a failure.
  295. */
  296. if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
  297. (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
  298. b->supported_page_sizes = 2;
  299. else
  300. b->supported_page_sizes = 1;
  301. if (!success) {
  302. pr_debug("%s - failed, hv returns %ld\n", __func__, status);
  303. STATS_INC(b->stats.start_fail);
  304. }
  305. return success;
  306. }
  307. static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
  308. {
  309. switch (status) {
  310. case VMW_BALLOON_SUCCESS:
  311. return true;
  312. case VMW_BALLOON_ERROR_RESET:
  313. b->reset_required = true;
  314. /* fall through */
  315. default:
  316. return false;
  317. }
  318. }
  319. /*
  320. * Communicate guest type to the host so that it can adjust ballooning
  321. * algorithm to the one most appropriate for the guest. This command
  322. * is normally issued after sending "start" command and is part of
  323. * standard reset sequence.
  324. */
  325. static bool vmballoon_send_guest_id(struct vmballoon *b)
  326. {
  327. unsigned long status, dummy = 0;
  328. status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy,
  329. dummy);
  330. STATS_INC(b->stats.guest_type);
  331. if (vmballoon_check_status(b, status))
  332. return true;
  333. pr_debug("%s - failed, hv returns %ld\n", __func__, status);
  334. STATS_INC(b->stats.guest_type_fail);
  335. return false;
  336. }
  337. static u16 vmballoon_page_size(bool is_2m_page)
  338. {
  339. if (is_2m_page)
  340. return 1 << VMW_BALLOON_2M_SHIFT;
  341. return 1;
  342. }
  343. /*
  344. * Retrieve desired balloon size from the host.
  345. */
  346. static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
  347. {
  348. unsigned long status;
  349. unsigned long target;
  350. unsigned long limit;
  351. unsigned long dummy = 0;
  352. u32 limit32;
  353. /*
  354. * si_meminfo() is cheap. Moreover, we want to provide dynamic
  355. * max balloon size later. So let us call si_meminfo() every
  356. * iteration.
  357. */
  358. si_meminfo(&b->sysinfo);
  359. limit = b->sysinfo.totalram;
  360. /* Ensure limit fits in 32-bits */
  361. limit32 = (u32)limit;
  362. if (limit != limit32)
  363. return false;
  364. /* update stats */
  365. STATS_INC(b->stats.target);
  366. status = VMWARE_BALLOON_CMD(GET_TARGET, limit, dummy, target);
  367. if (vmballoon_check_status(b, status)) {
  368. *new_target = target;
  369. return true;
  370. }
  371. pr_debug("%s - failed, hv returns %ld\n", __func__, status);
  372. STATS_INC(b->stats.target_fail);
  373. return false;
  374. }
  375. /*
  376. * Notify the host about allocated page so that host can use it without
  377. * fear that guest will need it. Host may reject some pages, we need to
  378. * check the return value and maybe submit a different page.
  379. */
  380. static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
  381. unsigned int *hv_status, unsigned int *target)
  382. {
  383. unsigned long status, dummy = 0;
  384. u32 pfn32;
  385. pfn32 = (u32)pfn;
  386. if (pfn32 != pfn)
  387. return -EINVAL;
  388. STATS_INC(b->stats.lock[false]);
  389. *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy, *target);
  390. if (vmballoon_check_status(b, status))
  391. return 0;
  392. pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
  393. STATS_INC(b->stats.lock_fail[false]);
  394. return -EIO;
  395. }
  396. static int vmballoon_send_batched_lock(struct vmballoon *b,
  397. unsigned int num_pages, bool is_2m_pages, unsigned int *target)
  398. {
  399. unsigned long status;
  400. unsigned long pfn = PHYS_PFN(virt_to_phys(b->batch_page));
  401. STATS_INC(b->stats.lock[is_2m_pages]);
  402. if (is_2m_pages)
  403. status = VMWARE_BALLOON_CMD(BATCHED_2M_LOCK, pfn, num_pages,
  404. *target);
  405. else
  406. status = VMWARE_BALLOON_CMD(BATCHED_LOCK, pfn, num_pages,
  407. *target);
  408. if (vmballoon_check_status(b, status))
  409. return 0;
  410. pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
  411. STATS_INC(b->stats.lock_fail[is_2m_pages]);
  412. return 1;
  413. }
  414. /*
  415. * Notify the host that guest intends to release given page back into
  416. * the pool of available (to the guest) pages.
  417. */
  418. static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn,
  419. unsigned int *target)
  420. {
  421. unsigned long status, dummy = 0;
  422. u32 pfn32;
  423. pfn32 = (u32)pfn;
  424. if (pfn32 != pfn)
  425. return false;
  426. STATS_INC(b->stats.unlock[false]);
  427. status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy, *target);
  428. if (vmballoon_check_status(b, status))
  429. return true;
  430. pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
  431. STATS_INC(b->stats.unlock_fail[false]);
  432. return false;
  433. }
  434. static bool vmballoon_send_batched_unlock(struct vmballoon *b,
  435. unsigned int num_pages, bool is_2m_pages, unsigned int *target)
  436. {
  437. unsigned long status;
  438. unsigned long pfn = PHYS_PFN(virt_to_phys(b->batch_page));
  439. STATS_INC(b->stats.unlock[is_2m_pages]);
  440. if (is_2m_pages)
  441. status = VMWARE_BALLOON_CMD(BATCHED_2M_UNLOCK, pfn, num_pages,
  442. *target);
  443. else
  444. status = VMWARE_BALLOON_CMD(BATCHED_UNLOCK, pfn, num_pages,
  445. *target);
  446. if (vmballoon_check_status(b, status))
  447. return true;
  448. pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
  449. STATS_INC(b->stats.unlock_fail[is_2m_pages]);
  450. return false;
  451. }
  452. static struct page *vmballoon_alloc_page(gfp_t flags, bool is_2m_page)
  453. {
  454. if (is_2m_page)
  455. return alloc_pages(flags, VMW_BALLOON_2M_SHIFT);
  456. return alloc_page(flags);
  457. }
  458. static void vmballoon_free_page(struct page *page, bool is_2m_page)
  459. {
  460. if (is_2m_page)
  461. __free_pages(page, VMW_BALLOON_2M_SHIFT);
  462. else
  463. __free_page(page);
  464. }
  465. /*
  466. * Quickly release all pages allocated for the balloon. This function is
  467. * called when host decides to "reset" balloon for one reason or another.
  468. * Unlike normal "deflate" we do not (shall not) notify host of the pages
  469. * being released.
  470. */
  471. static void vmballoon_pop(struct vmballoon *b)
  472. {
  473. struct page *page, *next;
  474. unsigned is_2m_pages;
  475. for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
  476. is_2m_pages++) {
  477. struct vmballoon_page_size *page_size =
  478. &b->page_sizes[is_2m_pages];
  479. u16 size_per_page = vmballoon_page_size(is_2m_pages);
  480. list_for_each_entry_safe(page, next, &page_size->pages, lru) {
  481. list_del(&page->lru);
  482. vmballoon_free_page(page, is_2m_pages);
  483. STATS_INC(b->stats.free[is_2m_pages]);
  484. b->size -= size_per_page;
  485. cond_resched();
  486. }
  487. }
  488. /* Clearing the batch_page unconditionally has no adverse effect */
  489. free_page((unsigned long)b->batch_page);
  490. b->batch_page = NULL;
  491. }
  492. /*
  493. * Notify the host of a ballooned page. If host rejects the page put it on the
  494. * refuse list, those refused page are then released at the end of the
  495. * inflation cycle.
  496. */
  497. static int vmballoon_lock_page(struct vmballoon *b, unsigned int num_pages,
  498. bool is_2m_pages, unsigned int *target)
  499. {
  500. int locked, hv_status;
  501. struct page *page = b->page;
  502. struct vmballoon_page_size *page_size = &b->page_sizes[false];
  503. /* is_2m_pages can never happen as 2m pages support implies batching */
  504. locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status,
  505. target);
  506. if (locked) {
  507. STATS_INC(b->stats.refused_alloc[false]);
  508. if (locked == -EIO &&
  509. (hv_status == VMW_BALLOON_ERROR_RESET ||
  510. hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED)) {
  511. vmballoon_free_page(page, false);
  512. return -EIO;
  513. }
  514. /*
  515. * Place page on the list of non-balloonable pages
  516. * and retry allocation, unless we already accumulated
  517. * too many of them, in which case take a breather.
  518. */
  519. if (page_size->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
  520. page_size->n_refused_pages++;
  521. list_add(&page->lru, &page_size->refused_pages);
  522. } else {
  523. vmballoon_free_page(page, false);
  524. }
  525. return locked;
  526. }
  527. /* track allocated page */
  528. list_add(&page->lru, &page_size->pages);
  529. /* update balloon size */
  530. b->size++;
  531. return 0;
  532. }
  533. static int vmballoon_lock_batched_page(struct vmballoon *b,
  534. unsigned int num_pages, bool is_2m_pages, unsigned int *target)
  535. {
  536. int locked, i;
  537. u16 size_per_page = vmballoon_page_size(is_2m_pages);
  538. locked = vmballoon_send_batched_lock(b, num_pages, is_2m_pages,
  539. target);
  540. if (locked > 0) {
  541. for (i = 0; i < num_pages; i++) {
  542. u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
  543. struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
  544. vmballoon_free_page(p, is_2m_pages);
  545. }
  546. return -EIO;
  547. }
  548. for (i = 0; i < num_pages; i++) {
  549. u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
  550. struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
  551. struct vmballoon_page_size *page_size =
  552. &b->page_sizes[is_2m_pages];
  553. locked = vmballoon_batch_get_status(b->batch_page, i);
  554. switch (locked) {
  555. case VMW_BALLOON_SUCCESS:
  556. list_add(&p->lru, &page_size->pages);
  557. b->size += size_per_page;
  558. break;
  559. case VMW_BALLOON_ERROR_PPN_PINNED:
  560. case VMW_BALLOON_ERROR_PPN_INVALID:
  561. if (page_size->n_refused_pages
  562. < VMW_BALLOON_MAX_REFUSED) {
  563. list_add(&p->lru, &page_size->refused_pages);
  564. page_size->n_refused_pages++;
  565. break;
  566. }
  567. /* Fallthrough */
  568. case VMW_BALLOON_ERROR_RESET:
  569. case VMW_BALLOON_ERROR_PPN_NOTNEEDED:
  570. vmballoon_free_page(p, is_2m_pages);
  571. break;
  572. default:
  573. /* This should never happen */
  574. WARN_ON_ONCE(true);
  575. }
  576. }
  577. return 0;
  578. }
  579. /*
  580. * Release the page allocated for the balloon. Note that we first notify
  581. * the host so it can make sure the page will be available for the guest
  582. * to use, if needed.
  583. */
  584. static int vmballoon_unlock_page(struct vmballoon *b, unsigned int num_pages,
  585. bool is_2m_pages, unsigned int *target)
  586. {
  587. struct page *page = b->page;
  588. struct vmballoon_page_size *page_size = &b->page_sizes[false];
  589. /* is_2m_pages can never happen as 2m pages support implies batching */
  590. if (!vmballoon_send_unlock_page(b, page_to_pfn(page), target)) {
  591. list_add(&page->lru, &page_size->pages);
  592. return -EIO;
  593. }
  594. /* deallocate page */
  595. vmballoon_free_page(page, false);
  596. STATS_INC(b->stats.free[false]);
  597. /* update balloon size */
  598. b->size--;
  599. return 0;
  600. }
  601. static int vmballoon_unlock_batched_page(struct vmballoon *b,
  602. unsigned int num_pages, bool is_2m_pages,
  603. unsigned int *target)
  604. {
  605. int locked, i, ret = 0;
  606. bool hv_success;
  607. u16 size_per_page = vmballoon_page_size(is_2m_pages);
  608. hv_success = vmballoon_send_batched_unlock(b, num_pages, is_2m_pages,
  609. target);
  610. if (!hv_success)
  611. ret = -EIO;
  612. for (i = 0; i < num_pages; i++) {
  613. u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
  614. struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
  615. struct vmballoon_page_size *page_size =
  616. &b->page_sizes[is_2m_pages];
  617. locked = vmballoon_batch_get_status(b->batch_page, i);
  618. if (!hv_success || locked != VMW_BALLOON_SUCCESS) {
  619. /*
  620. * That page wasn't successfully unlocked by the
  621. * hypervisor, re-add it to the list of pages owned by
  622. * the balloon driver.
  623. */
  624. list_add(&p->lru, &page_size->pages);
  625. } else {
  626. /* deallocate page */
  627. vmballoon_free_page(p, is_2m_pages);
  628. STATS_INC(b->stats.free[is_2m_pages]);
  629. /* update balloon size */
  630. b->size -= size_per_page;
  631. }
  632. }
  633. return ret;
  634. }
  635. /*
  636. * Release pages that were allocated while attempting to inflate the
  637. * balloon but were refused by the host for one reason or another.
  638. */
  639. static void vmballoon_release_refused_pages(struct vmballoon *b,
  640. bool is_2m_pages)
  641. {
  642. struct page *page, *next;
  643. struct vmballoon_page_size *page_size =
  644. &b->page_sizes[is_2m_pages];
  645. list_for_each_entry_safe(page, next, &page_size->refused_pages, lru) {
  646. list_del(&page->lru);
  647. vmballoon_free_page(page, is_2m_pages);
  648. STATS_INC(b->stats.refused_free[is_2m_pages]);
  649. }
  650. page_size->n_refused_pages = 0;
  651. }
  652. static void vmballoon_add_page(struct vmballoon *b, int idx, struct page *p)
  653. {
  654. b->page = p;
  655. }
  656. static void vmballoon_add_batched_page(struct vmballoon *b, int idx,
  657. struct page *p)
  658. {
  659. vmballoon_batch_set_pa(b->batch_page, idx,
  660. (u64)page_to_pfn(p) << PAGE_SHIFT);
  661. }
  662. /*
  663. * Inflate the balloon towards its target size. Note that we try to limit
  664. * the rate of allocation to make sure we are not choking the rest of the
  665. * system.
  666. */
  667. static void vmballoon_inflate(struct vmballoon *b)
  668. {
  669. unsigned rate;
  670. unsigned int allocations = 0;
  671. unsigned int num_pages = 0;
  672. int error = 0;
  673. gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
  674. bool is_2m_pages;
  675. pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
  676. /*
  677. * First try NOSLEEP page allocations to inflate balloon.
  678. *
  679. * If we do not throttle nosleep allocations, we can drain all
  680. * free pages in the guest quickly (if the balloon target is high).
  681. * As a side-effect, draining free pages helps to inform (force)
  682. * the guest to start swapping if balloon target is not met yet,
  683. * which is a desired behavior. However, balloon driver can consume
  684. * all available CPU cycles if too many pages are allocated in a
  685. * second. Therefore, we throttle nosleep allocations even when
  686. * the guest is not under memory pressure. OTOH, if we have already
  687. * predicted that the guest is under memory pressure, then we
  688. * slowdown page allocations considerably.
  689. */
  690. /*
  691. * Start with no sleep allocation rate which may be higher
  692. * than sleeping allocation rate.
  693. */
  694. if (b->slow_allocation_cycles) {
  695. rate = b->rate_alloc;
  696. is_2m_pages = false;
  697. } else {
  698. rate = UINT_MAX;
  699. is_2m_pages =
  700. b->supported_page_sizes == VMW_BALLOON_NUM_PAGE_SIZES;
  701. }
  702. pr_debug("%s - goal: %d, no-sleep rate: %u, sleep rate: %d\n",
  703. __func__, b->target - b->size, rate, b->rate_alloc);
  704. while (!b->reset_required &&
  705. b->size + num_pages * vmballoon_page_size(is_2m_pages)
  706. < b->target) {
  707. struct page *page;
  708. if (flags == VMW_PAGE_ALLOC_NOSLEEP)
  709. STATS_INC(b->stats.alloc[is_2m_pages]);
  710. else
  711. STATS_INC(b->stats.sleep_alloc);
  712. page = vmballoon_alloc_page(flags, is_2m_pages);
  713. if (!page) {
  714. STATS_INC(b->stats.alloc_fail[is_2m_pages]);
  715. if (is_2m_pages) {
  716. b->ops->lock(b, num_pages, true, &b->target);
  717. /*
  718. * ignore errors from locking as we now switch
  719. * to 4k pages and we might get different
  720. * errors.
  721. */
  722. num_pages = 0;
  723. is_2m_pages = false;
  724. continue;
  725. }
  726. if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
  727. /*
  728. * CANSLEEP page allocation failed, so guest
  729. * is under severe memory pressure. Quickly
  730. * decrease allocation rate.
  731. */
  732. b->rate_alloc = max(b->rate_alloc / 2,
  733. VMW_BALLOON_RATE_ALLOC_MIN);
  734. STATS_INC(b->stats.sleep_alloc_fail);
  735. break;
  736. }
  737. /*
  738. * NOSLEEP page allocation failed, so the guest is
  739. * under memory pressure. Let us slow down page
  740. * allocations for next few cycles so that the guest
  741. * gets out of memory pressure. Also, if we already
  742. * allocated b->rate_alloc pages, let's pause,
  743. * otherwise switch to sleeping allocations.
  744. */
  745. b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
  746. if (allocations >= b->rate_alloc)
  747. break;
  748. flags = VMW_PAGE_ALLOC_CANSLEEP;
  749. /* Lower rate for sleeping allocations. */
  750. rate = b->rate_alloc;
  751. continue;
  752. }
  753. b->ops->add_page(b, num_pages++, page);
  754. if (num_pages == b->batch_max_pages) {
  755. error = b->ops->lock(b, num_pages, is_2m_pages,
  756. &b->target);
  757. num_pages = 0;
  758. if (error)
  759. break;
  760. }
  761. cond_resched();
  762. if (allocations >= rate) {
  763. /* We allocated enough pages, let's take a break. */
  764. break;
  765. }
  766. }
  767. if (num_pages > 0)
  768. b->ops->lock(b, num_pages, is_2m_pages, &b->target);
  769. /*
  770. * We reached our goal without failures so try increasing
  771. * allocation rate.
  772. */
  773. if (error == 0 && allocations >= b->rate_alloc) {
  774. unsigned int mult = allocations / b->rate_alloc;
  775. b->rate_alloc =
  776. min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
  777. VMW_BALLOON_RATE_ALLOC_MAX);
  778. }
  779. vmballoon_release_refused_pages(b, true);
  780. vmballoon_release_refused_pages(b, false);
  781. }
  782. /*
  783. * Decrease the size of the balloon allowing guest to use more memory.
  784. */
  785. static void vmballoon_deflate(struct vmballoon *b)
  786. {
  787. unsigned is_2m_pages;
  788. pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
  789. /* free pages to reach target */
  790. for (is_2m_pages = 0; is_2m_pages < b->supported_page_sizes;
  791. is_2m_pages++) {
  792. struct page *page, *next;
  793. unsigned int num_pages = 0;
  794. struct vmballoon_page_size *page_size =
  795. &b->page_sizes[is_2m_pages];
  796. list_for_each_entry_safe(page, next, &page_size->pages, lru) {
  797. if (b->reset_required ||
  798. (b->target > 0 &&
  799. b->size - num_pages
  800. * vmballoon_page_size(is_2m_pages)
  801. < b->target + vmballoon_page_size(true)))
  802. break;
  803. list_del(&page->lru);
  804. b->ops->add_page(b, num_pages++, page);
  805. if (num_pages == b->batch_max_pages) {
  806. int error;
  807. error = b->ops->unlock(b, num_pages,
  808. is_2m_pages, &b->target);
  809. num_pages = 0;
  810. if (error)
  811. return;
  812. }
  813. cond_resched();
  814. }
  815. if (num_pages > 0)
  816. b->ops->unlock(b, num_pages, is_2m_pages, &b->target);
  817. }
  818. }
  819. static const struct vmballoon_ops vmballoon_basic_ops = {
  820. .add_page = vmballoon_add_page,
  821. .lock = vmballoon_lock_page,
  822. .unlock = vmballoon_unlock_page
  823. };
  824. static const struct vmballoon_ops vmballoon_batched_ops = {
  825. .add_page = vmballoon_add_batched_page,
  826. .lock = vmballoon_lock_batched_page,
  827. .unlock = vmballoon_unlock_batched_page
  828. };
  829. static bool vmballoon_init_batching(struct vmballoon *b)
  830. {
  831. struct page *page;
  832. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  833. if (!page)
  834. return false;
  835. b->batch_page = page_address(page);
  836. return true;
  837. }
  838. /*
  839. * Receive notification and resize balloon
  840. */
  841. static void vmballoon_doorbell(void *client_data)
  842. {
  843. struct vmballoon *b = client_data;
  844. STATS_INC(b->stats.doorbell);
  845. mod_delayed_work(system_freezable_wq, &b->dwork, 0);
  846. }
  847. /*
  848. * Clean up vmci doorbell
  849. */
  850. static void vmballoon_vmci_cleanup(struct vmballoon *b)
  851. {
  852. int error;
  853. VMWARE_BALLOON_CMD(VMCI_DOORBELL_SET, VMCI_INVALID_ID,
  854. VMCI_INVALID_ID, error);
  855. STATS_INC(b->stats.doorbell_unset);
  856. if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
  857. vmci_doorbell_destroy(b->vmci_doorbell);
  858. b->vmci_doorbell = VMCI_INVALID_HANDLE;
  859. }
  860. }
  861. /*
  862. * Initialize vmci doorbell, to get notified as soon as balloon changes
  863. */
  864. static int vmballoon_vmci_init(struct vmballoon *b)
  865. {
  866. unsigned long error, dummy;
  867. if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
  868. return 0;
  869. error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
  870. VMCI_PRIVILEGE_FLAG_RESTRICTED,
  871. vmballoon_doorbell, b);
  872. if (error != VMCI_SUCCESS)
  873. goto fail;
  874. error = VMWARE_BALLOON_CMD(VMCI_DOORBELL_SET, b->vmci_doorbell.context,
  875. b->vmci_doorbell.resource, dummy);
  876. STATS_INC(b->stats.doorbell_set);
  877. if (error != VMW_BALLOON_SUCCESS)
  878. goto fail;
  879. return 0;
  880. fail:
  881. vmballoon_vmci_cleanup(b);
  882. return -EIO;
  883. }
  884. /*
  885. * Perform standard reset sequence by popping the balloon (in case it
  886. * is not empty) and then restarting protocol. This operation normally
  887. * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
  888. */
  889. static void vmballoon_reset(struct vmballoon *b)
  890. {
  891. int error;
  892. vmballoon_vmci_cleanup(b);
  893. /* free all pages, skipping monitor unlock */
  894. vmballoon_pop(b);
  895. if (!vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
  896. return;
  897. if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
  898. b->ops = &vmballoon_batched_ops;
  899. b->batch_max_pages = VMW_BALLOON_BATCH_MAX_PAGES;
  900. if (!vmballoon_init_batching(b)) {
  901. /*
  902. * We failed to initialize batching, inform the monitor
  903. * about it by sending a null capability.
  904. *
  905. * The guest will retry in one second.
  906. */
  907. vmballoon_send_start(b, 0);
  908. return;
  909. }
  910. } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
  911. b->ops = &vmballoon_basic_ops;
  912. b->batch_max_pages = 1;
  913. }
  914. b->reset_required = false;
  915. error = vmballoon_vmci_init(b);
  916. if (error)
  917. pr_err("failed to initialize vmci doorbell\n");
  918. if (!vmballoon_send_guest_id(b))
  919. pr_err("failed to send guest ID to the host\n");
  920. }
  921. /*
  922. * Balloon work function: reset protocol, if needed, get the new size and
  923. * adjust balloon as needed. Repeat in 1 sec.
  924. */
  925. static void vmballoon_work(struct work_struct *work)
  926. {
  927. struct delayed_work *dwork = to_delayed_work(work);
  928. struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
  929. unsigned int target;
  930. STATS_INC(b->stats.timer);
  931. if (b->reset_required)
  932. vmballoon_reset(b);
  933. if (b->slow_allocation_cycles > 0)
  934. b->slow_allocation_cycles--;
  935. if (!b->reset_required && vmballoon_send_get_target(b, &target)) {
  936. /* update target, adjust size */
  937. b->target = target;
  938. if (b->size < target)
  939. vmballoon_inflate(b);
  940. else if (target == 0 ||
  941. b->size > target + vmballoon_page_size(true))
  942. vmballoon_deflate(b);
  943. }
  944. /*
  945. * We are using a freezable workqueue so that balloon operations are
  946. * stopped while the system transitions to/from sleep/hibernation.
  947. */
  948. queue_delayed_work(system_freezable_wq,
  949. dwork, round_jiffies_relative(HZ));
  950. }
  951. /*
  952. * DEBUGFS Interface
  953. */
  954. #ifdef CONFIG_DEBUG_FS
  955. static int vmballoon_debug_show(struct seq_file *f, void *offset)
  956. {
  957. struct vmballoon *b = f->private;
  958. struct vmballoon_stats *stats = &b->stats;
  959. /* format capabilities info */
  960. seq_printf(f,
  961. "balloon capabilities: %#4x\n"
  962. "used capabilities: %#4lx\n"
  963. "is resetting: %c\n",
  964. VMW_BALLOON_CAPABILITIES, b->capabilities,
  965. b->reset_required ? 'y' : 'n');
  966. /* format size info */
  967. seq_printf(f,
  968. "target: %8d pages\n"
  969. "current: %8d pages\n",
  970. b->target, b->size);
  971. /* format rate info */
  972. seq_printf(f,
  973. "rateSleepAlloc: %8d pages/sec\n",
  974. b->rate_alloc);
  975. seq_printf(f,
  976. "\n"
  977. "timer: %8u\n"
  978. "doorbell: %8u\n"
  979. "start: %8u (%4u failed)\n"
  980. "guestType: %8u (%4u failed)\n"
  981. "2m-lock: %8u (%4u failed)\n"
  982. "lock: %8u (%4u failed)\n"
  983. "2m-unlock: %8u (%4u failed)\n"
  984. "unlock: %8u (%4u failed)\n"
  985. "target: %8u (%4u failed)\n"
  986. "prim2mAlloc: %8u (%4u failed)\n"
  987. "primNoSleepAlloc: %8u (%4u failed)\n"
  988. "primCanSleepAlloc: %8u (%4u failed)\n"
  989. "prim2mFree: %8u\n"
  990. "primFree: %8u\n"
  991. "err2mAlloc: %8u\n"
  992. "errAlloc: %8u\n"
  993. "err2mFree: %8u\n"
  994. "errFree: %8u\n"
  995. "doorbellSet: %8u\n"
  996. "doorbellUnset: %8u\n",
  997. stats->timer,
  998. stats->doorbell,
  999. stats->start, stats->start_fail,
  1000. stats->guest_type, stats->guest_type_fail,
  1001. stats->lock[true], stats->lock_fail[true],
  1002. stats->lock[false], stats->lock_fail[false],
  1003. stats->unlock[true], stats->unlock_fail[true],
  1004. stats->unlock[false], stats->unlock_fail[false],
  1005. stats->target, stats->target_fail,
  1006. stats->alloc[true], stats->alloc_fail[true],
  1007. stats->alloc[false], stats->alloc_fail[false],
  1008. stats->sleep_alloc, stats->sleep_alloc_fail,
  1009. stats->free[true],
  1010. stats->free[false],
  1011. stats->refused_alloc[true], stats->refused_alloc[false],
  1012. stats->refused_free[true], stats->refused_free[false],
  1013. stats->doorbell_set, stats->doorbell_unset);
  1014. return 0;
  1015. }
  1016. static int vmballoon_debug_open(struct inode *inode, struct file *file)
  1017. {
  1018. return single_open(file, vmballoon_debug_show, inode->i_private);
  1019. }
  1020. static const struct file_operations vmballoon_debug_fops = {
  1021. .owner = THIS_MODULE,
  1022. .open = vmballoon_debug_open,
  1023. .read = seq_read,
  1024. .llseek = seq_lseek,
  1025. .release = single_release,
  1026. };
  1027. static int __init vmballoon_debugfs_init(struct vmballoon *b)
  1028. {
  1029. int error;
  1030. b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
  1031. &vmballoon_debug_fops);
  1032. if (IS_ERR(b->dbg_entry)) {
  1033. error = PTR_ERR(b->dbg_entry);
  1034. pr_err("failed to create debugfs entry, error: %d\n", error);
  1035. return error;
  1036. }
  1037. return 0;
  1038. }
  1039. static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
  1040. {
  1041. debugfs_remove(b->dbg_entry);
  1042. }
  1043. #else
  1044. static inline int vmballoon_debugfs_init(struct vmballoon *b)
  1045. {
  1046. return 0;
  1047. }
  1048. static inline void vmballoon_debugfs_exit(struct vmballoon *b)
  1049. {
  1050. }
  1051. #endif /* CONFIG_DEBUG_FS */
  1052. static int __init vmballoon_init(void)
  1053. {
  1054. int error;
  1055. unsigned is_2m_pages;
  1056. /*
  1057. * Check if we are running on VMware's hypervisor and bail out
  1058. * if we are not.
  1059. */
  1060. if (x86_hyper != &x86_hyper_vmware)
  1061. return -ENODEV;
  1062. for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
  1063. is_2m_pages++) {
  1064. INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].pages);
  1065. INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].refused_pages);
  1066. }
  1067. /* initialize rates */
  1068. balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
  1069. INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
  1070. error = vmballoon_debugfs_init(&balloon);
  1071. if (error)
  1072. return error;
  1073. balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
  1074. balloon.batch_page = NULL;
  1075. balloon.page = NULL;
  1076. balloon.reset_required = true;
  1077. queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
  1078. return 0;
  1079. }
  1080. /*
  1081. * Using late_initcall() instead of module_init() allows the balloon to use the
  1082. * VMCI doorbell even when the balloon is built into the kernel. Otherwise the
  1083. * VMCI is probed only after the balloon is initialized. If the balloon is used
  1084. * as a module, late_initcall() is equivalent to module_init().
  1085. */
  1086. late_initcall(vmballoon_init);
  1087. static void __exit vmballoon_exit(void)
  1088. {
  1089. vmballoon_vmci_cleanup(&balloon);
  1090. cancel_delayed_work_sync(&balloon.dwork);
  1091. vmballoon_debugfs_exit(&balloon);
  1092. /*
  1093. * Deallocate all reserved memory, and reset connection with monitor.
  1094. * Reset connection before deallocating memory to avoid potential for
  1095. * additional spurious resets from guest touching deallocated pages.
  1096. */
  1097. vmballoon_send_start(&balloon, 0);
  1098. vmballoon_pop(&balloon);
  1099. }
  1100. module_exit(vmballoon_exit);