Skip to content

Commit f5396f2

Browse files
committed
Merge branch 'kvm-5.16-fixes' into kvm-master
* Fix misuse of gfn-to-pfn cache when recording guest steal time / preempted status * Fix selftests on APICv machines * Fix sparse warnings * Fix detection of KVM features in CPUID * Cleanups for bogus writes to MSR_KVM_PV_EOI_EN * Fixes and cleanups for MSR bitmap handling * Cleanups for INVPCID * Make x86 KVM_SOFT_MAX_VCPUS consistent with other architectures
2 parents 1f05833 + da1bfd5 commit f5396f2

File tree

20 files changed

+317
-264
lines changed

20 files changed

+317
-264
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#define __KVM_HAVE_ARCH_VCPU_DEBUGFS
3939

4040
#define KVM_MAX_VCPUS 1024
41-
#define KVM_SOFT_MAX_VCPUS 710
4241

4342
/*
4443
* In x86, the VCPU ID corresponds to the APIC ID, and APIC IDs
@@ -725,6 +724,7 @@ struct kvm_vcpu_arch {
725724

726725
int cpuid_nent;
727726
struct kvm_cpuid_entry2 *cpuid_entries;
727+
u32 kvm_cpuid_base;
728728

729729
u64 reserved_gpa_bits;
730730
int maxphyaddr;
@@ -748,7 +748,7 @@ struct kvm_vcpu_arch {
748748
u8 preempted;
749749
u64 msr_val;
750750
u64 last_steal;
751-
struct gfn_to_pfn_cache cache;
751+
struct gfn_to_hva_cache cache;
752752
} st;
753753

754754
u64 l1_tsc_offset;
@@ -1034,6 +1034,7 @@ struct kvm_x86_msr_filter {
10341034
#define APICV_INHIBIT_REASON_IRQWIN 3
10351035
#define APICV_INHIBIT_REASON_PIT_REINJ 4
10361036
#define APICV_INHIBIT_REASON_X2APIC 5
1037+
#define APICV_INHIBIT_REASON_BLOCKIRQ 6
10371038

10381039
struct kvm_arch {
10391040
unsigned long n_used_mmu_pages;

arch/x86/include/asm/processor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,14 @@ static inline u32 amd_get_nodes_per_socket(void) { return 0; }
806806
static inline u32 amd_get_highest_perf(void) { return 0; }
807807
#endif
808808

809+
#define for_each_possible_hypervisor_cpuid_base(function) \
810+
for (function = 0x40000000; function < 0x40010000; function += 0x100)
811+
809812
static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
810813
{
811814
uint32_t base, eax, signature[3];
812815

813-
for (base = 0x40000000; base < 0x40010000; base += 0x100) {
816+
for_each_possible_hypervisor_cpuid_base(base) {
814817
cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
815818

816819
if (!memcmp(sig, signature, 12) &&

arch/x86/include/uapi/asm/kvm_para.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* should be used to determine that a VM is running under KVM.
99
*/
1010
#define KVM_CPUID_SIGNATURE 0x40000000
11+
#define KVM_SIGNATURE "KVMKVMKVM\0\0\0"
1112

1213
/* This CPUID returns two feature bitmaps in eax, edx. Before enabling
1314
* a particular paravirtualization, the appropriate feature bit should

arch/x86/kernel/kvm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ static noinline uint32_t __kvm_cpuid_base(void)
809809
return 0; /* So we don't blow up on old processors */
810810

811811
if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
812-
return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
812+
return hypervisor_cpuid_base(KVM_SIGNATURE, 0);
813813

814814
return 0;
815815
}

arch/x86/kvm/cpuid.c

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,45 @@ static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent)
9999
return 0;
100100
}
101101

102-
void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
102+
static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
103103
{
104-
struct kvm_cpuid_entry2 *best;
104+
u32 function;
105+
struct kvm_cpuid_entry2 *entry;
106+
107+
vcpu->arch.kvm_cpuid_base = 0;
108+
109+
for_each_possible_hypervisor_cpuid_base(function) {
110+
entry = kvm_find_cpuid_entry(vcpu, function, 0);
105111

106-
best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
112+
if (entry) {
113+
u32 signature[3];
114+
115+
signature[0] = entry->ebx;
116+
signature[1] = entry->ecx;
117+
signature[2] = entry->edx;
118+
119+
BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
120+
if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
121+
vcpu->arch.kvm_cpuid_base = function;
122+
break;
123+
}
124+
}
125+
}
126+
}
127+
128+
struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
129+
{
130+
u32 base = vcpu->arch.kvm_cpuid_base;
131+
132+
if (!base)
133+
return NULL;
134+
135+
return kvm_find_cpuid_entry(vcpu, base | KVM_CPUID_FEATURES, 0);
136+
}
137+
138+
void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
139+
{
140+
struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
107141

108142
/*
109143
* save the feature bitmap to avoid cpuid lookup for every PV
@@ -142,7 +176,7 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
142176
cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
143177
best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
144178

145-
best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
179+
best = kvm_find_kvm_cpuid_features(vcpu);
146180
if (kvm_hlt_in_guest(vcpu->kvm) && best &&
147181
(best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
148182
best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
@@ -239,6 +273,26 @@ u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
239273
return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
240274
}
241275

276+
static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
277+
int nent)
278+
{
279+
int r;
280+
281+
r = kvm_check_cpuid(e2, nent);
282+
if (r)
283+
return r;
284+
285+
kvfree(vcpu->arch.cpuid_entries);
286+
vcpu->arch.cpuid_entries = e2;
287+
vcpu->arch.cpuid_nent = nent;
288+
289+
kvm_update_kvm_cpuid_base(vcpu);
290+
kvm_update_cpuid_runtime(vcpu);
291+
kvm_vcpu_after_set_cpuid(vcpu);
292+
293+
return 0;
294+
}
295+
242296
/* when an old userspace process fills a new kernel module */
243297
int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
244298
struct kvm_cpuid *cpuid,
@@ -275,18 +329,9 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
275329
e2[i].padding[2] = 0;
276330
}
277331

278-
r = kvm_check_cpuid(e2, cpuid->nent);
279-
if (r) {
332+
r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
333+
if (r)
280334
kvfree(e2);
281-
goto out_free_cpuid;
282-
}
283-
284-
kvfree(vcpu->arch.cpuid_entries);
285-
vcpu->arch.cpuid_entries = e2;
286-
vcpu->arch.cpuid_nent = cpuid->nent;
287-
288-
kvm_update_cpuid_runtime(vcpu);
289-
kvm_vcpu_after_set_cpuid(vcpu);
290335

291336
out_free_cpuid:
292337
kvfree(e);
@@ -310,20 +355,11 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
310355
return PTR_ERR(e2);
311356
}
312357

313-
r = kvm_check_cpuid(e2, cpuid->nent);
314-
if (r) {
358+
r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
359+
if (r)
315360
kvfree(e2);
316-
return r;
317-
}
318361

319-
kvfree(vcpu->arch.cpuid_entries);
320-
vcpu->arch.cpuid_entries = e2;
321-
vcpu->arch.cpuid_nent = cpuid->nent;
322-
323-
kvm_update_cpuid_runtime(vcpu);
324-
kvm_vcpu_after_set_cpuid(vcpu);
325-
326-
return 0;
362+
return r;
327363
}
328364

329365
int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
@@ -871,8 +907,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
871907
}
872908
break;
873909
case KVM_CPUID_SIGNATURE: {
874-
static const char signature[12] = "KVMKVMKVM\0\0";
875-
const u32 *sigptr = (const u32 *)signature;
910+
const u32 *sigptr = (const u32 *)KVM_SIGNATURE;
876911
entry->eax = KVM_CPUID_FEATURES;
877912
entry->ebx = sigptr[0];
878913
entry->ecx = sigptr[1];

arch/x86/kvm/hyperv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
14721472

14731473
if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
14741474
hv_vcpu->hv_vapic = data;
1475-
if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0))
1475+
if (kvm_lapic_set_pv_eoi(vcpu, 0, 0))
14761476
return 1;
14771477
break;
14781478
}
@@ -1490,7 +1490,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
14901490
return 1;
14911491
hv_vcpu->hv_vapic = data;
14921492
kvm_vcpu_mark_page_dirty(vcpu, gfn);
1493-
if (kvm_lapic_enable_pv_eoi(vcpu,
1493+
if (kvm_lapic_set_pv_eoi(vcpu,
14941494
gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
14951495
sizeof(struct hv_vp_assist_page)))
14961496
return 1;

arch/x86/kvm/lapic.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,25 +2856,30 @@ int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
28562856
return 0;
28572857
}
28582858

2859-
int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
2859+
int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
28602860
{
28612861
u64 addr = data & ~KVM_MSR_ENABLED;
28622862
struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
28632863
unsigned long new_len;
2864+
int ret;
28642865

28652866
if (!IS_ALIGNED(addr, 4))
28662867
return 1;
28672868

2868-
vcpu->arch.pv_eoi.msr_val = data;
2869-
if (!pv_eoi_enabled(vcpu))
2870-
return 0;
2869+
if (data & KVM_MSR_ENABLED) {
2870+
if (addr == ghc->gpa && len <= ghc->len)
2871+
new_len = ghc->len;
2872+
else
2873+
new_len = len;
28712874

2872-
if (addr == ghc->gpa && len <= ghc->len)
2873-
new_len = ghc->len;
2874-
else
2875-
new_len = len;
2875+
ret = kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
2876+
if (ret)
2877+
return ret;
2878+
}
2879+
2880+
vcpu->arch.pv_eoi.msr_val = data;
28762881

2877-
return kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
2882+
return 0;
28782883
}
28792884

28802885
int kvm_apic_accept_events(struct kvm_vcpu *vcpu)

arch/x86/kvm/lapic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
127127
int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
128128
int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
129129

130-
int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len);
130+
int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len);
131131
void kvm_lapic_exit(void);
132132

133133
#define VEC_POS(v) ((v) & (32 - 1))

arch/x86/kvm/mmu/mmu.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3191,17 +3191,17 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
31913191
new_spte |= PT_WRITABLE_MASK;
31923192

31933193
/*
3194-
* Do not fix write-permission on the large spte. Since
3195-
* we only dirty the first page into the dirty-bitmap in
3194+
* Do not fix write-permission on the large spte when
3195+
* dirty logging is enabled. Since we only dirty the
3196+
* first page into the dirty-bitmap in
31963197
* fast_pf_fix_direct_spte(), other pages are missed
31973198
* if its slot has dirty logging enabled.
31983199
*
31993200
* Instead, we let the slow page fault path create a
32003201
* normal spte to fix the access.
3201-
*
3202-
* See the comments in kvm_arch_commit_memory_region().
32033202
*/
3204-
if (sp->role.level > PG_LEVEL_4K)
3203+
if (sp->role.level > PG_LEVEL_4K &&
3204+
kvm_slot_dirty_track_enabled(fault->slot))
32053205
break;
32063206
}
32073207

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,
897897
struct kvm_page_fault *fault,
898898
struct tdp_iter *iter)
899899
{
900-
struct kvm_mmu_page *sp = sptep_to_sp(iter->sptep);
900+
struct kvm_mmu_page *sp = sptep_to_sp(rcu_dereference(iter->sptep));
901901
u64 new_spte;
902902
int ret = RET_PF_FIXED;
903903
bool wrprot = false;

0 commit comments

Comments
 (0)