Skip to content

Commit 32cb4d0

Browse files
tlendackysuryasaimadhu
authored andcommitted
x86/sme: Replace occurrences of sme_active() with cc_platform_has()
Replace uses of sme_active() with the more generic cc_platform_has() using CC_ATTR_HOST_MEM_ENCRYPT. If future support is added for other memory encryption technologies, the use of CC_ATTR_HOST_MEM_ENCRYPT can be updated, as required. This also replaces two usages of sev_active() that are really geared towards detecting if SME is active. Signed-off-by: Tom Lendacky <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent bfebd37 commit 32cb4d0

File tree

10 files changed

+36
-34
lines changed

10 files changed

+36
-34
lines changed

arch/x86/include/asm/kexec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ relocate_kernel(unsigned long indirection_page,
129129
unsigned long page_list,
130130
unsigned long start_address,
131131
unsigned int preserve_context,
132-
unsigned int sme_active);
132+
unsigned int host_mem_enc_active);
133133
#endif
134134

135135
#define ARCH_HAS_KIMAGE_ARCH

arch/x86/include/asm/mem_encrypt.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ void __init mem_encrypt_free_decrypted_mem(void);
5151
void __init mem_encrypt_init(void);
5252

5353
void __init sev_es_init_vc_handling(void);
54-
bool sme_active(void);
5554
bool sev_active(void);
5655
bool sev_es_active(void);
5756

@@ -76,7 +75,6 @@ static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }
7675
static inline void __init sme_enable(struct boot_params *bp) { }
7776

7877
static inline void sev_es_init_vc_handling(void) { }
79-
static inline bool sme_active(void) { return false; }
8078
static inline bool sev_active(void) { return false; }
8179
static inline bool sev_es_active(void) { return false; }
8280

arch/x86/kernel/machine_kexec_64.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/suspend.h>
1818
#include <linux/vmalloc.h>
1919
#include <linux/efi.h>
20+
#include <linux/cc_platform.h>
2021

2122
#include <asm/init.h>
2223
#include <asm/tlbflush.h>
@@ -358,7 +359,7 @@ void machine_kexec(struct kimage *image)
358359
(unsigned long)page_list,
359360
image->start,
360361
image->preserve_context,
361-
sme_active());
362+
cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT));
362363

363364
#ifdef CONFIG_KEXEC_JUMP
364365
if (image->preserve_context)
@@ -569,25 +570,25 @@ void arch_kexec_unprotect_crashkres(void)
569570
*/
570571
int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
571572
{
572-
if (sev_active())
573+
if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
573574
return 0;
574575

575576
/*
576-
* If SME is active we need to be sure that kexec pages are
577-
* not encrypted because when we boot to the new kernel the
577+
* If host memory encryption is active we need to be sure that kexec
578+
* pages are not encrypted because when we boot to the new kernel the
578579
* pages won't be accessed encrypted (initially).
579580
*/
580581
return set_memory_decrypted((unsigned long)vaddr, pages);
581582
}
582583

583584
void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
584585
{
585-
if (sev_active())
586+
if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
586587
return;
587588

588589
/*
589-
* If SME is active we need to reset the pages back to being
590-
* an encrypted mapping before freeing them.
590+
* If host memory encryption is active we need to reset the pages back
591+
* to being an encrypted mapping before freeing them.
591592
*/
592593
set_memory_encrypted((unsigned long)vaddr, pages);
593594
}

arch/x86/kernel/pci-swiotlb.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/swiotlb.h>
77
#include <linux/memblock.h>
88
#include <linux/dma-direct.h>
9-
#include <linux/mem_encrypt.h>
9+
#include <linux/cc_platform.h>
1010

1111
#include <asm/iommu.h>
1212
#include <asm/swiotlb.h>
@@ -45,11 +45,10 @@ int __init pci_swiotlb_detect_4gb(void)
4545
swiotlb = 1;
4646

4747
/*
48-
* If SME is active then swiotlb will be set to 1 so that bounce
49-
* buffers are allocated and used for devices that do not support
50-
* the addressing range required for the encryption mask.
48+
* Set swiotlb to 1 so that bounce buffers are allocated and used for
49+
* devices that can't support DMA to encrypted memory.
5150
*/
52-
if (sme_active())
51+
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
5352
swiotlb = 1;
5453

5554
return swiotlb;

arch/x86/kernel/relocate_kernel_64.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ SYM_CODE_START_NOALIGN(relocate_kernel)
4747
* %rsi page_list
4848
* %rdx start address
4949
* %rcx preserve_context
50-
* %r8 sme_active
50+
* %r8 host_mem_enc_active
5151
*/
5252

5353
/* Save the CPU context, used for jumping back */

arch/x86/mm/ioremap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <linux/slab.h>
1515
#include <linux/vmalloc.h>
1616
#include <linux/mmiotrace.h>
17-
#include <linux/mem_encrypt.h>
17+
#include <linux/cc_platform.h>
1818
#include <linux/efi.h>
1919
#include <linux/pgtable.h>
2020

@@ -703,7 +703,7 @@ bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size,
703703
if (flags & MEMREMAP_DEC)
704704
return false;
705705

706-
if (sme_active()) {
706+
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
707707
if (memremap_is_setup_data(phys_addr, size) ||
708708
memremap_is_efi_data(phys_addr, size))
709709
return false;
@@ -729,7 +729,7 @@ pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr,
729729

730730
encrypted_prot = true;
731731

732-
if (sme_active()) {
732+
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
733733
if (early_memremap_is_setup_data(phys_addr, size) ||
734734
memremap_is_efi_data(phys_addr, size))
735735
encrypted_prot = false;

arch/x86/mm/mem_encrypt.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void __init sme_unmap_bootdata(char *real_mode_data)
144144
struct boot_params *boot_data;
145145
unsigned long cmdline_paddr;
146146

147-
if (!sme_active())
147+
if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
148148
return;
149149

150150
/* Get the command line address before unmapping the real_mode_data */
@@ -164,7 +164,7 @@ void __init sme_map_bootdata(char *real_mode_data)
164164
struct boot_params *boot_data;
165165
unsigned long cmdline_paddr;
166166

167-
if (!sme_active())
167+
if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
168168
return;
169169

170170
__sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
@@ -377,11 +377,6 @@ bool sev_active(void)
377377
{
378378
return sev_status & MSR_AMD64_SEV_ENABLED;
379379
}
380-
381-
bool sme_active(void)
382-
{
383-
return sme_me_mask && !sev_active();
384-
}
385380
EXPORT_SYMBOL_GPL(sev_active);
386381

387382
/* Needs to be called from non-instrumentable code */
@@ -404,7 +399,7 @@ bool force_dma_unencrypted(struct device *dev)
404399
* device does not support DMA to addresses that include the
405400
* encryption mask.
406401
*/
407-
if (sme_active()) {
402+
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
408403
u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
409404
u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
410405
dev->bus_dma_limit);
@@ -445,7 +440,7 @@ static void print_mem_encrypt_feature_info(void)
445440
pr_info("AMD Memory Encryption Features active:");
446441

447442
/* Secure Memory Encryption */
448-
if (sme_active()) {
443+
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
449444
/*
450445
* SME is mutually exclusive with any of the SEV
451446
* features below.

arch/x86/mm/mem_encrypt_identity.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/kernel.h>
3131
#include <linux/mm.h>
3232
#include <linux/mem_encrypt.h>
33+
#include <linux/cc_platform.h>
3334

3435
#include <asm/setup.h>
3536
#include <asm/sections.h>
@@ -287,7 +288,13 @@ void __init sme_encrypt_kernel(struct boot_params *bp)
287288
unsigned long pgtable_area_len;
288289
unsigned long decrypted_base;
289290

290-
if (!sme_active())
291+
/*
292+
* This is early code, use an open coded check for SME instead of
293+
* using cc_platform_has(). This eliminates worries about removing
294+
* instrumentation or checking boot_cpu_data in the cc_platform_has()
295+
* function.
296+
*/
297+
if (!sme_get_me_mask() || sev_status & MSR_AMD64_SEV_ENABLED)
291298
return;
292299

293300
/*

arch/x86/realmode/init.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <linux/slab.h>
44
#include <linux/memblock.h>
55
#include <linux/mem_encrypt.h>
6+
#include <linux/cc_platform.h>
67
#include <linux/pgtable.h>
78

89
#include <asm/set_memory.h>
@@ -44,7 +45,7 @@ void __init reserve_real_mode(void)
4445
static void sme_sev_setup_real_mode(struct trampoline_header *th)
4546
{
4647
#ifdef CONFIG_AMD_MEM_ENCRYPT
47-
if (sme_active())
48+
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
4849
th->flags |= TH_FLAGS_SME_ACTIVE;
4950

5051
if (sev_es_active()) {
@@ -81,7 +82,7 @@ static void __init setup_real_mode(void)
8182
* decrypted memory in order to bring up other processors
8283
* successfully. This is not needed for SEV.
8384
*/
84-
if (sme_active())
85+
if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
8586
set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT);
8687

8788
memcpy(base, real_mode_blob, size);

drivers/iommu/amd/init.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <linux/amd-iommu.h>
2121
#include <linux/export.h>
2222
#include <linux/kmemleak.h>
23-
#include <linux/mem_encrypt.h>
23+
#include <linux/cc_platform.h>
2424
#include <asm/pci-direct.h>
2525
#include <asm/iommu.h>
2626
#include <asm/apic.h>
@@ -964,7 +964,7 @@ static bool copy_device_table(void)
964964
pr_err("The address of old device table is above 4G, not trustworthy!\n");
965965
return false;
966966
}
967-
old_devtb = (sme_active() && is_kdump_kernel())
967+
old_devtb = (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && is_kdump_kernel())
968968
? (__force void *)ioremap_encrypted(old_devtb_phys,
969969
dev_table_size)
970970
: memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
@@ -3032,7 +3032,8 @@ static int __init amd_iommu_init(void)
30323032

30333033
static bool amd_iommu_sme_check(void)
30343034
{
3035-
if (!sme_active() || (boot_cpu_data.x86 != 0x17))
3035+
if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
3036+
(boot_cpu_data.x86 != 0x17))
30363037
return true;
30373038

30383039
/* For Fam17h, a specific level of support is required */

0 commit comments

Comments
 (0)