0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018
0019 #include <linux/types.h>
0020 #include <linux/io.h>
0021 #include <linux/kernel.h>
0022 #include <linux/mm.h>
0023 #include <linux/vmalloc.h>
0024 #include <linux/sched.h>
0025 #include <linux/module.h>
0026 #include <linux/workqueue.h>
0027 #include <linux/debugfs.h>
0028 #include <linux/seq_file.h>
0029 #include <linux/rwsem.h>
0030 #include <linux/slab.h>
0031 #include <linux/spinlock.h>
0032 #include <linux/balloon_compaction.h>
0033 #include <linux/vmw_vmci_defs.h>
0034 #include <linux/vmw_vmci_api.h>
0035 #include <asm/hypervisor.h>
0036
0037 MODULE_AUTHOR("VMware, Inc.");
0038 MODULE_DESCRIPTION("VMware Memory Control (Balloon) Driver");
0039 MODULE_ALIAS("dmi:*:svnVMware*:*");
0040 MODULE_ALIAS("vmware_vmmemctl");
0041 MODULE_LICENSE("GPL");
0042
0043 static bool __read_mostly vmwballoon_shrinker_enable;
0044 module_param(vmwballoon_shrinker_enable, bool, 0444);
0045 MODULE_PARM_DESC(vmwballoon_shrinker_enable,
0046 "Enable non-cooperative out-of-memory protection. Disabled by default as it may degrade performance.");
0047
0048
0049 #define VMBALLOON_SHRINK_DELAY (5)
0050
0051
0052 #define VMW_BALLOON_MAX_REFUSED 16
0053
0054
0055 #define BALLOON_VMW_MAGIC 0x0ba11007
0056
0057
0058
0059
0060 #define VMW_BALLOON_HV_PORT 0x5670
0061 #define VMW_BALLOON_HV_MAGIC 0x456c6d6f
0062 #define VMW_BALLOON_GUEST_ID 1
0063
0064 enum vmwballoon_capabilities {
0065
0066
0067
0068 VMW_BALLOON_BASIC_CMDS = (1 << 1),
0069 VMW_BALLOON_BATCHED_CMDS = (1 << 2),
0070 VMW_BALLOON_BATCHED_2M_CMDS = (1 << 3),
0071 VMW_BALLOON_SIGNALLED_WAKEUP_CMD = (1 << 4),
0072 VMW_BALLOON_64_BIT_TARGET = (1 << 5)
0073 };
0074
0075 #define VMW_BALLOON_CAPABILITIES_COMMON (VMW_BALLOON_BASIC_CMDS \
0076 | VMW_BALLOON_BATCHED_CMDS \
0077 | VMW_BALLOON_BATCHED_2M_CMDS \
0078 | VMW_BALLOON_SIGNALLED_WAKEUP_CMD)
0079
0080 #define VMW_BALLOON_2M_ORDER (PMD_SHIFT - PAGE_SHIFT)
0081
0082
0083
0084
0085 #ifdef CONFIG_64BIT
0086 #define VMW_BALLOON_CAPABILITIES (VMW_BALLOON_CAPABILITIES_COMMON \
0087 | VMW_BALLOON_64_BIT_TARGET)
0088 #else
0089 #define VMW_BALLOON_CAPABILITIES VMW_BALLOON_CAPABILITIES_COMMON
0090 #endif
0091
0092 enum vmballoon_page_size_type {
0093 VMW_BALLOON_4K_PAGE,
0094 VMW_BALLOON_2M_PAGE,
0095 VMW_BALLOON_LAST_SIZE = VMW_BALLOON_2M_PAGE
0096 };
0097
0098 #define VMW_BALLOON_NUM_PAGE_SIZES (VMW_BALLOON_LAST_SIZE + 1)
0099
0100 static const char * const vmballoon_page_size_names[] = {
0101 [VMW_BALLOON_4K_PAGE] = "4k",
0102 [VMW_BALLOON_2M_PAGE] = "2M"
0103 };
0104
0105 enum vmballoon_op {
0106 VMW_BALLOON_INFLATE,
0107 VMW_BALLOON_DEFLATE
0108 };
0109
0110 enum vmballoon_op_stat_type {
0111 VMW_BALLOON_OP_STAT,
0112 VMW_BALLOON_OP_FAIL_STAT
0113 };
0114
0115 #define VMW_BALLOON_OP_STAT_TYPES (VMW_BALLOON_OP_FAIL_STAT + 1)
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 enum vmballoon_cmd_type {
0162 VMW_BALLOON_CMD_START,
0163 VMW_BALLOON_CMD_GET_TARGET,
0164 VMW_BALLOON_CMD_LOCK,
0165 VMW_BALLOON_CMD_UNLOCK,
0166 VMW_BALLOON_CMD_GUEST_ID,
0167
0168 VMW_BALLOON_CMD_BATCHED_LOCK = 6,
0169 VMW_BALLOON_CMD_BATCHED_UNLOCK,
0170 VMW_BALLOON_CMD_BATCHED_2M_LOCK,
0171 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK,
0172 VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
0173 VMW_BALLOON_CMD_LAST = VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
0174 };
0175
0176 #define VMW_BALLOON_CMD_NUM (VMW_BALLOON_CMD_LAST + 1)
0177
0178 enum vmballoon_error_codes {
0179 VMW_BALLOON_SUCCESS,
0180 VMW_BALLOON_ERROR_CMD_INVALID,
0181 VMW_BALLOON_ERROR_PPN_INVALID,
0182 VMW_BALLOON_ERROR_PPN_LOCKED,
0183 VMW_BALLOON_ERROR_PPN_UNLOCKED,
0184 VMW_BALLOON_ERROR_PPN_PINNED,
0185 VMW_BALLOON_ERROR_PPN_NOTNEEDED,
0186 VMW_BALLOON_ERROR_RESET,
0187 VMW_BALLOON_ERROR_BUSY
0188 };
0189
0190 #define VMW_BALLOON_SUCCESS_WITH_CAPABILITIES (0x03000000)
0191
0192 #define VMW_BALLOON_CMD_WITH_TARGET_MASK \
0193 ((1UL << VMW_BALLOON_CMD_GET_TARGET) | \
0194 (1UL << VMW_BALLOON_CMD_LOCK) | \
0195 (1UL << VMW_BALLOON_CMD_UNLOCK) | \
0196 (1UL << VMW_BALLOON_CMD_BATCHED_LOCK) | \
0197 (1UL << VMW_BALLOON_CMD_BATCHED_UNLOCK) | \
0198 (1UL << VMW_BALLOON_CMD_BATCHED_2M_LOCK) | \
0199 (1UL << VMW_BALLOON_CMD_BATCHED_2M_UNLOCK))
0200
0201 static const char * const vmballoon_cmd_names[] = {
0202 [VMW_BALLOON_CMD_START] = "start",
0203 [VMW_BALLOON_CMD_GET_TARGET] = "target",
0204 [VMW_BALLOON_CMD_LOCK] = "lock",
0205 [VMW_BALLOON_CMD_UNLOCK] = "unlock",
0206 [VMW_BALLOON_CMD_GUEST_ID] = "guestType",
0207 [VMW_BALLOON_CMD_BATCHED_LOCK] = "batchLock",
0208 [VMW_BALLOON_CMD_BATCHED_UNLOCK] = "batchUnlock",
0209 [VMW_BALLOON_CMD_BATCHED_2M_LOCK] = "2m-lock",
0210 [VMW_BALLOON_CMD_BATCHED_2M_UNLOCK] = "2m-unlock",
0211 [VMW_BALLOON_CMD_VMCI_DOORBELL_SET] = "doorbellSet"
0212 };
0213
0214 enum vmballoon_stat_page {
0215 VMW_BALLOON_PAGE_STAT_ALLOC,
0216 VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
0217 VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
0218 VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
0219 VMW_BALLOON_PAGE_STAT_FREE,
0220 VMW_BALLOON_PAGE_STAT_LAST = VMW_BALLOON_PAGE_STAT_FREE
0221 };
0222
0223 #define VMW_BALLOON_PAGE_STAT_NUM (VMW_BALLOON_PAGE_STAT_LAST + 1)
0224
0225 enum vmballoon_stat_general {
0226 VMW_BALLOON_STAT_TIMER,
0227 VMW_BALLOON_STAT_DOORBELL,
0228 VMW_BALLOON_STAT_RESET,
0229 VMW_BALLOON_STAT_SHRINK,
0230 VMW_BALLOON_STAT_SHRINK_FREE,
0231 VMW_BALLOON_STAT_LAST = VMW_BALLOON_STAT_SHRINK_FREE
0232 };
0233
0234 #define VMW_BALLOON_STAT_NUM (VMW_BALLOON_STAT_LAST + 1)
0235
0236 static DEFINE_STATIC_KEY_TRUE(vmw_balloon_batching);
0237 static DEFINE_STATIC_KEY_FALSE(balloon_stat_enabled);
0238
0239 struct vmballoon_ctl {
0240 struct list_head pages;
0241 struct list_head refused_pages;
0242 struct list_head prealloc_pages;
0243 unsigned int n_refused_pages;
0244 unsigned int n_pages;
0245 enum vmballoon_page_size_type page_size;
0246 enum vmballoon_op op;
0247 };
0248
0249
0250
0251
0252
0253
0254
0255
0256 struct vmballoon_batch_entry {
0257 u64 status : 5;
0258 u64 reserved : PAGE_SHIFT - 5;
0259 u64 pfn : 52;
0260 } __packed;
0261
0262 struct vmballoon {
0263
0264
0265
0266
0267
0268 enum vmballoon_page_size_type max_page_size;
0269
0270
0271
0272
0273
0274
0275
0276 atomic64_t size;
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287 unsigned long target;
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297 bool reset_required;
0298
0299
0300
0301
0302
0303
0304 unsigned long capabilities;
0305
0306
0307
0308
0309
0310
0311
0312 struct vmballoon_batch_entry *batch_page;
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323 unsigned int batch_max_pages;
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333 struct page *page;
0334
0335
0336
0337
0338
0339
0340
0341
0342 unsigned long shrink_timeout;
0343
0344
0345 struct vmballoon_stats *stats;
0346
0347
0348
0349
0350 struct balloon_dev_info b_dev_info;
0351
0352 struct delayed_work dwork;
0353
0354
0355
0356
0357
0358
0359 struct list_head huge_pages;
0360
0361
0362
0363
0364
0365
0366 struct vmci_handle vmci_doorbell;
0367
0368
0369
0370
0371 struct rw_semaphore conf_sem;
0372
0373
0374
0375
0376
0377
0378 spinlock_t comm_lock;
0379
0380
0381
0382
0383 struct shrinker shrinker;
0384
0385
0386
0387
0388
0389
0390
0391
0392 bool shrinker_registered;
0393 };
0394
0395 static struct vmballoon balloon;
0396
0397 struct vmballoon_stats {
0398
0399 atomic64_t general_stat[VMW_BALLOON_STAT_NUM];
0400
0401
0402 atomic64_t
0403 page_stat[VMW_BALLOON_PAGE_STAT_NUM][VMW_BALLOON_NUM_PAGE_SIZES];
0404
0405
0406 atomic64_t ops[VMW_BALLOON_CMD_NUM][VMW_BALLOON_OP_STAT_TYPES];
0407 };
0408
0409 static inline bool is_vmballoon_stats_on(void)
0410 {
0411 return IS_ENABLED(CONFIG_DEBUG_FS) &&
0412 static_branch_unlikely(&balloon_stat_enabled);
0413 }
0414
0415 static inline void vmballoon_stats_op_inc(struct vmballoon *b, unsigned int op,
0416 enum vmballoon_op_stat_type type)
0417 {
0418 if (is_vmballoon_stats_on())
0419 atomic64_inc(&b->stats->ops[op][type]);
0420 }
0421
0422 static inline void vmballoon_stats_gen_inc(struct vmballoon *b,
0423 enum vmballoon_stat_general stat)
0424 {
0425 if (is_vmballoon_stats_on())
0426 atomic64_inc(&b->stats->general_stat[stat]);
0427 }
0428
0429 static inline void vmballoon_stats_gen_add(struct vmballoon *b,
0430 enum vmballoon_stat_general stat,
0431 unsigned int val)
0432 {
0433 if (is_vmballoon_stats_on())
0434 atomic64_add(val, &b->stats->general_stat[stat]);
0435 }
0436
0437 static inline void vmballoon_stats_page_inc(struct vmballoon *b,
0438 enum vmballoon_stat_page stat,
0439 enum vmballoon_page_size_type size)
0440 {
0441 if (is_vmballoon_stats_on())
0442 atomic64_inc(&b->stats->page_stat[stat][size]);
0443 }
0444
0445 static inline void vmballoon_stats_page_add(struct vmballoon *b,
0446 enum vmballoon_stat_page stat,
0447 enum vmballoon_page_size_type size,
0448 unsigned int val)
0449 {
0450 if (is_vmballoon_stats_on())
0451 atomic64_add(val, &b->stats->page_stat[stat][size]);
0452 }
0453
0454 static inline unsigned long
0455 __vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
0456 unsigned long arg2, unsigned long *result)
0457 {
0458 unsigned long status, dummy1, dummy2, dummy3, local_result;
0459
0460 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_STAT);
0461
0462 asm volatile ("inl %%dx" :
0463 "=a"(status),
0464 "=c"(dummy1),
0465 "=d"(dummy2),
0466 "=b"(local_result),
0467 "=S"(dummy3) :
0468 "0"(VMW_BALLOON_HV_MAGIC),
0469 "1"(cmd),
0470 "2"(VMW_BALLOON_HV_PORT),
0471 "3"(arg1),
0472 "4"(arg2) :
0473 "memory");
0474
0475
0476 if (result)
0477 *result = (cmd == VMW_BALLOON_CMD_START) ? dummy1 :
0478 local_result;
0479
0480
0481 if (status == VMW_BALLOON_SUCCESS &&
0482 ((1ul << cmd) & VMW_BALLOON_CMD_WITH_TARGET_MASK))
0483 WRITE_ONCE(b->target, local_result);
0484
0485 if (status != VMW_BALLOON_SUCCESS &&
0486 status != VMW_BALLOON_SUCCESS_WITH_CAPABILITIES) {
0487 vmballoon_stats_op_inc(b, cmd, VMW_BALLOON_OP_FAIL_STAT);
0488 pr_debug("%s: %s [0x%lx,0x%lx) failed, returned %ld\n",
0489 __func__, vmballoon_cmd_names[cmd], arg1, arg2,
0490 status);
0491 }
0492
0493
0494 if (status == VMW_BALLOON_ERROR_RESET)
0495 b->reset_required = true;
0496
0497 return status;
0498 }
0499
0500 static __always_inline unsigned long
0501 vmballoon_cmd(struct vmballoon *b, unsigned long cmd, unsigned long arg1,
0502 unsigned long arg2)
0503 {
0504 unsigned long dummy;
0505
0506 return __vmballoon_cmd(b, cmd, arg1, arg2, &dummy);
0507 }
0508
0509
0510
0511
0512
0513 static int vmballoon_send_start(struct vmballoon *b, unsigned long req_caps)
0514 {
0515 unsigned long status, capabilities;
0516
0517 status = __vmballoon_cmd(b, VMW_BALLOON_CMD_START, req_caps, 0,
0518 &capabilities);
0519
0520 switch (status) {
0521 case VMW_BALLOON_SUCCESS_WITH_CAPABILITIES:
0522 b->capabilities = capabilities;
0523 break;
0524 case VMW_BALLOON_SUCCESS:
0525 b->capabilities = VMW_BALLOON_BASIC_CMDS;
0526 break;
0527 default:
0528 return -EIO;
0529 }
0530
0531
0532
0533
0534
0535
0536 b->max_page_size = VMW_BALLOON_4K_PAGE;
0537 if ((b->capabilities & VMW_BALLOON_BATCHED_2M_CMDS) &&
0538 (b->capabilities & VMW_BALLOON_BATCHED_CMDS))
0539 b->max_page_size = VMW_BALLOON_2M_PAGE;
0540
0541
0542 return 0;
0543 }
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557 static int vmballoon_send_guest_id(struct vmballoon *b)
0558 {
0559 unsigned long status;
0560
0561 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GUEST_ID,
0562 VMW_BALLOON_GUEST_ID, 0);
0563
0564 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
0565 }
0566
0567
0568
0569
0570
0571
0572
0573 static inline
0574 unsigned int vmballoon_page_order(enum vmballoon_page_size_type page_size)
0575 {
0576 return page_size == VMW_BALLOON_2M_PAGE ? VMW_BALLOON_2M_ORDER : 0;
0577 }
0578
0579
0580
0581
0582
0583
0584
0585 static inline unsigned int
0586 vmballoon_page_in_frames(enum vmballoon_page_size_type page_size)
0587 {
0588 return 1 << vmballoon_page_order(page_size);
0589 }
0590
0591
0592
0593
0594
0595
0596 static void
0597 vmballoon_mark_page_offline(struct page *page,
0598 enum vmballoon_page_size_type page_size)
0599 {
0600 int i;
0601
0602 for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
0603 __SetPageOffline(page + i);
0604 }
0605
0606
0607
0608
0609
0610
0611 static void
0612 vmballoon_mark_page_online(struct page *page,
0613 enum vmballoon_page_size_type page_size)
0614 {
0615 int i;
0616
0617 for (i = 0; i < vmballoon_page_in_frames(page_size); i++)
0618 __ClearPageOffline(page + i);
0619 }
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630 static int vmballoon_send_get_target(struct vmballoon *b)
0631 {
0632 unsigned long status;
0633 unsigned long limit;
0634
0635 limit = totalram_pages();
0636
0637
0638 if (!(b->capabilities & VMW_BALLOON_64_BIT_TARGET) &&
0639 limit != (u32)limit)
0640 return -EINVAL;
0641
0642 status = vmballoon_cmd(b, VMW_BALLOON_CMD_GET_TARGET, limit, 0);
0643
0644 return status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
0645 }
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659 static int vmballoon_alloc_page_list(struct vmballoon *b,
0660 struct vmballoon_ctl *ctl,
0661 unsigned int req_n_pages)
0662 {
0663 struct page *page;
0664 unsigned int i;
0665
0666 for (i = 0; i < req_n_pages; i++) {
0667
0668
0669
0670
0671
0672 if (!list_empty(&ctl->prealloc_pages)) {
0673 page = list_first_entry(&ctl->prealloc_pages,
0674 struct page, lru);
0675 list_del(&page->lru);
0676 } else {
0677 if (ctl->page_size == VMW_BALLOON_2M_PAGE)
0678 page = alloc_pages(__GFP_HIGHMEM|__GFP_NOWARN|
0679 __GFP_NOMEMALLOC, VMW_BALLOON_2M_ORDER);
0680 else
0681 page = balloon_page_alloc();
0682
0683 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC,
0684 ctl->page_size);
0685 }
0686
0687 if (page) {
0688
0689 list_add(&page->lru, &ctl->pages);
0690 continue;
0691 }
0692
0693
0694 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_ALLOC_FAIL,
0695 ctl->page_size);
0696 break;
0697 }
0698
0699 ctl->n_pages = i;
0700
0701 return req_n_pages == ctl->n_pages ? 0 : -ENOMEM;
0702 }
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712 static int vmballoon_handle_one_result(struct vmballoon *b, struct page *page,
0713 enum vmballoon_page_size_type page_size,
0714 unsigned long status)
0715 {
0716
0717 if (likely(status == VMW_BALLOON_SUCCESS))
0718 return 0;
0719
0720 pr_debug("%s: failed comm pfn %lx status %lu page_size %s\n", __func__,
0721 page_to_pfn(page), status,
0722 vmballoon_page_size_names[page_size]);
0723
0724
0725 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC,
0726 page_size);
0727
0728 return -EIO;
0729 }
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744 static unsigned long vmballoon_status_page(struct vmballoon *b, int idx,
0745 struct page **p)
0746 {
0747 if (static_branch_likely(&vmw_balloon_batching)) {
0748
0749 *p = pfn_to_page(b->batch_page[idx].pfn);
0750 return b->batch_page[idx].status;
0751 }
0752
0753
0754 *p = b->page;
0755
0756
0757
0758
0759
0760
0761
0762 return VMW_BALLOON_SUCCESS;
0763 }
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780 static unsigned long vmballoon_lock_op(struct vmballoon *b,
0781 unsigned int num_pages,
0782 enum vmballoon_page_size_type page_size,
0783 enum vmballoon_op op)
0784 {
0785 unsigned long cmd, pfn;
0786
0787 lockdep_assert_held(&b->comm_lock);
0788
0789 if (static_branch_likely(&vmw_balloon_batching)) {
0790 if (op == VMW_BALLOON_INFLATE)
0791 cmd = page_size == VMW_BALLOON_2M_PAGE ?
0792 VMW_BALLOON_CMD_BATCHED_2M_LOCK :
0793 VMW_BALLOON_CMD_BATCHED_LOCK;
0794 else
0795 cmd = page_size == VMW_BALLOON_2M_PAGE ?
0796 VMW_BALLOON_CMD_BATCHED_2M_UNLOCK :
0797 VMW_BALLOON_CMD_BATCHED_UNLOCK;
0798
0799 pfn = PHYS_PFN(virt_to_phys(b->batch_page));
0800 } else {
0801 cmd = op == VMW_BALLOON_INFLATE ? VMW_BALLOON_CMD_LOCK :
0802 VMW_BALLOON_CMD_UNLOCK;
0803 pfn = page_to_pfn(b->page);
0804
0805
0806 if (unlikely(pfn != (u32)pfn))
0807 return VMW_BALLOON_ERROR_PPN_INVALID;
0808 }
0809
0810 return vmballoon_cmd(b, cmd, pfn, num_pages);
0811 }
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822 static void vmballoon_add_page(struct vmballoon *b, unsigned int idx,
0823 struct page *p)
0824 {
0825 lockdep_assert_held(&b->comm_lock);
0826
0827 if (static_branch_likely(&vmw_balloon_batching))
0828 b->batch_page[idx] = (struct vmballoon_batch_entry)
0829 { .pfn = page_to_pfn(p) };
0830 else
0831 b->page = p;
0832 }
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853 static int vmballoon_lock(struct vmballoon *b, struct vmballoon_ctl *ctl)
0854 {
0855 unsigned long batch_status;
0856 struct page *page;
0857 unsigned int i, num_pages;
0858
0859 num_pages = ctl->n_pages;
0860 if (num_pages == 0)
0861 return 0;
0862
0863
0864 spin_lock(&b->comm_lock);
0865
0866 i = 0;
0867 list_for_each_entry(page, &ctl->pages, lru)
0868 vmballoon_add_page(b, i++, page);
0869
0870 batch_status = vmballoon_lock_op(b, ctl->n_pages, ctl->page_size,
0871 ctl->op);
0872
0873
0874
0875
0876
0877
0878 for (i = 0; i < num_pages; i++) {
0879 unsigned long status;
0880
0881 status = vmballoon_status_page(b, i, &page);
0882
0883
0884
0885
0886
0887 if (batch_status != VMW_BALLOON_SUCCESS)
0888 status = batch_status;
0889
0890
0891 if (!vmballoon_handle_one_result(b, page, ctl->page_size,
0892 status))
0893 continue;
0894
0895
0896
0897
0898
0899 list_move(&page->lru, &ctl->refused_pages);
0900 ctl->n_pages--;
0901 ctl->n_refused_pages++;
0902 }
0903
0904 spin_unlock(&b->comm_lock);
0905
0906 return batch_status == VMW_BALLOON_SUCCESS ? 0 : -EIO;
0907 }
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918 static void vmballoon_release_page_list(struct list_head *page_list,
0919 int *n_pages,
0920 enum vmballoon_page_size_type page_size)
0921 {
0922 struct page *page, *tmp;
0923
0924 list_for_each_entry_safe(page, tmp, page_list, lru) {
0925 list_del(&page->lru);
0926 __free_pages(page, vmballoon_page_order(page_size));
0927 }
0928
0929 if (n_pages)
0930 *n_pages = 0;
0931 }
0932
0933
0934
0935
0936
0937
0938 static void vmballoon_release_refused_pages(struct vmballoon *b,
0939 struct vmballoon_ctl *ctl)
0940 {
0941 vmballoon_stats_page_inc(b, VMW_BALLOON_PAGE_STAT_REFUSED_FREE,
0942 ctl->page_size);
0943
0944 vmballoon_release_page_list(&ctl->refused_pages, &ctl->n_refused_pages,
0945 ctl->page_size);
0946 }
0947
0948
0949
0950
0951
0952
0953
0954
0955
0956 static int64_t vmballoon_change(struct vmballoon *b)
0957 {
0958 int64_t size, target;
0959
0960 size = atomic64_read(&b->size);
0961 target = READ_ONCE(b->target);
0962
0963
0964
0965
0966
0967
0968 if (b->reset_required)
0969 return 0;
0970
0971
0972 if (target < size && target != 0 &&
0973 size - target < vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE))
0974 return 0;
0975
0976
0977 if (target > size && time_before(jiffies, READ_ONCE(b->shrink_timeout)))
0978 return 0;
0979
0980 return target - size;
0981 }
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994 static void vmballoon_enqueue_page_list(struct vmballoon *b,
0995 struct list_head *pages,
0996 unsigned int *n_pages,
0997 enum vmballoon_page_size_type page_size)
0998 {
0999 unsigned long flags;
1000 struct page *page;
1001
1002 if (page_size == VMW_BALLOON_4K_PAGE) {
1003 balloon_page_list_enqueue(&b->b_dev_info, pages);
1004 } else {
1005
1006
1007
1008
1009 spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1010
1011 list_for_each_entry(page, pages, lru) {
1012 vmballoon_mark_page_offline(page, VMW_BALLOON_2M_PAGE);
1013 }
1014
1015 list_splice_init(pages, &b->huge_pages);
1016 __count_vm_events(BALLOON_INFLATE, *n_pages *
1017 vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1018 spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1019 }
1020
1021 *n_pages = 0;
1022 }
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 static void vmballoon_dequeue_page_list(struct vmballoon *b,
1038 struct list_head *pages,
1039 unsigned int *n_pages,
1040 enum vmballoon_page_size_type page_size,
1041 unsigned int n_req_pages)
1042 {
1043 struct page *page, *tmp;
1044 unsigned int i = 0;
1045 unsigned long flags;
1046
1047
1048 if (page_size == VMW_BALLOON_4K_PAGE) {
1049 *n_pages = balloon_page_list_dequeue(&b->b_dev_info, pages,
1050 n_req_pages);
1051 return;
1052 }
1053
1054
1055 spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1056 list_for_each_entry_safe(page, tmp, &b->huge_pages, lru) {
1057 vmballoon_mark_page_online(page, VMW_BALLOON_2M_PAGE);
1058
1059 list_move(&page->lru, pages);
1060 if (++i == n_req_pages)
1061 break;
1062 }
1063
1064 __count_vm_events(BALLOON_DEFLATE,
1065 i * vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1066 spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1067 *n_pages = i;
1068 }
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080 static void vmballoon_split_refused_pages(struct vmballoon_ctl *ctl)
1081 {
1082 struct page *page, *tmp;
1083 unsigned int i, order;
1084
1085 order = vmballoon_page_order(ctl->page_size);
1086
1087 list_for_each_entry_safe(page, tmp, &ctl->refused_pages, lru) {
1088 list_del(&page->lru);
1089 split_page(page, order);
1090 for (i = 0; i < (1 << order); i++)
1091 list_add(&page[i].lru, &ctl->prealloc_pages);
1092 }
1093 ctl->n_refused_pages = 0;
1094 }
1095
1096
1097
1098
1099
1100
1101 static void vmballoon_inflate(struct vmballoon *b)
1102 {
1103 int64_t to_inflate_frames;
1104 struct vmballoon_ctl ctl = {
1105 .pages = LIST_HEAD_INIT(ctl.pages),
1106 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
1107 .prealloc_pages = LIST_HEAD_INIT(ctl.prealloc_pages),
1108 .page_size = b->max_page_size,
1109 .op = VMW_BALLOON_INFLATE
1110 };
1111
1112 while ((to_inflate_frames = vmballoon_change(b)) > 0) {
1113 unsigned int to_inflate_pages, page_in_frames;
1114 int alloc_error, lock_error = 0;
1115
1116 VM_BUG_ON(!list_empty(&ctl.pages));
1117 VM_BUG_ON(ctl.n_pages != 0);
1118
1119 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
1120
1121 to_inflate_pages = min_t(unsigned long, b->batch_max_pages,
1122 DIV_ROUND_UP_ULL(to_inflate_frames,
1123 page_in_frames));
1124
1125
1126 alloc_error = vmballoon_alloc_page_list(b, &ctl,
1127 to_inflate_pages);
1128
1129
1130 lock_error = vmballoon_lock(b, &ctl);
1131
1132
1133
1134
1135
1136 if (lock_error)
1137 break;
1138
1139
1140 atomic64_add(ctl.n_pages * page_in_frames, &b->size);
1141
1142 vmballoon_enqueue_page_list(b, &ctl.pages, &ctl.n_pages,
1143 ctl.page_size);
1144
1145
1146
1147
1148
1149 if (alloc_error ||
1150 ctl.n_refused_pages >= VMW_BALLOON_MAX_REFUSED) {
1151 if (ctl.page_size == VMW_BALLOON_4K_PAGE)
1152 break;
1153
1154
1155
1156
1157
1158 vmballoon_split_refused_pages(&ctl);
1159 ctl.page_size--;
1160 }
1161
1162 cond_resched();
1163 }
1164
1165
1166
1167
1168
1169
1170 if (ctl.n_refused_pages != 0)
1171 vmballoon_release_refused_pages(b, &ctl);
1172
1173 vmballoon_release_page_list(&ctl.prealloc_pages, NULL, ctl.page_size);
1174 }
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188 static unsigned long vmballoon_deflate(struct vmballoon *b, uint64_t n_frames,
1189 bool coordinated)
1190 {
1191 unsigned long deflated_frames = 0;
1192 unsigned long tried_frames = 0;
1193 struct vmballoon_ctl ctl = {
1194 .pages = LIST_HEAD_INIT(ctl.pages),
1195 .refused_pages = LIST_HEAD_INIT(ctl.refused_pages),
1196 .page_size = VMW_BALLOON_4K_PAGE,
1197 .op = VMW_BALLOON_DEFLATE
1198 };
1199
1200
1201 while (true) {
1202 unsigned int to_deflate_pages, n_unlocked_frames;
1203 unsigned int page_in_frames;
1204 int64_t to_deflate_frames;
1205 bool deflated_all;
1206
1207 page_in_frames = vmballoon_page_in_frames(ctl.page_size);
1208
1209 VM_BUG_ON(!list_empty(&ctl.pages));
1210 VM_BUG_ON(ctl.n_pages);
1211 VM_BUG_ON(!list_empty(&ctl.refused_pages));
1212 VM_BUG_ON(ctl.n_refused_pages);
1213
1214
1215
1216
1217
1218
1219 to_deflate_frames = n_frames ? n_frames - tried_frames :
1220 -vmballoon_change(b);
1221
1222
1223 if (to_deflate_frames <= 0)
1224 break;
1225
1226
1227
1228
1229
1230 to_deflate_pages = min_t(unsigned long, b->batch_max_pages,
1231 DIV_ROUND_UP_ULL(to_deflate_frames,
1232 page_in_frames));
1233
1234
1235 vmballoon_dequeue_page_list(b, &ctl.pages, &ctl.n_pages,
1236 ctl.page_size, to_deflate_pages);
1237
1238
1239
1240
1241
1242 tried_frames += ctl.n_pages * page_in_frames;
1243
1244
1245
1246
1247
1248
1249
1250
1251 if (coordinated)
1252 vmballoon_lock(b, &ctl);
1253
1254
1255
1256
1257
1258
1259
1260 deflated_all = (ctl.n_pages == to_deflate_pages);
1261
1262
1263 n_unlocked_frames = ctl.n_pages * page_in_frames;
1264 atomic64_sub(n_unlocked_frames, &b->size);
1265 deflated_frames += n_unlocked_frames;
1266
1267 vmballoon_stats_page_add(b, VMW_BALLOON_PAGE_STAT_FREE,
1268 ctl.page_size, ctl.n_pages);
1269
1270
1271 vmballoon_release_page_list(&ctl.pages, &ctl.n_pages,
1272 ctl.page_size);
1273
1274
1275 vmballoon_enqueue_page_list(b, &ctl.refused_pages,
1276 &ctl.n_refused_pages,
1277 ctl.page_size);
1278
1279
1280 if (!deflated_all) {
1281 if (ctl.page_size == b->max_page_size)
1282 break;
1283 ctl.page_size++;
1284 }
1285
1286 cond_resched();
1287 }
1288
1289 return deflated_frames;
1290 }
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300 static void vmballoon_deinit_batching(struct vmballoon *b)
1301 {
1302 free_page((unsigned long)b->batch_page);
1303 b->batch_page = NULL;
1304 static_branch_disable(&vmw_balloon_batching);
1305 b->batch_max_pages = 1;
1306 }
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318 static int vmballoon_init_batching(struct vmballoon *b)
1319 {
1320 struct page *page;
1321
1322 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1323 if (!page)
1324 return -ENOMEM;
1325
1326 b->batch_page = page_address(page);
1327 b->batch_max_pages = PAGE_SIZE / sizeof(struct vmballoon_batch_entry);
1328
1329 static_branch_enable(&vmw_balloon_batching);
1330
1331 return 0;
1332 }
1333
1334
1335
1336
1337 static void vmballoon_doorbell(void *client_data)
1338 {
1339 struct vmballoon *b = client_data;
1340
1341 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_DOORBELL);
1342
1343 mod_delayed_work(system_freezable_wq, &b->dwork, 0);
1344 }
1345
1346
1347
1348
1349 static void vmballoon_vmci_cleanup(struct vmballoon *b)
1350 {
1351 vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1352 VMCI_INVALID_ID, VMCI_INVALID_ID);
1353
1354 if (!vmci_handle_is_invalid(b->vmci_doorbell)) {
1355 vmci_doorbell_destroy(b->vmci_doorbell);
1356 b->vmci_doorbell = VMCI_INVALID_HANDLE;
1357 }
1358 }
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370 static int vmballoon_vmci_init(struct vmballoon *b)
1371 {
1372 unsigned long error;
1373
1374 if ((b->capabilities & VMW_BALLOON_SIGNALLED_WAKEUP_CMD) == 0)
1375 return 0;
1376
1377 error = vmci_doorbell_create(&b->vmci_doorbell, VMCI_FLAG_DELAYED_CB,
1378 VMCI_PRIVILEGE_FLAG_RESTRICTED,
1379 vmballoon_doorbell, b);
1380
1381 if (error != VMCI_SUCCESS)
1382 goto fail;
1383
1384 error = __vmballoon_cmd(b, VMW_BALLOON_CMD_VMCI_DOORBELL_SET,
1385 b->vmci_doorbell.context,
1386 b->vmci_doorbell.resource, NULL);
1387
1388 if (error != VMW_BALLOON_SUCCESS)
1389 goto fail;
1390
1391 return 0;
1392 fail:
1393 vmballoon_vmci_cleanup(b);
1394 return -EIO;
1395 }
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406 static void vmballoon_pop(struct vmballoon *b)
1407 {
1408 unsigned long size;
1409
1410 while ((size = atomic64_read(&b->size)))
1411 vmballoon_deflate(b, size, false);
1412 }
1413
1414
1415
1416
1417
1418
1419 static void vmballoon_reset(struct vmballoon *b)
1420 {
1421 int error;
1422
1423 down_write(&b->conf_sem);
1424
1425 vmballoon_vmci_cleanup(b);
1426
1427
1428 vmballoon_pop(b);
1429
1430 if (vmballoon_send_start(b, VMW_BALLOON_CAPABILITIES))
1431 goto unlock;
1432
1433 if ((b->capabilities & VMW_BALLOON_BATCHED_CMDS) != 0) {
1434 if (vmballoon_init_batching(b)) {
1435
1436
1437
1438
1439
1440
1441 vmballoon_send_start(b, 0);
1442 goto unlock;
1443 }
1444 } else if ((b->capabilities & VMW_BALLOON_BASIC_CMDS) != 0) {
1445 vmballoon_deinit_batching(b);
1446 }
1447
1448 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_RESET);
1449 b->reset_required = false;
1450
1451 error = vmballoon_vmci_init(b);
1452 if (error)
1453 pr_err_once("failed to initialize vmci doorbell\n");
1454
1455 if (vmballoon_send_guest_id(b))
1456 pr_err_once("failed to send guest ID to the host\n");
1457
1458 unlock:
1459 up_write(&b->conf_sem);
1460 }
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470 static void vmballoon_work(struct work_struct *work)
1471 {
1472 struct delayed_work *dwork = to_delayed_work(work);
1473 struct vmballoon *b = container_of(dwork, struct vmballoon, dwork);
1474 int64_t change = 0;
1475
1476 if (b->reset_required)
1477 vmballoon_reset(b);
1478
1479 down_read(&b->conf_sem);
1480
1481
1482
1483
1484
1485
1486 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_TIMER);
1487
1488 if (!vmballoon_send_get_target(b))
1489 change = vmballoon_change(b);
1490
1491 if (change != 0) {
1492 pr_debug("%s - size: %llu, target %lu\n", __func__,
1493 atomic64_read(&b->size), READ_ONCE(b->target));
1494
1495 if (change > 0)
1496 vmballoon_inflate(b);
1497 else
1498 vmballoon_deflate(b, 0, true);
1499 }
1500
1501 up_read(&b->conf_sem);
1502
1503
1504
1505
1506
1507 queue_delayed_work(system_freezable_wq,
1508 dwork, round_jiffies_relative(HZ));
1509
1510 }
1511
1512
1513
1514
1515
1516
1517
1518
1519 static unsigned long vmballoon_shrinker_scan(struct shrinker *shrinker,
1520 struct shrink_control *sc)
1521 {
1522 struct vmballoon *b = &balloon;
1523 unsigned long deflated_frames;
1524
1525 pr_debug("%s - size: %llu", __func__, atomic64_read(&b->size));
1526
1527 vmballoon_stats_gen_inc(b, VMW_BALLOON_STAT_SHRINK);
1528
1529
1530
1531
1532
1533 if (!down_read_trylock(&b->conf_sem))
1534 return 0;
1535
1536 deflated_frames = vmballoon_deflate(b, sc->nr_to_scan, true);
1537
1538 vmballoon_stats_gen_add(b, VMW_BALLOON_STAT_SHRINK_FREE,
1539 deflated_frames);
1540
1541
1542
1543
1544
1545
1546 WRITE_ONCE(b->shrink_timeout, jiffies + HZ * VMBALLOON_SHRINK_DELAY);
1547
1548 up_read(&b->conf_sem);
1549
1550 return deflated_frames;
1551 }
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561 static unsigned long vmballoon_shrinker_count(struct shrinker *shrinker,
1562 struct shrink_control *sc)
1563 {
1564 struct vmballoon *b = &balloon;
1565
1566 return atomic64_read(&b->size);
1567 }
1568
1569 static void vmballoon_unregister_shrinker(struct vmballoon *b)
1570 {
1571 if (b->shrinker_registered)
1572 unregister_shrinker(&b->shrinker);
1573 b->shrinker_registered = false;
1574 }
1575
1576 static int vmballoon_register_shrinker(struct vmballoon *b)
1577 {
1578 int r;
1579
1580
1581 if (!vmwballoon_shrinker_enable)
1582 return 0;
1583
1584 b->shrinker.scan_objects = vmballoon_shrinker_scan;
1585 b->shrinker.count_objects = vmballoon_shrinker_count;
1586 b->shrinker.seeks = DEFAULT_SEEKS;
1587
1588 r = register_shrinker(&b->shrinker, "vmw-balloon");
1589
1590 if (r == 0)
1591 b->shrinker_registered = true;
1592
1593 return r;
1594 }
1595
1596
1597
1598
1599 #ifdef CONFIG_DEBUG_FS
1600
1601 static const char * const vmballoon_stat_page_names[] = {
1602 [VMW_BALLOON_PAGE_STAT_ALLOC] = "alloc",
1603 [VMW_BALLOON_PAGE_STAT_ALLOC_FAIL] = "allocFail",
1604 [VMW_BALLOON_PAGE_STAT_REFUSED_ALLOC] = "errAlloc",
1605 [VMW_BALLOON_PAGE_STAT_REFUSED_FREE] = "errFree",
1606 [VMW_BALLOON_PAGE_STAT_FREE] = "free"
1607 };
1608
1609 static const char * const vmballoon_stat_names[] = {
1610 [VMW_BALLOON_STAT_TIMER] = "timer",
1611 [VMW_BALLOON_STAT_DOORBELL] = "doorbell",
1612 [VMW_BALLOON_STAT_RESET] = "reset",
1613 [VMW_BALLOON_STAT_SHRINK] = "shrink",
1614 [VMW_BALLOON_STAT_SHRINK_FREE] = "shrinkFree"
1615 };
1616
1617 static int vmballoon_enable_stats(struct vmballoon *b)
1618 {
1619 int r = 0;
1620
1621 down_write(&b->conf_sem);
1622
1623
1624 if (b->stats)
1625 goto out;
1626
1627 b->stats = kzalloc(sizeof(*b->stats), GFP_KERNEL);
1628
1629 if (!b->stats) {
1630
1631 r = -ENOMEM;
1632 goto out;
1633 }
1634 static_key_enable(&balloon_stat_enabled.key);
1635 out:
1636 up_write(&b->conf_sem);
1637 return r;
1638 }
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651 static int vmballoon_debug_show(struct seq_file *f, void *offset)
1652 {
1653 struct vmballoon *b = f->private;
1654 int i, j;
1655
1656
1657 if (!b->stats) {
1658 int r = vmballoon_enable_stats(b);
1659
1660 if (r)
1661 return r;
1662 }
1663
1664
1665 seq_printf(f, "%-22s: %#16x\n", "balloon capabilities",
1666 VMW_BALLOON_CAPABILITIES);
1667 seq_printf(f, "%-22s: %#16lx\n", "used capabilities", b->capabilities);
1668 seq_printf(f, "%-22s: %16s\n", "is resetting",
1669 b->reset_required ? "y" : "n");
1670
1671
1672 seq_printf(f, "%-22s: %16lu\n", "target", READ_ONCE(b->target));
1673 seq_printf(f, "%-22s: %16llu\n", "current", atomic64_read(&b->size));
1674
1675 for (i = 0; i < VMW_BALLOON_CMD_NUM; i++) {
1676 if (vmballoon_cmd_names[i] == NULL)
1677 continue;
1678
1679 seq_printf(f, "%-22s: %16llu (%llu failed)\n",
1680 vmballoon_cmd_names[i],
1681 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_STAT]),
1682 atomic64_read(&b->stats->ops[i][VMW_BALLOON_OP_FAIL_STAT]));
1683 }
1684
1685 for (i = 0; i < VMW_BALLOON_STAT_NUM; i++)
1686 seq_printf(f, "%-22s: %16llu\n",
1687 vmballoon_stat_names[i],
1688 atomic64_read(&b->stats->general_stat[i]));
1689
1690 for (i = 0; i < VMW_BALLOON_PAGE_STAT_NUM; i++) {
1691 for (j = 0; j < VMW_BALLOON_NUM_PAGE_SIZES; j++)
1692 seq_printf(f, "%-18s(%s): %16llu\n",
1693 vmballoon_stat_page_names[i],
1694 vmballoon_page_size_names[j],
1695 atomic64_read(&b->stats->page_stat[i][j]));
1696 }
1697
1698 return 0;
1699 }
1700
1701 DEFINE_SHOW_ATTRIBUTE(vmballoon_debug);
1702
1703 static void __init vmballoon_debugfs_init(struct vmballoon *b)
1704 {
1705 debugfs_create_file("vmmemctl", S_IRUGO, NULL, b,
1706 &vmballoon_debug_fops);
1707 }
1708
1709 static void __exit vmballoon_debugfs_exit(struct vmballoon *b)
1710 {
1711 static_key_disable(&balloon_stat_enabled.key);
1712 debugfs_remove(debugfs_lookup("vmmemctl", NULL));
1713 kfree(b->stats);
1714 b->stats = NULL;
1715 }
1716
1717 #else
1718
1719 static inline void vmballoon_debugfs_init(struct vmballoon *b)
1720 {
1721 }
1722
1723 static inline void vmballoon_debugfs_exit(struct vmballoon *b)
1724 {
1725 }
1726
1727 #endif
1728
1729
1730 #ifdef CONFIG_BALLOON_COMPACTION
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745 static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
1746 struct page *newpage, struct page *page,
1747 enum migrate_mode mode)
1748 {
1749 unsigned long status, flags;
1750 struct vmballoon *b;
1751 int ret;
1752
1753 b = container_of(b_dev_info, struct vmballoon, b_dev_info);
1754
1755
1756
1757
1758
1759 if (!down_read_trylock(&b->conf_sem))
1760 return -EAGAIN;
1761
1762 spin_lock(&b->comm_lock);
1763
1764
1765
1766
1767
1768
1769
1770 vmballoon_add_page(b, 0, page);
1771 status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
1772 VMW_BALLOON_DEFLATE);
1773
1774 if (status == VMW_BALLOON_SUCCESS)
1775 status = vmballoon_status_page(b, 0, &page);
1776
1777
1778
1779
1780
1781 if (status != VMW_BALLOON_SUCCESS) {
1782 spin_unlock(&b->comm_lock);
1783 ret = -EBUSY;
1784 goto out_unlock;
1785 }
1786
1787
1788
1789
1790
1791
1792 balloon_page_delete(page);
1793
1794 put_page(page);
1795
1796
1797 vmballoon_add_page(b, 0, newpage);
1798 status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
1799 VMW_BALLOON_INFLATE);
1800
1801 if (status == VMW_BALLOON_SUCCESS)
1802 status = vmballoon_status_page(b, 0, &newpage);
1803
1804 spin_unlock(&b->comm_lock);
1805
1806 if (status != VMW_BALLOON_SUCCESS) {
1807
1808
1809
1810
1811
1812
1813 atomic64_dec(&b->size);
1814 ret = -EBUSY;
1815 } else {
1816
1817
1818
1819
1820 get_page(newpage);
1821 ret = MIGRATEPAGE_SUCCESS;
1822 }
1823
1824
1825 spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1826
1827
1828
1829
1830
1831
1832 if (ret == MIGRATEPAGE_SUCCESS) {
1833 balloon_page_insert(&b->b_dev_info, newpage);
1834 __count_vm_event(BALLOON_MIGRATE);
1835 }
1836
1837
1838
1839
1840
1841 b->b_dev_info.isolated_pages--;
1842 spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1843
1844 out_unlock:
1845 up_read(&b->conf_sem);
1846 return ret;
1847 }
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860 static __init void vmballoon_compaction_init(struct vmballoon *b)
1861 {
1862 b->b_dev_info.migratepage = vmballoon_migratepage;
1863 }
1864
1865 #else
1866 static inline void vmballoon_compaction_init(struct vmballoon *b)
1867 {
1868 }
1869 #endif
1870
1871 static int __init vmballoon_init(void)
1872 {
1873 int error;
1874
1875
1876
1877
1878
1879 if (x86_hyper_type != X86_HYPER_VMWARE)
1880 return -ENODEV;
1881
1882 INIT_DELAYED_WORK(&balloon.dwork, vmballoon_work);
1883
1884 error = vmballoon_register_shrinker(&balloon);
1885 if (error)
1886 goto fail;
1887
1888
1889
1890
1891
1892 balloon_devinfo_init(&balloon.b_dev_info);
1893 vmballoon_compaction_init(&balloon);
1894
1895 INIT_LIST_HEAD(&balloon.huge_pages);
1896 spin_lock_init(&balloon.comm_lock);
1897 init_rwsem(&balloon.conf_sem);
1898 balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
1899 balloon.batch_page = NULL;
1900 balloon.page = NULL;
1901 balloon.reset_required = true;
1902
1903 queue_delayed_work(system_freezable_wq, &balloon.dwork, 0);
1904
1905 vmballoon_debugfs_init(&balloon);
1906
1907 return 0;
1908 fail:
1909 vmballoon_unregister_shrinker(&balloon);
1910 return error;
1911 }
1912
1913
1914
1915
1916
1917
1918
1919 late_initcall(vmballoon_init);
1920
1921 static void __exit vmballoon_exit(void)
1922 {
1923 vmballoon_unregister_shrinker(&balloon);
1924 vmballoon_vmci_cleanup(&balloon);
1925 cancel_delayed_work_sync(&balloon.dwork);
1926
1927 vmballoon_debugfs_exit(&balloon);
1928
1929
1930
1931
1932
1933
1934 vmballoon_send_start(&balloon, 0);
1935 vmballoon_pop(&balloon);
1936 }
1937 module_exit(vmballoon_exit);