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/kernel.h>
39#include <linux/mm.h>
40#include <linux/sched.h>
41#include <linux/module.h>
42#include <linux/workqueue.h>
43#include <linux/debugfs.h>
44#include <linux/seq_file.h>
45#include <asm/hypervisor.h>
46
47MODULE_AUTHOR("VMware, Inc.");
48MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
49MODULE_VERSION("1.2.1.3-k");
50MODULE_ALIAS("dmi:*:svnVMware*:*");
51MODULE_ALIAS("vmware_vmmemctl");
52MODULE_LICENSE("GPL");
53
54
55
56
57
58
59
60
61
62
63#define VMW_BALLOON_NOSLEEP_ALLOC_MAX 16384U
64
65
66
67
68
69#define VMW_BALLOON_RATE_ALLOC_MIN 512U
70#define VMW_BALLOON_RATE_ALLOC_MAX 2048U
71#define VMW_BALLOON_RATE_ALLOC_INC 16U
72
73
74
75
76#define VMW_BALLOON_RATE_FREE_MIN 512U
77#define VMW_BALLOON_RATE_FREE_MAX 16384U
78#define VMW_BALLOON_RATE_FREE_INC 16U
79
80
81
82
83
84#define VMW_BALLOON_SLOW_CYCLES 4
85
86
87
88
89
90
91#define VMW_PAGE_ALLOC_NOSLEEP (__GFP_HIGHMEM|__GFP_NOWARN)
92
93
94
95
96
97
98
99
100#define VMW_PAGE_ALLOC_CANSLEEP (GFP_HIGHUSER)
101
102
103#define VMW_BALLOON_YIELD_THRESHOLD 1024
104
105
106#define VMW_BALLOON_MAX_REFUSED 16
107
108
109
110
111#define VMW_BALLOON_HV_PORT 0x5670
112#define VMW_BALLOON_HV_MAGIC 0x456c6d6f
113#define VMW_BALLOON_PROTOCOL_VERSION 2
114#define VMW_BALLOON_GUEST_ID 1
115
116#define VMW_BALLOON_CMD_START 0
117#define VMW_BALLOON_CMD_GET_TARGET 1
118#define VMW_BALLOON_CMD_LOCK 2
119#define VMW_BALLOON_CMD_UNLOCK 3
120#define VMW_BALLOON_CMD_GUEST_ID 4
121
122
123#define VMW_BALLOON_SUCCESS 0
124#define VMW_BALLOON_FAILURE -1
125#define VMW_BALLOON_ERROR_CMD_INVALID 1
126#define VMW_BALLOON_ERROR_PPN_INVALID 2
127#define VMW_BALLOON_ERROR_PPN_LOCKED 3
128#define VMW_BALLOON_ERROR_PPN_UNLOCKED 4
129#define VMW_BALLOON_ERROR_PPN_PINNED 5
130#define VMW_BALLOON_ERROR_PPN_NOTNEEDED 6
131#define VMW_BALLOON_ERROR_RESET 7
132#define VMW_BALLOON_ERROR_BUSY 8
133
134#define VMWARE_BALLOON_CMD(cmd, data, result) \
135({ \
136 unsigned long __stat, __dummy1, __dummy2; \
137 __asm__ __volatile__ ("inl %%dx" : \
138 "=a"(__stat), \
139 "=c"(__dummy1), \
140 "=d"(__dummy2), \
141 "=b"(result) : \
142 "0"(VMW_BALLOON_HV_MAGIC), \
143 "1"(VMW_BALLOON_CMD_##cmd), \
144 "2"(VMW_BALLOON_HV_PORT), \
145 "3"(data) : \
146 "memory"); \
147 result &= -1UL; \
148 __stat & -1UL; \
149})
150
151#ifdef CONFIG_DEBUG_FS
152struct vmballoon_stats {
153 unsigned int timer;
154
155
156 unsigned int alloc;
157 unsigned int alloc_fail;
158 unsigned int sleep_alloc;
159 unsigned int sleep_alloc_fail;
160 unsigned int refused_alloc;
161 unsigned int refused_free;
162 unsigned int free;
163
164
165 unsigned int lock;
166 unsigned int lock_fail;
167 unsigned int unlock;
168 unsigned int unlock_fail;
169 unsigned int target;
170 unsigned int target_fail;
171 unsigned int start;
172 unsigned int start_fail;
173 unsigned int guest_type;
174 unsigned int guest_type_fail;
175};
176
177#define STATS_INC(stat) (stat)++
178#else
179#define STATS_INC(stat)
180#endif
181
182struct vmballoon {
183
184
185 struct list_head pages;
186
187
188 struct list_head refused_pages;
189 unsigned int n_refused_pages;
190
191
192 unsigned int size;
193 unsigned int target;
194
195
196 bool reset_required;
197
198
199 unsigned int rate_alloc;
200 unsigned int rate_free;
201
202
203 unsigned int slow_allocation_cycles;
204
205#ifdef CONFIG_DEBUG_FS
206
207 struct vmballoon_stats stats;
208
209
210 struct dentry *dbg_entry;
211#endif
212
213 struct sysinfo sysinfo;
214
215 struct delayed_work dwork;
216};
217
218static struct vmballoon balloon;
219
220
221
222
223
224static bool vmballoon_send_start(struct vmballoon *b)
225{
226 unsigned long status, dummy;
227
228 STATS_INC(b->stats.start);
229
230 status = VMWARE_BALLOON_CMD(START, VMW_BALLOON_PROTOCOL_VERSION, dummy);
231 if (status == VMW_BALLOON_SUCCESS)
232 return true;
233
234 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
235 STATS_INC(b->stats.start_fail);
236 return false;
237}
238
239static bool vmballoon_check_status(struct vmballoon *b, unsigned long status)
240{
241 switch (status) {
242 case VMW_BALLOON_SUCCESS:
243 return true;
244
245 case VMW_BALLOON_ERROR_RESET:
246 b->reset_required = true;
247
248
249 default:
250 return false;
251 }
252}
253
254
255
256
257
258
259
260static bool vmballoon_send_guest_id(struct vmballoon *b)
261{
262 unsigned long status, dummy;
263
264 status = VMWARE_BALLOON_CMD(GUEST_ID, VMW_BALLOON_GUEST_ID, dummy);
265
266 STATS_INC(b->stats.guest_type);
267
268 if (vmballoon_check_status(b, status))
269 return true;
270
271 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
272 STATS_INC(b->stats.guest_type_fail);
273 return false;
274}
275
276
277
278
279static bool vmballoon_send_get_target(struct vmballoon *b, u32 *new_target)
280{
281 unsigned long status;
282 unsigned long target;
283 unsigned long limit;
284 u32 limit32;
285
286
287
288
289
290
291 si_meminfo(&b->sysinfo);
292 limit = b->sysinfo.totalram;
293
294
295 limit32 = (u32)limit;
296 if (limit != limit32)
297 return false;
298
299
300 STATS_INC(b->stats.target);
301
302 status = VMWARE_BALLOON_CMD(GET_TARGET, limit, target);
303 if (vmballoon_check_status(b, status)) {
304 *new_target = target;
305 return true;
306 }
307
308 pr_debug("%s - failed, hv returns %ld\n", __func__, status);
309 STATS_INC(b->stats.target_fail);
310 return false;
311}
312
313
314
315
316
317
318static int vmballoon_send_lock_page(struct vmballoon *b, unsigned long pfn,
319 unsigned int *hv_status)
320{
321 unsigned long status, dummy;
322 u32 pfn32;
323
324 pfn32 = (u32)pfn;
325 if (pfn32 != pfn)
326 return -1;
327
328 STATS_INC(b->stats.lock);
329
330 *hv_status = status = VMWARE_BALLOON_CMD(LOCK, pfn, dummy);
331 if (vmballoon_check_status(b, status))
332 return 0;
333
334 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
335 STATS_INC(b->stats.lock_fail);
336 return 1;
337}
338
339
340
341
342
343static bool vmballoon_send_unlock_page(struct vmballoon *b, unsigned long pfn)
344{
345 unsigned long status, dummy;
346 u32 pfn32;
347
348 pfn32 = (u32)pfn;
349 if (pfn32 != pfn)
350 return false;
351
352 STATS_INC(b->stats.unlock);
353
354 status = VMWARE_BALLOON_CMD(UNLOCK, pfn, dummy);
355 if (vmballoon_check_status(b, status))
356 return true;
357
358 pr_debug("%s - ppn %lx, hv returns %ld\n", __func__, pfn, status);
359 STATS_INC(b->stats.unlock_fail);
360 return false;
361}
362
363
364
365
366
367
368
369static void vmballoon_pop(struct vmballoon *b)
370{
371 struct page *page, *next;
372 unsigned int count = 0;
373
374 list_for_each_entry_safe(page, next, &b->pages, lru) {
375 list_del(&page->lru);
376 __free_page(page);
377 STATS_INC(b->stats.free);
378 b->size--;
379
380 if (++count >= b->rate_free) {
381 count = 0;
382 cond_resched();
383 }
384 }
385}
386
387
388
389
390
391
392static void vmballoon_reset(struct vmballoon *b)
393{
394
395 vmballoon_pop(b);
396
397 if (vmballoon_send_start(b)) {
398 b->reset_required = false;
399 if (!vmballoon_send_guest_id(b))
400 pr_err("failed to send guest ID to the host\n");
401 }
402}
403
404
405
406
407
408
409
410static int vmballoon_reserve_page(struct vmballoon *b, bool can_sleep)
411{
412 struct page *page;
413 gfp_t flags;
414 unsigned int hv_status;
415 int locked;
416 flags = can_sleep ? VMW_PAGE_ALLOC_CANSLEEP : VMW_PAGE_ALLOC_NOSLEEP;
417
418 do {
419 if (!can_sleep)
420 STATS_INC(b->stats.alloc);
421 else
422 STATS_INC(b->stats.sleep_alloc);
423
424 page = alloc_page(flags);
425 if (!page) {
426 if (!can_sleep)
427 STATS_INC(b->stats.alloc_fail);
428 else
429 STATS_INC(b->stats.sleep_alloc_fail);
430 return -ENOMEM;
431 }
432
433
434 locked = vmballoon_send_lock_page(b, page_to_pfn(page), &hv_status);
435 if (locked > 0) {
436 STATS_INC(b->stats.refused_alloc);
437
438 if (hv_status == VMW_BALLOON_ERROR_RESET ||
439 hv_status == VMW_BALLOON_ERROR_PPN_NOTNEEDED) {
440 __free_page(page);
441 return -EIO;
442 }
443
444
445
446
447
448
449 list_add(&page->lru, &b->refused_pages);
450 if (++b->n_refused_pages >= VMW_BALLOON_MAX_REFUSED)
451 return -EIO;
452 }
453 } while (locked != 0);
454
455
456 list_add(&page->lru, &b->pages);
457
458
459 b->size++;
460
461 return 0;
462}
463
464
465
466
467
468
469static int vmballoon_release_page(struct vmballoon *b, struct page *page)
470{
471 if (!vmballoon_send_unlock_page(b, page_to_pfn(page)))
472 return -EIO;
473
474 list_del(&page->lru);
475
476
477 __free_page(page);
478 STATS_INC(b->stats.free);
479
480
481 b->size--;
482
483 return 0;
484}
485
486
487
488
489
490static void vmballoon_release_refused_pages(struct vmballoon *b)
491{
492 struct page *page, *next;
493
494 list_for_each_entry_safe(page, next, &b->refused_pages, lru) {
495 list_del(&page->lru);
496 __free_page(page);
497 STATS_INC(b->stats.refused_free);
498 }
499
500 b->n_refused_pages = 0;
501}
502
503
504
505
506
507
508static void vmballoon_inflate(struct vmballoon *b)
509{
510 unsigned int goal;
511 unsigned int rate;
512 unsigned int i;
513 unsigned int allocations = 0;
514 int error = 0;
515 bool alloc_can_sleep = false;
516
517 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534 goal = b->target - b->size;
535
536
537
538
539 rate = b->slow_allocation_cycles ?
540 b->rate_alloc : VMW_BALLOON_NOSLEEP_ALLOC_MAX;
541
542 pr_debug("%s - goal: %d, no-sleep rate: %d, sleep rate: %d\n",
543 __func__, goal, rate, b->rate_alloc);
544
545 for (i = 0; i < goal; i++) {
546
547 error = vmballoon_reserve_page(b, alloc_can_sleep);
548 if (error) {
549 if (error != -ENOMEM) {
550
551
552
553
554
555 break;
556 }
557
558 if (alloc_can_sleep) {
559
560
561
562
563
564 b->rate_alloc = max(b->rate_alloc / 2,
565 VMW_BALLOON_RATE_ALLOC_MIN);
566 break;
567 }
568
569
570
571
572
573
574
575
576
577 b->slow_allocation_cycles = VMW_BALLOON_SLOW_CYCLES;
578
579 if (i >= b->rate_alloc)
580 break;
581
582 alloc_can_sleep = true;
583
584 rate = b->rate_alloc;
585 }
586
587 if (++allocations > VMW_BALLOON_YIELD_THRESHOLD) {
588 cond_resched();
589 allocations = 0;
590 }
591
592 if (i >= rate) {
593
594 break;
595 }
596 }
597
598
599
600
601
602 if (error == 0 && i >= b->rate_alloc) {
603 unsigned int mult = i / b->rate_alloc;
604
605 b->rate_alloc =
606 min(b->rate_alloc + mult * VMW_BALLOON_RATE_ALLOC_INC,
607 VMW_BALLOON_RATE_ALLOC_MAX);
608 }
609
610 vmballoon_release_refused_pages(b);
611}
612
613
614
615
616static void vmballoon_deflate(struct vmballoon *b)
617{
618 struct page *page, *next;
619 unsigned int i = 0;
620 unsigned int goal;
621 int error;
622
623 pr_debug("%s - size: %d, target %d\n", __func__, b->size, b->target);
624
625
626 goal = min(b->size - b->target, b->rate_free);
627
628 pr_debug("%s - goal: %d, rate: %d\n", __func__, goal, b->rate_free);
629
630
631 list_for_each_entry_safe(page, next, &b->pages, lru) {
632 error = vmballoon_release_page(b, page);
633 if (error) {
634
635 b->rate_free = max(b->rate_free / 2,
636 VMW_BALLOON_RATE_FREE_MIN);
637 return;
638 }
639
640 if (++i >= goal)
641 break;
642 }
643
644
645 b->rate_free = min(b->rate_free + VMW_BALLOON_RATE_FREE_INC,
646 VMW_BALLOON_RATE_FREE_MAX);
647}
648
649
650
651
652
653static void vmballoon_work(struct work_struct *work)
654{
655 struct delayed_work *dwork = to_delayed_work(work);
656 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
657 unsigned int target;
658
659 STATS_INC(b->stats.timer);
660
661 if (b->reset_required)
662 vmballoon_reset(b);
663
664 if (b->slow_allocation_cycles > 0)
665 b->slow_allocation_cycles--;
666
667 if (vmballoon_send_get_target(b, &target)) {
668
669 b->target = target;
670
671 if (b->size < target)
672 vmballoon_inflate(b);
673 else if (b->size > target)
674 vmballoon_deflate(b);
675 }
676
677
678
679
680
681 queue_delayed_work(system_freezable_wq,
682 dwork, round_jiffies_relative(HZ));
683}
684
685
686
687
688#ifdef CONFIG_DEBUG_FS
689
690static int vmballoon_debug_show(struct seq_file *f, void *offset)
691{
692 struct vmballoon *b = f->private;
693 struct vmballoon_stats *stats = &b->stats;
694
695
696 seq_printf(f,
697 "target: %8d pages\n"
698 "current: %8d pages\n",
699 b->target, b->size);
700
701
702 seq_printf(f,
703 "rateNoSleepAlloc: %8d pages/sec\n"
704 "rateSleepAlloc: %8d pages/sec\n"
705 "rateFree: %8d pages/sec\n",
706 VMW_BALLOON_NOSLEEP_ALLOC_MAX,
707 b->rate_alloc, b->rate_free);
708
709 seq_printf(f,
710 "\n"
711 "timer: %8u\n"
712 "start: %8u (%4u failed)\n"
713 "guestType: %8u (%4u failed)\n"
714 "lock: %8u (%4u failed)\n"
715 "unlock: %8u (%4u failed)\n"
716 "target: %8u (%4u failed)\n"
717 "primNoSleepAlloc: %8u (%4u failed)\n"
718 "primCanSleepAlloc: %8u (%4u failed)\n"
719 "primFree: %8u\n"
720 "errAlloc: %8u\n"
721 "errFree: %8u\n",
722 stats->timer,
723 stats->start, stats->start_fail,
724 stats->guest_type, stats->guest_type_fail,
725 stats->lock, stats->lock_fail,
726 stats->unlock, stats->unlock_fail,
727 stats->target, stats->target_fail,
728 stats->alloc, stats->alloc_fail,
729 stats->sleep_alloc, stats->sleep_alloc_fail,
730 stats->free,
731 stats->refused_alloc, stats->refused_free);
732
733 return 0;
734}
735
736static int vmballoon_debug_open(struct inode *inode, struct file *file)
737{
738 return single_open(file, vmballoon_debug_show, inode->i_private);
739}
740
741static const struct file_operations vmballoon_debug_fops = {
742 .owner = THIS_MODULE,
743 .open = vmballoon_debug_open,
744 .read = seq_read,
745 .llseek = seq_lseek,
746 .release = single_release,
747};
748
749static int __init vmballoon_debugfs_init(struct vmballoon *b)
750{
751 int error;
752
753 b->dbg_entry = debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
754 &vmballoon_debug_fops);
755 if (IS_ERR(b->dbg_entry)) {
756 error = PTR_ERR(b->dbg_entry);
757 pr_err("failed to create debugfs entry, error: %d\n", error);
758 return error;
759 }
760
761 return 0;
762}
763
764static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
765{
766 debugfs_remove(b->dbg_entry);
767}
768
769#else
770
771static inline int vmballoon_debugfs_init(struct vmballoon *b)
772{
773 return 0;
774}
775
776static inline void vmballoon_debugfs_exit(struct vmballoon *b)
777{
778}
779
780#endif
781
782static int __init vmballoon_init(void)
783{
784 int error;
785
786
787
788
789
790 if (x86_hyper != &x86_hyper_vmware)
791 return -ENODEV;
792
793 INIT_LIST_HEAD(&balloon.pages);
794 INIT_LIST_HEAD(&balloon.refused_pages);
795
796
797 balloon.rate_alloc = VMW_BALLOON_RATE_ALLOC_MAX;
798 balloon.rate_free = VMW_BALLOON_RATE_FREE_MAX;
799
800 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
801
802
803
804
805 if (!vmballoon_send_start(&balloon)) {
806 pr_err("failed to send start command to the host\n");
807 return -EIO;
808 }
809
810 if (!vmballoon_send_guest_id(&balloon)) {
811 pr_err("failed to send guest ID to the host\n");
812 return -EIO;
813 }
814
815 error = vmballoon_debugfs_init(&balloon);
816 if (error)
817 return error;
818
819 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
820
821 return 0;
822}
823module_init(vmballoon_init);
824
825static void __exit vmballoon_exit(void)
826{
827 cancel_delayed_work_sync(&balloon.dwork);
828
829 vmballoon_debugfs_exit(&balloon);
830
831
832
833
834
835
836 vmballoon_send_start(&balloon);
837 vmballoon_pop(&balloon);
838}
839module_exit(vmballoon_exit);
840