1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37#include <linux/types.h>
38#include <linux/io.h>
39#include <linux/kernel.h>
40#include <linux/mm.h>
41#include <linux/vmalloc.h>
42#include <linux/sched.h>
43#include <linux/module.h>
44#include <linux/workqueue.h>
45#include <linux/debugfs.h>
46#include <linux/seq_file.h>
47#include <linux/vmw_vmci_defs.h>
48#include <linux/vmw_vmci_api.h>
49#include <asm/hypervisor.h>
50
51MODULE_AUTHOR("VMware, Inc.");
52MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
53MODULE_VERSION("1.5.0.0-k");
54MODULE_ALIAS("dmi:*:svnVMware*:*");
55MODULE_ALIAS("vmware_vmmemctl");
56MODULE_LICENSE("GPL");
57
58
59
60
61
62
63
64
65
66
67#define VMW_BALLOON_RATE_ALLOC_MIN 512U
68#define VMW_BALLOON_RATE_ALLOC_MAX 2048U
69#define VMW_BALLOON_RATE_ALLOC_INC 16U
70
71
72
73
74
75#define VMW_BALLOON_SLOW_CYCLES 4
76
77
78
79
80
81
82#define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
83
84
85
86
87
88
89
90
91#define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
92
93
94#define VMW_BALLOON_MAX_REFUSED 16
95
96
97
98
99#define VMW_BALLOON_HV_PORT 0x5670
100#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
101#define VMW_BALLOON_GUEST_ID 1
102
103enum vmwballoon_capabilities {
104
105
106
107 VMW_BALLOON_BASIC_CMDS = (1 << 1),
108 VMW_BALLOON_BATCHED_CMDS = (1 << 2),
109 VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
110 VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
111};
112
113#define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_BASIC_CMDS \
114 | VMW_BALLOON_BATCHED_CMDS \
115 | VMW_BALLOON_BATCHED_2M_CMDS \
116 | VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
117
118#define VMW_BALLOON_2M_SHIFT (9)
119#define VMW_BALLOON_NUM_PAGE_SIZES (2)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135#define VMW_BALLOON_CMD_START 0
136#define VMW_BALLOON_CMD_GET_TARGET 1
137#define VMW_BALLOON_CMD_LOCK 2
138#define VMW_BALLOON_CMD_UNLOCK 3
139#define VMW_BALLOON_CMD_GUEST_ID 4
140#define VMW_BALLOON_CMD_BATCHED_LOCK 6
141#define VMW_BALLOON_CMD_BATCHED_UNLOCK 7
142#define VMW_BALLOON_CMD_BATCHED_2M_LOCK 8
143#define VMW_BALLOON_CMD_BATCHED_2M_UNLOCK 9
144#define VMW_BALLOON_CMD_VMCI_DOORBELL_SET 10
145
146
147
148#define VMW_BALLOON_SUCCESS 0
149#define VMW_BALLOON_FAILURE -1
150#define VMW_BALLOON_ERROR_CMD_INVALID 1
151#define VMW_BALLOON_ERROR_PPN_INVALID 2
152#define VMW_BALLOON_ERROR_PPN_LOCKED 3
153#define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
154#define VMW_BALLOON_ERROR_PPN_PINNED 5
155#define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
156#define VMW_BALLOON_ERROR_RESET 7
157#define VMW_BALLOON_ERROR_BUSY 8
158
159#define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175#define VMW_BALLOON_BATCH_MAX_PAGES (PAGE_SIZE / sizeof(u64))
176#define VMW_BALLOON_BATCH_STATUS_MASK ((1UL << 5) - 1)
177#define VMW_BALLOON_BATCH_PAGE_MASK (~((1UL << PAGE_SHIFT) - 1))
178
179struct vmballoon_batch_page {
180 u64 pages[VMW_BALLOON_BATCH_MAX_PAGES];
181};
182
183static u64 vmballoon_batch_get_pa(struct vmballoon_batch_page *batch, int idx)
184{
185 return batch->pages[idx] & VMW_BALLOON_BATCH_PAGE_MASK;
186}
187
188static int vmballoon_batch_get_status(struct vmballoon_batch_page *batch,
189 int idx)
190{
191 return (int)(batch->pages[idx] & VMW_BALLOON_BATCH_STATUS_MASK);
192}
193
194static void vmballoon_batch_set_pa(struct vmballoon_batch_page *batch, int idx,
195 u64 pa)
196{
197 batch->pages[idx] = pa;
198}
199
200
201#define VMWARE_BALLOON_CMD(cmd, arg1, arg2, result) \
202({ \
203 unsigned long __status, __dummy1, __dummy2, __dummy3; \
204 __asm__ __volatile__ ("inl %%dx" : \
205 "=a"(__status), \
206 "=c"(__dummy1), \
207 "=d"(__dummy2), \
208 "=b"(result), \
209 "=S" (__dummy3) : \
210 "0"(VMW_BALLOON_HV_MAGIC), \
211 "1"(VMW_BALLOON_CMD_##cmd), \
212 "2"(VMW_BALLOON_HV_PORT), \
213 "3"(arg1), \
214 "4" (arg2) : \
215 "memory"); \
216 if (VMW_BALLOON_CMD_##cmd == VMW_BALLOON_CMD_START) \
217 result = __dummy1; \
218 result &= -1UL; \
219 __status & -1UL; \
220})
221
222#ifdef CONFIG_DEBUG_FS
223struct vmballoon_stats {
224 unsigned int timer;
225 unsigned int doorbell;
226
227
228 unsigned int alloc[VMW_BALLOON_NUM_PAGE_SIZES];
229 unsigned int alloc_fail[VMW_BALLOON_NUM_PAGE_SIZES];
230 unsigned int sleep_alloc;
231 unsigned int sleep_alloc_fail;
232 unsigned int refused_alloc[VMW_BALLOON_NUM_PAGE_SIZES];
233 unsigned int refused_free[VMW_BALLOON_NUM_PAGE_SIZES];
234 unsigned int free[VMW_BALLOON_NUM_PAGE_SIZES];
235
236
237 unsigned int lock[VMW_BALLOON_NUM_PAGE_SIZES];
238 unsigned int lock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
239 unsigned int unlock[VMW_BALLOON_NUM_PAGE_SIZES];
240 unsigned int unlock_fail[VMW_BALLOON_NUM_PAGE_SIZES];
241 unsigned int target;
242 unsigned int target_fail;
243 unsigned int start;
244 unsigned int start_fail;
245 unsigned int guest_type;
246 unsigned int guest_type_fail;
247 unsigned int doorbell_set;
248 unsigned int doorbell_unset;
249};
250
251#define STATS_INC(stat) (stat)++
252#else
253#define STATS_INC(stat)
254#endif
255
256struct vmballoon;
257
258struct vmballoon_ops {
259 void (*add_page)(struct vmballoon *b, int idx, struct page *p);
260 int (*lock)(struct vmballoon *b, unsigned int num_pages,
261 bool is_2m_pages, unsigned int *target);
262 int (*unlock)(struct vmballoon *b, unsigned int num_pages,
263 bool is_2m_pages, unsigned int *target);
264};
265
266struct vmballoon_page_size {
267
268 struct list_head pages;
269
270
271 struct list_head refused_pages;
272 unsigned int n_refused_pages;
273};
274
275struct vmballoon {
276 struct vmballoon_page_size page_sizes[VMW_BALLOON_NUM_PAGE_SIZES];
277
278
279 unsigned supported_page_sizes;
280
281
282 unsigned int size;
283 unsigned int target;
284
285
286 bool reset_required;
287
288
289 unsigned int rate_alloc;
290
291
292 unsigned int slow_allocation_cycles;
293
294 unsigned long capabilities;
295
296 struct vmballoon_batch_page *batch_page;
297 unsigned int batch_max_pages;
298 struct page *page;
299
300 const struct vmballoon_ops *ops;
301
302#ifdef CONFIG_DEBUG_FS
303
304 struct vmballoon_stats stats;
305
306
307 struct dentry *dbg_entry;
308#endif
309
310 struct sysinfo sysinfo;
311
312 struct delayed_work dwork;
313
314 struct vmci_handle vmci_doorbell;
315};
316
317static struct vmballoon balloon;
318
319
320
321
322
323static bool vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
324{
325 unsigned long status, capabilities, dummy = 0;
326 bool success;
327
328 STATS_INC(b->stats.start);
329
330 status = VMWARE_BALLOON_CMD(START, req_caps, dummy, capabilities);
331
332 switch (status) {
333 case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
334 b->capabilities = capabilities;
335 success = true;
336 break;
337 case VMW_BALLOON_SUCCESS:
338 b->capabilities = VMW_BALLOON_BASIC_CMDS;
339 success = true;
340 break;
341 default:
342 success = false;
343 }
344
345 if (b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS)
346 b->supported_page_sizes = 2;
347 else
348 b->supported_page_sizes = 1;
349
350 if (!success) {
351 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
352 STATS_INC(b->stats.start_fail);
353 }
354 return success;
355}
356
357static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
358{
359 switch (status) {
360 case VMW_BALLOON_SUCCESS:
361 return true;
362
363 case VMW_BALLOON_ERROR_RESET:
364 b->reset_required = true;
365
366
367 default:
368 return false;
369 }
370}
371
372
373
374
375
376
377
378static bool vmballoon_send_guest_id(struct vmballoon *b)
379{
380 unsigned long status, dummy = 0;
381
382 status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy,
383 dummy);
384
385 STATS_INC(b->stats.guest_type);
386
387 if (vmballoon_check_status(b, status))
388 return true;
389
390 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
391 STATS_INC(b->stats.guest_type_fail);
392 return false;
393}
394
395static u16 vmballoon_page_size(bool is_2m_page)
396{
397 if (is_2m_page)
398 return 1 << VMW_BALLOON_2M_SHIFT;
399
400 return 1;
401}
402
403
404
405
406static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
407{
408 unsigned long status;
409 unsigned long target;
410 unsigned long limit;
411 unsigned long dummy = 0;
412 u32 limit32;
413
414
415
416
417
418
419 si_meminfo(&b->sysinfo);
420 limit = b->sysinfo.totalram;
421
422
423 limit32 = (u32)limit;
424 if (limit != limit32)
425 return false;
426
427
428 STATS_INC(b->stats.target);
429
430 status = VMWARE_BALLOON_CMD(GET_TARGET, limit, dummy, target);
431 if (vmballoon_check_status(b, status)) {
432 *new_target = target;
433 return true;
434 }
435
436 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
437 STATS_INC(b->stats.target_fail);
438 return false;
439}
440
441
442
443
444
445
446static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
447 unsigned int *hv_status, unsigned int *target)
448{
449 unsigned long status, dummy = 0;
450 u32 pfn32;
451
452 pfn32 = (u32)pfn;
453 if (pfn32 != pfn)
454 return -1;
455
456 STATS_INC(b->stats.lock[false]);
457
458 *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy, *target);
459 if (vmballoon_check_status(b, status))
460 return 0;
461
462 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
463 STATS_INC(b->stats.lock_fail[false]);
464 return 1;
465}
466
467static int vmballoon_send_batched_lock(struct vmballoon *b,
468 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
469{
470 unsigned long status;
471 unsigned long pfn = PHYS_PFN(virt_to_phys(b->batch_page));
472
473 STATS_INC(b->stats.lock[is_2m_pages]);
474
475 if (is_2m_pages)
476 status = VMWARE_BALLOON_CMD(BATCHED_2M_LOCK, pfn, num_pages,
477 *target);
478 else
479 status = VMWARE_BALLOON_CMD(BATCHED_LOCK, pfn, num_pages,
480 *target);
481
482 if (vmballoon_check_status(b, status))
483 return 0;
484
485 pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
486 STATS_INC(b->stats.lock_fail[is_2m_pages]);
487 return 1;
488}
489
490
491
492
493
494static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn,
495 unsigned int *target)
496{
497 unsigned long status, dummy = 0;
498 u32 pfn32;
499
500 pfn32 = (u32)pfn;
501 if (pfn32 != pfn)
502 return false;
503
504 STATS_INC(b->stats.unlock[false]);
505
506 status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy, *target);
507 if (vmballoon_check_status(b, status))
508 return true;
509
510 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
511 STATS_INC(b->stats.unlock_fail[false]);
512 return false;
513}
514
515static bool vmballoon_send_batched_unlock(struct vmballoon *b,
516 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
517{
518 unsigned long status;
519 unsigned long pfn = PHYS_PFN(virt_to_phys(b->batch_page));
520
521 STATS_INC(b->stats.unlock[is_2m_pages]);
522
523 if (is_2m_pages)
524 status = VMWARE_BALLOON_CMD(BATCHED_2M_UNLOCK, pfn, num_pages,
525 *target);
526 else
527 status = VMWARE_BALLOON_CMD(BATCHED_UNLOCK, pfn, num_pages,
528 *target);
529
530 if (vmballoon_check_status(b, status))
531 return true;
532
533 pr_debug("%s - batch ppn %lx, hv returns %ld\n", __func__, pfn, status);
534 STATS_INC(b->stats.unlock_fail[is_2m_pages]);
535 return false;
536}
537
538static struct page *vmballoon_alloc_page(gfp_t flags, bool is_2m_page)
539{
540 if (is_2m_page)
541 return alloc_pages(flags, VMW_BALLOON_2M_SHIFT);
542
543 return alloc_page(flags);
544}
545
546static void vmballoon_free_page(struct page *page, bool is_2m_page)
547{
548 if (is_2m_page)
549 __free_pages(page, VMW_BALLOON_2M_SHIFT);
550 else
551 __free_page(page);
552}
553
554
555
556
557
558
559
560static void vmballoon_pop(struct vmballoon *b)
561{
562 struct page *page, *next;
563 unsigned is_2m_pages;
564
565 for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
566 is_2m_pages++) {
567 struct vmballoon_page_size *page_size =
568 &b->page_sizes[is_2m_pages];
569 u16 size_per_page = vmballoon_page_size(is_2m_pages);
570
571 list_for_each_entry_safe(page, next, &page_size->pages, lru) {
572 list_del(&page->lru);
573 vmballoon_free_page(page, is_2m_pages);
574 STATS_INC(b->stats.free[is_2m_pages]);
575 b->size -= size_per_page;
576 cond_resched();
577 }
578 }
579
580
581 free_page((unsigned long)b->batch_page);
582 b->batch_page = NULL;
583}
584
585
586
587
588
589
590static int vmballoon_lock_page(struct vmballoon *b, unsigned int num_pages,
591 bool is_2m_pages, unsigned int *target)
592{
593 int locked, hv_status;
594 struct page *page = b->page;
595 struct vmballoon_page_size *page_size = &b->page_sizes[false];
596
597
598
599 locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status,
600 target);
601 if (locked > 0) {
602 STATS_INC(b->stats.refused_alloc[false]);
603
604 if (hv_status == VMW_BALLOON_ERROR_RESET ||
605 hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) {
606 vmballoon_free_page(page, false);
607 return -EIO;
608 }
609
610
611
612
613
614
615 if (page_size->n_refused_pages < VMW_BALLOON_MAX_REFUSED) {
616 page_size->n_refused_pages++;
617 list_add(&page->lru, &page_size->refused_pages);
618 } else {
619 vmballoon_free_page(page, false);
620 }
621 return -EIO;
622 }
623
624
625 list_add(&page->lru, &page_size->pages);
626
627
628 b->size++;
629
630 return 0;
631}
632
633static int vmballoon_lock_batched_page(struct vmballoon *b,
634 unsigned int num_pages, bool is_2m_pages, unsigned int *target)
635{
636 int locked, i;
637 u16 size_per_page = vmballoon_page_size(is_2m_pages);
638
639 locked = vmballoon_send_batched_lock(b, num_pages, is_2m_pages,
640 target);
641 if (locked > 0) {
642 for (i = 0; i < num_pages; i++) {
643 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
644 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
645
646 vmballoon_free_page(p, is_2m_pages);
647 }
648
649 return -EIO;
650 }
651
652 for (i = 0; i < num_pages; i++) {
653 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
654 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
655 struct vmballoon_page_size *page_size =
656 &b->page_sizes[is_2m_pages];
657
658 locked = vmballoon_batch_get_status(b->batch_page, i);
659
660 switch (locked) {
661 case VMW_BALLOON_SUCCESS:
662 list_add(&p->lru, &page_size->pages);
663 b->size += size_per_page;
664 break;
665 case VMW_BALLOON_ERROR_PPN_PINNED:
666 case VMW_BALLOON_ERROR_PPN_INVALID:
667 if (page_size->n_refused_pages
668 < VMW_BALLOON_MAX_REFUSED) {
669 list_add(&p->lru, &page_size->refused_pages);
670 page_size->n_refused_pages++;
671 break;
672 }
673
674 case VMW_BALLOON_ERROR_RESET:
675 case VMW_BALLOON_ERROR_PPN_NOTNEEDED:
676 vmballoon_free_page(p, is_2m_pages);
677 break;
678 default:
679
680 WARN_ON_ONCE(true);
681 }
682 }
683
684 return 0;
685}
686
687
688
689
690
691
692static int vmballoon_unlock_page(struct vmballoon *b, unsigned int num_pages,
693 bool is_2m_pages, unsigned int *target)
694{
695 struct page *page = b->page;
696 struct vmballoon_page_size *page_size = &b->page_sizes[false];
697
698
699
700 if (!vmballoon_send_unlock_page(b, page_to_pfn(page), target)) {
701 list_add(&page->lru, &page_size->pages);
702 return -EIO;
703 }
704
705
706 vmballoon_free_page(page, false);
707 STATS_INC(b->stats.free[false]);
708
709
710 b->size--;
711
712 return 0;
713}
714
715static int vmballoon_unlock_batched_page(struct vmballoon *b,
716 unsigned int num_pages, bool is_2m_pages,
717 unsigned int *target)
718{
719 int locked, i, ret = 0;
720 bool hv_success;
721 u16 size_per_page = vmballoon_page_size(is_2m_pages);
722
723 hv_success = vmballoon_send_batched_unlock(b, num_pages, is_2m_pages,
724 target);
725 if (!hv_success)
726 ret = -EIO;
727
728 for (i = 0; i < num_pages; i++) {
729 u64 pa = vmballoon_batch_get_pa(b->batch_page, i);
730 struct page *p = pfn_to_page(pa >> PAGE_SHIFT);
731 struct vmballoon_page_size *page_size =
732 &b->page_sizes[is_2m_pages];
733
734 locked = vmballoon_batch_get_status(b->batch_page, i);
735 if (!hv_success || locked != VMW_BALLOON_SUCCESS) {
736
737
738
739
740
741 list_add(&p->lru, &page_size->pages);
742 } else {
743
744 vmballoon_free_page(p, is_2m_pages);
745 STATS_INC(b->stats.free[is_2m_pages]);
746
747
748 b->size -= size_per_page;
749 }
750 }
751
752 return ret;
753}
754
755
756
757
758
759static void vmballoon_release_refused_pages(struct vmballoon *b,
760 bool is_2m_pages)
761{
762 struct page *page, *next;
763 struct vmballoon_page_size *page_size =
764 &b->page_sizes[is_2m_pages];
765
766 list_for_each_entry_safe(page, next, &page_size->refused_pages, lru) {
767 list_del(&page->lru);
768 vmballoon_free_page(page, is_2m_pages);
769 STATS_INC(b->stats.refused_free[is_2m_pages]);
770 }
771
772 page_size->n_refused_pages = 0;
773}
774
775static void vmballoon_add_page(struct vmballoon *b, int idx, struct page *p)
776{
777 b->page = p;
778}
779
780static void vmballoon_add_batched_page(struct vmballoon *b, int idx,
781 struct page *p)
782{
783 vmballoon_batch_set_pa(b->batch_page, idx,
784 (u64)page_to_pfn(p) << PAGE_SHIFT);
785}
786
787
788
789
790
791
792static void vmballoon_inflate(struct vmballoon *b)
793{
794 unsigned rate;
795 unsigned int allocations = 0;
796 unsigned int num_pages = 0;
797 int error = 0;
798 gfp_t flags = VMW_PAGE_ALLOC_NOSLEEP;
799 bool is_2m_pages;
800
801 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822 if (b->slow_allocation_cycles) {
823 rate = b->rate_alloc;
824 is_2m_pages = false;
825 } else {
826 rate = UINT_MAX;
827 is_2m_pages =
828 b->supported_page_sizes == VMW_BALLOON_NUM_PAGE_SIZES;
829 }
830
831 pr_debug("%s - goal: %d, no-sleep rate: %u, sleep rate: %d\n",
832 __func__, b->target - b->size, rate, b->rate_alloc);
833
834 while (!b->reset_required &&
835 b->size + num_pages * vmballoon_page_size(is_2m_pages)
836 < b->target) {
837 struct page *page;
838
839 if (flags == VMW_PAGE_ALLOC_NOSLEEP)
840 STATS_INC(b->stats.alloc[is_2m_pages]);
841 else
842 STATS_INC(b->stats.sleep_alloc);
843
844 page = vmballoon_alloc_page(flags, is_2m_pages);
845 if (!page) {
846 STATS_INC(b->stats.alloc_fail[is_2m_pages]);
847
848 if (is_2m_pages) {
849 b->ops->lock(b, num_pages, true, &b->target);
850
851
852
853
854
855
856
857 num_pages = 0;
858 is_2m_pages = false;
859 continue;
860 }
861
862 if (flags == VMW_PAGE_ALLOC_CANSLEEP) {
863
864
865
866
867
868 b->rate_alloc = max(b->rate_alloc / 2,
869 VMW_BALLOON_RATE_ALLOC_MIN);
870 STATS_INC(b->stats.sleep_alloc_fail);
871 break;
872 }
873
874
875
876
877
878
879
880
881
882 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
883
884 if (allocations >= b->rate_alloc)
885 break;
886
887 flags = VMW_PAGE_ALLOC_CANSLEEP;
888
889 rate = b->rate_alloc;
890 continue;
891 }
892
893 b->ops->add_page(b, num_pages++, page);
894 if (num_pages == b->batch_max_pages) {
895 error = b->ops->lock(b, num_pages, is_2m_pages,
896 &b->target);
897 num_pages = 0;
898 if (error)
899 break;
900 }
901
902 cond_resched();
903
904 if (allocations >= rate) {
905
906 break;
907 }
908 }
909
910 if (num_pages > 0)
911 b->ops->lock(b, num_pages, is_2m_pages, &b->target);
912
913
914
915
916
917 if (error == 0 && allocations >= b->rate_alloc) {
918 unsigned int mult = allocations / b->rate_alloc;
919
920 b->rate_alloc =
921 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
922 VMW_BALLOON_RATE_ALLOC_MAX);
923 }
924
925 vmballoon_release_refused_pages(b, true);
926 vmballoon_release_refused_pages(b, false);
927}
928
929
930
931
932static void vmballoon_deflate(struct vmballoon *b)
933{
934 unsigned is_2m_pages;
935
936 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
937
938
939 for (is_2m_pages = 0; is_2m_pages < b->supported_page_sizes;
940 is_2m_pages++) {
941 struct page *page, *next;
942 unsigned int num_pages = 0;
943 struct vmballoon_page_size *page_size =
944 &b->page_sizes[is_2m_pages];
945
946 list_for_each_entry_safe(page, next, &page_size->pages, lru) {
947 if (b->reset_required ||
948 (b->target > 0 &&
949 b->size - num_pages
950 * vmballoon_page_size(is_2m_pages)
951 < b->target + vmballoon_page_size(true)))
952 break;
953
954 list_del(&page->lru);
955 b->ops->add_page(b, num_pages++, page);
956
957 if (num_pages == b->batch_max_pages) {
958 int error;
959
960 error = b->ops->unlock(b, num_pages,
961 is_2m_pages, &b->target);
962 num_pages = 0;
963 if (error)
964 return;
965 }
966
967 cond_resched();
968 }
969
970 if (num_pages > 0)
971 b->ops->unlock(b, num_pages, is_2m_pages, &b->target);
972 }
973}
974
975static const struct vmballoon_ops vmballoon_basic_ops = {
976 .add_page = vmballoon_add_page,
977 .lock = vmballoon_lock_page,
978 .unlock = vmballoon_unlock_page
979};
980
981static const struct vmballoon_ops vmballoon_batched_ops = {
982 .add_page = vmballoon_add_batched_page,
983 .lock = vmballoon_lock_batched_page,
984 .unlock = vmballoon_unlock_batched_page
985};
986
987static bool vmballoon_init_batching(struct vmballoon *b)
988{
989 struct page *page;
990
991 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
992 if (!page)
993 return false;
994
995 b->batch_page = page_address(page);
996 return true;
997}
998
999
1000
1001
1002static void vmballoon_doorbell(void *client_data)
1003{
1004 struct vmballoon *b = client_data;
1005
1006 STATS_INC(b->stats.doorbell);
1007
1008 mod_delayed_work(system_freezable_wq, &b->dwork, 0);
1009}
1010
1011
1012
1013
1014static void vmballoon_vmci_cleanup(struct vmballoon *b)
1015{
1016 int error;
1017
1018 VMWARE_BALLOON_CMD(VMCI_DOORBELL_SET, VMCI_INVALID_ID,
1019 VMCI_INVALID_ID, error);
1020 STATS_INC(b->stats.doorbell_unset);
1021
1022 if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
1023 vmci_doorbell_destroy(b->vmci_doorbell);
1024 b->vmci_doorbell = VMCI_INVALID_HANDLE;
1025 }
1026}
1027
1028
1029
1030
1031static int vmballoon_vmci_init(struct vmballoon *b)
1032{
1033 int error = 0;
1034
1035 if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) != 0) {
1036 error = vmci_doorbell_create(&b->vmci_doorbell,
1037 VMCI_FLAG_DELAYED_CB,
1038 VMCI_PRIVILEGE_FLAG_RESTRICTED,
1039 vmballoon_doorbell, b);
1040
1041 if (error == VMCI_SUCCESS) {
1042 VMWARE_BALLOON_CMD(VMCI_DOORBELL_SET,
1043 b->vmci_doorbell.context,
1044 b->vmci_doorbell.resource, error);
1045 STATS_INC(b->stats.doorbell_set);
1046 }
1047 }
1048
1049 if (error != 0) {
1050 vmballoon_vmci_cleanup(b);
1051
1052 return -EIO;
1053 }
1054
1055 return 0;
1056}
1057
1058
1059
1060
1061
1062
1063static void vmballoon_reset(struct vmballoon *b)
1064{
1065 int error;
1066
1067 vmballoon_vmci_cleanup(b);
1068
1069
1070 vmballoon_pop(b);
1071
1072 if (!vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
1073 return;
1074
1075 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
1076 b->ops = &vmballoon_batched_ops;
1077 b->batch_max_pages = VMW_BALLOON_BATCH_MAX_PAGES;
1078 if (!vmballoon_init_batching(b)) {
1079
1080
1081
1082
1083
1084
1085 vmballoon_send_start(b, 0);
1086 return;
1087 }
1088 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
1089 b->ops = &vmballoon_basic_ops;
1090 b->batch_max_pages = 1;
1091 }
1092
1093 b->reset_required = false;
1094
1095 error = vmballoon_vmci_init(b);
1096 if (error)
1097 pr_err("failed to initialize vmci doorbell\n");
1098
1099 if (!vmballoon_send_guest_id(b))
1100 pr_err("failed to send guest ID to the host\n");
1101}
1102
1103
1104
1105
1106
1107static void vmballoon_work(struct work_struct *work)
1108{
1109 struct delayed_work *dwork = to_delayed_work(work);
1110 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
1111 unsigned int target;
1112
1113 STATS_INC(b->stats.timer);
1114
1115 if (b->reset_required)
1116 vmballoon_reset(b);
1117
1118 if (b->slow_allocation_cycles > 0)
1119 b->slow_allocation_cycles--;
1120
1121 if (!b->reset_required && vmballoon_send_get_target(b, &target)) {
1122
1123 b->target = target;
1124
1125 if (b->size < target)
1126 vmballoon_inflate(b);
1127 else if (target == 0 ||
1128 b->size > target + vmballoon_page_size(true))
1129 vmballoon_deflate(b);
1130 }
1131
1132
1133
1134
1135
1136 queue_delayed_work(system_freezable_wq,
1137 dwork, round_jiffies_relative(HZ));
1138}
1139
1140
1141
1142
1143#ifdef CONFIG_DEBUG_FS
1144
1145static int vmballoon_debug_show(struct seq_file *f, void *offset)
1146{
1147 struct vmballoon *b = f->private;
1148 struct vmballoon_stats *stats = &b->stats;
1149
1150
1151 seq_printf(f,
1152 "balloon capabilities: %#4x\n"
1153 "used capabilities: %#4lx\n"
1154 "is resetting: %c\n",
1155 VMW_BALLOON_CAPABILITIES, b->capabilities,
1156 b->reset_required ? 'y' : 'n');
1157
1158
1159 seq_printf(f,
1160 "target: %8d pages\n"
1161 "current: %8d pages\n",
1162 b->target, b->size);
1163
1164
1165 seq_printf(f,
1166 "rateSleepAlloc: %8d pages/sec\n",
1167 b->rate_alloc);
1168
1169 seq_printf(f,
1170 "\n"
1171 "timer: %8u\n"
1172 "doorbell: %8u\n"
1173 "start: %8u (%4u failed)\n"
1174 "guestType: %8u (%4u failed)\n"
1175 "2m-lock: %8u (%4u failed)\n"
1176 "lock: %8u (%4u failed)\n"
1177 "2m-unlock: %8u (%4u failed)\n"
1178 "unlock: %8u (%4u failed)\n"
1179 "target: %8u (%4u failed)\n"
1180 "prim2mAlloc: %8u (%4u failed)\n"
1181 "primNoSleepAlloc: %8u (%4u failed)\n"
1182 "primCanSleepAlloc: %8u (%4u failed)\n"
1183 "prim2mFree: %8u\n"
1184 "primFree: %8u\n"
1185 "err2mAlloc: %8u\n"
1186 "errAlloc: %8u\n"
1187 "err2mFree: %8u\n"
1188 "errFree: %8u\n"
1189 "doorbellSet: %8u\n"
1190 "doorbellUnset: %8u\n",
1191 stats->timer,
1192 stats->doorbell,
1193 stats->start, stats->start_fail,
1194 stats->guest_type, stats->guest_type_fail,
1195 stats->lock[true], stats->lock_fail[true],
1196 stats->lock[false], stats->lock_fail[false],
1197 stats->unlock[true], stats->unlock_fail[true],
1198 stats->unlock[false], stats->unlock_fail[false],
1199 stats->target, stats->target_fail,
1200 stats->alloc[true], stats->alloc_fail[true],
1201 stats->alloc[false], stats->alloc_fail[false],
1202 stats->sleep_alloc, stats->sleep_alloc_fail,
1203 stats->free[true],
1204 stats->free[false],
1205 stats->refused_alloc[true], stats->refused_alloc[false],
1206 stats->refused_free[true], stats->refused_free[false],
1207 stats->doorbell_set, stats->doorbell_unset);
1208
1209 return 0;
1210}
1211
1212static int vmballoon_debug_open(struct inode *inode, struct file *file)
1213{
1214 return single_open(file, vmballoon_debug_show, inode->i_private);
1215}
1216
1217static const struct file_operations vmballoon_debug_fops = {
1218 .owner = THIS_MODULE,
1219 .open = vmballoon_debug_open,
1220 .read = seq_read,
1221 .llseek = seq_lseek,
1222 .release = single_release,
1223};
1224
1225static int __init vmballoon_debugfs_init(struct vmballoon *b)
1226{
1227 int error;
1228
1229 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1230 &vmballoon_debug_fops);
1231 if (IS_ERR(b->dbg_entry)) {
1232 error = PTR_ERR(b->dbg_entry);
1233 pr_err("failed to create debugfs entry, error: %d\n", error);
1234 return error;
1235 }
1236
1237 return 0;
1238}
1239
1240static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1241{
1242 debugfs_remove(b->dbg_entry);
1243}
1244
1245#else
1246
1247static inline int vmballoon_debugfs_init(struct vmballoon *b)
1248{
1249 return 0;
1250}
1251
1252static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1253{
1254}
1255
1256#endif
1257
1258static int __init vmballoon_init(void)
1259{
1260 int error;
1261 unsigned is_2m_pages;
1262
1263
1264
1265
1266 if (x86_hyper_type != X86_HYPER_VMWARE)
1267 return -ENODEV;
1268
1269 for (is_2m_pages = 0; is_2m_pages < VMW_BALLOON_NUM_PAGE_SIZES;
1270 is_2m_pages++) {
1271 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].pages);
1272 INIT_LIST_HEAD(&balloon.page_sizes[is_2m_pages].refused_pages);
1273 }
1274
1275
1276 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
1277
1278 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1279
1280 error = vmballoon_debugfs_init(&balloon);
1281 if (error)
1282 return error;
1283
1284 balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
1285 balloon.batch_page = NULL;
1286 balloon.page = NULL;
1287 balloon.reset_required = true;
1288
1289 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
1290
1291 return 0;
1292}
1293module_init(vmballoon_init);
1294
1295static void __exit vmballoon_exit(void)
1296{
1297 vmballoon_vmci_cleanup(&balloon);
1298 cancel_delayed_work_sync(&balloon.dwork);
1299
1300 vmballoon_debugfs_exit(&balloon);
1301
1302
1303
1304
1305
1306
1307 vmballoon_send_start(&balloon, 0);
1308 vmballoon_pop(&balloon);
1309}
1310module_exit(vmballoon_exit);
1311