Skip to contentSkip to conversation

KVM: Switch 'requests' to be 64-bit (explicitly)

PATCH In review KarimAllah Ahmed 4 participants 7 messages Jul 10, 2018 – Jul 18, 2018

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

PATCH v5 1/2 KVM: Switch 'requests' to be 64-bit (explicitly)

Switch 'requests' to be explicitly 64-bit and update BUILD_BUG_ON check to use the size of "requests" instead of the hard-coded '32'.

That gives us a bit more room again for arch-specific requests as we already ran out of space for x86 due to the hard-coded check.

The only exception here is ARM32 as it is still 32-bits.

---
v1 -> v2:
- Use FIELD_SIZEOF

---
 include/linux/kvm_host.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
include/linux/kvm_host.h +5 -5
index 4ee7bc5..64518a1 100644
@@ -130,7 +130,7 @@ static inline bool is_error_page(struct page *page)
 #define KVM_REQUEST_ARCH_BASE     8
 
 #define KVM_ARCH_REQ_FLAGS(nr, flags) ({ \
-	BUILD_BUG_ON((unsigned)(nr) >= 32 - KVM_REQUEST_ARCH_BASE); \
+	BUILD_BUG_ON((unsigned)(nr) >= (FIELD_SIZEOF(struct kvm_vcpu, requests) * 8) - KVM_REQUEST_ARCH_BASE); \
 	(unsigned)(((nr) + KVM_REQUEST_ARCH_BASE) | (flags)); \
 })
 #define KVM_ARCH_REQ(nr)           KVM_ARCH_REQ_FLAGS(nr, 0)
@@ -224,7 +224,7 @@ struct kvm_vcpu {
 	int vcpu_id;
 	int srcu_idx;
 	int mode;
-	unsigned long requests;
+	u64 requests;
 	unsigned long guest_debug;
 
 	int pre_pcpu;
@@ -1124,7 +1124,7 @@ static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
 	 * caller.  Paired with the smp_mb__after_atomic in kvm_check_request.
 	 */
 	smp_wmb();
-	set_bit(req & KVM_REQUEST_MASK, &vcpu->requests);
+	set_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
 }
 
 static inline bool kvm_request_pending(struct kvm_vcpu *vcpu)
@@ -1134,12 +1134,12 @@ static inline bool kvm_request_pending(struct kvm_vcpu *vcpu)
 
 static inline bool kvm_test_request(int req, struct kvm_vcpu *vcpu)
 {
-	return test_bit(req & KVM_REQUEST_MASK, &vcpu->requests);
+	return test_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
 }
 
 static inline void kvm_clear_request(int req, struct kvm_vcpu *vcpu)
 {
-	clear_bit(req & KVM_REQUEST_MASK, &vcpu->requests);
+	clear_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
 }
 
 static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v5 2/2 kvm: nVMX: Introduce KVM_CAP_NESTED_STATE

From: Jim Mattson <[email protected]>

For nested virtualization L0 KVM is managing a bit of state for L2 guests, this state can not be captured through the currently available IOCTLs. In fact the state captured through all of these IOCTLs is usually a mix of L1 and L2 state. It is also dependent on whether the L2 guest was running at the moment when the process was interrupted to save its state.

With this capability, there are two new vcpu ioctls: KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE. These can be used for saving and restoring a VM that is in VMX operation.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc H. Peter Anvin <[email protected]>

[karahmed@ - rename structs and functions and make them ready for AMD and
address previous comments.
- handle nested.smm state.
- rebase & a bit of refactoring.
- Merge 7/8 and 8/8 into one patch. ]

---
v4 -> v5:
- Drop the update to KVM_REQUEST_ARCH_BASE in favor of a patch to switch to u64 instead.
- Fix commit message.
- Handle nested.smm state as well.
- rebase

v3 -> v4:
- Rename function to have _nested

v2 -> v3:
- Remove the forced VMExit from L2 after reading the kvm_state. The actual problem is solved.
- Rebase again!
- Set nested_run_pending during restore (not sure if it makes sense yet or not).
- Reduce KVM_REQUEST_ARCH_BASE to 7 instead of 8 (the other alternative is to switch everything to u64)

v1 -> v2:
- Rename structs and functions and make them ready for AMD and address previous comments.
- Rebase & a bit of refactoring.
- Merge 7/8 and 8/8 into one patch.
- Force a VMExit from L2 after reading the kvm_state to avoid mixed state between L1 and L2 on resurrecting the instance.

---
 Documentation/virtual/kvm/api.txt |  46 +++++++++
 arch/x86/include/asm/kvm_host.h   |   7 ++
 arch/x86/include/uapi/asm/kvm.h   |  45 +++++++++
 arch/x86/kvm/vmx.c                | 202 +++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c                |  21 ++++
 include/uapi/linux/kvm.h          |   4 +
 6 files changed, 322 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/api.txt +46 -0
index d10944e..925c509 100644
@@ -3561,6 +3561,52 @@ Returns: 0 on success,
 	-ENOENT on deassign if the conn_id isn't registered
 	-EEXIST on assign if the conn_id is already registered
 
+4.114 KVM_GET_NESTED_STATE
+
+Capability: KVM_CAP_NESTED_STATE
+Architectures: x86
+Type: vcpu ioctl
+Parameters: struct kvm_nested_state (in/out)
+Returns: 0 on success, -1 on error
+Errors:
+  E2BIG:     the data size exceeds the value of 'size' specified by
+             the user (the size required will be written into size).
+
+struct kvm_nested_state {
+	__u16 flags;
+	__u16 format;
+	__u32 size;
+	union {
+		struct kvm_vmx_nested_state vmx;
+		struct kvm_svm_nested_state svm;
+		__u8 pad[120];
+	};
+	__u8 data[0];
+};
+
+This ioctl copies the vcpu's kvm_nested_state struct from the kernel to userspace.
+
+4.115 KVM_SET_NESTED_STATE
+
+Capability: KVM_CAP_NESTED_STATE
+Architectures: x86
+Type: vcpu ioctl
+Parameters: struct kvm_nested_state (in)
+Returns: 0 on success, -1 on error
+
+struct kvm_nested_state {
+	__u16 flags;
+	__u16 format;
+	__u32 size;
+	union {
+		struct kvm_vmx_nested_state vmx;
+		struct kvm_svm_nested_state svm;
+		__u8 pad[120];
+	};
+	__u8 data[0];
+};
+
+This copies the vcpu's kvm_nested_state struct from userspace to the kernel.
 
 5. The kvm_run structure
 ------------------------
arch/x86/include/asm/kvm_host.h +7 -0
index 0dab702..2e8eb08 100644
@@ -75,6 +75,7 @@
 #define KVM_REQ_HV_EXIT			KVM_ARCH_REQ(21)
 #define KVM_REQ_HV_STIMER		KVM_ARCH_REQ(22)
 #define KVM_REQ_LOAD_EOI_EXITMAP	KVM_ARCH_REQ(23)
+#define KVM_REQ_GET_VMCS12_PAGES	KVM_ARCH_REQ(24)
 
 #define CR0_RESERVED_BITS                                               \
 	(~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
@@ -1087,6 +1088,12 @@ struct kvm_x86_ops {
 
 	void (*setup_mce)(struct kvm_vcpu *vcpu);
 
+	int (*get_nested_state)(struct kvm_vcpu *vcpu,
+				struct kvm_nested_state __user *user_kvm_nested_state);
+	int (*set_nested_state)(struct kvm_vcpu *vcpu,
+				struct kvm_nested_state __user *user_kvm_nested_state);
+	void (*get_vmcs12_pages)(struct kvm_vcpu *vcpu);
+
 	int (*smi_allowed)(struct kvm_vcpu *vcpu);
 	int (*pre_enter_smm)(struct kvm_vcpu *vcpu, char *smstate);
 	int (*pre_leave_smm)(struct kvm_vcpu *vcpu, u64 smbase);
arch/x86/include/uapi/asm/kvm.h +45 -0

49 lines not on this page · raw message

arch/x86/kvm/vmx.c +199 -3

233 lines not on this page · raw message

arch/x86/kvm/x86.c +21 -0
index 1b37f2f..2ec6cb9 100644
@@ -2943,6 +2943,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_X2APIC_API:
 		r = KVM_X2APIC_API_VALID_FLAGS;
 		break;
+	case KVM_CAP_STATE:
+		r = !!kvm_x86_ops->get_nested_state;
+		break;
 	default:
 		break;
 	}
@@ -3961,6 +3964,22 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
 		break;
 	}
+	case KVM_GET_NESTED_STATE: {
+		struct kvm_nested_state __user *user_kvm_nested_state = argp;
+
+		r = -EINVAL;
+		if (kvm_x86_ops->get_nested_state)
+			r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state);
+		break;
+	}
+	case KVM_SET_NESTED_STATE: {
+		struct kvm_nested_state __user *user_kvm_nested_state = argp;
+
+		r = -EINVAL;
+		if (kvm_x86_ops->set_nested_state)
+			r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state);
+		break;
+	}
 	default:
 		r = -EINVAL;
 	}
@@ -7309,6 +7328,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	bool req_immediate_exit = false;
 
 	if (kvm_request_pending(vcpu)) {
+		if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu))
+			kvm_x86_ops->get_vmcs12_pages(vcpu);
 		if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
 			kvm_mmu_unload(vcpu);
 		if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
include/uapi/linux/kvm.h +4 -0
index 277cd86..a717e10 100644
@@ -963,6 +963,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_GET_MSR_FEATURES 153
 #define KVM_CAP_HYPERV_EVENTFD 154
 #define KVM_CAP_HYPERV_TLBFLUSH 155
+#define KVM_CAP_STATE 156
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1405,6 +1406,9 @@ struct kvm_enc_region {
 /* Available with KVM_CAP_HYPERV_EVENTFD */
 #define KVM_HYPERV_EVENTFD        _IOW(KVMIO,  0xbd, struct kvm_hyperv_eventfd)
 
+/* Available with KVM_CAP_STATE */
+#define KVM_GET_NESTED_STATE         _IOWR(KVMIO, 0xbe, struct kvm_nested_state)
+#define KVM_SET_NESTED_STATE         _IOW(KVMIO,  0xbf, struct kvm_nested_state)
 
 /* Secure Encrypted Virtualization command */
 enum sev_cmd_id {
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH v5 2/2] kvm: nVMX: Introduce KVM_CAP_NESTED_STATE

arch/x86/kvm/vmx.c static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu) · line 11777

Quoted 8 lines · KarimAllah Ahmed
arch/x86/kvm/vmx.c +0 -1
@@ -11772,7 +11772,6 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
 	if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
 		goto fail;
 
-	nested_get_vmcs12_pages(vcpu, vmcs12);
 
 	r = EXIT_REASON_MSR_LOAD_FAIL;
 	msr_entry_idx = nested_vmx_load_msr(vcpu,

I think this is not enough, the MSR load should not be redone on KVM_SET_NESTED_STATE. This issue is preexisting and happens for SMM exit as well. SMM exit in fact also needs to defer nested_get_vmcs12_pages, or the pages could be read from SMRAM.

I'll send a v6 patch and a testcase, and in the meanwhile I have applied patch 1.

Paolo

#3 lore raw

Re: [PATCH v5 2/2] kvm: nVMX: Introduce KVM_CAP_NESTED_STATE

In reply to KarimAllah Ahmed

2018-07-10 11:27+0200, KarimAllah Ahmed:

Quoted 75 lines

From: Jim Mattson <[email protected]>

For nested virtualization L0 KVM is managing a bit of state for L2 guests, this state can not be captured through the currently available IOCTLs. In fact the state captured through all of these IOCTLs is usually a mix of L1 and L2 state. It is also dependent on whether the L2 guest was running at the moment when the process was interrupted to save its state.

With this capability, there are two new vcpu ioctls: KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE. These can be used for saving and restoring a VM that is in VMX operation.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc H. Peter Anvin <[email protected]>

[karahmed@ - rename structs and functions and make them ready for AMD and
address previous comments.
- handle nested.smm state.
- rebase & a bit of refactoring.
- Merge 7/8 and 8/8 into one patch. ]

---
v4 -> v5:
- Drop the update to KVM_REQUEST_ARCH_BASE in favor of a patch to switch to u64 instead.
- Fix commit message.
- Handle nested.smm state as well.
- rebase

v3 -> v4:
- Rename function to have _nested

v2 -> v3:
- Remove the forced VMExit from L2 after reading the kvm_state. The actual problem is solved.
- Rebase again!
- Set nested_run_pending during restore (not sure if it makes sense yet or not).
- Reduce KVM_REQUEST_ARCH_BASE to 7 instead of 8 (the other alternative is to switch everything to u64)

v1 -> v2:
- Rename structs and functions and make them ready for AMD and address previous comments.
- Rebase & a bit of refactoring.
- Merge 7/8 and 8/8 into one patch.
- Force a VMExit from L2 after reading the kvm_state to avoid mixed state between L1 and L2 on resurrecting the instance.
---

arch/x86/kvm/vmx.c +17 -0
@@ -12976,6 +12977,197 @@ static int enable_smi_window(struct kvm_vcpu *vcpu)
+static int set_vmcs_cache(struct kvm_vcpu *vcpu,
+			  struct kvm_nested_state __user *user_kvm_nested_state,
+			  struct kvm_nested_state *kvm_state)
+
+{
[...]
+
+	if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
+		vmx->nested.nested_run_pending = 1;
+
+	if (check_vmentry_prereqs(vcpu, vmcs12) ||
+	    check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
+		return -EINVAL;
+
+	ret = enter_vmx_non_root_mode(vcpu);
+	if (ret)
+		return ret;
+

arch/x86/kvm/vmx.c static int enable_smi_window(struct kvm_vcpu *vcpu) · line 13121

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	/*
+	 * The MMU is not initialized to point at the right entities yet and
+	 * "get pages" would need to read data from the guest (i.e. we will
+	 * need to perform gpa to hpa translation). So, This request will
+	 * result in a call to nested_get_vmcs12_pages before the next
+	 * VM-entry.
+	 */
+	kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
+
+	vmx->nested.nested_run_pending = 1;

This is not necessary. We're only copying state and do not add anything that would be lost on a nested VM exit without prior VM entry.

Quoted 1 line

+

Halting the VCPU should probably be done here, just like at the end of nested_vmx_run().

include/uapi/linux/kvm.h struct kvm_ppc_resize_hpt { · line 966

Quoted 9 lines
arch/x86/kvm/vmx.c +3 -0
+	return 0;
+}
+
include/uapi/linux/kvm.h +1 -0
@@ -963,6 +963,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_GET_MSR_FEATURES 153
 #define KVM_CAP_HYPERV_EVENTFD 154
 #define KVM_CAP_HYPERV_TLBFLUSH 155
+#define KVM_CAP_STATE 156

KVM_CAP_NESTED_STATE

(good documentation makes code worse. :])

#4 lore raw

Re: [PATCH v5 2/2] kvm: nVMX: Introduce KVM_CAP_NESTED_STATE

Quoted 4 lines · Radim Krčmář
Quoted 1 line

+ vmx->nested.nested_run_pending = 1;

This is not necessary. We're only copying state and do not add anything that would be lost on a nested VM exit without prior VM entry.

If nested_run_pending is blindly set on restore, then prepare_vmcs02 will do the wrong thing. For example, if there was an injected event in the vmcs12, it will get injected again, even if the vCPU has been in L2 for some time.

The value of nested_run_pending should always come from the saved VMX state (a few lines above).

maintainer
#5 lore raw

Re: [PATCH v5 2/2] kvm: nVMX: Introduce KVM_CAP_NESTED_STATE

Quoted 5 lines · Jim Mattson
Quoted 3 lines · Radim Krčmář
Quoted 1 line

+ vmx->nested.nested_run_pending = 1;

This is not necessary. We're only copying state and do not add anything that would be lost on a nested VM exit without prior VM entry.

Quoted 8 lines

If nested_run_pending is blindly set on restore, then prepare_vmcs02 will do the wrong thing. For example, if there was an injected event in the vmcs12, it will get injected again, even if the vCPU has been in L2 for some time.

The value of nested_run_pending should always come from the saved VMX state (a few lines above).

Yep, and there are a couple other things that need adjustment. Stay tuned...

Paolo

#6 lore raw
In reply to KarimAllah Ahmed

2018-07-10 11:27+0200, KarimAllah Ahmed:

Quoted 7 lines

Switch 'requests' to be explicitly 64-bit and update BUILD_BUG_ON check to use the size of "requests" instead of the hard-coded '32'.

That gives us a bit more room again for arch-specific requests as we already ran out of space for x86 due to the hard-coded check.

The only exception here is ARM32 as it is still 32-bits.

What do you mean?

I think we're just going to slow down kvm_request_pending() on 32 bit architectures.

include/linux/kvm_host.h static inline bool is_error_page(struct page *page) · line 130

Quoted 13 lines

---
v1 -> v2:
- Use FIELD_SIZEOF
---

include/linux/kvm_host.h +0 -0
@@ -130,7 +130,7 @@ static inline bool is_error_page(struct page *page)
 #define KVM_REQUEST_ARCH_BASE     8

Now that the base is easily moveable, we could also lower it to 4 and get few more arch flags.

Bumping requests to 64 bits is probably inevitable and this patch looks good.

In v4, you have proposed the bitmap-array solution that would easily allow more than 64 requests -- was the problem that possible implementations of kvm_request_pending were not as efficient for current amount of requests?

Thanks.

Quoted 6 lines
include/linux/kvm_host.h +1 -1
 #define KVM_ARCH_REQ_FLAGS(nr, flags) ({ \
-	BUILD_BUG_ON((unsigned)(nr) >= 32 - KVM_REQUEST_ARCH_BASE); \
+	BUILD_BUG_ON((unsigned)(nr) >= (FIELD_SIZEOF(struct kvm_vcpu, requests) * 8) - KVM_REQUEST_ARCH_BASE); \
 	(unsigned)(((nr) + KVM_REQUEST_ARCH_BASE) | (flags)); \
 })
 #define KVM_ARCH_REQ(nr)           KVM_ARCH_REQ_FLAGS(nr, 0)