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