Skip to content

Commit bc8a3d8

Browse files
Ben Gardonbonzini
authored andcommitted
kvm: mmu: Fix overflow on kvm mmu page limit calculation
KVM bases its memory usage limits on the total number of guest pages across all memslots. However, those limits, and the calculations to produce them, use 32 bit unsigned integers. This can result in overflow if a VM has more guest pages that can be represented by a u32. As a result of this overflow, KVM can use a low limit on the number of MMU pages it will allocate. This makes KVM unable to map all of guest memory at once, prompting spurious faults. Tested: Ran all kvm-unit-tests on an Intel Haswell machine. This patch introduced no new failures. Signed-off-by: Ben Gardon <bgardon@google.com> Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 2b27924 commit bc8a3d8

4 files changed

Lines changed: 15 additions & 16 deletions

File tree

‎arch/x86/include/asm/kvm_host.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
126126
}
127127

128128
#define KVM_PERMILLE_MMU_PAGES 20
129-
#define KVM_MIN_ALLOC_MMU_PAGES 64
129+
#define KVM_MIN_ALLOC_MMU_PAGES 64UL
130130
#define KVM_MMU_HASH_SHIFT 12
131131
#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
132132
#define KVM_MIN_FREE_MMU_PAGES 5
@@ -844,9 +844,9 @@ enum kvm_irqchip_mode {
844844
};
845845

846846
struct kvm_arch {
847-
unsigned int n_used_mmu_pages;
848-
unsigned int n_requested_mmu_pages;
849-
unsigned int n_max_mmu_pages;
847+
unsigned long n_used_mmu_pages;
848+
unsigned long n_requested_mmu_pages;
849+
unsigned long n_max_mmu_pages;
850850
unsigned int indirect_shadow_pages;
851851
struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
852852
/*
@@ -1256,8 +1256,8 @@ void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
12561256
gfn_t gfn_offset, unsigned long mask);
12571257
void kvm_mmu_zap_all(struct kvm *kvm);
12581258
void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen);
1259-
unsigned int kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm);
1260-
void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
1259+
unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm);
1260+
void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages);
12611261

12621262
int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3);
12631263
bool pdptrs_changed(struct kvm_vcpu *vcpu);

‎arch/x86/kvm/mmu.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,7 +2007,7 @@ static int is_empty_shadow_page(u64 *spt)
20072007
* aggregate version in order to make the slab shrinker
20082008
* faster
20092009
*/
2010-
static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
2010+
static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, unsigned long nr)
20112011
{
20122012
kvm->arch.n_used_mmu_pages += nr;
20132013
percpu_counter_add(&kvm_total_used_mmu_pages, nr);
@@ -2763,7 +2763,7 @@ static bool prepare_zap_oldest_mmu_page(struct kvm *kvm,
27632763
* Changing the number of mmu pages allocated to the vm
27642764
* Note: if goal_nr_mmu_pages is too small, you will get dead lock
27652765
*/
2766-
void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
2766+
void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long goal_nr_mmu_pages)
27672767
{
27682768
LIST_HEAD(invalid_list);
27692769

@@ -6031,10 +6031,10 @@ int kvm_mmu_module_init(void)
60316031
/*
60326032
* Calculate mmu pages needed for kvm.
60336033
*/
6034-
unsigned int kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm)
6034+
unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm)
60356035
{
6036-
unsigned int nr_mmu_pages;
6037-
unsigned int nr_pages = 0;
6036+
unsigned long nr_mmu_pages;
6037+
unsigned long nr_pages = 0;
60386038
struct kvm_memslots *slots;
60396039
struct kvm_memory_slot *memslot;
60406040
int i;
@@ -6047,8 +6047,7 @@ unsigned int kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm)
60476047
}
60486048

60496049
nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
6050-
nr_mmu_pages = max(nr_mmu_pages,
6051-
(unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
6050+
nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES);
60526051

60536052
return nr_mmu_pages;
60546053
}

‎arch/x86/kvm/mmu.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu);
6464
int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
6565
u64 fault_address, char *insn, int insn_len);
6666

67-
static inline unsigned int kvm_mmu_available_pages(struct kvm *kvm)
67+
static inline unsigned long kvm_mmu_available_pages(struct kvm *kvm)
6868
{
6969
if (kvm->arch.n_max_mmu_pages > kvm->arch.n_used_mmu_pages)
7070
return kvm->arch.n_max_mmu_pages -

‎arch/x86/kvm/x86.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4270,7 +4270,7 @@ static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
42704270
}
42714271

42724272
static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
4273-
u32 kvm_nr_mmu_pages)
4273+
unsigned long kvm_nr_mmu_pages)
42744274
{
42754275
if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
42764276
return -EINVAL;
@@ -4284,7 +4284,7 @@ static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
42844284
return 0;
42854285
}
42864286

4287-
static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
4287+
static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
42884288
{
42894289
return kvm->arch.n_max_mmu_pages;
42904290
}

0 commit comments

Comments
 (0)