* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit [not found] <Ygz88uacbwuTTNat@zn.tnic.mbx> @ 2022-02-16 16:04 ` Brijesh Singh 2022-02-21 17:41 ` Kirill A. Shutemov 0 siblings, 1 reply; 16+ messages in thread From: Brijesh Singh @ 2022-02-16 16:04 UTC (permalink / raw) To: Borislav Petkov, Kirill A . Shutemov Cc: Brijesh Singh, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Fri, Feb 11, 2022 at 03:55:23PM +0100, Borislav Petkov wrote: >> Also, I think adding required functions to x86_platform.guest. is a very >> nice way to solve the ugly if (guest_type) querying all over the place. > So I guess something like below. It builds here... I made a small change to use prepare() and finish(). The idea is that prepare() will be called before the encryption mask is changed in the page table, and finish() will be called after the change. I have not tried integrating the SNP PSC yet, but want to check if approach will work for Krill. --- arch/x86/include/asm/set_memory.h | 1 - arch/x86/include/asm/sev.h | 3 +++ arch/x86/include/asm/x86_init.h | 15 +++++++++++++++ arch/x86/kernel/sev.c | 3 +++ arch/x86/mm/mem_encrypt_amd.c | 14 ++++++++++---- arch/x86/mm/pat/set_memory.c | 13 +++++++------ 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index ff0f2d90338a..ce8dd215f5b3 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -84,7 +84,6 @@ int set_pages_rw(struct page *page, int numpages); int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); bool kernel_page_present(struct page *page); -void notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc); extern int kernel_set_to_readonly; diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ec060c433589..2ebd8c225257 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -87,6 +87,9 @@ extern enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, struct es_em_ctxt *ctxt, u64 exit_code, u64 exit_info_1, u64 exit_info_2); +void amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc); +void amd_enc_status_change_finish(unsigned long vaddr, int npages, bool enc); + #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 22b7412c08f6..da7fc1c0b917 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -141,6 +141,20 @@ struct x86_init_acpi { void (*reduced_hw_early_init)(void); }; +/** + * struct x86_guest - Functions used by misc guest incarnations like SEV, TDX, etc. + * + * @enc_status_change_prepare Notify HV before the encryption status of a range + * is changed. + * + * @enc_status_change_finish Notify HV after the encryption status of a range + * is changed. + */ +struct x86_guest { + void (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc); + void (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc); +}; + /** * struct x86_init_ops - functions for platform specific setup * @@ -287,6 +301,7 @@ struct x86_platform_ops { struct x86_legacy_features legacy; void (*set_legacy_features)(void); struct x86_hyper_runtime hyper; + struct x86_guest guest; }; struct x86_apic_ops { diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index e6d316a01fdd..3b2133fd6682 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -760,6 +760,9 @@ void __init sev_es_init_vc_handling(void) BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE); + x86_platform.guest.enc_status_change_prepare = amd_enc_status_change_prepare; + x86_platform.guest.enc_status_change_finish = amd_enc_status_change_finish; + if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) return; diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index 2b2d018ea345..c93b6c2fc6a3 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -256,7 +256,11 @@ static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot) return pfn; } -void notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc) +void amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc) +{ +} + +void amd_enc_status_change_finish(unsigned long vaddr, int npages, bool enc) { #ifdef CONFIG_PARAVIRT unsigned long sz = npages << PAGE_SHIFT; @@ -280,7 +284,7 @@ void notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc) psize = page_level_size(level); pmask = page_level_mask(level); - notify_page_enc_status_changed(pfn, psize >> PAGE_SHIFT, enc); + amd_enc_status_change_finish(pfn, psize >> PAGE_SHIFT, enc); vaddr = (vaddr & pmask) + psize; } @@ -341,6 +345,8 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr, vaddr_next = vaddr; vaddr_end = vaddr + size; + amd_enc_status_change_prepare(start, PAGE_ALIGN(size) >> PAGE_SHIFT, enc); + for (; vaddr < vaddr_end; vaddr = vaddr_next) { kpte = lookup_address(vaddr, &level); if (!kpte || pte_none(*kpte)) { @@ -392,7 +398,7 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr, ret = 0; - notify_range_enc_status_changed(start, PAGE_ALIGN(size) >> PAGE_SHIFT, enc); + amd_enc_status_change_finish(start, PAGE_ALIGN(size) >> PAGE_SHIFT, enc); out: __flush_tlb_all(); return ret; @@ -410,7 +416,7 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size) void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages, bool enc) { - notify_range_enc_status_changed(vaddr, npages, enc); + amd_enc_status_change_finish(vaddr, npages, enc); } void __init mem_encrypt_free_decrypted_mem(void) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index b4072115c8ef..a55477a6e578 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2012,8 +2012,15 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) */ cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT)); + /* Notify HV that we are about to set/clr encryption attribute. */ + x86_platform.guest.enc_status_change_prepare(addr, numpages, enc); + ret = __change_page_attr_set_clr(&cpa, 1); + /* Notify HV that we have succesfully set/clr encryption attribute. */ + if (!ret) + x86_platform.guest.enc_status_change_finish(addr, numpages, enc); + /* * After changing the encryption attribute, we need to flush TLBs again * in case any speculative TLB caching occurred (but no need to flush @@ -2023,12 +2030,6 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) */ cpa_flush(&cpa, 0); - /* - * Notify hypervisor that a given memory range is mapped encrypted - * or decrypted. - */ - notify_range_enc_status_changed(addr, numpages, enc); - return ret; } -- 2.25.1 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-16 16:04 ` [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit Brijesh Singh @ 2022-02-21 17:41 ` Kirill A. Shutemov 2022-02-21 18:06 ` Borislav Petkov 2022-02-21 19:54 ` Brijesh Singh 0 siblings, 2 replies; 16+ messages in thread From: Kirill A. Shutemov @ 2022-02-21 17:41 UTC (permalink / raw) To: Brijesh Singh Cc: Borislav Petkov, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Wed, Feb 16, 2022 at 10:04:57AM -0600, Brijesh Singh wrote: > @@ -287,6 +301,7 @@ struct x86_platform_ops { > struct x86_legacy_features legacy; > void (*set_legacy_features)(void); > struct x86_hyper_runtime hyper; > + struct x86_guest guest; > }; I used 'cc' instead of 'guest'. 'guest' looks too generic. Also, I'm not sure why not to use pointer to ops struct instead of stroing them directly in x86_platform. Yes, it is consistent with 'hyper', but I don't see it as a strong argument. > > index b4072115c8ef..a55477a6e578 100644 > --- a/arch/x86/mm/pat/set_memory.c > +++ b/arch/x86/mm/pat/set_memory.c > @@ -2012,8 +2012,15 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) > */ > cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT)); > > + /* Notify HV that we are about to set/clr encryption attribute. */ > + x86_platform.guest.enc_status_change_prepare(addr, numpages, enc); > + > ret = __change_page_attr_set_clr(&cpa, 1); This doesn't cover difference in flushing requirements. Can we get it too? > > + /* Notify HV that we have succesfully set/clr encryption attribute. */ > + if (!ret) > + x86_platform.guest.enc_status_change_finish(addr, numpages, enc); > + Any particular reason you moved it above cpa_flush()? I don't think it makes a difference for TDX, but still. > /* > * After changing the encryption attribute, we need to flush TLBs again > * in case any speculative TLB caching occurred (but no need to flush > @@ -2023,12 +2030,6 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) > */ > cpa_flush(&cpa, 0); > > - /* > - * Notify hypervisor that a given memory range is mapped encrypted > - * or decrypted. > - */ > - notify_range_enc_status_changed(addr, numpages, enc); > - > return ret; > } > > -- > 2.25.1 > -- Kirill A. Shutemov ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-21 17:41 ` Kirill A. Shutemov @ 2022-02-21 18:06 ` Borislav Petkov 2022-02-21 19:54 ` Brijesh Singh 1 sibling, 0 replies; 16+ messages in thread From: Borislav Petkov @ 2022-02-21 18:06 UTC (permalink / raw) To: Kirill A. Shutemov Cc: Brijesh Singh, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Mon, Feb 21, 2022 at 08:41:21PM +0300, Kirill A. Shutemov wrote: > On Wed, Feb 16, 2022 at 10:04:57AM -0600, Brijesh Singh wrote: > > @@ -287,6 +301,7 @@ struct x86_platform_ops { > > struct x86_legacy_features legacy; > > void (*set_legacy_features)(void); > > struct x86_hyper_runtime hyper; > > + struct x86_guest guest; > > }; > > I used 'cc' instead of 'guest'. 'guest' looks too generic. But guest is what is needed there. Not all cases where the kernel runs as a guest are confidential ones. Later, that hyperv thing should be merged into the guest one too as the hyperv should be a guest too. AFAICT. > Also, I'm not sure why not to use pointer to ops struct instead of stroing > them directly in x86_platform. Yes, it is consistent with 'hyper', but I > don't see it as a strong argument. There should be no big difference but we're doing it with direct struct member assignment so far so we should keep doing the same and not start doing pointers now, all of a sudden. > This doesn't cover difference in flushing requirements. Can we get it too? What are the requirements you have for TDX on this path? This is the main reason why I'm asking you to review this - I'd like to have one version which works for both and then I'll queue it on a common branch. This is also why I'd like for you and SEV folks to agree on all the common code so that I can apply it and you can both base your patchsets ontop. Thx. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-21 17:41 ` Kirill A. Shutemov 2022-02-21 18:06 ` Borislav Petkov @ 2022-02-21 19:54 ` Brijesh Singh 1 sibling, 0 replies; 16+ messages in thread From: Brijesh Singh @ 2022-02-21 19:54 UTC (permalink / raw) To: Kirill A. Shutemov Cc: brijesh.singh, Borislav Petkov, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On 2/21/22 11:41, Kirill A. Shutemov wrote: > On Wed, Feb 16, 2022 at 10:04:57AM -0600, Brijesh Singh wrote: >> @@ -287,6 +301,7 @@ struct x86_platform_ops { >> struct x86_legacy_features legacy; >> void (*set_legacy_features)(void); >> struct x86_hyper_runtime hyper; >> + struct x86_guest guest; >> }; > > I used 'cc' instead of 'guest'. 'guest' looks too generic. I am fine with either of them. > > Also, I'm not sure why not to use pointer to ops struct instead of stroing > them directly in x86_platform. Yes, it is consistent with 'hyper', but I > don't see it as a strong argument. > >> >> index b4072115c8ef..a55477a6e578 100644 >> --- a/arch/x86/mm/pat/set_memory.c >> +++ b/arch/x86/mm/pat/set_memory.c >> @@ -2012,8 +2012,15 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) >> */ >> cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT)); >> >> + /* Notify HV that we are about to set/clr encryption attribute. */ >> + x86_platform.guest.enc_status_change_prepare(addr, numpages, enc); >> + >> ret = __change_page_attr_set_clr(&cpa, 1); > > This doesn't cover difference in flushing requirements. Can we get it too? > Yes, we can work to include that too. >> >> + /* Notify HV that we have succesfully set/clr encryption attribute. */ >> + if (!ret) >> + x86_platform.guest.enc_status_change_finish(addr, numpages, enc); >> + > > Any particular reason you moved it above cpa_flush()? I don't think it > makes a difference for TDX, but still. > It does not make any difference for the SNP as well. We can keep it where it was. >> /* >> * After changing the encryption attribute, we need to flush TLBs again >> * in case any speculative TLB caching occurred (but no need to flush >> @@ -2023,12 +2030,6 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) >> */ >> cpa_flush(&cpa, 0); >> >> - /* >> - * Notify hypervisor that a given memory range is mapped encrypted >> - * or decrypted. >> - */ >> - notify_range_enc_status_changed(addr, numpages, enc); >> - >> return ret; >> } >> >> -- >> 2.25.1 >> > ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v10 00/45] Add AMD Secure Nested Paging (SEV-SNP) Guest Support @ 2022-02-09 18:09 Brijesh Singh 2022-02-09 18:10 ` [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit Brijesh Singh 0 siblings, 1 reply; 16+ messages in thread From: Brijesh Singh @ 2022-02-09 18:09 UTC (permalink / raw) To: x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm Cc: Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Borislav Petkov, Michael Roth, Vlastimil Babka, Kirill A . Shutemov, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy, Brijesh Singh This part of Secure Encrypted Paging (SEV-SNP) series focuses on the changes required in a guest OS for SEV-SNP support. SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware-based memory protections. SEV-SNP adds strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping and more in order to create an isolated memory encryption environment. This series provides the basic building blocks to support booting the SEV-SNP VMs, it does not cover all the security enhancement introduced by the SEV-SNP such as interrupt protection. Many of the integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). Adding a new page to SEV-SNP VM requires a 2-step process. First, the hypervisor assigns a page to the guest using the new RMPUPDATE instruction. This transitions the page to guest-invalid. Second, the guest validates the page using the new PVALIDATE instruction. The SEV-SNP VMs can use the new "Page State Change Request NAE" defined in the GHCB specification to ask hypervisor to add or remove page from the RMP table. Each page assigned to the SEV-SNP VM can either be validated or unvalidated, as indicated by the Validated flag in the page's RMP entry. There are two approaches that can be taken for the page validation: Pre-validation and Lazy Validation. Under pre-validation, the pages are validated prior to first use. And under lazy validation, pages are validated when first accessed. An access to a unvalidated page results in a #VC exception, at which time the exception handler may validate the page. Lazy validation requires careful tracking of the validated pages to avoid validating the same GPA more than once. The recently introduced "Unaccepted" memory type can be used to communicate the unvalidated memory ranges to the Guest OS. At this time we only support the pre-validation, the OVMF guest BIOS validates the entire RAM before the control is handed over to the guest kernel. The early_set_memory_{encrypted,decrypted} and set_memory_{encrypted,decrypted} are enlightened to perform the page validation or invalidation while setting or clearing the encryption attribute from the page table. This series does not provide support for the Interrupt security yet which will be added after the base support. The series is based on tip/master 63812a9c80a Merge x86/cpu into tip/master The complete branch is at https://github.com/AMDESE/linux/tree/sev-snp-v10 Patch 1-4 defines multiple VMSA save area to support SEV,SEV-ES and SEV-SNP guests. It is a pre-requisite for the SEV-SNP guest support, and included in the series for the completeness. These patch apply cleanly to kvm/next. It is also posted on KVM mailing list: https://lore.kernel.org/lkml/20211213173356.138726-3-brijesh.singh@amd.com/T/#m7d6868f3e81624323ea933d3a63a68949b286103 Additional resources --------------------- SEV-SNP whitepaper https://www.amd.com/system/files/TechDocs/SEV-SNP-strengthening-vm-isolation-with-integrity-protection-and-more.pdf APM 2: https://www.amd.com/system/files/TechDocs/24593.pdf (section 15.36) GHCB spec: https://developer.amd.com/wp-content/resources/56421.pdf SEV-SNP firmware specification: https://developer.amd.com/sev/ v9: https://lore.kernel.org/linux-mm/20220208052542.3g6nskck7uhjnfji@amd.com v8: https://lore.kernel.org/lkml/20211210154332.11526-1-brijesh.singh@amd.com/ v7: https://lore.kernel.org/linux-mm/20211110220731.2396491-40-brijesh.singh@amd.com/ v6: https://lore.kernel.org/linux-mm/20211008180453.462291-1-brijesh.singh@amd.com/ v5: https://lore.kernel.org/lkml/20210820151933.22401-1-brijesh.singh@amd.com/ Changes since v9: * Removed unecessary checks on CPUID table contents, added kernel param to dump CPUID table during boot * Added boot_{rd,wr}msr() helpers * Renamed/refactored SNP CPUID code/definitions for clarity/consistency * Re-worked comments for clarity and avoid redundancies * Moved SNP CPUID table documentation to Documentation/virt/coco/sevguest.rst * Documented cc_blob_address/acpi_rsdp_addr in zero-page.rst Changes since v8: * Setup the GHCB before taking the first #VC. * Make the CC blob structure size invariant. * Define the AP INIT macro and update the AP creation to use those macro instead of the hardcoded values. * Expand the comments to cover some of previous feedbacks. * Fix the commit messages based on the feedbacks. * Multiple fixes/cleanup on cpuid patches (based on Boris and Dave feedback) * drop is_efi64 return arguments in favor of a separate efi_get_type() helper. * drop is_efi64 input arguments in favor of calling efi_get_type() as-needed. * move acpi.c's kexec-specific handling into library code. * fix stack protection for 32/64-bit builds. * Export add_identity_map() to avoid SEV-specific code in ident_map_64.c. * use snp_abort() when terminating via initial ccblob scan. * fix the copyright header after the code refactor. * remove code duplication whereever possible. Changes since v7: * sevguest: extend the get report structure to accept the vmpl from userspace. * In the compressed path, move the GHCB protocol negotiation from VC handler to sev_enable(). * sev_enable(): don't expect SEV bit in status MSR when cpuid bit is present, update comments. * sme_enable(): call directly from head_64.S rather than as part of startup_64_setup_env, add comments * snp_find_cc_blob(), sev_prep_identity_maps(): add missing 'static' keywords to function prototypes Changes since v6: * Add rmpadjust() helper to be used by AP creation and vmpl0 detect function. * Clear the VM communication key if guest detects that hypervisor is modifying the SNP_GUEST_REQ response header. * Move the per-cpu GHCB registration from first #VC to idt setup. * Consolidate initial SEV/SME setup into a common entry point that gets called early enough to also be used for SEV-SNP CPUID table setup. * SNP CPUID: separate initial SEV-SNP feature detection out into standalone snp_init() routines, then add CPUID table setup to it as a separate patch. * SNP CPUID: fix boot issue with Seabios due to ACPI relying on certain EFI config table lookup failures as fallthrough cases rather than error cases. * SNP CPUID: drop the use of a separate init routines to handle pointer fixups after switching to kernel virtual addresses, instead use a helper that uses RIP-relative addressing to access CPUID table when either on identity mapping or kernel virtual addresses. Changes since v5: * move the seqno allocation in the sevguest driver. * extend snp_issue_guest_request() to accept the exit_info to simplify the logic. * use smaller structure names based on feedback. * explicitly clear the memory after the SNP guest request is completed. * cpuid validation: use a local copy of cpuid table instead of keeping firmware table mapped throughout boot. * cpuid validation: coding style fix-ups and refactor cpuid-related helpers as suggested. * cpuid validation: drop a number of BOOT_COMPRESSED-guarded defs/declarations by moving things like snp_cpuid_init*() out of sev-shared.c and keeping only the common bits there. * Break up EFI config table helpers and related acpi.c changes into separate patches. * re-enable stack protection for 32-bit kernels as well, not just 64-bit Changes since v4: * Address the cpuid specific review comment * Simplified the macro based on the review feedback * Move macro definition to the patch that needs it * Fix the issues reported by the checkpath * Address the AP creation specific review comment Changes since v3: * Add support to use the PSP filtered CPUID. * Add support for the extended guest request. * Move sevguest driver in driver/virt/coco. * Add documentation for sevguest ioctl. * Add support to check the vmpl0. * Pass the VM encryption key and id to be used for encrypting guest messages through the platform drv data. * Multiple cleanup and fixes to address the review feedbacks. Changes since v2: * Add support for AP startup using SNP specific vmgexit. * Add snp_prep_memory() helper. * Drop sev_snp_active() helper. * Add sev_feature_enabled() helper to check which SEV feature is active. * Sync the SNP guest message request header with latest SNP FW spec. * Multiple cleanup and fixes to address the review feedbacks. Changes since v1: * Integerate the SNP support in sev.{ch}. * Add support to query the hypervisor feature and detect whether SNP is supported. * Define Linux specific reason code for the SNP guest termination. * Extend the setup_header provide a way for hypervisor to pass secret and cpuid page. * Add support to create a platform device and driver to query the attestation report and the derive a key. * Multiple cleanup and fixes to address Boris's review fedback. Brijesh Singh (20): KVM: SVM: Define sev_features and vmpl field in the VMSA x86/mm: Extend cc_attr to include AMD SEV-SNP x86/sev: Define the Linux specific guest termination reasons x86/sev: Save the negotiated GHCB version x86/sev: Check SEV-SNP features support x86/sev: Add a helper for the PVALIDATE instruction x86/sev: Check the vmpl level x86/compressed: Add helper for validating pages in the decompression stage x86/compressed: Register GHCB memory when SEV-SNP is active x86/sev: Register GHCB memory when SEV-SNP is active x86/sev: Add helper for validating pages in early enc attribute changes x86/kernel: Make the .bss..decrypted section shared in RMP table x86/kernel: Validate ROM memory before accessing when SEV-SNP is active x86/mm: Add support to validate memory when changing C-bit x86/boot: Add Confidential Computing type to setup_data x86/sev: Provide support for SNP guest request NAEs x86/sev: Register SEV-SNP guest request platform device virt: Add SEV-SNP guest driver virt: sevguest: Add support to derive key virt: sevguest: Add support to get extended report Michael Roth (21): x86/boot: Introduce helpers for MSR reads/writes x86/boot: Use MSR read/write helpers instead of inline assembly x86/compressed/64: Detect/setup SEV/SME features earlier in boot x86/sev: Detect/setup SEV/SME features earlier in boot x86/head/64: Re-enable stack protection x86/compressed/acpi: Move EFI detection to helper x86/compressed/acpi: Move EFI system table lookup to helper x86/compressed/acpi: Move EFI config table lookup to helper x86/compressed/acpi: Move EFI vendor table lookup to helper x86/compressed/acpi: Move EFI kexec handling into common code KVM: x86: Move lookup of indexed CPUID leafs to helper x86/sev: Move MSR-based VMGEXITs for CPUID to helper x86/compressed/64: Add support for SEV-SNP CPUID table in #VC handlers x86/boot: Add a pointer to Confidential Computing blob in bootparams x86/compressed: Add SEV-SNP feature detection/setup x86/compressed: Use firmware-validated CPUID leaves for SEV-SNP guests x86/compressed: Export and rename add_identity_map() x86/compressed/64: Add identity mapping for Confidential Computing blob x86/sev: Add SEV-SNP feature detection/setup x86/sev: Use firmware-validated CPUID for SEV-SNP guests virt: sevguest: Add documentation for SEV-SNP CPUID Enforcement Tom Lendacky (4): KVM: SVM: Create a separate mapping for the SEV-ES save area KVM: SVM: Create a separate mapping for the GHCB save area KVM: SVM: Update the SEV-ES save area mapping x86/sev: Use SEV-SNP AP creation to start secondary CPUs .../admin-guide/kernel-parameters.txt | 4 + Documentation/virt/coco/sevguest.rst | 155 ++++ Documentation/virt/index.rst | 1 + Documentation/x86/zero-page.rst | 2 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/acpi.c | 173 +--- arch/x86/boot/compressed/efi.c | 238 ++++++ arch/x86/boot/compressed/head_64.S | 37 +- arch/x86/boot/compressed/ident_map_64.c | 39 +- arch/x86/boot/compressed/idt_64.c | 18 +- arch/x86/boot/compressed/mem_encrypt.S | 36 - arch/x86/boot/compressed/misc.h | 55 +- arch/x86/boot/compressed/sev.c | 263 +++++- arch/x86/boot/cpucheck.c | 30 +- arch/x86/boot/msr.h | 28 + arch/x86/include/asm/bootparam_utils.h | 1 + arch/x86/include/asm/cpuid.h | 34 + arch/x86/include/asm/msr-index.h | 2 + arch/x86/include/asm/msr.h | 11 +- arch/x86/include/asm/setup.h | 1 - arch/x86/include/asm/sev-common.h | 82 ++ arch/x86/include/asm/sev.h | 102 ++- arch/x86/include/asm/shared/msr.h | 15 + arch/x86/include/asm/svm.h | 171 +++- arch/x86/include/uapi/asm/bootparam.h | 4 +- arch/x86/include/uapi/asm/svm.h | 13 + arch/x86/kernel/Makefile | 1 - arch/x86/kernel/cc_platform.c | 2 + arch/x86/kernel/cpu/common.c | 4 + arch/x86/kernel/head64.c | 29 +- arch/x86/kernel/head_64.S | 37 +- arch/x86/kernel/probe_roms.c | 13 +- arch/x86/kernel/sev-shared.c | 529 +++++++++++- arch/x86/kernel/sev.c | 802 +++++++++++++++++- arch/x86/kernel/smpboot.c | 3 + arch/x86/kvm/cpuid.c | 19 +- arch/x86/kvm/svm/sev.c | 24 +- arch/x86/kvm/svm/svm.c | 4 +- arch/x86/kvm/svm/svm.h | 2 +- arch/x86/mm/mem_encrypt.c | 4 + arch/x86/mm/mem_encrypt_amd.c | 58 +- arch/x86/mm/mem_encrypt_identity.c | 8 + arch/x86/mm/pat/set_memory.c | 15 + drivers/virt/Kconfig | 3 + drivers/virt/Makefile | 1 + drivers/virt/coco/sevguest/Kconfig | 12 + drivers/virt/coco/sevguest/Makefile | 2 + drivers/virt/coco/sevguest/sevguest.c | 736 ++++++++++++++++ drivers/virt/coco/sevguest/sevguest.h | 98 +++ include/linux/cc_platform.h | 8 + include/linux/efi.h | 1 + include/uapi/linux/sev-guest.h | 80 ++ 52 files changed, 3639 insertions(+), 372 deletions(-) create mode 100644 Documentation/virt/coco/sevguest.rst create mode 100644 arch/x86/boot/compressed/efi.c create mode 100644 arch/x86/boot/msr.h create mode 100644 arch/x86/include/asm/cpuid.h create mode 100644 arch/x86/include/asm/shared/msr.h create mode 100644 drivers/virt/coco/sevguest/Kconfig create mode 100644 drivers/virt/coco/sevguest/Makefile create mode 100644 drivers/virt/coco/sevguest/sevguest.c create mode 100644 drivers/virt/coco/sevguest/sevguest.h create mode 100644 include/uapi/linux/sev-guest.h -- 2.25.1 ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-09 18:09 [PATCH v10 00/45] Add AMD Secure Nested Paging (SEV-SNP) Guest Support Brijesh Singh @ 2022-02-09 18:10 ` Brijesh Singh 2022-02-10 16:48 ` Borislav Petkov 2022-02-11 14:55 ` Borislav Petkov 0 siblings, 2 replies; 16+ messages in thread From: Brijesh Singh @ 2022-02-09 18:10 UTC (permalink / raw) To: x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm Cc: Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Borislav Petkov, Michael Roth, Vlastimil Babka, Kirill A . Shutemov, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy, Brijesh Singh The set_memory_{encrypted,decrypted}() are used for changing the pages from decrypted (shared) to encrypted (private) and vice versa. When SEV-SNP is active, the page state transition needs to go through additional steps done by the guest. If the page is transitioned from shared to private, then perform the following after the encryption attribute is set in the page table: 1. Issue the page state change VMGEXIT to add the memory region in the RMP table. 2. Validate the memory region after the RMP entry is added. To maintain the security guarantees, if the page is transitioned from private to shared, then perform the following before encryption attribute is removed from the page table: 1. Invalidate the page. 2. Issue the page state change VMGEXIT to remove the page from RMP table. To change the page state in the RMP table, use the Page State Change VMGEXIT defined in the GHCB specification. The GHCB specification provides the flexibility to use either 4K or 2MB page size in during the page state change (PSC) request. For now use the 4K page size for all the PSC until RMP page size tracking is supported in the kernel. Signed-off-by: Brijesh Singh <brijesh.singh@amd.com> --- arch/x86/include/asm/sev-common.h | 22 ++++ arch/x86/include/asm/sev.h | 4 + arch/x86/include/uapi/asm/svm.h | 2 + arch/x86/kernel/sev.c | 168 ++++++++++++++++++++++++++++++ arch/x86/mm/pat/set_memory.c | 15 +++ 5 files changed, 211 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index f077a6c95e67..1aa72b5c2490 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -105,6 +105,28 @@ enum psc_op { #define GHCB_HV_FT_SNP BIT_ULL(0) +/* SNP Page State Change NAE event */ +#define VMGEXIT_PSC_MAX_ENTRY 253 + +struct psc_hdr { + u16 cur_entry; + u16 end_entry; + u32 reserved; +} __packed; + +struct psc_entry { + u64 cur_page : 12, + gfn : 40, + operation : 4, + pagesize : 1, + reserved : 7; +} __packed; + +struct snp_psc_desc { + struct psc_hdr hdr; + struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY]; +} __packed; + #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index f65d257e3d4a..feeb93e6ec97 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -128,6 +128,8 @@ void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long padd void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages); void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op); +void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); +void snp_set_memory_private(unsigned long vaddr, unsigned int npages); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -142,6 +144,8 @@ early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned static inline void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { } +static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { } +static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } #endif #endif diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index b0ad00f4c1e1..0dcdb6e0c913 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -108,6 +108,7 @@ #define SVM_VMGEXIT_AP_JUMP_TABLE 0x80000005 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 #define SVM_VMGEXIT_GET_AP_JUMP_TABLE 1 +#define SVM_VMGEXIT_PSC 0x80000010 #define SVM_VMGEXIT_HV_FEATURES 0x8000fffd #define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff @@ -219,6 +220,7 @@ { SVM_VMGEXIT_NMI_COMPLETE, "vmgexit_nmi_complete" }, \ { SVM_VMGEXIT_AP_HLT_LOOP, "vmgexit_ap_hlt_loop" }, \ { SVM_VMGEXIT_AP_JUMP_TABLE, "vmgexit_ap_jump_table" }, \ + { SVM_VMGEXIT_PSC, "vmgexit_page_state_change" }, \ { SVM_VMGEXIT_HV_FEATURES, "vmgexit_hypervisor_feature" }, \ { SVM_EXIT_ERR, "invalid_guest_state" } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 1e8dc71e7ba6..4315be1602d1 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -655,6 +655,174 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op WARN(1, "invalid memory op %d\n", op); } +static int vmgexit_psc(struct snp_psc_desc *desc) +{ + int cur_entry, end_entry, ret = 0; + struct snp_psc_desc *data; + struct ghcb_state state; + struct es_em_ctxt ctxt; + unsigned long flags; + struct ghcb *ghcb; + + /* + * __sev_get_ghcb() needs to run with IRQs disabled because it is using + * a per-CPU GHCB. + */ + local_irq_save(flags); + + ghcb = __sev_get_ghcb(&state); + if (!ghcb) { + ret = 1; + goto out_unlock; + } + + /* Copy the input desc into GHCB shared buffer */ + data = (struct snp_psc_desc *)ghcb->shared_buffer; + memcpy(ghcb->shared_buffer, desc, min_t(int, GHCB_SHARED_BUF_SIZE, sizeof(*desc))); + + /* + * As per the GHCB specification, the hypervisor can resume the guest + * before processing all the entries. Check whether all the entries + * are processed. If not, then keep retrying. Note, the hypervisor + * will update the data memory directly to indicate the status, so + * reference the data->hdr everywhere. + * + * The strategy here is to wait for the hypervisor to change the page + * state in the RMP table before guest accesses the memory pages. If the + * page state change was not successful, then later memory access will + * result in a crash. + */ + cur_entry = data->hdr.cur_entry; + end_entry = data->hdr.end_entry; + + while (data->hdr.cur_entry <= data->hdr.end_entry) { + ghcb_set_sw_scratch(ghcb, (u64)__pa(data)); + + /* This will advance the shared buffer data points to. */ + ret = sev_es_ghcb_hv_call(ghcb, true, &ctxt, SVM_VMGEXIT_PSC, 0, 0); + + /* + * Page State Change VMGEXIT can pass error code through + * exit_info_2. + */ + if (WARN(ret || ghcb->save.sw_exit_info_2, + "SNP: PSC failed ret=%d exit_info_2=%llx\n", + ret, ghcb->save.sw_exit_info_2)) { + ret = 1; + goto out; + } + + /* Verify that reserved bit is not set */ + if (WARN(data->hdr.reserved, "Reserved bit is set in the PSC header\n")) { + ret = 1; + goto out; + } + + /* + * Sanity check that entry processing is not going backwards. + * This will happen only if hypervisor is tricking us. + */ + if (WARN(data->hdr.end_entry > end_entry || cur_entry > data->hdr.cur_entry, +"SNP: PSC processing going backward, end_entry %d (got %d) cur_entry %d (got %d)\n", + end_entry, data->hdr.end_entry, cur_entry, data->hdr.cur_entry)) { + ret = 1; + goto out; + } + } + +out: + __sev_put_ghcb(&state); + +out_unlock: + local_irq_restore(flags); + + return ret; +} + +static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, + unsigned long vaddr_end, int op) +{ + struct psc_hdr *hdr; + struct psc_entry *e; + unsigned long pfn; + int i; + + hdr = &data->hdr; + e = data->entries; + + memset(data, 0, sizeof(*data)); + i = 0; + + while (vaddr < vaddr_end) { + if (is_vmalloc_addr((void *)vaddr)) + pfn = vmalloc_to_pfn((void *)vaddr); + else + pfn = __pa(vaddr) >> PAGE_SHIFT; + + e->gfn = pfn; + e->operation = op; + hdr->end_entry = i; + + /* + * Current SNP implementation doesn't keep track of the RMP page + * size so use 4K for simplicity. + */ + e->pagesize = RMP_PG_SIZE_4K; + + vaddr = vaddr + PAGE_SIZE; + e++; + i++; + } + + if (vmgexit_psc(data)) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); +} + +static void set_pages_state(unsigned long vaddr, unsigned int npages, int op) +{ + unsigned long vaddr_end, next_vaddr; + struct snp_psc_desc *desc; + + desc = kmalloc(sizeof(*desc), GFP_KERNEL_ACCOUNT); + if (!desc) + panic("SNP: failed to allocate memory for PSC descriptor\n"); + + vaddr = vaddr & PAGE_MASK; + vaddr_end = vaddr + (npages << PAGE_SHIFT); + + while (vaddr < vaddr_end) { + /* Calculate the last vaddr that fits in one struct snp_psc_desc. */ + next_vaddr = min_t(unsigned long, vaddr_end, + (VMGEXIT_PSC_MAX_ENTRY * PAGE_SIZE) + vaddr); + + __set_pages_state(desc, vaddr, next_vaddr, op); + + vaddr = next_vaddr; + } + + kfree(desc); +} + +void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) +{ + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) + return; + + pvalidate_pages(vaddr, npages, false); + + set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED); +} + +void snp_set_memory_private(unsigned long vaddr, unsigned int npages) +{ + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) + return; + + set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); + + pvalidate_pages(vaddr, npages, true); +} + int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { u16 startup_cs, startup_ip; diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index b4072115c8ef..e58d57b038ee 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -32,6 +32,7 @@ #include <asm/set_memory.h> #include <asm/hyperv-tlfs.h> #include <asm/mshyperv.h> +#include <asm/sev.h> #include "../mm_internal.h" @@ -2012,8 +2013,22 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) */ cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT)); + /* + * To maintain the security guarantees of SEV-SNP guests, make sure + * to invalidate the memory before clearing the encryption attribute. + */ + if (!enc) + snp_set_memory_shared(addr, numpages); + ret = __change_page_attr_set_clr(&cpa, 1); + /* + * Now that memory is mapped encrypted in the page table, validate it + * so that it is consistent with the above page state. + */ + if (!ret && enc) + snp_set_memory_private(addr, numpages); + /* * After changing the encryption attribute, we need to flush TLBs again * in case any speculative TLB caching occurred (but no need to flush -- 2.25.1 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-09 18:10 ` [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit Brijesh Singh @ 2022-02-10 16:48 ` Borislav Petkov 2022-02-11 14:55 ` Borislav Petkov 1 sibling, 0 replies; 16+ messages in thread From: Borislav Petkov @ 2022-02-10 16:48 UTC (permalink / raw) To: Brijesh Singh Cc: x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Kirill A . Shutemov, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Wed, Feb 09, 2022 at 12:10:15PM -0600, Brijesh Singh wrote: > The set_memory_{encrypted,decrypted}() are used for changing the pages > from decrypted (shared) to encrypted (private) and vice versa. > When SEV-SNP is active, the page state transition needs to go through > additional steps done by the guest. > > If the page is transitioned from shared to private, then perform the > following after the encryption attribute is set in the page table: > > 1. Issue the page state change VMGEXIT to add the memory region in > the RMP table. > 2. Validate the memory region after the RMP entry is added. > > To maintain the security guarantees, if the page is transitioned from > private to shared, then perform the following before encryption attribute > is removed from the page table: > > 1. Invalidate the page. > 2. Issue the page state change VMGEXIT to remove the page from RMP table. > > To change the page state in the RMP table, use the Page State Change > VMGEXIT defined in the GHCB specification. > > The GHCB specification provides the flexibility to use either 4K or 2MB > page size in during the page state change (PSC) request. For now use the > 4K page size for all the PSC until RMP page size tracking is supported > in the kernel. This commit message sounds familiar because I've read it before - patch 18 - and it looks copied. So I've turned into a simple one which says it all: x86/mm: Validate memory when changing the C-bit Add the needed functionality to change pages state from shared to private and vice-versa using the Page State Change VMGEXIT as documented in the GHCB spec. Thx. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-09 18:10 ` [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit Brijesh Singh 2022-02-10 16:48 ` Borislav Petkov @ 2022-02-11 14:55 ` Borislav Petkov 2022-02-11 17:27 ` Brijesh Singh 2022-02-16 13:32 ` Borislav Petkov 1 sibling, 2 replies; 16+ messages in thread From: Borislav Petkov @ 2022-02-11 14:55 UTC (permalink / raw) To: Brijesh Singh, Kirill A . Shutemov Cc: x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy + Kirill. On Wed, Feb 09, 2022 at 12:10:15PM -0600, Brijesh Singh wrote: > @@ -2012,8 +2013,22 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) > */ > cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT)); > > + /* > + * To maintain the security guarantees of SEV-SNP guests, make sure > + * to invalidate the memory before clearing the encryption attribute. > + */ > + if (!enc) > + snp_set_memory_shared(addr, numpages); > + > ret = __change_page_attr_set_clr(&cpa, 1); > > + /* > + * Now that memory is mapped encrypted in the page table, validate it > + * so that it is consistent with the above page state. > + */ > + if (!ret && enc) > + snp_set_memory_private(addr, numpages); > + > /* > * After changing the encryption attribute, we need to flush TLBs again > * in case any speculative TLB caching occurred (but no need to flush > -- Right, as tglx rightfully points out here: https://lore.kernel.org/r/875ypyvz07.ffs@tglx this piece of code needs careful coordinated design so that it is clean for both SEV and TDX. First, as we've said here: https://lore.kernel.org/r/1d77e91c-e151-7846-6cd4-6264236ca5ae@intel.com we'd need generic functions which turn a pgprot into an encrypted or decrypted pgprot on both SEV and TDX so we could do: cc_pgprot_enc() cc_pgprot_dec() which does the required conversion on each guest type. Also, I think adding required functions to x86_platform.guest. is a very nice way to solve the ugly if (guest_type) querying all over the place. Also, I was thinking of sme_me_mask and the corresponding tdx_shared_mask I threw into the mix here: https://lore.kernel.org/r/YgFIaJ8ijgQQ04Nv@zn.tnic and we should simply add those without ifdeffery but unconditionally. Simply have them always present. They will have !0 values on the respective guest types and 0 otherwise. This should simplify a lot of code and another unconditionally present u64 won't be the end of the world. Any other aspect I'm missing? -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-11 14:55 ` Borislav Petkov @ 2022-02-11 17:27 ` Brijesh Singh 2022-02-13 12:15 ` Borislav Petkov 2022-02-16 13:32 ` Borislav Petkov 1 sibling, 1 reply; 16+ messages in thread From: Brijesh Singh @ 2022-02-11 17:27 UTC (permalink / raw) To: Borislav Petkov, Kirill A . Shutemov Cc: brijesh.singh, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On 2/11/22 8:55 AM, Borislav Petkov wrote: > > Simply have them always present. They will have !0 values on the > respective guest types and 0 otherwise. This should simplify a lot of > code and another unconditionally present u64 won't be the end of the > world. > > Any other aspect I'm missing? I think that's mostly about it. IIUC, the recommendation is to define a new callback in x86_platform_op. The callback will be invoked unconditionally; The default implementation for this callback is NOP; The TDX and SEV will override with the platform specific implementation. I think we may able to handle everything in one callback hook but having pre and post will be a more desirable. Here is why I am thinking so: * On SNP, the page must be invalidated before clearing the _PAGE_ENC from the page table attribute * On SNP, the page must be validated after setting the _PAGE_ENC in the page table attribute. ~Brijesh ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-11 17:27 ` Brijesh Singh @ 2022-02-13 12:15 ` Borislav Petkov 2022-02-13 14:50 ` Tom Lendacky 2022-02-15 12:43 ` Kirill A. Shutemov 0 siblings, 2 replies; 16+ messages in thread From: Borislav Petkov @ 2022-02-13 12:15 UTC (permalink / raw) To: Brijesh Singh, Kirill A . Shutemov Cc: x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Fri, Feb 11, 2022 at 11:27:54AM -0600, Brijesh Singh wrote: > > Simply have them always present. They will have !0 values on the > > respective guest types and 0 otherwise. This should simplify a lot of > > code and another unconditionally present u64 won't be the end of the > > world. > > > > Any other aspect I'm missing? > > I think that's mostly about it. IIUC, the recommendation is to define a > new callback in x86_platform_op. The callback will be invoked > unconditionally; The default implementation for this callback is NOP; > The TDX and SEV will override with the platform specific implementation. > I think we may able to handle everything in one callback hook but having > pre and post will be a more desirable. Here is why I am thinking so: > > * On SNP, the page must be invalidated before clearing the _PAGE_ENC > from the page table attribute > > * On SNP, the page must be validated after setting the _PAGE_ENC in the > page table attribute. Right, we could have a pre- and post- callback, if that would make things simpler/clearer. Also, in thinking further about the encryption mask, we could make it a *single*, *global* variable called cc_mask which each guest type sets it as it wants to. Then, it would use it in the vendor-specific encrypt/decrypt helpers accordingly and that would simplify a lot of code. And we can get rid of all the ifdeffery around it too. So I think the way to go should be we do the common functionality, I queue it on the common tip:x86/cc branch and then SNP and TDX will be both based ontop of it. Thoughts? -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-13 12:15 ` Borislav Petkov @ 2022-02-13 14:50 ` Tom Lendacky 2022-02-13 17:21 ` Borislav Petkov 2022-02-15 12:43 ` Kirill A. Shutemov 1 sibling, 1 reply; 16+ messages in thread From: Tom Lendacky @ 2022-02-13 14:50 UTC (permalink / raw) To: Borislav Petkov, Brijesh Singh, Kirill A . Shutemov Cc: x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On 2/13/22 06:15, Borislav Petkov wrote: > On Fri, Feb 11, 2022 at 11:27:54AM -0600, Brijesh Singh wrote: >>> Simply have them always present. They will have !0 values on the >>> respective guest types and 0 otherwise. This should simplify a lot of >>> code and another unconditionally present u64 won't be the end of the >>> world. >>> >>> Any other aspect I'm missing? >> >> I think that's mostly about it. IIUC, the recommendation is to define a >> new callback in x86_platform_op. The callback will be invoked >> unconditionally; The default implementation for this callback is NOP; >> The TDX and SEV will override with the platform specific implementation. >> I think we may able to handle everything in one callback hook but having >> pre and post will be a more desirable. Here is why I am thinking so: >> >> * On SNP, the page must be invalidated before clearing the _PAGE_ENC >> from the page table attribute >> >> * On SNP, the page must be validated after setting the _PAGE_ENC in the >> page table attribute. > > Right, we could have a pre- and post- callback, if that would make > things simpler/clearer. > > Also, in thinking further about the encryption mask, we could make it a > *single*, *global* variable called cc_mask which each guest type sets it > as it wants to. > > Then, it would use it in the vendor-specific encrypt/decrypt helpers > accordingly and that would simplify a lot of code. And we can get rid of > all the ifdeffery around it too. > > So I think the way to go should be we do the common functionality, I > queue it on the common tip:x86/cc branch and then SNP and TDX will be > both based ontop of it. > > Thoughts? I think there were a lot of assumptions that only SME/SEV would set sme_me_mask and that is used, for example, in the cc_platform_has() routine to figure out whether we're AMD or Intel. If you go the cc_mask route, I think we'll need to add a cc_vendor variable that would then be checked in cc_platform_has(). All other uses of sme_me_mask would need to be audited to see whether cc_vendor would need to be checked, too. Thanks, Tom > ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-13 14:50 ` Tom Lendacky @ 2022-02-13 17:21 ` Borislav Petkov 0 siblings, 0 replies; 16+ messages in thread From: Borislav Petkov @ 2022-02-13 17:21 UTC (permalink / raw) To: Tom Lendacky Cc: Brijesh Singh, Kirill A . Shutemov, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Sun, Feb 13, 2022 at 08:50:48AM -0600, Tom Lendacky wrote: > I think there were a lot of assumptions that only SME/SEV would set > sme_me_mask and that is used, for example, in the cc_platform_has() routine > to figure out whether we're AMD or Intel. If you go the cc_mask route, I > think we'll need to add a cc_vendor variable that would then be checked in > cc_platform_has(). Right, or cc_platform_type or whatever. It would probably be a good idea to have a variable explicitly state what the active coco flavor is anyway, as we had some ambiguity questions in the past along the lines of, what does cc_platform_has() need to return when running as a guest on the respective platform. If you have it explicitly, then it would work unambiguously simple. And then we can get rid of CC_ATTR_GUEST_SEV_SNP or CC_ATTR_GUEST_TDX which is clumsy. Thx. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-13 12:15 ` Borislav Petkov 2022-02-13 14:50 ` Tom Lendacky @ 2022-02-15 12:43 ` Kirill A. Shutemov 2022-02-15 12:54 ` Borislav Petkov 1 sibling, 1 reply; 16+ messages in thread From: Kirill A. Shutemov @ 2022-02-15 12:43 UTC (permalink / raw) To: Borislav Petkov Cc: Brijesh Singh, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Sun, Feb 13, 2022 at 01:15:23PM +0100, Borislav Petkov wrote: > On Fri, Feb 11, 2022 at 11:27:54AM -0600, Brijesh Singh wrote: > > > Simply have them always present. They will have !0 values on the > > > respective guest types and 0 otherwise. This should simplify a lot of > > > code and another unconditionally present u64 won't be the end of the > > > world. > > > > > > Any other aspect I'm missing? > > > > I think that's mostly about it. IIUC, the recommendation is to define a > > new callback in x86_platform_op. The callback will be invoked > > unconditionally; The default implementation for this callback is NOP; > > The TDX and SEV will override with the platform specific implementation. > > I think we may able to handle everything in one callback hook but having > > pre and post will be a more desirable. Here is why I am thinking so: > > > > * On SNP, the page must be invalidated before clearing the _PAGE_ENC > > from the page table attribute > > > > * On SNP, the page must be validated after setting the _PAGE_ENC in the > > page table attribute. > > Right, we could have a pre- and post- callback, if that would make > things simpler/clearer. > > Also, in thinking further about the encryption mask, we could make it a > *single*, *global* variable called cc_mask which each guest type sets it > as it wants to. I don't think it works. TDX and SME/SEV has opposite polarity of the mask. SME/SEV has to clear the mask to share the page. TDX has to set it. Making a single global mask only increases confusion. -- Kirill A. Shutemov ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-15 12:43 ` Kirill A. Shutemov @ 2022-02-15 12:54 ` Borislav Petkov 2022-02-15 13:15 ` Kirill A. Shutemov 0 siblings, 1 reply; 16+ messages in thread From: Borislav Petkov @ 2022-02-15 12:54 UTC (permalink / raw) To: Kirill A. Shutemov Cc: Brijesh Singh, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Tue, Feb 15, 2022 at 03:43:31PM +0300, Kirill A. Shutemov wrote: > I don't think it works. TDX and SME/SEV has opposite polarity of the mask. > SME/SEV has to clear the mask to share the page. TDX has to set it. > > Making a single global mask only increases confusion. Didn't you read the rest of the thread with Tom's suggestion? I think there's a merit in having a cc_vendor or so which explicitly states what type of HV the kernel runs on... -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-15 12:54 ` Borislav Petkov @ 2022-02-15 13:15 ` Kirill A. Shutemov 2022-02-15 14:41 ` Borislav Petkov 0 siblings, 1 reply; 16+ messages in thread From: Kirill A. Shutemov @ 2022-02-15 13:15 UTC (permalink / raw) To: Borislav Petkov Cc: Brijesh Singh, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Tue, Feb 15, 2022 at 01:54:48PM +0100, Borislav Petkov wrote: > On Tue, Feb 15, 2022 at 03:43:31PM +0300, Kirill A. Shutemov wrote: > > I don't think it works. TDX and SME/SEV has opposite polarity of the mask. > > SME/SEV has to clear the mask to share the page. TDX has to set it. > > > > Making a single global mask only increases confusion. > > Didn't you read the rest of the thread with Tom's suggestion? I think > there's a merit in having a cc_vendor or so which explicitly states what > type of HV the kernel runs on... I have no problem with cc_vendor idea. It looks good. Regarding the masks, if we want to have common ground here we can add two mask: cc_enc_mask and cc_dec_mask. And then pgprotval_t cc_enc(pgprotval_t protval) { protval |= cc_enc_mask; protval &= ~cc_dec_mask; return protval; } pgprotval_t cc_dec(pgprotval_t protval) { protval |= cc_dec_mask; protval &= ~cc_enc_mask; return protval; } It assumes (cc_enc_mask & cc_dec_mask) == 0. Any opinions? -- Kirill A. Shutemov ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-15 13:15 ` Kirill A. Shutemov @ 2022-02-15 14:41 ` Borislav Petkov 0 siblings, 0 replies; 16+ messages in thread From: Borislav Petkov @ 2022-02-15 14:41 UTC (permalink / raw) To: Kirill A. Shutemov Cc: Brijesh Singh, x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Tue, Feb 15, 2022 at 04:15:22PM +0300, Kirill A. Shutemov wrote: > I have no problem with cc_vendor idea. It looks good. Good. > Regarding the masks, if we want to have common ground here we can add two > mask: cc_enc_mask and cc_dec_mask. And then If we do two masks, then we can just as well leave the SME and TDX masks. The point of the whole exercise is to have simpler code and less ifdeffery. If you "hide" how the mask works on each vendor in the respective functions - and yes, cc_pgprot_dec/enc() reads better - then it doesn't matter how the mask is defined. Because you don't need two masks to encrypt/decrypt pages - you need a single mask but apply it differently. Thx. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit 2022-02-11 14:55 ` Borislav Petkov 2022-02-11 17:27 ` Brijesh Singh @ 2022-02-16 13:32 ` Borislav Petkov 1 sibling, 0 replies; 16+ messages in thread From: Borislav Petkov @ 2022-02-16 13:32 UTC (permalink / raw) To: Brijesh Singh, Kirill A . Shutemov Cc: x86, linux-kernel, kvm, linux-efi, platform-driver-x86, linux-coco, linux-mm, Thomas Gleixner, Ingo Molnar, Joerg Roedel, Tom Lendacky, H. Peter Anvin, Ard Biesheuvel, Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Jim Mattson, Andy Lutomirski, Dave Hansen, Sergio Lopez, Peter Gonda, Peter Zijlstra, Srinivas Pandruvada, David Rientjes, Dov Murik, Tobin Feldman-Fitzthum, Michael Roth, Vlastimil Babka, Andi Kleen, Dr . David Alan Gilbert, brijesh.ksingh, tony.luck, marcorr, sathyanarayanan.kuppuswamy On Fri, Feb 11, 2022 at 03:55:23PM +0100, Borislav Petkov wrote: > Also, I think adding required functions to x86_platform.guest. is a very > nice way to solve the ugly if (guest_type) querying all over the place. So I guess something like below. It builds here... --- arch/x86/include/asm/set_memory.h | 1 - arch/x86/include/asm/sev.h | 2 ++ arch/x86/include/asm/x86_init.h | 12 ++++++++++++ arch/x86/kernel/sev.c | 2 ++ arch/x86/mm/mem_encrypt_amd.c | 6 +++--- arch/x86/mm/pat/set_memory.c | 2 +- 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index ff0f2d90338a..ce8dd215f5b3 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -84,7 +84,6 @@ int set_pages_rw(struct page *page, int numpages); int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); bool kernel_page_present(struct page *page); -void notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc); extern int kernel_set_to_readonly; diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ec060c433589..2435b0ca6cfc 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -95,4 +95,6 @@ static inline void sev_es_nmi_complete(void) { } static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } #endif +void amd_notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc); + #endif diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 22b7412c08f6..226663e2d769 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -141,6 +141,17 @@ struct x86_init_acpi { void (*reduced_hw_early_init)(void); }; +/** + * struct x86_guest - Functions used by misc guest incarnations like SEV, TDX, + * etc. + * + * @enc_status_change Notify HV about change of encryption status of a + * range of pages + */ +struct x86_guest { + void (*enc_status_change)(unsigned long vaddr, int npages, bool enc); +}; + /** * struct x86_init_ops - functions for platform specific setup * @@ -287,6 +298,7 @@ struct x86_platform_ops { struct x86_legacy_features legacy; void (*set_legacy_features)(void); struct x86_hyper_runtime hyper; + struct x86_guest guest; }; struct x86_apic_ops { diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index e6d316a01fdd..e645e868a49b 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -766,6 +766,8 @@ void __init sev_es_init_vc_handling(void) if (!sev_es_check_cpu_features()) panic("SEV-ES CPU Features missing"); + x86_platform.guest.enc_status_change = amd_notify_range_enc_status_changed; + /* Enable SEV-ES special handling */ static_branch_enable(&sev_es_enable_key); diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index 2b2d018ea345..7038a9f7ae55 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -256,7 +256,7 @@ static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot) return pfn; } -void notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc) +void amd_notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc) { #ifdef CONFIG_PARAVIRT unsigned long sz = npages << PAGE_SHIFT; @@ -392,7 +392,7 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr, ret = 0; - notify_range_enc_status_changed(start, PAGE_ALIGN(size) >> PAGE_SHIFT, enc); + amd_notify_range_enc_status_changed(start, PAGE_ALIGN(size) >> PAGE_SHIFT, enc); out: __flush_tlb_all(); return ret; @@ -410,7 +410,7 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size) void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages, bool enc) { - notify_range_enc_status_changed(vaddr, npages, enc); + amd_notify_range_enc_status_changed(vaddr, npages, enc); } void __init mem_encrypt_free_decrypted_mem(void) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index b4072115c8ef..0acc52a3a5b7 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2027,7 +2027,7 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) * Notify hypervisor that a given memory range is mapped encrypted * or decrypted. */ - notify_range_enc_status_changed(addr, numpages, enc); + x86_platform.guest.enc_status_change(addr, numpages, enc); return ret; } -- 2.29.2 -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply related [flat|nested] 16+ messages in thread
end of thread, other threads:[~2022-02-21 19:54 UTC | newest] Thread overview: 16+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- [not found] <Ygz88uacbwuTTNat@zn.tnic.mbx> 2022-02-16 16:04 ` [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit Brijesh Singh 2022-02-21 17:41 ` Kirill A. Shutemov 2022-02-21 18:06 ` Borislav Petkov 2022-02-21 19:54 ` Brijesh Singh 2022-02-09 18:09 [PATCH v10 00/45] Add AMD Secure Nested Paging (SEV-SNP) Guest Support Brijesh Singh 2022-02-09 18:10 ` [PATCH v10 21/45] x86/mm: Add support to validate memory when changing C-bit Brijesh Singh 2022-02-10 16:48 ` Borislav Petkov 2022-02-11 14:55 ` Borislav Petkov 2022-02-11 17:27 ` Brijesh Singh 2022-02-13 12:15 ` Borislav Petkov 2022-02-13 14:50 ` Tom Lendacky 2022-02-13 17:21 ` Borislav Petkov 2022-02-15 12:43 ` Kirill A. Shutemov 2022-02-15 12:54 ` Borislav Petkov 2022-02-15 13:15 ` Kirill A. Shutemov 2022-02-15 14:41 ` Borislav Petkov 2022-02-16 13:32 ` Borislav Petkov
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).