Skip to contentSkip to conversation

x86/mm: Fix memory encryption features advertisement

PATCH Superseded Kirill A. Shutemov 5 participants 8 messages Jan 11, 2024 – Jan 16, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/mm: Fix memory encryption features advertisement

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

To avoid confusion, check the cc_vendor directly.

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>
---
 arch/x86/mm/mem_encrypt.c | 56 +++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
arch/x86/mm/mem_encrypt.c +30 -26
index c290c55b632b..d035bce3a2b0 100644
@@ -42,38 +42,42 @@ bool force_dma_unencrypted(struct device *dev)
 
 static void print_mem_encrypt_feature_info(void)
 {
-	pr_info("Memory Encryption Features active:");
+	pr_info("Memory Encryption Features active: ");
 
-	if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
-		pr_cont(" Intel TDX\n");
-		return;
-	}
+	switch (cc_vendor) {
+	case CC_VENDOR_INTEL:
+		pr_cont("Intel TDX\n");
+		break;
+	case CC_VENDOR_AMD:
+		pr_cont("AMD");
 
-	pr_cont(" AMD");
-
-	/* Secure Memory Encryption */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+		/* Secure Memory Encryption */
+		if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
 		/*
 		 * SME is mutually exclusive with any of the SEV
 		 * features below.
-		 */
-		pr_cont(" SME\n");
-		return;
+		*/
+			pr_cont(" SME\n");
+			return;
+		}
+
+		/* Secure Encrypted Virtualization */
+		if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+			pr_cont(" SEV");
+
+		/* Encrypted Register State */
+		if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
+			pr_cont(" SEV-ES");
+
+		/* Secure Nested Paging */
+		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+			pr_cont(" SEV-SNP");
+
+		pr_cont("\n");
+		break;
+	default:
+		pr_cont("Unknown\n");
 	}
-
-	/* Secure Encrypted Virtualization */
-	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
-		pr_cont(" SEV");
-
-	/* Encrypted Register State */
-	if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
-		pr_cont(" SEV-ES");
-
-	/* Secure Nested Paging */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
-		pr_cont(" SEV-SNP");
-
-	pr_cont("\n");
 }
 
 /* Architecture __weak replacement functions */
Signature · 1 line
2.41.0
#1 lore raw
Quoted 10 lines · Kirill A. Shutemov

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

Quoted 6 lines

To avoid confusion, check the cc_vendor directly.

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

With this change, will it print "Intel TDX" for Hyper-V?

IMO, since there is already a debug message for type identification, we can remove this part.

Quoted 81 lines
Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>
---
 arch/x86/mm/mem_encrypt.c | 56 +++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
arch/x86/mm/mem_encrypt.c +30 -26
index c290c55b632b..d035bce3a2b0 100644
@@ -42,38 +42,42 @@ bool force_dma_unencrypted(struct device *dev)
 
 static void print_mem_encrypt_feature_info(void)
 {
-	pr_info("Memory Encryption Features active:");
+	pr_info("Memory Encryption Features active: ");
 
-	if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
-		pr_cont(" Intel TDX\n");
-		return;
-	}
+	switch (cc_vendor) {
+	case CC_VENDOR_INTEL:
+		pr_cont("Intel TDX\n");
+		break;
+	case CC_VENDOR_AMD:
+		pr_cont("AMD");
 
-	pr_cont(" AMD");
-
-	/* Secure Memory Encryption */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+		/* Secure Memory Encryption */
+		if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
 		/*
 		 * SME is mutually exclusive with any of the SEV
 		 * features below.
-		 */
-		pr_cont(" SME\n");
-		return;
+		*/
+			pr_cont(" SME\n");
+			return;
+		}
+
+		/* Secure Encrypted Virtualization */
+		if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+			pr_cont(" SEV");
+
+		/* Encrypted Register State */
+		if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
+			pr_cont(" SEV-ES");
+
+		/* Secure Nested Paging */
+		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+			pr_cont(" SEV-SNP");
+
+		pr_cont("\n");
+		break;
+	default:
+		pr_cont("Unknown\n");
 	}
-
-	/* Secure Encrypted Virtualization */
-	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
-		pr_cont(" SEV");
-
-	/* Encrypted Register State */
-	if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
-		pr_cont(" SEV-ES");
-
-	/* Secure Nested Paging */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
-		pr_cont(" SEV-SNP");
-
-	pr_cont("\n");
 }
 
 /* Architecture __weak replacement functions */
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Reviewed-by
#2 lore raw
Quoted 13 lines · Kuppuswamy Sathyanarayanan
Quoted 10 lines · Kirill A. Shutemov

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

Quoted 8 lines
Quoted 6 lines

To avoid confusion, check the cc_vendor directly.

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

With this change, will it print "Intel TDX" for Hyper-V?

Yes, I just tested on AMD and Intel and the print is accurate now. Thanks.

Quoted 4 lines

IMO, since there is already a debug message for type identification, we can remove this part.

If that's the only way to get a fix merged then so be it, but I appreciate having the possibility of greping for a single prefix for either vendor that the current code provides.

Quoted 82 lines
Quoted 81 lines
Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>
---
 arch/x86/mm/mem_encrypt.c | 56 +++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
arch/x86/mm/mem_encrypt.c +30 -26
index c290c55b632b..d035bce3a2b0 100644
@@ -42,38 +42,42 @@ bool force_dma_unencrypted(struct device *dev)
 
 static void print_mem_encrypt_feature_info(void)
 {
-	pr_info("Memory Encryption Features active:");
+	pr_info("Memory Encryption Features active: ");
 
-	if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
-		pr_cont(" Intel TDX\n");
-		return;
-	}
+	switch (cc_vendor) {
+	case CC_VENDOR_INTEL:
+		pr_cont("Intel TDX\n");
+		break;
+	case CC_VENDOR_AMD:
+		pr_cont("AMD");
 
-	pr_cont(" AMD");
-
-	/* Secure Memory Encryption */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+		/* Secure Memory Encryption */
+		if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
 		/*
 		 * SME is mutually exclusive with any of the SEV
 		 * features below.
-		 */
-		pr_cont(" SME\n");
-		return;
+		*/
+			pr_cont(" SME\n");
+			return;
+		}
+
+		/* Secure Encrypted Virtualization */
+		if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+			pr_cont(" SEV");
+
+		/* Encrypted Register State */
+		if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
+			pr_cont(" SEV-ES");
+
+		/* Secure Nested Paging */
+		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+			pr_cont(" SEV-SNP");
+
+		pr_cont("\n");
+		break;
+	default:
+		pr_cont("Unknown\n");
 	}
-
-	/* Secure Encrypted Virtualization */
-	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
-		pr_cont(" SEV");
-
-	/* Encrypted Register State */
-	if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
-		pr_cont(" SEV-ES");
-
-	/* Secure Nested Paging */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
-		pr_cont(" SEV-SNP");
-
-	pr_cont("\n");
 }
 
 /* Architecture __weak replacement functions */
#3 lore raw
In reply to Kuppuswamy Sathyanarayanan
Quoted 20 lines · Kuppuswamy Sathyanarayanan
Quoted 10 lines · Kirill A. Shutemov

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

Quoted 6 lines

To avoid confusion, check the cc_vendor directly.

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

Quoted 10 lines

With this change, will it print "Intel TDX" for Hyper-V?

IMO, since there is already a debug message for type identification, we can remove this part.

Quoted 5 lines
Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>

---

Quoted 77 lines
Quoted 76 lines
 arch/x86/mm/mem_encrypt.c | 56 +++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
arch/x86/mm/mem_encrypt.c +30 -26
index c290c55b632b..d035bce3a2b0 100644
@@ -42,38 +42,42 @@ bool force_dma_unencrypted(struct device *dev)
 
 static void print_mem_encrypt_feature_info(void)
 {
-	pr_info("Memory Encryption Features active:");
+	pr_info("Memory Encryption Features active: ");
 
-	if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
-		pr_cont(" Intel TDX\n");
-		return;
-	}
+	switch (cc_vendor) {
+	case CC_VENDOR_INTEL:
+		pr_cont("Intel TDX\n");
+		break;
+	case CC_VENDOR_AMD:
+		pr_cont("AMD");
 
-	pr_cont(" AMD");
-
-	/* Secure Memory Encryption */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+		/* Secure Memory Encryption */
+		if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
 		/*
 		 * SME is mutually exclusive with any of the SEV
 		 * features below.
-		 */
-		pr_cont(" SME\n");
-		return;
+		*/
+			pr_cont(" SME\n");
+			return;
+		}
+
+		/* Secure Encrypted Virtualization */
+		if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+			pr_cont(" SEV");
+
+		/* Encrypted Register State */
+		if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
+			pr_cont(" SEV-ES");
+
+		/* Secure Nested Paging */
+		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+			pr_cont(" SEV-SNP");
+
+		pr_cont("\n");
+		break;
+	default:
+		pr_cont("Unknown\n");
 	}
-
-	/* Secure Encrypted Virtualization */
-	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
-		pr_cont(" SEV");
-
-	/* Encrypted Register State */
-	if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
-		pr_cont(" SEV-ES");
-
-	/* Secure Nested Paging */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
-		pr_cont(" SEV-SNP");
-
-	pr_cont("\n");
 }
 
 /* Architecture __weak replacement functions */
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Acked-by on v2 1/1
#4 lore raw
In reply to Kirill A. Shutemov
Quoted 12 lines · Kirill A. Shutemov

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

To avoid confusion, check the cc_vendor directly.

Quoted 8 lines

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>
Quoted 5 lines
---
  arch/x86/mm/mem_encrypt.c | 56 +++++++++++++++++++++------------------
  1 file changed, 30 insertions(+), 26 deletions(-)
arch/x86/mm/mem_encrypt.c +0 -0
Acked-by on v2 1/1
#5 lore raw
In reply to Kirill A. Shutemov
Quoted 12 lines · Kirill A. Shutemov

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

To avoid confusion, check the cc_vendor directly.

Quoted 8 lines

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>

Seems this fix is for userspace running in hyperv environment being able to use some easy grep to get which coco vendor it is running on?

If so I think it would be nice to mention it too.

Quoted 77 lines
---
 arch/x86/mm/mem_encrypt.c | 56 +++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
arch/x86/mm/mem_encrypt.c +30 -26
index c290c55b632b..d035bce3a2b0 100644
@@ -42,38 +42,42 @@ bool force_dma_unencrypted(struct device *dev)
 
 static void print_mem_encrypt_feature_info(void)
 {
-	pr_info("Memory Encryption Features active:");
+	pr_info("Memory Encryption Features active: ");
 
-	if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
-		pr_cont(" Intel TDX\n");
-		return;
-	}
+	switch (cc_vendor) {
+	case CC_VENDOR_INTEL:
+		pr_cont("Intel TDX\n");
+		break;
+	case CC_VENDOR_AMD:
+		pr_cont("AMD");
 
-	pr_cont(" AMD");
-
-	/* Secure Memory Encryption */
-	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
+		/* Secure Memory Encryption */
+		if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
 		/*
 		 * SME is mutually exclusive with any of the SEV
 		 * features below.
-		 */
-		pr_cont(" SME\n");
-		return;
+		*/
+			pr_cont(" SME\n");
+			return;
+		}
+
+		/* Secure Encrypted Virtualization */
+		if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+			pr_cont(" SEV");
+
+		/* Encrypted Register State */
+		if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
+			pr_cont(" SEV-ES");
+
+		/* Secure Nested Paging */
+		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+			pr_cont(" SEV-SNP");
+
+		pr_cont("\n");
+		break;
+	default:
+		pr_cont("Unknown\n");
 	}
-
-	/* Secure Encrypted Virtualization */
-	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
-		pr_cont(" SEV");
-
-	/* Encrypted Register State */
-	if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
-		pr_cont(" SEV-ES");
-
-	/* Secure Nested Paging */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
-		pr_cont(" SEV-SNP");
-
-	pr_cont("\n");
 }
 
 /* Architecture __weak replacement functions */
#6 lore raw
Quoted 18 lines · Kai
Quoted 17 lines · Kirill A. Shutemov

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

To avoid confusion, check the cc_vendor directly.

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

Quoted 6 lines
Quoted 3 lines
Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>

Seems this fix is for userspace running in hyperv environment being able to use some easy grep to get which coco vendor it is running on?

Making decision in userspace by grepping dmesg is bad idea and nobody
should do this. It can easily give false result: dmesg is not ABI, format can change and ring buffer has finite size, the message can be overridden.

If we need a way for userspace to discover which CoCo environment it runs on, we need proper ABI for that. Maybe sysfs file or something.

Quoted 3 lines

If so I think it would be nice to mention it too.

Thanks.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#7 lore raw
Quoted 26 lines · On Tue, 2024-01-16 at 13:58 +0300, [email protected] wrote:
Quoted 18 lines · Kai
Quoted 17 lines · Kirill A. Shutemov

When memory encryption is enabled, the kernel prints the encryption flavor that the system supports.

The check assumes that everything is AMD SME/SEV if it doesn't have the TDX CPU feature set.

Hyper-V vTOM sets cc_vendor to CC_VENDOR_INTEL when it runs as L2 guest on top of TDX, but not X86_FEATURE_TDX_GUEST. Hyper-V only needs memory encryption enabled for I/O without the rest of CoCo enabling.

To avoid confusion, check the cc_vendor directly.

Possible alternative is to completely removing the print statement. For a regular TDX guest, the kernel already prints a message indicating that it is booting on TDX. Similarly, AMD and Hyper-V can also display a message during their enumeration process.

Quoted 6 lines
Quoted 3 lines
Cc Dexuan Cui <[email protected]>
Cc Jeremi Piotrowski <[email protected]>

Seems this fix is for userspace running in hyperv environment being able to use some easy grep to get which coco vendor it is running on?

Quoted 6 lines

Making decision in userspace by grepping dmesg is bad idea and nobody
should do this. It can easily give false result: dmesg is not ABI, format can change and ring buffer has finite size, the message can be overridden.

If we need a way for userspace to discover which CoCo environment it runs on, we need proper ABI for that. Maybe sysfs file or something.

Yeah agreed. :-)