Skip to contentSkip to conversation

KVM: nSVM: ondemand nested state allocation

PATCH Superseded Maxim Levitsky 2 participants 8 messages Sep 21, 2020 – Sep 22, 2020 1 message not in the archive

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover KVM: nSVM: ondemand nested state allocation

This is yet another version of ondemand nested state allocation.

In this version I adoped the suggestion of Sean Christopherson to return make EFER write return a negative error which then should propogate to the userspace.

So I fixed the WRMSR code to actually obey this (#GP on positive return value, exit to userspace when negative error value, and success on 0 error value, and fixed one user (xen) that returned negative error code on failures.

The XEN patch is only compile tested. The rest were tested by always returning -ENOMEM from svm_allocate_nested.

Best regards,
Maxim Levitsky

Maxim Levitsky (4):
KVM: x86: xen_hvm_config cleanup return values
KVM: x86: report negative values from wrmsr to userspace
KVM: x86: allow kvm_x86_ops.set_efer to return a value
KVM: nSVM: implement ondemand allocation of the nested state

 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/emulate.c          |  7 ++--
 arch/x86/kvm/svm/nested.c       | 42 ++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c          | 58 +++++++++++++++++++--------------
 arch/x86/kvm/svm/svm.h          |  8 ++++-
 arch/x86/kvm/vmx/vmx.c          |  9 +++--
 arch/x86/kvm/x86.c              | 36 ++++++++++----------
 7 files changed, 113 insertions(+), 49 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v5 1/4 KVM: x86: xen_hvm_config: cleanup return values

MSR writes should return 1 when giving #GP to the user, and negative value when fatal error (e.g out of memory) happened.

---
 arch/x86/kvm/x86.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
arch/x86/kvm/x86.c +9 -14
index 17f4995e80a7e..063d70e736f7f 100644
@@ -2694,24 +2694,19 @@ static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
 	u32 page_num = data & ~PAGE_MASK;
 	u64 page_addr = data & PAGE_MASK;
 	u8 *page;
-	int r;
 
-	r = -E2BIG;
 	if (page_num >= blob_size)
-		goto out;
-	r = -ENOMEM;
+		return 1;
+
 	page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
-	if (IS_ERR(page)) {
-		r = PTR_ERR(page);
-		goto out;
+	if (IS_ERR(page))
+		return PTR_ERR(page);
+
+	if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) {
+		kfree(page);
+		return 1;
 	}
-	if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
-		goto out_free;
-	r = 0;
-out_free:
-	kfree(page);
-out:
-	return r;
+	return 0;
 }
 
 static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu)
Signature · 1 line
2.26.2
#2 lore raw

PATCH v5 2/4 KVM: x86: report negative values from wrmsr to userspace

In reply to Maxim Levitsky

This will allow us to make some MSR writes fatal to the guest (e.g when out of memory condition occurs)

---
 arch/x86/kvm/emulate.c | 7 +++++--
 arch/x86/kvm/x86.c     | 5 +++--
 2 files changed, 8 insertions(+), 4 deletions(-)
arch/x86/kvm/emulate.c +5 -2
index 1d450d7710d63..d855304f5a509 100644
@@ -3702,13 +3702,16 @@ static int em_dr_write(struct x86_emulate_ctxt *ctxt)
 static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
 {
 	u64 msr_data;
+	int ret;
 
 	msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
 		| ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
-	if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
+
+	ret = ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data);
+	if (ret > 0)
 		return emulate_gp(ctxt, 0);
 
-	return X86EMUL_CONTINUE;
+	return ret < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
 }
 
 static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
arch/x86/kvm/x86.c +3 -2
index 063d70e736f7f..b6c67ab7c4f34 100644
@@ -1612,15 +1612,16 @@ int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
 {
 	u32 ecx = kvm_rcx_read(vcpu);
 	u64 data = kvm_read_edx_eax(vcpu);
+	int ret = kvm_set_msr(vcpu, ecx, data);
 
-	if (kvm_set_msr(vcpu, ecx, data)) {
+	if (ret > 0) {
 		trace_kvm_msr_write_ex(ecx, data);
 		kvm_inject_gp(vcpu, 0);
 		return 1;
 	}
 
 	trace_kvm_msr_write(ecx, data);
-	return kvm_skip_emulated_instruction(vcpu);
+	return ret < 0 ? ret : kvm_skip_emulated_instruction(vcpu);
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
 
Signature · 1 line
2.26.2
#3 lore raw

Re: [PATCH v5 2/4] KVM: x86: report negative values from wrmsr to userspace

Quoted 40 lines · Maxim Levitsky

This will allow us to make some MSR writes fatal to the guest (e.g when out of memory condition occurs)

---
 arch/x86/kvm/emulate.c | 7 +++++--
 arch/x86/kvm/x86.c     | 5 +++--
 2 files changed, 8 insertions(+), 4 deletions(-)
arch/x86/kvm/emulate.c +5 -2
index 1d450d7710d63..d855304f5a509 100644
@@ -3702,13 +3702,16 @@ static int em_dr_write(struct x86_emulate_ctxt *ctxt)
 static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
 {
 	u64 msr_data;
+	int ret;
 
 	msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
 		| ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
-	if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
+
+	ret = ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data);
+	if (ret > 0)
 		return emulate_gp(ctxt, 0);
 
-	return X86EMUL_CONTINUE;
+	return ret < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
 }
 
 static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
arch/x86/kvm/x86.c +0 -0
index 063d70e736f7f..b6c67ab7c4f34 100644
@@ -1612,15 +1612,16 @@ int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
 {
 	u32 ecx = kvm_rcx_read(vcpu);
 	u64 data = kvm_read_edx_eax(vcpu);

arch/x86/kvm/x86.c int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) · line 1623

Quoted 10 lines
arch/x86/kvm/x86.c +2 -1
+	int ret = kvm_set_msr(vcpu, ecx, data);
 
-	if (kvm_set_msr(vcpu, ecx, data)) {
+	if (ret > 0) {
 		trace_kvm_msr_write_ex(ecx, data);
 		kvm_inject_gp(vcpu, 0);
 		return 1;
 	}
 
 	trace_kvm_msr_write(ecx, data);

Tracing the access as non-faulting feels wrong. The WRMSR has not completed, e.g. if userspace cleanly handles -ENOMEM and restarts the guest, KVM would trace the WRMSR twice.

What about:

	int ret = kvm_set_msr(vcpu, ecx, data);

	if (ret < 0)
		return ret;

	if (ret) {
		trace_kvm_msr_write_ex(ecx, data);
		kvm_inject_gp(vcpu, 0);
		return 1;
	}

	trace_kvm_msr_write(ecx, data);
	return kvm_skip_emulated_instruction(vcpu);
Quoted 8 lines
arch/x86/kvm/x86.c +1 -1
-	return kvm_skip_emulated_instruction(vcpu);
+	return ret < 0 ? ret : kvm_skip_emulated_instruction(vcpu);
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
 
Signature · 1 line
2.26.2
#4 lore raw

Re: [PATCH v5 2/4] KVM: x86: report negative values from wrmsr to userspace

Quoted 50 lines · Sean Christopherson
Quoted 49 lines · Maxim Levitsky

This will allow us to make some MSR writes fatal to the guest (e.g when out of memory condition occurs)

---
 arch/x86/kvm/emulate.c | 7 +++++--
 arch/x86/kvm/x86.c     | 5 +++--
 2 files changed, 8 insertions(+), 4 deletions(-)
arch/x86/kvm/emulate.c +5 -2
index 1d450d7710d63..d855304f5a509 100644
@@ -3702,13 +3702,16 @@ static int em_dr_write(struct x86_emulate_ctxt *ctxt)
 static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
 {
 	u64 msr_data;
+	int ret;
 
 	msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
 		| ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
-	if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
+
+	ret = ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data);
+	if (ret > 0)
 		return emulate_gp(ctxt, 0);
 
-	return X86EMUL_CONTINUE;
+	return ret < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
 }
 
 static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
arch/x86/kvm/x86.c +2 -1
index 063d70e736f7f..b6c67ab7c4f34 100644
@@ -1612,15 +1612,16 @@ int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
 {
 	u32 ecx = kvm_rcx_read(vcpu);
 	u64 data = kvm_read_edx_eax(vcpu);
+	int ret = kvm_set_msr(vcpu, ecx, data);
 
-	if (kvm_set_msr(vcpu, ecx, data)) {
+	if (ret > 0) {
 		trace_kvm_msr_write_ex(ecx, data);
 		kvm_inject_gp(vcpu, 0);
 		return 1;
 	}
 
Quoted 5 lines

arch/x86/kvm/x86.c int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) · line 1623

Quoted 1 line
arch/x86/kvm/x86.c +0 -0
 	trace_kvm_msr_write(ecx, data);

Tracing the access as non-faulting feels wrong. The WRMSR has not completed, e.g. if userspace cleanly handles -ENOMEM and restarts the guest, KVM would trace the WRMSR twice.

I guess you are right. Since in this case we didn't actually executed the instruction (exception can also be thought as an execution of an instruction, since it leads to the exception handler), but in
this case we just fail
and let the userspace do something so we can restart from the same point again.

So I'll go with your suggestion.

Thanks for the review,
Best regards,
Maxim Levitsky

Quoted 25 lines

What about:

	int ret = kvm_set_msr(vcpu, ecx, data);

	if (ret < 0)
		return ret;

	if (ret) {
		trace_kvm_msr_write_ex(ecx, data);
		kvm_inject_gp(vcpu, 0);
		return 1;
	}

	trace_kvm_msr_write(ecx, data);
	return kvm_skip_emulated_instruction(vcpu);
Quoted 8 lines
arch/x86/kvm/x86.c +1 -1
-	return kvm_skip_emulated_instruction(vcpu);
+	return ret < 0 ? ret : kvm_skip_emulated_instruction(vcpu);
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
 
Signature · 1 line
2.26.2
#5 lore raw

PATCH v5 3/4 KVM: x86: allow kvm_x86_ops.set_efer to return a value

In reply to Maxim Levitsky

This will be used later to return an error when setting this msr fails.

Note that we ignore this return value for qemu initiated writes to avoid breaking backward compatibility.

---
 arch/x86/include/asm/kvm_host.h | 2 +-
 arch/x86/kvm/svm/svm.c          | 3 ++-
 arch/x86/kvm/svm/svm.h          | 2 +-
 arch/x86/kvm/vmx/vmx.c          | 9 ++++++---
 arch/x86/kvm/x86.c              | 8 +++++++-
 5 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 5303dbc5c9bce..b273c199b9a55 100644
@@ -1069,7 +1069,7 @@ struct kvm_x86_ops {
 	void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
 	void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
 	int (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
-	void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
+	int (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
 	void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
 	void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
 	void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
arch/x86/kvm/svm/svm.c +2 -1
index 3da5b2f1b4a19..18f8af55e970a 100644
@@ -263,7 +263,7 @@ static int get_max_npt_level(void)
 #endif
 }
 
-void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
+int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 	vcpu->arch.efer = efer;
@@ -283,6 +283,7 @@ void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 
 	svm->vmcb->save.efer = efer | EFER_SVME;
 	vmcb_mark_dirty(svm->vmcb, VMCB_CR);
+	return 0;
 }
 
 static int is_external_interrupt(u32 info)
arch/x86/kvm/svm/svm.h +1 -1
index 45496775f0db2..1e1842de0efe7 100644
@@ -338,7 +338,7 @@ static inline bool gif_set(struct vcpu_svm *svm)
 #define MSR_INVALID				0xffffffffU
 
 u32 svm_msrpm_offset(u32 msr);
-void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
+int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
 int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
 void svm_flush_tlb(struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx/vmx.c +6 -3
index 6f9a0c6d5dc59..8aef1926e26be 100644
@@ -2835,13 +2835,15 @@ static void enter_rmode(struct kvm_vcpu *vcpu)
 	kvm_mmu_reset_context(vcpu);
 }
 
-void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
+int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
 
-	if (!msr)
-		return;
+	if (!msr) {
+		/* Host doen't support EFER, nothing to do */
+		return 0;
+	}
 
 	vcpu->arch.efer = efer;
 	if (efer & EFER_LMA) {
@@ -2853,6 +2855,7 @@ void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 		msr->data = efer & ~EFER_LME;
 	}
 	setup_msrs(vmx);
+	return 0;
 }
 
 #ifdef CONFIG_X86_64
arch/x86/kvm/x86.c +7 -1
index b6c67ab7c4f34..cab189a71cbb7 100644
@@ -1456,6 +1456,7 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	u64 old_efer = vcpu->arch.efer;
 	u64 efer = msr_info->data;
+	int r;
 
 	if (efer & efer_reserved_bits)
 		return 1;
@@ -1472,7 +1473,12 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	efer &= ~EFER_LMA;
 	efer |= vcpu->arch.efer & EFER_LMA;
 
-	kvm_x86_ops.set_efer(vcpu, efer);
+	r = kvm_x86_ops.set_efer(vcpu, efer);
+
+	if (r && !msr_info->host_initiated) {
+		WARN_ON(r > 0);
+		return r;
+	}
 
 	/* Update reserved bits */
 	if ((efer ^ old_efer) & EFER_NX)
Signature · 1 line
2.26.2

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Re: [PATCH v5 3/4] KVM: x86: allow kvm_x86_ops.set_efer to return a value

Quoted 20 lines · Sean Christopherson
Quoted 19 lines · Maxim Levitsky

This will be used later to return an error when setting this msr fails.

Note that we ignore this return value for qemu initiated writes to avoid breaking backward compatibility.

---

arch/x86/kvm/vmx/vmx.c +1 -1
@@ -2835,13 +2835,15 @@ static void enter_rmode(struct kvm_vcpu *vcpu)
 	kvm_mmu_reset_context(vcpu);
 }
 
-void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
+int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
 
Quoted 9 lines

arch/x86/kvm/vmx/vmx.c static void enter_rmode(struct kvm_vcpu *vcpu) · line 2846

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +4 -2
-	if (!msr)
-		return;
+	if (!msr) {
+		/* Host doen't support EFER, nothing to do */
+		return 0;
+	}

Kernel style is to omit braces, even with a line comment. Though I would do something like so to avoid the question.

I didn't knew this, but next time I'll will take this in account!

Quoted 4 lines
	/* Nothing to do if hardware doesn't support EFER. */
	if (!msr)
		return 0

I'll do this.

Quoted 27 lines
Quoted 27 lines
arch/x86/kvm/vmx/vmx.c +1 -0
 	vcpu->arch.efer = efer;
 	if (efer & EFER_LMA) {
@@ -2853,6 +2855,7 @@ void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 		msr->data = efer & ~EFER_LME;
 	}
 	setup_msrs(vmx);
+	return 0;
 }
 
 #ifdef CONFIG_X86_64
arch/x86/kvm/x86.c +1 -0
index b6c67ab7c4f34..cab189a71cbb7 100644
@@ -1456,6 +1456,7 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	u64 old_efer = vcpu->arch.efer;
 	u64 efer = msr_info->data;
+	int r;
 
 	if (efer & efer_reserved_bits)
 		return 1;
@@ -1472,7 +1473,12 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	efer &= ~EFER_LMA;
 	efer |= vcpu->arch.efer & EFER_LMA;
 
Quoted 10 lines

arch/x86/kvm/x86.c static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 1478

Quoted 4 lines
arch/x86/kvm/x86.c +3 -1
-	kvm_x86_ops.set_efer(vcpu, efer);
+	r = kvm_x86_ops.set_efer(vcpu, efer);
+
+	if (r && !msr_info->host_initiated) {

I get the desire to not break backwards compatibility, but this feels all kinds of wrong, and potentially dangerous as it will KVM in a mixed state. E.g. vcpu->arch.efer will show that nSVM is enabled, but SVM will not have the necessary tracking state allocated. That could lead to a userspace triggerable #GP/panic.

Actually I take care to restore the vcpu->arch.efer to its old value if an error happens, so in case of failure everything would indicate that nothing happened, and the offending EFER write can even be retried, however since we agreed that .set_efer will only fail with negative errors like -ENOMEM, I agree that there is no reason to treat userspace writes differently. This code is actually a leftover from previous version, which I should have removed.

I'll send a new version soon.

Thanks for the review,
Best regards,
Maxim Levitsky

Quoted 16 lines

Is ignoring OOM scenario really considered backwards compability? The VM is probably hosted if KVM returns -ENOMEM, e.g. a sophisticated userspace stack could trigger OOM killer to free memory and resume the VM. On the other hand, the VM is most definitely hosed if KVM ignores the error and puts itself into an invalid state.

Quoted 9 lines
arch/x86/kvm/x86.c +3 -0
+		WARN_ON(r > 0);
+		return r;
+	}
 
 	/* Update reserved bits */
 	if ((efer ^ old_efer) & EFER_NX)
Signature · 1 line
2.26.2
#8 lore raw

PATCH v5 4/4 KVM: nSVM: implement ondemand allocation of the nested state

In reply to Maxim Levitsky

This way we don't waste memory on VMs which don't use nesting virtualization even if it is available to them.

If allocation of nested state fails (which should happen, only when host is about to OOM anyway), use new KVM_REQ_OUT_OF_MEMORY request to shut down the guest

---
 arch/x86/kvm/svm/nested.c | 42 ++++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c    | 55 ++++++++++++++++++++++-----------------
 arch/x86/kvm/svm/svm.h    |  6 +++++
 3 files changed, 79 insertions(+), 24 deletions(-)
arch/x86/kvm/svm/nested.c +42 -0
index 09417f5197410..dd13856818a03 100644
@@ -467,6 +467,9 @@ int nested_svm_vmrun(struct vcpu_svm *svm)
 
 	vmcb12 = map.hva;
 
+	if (WARN_ON(!svm->nested.initialized))
+		return 1;
+
 	if (!nested_vmcb_checks(svm, vmcb12)) {
 		vmcb12->control.exit_code    = SVM_EXIT_ERR;
 		vmcb12->control.exit_code_hi = 0;
@@ -684,6 +687,45 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 	return 0;
 }
 
+int svm_allocate_nested(struct vcpu_svm *svm)
+{
+	struct page *hsave_page;
+
+	if (svm->nested.initialized)
+		return 0;
+
+	hsave_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+	if (!hsave_page)
+		return -ENOMEM;
+
+	svm->nested.hsave = page_address(hsave_page);
+
+	svm->nested.msrpm = svm_vcpu_init_msrpm();
+	if (!svm->nested.msrpm)
+		goto err_free_hsave;
+
+	svm->nested.initialized = true;
+	return 0;
+
+err_free_hsave:
+	__free_page(hsave_page);
+	return -ENOMEM;
+}
+
+void svm_free_nested(struct vcpu_svm *svm)
+{
+	if (!svm->nested.initialized)
+		return;
+
+	svm_vcpu_free_msrpm(svm->nested.msrpm);
+	svm->nested.msrpm = NULL;
+
+	__free_page(virt_to_page(svm->nested.hsave));
+	svm->nested.hsave = NULL;
+
+	svm->nested.initialized = false;
+}
+
 /*
  * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  */
arch/x86/kvm/svm/svm.c +31 -24

121 lines not on this page · raw message

arch/x86/kvm/svm/svm.h +6 -0
index 1e1842de0efe7..10453abc5bed3 100644
@@ -96,6 +96,8 @@ struct svm_nested_state {
 
 	/* cache for control fields of the guest */
 	struct vmcb_control_area ctl;
+
+	bool initialized;
 };
 
 struct vcpu_svm {
@@ -339,6 +341,8 @@ static inline bool gif_set(struct vcpu_svm *svm)
 
 u32 svm_msrpm_offset(u32 msr);
 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
+u32 *svm_vcpu_init_msrpm(void);
+void svm_vcpu_free_msrpm(u32 *msrpm);
 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
 int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
 void svm_flush_tlb(struct kvm_vcpu *vcpu);
@@ -379,6 +383,8 @@ static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
 int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
 			 struct vmcb *nested_vmcb);
 void svm_leave_nested(struct vcpu_svm *svm);
+void svm_free_nested(struct vcpu_svm *svm);
+int svm_allocate_nested(struct vcpu_svm *svm);
 int nested_svm_vmrun(struct vcpu_svm *svm);
 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb);
 int nested_svm_vmexit(struct vcpu_svm *svm);
Signature · 1 line
2.26.2