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