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