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

Pull kvm fixes from Paolo Bonzini:

- ARM/ARM64 locking fixes

- x86 fixes: PCID, UMIP, locking

- improved support for recent Windows version that have a 2048 Hz APIC
timer

- rename KVM_HINTS_DEDICATED CPUID bit to KVM_HINTS_REALTIME

- better behaved selftests

* tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm:
kvm: rename KVM_HINTS_DEDICATED to KVM_HINTS_REALTIME
KVM: arm/arm64: VGIC/ITS save/restore: protect kvm_read_guest() calls
KVM: arm/arm64: VGIC/ITS: protect kvm_read_guest() calls with SRCU lock
KVM: arm/arm64: VGIC/ITS: Promote irq_lock() in update_affinity
KVM: arm/arm64: Properly protect VGIC locks from IRQs
KVM: X86: Lower the default timer frequency limit to 200us
KVM: vmx: update sec exec controls for UMIP iff emulating UMIP
kvm: x86: Suppress CR3_PCID_INVD bit only when PCIDs are enabled
KVM: selftests: exit with 0 status code when tests cannot be run
KVM: hyperv: idr_find needs RCU protection
x86: Delay skip of emulated hypercall instruction
KVM: Extend MAX_IRQ_ROUTES to 4096 for all archs

+157 -87
+3 -3
Documentation/virtual/kvm/cpuid.txt
··· 72 73 flag || value || meaning 74 ================================================================================== 75 - KVM_HINTS_DEDICATED || 0 || guest checks this feature bit to 76 - || || determine if there is vCPU pinning 77 - || || and there is no vCPU over-commitment, 78 || || allowing optimizations 79 ----------------------------------------------------------------------------------
··· 72 73 flag || value || meaning 74 ================================================================================== 75 + KVM_HINTS_REALTIME || 0 || guest checks this feature bit to 76 + || || determine that vCPUs are never 77 + || || preempted for an unlimited time, 78 || || allowing optimizations 79 ----------------------------------------------------------------------------------
+16
arch/arm/include/asm/kvm_mmu.h
··· 309 return 8; 310 } 311 312 static inline void *kvm_get_hyp_vector(void) 313 { 314 return kvm_ksym_ref(__kvm_hyp_vector);
··· 309 return 8; 310 } 311 312 + /* 313 + * We are not in the kvm->srcu critical section most of the time, so we take 314 + * the SRCU read lock here. Since we copy the data from the user page, we 315 + * can immediately drop the lock again. 316 + */ 317 + static inline int kvm_read_guest_lock(struct kvm *kvm, 318 + gpa_t gpa, void *data, unsigned long len) 319 + { 320 + int srcu_idx = srcu_read_lock(&kvm->srcu); 321 + int ret = kvm_read_guest(kvm, gpa, data, len); 322 + 323 + srcu_read_unlock(&kvm->srcu, srcu_idx); 324 + 325 + return ret; 326 + } 327 + 328 static inline void *kvm_get_hyp_vector(void) 329 { 330 return kvm_ksym_ref(__kvm_hyp_vector);
+16
arch/arm64/include/asm/kvm_mmu.h
··· 360 return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; 361 } 362 363 #ifdef CONFIG_KVM_INDIRECT_VECTORS 364 /* 365 * EL2 vectors can be mapped and rerouted in a number of ways,
··· 360 return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; 361 } 362 363 + /* 364 + * We are not in the kvm->srcu critical section most of the time, so we take 365 + * the SRCU read lock here. Since we copy the data from the user page, we 366 + * can immediately drop the lock again. 367 + */ 368 + static inline int kvm_read_guest_lock(struct kvm *kvm, 369 + gpa_t gpa, void *data, unsigned long len) 370 + { 371 + int srcu_idx = srcu_read_lock(&kvm->srcu); 372 + int ret = kvm_read_guest(kvm, gpa, data, len); 373 + 374 + srcu_read_unlock(&kvm->srcu, srcu_idx); 375 + 376 + return ret; 377 + } 378 + 379 #ifdef CONFIG_KVM_INDIRECT_VECTORS 380 /* 381 * EL2 vectors can be mapped and rerouted in a number of ways,
+1 -1
arch/x86/include/uapi/asm/kvm_para.h
··· 29 #define KVM_FEATURE_PV_TLB_FLUSH 9 30 #define KVM_FEATURE_ASYNC_PF_VMEXIT 10 31 32 - #define KVM_HINTS_DEDICATED 0 33 34 /* The last 8 bits are used to indicate how to interpret the flags field 35 * in pvclock structure. If no bits are set, all flags are ignored.
··· 29 #define KVM_FEATURE_PV_TLB_FLUSH 9 30 #define KVM_FEATURE_ASYNC_PF_VMEXIT 10 31 32 + #define KVM_HINTS_REALTIME 0 33 34 /* The last 8 bits are used to indicate how to interpret the flags field 35 * in pvclock structure. If no bits are set, all flags are ignored.
+4 -4
arch/x86/kernel/kvm.c
··· 457 static void __init kvm_smp_prepare_cpus(unsigned int max_cpus) 458 { 459 native_smp_prepare_cpus(max_cpus); 460 - if (kvm_para_has_hint(KVM_HINTS_DEDICATED)) 461 static_branch_disable(&virt_spin_lock_key); 462 } 463 ··· 553 } 554 555 if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) && 556 - !kvm_para_has_hint(KVM_HINTS_DEDICATED) && 557 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) 558 pv_mmu_ops.flush_tlb_others = kvm_flush_tlb_others; 559 ··· 649 int cpu; 650 651 if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) && 652 - !kvm_para_has_hint(KVM_HINTS_DEDICATED) && 653 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { 654 for_each_possible_cpu(cpu) { 655 zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu), ··· 745 if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) 746 return; 747 748 - if (kvm_para_has_hint(KVM_HINTS_DEDICATED)) 749 return; 750 751 __pv_init_lock_hash();
··· 457 static void __init kvm_smp_prepare_cpus(unsigned int max_cpus) 458 { 459 native_smp_prepare_cpus(max_cpus); 460 + if (kvm_para_has_hint(KVM_HINTS_REALTIME)) 461 static_branch_disable(&virt_spin_lock_key); 462 } 463 ··· 553 } 554 555 if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) && 556 + !kvm_para_has_hint(KVM_HINTS_REALTIME) && 557 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) 558 pv_mmu_ops.flush_tlb_others = kvm_flush_tlb_others; 559 ··· 649 int cpu; 650 651 if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) && 652 + !kvm_para_has_hint(KVM_HINTS_REALTIME) && 653 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { 654 for_each_possible_cpu(cpu) { 655 zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu), ··· 745 if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) 746 return; 747 748 + if (kvm_para_has_hint(KVM_HINTS_REALTIME)) 749 return; 750 751 __pv_init_lock_hash();
+4 -2
arch/x86/kvm/hyperv.c
··· 1265 struct kvm_run *run = vcpu->run; 1266 1267 kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result); 1268 - return 1; 1269 } 1270 1271 static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param) ··· 1296 if (param & ~KVM_HYPERV_CONN_ID_MASK) 1297 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1298 1299 - /* conn_to_evt is protected by vcpu->kvm->srcu */ 1300 eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param); 1301 if (!eventfd) 1302 return HV_STATUS_INVALID_PORT_ID; 1303
··· 1265 struct kvm_run *run = vcpu->run; 1266 1267 kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result); 1268 + return kvm_skip_emulated_instruction(vcpu); 1269 } 1270 1271 static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param) ··· 1296 if (param & ~KVM_HYPERV_CONN_ID_MASK) 1297 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1298 1299 + /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */ 1300 + rcu_read_lock(); 1301 eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param); 1302 + rcu_read_unlock(); 1303 if (!eventfd) 1304 return HV_STATUS_INVALID_PORT_ID; 1305
+15 -13
arch/x86/kvm/vmx.c
··· 1494 SECONDARY_EXEC_ENABLE_VMFUNC; 1495 } 1496 1497 static inline bool report_flexpriority(void) 1498 { 1499 return flexpriority_enabled; ··· 4767 else 4768 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; 4769 4770 - if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) { 4771 - vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, 4772 - SECONDARY_EXEC_DESC); 4773 - hw_cr4 &= ~X86_CR4_UMIP; 4774 - } else if (!is_guest_mode(vcpu) || 4775 - !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) 4776 - vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, 4777 SECONDARY_EXEC_DESC); 4778 4779 if (cr4 & X86_CR4_VMXE) { 4780 /* ··· 9503 { 9504 return vmcs_config.cpu_based_2nd_exec_ctrl & 9505 SECONDARY_EXEC_XSAVES; 9506 - } 9507 - 9508 - static bool vmx_umip_emulated(void) 9509 - { 9510 - return vmcs_config.cpu_based_2nd_exec_ctrl & 9511 - SECONDARY_EXEC_DESC; 9512 } 9513 9514 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
··· 1494 SECONDARY_EXEC_ENABLE_VMFUNC; 1495 } 1496 1497 + static bool vmx_umip_emulated(void) 1498 + { 1499 + return vmcs_config.cpu_based_2nd_exec_ctrl & 1500 + SECONDARY_EXEC_DESC; 1501 + } 1502 + 1503 static inline bool report_flexpriority(void) 1504 { 1505 return flexpriority_enabled; ··· 4761 else 4762 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; 4763 4764 + if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { 4765 + if (cr4 & X86_CR4_UMIP) { 4766 + vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, 4767 SECONDARY_EXEC_DESC); 4768 + hw_cr4 &= ~X86_CR4_UMIP; 4769 + } else if (!is_guest_mode(vcpu) || 4770 + !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) 4771 + vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, 4772 + SECONDARY_EXEC_DESC); 4773 + } 4774 4775 if (cr4 & X86_CR4_VMXE) { 4776 /* ··· 9495 { 9496 return vmcs_config.cpu_based_2nd_exec_ctrl & 9497 SECONDARY_EXEC_XSAVES; 9498 } 9499 9500 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
+16 -10
arch/x86/kvm/x86.c
··· 114 static bool __read_mostly report_ignored_msrs = true; 115 module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR); 116 117 - unsigned int min_timer_period_us = 500; 118 module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); 119 120 static bool __read_mostly kvmclock_periodic_sync = true; ··· 843 int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) 844 { 845 #ifdef CONFIG_X86_64 846 - cr3 &= ~CR3_PCID_INVD; 847 #endif 848 849 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) { ··· 6674 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) 6675 { 6676 unsigned long nr, a0, a1, a2, a3, ret; 6677 - int op_64_bit, r; 6678 6679 - r = kvm_skip_emulated_instruction(vcpu); 6680 - 6681 - if (kvm_hv_hypercall_enabled(vcpu->kvm)) 6682 - return kvm_hv_hypercall(vcpu); 6683 6684 nr = kvm_register_read(vcpu, VCPU_REGS_RAX); 6685 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); ··· 6701 6702 if (kvm_x86_ops->get_cpl(vcpu) != 0) { 6703 ret = -KVM_EPERM; 6704 - goto out; 6705 } 6706 6707 switch (nr) { ··· 6721 ret = -KVM_ENOSYS; 6722 break; 6723 } 6724 - out: 6725 if (!op_64_bit) 6726 ret = (u32)ret; 6727 kvm_register_write(vcpu, VCPU_REGS_RAX, ret); 6728 ++vcpu->stat.hypercalls; 6729 - return r; 6730 } 6731 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); 6732
··· 114 static bool __read_mostly report_ignored_msrs = true; 115 module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR); 116 117 + unsigned int min_timer_period_us = 200; 118 module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); 119 120 static bool __read_mostly kvmclock_periodic_sync = true; ··· 843 int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) 844 { 845 #ifdef CONFIG_X86_64 846 + bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); 847 + 848 + if (pcid_enabled) 849 + cr3 &= ~CR3_PCID_INVD; 850 #endif 851 852 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) { ··· 6671 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) 6672 { 6673 unsigned long nr, a0, a1, a2, a3, ret; 6674 + int op_64_bit; 6675 6676 + if (kvm_hv_hypercall_enabled(vcpu->kvm)) { 6677 + if (!kvm_hv_hypercall(vcpu)) 6678 + return 0; 6679 + goto out; 6680 + } 6681 6682 nr = kvm_register_read(vcpu, VCPU_REGS_RAX); 6683 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); ··· 6697 6698 if (kvm_x86_ops->get_cpl(vcpu) != 0) { 6699 ret = -KVM_EPERM; 6700 + goto out_error; 6701 } 6702 6703 switch (nr) { ··· 6717 ret = -KVM_ENOSYS; 6718 break; 6719 } 6720 + out_error: 6721 if (!op_64_bit) 6722 ret = (u32)ret; 6723 kvm_register_write(vcpu, VCPU_REGS_RAX, ret); 6724 + 6725 + out: 6726 ++vcpu->stat.hypercalls; 6727 + return kvm_skip_emulated_instruction(vcpu); 6728 } 6729 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); 6730
+1 -7
include/linux/kvm_host.h
··· 1045 1046 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING 1047 1048 - #ifdef CONFIG_S390 1049 - #define KVM_MAX_IRQ_ROUTES 4096 //FIXME: we can have more than that... 1050 - #elif defined(CONFIG_ARM64) 1051 - #define KVM_MAX_IRQ_ROUTES 4096 1052 - #else 1053 - #define KVM_MAX_IRQ_ROUTES 1024 1054 - #endif 1055 1056 bool kvm_arch_can_set_irq_routing(struct kvm *kvm); 1057 int kvm_set_irq_routing(struct kvm *kvm,
··· 1045 1046 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING 1047 1048 + #define KVM_MAX_IRQ_ROUTES 4096 /* might need extension/rework in the future */ 1049 1050 bool kvm_arch_can_set_irq_routing(struct kvm *kvm); 1051 int kvm_set_irq_routing(struct kvm *kvm,
+1 -1
tools/testing/selftests/kvm/Makefile
··· 15 16 INSTALL_HDR_PATH = $(top_srcdir)/usr 17 LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ 18 - CFLAGS += -O2 -g -std=gnu99 -I$(LINUX_HDR_PATH) -Iinclude -I$(<D) 19 20 # After inclusion, $(OUTPUT) is defined and 21 # $(TEST_GEN_PROGS) starts with $(OUTPUT)/
··· 15 16 INSTALL_HDR_PATH = $(top_srcdir)/usr 17 LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ 18 + CFLAGS += -O2 -g -std=gnu99 -I$(LINUX_HDR_PATH) -Iinclude -I$(<D) -I.. 19 20 # After inclusion, $(OUTPUT) is defined and 21 # $(TEST_GEN_PROGS) starts with $(OUTPUT)/
+1
tools/testing/selftests/kvm/include/test_util.h
··· 19 #include <errno.h> 20 #include <unistd.h> 21 #include <fcntl.h> 22 23 ssize_t test_write(int fd, const void *buf, size_t count); 24 ssize_t test_read(int fd, void *buf, size_t count);
··· 19 #include <errno.h> 20 #include <unistd.h> 21 #include <fcntl.h> 22 + #include "kselftest.h" 23 24 ssize_t test_write(int fd, const void *buf, size_t count); 25 ssize_t test_read(int fd, void *buf, size_t count);
+8 -8
tools/testing/selftests/kvm/lib/kvm_util.c
··· 50 int kvm_fd; 51 52 kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 53 - TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i", 54 - KVM_DEV_PATH, kvm_fd, errno); 55 56 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap); 57 TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n" ··· 91 92 vm->mode = mode; 93 kvm_fd = open(KVM_DEV_PATH, perm); 94 - TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i", 95 - KVM_DEV_PATH, kvm_fd, errno); 96 97 /* Create VM. */ 98 vm->fd = ioctl(kvm_fd, KVM_CREATE_VM, NULL); ··· 418 419 cpuid = allocate_kvm_cpuid2(); 420 kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 421 - TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i", 422 - KVM_DEV_PATH, kvm_fd, errno); 423 424 ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid); 425 TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n", ··· 675 int dev_fd, ret; 676 677 dev_fd = open(KVM_DEV_PATH, O_RDONLY); 678 - TEST_ASSERT(dev_fd >= 0, "%s open %s failed, rc: %i errno: %i", 679 - __func__, KVM_DEV_PATH, dev_fd, errno); 680 681 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL); 682 TEST_ASSERT(ret >= sizeof(struct kvm_run),
··· 50 int kvm_fd; 51 52 kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 53 + if (kvm_fd < 0) 54 + exit(KSFT_SKIP); 55 56 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap); 57 TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n" ··· 91 92 vm->mode = mode; 93 kvm_fd = open(KVM_DEV_PATH, perm); 94 + if (kvm_fd < 0) 95 + exit(KSFT_SKIP); 96 97 /* Create VM. */ 98 vm->fd = ioctl(kvm_fd, KVM_CREATE_VM, NULL); ··· 418 419 cpuid = allocate_kvm_cpuid2(); 420 kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 421 + if (kvm_fd < 0) 422 + exit(KSFT_SKIP); 423 424 ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid); 425 TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n", ··· 675 int dev_fd, ret; 676 677 dev_fd = open(KVM_DEV_PATH, O_RDONLY); 678 + if (dev_fd < 0) 679 + exit(KSFT_SKIP); 680 681 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL); 682 TEST_ASSERT(ret >= sizeof(struct kvm_run),
+31 -9
tools/testing/selftests/kvm/sync_regs_test.c
··· 85 { 86 } 87 88 int main(int argc, char *argv[]) 89 { 90 struct kvm_vm *vm; ··· 101 setbuf(stdout, NULL); 102 103 cap = kvm_check_cap(KVM_CAP_SYNC_REGS); 104 - TEST_ASSERT((unsigned long)cap == KVM_SYNC_X86_VALID_FIELDS, 105 - "KVM_CAP_SYNC_REGS (0x%x) != KVM_SYNC_X86_VALID_FIELDS (0x%lx)\n", 106 - cap, KVM_SYNC_X86_VALID_FIELDS); 107 108 /* Create VM */ 109 vm = vm_create_default(VCPU_ID, guest_code); ··· 116 run = vcpu_state(vm, VCPU_ID); 117 118 /* Request reading invalid register set from VCPU. */ 119 - run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS << 1; 120 rv = _vcpu_run(vm, VCPU_ID); 121 TEST_ASSERT(rv < 0 && errno == EINVAL, 122 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n", ··· 131 vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0; 132 133 /* Request setting invalid register set into VCPU. */ 134 - run->kvm_dirty_regs = KVM_SYNC_X86_VALID_FIELDS << 1; 135 rv = _vcpu_run(vm, VCPU_ID); 136 TEST_ASSERT(rv < 0 && errno == EINVAL, 137 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n", ··· 147 148 /* Request and verify all valid register sets. */ 149 /* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */ 150 - run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS; 151 rv = _vcpu_run(vm, VCPU_ID); 152 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, 153 "Unexpected exit reason: %u (%s),\n", ··· 168 run->s.regs.sregs.apic_base = 1 << 11; 169 /* TODO run->s.regs.events.XYZ = ABC; */ 170 171 - run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS; 172 run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS; 173 rv = _vcpu_run(vm, VCPU_ID); 174 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, ··· 194 /* Clear kvm_dirty_regs bits, verify new s.regs values are 195 * overwritten with existing guest values. 196 */ 197 - run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS; 198 run->kvm_dirty_regs = 0; 199 run->s.regs.regs.r11 = 0xDEADBEEF; 200 rv = _vcpu_run(vm, VCPU_ID); ··· 233 * with kvm_sync_regs values. 234 */ 235 run->kvm_valid_regs = 0; 236 - run->kvm_dirty_regs = KVM_SYNC_X86_VALID_FIELDS; 237 run->s.regs.regs.r11 = 0xBBBB; 238 rv = _vcpu_run(vm, VCPU_ID); 239 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
··· 85 { 86 } 87 88 + #define TEST_SYNC_FIELDS (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS) 89 + #define INVALID_SYNC_FIELD 0x80000000 90 + 91 int main(int argc, char *argv[]) 92 { 93 struct kvm_vm *vm; ··· 98 setbuf(stdout, NULL); 99 100 cap = kvm_check_cap(KVM_CAP_SYNC_REGS); 101 + if ((cap & TEST_SYNC_FIELDS) != TEST_SYNC_FIELDS) { 102 + fprintf(stderr, "KVM_CAP_SYNC_REGS not supported, skipping test\n"); 103 + exit(KSFT_SKIP); 104 + } 105 + if ((cap & INVALID_SYNC_FIELD) != 0) { 106 + fprintf(stderr, "The \"invalid\" field is not invalid, skipping test\n"); 107 + exit(KSFT_SKIP); 108 + } 109 110 /* Create VM */ 111 vm = vm_create_default(VCPU_ID, guest_code); ··· 108 run = vcpu_state(vm, VCPU_ID); 109 110 /* Request reading invalid register set from VCPU. */ 111 + run->kvm_valid_regs = INVALID_SYNC_FIELD; 112 + rv = _vcpu_run(vm, VCPU_ID); 113 + TEST_ASSERT(rv < 0 && errno == EINVAL, 114 + "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n", 115 + rv); 116 + vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0; 117 + 118 + run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS; 119 rv = _vcpu_run(vm, VCPU_ID); 120 TEST_ASSERT(rv < 0 && errno == EINVAL, 121 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n", ··· 116 vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0; 117 118 /* Request setting invalid register set into VCPU. */ 119 + run->kvm_dirty_regs = INVALID_SYNC_FIELD; 120 + rv = _vcpu_run(vm, VCPU_ID); 121 + TEST_ASSERT(rv < 0 && errno == EINVAL, 122 + "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n", 123 + rv); 124 + vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0; 125 + 126 + run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS; 127 rv = _vcpu_run(vm, VCPU_ID); 128 TEST_ASSERT(rv < 0 && errno == EINVAL, 129 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n", ··· 125 126 /* Request and verify all valid register sets. */ 127 /* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */ 128 + run->kvm_valid_regs = TEST_SYNC_FIELDS; 129 rv = _vcpu_run(vm, VCPU_ID); 130 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, 131 "Unexpected exit reason: %u (%s),\n", ··· 146 run->s.regs.sregs.apic_base = 1 << 11; 147 /* TODO run->s.regs.events.XYZ = ABC; */ 148 149 + run->kvm_valid_regs = TEST_SYNC_FIELDS; 150 run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS; 151 rv = _vcpu_run(vm, VCPU_ID); 152 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, ··· 172 /* Clear kvm_dirty_regs bits, verify new s.regs values are 173 * overwritten with existing guest values. 174 */ 175 + run->kvm_valid_regs = TEST_SYNC_FIELDS; 176 run->kvm_dirty_regs = 0; 177 run->s.regs.regs.r11 = 0xDEADBEEF; 178 rv = _vcpu_run(vm, VCPU_ID); ··· 211 * with kvm_sync_regs values. 212 */ 213 run->kvm_valid_regs = 0; 214 + run->kvm_dirty_regs = TEST_SYNC_FIELDS; 215 run->s.regs.regs.r11 = 0xBBBB; 216 rv = _vcpu_run(vm, VCPU_ID); 217 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+2 -2
tools/testing/selftests/kvm/vmx_tsc_adjust_test.c
··· 189 struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1); 190 191 if (!(entry->ecx & CPUID_VMX)) { 192 - printf("nested VMX not enabled, skipping test"); 193 - return 0; 194 } 195 196 vm = vm_create_default_vmx(VCPU_ID, (void *) l1_guest_code);
··· 189 struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1); 190 191 if (!(entry->ecx & CPUID_VMX)) { 192 + fprintf(stderr, "nested VMX not enabled, skipping test\n"); 193 + exit(KSFT_SKIP); 194 } 195 196 vm = vm_create_default_vmx(VCPU_ID, (void *) l1_guest_code);
+3 -2
virt/kvm/arm/vgic/vgic-debug.c
··· 211 struct vgic_state_iter *iter = (struct vgic_state_iter *)v; 212 struct vgic_irq *irq; 213 struct kvm_vcpu *vcpu = NULL; 214 215 if (iter->dist_id == 0) { 216 print_dist_state(s, &kvm->arch.vgic); ··· 228 irq = &kvm->arch.vgic.spis[iter->intid - VGIC_NR_PRIVATE_IRQS]; 229 } 230 231 - spin_lock(&irq->irq_lock); 232 print_irq_state(s, irq, vcpu); 233 - spin_unlock(&irq->irq_lock); 234 235 return 0; 236 }
··· 211 struct vgic_state_iter *iter = (struct vgic_state_iter *)v; 212 struct vgic_irq *irq; 213 struct kvm_vcpu *vcpu = NULL; 214 + unsigned long flags; 215 216 if (iter->dist_id == 0) { 217 print_dist_state(s, &kvm->arch.vgic); ··· 227 irq = &kvm->arch.vgic.spis[iter->intid - VGIC_NR_PRIVATE_IRQS]; 228 } 229 230 + spin_lock_irqsave(&irq->irq_lock, flags); 231 print_irq_state(s, irq, vcpu); 232 + spin_unlock_irqrestore(&irq->irq_lock, flags); 233 234 return 0; 235 }
+19 -15
virt/kvm/arm/vgic/vgic-its.c
··· 52 { 53 struct vgic_dist *dist = &kvm->arch.vgic; 54 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq; 55 int ret; 56 57 /* In this case there is no put, since we keep the reference. */ ··· 72 irq->intid = intid; 73 irq->target_vcpu = vcpu; 74 75 - spin_lock(&dist->lpi_list_lock); 76 77 /* 78 * There could be a race with another vgic_add_lpi(), so we need to ··· 100 dist->lpi_list_count++; 101 102 out_unlock: 103 - spin_unlock(&dist->lpi_list_lock); 104 105 /* 106 * We "cache" the configuration table entries in our struct vgic_irq's. ··· 281 int ret; 282 unsigned long flags; 283 284 - ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET, 285 - &prop, 1); 286 287 if (ret) 288 return ret; ··· 316 { 317 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 318 struct vgic_irq *irq; 319 u32 *intids; 320 int irq_count, i = 0; 321 ··· 332 if (!intids) 333 return -ENOMEM; 334 335 - spin_lock(&dist->lpi_list_lock); 336 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { 337 if (i == irq_count) 338 break; ··· 341 continue; 342 intids[i++] = irq->intid; 343 } 344 - spin_unlock(&dist->lpi_list_lock); 345 346 *intid_ptr = intids; 347 return i; ··· 350 static int update_affinity(struct vgic_irq *irq, struct kvm_vcpu *vcpu) 351 { 352 int ret = 0; 353 354 - spin_lock(&irq->irq_lock); 355 irq->target_vcpu = vcpu; 356 - spin_unlock(&irq->irq_lock); 357 358 if (irq->hw) { 359 struct its_vlpi_map map; ··· 444 * this very same byte in the last iteration. Reuse that. 445 */ 446 if (byte_offset != last_byte_offset) { 447 - ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset, 448 - &pendmask, 1); 449 if (ret) { 450 kfree(intids); 451 return ret; ··· 790 return false; 791 792 /* Each 1st level entry is represented by a 64-bit value. */ 793 - if (kvm_read_guest(its->dev->kvm, 794 BASER_ADDRESS(baser) + index * sizeof(indirect_ptr), 795 &indirect_ptr, sizeof(indirect_ptr))) 796 return false; ··· 1371 cbaser = CBASER_ADDRESS(its->cbaser); 1372 1373 while (its->cwriter != its->creadr) { 1374 - int ret = kvm_read_guest(kvm, cbaser + its->creadr, 1375 - cmd_buf, ITS_CMD_SIZE); 1376 /* 1377 * If kvm_read_guest() fails, this could be due to the guest 1378 * programming a bogus value in CBASER or something else going ··· 1897 int next_offset; 1898 size_t byte_offset; 1899 1900 - ret = kvm_read_guest(kvm, gpa, entry, esz); 1901 if (ret) 1902 return ret; 1903 ··· 2267 int ret; 2268 2269 BUG_ON(esz > sizeof(val)); 2270 - ret = kvm_read_guest(kvm, gpa, &val, esz); 2271 if (ret) 2272 return ret; 2273 val = le64_to_cpu(val);
··· 52 { 53 struct vgic_dist *dist = &kvm->arch.vgic; 54 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq; 55 + unsigned long flags; 56 int ret; 57 58 /* In this case there is no put, since we keep the reference. */ ··· 71 irq->intid = intid; 72 irq->target_vcpu = vcpu; 73 74 + spin_lock_irqsave(&dist->lpi_list_lock, flags); 75 76 /* 77 * There could be a race with another vgic_add_lpi(), so we need to ··· 99 dist->lpi_list_count++; 100 101 out_unlock: 102 + spin_unlock_irqrestore(&dist->lpi_list_lock, flags); 103 104 /* 105 * We "cache" the configuration table entries in our struct vgic_irq's. ··· 280 int ret; 281 unsigned long flags; 282 283 + ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET, 284 + &prop, 1); 285 286 if (ret) 287 return ret; ··· 315 { 316 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 317 struct vgic_irq *irq; 318 + unsigned long flags; 319 u32 *intids; 320 int irq_count, i = 0; 321 ··· 330 if (!intids) 331 return -ENOMEM; 332 333 + spin_lock_irqsave(&dist->lpi_list_lock, flags); 334 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { 335 if (i == irq_count) 336 break; ··· 339 continue; 340 intids[i++] = irq->intid; 341 } 342 + spin_unlock_irqrestore(&dist->lpi_list_lock, flags); 343 344 *intid_ptr = intids; 345 return i; ··· 348 static int update_affinity(struct vgic_irq *irq, struct kvm_vcpu *vcpu) 349 { 350 int ret = 0; 351 + unsigned long flags; 352 353 + spin_lock_irqsave(&irq->irq_lock, flags); 354 irq->target_vcpu = vcpu; 355 + spin_unlock_irqrestore(&irq->irq_lock, flags); 356 357 if (irq->hw) { 358 struct its_vlpi_map map; ··· 441 * this very same byte in the last iteration. Reuse that. 442 */ 443 if (byte_offset != last_byte_offset) { 444 + ret = kvm_read_guest_lock(vcpu->kvm, 445 + pendbase + byte_offset, 446 + &pendmask, 1); 447 if (ret) { 448 kfree(intids); 449 return ret; ··· 786 return false; 787 788 /* Each 1st level entry is represented by a 64-bit value. */ 789 + if (kvm_read_guest_lock(its->dev->kvm, 790 BASER_ADDRESS(baser) + index * sizeof(indirect_ptr), 791 &indirect_ptr, sizeof(indirect_ptr))) 792 return false; ··· 1367 cbaser = CBASER_ADDRESS(its->cbaser); 1368 1369 while (its->cwriter != its->creadr) { 1370 + int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr, 1371 + cmd_buf, ITS_CMD_SIZE); 1372 /* 1373 * If kvm_read_guest() fails, this could be due to the guest 1374 * programming a bogus value in CBASER or something else going ··· 1893 int next_offset; 1894 size_t byte_offset; 1895 1896 + ret = kvm_read_guest_lock(kvm, gpa, entry, esz); 1897 if (ret) 1898 return ret; 1899 ··· 2263 int ret; 2264 2265 BUG_ON(esz > sizeof(val)); 2266 + ret = kvm_read_guest_lock(kvm, gpa, &val, esz); 2267 if (ret) 2268 return ret; 2269 val = le64_to_cpu(val);
+2 -2
virt/kvm/arm/vgic/vgic-v3.c
··· 344 bit_nr = irq->intid % BITS_PER_BYTE; 345 ptr = pendbase + byte_offset; 346 347 - ret = kvm_read_guest(kvm, ptr, &val, 1); 348 if (ret) 349 return ret; 350 ··· 397 ptr = pendbase + byte_offset; 398 399 if (byte_offset != last_byte_offset) { 400 - ret = kvm_read_guest(kvm, ptr, &val, 1); 401 if (ret) 402 return ret; 403 last_byte_offset = byte_offset;
··· 344 bit_nr = irq->intid % BITS_PER_BYTE; 345 ptr = pendbase + byte_offset; 346 347 + ret = kvm_read_guest_lock(kvm, ptr, &val, 1); 348 if (ret) 349 return ret; 350 ··· 397 ptr = pendbase + byte_offset; 398 399 if (byte_offset != last_byte_offset) { 400 + ret = kvm_read_guest_lock(kvm, ptr, &val, 1); 401 if (ret) 402 return ret; 403 last_byte_offset = byte_offset;
+14 -8
virt/kvm/arm/vgic/vgic.c
··· 43 * kvm->lock (mutex) 44 * its->cmd_lock (mutex) 45 * its->its_lock (mutex) 46 - * vgic_cpu->ap_list_lock 47 - * kvm->lpi_list_lock 48 - * vgic_irq->irq_lock 49 * 50 * If you need to take multiple locks, always take the upper lock first, 51 * then the lower ones, e.g. first take the its_lock, then the irq_lock. ··· 76 { 77 struct vgic_dist *dist = &kvm->arch.vgic; 78 struct vgic_irq *irq = NULL; 79 80 - spin_lock(&dist->lpi_list_lock); 81 82 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { 83 if (irq->intid != intid) ··· 94 irq = NULL; 95 96 out_unlock: 97 - spin_unlock(&dist->lpi_list_lock); 98 99 return irq; 100 } ··· 139 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) 140 { 141 struct vgic_dist *dist = &kvm->arch.vgic; 142 143 if (irq->intid < VGIC_MIN_LPI) 144 return; 145 146 - spin_lock(&dist->lpi_list_lock); 147 if (!kref_put(&irq->refcount, vgic_irq_release)) { 148 - spin_unlock(&dist->lpi_list_lock); 149 return; 150 }; 151 152 list_del(&irq->lpi_list); 153 dist->lpi_list_count--; 154 - spin_unlock(&dist->lpi_list_lock); 155 156 kfree(irq); 157 }
··· 43 * kvm->lock (mutex) 44 * its->cmd_lock (mutex) 45 * its->its_lock (mutex) 46 + * vgic_cpu->ap_list_lock must be taken with IRQs disabled 47 + * kvm->lpi_list_lock must be taken with IRQs disabled 48 + * vgic_irq->irq_lock must be taken with IRQs disabled 49 + * 50 + * As the ap_list_lock might be taken from the timer interrupt handler, 51 + * we have to disable IRQs before taking this lock and everything lower 52 + * than it. 53 * 54 * If you need to take multiple locks, always take the upper lock first, 55 * then the lower ones, e.g. first take the its_lock, then the irq_lock. ··· 72 { 73 struct vgic_dist *dist = &kvm->arch.vgic; 74 struct vgic_irq *irq = NULL; 75 + unsigned long flags; 76 77 + spin_lock_irqsave(&dist->lpi_list_lock, flags); 78 79 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { 80 if (irq->intid != intid) ··· 89 irq = NULL; 90 91 out_unlock: 92 + spin_unlock_irqrestore(&dist->lpi_list_lock, flags); 93 94 return irq; 95 } ··· 134 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) 135 { 136 struct vgic_dist *dist = &kvm->arch.vgic; 137 + unsigned long flags; 138 139 if (irq->intid < VGIC_MIN_LPI) 140 return; 141 142 + spin_lock_irqsave(&dist->lpi_list_lock, flags); 143 if (!kref_put(&irq->refcount, vgic_irq_release)) { 144 + spin_unlock_irqrestore(&dist->lpi_list_lock, flags); 145 return; 146 }; 147 148 list_del(&irq->lpi_list); 149 dist->lpi_list_count--; 150 + spin_unlock_irqrestore(&dist->lpi_list_lock, flags); 151 152 kfree(irq); 153 }