Skip to contentSkip to conversation

KVM: x86: guest interface for SEV live migration

PATCH In review Paolo Bonzini 3 participants 12 messages Apr 29, 2021 – May 4, 2021

Subsystems (5)
Documentation x86 KVM KVM x86 +1 more subsystems
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

v3 cover KVM: x86: guest interface for SEV live migration

This is a reviewed version of the guest interface (hypercall+MSR) for SEV live migration. The differences lie mostly in the API for userspace. In particular:

- the CPUID feature is not exposed in KVM_GET_SUPPORTED_CPUID

- the hypercall must be enabled manually with KVM_ENABLE_CAP

- the MSR has sensible behavior if not filtered

Compared to v2, the KVM-provided behavior of the MSR is different:
it is set to 0 if the guest memory is encrypted, and 1 if it is not. The idea is that the MSR is read-only if KVM_FEATURE_HC_PAGE_ENC_STATUS is not exposed to the guest (it should only be exposed if the guest has encrypted memory), but it also has a sensible value for non-encrypted guests. QEMU could however expose a "0" value for the special "-cpu host,migratable=no" mode if it wanted.

Because of this new behavior, the CPUID bit are split.

Paolo

Ashish Kalra (1):
KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Paolo Bonzini (1):
KVM: x86: add MSR_KVM_MIGRATION_CONTROL

 Documentation/virt/kvm/api.rst        | 14 +++++++
 Documentation/virt/kvm/cpuid.rst      |  9 +++++
 Documentation/virt/kvm/hypercalls.rst | 21 ++++++++++
 Documentation/virt/kvm/msr.rst        | 14 +++++++
 arch/x86/include/asm/kvm-x86-ops.h    |  1 +
 arch/x86/include/asm/kvm_host.h       |  3 ++
 arch/x86/include/uapi/asm/kvm_para.h  |  5 +++
 arch/x86/kvm/cpuid.c                  |  3 +-
 arch/x86/kvm/svm/svm.c                |  1 +
 arch/x86/kvm/vmx/vmx.c                |  7 ++++
 arch/x86/kvm/x86.c                    | 56 +++++++++++++++++++++++++++
 include/uapi/linux/kvm.h              |  1 +
 include/uapi/linux/kvm_para.h         |  1 +
 13 files changed, 135 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH v3 1/2 KVM: x86: add MSR_KVM_MIGRATION_CONTROL

Add a new MSR that can be used to communicate whether the page encryption status bitmap is up to date and therefore whether live migration of an encrypted guest is possible.

The MSR should be processed by userspace if it is going to live migrate the guest; the default implementation does nothing.

---
 Documentation/virt/kvm/cpuid.rst     |  3 +++
 Documentation/virt/kvm/msr.rst       |  9 +++++++++
 arch/x86/include/asm/kvm-x86-ops.h   |  1 +
 arch/x86/include/asm/kvm_host.h      |  1 +
 arch/x86/include/uapi/asm/kvm_para.h |  4 ++++
 arch/x86/kvm/cpuid.c                 |  3 ++-
 arch/x86/kvm/svm/svm.c               |  1 +
 arch/x86/kvm/vmx/vmx.c               |  7 +++++++
 arch/x86/kvm/x86.c                   | 14 ++++++++++++++
 9 files changed, 42 insertions(+), 1 deletion(-)
Documentation/virt/kvm/cpuid.rst +3 -0
index cf62162d4be2..c53d7e2b8ff4 100644
@@ -96,6 +96,9 @@ KVM_FEATURE_MSI_EXT_DEST_ID        15          guest checks this feature bit
                                                before using extended destination
                                                ID bits in MSI address bits 11-5.
 
+KVM_FEATURE_MIGRATION_CONTROL      16          guest checks this feature bit before
+                                               using MSR_KVM_MIGRATION_CONTROL
+
 KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24          host will warn if no guest-side
                                                per-cpu warps are expected in
                                                kvmclock
Documentation/virt/kvm/msr.rst +9 -0
index e37a14c323d2..57fc4090031a 100644
@@ -376,3 +376,12 @@ data:
 	write '1' to bit 0 of the MSR, this causes the host to re-scan its queue
 	and check if there are more notifications pending. The MSR is available
 	if KVM_FEATURE_ASYNC_PF_INT is present in CPUID.
+
+MSR_KVM_MIGRATION_CONTROL:
+        0x4b564d08
+
+data:
+        This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in
+        CPUID.  Bit 0 represents whether live migration of the guest is allowed.
+        When a guest is started, bit 0 will be 1 if the guest has encrypted
+        memory and 0 if the guest does not have encrypted memory.
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index 323641097f63..5b19abc1b86f 100644
@@ -111,6 +111,7 @@ KVM_X86_OP(enable_smi_window)
 KVM_X86_OP_NULL(mem_enc_op)
 KVM_X86_OP_NULL(mem_enc_reg_region)
 KVM_X86_OP_NULL(mem_enc_unreg_region)
+KVM_X86_OP(has_encrypted_memory)
 KVM_X86_OP(get_msr_feature)
 KVM_X86_OP(can_emulate_instruction)
 KVM_X86_OP(apic_init_signal_blocked)
arch/x86/include/asm/kvm_host.h +1 -0
index ad22d4839bcc..5491fc617451 100644
@@ -1364,6 +1364,7 @@ struct kvm_x86_ops {
 	int (*pre_leave_smm)(struct kvm_vcpu *vcpu, const char *smstate);
 	void (*enable_smi_window)(struct kvm_vcpu *vcpu);
 
+	bool (*has_encrypted_memory)(struct kvm *kvm);
 	int (*mem_enc_op)(struct kvm *kvm, void __user *argp);
 	int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
 	int (*mem_enc_unreg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
arch/x86/include/uapi/asm/kvm_para.h +4 -0
index 950afebfba88..21390fccfb90 100644
@@ -33,6 +33,7 @@
 #define KVM_FEATURE_PV_SCHED_YIELD	13
 #define KVM_FEATURE_ASYNC_PF_INT	14
 #define KVM_FEATURE_MSI_EXT_DEST_ID	15
+#define KVM_FEATURE_MIGRATION_CONTROL	16
 
 #define KVM_HINTS_REALTIME      0
 
@@ -54,6 +55,7 @@
 #define MSR_KVM_POLL_CONTROL	0x4b564d05
 #define MSR_KVM_ASYNC_PF_INT	0x4b564d06
 #define MSR_KVM_ASYNC_PF_ACK	0x4b564d07
+#define MSR_KVM_MIGRATION_CONTROL	0x4b564d08
 
 struct kvm_steal_time {
 	__u64 steal;
@@ -90,6 +92,8 @@ struct kvm_clock_pairing {
 /* MSR_KVM_ASYNC_PF_INT */
 #define KVM_ASYNC_PF_VEC_MASK			GENMASK(7, 0)
 
+/* MSR_KVM_MIGRATION_CONTROL */
+#define KVM_MIGRATION_READY		(1 << 0)
 
 /* Operations for KVM_HC_MMU_OP */
 #define KVM_MMU_OP_WRITE_PTE            1
arch/x86/kvm/cpuid.c +2 -1
index e9d644147bf5..f765bf7a529c 100644
@@ -892,7 +892,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 			     (1 << KVM_FEATURE_PV_SEND_IPI) |
 			     (1 << KVM_FEATURE_POLL_CONTROL) |
 			     (1 << KVM_FEATURE_PV_SCHED_YIELD) |
-			     (1 << KVM_FEATURE_ASYNC_PF_INT);
+			     (1 << KVM_FEATURE_ASYNC_PF_INT) |
+			     (1 << KVM_FEATURE_MIGRATION_CONTROL);
 
 		if (sched_info_on())
 			entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
arch/x86/kvm/svm/svm.c +1 -0
index a7271f31df47..e75bfbf3e65a 100644
@@ -4591,6 +4591,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 	.pre_leave_smm = svm_pre_leave_smm,
 	.enable_smi_window = svm_enable_smi_window,
 
+	.has_encrypted_memory = sev_guest,
 	.mem_enc_op = svm_mem_enc_op,
 	.mem_enc_reg_region = svm_register_enc_region,
 	.mem_enc_unreg_region = svm_unregister_enc_region,
arch/x86/kvm/vmx/vmx.c +7 -0
index 10b610fc7bbc..54843fbc12f9 100644
@@ -7670,6 +7670,11 @@ static bool vmx_check_apicv_inhibit_reasons(ulong bit)
 	return supported & BIT(bit);
 }
 
+static bool vmx_has_encrypted_memory(struct kvm *kvm)
+{
+	return false;
+}
+
 static struct kvm_x86_ops vmx_x86_ops __initdata = {
 	.hardware_unsetup = hardware_unsetup,
 
@@ -7800,6 +7805,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
 	.complete_emulated_msr = kvm_complete_insn_gp,
 
 	.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
+
+	.has_encrypted_memory = vmx_has_encrypted_memory,
 };
 
 static __init int hardware_setup(void)
arch/x86/kvm/x86.c +14 -0
index cf3b67679cf0..e9c40be9235c 100644
@@ -3275,6 +3275,14 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		vcpu->arch.msr_kvm_poll_control = data;
 		break;
 
+	case MSR_KVM_MIGRATION_CONTROL:
+		if (!guest_pv_has(vcpu, KVM_FEATURE_MIGRATION_CONTROL))
+			return 1;
+
+		if (data != !static_call(kvm_x86_has_encrypted_memory)(vcpu->kvm))
+			return 1;
+		break;
+
 	case MSR_IA32_MCG_CTL:
 	case MSR_IA32_MCG_STATUS:
 	case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
@@ -3568,6 +3576,12 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 		msr_info->data = 0;
 		break;
+	case MSR_KVM_MIGRATION_CONTROL:
+		if (!guest_pv_has(vcpu, KVM_FEATURE_MIGRATION_CONTROL))
+			return 1;
+
+		msr_info->data = !static_call(kvm_x86_has_encrypted_memory)(vcpu->kvm);
+		break;
 	case MSR_KVM_STEAL_TIME:
 		if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
 			return 1;
Signature · 1 line
2.26.2
author maintainer
#2 lore raw

PATCH v3 2/2 KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

In reply to Paolo Bonzini

From: Ashish Kalra <[email protected]>

This hypercall is used by the SEV guest to notify a change in the page encryption status to the hypervisor. The hypercall should be invoked only when the encryption attribute is changed from encrypted -> decrypted and vice versa. By default all guest pages are considered encrypted.

The hypercall exits to userspace to manage the guest shared regions and integrate with the userspace VMM's migration code.

Message-Id: <93d7f2c2888315adc48905722574d89699edde33.1618498113.git.ashish.kalra@amd.com>

---
 Documentation/virt/kvm/api.rst        | 14 +++++++++
 Documentation/virt/kvm/cpuid.rst      |  6 ++++
 Documentation/virt/kvm/hypercalls.rst | 21 ++++++++++++++
 Documentation/virt/kvm/msr.rst        |  7 ++++-
 arch/x86/include/asm/kvm_host.h       |  2 ++
 arch/x86/include/uapi/asm/kvm_para.h  |  1 +
 arch/x86/kvm/x86.c                    | 42 +++++++++++++++++++++++++++
 include/uapi/linux/kvm.h              |  1 +
 include/uapi/linux/kvm_para.h         |  1 +
 9 files changed, 94 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +14 -0
index 37c520ddb7e8..f8794fed23a4 100644
@@ -6891,3 +6891,17 @@ This capability is always enabled.
 This capability indicates that the KVM virtual PTP service is
 supported in the host. A VMM can check whether the service is
 available to the guest on migration.
+
+8.33 KVM_CAP_EXIT_HYPERCALL
+---------------------------
+
+:Capability: KVM_CAP_EXIT_HYPERCALL
+:Architectures: x86
+:Type: vm
+
+This capability, if enabled, will cause KVM to exit to userspace
+with KVM_EXIT_HYPERCALL exit reason to process some hypercalls.
+Right now, the only such hypercall is KVM_HC_PAGE_ENC_STATUS.
+
+Calling KVM_CHECK_EXTENSION for this capability will return a bitmask
+of hypercalls that will exit to userspace.
Documentation/virt/kvm/cpuid.rst +6 -0
index c53d7e2b8ff4..014ceffc46f2 100644
@@ -99,6 +99,12 @@ KVM_FEATURE_MSI_EXT_DEST_ID        15          guest checks this feature bit
 KVM_FEATURE_MIGRATION_CONTROL      16          guest checks this feature bit before
                                                using MSR_KVM_MIGRATION_CONTROL
 
+KVM_FEATURE_HC_PAGE_ENC_STATUS     17          guest checks this feature bit before
+                                               using the page encryption state
+                                               hypercall to notify the page state
+                                               change, and before modifying bit 0 of
+                                               MSR_KVM_MIGRATION_CONTROL
+
 KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24          host will warn if no guest-side
                                                per-cpu warps are expected in
                                                kvmclock
Documentation/virt/kvm/hypercalls.rst +21 -0
index ed4fddd364ea..117ff3b27d3c 100644
@@ -169,3 +169,24 @@ a0: destination APIC ID
 
 :Usage example: When sending a call-function IPI-many to vCPUs, yield if
 	        any of the IPI target vCPUs was preempted.
+
+
+8. KVM_HC_PAGE_ENC_STATUS
+-------------------------
+:Architecture: x86
+:Status: active
+:Purpose: Notify the encryption status changes in guest page table (SEV guest)
+
+a0: the guest physical address of the start page
+a1: the number of pages
+a2: page encryption status
+
+   Where:
+	* 1: Page is encrypted
+	* 0: Page is decrypted
+
+**Implementation note**: this hypercall is implemented in userspace via
+the KVM_CAP_EXIT_HYPERCALL capability.  Userspace must enable that capability
+before advertising KVM_FEATURE_HC_PAGE_ENC_STATUS in the guest CPUID.  In
+addition, if the guest supports KVM_FEATURE_MIGRATION_CONTROL, userspace
+must also set up an MSR filter to process writes to MSR_KVM_MIGRATION_CONTROL.
Documentation/virt/kvm/msr.rst +6 -1
index 57fc4090031a..cf1b0b2099b0 100644
@@ -383,5 +383,10 @@ MSR_KVM_MIGRATION_CONTROL:
 data:
         This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in
         CPUID.  Bit 0 represents whether live migration of the guest is allowed.
+
         When a guest is started, bit 0 will be 1 if the guest has encrypted
-        memory and 0 if the guest does not have encrypted memory.
+        memory and 0 if the guest does not have encrypted memory.  If the
+        guest is communicating page encryption status to the host using the
+        ``KVM_HC_PAGE_ENC_STATUS`` hypercall, it can set bit 0 in this MSR to
+        allow live migration of the guest.  The MSR is read-only if
+        ``KVM_FEATURE_HC_PAGE_STATUS`` is not advertised to the guest.
arch/x86/include/asm/kvm_host.h +2 -0
index 5491fc617451..9b90a0faeab4 100644
@@ -1065,6 +1065,8 @@ struct kvm_arch {
 	u32 user_space_msr_mask;
 	struct kvm_x86_msr_filter __rcu *msr_filter;
 
+	bool hypercall_exit_enabled;
+
 	/* Guest can access the SGX PROVISIONKEY. */
 	bool sgx_provisioning_allowed;
 
arch/x86/include/uapi/asm/kvm_para.h +1 -0
index 21390fccfb90..8fadae64bc66 100644
@@ -34,6 +34,7 @@
 #define KVM_FEATURE_ASYNC_PF_INT	14
 #define KVM_FEATURE_MSI_EXT_DEST_ID	15
 #define KVM_FEATURE_MIGRATION_CONTROL	16
+#define KVM_FEATURE_HC_PAGE_ENC_STATUS	17
 
 #define KVM_HINTS_REALTIME      0
 
arch/x86/kvm/x86.c +42 -0

72 lines not on this page · raw message

include/uapi/linux/kvm.h +1 -0
index 3fd9a7e9d90c..1fb4fd863324 100644
@@ -1082,6 +1082,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_SGX_ATTRIBUTE 196
 #define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197
 #define KVM_CAP_PTP_KVM 198
+#define KVM_CAP_EXIT_HYPERCALL 199
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
include/uapi/linux/kvm_para.h +1 -0
index 8b86609849b9..847b83b75dc8 100644
@@ -29,6 +29,7 @@
 #define KVM_HC_CLOCK_PAIRING		9
 #define KVM_HC_SEND_IPI		10
 #define KVM_HC_SCHED_YIELD		11
+#define KVM_HC_PAGE_ENC_STATUS		12
 
 /*
  * hypercalls use architecture specific
Signature · 1 line
2.26.2
#3 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 6 lines · Paolo Bonzini
Documentation/virt/kvm/msr.rst +0 -0
index 57fc4090031a..cf1b0b2099b0 100644
@@ -383,5 +383,10 @@ MSR_KVM_MIGRATION_CONTROL:
 data:

Documentation/virt/kvm/msr.rst MSR_KVM_MIGRATION_CONTROL: · line 392

Quoted 10 lines
Documentation/virt/kvm/msr.rst +6 -1
         This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in
         CPUID.  Bit 0 represents whether live migration of the guest is allowed.
+
         When a guest is started, bit 0 will be 1 if the guest has encrypted
-        memory and 0 if the guest does not have encrypted memory.
+        memory and 0 if the guest does not have encrypted memory.  If the
+        guest is communicating page encryption status to the host using the
+        ``KVM_HC_PAGE_ENC_STATUS`` hypercall, it can set bit 0 in this MSR to
+        allow live migration of the guest.  The MSR is read-only if
+        ``KVM_FEATURE_HC_PAGE_STATUS`` is not advertised to the guest.

I still don't get the desire to tie MSR_KVM_MIGRATION_CONTROL to PAGE_ENC_STATUS in any way shape or form. I can understand making it read-only or dropping writes if it's not intercepted by userspace, but making it read-only for non-encrypted guests makes it useful only for encrypted guests, which defeats the purpose of genericizing the MSR.

Quoted 6 lines
arch/x86/kvm/x86.c +0 -0
index e9c40be9235c..0c2524bbaa84 100644
@@ -3279,6 +3279,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!guest_pv_has(vcpu, KVM_FEATURE_MIGRATION_CONTROL))

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 3289

Quoted 10 lines
arch/x86/kvm/x86.c +6 -0
 			return 1;
 
+		/*
+		 * This implementation is only good if userspace has *not*
+		 * enabled KVM_FEATURE_HC_PAGE_ENC_STATUS.  If userspace
+		 * enables KVM_FEATURE_HC_PAGE_ENC_STATUS it must set up an
+		 * MSR filter in order to accept writes that change bit 0.
+		 */
 		if (data != !static_call(kvm_x86_has_encrypted_memory)(vcpu->kvm))
 			return 1;

This behavior doesn't match the documentation.

a. The MSR is not read-only for legacy guests since they can write '0'. b. The MSR is not read-only if KVM_FEATURE_HC_PAGE_STATUS isn't advertised,
a guest with encrypted memory can write '1' regardless of whether userspace has enabled KVM_FEATURE_HC_PAGE_STATUS.
c. The MSR is never fully writable, e.g. a guest with encrypted memory can set
bit 0, but not clear it. This doesn't seem intentional?

Why not simply drop writes? E.g.

		if (data & ~KVM_MIGRATION_READY)
			return 1;
		break;

And then do "msr->data = 0;" in the read path. That's just as effective as making the MSR read-only to force userspace to intercept the MSR if it wants to do anything useful with the information, and it's easy to document.

Quoted 1 line
arch/x86/kvm/x86.c +0 -0
 		break;
author maintainer
#4 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 15 lines · Sean Christopherson
Quoted 14 lines · Paolo Bonzini
Documentation/virt/kvm/msr.rst +4 -1
index 57fc4090031a..cf1b0b2099b0 100644
@@ -383,5 +383,10 @@ MSR_KVM_MIGRATION_CONTROL:
  data:
          This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in
          CPUID.  Bit 0 represents whether live migration of the guest is allowed.
+
          When a guest is started, bit 0 will be 1 if the guest has encrypted
-        memory and 0 if the guest does not have encrypted memory.
+        memory and 0 if the guest does not have encrypted memory.  If the
+        guest is communicating page encryption status to the host using the
+        ``KVM_HC_PAGE_ENC_STATUS`` hypercall, it can set bit 0 in this MSR to
Quoted 8 lines

Documentation/virt/kvm/msr.rst MSR_KVM_MIGRATION_CONTROL: · line 392

Quoted 2 lines
Documentation/virt/kvm/msr.rst +2 -0
+        allow live migration of the guest.  The MSR is read-only if
+        ``KVM_FEATURE_HC_PAGE_STATUS`` is not advertised to the guest.

I still don't get the desire to tie MSR_KVM_MIGRATION_CONTROL to PAGE_ENC_STATUS in any way shape or form. I can understand making it read-only or dropping writes if it's not intercepted by userspace, but making it read-only for non-encrypted guests makes it useful only for encrypted guests, which defeats the purpose of genericizing the MSR.

Yeah, I see your point. On the other hand by making it unconditionally writable we must implement the writability in KVM, because a read-only implementation would not comply with the spec.

Quoted 19 lines
Quoted 6 lines
arch/x86/kvm/x86.c +0 -0
index e9c40be9235c..0c2524bbaa84 100644
@@ -3279,6 +3279,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  		if (!guest_pv_has(vcpu, KVM_FEATURE_MIGRATION_CONTROL))

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 3289

Quoted 10 lines
arch/x86/kvm/x86.c +6 -0
  			return 1;
 
+		/*
+		 * This implementation is only good if userspace has *not*
+		 * enabled KVM_FEATURE_HC_PAGE_ENC_STATUS.  If userspace
+		 * enables KVM_FEATURE_HC_PAGE_ENC_STATUS it must set up an
+		 * MSR filter in order to accept writes that change bit 0.
+		 */
  		if (data != !static_call(kvm_x86_has_encrypted_memory)(vcpu->kvm))
  			return 1;

This behavior doesn't match the documentation.

Quoted 4 lines

a. The MSR is not read-only for legacy guests since they can write '0'. b. The MSR is not read-only if KVM_FEATURE_HC_PAGE_STATUS isn't advertised,
a guest with encrypted memory can write '1' regardless of whether userspace has enabled KVM_FEATURE_HC_PAGE_STATUS.

Right, I should have said "not changeable" rather than "read-only".

Quoted 2 lines

c. The MSR is never fully writable, e.g. a guest with encrypted memory can set
bit 0, but not clear it. This doesn't seem intentional?

It is intentional, clearing it would mean preserving the value in the kernel so that userspace can read it.

So... I don't know, all in all having both the separate CPUID and the userspace implementation reeks of overengineering. It should be either of these:

- separate CPUID bit, MSR unconditionally writable and implemented in KVM. Userspace is expected to ignore the MSR value for encrypted guests unless KVM_FEATURE_HC_PAGE_STATUS is exposed. Userspace should respect it even for unencrypted guests (not a migration-DoS vector, because userspace can just not expose the feature).

- make it completely independent from migration, i.e. it's just a facet of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It would use CPUID bit as the encryption status bitmap and have no code at all in KVM (userspace needs to set up the filter and implement everything).

At this point I very much prefer the latter, which is basically Ashish's earlier patch.

Paolo

Quoted 9 lines

Why not simply drop writes? E.g.

		if (data & ~KVM_MIGRATION_READY)
			return 1;
		break;

And then do "msr->data = 0;" in the read path. That's just as effective as making the MSR read-only to force userspace to intercept the MSR if it wants to do anything useful with the information, and it's easy to document.

#5 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 72 lines · Paolo Bonzini
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · Paolo Bonzini
Documentation/virt/kvm/msr.rst +4 -1
index 57fc4090031a..cf1b0b2099b0 100644
@@ -383,5 +383,10 @@ MSR_KVM_MIGRATION_CONTROL:
  data:
          This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in
          CPUID.  Bit 0 represents whether live migration of the guest is allowed.
+
          When a guest is started, bit 0 will be 1 if the guest has encrypted
-        memory and 0 if the guest does not have encrypted memory.
+        memory and 0 if the guest does not have encrypted memory.  If the
+        guest is communicating page encryption status to the host using the
+        ``KVM_HC_PAGE_ENC_STATUS`` hypercall, it can set bit 0 in this MSR to
Quoted 8 lines

Documentation/virt/kvm/msr.rst MSR_KVM_MIGRATION_CONTROL: · line 392

Quoted 2 lines
Documentation/virt/kvm/msr.rst +2 -0
+        allow live migration of the guest.  The MSR is read-only if
+        ``KVM_FEATURE_HC_PAGE_STATUS`` is not advertised to the guest.

I still don't get the desire to tie MSR_KVM_MIGRATION_CONTROL to PAGE_ENC_STATUS in any way shape or form. I can understand making it read-only or dropping writes if it's not intercepted by userspace, but making it read-only for non-encrypted guests makes it useful only for encrypted guests, which defeats the purpose of genericizing the MSR.

Yeah, I see your point. On the other hand by making it unconditionally writable we must implement the writability in KVM, because a read-only implementation would not comply with the spec.

Quoted 19 lines
Quoted 6 lines
arch/x86/kvm/x86.c +0 -0
index e9c40be9235c..0c2524bbaa84 100644
@@ -3279,6 +3279,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
             if (!guest_pv_has(vcpu, KVM_FEATURE_MIGRATION_CONTROL))

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 3289

Quoted 10 lines
arch/x86/kvm/x86.c +6 -0
                     return 1;
 
+            /*
+             * This implementation is only good if userspace has *not*
+             * enabled KVM_FEATURE_HC_PAGE_ENC_STATUS.  If userspace
+             * enables KVM_FEATURE_HC_PAGE_ENC_STATUS it must set up an
+             * MSR filter in order to accept writes that change bit 0.
+             */
             if (data != !static_call(kvm_x86_has_encrypted_memory)(vcpu->kvm))
                     return 1;

This behavior doesn't match the documentation.

Quoted 4 lines

a. The MSR is not read-only for legacy guests since they can write '0'. b. The MSR is not read-only if KVM_FEATURE_HC_PAGE_STATUS isn't advertised,
a guest with encrypted memory can write '1' regardless of whether userspace has enabled KVM_FEATURE_HC_PAGE_STATUS.

Right, I should have said "not changeable" rather than "read-only".

Quoted 2 lines

c. The MSR is never fully writable, e.g. a guest with encrypted memory can set
bit 0, but not clear it. This doesn't seem intentional?

It is intentional, clearing it would mean preserving the value in the kernel so that userspace can read it.

So... I don't know, all in all having both the separate CPUID and the userspace implementation reeks of overengineering. It should be either of these:

- separate CPUID bit, MSR unconditionally writable and implemented in KVM. Userspace is expected to ignore the MSR value for encrypted guests unless KVM_FEATURE_HC_PAGE_STATUS is exposed. Userspace should respect it even for unencrypted guests (not a migration-DoS vector, because userspace can just not expose the feature).

Quoted 4 lines

- make it completely independent from migration, i.e. it's just a facet of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It would use CPUID bit as the encryption status bitmap and have no code at all in KVM (userspace needs to set up the filter and implement everything).

As far as I know, because of MSR filtering, the only "code" that needs to be in KVM for MSR handling is a #define reserving the PV feature number and a #define for the MSR number.

Arguably, you don't even need to add the new PV bits to the supported cpuid, since MSR filtering is really what determines if kernel support is present.

Quoted 3 lines

At this point I very much prefer the latter, which is basically Ashish's earlier patch.

The minor distinction would be that if you expose the cpuid bit to the guest you plan on intercepting the MSR with filters, and would not need any handler code in the kernel.

Steve

Quoted 2 lines

Paolo

author maintainer
#6 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 7 lines · Steve Rutherford

As far as I know, because of MSR filtering, the only "code" that needs to be in KVM for MSR handling is a #define reserving the PV feature number and a #define for the MSR number.

Arguably, you don't even need to add the new PV bits to the supported cpuid, since MSR filtering is really what determines if kernel support is present.

Not only I don't need to do that, I must not. :)

Quoted 6 lines
Quoted 2 lines

At this point I very much prefer the latter, which is basically Ashish's earlier patch.

The minor distinction would be that if you expose the cpuid bit to the guest you plan on intercepting the MSR with filters, and would not need any handler code in the kernel.

Yep, and it's not a minor distinction after all (especially from the PoV of the guy who actually ends up maintaining the code, i.e. me), so that's what I'm going for.

Paolo

#7 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

In reply to Paolo Bonzini
Quoted 4 lines · Paolo Bonzini

- make it completely independent from migration, i.e. it's just a facet of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It would use CPUID bit as the encryption status bitmap and have no code at all in KVM (userspace needs to set up the filter and implement everything).

If the bit is purely a "page encryption status is up-to-date", what about
overloading KVM_HC_PAGE_ENC_STATUS to handle that status update as well? That
would eliminate my biggest complaint about having what is effectively a single paravirt feature split into two separate, but intertwined chunks of ABI.

#define KVM_HC_PAGE_ENC_UPDATE 12

#define KVM_HC_PAGE_ENC_REGION_UPDATE 0 /* encrypted vs. plain text */
#define KVM_HC_PAGE_ENC_STATUS_UPDATE 1 /* up-to-date vs. stale */

		ret = -KVM_ENOSYS;
		if (!vcpu->kvm->arch.hypercall_exit_enabled)
		        break;

		ret = -EINVAL;
		if (a0 == KVM_HC_PAGE_ENC_REGION_UPDATE) {
			u64 gpa = a1, npages = a2;

			if (!PAGE_ALIGNED(gpa) || !npages ||
			    gpa_to_gfn(gpa) + npages <= gpa_to_gfn(gpa))
				break;
		} else if (a0 != KVM_HC_PAGE_ENC_STATUS_UPDATE) {
			break;
		}

		vcpu->run->exit_reason        = KVM_EXIT_HYPERCALL;
		vcpu->run->hypercall.nr       = KVM_HC_PAGE_ENC_STATUS;
		vcpu->run->hypercall.args[0]  = a0;
		vcpu->run->hypercall.args[1]  = a1;
		vcpu->run->hypercall.args[2]  = a2;
		vcpu->run->hypercall.args[3]  = a3;
		vcpu->run->hypercall.longmode = op_64_bit;
		vcpu->arch.complete_userspace_io = complete_hypercall_exit;
		return 0;
author maintainer
#8 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 10 lines · Sean Christopherson
Quoted 4 lines · Paolo Bonzini

- make it completely independent from migration, i.e. it's just a facet of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It would use CPUID bit as the encryption status bitmap and have no code at all in KVM (userspace needs to set up the filter and implement everything).

If the bit is purely a "page encryption status is up-to-date", what about
overloading KVM_HC_PAGE_ENC_STATUS to handle that status update as well? That
would eliminate my biggest complaint about having what is effectively a single paravirt feature split into two separate, but intertwined chunks of ABI.

It's true that they are intertwined, but I dislike not having a way to read the current state.

Paolo

Quoted 31 lines

#define KVM_HC_PAGE_ENC_UPDATE 12

#define KVM_HC_PAGE_ENC_REGION_UPDATE 0 /* encrypted vs. plain text */
#define KVM_HC_PAGE_ENC_STATUS_UPDATE 1 /* up-to-date vs. stale */

		ret = -KVM_ENOSYS;
		if (!vcpu->kvm->arch.hypercall_exit_enabled)
		        break;

		ret = -EINVAL;
		if (a0 == KVM_HC_PAGE_ENC_REGION_UPDATE) {
			u64 gpa = a1, npages = a2;

			if (!PAGE_ALIGNED(gpa) || !npages ||
			    gpa_to_gfn(gpa) + npages <= gpa_to_gfn(gpa))
				break;
		} else if (a0 != KVM_HC_PAGE_ENC_STATUS_UPDATE) {
			break;
		}

		vcpu->run->exit_reason        = KVM_EXIT_HYPERCALL;
		vcpu->run->hypercall.nr       = KVM_HC_PAGE_ENC_STATUS;
		vcpu->run->hypercall.args[0]  = a0;
		vcpu->run->hypercall.args[1]  = a1;
		vcpu->run->hypercall.args[2]  = a2;
		vcpu->run->hypercall.args[3]  = a3;
		vcpu->run->hypercall.longmode = op_64_bit;
		vcpu->arch.complete_userspace_io = complete_hypercall_exit;
		return 0;
#9 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Quoted 4 lines · Paolo Bonzini

- make it completely independent from migration, i.e. it's just a facet of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It would use CPUID bit as the encryption status bitmap and have no code at all in KVM (userspace needs to set up the filter and implement everything).

Quoted 7 lines
Quoted 4 lines

If the bit is purely a "page encryption status is up-to-date", what about
overloading KVM_HC_PAGE_ENC_STATUS to handle that status update as well? That
would eliminate my biggest complaint about having what is effectively a single paravirt feature split into two separate, but intertwined chunks of ABI.

It's true that they are intertwined, but I dislike not having a way to read the current state.

From the guest?

author maintainer
#10 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 13 lines · Sean Christopherson
Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Sean Christopherson
Quoted 4 lines · Paolo Bonzini

- make it completely independent from migration, i.e. it's just a facet of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It would use CPUID bit as the encryption status bitmap and have no code at all in KVM (userspace needs to set up the filter and implement everything).

If the bit is purely a "page encryption status is up-to-date", what about
overloading KVM_HC_PAGE_ENC_STATUS to handle that status update as well? That
would eliminate my biggest complaint about having what is effectively a single paravirt feature split into two separate, but intertwined chunks of ABI.

Quoted 4 lines
Quoted 2 lines

It's true that they are intertwined, but I dislike not having a way to read the current state.

From the guest?

Yes, host userspace obviously doesn't need one since it's implemented through an MSR filter. It may not be really necessary to read it, but it's a bit jarring compared to how the rest of the PV APIs uses MSRs.

Also from a debugging/crashdump point of view the VMM may have an established way to read an MSR from a vCPU, but it won't work if you come up with a new way to set the state.

Paolo

#11 lore raw

Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 20 lines · Paolo Bonzini
Quoted 13 lines · Sean Christopherson
Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Sean Christopherson
Quoted 4 lines · Paolo Bonzini

- make it completely independent from migration, i.e. it's just a facet of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It would use CPUID bit as the encryption status bitmap and have no code at all in KVM (userspace needs to set up the filter and implement everything).

If the bit is purely a "page encryption status is up-to-date", what about
overloading KVM_HC_PAGE_ENC_STATUS to handle that status update as well? That
would eliminate my biggest complaint about having what is effectively a single paravirt feature split into two separate, but intertwined chunks of ABI.

Quoted 4 lines
Quoted 2 lines

It's true that they are intertwined, but I dislike not having a way to read the current state.

From the guest?

Quoted 7 lines

Yes, host userspace obviously doesn't need one since it's implemented through an MSR filter. It may not be really necessary to read it, but it's a bit jarring compared to how the rest of the PV APIs uses MSRs.

Also from a debugging/crashdump point of view the VMM may have an established way to read an MSR from a vCPU, but it won't work if you come up with a new way to set the state.

Agreed on the preference for an MSR. I particularly appreciate that it reduces the kernel footprint for these changes.

Steve