Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
Subsystems (12)
Re: [PATCH RFC v8 52/56] ccp: Add support to decrypt the page
In reply to Michael RothQuoted 5 lines · On Mon, 20 Feb 2023 12:38:43 -0600 Michael Roth <[email protected]> wrote:
From: Brijesh Singh <[email protected]>
Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit.
What kinds of check will be applied from firmware when VMM decrypts this page? I suppose there has to be kinda mechanism to prevent VMM to decrypt any page in the guest. It would be nice to have some introduction about it in the comments.
Quoted 105 lines
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: minor commit fixups]Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 32 ++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 22 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-)drivers/crypto/ccp/sev-dev.c +32
-0@@ -2017,6 +2017,38 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +struct sev_data_snp_dbg data = {0 }; +struct sev_device *sev; +int ret; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; + + data.gctx_paddr = sme_me_mask | (gctx_pfn <<PAGE_SHIFT ); + data.src_addr = sme_me_mask | (src_pfn <<PAGE_SHIFT ); + data.dst_addr = sme_me_mask | (dst_pfn <<PAGE_SHIFT ); + +/* The destination page must be in the firmware state. */ +if (rmp_mark_pages_firmware (data.dst_addr,1 , false)) +return -EIO ; + + ret =sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , &data, error); + +/* Restore the page state */ +if (snp_reclaim_pages (data.dst_addr,1 , false)) + ret = -EIO ; + +return ret; +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt_page); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data,unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) {include/linux/psp-sev.h +20
-2@@ -710,7 +710,6 @@ struct sev_data_snp_dbg {u64 gctx_paddr;/* In */ u64 src_addr;/* In */ u64 dst_addr;/* In */ -u32 len;/* In */ } __packed;/** @@ -913,13 +912,27 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error);* @error: SEV command return code * * Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int sev_do_cmd (int cmd,void *data,int *psp_ret); + +/** +* snp_guest_dbg_decrypt_page - perform SEV SNP_DBG_DECRYPT command +* +* @sev_ret: sev command return code +* +* Returns: * 0 if the SEV successfully processed the command * -%ENODEV if the SEV device is not available * -%ENOTSUPP if the SEV does not support SEV * -%ETIMEDOUT if the SEV command timed out * -%EIO if the SEV returned a non-zero return code */ -int sev_do_cmd (int cmd,void *data,int *psp_ret); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len);void *snp_alloc_firmware_page (gfp_t mask);@@ -987,6 +1000,11 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_Pvoid snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +static inline int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +return -ENODEV ; +} +static inline void *snp_alloc_firmware_page (gfp_t mask) {return NULL ;
Re: [PATCH RFC v8 53/56] KVM: SVM: Make VMSAVE target area memory allocation SNP safe
In reply to Michael RothQuoted 11 lines · On Mon, 20 Feb 2023 12:38:44 -0600 Michael Roth <[email protected]> wrote:
From: Ashish Kalra <[email protected]>
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of the VMSAVE target page.
When SEV-SNP is globally enabled, the CPU marks the VMSAVE target page as "InUse" while the VMSAVE instruction is executing. If another CPU writes to a different page in the same 2MB region while the VMSAVE is executing, the CPU will throw an RMP violation #PF.
Quoted 5 lines
Use the snp safe generic allocator for allocating the VMSA target page which will ensure that the page returned is not a hugepage, as it is already being used for the allocating the VMCB, VMSA and AVIC backing page.
This should be merged with patch where implements the snp_safe_alloc_page().
Quoted 22 lines
Co-developed-by Marc Orr <[email protected]>Signed-off-by Marc Orr <[email protected]>Reported-by Alper Gun <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/svm/svm.c +1
-1@@ -665,7 +665,7 @@ static int svm_cpu_init(int cpu)int ret = -ENOMEM ;memset (sd,0 ,sizeof (struct svm_cpu_data )); - sd->save_area =alloc_page (GFP_KERNEL | __GFP_ZERO); + sd->save_area =snp_safe_alloc_page (NULL );if (!sd->save_area)return ret;
Quoted 11 lines · On Wed, 1 Mar 2023 08:56:05 -0800 Dave Hansen <[email protected]> wrote:
Quoted 7 lines · Michael Roth
The RMP check is enforced as soon as SEV-SNP is enabled. Not every memory access requires an RMP check. In particular, the read accesses from the hypervisor do not require RMP checks because the data confidentiality is already protected via memory encryption. When hardware encounters an RMP checks failure, it raises a page-fault exception. If RMP check failure is due to the page-size mismatch, then split the large page to resolve the fault.
What does this all _mean_?
Unlike TDX which implements secure EPT to hold the restricted memory mapping, SEV-SNP is still using one NPT (similar to Intel EPT) while adding another level of HW-enforced check controlled by the "RMP" table. Similar to TDX, it has firmware calls to modify the attributes in the RMP table to achieve isolation and shared-private memory conversion.
The purpose and structure of RMP is quite similar to the PAMT table in TDX from the perspective of managing the per-page attributes. Each system page has a collection of attribute bits. But TDX only uses the PAMT as metadata as it has a separate secure EPT to achieve HW-enforced check.
The RMP memory access checks has its own schematics. E.g. data write, page table access from VMM will be checked, but data read is not, mostly I guess, due to performance consideration. More details can be found from Table 15-39. RMP Memory Access Checks in [1].
Quoted 1 line
When does the kernel need to care about a "page-size mismatch"?
The RMP table has the ability to describe a large page (similar to a large page with a description of large-page PAMT entry in TDX that can be demoted via TDX SEAMCALLs). E.g. 2MB page.
When the userspace sets the memory attribute of a GFN range through the restricted memory ioctl, the sev logic (sev_update_mem_attr() in PATCH 48, to be precise) will try to build a large page description in the RMP table if the PFNs are continuous. When kernel mm breaks the the large page due to THP, KVM updates the NPT accordingly.
Then there will be a page-size mismatch between NPT and RMP. It will be resolved by a RMP fault later. Kinda of lazy sync.
Re: [PATCH RFC v8 48/56] KVM: SVM: Add SNP-specific handling for memory attribute updates
In reply to Michael Rotharch/x86/kvm/svm/sev.c void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) · line 4466
Quoted 11 lines · Michael Roth
arch/x86/kvm/svm/sev.c +11
-0+/* +* TODO: The RMP entry's hugepage bit is ignored for +* shared/unassigned pages. Either handle looping through each +* sub-page as part of snp_make_page_shared(), or remove the +* level argument. +*/ +if (op ==SNP_PAGE_STATE_PRIVATE && order && +IS_ALIGNED (gfn,1 << order) && (gfn + (1 << order)) <= end) { + level =order_to_level (order); + npages =1 << order; + }
That's a wee bit obtuse.
First of all, I assume that the 'RFC' is because of these TODOs and they won't survive to the point when you ask for this to be merged.
BTW, what keeps the restrictedmem_get_page() offset and the gfn aligned?
Let's start with this:
arch/x86/kvm/svm/sev.c void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) · line 4413
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+static inline u8 order_to_level (int order) +{ +BUILD_BUG_ON (KVM_MAX_HUGEPAGE_LEVEL >PG_LEVEL_1G ); + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_1G )) +return PG_LEVEL_1G ; + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_2M )) +return PG_LEVEL_2M ; + +return PG_LEVEL_4K ; +}
Right now, 'order' comes only from restrictedmem_get_page(), which I dug out of:
That order is *only* filled in by THPs. That makes the PG_LEVEL_1G stuff here kinda silly. I guess it might be seen as thorough, but it's dead code. I'd probably just make this work on order==9 || order==0 and warn on anything else.
I'd also highly recommend some comments about how racy this all is. I guess it probably works, but it would be good to add some comments about page splits and collapsing.
It's also not obvious why this only cares about private pages.
Anyway, this is the exact kind of thing where I really like a well-commented helper:
bool can_install_large_rmp_entry(gfn, order)
{
// small pages, blah blah
if (!order)
return false;
// The region being updated must be aligned
if (!IS_ALIGNED(gfn, 1 << order))
return false;
// ... and fit
if (gfn + (1 << order)) > end)
return false;
return true;
}Which gets used like this:
if (op == SNP_PAGE_STATE_PRIVATE &&
can_install_large_rmp_entry(gfn, order)) {
level = ...
}Quoted 5 lines · Zhi Wang
When the userspace sets the memory attribute of a GFN range through the restricted memory ioctl, the sev logic (
sev_update_mem_attr()in PATCH 48, to be precise) will try to build a large page description in the RMP table if the PFNs are continuous. When kernel mm breaks the the large page due to THP, KVM updates the NPT accordingly.
Gah, this really confused me.
It's *NOT* looking for contiguous PFNs. It's looking for a restrictedmem THP, which really is something different. Restrictedmem THPs have contiguous PFNs, but not all contiguous PFNs will result in trying to build a large page.
Anyway, I'll reply over to the other patch.
But, either way, I'd appreciate this kind of summary in the changelogs and probably a comment or two:
The RMP needs to be consistent with the contents of the NPT. KVM updates the NPT but will neglect to update the RMP. It is updated in response to faults when RMP and NPT get out of sync.
Right?
BTW, why doesn't KVM just update the RMP? Why bother taking the fault?
Re: [PATCH RFC v8 54/56] x86/sev: Add KVM commands for instance certs
In reply to Michael RothOn Mon, 20 Feb 2023 12:38:45 -0600
Michael Roth <[email protected]> wrote:
In the host-wide certificates installing/uninstalling, a lock is to prevent the racing between cert upload/download path and the handling of guest request path.
Guess we need a similar lock to prevent the racing between snp_{set,get}_instance_certs() and snp_handle_ext_guest_request().
This patch and PATCH 27 are focusing on cert from different sources. It would be better to abstract. With the functions and data structure of cert, the cert installing/uninstalling, checks, expectation of the output buffer can be unified. Then we don't need mostly duplicate routines in two different paths.
Quoted 45 lines
From: Dionna Glaze <[email protected]>
The
/dev/sevdevice has the ability to store host-wide certificates for the key used by the AMD-SP for SEV-SNP attestation report signing, but for hosts that want to specify additional certificates that are specific to the image launched in a VM, a different way is needed to communicate those certificates.Add two new KVM ioctl to handle this: KVM_SEV_SNP_{GET,SET}_CERTS
The certificates that are set with this command are expected to follow the same format as the host certificates, but that format is opaque to the kernel.
The new behavior for custom certificates is that the extended guest request command will now return the overridden certificates if they were installed for the instance. The error condition for a too small data buffer is changed to return the overridden certificate data size if there is an overridden certificate set installed.
Setting a 0 length certificate returns the system state to only return the host certificates on an extended guest request.
Also increase the
SEV_FW_BLOB_MAX_SIZEanother 4K page to allow space for an extra certificate.Cc Tom Lendacky <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Dionna Glaze <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: remove used of "we" and "this patch" in commit log]Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 111 ++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 +++++ 4 files changed, 123 insertions(+), 3 deletions(-)arch/x86/kvm/svm/sev.c +0
-0
arch/x86/kvm/svm/sev.c static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2095
Quoted 7 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2089,6 +2089,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free; sev->snp_certs_data = certs_data; + sev->snp_certs_len =0 ;return context;
Better to move the fix to PATCH 45.
Quoted 186 lines
arch/x86/kvm/svm/sev.c +108
-2@@ -2404,6 +2405,86 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int snp_get_instance_certs (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct kvm_sev_snp_get_certs params; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data, +sizeof (params))) +return -EFAULT ; + +/* No instance certs set. */ +if (!sev->snp_certs_len) +return -ENOENT ; + +if (params.certs_len < sev->snp_certs_len) { +/* Output buffer too small. Return the required size. */ + params.certs_len = sev->snp_certs_len; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms, +sizeof (params))) +return -EFAULT ; + +return -EINVAL ; + } + +if (copy_to_user ((void __user *)(uintptr_t )params.certs_uaddr, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + +return 0 ; +} + +static int snp_set_instance_certs (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +unsigned long length =SEV_FW_BLOB_MAX_SIZE ; +void *to_certs = sev->snp_certs_data; +struct kvm_sev_snp_set_certs params; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data, +sizeof (params))) +return -EFAULT ; + +if (params.certs_len >SEV_FW_BLOB_MAX_SIZE ) +return -EINVAL ; + +/* +* Setting a length of 0 is the same as "uninstalling" instance- +* specific certificates. +*/ +if (params.certs_len ==0 ) { + sev->snp_certs_len =0 ; +return 0 ; + } + +/* Page-align the length */ + length = (params.certs_len +PAGE_SIZE -1 ) &PAGE_MASK ; + +if (copy_from_user (to_certs, + (void __user *)(uintptr_t )params.certs_uaddr, + params.certs_len)) { +return -EFAULT ; + } + + sev->snp_certs_len = length; + +return 0 ; +} +int sev_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -2503,6 +2584,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)case KVM_SEV_SNP_LAUNCH_FINISH : r =snp_launch_finish (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_GET_CERTS : + r =snp_get_instance_certs (kvm, &sev_cmd); +break ; +case KVM_SEV_SNP_SET_CERTS : + r =snp_set_instance_certs (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;@@ -3550,8 +3637,28 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpif (rc) goto unlock; - rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, - &data_npages, &err); +/* +* If the VMM has overridden the certs, then change the error message +* if the size is inappropriate for the override. Otherwise, use a +* regular guest request and copy back the instance certs. +*/ +if (sev->snp_certs_len) { +if ((data_npages <<PAGE_SHIFT ) < sev->snp_certs_len) { + rc = -EINVAL ; + err =SNP_GUEST_REQ_INVALID_LEN ; + goto datalen; + } + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &req, + (int *)&err); + }else { + rc =snp_guest_ext_guest_request (&req, + (unsigned long )sev->snp_certs_data, + &data_npages, &err); + } +datalen: +if (sev->snp_certs_len) + data_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (rc) {/* * If buffer length is small then return the expected arch/x86/kvm/svm/svm.h +1
-0@@ -94,6 +94,7 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ void *snp_certs_data; +unsigned int snp_certs_len;/* Size of instance override for certs */ struct mutex guest_req_lock;/* Lock for guest request handling */ u64 sev_features;/* Features set at VMSA creation */ include/linux/psp-sev.h +1
-1@@ -22,7 +22,7 @@#define __psp_pa (x) __pa(x)#endif -#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ +#define SEV_FW_BLOB_MAX_SIZE 0x5000/* 20KB */ /** * SEV platform state include/uapi/linux/kvm.h +12
-0@@ -1928,6 +1928,8 @@ enum sev_cmd_id {KVM_SEV_SNP_LAUNCH_START ,KVM_SEV_SNP_LAUNCH_UPDATE ,KVM_SEV_SNP_LAUNCH_FINISH , +KVM_SEV_SNP_GET_CERTS , +KVM_SEV_SNP_SET_CERTS , KVM_SEV_NR_MAX, };@@ -2075,6 +2077,16 @@ struct kvm_sev_snp_launch_finish {__u8 pad[6 ]; }; +struct kvm_sev_snp_get_certs { +__u64 certs_uaddr; +__u64 certs_len; +}; + +struct kvm_sev_snp_set_certs { +__u64 certs_uaddr; +__u64 certs_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)
Re: [PATCH RFC v8 54/56] x86/sev: Add KVM commands for instance certs
Quoted 11 lines
arch/x86/kvm/svm/sev.c static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2095
Quoted 8 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2089,6 +2089,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free; sev->snp_certs_data = certs_data; + sev->snp_certs_len =0 ;return context;Better to move the fix to PATCH 45.
This part isn't a fix, but part of the implementation since snp_certs_len is added in this patch here
Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/svm/svm.h +1
-0@@ -94,6 +94,7 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ void *snp_certs_data; +unsigned int snp_certs_len;/* Size of instance override for certs */ struct mutex guest_req_lock;/* Lock for guest request handling */ u64 sev_features;/* Features set at VMSA creation */
Signature · 1 line
-Dionna Glaze, PhD (she/her)
Re: [PATCH RFC v8 52/56] ccp: Add support to decrypt the page
In reply to Zhi WangHi Mike, Zhi,
Quoted 5 lines · Zhi Wang
Quoted 2 lines · On Mon, 20 Feb 2023 12:38:43 -0600 Michael Roth <[email protected]> wrote:
From: Brijesh Singh <[email protected]>
Quoted 9 lines
Quoted 3 lines
Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit.
What kinds of check will be applied from firmware when VMM decrypts this page? I suppose there has to be kinda mechanism to prevent VMM to decrypt any page in the guest. It would be nice to have some introduction about it in the comments.
The SNP ABI spec says (section 8.27.2 SNP_DBG_DECRYPT):
The firmware checks that the guest's policy allows debugging. If not, the firmware returns POLICY_FAILURE.
and in the Guest Policy (section 4.3):
Bit 19 - DEBUG
0: Debugging is disallowed.
1: Debugging is allowed.
In the kernel, that firmware error code is defined as SEV_RET_POLICY_FAILURE.
Quoted 26 lines
Quoted 26 lines
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: minor commit fixups]Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 32 ++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 22 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-)drivers/crypto/ccp/sev-dev.c +9
-0@@ -2017,6 +2017,38 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +struct sev_data_snp_dbg data = {0 }; +struct sev_device *sev; +int ret; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; +
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c int sev_guest_df_flush(int *error) · line 2036
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+ sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; + + data.gctx_paddr = sme_me_mask | (gctx_pfn <<PAGE_SHIFT ); + data.src_addr = sme_me_mask | (src_pfn <<PAGE_SHIFT ); + data.dst_addr = sme_me_mask | (dst_pfn <<PAGE_SHIFT );
I guess this works, but I wonder why we need to turn on sme_me_mask on teh dst_addr. I thought that the firmware decrypts the guest page (src_addr) to a plaintext page. Couldn't find this requirement in the SNP spec.
Quoted 20 lines
Quoted 20 lines
drivers/crypto/ccp/sev-dev.c +15
-0+ +/* The destination page must be in the firmware state. */ +if (rmp_mark_pages_firmware (data.dst_addr,1 , false)) +return -EIO ; + + ret =sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , &data, error); + +/* Restore the page state */ +if (snp_reclaim_pages (data.dst_addr,1 , false)) + ret = -EIO ; + +return ret; +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt_page); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data,unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) {include/linux/psp-sev.h +0
-0
Quoted 8 lines
include/linux/psp-sev.h struct sev_data_snp_dbg { · line 713
Quoted 8 lines
include/linux/psp-sev.h +0
-1@@ -710,7 +710,6 @@ struct sev_data_snp_dbg {u64 gctx_paddr;/* In */ u64 src_addr;/* In */ u64 dst_addr;/* In */ -u32 len;/* In */ } __packed;
The comment above this ^^^ struct still lists the 'len' field, and also calls the first field 'handle' instead of 'gctx_paddr'.
Also - why is this change happening in this patch? Why was the incorrect 'len' field added in the first place in "[PATCH RFC v8 20/56] crypto:ccp: Define the SEV-SNP commands" ? (the comment fixes should probably go there too).
Quoted 12 lines
include/linux/psp-sev.h int sev_guest_decommission(struct sev_data_decommission *data, int *error); · line 920
Quoted 12 lines
include/linux/psp-sev.h +6
-0/** @@ -913,13 +912,27 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error);* @error: SEV command return code * * Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/
I think that if the word 'sev' would be 'SEV' in this comment, the diff will be a bit less misleading (basically this patch should not introduce changes to sev_do_cmd).
-Dov
Quoted 32 lines
Quoted 31 lines
include/linux/psp-sev.h +14
-1+int sev_do_cmd (int cmd,void *data,int *psp_ret); + +/** +* snp_guest_dbg_decrypt_page - perform SEV SNP_DBG_DECRYPT command +* +* @sev_ret: sev command return code +* +* Returns: * 0 if the SEV successfully processed the command * -%ENODEV if the SEV device is not available * -%ENOTSUPP if the SEV does not support SEV * -%ETIMEDOUT if the SEV command timed out * -%EIO if the SEV returned a non-zero return code */ -int sev_do_cmd (int cmd,void *data,int *psp_ret); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len);void *snp_alloc_firmware_page (gfp_t mask);@@ -987,6 +1000,11 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_Pvoid snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +static inline int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +return -ENODEV ; +} +static inline void *snp_alloc_firmware_page (gfp_t mask) {return NULL ;
Re: [PATCH RFC v8 54/56] x86/sev: Add KVM commands for instance certs
In reply to Dionna Amalie GlazeQuoted 6 lines · On Wed, 1 Mar 2023 17:41:11 -0800 Dionna Amalie Glaze <[email protected]>…
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2089,6 +2089,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free; sev->snp_certs_data = certs_data; + sev->snp_certs_len =0 ;
Quoted 9 lines
Quoted 5 lines
arch/x86/kvm/svm/sev.c static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2095
Quoted 2 lines
arch/x86/kvm/svm/sev.c +0
-0return context;Better to move the fix to PATCH 45.
This part isn't a fix, but part of the implementation since
snp_certs_lenis added in this patch here
I see. My bad. Was thinking it was the snp_serts_len in the global sev as they has the same name.
Quoted 14 lines
Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/svm/svm.h +1
-0@@ -94,6 +94,7 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ void *snp_certs_data; +unsigned int snp_certs_len;/* Size of instance override for certs */ struct mutex guest_req_lock;/* Lock for guest request handling */ u64 sev_features;/* Features set at VMSA creation */
Re: [PATCH RFC v8 54/56] x86/sev: Add KVM commands for instance certs
In reply to Michael RothQuoted 40 lines · Michael Roth
From: Dionna Glaze <[email protected]>
The
/dev/sevdevice has the ability to store host-wide certificates for the key used by the AMD-SP for SEV-SNP attestation report signing, but for hosts that want to specify additional certificates that are specific to the image launched in a VM, a different way is needed to communicate those certificates.Add two new KVM ioctl to handle this: KVM_SEV_SNP_{GET,SET}_CERTS
The certificates that are set with this command are expected to follow the same format as the host certificates, but that format is opaque to the kernel.
The new behavior for custom certificates is that the extended guest request command will now return the overridden certificates if they were installed for the instance. The error condition for a too small data buffer is changed to return the overridden certificate data size if there is an overridden certificate set installed.
Setting a 0 length certificate returns the system state to only return the host certificates on an extended guest request.
Also increase the
SEV_FW_BLOB_MAX_SIZEanother 4K page to allow space for an extra certificate.Cc Tom Lendacky <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Dionna Glaze <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: remove used of "we" and "this patch" in commit log]Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 111 ++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 +++++ 4 files changed, 123 insertions(+), 3 deletions(-)
[...]
include/linux/psp-sev.h line 25
Quoted 10 lines
include/linux/psp-sev.h +1
-1@@ -22,7 +22,7 @@#define __psp_pa (x) __pa(x)#endif -#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ +#define SEV_FW_BLOB_MAX_SIZE 0x5000/* 20KB */
This change should be removed (it was also discussed in v7). If I understand correctly, 16KB is a limit of the PSP.
-Dov
Quoted 3 lines
include/linux/psp-sev.h +0
-0/** * SEV platform state
Tom Lendacky
maintainerRe: [PATCH RFC v8 52/56] ccp: Add support to decrypt the page
In reply to Dov MurikQuoted 64 lines · Dov Murik
Hi Mike, Zhi,
Quoted 5 lines · Zhi Wang
Quoted 2 lines · On Mon, 20 Feb 2023 12:38:43 -0600 Michael Roth <[email protected]> wrote:
From: Brijesh Singh <[email protected]>
Quoted 9 lines
Quoted 3 lines
Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit.
What kinds of check will be applied from firmware when VMM decrypts this page? I suppose there has to be kinda mechanism to prevent VMM to decrypt any page in the guest. It would be nice to have some introduction about it in the comments.
The SNP ABI spec says (section 8.27.2
SNP_DBG_DECRYPT):The firmware checks that the guest's policy allows debugging. If not, the firmware returns
POLICY_FAILURE.and in the Guest Policy (section 4.3):
Bit 19 - DEBUG
0: Debugging is disallowed.
1: Debugging is allowed.In the kernel, that firmware error code is defined as
SEV_RET_POLICY_FAILURE.Quoted 31 lines
Quoted 31 lines
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: minor commit fixups]Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 32 ++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 22 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-)drivers/crypto/ccp/sev-dev.c +14
-0@@ -2017,6 +2017,38 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +struct sev_data_snp_dbg data = {0 }; +struct sev_device *sev; +int ret; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; +
Quoted 8 lines
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c int sev_guest_df_flush(int *error) · line 2036
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+ data.gctx_paddr = sme_me_mask | (gctx_pfn <<PAGE_SHIFT ); + data.src_addr = sme_me_mask | (src_pfn <<PAGE_SHIFT ); + data.dst_addr = sme_me_mask | (dst_pfn <<PAGE_SHIFT );I guess this works, but I wonder why we need to turn on
sme_me_maskon tehdst_addr. I thought that the firmware decrypts the guest page (src_addr) to a plaintext page. Couldn't find this requirement in the SNP spec.
This sme_me_mask tells the firmware how to access the host memory (similar to how DMA uses sme_me_mask when supplying addresses to devices under SME). This needs to match the pagetable mapping being used by the host, otherwise the contents will appears as ciphertext to the host if they are not in sync. Since the default pagetable mapping is encrypted, the sme_me_mask bit must be provided on the destination address. So it is not a spec requirement, but an SME implementation requirement.
Thanks,
Tom
Quoted 92 lines
Quoted 22 lines
Quoted 22 lines
drivers/crypto/ccp/sev-dev.c +15
-0+ +/* The destination page must be in the firmware state. */ +if (rmp_mark_pages_firmware (data.dst_addr,1 , false)) +return -EIO ; + + ret =sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , &data, error); + +/* Restore the page state */ +if (snp_reclaim_pages (data.dst_addr,1 , false)) + ret = -EIO ; + +return ret; +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt_page); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data,unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) {include/linux/psp-sev.h +0
-0Quoted 8 lines
include/linux/psp-sev.h struct sev_data_snp_dbg { · line 713
Quoted 8 lines
include/linux/psp-sev.h +0
-1@@ -710,7 +710,6 @@ struct sev_data_snp_dbg {u64 gctx_paddr;/* In */ u64 src_addr;/* In */ u64 dst_addr;/* In */ -u32 len;/* In */ } __packed;The comment above this ^^^ struct still lists the 'len' field, and also calls the first field 'handle' instead of '
gctx_paddr'.Also - why is this change happening in this patch? Why was the incorrect 'len' field added in the first place in "[PATCH RFC v8 20/56] crypto:ccp: Define the SEV-SNP commands" ? (the comment fixes should probably go there too).
Quoted 12 lines
include/linux/psp-sev.h int sev_guest_decommission(struct sev_data_decommission *data, int *error); · line 920
Quoted 12 lines
include/linux/psp-sev.h +6
-0/** @@ -913,13 +912,27 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error);* @error: SEV command return code * * Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ I think that if the word 'sev' would be 'SEV' in this comment, the diff will be a bit less misleading (basically this patch should not introduce changes to
sev_do_cmd).-Dov
Quoted 32 lines
Quoted 31 lines
include/linux/psp-sev.h +14
-1+int sev_do_cmd (int cmd,void *data,int *psp_ret); + +/** +* snp_guest_dbg_decrypt_page - perform SEV SNP_DBG_DECRYPT command +* +* @sev_ret: sev command return code +* +* Returns: * 0 if the SEV successfully processed the command * -%ENODEV if the SEV device is not available * -%ENOTSUPP if the SEV does not support SEV * -%ETIMEDOUT if the SEV command timed out * -%EIO if the SEV returned a non-zero return code */ -int sev_do_cmd (int cmd,void *data,int *psp_ret); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len);void *snp_alloc_firmware_page (gfp_t mask);@@ -987,6 +1000,11 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_Pvoid snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +static inline int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +return -ENODEV ; +} +static inline void *snp_alloc_firmware_page (gfp_t mask) {return NULL ;
Re: [PATCH RFC v8 52/56] ccp: Add support to decrypt the page
Quoted 76 lines · Tom Lendacky
Quoted 66 lines · Dov Murik
Hi Mike, Zhi,
Quoted 9 lines · Zhi Wang
Quoted 5 lines · On Mon, 20 Feb 2023 12:38:43 -0600 Michael Roth <[email protected]> wrote:
From: Brijesh Singh <[email protected]>
Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit.
Quoted 6 lines
What kinds of check will be applied from firmware when VMM decrypts this page? I suppose there has to be kinda mechanism to prevent VMM to decrypt
any page in the guest. It would be nice to have some introduction about it in the comments.The SNP ABI spec says (section 8.27.2
SNP_DBG_DECRYPT):The firmware checks that the guest's policy allows debugging. If not, the firmware returns
POLICY_FAILURE.and in the Guest Policy (section 4.3):
Bit 19 - DEBUG
0: Debugging is disallowed.
1: Debugging is allowed.In the kernel, that firmware error code is defined as
SEV_RET_POLICY_FAILURE.Quoted 32 lines
Quoted 32 lines
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: minor commit fixups]Signed-off-by Michael Roth <[email protected]>---
drivers/crypto/ccp/sev-dev.c| 32 ++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 22 ++++++++++++++++++++--2 files changed, 52 insertions(+), 2 deletions(-)drivers/crypto/ccp/sev-dev.c +0
-0@@ -2017,6 +2017,38 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush);+int
snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error)drivers/crypto/ccp/sev-dev.c +13
-0+{ + struct sev_data_snp_dbg data = {0}; + struct sev_device *sev; + int ret; + + if (!psp_master || !psp_master->sev_data) + return -ENODEV; + + sev = psp_master->sev_data; + + if (!sev->snp_initialized) + return -EINVAL; +Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
+ data.gctx_paddr =
sme_me_mask| (gctx_pfn<<PAGE_SHIFT); + data.src_addr =sme_me_mask| (src_pfn<<PAGE_SHIFT); + data.dst_addr =sme_me_mask| (dst_pfn<<PAGE_SHIFT);I guess this works, but I wonder why we need to turn on
sme_me_maskon teh dst_addr. I thought that the firmware decrypts the guest page (src_addr) to a plaintext page. Couldn't find this requirement in the SNP spec.
Quoted 8 lines
This
sme_me_masktells the firmware how to access the host memory (similar to how DMA usessme_me_maskwhen supplying addresses to devices under SME). This needs to match the pagetable mapping being used by the host, otherwise the contents will appears as ciphertext to the host if they are not in sync. Since the default pagetable mapping is encrypted, thesme_me_maskbit must be provided on the destination address. So it is not a spec requirement, but an SME implementation requirement.
Ah, OK, that's clear now. Thanks Tom.
-Dov
Re: [PATCH RFC v8 08/56] KVM: SEV: Rename sev_{pin,unpin}_memory
In reply to Michael RothQuoted 6 lines · Michael Roth
From: Nikunj A Dadhania <[email protected]>
Rename sev_{pin|unpin}_memory to sev_memory_{get|put}_pages. Apart from pinning the pages,
sev_pin_memoryalso populates the pages array which is used by its callers. SEV guest using restricted memfd do not to pin the memory but will require the pages array to be populated.
^need to?
Quoted 6 lines
Rename the function appropriately.
No functional change intended.
Signed-off-by Nikunj A Dadhania <[email protected]>Signed-off-by Michael Roth <[email protected]>
Re: [PATCH RFC v8 09/56] KVM: SEV: Handle memory backed by restricted memfd
In reply to Michael RothQuoted 5 lines · Michael Roth
From: Nikunj A Dadhania <[email protected]>
Do not pin the guest memory backed by a restrictedmem backend, as pages in the restrictedmem are already pinned. Instead, populate the pages array for these guests using the already-pinned pages provided by
IIUC the "already pinned" became "effectively unmovable and unevictable" since the earlier versions, so that would be more accurate now?
Quoted 5 lines
arch/x86/kvm/svm/sev.c +5
-0+static int sev_private_mem_get_pages (struct kvm *kvm,unsigned long addr, +unsigned long size,unsigned long npages, +struct page **pages) +{ + return kvm_vm_do_hva_range_op(kvm, addr, addr + size,
arch/x86/kvm/svm/sev.c static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 425
Quoted 10 lines
arch/x86/kvm/svm/sev.c +7
-0+ sev_private_mem_get_pages_handler, pages); +} +/* * Legacy SEV guest pin the pages and return the array populated with pinned * pages. +* +* SEV guests using restricted memfd backend, pages are already marked as +* unmovable and unevictable. Populate the pages array for these guests using +* restrictedmem get_pfn.
Right.
Re: [PATCH RFC v8 12/56] x86/sev: Add RMP entry lookup helpers
In reply to Michael RothQuoted 10 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The
snp_lookup_page_in_rmptable()can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see
https://bugzilla.kernel.org/attachment.cgi?id=296015.Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Michael Roth <[email protected]>
Quoted 6 lines
---
+/*
+ * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned,
+ * and -errno if there is no corresponding RMP entry.
+ */
Hmm IMHO the kernel's idiomatic way is to return 0 on "success" and I'd assume the more intuitive expectation of success here if the entry is assigned? The various callers seem to differ though so I guess it depends on context. Some however don't distinguish their "failure" from an ERR and maybe they should, at least for the purposes of the various printks?
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+int snp_lookup_rmpentry (u64 pfn,int *level) +{ +struct rmpentry *e; + + e =__snp_lookup_rmpentry (pfn, level); +if (IS_ERR (e)) +return PTR_ERR (e); + +return !!rmpentry_assigned (e); +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);
Re: [PATCH RFC v8 17/56] x86/fault: Add support to handle the RMP fault for user address
In reply to Michael RothQuoted 43 lines · Michael Roth
arch/x86/mm/fault.c +43
-0+static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pgd_t *pgd; +pte_t *pte; +u64 pfn; + + pgd =__va (read_cr3_pa ()); + pgd +=pgd_index (address); + + pte =lookup_address_in_pgd (pgd, address, &level); + +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/ +if (!pte || !pte_present (*pte)) +return RMP_PF_UNMAP ; + +/* +* RMP page fault handler follows this algorithm: +* 1. Compute the pfn for the 4kb page being accessed +* 2. Read that RMP entry -- If it is assigned then kill the process +* 3. Otherwise, check the level from the host page table +* If level=PG_LEVEL_4K then the page is already smashed +* so just retry the instruction +* 4. If level=PG_LEVEL_2M/1G, then the host page needs to be split +*/ + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) + pfn = pfn |PFN_DOWN (address & (page_level_size (level) -1 )); + +/* +* If its a guest private page, then the fault cannot be resolved. +* Send a SIGBUS to terminate the process. +* +* As documented in APM vol3 pseudo-code for RMPUPDATE, when the 2M range +* is covered by a valid (Assigned=1) 2M entry, the middle 511 4k entries +* also have Assigned=1. This means that if there is an access to a page
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1322
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+* which happens to lie within an Assigned 2M entry, the 4k RMP entry +* will also have Assigned=1. Therefore, the kernel should see that +* the page is not a valid page and the fault cannot be resolved. +*/ +if (snp_lookup_rmpentry (pfn, &rmp_level)) { +pr_info ("Fatal RMP page fault, terminating process, entry assigned for pfn 0x%llx\n" , + pfn); +do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return RMP_PF_RETRY ; + }
WRT my reply to 12/56, for example here it might be useful to distinguish the rmp being assigned from an error of snp_lookup_rmpentry()?
Quoted 12 lines
arch/x86/mm/fault.c +11
-0+ +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return RMP_PF_SPLIT ; + +return RMP_PF_RETRY ; +} +/*
Re: [PATCH RFC v8 23/56] crypto: ccp: Introduce snp leaked pages list
In reply to Michael RothQuoted 33 lines · Michael Roth
From: Ashish Kalra <[email protected]>
Pages are unsafe to be released back to the page-allocator, if they have been transitioned to
firmware/gueststate and can't be reclaimed or transitioned back to hypervisor/shared state. In this case add them to an internal leaked pages list to ensure that they are not freed or touched/accessed to cause fatal page faults.Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 28 ++++++++++++++++++++++++++++ include/linux/psp-sev.h | 8 ++++++++ 2 files changed, 36 insertions(+)drivers/crypto/ccp/sev-dev.c +6
-0@@ -42,6 +42,12 @@static DEFINE_MUTEX (sev_cmd_mutex);static struct sev_misc_dev *misc_dev; +/* list of pages which are leaked and cannot be reclaimed */ +static LIST_HEAD (snp_leaked_pages_list); +static DEFINE_SPINLOCK (snp_leaked_pages_list_lock); + +static atomic_long_t snp_nr_leaked_pages =ATOMIC_LONG_INIT (0 ); +static int psp_cmd_timeout =100 ;module_param (psp_cmd_timeout, int,0644 );MODULE_PARM_DESC (psp_cmd_timeout," default timeout value, in seconds, for PSP commands" );
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 197
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +1
-0@@ -188,6 +194,28 @@ static int sev_cmd_buffer_len(int cmd)return 0 ; } +void snp_mark_pages_offline (unsigned long pfn,unsigned int npages)
Why call it offline which has usually a memory hotplug-related meaning? What about e.g. snp_leak_bad_pages() ?
Quoted 50 lines
drivers/crypto/ccp/sev-dev.c +21
-0+{ +struct page *page =pfn_to_page (pfn); + +WARN (1 ,"psc failed, pfn 0x%lx pages %d (marked offline)\n" , pfn, npages); + +spin_lock (&snp_leaked_pages_list_lock); +while (npages--) { +/* +* Reuse the page's buddy list for chaining into the leaked +* pages list. This page should not be on a free list currently +* and is also unsafe to be added to a free list. +*/ +list_add_tail (&page->buddy_list, &snp_leaked_pages_list); +sev_dump_rmpentry (pfn); + pfn++; + } +spin_unlock (&snp_leaked_pages_list_lock); +atomic_long_inc (&snp_nr_leaked_pages); +} +EXPORT_SYMBOL_GPL (snp_mark_pages_offline); +static void *sev_fw_alloc (unsigned long len) {struct page *page;include/linux/psp-sev.h +8
-0@@ -923,6 +923,12 @@ int sev_do_cmd(int cmd, void *data, int *psp_ret);void *psp_copy_user_blob (u64 uaddr,u32 len); +/** +* sev_mark_pages_offline - insert non-reclaimed firmware/guest pages +* into a leaked pages list. +*/ +void snp_mark_pages_offline (unsigned long pfn,unsigned int npages); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int@@ -951,6 +957,8 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, intstatic inline void *psp_copy_user_blob (u64 __user uaddr,u32 len) {return ERR_PTR (-EINVAL ); } +void snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Re: [PATCH RFC v8 08/56] KVM: SEV: Rename sev_{pin,unpin}_memory
In reply to Vlastimil BabkaQuoted 10 lines · Vlastimil Babka
Quoted 6 lines · Michael Roth
From: Nikunj A Dadhania <[email protected]>
Rename sev_{pin|unpin}_memory to sev_memory_{get|put}_pages. Apart from pinning the pages,
sev_pin_memoryalso populates the pages array which is used by its callers. SEV guest using restricted memfd do not to pin the memory but will require the pages array to be populated.^need to?
Sure
Regards,
Nikunj
Re: [PATCH RFC v8 09/56] KVM: SEV: Handle memory backed by restricted memfd
In reply to Vlastimil BabkaQuoted 9 lines · Vlastimil Babka
Quoted 5 lines · Michael Roth
From: Nikunj A Dadhania <[email protected]>
Do not pin the guest memory backed by a restrictedmem backend, as pages in the restrictedmem are already pinned. Instead, populate the pages array for these guests using the already-pinned pages provided by
IIUC the "already pinned" became "effectively unmovable and unevictable" since the earlier versions, so that would be more accurate now?
Yes, that makes sense.
Regards
Nikunj
Re: [PATCH RFC v8 01/56] KVM: x86: Add 'fault_is_private' x86 op
In reply to Michael RothQuoted 7 lines · Michael Roth
This callback is used by the KVM MMU to check whether a #NPF was for a private GPA or not.
In some cases the full 64-bit error code for the #NPF will be needed to make this determination, so also update
kvm_mmu_do_page_fault()to accept the full 64-bit value so it can be plumbed through to the callback.
We can split 64-bit part into the independent patch.
Quoted 103 lines
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 5 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -131,6 +131,7 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); +KVM_X86_OP_OPTIONAL_RET0(fault_is_private); #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +1
-0@@ -1643,6 +1643,7 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu/mmu.c +1
-2@@ -5724,8 +5724,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err }if (r ==RET_PF_INVALID ) { - r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, -lower_32_bits (error_code), false); + r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, error_code, false);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ; }arch/x86/kvm/mmu/mmu_internal.h +33
-2@@ -231,6 +231,37 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +bool private_fault = false; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +pr_debug ("%s: no slot, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (!kvm_slot_can_be_private (slot)) { +pr_debug ("%s: slot is not private, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (static_call (kvm_x86_fault_is_private)(kvm, gpa, err, &private_fault)) + goto out; + +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ + private_fault =kvm_mem_is_private (kvm, gpa >>PAGE_SHIFT ); + +out: +pr_debug ("%s: GFN: 0x%llx, private: %d\n" , __func__, gfn, private_fault); +return private_fault; +} +/* * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(), * and of course kvm_mmu_do_page_fault(). @@ -262,11 +293,11 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch) +u64 err,bool prefetch) {struct kvm_page_fault fault = { .addr = cr2_or_gpa, - .error_code = err, + .error_code =lower_32_bits (err), .exec = err &PFERR_FETCH_MASK , .write = err &PFERR_WRITE_MASK , .present = err &PFERR_PRESENT_MASK ,
arch/x86/kvm/mmu/mmu_internal.h static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 314
Quoted 5 lines
arch/x86/kvm/mmu/mmu_internal.h +1
-1@@ -280,7 +311,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err),
I don't think kvm_mmu_fault_is_private(). It's too heavy. We can make it it's own. I.e. the following.
From b0f914a1a4d154f076c0294831ce9ef0df7eb3d3 Mon Sep 17 00:00:00 2001
Message-Id: <b0f914a1a4d154f076c0294831ce9ef0df7eb3d3.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 11:18:13 -0700
Subject: [PATCH 2/4] KVM: x86: Add 'fault_is_private' x86 op
This callback is used by the KVM MMU to check whether a KVM page fault was for a private GPA or not.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.h | 19 +++++++++++++++++++ arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/x86.c | 8 ++++++++ 5 files changed, 30 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -99,6 +99,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP(fault_is_private) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1730,6 +1730,7 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);
arch/x86/kvm/mmu.h +19 -0
@@ -289,6 +289,25 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,return translate_nested_gpa (vcpu, gpa, access, exception); } +static inline bool kvm_mmu_fault_is_private_default (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) +return false; + +if (!kvm_slot_can_be_private (slot)) +return false; + +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ +return kvm_mem_is_private (kvm, gfn); +} +static inline gfn_t kvm_gfn_shared_mask (const struct kvm *kvm) {#ifdef CONFIG_KVM_MMU_PRIVATE
arch/x86/kvm/mmu/mmu_internal.h +1 -1
@@ -445,7 +445,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa), + .is_private = static_call(kvm_x86_fault_is_private)(vcpu->kvm, cr2_or_gpa, err), };int r;
arch/x86/kvm/x86.c +8 -0
@@ -9419,6 +9419,14 @@ static inline void kvm_ops_update(struct kvm_x86_init_ops *ops)#undef __KVM_X86_OPkvm_pmu_ops_update (ops->pmu_ops); + +/* +* TODO: Once all backend fills this option, remove this and the default +* function. +*/ +if (!ops->runtime_ops->fault_is_private) +static_call_update (kvm_x86_fault_is_private, + kvm_mmu_fault_is_private_default); }static int kvm_x86_check_processor_compatibility (void )
Signature · 7 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 01/56] KVM: x86: Add 'fault_is_private' x86 op
In reply to Michael RothQuoted 7 lines · Michael Roth
This callback is used by the KVM MMU to check whether a #NPF was for a private GPA or not.
In some cases the full 64-bit error code for the #NPF will be needed to make this determination, so also update
kvm_mmu_do_page_fault()to accept the full 64-bit value so it can be plumbed through to the callback.
Here is a patch to change error code 64-bit.
From 428a676face7a06a90e59dca1c32941c9b6ee001 Mon Sep 17 00:00:00 2001
Message-Id: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:58:42 -0700
Subject: [PATCH 1/4] KVM: x86/mmu: Pass round full 64-bit error code for the
KVM page fault
In some cases the full 64-bit error code for the KVM page fault will be needed to make this determination, so also update kvm_mmu_do_page_fault() to accept the full 64-bit value so it can be plumbed through to the callback.
The upper 32 bits of error code is discarded at kvm_mmu_page_fault() by lower_32_bits(). Now it's passed down as full 64 bits. It turns out that only FNAME(page_fault) depends on it. Move lower_32_bits() into FNAME(page_fault).
The accesses of fault->error_code are as follows
- FNAME(page_fault): change to explicitly use lower_32_bits()
- kvm_tdp_page_fault(): explicit mask with PFERR_LEVEL_MASK
- kvm_mmu_page_fault(): explicit mask with PFERR_RSVD_MASK, PFERR_NESTED_GUEST_PAGE
- mmutrace: changed u32 -> u64
- pgprintk(): change %x -> %llx
--- arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/mmu/mmu.c | 7 +++---- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 4 ++-- 5 files changed, 9 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu.h +1 -1
@@ -156,7 +156,7 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) }kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, -u32 error_code,int max_level); +u64 error_code,int max_level);/* * Check if a given access (described through the I/D, W/R and U/S bits of a
arch/x86/kvm/mmu/mmu.c +3 -4
@@ -4860,7 +4860,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultstatic int nonpaging_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -pgprintk ("%s: gva %llx error %x\n" , __func__, fault->addr, fault->error_code); +pgprintk ("%s: gva %llx error %llx\n" , __func__, fault->addr, fault->error_code);/* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ fault->max_level =PG_LEVEL_2M ;
@@ -4986,7 +4986,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) }kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, -u32 error_code,int max_level) +u64 error_code,int max_level) {int r;struct kvm_page_fault fault = (struct kvm_page_fault ) {
@@ -6238,8 +6238,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err }if (r ==RET_PF_INVALID ) { - r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, -lower_32_bits (error_code), false); + r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, error_code, false);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ; }
arch/x86/kvm/mmu/mmu_internal.h +2 -2
@@ -341,7 +341,7 @@ static inline bool is_nx_huge_page_enabled(struct kvm *kvm)struct kvm_page_fault {/* arguments to kvm_mmu_do_page_fault. */ const gpa_t addr; -const u32 error_code; +const u64 error_code;const bool prefetch;/* Derived from error_code. */
@@ -427,7 +427,7 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch) +u64 err,bool prefetch) {struct kvm_page_fault fault = { .addr = cr2_or_gpa,
arch/x86/kvm/mmu/mmutrace.h +1 -1
@@ -261,7 +261,7 @@ TRACE_EVENT(TP_STRUCT__entry (__field (int ,vcpu_id )__field (gpa_t, cr2_or_gpa) -__field (u32, error_code) +__field (u64, error_code) __field(u64 *, sptep)__field (u64, old_spte) __field(u64, new_spte)
arch/x86/kvm/mmu/paging_tmpl.h +2 -2
@@ -791,7 +791,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultint r;bool is_self_change_mapping; -pgprintk ("%s: addr %llx err %x\n" , __func__, fault->addr, fault->error_code); +pgprintk ("%s: addr %llx err %llx\n" , __func__, fault->addr, fault->error_code);WARN_ON_ONCE (fault->is_tdp);/*
@@ -800,7 +800,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* The bit needs to be cleared before walking guest page tables. */ r =FNAME (walk_addr)(&walker, vcpu, fault->addr, - fault->error_code & ~PFERR_RSVD_MASK ); +lower_32_bits (fault->error_code) & ~PFERR_RSVD_MASK );/* * The page is not mapped by the guest. Let the guest handle it.
Signature · 5 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
In reply to Michael RothQuoted 112 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -132,6 +132,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons);KVM_X86_OP_OPTIONAL_RET0 (fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +2
-0@@ -1644,6 +1644,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault); +int (*update_mem_attr)(struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu/mmu.c +13
-0@@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end) +{ +int ret; + + ret =static_call (kvm_x86_update_mem_attr)(slot, attrs, start, end); +if (ret) +pr_warn_ratelimited ("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n" , + start, end, attrs, ret); +}#endif include/linux/kvm_host.h +4
-0@@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm,struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end);static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) {virt/kvm/kvm_main.c +26
-0@@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,kvm_flush_remote_tlbs (kvm); } +static void kvm_post_mem_attrs_changed (struct kvm *kvm,unsigned long attrs, +gfn_t start_orig,gfn_t end_orig) +{ +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +int i; + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, start_orig, end_orig) { +gfn_t start, end; + + slot = iter.slot; + start =max (start_orig, slot->base_gfn); + end =min (end_orig, slot->base_gfn + slot->npages); + +if (start >= end) +continue ; + +kvm_arch_post_set_memory_attributes (kvm, slot, attrs, start, end); + } + } +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm,struct kvm_memory_attributes *attrs) {
virt/kvm/kvm_main.c static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 6 lines
virt/kvm/kvm_main.c +3
-0@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,kvm_mmu_invalidate_end (kvm);KVM_MMU_UNLOCK (kvm); +if (i > start) +kvm_post_mem_attrs_changed (kvm, attrs->attributes, start, i); +
Doesn't kvm_arch_set_memory_attributes() work for you? i.e the following patch. The error check and pr_warn_ratelimited() can be pushed down into the callback.
From 7c618c1f3c236c382e64680efcbe7d8a672aa870 Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 op
This callback will do any platform-specific handling needed for converting pages between shared/private.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,
Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 03/56] KVM: x86: Add platform hooks for private memory invalidations
In reply to Michael RothQuoted 8 lines · Michael Roth
In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Add hooks to wire up handling of this sort to the invalidation notifiers for restricted memory.
Also issue invalidations of all allocated pages during notifier/memslot unbinding so that the pages are not left in an unusable state when they eventually get freed back to the host upon FD release.
I'm just curios. Could you please elaborate?
Unbind is happen only when memory slot is delete or vm is destroyed. In the case of memory slot deletion, the gpa region is zapped via kvm_arch_commit_memory_region(). In the case of VM destroy, we have kvm_flush_shadow_all() which callskvm_arch_flush_shadow_all() =>kvm_mmu_zap_all(). Doesn't it work?
Thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 01/56] KVM: x86: Add 'fault_is_private' x86 op
In reply to Isaku YamahataQuoted 10 lines · Isaku Yamahata
Quoted 7 lines · Michael Roth
This callback is used by the KVM MMU to check whether a #NPF was for a private GPA or not.
In some cases the full 64-bit error code for the #NPF will be needed to make this determination, so also update
kvm_mmu_do_page_fault()to accept the full 64-bit value so it can be plumbed through to the callback.
Hi Isaku, Zhi,
Thanks for your efforts trying to get us in sync on these shared interfaces. Would be great to have a common base we can build on for the SNP/TDX series. You mentioned a couple patches here that I couldn't find on the list, are you planning to submit these as a separate series?
Quoted 2 lines
We can split 64-bit part into the independent patch.
Agreed that makes sense.
Quoted 3 lines
Quoted 3 lines
Signed-off-by Michael Roth <[email protected]>---
<snip>
Quoted 49 lines
Quoted 49 lines
(quoted diff) +33
-2+static bool kvm_mmu_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 err) +{ + struct kvm_memory_slot *slot; + bool private_fault = false; + gfn_t gfn = gpa_to_gfn(gpa); + + slot = gfn_to_memslot(kvm, gfn); + if (!slot) { + pr_debug("%s: no slot, GFN: 0x%llx\n", __func__, gfn); + goto out; + } + + if (!kvm_slot_can_be_private(slot)) { + pr_debug("%s: slot is not private, GFN: 0x%llx\n", __func__, gfn); + goto out; + } + + if (static_call(kvm_x86_fault_is_private)(kvm, gpa, err, &private_fault)) + goto out; + + /* + * Handling below is for UPM self-tests and guests that treat userspace + * as the authority on whether a fault should be private or not. + */ + private_fault = kvm_mem_is_private(kvm, gpa >> PAGE_SHIFT); + +out: + pr_debug("%s: GFN: 0x%llx, private: %d\n", __func__, gfn, private_fault); + return private_fault; +} + /* * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(), * and of course kvm_mmu_do_page_fault().@@ -262,11 +293,11 @@ enum { }; static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, - u32 err, bool prefetch) + u64 err, bool prefetch) { struct kvm_page_fault fault = { .addr = cr2_or_gpa, - .error_code = err, + .error_code = lower_32_bits(err), .exec = err & PFERR_FETCH_MASK, .write = err & PFERR_WRITE_MASK, .present = err & PFERR_PRESENT_MASK,@@ -280,7 +311,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Quoted 8 lines
Review static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 314
Quoted 5 lines
(quoted diff) +1
-1.max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err),I don't think
kvm_mmu_fault_is_private(). It's too heavy. We can make it it's own. I.e. the following.
Is it causing performance issues? If most of that is mainly due to gfn_to_memslot()/kvm_slot_can_be_private() check, then maybe that part can be dropped. In the past Sean has mentioned that we shouldn't have to do kvm_slot_can_be_private() checks prior to kvm_mem_is_private(), but I haven't tried removing those yet to see if things still work as expected.
Quoted 95 lines
From
b0f914a1a4d154f076c0294831ce9ef0df7eb3d3Mon Sep 17 00:00:00 2001
Message-Id: <b0f914a1a4d154f076c0294831ce9ef0df7eb3d3.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 11:18:13 -0700
Subject: [PATCH 2/4] KVM: x86: Add 'fault_is_private' x86 opThis callback is used by the KVM MMU to check whether a KVM page fault was for a private GPA or not.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.h | 19 +++++++++++++++++++ arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/x86.c | 8 ++++++++ 5 files changed, 30 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -99,6 +99,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP(fault_is_private) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +1
-0@@ -1730,6 +1730,7 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu.h +19
-0@@ -289,6 +289,25 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,return translate_nested_gpa (vcpu, gpa, access, exception); } +static inline bool kvm_mmu_fault_is_private_default (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) +return false; + +if (!kvm_slot_can_be_private (slot)) +return false; + +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ +return kvm_mem_is_private (kvm, gfn); +} +static inline gfn_t kvm_gfn_shared_mask (const struct kvm *kvm) {#ifdef CONFIG_KVM_MMU_PRIVATE arch/x86/kvm/mmu/mmu_internal.h +1
-1@@ -445,7 +445,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa), + .is_private = static_call(kvm_x86_fault_is_private)(vcpu->kvm, cr2_or_gpa, err), };int r;arch/x86/kvm/x86.c +0
-0@@ -9419,6 +9419,14 @@ static inline void kvm_ops_update(struct kvm_x86_init_ops *ops)#undef __KVM_X86_OP
arch/x86/kvm/x86.c static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) · line 9429
Quoted 10 lines
arch/x86/kvm/x86.c +8
-0kvm_pmu_ops_update (ops->pmu_ops); + +/* +* TODO: Once all backend fills this option, remove this and the default +* function. +*/ +if (!ops->runtime_ops->fault_is_private) +static_call_update (kvm_x86_fault_is_private, + kvm_mmu_fault_is_private_default);
I'm not sure about this approach, since the self-tests (and possibly SEV (which doesn't use a separate #NPF error bit like SNP/TDX)) currently rely on that kvm_mem_is_private() call to determine whether to handle as a private fault or not. But to run either of those, we would need to load the kvm_amd module, which will have already introduced it's own kvm_x86_fault_is_private implementation via svm_init(), so the handling provided by kvm_mmu_fault_is_private_default would never be available and so we wouldn't be able to run the UPM self-tests.
To me it seems like that handling always needs to be in place as a fallback when not running SNP/TDX. It doesn't necessarily need to be in the kvm_x86_fault_is_private handler though, maybe some generic handling for UPM selftests can be pushed down into KVM MMU. Doing so could also address a race that Sean mentioned between the time kvm_mem_is_private() is called here (which happens before mmu_invalidate_seq is recorded for the #NPF) vs. when it actually gets used in __kvm_faultin_pfn().
If we take that approach, then the requirements for specific TDX/SNP handling are reduced as well, since we only need to check the encryption/shared bit, and that could maybe be done as a simple setting that where you tell KVM MMU the position of the bit, whether it indicates shared vs. private, then both TDX/SNP could re-use a simple helper to check the #NPF error code and set .is_private based on that.
Then KVM MMU could, if no bit is indicated, just fall back to using the value of kvm_mem_is_private() somewhere in __kvm_fault_pfn() or something.
I mentioned this to Sean a while back, which I think is compatible with what he was looking for:
https://lore.kernel.org/lkml/[email protected]/
Would be good to get his input before spending too much time adding new state/configuration stuff in KVM MMU though.
As an interim solution, would my original patch work if we could confirm that the gfn_to_memslot()/kvm_slot_can_be_private() sequence is no longer needed?
Thanks!
-Mike
Quoted 11 lines
arch/x86/kvm/x86.c +0
-0}static int kvm_x86_check_processor_compatibility (void )Signature · 7 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
In reply to Isaku YamahataQuoted 118 lines · Isaku Yamahata
Quoted 115 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -132,6 +132,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons);KVM_X86_OP_OPTIONAL_RET0 (fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +2
-0@@ -1644,6 +1644,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault); +int (*update_mem_attr)(struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu/mmu.c +13
-0@@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end) +{ +int ret; + + ret =static_call (kvm_x86_update_mem_attr)(slot, attrs, start, end); +if (ret) +pr_warn_ratelimited ("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n" , + start, end, attrs, ret); +}#endif include/linux/kvm_host.h +4
-0@@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm,struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end);static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) {virt/kvm/kvm_main.c +26
-0@@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,kvm_flush_remote_tlbs (kvm); } +static void kvm_post_mem_attrs_changed (struct kvm *kvm,unsigned long attrs, +gfn_t start_orig,gfn_t end_orig) +{ +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +int i; + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, start_orig, end_orig) { +gfn_t start, end; + + slot = iter.slot; + start =max (start_orig, slot->base_gfn); + end =min (end_orig, slot->base_gfn + slot->npages); + +if (start >= end) +continue ; + +kvm_arch_post_set_memory_attributes (kvm, slot, attrs, start, end); + } + } +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm,struct kvm_memory_attributes *attrs) {@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,kvm_mmu_invalidate_end (kvm);KVM_MMU_UNLOCK (kvm);
Quoted 6 lines
virt/kvm/kvm_main.c static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+if (i > start) +kvm_post_mem_attrs_changed (kvm, attrs->attributes, start, i); +Doesn't
kvm_arch_set_memory_attributes()work for you? i.e the following patch. The error check andpr_warn_ratelimited()can be pushed down into the callback.
This is originally how I had but when CONFIG_PREEMPT_COUNT is set this will generate warnings for this callback as well as the invalidation callback as reported in v7 here:
https://lore.kernel.org/lkml/Y80vhKwQyw8hS%2F22@notebook/
The main issue is that kvm_mem_attrs_changed() is called while holding the KVM MMU lock, which disables preemption. But when updating attributes for SNP, we also need to remove private pages from kernel directmap, which involves acquiring a mutex which results in "BUG: scheduling while atomic" warnings.
So that's why we ended up somewhat duplicating some of the logic and using a separate callback chain that happens out of KVM MMU lock.
-Mike
Quoted 62 lines
From
7c618c1f3c236c382e64680efcbe7d8a672aa870Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 opThis callback will do any platform-specific handling needed for converting pages between shared/private.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +2
-0@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu/mmu.c +1
-0@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 03/56] KVM: x86: Add platform hooks for private memory invalidations
In reply to Isaku YamahataQuoted 12 lines · Isaku Yamahata
Quoted 8 lines · Michael Roth
In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Add hooks to wire up handling of this sort to the invalidation notifiers for restricted memory.
Also issue invalidations of all allocated pages during notifier/memslot unbinding so that the pages are not left in an unusable state when they eventually get freed back to the host upon FD release.
Quoted 6 lines
I'm just curios. Could you please elaborate?
Unbind is happen only when memory slot is delete or vm is destroyed. In the case of memory slot deletion, the gpa region is zapped viakvm_arch_commit_memory_region(). In the case of VM destroy, we havekvm_flush_shadow_all()which callskvm_arch_flush_shadow_all()=>kvm_mmu_zap_all(). Doesn't it work?
The main thing here is unbind happens right before the restrictedmem pages are released back to the host, and for SNP we need to clear the associated RMP table entries to switch them from guest-owned to hypervisor-owned. It doesn't necessarily need to be a separate callback, but I'm not sure if it makes sense to squash that down into the various MMU zapping helpers.
-Mike
Quoted 4 lines
Thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
In reply to Michael RothQuoted 132 lines · On Mon, 20 Mar 2023 13:05:43 -0500 Michael Roth <[email protected]> wrote:
Quoted 118 lines · Isaku Yamahata
Quoted 115 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -132,6 +132,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons);KVM_X86_OP_OPTIONAL_RET0 (fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +2
-0@@ -1644,6 +1644,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault); +int (*update_mem_attr)(struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu/mmu.c +13
-0@@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end) +{ +int ret; + + ret =static_call (kvm_x86_update_mem_attr)(slot, attrs, start, end); +if (ret) +pr_warn_ratelimited ("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n" , + start, end, attrs, ret); +}#endif include/linux/kvm_host.h +4
-0@@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm,struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end);static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) {virt/kvm/kvm_main.c +26
-0@@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,kvm_flush_remote_tlbs (kvm); } +static void kvm_post_mem_attrs_changed (struct kvm *kvm,unsigned long attrs, +gfn_t start_orig,gfn_t end_orig) +{ +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +int i; + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, start_orig, end_orig) { +gfn_t start, end; + + slot = iter.slot; + start =max (start_orig, slot->base_gfn); + end =min (end_orig, slot->base_gfn + slot->npages); + +if (start >= end) +continue ; + +kvm_arch_post_set_memory_attributes (kvm, slot, attrs, start, end); + } + } +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm,struct kvm_memory_attributes *attrs) {@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,kvm_mmu_invalidate_end (kvm);KVM_MMU_UNLOCK (kvm);Quoted 6 lines
virt/kvm/kvm_main.c static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+if (i > start) +kvm_post_mem_attrs_changed (kvm, attrs->attributes, start, i); +Doesn't
kvm_arch_set_memory_attributes()work for you? i.e the following patch. The error check andpr_warn_ratelimited()can be pushed down into the callback.This is originally how I had but when
CONFIG_PREEMPT_COUNTis set this will generate warnings for this callback as well as the invalidation callback as reported in v7 here:
Quoted 8 lines
The main issue is that
kvm_mem_attrs_changed()is called while holding the KVM MMU lock, which disables preemption. But when updating attributes for SNP, we also need to remove private pages from kernel directmap, which involves acquiring a mutex which results in "BUG: scheduling while atomic" warnings.So that's why we ended up somewhat duplicating some of the logic and using a separate callback chain that happens out of KVM MMU lock.
Let's split the things of changing memory attributes:
1) Update the memory attributes in the xa array (Both TDX and SNP)
2) Zapping the EPT/NPT mappings (Required by TDX)
3) Update RMP table (Required by SNP)
4) Update the directmap of kernel (SNP, but I guess TDX needs it as well)
Does SNP really need to zap the NPT mappings when changing the memory attributes? (The new mappings will be created later in the fault). I don't find this requirement from APM.
If yes, can we postpone the update of the RMP table in the later fault, like TDX? So that we can save this update_mem_attr x86 ops as things will be solved in the SNP-specific fault handler.
If no, guess we need a x86 ops to tell if a zapping is required.
Back to the lock, updating RMP table doesn't require a mutex. Taking the lock is required when updating the directmap. both TDX/SNP requires this update the directmap when changing memory attributes.
Wouldn't it better to factor the touching directmap of kernel part out?
Then you can call the x86 ops.update_mem_attr() in kvm_mem_attrs_changed(). And update the direct kernel mapping for both TDX/SNP in the kvm_post_mem_attrs_changed().
Quoted 65 lines
-Mike
Quoted 62 lines
From
7c618c1f3c236c382e64680efcbe7d8a672aa870Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 opThis callback will do any platform-specific handling needed for converting pages between shared/private.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +2
-0@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu/mmu.c +1
-0@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
Quoted 144 lines · Zhi Wang
Quoted 132 lines · On Mon, 20 Mar 2023 13:05:43 -0500 Michael Roth <[email protected]> wrote:
Quoted 118 lines · Isaku Yamahata
Quoted 115 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -132,6 +132,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons);KVM_X86_OP_OPTIONAL_RET0 (fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +2
-0@@ -1644,6 +1644,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault); +int (*update_mem_attr)(struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu/mmu.c +13
-0@@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end) +{ +int ret; + + ret =static_call (kvm_x86_update_mem_attr)(slot, attrs, start, end); +if (ret) +pr_warn_ratelimited ("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n" , + start, end, attrs, ret); +}#endif include/linux/kvm_host.h +4
-0@@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm,struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end);static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) {virt/kvm/kvm_main.c +26
-0@@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,kvm_flush_remote_tlbs (kvm); } +static void kvm_post_mem_attrs_changed (struct kvm *kvm,unsigned long attrs, +gfn_t start_orig,gfn_t end_orig) +{ +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +int i; + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, start_orig, end_orig) { +gfn_t start, end; + + slot = iter.slot; + start =max (start_orig, slot->base_gfn); + end =min (end_orig, slot->base_gfn + slot->npages); + +if (start >= end) +continue ; + +kvm_arch_post_set_memory_attributes (kvm, slot, attrs, start, end); + } + } +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm,struct kvm_memory_attributes *attrs) {@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,kvm_mmu_invalidate_end (kvm);KVM_MMU_UNLOCK (kvm);Quoted 6 lines
virt/kvm/kvm_main.c static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+if (i > start) +kvm_post_mem_attrs_changed (kvm, attrs->attributes, start, i); +Doesn't
kvm_arch_set_memory_attributes()work for you? i.e the following patch. The error check andpr_warn_ratelimited()can be pushed down into the callback.This is originally how I had but when
CONFIG_PREEMPT_COUNTis set this will generate warnings for this callback as well as the invalidation callback as reported in v7 here:Quoted 8 lines
The main issue is that
kvm_mem_attrs_changed()is called while holding the KVM MMU lock, which disables preemption. But when updating attributes for SNP, we also need to remove private pages from kernel directmap, which involves acquiring a mutex which results in "BUG: scheduling while atomic" warnings.So that's why we ended up somewhat duplicating some of the logic and using a separate callback chain that happens out of KVM MMU lock.
Quoted 6 lines
Let's split the things of changing memory attributes:
1) Update the memory attributes in the xa array (Both TDX and SNP)
2) Zapping the EPT/NPT mappings (Required by TDX)
3) Update RMP table (Required by SNP)
4) Update the directmap of kernel (SNP, but I guess TDX needs it as well)
I'm not so sure TDX requires this. I was under that impression, but Kirill raised some doubts about this and I'm not sure it's been confirmed. If it's purely an SNP thing then there may not be much value in creating a separate callback for it:
https://lore.kernel.org/linux-mm/[email protected]/T/#meba4ce80709cd3afd3818b61e6419fd800287b9e
And for SNP, the current code does the unmapping/RMP update in the same function:
[PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
I'm not against splitting RMP/directmap handling, but just want to understand what the requirements are around that a bit better.
Does handling the #3 / RMP update / kvm_arch_post_set_memory_attributes stuff outside of MMU lock cause issues on TDX side? What sort of handling is needed in these callbacks for TDX (if anything)?
Quoted 4 lines
Does SNP really need to zap the NPT mappings when changing the memory attributes? (The new mappings will be created later in the fault). I don't find this requirement from APM.
I don't think we've added anything specifically for SNP. Do you mean the generic kvm_unmap_gfn_range/kvm_flush_remote_tlbs sequence below?
kvm_vm_ioctl_set_mem_attributes():
KVM_MMU_LOCK(kvm)
kvm_mmu_invalidate_begin()
...
KVM_MMU_UNLOCK(kvm)
kvm_vm_set_region_attr() // xarray/attribute update
...
KVM_MMU_LOCK(kvm)
kvm_mem_attrs_changed():
flush |= kvm_unmap_gfn_range()
if (flush)
kvm_flush_remote_tlbs()
KVM_MMU_UNLOCK(kvm)In general, when the RMPUPDATE instruction happens, the TLB entries for the GPAs being modified will be flushed, so subsequent nested page fault should be able to obtain the updated mapping based on xarray/#NPF at that point. In that respect *maybe* we don't need to zap the entries there.
But if the nested page fault occurs before the RMPUPDATE, I think we would have a race if the above sequence isn't in place to handle the unmap/flush, since in that case we might get a stale mapping because nothing would've forced a tlbflush.
There's also stuff like the UPM selftests and SEV lazy-pinning where I think that kvm_unmap_gfn_range() sequence is also needed. But I might be misunderstanding the question here.
Quoted 3 lines
If yes, can we postpone the update of the RMP table in the later fault, like TDX? So that we can save this
update_mem_attrx86 ops as things will be solved in the SNP-specific fault handler.
Hmm, I think this would be possible. But it's nice to be able to handle the RMPUPDATE as part of KVM_SET_MEMORY_ATTRIBUTES, since it allows KVM MMU code to rely solely on xarray state and not have to query RMP table to check if a particular PFN needs an RMPUPDATE before mapping it into RMP table.
At least... it would *in theory*, if the RMPUPDATE happened under protection of mmu_invalidate_seq (in which case it could inherit all the same protections KVM MMU has around mmu_invalidate_seq/fault->mmu_seq, e.g. letting the guest retry the #PF if fault->mmu_seq is stale).
But currently, RMPUPDATE (via kvm_arch_post_set_memory_attributes) happens *after* the invalidation sequence above, so in theory a guest could fault on a page just after xarray state is updated, but before the RMPUPDATE has been done, in which case the KVM MMU code would properly map the page accordingly to xarray, but since RMPUPDATE wouldn't have happened yet, the state of the corresponding PFN in RMP table won't match the shared/private access type expected by the guest, so when it tries to access it it will get another #NPF with RMP bit set in the error code, which will get handled as a no-op in handle_rmp_page_fault() (patch #44) and loop like this until the RMPUPDATE is finally done. So it still works out, but maybe not keeping as much in sync with xarray state and could be.
But deferring RMPUPDATE to fault time goes in the other direction of that. Are there benefits/requirements for doing things this way for TDX? I could see it being beneficial in terms of reducing overhead for uneeded page-state transitions, since they are only done on-demand but doesn't seem like it would be that much overhead compared to some of the other operations being done.
Quoted 2 lines
If no, guess we need a x86 ops to tell if a zapping is required.
Sorry don't think I quite understand the suggestion. What would this zapping be covering vs. the invalidation sequence that currently happens in kvm_vm_ioctl_set_mem_attributes()?
Quoted 4 lines
Back to the lock, updating RMP table doesn't require a mutex. Taking the lock is required when updating the directmap. both TDX/SNP requires this update the directmap when changing memory attributes.
Is that confirmed? If so, do you have a pointer to the associated documentation? I'm a bit unclear on this due to above-mentioned discussion.
Quoted 2 lines
Wouldn't it better to factor the touching directmap of kernel part out?
It actually needs to happen before the RMPUPDATE. As soon as there is a shared->private conversion in the RMP table for a particular PFN, then any access via directmap by any particular kernel thread to any PFN that happens to be in the same physical 2M range can cause an RMP fault on the host, which would be fatal. So the rmpupdate() helper in this series will unmap directmap entry corresponding the PFN before a shared->private RMPUPDATE, and restore mappings after private->shared RMPUPDATE
So we could still factor it out, but it would be something like:
if (attr == private)
kvm_unmap_directmap(start, end)
kvm_mem_attrs_changed()
if (attr == shared)
kvm_map_directmap(start, end)
Quoted 4 lines
Then you can call the x86
ops.update_mem_attr()inkvm_mem_attrs_changed(). And update the direct kernel mapping for both TDX/SNP in thekvm_post_mem_attrs_changed().
Or, adjusting for the above logic, move the unmapping/mapping to a new kvm_pre_mem_attrs_changed() and kvm_post_mem_attrs_changed(), respectively.
Which seems pretty reasonable to me. Then we can:
- drop duplicating the kvm_for_each_memslot_in_gfn_range() walk stuff because we'd just need to know what PFNs to map/unmap from directmap (although we'd still need a loop around kvm_restrictedmem_get_pfn() for the GFN range so not necessarily prettier)
- call the RMPUPDATE / corresponding TDX handling via kvm_mem_attrs_changed() which brings it both under KVM MMU lock and also let's it piggyback off the fault->mmu_seq handling so it doesn't get out of sync with xarray during fault time.
But would be good to hear others' opinions on this. And also confirm whether TDX needs that pre/post directmap handle or not.
Thanks!
-Mike
Quoted 68 lines
Quoted 66 lines
-Mike
Quoted 62 lines
From
7c618c1f3c236c382e64680efcbe7d8a672aa870Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 opThis callback will do any platform-specific handling needed for converting pages between shared/private.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +2
-0@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu/mmu.c +1
-0@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
Quoted 145 lines · On Tue, 21 Mar 2023 20:58:38 -0500 Michael Roth <[email protected]> wrote:
Quoted 144 lines · Zhi Wang
Quoted 140 lines · On Mon, 20 Mar 2023 13:05:43 -0500 Michael Roth <[email protected]> wrote:
Quoted 118 lines · Isaku Yamahata
Quoted 115 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -132,6 +132,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons);KVM_X86_OP_OPTIONAL_RET0 (fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +2
-0@@ -1644,6 +1644,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault); +int (*update_mem_attr)(struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu/mmu.c +13
-0@@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end) +{ +int ret; + + ret =static_call (kvm_x86_update_mem_attr)(slot, attrs, start, end); +if (ret) +pr_warn_ratelimited ("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n" , + start, end, attrs, ret); +}#endif include/linux/kvm_host.h +4
-0@@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm,struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end);static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) {virt/kvm/kvm_main.c +26
-0@@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,kvm_flush_remote_tlbs (kvm); } +static void kvm_post_mem_attrs_changed (struct kvm *kvm,unsigned long attrs, +gfn_t start_orig,gfn_t end_orig) +{ +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +int i; + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, start_orig, end_orig) { +gfn_t start, end; + + slot = iter.slot; + start =max (start_orig, slot->base_gfn); + end =min (end_orig, slot->base_gfn + slot->npages); + +if (start >= end) +continue ; + +kvm_arch_post_set_memory_attributes (kvm, slot, attrs, start, end); + } + } +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm,struct kvm_memory_attributes *attrs) {@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,kvm_mmu_invalidate_end (kvm);KVM_MMU_UNLOCK (kvm);Quoted 6 lines
virt/kvm/kvm_main.c static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+if (i > start) +kvm_post_mem_attrs_changed (kvm, attrs->attributes, start, i); +Doesn't
kvm_arch_set_memory_attributes()work for you? i.e the following patch. The error check andpr_warn_ratelimited()can be pushed down into the callback.This is originally how I had but when
CONFIG_PREEMPT_COUNTis set this will generate warnings for this callback as well as the invalidation callback as reported in v7 here:https://lore.kernel.org/lkml/Y80vhKwQyw8hS%2F22@notebook/
The main issue is that
kvm_mem_attrs_changed()is called while holding the KVM MMU lock, which disables preemption. But when updating attributes for SNP, we also need to remove private pages from kernel directmap, which involves acquiring a mutex which results in "BUG: scheduling while atomic" warnings.So that's why we ended up somewhat duplicating some of the logic and using a separate callback chain that happens out of KVM MMU lock.
Quoted 7 lines
Quoted 6 lines
Let's split the things of changing memory attributes:
1) Update the memory attributes in the xa array (Both TDX and SNP)
2) Zapping the EPT/NPT mappings (Required by TDX)
3) Update RMP table (Required by SNP)
4) Update the directmap of kernel (SNP, but I guess TDX needs it as well)
Thanks for the effort of detailed reply. It is very informative.
Quoted 7 lines
I'm not so sure TDX requires this. I was under that impression, but Kirill raised some doubts about this and I'm not sure it's been confirmed. If it's purely an SNP thing then there may not be much value in creating a separate callback for it:
https://lore.kernel.org/linux-mm/[email protected]/T/#meba4ce80709cd3afd3818b61e6419fd800287b9e
Hmm, Krill and Isaku, can you confirm that TDX doesn't need this?
I think it is a generic requirement that TDX/SNP are not expecting the host to touch a private page either from the kernel or the userspace.
Quoted 12 lines
And for SNP, the current code does the unmapping/RMP update in the same function:
[PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
I'm not against splitting RMP/directmap handling, but just want to understand what the requirements are around that a bit better.
Does handling the #3 / RMP update /
kvm_arch_post_set_memory_attributesstuff outside of MMU lock cause issues on TDX side? What sort of handling is needed in these callbacks for TDX (if anything)?
No, it doesn't cause problem for TDX as TDX doesn't need such callback.
Unlike SNP, which has (1 NPT + 1 RMP) and the enforced HW check is done by RMP, TDX has two EPT(smiliar with NPT)s (1 shared + 1 private). Converting the memory attr is achieved by zapping the mapping from one EPT and creating the mapping in the other one in the fault when guest access the memory. The fault GPA will carry the "SHARED" bit (!C-BIT), so KVM knows which EPT should be chosen for populating the mapping.
I was trying to figure out what should be the proper callback and at which layer it should sit for achieving changing memory attr for both TDX/SNP. The current callback looks a little bit hacky. Duplicate code pieces because of locks implies the SW structure might need to be re-considered.
Quoted 9 lines
Quoted 4 lines
Does SNP really need to zap the NPT mappings when changing the memory attributes? (The new mappings will be created later in the fault). I don't find this requirement from APM.
I don't think we've added anything specifically for SNP. Do you mean the generic kvm_unmap_gfn_range/kvm_flush_remote_tlbs sequence below?
kvm_vm_ioctl_set_mem_attributes():
Quoted 15 lines
KVM_MMU_LOCK(kvm)
kvm_mmu_invalidate_begin()
...
KVM_MMU_UNLOCK(kvm)
kvm_vm_set_region_attr() // xarray/attribute update
...
KVM_MMU_LOCK(kvm)
kvm_mem_attrs_changed():
flush |= kvm_unmap_gfn_range()
if (flush)
kvm_flush_remote_tlbs()
KVM_MMU_UNLOCK(kvm)Yes, I was talking about the sequence above. I was confused of why changing RMP requires a zapping-recreating flow of NPT in SNP.
Quoted 5 lines
In general, when the RMPUPDATE instruction happens, the TLB entries for the GPAs being modified will be flushed, so subsequent nested page fault should be able to obtain the updated mapping based on xarray/#NPF at that point. In that respect *maybe* we don't need to zap the entries there.
Quoted 9 lines
But if the nested page fault occurs before the RMPUPDATE, I think we would have a race if the above sequence isn't in place to handle the unmap/flush, since in that case we might get a stale mapping because nothing would've forced a tlbflush.
There's also stuff like the UPM selftests and SEV lazy-pinning where I think that
kvm_unmap_gfn_range()sequence is also needed. But I might be misunderstanding the question here.
In this case, an extra tlbflush would solve? Still, the unnecessary zapping/recreating of mapping is not promising. I understand that the way how this patch goes is probably to minimize the changes, but it would be nice to focus more on what is really needed in a common path and abstract and re-factor from there.
Can you elaborate more about how the lazy-pinning unpin path is connected with the zapping here? So that I can dig more about it.
Selftest is a minor case, guess we deal with them via enabling a switch. E.g. a prop in debugfs.
Quoted 15 lines
Quoted 3 lines
If yes, can we postpone the update of the RMP table in the later fault, like TDX? So that we can save this
update_mem_attrx86 ops as things will be solved in the SNP-specific fault handler.Hmm, I think this would be possible. But it's nice to be able to handle the RMPUPDATE as part of
KVM_SET_MEMORY_ATTRIBUTES, since it allows KVM MMU code to rely solely on xarray state and not have to query RMP table to check if a particular PFN needs an RMPUPDATE before mapping it into RMP table.At least... it would *in theory*, if the RMPUPDATE happened under protection of
mmu_invalidate_seq(in which case it could inherit all the same protections KVM MMU has around mmu_invalidate_seq/fault->mmu_seq, e.g. letting the guest retry the #PF iffault->mmu_seqis stale).
Quoted 12 lines
But currently, RMPUPDATE (via
kvm_arch_post_set_memory_attributes) happens *after* the invalidation sequence above, so in theory a guest could fault on a page just after xarray state is updated, but before the RMPUPDATE has been done, in which case the KVM MMU code would properly map the page accordingly to xarray, but since RMPUPDATE wouldn't have happened yet, the state of the corresponding PFN in RMP table won't match the shared/private access type expected by the guest, so when it tries to access it it will get another #NPF with RMP bit set in the error code, which will get handled as a no-op inhandle_rmp_page_fault()(patch #44) and loop like this until the RMPUPDATE is finally done. So it still works out, but maybe not keeping as much in sync with xarray state and could be.
I see. rmp fault handler only deals with page size mismatch for now.
Quoted 7 lines
But deferring RMPUPDATE to fault time goes in the other direction of that. Are there benefits/requirements for doing things this way for TDX? I could see it being beneficial in terms of reducing overhead for uneeded page-state transitions, since they are only done on-demand but doesn't seem like it would be that much overhead compared to some of the other operations being done.
Besides the HW design, I guess one major purpose is to optimize the booting time of VMs with large memory. Also, post migration can be another case.
Out of curiosity, What is the avg cost of RMUPDATE? Suppose it is an x86 instruction and not going through PSP firmware.
Quoted 6 lines
Quoted 2 lines
If no, guess we need a x86 ops to tell if a zapping is required.
Sorry don't think I quite understand the suggestion. What would this zapping be covering vs. the invalidation sequence that currently happens in
kvm_vm_ioctl_set_mem_attributes()?
I was thinking that zapping of the mapping in EPT/NPT was required by TDX while SNP might only need an RMP update + TLB flush. Thus, the abstraction of the kvm_x86_ops.update_mem_attr should sit at this level. But let's scratch this for now as I need to dig more about the lazy pinning stuff.
Quoted 37 lines
Quoted 5 lines
Back to the lock, updating RMP table doesn't require a mutex. Taking the lock is required when updating the directmap. both TDX/SNP requires this update the directmap when changing memory attributes.
Is that confirmed? If so, do you have a pointer to the associated documentation? I'm a bit unclear on this due to above-mentioned discussion.
Quoted 2 lines
Wouldn't it better to factor the touching directmap of kernel part out?
It actually needs to happen before the RMPUPDATE. As soon as there is a
shared->privateconversion in the RMP table for a particular PFN, then any access via directmap by any particular kernel thread to any PFN that happens to be in the same physical 2M range can cause an RMP fault on the host, which would be fatal. So thermpupdate()helper in this series will unmap directmap entry corresponding the PFN before ashared->privateRMPUPDATE, and restore mappings afterprivate->sharedRMPUPDATESo we could still factor it out, but it would be something like:
if (attr == private)
kvm_unmap_directmap(start, end)
kvm_mem_attrs_changed()
if (attr == shared)
kvm_map_directmap(start, end)Quoted 4 lines
Then you can call the x86
ops.update_mem_attr()inkvm_mem_attrs_changed(). And update the direct kernel mapping for both TDX/SNP in thekvm_post_mem_attrs_changed().Or, adjusting for the above logic, move the unmapping/mapping to a new
kvm_pre_mem_attrs_changed()andkvm_post_mem_attrs_changed(), respectively.
Quoted 10 lines
Which seems pretty reasonable to me. Then we can:
- drop duplicating thekvm_for_each_memslot_in_gfn_range()walk stuff because we'd just need to know what PFNs to map/unmap from directmap (although we'd still need a loop aroundkvm_restrictedmem_get_pfn()for the GFN range so not necessarily prettier)
- call the RMPUPDATE / corresponding TDX handling viakvm_mem_attrs_changed()which brings it both under KVM MMU lock and also let's it piggyback off thefault->mmu_seqhandling so it doesn't get out of sync with xarray during fault time.
That sounds better. I am just little bit worried that update_mem_attr() will end up as an SNP-only callback.
Quoted 2 lines
But would be good to hear others' opinions on this. And also confirm whether TDX needs that pre/post directmap handle or not.
Yes.
Quoted 73 lines
Thanks!
-Mike
Quoted 68 lines
Quoted 66 lines
-Mike
Quoted 62 lines
From
7c618c1f3c236c382e64680efcbe7d8a672aa870Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 opThis callback will do any platform-specific handling needed for converting pages between shared/private.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +2
-0@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu/mmu.c +1
-0@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 36/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
In reply to Michael RothQuoted 23 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The
KVM_SEV_SNP_LAUNCH_FINISHfinalize the cryptographic digest and stores it as the measurement of the guest at launch.While finalizing the launch flow, it also issues the
LAUNCH_UPDATEcommand to encrypt the VMSA pages.If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Harald Hoyer <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- .../virt/kvm/x86/amd-memory-encryption.rst | 23 ++++ arch/x86/kvm/svm/sev.c | 122 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 ++ 3 files changed, 159 insertions(+)
[...]
Quoted 27 lines
arch/x86/kvm/svm/sev.c +19
-0@@ -2280,6 +2280,109 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) snp_launch_update_gfn_handler, argp); } +static int snp_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_launch_update data = {}; +struct kvm_vcpu *vcpu; +unsigned long i; +int ret; + + data.gctx_paddr =__psp_pa (sev->snp_context); + data.page_type =SNP_PAGE_TYPE_VMSA ; + +kvm_for_each_vcpu (i, vcpu, kvm) { +struct vcpu_svm *svm =to_svm (vcpu); +u64 pfn =__pa (svm->sev_es.vmsa) >>PAGE_SHIFT ; + +/* Perform some pre-encryption checks against the VMSA */ + ret =sev_es_sync_vmsa (svm); +if (ret) +return ret;
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2311
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ +/* Transition the VMSA page to a firmware state. */ + ret =rmp_make_private (pfn,-1 ,PG_LEVEL_4K , sev->asid, true); +if (ret) +return ret; + +/* Issue the SNP command to encrypt the VMSA */ + data.address =__sme_pa (svm->sev_es.vmsa); + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , + &data, &argp->error);
There is no contract in KVM that dictates that the first entry in the vcpu list needs to be vcpu_id==0 (BSP). That means if you use a user space that spawns vCPUs in parallel on init, you will end up with the BSP behind APs in the LAUNCH_UPDATE order.
This is a problem because for LAUNCH_UPDATE, the order matters. BSP and AP vCPUs have different initial state and so if you want to reconstruct the launch digest, you need to ensure that the guest knows the order.
The easiest way I can think of to fix this is to call snp_launch_update_vmsa twice: Once filtering for vcpu_id == 0 and once for vcpu_id != 0.
Thanks,
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 04/56] KVM: Add HVA range operator
In reply to Zhi WangQuoted 36 lines · Zhi Wang
Quoted 33 lines · On Mon, 20 Feb 2023 12:37:55 -0600 Michael Roth <[email protected]> wrote:
From: Vishal Annapurve <[email protected]>
Introduce HVA range operator so that other KVM subsystems can operate on HVA range.
Signed-off-by Vishal Annapurve <[email protected]>[mdr: minor checkpatch alignment fixups]Signed-off-by Michael Roth <[email protected]>--- include/linux/kvm_host.h | 6 +++++ virt/kvm/kvm_main.c | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+)include/linux/kvm_host.h +6
-0@@ -1402,6 +1402,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm);void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end);void kvm_mmu_invalidate_end (struct kvm *kvm); +typedef int (*kvm_hva_range_op_t )(struct kvm *kvm, +struct kvm_gfn_range *range,void *data); + +int kvm_vm_do_hva_range_op (struct kvm *kvm,unsigned long hva_start, +unsigned long hva_end,kvm_hva_range_op_t handler,void *data); +long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg); long kvm_arch_vcpu_ioctl(struct file *filp,virt/kvm/kvm_main.c +0
-1--- a/virt/kvm/kvm_main.c
Quoted 8 lines
virt/kvm/kvm_main.c static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, · line 644
Quoted 5 lines
+++
b/virt/kvm/kvm_main.cvirt/kvm/kvm_main.c +0
-0@@ -642,6 +642,54 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,return (int )ret; }Below function seems a reduced duplicate of
__kvm_handle_hva_range()invirt/kvm/kvm_main.c. It would be nice to factor__kvm_handle_hva_range().
A few differences make it difficult to refactor this clearly:
- This handler is mainly used for loading initial contents into guest image before booting and doesn't rely on the MMU lock being held. It also *can't* be called with MMU lock held because it suffers from the same issue with mem_attr_update() hook where it needs to take a mutex as part of unmapping from directmap when transitioning page to private state in RMP table
- This handler wants to return an error code, as opposed to existing handlers which return a true/false values which are passed along to MMU notifier call-site and handled differently.
- This handler wants to terminate iterating through memslots as soon as it encounters the first failure, whereas the existing handlers expect to be called for each slot regardless of return value.
So it's a pretty different use-case that adds enough complexity to __kvm_handle_hva_range() that it might need be worth refactoring it, since it complicates some bits that are closely tied to dealing with invalidations where the extra complexity probably needs to be well-warranted.
I took a stab at it here for reference, but even with what seems to be the minimal set of changes it doesn't save on any code and ultimately I think it makes it harder to make sense of what going on:
https://github.com/mdroth/linux/commit/976c5fb708f7babe899fd80e27e19f8ba3f6818d
Is there a better approach?
Thanks,
-Mike
Quoted 53 lines
Quoted 52 lines
virt/kvm/kvm_main.c +48
-0+int kvm_vm_do_hva_range_op (struct kvm *kvm,unsigned long hva_start, +unsigned long hva_end,kvm_hva_range_op_t handler,void *data) +{ +int ret =0 ; +struct kvm_gfn_range gfn_range; +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +int i, idx; + +if (WARN_ON_ONCE (hva_end <= hva_start)) +return -EINVAL ; + + idx =srcu_read_lock (&kvm->srcu); + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { +struct interval_tree_node *node; + + slots =__kvm_memslots (kvm, i); +kvm_for_each_memslot_in_hva_range (node, slots, + hva_start, hva_end -1 ) { +unsigned long start, end; + + slot = container_of(node, struct kvm_memory_slot, + hva_node[slots->node_idx]); + start =max (hva_start, slot->userspace_addr); + end =min (hva_end, slot->userspace_addr + + (slot->npages <<PAGE_SHIFT )); + +/* +* {gfn(page) | page intersects with [hva_start, hva_end)} = +* {gfn_start, gfn_start+1, ..., gfn_end-1}. +*/ + gfn_range.start =hva_to_gfn_memslot (start, slot); + gfn_range.end =hva_to_gfn_memslot (end +PAGE_SIZE -1 , slot); + gfn_range.slot = slot; + + ret =handler (kvm, &gfn_range, data); +if (ret) + goto e_ret; + } + } + +e_ret: +srcu_read_unlock (&kvm->srcu, idx); + +return ret; +} + static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end,
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
In reply to Zhi WangQuoted 16 lines · Zhi Wang
Quoted 13 lines · On Tue, 21 Mar 2023 20:58:38 -0500 Michael Roth <[email protected]> wrote:
Quoted 12 lines · Zhi Wang
Quoted 9 lines · On Mon, 20 Mar 2023 13:05:43 -0500 Michael Roth <[email protected]> wrote:
Quoted 8 lines · Isaku Yamahata
Quoted 5 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>---
<snip>
Quoted 44 lines
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 7 lines
Quoted 7 lines
static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
(quoted diff) +0
-0struct kvm_memory_attributes *attrs) {@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, kvm_mmu_invalidate_end(kvm); KVM_MMU_UNLOCK(kvm);Quoted 6 lines
Review static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 3 lines
(quoted diff) +3
-0+ if (i > start) + kvm_post_mem_attrs_changed(kvm, attrs->attributes, start, i); +Doesn't
kvm_arch_set_memory_attributes()work for you? i.e the following patch. The error check andpr_warn_ratelimited()can be pushed down into the callback.This is originally how I had but when
CONFIG_PREEMPT_COUNTis set this will generate warnings for this callback as well as the invalidation callback as reported in v7 here:https://lore.kernel.org/lkml/Y80vhKwQyw8hS%2F22@notebook/
The main issue is that
kvm_mem_attrs_changed()is called while holding the KVM MMU lock, which disables preemption. But when updating attributes for SNP, we also need to remove private pages from kernel directmap, which involves acquiring a mutex which results in "BUG: scheduling while atomic" warnings.So that's why we ended up somewhat duplicating some of the logic and using a separate callback chain that happens out of KVM MMU lock.
Quoted 7 lines
Quoted 6 lines
Let's split the things of changing memory attributes:
1) Update the memory attributes in the xa array (Both TDX and SNP)
2) Zapping the EPT/NPT mappings (Required by TDX)
3) Update RMP table (Required by SNP)
4) Update the directmap of kernel (SNP, but I guess TDX needs it as well)Thanks for the effort of detailed reply. It is very informative.
Quoted 5 lines
I'm not so sure TDX requires this. I was under that impression, but Kirill raised some doubts about this and I'm not sure it's been confirmed. If it's purely an SNP thing then there may not be much value in creating a separate callback for it:
Quoted 7 lines
Quoted 2 lines
https://lore.kernel.org/linux-mm/[email protected]/T/#meba4ce80709cd3afd3818b61e6419fd800287b9e
Hmm, Krill and Isaku, can you confirm that TDX doesn't need this?
I think it is a generic requirement that TDX/SNP are not expecting the host to touch a private page either from the kernel or the userspace.
This main issue is that in the case of the RMP table, a write to a 2M mapping is interpreted as a write to a private page even if the write didn't actually touch any private pages within the range. It may be possible in firmware/hardware to distinguish between these 2 cases, but I'm not sure whether that's the case for TDX or not.
Quoted 52 lines
Quoted 4 lines
And for SNP, the current code does the unmapping/RMP update in the same function:
Quoted 9 lines
[PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
I'm not against splitting RMP/directmap handling, but just want to understand what the requirements are around that a bit better.
Does handling the #3 / RMP update /
kvm_arch_post_set_memory_attributesstuff outside of MMU lock cause issues on TDX side? What sort of handling is needed in these callbacks for TDX (if anything)?No, it doesn't cause problem for TDX as TDX doesn't need such callback.
Unlike SNP, which has (1 NPT + 1 RMP) and the enforced HW check is done by RMP, TDX has two EPT(smiliar with NPT)s (1 shared + 1 private). Converting the memory attr is achieved by zapping the mapping from one EPT and creating the mapping in the other one in the fault when guest access the memory. The fault GPA will carry the "SHARED" bit (!C-BIT), so KVM knows which EPT should be chosen for populating the mapping.
I was trying to figure out what should be the proper callback and at which layer it should sit for achieving changing memory attr for both TDX/SNP. The current callback looks a little bit hacky. Duplicate code pieces because of locks implies the SW structure might need to be re-considered.
Quoted 9 lines
Quoted 4 lines
Does SNP really need to zap the NPT mappings when changing the memory attributes? (The new mappings will be created later in the fault). I don't find this requirement from APM.
I don't think we've added anything specifically for SNP. Do you mean the generic kvm_unmap_gfn_range/kvm_flush_remote_tlbs sequence below?
kvm_vm_ioctl_set_mem_attributes():Quoted 15 lines
KVM_MMU_LOCK(kvm) kvm_mmu_invalidate_begin() ... KVM_MMU_UNLOCK(kvm) kvm_vm_set_region_attr() // xarray/attribute update ... KVM_MMU_LOCK(kvm) kvm_mem_attrs_changed(): flush |= kvm_unmap_gfn_range() if (flush) kvm_flush_remote_tlbs() KVM_MMU_UNLOCK(kvm)
Quoted 2 lines
Yes, I was talking about the sequence above. I was confused of why changing RMP requires a zapping-recreating flow of NPT in SNP.
Hmm, so you're suggesting doing something like moving the kvm_unmap_gfn_range()/kvm_flush_remote_tlbs() into .update_mem_attr() callback so the platform can decide if it needs the zap/flush rather than handling it in generic code?
If SNP really did't need the handling it's an interesting thought, but I think it is needed there after all...
Quoted 16 lines
Quoted 6 lines
In general, when the RMPUPDATE instruction happens, the TLB entries for the GPAs being modified will be flushed, so subsequent nested page fault should be able to obtain the updated mapping based on xarray/#NPF at that point. In that respect *maybe* we don't need to zap the entries there.
Quoted 9 lines
But if the nested page fault occurs before the RMPUPDATE, I think we would have a race if the above sequence isn't in place to handle the unmap/flush, since in that case we might get a stale mapping because nothing would've forced a tlbflush.
There's also stuff like the UPM selftests and SEV lazy-pinning where I think that
kvm_unmap_gfn_range()sequence is also needed. But I might be misunderstanding the question here.
Quoted 5 lines
In this case, an extra tlbflush would solve? Still, the unnecessary zapping/recreating of mapping is not promising. I understand that the way how this patch goes is probably to minimize the changes, but it would be nice to focus more on what is really needed in a common path and abstract and re-factor from there.
I tested this and just doing the tlbflush is insufficient. If the entries are still present in nested page table HV doesn't necessarily get an #NPF so the guest can still end up with a stale mapping. After a shared->private conversion the guest would cause #NPF with RMP/ENC bits set once it tries to access it as a private page, but for a private->shared conversion the guest can subsequently access the page with ENC-bit=0 without causing an #NPF, but in this case GFN can still be mapped to the PFN restrictedmem was using to back it when it was in private state, instead of normal non-restricted memory.
So it seems like SNP needs the zapping behavior as well, and that it isn't very different from the TDX/SEV-lazy/selftest users. So having common handling seems worthwhile.
Quoted 3 lines
Can you elaborate more about how the lazy-pinning unpin path is connected with the zapping here? So that I can dig more about it.
Just to be clear this is with regard to SEV lazy-pinning, which makes used of restrictedmem mainly for lazy-pinning of private pages, rather than SNP (which also inherits lazy-pinning from restrictedmem).
The main thing with SEV is that the private/shared status of a page is completely up to how the guest decides to map it in its page tables, unlike with SNP where a mismatch between guest-expected status and actual status in the RMP table will generate a #NPF. So SEV would be even more reliant on current zapping behavior to ensure the NPT will be updated.
Quoted 3 lines
Selftest is a minor case, guess we deal with them via enabling a switch. E.g. a prop in debugfs.
Wouldn't want to break things if a guest was running while selftest was running, or something along that line. Especially since it seems to be a common requirement given the above.
Quoted 29 lines
Quoted 16 lines
Quoted 4 lines
If yes, can we postpone the update of the RMP table in the later fault, like TDX? So that we can save this
update_mem_attrx86 ops as things will be solved in the SNP-specific fault handler.Hmm, I think this would be possible. But it's nice to be able to handle the RMPUPDATE as part of
KVM_SET_MEMORY_ATTRIBUTES, since it allows KVM MMU code to rely solely on xarray state and not have to query RMP table to check if a particular PFN needs an RMPUPDATE before mapping it into RMP table.At least... it would *in theory*, if the RMPUPDATE happened under protection of
mmu_invalidate_seq(in which case it could inherit all the same protections KVM MMU has around mmu_invalidate_seq/fault->mmu_seq, e.g. letting the guest retry the #PF iffault->mmu_seqis stale).Quoted 12 lines
But currently, RMPUPDATE (via
kvm_arch_post_set_memory_attributes) happens *after* the invalidation sequence above, so in theory a guest could fault on a page just after xarray state is updated, but before the RMPUPDATE has been done, in which case the KVM MMU code would properly map the page accordingly to xarray, but since RMPUPDATE wouldn't have happened yet, the state of the corresponding PFN in RMP table won't match the shared/private access type expected by the guest, so when it tries to access it it will get another #NPF with RMP bit set in the error code, which will get handled as a no-op inhandle_rmp_page_fault()(patch #44) and loop like this until the RMPUPDATE is finally done. So it still works out, but maybe not keeping as much in sync with xarray state and could be.
Quoted 1 line
I see. rmp fault handler only deals with page size mismatch for now.
That's correct.
Quoted 11 lines
Quoted 8 lines
But deferring RMPUPDATE to fault time goes in the other direction of that. Are there benefits/requirements for doing things this way for TDX? I could see it being beneficial in terms of reducing overhead for uneeded page-state transitions, since they are only done on-demand but doesn't seem like it would be that much overhead compared to some of the other operations being done.
Besides the HW design, I guess one major purpose is to optimize the booting time of VMs with large memory. Also, post migration can be another case.
It seems like without lazy-acceptance support in the guest there isn't too much reason to optimize here, since the guest will necessarily fault in every page as part of pre-accepting the memory in OVMF.
And if we're making use of lazy-acceptance, for SNP at least, we wouldn't end up getting .update_mem_attr() callbacks in the first place since those are ultimately the result of the guest issuing a shared->private conversion request, which would generally happen until just before the guest decides to accept/pvalidate that GFN. So with lazy-acceptance the guest optimizes most of this potential overhead away already.
Quoted 3 lines
Out of curiosity, What is the avg cost of RMUPDATE? Suppose it is an x86 instruction and not going through PSP firmware.
Yes, it's an x86 instruction, no firmware calls there. Average seems to be about 1us per instruction. It's not insignificant, up to 5s for a 16GB guest in a worst case scenario where the guest does not optimize for 2MB shared->private conversions and has no lazy-acceptance support, but it doesn't seem like it would be common to try to boot large guests in such a configuration.
Quoted 61 lines
Quoted 7 lines
Quoted 3 lines
If no, guess we need a x86 ops to tell if a zapping is required.
Sorry don't think I quite understand the suggestion. What would this zapping be covering vs. the invalidation sequence that currently happens in
kvm_vm_ioctl_set_mem_attributes()?I was thinking that zapping of the mapping in EPT/NPT was required by TDX while SNP might only need an RMP update + TLB flush. Thus, the abstraction of the kvm_x86_ops.update_mem_attr should sit at this level. But let's scratch this for now as I need to dig more about the lazy pinning stuff.
Quoted 37 lines
Quoted 5 lines
Back to the lock, updating RMP table doesn't require a mutex. Taking the lock is required when updating the directmap. both TDX/SNP requires this update the directmap when changing memory attributes.
Is that confirmed? If so, do you have a pointer to the associated documentation? I'm a bit unclear on this due to above-mentioned discussion.
Quoted 2 lines
Wouldn't it better to factor the touching directmap of kernel part out?
It actually needs to happen before the RMPUPDATE. As soon as there is a
shared->privateconversion in the RMP table for a particular PFN, then any access via directmap by any particular kernel thread to any PFN that happens to be in the same physical 2M range can cause an RMP fault on the host, which would be fatal. So thermpupdate()helper in this series will unmap directmap entry corresponding the PFN before ashared->privateRMPUPDATE, and restore mappings afterprivate->sharedRMPUPDATESo we could still factor it out, but it would be something like:
if (attr == private)
kvm_unmap_directmap(start, end)
kvm_mem_attrs_changed()
if (attr == shared)
kvm_map_directmap(start, end)Quoted 4 lines
Then you can call the x86
ops.update_mem_attr()inkvm_mem_attrs_changed(). And update the direct kernel mapping for both TDX/SNP in thekvm_post_mem_attrs_changed().Or, adjusting for the above logic, move the unmapping/mapping to a new
kvm_pre_mem_attrs_changed()andkvm_post_mem_attrs_changed(), respectively.Quoted 10 lines
Which seems pretty reasonable to me. Then we can:
- drop duplicating thekvm_for_each_memslot_in_gfn_range()walk stuff because we'd just need to know what PFNs to map/unmap from directmap (although we'd still need a loop aroundkvm_restrictedmem_get_pfn()for the GFN range so not necessarily prettier)
- call the RMPUPDATE / corresponding TDX handling viakvm_mem_attrs_changed()which brings it both under KVM MMU lock and also let's it piggyback off thefault->mmu_seqhandling so it doesn't get out of sync with xarray during fault time.
Quoted 2 lines
That sounds better. I am just little bit worried that
update_mem_attr()will end up as an SNP-only callback.
If it really ends up looking like an SNP-only thing, I don't see any immediate issue with deferring this handling until fault time. But the previous constraints remain:
- directmap unmap needs to happen before shared->private RMPUPDATE, can't be called while holding KVM MMU lock or other spinlock
- RMPUPDATE, and that needs to happen outside
- directmap restore needs to happen after private->shared RMPUPDATE, can't be called while holding KVM MMU lock or other spinlock
I saw the TDX patches added some x86 ops / hooks in KVM MMU to handle mapping secure pages. Is there anything there you think is worth re-using/re-purposing for SNP use-case?
Thanks,
-Mike
Quoted 80 lines
Quoted 3 lines
But would be good to hear others' opinions on this. And also confirm whether TDX needs that pre/post directmap handle or not.
Yes.
Quoted 73 lines
Thanks!
-Mike
Quoted 68 lines
Quoted 66 lines
-Mike
Quoted 62 lines
From
7c618c1f3c236c382e64680efcbe7d8a672aa870Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 opThis callback will do any platform-specific handling needed for converting pages between shared/private.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +2
-0@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu/mmu.c +1
-0@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
Quoted 69 lines · On Mon, 27 Mar 2023 23:36:35 -0500 Michael Roth <[email protected]> wrote:
Quoted 16 lines · Zhi Wang
Quoted 13 lines · On Tue, 21 Mar 2023 20:58:38 -0500 Michael Roth <[email protected]> wrote:
Quoted 12 lines · Zhi Wang
Quoted 9 lines · On Mon, 20 Mar 2023 13:05:43 -0500 Michael Roth <[email protected]> wrote:
Quoted 8 lines · Isaku Yamahata
Quoted 5 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>---
<snip>
Quoted 49 lines
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 7 lines
Quoted 7 lines
static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
(quoted diff) +0
-0struct kvm_memory_attributes *attrs) {@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, kvm_mmu_invalidate_end(kvm); KVM_MMU_UNLOCK(kvm);Quoted 6 lines
Review static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 3 lines
(quoted diff) +3
-0+ if (i > start) + kvm_post_mem_attrs_changed(kvm, attrs->attributes, start, i); +Doesn't
kvm_arch_set_memory_attributes()work for you? i.e the following patch. The error check andpr_warn_ratelimited()can be pushed down into the callback.This is originally how I had but when
CONFIG_PREEMPT_COUNTis set this will generate warnings for this callback as well as the invalidation callback as reported in v7 here:https://lore.kernel.org/lkml/Y80vhKwQyw8hS%2F22@notebook/
The main issue is that
kvm_mem_attrs_changed()is called while holding the KVM MMU lock, which disables preemption. But when updating attributes for SNP, we also need to remove private pages from kernel directmap, which involves acquiring a mutex which results in "BUG: scheduling while atomic" warnings.So that's why we ended up somewhat duplicating some of the logic and using a separate callback chain that happens out of KVM MMU lock.
Quoted 7 lines
Quoted 6 lines
Let's split the things of changing memory attributes:
1) Update the memory attributes in the xa array (Both TDX and SNP)
2) Zapping the EPT/NPT mappings (Required by TDX)
3) Update RMP table (Required by SNP)
4) Update the directmap of kernel (SNP, but I guess TDX needs it as well)Thanks for the effort of detailed reply. It is very informative.
Quoted 7 lines
I'm not so sure TDX requires this. I was under that impression, but Kirill raised some doubts about this and I'm not sure it's been confirmed. If it's purely an SNP thing then there may not be much value in creating a separate callback for it:
https://lore.kernel.org/linux-mm/[email protected]/T/#meba4ce80709cd3afd3818b61e6419fd800287b9e
Hmm, Krill and Isaku, can you confirm that TDX doesn't need this?
Quoted 9 lines
Quoted 2 lines
I think it is a generic requirement that TDX/SNP are not expecting the host to touch a private page either from the kernel or the userspace.
This main issue is that in the case of the RMP table, a write to a 2M mapping is interpreted as a write to a private page even if the write didn't actually touch any private pages within the range. It may be possible in
firmware/hardwareto distinguish between these 2 cases, but I'm not sure whether that's the case for TDX or not.
I was thinking this today. SNP uses this trick to cope with the existing HW design. It is mandatory for SNP. What was the benefit of removing/restoring the directmap for the purpose of restricting the access from the kernel for TDX? Probably catching a wrong touch to a private kernel page from the kernel space instead of triggering MCE.
My point goes to let them stay in SNP for now as they seems closely coupled with RMUPDATE so far and the timing of doing RMUPDATE is still under discussion. Maybe we invent some new pre/post, but they turn not necessary later if the timing of RMUPDATE is changed.
Quoted 55 lines
Quoted 52 lines
Quoted 4 lines
And for SNP, the current code does the unmapping/RMP update in the same function:
Quoted 9 lines
[PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
I'm not against splitting RMP/directmap handling, but just want to understand what the requirements are around that a bit better.
Does handling the #3 / RMP update /
kvm_arch_post_set_memory_attributesstuff outside of MMU lock cause issues on TDX side? What sort of handling is needed in these callbacks for TDX (if anything)?No, it doesn't cause problem for TDX as TDX doesn't need such callback.
Unlike SNP, which has (1 NPT + 1 RMP) and the enforced HW check is done by RMP, TDX has two EPT(smiliar with NPT)s (1 shared + 1 private). Converting the memory attr is achieved by zapping the mapping from one EPT and creating the mapping in the other one in the fault when guest access the memory. The fault GPA will carry the "SHARED" bit (!C-BIT), so KVM knows which EPT should be chosen for populating the mapping.
I was trying to figure out what should be the proper callback and at which layer it should sit for achieving changing memory attr for both TDX/SNP. The current callback looks a little bit hacky. Duplicate code pieces because of locks implies the SW structure might need to be re-considered.
Quoted 9 lines
Quoted 4 lines
Does SNP really need to zap the NPT mappings when changing the memory attributes? (The new mappings will be created later in the fault). I don't find this requirement from APM.
I don't think we've added anything specifically for SNP. Do you mean the generic kvm_unmap_gfn_range/kvm_flush_remote_tlbs sequence below?
kvm_vm_ioctl_set_mem_attributes():Quoted 15 lines
KVM_MMU_LOCK(kvm) kvm_mmu_invalidate_begin() ... KVM_MMU_UNLOCK(kvm) kvm_vm_set_region_attr() // xarray/attribute update ... KVM_MMU_LOCK(kvm) kvm_mem_attrs_changed(): flush |= kvm_unmap_gfn_range() if (flush) kvm_flush_remote_tlbs() KVM_MMU_UNLOCK(kvm)Quoted 2 lines
Yes, I was talking about the sequence above. I was confused of why changing RMP requires a zapping-recreating flow of NPT in SNP.
Quoted 7 lines
Hmm, so you're suggesting doing something like moving the kvm_unmap_gfn_range()/kvm_flush_remote_tlbs() into .update_mem_attr() callback so the platform can decide if it needs the zap/flush rather than handling it in generic code?
If SNP really did't need the handling it's an interesting thought, but I think it is needed there after all...
Yes. I was thinking so. When I was reading the APM, I thought one of the advantages of SNP architecture was splitting the HW-enforced check from NPT mapping management. Thus when updating the RMP, the NPT can stay as it is. I guess that's why I was so fancying of eliminating the extra zapping.
After reading the code of the UPM, I think we did need the zapping here for SNP as pages for private memory and shared memory seem from different sources. shared <->private memory conversion needs to wipe the mapping of pages from one source and go through a fault-mapping flow on the pages from the other source. Still wondering if there is any good idea to improve it based on the current approaches.
Quoted 102 lines
Quoted 17 lines
Quoted 7 lines
In general, when the RMPUPDATE instruction happens, the TLB entries for the GPAs being modified will be flushed, so subsequent nested page fault should be able to obtain the updated mapping based on xarray/#NPF at that point. In that respect *maybe* we don't need to zap the entries there.
Quoted 9 lines
But if the nested page fault occurs before the RMPUPDATE, I think we would have a race if the above sequence isn't in place to handle the unmap/flush, since in that case we might get a stale mapping because nothing would've forced a tlbflush.
There's also stuff like the UPM selftests and SEV lazy-pinning where I think that
kvm_unmap_gfn_range()sequence is also needed. But I might be misunderstanding the question here.Quoted 5 lines
In this case, an extra tlbflush would solve? Still, the unnecessary zapping/recreating of mapping is not promising. I understand that the way how this patch goes is probably to minimize the changes, but it would be nice to focus more on what is really needed in a common path and abstract and re-factor from there.
I tested this and just doing the tlbflush is insufficient. If the entries are still present in nested page table HV doesn't necessarily get an #NPF so the guest can still end up with a stale mapping. After a
shared->privateconversion the guest would cause #NPF with RMP/ENC bits set once it tries to access it as a private page, but for aprivate->sharedconversion the guest can subsequently access the page with ENC-bit=0 without causing an #NPF, but in this case GFN can still be mapped to the PFN restrictedmem was using to back it when it was in private state, instead of normal non-restricted memory.So it seems like SNP needs the zapping behavior as well, and that it isn't very different from the TDX/SEV-lazy/selftest users. So having common handling seems worthwhile.
Quoted 3 lines
Can you elaborate more about how the lazy-pinning unpin path is connected with the zapping here? So that I can dig more about it.
Just to be clear this is with regard to SEV lazy-pinning, which makes used of restrictedmem mainly for lazy-pinning of private pages, rather than SNP (which also inherits lazy-pinning from restrictedmem).
The main thing with SEV is that the private/shared status of a page is completely up to how the guest decides to map it in its page tables, unlike with SNP where a mismatch between guest-expected status and actual status in the RMP table will generate a #NPF. So SEV would be even more reliant on current zapping behavior to ensure the NPT will be updated.
Quoted 3 lines
Selftest is a minor case, guess we deal with them via enabling a switch. E.g. a prop in debugfs.
Wouldn't want to break things if a guest was running while selftest was running, or something along that line. Especially since it seems to be a common requirement given the above.
Quoted 29 lines
Quoted 16 lines
Quoted 4 lines
If yes, can we postpone the update of the RMP table in the later fault, like TDX? So that we can save this
update_mem_attrx86 ops as things will be solved in the SNP-specific fault handler.Hmm, I think this would be possible. But it's nice to be able to handle the RMPUPDATE as part of
KVM_SET_MEMORY_ATTRIBUTES, since it allows KVM MMU code to rely solely on xarray state and not have to query RMP table to check if a particular PFN needs an RMPUPDATE before mapping it into RMP table.At least... it would *in theory*, if the RMPUPDATE happened under protection of
mmu_invalidate_seq(in which case it could inherit all the same protections KVM MMU has around mmu_invalidate_seq/fault->mmu_seq, e.g. letting the guest retry the #PF iffault->mmu_seqis stale).Quoted 12 lines
But currently, RMPUPDATE (via
kvm_arch_post_set_memory_attributes) happens *after* the invalidation sequence above, so in theory a guest could fault on a page just after xarray state is updated, but before the RMPUPDATE has been done, in which case the KVM MMU code would properly map the page accordingly to xarray, but since RMPUPDATE wouldn't have happened yet, the state of the corresponding PFN in RMP table won't match the shared/private access type expected by the guest, so when it tries to access it it will get another #NPF with RMP bit set in the error code, which will get handled as a no-op inhandle_rmp_page_fault()(patch #44) and loop like this until the RMPUPDATE is finally done. So it still works out, but maybe not keeping as much in sync with xarray state and could be.Quoted 1 line
I see. rmp fault handler only deals with page size mismatch for now.
That's correct.
Quoted 9 lines
Quoted 8 lines
But deferring RMPUPDATE to fault time goes in the other direction of that. Are there benefits/requirements for doing things this way for TDX? I could see it being beneficial in terms of reducing overhead for uneeded page-state transitions, since they are only done on-demand but doesn't seem like it would be that much overhead compared to some of the other operations being done.
Quoted 7 lines
Quoted 2 lines
Besides the HW design, I guess one major purpose is to optimize the booting time of VMs with large memory. Also, post migration can be another case.
It seems like without lazy-acceptance support in the guest there isn't too much reason to optimize here, since the guest will necessarily fault in every page as part of pre-accepting the memory in OVMF.
Do you mean pre-accepting the whole system memory? or only the initial allocated memory for OVMF?
TDX has already been working in a lazy-acceptance style. I believe the lazy-acceptance will be the furture direction. But I understand that part can be in stage 2 with the lazy-acceptance for SNP.
Quoted 6 lines
And if we're making use of lazy-acceptance, for SNP at least, we wouldn't end up getting .update_mem_attr() callbacks in the first place since those are ultimately the result of the guest issuing a
shared->privateconversion request, which would generally happen until just before the guest decides to accept/pvalidate that GFN. So with lazy-acceptance the guest optimizes most of this potential overhead away already.
I understand there can be
Quoted 5 lines
Quoted 4 lines
Out of curiosity, What is the avg cost of RMUPDATE? Suppose it is an x86 instruction and not going through PSP firmware.
Quoted 7 lines
Yes, it's an x86 instruction, no firmware calls there. Average seems to be about 1us per instruction. It's not insignificant, up to 5s for a 16GB guest in a worst case scenario where the guest does not optimize for 2MB
shared->privateconversions and has no lazy-acceptance support, but it doesn't seem like it would be common to try to boot large guests in such a configuration.
I see.
Quoted 74 lines
Quoted 61 lines
Quoted 7 lines
Quoted 3 lines
If no, guess we need a x86 ops to tell if a zapping is required.
Sorry don't think I quite understand the suggestion. What would this zapping be covering vs. the invalidation sequence that currently happens in
kvm_vm_ioctl_set_mem_attributes()?I was thinking that zapping of the mapping in EPT/NPT was required by TDX while SNP might only need an RMP update + TLB flush. Thus, the abstraction of the kvm_x86_ops.update_mem_attr should sit at this level. But let's scratch this for now as I need to dig more about the lazy pinning stuff.
Quoted 37 lines
Quoted 5 lines
Back to the lock, updating RMP table doesn't require a mutex. Taking the lock is required when updating the directmap. both TDX/SNP requires this update the directmap when changing memory attributes.
Is that confirmed? If so, do you have a pointer to the associated documentation? I'm a bit unclear on this due to above-mentioned discussion.
Quoted 2 lines
Wouldn't it better to factor the touching directmap of kernel part out?
It actually needs to happen before the RMPUPDATE. As soon as there is a
shared->privateconversion in the RMP table for a particular PFN, then any access via directmap by any particular kernel thread to any PFN that happens to be in the same physical 2M range can cause an RMP fault on the host, which would be fatal. So thermpupdate()helper in this series will unmap directmap entry corresponding the PFN before ashared->privateRMPUPDATE, and restore mappings afterprivate->sharedRMPUPDATESo we could still factor it out, but it would be something like:
if (attr == private)
kvm_unmap_directmap(start, end)
kvm_mem_attrs_changed()
if (attr == shared)
kvm_map_directmap(start, end)Quoted 4 lines
Then you can call the x86
ops.update_mem_attr()inkvm_mem_attrs_changed(). And update the direct kernel mapping for both TDX/SNP in thekvm_post_mem_attrs_changed().Or, adjusting for the above logic, move the unmapping/mapping to a new
kvm_pre_mem_attrs_changed()andkvm_post_mem_attrs_changed(), respectively.Quoted 10 lines
Which seems pretty reasonable to me. Then we can:
- drop duplicating thekvm_for_each_memslot_in_gfn_range()walk stuff because we'd just need to know what PFNs to map/unmap from directmap (although we'd still need a loop aroundkvm_restrictedmem_get_pfn()for the GFN range so not necessarily prettier)
- call the RMPUPDATE / corresponding TDX handling viakvm_mem_attrs_changed()which brings it both under KVM MMU lock and also let's it piggyback off thefault->mmu_seqhandling so it doesn't get out of sync with xarray during fault time.Quoted 2 lines
That sounds better. I am just little bit worried that
update_mem_attr()will end up as an SNP-only callback.If it really ends up looking like an SNP-only thing, I don't see any immediate issue with deferring this handling until fault time. But the previous constraints remain:
- directmap unmap needs to happen before
shared->privateRMPUPDATE, can't be called while holding KVM MMU lock or other spinlock
- RMPUPDATE, and that needs to happen outside
- directmap restore needs to happen afterprivate->sharedRMPUPDATE, can't be called while holding KVM MMU lock or other spinlock
Quoted 4 lines
I saw the TDX patches added some x86 ops / hooks in KVM MMU to handle mapping secure pages. Is there anything there you think is worth re-using/re-purposing for SNP use-case?
They are mainly for the TDP MMU to manipulate the TDX secure EPT through firmware call. Due to the TDX architecture (1 shared + 1 secure EPT) and mirror secure EPT design, TDP MMU is aware there is a "secure EPT but with reduced-features", when a #PF happens which EPT should be operated, and when memory attr is changed which EPT should be zapped. Those callbacks in TDP MMU are used for maintaining the mirror secure EPT.
SNP designs in a different way. It doesn't need the TDP MMU to be aware of "there is another kind of NPT" as the enforced check is done in RMP, SNP can easily, keep all the RMP routines out of the TDP MMU. Also, SNP doesn't need
a mirror NPT. Thus, SNP doesn't need those callbacks.
In *theory*, it is possible to re-purpose some of the callbacks and merge RMP update into TDP MMU, like omitting the manipulating mirror secure EPT part for SNP, but updating the RMP in the callback of setting SPTE. But I have to think about what would be the benefit as it doesn't fit the SNP HW nature.
Quoted 84 lines
Thanks,
-Mike
Quoted 80 lines
Quoted 3 lines
But would be good to hear others' opinions on this. And also confirm whether TDX needs that pre/post directmap handle or not.
Yes.
Quoted 73 lines
Thanks!
-Mike
Quoted 68 lines
Quoted 66 lines
-Mike
Quoted 62 lines
From
7c618c1f3c236c382e64680efcbe7d8a672aa870Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 opThis callback will do any platform-specific handling needed for converting pages between shared/private.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +2
-0@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu/mmu.c +1
-0@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
In reply to Dave HansenQuoted 15 lines · Dave Hansen
Quoted 8 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. When hardware encounters an RMP-check failure, it raises a page-fault exception.
Quoted 5 lines
The
rmp_make_private()andrmp_make_shared()helpers are used to add or remove the pages from the RMP table. Improve thermp_make_private()to invalidate state so that pages cannot be used in the direct-map after they are added the RMP table, and restored to their default valid permission after the pages are removed from the RMP table.
Quoted 4 lines
This is a purely "what" changelog. It doesn't explain the "why" at all.
Could you please elaborate on why this unmapping operation is necessary?
Here's my attempt at an updated changelog that explains why this handling is needed:
With SEV-SNP, if a host attempts to write to a page that is owned by a guest (according to the SEV-SNP RMP table), the host will get a #PF with a bit set to indicate that an RMP violation occurred. This shouldn't normally occur, since guest-owned pages are encrypted, so any attempt to write to them would just result in garbage being written.
However, if a host writes to a page via a 2M/1G mapping in the host process' page table, the above #PF condition will trigger if *any* 4K sub-pages mapped by that PTE are guest-owned, even if the write is only to 4K pages that are owned by the host.
This becomes an issue with the kernel directmap, which provides a static/linear mapping of all kernel memory and defaults to using 2M pages. In cases where a page from one of these 2M ranges gets assigned to a guest, the kernel can inadvertantly trigger #PF by writing to some other page in that 2M region via the virtual addresses provided by the directmap.
Address this by removing directmap mappings for these PFNs before marking them as guest-owned in the RMP table, which will result in the original 2M mapping being split and ensure that guest-owned pages can't be written to via the kernel directmap.
-Mike
Re: [PATCH RFC v8 17/56] x86/fault: Add support to handle the RMP fault for user address
In reply to Dave HansenQuoted 12 lines · Dave Hansen
Quoted 11 lines · Michael Roth
mm/memory.c +6
-0+static int handle_split_page_fault (struct vm_fault *vmf) +{ +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +} +/* * By the time we get here, we already hold the mm semaphore * @@ -5078,6 +5084,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,pmd_migration_entry_wait (mm, vmf.pmd);
Quoted 9 lines
mm/memory.c static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, · line 5089
Quoted 5 lines
mm/memory.c +3
-0return 0 ; } + +if (flags &FAULT_FLAG_PAGE_SPLIT ) +return handle_split_page_fault (&vmf);I asked this long ago, but how do you prevent these faults from occurring on hugetlbfs mappings that can't be split?
In v6 there used to be a KVM ioctl to register a user HVA range for use with SEV-SNP guests, and as part of that registration the code would scan all the VMAs encompassed by that range and check for VM_HUGETLB in vma->vm_flags.
With v7+ this registration mechanism has been replaced with the new restricted memfd implementation provided by UPM to manage private guest memory. Normal shmem/memfd backend can specify HugeTLBFS via a MFD_HUGETLB flag when creating the memfd, but for restricted memfd no special flags are allowed, so HugeTLBFS isn't possible for the pages that are used for private memory. Though it might make sense to enforce that in SNP-specific code still, in case restricted memfd does eventually gain that ability...
But now, with v7+, the non-private memory that doesn't get allocated via restricted memfd (and thus can actually be mapped into userspace and used for things like buffers shared between host/guest), can still be allocated via HugeTLBFS since there is nothing SNP is doing to specifically guard against that. So we'd probably want to reimplement similar logic to what was in v6 to guard against this, since it's these mapping that would potentially be triggering the RMP faults and require splitting.
However...
The fact that any pages potentially triggering these #PFs are able to be mapped as 2M in the first place means that all the PFNs covered by that 2M mapping must also been allocated by via mappable/VMA memory rather than via restricted memfd where userspace mappings are not possible.
So I think we should be able to drop this patch entirely, as well as allow the use of HugeTLBFS for non-restricted memfd memory (though eventually the guest will switch all its memory to private/restricted so not gaining much there other than reducing management complexity).
-Mike
Re: [PATCH RFC v8 12/56] x86/sev: Add RMP entry lookup helpers
In reply to Vlastimil BabkaQuoted 13 lines · Vlastimil Babka
Quoted 11 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The
snp_lookup_page_in_rmptable()can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see
https://bugzilla.kernel.org/attachment.cgi?id=296015.Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Michael Roth <[email protected]>---
Quoted 8 lines
Quoted 4 lines
+/*
+ * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned,
+ * and -errno if there is no corresponding RMP entry.
+ */Hmm IMHO the kernel's idiomatic way is to return 0 on "success" and I'd assume the more intuitive expectation of success here if the entry is assigned?
In general I'd agree. Here's it's a little awkward though. snp_lookup_rmpentry() sort of wants to be a bool, where true indicates an assigned entry was found, false indicates no assigned entry.
But it also has to deal with error values, so the most direct way to encapsulate that is true == 1, false == 0, and < 0 for errors.
Inverting it to align more with kernel expections of 0 == success/true gets awkward too, because stuff like:
if (snp_lookup_rmpentry(...))
//error
still doesn't work the way most other functions written in this way would since it could still be "successful" if we were expecting PFN to be in shared state. So the return value needs special handling there too.
Would it make sense to define it something like this?:
/*
* Query information about the RMP entry corresponding to the given
* PFN.
*
* Returns 0 on success, and -errno if there was a problem accessing
* the RMP entry.
*/
int snp_lookup_rmpentry(u64 pfn, int *level, bool *assigned)
Quoted 3 lines
The various callers seem to differ though so I guess it depends on context. Some however don't distinguish their "failure" from an ERR and maybe they should, at least for the purposes of the various printks?
Yes, regardless of what we decide above, the call-sites should properly distinguish between failure/assigned/not-assigned and report the information accordingly. I'll get those fixed up where needed.
Thanks,
-Mike
Quoted 13 lines
Quoted 12 lines
arch/x86/kernel/sev.c +11
-0+int snp_lookup_rmpentry (u64 pfn,int *level) +{ +struct rmpentry *e; + + e =__snp_lookup_rmpentry (pfn, level); +if (IS_ERR (e)) +return PTR_ERR (e); + +return !!rmpentry_assigned (e); +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);
Re: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op
In reply to Zhi WangQuoted 82 lines · Zhi Wang
Quoted 69 lines · On Mon, 27 Mar 2023 23:36:35 -0500 Michael Roth <[email protected]> wrote:
Quoted 16 lines · Zhi Wang
Quoted 13 lines · On Tue, 21 Mar 2023 20:58:38 -0500 Michael Roth <[email protected]> wrote:
Quoted 12 lines · Zhi Wang
Quoted 9 lines · On Mon, 20 Mar 2023 13:05:43 -0500 Michael Roth <[email protected]> wrote:
Quoted 8 lines · Isaku Yamahata
Quoted 5 lines · Michael Roth
This callback will do any platform-specific handling needed for converting pages between shared/private.
Signed-off-by Michael Roth <[email protected]>---
<snip>
Quoted 49 lines
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 7 lines
Quoted 7 lines
static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
(quoted diff) +0
-0struct kvm_memory_attributes *attrs) {@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, kvm_mmu_invalidate_end(kvm); KVM_MMU_UNLOCK(kvm);Quoted 6 lines
Review static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, · line 2633
Quoted 3 lines
(quoted diff) +3
-0+ if (i > start) + kvm_post_mem_attrs_changed(kvm, attrs->attributes, start, i); +Doesn't
kvm_arch_set_memory_attributes()work for you? i.e the following patch. The error check andpr_warn_ratelimited()can be pushed down into the callback.This is originally how I had but when
CONFIG_PREEMPT_COUNTis set this will generate warnings for this callback as well as the invalidation callback as reported in v7 here:https://lore.kernel.org/lkml/Y80vhKwQyw8hS%2F22@notebook/
The main issue is that
kvm_mem_attrs_changed()is called while holding the KVM MMU lock, which disables preemption. But when updating attributes for SNP, we also need to remove private pages from kernel directmap, which involves acquiring a mutex which results in "BUG: scheduling while atomic" warnings.So that's why we ended up somewhat duplicating some of the logic and using a separate callback chain that happens out of KVM MMU lock.
Quoted 7 lines
Quoted 6 lines
Let's split the things of changing memory attributes:
1) Update the memory attributes in the xa array (Both TDX and SNP)
2) Zapping the EPT/NPT mappings (Required by TDX)
3) Update RMP table (Required by SNP)
4) Update the directmap of kernel (SNP, but I guess TDX needs it as well)Thanks for the effort of detailed reply. It is very informative.
Quoted 7 lines
I'm not so sure TDX requires this. I was under that impression, but Kirill raised some doubts about this and I'm not sure it's been confirmed. If it's purely an SNP thing then there may not be much value in creating a separate callback for it:
https://lore.kernel.org/linux-mm/[email protected]/T/#meba4ce80709cd3afd3818b61e6419fd800287b9e
Hmm, Krill and Isaku, can you confirm that TDX doesn't need this?
Quoted 9 lines
Quoted 2 lines
I think it is a generic requirement that TDX/SNP are not expecting the host to touch a private page either from the kernel or the userspace.
This main issue is that in the case of the RMP table, a write to a 2M mapping is interpreted as a write to a private page even if the write didn't actually touch any private pages within the range. It may be possible in
firmware/hardwareto distinguish between these 2 cases, but I'm not sure whether that's the case for TDX or not.
Quoted 5 lines
I was thinking this today. SNP uses this trick to cope with the existing HW design. It is mandatory for SNP. What was the benefit of removing/restoring the directmap for the purpose of restricting the access from the kernel for TDX? Probably catching a wrong touch to a private kernel page from the kernel space instead of triggering MCE.
Not sure, but yah might've been something like that.
Quoted 5 lines
My point goes to let them stay in SNP for now as they seems closely coupled with RMUPDATE so far and the timing of doing RMUPDATE is still under discussion. Maybe we invent some new pre/post, but they turn not necessary later if the timing of RMUPDATE is changed.
Makes sense.
Quoted 61 lines
Quoted 61 lines
Quoted 53 lines
Quoted 5 lines
And for SNP, the current code does the unmapping/RMP update in the same function:
Quoted 9 lines
[PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
I'm not against splitting RMP/directmap handling, but just want to understand what the requirements are around that a bit better.
Does handling the #3 / RMP update /
kvm_arch_post_set_memory_attributesstuff outside of MMU lock cause issues on TDX side? What sort of handling is needed in these callbacks for TDX (if anything)?No, it doesn't cause problem for TDX as TDX doesn't need such callback.
Unlike SNP, which has (1 NPT + 1 RMP) and the enforced HW check is done by RMP, TDX has two EPT(smiliar with NPT)s (1 shared + 1 private). Converting the memory attr is achieved by zapping the mapping from one EPT and creating the mapping in the other one in the fault when guest access the memory. The fault GPA will carry the "SHARED" bit (!C-BIT), so KVM knows which EPT should be chosen for populating the mapping.
I was trying to figure out what should be the proper callback and at which layer it should sit for achieving changing memory attr for both TDX/SNP. The current callback looks a little bit hacky. Duplicate code pieces because of locks implies the SW structure might need to be re-considered.
Quoted 9 lines
Quoted 4 lines
Does SNP really need to zap the NPT mappings when changing the memory attributes? (The new mappings will be created later in the fault). I don't find this requirement from APM.
I don't think we've added anything specifically for SNP. Do you mean the generic kvm_unmap_gfn_range/kvm_flush_remote_tlbs sequence below?
kvm_vm_ioctl_set_mem_attributes():Quoted 15 lines
KVM_MMU_LOCK(kvm) kvm_mmu_invalidate_begin() ... KVM_MMU_UNLOCK(kvm) kvm_vm_set_region_attr() // xarray/attribute update ... KVM_MMU_LOCK(kvm) kvm_mem_attrs_changed(): flush |= kvm_unmap_gfn_range() if (flush) kvm_flush_remote_tlbs() KVM_MMU_UNLOCK(kvm)Quoted 2 lines
Yes, I was talking about the sequence above. I was confused of why changing RMP requires a zapping-recreating flow of NPT in SNP.
Hmm, so you're suggesting doing something like moving the kvm_unmap_gfn_range()/kvm_flush_remote_tlbs() into .update_mem_attr() callback so the platform can decide if it needs the zap/flush rather than handling it in generic code?
Quoted 7 lines
Quoted 2 lines
If SNP really did't need the handling it's an interesting thought, but I think it is needed there after all...
Yes. I was thinking so. When I was reading the APM, I thought one of the advantages of SNP architecture was splitting the HW-enforced check from NPT mapping management. Thus when updating the RMP, the NPT can stay as it is. I guess that's why I was so fancying of eliminating the extra zapping.
Yah it was an interesting thought. Maybe before the switch to UPM it might've been possible to avoid the zapping, but seems like it's a fairly general requirement now.
Quoted 7 lines
After reading the code of the UPM, I think we did need the zapping here for SNP as pages for private memory and shared memory seem from different sources. shared <->private memory conversion needs to wipe the mapping of pages from one source and go through a fault-mapping flow on the pages from the other source. Still wondering if there is any good idea to improve it based on the current approaches.
It may still be possible to avoiding zapping for shared->private conversions since the subsequent access with C-bit set would cause an #NPF with RMP bit set, but yah, it won't work for private->shared and doesn't seem worth it to complicate the logic for anything SNP-specific unless it would make a significant performance impact, but I think it would be lost in the noise vs. RMPUPDATE/initial faulting of private pages/directmap handling/guest exits for page-state change/etc.
Quoted 106 lines
Quoted 106 lines
Quoted 18 lines
Quoted 8 lines
In general, when the RMPUPDATE instruction happens, the TLB entries for the GPAs being modified will be flushed, so subsequent nested page fault should be able to obtain the updated mapping based on xarray/#NPF at that point. In that respect *maybe* we don't need to zap the entries there.
Quoted 9 lines
But if the nested page fault occurs before the RMPUPDATE, I think we would have a race if the above sequence isn't in place to handle the unmap/flush, since in that case we might get a stale mapping because nothing would've forced a tlbflush.
There's also stuff like the UPM selftests and SEV lazy-pinning where I think that
kvm_unmap_gfn_range()sequence is also needed. But I might be misunderstanding the question here.Quoted 5 lines
In this case, an extra tlbflush would solve? Still, the unnecessary zapping/recreating of mapping is not promising. I understand that the way how this patch goes is probably to minimize the changes, but it would be nice to focus more on what is really needed in a common path and abstract and re-factor from there.
I tested this and just doing the tlbflush is insufficient. If the entries are still present in nested page table HV doesn't necessarily get an #NPF so the guest can still end up with a stale mapping. After a
shared->privateconversion the guest would cause #NPF with RMP/ENC bits set once it tries to access it as a private page, but for aprivate->sharedconversion the guest can subsequently access the page with ENC-bit=0 without causing an #NPF, but in this case GFN can still be mapped to the PFN restrictedmem was using to back it when it was in private state, instead of normal non-restricted memory.So it seems like SNP needs the zapping behavior as well, and that it isn't very different from the TDX/SEV-lazy/selftest users. So having common handling seems worthwhile.
Quoted 3 lines
Can you elaborate more about how the lazy-pinning unpin path is connected with the zapping here? So that I can dig more about it.
Just to be clear this is with regard to SEV lazy-pinning, which makes used of restrictedmem mainly for lazy-pinning of private pages, rather than SNP (which also inherits lazy-pinning from restrictedmem).
The main thing with SEV is that the private/shared status of a page is completely up to how the guest decides to map it in its page tables, unlike with SNP where a mismatch between guest-expected status and actual status in the RMP table will generate a #NPF. So SEV would be even more reliant on current zapping behavior to ensure the NPT will be updated.
Quoted 3 lines
Selftest is a minor case, guess we deal with them via enabling a switch. E.g. a prop in debugfs.
Wouldn't want to break things if a guest was running while selftest was running, or something along that line. Especially since it seems to be a common requirement given the above.
Quoted 29 lines
Quoted 16 lines
Quoted 4 lines
If yes, can we postpone the update of the RMP table in the later fault, like TDX? So that we can save this
update_mem_attrx86 ops as things will be solved in the SNP-specific fault handler.Hmm, I think this would be possible. But it's nice to be able to handle the RMPUPDATE as part of
KVM_SET_MEMORY_ATTRIBUTES, since it allows KVM MMU code to rely solely on xarray state and not have to query RMP table to check if a particular PFN needs an RMPUPDATE before mapping it into RMP table.At least... it would *in theory*, if the RMPUPDATE happened under protection of
mmu_invalidate_seq(in which case it could inherit all the same protections KVM MMU has around mmu_invalidate_seq/fault->mmu_seq, e.g. letting the guest retry the #PF iffault->mmu_seqis stale).Quoted 12 lines
But currently, RMPUPDATE (via
kvm_arch_post_set_memory_attributes) happens *after* the invalidation sequence above, so in theory a guest could fault on a page just after xarray state is updated, but before the RMPUPDATE has been done, in which case the KVM MMU code would properly map the page accordingly to xarray, but since RMPUPDATE wouldn't have happened yet, the state of the corresponding PFN in RMP table won't match the shared/private access type expected by the guest, so when it tries to access it it will get another #NPF with RMP bit set in the error code, which will get handled as a no-op inhandle_rmp_page_fault()(patch #44) and loop like this until the RMPUPDATE is finally done. So it still works out, but maybe not keeping as much in sync with xarray state and could be.Quoted 1 line
I see. rmp fault handler only deals with page size mismatch for now.
That's correct.
Quoted 11 lines
Quoted 8 lines
But deferring RMPUPDATE to fault time goes in the other direction of that. Are there benefits/requirements for doing things this way for TDX? I could see it being beneficial in terms of reducing overhead for uneeded page-state transitions, since they are only done on-demand but doesn't seem like it would be that much overhead compared to some of the other operations being done.
Besides the HW design, I guess one major purpose is to optimize the booting time of VMs with large memory. Also, post migration can be another case.
Quoted 7 lines
Quoted 4 lines
It seems like without lazy-acceptance support in the guest there isn't too much reason to optimize here, since the guest will necessarily fault in every page as part of pre-accepting the memory in OVMF.
Do you mean pre-accepting the whole system memory? or only the initial allocated memory for OVMF?
I mean "pre-accepting" in the sense of "guests that don't have lazy-acceptance enabled", so yes the whole of system memory.
Quoted 4 lines
TDX has already been working in a lazy-acceptance style. I believe the lazy-acceptance will be the furture direction. But I understand that part can be in stage 2 with the lazy-acceptance for SNP.
For SNP the OVMF bits are there:
https://www.mail-archive.com/[email protected]/msg53857.html
but guest-side kernel support still requires these patches getting upstream:
Kirill's base+TDX lazy-acceptance support:
https://lore.kernel.org/linux-mm/[email protected]/T/
Tom's follow-on SNP support:
https://lore.kernel.org/lkml/[email protected]/
Dionna's patch to instruct OVMF to not pre-accept remaining memory after EFI_EXIT_BOOT_SERVICES:
https://lore.kernel.org/lkml/[email protected]/T/
But yes, I agree lazy-acceptance will probably be the common default for TDX/SNP guests, where we'd only be zapping entries that we know the guest is going to be using anyway, as well as avoiding all the other related overhead.
Quoted 112 lines
Quoted 7 lines
And if we're making use of lazy-acceptance, for SNP at least, we wouldn't end up getting .update_mem_attr() callbacks in the first place since those are ultimately the result of the guest issuing a
shared->privateconversion request, which would generally happen until just before the guest decides to accept/pvalidate that GFN. So with lazy-acceptance the guest optimizes most of this potential overhead away already.I understand there can be
Quoted 5 lines
Quoted 4 lines
Out of curiosity, What is the avg cost of RMUPDATE? Suppose it is an x86 instruction and not going through PSP firmware.
Quoted 7 lines
Yes, it's an x86 instruction, no firmware calls there. Average seems to be about 1us per instruction. It's not insignificant, up to 5s for a 16GB guest in a worst case scenario where the guest does not optimize for 2MB
shared->privateconversions and has no lazy-acceptance support, but it doesn't seem like it would be common to try to boot large guests in such a configuration.I see.
Quoted 74 lines
Quoted 61 lines
Quoted 7 lines
Quoted 3 lines
If no, guess we need a x86 ops to tell if a zapping is required.
Sorry don't think I quite understand the suggestion. What would this zapping be covering vs. the invalidation sequence that currently happens in
kvm_vm_ioctl_set_mem_attributes()?I was thinking that zapping of the mapping in EPT/NPT was required by TDX while SNP might only need an RMP update + TLB flush. Thus, the abstraction of the kvm_x86_ops.update_mem_attr should sit at this level. But let's scratch this for now as I need to dig more about the lazy pinning stuff.
Quoted 37 lines
Quoted 5 lines
Back to the lock, updating RMP table doesn't require a mutex. Taking the lock is required when updating the directmap. both TDX/SNP requires this update the directmap when changing memory attributes.
Is that confirmed? If so, do you have a pointer to the associated documentation? I'm a bit unclear on this due to above-mentioned discussion.
Quoted 2 lines
Wouldn't it better to factor the touching directmap of kernel part out?
It actually needs to happen before the RMPUPDATE. As soon as there is a
shared->privateconversion in the RMP table for a particular PFN, then any access via directmap by any particular kernel thread to any PFN that happens to be in the same physical 2M range can cause an RMP fault on the host, which would be fatal. So thermpupdate()helper in this series will unmap directmap entry corresponding the PFN before ashared->privateRMPUPDATE, and restore mappings afterprivate->sharedRMPUPDATESo we could still factor it out, but it would be something like:
if (attr == private)
kvm_unmap_directmap(start, end)
kvm_mem_attrs_changed()
if (attr == shared)
kvm_map_directmap(start, end)Quoted 4 lines
Then you can call the x86
ops.update_mem_attr()inkvm_mem_attrs_changed(). And update the direct kernel mapping for both TDX/SNP in thekvm_post_mem_attrs_changed().Or, adjusting for the above logic, move the unmapping/mapping to a new
kvm_pre_mem_attrs_changed()andkvm_post_mem_attrs_changed(), respectively.Quoted 10 lines
Which seems pretty reasonable to me. Then we can:
- drop duplicating thekvm_for_each_memslot_in_gfn_range()walk stuff because we'd just need to know what PFNs to map/unmap from directmap (although we'd still need a loop aroundkvm_restrictedmem_get_pfn()for the GFN range so not necessarily prettier)
- call the RMPUPDATE / corresponding TDX handling viakvm_mem_attrs_changed()which brings it both under KVM MMU lock and also let's it piggyback off thefault->mmu_seqhandling so it doesn't get out of sync with xarray during fault time.Quoted 2 lines
That sounds better. I am just little bit worried that
update_mem_attr()will end up as an SNP-only callback.If it really ends up looking like an SNP-only thing, I don't see any immediate issue with deferring this handling until fault time. But the previous constraints remain:
- directmap unmap needs to happen before
shared->privateRMPUPDATE, can't be called while holding KVM MMU lock or other spinlock
- RMPUPDATE, and that needs to happen outside
- directmap restore needs to happen afterprivate->sharedRMPUPDATE, can't be called while holding KVM MMU lock or other spinlockQuoted 4 lines
I saw the TDX patches added some x86 ops / hooks in KVM MMU to handle mapping secure pages. Is there anything there you think is worth re-using/re-purposing for SNP use-case?
They are mainly for the TDP MMU to manipulate the TDX secure EPT through firmware call. Due to the TDX architecture (1 shared + 1 secure EPT) and mirror secure EPT design, TDP MMU is aware there is a "secure EPT but with reduced-features", when a #PF happens which EPT should be operated, and when memory attr is changed which EPT should be zapped. Those callbacks in TDP MMU are used for maintaining the mirror secure EPT.
SNP designs in a different way. It doesn't need the TDP MMU to be aware of "there is another kind of NPT" as the enforced check is done in RMP, SNP can easily, keep all the RMP routines out of the TDP MMU. Also, SNP doesn't need
a mirror NPT. Thus, SNP doesn't need those callbacks.
Quoted 4 lines
In *theory*, it is possible to re-purpose some of the callbacks and merge RMP update into TDP MMU, like omitting the manipulating mirror secure EPT part for SNP, but updating the RMP in the callback of setting SPTE. But I have to think about what would be the benefit as it doesn't fit the SNP HW nature.
Yah, can't think of any benefit currently. With both pre-acceptance and lazy-acceptance those pages will get touched shorty after KVM_SET_MEMORY_ATTRIBUTES, so the more obvious benefit of avoiding unecessary RMPUPDATEs doesn't end up working here. And in terms of re-using callbacks it seems like the TDX and SNP hooks are both where it makes the most sense to have them. Would be nice to have a shared callback for this but might not be the right approach here.
-Mike
Quoted 87 lines
Quoted 86 lines
Thanks,
-Mike
Quoted 80 lines
Quoted 3 lines
But would be good to hear others' opinions on this. And also confirm whether TDX needs that pre/post directmap handle or not.
Yes.
Quoted 73 lines
Thanks!
-Mike
Quoted 68 lines
Quoted 66 lines
-Mike
Quoted 62 lines
From
7c618c1f3c236c382e64680efcbe7d8a672aa870Mon Sep 17 00:00:00 2001
Message-Id: <7c618c1f3c236c382e64680efcbe7d8a672aa870.1679114841.git.isaku.yamahata@intel.com>
In-Reply-To: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
References: <428a676face7a06a90e59dca1c32941c9b6ee001.1679114841.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:00:09 -0700
Subject: [PATCH 4/4] KVM: x86: Add 'set_mem_attr' x86 opThis callback will do any platform-specific handling needed for converting pages between shared/private.
Originally-by Michael Roth <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 4 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -100,6 +100,7 @@ KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) KVM_X86_OP(fault_is_private) +KVM_X86_OP_OPTIONAL(set_mem_attr) KVM_X86_OP_OPTIONAL(link_private_spt) KVM_X86_OP_OPTIONAL(free_private_spt) KVM_X86_OP_OPTIONAL(split_private_spt)arch/x86/include/asm/kvm_host.h +2
-0@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code); +void (*set_mem_attr)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int attr,gfn_t start,gfn_t end);int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level,void *private_spt);arch/x86/kvm/mmu/mmu.c +1
-0@@ -7908,6 +7908,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end) {kvm_update_lpage_mixed_flag (kvm ,slot ,true ,attrs ,start ,end ); +static_call (kvm_x86_set_mem_attr)(kvm, slot, attrs, start, end); } void kvm_memory_attributes_create_memslot(struct kvm *kvm,Signature · 4 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH RFC v8 04/56] KVM: Add HVA range operator
In reply to Michael RothQuoted 67 lines · On Sun, 26 Mar 2023 19:34:44 -0500 Michael Roth <[email protected]> wrote:
Quoted 36 lines · Zhi Wang
Quoted 33 lines · On Mon, 20 Feb 2023 12:37:55 -0600 Michael Roth <[email protected]> wrote:
From: Vishal Annapurve <[email protected]>
Introduce HVA range operator so that other KVM subsystems can operate on HVA range.
Signed-off-by Vishal Annapurve <[email protected]>[mdr: minor checkpatch alignment fixups]Signed-off-by Michael Roth <[email protected]>--- include/linux/kvm_host.h | 6 +++++ virt/kvm/kvm_main.c | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+)include/linux/kvm_host.h +6
-0@@ -1402,6 +1402,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm);void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end);void kvm_mmu_invalidate_end (struct kvm *kvm); +typedef int (*kvm_hva_range_op_t )(struct kvm *kvm, +struct kvm_gfn_range *range,void *data); + +int kvm_vm_do_hva_range_op (struct kvm *kvm,unsigned long hva_start, +unsigned long hva_end,kvm_hva_range_op_t handler,void *data); +long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg); long kvm_arch_vcpu_ioctl(struct file *filp,virt/kvm/kvm_main.c +0
-1--- a/virt/kvm/kvm_main.cQuoted 8 lines
virt/kvm/kvm_main.c static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, · line 644
Quoted 5 lines
+++
b/virt/kvm/kvm_main.cvirt/kvm/kvm_main.c +0
-0@@ -642,6 +642,54 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,return (int )ret; }Below function seems a reduced duplicate of
__kvm_handle_hva_range()invirt/kvm/kvm_main.c. It would be nice to factor__kvm_handle_hva_range().A few differences make it difficult to refactor this clearly:
- This handler is mainly used for loading initial contents into guest image before booting and doesn't rely on the MMU lock being held. It also *can't* be called with MMU lock held because it suffers from the same issue with
mem_attr_update()hook where it needs to take a mutex as part of unmapping from directmap when transitioning page to private state in RMP table
- This handler wants to return an error code, as opposed to existing handlers which return a true/false values which are passed along to MMU notifier call-site and handled differently.
- This handler wants to terminate iterating through memslots as soon as it encounters the first failure, whereas the existing handlers expect to be called for each slot regardless of return value.So it's a pretty different use-case that adds enough complexity to
__kvm_handle_hva_range()that it might need be worth refactoring it, since it complicates some bits that are closely tied to dealing with invalidations where the extra complexity probably needs to be well-warranted.
Quoted 8 lines
I took a stab at it here for reference, but even with what seems to be the minimal set of changes it doesn't save on any code and ultimately I think it makes it harder to make sense of what going on:
https://github.com/mdroth/linux/commit/976c5fb708f7babe899fd80e27e19f8ba3f6818d
Is there a better approach?
Those requirements looks pretty suitable for kvm_handle_hva_range(). Guess we just need to extend the iterator a little bit.
My ideas:
1) Add a lock flag in struct kvm_hva_range to indicate if kvm_lock is required
or not during the iteration. Check the flag with if (!locked && hva_range.need_lock). Then the unlock part can be left un-touched.
2) Add an error code in struct kvm_gfn_range, the handler can set it so that __kvm_handle_hva_range() can check gfn_range.err after ret|= handler(xxx); If the err is set, bail out.
3) Return the gfn_range.err to the caller. The caller can decide how to convert it (to boolean or keep it)
4) Set hva_range.need_lock in the existing and the new caller.
How about this?
Quoted 57 lines
Thanks,
-Mike
Quoted 53 lines
Quoted 52 lines
virt/kvm/kvm_main.c +48
-0+int kvm_vm_do_hva_range_op (struct kvm *kvm,unsigned long hva_start, +unsigned long hva_end,kvm_hva_range_op_t handler,void *data) +{ +int ret =0 ; +struct kvm_gfn_range gfn_range; +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +int i, idx; + +if (WARN_ON_ONCE (hva_end <= hva_start)) +return -EINVAL ; + + idx =srcu_read_lock (&kvm->srcu); + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { +struct interval_tree_node *node; + + slots =__kvm_memslots (kvm, i); +kvm_for_each_memslot_in_hva_range (node, slots, + hva_start, hva_end -1 ) { +unsigned long start, end; + + slot = container_of(node, struct kvm_memory_slot, + hva_node[slots->node_idx]); + start =max (hva_start, slot->userspace_addr); + end =min (hva_end, slot->userspace_addr + + (slot->npages <<PAGE_SHIFT )); + +/* +* {gfn(page) | page intersects with [hva_start, hva_end)} = +* {gfn_start, gfn_start+1, ..., gfn_end-1}. +*/ + gfn_range.start =hva_to_gfn_memslot (start, slot); + gfn_range.end =hva_to_gfn_memslot (end +PAGE_SIZE -1 , slot); + gfn_range.slot = slot; + + ret =handler (kvm, &gfn_range, data); +if (ret) + goto e_ret; + } + } + +e_ret: +srcu_read_unlock (&kvm->srcu, idx); + +return ret; +} + static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end,
Re: [PATCH RFC v8 47/56] KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Alexander GrafQuoted 51 lines · Alexander Graf
Quoted 36 lines · Michael Roth
From: Tom Lendacky <[email protected]>
Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI.
A new event,
KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running.For CREATE
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent is added to the vCPU and then the vCPU is kicked.For
CREATE_ON_INIT:
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU.For DESTROY:
The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent is added to vCPU and then the vCPU is kicked.The
KVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to theKVM_MP_STATE_UNINITIALIZEDstate, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set toKVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left asKVM_MP_STATE_UNINITIALIZEDto prevent it from being run.Quoted 5 lines
Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: add handling for restrictedmem]Signed-off-by Michael Roth <[email protected]>What is the intended boot sequence for SEV-SNP guests? FWIW with this interface in place, guests will typically use in-guest VMSA pages to hold secondary vcpu state. But that means we're now allocating 4kb of memory for every vcpu that we create that will be for most of the guest's lifetime superfluous.
Quoted 3 lines
Wouldn't it make more sense to have a model where we only allocate the VMSA for the boot CPU and leave secondary allocation to the guest? We already need firmware changes for SEV-SNP - may as well make this one more.
I don't think we'd necessarily need a firmware change. We could just free original VMSA back to the hypervisor as soon as those APs come online. The down-side to that versus deferring cleaning till guest shutdown is there is some flushing activity (see:sev_flush_encrypted_page()) that would now likely be occuring during guest boot up where the overhead might be more noticeable. But for SNP the host likely supports X86_FEATURE_SME_COHERENT so the overhead probably isn't that bad.
Quoted 15 lines
[...]
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+ +static int sev_snp_ap_creation (struct vcpu_svm *svm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (svm->vcpu.kvm)->sev_info; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm_vcpu *target_vcpu; +struct vcpu_svm *target_svm; +unsigned int request; +unsigned int apic_id; +bool kick; +int ret; +
Quoted 8 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3688
Quoted 5 lines
arch/x86/kvm/svm/sev.c +5
-0+ request =lower_32_bits (svm->vmcb->control.exit_info_1); + apic_id =upper_32_bits (svm->vmcb->control.exit_info_1); + +/* Validate the APIC ID */ + target_vcpu =kvm_get_vcpu_by_id (vcpu->kvm, apic_id);Out of curiosity: The target CPU can be my own vCPU, right?
I don't think that would be the normal behavior, but maybe with some care it's possible for a guest to do things that way. I haven't seen anything strictly prohibiting this in the relevant specs.
Quoted 54 lines
Quoted 54 lines
arch/x86/kvm/svm/sev.c +52
-0+if (!target_vcpu) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP APIC ID [%#x] from guest\n" , + apic_id); +return -EINVAL ; + } + + ret =0 ; + + target_svm =to_svm (target_vcpu); + +/* +* The target vCPU is valid, so the vCPU will be kicked unless the +* request is for CREATE_ON_INIT. For any errors at this stage, the +* kick will place the vCPU in an non-runnable state. +*/ + kick = true; + +mutex_lock (&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; + target_svm->sev_es.snp_ap_create = true; + +/* Interrupt injection mode shouldn't change for AP creation */ +if (request <SVM_VMGEXIT_AP_DESTROY ) { +u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX ]; + sev_features ^= sev->sev_features; +if (sev_features &SVM_SEV_FEAT_INT_INJ_MODES ) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP injection mode [%#lx] from guest\n" , + vcpu->arch.regs[VCPU_REGS_RAX ]); + ret = -EINVAL ; + goto out; + } + } + +switch (request) { +case SVM_VMGEXIT_AP_CREATE_ON_INIT : + kick = false; + fallthrough; +case SVM_VMGEXIT_AP_CREATE : +if (!page_address_valid (vcpu, svm->vmcb->control.exit_info_2)) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP VMSA address [%#llx] from guest\n" , + svm->vmcb->control.exit_info_2); + ret = -EINVAL ; + goto out; + } + +/* +* Malicious guest can RMPADJUST a large page into VMSA which +* will hit the SNP erratum where the CPU will incorrectly signal +* an RMP violation #PF if a hugepage collides with the RMP entry
Quoted 8 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3742
Quoted 2 lines
arch/x86/kvm/svm/sev.c +2
-0+* of VMSA page, reject the AP CREATE request if VMSA address from +* guest is 2M aligned. This will break genuine current Linux kernels that just happen to allocate a guest page, no? In fact, given enough vCPUs you're almost guaranteed to hit an aligned structure somewhere. What is the guest supposed to do in that situation?
The initial SNP support for guest kernels already made use of snp_alloc_vmsa_page() to do the appropriate workaround to avoid allocating 2MB-aligned VMSA pages.
-Mike
Quoted 12 lines
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 47/56] KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Alexander GrafQuoted 11 lines · Alexander Graf
Quoted 9 lines · Zhi Wang
Quoted 6 lines · On Fri, 24 Feb 2023 13:37:48 +0100 Alexander Graf <[email protected]> wrote:
Quoted 5 lines · Michael Roth
From: Tom Lendacky <[email protected]>
Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI.
<snip>
Quoted 62 lines
Quoted 56 lines
Quoted 16 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3754
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+*/ +if (IS_ALIGNED (svm->vmcb->control.exit_info_2,PMD_SIZE )) { +vcpu_unimpl (vcpu, +"vmgexit: AP VMSA address [%llx] from guest is unsafe as it is 2M aligned\n" , + svm->vmcb->control.exit_info_2); + ret = -EINVAL ; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; +break ; +case SVM_VMGEXIT_AP_DESTROY :I don't understand the destroy path. Why does this case destroy anything?
Quoted 15 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3766
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+break ; +default : +vcpu_unimpl (vcpu,"vmgexit: invalid AP creation request [%#x] from guest\n" , + request); + ret = -EINVAL ; +break ; + } + +out: +if (kick) { +if (target_vcpu->arch.mp_state ==KVM_MP_STATE_UNINITIALIZED ) + target_vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;What if the guest AP goes through a create -> destroy -> create cycle? Will it stay runnable while destroyed?
The code is not very straightforward.
1)
target_svm->sev_es.snp_vmsa_gpais set asINVALID_PAGEin the beginning of this function.2) If a DESTROY is hit in this function,
target_svm->sev_es.snp_vmsa_gpawill be left asINVALID_PAGE.3) At the end of this function, it calls
kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE).4) In the
vcpu_enter_guest(), the kvm_vcpu_reset()->sev_snp_init_protected_guest_state() ->__sev_snp_init_protected_guest_state() is called.5) The
mp_stateis set toKVM_MP_STATE_STOPPEDby default and the runtime VMSA is cleared. Then the it will be initialized according to the guest's configuration.6) As the
snp_vmsa_gpais set asINVALID_PAGEin 1, themp_statewill be left asKVM_MP_STATE_STOPPED.7) With this code piece:
arch/x86/kvm/x86.c +3
-0+kvm_vcpu_reset (vcpu, true); +if (vcpu->arch.mp_state !=KVM_MP_STATE_RUNNABLE ) + goto out;Quoted 1 line
vcpu_enter_guest()bails out.Thanks a lot Zhi for the detailed explanation! I think this code flow wants to become slightly more obvious. For example, if we just said
Quoted 8 lines
case
SVM_VMGEXIT_AP_DESTROY:
/* This will tell__sev_snp_update_protected_guest_stateto unmap the VMSA */
target_svm->sev_es.snp_vmsa_gpa=INVALID_PAGE;
break;We'd get a big win in readability with little effort. It makes it immediately obvious where to look for the destroy operation.
The target->snp_vmsa_gpa value mainly serves to cache the GPA of the new VMSA up until the point where KVM points the target vCPU's VMCB over to using it as part of sev_snp_init_protected_guest_state(). We want to make sure it is either INVALID_PAGE, or whatever GPA the guest want us to set it to.
Once sev_snp_init_protected_guest_state() processes the update, it will set it back to INVALID_PAGE. So if we initialized it once during VM creation it wouldn't be necessary to keep setting it to INVALID_PAGE at all, except for one case ...
There is a window where the guest could do an AP_CREATE with new GPA, followed immediately by AP_DESTROY. If that AP_DESTROY got processed before the vCPU re-enters the guest, it might instead restart the vCPU (due to taking VALID_PAGE(snp_vmsa_gpa) branch in __sev_snp_update_protected_guest_state()). So for that reason it makes sense to always initialize it to INVALID_PAGE when an AP_CREATION request comes in, to essentially clear any pending updates from a previous pending AP_CREATION for the same vCPU. If we move it to the AP_DESTROY case, but hit a failure elsewhere in sev_snp_ap_creation(), then the calling vCPU will get a #GP, but the target vCPU might get set up with the VMSA for the previous pending request. That might be appropriate though, just because calling vCPU hosed itself doesn't mean a previous valid AP_CREATION request shouldn't go through for target. Hmm...
And having said all that... yes, I guess that basically boils down to INVALID_PAGE being used as an indicator of whether or not to unmap VMSA. I'll try to come up with some wording to better convey all this a bit more clearly.
Thanks,
-Mike
Quoted 17 lines
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Tom Lendacky
maintainerRe: [PATCH RFC v8 47/56] KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Michael RothQuoted 56 lines · Michael Roth
Quoted 51 lines · Alexander Graf
Quoted 36 lines · Michael Roth
From: Tom Lendacky <[email protected]>
Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI.
A new event,
KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running.For CREATE
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent is added to the vCPU and then the vCPU is kicked.For
CREATE_ON_INIT:
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU.For DESTROY:
The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent is added to vCPU and then the vCPU is kicked.The
KVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to theKVM_MP_STATE_UNINITIALIZEDstate, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set toKVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left asKVM_MP_STATE_UNINITIALIZEDto prevent it from being run.Quoted 5 lines
Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: add handling for restrictedmem]Signed-off-by Michael Roth <[email protected]>What is the intended boot sequence for SEV-SNP guests? FWIW with this interface in place, guests will typically use in-guest VMSA pages to hold secondary vcpu state. But that means we're now allocating 4kb of memory for every vcpu that we create that will be for most of the guest's lifetime superfluous.
Quoted 3 lines
Wouldn't it make more sense to have a model where we only allocate the VMSA for the boot CPU and leave secondary allocation to the guest? We already need firmware changes for SEV-SNP - may as well make this one more.
Quoted 8 lines
I don't think we'd necessarily need a firmware change. We could just free original VMSA back to the hypervisor as soon as those APs come online. The down-side to that versus deferring cleaning till guest shutdown is there is some flushing activity (see:
sev_flush_encrypted_page()) that would now likely be occuring during guest boot up where the overhead might be more noticeable. But for SNP the host likely supportsX86_FEATURE_SME_COHERENTso the overhead probably isn't that bad.
Currently, OVMF code will perform a broadcast IPI to start all the APs because it doesn't know the APIC IDs until they start for the first time. Until the APIC IDs are known, the guest BSP can't create the VMSAs.
However, a new GHCB event is in plan to retrieve the APIC IDs for the guest. Once that is in place, then you could create just a single VMSA for the BSP and then allow the guest to create the remainder (the current OVMF PoC patches to support an SVSM do this). The VMM would have to know that the hypervisor and the firmware both support that, though. That could be advertised as part of the GUID table of the firmware (in the case of OVMF) and as a capability from KVM.
Thanks,
Tom
Quoted 110 lines
Quoted 16 lines
[...]
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+ +static int sev_snp_ap_creation (struct vcpu_svm *svm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (svm->vcpu.kvm)->sev_info; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm_vcpu *target_vcpu; +struct vcpu_svm *target_svm; +unsigned int request; +unsigned int apic_id; +bool kick; +int ret; +Quoted 8 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3688
Quoted 5 lines
arch/x86/kvm/svm/sev.c +5
-0+ request =lower_32_bits (svm->vmcb->control.exit_info_1); + apic_id =upper_32_bits (svm->vmcb->control.exit_info_1); + +/* Validate the APIC ID */ + target_vcpu =kvm_get_vcpu_by_id (vcpu->kvm, apic_id);Out of curiosity: The target CPU can be my own vCPU, right?
I don't think that would be the normal behavior, but maybe with some care it's possible for a guest to do things that way. I haven't seen anything strictly prohibiting this in the relevant specs.
Quoted 54 lines
Quoted 54 lines
arch/x86/kvm/svm/sev.c +52
-0+if (!target_vcpu) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP APIC ID [%#x] from guest\n" , + apic_id); +return -EINVAL ; + } + + ret =0 ; + + target_svm =to_svm (target_vcpu); + +/* +* The target vCPU is valid, so the vCPU will be kicked unless the +* request is for CREATE_ON_INIT. For any errors at this stage, the +* kick will place the vCPU in an non-runnable state. +*/ + kick = true; + +mutex_lock (&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; + target_svm->sev_es.snp_ap_create = true; + +/* Interrupt injection mode shouldn't change for AP creation */ +if (request <SVM_VMGEXIT_AP_DESTROY ) { +u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX ]; + sev_features ^= sev->sev_features; +if (sev_features &SVM_SEV_FEAT_INT_INJ_MODES ) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP injection mode [%#lx] from guest\n" , + vcpu->arch.regs[VCPU_REGS_RAX ]); + ret = -EINVAL ; + goto out; + } + } + +switch (request) { +case SVM_VMGEXIT_AP_CREATE_ON_INIT : + kick = false; + fallthrough; +case SVM_VMGEXIT_AP_CREATE : +if (!page_address_valid (vcpu, svm->vmcb->control.exit_info_2)) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP VMSA address [%#llx] from guest\n" , + svm->vmcb->control.exit_info_2); + ret = -EINVAL ; + goto out; + } + +/* +* Malicious guest can RMPADJUST a large page into VMSA which +* will hit the SNP erratum where the CPU will incorrectly signal +* an RMP violation #PF if a hugepage collides with the RMP entry Quoted 8 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3742
Quoted 2 lines
arch/x86/kvm/svm/sev.c +2
-0+* of VMSA page, reject the AP CREATE request if VMSA address from +* guest is 2M aligned. This will break genuine current Linux kernels that just happen to allocate a guest page, no? In fact, given enough vCPUs you're almost guaranteed to hit an aligned structure somewhere. What is the guest supposed to do in that situation?
The initial SNP support for guest kernels already made use of
snp_alloc_vmsa_page()to do the appropriate workaround to avoid allocating 2MB-aligned VMSA pages.-Mike
Quoted 12 lines
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 48/56] KVM: SVM: Add SNP-specific handling for memory attribute updates
In reply to Dave HansenQuoted 15 lines · Dave Hansen
arch/x86/kvm/svm/sev.c void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) · line 4466
Quoted 11 lines · Michael Roth
arch/x86/kvm/svm/sev.c +11
-0+/* +* TODO: The RMP entry's hugepage bit is ignored for +* shared/unassigned pages. Either handle looping through each +* sub-page as part of snp_make_page_shared(), or remove the +* level argument. +*/ +if (op ==SNP_PAGE_STATE_PRIVATE && order && +IS_ALIGNED (gfn,1 << order) && (gfn + (1 << order)) <= end) { + level =order_to_level (order); + npages =1 << order; + }That's a wee bit obtuse.
Quoted 2 lines
First of all, I assume that the 'RFC' is because of these TODOs and they won't survive to the point when you ask for this to be merged.
Yes, will make sure to have all the TODOs in the tree addressed before dropping the RFC tag.
Quoted 2 lines
BTW, what keeps the
restrictedmem_get_page()offset and the gfn aligned?
I don't think anything enforces that currently, but there is a TODO in the UPM v10 patchset to enforce that:
So currently this patch relies on the following:
- the fact that the memslot alignment/sizes for a standard x86 guest's associated memory regions are 2M-aligned, so when they are bound to a restrictedmem FD they are naturally packed in at restrictedmem offsets that are also 2M-aligned. But of course we can't assume userspace will live up to this assumption and need the above TODO in KVM to enforce this when registering new memslots.
- that restrictedmem/shmem will ensure that THPs are only allocated for restrictedmem offsets that are 2M-aligned. I think this enforcement happens in shmem_alloc_hugefolio().
which both seem to hold in testing. But it's probably a good idea to add an explicit check for this, at least until KVM implements something to enforce this earlier in guest life-cycle.
Quoted 19 lines
Let's start with this:
arch/x86/kvm/svm/sev.c void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) · line 4413
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+static inline u8 order_to_level (int order) +{ +BUILD_BUG_ON (KVM_MAX_HUGEPAGE_LEVEL >PG_LEVEL_1G ); + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_1G )) +return PG_LEVEL_1G ; + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_2M )) +return PG_LEVEL_2M ; + +return PG_LEVEL_4K ; +}Right now, 'order' comes only from
restrictedmem_get_page(), which I dug out of:
Quoted 6 lines
That order is *only* filled in by THPs. That makes the
PG_LEVEL_1Gstuff here kinda silly. I guess it might be seen as thorough, but it's dead code. I'd probably just make this work on order==9 || order==0 and warn on anything else.
Ok, makes sense.
Quoted 4 lines
I'd also highly recommend some comments about how racy this all is. I guess it probably works, but it would be good to add some comments about page splits and collapsing.
Collapsing while in this code path should be ok since the 4K sub-pages will just end up getting mapped as 4K in RMP table. KVM MMU will then map them into nested page table as 4K as well and we'll get non-optimal performance, but things should otherwise work.
Splitting is a similar story: if we map as 2M in RMP table, and then afterward the page gets split, then KVM MMU during fault time would map the pages in the NPT as 4K, and when the guest attempts to access private pages of this sort they'll generate a nested page fault with PFERR_GUEST_RMP_BIT and PFERR_GUEST_SIZEM_BIT set, and the code in handle_rmp_page_fault() will issue a PSMASH instruction to split the 2M RMP entry into 512 4K RMP entries.
Will add some comments around this.
Quoted 2 lines
It's also not obvious why this only cares about private pages.
Mainly because the shared memory that actually get mapped into the guest is always shared in the RMP table. It is normal VMA memory that is not allocated by UPM/restrictedmem. We will never attempt to make them private, so there is never a need to bother with switching them back to shared.
So we only need to handle RMP updates for the UPM/restrictedmem PFNs. Obviously for shared->private conversion before mapping it into the guest, but also for private->shared conversion since we will still get RMP check failures if we try to leave the PFNs as private in the RMP table and map the above-mentioned VMA memory into the guest instead.
Will add some more comments around this.
Quoted 20 lines
Anyway, this is the exact kind of thing where I really like a well-commented helper:
bool
can_install_large_rmp_entry(gfn, order){ // small pages, blah blah if (!order) return false; // The region being updated must be aligned if (!IS_ALIGNED(gfn, 1 << order)) return false; // ... and fit if (gfn + (1 << order)) > end) return false; return true; }
Quoted 6 lines
Which gets used like this:
if (op == SNP_PAGE_STATE_PRIVATE && can_install_large_rmp_entry(gfn, order)) { level = ... }
Makes sense, will implement something along these lines.
Thanks!
-Mike
Re: [PATCH RFC v8 17/56] x86/fault: Add support to handle the RMP fault for user address
In reply to Michael RothQuoted 11 lines · Michael Roth
However...
The fact that any pages potentially triggering these #PFs are able to be mapped as 2M in the first place means that all the PFNs covered by that 2M mapping must also been allocated by via mappable/VMA memory rather than via restricted memfd where userspace mappings are not possible.
So I think we should be able to drop this patch entirely, as well as allow the use of HugeTLBFS for non-restricted memfd memory (though eventually the guest will switch all its memory to private/restricted so not gaining much there other than reducing management complexity).
This is sounding a bit voodoo-ish to me.
If this whole series is predicated on having its memory supplied via one very specific ABI with very specific behavior.
That connection and the associated contract isn't spelled out very clearly in this series. I'm sure it works on your machine and is clear to _you_ but I'm worried that nobody else is going to be able to figure out the voodoo.
Could we make sure that this stuff is made very clear in the Documentation and cover letter, please?
Re: [PATCH RFC v8 45/56] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Michael RothQuoted 158 lines · Michael Roth
From: Brijesh Singh <[email protected]>
Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the
SNP_GUEST_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 181 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +84
-6@@ -327,6 +327,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (ret) goto e_free; +mutex_init (&sev->guest_req_lock); ret =sev_snp_init (&argp->error, false); }else { ret =sev_platform_init (&argp->error);@@ -2059,23 +2060,34 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)*/ static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) { +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_addr data = {}; -void *context; +void *context, *certs_data;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kzalloc (SEV_FW_BLOB_MAX_SIZE ,GFP_KERNEL_ACCOUNT ); +if (!certs_data) +return NULL ; +/* Allocate memory for context page */ context =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT );if (!context) -return NULL ; + goto e_free; data.gctx_paddr =__psp_pa (context); rc =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_GCTX_CREATE , &data, &argp->error); -if (rc) { -snp_free_firmware_page (context); -return NULL ; - } +if (rc) + goto e_free; + + sev->snp_certs_data = certs_data;return context; + +e_free: +snp_free_firmware_page (context); +kfree (certs_data); +return NULL ; }static int snp_bind_asid (struct kvm *kvm,int *error)@@ -2693,6 +2705,8 @@ static int snp_decommission_context(struct kvm *kvm)snp_free_firmware_page (sev->snp_context); sev->snp_context =NULL ; +kfree (sev->snp_certs_data); +return 0 ; }@@ -3153,6 +3167,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)case SVM_VMGEXIT_UNSUPPORTED_EVENT :case SVM_VMGEXIT_HV_FEATURES :case SVM_VMGEXIT_PSC : +case SVM_VMGEXIT_GUEST_REQUEST : +case SVM_VMGEXIT_EXT_GUEST_REQUEST :break ;default : reason =GHCB_ERR_INVALID_EVENT ;@@ -3384,6 +3400,149 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu)return 1 ; } +static unsigned long snp_setup_guest_buf (struct vcpu_svm *svm, +struct sev_data_snp_guest_request *data, +gpa_t req_gpa,gpa_t resp_gpa) +{ +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +kvm_pfn_t req_pfn, resp_pfn; +struct kvm_sev_info *sev; + + sev = &to_kvm_svm (kvm)->sev_info; + +if (!IS_ALIGNED (req_gpa,PAGE_SIZE ) || !IS_ALIGNED (resp_gpa,PAGE_SIZE )) +return SEV_RET_INVALID_PARAM ; + + req_pfn =gfn_to_pfn (kvm,gpa_to_gfn (req_gpa)); +if (is_error_noslot_pfn (req_pfn)) +return SEV_RET_INVALID_ADDRESS ; + + resp_pfn =gfn_to_pfn (kvm,gpa_to_gfn (resp_gpa)); +if (is_error_noslot_pfn (resp_pfn)) +return SEV_RET_INVALID_ADDRESS ; + +if (rmp_make_private (resp_pfn,0 ,PG_LEVEL_4K ,0 , true)) +return SEV_RET_INVALID_ADDRESS ; + + data->gctx_paddr =__psp_pa (sev->snp_context); + data->req_paddr =__sme_set (req_pfn <<PAGE_SHIFT ); + data->res_paddr =__sme_set (resp_pfn <<PAGE_SHIFT ); + +return 0 ; +} + +static void snp_cleanup_guest_buf (struct sev_data_snp_guest_request *data,unsigned long *rc) +{ +u64 pfn =__sme_clr (data->res_paddr) >>PAGE_SHIFT ; +int ret; + + ret =snp_page_reclaim (pfn); +if (ret) + *rc =SEV_RET_INVALID_ADDRESS ; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + *rc =SEV_RET_INVALID_ADDRESS ; +} + +static void snp_handle_guest_request (struct vcpu_svm *svm,gpa_t req_gpa,gpa_t resp_gpa) +{ +struct sev_data_snp_guest_request data = {0 }; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +struct kvm_sev_info *sev; +unsigned long rc; +int err; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info; +
arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3474
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &data, req_gpa, resp_gpa); +if (rc) + goto unlock; + + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &data, &err); +if (rc) +/* use the firmware error code */ + rc = err;
There are cases where sev_issue_cmd can fail, but not set err. For example, when the file descriptor is incorrect. In that case, this code path leaks uninitialized state from the host stack (err) all the way into a guest.
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 36/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
In reply to Michael RothQuoted 23 lines · Michael Roth
From: Brijesh Singh<[email protected]>
The
KVM_SEV_SNP_LAUNCH_FINISHfinalize the cryptographic digest and stores it as the measurement of the guest at launch.While finalizing the launch flow, it also issues the
LAUNCH_UPDATEcommand to encrypt the VMSA pages.If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.
Signed-off-by Brijesh Singh<[email protected]>Signed-off-by Harald Hoyer<[email protected]>Signed-off-by Ashish Kalra<[email protected]>Signed-off-by Michael Roth<[email protected]>--- .../virt/kvm/x86/amd-memory-encryption.rst | 23 ++++ arch/x86/kvm/svm/sev.c | 122 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 ++ 3 files changed, 159 insertions(+)
[...]
include/uapi/linux/kvm.h struct kvm_sev_snp_launch_update { · line 2069
Quoted 12 lines
include/uapi/linux/kvm.h +11
-0+#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { +__u64 id_block_uaddr; +__u64 id_auth_uaddr; +__u8 id_block_en; +__u8 auth_key_en; +__u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE ]; +__u8 pad[6 ];
The LAUNCH_FINISH command received a new argument to disable VCEK (VCEK_DIS). Shouldn't we add that in this patch already?
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 20/56] crypto:ccp: Define the SEV-SNP commands
In reply to Michael RothQuoted 41 lines · Michael Roth
From: Brijesh Singh <[email protected]>
AMD introduced the next generation of SEV called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware security protection.
Define the commands and structures used to communicate with the AMD-SP when creating and managing the SEV-SNP guests. The SEV-SNP firmware spec is available at developer.amd.com/sev.
Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 16 +++ include/linux/psp-sev.h | 247 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 44 +++++++ 3 files changed, 307 insertions(+)drivers/crypto/ccp/sev-dev.c +6
-0@@ -126,6 +126,8 @@ static int sev_cmd_buffer_len(int cmd)switch (cmd) {case SEV_CMD_INIT :return sizeof (struct sev_data_init );case SEV_CMD_INIT_EX :return sizeof (struct sev_data_init_ex ); +case SEV_CMD_SNP_SHUTDOWN_EX :return sizeof (struct sev_data_snp_shutdown_ex ); +case SEV_CMD_SNP_INIT_EX :return sizeof (struct sev_data_snp_init_ex );case SEV_CMD_PLATFORM_STATUS :return sizeof (struct sev_user_data_status );case SEV_CMD_PEK_CSR :return sizeof (struct sev_data_pek_csr );case SEV_CMD_PEK_CERT_IMPORT :return sizeof (struct sev_data_pek_cert_import );@@ -154,6 +156,20 @@ static int sev_cmd_buffer_len(int cmd)case SEV_CMD_GET_ID :return sizeof (struct sev_data_get_id );case SEV_CMD_ATTESTATION_REPORT :return sizeof (struct sev_data_attestation_report );case SEV_CMD_SEND_CANCEL :return sizeof (struct sev_data_send_cancel ); +case SEV_CMD_SNP_GCTX_CREATE :return sizeof (struct sev_data_snp_addr ); +case SEV_CMD_SNP_LAUNCH_START :return sizeof (struct sev_data_snp_launch_start ); +case SEV_CMD_SNP_LAUNCH_UPDATE :return sizeof (struct sev_data_snp_launch_update ); +case SEV_CMD_SNP_ACTIVATE :return sizeof (struct sev_data_snp_activate );
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 172
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+case SEV_CMD_SNP_DECOMMISSION :return sizeof (struct sev_data_snp_addr ); +case SEV_CMD_SNP_PAGE_RECLAIM :return sizeof (struct sev_data_snp_page_reclaim ); +case SEV_CMD_SNP_GUEST_STATUS :return sizeof (struct sev_data_snp_guest_status ); +case SEV_CMD_SNP_LAUNCH_FINISH :return sizeof (struct sev_data_snp_launch_finish ); +case SEV_CMD_SNP_DBG_DECRYPT :return sizeof (struct sev_data_snp_dbg ); +case SEV_CMD_SNP_DBG_ENCRYPT :return sizeof (struct sev_data_snp_dbg ); +case SEV_CMD_SNP_PAGE_UNSMASH :return sizeof (struct sev_data_snp_page_unsmash ); +case SEV_CMD_SNP_PLATFORM_STATUS :return sizeof (struct sev_data_snp_addr ); +case SEV_CMD_SNP_GUEST_REQUEST :return sizeof (struct sev_data_snp_guest_request ); +case SEV_CMD_SNP_CONFIG :return sizeof (struct sev_user_data_snp_config );
This needs SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX, SEV_CMD_SNP_COMMIT and SEV_CMD_SNP_VLEK_LOAD from 1.54 ABI release.
Quoted 14 lines
drivers/crypto/ccp/sev-dev.c +11
-0default :return 0 ; } +/** +* struct sev_user_data_snp_status - SNP status +* +* @major: API major version +* @minor: API minor version +* @state: current platform state +* @build: firmware build id for the API version +* @guest_count: the number of guest currently managed by the firmware +* @tcb_version: current TCB version +*/ +struct sev_user_data_snp_status {
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd)
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+__u8 api_major;/* Out */ +__u8 api_minor;/* Out */ +__u8 state;/* Out */ +__u8 rsvd; +__u32 build_id;/* Out */ +__u32 rsvd1; +__u32 guest_count;/* Out */ +__u64 tcb_version;/* Out */ +__u64 rsvd2; +} __packed;
Could you please update this to 1.54 ABI version?
Should include something along these lines:
include/uapi/linux/psp-sev.h +19 -10
@@ -163,20 +163,29 @@ struct sev_user_data_get_id2 {* @major: API major version * @minor: API minor version * @state: current platform state +* @is_rmp_initialized: whether RMP is initialized or not * @build: firmware build id for the API version +* @mask_chip_id: whether chip id is present in attestation reports or not +* @mask_chip_key: whether attestation reports are signed or not +* @vlek_en: VLEK hashstick is loaded * @guest_count: the number of guest currently managed by the firmware -* @tcb_version: current TCB version +* @current_tcb_version: current TCB version +* @reported_tcb_version: reported TCB version */ struct sev_user_data_snp_status { -__u8 api_major;/* Out */ -__u8 api_minor;/* Out */ -__u8 state;/* Out */ -__u8 rsvd; -__u32 build_id;/* Out */ -__u32 rsvd1; -__u32 guest_count;/* Out */ -__u64 tcb_version;/* Out */ -__u64 rsvd2; +__u8 api_major;/* Out */ +__u8 api_minor;/* Out */ +__u8 state;/* Out */ +__u8 is_rmp_initialized:1 ;/* Out */ +__u8 rsvd:7 ; +__u32 build_id;/* Out */ +__u32 mask_chip_id:1 ;/* Out */ +__u32 mask_chip_key:1 ;/* Out */ +__u32 vlek_en:1 ;/* Out */ +__u32 rsvd1:29 ; +__u32 guest_count;/* Out */ +__u64 current_tcb_version;/* Out */ +__u64 reported_tcb_version;/* Out */ } __packed;/*
Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.
Re: [PATCH RFC v8 12/56] x86/sev: Add RMP entry lookup helpers
In reply to Michael RothQuoted 43 lines · Michael Roth
Quoted 13 lines · Vlastimil Babka
Quoted 11 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The
snp_lookup_page_in_rmptable()can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see
https://bugzilla.kernel.org/attachment.cgi?id=296015.Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Michael Roth <[email protected]>---
Quoted 8 lines
Quoted 4 lines
+/*
+ * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned,
+ * and -errno if there is no corresponding RMP entry.
+ */Hmm IMHO the kernel's idiomatic way is to return 0 on "success" and I'd assume the more intuitive expectation of success here if the entry is assigned?
In general I'd agree. Here's it's a little awkward though.
snp_lookup_rmpentry()sort of wants to be a bool, where true indicates an assigned entry was found, false indicates no assigned entry.But it also has to deal with error values, so the most direct way to encapsulate that is true == 1, false == 0, and < 0 for errors.
Inverting it to align more with kernel expections of 0 == success/true gets awkward too, because stuff like:
if (
snp_lookup_rmpentry(...))//errorstill doesn't work the way most other functions written in this way would since it could still be "successful" if we were expecting PFN to be in shared state. So the return value needs special handling there too.
Would it make sense to define it something like this?:
Quoted 8 lines
/*
* Query information about the RMP entry corresponding to the given
* PFN.
*
* Returns 0 on success, and -errno if there was a problem accessing
* the RMP entry.
*/
intsnp_lookup_rmpentry(u64 pfn, int *level, bool *assigned)
Yeah that looks fine to me. Hope you find out it makes it easier to work with in the callers too.
Quoted 8 lines
Quoted 4 lines
The various callers seem to differ though so I guess it depends on context. Some however don't distinguish their "failure" from an ERR and maybe they should, at least for the purposes of the various printks?
Yes, regardless of what we decide above, the call-sites should properly distinguish between failure/assigned/not-assigned and report the information accordingly. I'll get those fixed up where needed.
Great, thanks!
Quoted 17 lines
Thanks,
-Mike
Quoted 13 lines
Quoted 12 lines
arch/x86/kernel/sev.c +11
-0+int snp_lookup_rmpentry (u64 pfn,int *level) +{ +struct rmpentry *e; + + e =__snp_lookup_rmpentry (pfn, level); +if (IS_ERR (e)) +return PTR_ERR (e); + +return !!rmpentry_assigned (e); +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);
Re: [PATCH RFC v8 34/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
In reply to Michael RothQuoted 15 lines · Michael Roth
From: Brijesh Singh <[email protected]>
arch/x86/kvm/svm/sev.c +12
-0+static int snp_decommission_context (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_addr data = {}; +int ret; + +/* If context is not created then do nothing */ +if (!sev->snp_context) +return 0 ; + + data.gctx_paddr =__sme_pa (sev->snp_context); + ret =sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , &data,NULL );
arch/x86/kvm/svm/sev.c int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) · line 2401
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+if (WARN_ONCE (ret,"failed to release guest context" )) +return ret; + +/* free the context page now */ +snp_free_firmware_page (sev->snp_context); + sev->snp_context =NULL ; + +return 0 ; +} +
Even though it's not documented, SNP_DECOMMISSION seems to clear the WBINVD indicator just like DEACTIVATE does for SEV.
Won't ASID recycling race with SNP_DECOMMISSION if the latter isn't guarded with sev_deactivate_lock?
Quoted 10 lines
void
sev_vm_destroy(struct kvm *kvm)
{arch/x86/kvm/svm/sev.c +2
-1struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;@@ -2333,7 +2440,15 @@ void sev_vm_destroy(struct kvm *kvm) } } -sev_unbind_asid (kvm, sev->handle); +if (sev_snp_guest (kvm)) { +if (snp_decommission_context (kvm)) {
arch/x86/kvm/svm/sev.c void sev_vm_destroy(struct kvm *kvm) · line 2454
Quoted 10 lines
arch/x86/kvm/svm/sev.c +7
-0+WARN_ONCE (1 ,"Failed to free SNP guest context, leaking asid!\n" ); +return ; + } + }else { +sev_unbind_asid (kvm, sev->handle); + } +sev_asid_free (sev); }
Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.
Tom Lendacky
maintainerRe: [PATCH RFC v8 34/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
Quoted 23 lines · Sabin Rapan
Quoted 22 lines · Michael Roth
From: Brijesh Singh <[email protected]>
arch/x86/kvm/svm/sev.c +19
-0+static int snp_decommission_context (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_addr data = {}; +int ret; + +/* If context is not created then do nothing */ +if (!sev->snp_context) +return 0 ; + + data.gctx_paddr =__sme_pa (sev->snp_context); + ret =sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , &data,NULL ); +if (WARN_ONCE (ret,"failed to release guest context" )) +return ret; + +/* free the context page now */ +snp_free_firmware_page (sev->snp_context); + sev->snp_context =NULL ; +
Quoted 8 lines
arch/x86/kvm/svm/sev.c int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) · line 2095
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+return 0 ; +} +Even though it's not documented,
SNP_DECOMMISSIONseems to clear the WBINVD indicator just like DEACTIVATE does for SEV.
Won't ASID recycling race withSNP_DECOMMISSIONif the latter isn't guarded withsev_deactivate_lock?
Good catch, yes, this needs to use the sev_deactivate_lock around the DECOMMISSION command.
Thanks,
Tom
Quoted 26 lines
Quoted 12 lines
void sev_vm_destroy(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;arch/x86/kvm/svm/sev.c +2
-1@@ -2333,7 +2440,15 @@ void sev_vm_destroy(struct kvm *kvm) } } -sev_unbind_asid (kvm, sev->handle); +if (sev_snp_guest (kvm)) { +if (snp_decommission_context (kvm)) {arch/x86/kvm/svm/sev.c void sev_vm_destroy(struct kvm *kvm) · line 2454
Quoted 10 lines
arch/x86/kvm/svm/sev.c +7
-0+WARN_ONCE (1 ,"Failed to free SNP guest context, leaking asid!\n" ); +return ; + } + }else { +sev_unbind_asid (kvm, sev->handle); + } +sev_asid_free (sev); }Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.
We discovered that the kdump crashkernel would not work with our SEV- SNP configuration.
After reading the device table from the previour kernel we would see a lot of
AMD-Vi: Completion-Wait loop timed out
errors and finally crash:
Kernel panic - not syncing: timer doesn't work through Interrupt-
remapped IO-APIC
We found that disabeling SNP in the outgoing (crashing) kernel would enable the crashkernel to take over the iommu config and boot from there.
We opened a PR over on github against the rfc-v9 branch to discuss the issue:
https://github.com/AMDESE/linux/pull/5
Cheers Johanna
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Kalra, Ashish
maintainerPlease look at the response on github and followup discussion.
Thanks,
Ashish
Quoted 35 lines · Johanna 'Mimoja' Amelie
We discovered that the kdump crashkernel would not work with our SEV- SNP configuration.
After reading the device table from the previour kernel we would see a lot of
AMD-Vi: Completion-Wait loop timed out
errors and finally crash:Kernel panic - not syncing: timer doesn't work through Interrupt-remapped IO-APIC
We found that disabeling SNP in the outgoing (crashing) kernel would enable the crashkernel to take over the iommu config and boot from there.
We opened a PR over on github against the rfc-v9 branch to discuss the issue:
https://github.com/AMDESE/linux/pull/5Cheers Johanna
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879