Skip to contentSkip to conversation

kvm: nVMX: Don't set vmcs12 to "launched" when VMLAUNCH fails

PATCH Superseded Jim Mattson 3 participants 10 messages Jul 6, 2017 – Jul 10, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 kvm: nVMX: Don't set vmcs12 to "launched" when VMLAUNCH fails

The VMCS launch state is not set to "launched" unless the VMLAUNCH actually succeeds. VMLAUNCH failure includes VM-exits with bit 31 set.

Note that this change does not address the general problem that a failure to launch/resume vmcs02 (i.e. vmx->fail) is not handled correctly.

---
 arch/x86/kvm/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +2 -2
index b4cfdcfdc1c1..8ab625feb46e 100644
@@ -10411,8 +10411,6 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
 		return 1;
 	}
 
-	vmcs12->launch_state = 1;
-
 	/*
 	 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
 	 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
@@ -10786,6 +10784,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
 	vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
 
 	if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
+		vmcs12->launch_state = 1;
+
 		/* vm_entry_intr_info_field is cleared on exit. Emulate this
 		 * instead of reading the real value. */
 		vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#1 lore raw

PATCH 2/3 kvm: nVMX: Validate the I/O bitmaps on nested VM-entry

According to the SDM, if the "use I/O bitmaps" VM-execution control is 1, bits 11:0 of each I/O-bitmap address must be 0. Neither address should set any bits beyond the processor's physical-address width.

---
 arch/x86/kvm/vmx.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/x86/kvm/vmx.c +16 -0
index 8ab625feb46e..f03dcd744557 100644
@@ -9571,6 +9571,19 @@ static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
 		      ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
 }
 
+static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
+					       struct vmcs12 *vmcs12)
+{
+	if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
+		return 0;
+
+	if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
+	    !page_address_valid(vcpu, vmcs12->io_bitmap_b))
+		return -EINVAL;
+
+	return 0;
+}
+
 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
 						struct vmcs12 *vmcs12)
 {
@@ -10275,6 +10288,9 @@ static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
 	    vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
 		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
 
+	if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
+		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
+
 	if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
 		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
 
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
maintainer Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] kvm: nVMX: Validate the I/O bitmaps on nested VM-entry

Quoted 5 lines · Jim Mattson

According to the SDM, if the "use I/O bitmaps" VM-execution control is 1, bits 11:0 of each I/O-bitmap address must be 0. Neither address should set any bits beyond the processor's physical-address width.

Quoted 39 lines
---
 arch/x86/kvm/vmx.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/x86/kvm/vmx.c +16 -0
index 8ab625feb46e..f03dcd744557 100644
@@ -9571,6 +9571,19 @@ static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
 		      ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
 }
 
+static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
+					       struct vmcs12 *vmcs12)
+{
+	if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
+		return 0;
+
+	if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
+	    !page_address_valid(vcpu, vmcs12->io_bitmap_b))
+		return -EINVAL;
+
+	return 0;
+}
+
 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
 						struct vmcs12 *vmcs12)
 {
@@ -10275,6 +10288,9 @@ static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
 	    vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
 		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
 
+	if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
+		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
+
 	if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
 		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
 
#3 lore raw

Re: [PATCH 2/3] kvm: nVMX: Validate the I/O bitmaps on nested VM-entry

In reply to Jim Mattson
reproduce:

Hi Jim,

[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v4.12 next-20170707]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Jim-Mattson/kvm-nVMX-Don-t-set-vmcs12-to-launched-when-VMLAUNCH-fails/20170708-143456
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: x86_64-randconfig-x018-201727 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

Note: the linux-review/Jim-Mattson/kvm-nVMX-Don-t-set-vmcs12-to-launched-when-VMLAUNCH-fails/20170708-143456 HEAD 34a49303d38afcc4d67de2391b2fa3aa769e458a builds fine.
It only hurts bisectibility.

All errors (new ones prefixed by >>):

In file included from include/uapi/linux/stddef.h:1:0,

                    from include/linux/stddef.h:4,
                    from include/uapi/linux/posix_types.h:4,
                    from include/uapi/linux/types.h:13,
                    from include/linux/types.h:5,
                    from include/linux/mm_types_task.h:10,
                    from include/linux/mm_types.h:4,
                    from arch/x86/kvm/irq.h:25,
                    from arch/x86/kvm/vmx.c:19:
   arch/x86/kvm/vmx.c: In function 'nested_vmx_check_io_bitmap_controls':
>> arch/x86/kvm/vmx.c:9580:7: error: implicit declaration of function 'page_address_valid' [-Werror=implicit-function-declaration]
     if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
          ^
   include/linux/compiler.h:160:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
   arch/x86/kvm/vmx.c:9580:2: note: in expansion of macro 'if'
     if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
     ^~

Cyclomatic Complexity 5 include/linux/compiler.h:__read_once_size Cyclomatic Complexity 5 include/linux/compiler.h:__write_once_size Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:atomic_read Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:atomic_dec_and_test Cyclomatic Complexity 3 arch/x86/include/asm/bitops.h:set_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:__set_bit Cyclomatic Complexity 3 arch/x86/include/asm/bitops.h:clear_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:__clear_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:test_and_set_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:test_and_clear_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:constant_test_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:variable_test_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:fls Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:fls64 Cyclomatic Complexity 1 include/linux/log2.h:__ilog2_u32
Cyclomatic Complexity 1 include/linux/log2.h:__ilog2_u64
Cyclomatic Complexity 2 arch/x86/include/asm/jump_label.h:arch_static_branch Cyclomatic Complexity 1 include/linux/jump_label.h:static_key_false Cyclomatic Complexity 1 include/linux/cpumask.h:cpumask_check Cyclomatic Complexity 1 include/linux/cpumask.h:cpumask_set_cpu Cyclomatic Complexity 1 include/linux/cpumask.h:cpumask_clear_cpu Cyclomatic Complexity 1 include/linux/cpumask.h:cpumask_test_cpu Cyclomatic Complexity 1 include/linux/cpumask.h:get_cpu_mask Cyclomatic Complexity 1 include/asm-generic/getorder.h:__get_order Cyclomatic Complexity 1 include/linux/list.h:INIT_LIST_HEAD Cyclomatic Complexity 1 include/linux/list.h:__list_del
Cyclomatic Complexity 1 arch/x86/include/asm/current.h:get_current Cyclomatic Complexity 1 arch/x86/include/asm/segment.h:__loadsegment_fs Cyclomatic Complexity 1 arch/x86/include/asm/msr.h:rdtsc
Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:__cpuid Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:paravirt_get_debugreg Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:read_cr0 Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:read_cr3 Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:__write_cr4 Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:paravirt_read_msr Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:paravirt_write_msr Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:paravirt_read_msr_safe Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:paravirt_write_msr_safe Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:wrmsrl Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:rdmsrl_safe Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:load_TR_desc Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:load_gs_index Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:write_gdt_entry Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:arch_local_save_flags Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:arch_local_irq_disable Cyclomatic Complexity 1 arch/x86/include/asm/paravirt.h:arch_local_irq_enable Cyclomatic Complexity 1 arch/x86/include/asm/special_insns.h:__read_pkru Cyclomatic Complexity 1 arch/x86/include/asm/special_insns.h:__write_pkru Cyclomatic Complexity 1 include/linux/err.h:ERR_PTR
Cyclomatic Complexity 1 arch/x86/include/asm/irqflags.h:arch_irqs_disabled_flags Cyclomatic Complexity 1 arch/x86/include/asm/processor.h:cpuid Cyclomatic Complexity 1 arch/x86/include/asm/processor.h:cpuid_eax Cyclomatic Complexity 1 arch/x86/include/asm/processor.h:cpuid_ecx Cyclomatic Complexity 1 arch/x86/include/asm/processor.h:get_debugctlmsr Cyclomatic Complexity 1 arch/x86/include/asm/processor.h:update_debugctlmsr Cyclomatic Complexity 1 arch/x86/include/asm/preempt.h:preempt_count Cyclomatic Complexity 7 arch/x86/include/asm/preempt.h:__preempt_count_add Cyclomatic Complexity 7 arch/x86/include/asm/preempt.h:__preempt_count_sub Cyclomatic Complexity 1 include/linux/spinlock.h:spinlock_check Cyclomatic Complexity 1 include/linux/spinlock.h:spin_lock Cyclomatic Complexity 1 include/linux/spinlock.h:spin_unlock Cyclomatic Complexity 1 include/linux/spinlock.h:spin_unlock_irqrestore Cyclomatic Complexity 1 include/linux/ktime.h:ns_to_ktime Cyclomatic Complexity 1 include/linux/rcupdate.h:rcu_read_lock_sched_notrace Cyclomatic Complexity 1 include/linux/rcupdate.h:rcu_read_unlock_sched_notrace Cyclomatic Complexity 1 include/linux/nodemask.h:node_state Cyclomatic Complexity 1 arch/x86/include/asm/topology.h:numa_node_id Cyclomatic Complexity 1 arch/x86/include/asm/apic.h:x2apic_enabled Cyclomatic Complexity 1 include/linux/topology.h:numa_mem_id Cyclomatic Complexity 1 include/linux/gfp.h:gfp_zonelist
Cyclomatic Complexity 1 include/linux/gfp.h:node_zonelist Cyclomatic Complexity 1 include/linux/sched.h:task_tgid_nr Cyclomatic Complexity 1 include/linux/sched.h:task_thread_info Cyclomatic Complexity 1 include/linux/sched.h:need_resched Cyclomatic Complexity 1 include/linux/page_ref.h:page_ref_count Cyclomatic Complexity 1 include/linux/page_ref.h:page_ref_dec_and_test Cyclomatic Complexity 2 arch/x86/include/asm/pgtable.h:read_pkru Cyclomatic Complexity 1 include/linux/mm.h:lowmem_page_address Cyclomatic Complexity 56 include/linux/slab.h:kmalloc_index Cyclomatic Complexity 68 include/linux/slab.h:kmalloc_large Cyclomatic Complexity 9 include/linux/slab.h:kmalloc
Cyclomatic Complexity 1 arch/x86/include/asm/desc.h:get_current_gdt_rw Cyclomatic Complexity 1 arch/x86/include/asm/desc.h:get_cpu_gdt_ro_index Cyclomatic Complexity 1 arch/x86/include/asm/desc.h:get_cpu_gdt_ro Cyclomatic Complexity 1 arch/x86/include/asm/desc.h:get_current_gdt_ro Cyclomatic Complexity 1 arch/x86/include/asm/desc.h:native_store_idt Cyclomatic Complexity 1 arch/x86/include/asm/desc.h:force_reload_TR Cyclomatic Complexity 1 arch/x86/include/asm/kvm_host.h:kvm_read_ldt Cyclomatic Complexity 1 arch/x86/include/asm/kvm_host.h:kvm_load_ldt Cyclomatic Complexity 1 arch/x86/include/asm/kvm_host.h:read_msr Cyclomatic Complexity 1 arch/x86/include/asm/kvm_host.h:get_rdx_init_val Cyclomatic Complexity 1 arch/x86/include/asm/kvm_host.h:get_canonical

vim +/page_address_valid +9580 arch/x86/kvm/vmx.c

9574 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
9575 struct vmcs12 *vmcs12)
9576 {
9577 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9578 return 0;
9579

> 9580		if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||

9581 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
9582 return -EINVAL;
9583

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw

PATCH v2 1/3 kvm: nVMX: Don't set vmcs12 to "launched" when VMLAUNCH fails

The VMCS launch state is not set to "launched" unless the VMLAUNCH actually succeeds. VMLAUNCH failure includes VM-exits with bit 31 set.

Note that this change does not address the general problem that a failure to launch/resume vmcs02 (i.e. vmx->fail) is not handled correctly.

---
 arch/x86/kvm/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +2 -2
index f76efad248ab..7ffe1c8438c2 100644
@@ -10429,8 +10429,6 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
 		return 1;
 	}
 
-	vmcs12->launch_state = 1;
-
 	/*
 	 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
 	 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
@@ -10804,6 +10802,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
 	vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
 
 	if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
+		vmcs12->launch_state = 1;
+
 		/* vm_entry_intr_info_field is cleared on exit. Emulate this
 		 * instead of reading the real value. */
 		vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#5 lore raw

PATCH v2 2/3 kvm: nVMX: Fix nested_vmx_check_msr_bitmap_controls

Allow the L1 guest to specify the last page of addressable guest physical memory for an L2 MSR permission bitmap. Also remove the vmcs12_read_any() check that should never fail.

Fixes 3af18d9c5fe95 ("KVM: nVMX: Prepare for using hardware MSR bitmap")
Change-Id I37f0482f32c4e77e403f6726e63bbe99880a185b
---
 arch/x86/kvm/vmx.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
arch/x86/kvm/vmx.c +6 -11
index 7ffe1c8438c2..eec9e0191311 100644
@@ -4634,6 +4634,11 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu)
 	return true;
 }
 
+static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
+{
+	return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
+}
+
 static int init_rmode_tss(struct kvm *kvm)
 {
 	gfn_t fn;
@@ -9592,20 +9597,10 @@ static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
 						struct vmcs12 *vmcs12)
 {
-	int maxphyaddr;
-	u64 addr;
-
 	if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
 		return 0;
 
-	if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
-		WARN_ON(1);
-		return -EINVAL;
-	}
-	maxphyaddr = cpuid_maxphyaddr(vcpu);
-
-	if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
-	   ((addr + PAGE_SIZE) >> maxphyaddr))
+	if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
 		return -EINVAL;
 
 	return 0;
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#6 lore raw

PATCH v2 3/3 kvm: nVMX: Validate the I/O bitmaps on nested VM-entry

In reply to Jim Mattson

According to the SDM, if the “use I/O bitmaps” VM-execution control is 1, bits 11:0 of each I/O-bitmap address must be 0. Neither address should set any bits beyond the processor’s physical-address width.

---
 arch/x86/kvm/vmx.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/x86/kvm/vmx.c +16 -0
index eec9e0191311..7f27e7908477 100644
@@ -9594,6 +9594,19 @@ static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
 		      ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
 }
 
+static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
+					       struct vmcs12 *vmcs12)
+{
+	if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
+		return 0;
+
+	if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
+	    !page_address_valid(vcpu, vmcs12->io_bitmap_b))
+		return -EINVAL;
+
+	return 0;
+}
+
 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
 						struct vmcs12 *vmcs12)
 {
@@ -10288,6 +10301,9 @@ static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
 	    vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
 		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
 
+	if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
+		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
+
 	if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
 		return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
 
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#7 lore raw

PATCH RESEND 3/3 kvm: nVMX: Fix nested_vmx_check_msr_bitmap_controls

In reply to Jim Mattson

Allow the L1 guest to specify the last page of addressable guest physical memory for an L2 MSR permission bitmap. Also remove the vmcs12_read_any() check that should never fail.

Fixes 3af18d9c5fe95 ("KVM: nVMX: Prepare for using hardware MSR bitmap")
---
 arch/x86/kvm/vmx.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
arch/x86/kvm/vmx.c +6 -11
index f03dcd744557..9bca5a59d65a 100644
@@ -4631,6 +4631,11 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu)
 	return true;
 }
 
+static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
+{
+	return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
+}
+
 static int init_rmode_tss(struct kvm *kvm)
 {
 	gfn_t fn;
@@ -9587,20 +9592,10 @@ static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
 						struct vmcs12 *vmcs12)
 {
-	int maxphyaddr;
-	u64 addr;
-
 	if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
 		return 0;
 
-	if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
-		WARN_ON(1);
-		return -EINVAL;
-	}
-	maxphyaddr = cpuid_maxphyaddr(vcpu);
-
-	if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
-	   ((addr + PAGE_SIZE) >> maxphyaddr))
+	if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
 		return -EINVAL;
 
 	return 0;
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
maintainer Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3 RESEND] kvm: nVMX: Fix nested_vmx_check_msr_bitmap_controls

Quoted 9 lines · Jim Mattson

Allow the L1 guest to specify the last page of addressable guest physical memory for an L2 MSR permission bitmap. Also remove the vmcs12_read_any() check that should never fail.

Fixes 3af18d9c5fe95 ("KVM: nVMX: Prepare for using hardware MSR bitmap")
---
 arch/x86/kvm/vmx.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
Quoted 39 lines
arch/x86/kvm/vmx.c +6 -11
index f03dcd744557..9bca5a59d65a 100644
@@ -4631,6 +4631,11 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu)
 	return true;
 }
 
+static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
+{
+	return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
+}
+
 static int init_rmode_tss(struct kvm *kvm)
 {
 	gfn_t fn;
@@ -9587,20 +9592,10 @@ static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
 						struct vmcs12 *vmcs12)
 {
-	int maxphyaddr;
-	u64 addr;
-
 	if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
 		return 0;
 
-	if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
-		WARN_ON(1);
-		return -EINVAL;
-	}
-	maxphyaddr = cpuid_maxphyaddr(vcpu);
-
-	if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
-	   ((addr + PAGE_SIZE) >> maxphyaddr))
+	if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
 		return -EINVAL;
 
 	return 0;
maintainer Reviewed-by on v1 1/3
#9 lore raw
In reply to Jim Mattson
Quoted 8 lines · Jim Mattson

The VMCS launch state is not set to "launched" unless the VMLAUNCH actually succeeds. VMLAUNCH failure includes VM-exits with bit 31 set.

Note that this change does not address the general problem that a failure to launch/resume vmcs02 (i.e. vmx->fail) is not handled correctly.

Quoted 27 lines
---
 arch/x86/kvm/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +2 -2
index b4cfdcfdc1c1..8ab625feb46e 100644
@@ -10411,8 +10411,6 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
 		return 1;
 	}
 
-	vmcs12->launch_state = 1;
-
 	/*
 	 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
 	 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
@@ -10786,6 +10784,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
 	vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
 
 	if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
+		vmcs12->launch_state = 1;
+
 		/* vm_entry_intr_info_field is cleared on exit. Emulate this
 		 * instead of reading the real value. */
 		vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;