Skip to content

Commit 4d96f91

Browse files
tlendackysuryasaimadhu
authored andcommitted
x86/sev: Replace occurrences of sev_active() with cc_platform_has()
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 <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 32cb4d0 commit 4d96f91

File tree

9 files changed

+27
-29
lines changed

9 files changed

+27
-29
lines changed

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 sev_active(void);
5554
bool sev_es_active(void);
5655

5756
#define __bss_decrypted __section(".bss..decrypted")
@@ -75,7 +74,6 @@ static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }
7574
static inline void __init sme_enable(struct boot_params *bp) { }
7675

7776
static inline void sev_es_init_vc_handling(void) { }
78-
static inline bool sev_active(void) { return false; }
7977
static inline bool sev_es_active(void) { return false; }
8078

8179
static inline int __init

arch/x86/kernel/crash_dump_64.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/crash_dump.h>
1111
#include <linux/uaccess.h>
1212
#include <linux/io.h>
13+
#include <linux/cc_platform.h>
1314

1415
static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
1516
unsigned long offset, int userbuf,
@@ -73,5 +74,6 @@ ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
7374

7475
ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
7576
{
76-
return read_from_oldmem(buf, count, ppos, 0, sev_active());
77+
return read_from_oldmem(buf, count, ppos, 0,
78+
cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT));
7779
}

arch/x86/kernel/kvm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/nmi.h>
2828
#include <linux/swait.h>
2929
#include <linux/syscore_ops.h>
30+
#include <linux/cc_platform.h>
3031
#include <asm/timer.h>
3132
#include <asm/cpu.h>
3233
#include <asm/traps.h>
@@ -418,7 +419,7 @@ static void __init sev_map_percpu_data(void)
418419
{
419420
int cpu;
420421

421-
if (!sev_active())
422+
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
422423
return;
423424

424425
for_each_possible_cpu(cpu) {

arch/x86/kernel/kvmclock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#include <linux/mm.h>
1717
#include <linux/slab.h>
1818
#include <linux/set_memory.h>
19+
#include <linux/cc_platform.h>
1920

2021
#include <asm/hypervisor.h>
21-
#include <asm/mem_encrypt.h>
2222
#include <asm/x86_init.h>
2323
#include <asm/kvmclock.h>
2424

@@ -223,7 +223,7 @@ static void __init kvmclock_init_mem(void)
223223
* hvclock is shared between the guest and the hypervisor, must
224224
* be mapped decrypted.
225225
*/
226-
if (sev_active()) {
226+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
227227
r = set_memory_decrypted((unsigned long) hvclock_mem,
228228
1UL << order);
229229
if (r) {

arch/x86/kernel/machine_kexec_64.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
167167
}
168168
pte = pte_offset_kernel(pmd, vaddr);
169169

170-
if (sev_active())
170+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
171171
prot = PAGE_KERNEL_EXEC;
172172

173173
set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
@@ -207,7 +207,7 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
207207
level4p = (pgd_t *)__va(start_pgtable);
208208
clear_page(level4p);
209209

210-
if (sev_active()) {
210+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
211211
info.page_flag |= _PAGE_ENC;
212212
info.kernpg_flag |= _PAGE_ENC;
213213
}

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/pagemap.h>
2626
#include <linux/swap.h>
2727
#include <linux/rwsem.h>
28+
#include <linux/cc_platform.h>
2829

2930
#include <asm/apic.h>
3031
#include <asm/perf_event.h>
@@ -455,7 +456,7 @@ static int has_svm(void)
455456
return 0;
456457
}
457458

458-
if (sev_active()) {
459+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
459460
pr_info("KVM is unsupported when running as an SEV guest\n");
460461
return 0;
461462
}

arch/x86/mm/ioremap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static unsigned int __ioremap_check_ram(struct resource *res)
9292
*/
9393
static unsigned int __ioremap_check_encrypted(struct resource *res)
9494
{
95-
if (!sev_active())
95+
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
9696
return 0;
9797

9898
switch (res->desc) {
@@ -112,7 +112,7 @@ static unsigned int __ioremap_check_encrypted(struct resource *res)
112112
*/
113113
static void __ioremap_check_other(resource_size_t addr, struct ioremap_desc *desc)
114114
{
115-
if (!sev_active())
115+
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
116116
return;
117117

118118
if (!IS_ENABLED(CONFIG_EFI))
@@ -556,7 +556,7 @@ static bool memremap_should_map_decrypted(resource_size_t phys_addr,
556556
case E820_TYPE_NVS:
557557
case E820_TYPE_UNUSABLE:
558558
/* For SEV, these areas are encrypted */
559-
if (sev_active())
559+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
560560
break;
561561
fallthrough;
562562

arch/x86/mm/mem_encrypt.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void __init sme_early_init(void)
194194
for (i = 0; i < ARRAY_SIZE(protection_map); i++)
195195
protection_map[i] = pgprot_encrypted(protection_map[i]);
196196

197-
if (sev_active())
197+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
198198
swiotlb_force = SWIOTLB_FORCE;
199199
}
200200

@@ -203,7 +203,7 @@ void __init sev_setup_arch(void)
203203
phys_addr_t total_mem = memblock_phys_mem_size();
204204
unsigned long size;
205205

206-
if (!sev_active())
206+
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
207207
return;
208208

209209
/*
@@ -364,20 +364,15 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
364364
/*
365365
* SME and SEV are very similar but they are not the same, so there are
366366
* times that the kernel will need to distinguish between SME and SEV. The
367-
* sme_active() and sev_active() functions are used for this. When a
368-
* distinction isn't needed, the mem_encrypt_active() function can be used.
367+
* cc_platform_has() function is used for this. When a distinction isn't
368+
* needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
369369
*
370370
* The trampoline code is a good example for this requirement. Before
371371
* paging is activated, SME will access all memory as decrypted, but SEV
372372
* will access all memory as encrypted. So, when APs are being brought
373373
* up under SME the trampoline area cannot be encrypted, whereas under SEV
374374
* the trampoline area must be encrypted.
375375
*/
376-
bool sev_active(void)
377-
{
378-
return sev_status & MSR_AMD64_SEV_ENABLED;
379-
}
380-
EXPORT_SYMBOL_GPL(sev_active);
381376

382377
/* Needs to be called from non-instrumentable code */
383378
bool noinstr sev_es_active(void)
@@ -391,7 +386,7 @@ bool force_dma_unencrypted(struct device *dev)
391386
/*
392387
* For SEV, all DMA must be to unencrypted addresses.
393388
*/
394-
if (sev_active())
389+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
395390
return true;
396391

397392
/*
@@ -450,7 +445,7 @@ static void print_mem_encrypt_feature_info(void)
450445
}
451446

452447
/* Secure Encrypted Virtualization */
453-
if (sev_active())
448+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
454449
pr_cont(" SEV");
455450

456451
/* Encrypted Register State */
@@ -473,14 +468,14 @@ void __init mem_encrypt_init(void)
473468
* With SEV, we need to unroll the rep string I/O instructions,
474469
* but SEV-ES supports them through the #VC handler.
475470
*/
476-
if (sev_active() && !sev_es_active())
471+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) && !sev_es_active())
477472
static_branch_enable(&sev_enable_key);
478473

479474
print_mem_encrypt_feature_info();
480475
}
481476

482477
int arch_has_restricted_virtio_memory_access(void)
483478
{
484-
return sev_active();
479+
return cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT);
485480
}
486481
EXPORT_SYMBOL_GPL(arch_has_restricted_virtio_memory_access);

arch/x86/platform/efi/efi_64.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <linux/reboot.h>
3434
#include <linux/slab.h>
3535
#include <linux/ucs2_string.h>
36-
#include <linux/mem_encrypt.h>
36+
#include <linux/cc_platform.h>
3737
#include <linux/sched/task.h>
3838

3939
#include <asm/setup.h>
@@ -284,7 +284,8 @@ static void __init __map_region(efi_memory_desc_t *md, u64 va)
284284
if (!(md->attribute & EFI_MEMORY_WB))
285285
flags |= _PAGE_PCD;
286286

287-
if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
287+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) &&
288+
md->type != EFI_MEMORY_MAPPED_IO)
288289
flags |= _PAGE_ENC;
289290

290291
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
390391
if (!(md->attribute & EFI_MEMORY_RO))
391392
pf |= _PAGE_RW;
392393

393-
if (sev_active())
394+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
394395
pf |= _PAGE_ENC;
395396

396397
return efi_update_mappings(md, pf);
@@ -438,7 +439,7 @@ void __init efi_runtime_update_mappings(void)
438439
(md->type != EFI_RUNTIME_SERVICES_CODE))
439440
pf |= _PAGE_RW;
440441

441-
if (sev_active())
442+
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
442443
pf |= _PAGE_ENC;
443444

444445
efi_update_mappings(md, pf);

0 commit comments

Comments
 (0)