vmw_balloon.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * VMware Balloon driver.
  4. *
  5. * Copyright (C) 2000-2018, VMware, Inc. All Rights Reserved.
  6. *
  7. * This is VMware physical memory management driver for Linux. The driver
  8. * acts like a "balloon" that can be inflated to reclaim physical pages by
  9. * reserving them in the guest and invalidating them in the monitor,
  10. * freeing up the underlying machine pages so they can be allocated to
  11. * other guests. The balloon can also be deflated to allow the guest to
  12. * use more physical memory. Higher level policies can control the sizes
  13. * of balloons in VMs in order to manage physical memory resources.
  14. */
  15. //#define DEBUG
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/types.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/sched.h>
  23. #include <linux/module.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/rwsem.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/balloon.h>
  31. #include <linux/vmw_vmci_defs.h>
  32. #include <linux/vmw_vmci_api.h>
  33. #include <asm/hypervisor.h>
  34. MODULE_AUTHOR("VMware, Inc.");
  35. MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
  36. MODULE_ALIAS("dmi:*:svnVMware*:*");
  37. MODULE_ALIAS("vmware_vmmemctl");
  38. MODULE_LICENSE("GPL");
  39. static bool __read_mostly vmwballoon_shrinker_enable;
  40. module_param(vmwballoon_shrinker_enable, bool, 0444);
  41. MODULE_PARM_DESC(vmwballoon_shrinker_enable,
  42. "Enable non-cooperative out-of-memory protection. Disabled by default as it may degrade performance.");
  43. /* Delay in seconds after shrink before inflation. */
  44. #define VMBALLOON_SHRINK_DELAY (5)
  45. /* Maximum number of refused pages we accumulate during inflation cycle */
  46. #define VMW_BALLOON_MAX_REFUSED 16
  47. /* Magic number for the balloon mount-point */
  48. #define BALLOON_VMW_MAGIC 0x0ba11007
  49. /*
  50. * Hypervisor communication port definitions.
  51. */
  52. #define VMW_BALLOON_HV_PORT 0x5670
  53. #define VMW_BALLOON_HV_MAGIC 0x456c6d6f
  54. #define VMW_BALLOON_GUEST_ID 1 /* Linux */
  55. enum vmwballoon_capabilities {
  56. /*
  57. * Bit 0 is reserved and not associated to any capability.
  58. */
  59. VMW_BALLOON_BASIC_CMDS = (1 << 1),
  60. VMW_BALLOON_BATCHED_CMDS = (1 << 2),
  61. VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
  62. VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
  63. VMW_BALLOON_64_BIT_TARGET = (1 << 5)
  64. };
  65. #define VMW_BALLOON_CAPABILITIES_COMMON (VMW_BALLOON_BASIC_CMDS \
  66. | VMW_BALLOON_BATCHED_CMDS \
  67. | VMW_BALLOON_BATCHED_2M_CMDS \
  68. | VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
  69. #define VMW_BALLOON_2M_ORDER (PMD_SHIFT - PAGE_SHIFT)
  70. /*
  71. * 64-bit targets are only supported in 64-bit
  72. */
  73. #ifdef CONFIG_64BIT
  74. #define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_CAPABILITIES_COMMON \
  75. | VMW_BALLOON_64_BIT_TARGET)
  76. #else
  77. #define VMW_BALLOON_CAPABILITIES VMW_BALLOON_CAPABILITIES_COMMON
  78. #endif
  79. enum vmballoon_page_size_type {
  80. VMW_BALLOON_4K_PAGE,
  81. VMW_BALLOON_2M_PAGE,
  82. VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
  83. };
  84. #define VMW_BALLOON_NUM_PAGE_SIZES (VMW_BALLOON_LAST_SIZE + 1)
  85. static const char * const vmballoon_page_size_names[] = {
  86. [VMW_BALLOON_4K_PAGE] = "4k",
  87. [VMW_BALLOON_2M_PAGE] = "2M"
  88. };
  89. enum vmballoon_op {
  90. VMW_BALLOON_INFLATE,
  91. VMW_BALLOON_DEFLATE
  92. };
  93. enum vmballoon_op_stat_type {
  94. VMW_BALLOON_OP_STAT,
  95. VMW_BALLOON_OP_FAIL_STAT
  96. };
  97. #define VMW_BALLOON_OP_STAT_TYPES (VMW_BALLOON_OP_FAIL_STAT + 1)
  98. /**
  99. * enum vmballoon_cmd_type - backdoor commands.
  100. *
  101. * Availability of the commands is as followed:
  102. *
  103. * %VMW_BALLOON_CMD_START, %VMW_BALLOON_CMD_GET_TARGET and
  104. * %VMW_BALLOON_CMD_GUEST_ID are always available.
  105. *
  106. * If the host reports %VMW_BALLOON_BASIC_CMDS are supported then
  107. * %VMW_BALLOON_CMD_LOCK and %VMW_BALLOON_CMD_UNLOCK commands are available.
  108. *
  109. * If the host reports %VMW_BALLOON_BATCHED_CMDS are supported then
  110. * %VMW_BALLOON_CMD_BATCHED_LOCK and VMW_BALLOON_CMD_BATCHED_UNLOCK commands
  111. * are available.
  112. *
  113. * If the host reports %VMW_BALLOON_BATCHED_2M_CMDS are supported then
  114. * %VMW_BALLOON_CMD_BATCHED_2M_LOCK and %VMW_BALLOON_CMD_BATCHED_2M_UNLOCK
  115. * are supported.
  116. *
  117. * If the host reports VMW_BALLOON_SIGNALLED_WAKEUP_CMD is supported then
  118. * VMW_BALLOON_CMD_VMCI_DOORBELL_SET command is supported.
  119. *
  120. * @VMW_BALLOON_CMD_START: Communicating supported version with the hypervisor.
  121. * @VMW_BALLOON_CMD_GET_TARGET: Gets the balloon target size.
  122. * @VMW_BALLOON_CMD_LOCK: Informs the hypervisor about a ballooned page.
  123. * @VMW_BALLOON_CMD_UNLOCK: Informs the hypervisor about a page that is about
  124. * to be deflated from the balloon.
  125. * @VMW_BALLOON_CMD_GUEST_ID: Informs the hypervisor about the type of OS that
  126. * runs in the VM.
  127. * @VMW_BALLOON_CMD_BATCHED_LOCK: Inform the hypervisor about a batch of
  128. * ballooned pages (up to 512).
  129. * @VMW_BALLOON_CMD_BATCHED_UNLOCK: Inform the hypervisor about a batch of
  130. * pages that are about to be deflated from the
  131. * balloon (up to 512).
  132. * @VMW_BALLOON_CMD_BATCHED_2M_LOCK: Similar to @VMW_BALLOON_CMD_BATCHED_LOCK
  133. * for 2MB pages.
  134. * @VMW_BALLOON_CMD_BATCHED_2M_UNLOCK: Similar to
  135. * @VMW_BALLOON_CMD_BATCHED_UNLOCK for 2MB
  136. * pages.
  137. * @VMW_BALLOON_CMD_VMCI_DOORBELL_SET: A command to set doorbell notification
  138. * that would be invoked when the balloon
  139. * size changes.
  140. * @VMW_BALLOON_CMD_LAST: Value of the last command.
  141. */
  142. enum vmballoon_cmd_type {
  143. VMW_BALLOON_CMD_START,
  144. VMW_BALLOON_CMD_GET_TARGET,
  145. VMW_BALLOON_CMD_LOCK,
  146. VMW_BALLOON_CMD_UNLOCK,
  147. VMW_BALLOON_CMD_GUEST_ID,
  148. /* No command 5 */
  149. VMW_BALLOON_CMD_BATCHED_LOCK = 6,
  150. VMW_BALLOON_CMD_BATCHED_UNLOCK,
  151. VMW_BALLOON_CMD_BATCHED_2M_LOCK,
  152. VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
  153. VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  154. VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  155. };
  156. #define VMW_BALLOON_CMD_NUM (VMW_BALLOON_CMD_LAST + 1)
  157. enum vmballoon_error_codes {
  158. VMW_BALLOON_SUCCESS,
  159. VMW_BALLOON_ERROR_CMD_INVALID,
  160. VMW_BALLOON_ERROR_PPN_INVALID,
  161. VMW_BALLOON_ERROR_PPN_LOCKED,
  162. VMW_BALLOON_ERROR_PPN_UNLOCKED,
  163. VMW_BALLOON_ERROR_PPN_PINNED,
  164. VMW_BALLOON_ERROR_PPN_NOTNEEDED,
  165. VMW_BALLOON_ERROR_RESET,
  166. VMW_BALLOON_ERROR_BUSY
  167. };
  168. #define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
  169. #define VMW_BALLOON_CMD_WITH_TARGET_MASK \
  170. ((1UL << VMW_BALLOON_CMD_GET_TARGET) | \
  171. (1UL << VMW_BALLOON_CMD_LOCK) | \
  172. (1UL << VMW_BALLOON_CMD_UNLOCK) | \
  173. (1UL << VMW_BALLOON_CMD_BATCHED_LOCK) | \
  174. (1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK) | \
  175. (1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK) | \
  176. (1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
  177. static const char * const vmballoon_cmd_names[] = {
  178. [VMW_BALLOON_CMD_START] = "start",
  179. [VMW_BALLOON_CMD_GET_TARGET] = "target",
  180. [VMW_BALLOON_CMD_LOCK] = "lock",
  181. [VMW_BALLOON_CMD_UNLOCK] = "unlock",
  182. [VMW_BALLOON_CMD_GUEST_ID] = "guestType",
  183. [VMW_BALLOON_CMD_BATCHED_LOCK] = "batchLock",
  184. [VMW_BALLOON_CMD_BATCHED_UNLOCK] = "batchUnlock",
  185. [VMW_BALLOON_CMD_BATCHED_2M_LOCK] = "2m-lock",
  186. [VMW_BALLOON_CMD_BATCHED_2M_UNLOCK] = "2m-unlock",
  187. [VMW_BALLOON_CMD_VMCI_DOORBELL_SET] = "doorbellSet"
  188. };
  189. enum vmballoon_stat_page {
  190. VMW_BALLOON_PAGE_STAT_ALLOC,
  191. VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
  192. VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
  193. VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
  194. VMW_BALLOON_PAGE_STAT_FREE,
  195. VMW_BALLOON_PAGE_STAT_LAST = VMW_BALLOON_PAGE_STAT_FREE
  196. };
  197. #define VMW_BALLOON_PAGE_STAT_NUM (VMW_BALLOON_PAGE_STAT_LAST + 1)
  198. enum vmballoon_stat_general {
  199. VMW_BALLOON_STAT_TIMER,
  200. VMW_BALLOON_STAT_DOORBELL,
  201. VMW_BALLOON_STAT_RESET,
  202. VMW_BALLOON_STAT_SHRINK,
  203. VMW_BALLOON_STAT_SHRINK_FREE,
  204. VMW_BALLOON_STAT_LAST = VMW_BALLOON_STAT_SHRINK_FREE
  205. };
  206. #define VMW_BALLOON_STAT_NUM (VMW_BALLOON_STAT_LAST + 1)
  207. static DEFINE_STATIC_KEY_TRUE(vmw_balloon_batching);
  208. static DEFINE_STATIC_KEY_FALSE(balloon_stat_enabled);
  209. struct vmballoon_ctl {
  210. struct list_head pages;
  211. struct list_head refused_pages;
  212. struct list_head prealloc_pages;
  213. unsigned int n_refused_pages;
  214. unsigned int n_pages;
  215. enum vmballoon_page_size_type page_size;
  216. enum vmballoon_op op;
  217. };
  218. /**
  219. * struct vmballoon_batch_entry - a batch entry for lock or unlock.
  220. *
  221. * @status: the status of the operation, which is written by the hypervisor.
  222. * @reserved: reserved for future use. Must be set to zero.
  223. * @pfn: the physical frame number of the page to be locked or unlocked.
  224. */
  225. struct vmballoon_batch_entry {
  226. u64 status : 5;
  227. u64 reserved : PAGE_SHIFT - 5;
  228. u64 pfn : 52;
  229. } __packed;
  230. struct vmballoon {
  231. /**
  232. * @max_page_size: maximum supported page size for ballooning.
  233. *
  234. * Protected by @conf_sem
  235. */
  236. enum vmballoon_page_size_type max_page_size;
  237. /**
  238. * @size: balloon actual size in basic page size (frames).
  239. *
  240. * While we currently do not support size which is bigger than 32-bit,
  241. * in preparation for future support, use 64-bits.
  242. */
  243. atomic64_t size;
  244. /**
  245. * @target: balloon target size in basic page size (frames).
  246. *
  247. * We do not protect the target under the assumption that setting the
  248. * value is always done through a single write. If this assumption ever
  249. * breaks, we would have to use X_ONCE for accesses, and suffer the less
  250. * optimized code. Although we may read stale target value if multiple
  251. * accesses happen at once, the performance impact should be minor.
  252. */
  253. unsigned long target;
  254. /**
  255. * @reset_required: reset flag
  256. *
  257. * Setting this flag may introduce races, but the code is expected to
  258. * handle them gracefully. In the worst case, another operation will
  259. * fail as reset did not take place. Clearing the flag is done while
  260. * holding @conf_sem for write.
  261. */
  262. bool reset_required;
  263. /**
  264. * @capabilities: hypervisor balloon capabilities.
  265. *
  266. * Protected by @conf_sem.
  267. */
  268. unsigned long capabilities;
  269. /**
  270. * @batch_page: pointer to communication batch page.
  271. *
  272. * When batching is used, batch_page points to a page, which holds up to
  273. * %VMW_BALLOON_BATCH_MAX_PAGES entries for locking or unlocking.
  274. */
  275. struct vmballoon_batch_entry *batch_page;
  276. /**
  277. * @batch_max_pages: maximum pages that can be locked/unlocked.
  278. *
  279. * Indicates the number of pages that the hypervisor can lock or unlock
  280. * at once, according to whether batching is enabled. If batching is
  281. * disabled, only a single page can be locked/unlock on each operation.
  282. *
  283. * Protected by @conf_sem.
  284. */
  285. unsigned int batch_max_pages;
  286. /**
  287. * @page: page to be locked/unlocked by the hypervisor
  288. *
  289. * @page is only used when batching is disabled and a single page is
  290. * reclaimed on each iteration.
  291. *
  292. * Protected by @comm_lock.
  293. */
  294. struct page *page;
  295. /**
  296. * @shrink_timeout: timeout until the next inflation.
  297. *
  298. * After an shrink event, indicates the time in jiffies after which
  299. * inflation is allowed again. Can be written concurrently with reads,
  300. * so must use READ_ONCE/WRITE_ONCE when accessing.
  301. */
  302. unsigned long shrink_timeout;
  303. /* statistics */
  304. struct vmballoon_stats *stats;
  305. /**
  306. * @b_dev_info: balloon device information descriptor.
  307. */
  308. struct balloon_dev_info b_dev_info;
  309. struct delayed_work dwork;
  310. /**
  311. * @huge_pages - list of the inflated 2MB pages.
  312. *
  313. * Protected by @huge_pages_lock.
  314. */
  315. struct list_head huge_pages;
  316. /**
  317. * @huge_pages_lock: lock for the list of inflated 2MB pages.
  318. */
  319. spinlock_t huge_pages_lock;
  320. /**
  321. * @vmci_doorbell.
  322. *
  323. * Protected by @conf_sem.
  324. */
  325. struct vmci_handle vmci_doorbell;
  326. /**
  327. * @conf_sem: semaphore to protect the configuration and the statistics.
  328. */
  329. struct rw_semaphore conf_sem;
  330. /**
  331. * @comm_lock: lock to protect the communication with the host.
  332. *
  333. * Lock ordering: @conf_sem -> @comm_lock .
  334. */
  335. spinlock_t comm_lock;
  336. /**
  337. * @shrinker: shrinker interface that is used to avoid over-inflation.
  338. */
  339. struct shrinker *shrinker;
  340. };
  341. static struct vmballoon balloon;
  342. struct vmballoon_stats {
  343. /* timer / doorbell operations */
  344. atomic64_t general_stat[VMW_BALLOON_STAT_NUM];
  345. /* allocation statistics for huge and small pages */
  346. atomic64_t
  347. page_stat[VMW_BALLOON_PAGE_STAT_NUM][VMW_BALLOON_NUM_PAGE_SIZES];
  348. /* Monitor operations: total operations, and failures */
  349. atomic64_t ops[VMW_BALLOON_CMD_NUM][VMW_BALLOON_OP_STAT_TYPES];
  350. };
  351. static inline bool is_vmballoon_stats_on(void)
  352. {
  353. return IS_ENABLED(CONFIG_DEBUG_FS) &&
  354. static_branch_unlikely(&balloon_stat_enabled);
  355. }
  356. static inline void vmballoon_stats_op_inc(struct vmballoon *b, unsigned int op,
  357. enum vmballoon_op_stat_type type)
  358. {
  359. if (is_vmballoon_stats_on())
  360. atomic64_inc(&b->stats->ops[op][type]);
  361. }
  362. static inline void vmballoon_stats_gen_inc(struct vmballoon *b,
  363. enum vmballoon_stat_general stat)
  364. {
  365. if (is_vmballoon_stats_on())
  366. atomic64_inc(&b->stats->general_stat[stat]);
  367. }
  368. static inline void vmballoon_stats_gen_add(struct vmballoon *b,
  369. enum vmballoon_stat_general stat,
  370. unsigned int val)
  371. {
  372. if (is_vmballoon_stats_on())
  373. atomic64_add(val, &b->stats->general_stat[stat]);
  374. }
  375. static inline void vmballoon_stats_page_inc(struct vmballoon *b,
  376. enum vmballoon_stat_page stat,
  377. enum vmballoon_page_size_type size)
  378. {
  379. if (is_vmballoon_stats_on())
  380. atomic64_inc(&b->stats->page_stat[stat][size]);
  381. }
  382. static inline void vmballoon_stats_page_add(struct vmballoon *b,
  383. enum vmballoon_stat_page stat,
  384. enum vmballoon_page_size_type size,
  385. unsigned int val)
  386. {
  387. if (is_vmballoon_stats_on())
  388. atomic64_add(val, &b->stats->page_stat[stat][size]);
  389. }
  390. static inline unsigned long
  391. __vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
  392. unsigned long arg2, unsigned long *result)
  393. {
  394. unsigned long status, dummy1, dummy2, dummy3, local_result;
  395. vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_STAT);
  396. asm volatile ("inl %%dx" :
  397. "=a"(status),
  398. "=c"(dummy1),
  399. "=d"(dummy2),
  400. "=b"(local_result),
  401. "=S"(dummy3) :
  402. "0"(VMW_BALLOON_HV_MAGIC),
  403. "1"(cmd),
  404. "2"(VMW_BALLOON_HV_PORT),
  405. "3"(arg1),
  406. "4"(arg2) :
  407. "memory");
  408. /* update the result if needed */
  409. if (result)
  410. *result = (cmd == VMW_BALLOON_CMD_START) ? dummy1 :
  411. local_result;
  412. /* update target when applicable */
  413. if (status == VMW_BALLOON_SUCCESS &&
  414. ((1ul << cmd) & VMW_BALLOON_CMD_WITH_TARGET_MASK))
  415. WRITE_ONCE(b->target, local_result);
  416. if (status != VMW_BALLOON_SUCCESS &&
  417. status != VMW_BALLOON_SUCCESS_WITH_CAPABILITIES) {
  418. vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_FAIL_STAT);
  419. pr_debug("%s: %s [0x%lx,0x%lx) failed, returned %ld\n",
  420. __func__, vmballoon_cmd_names[cmd], arg1, arg2,
  421. status);
  422. }
  423. /* mark reset required accordingly */
  424. if (status == VMW_BALLOON_ERROR_RESET)
  425. b->reset_required = true;
  426. return status;
  427. }
  428. static __always_inline unsigned long
  429. vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
  430. unsigned long arg2)
  431. {
  432. unsigned long dummy;
  433. return __vmballoon_cmd(b, cmd, arg1, arg2, &dummy);
  434. }
  435. /*
  436. * Send "start" command to the host, communicating supported version
  437. * of the protocol.
  438. */
  439. static int vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
  440. {
  441. unsigned long status, capabilities;
  442. status = __vmballoon_cmd(b, VMW_BALLOON_CMD_START, req_caps, 0,
  443. &capabilities);
  444. switch (status) {
  445. case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
  446. b->capabilities = capabilities;
  447. break;
  448. case VMW_BALLOON_SUCCESS:
  449. b->capabilities = VMW_BALLOON_BASIC_CMDS;
  450. break;
  451. default:
  452. return -EIO;
  453. }
  454. /*
  455. * 2MB pages are only supported with batching. If batching is for some
  456. * reason disabled, do not use 2MB pages, since otherwise the legacy
  457. * mechanism is used with 2MB pages, causing a failure.
  458. */
  459. b->max_page_size = VMW_BALLOON_4K_PAGE;
  460. if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
  461. (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
  462. b->max_page_size = VMW_BALLOON_2M_PAGE;
  463. return 0;
  464. }
  465. /**
  466. * vmballoon_send_guest_id - communicate guest type to the host.
  467. *
  468. * @b: pointer to the balloon.
  469. *
  470. * Communicate guest type to the host so that it can adjust ballooning
  471. * algorithm to the one most appropriate for the guest. This command
  472. * is normally issued after sending "start" command and is part of
  473. * standard reset sequence.
  474. *
  475. * Return: zero on success or appropriate error code.
  476. */
  477. static int vmballoon_send_guest_id(struct vmballoon *b)
  478. {
  479. unsigned long status;
  480. status = vmballoon_cmd(b, VMW_BALLOON_CMD_GUEST_ID,
  481. VMW_BALLOON_GUEST_ID, 0);
  482. return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
  483. }
  484. /**
  485. * vmballoon_page_order() - return the order of the page
  486. * @page_size: the size of the page.
  487. *
  488. * Return: the allocation order.
  489. */
  490. static inline
  491. unsigned int vmballoon_page_order(enum vmballoon_page_size_type page_size)
  492. {
  493. return page_size == VMW_BALLOON_2M_PAGE ? VMW_BALLOON_2M_ORDER : 0;
  494. }
  495. /**
  496. * vmballoon_page_in_frames() - returns the number of frames in a page.
  497. * @page_size: the size of the page.
  498. *
  499. * Return: the number of 4k frames.
  500. */
  501. static inline unsigned int
  502. vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)
  503. {
  504. return 1 << vmballoon_page_order(page_size);
  505. }
  506. /**
  507. * vmballoon_mark_page_offline() - mark a page as offline
  508. * @page: pointer for the page.
  509. * @page_size: the size of the page.
  510. */
  511. static void
  512. vmballoon_mark_page_offline(struct page *page,
  513. enum vmballoon_page_size_type page_size)
  514. {
  515. int i;
  516. for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
  517. __SetPageOffline(page + i);
  518. }
  519. /**
  520. * vmballoon_mark_page_online() - mark a page as online
  521. * @page: pointer for the page.
  522. * @page_size: the size of the page.
  523. */
  524. static void
  525. vmballoon_mark_page_online(struct page *page,
  526. enum vmballoon_page_size_type page_size)
  527. {
  528. int i;
  529. for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
  530. __ClearPageOffline(page + i);
  531. }
  532. /**
  533. * vmballoon_send_get_target() - Retrieve desired balloon size from the host.
  534. *
  535. * @b: pointer to the balloon.
  536. *
  537. * Return: zero on success, EINVAL if limit does not fit in 32-bit, as required
  538. * by the host-guest protocol and EIO if an error occurred in communicating with
  539. * the host.
  540. */
  541. static int vmballoon_send_get_target(struct vmballoon *b)
  542. {
  543. unsigned long status;
  544. unsigned long limit;
  545. limit = totalram_pages();
  546. /* Ensure limit fits in 32-bits if 64-bit targets are not supported */
  547. if (!(b->capabilities & VMW_BALLOON_64_BIT_TARGET) &&
  548. limit != (u32)limit)
  549. return -EINVAL;
  550. status = vmballoon_cmd(b, VMW_BALLOON_CMD_GET_TARGET, limit, 0);
  551. return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
  552. }
  553. /**
  554. * vmballoon_alloc_page_list - allocates a list of pages.
  555. *
  556. * @b: pointer to the balloon.
  557. * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
  558. * @req_n_pages: the number of requested pages.
  559. *
  560. * Tries to allocate @req_n_pages. Add them to the list of balloon pages in
  561. * @ctl.pages and updates @ctl.n_pages to reflect the number of pages.
  562. *
  563. * Return: zero on success or error code otherwise.
  564. */
  565. static int vmballoon_alloc_page_list(struct vmballoon *b,
  566. struct vmballoon_ctl *ctl,
  567. unsigned int req_n_pages)
  568. {
  569. struct page *page;
  570. unsigned int i;
  571. for (i = 0; i < req_n_pages; i++) {
  572. /*
  573. * First check if we happen to have pages that were allocated
  574. * before. This happens when 2MB page rejected during inflation
  575. * by the hypervisor, and then split into 4KB pages.
  576. */
  577. if (!list_empty(&ctl->prealloc_pages)) {
  578. page = list_first_entry(&ctl->prealloc_pages,
  579. struct page, lru);
  580. list_del(&page->lru);
  581. } else {
  582. if (ctl->page_size == VMW_BALLOON_2M_PAGE)
  583. page = alloc_pages(__GFP_HIGHMEM|__GFP_NOWARN|
  584. __GFP_NOMEMALLOC, VMW_BALLOON_2M_ORDER);
  585. else
  586. page = balloon_page_alloc();
  587. vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC,
  588. ctl->page_size);
  589. }
  590. if (page) {
  591. /* Success. Add the page to the list and continue. */
  592. list_add(&page->lru, &ctl->pages);
  593. continue;
  594. }
  595. /* Allocation failed. Update statistics and stop. */
  596. vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
  597. ctl->page_size);
  598. break;
  599. }
  600. ctl->n_pages = i;
  601. return req_n_pages == ctl->n_pages ? 0 : -ENOMEM;
  602. }
  603. /**
  604. * vmballoon_handle_one_result - Handle lock/unlock result for a single page.
  605. *
  606. * @b: pointer for %struct vmballoon.
  607. * @page: pointer for the page whose result should be handled.
  608. * @page_size: size of the page.
  609. * @status: status of the operation as provided by the hypervisor.
  610. */
  611. static int vmballoon_handle_one_result(struct vmballoon *b, struct page *page,
  612. enum vmballoon_page_size_type page_size,
  613. unsigned long status)
  614. {
  615. /* On success do nothing. The page is already on the balloon list. */
  616. if (likely(status == VMW_BALLOON_SUCCESS))
  617. return 0;
  618. pr_debug("%s: failed comm pfn %lx status %lu page_size %s\n", __func__,
  619. page_to_pfn(page), status,
  620. vmballoon_page_size_names[page_size]);
  621. /* Error occurred */
  622. vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
  623. page_size);
  624. return -EIO;
  625. }
  626. /**
  627. * vmballoon_status_page - returns the status of (un)lock operation
  628. *
  629. * @b: pointer to the balloon.
  630. * @idx: index for the page for which the operation is performed.
  631. * @p: pointer to where the page struct is returned.
  632. *
  633. * Following a lock or unlock operation, returns the status of the operation for
  634. * an individual page. Provides the page that the operation was performed on on
  635. * the @page argument.
  636. *
  637. * Returns: The status of a lock or unlock operation for an individual page.
  638. */
  639. static unsigned long vmballoon_status_page(struct vmballoon *b, int idx,
  640. struct page **p)
  641. {
  642. if (static_branch_likely(&vmw_balloon_batching)) {
  643. /* batching mode */
  644. *p = pfn_to_page(b->batch_page[idx].pfn);
  645. return b->batch_page[idx].status;
  646. }
  647. /* non-batching mode */
  648. *p = b->page;
  649. /*
  650. * If a failure occurs, the indication will be provided in the status
  651. * of the entire operation, which is considered before the individual
  652. * page status. So for non-batching mode, the indication is always of
  653. * success.
  654. */
  655. return VMW_BALLOON_SUCCESS;
  656. }
  657. /**
  658. * vmballoon_lock_op - notifies the host about inflated/deflated pages.
  659. * @b: pointer to the balloon.
  660. * @num_pages: number of inflated/deflated pages.
  661. * @page_size: size of the page.
  662. * @op: the type of operation (lock or unlock).
  663. *
  664. * Notify the host about page(s) that were ballooned (or removed from the
  665. * balloon) so that host can use it without fear that guest will need it (or
  666. * stop using them since the VM does). Host may reject some pages, we need to
  667. * check the return value and maybe submit a different page. The pages that are
  668. * inflated/deflated are pointed by @b->page.
  669. *
  670. * Return: result as provided by the hypervisor.
  671. */
  672. static unsigned long vmballoon_lock_op(struct vmballoon *b,
  673. unsigned int num_pages,
  674. enum vmballoon_page_size_type page_size,
  675. enum vmballoon_op op)
  676. {
  677. unsigned long cmd, pfn;
  678. lockdep_assert_held(&b->comm_lock);
  679. if (static_branch_likely(&vmw_balloon_batching)) {
  680. if (op == VMW_BALLOON_INFLATE)
  681. cmd = page_size == VMW_BALLOON_2M_PAGE ?
  682. VMW_BALLOON_CMD_BATCHED_2M_LOCK :
  683. VMW_BALLOON_CMD_BATCHED_LOCK;
  684. else
  685. cmd = page_size == VMW_BALLOON_2M_PAGE ?
  686. VMW_BALLOON_CMD_BATCHED_2M_UNLOCK :
  687. VMW_BALLOON_CMD_BATCHED_UNLOCK;
  688. pfn = PHYS_PFN(virt_to_phys(b->batch_page));
  689. } else {
  690. cmd = op == VMW_BALLOON_INFLATE ? VMW_BALLOON_CMD_LOCK :
  691. VMW_BALLOON_CMD_UNLOCK;
  692. pfn = page_to_pfn(b->page);
  693. /* In non-batching mode, PFNs must fit in 32-bit */
  694. if (unlikely(pfn != (u32)pfn))
  695. return VMW_BALLOON_ERROR_PPN_INVALID;
  696. }
  697. return vmballoon_cmd(b, cmd, pfn, num_pages);
  698. }
  699. /**
  700. * vmballoon_add_page - adds a page towards lock/unlock operation.
  701. *
  702. * @b: pointer to the balloon.
  703. * @idx: index of the page to be ballooned in this batch.
  704. * @p: pointer to the page that is about to be ballooned.
  705. *
  706. * Adds the page to be ballooned. Must be called while holding @comm_lock.
  707. */
  708. static void vmballoon_add_page(struct vmballoon *b, unsigned int idx,
  709. struct page *p)
  710. {
  711. lockdep_assert_held(&b->comm_lock);
  712. if (static_branch_likely(&vmw_balloon_batching))
  713. b->batch_page[idx] = (struct vmballoon_batch_entry)
  714. { .pfn = page_to_pfn(p) };
  715. else
  716. b->page = p;
  717. }
  718. /**
  719. * vmballoon_lock - lock or unlock a batch of pages.
  720. *
  721. * @b: pointer to the balloon.
  722. * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
  723. *
  724. * Notifies the host of about ballooned pages (after inflation or deflation,
  725. * according to @ctl). If the host rejects the page put it on the
  726. * @ctl refuse list. These refused page are then released when moving to the
  727. * next size of pages.
  728. *
  729. * Note that we neither free any @page here nor put them back on the ballooned
  730. * pages list. Instead we queue it for later processing. We do that for several
  731. * reasons. First, we do not want to free the page under the lock. Second, it
  732. * allows us to unify the handling of lock and unlock. In the inflate case, the
  733. * caller will check if there are too many refused pages and release them.
  734. * Although it is not identical to the past behavior, it should not affect
  735. * performance.
  736. */
  737. static int vmballoon_lock(struct vmballoon *b, struct vmballoon_ctl *ctl)
  738. {
  739. unsigned long batch_status;
  740. struct page *page;
  741. unsigned int i, num_pages;
  742. num_pages = ctl->n_pages;
  743. if (num_pages == 0)
  744. return 0;
  745. /* communication with the host is done under the communication lock */
  746. spin_lock(&b->comm_lock);
  747. i = 0;
  748. list_for_each_entry(page, &ctl->pages, lru)
  749. vmballoon_add_page(b, i++, page);
  750. batch_status = vmballoon_lock_op(b, ctl->n_pages, ctl->page_size,
  751. ctl->op);
  752. /*
  753. * Iterate over the pages in the provided list. Since we are changing
  754. * @ctl->n_pages we are saving the original value in @num_pages and
  755. * use this value to bound the loop.
  756. */
  757. for (i = 0; i < num_pages; i++) {
  758. unsigned long status;
  759. status = vmballoon_status_page(b, i, &page);
  760. /*
  761. * Failure of the whole batch overrides a single operation
  762. * results.
  763. */
  764. if (batch_status != VMW_BALLOON_SUCCESS)
  765. status = batch_status;
  766. /* Continue if no error happened */
  767. if (!vmballoon_handle_one_result(b, page, ctl->page_size,
  768. status))
  769. continue;
  770. /*
  771. * Error happened. Move the pages to the refused list and update
  772. * the pages number.
  773. */
  774. list_move(&page->lru, &ctl->refused_pages);
  775. ctl->n_pages--;
  776. ctl->n_refused_pages++;
  777. }
  778. spin_unlock(&b->comm_lock);
  779. return batch_status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
  780. }
  781. /**
  782. * vmballoon_release_page_list() - Releases a page list
  783. *
  784. * @page_list: list of pages to release.
  785. * @n_pages: pointer to the number of pages.
  786. * @page_size: whether the pages in the list are 2MB (or else 4KB).
  787. *
  788. * Releases the list of pages and zeros the number of pages.
  789. */
  790. static void vmballoon_release_page_list(struct list_head *page_list,
  791. int *n_pages,
  792. enum vmballoon_page_size_type page_size)
  793. {
  794. struct page *page, *tmp;
  795. list_for_each_entry_safe(page, tmp, page_list, lru) {
  796. list_del(&page->lru);
  797. __free_pages(page, vmballoon_page_order(page_size));
  798. }
  799. if (n_pages)
  800. *n_pages = 0;
  801. }
  802. /*
  803. * Release pages that were allocated while attempting to inflate the
  804. * balloon but were refused by the host for one reason or another.
  805. */
  806. static void vmballoon_release_refused_pages(struct vmballoon *b,
  807. struct vmballoon_ctl *ctl)
  808. {
  809. vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
  810. ctl->page_size);
  811. vmballoon_release_page_list(&ctl->refused_pages, &ctl->n_refused_pages,
  812. ctl->page_size);
  813. }
  814. /**
  815. * vmballoon_change - retrieve the required balloon change
  816. *
  817. * @b: pointer for the balloon.
  818. *
  819. * Return: the required change for the balloon size. A positive number
  820. * indicates inflation, a negative number indicates a deflation.
  821. */
  822. static int64_t vmballoon_change(struct vmballoon *b)
  823. {
  824. int64_t size, target;
  825. size = atomic64_read(&b->size);
  826. target = READ_ONCE(b->target);
  827. /*
  828. * We must cast first because of int sizes
  829. * Otherwise we might get huge positives instead of negatives
  830. */
  831. if (b->reset_required)
  832. return 0;
  833. /* consider a 2MB slack on deflate, unless the balloon is emptied */
  834. if (target < size && target != 0 &&
  835. size - target < vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE))
  836. return 0;
  837. /* If an out-of-memory recently occurred, inflation is disallowed. */
  838. if (target > size && time_before(jiffies, READ_ONCE(b->shrink_timeout)))
  839. return 0;
  840. return target - size;
  841. }
  842. /**
  843. * vmballoon_enqueue_page_list() - Enqueues list of pages after inflation.
  844. *
  845. * @b: pointer to balloon.
  846. * @pages: list of pages to enqueue.
  847. * @n_pages: pointer to number of pages in list. The value is zeroed.
  848. * @page_size: whether the pages are 2MB or 4KB pages.
  849. *
  850. * Enqueues the provides list of pages in the ballooned page list, clears the
  851. * list and zeroes the number of pages that was provided.
  852. */
  853. static void vmballoon_enqueue_page_list(struct vmballoon *b,
  854. struct list_head *pages,
  855. unsigned int *n_pages,
  856. enum vmballoon_page_size_type page_size)
  857. {
  858. struct page *page;
  859. if (page_size == VMW_BALLOON_4K_PAGE) {
  860. balloon_page_list_enqueue(&b->b_dev_info, pages);
  861. } else {
  862. /*
  863. * Keep the huge pages in a local list which is not available
  864. * for the balloon page migration.
  865. */
  866. spin_lock(&b->huge_pages_lock);
  867. list_for_each_entry(page, pages, lru) {
  868. vmballoon_mark_page_offline(page, VMW_BALLOON_2M_PAGE);
  869. }
  870. list_splice_init(pages, &b->huge_pages);
  871. __count_vm_events(BALLOON_INFLATE, *n_pages *
  872. vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
  873. spin_unlock(&b->huge_pages_lock);
  874. }
  875. *n_pages = 0;
  876. }
  877. /**
  878. * vmballoon_dequeue_page_list() - Dequeues page lists for deflation.
  879. *
  880. * @b: pointer to balloon.
  881. * @pages: list of pages to enqueue.
  882. * @n_pages: pointer to number of pages in list. The value is zeroed.
  883. * @page_size: whether the pages are 2MB or 4KB pages.
  884. * @n_req_pages: the number of requested pages.
  885. *
  886. * Dequeues the number of requested pages from the balloon for deflation. The
  887. * number of dequeued pages may be lower, if not enough pages in the requested
  888. * size are available.
  889. */
  890. static void vmballoon_dequeue_page_list(struct vmballoon *b,
  891. struct list_head *pages,
  892. unsigned int *n_pages,
  893. enum vmballoon_page_size_type page_size,
  894. unsigned int n_req_pages)
  895. {
  896. struct page *page, *tmp;
  897. unsigned int i = 0;
  898. /* In the case of 4k pages, use the compaction infrastructure */
  899. if (page_size == VMW_BALLOON_4K_PAGE) {
  900. *n_pages = balloon_page_list_dequeue(&b->b_dev_info, pages,
  901. n_req_pages);
  902. return;
  903. }
  904. /* 2MB pages */
  905. spin_lock(&b->huge_pages_lock);
  906. list_for_each_entry_safe(page, tmp, &b->huge_pages, lru) {
  907. vmballoon_mark_page_online(page, VMW_BALLOON_2M_PAGE);
  908. list_move(&page->lru, pages);
  909. if (++i == n_req_pages)
  910. break;
  911. }
  912. __count_vm_events(BALLOON_DEFLATE,
  913. i * vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
  914. spin_unlock(&b->huge_pages_lock);
  915. *n_pages = i;
  916. }
  917. /**
  918. * vmballoon_split_refused_pages() - Split the 2MB refused pages to 4k.
  919. *
  920. * If inflation of 2MB pages was denied by the hypervisor, it is likely to be
  921. * due to one or few 4KB pages. These 2MB pages may keep being allocated and
  922. * then being refused. To prevent this case, this function splits the refused
  923. * pages into 4KB pages and adds them into @prealloc_pages list.
  924. *
  925. * @ctl: pointer for the %struct vmballoon_ctl, which defines the operation.
  926. */
  927. static void vmballoon_split_refused_pages(struct vmballoon_ctl *ctl)
  928. {
  929. struct page *page, *tmp;
  930. unsigned int i, order;
  931. order = vmballoon_page_order(ctl->page_size);
  932. list_for_each_entry_safe(page, tmp, &ctl->refused_pages, lru) {
  933. list_del(&page->lru);
  934. split_page(page, order);
  935. for (i = 0; i < (1 << order); i++)
  936. list_add(&page[i].lru, &ctl->prealloc_pages);
  937. }
  938. ctl->n_refused_pages = 0;
  939. }
  940. /**
  941. * vmballoon_inflate() - Inflate the balloon towards its target size.
  942. *
  943. * @b: pointer to the balloon.
  944. */
  945. static void vmballoon_inflate(struct vmballoon *b)
  946. {
  947. int64_t to_inflate_frames;
  948. struct vmballoon_ctl ctl = {
  949. .pages = LIST_HEAD_INIT(ctl.pages),
  950. .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
  951. .prealloc_pages = LIST_HEAD_INIT(ctl.prealloc_pages),
  952. .page_size = b->max_page_size,
  953. .op = VMW_BALLOON_INFLATE
  954. };
  955. while ((to_inflate_frames = vmballoon_change(b)) > 0) {
  956. unsigned int to_inflate_pages, page_in_frames;
  957. int alloc_error, lock_error = 0;
  958. VM_BUG_ON(!list_empty(&ctl.pages));
  959. VM_BUG_ON(ctl.n_pages != 0);
  960. page_in_frames = vmballoon_page_in_frames(ctl.page_size);
  961. to_inflate_pages = min_t(unsigned long, b->batch_max_pages,
  962. DIV_ROUND_UP_ULL(to_inflate_frames,
  963. page_in_frames));
  964. /* Start by allocating */
  965. alloc_error = vmballoon_alloc_page_list(b, &ctl,
  966. to_inflate_pages);
  967. /* Actually lock the pages by telling the hypervisor */
  968. lock_error = vmballoon_lock(b, &ctl);
  969. /*
  970. * If an error indicates that something serious went wrong,
  971. * stop the inflation.
  972. */
  973. if (lock_error)
  974. break;
  975. /* Update the balloon size */
  976. atomic64_add(ctl.n_pages * page_in_frames, &b->size);
  977. vmballoon_enqueue_page_list(b, &ctl.pages, &ctl.n_pages,
  978. ctl.page_size);
  979. /*
  980. * If allocation failed or the number of refused pages exceeds
  981. * the maximum allowed, move to the next page size.
  982. */
  983. if (alloc_error ||
  984. ctl.n_refused_pages >= VMW_BALLOON_MAX_REFUSED) {
  985. if (ctl.page_size == VMW_BALLOON_4K_PAGE)
  986. break;
  987. /*
  988. * Split the refused pages to 4k. This will also empty
  989. * the refused pages list.
  990. */
  991. vmballoon_split_refused_pages(&ctl);
  992. ctl.page_size--;
  993. }
  994. cond_resched();
  995. }
  996. /*
  997. * Release pages that were allocated while attempting to inflate the
  998. * balloon but were refused by the host for one reason or another,
  999. * and update the statistics.
  1000. */
  1001. if (ctl.n_refused_pages != 0)
  1002. vmballoon_release_refused_pages(b, &ctl);
  1003. vmballoon_release_page_list(&ctl.prealloc_pages, NULL, ctl.page_size);
  1004. }
  1005. /**
  1006. * vmballoon_deflate() - Decrease the size of the balloon.
  1007. *
  1008. * @b: pointer to the balloon
  1009. * @n_frames: the number of frames to deflate. If zero, automatically
  1010. * calculated according to the target size.
  1011. * @coordinated: whether to coordinate with the host
  1012. *
  1013. * Decrease the size of the balloon allowing guest to use more memory.
  1014. *
  1015. * Return: The number of deflated frames (i.e., basic page size units)
  1016. */
  1017. static unsigned long vmballoon_deflate(struct vmballoon *b, uint64_t n_frames,
  1018. bool coordinated)
  1019. {
  1020. unsigned long deflated_frames = 0;
  1021. unsigned long tried_frames = 0;
  1022. struct vmballoon_ctl ctl = {
  1023. .pages = LIST_HEAD_INIT(ctl.pages),
  1024. .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
  1025. .page_size = VMW_BALLOON_4K_PAGE,
  1026. .op = VMW_BALLOON_DEFLATE
  1027. };
  1028. /* free pages to reach target */
  1029. while (true) {
  1030. unsigned int to_deflate_pages, n_unlocked_frames;
  1031. unsigned int page_in_frames;
  1032. int64_t to_deflate_frames;
  1033. bool deflated_all;
  1034. page_in_frames = vmballoon_page_in_frames(ctl.page_size);
  1035. VM_BUG_ON(!list_empty(&ctl.pages));
  1036. VM_BUG_ON(ctl.n_pages);
  1037. VM_BUG_ON(!list_empty(&ctl.refused_pages));
  1038. VM_BUG_ON(ctl.n_refused_pages);
  1039. /*
  1040. * If we were requested a specific number of frames, we try to
  1041. * deflate this number of frames. Otherwise, deflation is
  1042. * performed according to the target and balloon size.
  1043. */
  1044. to_deflate_frames = n_frames ? n_frames - tried_frames :
  1045. -vmballoon_change(b);
  1046. /* break if no work to do */
  1047. if (to_deflate_frames <= 0)
  1048. break;
  1049. /*
  1050. * Calculate the number of frames based on current page size,
  1051. * but limit the deflated frames to a single chunk
  1052. */
  1053. to_deflate_pages = min_t(unsigned long, b->batch_max_pages,
  1054. DIV_ROUND_UP_ULL(to_deflate_frames,
  1055. page_in_frames));
  1056. /* First take the pages from the balloon pages. */
  1057. vmballoon_dequeue_page_list(b, &ctl.pages, &ctl.n_pages,
  1058. ctl.page_size, to_deflate_pages);
  1059. /*
  1060. * Before pages are moving to the refused list, count their
  1061. * frames as frames that we tried to deflate.
  1062. */
  1063. tried_frames += ctl.n_pages * page_in_frames;
  1064. /*
  1065. * Unlock the pages by communicating with the hypervisor if the
  1066. * communication is coordinated (i.e., not pop). We ignore the
  1067. * return code. Instead we check if all the pages we manage to
  1068. * unlock all the pages. If we failed, we will move to the next
  1069. * page size, and would eventually try again later.
  1070. */
  1071. if (coordinated)
  1072. vmballoon_lock(b, &ctl);
  1073. /*
  1074. * Check if we deflated enough. We will move to the next page
  1075. * size if we did not manage to do so. This calculation takes
  1076. * place now, as once the pages are released, the number of
  1077. * pages is zeroed.
  1078. */
  1079. deflated_all = (ctl.n_pages == to_deflate_pages);
  1080. /* Update local and global counters */
  1081. n_unlocked_frames = ctl.n_pages * page_in_frames;
  1082. atomic64_sub(n_unlocked_frames, &b->size);
  1083. deflated_frames += n_unlocked_frames;
  1084. vmballoon_stats_page_add(b, VMW_BALLOON_PAGE_STAT_FREE,
  1085. ctl.page_size, ctl.n_pages);
  1086. /* free the ballooned pages */
  1087. vmballoon_release_page_list(&ctl.pages, &ctl.n_pages,
  1088. ctl.page_size);
  1089. /* Return the refused pages to the ballooned list. */
  1090. vmballoon_enqueue_page_list(b, &ctl.refused_pages,
  1091. &ctl.n_refused_pages,
  1092. ctl.page_size);
  1093. /* If we failed to unlock all the pages, move to next size. */
  1094. if (!deflated_all) {
  1095. if (ctl.page_size == b->max_page_size)
  1096. break;
  1097. ctl.page_size++;
  1098. }
  1099. cond_resched();
  1100. }
  1101. return deflated_frames;
  1102. }
  1103. /**
  1104. * vmballoon_deinit_batching - disables batching mode.
  1105. *
  1106. * @b: pointer to &struct vmballoon.
  1107. *
  1108. * Disables batching, by deallocating the page for communication with the
  1109. * hypervisor and disabling the static key to indicate that batching is off.
  1110. */
  1111. static void vmballoon_deinit_batching(struct vmballoon *b)
  1112. {
  1113. free_page((unsigned long)b->batch_page);
  1114. b->batch_page = NULL;
  1115. static_branch_disable(&vmw_balloon_batching);
  1116. b->batch_max_pages = 1;
  1117. }
  1118. /**
  1119. * vmballoon_init_batching - enable batching mode.
  1120. *
  1121. * @b: pointer to &struct vmballoon.
  1122. *
  1123. * Enables batching, by allocating a page for communication with the hypervisor
  1124. * and enabling the static_key to use batching.
  1125. *
  1126. * Return: zero on success or an appropriate error-code.
  1127. */
  1128. static int vmballoon_init_batching(struct vmballoon *b)
  1129. {
  1130. struct page *page;
  1131. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1132. if (!page)
  1133. return -ENOMEM;
  1134. b->batch_page = page_address(page);
  1135. b->batch_max_pages = PAGE_SIZE / sizeof(struct vmballoon_batch_entry);
  1136. static_branch_enable(&vmw_balloon_batching);
  1137. return 0;
  1138. }
  1139. /*
  1140. * Receive notification and resize balloon
  1141. */
  1142. static void vmballoon_doorbell(void *client_data)
  1143. {
  1144. struct vmballoon *b = client_data;
  1145. vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_DOORBELL);
  1146. mod_delayed_work(system_freezable_wq, &b->dwork, 0);
  1147. }
  1148. /*
  1149. * Clean up vmci doorbell
  1150. */
  1151. static void vmballoon_vmci_cleanup(struct vmballoon *b)
  1152. {
  1153. vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  1154. VMCI_INVALID_ID, VMCI_INVALID_ID);
  1155. if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
  1156. vmci_doorbell_destroy(b->vmci_doorbell);
  1157. b->vmci_doorbell = VMCI_INVALID_HANDLE;
  1158. }
  1159. }
  1160. /**
  1161. * vmballoon_vmci_init - Initialize vmci doorbell.
  1162. *
  1163. * @b: pointer to the balloon.
  1164. *
  1165. * Return: zero on success or when wakeup command not supported. Error-code
  1166. * otherwise.
  1167. *
  1168. * Initialize vmci doorbell, to get notified as soon as balloon changes.
  1169. */
  1170. static int vmballoon_vmci_init(struct vmballoon *b)
  1171. {
  1172. unsigned long error;
  1173. if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
  1174. return 0;
  1175. error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
  1176. VMCI_PRIVILEGE_FLAG_RESTRICTED,
  1177. vmballoon_doorbell, b);
  1178. if (error != VMCI_SUCCESS)
  1179. goto fail;
  1180. error = __vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
  1181. b->vmci_doorbell.context,
  1182. b->vmci_doorbell.resource, NULL);
  1183. if (error != VMW_BALLOON_SUCCESS)
  1184. goto fail;
  1185. return 0;
  1186. fail:
  1187. vmballoon_vmci_cleanup(b);
  1188. return -EIO;
  1189. }
  1190. /**
  1191. * vmballoon_pop - Quickly release all pages allocate for the balloon.
  1192. *
  1193. * @b: pointer to the balloon.
  1194. *
  1195. * This function is called when host decides to "reset" balloon for one reason
  1196. * or another. Unlike normal "deflate" we do not (shall not) notify host of the
  1197. * pages being released.
  1198. */
  1199. static void vmballoon_pop(struct vmballoon *b)
  1200. {
  1201. unsigned long size;
  1202. while ((size = atomic64_read(&b->size)))
  1203. vmballoon_deflate(b, size, false);
  1204. }
  1205. /*
  1206. * Perform standard reset sequence by popping the balloon (in case it
  1207. * is not empty) and then restarting protocol. This operation normally
  1208. * happens when host responds with VMW_BALLOON_ERROR_RESET to a command.
  1209. */
  1210. static void vmballoon_reset(struct vmballoon *b)
  1211. {
  1212. int error;
  1213. down_write(&b->conf_sem);
  1214. vmballoon_vmci_cleanup(b);
  1215. /* free all pages, skipping monitor unlock */
  1216. vmballoon_pop(b);
  1217. if (vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
  1218. goto unlock;
  1219. if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
  1220. if (vmballoon_init_batching(b)) {
  1221. /*
  1222. * We failed to initialize batching, inform the monitor
  1223. * about it by sending a null capability.
  1224. *
  1225. * The guest will retry in one second.
  1226. */
  1227. vmballoon_send_start(b, 0);
  1228. goto unlock;
  1229. }
  1230. } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
  1231. vmballoon_deinit_batching(b);
  1232. }
  1233. vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_RESET);
  1234. b->reset_required = false;
  1235. error = vmballoon_vmci_init(b);
  1236. if (error)
  1237. pr_err_once("failed to initialize vmci doorbell\n");
  1238. if (vmballoon_send_guest_id(b))
  1239. pr_err_once("failed to send guest ID to the host\n");
  1240. unlock:
  1241. up_write(&b->conf_sem);
  1242. }
  1243. /**
  1244. * vmballoon_work - periodic balloon worker for reset, inflation and deflation.
  1245. *
  1246. * @work: pointer to the &work_struct which is provided by the workqueue.
  1247. *
  1248. * Resets the protocol if needed, gets the new size and adjusts balloon as
  1249. * needed. Repeat in 1 sec.
  1250. */
  1251. static void vmballoon_work(struct work_struct *work)
  1252. {
  1253. struct delayed_work *dwork = to_delayed_work(work);
  1254. struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
  1255. int64_t change = 0;
  1256. if (b->reset_required)
  1257. vmballoon_reset(b);
  1258. down_read(&b->conf_sem);
  1259. /*
  1260. * Update the stats while holding the semaphore to ensure that
  1261. * @stats_enabled is consistent with whether the stats are actually
  1262. * enabled
  1263. */
  1264. vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_TIMER);
  1265. if (!vmballoon_send_get_target(b))
  1266. change = vmballoon_change(b);
  1267. if (change != 0) {
  1268. pr_debug("%s - size: %llu, target %lu\n", __func__,
  1269. atomic64_read(&b->size), READ_ONCE(b->target));
  1270. if (change > 0)
  1271. vmballoon_inflate(b);
  1272. else /* (change < 0) */
  1273. vmballoon_deflate(b, 0, true);
  1274. }
  1275. up_read(&b->conf_sem);
  1276. /*
  1277. * We are using a freezable workqueue so that balloon operations are
  1278. * stopped while the system transitions to/from sleep/hibernation.
  1279. */
  1280. queue_delayed_work(system_freezable_wq,
  1281. dwork, round_jiffies_relative(HZ));
  1282. }
  1283. /**
  1284. * vmballoon_shrinker_scan() - deflate the balloon due to memory pressure.
  1285. * @shrinker: pointer to the balloon shrinker.
  1286. * @sc: page reclaim information.
  1287. *
  1288. * Returns: number of pages that were freed during deflation.
  1289. */
  1290. static unsigned long vmballoon_shrinker_scan(struct shrinker *shrinker,
  1291. struct shrink_control *sc)
  1292. {
  1293. struct vmballoon *b = &balloon;
  1294. unsigned long deflated_frames;
  1295. pr_debug("%s - size: %llu", __func__, atomic64_read(&b->size));
  1296. vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_SHRINK);
  1297. /*
  1298. * If the lock is also contended for read, we cannot easily reclaim and
  1299. * we bail out.
  1300. */
  1301. if (!down_read_trylock(&b->conf_sem))
  1302. return 0;
  1303. deflated_frames = vmballoon_deflate(b, sc->nr_to_scan, true);
  1304. vmballoon_stats_gen_add(b, VMW_BALLOON_STAT_SHRINK_FREE,
  1305. deflated_frames);
  1306. /*
  1307. * Delay future inflation for some time to mitigate the situations in
  1308. * which balloon continuously grows and shrinks. Use WRITE_ONCE() since
  1309. * the access is asynchronous.
  1310. */
  1311. WRITE_ONCE(b->shrink_timeout, jiffies + HZ * VMBALLOON_SHRINK_DELAY);
  1312. up_read(&b->conf_sem);
  1313. return deflated_frames;
  1314. }
  1315. /**
  1316. * vmballoon_shrinker_count() - return the number of ballooned pages.
  1317. * @shrinker: pointer to the balloon shrinker.
  1318. * @sc: page reclaim information.
  1319. *
  1320. * Returns: number of 4k pages that are allocated for the balloon and can
  1321. * therefore be reclaimed under pressure.
  1322. */
  1323. static unsigned long vmballoon_shrinker_count(struct shrinker *shrinker,
  1324. struct shrink_control *sc)
  1325. {
  1326. struct vmballoon *b = &balloon;
  1327. return atomic64_read(&b->size);
  1328. }
  1329. static void vmballoon_unregister_shrinker(struct vmballoon *b)
  1330. {
  1331. shrinker_free(b->shrinker);
  1332. b->shrinker = NULL;
  1333. }
  1334. static int vmballoon_register_shrinker(struct vmballoon *b)
  1335. {
  1336. /* Do nothing if the shrinker is not enabled */
  1337. if (!vmwballoon_shrinker_enable)
  1338. return 0;
  1339. b->shrinker = shrinker_alloc(0, "vmw-balloon");
  1340. if (!b->shrinker)
  1341. return -ENOMEM;
  1342. b->shrinker->scan_objects = vmballoon_shrinker_scan;
  1343. b->shrinker->count_objects = vmballoon_shrinker_count;
  1344. b->shrinker->private_data = b;
  1345. shrinker_register(b->shrinker);
  1346. return 0;
  1347. }
  1348. /*
  1349. * DEBUGFS Interface
  1350. */
  1351. #ifdef CONFIG_DEBUG_FS
  1352. static const char * const vmballoon_stat_page_names[] = {
  1353. [VMW_BALLOON_PAGE_STAT_ALLOC] = "alloc",
  1354. [VMW_BALLOON_PAGE_STAT_ALLOC_FAIL] = "allocFail",
  1355. [VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC] = "errAlloc",
  1356. [VMW_BALLOON_PAGE_STAT_REFUSED_FREE] = "errFree",
  1357. [VMW_BALLOON_PAGE_STAT_FREE] = "free"
  1358. };
  1359. static const char * const vmballoon_stat_names[] = {
  1360. [VMW_BALLOON_STAT_TIMER] = "timer",
  1361. [VMW_BALLOON_STAT_DOORBELL] = "doorbell",
  1362. [VMW_BALLOON_STAT_RESET] = "reset",
  1363. [VMW_BALLOON_STAT_SHRINK] = "shrink",
  1364. [VMW_BALLOON_STAT_SHRINK_FREE] = "shrinkFree"
  1365. };
  1366. static int vmballoon_enable_stats(struct vmballoon *b)
  1367. {
  1368. int r = 0;
  1369. down_write(&b->conf_sem);
  1370. /* did we somehow race with another reader which enabled stats? */
  1371. if (b->stats)
  1372. goto out;
  1373. b->stats = kzalloc_obj(*b->stats);
  1374. if (!b->stats) {
  1375. /* allocation failed */
  1376. r = -ENOMEM;
  1377. goto out;
  1378. }
  1379. static_key_enable(&balloon_stat_enabled.key);
  1380. out:
  1381. up_write(&b->conf_sem);
  1382. return r;
  1383. }
  1384. /**
  1385. * vmballoon_debug_show - shows statistics of balloon operations.
  1386. * @f: pointer to the &struct seq_file.
  1387. * @offset: ignored.
  1388. *
  1389. * Provides the statistics that can be accessed in vmmemctl in the debugfs.
  1390. * To avoid the overhead - mainly that of memory - of collecting the statistics,
  1391. * we only collect statistics after the first time the counters are read.
  1392. *
  1393. * Return: zero on success or an error code.
  1394. */
  1395. static int vmballoon_debug_show(struct seq_file *f, void *offset)
  1396. {
  1397. struct vmballoon *b = f->private;
  1398. int i, j;
  1399. /* enables stats if they are disabled */
  1400. if (!b->stats) {
  1401. int r = vmballoon_enable_stats(b);
  1402. if (r)
  1403. return r;
  1404. }
  1405. /* format capabilities info */
  1406. seq_printf(f, "%-22s: %#16x\n", "balloon capabilities",
  1407. VMW_BALLOON_CAPABILITIES);
  1408. seq_printf(f, "%-22s: %#16lx\n", "used capabilities", b->capabilities);
  1409. seq_printf(f, "%-22s: %16s\n", "is resetting",
  1410. b->reset_required ? "y" : "n");
  1411. /* format size info */
  1412. seq_printf(f, "%-22s: %16lu\n", "target", READ_ONCE(b->target));
  1413. seq_printf(f, "%-22s: %16llu\n", "current", atomic64_read(&b->size));
  1414. for (i = 0; i < VMW_BALLOON_CMD_NUM; i++) {
  1415. if (vmballoon_cmd_names[i] == NULL)
  1416. continue;
  1417. seq_printf(f, "%-22s: %16llu (%llu failed)\n",
  1418. vmballoon_cmd_names[i],
  1419. atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_STAT]),
  1420. atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_FAIL_STAT]));
  1421. }
  1422. for (i = 0; i < VMW_BALLOON_STAT_NUM; i++)
  1423. seq_printf(f, "%-22s: %16llu\n",
  1424. vmballoon_stat_names[i],
  1425. atomic64_read(&b->stats->general_stat[i]));
  1426. for (i = 0; i < VMW_BALLOON_PAGE_STAT_NUM; i++) {
  1427. for (j = 0; j < VMW_BALLOON_NUM_PAGE_SIZES; j++)
  1428. seq_printf(f, "%-18s(%s): %16llu\n",
  1429. vmballoon_stat_page_names[i],
  1430. vmballoon_page_size_names[j],
  1431. atomic64_read(&b->stats->page_stat[i][j]));
  1432. }
  1433. return 0;
  1434. }
  1435. DEFINE_SHOW_ATTRIBUTE(vmballoon_debug);
  1436. static void __init vmballoon_debugfs_init(struct vmballoon *b)
  1437. {
  1438. debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
  1439. &vmballoon_debug_fops);
  1440. }
  1441. static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
  1442. {
  1443. static_key_disable(&balloon_stat_enabled.key);
  1444. debugfs_lookup_and_remove("vmmemctl", NULL);
  1445. kfree(b->stats);
  1446. b->stats = NULL;
  1447. }
  1448. #else
  1449. static inline void vmballoon_debugfs_init(struct vmballoon *b)
  1450. {
  1451. }
  1452. static inline void vmballoon_debugfs_exit(struct vmballoon *b)
  1453. {
  1454. }
  1455. #endif /* CONFIG_DEBUG_FS */
  1456. #ifdef CONFIG_BALLOON_MIGRATION
  1457. /**
  1458. * vmballoon_migratepage() - migrates a balloon page.
  1459. * @b_dev_info: balloon device information descriptor.
  1460. * @newpage: the page to which @page should be migrated.
  1461. * @page: a ballooned page that should be migrated.
  1462. * @mode: migration mode, ignored.
  1463. *
  1464. * Return: zero on success, -EAGAIN when migration cannot be performed
  1465. * momentarily, -EBUSY if migration failed and should be retried
  1466. * with that specific page, and -ENOENT when deflating @page
  1467. * succeeded but inflating @newpage failed, effectively deflating
  1468. * the balloon.
  1469. */
  1470. static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
  1471. struct page *newpage, struct page *page,
  1472. enum migrate_mode mode)
  1473. {
  1474. unsigned long status;
  1475. struct vmballoon *b;
  1476. int ret = 0;
  1477. b = container_of(b_dev_info, struct vmballoon, b_dev_info);
  1478. /*
  1479. * If the semaphore is taken, there is ongoing configuration change
  1480. * (i.e., balloon reset), so try again.
  1481. */
  1482. if (!down_read_trylock(&b->conf_sem))
  1483. return -EAGAIN;
  1484. spin_lock(&b->comm_lock);
  1485. /*
  1486. * We must start by deflating and not inflating, as otherwise the
  1487. * hypervisor may tell us that it has enough memory and the new page is
  1488. * not needed. Since the old page is isolated, we cannot use the list
  1489. * interface to unlock it, as the LRU field is used for isolation.
  1490. * Instead, we use the native interface directly.
  1491. */
  1492. vmballoon_add_page(b, 0, page);
  1493. status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
  1494. VMW_BALLOON_DEFLATE);
  1495. if (status == VMW_BALLOON_SUCCESS)
  1496. status = vmballoon_status_page(b, 0, &page);
  1497. /*
  1498. * If a failure happened, let the migration mechanism know that it
  1499. * should not retry.
  1500. */
  1501. if (status != VMW_BALLOON_SUCCESS) {
  1502. spin_unlock(&b->comm_lock);
  1503. ret = -EBUSY;
  1504. goto out_unlock;
  1505. }
  1506. /* Inflate */
  1507. vmballoon_add_page(b, 0, newpage);
  1508. status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
  1509. VMW_BALLOON_INFLATE);
  1510. if (status == VMW_BALLOON_SUCCESS)
  1511. status = vmballoon_status_page(b, 0, &newpage);
  1512. spin_unlock(&b->comm_lock);
  1513. if (status != VMW_BALLOON_SUCCESS) {
  1514. /*
  1515. * A failure happened. While we can deflate the page we just
  1516. * inflated, this deflation can also encounter an error. Instead
  1517. * we will decrease the size of the balloon to reflect the
  1518. * change.
  1519. */
  1520. atomic64_dec(&b->size);
  1521. /*
  1522. * Tell the core that we're deflating the old page and don't
  1523. * need the new page.
  1524. */
  1525. ret = -ENOENT;
  1526. }
  1527. out_unlock:
  1528. up_read(&b->conf_sem);
  1529. return ret;
  1530. }
  1531. #else /* CONFIG_BALLOON_MIGRATION */
  1532. int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
  1533. struct page *newpage, struct page *page,
  1534. enum migrate_mode mode);
  1535. #endif /* CONFIG_BALLOON_MIGRATION */
  1536. static int __init vmballoon_init(void)
  1537. {
  1538. int error;
  1539. /*
  1540. * Check if we are running on VMware's hypervisor and bail out
  1541. * if we are not.
  1542. */
  1543. if (x86_hyper_type != X86_HYPER_VMWARE)
  1544. return -ENODEV;
  1545. INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
  1546. error = vmballoon_register_shrinker(&balloon);
  1547. if (error)
  1548. return error;
  1549. balloon_devinfo_init(&balloon.b_dev_info);
  1550. if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))
  1551. balloon.b_dev_info.migratepage = vmballoon_migratepage;
  1552. INIT_LIST_HEAD(&balloon.huge_pages);
  1553. spin_lock_init(&balloon.huge_pages_lock);
  1554. spin_lock_init(&balloon.comm_lock);
  1555. init_rwsem(&balloon.conf_sem);
  1556. balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
  1557. balloon.batch_page = NULL;
  1558. balloon.page = NULL;
  1559. balloon.reset_required = true;
  1560. queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
  1561. vmballoon_debugfs_init(&balloon);
  1562. return 0;
  1563. }
  1564. /*
  1565. * Using late_initcall() instead of module_init() allows the balloon to use the
  1566. * VMCI doorbell even when the balloon is built into the kernel. Otherwise the
  1567. * VMCI is probed only after the balloon is initialized. If the balloon is used
  1568. * as a module, late_initcall() is equivalent to module_init().
  1569. */
  1570. late_initcall(vmballoon_init);
  1571. static void __exit vmballoon_exit(void)
  1572. {
  1573. vmballoon_unregister_shrinker(&balloon);
  1574. vmballoon_vmci_cleanup(&balloon);
  1575. cancel_delayed_work_sync(&balloon.dwork);
  1576. vmballoon_debugfs_exit(&balloon);
  1577. /*
  1578. * Deallocate all reserved memory, and reset connection with monitor.
  1579. * Reset connection before deallocating memory to avoid potential for
  1580. * additional spurious resets from guest touching deallocated pages.
  1581. */
  1582. vmballoon_send_start(&balloon, 0);
  1583. vmballoon_pop(&balloon);
  1584. }
  1585. module_exit(vmballoon_exit);