x86/sev: Replace occurrences of sev_active() with cc_platform_has()
authorTom Lendacky <thomas.lendacky@amd.com>
Wed, 8 Sep 2021 22:58:37 +0000 (17:58 -0500)
committerBorislav Petkov <bp@suse.de>
Mon, 4 Oct 2021 09:46:58 +0000 (11:46 +0200)
Replace uses of sev_active() with the more generic cc_platform_has()
using CC_ATTR_GUEST_MEM_ENCRYPT. If future support is added for other
memory encryption technologies, the use of CC_ATTR_GUEST_MEM_ENCRYPT
can be updated, as required.

Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20210928191009.32551-7-bp@alien8.de
arch/x86/include/asm/mem_encrypt.h
arch/x86/kernel/crash_dump_64.c
arch/x86/kernel/kvm.c
arch/x86/kernel/kvmclock.c
arch/x86/kernel/machine_kexec_64.c
arch/x86/kvm/svm/svm.c
arch/x86/mm/ioremap.c
arch/x86/mm/mem_encrypt.c
arch/x86/platform/efi/efi_64.c

index 63c5b99ccae5c7b9787fe20aa04ece9ec06b847b..a5a58ccd1ee395e6230433d78d3f55e3be23b9e8 100644 (file)
@@ -51,7 +51,6 @@ void __init mem_encrypt_free_decrypted_mem(void);
 void __init mem_encrypt_init(void);
 
 void __init sev_es_init_vc_handling(void);
-bool sev_active(void);
 bool sev_es_active(void);
 
 #define __bss_decrypted __section(".bss..decrypted")
@@ -75,7 +74,6 @@ static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }
 static inline void __init sme_enable(struct boot_params *bp) { }
 
 static inline void sev_es_init_vc_handling(void) { }
-static inline bool sev_active(void) { return false; }
 static inline bool sev_es_active(void) { return false; }
 
 static inline int __init
index 045e82e8945b3687e52d51bef8d59f5f2240e363..a7f617a3981d451c002317ac9bb64ab3baeaaa27 100644 (file)
@@ -10,6 +10,7 @@
 #include <linux/crash_dump.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
+#include <linux/cc_platform.h>
 
 static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
                                  unsigned long offset, int userbuf,
@@ -73,5 +74,6 @@ ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
 
 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
 {
-       return read_from_oldmem(buf, count, ppos, 0, sev_active());
+       return read_from_oldmem(buf, count, ppos, 0,
+                               cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT));
 }
index b656456c3a94461df54fcf05df2a8b53df801101..8863d1941f1bedff318aa234561701d3ef5c1031 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/nmi.h>
 #include <linux/swait.h>
 #include <linux/syscore_ops.h>
+#include <linux/cc_platform.h>
 #include <asm/timer.h>
 #include <asm/cpu.h>
 #include <asm/traps.h>
@@ -418,7 +419,7 @@ static void __init sev_map_percpu_data(void)
 {
        int cpu;
 
-       if (!sev_active())
+       if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                return;
 
        for_each_possible_cpu(cpu) {
index 73c74b961d0fd9b69b6dd0f9fab50b312b5fccd6..462dd8e9b03d5924226f84bf74e639e2dd1d53c8 100644 (file)
@@ -16,9 +16,9 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/set_memory.h>
+#include <linux/cc_platform.h>
 
 #include <asm/hypervisor.h>
-#include <asm/mem_encrypt.h>
 #include <asm/x86_init.h>
 #include <asm/kvmclock.h>
 
@@ -223,7 +223,7 @@ static void __init kvmclock_init_mem(void)
         * hvclock is shared between the guest and the hypervisor, must
         * be mapped decrypted.
         */
-       if (sev_active()) {
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
                r = set_memory_decrypted((unsigned long) hvclock_mem,
                                         1UL << order);
                if (r) {
index 7040c0fa921cdd16b095e9f4f4605bbc462c551c..f5da4a18070ae1a3f4546bc10de76af6cccaa23d 100644 (file)
@@ -167,7 +167,7 @@ static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
        }
        pte = pte_offset_kernel(pmd, vaddr);
 
-       if (sev_active())
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                prot = PAGE_KERNEL_EXEC;
 
        set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
@@ -207,7 +207,7 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
        level4p = (pgd_t *)__va(start_pgtable);
        clear_page(level4p);
 
-       if (sev_active()) {
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
                info.page_flag   |= _PAGE_ENC;
                info.kernpg_flag |= _PAGE_ENC;
        }
index 989685098b3ea7d62f251bd3b1ac39de7e2391c6..aa482827455797526b40816122cd93d2251db49d 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/pagemap.h>
 #include <linux/swap.h>
 #include <linux/rwsem.h>
+#include <linux/cc_platform.h>
 
 #include <asm/apic.h>
 #include <asm/perf_event.h>
@@ -455,7 +456,7 @@ static int has_svm(void)
                return 0;
        }
 
-       if (sev_active()) {
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
                pr_info("KVM is unsupported when running as an SEV guest\n");
                return 0;
        }
index a7250fa3d45fdf09230a6db5474989748f709a92..b59a5cbc6bc5c67987b60ea75acd88fb6da2b02f 100644 (file)
@@ -92,7 +92,7 @@ static unsigned int __ioremap_check_ram(struct resource *res)
  */
 static unsigned int __ioremap_check_encrypted(struct resource *res)
 {
-       if (!sev_active())
+       if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                return 0;
 
        switch (res->desc) {
@@ -112,7 +112,7 @@ static unsigned int __ioremap_check_encrypted(struct resource *res)
  */
 static void __ioremap_check_other(resource_size_t addr, struct ioremap_desc *desc)
 {
-       if (!sev_active())
+       if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                return;
 
        if (!IS_ENABLED(CONFIG_EFI))
@@ -556,7 +556,7 @@ static bool memremap_should_map_decrypted(resource_size_t phys_addr,
        case E820_TYPE_NVS:
        case E820_TYPE_UNUSABLE:
                /* For SEV, these areas are encrypted */
-               if (sev_active())
+               if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                        break;
                fallthrough;
 
index 2163485a74e1dc678a72d52d2cba9b3193f630c7..932007a6913b61f24ca549114e267694e9e5c771 100644 (file)
@@ -194,7 +194,7 @@ void __init sme_early_init(void)
        for (i = 0; i < ARRAY_SIZE(protection_map); i++)
                protection_map[i] = pgprot_encrypted(protection_map[i]);
 
-       if (sev_active())
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                swiotlb_force = SWIOTLB_FORCE;
 }
 
@@ -203,7 +203,7 @@ void __init sev_setup_arch(void)
        phys_addr_t total_mem = memblock_phys_mem_size();
        unsigned long size;
 
-       if (!sev_active())
+       if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                return;
 
        /*
@@ -364,8 +364,8 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
 /*
  * SME and SEV are very similar but they are not the same, so there are
  * times that the kernel will need to distinguish between SME and SEV. The
- * sme_active() and sev_active() functions are used for this.  When a
- * distinction isn't needed, the mem_encrypt_active() function can be used.
+ * cc_platform_has() function is used for this.  When a distinction isn't
+ * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
  *
  * The trampoline code is a good example for this requirement.  Before
  * paging is activated, SME will access all memory as decrypted, but SEV
@@ -373,11 +373,6 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
  * up under SME the trampoline area cannot be encrypted, whereas under SEV
  * the trampoline area must be encrypted.
  */
-bool sev_active(void)
-{
-       return sev_status & MSR_AMD64_SEV_ENABLED;
-}
-EXPORT_SYMBOL_GPL(sev_active);
 
 /* Needs to be called from non-instrumentable code */
 bool noinstr sev_es_active(void)
@@ -391,7 +386,7 @@ bool force_dma_unencrypted(struct device *dev)
        /*
         * For SEV, all DMA must be to unencrypted addresses.
         */
-       if (sev_active())
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                return true;
 
        /*
@@ -450,7 +445,7 @@ static void print_mem_encrypt_feature_info(void)
        }
 
        /* Secure Encrypted Virtualization */
-       if (sev_active())
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                pr_cont(" SEV");
 
        /* Encrypted Register State */
@@ -473,7 +468,7 @@ void __init mem_encrypt_init(void)
         * With SEV, we need to unroll the rep string I/O instructions,
         * but SEV-ES supports them through the #VC handler.
         */
-       if (sev_active() && !sev_es_active())
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) && !sev_es_active())
                static_branch_enable(&sev_enable_key);
 
        print_mem_encrypt_feature_info();
@@ -481,6 +476,6 @@ void __init mem_encrypt_init(void)
 
 int arch_has_restricted_virtio_memory_access(void)
 {
-       return sev_active();
+       return cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT);
 }
 EXPORT_SYMBOL_GPL(arch_has_restricted_virtio_memory_access);
index 7515e78ef89832fad802bc475249a672dbc79e98..1f3675453a57ab61bbae2f1074135246c8559fa0 100644 (file)
@@ -33,7 +33,7 @@
 #include <linux/reboot.h>
 #include <linux/slab.h>
 #include <linux/ucs2_string.h>
-#include <linux/mem_encrypt.h>
+#include <linux/cc_platform.h>
 #include <linux/sched/task.h>
 
 #include <asm/setup.h>
@@ -284,7 +284,8 @@ static void __init __map_region(efi_memory_desc_t *md, u64 va)
        if (!(md->attribute & EFI_MEMORY_WB))
                flags |= _PAGE_PCD;
 
-       if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) &&
+           md->type != EFI_MEMORY_MAPPED_IO)
                flags |= _PAGE_ENC;
 
        pfn = md->phys_addr >> PAGE_SHIFT;
@@ -390,7 +391,7 @@ static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *m
        if (!(md->attribute & EFI_MEMORY_RO))
                pf |= _PAGE_RW;
 
-       if (sev_active())
+       if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                pf |= _PAGE_ENC;
 
        return efi_update_mappings(md, pf);
@@ -438,7 +439,7 @@ void __init efi_runtime_update_mappings(void)
                        (md->type != EFI_RUNTIME_SERVICES_CODE))
                        pf |= _PAGE_RW;
 
-               if (sev_active())
+               if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                        pf |= _PAGE_ENC;
 
                efi_update_mappings(md, pf);