Skip to contentSkip to conversation

x86/cpu: clear SME features when not in use

PATCH Superseded Mario Limonciello 2 participants 2 messages Feb 15, 2022 x86

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 x86/cpu: clear SME features when not in use

Currently the SME CPU feature flag is 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.

As the feature flag is now cleared on systems that SME isn't active use CPUID 0x8000001f to confirm SME availability before calling native_wbinvd.

---
Changes from v2->v3:
* Drop all changes related to SEV, these will be worked out separately.
* Only clear SME flag rather than all flags if sme_me_mask is not set

 arch/x86/kernel/cpu/amd.c | 3 +++
 arch/x86/kernel/process.c | 5 ++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/amd.c +3 -0
index 4edb6f0f628c..8421b9d9f552 100644
@@ -607,6 +607,9 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
 		if (IS_ENABLED(CONFIG_X86_32))
 			goto clear_all;
 
+		if (!sme_me_mask)
+			setup_clear_cpu_cap(X86_FEATURE_SME);
+
 		rdmsrl(MSR_K7_HWCR, msr);
 		if (!(msr & MSR_K7_HWCR_SMMLOCK))
 			goto clear_sev;
arch/x86/kernel/process.c +4 -1
index 81d8ef036637..e131d71b3cae 100644
@@ -765,8 +765,11 @@ void stop_this_cpu(void *dummy)
 	 * without the encryption bit, they don't race each other when flushed
 	 * and potentially end up with the wrong entry being committed to
 	 * memory.
+	 *
+	 * Test the CPUID bit directly because the machine might've cleared
+	 * X86_FEATURE_SME due to cmdline options.
 	 */
-	if (boot_cpu_has(X86_FEATURE_SME))
+	if (cpuid_eax(0x8000001f) & BIT(0))
 		native_wbinvd();
 	for (;;) {
 		/*
Signature · 1 line
2.34.1
#1 lore raw
Quoted 2 lines · Mario Limonciello

Currently the SME CPU feature flag is reflective of whether the CPU supports the features but not whether they have been activated by the

s/features/feature/
s/they have/it has/

The plural stuff appears left over from the previous patch that was also working on SEV-related features.

Quoted 7 lines

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.

As the feature flag is now cleared on systems that SME isn't active

s/that SME isn't active/where SME isn't active,/

Quoted 18 lines

use CPUID 0x8000001f to confirm SME availability before calling native_wbinvd.

---
Changes from v2->v3:
* Drop all changes related to SEV, these will be worked out separately.
* Only clear SME flag rather than all flags if sme_me_mask is not set

  arch/x86/kernel/cpu/amd.c | 3 +++
  arch/x86/kernel/process.c | 5 ++++-
  2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/amd.c +0 -0
index 4edb6f0f628c..8421b9d9f552 100644

arch/x86/kernel/cpu/amd.c static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) · line 611

Quoted 5 lines
arch/x86/kernel/cpu/amd.c +2 -0
@@ -607,6 +607,9 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
  		if (IS_ENABLED(CONFIG_X86_32))
  			goto clear_all;
 
+		if (!sme_me_mask)
+			setup_clear_cpu_cap(X86_FEATURE_SME);

The block comment above the "if (cpu_has(c, X86_FEATURE_SME) ..." should be updated to match, too.

Thanks,
Tom

Quoted 21 lines
arch/x86/kernel/cpu/amd.c +1 -0
+
  		rdmsrl(MSR_K7_HWCR, msr);
  		if (!(msr & MSR_K7_HWCR_SMMLOCK))
  			goto clear_sev;
arch/x86/kernel/process.c +4 -1
index 81d8ef036637..e131d71b3cae 100644
@@ -765,8 +765,11 @@ void stop_this_cpu(void *dummy)
  	 * without the encryption bit, they don't race each other when flushed
  	 * and potentially end up with the wrong entry being committed to
  	 * memory.
+	 *
+	 * Test the CPUID bit directly because the machine might've cleared
+	 * X86_FEATURE_SME due to cmdline options.
  	 */
-	if (boot_cpu_has(X86_FEATURE_SME))
+	if (cpuid_eax(0x8000001f) & BIT(0))
  		native_wbinvd();
  	for (;;) {
  		/*