[PATCH 2/2] x86/cpu: clear SME/SEV/SEV_ES features when not in use

From: Mario Limonciello
Date: Fri Feb 11 2022 - 00:40:14 EST


Currently SME/SEV/SEV_ES features are reflective of whether the CPU
supports the features but not whether they have been activated by the
kernel.

Change this around to clear the features if the kernel is not using
them so userspace can determine if they are available and in use
from `/proc/cpuinfo`.

Signed-off-by: Mario Limonciello <mario.limonciello@xxxxxxx>
---
arch/x86/kernel/cpu/amd.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c
index 4edb6f0f628c..4a7d4801fa0b 100644
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -611,12 +611,20 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
if (!(msr & MSR_K7_HWCR_SMMLOCK))
goto clear_sev;

+ if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
+ goto clear_all;
+ if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+ goto clear_sev;
+ if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
+ goto clear_sev_es;
+
return;

clear_all:
setup_clear_cpu_cap(X86_FEATURE_SME);
clear_sev:
setup_clear_cpu_cap(X86_FEATURE_SEV);
+clear_sev_es:
setup_clear_cpu_cap(X86_FEATURE_SEV_ES);
}
}
--
2.34.1