Static per-CPU buffers must be shared before the page allocator is ready.
Provide a page conversion callback with common early page-table splitting,
including the kernel-image alias used by UP data. Flush large translations
before changing attributes and retain the AMD content and SNP transitions.
Drop UP image aliases before SNP kexec makes the backing pages private.

Suggested-by: Kiryl Shutsemau <[email protected]>
Link: https://lore.kernel.org/r/aqqGUAX65s4LdJkr@thinkstation
Signed-off-by: Zack Rusin <[email protected]>
---
 arch/x86/coco/sev/core.c           |  16 +++++
 arch/x86/include/asm/mem_encrypt.h |   7 ++-
 arch/x86/include/asm/x86_init.h    |   2 +
 arch/x86/mm/mem_encrypt.c          | 117 +++++++++++++++++++++++++++++++++++++
 arch/x86/mm/mem_encrypt_amd.c      |  12 +++-
 5 files changed, 149 insertions(+), 5 deletions(-)

diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index cc292d7c6fd1..bc1afa366c33 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -541,6 +541,10 @@ static void set_pte_enc(pte_t *kpte, int level, void *va)
        set_pte_enc_mask(kpte, d.pfn, d.new_pgprot);
 }
 
+#ifndef CONFIG_SMP
+extern char __start_percpu_decrypted[], __end_percpu_decrypted[];
+#endif
+
 static void unshare_all_memory(void)
 {
        unsigned long addr, end, size, ghcb;
@@ -550,6 +554,18 @@ static void unshare_all_memory(void)
        pte_t *pte;
        int cpu;
 
+#ifndef CONFIG_SMP
+       /* Drop image aliases before the direct-map walk makes pages private. */
+       addr = (unsigned long)__start_percpu_decrypted;
+       end = (unsigned long)__end_percpu_decrypted;
+       for (; addr < end; addr += PAGE_SIZE) {
+               pte = lookup_address(addr, &level);
+               if (pte && pte_decrypted(*pte))
+                       set_pte(pte, __pte(0));
+       }
+       __flush_tlb_all();
+#endif
+
        /* Unshare the direct mapping. */
        addr = PAGE_OFFSET;
        end  = PAGE_OFFSET + get_max_mapped();
diff --git a/arch/x86/include/asm/mem_encrypt.h 
b/arch/x86/include/asm/mem_encrypt.h
index ea6494628cb0..4d81f693b1d3 100644
--- a/arch/x86/include/asm/mem_encrypt.h
+++ b/arch/x86/include/asm/mem_encrypt.h
@@ -21,9 +21,13 @@ struct boot_params;
 #ifdef CONFIG_X86_MEM_ENCRYPT
 void __init mem_encrypt_init(void);
 void __init mem_encrypt_setup_arch(void);
+int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size);
+void __init early_set_page_decrypted(unsigned long addr, unsigned long alias);
 #else
 static inline void mem_encrypt_init(void) { }
 static inline void __init mem_encrypt_setup_arch(void) { }
+static inline int __init
+early_set_memory_decrypted(unsigned long vaddr, unsigned long size) { return 
0; }
 #endif
 
 #ifdef CONFIG_AMD_MEM_ENCRYPT
@@ -50,7 +54,6 @@ void __init sme_early_init(void);
 void sme_encrypt_kernel(struct boot_params *bp);
 void sme_enable(struct boot_params *bp);
 
-int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size);
 int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size);
 void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr,
                                            unsigned long size, bool enc);
@@ -86,8 +89,6 @@ static inline void sme_enable(struct boot_params *bp) { }
 
 static inline void sev_es_init_vc_handling(void) { }
 
-static inline int __init
-early_set_memory_decrypted(unsigned long vaddr, unsigned long size) { return 
0; }
 static inline int __init
 early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 
0; }
 static inline void __init
diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h
index 953d3199408a..e4131402c783 100644
--- a/arch/x86/include/asm/x86_init.h
+++ b/arch/x86/include/asm/x86_init.h
@@ -75,9 +75,11 @@ struct x86_init_oem {
  *                     the kernel pagetables and prepare accessors functions.
  *                     Callback must call paging_init(). Called once after the
  *                     direct mapping for phys memory is available.
+ * @early_decrypt_page:        Share a direct-mapped page and its optional 
image alias
  */
 struct x86_init_paging {
        void (*pagetable_init)(void);
+       int (*early_decrypt_page)(unsigned long addr, unsigned long alias);
 };
 
 /**
diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c
index 3aefdef5bcb0..c3e239a47b66 100644
--- a/arch/x86/mm/mem_encrypt.c
+++ b/arch/x86/mm/mem_encrypt.c
@@ -12,10 +12,127 @@
 #include <linux/swiotlb.h>
 #include <linux/cc_platform.h>
 #include <linux/mem_encrypt.h>
+#include <linux/pgalloc.h>
 #include <linux/virtio_anchor.h>
 #include <linux/iommu-dma.h>
 
+#include <asm/sections.h>
+#include <asm/set_memory.h>
 #include <asm/sev.h>
+#include <asm/tlbflush.h>
+#include <asm/x86_init.h>
+
+#include "mm_internal.h"
+
+static pte_t * __init early_lookup_pte(unsigned long addr)
+{
+       unsigned long pfn, step;
+       unsigned int level, i;
+       pgprot_t prot;
+       pte_t *pte, *table;
+
+       for (;;) {
+               pte = lookup_address(addr, &level);
+               if (!pte || !pte_present(*pte))
+                       return NULL;
+               if (level == PG_LEVEL_4K)
+                       return pte;
+
+               if (level == PG_LEVEL_2M) {
+                       pfn = pmd_pfn(*(pmd_t *)pte);
+                       prot = pgprot_large_2_4k(pmd_pgprot(*(pmd_t *)pte));
+                       step = 1;
+               } else if (level == PG_LEVEL_1G) {
+                       pfn = pud_pfn(*(pud_t *)pte);
+                       prot = pud_pgprot(*(pud_t *)pte);
+                       step = PMD_SIZE >> PAGE_SHIFT;
+               } else {
+                       return NULL;
+               }
+
+               table = alloc_low_page();
+               if (!table)
+                       return NULL;
+               for (i = 0; i < PTRS_PER_PTE; i++, pfn += step)
+                       set_pte(&table[i], pfn_pte(pfn, prot));
+
+               if (level == PG_LEVEL_2M)
+                       pmd_populate_kernel(&init_mm, (pmd_t *)pte, table);
+               else
+                       pud_populate(&init_mm, (pud_t *)pte, (pmd_t *)table);
+
+               if (addr - PAGE_OFFSET < get_max_mapped()) {
+                       update_page_count(level, -1);
+                       update_page_count(level - 1, PTRS_PER_PTE);
+               }
+
+               /* Flush the large translation before changing any attributes. 
*/
+               __flush_tlb_all();
+       }
+}
+
+/* The caller has split both mappings before starting the page transition. */
+void __init early_set_page_decrypted(unsigned long addr, unsigned long alias)
+{
+       unsigned int level;
+       pte_t *pte;
+
+       pte = lookup_address(addr, &level);
+       set_pte(pte, __pte(cc_mkdec(pte_val(*pte))));
+       if (alias) {
+               pte = lookup_address(alias, &level);
+               set_pte(pte, __pte(cc_mkdec(pte_val(*pte))));
+       }
+       __flush_tlb_all();
+}
+
+/* Boot CPU only; size is in bytes and the contents are preserved. */
+int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
+{
+       unsigned long end, addr, alias, pa;
+       pte_t *pte;
+       int ret;
+
+       if (!size || !cc_platform_has(CC_ATTR_MEM_ENCRYPT))
+               return 0;
+       if (!x86_init.paging.early_decrypt_page)
+               return -EOPNOTSUPP;
+       if (size > ULONG_MAX - vaddr)
+               return -EINVAL;
+       end = PAGE_ALIGN(vaddr + size);
+       if (end < vaddr)
+               return -EINVAL;
+
+       for (vaddr &= PAGE_MASK; vaddr < end; vaddr += PAGE_SIZE) {
+               if (vaddr - PAGE_OFFSET >= get_max_mapped() &&
+                   (vaddr < (unsigned long)_text || vaddr >= _brk_end))
+                       return -EINVAL;
+
+               pa = __pa(vaddr);
+               addr = (unsigned long)__va(pa);
+               pte = early_lookup_pte(addr);
+               if (!pte || pte_pfn(*pte) != PHYS_PFN(pa))
+                       return -EFAULT;
+
+               alias = 0;
+               if (pa >= __pa_symbol(_text) &&
+                   pa <= __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1)) {
+                       alias = (unsigned long)_text + pa - __pa_symbol(_text);
+                       if (!early_lookup_pte(alias))
+                               return -EFAULT;
+               }
+
+               if (pte_decrypted(*pte)) {
+                       early_set_page_decrypted(addr, alias);
+                       continue;
+               }
+               ret = x86_init.paging.early_decrypt_page(addr, alias);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
 
 /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
 bool force_dma_unencrypted(struct device *dev)
diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c
index 2f8c32173972..e854f3a039f7 100644
--- a/arch/x86/mm/mem_encrypt_amd.c
+++ b/arch/x86/mm/mem_encrypt_amd.c
@@ -459,9 +459,15 @@ static int __init early_set_memory_enc_dec(unsigned long 
vaddr,
        return ret;
 }
 
-int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
+static int __init amd_early_decrypt_page(unsigned long addr, unsigned long 
alias)
 {
-       return early_set_memory_enc_dec(vaddr, size, false);
+       unsigned int level;
+       pte_t *pte = lookup_address(addr, &level);
+
+       __set_clr_pte_enc(pte, PG_LEVEL_4K, false);
+       early_set_page_decrypted(addr, alias);
+       early_set_mem_enc_dec_hypercall(addr, PAGE_SIZE, false);
+       return 0;
 }
 
 int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
@@ -479,6 +485,8 @@ void __init sme_early_init(void)
        if (!sme_me_mask)
                return;
 
+       x86_init.paging.early_decrypt_page = amd_early_decrypt_page;
+
        early_pmd_flags = __sme_set(early_pmd_flags);
 
        __supported_pte_mask = __sme_set(__supported_pte_mask);

-- 
2.53.0


Reply via email to