Skip to contentSkip to conversation

Add AMD SEV guest live migration support

RFC PATCH Superseded Singh, Brijesh 4 participants 28 messages Apr 24, 2019 – May 3, 2019

Subsystems (6)
Documentation x86 KVM KVM x86 +2 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Add AMD SEV guest live migration support

The series add support for AMD SEV guest live migration commands. To protect the confidentiality of an SEV protected guest memory while in transit we need to use the SEV commands defined in SEV API spec [1].

SEV guest VMs have the concept of private and shared memory. Private memory is encrypted with the guest-specific key, while shared memory may be encrypted with hypervisor key. The commands provided by the SEV FW are meant to be used for the private memory only. The patch series introduces a new hypercall. The guest OS can use this hypercall to notify the page encryption status. If the page is encrypted with guest specific-key then we use SEV command during the migration. If page is not encrypted then fallback to default.

The patch adds a new ioctl KVM_GET_PAGE_ENC_BITMAP. The ioctl can be used by the qemu to get the page encrypted bitmap. Qemu can consult this bitmap during the migration to know whether the page is encrypted.

[1] https://developer.amd.com/wp-content/resources/55766.PDF

The series is tested with the Qemu, I am in process of cleaning up the Qemu code and will submit soon.

While implementing the migration I stumbled on the follow question:

- Since there is a guest OS changes required to support the migration, so how do we know whether guest OS is updated? Should we extend KVM capabilities/feature bits to check this?

TODO:
- add an ioctl to build encryption bitmap. The encryption bitmap is built during the guest bootup/execution. We should provide an ioctl so that destination can build the bitmap as it receives the pages.
- reset the bitmap on guest reboot.

The complete tree with patch is available at:
https://github.com/codomania/kvm/tree/sev-migration-rfc-v1

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>

Brijesh Singh (10):
KVM: SVM: Add KVM_SEV SEND_START command
KVM: SVM: Add KVM_SEND_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_SEND_FINISH command
KVM: SVM: Add support for KVM_SEV_RECEIVE_START command
KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_RECEIVE_FINISH command
KVM: x86: Add AMD SEV specific Hypercall3
KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall
KVM: x86: Introduce KVM_GET_PAGE_ENC_BITMAP ioctl
mm: x86: Invoke hypercall when page encryption status is changed

 .../virtual/kvm/amd-memory-encryption.rst     | 116 ++++
 Documentation/virtual/kvm/hypercalls.txt      |  14 +
 arch/x86/include/asm/kvm_host.h               |   3 +
 arch/x86/include/asm/kvm_para.h               |  12 +
 arch/x86/include/asm/mem_encrypt.h            |   3 +
 arch/x86/kvm/svm.c                            | 560 +++++++++++++++++-
 arch/x86/kvm/vmx/vmx.c                        |   1 +
 arch/x86/kvm/x86.c                            |  17 +
 arch/x86/mm/mem_encrypt.c                     |  45 +-
 arch/x86/mm/pageattr.c                        |  15 +
 include/uapi/linux/kvm.h                      |  51 ++
 include/uapi/linux/kvm_para.h                 |   1 +
 12 files changed, 834 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH 1/10 KVM: SVM: Add KVM_SEV SEND_START command

The command is used to create an outgoing SEV guest encryption context.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     |  24 +++++
 arch/x86/kvm/svm.c                            | 101 ++++++++++++++++++
 include/uapi/linux/kvm.h                      |  12 +++
 3 files changed, 137 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +24 -0
index 659bbc093b52..340ac4f87321 100644
@@ -238,6 +238,30 @@ Returns: 0 on success, -negative on error
                 __u32 trans_len;
         };
 
+10. KVM_SEV_SEND_START
+----------------------
+
+The KVM_SEV_SEND_START command can be used by the hypervisor to create an
+outgoing guest encryption context.
+
+Parameters (in): struct kvm_sev_send_start
+
+Returns: 0 on success, -negative on error
+
+::
+        struct kvm_sev_send_start {
+                __u32 policy;                 /* guest policy */
+
+                __u64 pdh_cert_uaddr;         /* platform Diffie-Hellman certificate */
+                __u32 pdh_cert_len;
+
+                __u64 plat_cert_uaddr;        /* platform certificate chain */
+                __u32 plat_cert_len;
+
+                __u64 amd_cert_uaddr;         /* AMD certificate */
+                __u32 amd_cert_len;
+        };
+
 References
 ==========
 
arch/x86/kvm/svm.c +101 -0

113 lines not on this page · raw message

include/uapi/linux/kvm.h +12 -0
index 6d4ea4b6c922..f425418bec13 100644
@@ -1520,6 +1520,18 @@ struct kvm_sev_dbg {
 	__u32 len;
 };
 
+struct kvm_sev_send_start {
+	__u32 policy;
+	__u64 pdh_cert_uaddr;
+	__u32 pdh_cert_len;
+	__u64 plat_cert_uaddr;
+	__u32 plat_cert_len;
+	__u64 amd_cert_uaddr;
+	__u32 amd_cert_len;
+	__u64 session_uaddr;
+	__u32 session_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [RFC PATCH v1 01/10] KVM: SVM: Add KVM_SEV SEND_START command

Quoted 40 lines · Brijesh

The command is used to create an outgoing SEV guest encryption context.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     |  24 +++++
 arch/x86/kvm/svm.c                            | 101 ++++++++++++++++++
 include/uapi/linux/kvm.h                      |  12 +++
 3 files changed, 137 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +12 -0
index 659bbc093b52..340ac4f87321 100644
@@ -238,6 +238,30 @@ Returns: 0 on success, -negative on error
                 __u32 trans_len;
         };
 
+10. KVM_SEV_SEND_START
+----------------------
+
+The KVM_SEV_SEND_START command can be used by the hypervisor to create an
+outgoing guest encryption context.
+
+Parameters (in): struct kvm_sev_send_start
+
+Returns: 0 on success, -negative on error
+
+::
+        struct kvm_sev_send_start {

Documentation/virtual/kvm/amd-memory-encryption.rst Returns: 0 on success, -negative on error · line 262

Quoted 10 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +10 -0
+                __u32 policy;                 /* guest policy */
+
+                __u64 pdh_cert_uaddr;         /* platform Diffie-Hellman certificate */
+                __u32 pdh_cert_len;
+
+                __u64 plat_cert_uaddr;        /* platform certificate chain */
+                __u32 plat_cert_len;
+
+                __u64 amd_cert_uaddr;         /* AMD certificate */
+                __u32 amd_cert_len;
        __u64 session_uaddr;
        __u32 session_len;

too, right?

Quoted 61 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +2 -0
+        };
+
 References
 ==========
 
arch/x86/kvm/svm.c +48 -0
index 406b558abfef..4c2a225ba546 100644
@@ -6955,6 +6955,104 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	void *amd_cert = NULL, *session_data = NULL;
+	void *pdh_cert = NULL, *plat_cert = NULL;
+	struct sev_data_send_start *data = NULL;
+	struct kvm_sev_send_start params;
+	int ret;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+				sizeof(struct kvm_sev_send_start)))
+		return -EFAULT;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	/* userspace wants to query the session length */
+	if (!params.session_len)
+		goto cmd;
+
+	if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
+	    !params.session_uaddr)
+		return -EINVAL;
+
+	/* copy the certificate blobs from userspace */
+	pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr, params.pdh_cert_len);
+	if (IS_ERR(pdh_cert)) {
+		ret = PTR_ERR(pdh_cert);
+		goto e_free;
+	}
+
+	data->pdh_cert_address = __psp_pa(pdh_cert);
+	data->pdh_cert_len = params.pdh_cert_len;
+
+	plat_cert = psp_copy_user_blob(params.plat_cert_uaddr, params.plat_cert_len);
+	if (IS_ERR(plat_cert)) {
+		ret = PTR_ERR(plat_cert);
+		goto e_free_pdh;
+	}
+
+	data->plat_cert_address = __psp_pa(plat_cert);
+	data->plat_cert_len = params.plat_cert_len;
+
+	amd_cert = psp_copy_user_blob(params.amd_cert_uaddr, params.amd_cert_len);

arch/x86/kvm/svm.c static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7015

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	if (IS_ERR(amd_cert)) {
+		ret = PTR_ERR(amd_cert);
+		goto e_free_plat_cert;
+	}
+
+	data->amd_cert_address = __psp_pa(amd_cert);
+	data->amd_cert_len = params.amd_cert_len;
+
+	ret = -ENOMEM;
+	session_data = kmalloc(params.session_len, GFP_KERNEL);

If the user is supposed to query the session length first, you could save it in a global variable perhaps and use that value instead of trusting the user to give you the correct one in params.session_len for the allocation...

arch/x86/kvm/svm.c static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7025

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	if (!session_data)
+		goto e_free_amd_cert;
+
+	data->session_address = __psp_pa(session_data);
+	data->session_len = params.session_len;
+cmd:
+	data->handle = sev->handle;
+	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, data, &argp->error);
+
+	/* if we queried the session length, FW responded with expected data */

<--- ... here you have the session length from the fw.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#3 lore raw

Re: [RFC PATCH v1 01/10] KVM: SVM: Add KVM_SEV SEND_START command

Quoted 55 lines · Borislav Petkov
Quoted 40 lines · Brijesh

The command is used to create an outgoing SEV guest encryption context.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
  .../virtual/kvm/amd-memory-encryption.rst     |  24 +++++
  arch/x86/kvm/svm.c                            | 101 ++++++++++++++++++
  include/uapi/linux/kvm.h                      |  12 +++
  3 files changed, 137 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +12 -0
index 659bbc093b52..340ac4f87321 100644
@@ -238,6 +238,30 @@ Returns: 0 on success, -negative on error
                  __u32 trans_len;
          };
 
+10. KVM_SEV_SEND_START
+----------------------
+
+The KVM_SEV_SEND_START command can be used by the hypervisor to create an
+outgoing guest encryption context.
+
+Parameters (in): struct kvm_sev_send_start
+
+Returns: 0 on success, -negative on error
+
+::
+        struct kvm_sev_send_start {

Documentation/virtual/kvm/amd-memory-encryption.rst Returns: 0 on success, -negative on error · line 262

Quoted 10 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +10 -0
+                __u32 policy;                 /* guest policy */
+
+                __u64 pdh_cert_uaddr;         /* platform Diffie-Hellman certificate */
+                __u32 pdh_cert_len;
+
+                __u64 plat_cert_uaddr;        /* platform certificate chain */
+                __u32 plat_cert_len;
+
+                __u64 amd_cert_uaddr;         /* AMD certificate */
+                __u32 amd_cert_len;
         __u64 session_uaddr;
         __u32 session_len;
Quoted 1 line

too, right?

Ah good catch, I will fix in next rev. thanks

Quoted 69 lines
Quoted 69 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +2 -0
+        };
+
  References
  ==========
 
arch/x86/kvm/svm.c +55 -0
index 406b558abfef..4c2a225ba546 100644
@@ -6955,6 +6955,104 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
  	return ret;
  }
 
+static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	void *amd_cert = NULL, *session_data = NULL;
+	void *pdh_cert = NULL, *plat_cert = NULL;
+	struct sev_data_send_start *data = NULL;
+	struct kvm_sev_send_start params;
+	int ret;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+				sizeof(struct kvm_sev_send_start)))
+		return -EFAULT;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	/* userspace wants to query the session length */
+	if (!params.session_len)
+		goto cmd;
+
+	if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
+	    !params.session_uaddr)
+		return -EINVAL;
+
+	/* copy the certificate blobs from userspace */
+	pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr, params.pdh_cert_len);
+	if (IS_ERR(pdh_cert)) {
+		ret = PTR_ERR(pdh_cert);
+		goto e_free;
+	}
+
+	data->pdh_cert_address = __psp_pa(pdh_cert);
+	data->pdh_cert_len = params.pdh_cert_len;
+
+	plat_cert = psp_copy_user_blob(params.plat_cert_uaddr, params.plat_cert_len);
+	if (IS_ERR(plat_cert)) {
+		ret = PTR_ERR(plat_cert);
+		goto e_free_pdh;
+	}
+
+	data->plat_cert_address = __psp_pa(plat_cert);
+	data->plat_cert_len = params.plat_cert_len;
+
+	amd_cert = psp_copy_user_blob(params.amd_cert_uaddr, params.amd_cert_len);
+	if (IS_ERR(amd_cert)) {
+		ret = PTR_ERR(amd_cert);
+		goto e_free_plat_cert;
+	}
+
+	data->amd_cert_address = __psp_pa(amd_cert);
+	data->amd_cert_len = params.amd_cert_len;
Quoted 9 lines

arch/x86/kvm/svm.c static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7015

Quoted 3 lines
arch/x86/kvm/svm.c +3 -0
+
+	ret = -ENOMEM;
+	session_data = kmalloc(params.session_len, GFP_KERNEL);

If the user is supposed to query the session length first, you could save it in a global variable perhaps and use that value instead of trusting the user to give you the correct one in params.session_len for the allocation...

Yes that's doable but I am afraid that caching the value may lead us to wrong path and also divergence from the SEV API spec. The spec says the returned length is a minimum length but its possible that caller can give a bigger buffer and FW will still work with it.

Quoted 13 lines

arch/x86/kvm/svm.c static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7025

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	if (!session_data)
+		goto e_free_amd_cert;
+
+	data->session_address = __psp_pa(session_data);
+	data->session_len = params.session_len;
+cmd:
+	data->handle = sev->handle;
+	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, data, &argp->error);
+
+	/* if we queried the session length, FW responded with expected data */

<--- ... here you have the session length from the fw.

maintainer
#4 lore raw

Re: [RFC PATCH v1 01/10] KVM: SVM: Add KVM_SEV SEND_START command

Quoted 4 lines · Brijesh

Yes that's doable but I am afraid that caching the value may lead us to wrong path and also divergence from the SEV API spec. The spec says the returned length is a minimum length but its possible that caller can give a bigger buffer and FW will still work with it.

Does the caller even have a valid reason to give a bigger buffer len?

I mean I'm still thinking defensively here but maybe the only thing that would happen here with a bigger buffer is if the kmalloc() would fail, leading to eventual failure of the migration.

If the code limits the allocation to some sane max length, the migration won't fail even if userspace gives it too big values...

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#5 lore raw

Re: [RFC PATCH v1 01/10] KVM: SVM: Add KVM_SEV SEND_START command

Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Brijesh

Yes that's doable but I am afraid that caching the value may lead us to wrong path and also divergence from the SEV API spec. The spec says the returned length is a minimum length but its possible that caller can give a bigger buffer and FW will still work with it.

Does the caller even have a valid reason to give a bigger buffer len?

Practically I don't see any reason why caller would do that but theoretically it can. If we cache the len then we also need to consider adding another flag to hint whether userspace ever requested length. e.g an application can compute the length of session blob by looking at the API version and spec and may never query the length.

Quoted 7 lines

I mean I'm still thinking defensively here but maybe the only thing that would happen here with a bigger buffer is if the kmalloc() would fail, leading to eventual failure of the migration.

If the code limits the allocation to some sane max length, the migration won't fail even if userspace gives it too big values...

maintainer
#6 lore raw

Re: [RFC PATCH v1 01/10] KVM: SVM: Add KVM_SEV SEND_START command

Quoted 12 lines · Brijesh

Practically I don't see any reason why caller would do that but theoretically it can. If we cache the len then we also need to consider adding another flag to hint whether userspace ever requested length. e.g an application can compute the length of session blob by looking at the API version and spec and may never query the length.

Quoted 6 lines

I mean I'm still thinking defensively here but maybe the only thing that would happen here with a bigger buffer is if the kmalloc() would fail, leading to eventual failure of the migration.

If the code limits the allocation to some sane max length, the migration won't fail even if userspace gives it too big values...

So what about this? Limiting to a sane length...

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#7 lore raw

Re: [RFC PATCH v1 01/10] KVM: SVM: Add KVM_SEV SEND_START command

Quoted 1 line · Borislav Petkov

So what about this? Limiting to a sane length...

Sure, we have defined a SEV_FW_BLOB_MAX_SIZE and can use it to limit the blob copy size. I will do this in next rev. thanks

-Brijesh

#8 lore raw

RFC PATCH 2/10 KVM: SVM: Add KVM_SEND_UPDATE_DATA command

In reply to Singh, Brijesh

The command is used for encrypting the guest memory region using the encryption context created with KVM_SEV_SEND_START.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     |  23 ++++
 arch/x86/kvm/svm.c                            | 128 +++++++++++++++++-
 include/uapi/linux/kvm.h                      |   9 ++
 3 files changed, 157 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/amd-memory-encryption.rst +23 -0
index 340ac4f87321..a0208e171489 100644
@@ -262,6 +262,29 @@ Returns: 0 on success, -negative on error
                 __u32 amd_cert_len;
         };
 
+11. KVM_SEV_SEND_UPDATE_DATA
+----------------------------
+
+The KVM_SEV_SEND_UPDATE_DATA command can be used by the hypervisor to encrypt the
+outgoing guest memory region with encryption context creating using KVM_SEV_SEND_START.
+
+Parameters (in): struct kvm_sev_send_update_data
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_launch_send_update_data {
+                __u64 hdr_uaddr;        /* userspace address containing the packet header */
+                __u32 hdr_len;
+
+                __u64 guest_uaddr;      /* the source memory region to be encrypted */
+                __u32 guest_len;
+
+                __u64 trans_uaddr;      /* the destition memory region  */
+                __u32 trans_len;
+        };
+
 References
 ==========
 
arch/x86/kvm/svm.c +125 -3

158 lines not on this page · raw message

include/uapi/linux/kvm.h +9 -0
index f425418bec13..0bee91bba329 100644
@@ -1532,6 +1532,15 @@ struct kvm_sev_send_start {
 	__u32 session_len;
 };
 
+struct kvm_sev_send_update_data {
+	__u64 hdr_uaddr;
+	__u32 hdr_len;
+	__u64 guest_uaddr;
+	__u32 guest_len;
+	__u64 trans_uaddr;
+	__u32 trans_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.17.1
#9 lore raw

Re: [RFC PATCH v1 02/10] KVM: SVM: Add KVM_SEND_UPDATE_DATA command

Quoted 26 lines · Brijesh

The command is used for encrypting the guest memory region using the encryption context created with KVM_SEV_SEND_START.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     |  23 ++++
 arch/x86/kvm/svm.c                            | 128 +++++++++++++++++-
 include/uapi/linux/kvm.h                      |   9 ++
 3 files changed, 157 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/amd-memory-encryption.rst +0 -0
index 340ac4f87321..a0208e171489 100644

Documentation/virtual/kvm/amd-memory-encryption.rst Returns: 0 on success, -negative on error · line 269

Quoted 8 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +5 -0
@@ -262,6 +262,29 @@ Returns: 0 on success, -negative on error
                 __u32 amd_cert_len;
         };
 
+11. KVM_SEV_SEND_UPDATE_DATA
+----------------------------
+
+The KVM_SEV_SEND_UPDATE_DATA command can be used by the hypervisor to encrypt the
+outgoing guest memory region with encryption context creating using KVM_SEV_SEND_START.

with the encryption context created using...

Quoted 26 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +18 -0
+
+Parameters (in): struct kvm_sev_send_update_data
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_launch_send_update_data {
+                __u64 hdr_uaddr;        /* userspace address containing the packet header */
+                __u32 hdr_len;
+
+                __u64 guest_uaddr;      /* the source memory region to be encrypted */
+                __u32 guest_len;
+
+                __u64 trans_uaddr;      /* the destition memory region  */
+                __u32 trans_len;
+        };
+
 References
 ==========
 
arch/x86/kvm/svm.c +0 -0
index 4c2a225ba546..a1cfd36d6195 100644

arch/x86/kvm/svm.c enum { · line 421

Quoted 4 lines
arch/x86/kvm/svm.c +1 -0
@@ -418,6 +418,7 @@ enum {
 
 static unsigned int max_sev_asid;
 static unsigned int min_sev_asid;
+static unsigned long me_mask;

sev_me_mask ?

Quoted 16 lines
arch/x86/kvm/svm.c +5 -2
 static unsigned long *sev_asid_bitmap;
 #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
 
@@ -1216,15 +1217,21 @@ static int avic_ga_log_notifier(u32 ga_tag)
 static __init int sev_hardware_setup(void)
 {
 	struct sev_user_data_status *status;
+	int eax, ebx;
 	int rc;
 
-	/* Maximum number of encrypted guests supported simultaneously */
-	max_sev_asid = cpuid_ecx(0x8000001F);
+	/*
+	 * Query the memory encryption information.
+	 *  EBX:  Bit 0:5 Pagetable bit position used to indicate encryption (aka Cbit).
+	 *  ECX:  Maximum number of encrypted guests supported simultaneously.

arch/x86/kvm/svm.c static int avic_ga_log_notifier(u32 ga_tag) · line 1235

Quoted 10 lines
arch/x86/kvm/svm.c +4 -1
+	 *  EDX:  Minimum ASID value that should be used for SEV guest.
+	 */
+	cpuid(0x8000001f, &eax, &ebx, &max_sev_asid, &min_sev_asid);
 
 	if (!max_sev_asid)
 		return 1;
 
-	/* Minimum ASID value that should be used for SEV guest */
+	me_mask = 1UL << (ebx & 0x3f);
 	min_sev_asid = cpuid_edx(0x8000001F);

You can remove this since you obtained it with the cpuid() call above.

Quoted 56 lines
arch/x86/kvm/svm.c +50 -0
 	/* Initialize SEV ASID bitmap */
@@ -7053,6 +7060,118 @@ static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct sev_data_send_update_data *data;
+	struct kvm_sev_send_update_data params;
+	void *hdr = NULL, *trans_data = NULL;
+	struct page **guest_page = NULL;
+	unsigned long n;
+	int ret, offset;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			sizeof(struct kvm_sev_send_update_data)))
+		return -EFAULT;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	/* userspace wants to query either header or trans length */
+	if (!params.trans_len || !params.hdr_len)
+		goto cmd;
+
+	ret = -EINVAL;
+	if (!params.trans_uaddr || !params.guest_uaddr ||
+	    !params.guest_len || !params.hdr_uaddr)
+		goto e_free;
+
+	/* Check if we are crossing the page boundry */
+	ret = -EINVAL;
+	offset = params.guest_uaddr & (PAGE_SIZE - 1);
+	if ((params.guest_len + offset > PAGE_SIZE))
+		goto e_free;
+
+	ret = -ENOMEM;
+	hdr = kmalloc(params.hdr_len, GFP_KERNEL);
+	if (!hdr)
+		goto e_free;
+
+	data->hdr_address = __psp_pa(hdr);
+	data->hdr_len = params.hdr_len;
+
+	ret = -ENOMEM;
+	trans_data = kmalloc(params.trans_len, GFP_KERNEL);
+	if (!trans_data)
+		goto e_free;
+
+	data->trans_address = __psp_pa(trans_data);

arch/x86/kvm/svm.c static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7122

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	data->trans_len = params.trans_len;
+
+	/* Pin guest memory */
+	ret = -EFAULT;
+	guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
+				    PAGE_SIZE, &n, 0);
+	if (!guest_page)
+		goto e_free;
+
+	data->guest_address = __sme_page_pa(guest_page[0]) + offset;

If the C-bit needs to be set regardless below, then you don't need the __sme version of this.

arch/x86/kvm/svm.c static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7128

Quoted 6 lines
arch/x86/kvm/svm.c +6 -0
+	data->guest_len = params.guest_len;
+
+	/*
+	 * The SEND_UPDATE_DATA command requires C-bit to be always set.
+	 * The __sme_page_pa() takes care of setting the C-bit only when SME
+	 * is enabled on the host. But we need to set the C-bit regarless of

regardless

Thanks,
Tom

Quoted 80 lines
arch/x86/kvm/svm.c +49 -0
+	 * the SME state.
+	 */
+	data->guest_address |= me_mask;
+
+	/* flush the caches to ensure that DRAM has recent contents */
+	sev_clflush_pages(guest_page, 1);
+
+cmd:
+	data->handle = sev->handle;
+	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, data, &argp->error);
+
+	/* userspace asked for header or trans length and FW responded with data */
+	if (!params.trans_len || !params.hdr_len) {
+		params.hdr_len = data->hdr_len;
+		params.trans_len = data->trans_len;
+		goto done;
+	}
+
+	if (ret)
+		goto e_unpin;
+
+	/* copy transport buffer to user space */
+	if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr,
+			 trans_data, params.trans_len)) {
+		ret = -EFAULT;
+		goto e_unpin;
+	}
+
+	/* copy packet header to userspace */
+	if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr, params.hdr_len))
+		ret = -EFAULT;
+
+e_unpin:
+	sev_unpin_memory(kvm, guest_page, n);
+done:
+	if (copy_to_user((void __user *)(uintptr_t)argp->data, &params,
+			sizeof(struct kvm_sev_send_update_data)))
+		ret = -EFAULT;
+e_free:
+	kfree(data);
+	kfree(trans_data);
+	kfree(hdr);
+
+	return ret;
+}
+
 static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -7097,6 +7216,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_SEND_START:
 		r = sev_send_start(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_SEND_UPDATE_DATA:
+		r = sev_send_update_data(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
include/uapi/linux/kvm.h +9 -0
index f425418bec13..0bee91bba329 100644
@@ -1532,6 +1532,15 @@ struct kvm_sev_send_start {
 	__u32 session_len;
 };
 
+struct kvm_sev_send_update_data {
+	__u64 hdr_uaddr;
+	__u32 hdr_len;
+	__u64 guest_uaddr;
+	__u32 guest_len;
+	__u64 trans_uaddr;
+	__u32 trans_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
#10 lore raw

Re: [RFC PATCH v1 02/10] KVM: SVM: Add KVM_SEND_UPDATE_DATA command

On 4/26/19 3:31 PM, Lendacky, Thomas wrote:
...

Quoted 7 lines
Quoted 4 lines

static unsigned int max_sev_asid;
static unsigned int min_sev_asid;
+static unsigned long me_mask;

sev_me_mask ?

Agreed.

Quoted 20 lines
Quoted 20 lines
  static unsigned long *sev_asid_bitmap;
  #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
arch/x86/kvm/svm.c +8 -2
@@ -1216,15 +1217,21 @@ static int avic_ga_log_notifier(u32 ga_tag)
  static __init int sev_hardware_setup(void)
  {
  	struct sev_user_data_status *status;
+	int eax, ebx;
  	int rc;
 
-	/* Maximum number of encrypted guests supported simultaneously */
-	max_sev_asid = cpuid_ecx(0x8000001F);
+	/*
+	 * Query the memory encryption information.
+	 *  EBX:  Bit 0:5 Pagetable bit position used to indicate encryption (aka Cbit).
+	 *  ECX:  Maximum number of encrypted guests supported simultaneously.
+	 *  EDX:  Minimum ASID value that should be used for SEV guest.
+	 */
+	cpuid(0x8000001f, &eax, &ebx, &max_sev_asid, &min_sev_asid);
 
Quoted 9 lines

arch/x86/kvm/svm.c static int avic_ga_log_notifier(u32 ga_tag) · line 1235

Quoted 6 lines
arch/x86/kvm/svm.c +1 -1
  	if (!max_sev_asid)
  		return 1;
 
-	/* Minimum ASID value that should be used for SEV guest */
+	me_mask = 1UL << (ebx & 0x3f);
  	min_sev_asid = cpuid_edx(0x8000001F);

You can remove this since you obtained it with the cpuid() call above.

I thought I removed it but.. I will take care in next rev.

Quoted 61 lines
Quoted 61 lines
arch/x86/kvm/svm.c +55 -0
  	/* Initialize SEV ASID bitmap */
@@ -7053,6 +7060,118 @@ static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
  	return ret;
  }
 
+static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct sev_data_send_update_data *data;
+	struct kvm_sev_send_update_data params;
+	void *hdr = NULL, *trans_data = NULL;
+	struct page **guest_page = NULL;
+	unsigned long n;
+	int ret, offset;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			sizeof(struct kvm_sev_send_update_data)))
+		return -EFAULT;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	/* userspace wants to query either header or trans length */
+	if (!params.trans_len || !params.hdr_len)
+		goto cmd;
+
+	ret = -EINVAL;
+	if (!params.trans_uaddr || !params.guest_uaddr ||
+	    !params.guest_len || !params.hdr_uaddr)
+		goto e_free;
+
+	/* Check if we are crossing the page boundry */
+	ret = -EINVAL;
+	offset = params.guest_uaddr & (PAGE_SIZE - 1);
+	if ((params.guest_len + offset > PAGE_SIZE))
+		goto e_free;
+
+	ret = -ENOMEM;
+	hdr = kmalloc(params.hdr_len, GFP_KERNEL);
+	if (!hdr)
+		goto e_free;
+
+	data->hdr_address = __psp_pa(hdr);
+	data->hdr_len = params.hdr_len;
+
+	ret = -ENOMEM;
+	trans_data = kmalloc(params.trans_len, GFP_KERNEL);
+	if (!trans_data)
+		goto e_free;
+
+	data->trans_address = __psp_pa(trans_data);
+	data->trans_len = params.trans_len;
+
+	/* Pin guest memory */
+	ret = -EFAULT;
+	guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
Quoted 9 lines

arch/x86/kvm/svm.c static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7122

Quoted 5 lines
arch/x86/kvm/svm.c +5 -0
+				    PAGE_SIZE, &n, 0);
+	if (!guest_page)
+		goto e_free;
+
+	data->guest_address = __sme_page_pa(guest_page[0]) + offset;

If the C-bit needs to be set regardless below, then you don't need the __sme version of this.

Noted.

#11 lore raw

RFC PATCH 3/10 KVM: SVM: Add KVM_SEV_SEND_FINISH command

In reply to Singh, Brijesh

The command is used to finailize the encryption context created with KVM_SEV_SEND_START command.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     |  8 +++++++
 arch/x86/kvm/svm.c                            | 23 +++++++++++++++++++
 2 files changed, 31 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +8 -0
index a0208e171489..006832256ae3 100644
@@ -285,6 +285,14 @@ Returns: 0 on success, -negative on error
                 __u32 trans_len;
         };
 
+12. KVM_SEV_SEND_FINISH
+------------------------
+
+After completion of the migration flow, the KVM_SEV_SEND_FINISH command can be
+issued by the hypervisor to delete the encryption context.
+
+Returns: 0 on success, -negative on error
+
 References
 ==========
 
arch/x86/kvm/svm.c +23 -0
index a1cfd36d6195..263f3c7deae7 100644
@@ -7172,6 +7172,26 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct sev_data_send_finish *data;
+	int ret;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->handle = sev->handle;
+	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, data, &argp->error);
+
+	kfree(data);
+	return ret;
+}
+
 static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -7219,6 +7239,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_SEND_UPDATE_DATA:
 		r = sev_send_update_data(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_SEND_FINISH:
+		r = sev_send_finish(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
Signature · 1 line
2.17.1
#12 lore raw

RFC PATCH 4/10 KVM: SVM: Add support for KVM_SEV_RECEIVE_START command

In reply to Singh, Brijesh

The command is used to create encryption context for the incoming SEV guest. The encryption context can be later unused by the hypervisor to import the incoming data into the SEV guest memory space.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     | 29 +++++++
 arch/x86/kvm/svm.c                            | 80 +++++++++++++++++++
 include/uapi/linux/kvm.h                      |  9 +++
 3 files changed, 118 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +29 -0
index 006832256ae3..acdff2454649 100644
@@ -293,6 +293,35 @@ issued by the hypervisor to delete the encryption context.
 
 Returns: 0 on success, -negative on error
 
+13. KVM_SEV_RECEIVE_START
+------------------------
+
+The KVM_SEV_RECEIVE_START command is used for creating the memory encryption
+context for an incoming SEV guest. To create the encryption context, user must
+provide a guest policy, the platform public Diffie-Hellman (PDH) key and session
+information.
+
+Parameters: struct  kvm_sev_receive_start (in/out)
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_receive_start {
+                __u32 handle;           /* if zero then firmware creates a new handle */
+                __u32 policy;           /* guest's policy */
+
+                __u64 pdh_uaddr;         /* userspace address pointing to the PDH key */
+                __u32 dh_len;
+
+                __u64 session_addr;     /* userspace address which points to the guest session information */
+                __u32 session_len;
+        };
+
+On success, the 'handle' field contains a new handle and on error, a negative value.
+
+For more details, see SEV spec Section 6.12.
+
 References
 ==========
 
arch/x86/kvm/svm.c +80 -0

92 lines not on this page · raw message

include/uapi/linux/kvm.h +9 -0
index 0bee91bba329..fee75bf1fd90 100644
@@ -1541,6 +1541,15 @@ struct kvm_sev_send_update_data {
 	__u32 trans_len;
 };
 
+struct kvm_sev_receive_start {
+	__u32 handle;
+	__u32 policy;
+	__u64 pdh_uaddr;
+	__u32 pdh_len;
+	__u64 session_uaddr;
+	__u32 session_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.17.1
#13 lore raw

Re: [RFC PATCH v1 04/10] KVM: SVM: Add support for KVM_SEV_RECEIVE_START command

Quoted 1 line · Brijesh

The command is used to create encryption context for the incoming

create the encryption context for an incoming

Quoted 1 line

SEV guest. The encryption context can be later unused by the hypervisor

can be later used by

Quoted 25 lines

to import the incoming data into the SEV guest memory space.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     | 29 +++++++
 arch/x86/kvm/svm.c                            | 80 +++++++++++++++++++
 include/uapi/linux/kvm.h                      |  9 +++
 3 files changed, 118 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +0 -0
index 006832256ae3..acdff2454649 100644

Documentation/virtual/kvm/amd-memory-encryption.rst issued by the hypervisor to delete the encryption context. · line 300

Quoted 8 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +5 -0
@@ -293,6 +293,35 @@ issued by the hypervisor to delete the encryption context.
 
 Returns: 0 on success, -negative on error
 
+13. KVM_SEV_RECEIVE_START
+------------------------
+
+The KVM_SEV_RECEIVE_START command is used for creating the memory encryption
+context for an incoming SEV guest. To create the encryption context, user must

the user must

Thanks,
Tom

Quoted 146 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +24 -0
+provide a guest policy, the platform public Diffie-Hellman (PDH) key and session
+information.
+
+Parameters: struct  kvm_sev_receive_start (in/out)
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_receive_start {
+                __u32 handle;           /* if zero then firmware creates a new handle */
+                __u32 policy;           /* guest's policy */
+
+                __u64 pdh_uaddr;         /* userspace address pointing to the PDH key */
+                __u32 dh_len;
+
+                __u64 session_addr;     /* userspace address which points to the guest session information */
+                __u32 session_len;
+        };
+
+On success, the 'handle' field contains a new handle and on error, a negative value.
+
+For more details, see SEV spec Section 6.12.
+
 References
 ==========
 
arch/x86/kvm/svm.c +80 -0
index 263f3c7deae7..a7dcf19baefb 100644
@@ -7192,6 +7192,83 @@ static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct sev_data_receive_start *start;
+	struct kvm_sev_receive_start params;
+	int *error = &argp->error;
+	void *session_data = NULL;
+	void *pdh_data = NULL;
+	int ret;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	/* Get parameter from the user */
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			sizeof(struct kvm_sev_receive_start)))
+		return -EFAULT;
+
+	if (!params.pdh_uaddr || !params.pdh_len ||
+	    !params.session_uaddr || !params.session_len)
+		return -EINVAL;
+
+	start = kzalloc(sizeof(*start), GFP_KERNEL);
+	if (!start)
+		return -ENOMEM;
+
+	start->handle = params.handle;
+	start->policy = params.policy;
+
+	pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
+	if (IS_ERR(pdh_data)) {
+		ret = PTR_ERR(pdh_data);
+		goto e_free;
+	}
+
+	start->pdh_cert_address = __psp_pa(pdh_data);
+	start->pdh_cert_len = params.pdh_len;
+
+	session_data = psp_copy_user_blob(params.session_uaddr, params.session_len);
+	if (IS_ERR(session_data)) {
+		ret = PTR_ERR(session_data);
+		goto e_free_pdh;
+	}
+
+	start->session_address = __psp_pa(session_data);
+	start->session_len = params.session_len;
+
+	/* create memory encryption context */
+	ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, start, error);
+	if (ret)
+		goto e_free_session;
+
+	/* Bind ASID to this guest */
+	ret = sev_bind_asid(kvm, start->handle, error);
+	if (ret)
+		goto e_free_session;
+
+	params.handle = start->handle;
+	if (copy_to_user((void __user *)(uintptr_t)argp->data,
+			 &params, sizeof(struct kvm_sev_receive_start))) {
+		ret = -EFAULT;
+		sev_unbind_asid(kvm, start->handle);
+		goto e_free_session;
+	}
+
+	sev->handle = start->handle;
+	sev->fd = argp->sev_fd;
+
+e_free_session:
+	kfree(session_data);
+e_free_pdh:
+	kfree(pdh_data);
+e_free:
+	kfree(start);
+	return ret;
+}
+
 static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -7242,6 +7319,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_SEND_FINISH:
 		r = sev_send_finish(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_RECEIVE_START:
+		r = sev_receive_start(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
include/uapi/linux/kvm.h +9 -0
index 0bee91bba329..fee75bf1fd90 100644
@@ -1541,6 +1541,15 @@ struct kvm_sev_send_update_data {
 	__u32 trans_len;
 };
 
+struct kvm_sev_receive_start {
+	__u32 handle;
+	__u32 policy;
+	__u64 pdh_uaddr;
+	__u32 pdh_len;
+	__u64 session_uaddr;
+	__u32 session_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
#14 lore raw

RFC PATCH 5/10 KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command

In reply to Singh, Brijesh

The command is used for copying the incoming buffer into the SEV guest memory space.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     | 24 ++++++
 arch/x86/kvm/svm.c                            | 84 +++++++++++++++++++
 include/uapi/linux/kvm.h                      |  9 ++
 3 files changed, 117 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +24 -0
index acdff2454649..b9c3dc02f344 100644
@@ -322,6 +322,30 @@ On success, the 'handle' field contains a new handle and on error, a negative va
 
 For more details, see SEV spec Section 6.12.
 
+14. KVM_SEV_RECEIVE_UPDATE_DATA
+----------------------------
+
+The KVM_SEV_RECEIVE_UPDATE_DATA command can be used by the hypervisor to copy
+the incoming buffers into the guest memory region with encryption context
+created during the KVM_SEV_RECEIVE_START.
+
+Parameters (in): struct kvm_sev_receive_update_data
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_launch_receive_update_data {
+                __u64 hdr_uaddr;        /* userspace address containing the packet header */
+                __u32 hdr_len;
+
+                __u64 guest_uaddr;      /* the destination guest memory region */
+                __u32 guest_len;
+
+                __u64 trans_uaddr;      /* the incoming buffer memory region  */
+                __u32 trans_len;
+        };
+
 References
 ==========
 
arch/x86/kvm/svm.c +84 -0

96 lines not on this page · raw message

include/uapi/linux/kvm.h +9 -0
index fee75bf1fd90..07e058a3ec11 100644
@@ -1550,6 +1550,15 @@ struct kvm_sev_receive_start {
 	__u32 session_len;
 };
 
+struct kvm_sev_receive_update_data {
+	__u64 hdr_uaddr;
+	__u32 hdr_len;
+	__u64 guest_uaddr;
+	__u32 guest_len;
+	__u64 trans_uaddr;
+	__u32 trans_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.17.1
#15 lore raw

Re: [RFC PATCH v1 05/10] KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command

Quoted 111 lines · Brijesh

The command is used for copying the incoming buffer into the SEV guest memory space.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     | 24 ++++++
 arch/x86/kvm/svm.c                            | 84 +++++++++++++++++++
 include/uapi/linux/kvm.h                      |  9 ++
 3 files changed, 117 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +24 -0
index acdff2454649..b9c3dc02f344 100644
@@ -322,6 +322,30 @@ On success, the 'handle' field contains a new handle and on error, a negative va
 
 For more details, see SEV spec Section 6.12.
 
+14. KVM_SEV_RECEIVE_UPDATE_DATA
+----------------------------
+
+The KVM_SEV_RECEIVE_UPDATE_DATA command can be used by the hypervisor to copy
+the incoming buffers into the guest memory region with encryption context
+created during the KVM_SEV_RECEIVE_START.
+
+Parameters (in): struct kvm_sev_receive_update_data
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_launch_receive_update_data {
+                __u64 hdr_uaddr;        /* userspace address containing the packet header */
+                __u32 hdr_len;
+
+                __u64 guest_uaddr;      /* the destination guest memory region */
+                __u32 guest_len;
+
+                __u64 trans_uaddr;      /* the incoming buffer memory region  */
+                __u32 trans_len;
+        };
+
 References
 ==========
 
arch/x86/kvm/svm.c +47 -0
index a7dcf19baefb..c81cac09c5af 100644
@@ -7269,6 +7269,87 @@ static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct kvm_sev_receive_update_data params;
+	struct sev_data_receive_update_data *data;
+	void *hdr = NULL, *trans = NULL;
+	struct page **guest_page;
+	unsigned long n;
+	int ret, offset;
+
+	if (!sev_guest(kvm))
+		return -EINVAL;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			sizeof(struct kvm_sev_receive_update_data)))
+		return -EFAULT;
+
+	if (!params.hdr_uaddr || !params.hdr_len ||
+	    !params.guest_uaddr || !params.guest_len ||
+	    !params.trans_uaddr || !params.trans_len)
+		return -EINVAL;
+
+	/* Check if we are crossing the page boundry */
+	offset = params.guest_uaddr & (PAGE_SIZE - 1);
+	if ((params.guest_len + offset > PAGE_SIZE))
+		return -EINVAL;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
+	if (IS_ERR(hdr)) {
+		ret = PTR_ERR(hdr);
+		goto e_free;
+	}
+
+	data->hdr_address = __psp_pa(hdr);
+	data->hdr_len = params.hdr_len;
+
+	trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
+		goto e_free;
+	}
+
+	data->trans_address = __psp_pa(trans);

arch/x86/kvm/svm.c static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7328

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	data->trans_len = params.trans_len;
+
+	/* Pin guest memory */
+	ret = -EFAULT;
+	guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
+				    PAGE_SIZE, &n, 0);
+	if (!guest_page)
+		goto e_free;
+
+	data->guest_address = __sme_page_pa(guest_page[0]) + offset;

Same comment here as in patch 02. No need to use the __sme version if you're going to unconditionally set it below.

Thanks,
Tom

Quoted 58 lines
arch/x86/kvm/svm.c +27 -0
+	data->guest_len = params.guest_len;
+
+	/*
+	 * The RECEIVE_UPDATE_DATA command requires C-bit to be always set.
+	 * The __sme_page_pa() takes care of setting the C-bit only when SME
+	 * is enabled on the host. But we need to set the C-bit regarless of
+	 * the SME state.
+	 */
+	data->guest_address |= me_mask;
+
+	/* flush the caches to ensure that DRAM has recent contents */
+	sev_clflush_pages(guest_page, n);
+
+	data->handle = sev->handle;
+	ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, data, &argp->error);
+
+	sev_unpin_memory(kvm, guest_page, n);
+e_free:
+	kfree(data);
+	kfree(hdr);
+	kfree(trans);
+	return ret;
+}
+
 static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -7322,6 +7403,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_RECEIVE_START:
 		r = sev_receive_start(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_RECEIVE_UPDATE_DATA:
+		r = sev_receive_update_data(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
include/uapi/linux/kvm.h +9 -0
index fee75bf1fd90..07e058a3ec11 100644
@@ -1550,6 +1550,15 @@ struct kvm_sev_receive_start {
 	__u32 session_len;
 };
 
+struct kvm_sev_receive_update_data {
+	__u64 hdr_uaddr;
+	__u32 hdr_len;
+	__u64 guest_uaddr;
+	__u32 guest_len;
+	__u64 trans_uaddr;
+	__u32 trans_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
#16 lore raw

RFC PATCH 6/10 KVM: SVM: Add KVM_SEV_RECEIVE_FINISH command

In reply to Singh, Brijesh

The command finalize the guest receiving process and make the SEV guest ready for the execution.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     |  8 +++++++
 arch/x86/kvm/svm.c                            | 23 +++++++++++++++++++
 2 files changed, 31 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +8 -0
index b9c3dc02f344..72c45f93abd7 100644
@@ -346,6 +346,14 @@ Returns: 0 on success, -negative on error
                 __u32 trans_len;
         };
 
+15. KVM_SEV_RECEIVE_FINISH
+------------------------
+
+After completion of the migration flow, the KVM_SEV_RECEIVE_FINISH command can be
+issued by the hypervisor to make the guest ready for the execution.
+
+Returns: 0 on success, -negative on error
+
 References
 ==========
 
arch/x86/kvm/svm.c +23 -0
index c81cac09c5af..74b57ab742ad 100644
@@ -7350,6 +7350,26 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct sev_data_receive_finish *data;
+	int ret;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->handle = sev->handle;
+	ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, data, &argp->error);
+
+	kfree(data);
+	return ret;
+}
+
 static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -7406,6 +7426,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_RECEIVE_UPDATE_DATA:
 		r = sev_receive_update_data(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_RECEIVE_FINISH:
+		r = sev_receive_finish(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
Signature · 1 line
2.17.1
#17 lore raw

Re: [RFC PATCH v1 06/10] KVM: SVM: Add KVM_SEV_RECEIVE_FINISH command

Quoted 25 lines · Brijesh

The command finalize the guest receiving process and make the SEV guest ready for the execution.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 .../virtual/kvm/amd-memory-encryption.rst     |  8 +++++++
 arch/x86/kvm/svm.c                            | 23 +++++++++++++++++++
 2 files changed, 31 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.rst +0 -0
index b9c3dc02f344..72c45f93abd7 100644

Documentation/virtual/kvm/amd-memory-encryption.rst Returns: 0 on success, -negative on error · line 353

Quoted 8 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +5 -0
@@ -346,6 +346,14 @@ Returns: 0 on success, -negative on error
                 __u32 trans_len;
         };
 
+15. KVM_SEV_RECEIVE_FINISH
+------------------------
+
+After completion of the migration flow, the KVM_SEV_RECEIVE_FINISH command can be
+issued by the hypervisor to make the guest ready for the execution.

for execution.

Thanks,
Tom

Quoted 48 lines
Documentation/virtual/kvm/amd-memory-encryption.rst +3 -0
+
+Returns: 0 on success, -negative on error
+
 References
 ==========
 
arch/x86/kvm/svm.c +23 -0
index c81cac09c5af..74b57ab742ad 100644
@@ -7350,6 +7350,26 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct sev_data_receive_finish *data;
+	int ret;
+
+	if (!sev_guest(kvm))
+		return -ENOTTY;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->handle = sev->handle;
+	ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, data, &argp->error);
+
+	kfree(data);
+	return ret;
+}
+
 static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -7406,6 +7426,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_RECEIVE_UPDATE_DATA:
 		r = sev_receive_update_data(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_RECEIVE_FINISH:
+		r = sev_receive_finish(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
#18 lore raw

RFC PATCH 7/10 KVM: x86: Add AMD SEV specific Hypercall3

In reply to Singh, Brijesh

KVM hypercall framework relies on alternative framework to patch the VMCALL -> VMMCALL on AMD platform. If a hypercall is made before apply_alternative() is called then it defaults to VMCALL. The approach works fine on non SEV guest. A VMCALL would causes #UD, and hypervisor will be able to decode the instruction and do the right things. But when SEV is active, guest memory is encrypted with guest key and hypervisor will not be able to decode the instruction bytes.

Add SEV specific hypercall3, it unconditionally uses VMMCALL. The hypercall will be used by the SEV guest to notify encrypted pages to the hypervisor.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 arch/x86/include/asm/kvm_para.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/include/asm/kvm_para.h +12 -0
index 5ed3cf1c3934..94e91c0bc2e0 100644
@@ -84,6 +84,18 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
 	return ret;
 }
 
+static inline long kvm_sev_hypercall3(unsigned int nr, unsigned long p1,
+				      unsigned long p2, unsigned long p3)
+{
+	long ret;
+
+	asm volatile("vmmcall"
+		     : "=a"(ret)
+		     : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
+		     : "memory");
+	return ret;
+}
+
 #ifdef CONFIG_KVM_GUEST
 bool kvm_para_available(void);
 unsigned int kvm_arch_para_features(void);
Signature · 1 line
2.17.1
#19 lore raw

RFC PATCH 8/10 KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

In reply to Singh, Brijesh

The hypercall can be used by the SEV guest to notify 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 the guest pages should be considered encrypted.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 Documentation/virtual/kvm/hypercalls.txt | 14 +++++
 arch/x86/include/asm/kvm_host.h          |  2 +
 arch/x86/kvm/svm.c                       | 69 ++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c                   |  1 +
 arch/x86/kvm/x86.c                       |  5 ++
 include/uapi/linux/kvm_para.h            |  1 +
 6 files changed, 92 insertions(+)
Documentation/virtual/kvm/hypercalls.txt +14 -0
index da24c138c8d1..ecd44e488679 100644
@@ -141,3 +141,17 @@ a0 corresponds to the APIC ID in the third argument (a2), bit 1
 corresponds to the APIC ID a2+1, and so on.
 
 Returns the number of CPUs to which the IPIs were delivered successfully.
+
+7. 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: set or clear the encryption attribute
+
+   Where:
+	* 1: Encryption attribute is set
+	* 0: Encryption attribute is cleared
arch/x86/include/asm/kvm_host.h +2 -0
index a9d03af34030..adb0ca035b97 100644
@@ -1196,6 +1196,8 @@ struct kvm_x86_ops {
 	uint16_t (*nested_get_evmcs_version)(struct kvm_vcpu *vcpu);
 
 	bool (*need_emulation_on_page_fault)(struct kvm_vcpu *vcpu);
+	int (*page_enc_status_hc)(struct kvm *kvm, unsigned long gpa,
+				  unsigned long sz, unsigned long mode);
 };
 
 struct kvm_arch_async_pf {
arch/x86/kvm/svm.c +69 -0

93 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.c +1 -0
index b4e7d645275a..9c814e560e0f 100644
@@ -7731,6 +7731,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
 	.get_vmcs12_pages = NULL,
 	.nested_enable_evmcs = NULL,
 	.need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
+	.page_enc_status_hc = NULL,
 };
 
 static void vmx_cleanup_l1d_flush(void)
arch/x86/kvm/x86.c +5 -0
index a0d1fc80ac5a..dea644be5992 100644
@@ -7141,6 +7141,11 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
 	case KVM_HC_SEND_IPI:
 		ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
 		break;
+	case KVM_HC_PAGE_ENC_STATUS:
+		ret = -KVM_ENOSYS;
+		if (kvm_x86_ops->page_enc_status_hc)
+			ret = kvm_x86_ops->page_enc_status_hc(vcpu->kvm, a0, a1, a2);
+		break;
 	default:
 		ret = -KVM_ENOSYS;
 		break;
include/uapi/linux/kvm_para.h +1 -0
index 6c0ce49931e5..3dc9e579f4f9 100644
@@ -28,6 +28,7 @@
 #define KVM_HC_MIPS_CONSOLE_OUTPUT	8
 #define KVM_HC_CLOCK_PAIRING		9
 #define KVM_HC_SEND_IPI		10
+#define KVM_HC_PAGE_ENC_STATUS		11
 
 /*
  * hypercalls use architecture specific
Signature · 1 line
2.17.1
#20 lore raw

Re: [RFC PATCH v1 08/10] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 1 line · Brijesh

The hypercall can be used by the SEV guest to notify the page encryption

This hyercall is used by the SEV guest to notify a change in the page...

Quoted 3 lines

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 the guest pages should be considered encrypted.

By default all guest page are considered

Quoted 30 lines
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 Documentation/virtual/kvm/hypercalls.txt | 14 +++++
 arch/x86/include/asm/kvm_host.h          |  2 +
 arch/x86/kvm/svm.c                       | 69 ++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c                   |  1 +
 arch/x86/kvm/x86.c                       |  5 ++
 include/uapi/linux/kvm_para.h            |  1 +
 6 files changed, 92 insertions(+)
Documentation/virtual/kvm/hypercalls.txt +0 -0
index da24c138c8d1..ecd44e488679 100644
@@ -141,3 +141,17 @@ a0 corresponds to the APIC ID in the third argument (a2), bit 1
 corresponds to the APIC ID a2+1, and so on.
 
 Returns the number of CPUs to which the IPIs were delivered successfully.

Documentation/virtual/kvm/hypercalls.txt a0 corresponds to the APIC ID in the third argument (a2), bit 1 · line 153

Quoted 10 lines
Documentation/virtual/kvm/hypercalls.txt +10 -0
+
+7. 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: set or clear the encryption attribute

a2: encryption attribute

Quoted 48 lines
Documentation/virtual/kvm/hypercalls.txt +4 -0
+
+   Where:
+	* 1: Encryption attribute is set
+	* 0: Encryption attribute is cleared
arch/x86/include/asm/kvm_host.h +2 -0
index a9d03af34030..adb0ca035b97 100644
@@ -1196,6 +1196,8 @@ struct kvm_x86_ops {
 	uint16_t (*nested_get_evmcs_version)(struct kvm_vcpu *vcpu);
 
 	bool (*need_emulation_on_page_fault)(struct kvm_vcpu *vcpu);
+	int (*page_enc_status_hc)(struct kvm *kvm, unsigned long gpa,
+				  unsigned long sz, unsigned long mode);
 };
 
 struct kvm_arch_async_pf {
arch/x86/kvm/svm.c +9 -0
index 74b57ab742ad..f024f208b052 100644
@@ -138,6 +138,8 @@ struct kvm_sev_info {
 	int fd;			/* SEV device fd */
 	unsigned long pages_locked; /* Number of pages locked */
 	struct list_head regions_list;  /* List of registered regions */
+	unsigned long *page_enc_bmap;
+	unsigned long page_enc_bmap_size;
 };
 
 struct kvm_svm {
@@ -1911,6 +1913,8 @@ static void sev_vm_destroy(struct kvm *kvm)
 
 	sev_unbind_asid(kvm, sev->handle);
 	sev_asid_free(kvm);
+
+	kvfree(sev->page_enc_bmap);
 }
 
 static void avic_vm_destroy(struct kvm *kvm)
@@ -7370,6 +7374,69 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int sev_resize_page_enc_bitmap(struct kvm *kvm, unsigned long new_size)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	unsigned long *map;
+	unsigned long sz;

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7391

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+
+	if (sev->page_enc_bmap_size >= new_size)
+		return 0;
+
+	sz = ALIGN(new_size, BITS_PER_LONG) / 8;
+
+	if (sz > PAGE_SIZE)
+		map = vmalloc(sz);
+	else
+		map = kmalloc(sz, GFP_KERNEL);

Any reason this can't always be vmalloc()?

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7395

Quoted 4 lines
arch/x86/kvm/svm.c +4 -0
+
+	if (!map) {
+		pr_err_once("Failed to allocate decrypted bitmap size %lx\n", sz);
+		return 1;

Should this be -ENOMEM?

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	}
+
+	/* mark the page encrypted (by default) */
+	memset(map, 0xff, sz);
+
+	bitmap_copy(map, sev->page_enc_bmap, sev->page_enc_bmap_size);
+	kvfree(sev->page_enc_bmap);
+
+	sev->page_enc_bmap = map;
+	sev->page_enc_bmap_size = new_size;

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7415

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+
+	return 0;
+}
+
+static int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa,
+				  unsigned long npages, unsigned long enc)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	gfn_t gfn_start, gfn_end;
+	int r;

int ret; ? "r" at first confused me.

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7426

Quoted 11 lines
arch/x86/kvm/svm.c +11 -0
+
+	if (!npages)
+		return 0;
+
+	gfn_start = gpa_to_gfn(gpa);
+	gfn_end = gfn_start + npages;
+
+	mutex_lock(&kvm->lock);
+
+	r = 1;
+	if (sev_resize_page_enc_bitmap(kvm, gfn_end))

ret = sev_resize_...
if (ret)

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7434

Quoted 8 lines
arch/x86/kvm/svm.c +8 -0
+		goto unlock;
+
+	if (enc)
+		__bitmap_set(sev->page_enc_bmap, gfn_start, gfn_end - gfn_start);
+	else
+		__bitmap_clear(sev->page_enc_bmap, gfn_start, gfn_end - gfn_start);
+
+	r = 0;

If you do the above, this is not needed.

Thanks,
Tom

Quoted 58 lines
arch/x86/kvm/svm.c +7 -0
+unlock:
+	mutex_unlock(&kvm->lock);
+	return r;
+}
+
 static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -7711,6 +7778,8 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
 	.nested_get_evmcs_version = nested_get_evmcs_version,
 
 	.need_emulation_on_page_fault = svm_need_emulation_on_page_fault,
+
+	.page_enc_status_hc = svm_page_enc_status_hc
 };
 
 static int __init svm_init(void)
arch/x86/kvm/vmx/vmx.c +1 -0
index b4e7d645275a..9c814e560e0f 100644
@@ -7731,6 +7731,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
 	.get_vmcs12_pages = NULL,
 	.nested_enable_evmcs = NULL,
 	.need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
+	.page_enc_status_hc = NULL,
 };
 
 static void vmx_cleanup_l1d_flush(void)
arch/x86/kvm/x86.c +5 -0
index a0d1fc80ac5a..dea644be5992 100644
@@ -7141,6 +7141,11 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
 	case KVM_HC_SEND_IPI:
 		ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
 		break;
+	case KVM_HC_PAGE_ENC_STATUS:
+		ret = -KVM_ENOSYS;
+		if (kvm_x86_ops->page_enc_status_hc)
+			ret = kvm_x86_ops->page_enc_status_hc(vcpu->kvm, a0, a1, a2);
+		break;
 	default:
 		ret = -KVM_ENOSYS;
 		break;
include/uapi/linux/kvm_para.h +1 -0
index 6c0ce49931e5..3dc9e579f4f9 100644
@@ -28,6 +28,7 @@
 #define KVM_HC_MIPS_CONSOLE_OUTPUT	8
 #define KVM_HC_CLOCK_PAIRING		9
 #define KVM_HC_SEND_IPI		10
+#define KVM_HC_PAGE_ENC_STATUS		11
 
 /*
  * hypercalls use architecture specific
#21 lore raw

Re: [RFC PATCH v1 08/10] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

Quoted 106 lines · Thomas
Quoted 1 line · Brijesh

The hypercall can be used by the SEV guest to notify the page encryption

This hyercall is used by the SEV guest to notify a change in the page...

Quoted 3 lines

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 the guest pages should be considered encrypted.

By default all guest page are considered

Quoted 30 lines
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
  Documentation/virtual/kvm/hypercalls.txt | 14 +++++
  arch/x86/include/asm/kvm_host.h          |  2 +
  arch/x86/kvm/svm.c                       | 69 ++++++++++++++++++++++++
  arch/x86/kvm/vmx/vmx.c                   |  1 +
  arch/x86/kvm/x86.c                       |  5 ++
  include/uapi/linux/kvm_para.h            |  1 +
  6 files changed, 92 insertions(+)
Documentation/virtual/kvm/hypercalls.txt +0 -0
index da24c138c8d1..ecd44e488679 100644
@@ -141,3 +141,17 @@ a0 corresponds to the APIC ID in the third argument (a2), bit 1
  corresponds to the APIC ID a2+1, and so on.
 
  Returns the number of CPUs to which the IPIs were delivered successfully.

Documentation/virtual/kvm/hypercalls.txt a0 corresponds to the APIC ID in the third argument (a2), bit 1 · line 153

Quoted 10 lines
Documentation/virtual/kvm/hypercalls.txt +10 -0
+
+7. 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: set or clear the encryption attribute

a2: encryption attribute

Quoted 52 lines
Documentation/virtual/kvm/hypercalls.txt +4 -0
+
+   Where:
+	* 1: Encryption attribute is set
+	* 0: Encryption attribute is cleared
arch/x86/include/asm/kvm_host.h +2 -0
index a9d03af34030..adb0ca035b97 100644
@@ -1196,6 +1196,8 @@ struct kvm_x86_ops {
  	uint16_t (*nested_get_evmcs_version)(struct kvm_vcpu *vcpu);
 
  	bool (*need_emulation_on_page_fault)(struct kvm_vcpu *vcpu);
+	int (*page_enc_status_hc)(struct kvm *kvm, unsigned long gpa,
+				  unsigned long sz, unsigned long mode);
  };
 
  struct kvm_arch_async_pf {
arch/x86/kvm/svm.c +13 -0
index 74b57ab742ad..f024f208b052 100644
@@ -138,6 +138,8 @@ struct kvm_sev_info {
  	int fd;			/* SEV device fd */
  	unsigned long pages_locked; /* Number of pages locked */
  	struct list_head regions_list;  /* List of registered regions */
+	unsigned long *page_enc_bmap;
+	unsigned long page_enc_bmap_size;
  };
 
  struct kvm_svm {
@@ -1911,6 +1913,8 @@ static void sev_vm_destroy(struct kvm *kvm)
 
  	sev_unbind_asid(kvm, sev->handle);
  	sev_asid_free(kvm);
+
+	kvfree(sev->page_enc_bmap);
  }
 
  static void avic_vm_destroy(struct kvm *kvm)
@@ -7370,6 +7374,69 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
  	return ret;
  }
 
+static int sev_resize_page_enc_bitmap(struct kvm *kvm, unsigned long new_size)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	unsigned long *map;
+	unsigned long sz;
+
+	if (sev->page_enc_bmap_size >= new_size)
+		return 0;
+
Quoted 9 lines

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7391

Quoted 6 lines
arch/x86/kvm/svm.c +6 -0
+	sz = ALIGN(new_size, BITS_PER_LONG) / 8;
+
+	if (sz > PAGE_SIZE)
+		map = vmalloc(sz);
+	else
+		map = kmalloc(sz, GFP_KERNEL);

Any reason this can't always be vmalloc()?

Yes, we can use vmalloc() unconditionally. The bitmap size will be mostly greater than PAGE_SIZE hence the above is useless anyway.

Quoted 21 lines

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7395

Quoted 4 lines
arch/x86/kvm/svm.c +4 -0
+
+	if (!map) {
+		pr_err_once("Failed to allocate decrypted bitmap size %lx\n", sz);
+		return 1;

Should this be -ENOMEM?

Quoted 14 lines
arch/x86/kvm/svm.c +14 -0
+	}
+
+	/* mark the page encrypted (by default) */
+	memset(map, 0xff, sz);
+
+	bitmap_copy(map, sev->page_enc_bmap, sev->page_enc_bmap_size);
+	kvfree(sev->page_enc_bmap);
+
+	sev->page_enc_bmap = map;
+	sev->page_enc_bmap_size = new_size;
+
+	return 0;
+}
+
Quoted 8 lines

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7415

Quoted 6 lines
arch/x86/kvm/svm.c +6 -0
+static int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa,
+				  unsigned long npages, unsigned long enc)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	gfn_t gfn_start, gfn_end;
+	int r;

int ret; ? "r" at first confused me.

I will fix it in next rev.

Quoted 16 lines

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7426

Quoted 12 lines
arch/x86/kvm/svm.c +11 -0
+
+	if (!npages)
+		return 0;
+
+	gfn_start = gpa_to_gfn(gpa);
+	gfn_end = gfn_start + npages;
+
+	mutex_lock(&kvm->lock);
+
+	r = 1;
+	if (sev_resize_page_enc_bitmap(kvm, gfn_end))

ret = sev_resize_...
if (ret)

Quoted 11 lines

arch/x86/kvm/svm.c static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 7434

Quoted 8 lines
arch/x86/kvm/svm.c +8 -0
+		goto unlock;
+
+	if (enc)
+		__bitmap_set(sev->page_enc_bmap, gfn_start, gfn_end - gfn_start);
+	else
+		__bitmap_clear(sev->page_enc_bmap, gfn_start, gfn_end - gfn_start);
+
+	r = 0;

If you do the above, this is not needed.

Yes agreed. thanks

#22 lore raw

RFC PATCH 9/10 KVM: x86: Introduce KVM_GET_PAGE_ENC_BITMAP ioctl

In reply to Singh, Brijesh

The ioctl can be used to retrieve page encryption bitmap for a given kvm memory slot.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/svm.c              | 54 ++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              | 12 ++++++++
 include/uapi/linux/kvm.h        | 12 ++++++++
 4 files changed, 78 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
index adb0ca035b97..9947c4be825d 100644
@@ -1198,6 +1198,7 @@ struct kvm_x86_ops {
 	bool (*need_emulation_on_page_fault)(struct kvm_vcpu *vcpu);
 	int (*page_enc_status_hc)(struct kvm *kvm, unsigned long gpa,
 				  unsigned long sz, unsigned long mode);
+	int (*get_page_enc_bitmap)(struct kvm *kvm, struct kvm_page_enc_bitmap *bmap);
 };
 
 struct kvm_arch_async_pf {
arch/x86/kvm/svm.c +53 -1

66 lines not on this page · raw message

arch/x86/kvm/x86.c +12 -0
index dea644be5992..44079979b1c5 100644
@@ -4882,6 +4882,18 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
 		break;
 	}
+	case KVM_GET_PAGE_ENC_BITMAP: {
+		struct kvm_page_enc_bitmap bitmap;
+
+		r = -EFAULT;
+		if (copy_from_user(&bitmap, argp, sizeof(bitmap)))
+			goto out;
+
+		r = -ENOTTY;
+		if (kvm_x86_ops->get_page_enc_bitmap)
+			r = kvm_x86_ops->get_page_enc_bitmap(kvm, &bitmap);
+		break;
+	}
 	default:
 		r = -ENOTTY;
 	}
include/uapi/linux/kvm.h +12 -0
index 07e058a3ec11..7f944d4e252c 100644
@@ -492,6 +492,16 @@ struct kvm_dirty_log {
 	};
 };
 
+/* for KVM_GET_PAGE_ENC_BITMAP */
+struct kvm_page_enc_bitmap {
+	__u32 slot;
+	__u32 padding1;
+	union {
+		void __user *enc_bitmap; /* one bit per page */
+		__u64 padding2;
+	};
+};
+
 /* for KVM_CLEAR_DIRTY_LOG */
 struct kvm_clear_dirty_log {
 	__u32 slot;
@@ -1440,6 +1450,8 @@ struct kvm_enc_region {
 /* Available with KVM_CAP_HYPERV_CPUID */
 #define KVM_GET_SUPPORTED_HV_CPUID _IOWR(KVMIO, 0xc1, struct kvm_cpuid2)
 
+#define KVM_GET_PAGE_ENC_BITMAP	_IOW(KVMIO, 0xc2, struct kvm_page_enc_bitmap)
+
 /* Secure Encrypted Virtualization command */
 enum sev_cmd_id {
 	/* Guest initialization commands */
Signature · 1 line
2.17.1
#23 lore raw

RFC PATCH 10/10 mm: x86: Invoke hypercall when page encryption status is changed

In reply to Singh, Brijesh

Invoke a hypercall when a memory region is changed from encrypted -> decrypted and vice versa. Hypervisor need to know the page encryption status during the guest migration.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 arch/x86/include/asm/mem_encrypt.h |  3 ++
 arch/x86/mm/mem_encrypt.c          | 45 +++++++++++++++++++++++++++++-
 arch/x86/mm/pageattr.c             | 15 ++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
arch/x86/include/asm/mem_encrypt.h +3 -0
index 616f8e637bc3..3f43cfdd0209 100644
@@ -97,4 +97,7 @@ extern char __start_bss_decrypted[], __end_bss_decrypted[], __start_bss_decrypte
 
 #endif	/* __ASSEMBLY__ */
 
+extern void set_memory_enc_dec_hypercall(unsigned long vaddr,
+					 unsigned long size, bool enc);
+
 #endif	/* __X86_MEM_ENCRYPT_H__ */
arch/x86/mm/mem_encrypt.c +44 -1

80 lines not on this page · raw message

arch/x86/mm/pageattr.c +15 -0
index 4c570612e24e..b1a68879f66d 100644
@@ -25,6 +25,7 @@
 #include <asm/proto.h>
 #include <asm/pat.h>
 #include <asm/set_memory.h>
+#include <asm/mem_encrypt.h>
 
 #include "mm_internal.h"
 
@@ -2019,6 +2020,12 @@ int set_memory_global(unsigned long addr, int numpages)
 				    __pgprot(_PAGE_GLOBAL), 0);
 }
 
+void __attribute__((weak)) set_memory_enc_dec_hypercall(unsigned long addr,
+							unsigned long size,
+							bool enc)
+{
+}
+
 static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
 {
 	struct cpa_data cpa;
@@ -2059,6 +2066,14 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
 	 */
 	cpa_flush(&cpa, 0);
 
+	/*
+	 * When SEV is active, notify hypervisor that a given memory range is mapped
+	 * encrypted or decrypted. Hypervisor will use this information during
+	 * the VM migration.
+	 */
+	if (sev_active())
+		set_memory_enc_dec_hypercall(addr, numpages << PAGE_SHIFT, enc);
+
 	return ret;
 }
 
Signature · 1 line
2.17.1
#24 lore raw
In reply to Singh, Brijesh
Quoted 75 lines · Brijesh

The series add support for AMD SEV guest live migration commands. To protect the confidentiality of an SEV protected guest memory while in transit we need to use the SEV commands defined in SEV API spec [1].

SEV guest VMs have the concept of private and shared memory. Private memory is encrypted with the guest-specific key, while shared memory may be encrypted with hypervisor key. The commands provided by the SEV FW are meant to be used for the private memory only. The patch series introduces a new hypercall. The guest OS can use this hypercall to notify the page encryption status. If the page is encrypted with guest specific-key then we use SEV command during the migration. If page is not encrypted then fallback to default.

The patch adds a new ioctl KVM_GET_PAGE_ENC_BITMAP. The ioctl can be used by the qemu to get the page encrypted bitmap. Qemu can consult this bitmap during the migration to know whether the page is encrypted.

[1] https://developer.amd.com/wp-content/resources/55766.PDF

The series is tested with the Qemu, I am in process of cleaning up the Qemu code and will submit soon.

While implementing the migration I stumbled on the follow question:

- Since there is a guest OS changes required to support the migration, so how do we know whether guest OS is updated? Should we extend KVM capabilities/feature bits to check this?

TODO:
- add an ioctl to build encryption bitmap. The encryption bitmap is built during the guest bootup/execution. We should provide an ioctl so that destination can build the bitmap as it receives the pages.
- reset the bitmap on guest reboot.

The complete tree with patch is available at:
https://github.com/codomania/kvm/tree/sev-migration-rfc-v1

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>

Brijesh Singh (10):
KVM: SVM: Add KVM_SEV SEND_START command
KVM: SVM: Add KVM_SEND_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_SEND_FINISH command
KVM: SVM: Add support for KVM_SEV_RECEIVE_START command
KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_RECEIVE_FINISH command
KVM: x86: Add AMD SEV specific Hypercall3
KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall
KVM: x86: Introduce KVM_GET_PAGE_ENC_BITMAP ioctl
mm: x86: Invoke hypercall when page encryption status is changed

 .../virtual/kvm/amd-memory-encryption.rst     | 116 ++++
 Documentation/virtual/kvm/hypercalls.txt      |  14 +
 arch/x86/include/asm/kvm_host.h               |   3 +
 arch/x86/include/asm/kvm_para.h               |  12 +
 arch/x86/include/asm/mem_encrypt.h            |   3 +
 arch/x86/kvm/svm.c                            | 560 +++++++++++++++++-
 arch/x86/kvm/vmx/vmx.c                        |   1 +
 arch/x86/kvm/x86.c                            |  17 +
 arch/x86/mm/mem_encrypt.c                     |  45 +-
 arch/x86/mm/pageattr.c                        |  15 +
 include/uapi/linux/kvm.h                      |  51 ++
 include/uapi/linux/kvm_para.h                 |   1 +
 12 files changed, 834 insertions(+), 4 deletions(-)
Quoted 3 lines

--
2.17.1

What's the back-of-the-envelope marginal cost of transferring a 16kB region from one host to another? I'm interested in what the end to end migration perf changes look like for this. If you have measured migration perf, I'm interested in that also.

#25 lore raw
Quoted 76 lines · Steve Rutherford
Quoted 75 lines · Brijesh

The series add support for AMD SEV guest live migration commands. To protect the confidentiality of an SEV protected guest memory while in transit we need to use the SEV commands defined in SEV API spec [1].

SEV guest VMs have the concept of private and shared memory. Private memory is encrypted with the guest-specific key, while shared memory may be encrypted with hypervisor key. The commands provided by the SEV FW are meant to be used for the private memory only. The patch series introduces a new hypercall. The guest OS can use this hypercall to notify the page encryption status. If the page is encrypted with guest specific-key then we use SEV command during the migration. If page is not encrypted then fallback to default.

The patch adds a new ioctl KVM_GET_PAGE_ENC_BITMAP. The ioctl can be used by the qemu to get the page encrypted bitmap. Qemu can consult this bitmap during the migration to know whether the page is encrypted.

[1] https://developer.amd.com/wp-content/resources/55766.PDF

The series is tested with the Qemu, I am in process of cleaning up the Qemu code and will submit soon.

While implementing the migration I stumbled on the follow question:

- Since there is a guest OS changes required to support the migration, so how do we know whether guest OS is updated? Should we extend KVM capabilities/feature bits to check this?

TODO:
- add an ioctl to build encryption bitmap. The encryption bitmap is built during the guest bootup/execution. We should provide an ioctl so that destination can build the bitmap as it receives the pages.
- reset the bitmap on guest reboot.

The complete tree with patch is available at:
https://github.com/codomania/kvm/tree/sev-migration-rfc-v1

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>

Brijesh Singh (10):
KVM: SVM: Add KVM_SEV SEND_START command
KVM: SVM: Add KVM_SEND_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_SEND_FINISH command
KVM: SVM: Add support for KVM_SEV_RECEIVE_START command
KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_RECEIVE_FINISH command
KVM: x86: Add AMD SEV specific Hypercall3
KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall
KVM: x86: Introduce KVM_GET_PAGE_ENC_BITMAP ioctl
mm: x86: Invoke hypercall when page encryption status is changed

  .../virtual/kvm/amd-memory-encryption.rst     | 116 ++++
  Documentation/virtual/kvm/hypercalls.txt      |  14 +
  arch/x86/include/asm/kvm_host.h               |   3 +
  arch/x86/include/asm/kvm_para.h               |  12 +
  arch/x86/include/asm/mem_encrypt.h            |   3 +
  arch/x86/kvm/svm.c                            | 560 +++++++++++++++++-
  arch/x86/kvm/vmx/vmx.c                        |   1 +
  arch/x86/kvm/x86.c                            |  17 +
  arch/x86/mm/mem_encrypt.c                     |  45 +-
  arch/x86/mm/pageattr.c                        |  15 +
  include/uapi/linux/kvm.h                      |  51 ++
  include/uapi/linux/kvm_para.h                 |   1 +
  12 files changed, 834 insertions(+), 4 deletions(-)
Quoted 9 lines
Quoted 3 lines

--
2.17.1

What's the back-of-the-envelope marginal cost of transferring a 16kB region from one host to another? I'm interested in what the end to end migration perf changes look like for this. If you have measured migration perf, I'm interested in that also.

I have not done a complete performance analysis yet! From the qemu QMP prompt (query-migration) I am getting ~8mbps throughput from one host to another (this is with 4kb regions). I have been told that increasing the transfer size from 4kb -> 16kb may not give a huge performance gain because at FW level they still operating on 4kb blocks. There is possibility that future FW updates may give a bit better performance on 16kb size.

-Brijesh

#26 lore raw

Do you mean MiB/s, MB/s or Mb/s? Since you are talking about network speeds, sometimes these get conflated.

I'm guessing you mean MB/s since you are also using 4kb for page size.

Quoted 112 lines · Brijesh
Quoted 1 line · Steve Rutherford

On Wed, Apr 24, 2019 at 9:10 AM Singh, Brijesh <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines

The series add support for AMD SEV guest live migration commands. To

protect the

Quoted 1 line
Quoted 1 line

confidentiality of an SEV protected guest memory while in transit we

need to

Quoted 3 lines
Quoted 3 lines

use the SEV commands defined in SEV API spec [1].

SEV guest VMs have the concept of private and shared memory. Private

memory

Quoted 1 line
Quoted 1 line

is encrypted with the guest-specific key, while shared memory may be

encrypted

Quoted 1 line
Quoted 1 line

with hypervisor key. The commands provided by the SEV FW are meant to

be used

Quoted 1 line
Quoted 1 line

for the private memory only. The patch series introduces a new

hypercall.

Quoted 1 line
Quoted 1 line

The guest OS can use this hypercall to notify the page encryption

status.

Quoted 1 line
Quoted 1 line

If the page is encrypted with guest specific-key then we use SEV

command during

Quoted 3 lines
Quoted 3 lines

the migration. If page is not encrypted then fallback to default.

The patch adds a new ioctl KVM_GET_PAGE_ENC_BITMAP. The ioctl can be

used

Quoted 1 line
Quoted 1 line

by the qemu to get the page encrypted bitmap. Qemu can consult this

bitmap

Quoted 7 lines
Quoted 7 lines

during the migration to know whether the page is encrypted.

[1] https://developer.amd.com/wp-content/resources/55766.PDF

The series is tested with the Qemu, I am in process of cleaning up the Qemu code and will submit soon.

Quoted 8 lines
Quoted 8 lines

While implementing the migration I stumbled on the follow question:

- Since there is a guest OS changes required to support the migration, so how do we know whether guest OS is updated? Should we extend KVM capabilities/feature bits to check this?

TODO:
- add an ioctl to build encryption bitmap. The encryption bitmap is

built during

Quoted 1 line
Quoted 1 line

the guest bootup/execution. We should provide an ioctl so that

destination

Quoted 44 lines
Quoted 44 lines

can build the bitmap as it receives the pages.
- reset the bitmap on guest reboot.

The complete tree with patch is available at:
https://github.com/codomania/kvm/tree/sev-migration-rfc-v1

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>

Brijesh Singh (10):
KVM: SVM: Add KVM_SEV SEND_START command
KVM: SVM: Add KVM_SEND_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_SEND_FINISH command
KVM: SVM: Add support for KVM_SEV_RECEIVE_START command
KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command
KVM: SVM: Add KVM_SEV_RECEIVE_FINISH command
KVM: x86: Add AMD SEV specific Hypercall3
KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall
KVM: x86: Introduce KVM_GET_PAGE_ENC_BITMAP ioctl
mm: x86: Invoke hypercall when page encryption status is changed

  .../virtual/kvm/amd-memory-encryption.rst     | 116 ++++
  Documentation/virtual/kvm/hypercalls.txt      |  14 +
  arch/x86/include/asm/kvm_host.h               |   3 +
  arch/x86/include/asm/kvm_para.h               |  12 +
  arch/x86/include/asm/mem_encrypt.h            |   3 +
  arch/x86/kvm/svm.c                            | 560 +++++++++++++++++-
  arch/x86/kvm/vmx/vmx.c                        |   1 +
  arch/x86/kvm/x86.c                            |  17 +
  arch/x86/mm/mem_encrypt.c                     |  45 +-
  arch/x86/mm/pageattr.c                        |  15 +
  include/uapi/linux/kvm.h                      |  51 ++
  include/uapi/linux/kvm_para.h                 |   1 +
  12 files changed, 834 insertions(+), 4 deletions(-)
Quoted 9 lines
Quoted 3 lines

--
2.17.1

What's the back-of-the-envelope marginal cost of transferring a 16kB region from one host to another? I'm interested in what the end to end migration perf changes look like for this. If you have measured migration perf, I'm interested in that also.

I have not done a complete performance analysis yet! From the qemu QMP prompt (query-migration) I am getting ~8mbps throughput from one host to another (this is with 4kb regions). I have been told that increasing the transfer size from 4kb -> 16kb may not give a huge performance gain because at FW level they still operating on 4kb blocks. There is possibility that future FW updates may give a bit better performance on 16kb size.

-Brijesh

#27 lore raw
Quoted 2 lines · Steve Rutherford

Do you mean MiB/s, MB/s or Mb/s? Since you are talking about network speeds, sometimes these get conflated.

It's megabits/sec. The QMP query-migration command shows the throughput in Mbits/s. It includes PSP command execution and the network write. Most of the time is spent in PSP FW. I have not performed raw PSP command benchmark yet but I believe SEV FW 0.17 may reproduce up to 12Mbits/s. I will update thread after I finish the further performance breakdown.

-Brijesh