Skip to content

Commit b397897

Browse files
committed
Merge tag 'kvm-x86-fixes-7.1-rc6' of https://github.com/kvm-x86/linux into HEAD
KVM x86 fixes for 7.1-rcN - Include the kernel's linux/mman.h in KVM selftests to ensure MADV_COLLAPSE is defined, as older libc versions may not provide it. - Include execinfo.h if and only if KVM selftests are building against glibc, and provide a test_dump_stack() for non-glibc builds. - Fudge around an RCU splat in the emegerncy reboot code that is technically a legitimate flaw, but in practice is a non-issue and fixing the flaw, e.g. by adding locking, would incur meaningful risk, i.e. do more harm than good. - Rate-limit global clock updates once again (but without delayed work), as KVM was subtly relying on the old rate-limiting for NPT correction to guard against "update storms" when running without a master clock on systems with overcommitted CPUs. - Fix a brown paper bag goof where KVM checked if ERAPS is "dirty" instead of marking it dirty when emulating INVPCID. - Flush the TLB when transitioning from xAVIC => x2AVIC to ensure the CPU TLB doesn't contain AVIC-tagged entries for the APIC base GPA.
2 parents e7ae89a + a9e18aa commit b397897

14 files changed

Lines changed: 79 additions & 20 deletions

File tree

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,7 @@ struct kvm_arch {
15041504
bool use_master_clock;
15051505
u64 master_kernel_ns;
15061506
u64 master_cycle_now;
1507+
struct ratelimit_state kvmclock_update_rs;
15071508

15081509
#ifdef CONFIG_KVM_HYPERV
15091510
struct kvm_hv hyperv;

arch/x86/kvm/svm/avic.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,35 @@ static void avic_activate_vmcb(struct vcpu_svm *svm)
206206

207207
svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
208208

209+
/*
210+
* Flush the TLB when enabling (x2)AVIC and when transitioning between
211+
* xAVIC and x2AVIC, as the CPU may have inserted a TLB entry for the
212+
* "wrong" mapping.
213+
*
214+
* KVM uses a per-VM "scratch" page to back the APIC memslot, because
215+
* KVM also uses per-VM page tables *and* maintains the page table (NPT
216+
* or shadow page) mappings for said memslot even if one or more vCPUs
217+
* have their local APIC hardware-disabled or are in x2APIC mode, i.e.
218+
* even if one or more vCPUs' APIC MMIO BAR is effectively disabled.
219+
*
220+
* If xAVIC is fully enabled, hardware ignores the physical address in
221+
* KVM's page tables, i.e. in the leaf SPTE for the APIC memslot, and
222+
* instead redirects the access to the AVIC backing page, i.e. to the
223+
* vCPU's virtual APIC page. If xAVIC is not enabled (APIC is either
224+
* hardware-disabled or in x2APIC mode), then guest accesses will use
225+
* the page table mapping verbatim, i.e. will access the per-VM scratch
226+
* page, as normal memory.
227+
*
228+
* In both cases, the CPU is allowed to cache TLB entries for the APIC
229+
* base GPA. So, KVM needs to flush the TLB when enabling xAVIC, as
230+
* accesses need to be redirected to the virtual APIC page, but the TLB
231+
* may contain entries pointing at the scratch page. KVM also needs to
232+
* flush the TLB when enabling x2AVIC, as accesses need to go to the
233+
* scratch page, but the TLB may contain entries tagged as xAVIC, i.e.
234+
* entries pointing to the vCPU's virtual APIC page.
235+
*/
236+
kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, &svm->vcpu);
237+
209238
/*
210239
* Note: KVM supports hybrid-AVIC mode, where KVM emulates x2APIC MSR
211240
* accesses, while interrupt injection to a running vCPU can be
@@ -219,12 +248,6 @@ static void avic_activate_vmcb(struct vcpu_svm *svm)
219248
/* Disabling MSR intercept for x2APIC registers */
220249
avic_set_x2apic_msr_interception(svm, false);
221250
} else {
222-
/*
223-
* Flush the TLB, the guest may have inserted a non-APIC
224-
* mapping into the TLB while AVIC was disabled.
225-
*/
226-
kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, &svm->vcpu);
227-
228251
/* Enabling MSR intercept for x2APIC registers */
229252
avic_set_x2apic_msr_interception(svm, true);
230253
}

arch/x86/kvm/x86.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5227,8 +5227,13 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
52275227
* On a host with synchronized TSC, there is no need to update
52285228
* kvmclock on vcpu->cpu migration
52295229
*/
5230-
if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
5231-
kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
5230+
if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) {
5231+
if (__ratelimit(&vcpu->kvm->arch.kvmclock_update_rs))
5232+
kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
5233+
else
5234+
kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
5235+
}
5236+
52325237
if (vcpu->cpu != cpu)
52335238
kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
52345239
vcpu->cpu = cpu;
@@ -13366,6 +13371,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1336613371
raw_spin_lock_init(&kvm->arch.tsc_write_lock);
1336713372
mutex_init(&kvm->arch.apic_map_lock);
1336813373
seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock);
13374+
ratelimit_state_init(&kvm->arch.kvmclock_update_rs, HZ, 10);
13375+
ratelimit_set_flags(&kvm->arch.kvmclock_update_rs, RATELIMIT_MSG_ON_RELEASE);
1336913376
kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
1337013377

1337113378
raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
@@ -14323,7 +14330,7 @@ int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)
1432314330
* the RAP (Return Address Predicator).
1432414331
*/
1432514332
if (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS))
14326-
kvm_register_is_dirty(vcpu, VCPU_EXREG_ERAPS);
14333+
kvm_register_mark_dirty(vcpu, VCPU_EXREG_ERAPS);
1432714334

1432814335
kvm_invalidate_pcid(vcpu, operand.pcid);
1432914336
return kvm_skip_emulated_instruction(vcpu);

arch/x86/virt/hw.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@ static void x86_virt_invoke_kvm_emergency_callback(void)
4949
{
5050
cpu_emergency_virt_cb *kvm_callback;
5151

52-
kvm_callback = rcu_dereference(kvm_emergency_callback);
52+
/*
53+
* RCU may not be watching the crashing CPU here, so rcu_dereference()
54+
* triggers a suspicious-RCU-usage splat. In principle, a concurrent
55+
* KVM module unload could race with this read; see commit 2baa33a8ddd6
56+
* ("KVM: x86: Leave user-return notifier registered on reboot/shutdown")
57+
* which notes that nothing prevents module unload during panic/reboot.
58+
*
59+
* However, taking a lock here would be riskier than the current race:
60+
* the system is going down via NMI shootdown, and any lock could be
61+
* held by an already-stopped CPU. Use rcu_dereference_raw() to silence
62+
* the lockdep splat and accept the comically small remaining race;
63+
* panic context inherently cannot guarantee complete correctness.
64+
*/
65+
kvm_callback = rcu_dereference_raw(kvm_emergency_callback);
5366
if (kvm_callback)
5467
kvm_callback();
5568
}

tools/testing/selftests/kvm/access_tracking_perf_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
#include <inttypes.h>
4242
#include <limits.h>
4343
#include <pthread.h>
44-
#include <sys/mman.h>
4544
#include <sys/types.h>
4645
#include <sys/stat.h>
4746

47+
#include "kvm_syscalls.h"
4848
#include "kvm_util.h"
4949
#include "test_util.h"
5050
#include "memstress.h"

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#include <linux/bitmap.h>
1515
#include <linux/falloc.h>
1616
#include <linux/sizes.h>
17-
#include <sys/mman.h>
1817
#include <sys/types.h>
1918
#include <sys/stat.h>
2019

20+
#include "kvm_syscalls.h"
2121
#include "kvm_util.h"
2222
#include "numaif.h"
2323
#include "test_util.h"

tools/testing/selftests/kvm/include/kvm_syscalls.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
#ifndef SELFTEST_KVM_SYSCALLS_H
33
#define SELFTEST_KVM_SYSCALLS_H
44

5+
/*
6+
* Include both the kernel and libc versions of mman.h. The kernel provides
7+
* the most up-to-date flags and definitions, while libc provides the syscall
8+
* wrappers tests expect.
9+
*/
10+
#include <linux/mman.h>
11+
12+
#include <sys/mman.h>
513
#include <sys/syscall.h>
614

15+
#include <test_util.h>
16+
717
#define MAP_ARGS0(m,...)
818
#define MAP_ARGS1(m,t,a,...) m(t,a)
919
#define MAP_ARGS2(m,t,a,...) m(t,a), MAP_ARGS1(m,__VA_ARGS__)

tools/testing/selftests/kvm/include/test_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include <errno.h>
2020
#include <unistd.h>
2121
#include <fcntl.h>
22-
#include <sys/mman.h>
2322
#include "kselftest.h"
2423

24+
#include <linux/mman.h>
2525
#include <linux/types.h>
2626

2727
#define msecs_to_usecs(msec) ((msec) * 1000ULL)

tools/testing/selftests/kvm/lib/assert.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
*/
77
#include "test_util.h"
88

9-
#include <execinfo.h>
9+
1010
#include <sys/syscall.h>
1111

1212
#include "kselftest.h"
1313

14+
#ifdef __GLIBC__
15+
#include <execinfo.h>
16+
1417
/* Dumps the current stack trace to stderr. */
1518
static void __attribute__((noinline)) test_dump_stack(void);
1619
static void test_dump_stack(void)
@@ -57,6 +60,9 @@ static void test_dump_stack(void)
5760
system(cmd);
5861
#pragma GCC diagnostic pop
5962
}
63+
#else
64+
static void test_dump_stack(void) {}
65+
#endif
6066

6167
static pid_t _gettid(void)
6268
{

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* Copyright (C) 2018, Google LLC.
66
*/
77
#include "test_util.h"
8+
#include "kvm_syscalls.h"
89
#include "kvm_util.h"
910
#include "processor.h"
1011
#include "ucall_common.h"
1112

1213
#include <assert.h>
1314
#include <sched.h>
14-
#include <sys/mman.h>
1515
#include <sys/resource.h>
1616
#include <sys/types.h>
1717
#include <sys/stat.h>

0 commit comments

Comments
 (0)