History

Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm

Pull KVM fixes from Paolo Bonzini: - PPC and ARM bugfixes from submaintainers - Fix old Windows versions on AMD (recent regression) - Fix old Linux versions on processors without EPT - Fixes for LAPIC timer optimizations * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (21 commits) KVM: nVMX: Fix size checks in vmx_set_nested_state KVM: selftests: make hyperv_cpuid test pass on AMD KVM: lapic: Check for in-kernel LAPIC before deferencing apic pointer KVM: fix KVM_CLEAR_DIRTY_LOG for memory slots of unaligned size x86/kvm/mmu: reset MMU context when 32-bit guest switches PAE KVM: x86: Whitelist port 0x7e for pre-incrementing %rip Documentation: kvm: fix dirty log ioctl arch lists KVM: VMX: Move RSB stuffing to before the first RET after VM-Exit KVM: arm/arm64: Don't emulate virtual timers on userspace ioctls kvm: arm: Skip stage2 huge mappings for unaligned ipa backed by THP KVM: arm/arm64: Ensure vcpu target is unset on reset failure KVM: lapic: Convert guest TSC to host time domain if necessary KVM: lapic: Allow user to disable adaptive tuning of timer advancement KVM: lapic: Track lapic timer advance per vCPU KVM: lapic: Disable timer advancement if adaptive tuning goes haywire x86: kvm: hyper-v: deal with buggy TLB flush requests from WS2012 KVM: x86: Consider LAPIC TSC-Deadline timer expired if deadline too short KVM: PPC: Book3S: Protect memslots while validating user address KVM: PPC: Book3S HV: Perserve PSSCR FAKE_SUSPEND bit on guest exit KVM: arm/arm64: vgic-v3: Retire pending interrupts on disabling LPIs ...

Linus Torvalds committed 7 years ago

Commit aa1be082 parents 8246343 e8ab8d223 files · +192 −65Browse files

Unified · Split

23 files
  1. Documentation/virtual/kvm/api.txt +6 −5
  2. arch/powerpc/kvm/book3s_64_vio.c +3 −3
  3. arch/powerpc/kvm/book3s_hv.c +3 −1
  4. arch/x86/include/asm/kvm_host.h +1 −0
  5. arch/x86/include/uapi/asm/kvm.h +1 −0
  6. arch/x86/kvm/hyperv.c +10 −1
  7. arch/x86/kvm/lapic.c +52 −21
  8. arch/x86/kvm/lapic.h +3 −1
  9. arch/x86/kvm/mmu.c +1 −0
  10. arch/x86/kvm/vmx/nested.c +2 −2
  11. arch/x86/kvm/vmx/vmenter.S +12 −0
  12. arch/x86/kvm/vmx/vmx.c +3 −4
  13. arch/x86/kvm/x86.c +29 −7
  14. arch/x86/kvm/x86.h +0 −2
  15. tools/testing/selftests/kvm/dirty_log_test.c +6 −3
  16. tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +8 −1
  17. virt/kvm/arm/arch_timer.c +10 −7
  18. virt/kvm/arm/arm.c +8 −3
  19. virt/kvm/arm/mmu.c +5 −1
  20. virt/kvm/arm/vgic/vgic-mmio-v3.c +3 −0
  21. virt/kvm/arm/vgic/vgic.c +21 −0
  22. virt/kvm/arm/vgic/vgic.h +1 −0
  23. virt/kvm/kvm_main.c +4 −3
Documentation/virtual/kvm/api.txt+6−5
@@ -321,7 +321,7 @@ cpu's hardware control block. · 320 unmodified lines
321321 4.8 KVM_GET_DIRTY_LOG (vm ioctl)
322322
323323 Capability: basic
324−Architectures: x86
324+Architectures: all
325325 Type: vm ioctl
326326 Parameters: struct kvm_dirty_log (in/out)
327327 Returns: 0 on success, -1 on error
@@ -3810,7 +3810,7 @@ to I/O ports. · 3482 unmodified lines
38103810 4.117 KVM_CLEAR_DIRTY_LOG (vm ioctl)
38113811
38123812 Capability: KVM_CAP_MANUAL_DIRTY_LOG_PROTECT
3813−Architectures: x86
3813+Architectures: x86, arm, arm64, mips
38143814 Type: vm ioctl
38153815 Parameters: struct kvm_dirty_log (in)
38163816 Returns: 0 on success, -1 on error
@@ -3830,8 +3830,9 @@ The ioctl clears the dirty status of pages in a memory slot, according to · 13 unmodified lines
38303830 the bitmap that is passed in struct kvm_clear_dirty_log's dirty_bitmap
38313831 field. Bit 0 of the bitmap corresponds to page "first_page" in the
38323832 memory slot, and num_pages is the size in bits of the input bitmap.
3833−Both first_page and num_pages must be a multiple of 64. For each bit
3834−that is set in the input bitmap, the corresponding page is marked "clean"
3833+first_page must be a multiple of 64; num_pages must also be a multiple of
3834+64 unless first_page + num_pages is the size of the memory slot. For each
3835+bit that is set in the input bitmap, the corresponding page is marked "clean"
38353836 in KVM's dirty bitmap, and dirty tracking is re-enabled for that page
38363837 (for example via write-protection, or by clearing the dirty bit in
38373838 a page table entry).
@@ -4799,7 +4800,7 @@ and injected exceptions. · 961 unmodified lines
47994800
48004801 7.18 KVM_CAP_MANUAL_DIRTY_LOG_PROTECT
48014802
4802−Architectures: all
4803+Architectures: x86, arm, arm64, mips
48034804 Parameters: args[0] whether feature should be enabled or not
48044805
48054806 With this capability enabled, KVM_GET_DIRTY_LOG will not automatically
arch/powerpc/kvm/book3s_64_vio.c+3−3
@@ -543,14 +543,14 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn, · 542 unmodified lines
543543 if (ret != H_SUCCESS)
544544 return ret;
545545
546+ idx = srcu_read_lock(&vcpu->kvm->srcu);
547+
546548 ret = kvmppc_tce_validate(stt, tce);
547549 if (ret != H_SUCCESS)
548− return ret;
550+ goto unlock_exit;
549551
550552 dir = iommu_tce_direction(tce);
551553
552− idx = srcu_read_lock(&vcpu->kvm->srcu);
553−
554554 if ((dir != DMA_NONE) && kvmppc_tce_to_ua(vcpu->kvm, tce, &ua, NULL)) {
555555 ret = H_PARAMETER;
556556 goto unlock_exit;
arch/powerpc/kvm/book3s_hv.c+3−1
@@ -3423,7 +3423,9 @@ static int kvmhv_load_hv_regs_and_go(struct kvm_vcpu *vcpu, u64 time_limit, · 3422 unmodified lines
34233423 vcpu->arch.shregs.sprg2 = mfspr(SPRN_SPRG2);
34243424 vcpu->arch.shregs.sprg3 = mfspr(SPRN_SPRG3);
34253425
3426− mtspr(SPRN_PSSCR, host_psscr);
3426+ /* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */
3427+ mtspr(SPRN_PSSCR, host_psscr |
3428+ (local_paca->kvm_hstate.fake_suspend << PSSCR_FAKE_SUSPEND_LG));
34273429 mtspr(SPRN_HFSCR, host_hfscr);
34283430 mtspr(SPRN_CIABR, host_ciabr);
34293431 mtspr(SPRN_DAWR, host_dawr);
arch/x86/include/asm/kvm_host.h+1−0
@@ -295,6 +295,7 @@ union kvm_mmu_extended_role { · 294 unmodified lines
295295 unsigned int valid:1;
296296 unsigned int execonly:1;
297297 unsigned int cr0_pg:1;
298+ unsigned int cr4_pae:1;
298299 unsigned int cr4_pse:1;
299300 unsigned int cr4_pke:1;
300301 unsigned int cr4_smap:1;
arch/x86/include/uapi/asm/kvm.h+1−0
@@ -381,6 +381,7 @@ struct kvm_sync_regs { · 380 unmodified lines
381381 #define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0)
382382 #define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1)
383383 #define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2)
384+#define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3)
384385
385386 #define KVM_STATE_NESTED_GUEST_MODE 0x00000001
386387 #define KVM_STATE_NESTED_RUN_PENDING 0x00000002
arch/x86/kvm/hyperv.c+10−1
@@ -1371,7 +1371,16 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *current_vcpu, u64 ingpa, · 1370 unmodified lines
13711371
13721372 valid_bank_mask = BIT_ULL(0);
13731373 sparse_banks[0] = flush.processor_mask;
1374− all_cpus = flush.flags & HV_FLUSH_ALL_PROCESSORS;
1374+
1375+ /*
1376+ * Work around possible WS2012 bug: it sends hypercalls
1377+ * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
1378+ * while also expecting us to flush something and crashing if
1379+ * we don't. Let's treat processor_mask == 0 same as
1380+ * HV_FLUSH_ALL_PROCESSORS.
1381+ */
1382+ all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
1383+ flush.processor_mask == 0;
13751384 } else {
13761385 if (unlikely(kvm_read_guest(kvm, ingpa, &flush_ex,
13771386 sizeof(flush_ex))))
arch/x86/kvm/lapic.c+52−21
@@ -70,7 +70,6 @@ · 69 unmodified lines
7070 #define APIC_BROADCAST 0xFF
7171 #define X2APIC_BROADCAST 0xFFFFFFFFul
7272
73−static bool lapic_timer_advance_adjust_done = false;
7473 #define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
7574 /* step-by-step approximation to mitigate fluctuation */
7675 #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
@@ -1482,14 +1481,32 @@ static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu) · 1405 unmodified lines
14821481 return false;
14831482 }
14841483
1484+static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
1485+{
1486+ u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
1487+
1488+ /*
1489+ * If the guest TSC is running at a different ratio than the host, then
1490+ * convert the delay to nanoseconds to achieve an accurate delay. Note
1491+ * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1492+ * always for VMX enabled hardware.
1493+ */
1494+ if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) {
1495+ __delay(min(guest_cycles,
1496+ nsec_to_cycles(vcpu, timer_advance_ns)));
1497+ } else {
1498+ u64 delay_ns = guest_cycles * 1000000ULL;
1499+ do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
1500+ ndelay(min_t(u32, delay_ns, timer_advance_ns));
1501+ }
1502+}
1503+
14851504 void wait_lapic_expire(struct kvm_vcpu *vcpu)
14861505 {
14871506 struct kvm_lapic *apic = vcpu->arch.apic;
1507+ u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
14881508 u64 guest_tsc, tsc_deadline, ns;
14891509
1490− if (!lapic_in_kernel(vcpu))
1491− return;
1492−
14931510 if (apic->lapic_timer.expired_tscdeadline == 0)
14941511 return;
14951512
@@ -1501,33 +1518,37 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu) · 5 unmodified lines
15011518 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
15021519 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
15031520
1504− /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
15051521 if (guest_tsc < tsc_deadline)
1506− __delay(min(tsc_deadline - guest_tsc,
1507− nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
1522+ __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
15081523
1509− if (!lapic_timer_advance_adjust_done) {
1524+ if (!apic->lapic_timer.timer_advance_adjust_done) {
15101525 /* too early */
15111526 if (guest_tsc < tsc_deadline) {
15121527 ns = (tsc_deadline - guest_tsc) * 1000000ULL;
15131528 do_div(ns, vcpu->arch.virtual_tsc_khz);
1514− lapic_timer_advance_ns -= min((unsigned int)ns,
1515− lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1529+ timer_advance_ns -= min((u32)ns,
1530+ timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
15161531 } else {
15171532 /* too late */
15181533 ns = (guest_tsc - tsc_deadline) * 1000000ULL;
15191534 do_div(ns, vcpu->arch.virtual_tsc_khz);
1520− lapic_timer_advance_ns += min((unsigned int)ns,
1521− lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1535+ timer_advance_ns += min((u32)ns,
1536+ timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
15221537 }
15231538 if (abs(guest_tsc - tsc_deadline) < LAPIC_TIMER_ADVANCE_ADJUST_DONE)
1524− lapic_timer_advance_adjust_done = true;
1539+ apic->lapic_timer.timer_advance_adjust_done = true;
1540+ if (unlikely(timer_advance_ns > 5000)) {
1541+ timer_advance_ns = 0;
1542+ apic->lapic_timer.timer_advance_adjust_done = true;
1543+ }
1544+ apic->lapic_timer.timer_advance_ns = timer_advance_ns;
15251545 }
15261546 }
15271547
15281548 static void start_sw_tscdeadline(struct kvm_lapic *apic)
15291549 {
1530− u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1550+ struct kvm_timer *ktimer = &apic->lapic_timer;
1551+ u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
15311552 u64 ns = 0;
15321553 ktime_t expire;
15331554 struct kvm_vcpu *vcpu = apic->vcpu;
@@ -1542,13 +1563,15 @@ static void start_sw_tscdeadline(struct kvm_lapic *apic) · 8 unmodified lines
15421563
15431564 now = ktime_get();
15441565 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1545− if (likely(tscdeadline > guest_tsc)) {
1546− ns = (tscdeadline - guest_tsc) * 1000000ULL;
1547− do_div(ns, this_tsc_khz);
1566+
1567+ ns = (tscdeadline - guest_tsc) * 1000000ULL;
1568+ do_div(ns, this_tsc_khz);
1569+
1570+ if (likely(tscdeadline > guest_tsc) &&
1571+ likely(ns > apic->lapic_timer.timer_advance_ns)) {
15481572 expire = ktime_add_ns(now, ns);
1549− expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1550− hrtimer_start(&apic->lapic_timer.timer,
1551− expire, HRTIMER_MODE_ABS_PINNED);
1573+ expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
1574+ hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_PINNED);
15521575 } else
15531576 apic_timer_expired(apic);
15541577
@@ -2255,7 +2278,7 @@ static enum hrtimer_restart apic_timer_fn(struct hrtimer *data) · 700 unmodified lines
22552278 return HRTIMER_NORESTART;
22562279 }
22572280
2258−int kvm_create_lapic(struct kvm_vcpu *vcpu)
2281+int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
22592282 {
22602283 struct kvm_lapic *apic;
22612284
@@ -2279,6 +2302,14 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu) · 17 unmodified lines
22792302 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
22802303 HRTIMER_MODE_ABS_PINNED);
22812304 apic->lapic_timer.timer.function = apic_timer_fn;
2305+ if (timer_advance_ns == -1) {
2306+ apic->lapic_timer.timer_advance_ns = 1000;
2307+ apic->lapic_timer.timer_advance_adjust_done = false;
2308+ } else {
2309+ apic->lapic_timer.timer_advance_ns = timer_advance_ns;
2310+ apic->lapic_timer.timer_advance_adjust_done = true;
2311+ }
2312+
22822313
22832314 /*
22842315 * APIC is created enabled. This will prevent kvm_lapic_set_base from
arch/x86/kvm/lapic.h+3−1
@@ -31,8 +31,10 @@ struct kvm_timer { · 30 unmodified lines
3131 u32 timer_mode_mask;
3232 u64 tscdeadline;
3333 u64 expired_tscdeadline;
34+ u32 timer_advance_ns;
3435 atomic_t pending; /* accumulated triggered timers */
3536 bool hv_timer_in_use;
37+ bool timer_advance_adjust_done;
3638 };
3739
3840 struct kvm_lapic {
@@ -62,7 +64,7 @@ struct kvm_lapic { · 23 unmodified lines
6264
6365 struct dest_map;
6466
65−int kvm_create_lapic(struct kvm_vcpu *vcpu);
67+int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns);
6668 void kvm_free_lapic(struct kvm_vcpu *vcpu);
6769
6870 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu.c+1−0
@@ -4781,6 +4781,7 @@ static union kvm_mmu_extended_role kvm_calc_mmu_role_ext(struct kvm_vcpu *vcpu) · 4780 unmodified lines
47814781 union kvm_mmu_extended_role ext = {0};
47824782
47834783 ext.cr0_pg = !!is_paging(vcpu);
4784+ ext.cr4_pae = !!is_pae(vcpu);
47844785 ext.cr4_smep = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
47854786 ext.cr4_smap = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
47864787 ext.cr4_pse = !!is_pse(vcpu);
arch/x86/kvm/vmx/nested.c+2−2
@@ -5423,7 +5423,7 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu, · 5422 unmodified lines
54235423 return ret;
54245424
54255425 /* Empty 'VMXON' state is permitted */
5426− if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
5426+ if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
54275427 return 0;
54285428
54295429 if (kvm_state->vmx.vmcs_pa != -1ull) {
@@ -5467,7 +5467,7 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu, · 37 unmodified lines
54675467 vmcs12->vmcs_link_pointer != -1ull) {
54685468 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
54695469
5470− if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
5470+ if (kvm_state->size < sizeof(*kvm_state) + 2 * sizeof(*vmcs12))
54715471 return -EINVAL;
54725472
54735473 if (copy_from_user(shadow_vmcs12,
arch/x86/kvm/vmx/vmenter.S+12−0
@@ -3,6 +3,7 @@ · 2 unmodified lines
33 #include <asm/asm.h>
44 #include <asm/bitsperlong.h>
55 #include <asm/kvm_vcpu_regs.h>
6+#include <asm/nospec-branch.h>
67
78 #define WORD_SIZE (BITS_PER_LONG / 8)
89
@@ -77,6 +78,17 @@ ENDPROC(vmx_vmenter) · 68 unmodified lines
7778 * referred to by VMCS.HOST_RIP.
7879 */
7980 ENTRY(vmx_vmexit)
81+#ifdef CONFIG_RETPOLINE
82+ ALTERNATIVE "jmp .Lvmexit_skip_rsb", "", X86_FEATURE_RETPOLINE
83+ /* Preserve guest's RAX, it's used to stuff the RSB. */
84+ push %_ASM_AX
85+
86+ /* IMPORTANT: Stuff the RSB immediately after VM-Exit, before RET! */
87+ FILL_RETURN_BUFFER %_ASM_AX, RSB_CLEAR_LOOPS, X86_FEATURE_RETPOLINE
88+
89+ pop %_ASM_AX
90+.Lvmexit_skip_rsb:
91+#endif
8092 ret
8193 ENDPROC(vmx_vmexit)
8294
arch/x86/kvm/vmx/vmx.c+3−4
@@ -6462,9 +6462,6 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) · 6461 unmodified lines
64626462
64636463 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
64646464
6465− /* Eliminate branch target predictions from guest mode */
6466− vmexit_fill_RSB();
6467−
64686465 /* All fields are clean at this point */
64696466 if (static_branch_unlikely(&enable_evmcs))
64706467 current_evmcs->hv_clean_fields |=
@@ -7032,6 +7029,7 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) · 561 unmodified lines
70327029 {
70337030 struct vcpu_vmx *vmx;
70347031 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
7032+ struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
70357033
70367034 if (kvm_mwait_in_guest(vcpu->kvm))
70377035 return -EOPNOTSUPP;
@@ -7040,7 +7038,8 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) · 2 unmodified lines
70407038 tscl = rdtsc();
70417039 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
70427040 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
7043− lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
7041+ lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7042+ ktimer->timer_advance_ns);
70447043
70457044 if (delta_tsc > lapic_timer_advance_cycles)
70467045 delta_tsc -= lapic_timer_advance_cycles;
arch/x86/kvm/x86.c+29−7
@@ -136,10 +136,14 @@ EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio); · 135 unmodified lines
136136 static u32 __read_mostly tsc_tolerance_ppm = 250;
137137 module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
138138
139−/* lapic timer advance (tscdeadline mode only) in nanoseconds */
140−unsigned int __read_mostly lapic_timer_advance_ns = 1000;
139+/*
140+ * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
141+ * adaptive tuning starting from default advancment of 1000ns. '0' disables
142+ * advancement entirely. Any other value is used as-is and disables adaptive
143+ * tuning, i.e. allows priveleged userspace to set an exact advancement time.
144+ */
145+static int __read_mostly lapic_timer_advance_ns = -1;
141146 module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
142−EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
143147
144148 static bool __read_mostly vector_hashing = true;
145149 module_param(vector_hashing, bool, S_IRUGO);
@@ -6535,6 +6539,12 @@ int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, · 6389 unmodified lines
65356539 }
65366540 EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
65376541
6542+static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6543+{
6544+ vcpu->arch.pio.count = 0;
6545+ return 1;
6546+}
6547+
65386548 static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
65396549 {
65406550 vcpu->arch.pio.count = 0;
@@ -6551,12 +6561,23 @@ static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, · 10 unmodified lines
65516561 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
65526562 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
65536563 size, port, &val, 1);
6564+ if (ret)
6565+ return ret;
65546566
6555− if (!ret) {
6567+ /*
6568+ * Workaround userspace that relies on old KVM behavior of %rip being
6569+ * incremented prior to exiting to userspace to handle "OUT 0x7e".
6570+ */
6571+ if (port == 0x7e &&
6572+ kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6573+ vcpu->arch.complete_userspace_io =
6574+ complete_fast_pio_out_port_0x7e;
6575+ kvm_skip_emulated_instruction(vcpu);
6576+ } else {
65566577 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
65576578 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
65586579 }
6559− return ret;
6580+ return 0;
65606581 }
65616582
65626583 static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
@@ -7873,7 +7894,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · 1310 unmodified lines
78737894 }
78747895
78757896 trace_kvm_entry(vcpu->vcpu_id);
7876− if (lapic_timer_advance_ns)
7897+ if (lapic_in_kernel(vcpu) &&
7898+ vcpu->arch.apic->lapic_timer.timer_advance_ns)
78777899 wait_lapic_expire(vcpu);
78787900 guest_enter_irqoff();
78797901
@@ -9061,7 +9083,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) · 1181 unmodified lines
90619083
90629084 if (irqchip_in_kernel(vcpu->kvm)) {
90639085 vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu);
9064− r = kvm_create_lapic(vcpu);
9086+ r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
90659087 if (r < 0)
90669088 goto fail_mmu_destroy;
90679089 } else
arch/x86/kvm/x86.h+0−2
@@ -294,8 +294,6 @@ extern u64 kvm_supported_xcr0(void); · 293 unmodified lines
294294
295295 extern unsigned int min_timer_period_us;
296296
297−extern unsigned int lapic_timer_advance_ns;
298−
299297 extern bool enable_vmware_backdoor;
300298
301299 extern struct static_key kvm_no_apic_vcpu;
tools/testing/selftests/kvm/dirty_log_test.c+6−3
@@ -288,8 +288,11 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, · 287 unmodified lines
288288 #endif
289289 max_gfn = (1ul << (guest_pa_bits - guest_page_shift)) - 1;
290290 guest_page_size = (1ul << guest_page_shift);
291− /* 1G of guest page sized pages */
292− guest_num_pages = (1ul << (30 - guest_page_shift));
291+ /*
292+ * A little more than 1G of guest page sized pages. Cover the
293+ * case where the size is not aligned to 64 pages.
294+ */
295+ guest_num_pages = (1ul << (30 - guest_page_shift)) + 3;
293296 host_page_size = getpagesize();
294297 host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
295298 !!((guest_num_pages * guest_page_size) % host_page_size);
@@ -359,7 +362,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, · 63 unmodified lines
359362 kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
360363 #ifdef USE_CLEAR_DIRTY_LOG
361364 kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap, 0,
362− DIV_ROUND_UP(host_num_pages, 64) * 64);
365+ host_num_pages);
363366 #endif
364367 vm_dirty_log_verify(bmap);
365368 iteration++;
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c+8−1
@@ -141,7 +141,13 @@ int main(int argc, char *argv[]) · 140 unmodified lines
141141
142142 free(hv_cpuid_entries);
143143
144− vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
144+ rv = _vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
145+
146+ if (rv) {
147+ fprintf(stderr,
148+ "Enlightened VMCS is unsupported, skip related test\n");
149+ goto vm_free;
150+ }
145151
146152 hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
147153 if (!hv_cpuid_entries)
@@ -151,6 +157,7 @@ int main(int argc, char *argv[]) · 3 unmodified lines
151157
152158 free(hv_cpuid_entries);
153159
160+vm_free:
154161 kvm_vm_free(vm);
155162
156163 return 0;
virt/kvm/arm/arch_timer.c+10−7
@@ -507,6 +507,14 @@ static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu) · 506 unmodified lines
507507 {
508508 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
509509
510+ /*
511+ * Update the timer output so that it is likely to match the
512+ * state we're about to restore. If the timer expires between
513+ * this point and the register restoration, we'll take the
514+ * interrupt anyway.
515+ */
516+ kvm_timer_update_irq(vcpu, kvm_timer_should_fire(vtimer), vtimer);
517+
510518 /*
511519 * When using a userspace irqchip with the architected timers and a
512520 * host interrupt controller that doesn't support an active state, we
@@ -730,7 +738,6 @@ static void kvm_timer_init_interrupt(void *info) · 217 unmodified lines
730738 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
731739 {
732740 struct arch_timer_context *timer;
733− bool level;
734741
735742 switch (regid) {
736743 case KVM_REG_ARM_TIMER_CTL:
@@ -758,10 +765,6 @@ int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) · 21 unmodified lines
758765 return -1;
759766 }
760767
761− level = kvm_timer_should_fire(timer);
762− kvm_timer_update_irq(vcpu, level, timer);
763− timer_emulate(timer);
764−
765768 return 0;
766769 }
767770
@@ -812,7 +815,7 @@ static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu, · 44 unmodified lines
812815
813816 switch (treg) {
814817 case TIMER_REG_TVAL:
815− val = kvm_phys_timer_read() - timer->cntvoff - timer->cnt_cval;
818+ val = timer->cnt_cval - kvm_phys_timer_read() + timer->cntvoff;
816819 break;
817820
818821 case TIMER_REG_CTL:
@@ -858,7 +861,7 @@ static void kvm_arm_timer_write(struct kvm_vcpu *vcpu, · 39 unmodified lines
858861 {
859862 switch (treg) {
860863 case TIMER_REG_TVAL:
861− timer->cnt_cval = val - kvm_phys_timer_read() - timer->cntvoff;
864+ timer->cnt_cval = kvm_phys_timer_read() - timer->cntvoff + val;
862865 break;
863866
864867 case TIMER_REG_CTL:
virt/kvm/arm/arm.c+8−3
@@ -934,7 +934,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · 933 unmodified lines
934934 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
935935 const struct kvm_vcpu_init *init)
936936 {
937− unsigned int i;
937+ unsigned int i, ret;
938938 int phys_target = kvm_target_cpu();
939939
940940 if (init->target != phys_target)
@@ -969,9 +969,14 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · 28 unmodified lines
969969 vcpu->arch.target = phys_target;
970970
971971 /* Now we know what it is, we can reset it. */
972− return kvm_reset_vcpu(vcpu);
973−}
972+ ret = kvm_reset_vcpu(vcpu);
973+ if (ret) {
974+ vcpu->arch.target = -1;
975+ bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
976+ }
974977
978+ return ret;
979+}
975980
976981 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
977982 struct kvm_vcpu_init *init)
virt/kvm/arm/mmu.c+5−1
@@ -1781,8 +1781,12 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · 1780 unmodified lines
17811781 * Only PMD_SIZE transparent hugepages(THP) are
17821782 * currently supported. This code will need to be
17831783 * updated to support other THP sizes.
1784+ *
1785+ * Make sure the host VA and the guest IPA are sufficiently
1786+ * aligned and that the block is contained within the memslot.
17841787 */
1785− if (transparent_hugepage_adjust(&pfn, &fault_ipa))
1788+ if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE) &&
1789+ transparent_hugepage_adjust(&pfn, &fault_ipa))
17861790 vma_pagesize = PMD_SIZE;
17871791 }
17881792
virt/kvm/arm/vgic/vgic-mmio-v3.c+3−0
@@ -200,6 +200,9 @@ static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu, · 199 unmodified lines
200200
201201 vgic_cpu->lpis_enabled = val & GICR_CTLR_ENABLE_LPIS;
202202
203+ if (was_enabled && !vgic_cpu->lpis_enabled)
204+ vgic_flush_pending_lpis(vcpu);
205+
203206 if (!was_enabled && vgic_cpu->lpis_enabled)
204207 vgic_enable_lpis(vcpu);
205208 }
virt/kvm/arm/vgic/vgic.c+21−0
@@ -151,6 +151,27 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) · 150 unmodified lines
151151 kfree(irq);
152152 }
153153
154+void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu)
155+{
156+ struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
157+ struct vgic_irq *irq, *tmp;
158+ unsigned long flags;
159+
160+ raw_spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
161+
162+ list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
163+ if (irq->intid >= VGIC_MIN_LPI) {
164+ raw_spin_lock(&irq->irq_lock);
165+ list_del(&irq->ap_list);
166+ irq->vcpu = NULL;
167+ raw_spin_unlock(&irq->irq_lock);
168+ vgic_put_irq(vcpu->kvm, irq);
169+ }
170+ }
171+
172+ raw_spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
173+}
174+
154175 void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
155176 {
156177 WARN_ON(irq_set_irqchip_state(irq->host_irq,
virt/kvm/arm/vgic/vgic.h+1−0
@@ -238,6 +238,7 @@ void vgic_v3_put(struct kvm_vcpu *vcpu); · 237 unmodified lines
238238 bool vgic_has_its(struct kvm *kvm);
239239 int kvm_vgic_register_its_device(void);
240240 void vgic_enable_lpis(struct kvm_vcpu *vcpu);
241+void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu);
241242 int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi);
242243 int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr);
243244 int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
virt/kvm/kvm_main.c+4−3
@@ -1240,7 +1240,7 @@ int kvm_clear_dirty_log_protect(struct kvm *kvm, · 1239 unmodified lines
12401240 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
12411241 return -EINVAL;
12421242
1243− if ((log->first_page & 63) || (log->num_pages & 63))
1243+ if (log->first_page & 63)
12441244 return -EINVAL;
12451245
12461246 slots = __kvm_memslots(kvm, as_id);
@@ -1253,8 +1253,9 @@ int kvm_clear_dirty_log_protect(struct kvm *kvm, · 6 unmodified lines
12531253 n = kvm_dirty_bitmap_bytes(memslot);
12541254
12551255 if (log->first_page > memslot->npages ||
1256− log->num_pages > memslot->npages - log->first_page)
1257− return -EINVAL;
1256+ log->num_pages > memslot->npages - log->first_page ||
1257+ (log->num_pages < memslot->npages - log->first_page && (log->num_pages & 63)))
1258+ return -EINVAL;
12581259
12591260 *flush = false;
12601261 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t