Skip to contentSkip to conversation

x86/sev: Report MSR_AMD64_SEV in SYSFS

PATCH In review Jörg Rödel 3 participants 7 messages Aug 3 – Sep 11 x86

lore
Share & tools
lore
#0 lore raw

v2 cover x86/sev: Report MSR_AMD64_SEV in SYSFS

From: Joerg Roedel <[email protected]>

Hi,

Here is an updated version of my original patch to report MSR_AMD64_SEV in SYSFS. The intention is to give user-space tooling a way to access the MSR value without needing to expose all MSRs via the msr.ko module.

The patches here are updated to address a Sashiko comment asking why SEV_STATUS is only reported for SEV-SNP guests. The sev_status file is now created for SEV and SEV-ES guests as well.

Please review.

Thanks,

Joerg

Joerg Roedel (3):
x86/coco: Add CC_ATTR_GUEST_SEV
x86/sev: Create /sys/devices/system/cpu/sev/ for all SEV guests x86/coco/sev: Make SEV_STATUS available via SYSFS

 .../ABI/testing/sysfs-devices-system-cpu      |  9 +++--
 arch/x86/coco/core.c                          |  3 ++
 arch/x86/coco/sev/core.c                      | 33 ++++++++++++++++---
 include/linux/cc_platform.h                   |  9 +++++
 4 files changed, 47 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/3 x86/coco: Add CC_ATTR_GUEST_SEV

From: Joerg Roedel <[email protected]>

Add a new attribute to the CC platform code to check whether the kernel runs in an environment using SEV memory encryption. This will be used to establish the an sev/ directory in SYSFS (which is currently SEV-SNP-only).

---
 arch/x86/coco/core.c        | 3 +++
 include/linux/cc_platform.h | 9 +++++++++
 2 files changed, 12 insertions(+)
arch/x86/coco/core.c +3 -0
index 989ca9f72ba3..63c06c2ac409 100644
@@ -95,6 +95,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 		return (sev_status & MSR_AMD64_SEV_ENABLED) &&
 			!(sev_status & MSR_AMD64_SEV_ES_ENABLED);
 
+	case CC_ATTR_GUEST_SEV:
+		return sev_status & MSR_AMD64_SEV_ENABLED;
+
 	case CC_ATTR_GUEST_SEV_SNP:
 		return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
 
include/linux/cc_platform.h +9 -0
index 559353ad64ac..f87ee0652b04 100644
@@ -73,6 +73,15 @@ enum cc_attr {
 	 */
 	CC_ATTR_GUEST_UNROLL_STRING_IO,
 
+	/**
+	 * @CC_ATTR_GUEST_SEV: Guest SEV is active.
+	 *
+	 * The platform/OS is running as a virtual machine guest and actively
+	 * using AMD SEV memory encryption. This is true for SEV, SEV-ES, and
+	 * SEV-SNP guests.
+	 */
+	CC_ATTR_GUEST_SEV,
+
 	/**
 	 * @CC_ATTR_GUEST_SEV_SNP: Guest SNP is active.
 	 *
Signature · 1 line
2.53.0
#5 lore raw

PATCH v2 2/3 x86/sev: Create /sys/devices/system/cpu/sev/ for all SEV guests

In reply to Jörg Rödel

From: Joerg Roedel <[email protected]>

The SYSFS directory for SEV is only created for SEV-SNP guests right now. Change it to create it for SEV and SEV-ES guests as well so attributes available on all versions of SEV have a place.

---
 .../ABI/testing/sysfs-devices-system-cpu      |  5 ++--
 arch/x86/coco/sev/core.c                      | 29 ++++++++++++-------
 2 files changed, 22 insertions(+), 12 deletions(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +3 -2
index 82d10d556cc8..63586e299aa8 100644
@@ -694,10 +694,11 @@ Date:		May 2024
 Contact:	Linux kernel mailing list <[email protected]>
 Description:	Secure Encrypted Virtualization (SEV) information
 
-		This directory is only present when running as an SEV-SNP guest.
+		This directory is only present when running as an SEV guest.
 
 		vmpl: Reports the Virtual Machine Privilege Level (VMPL) at which
-		      the SEV-SNP guest is running.
+		      the SEV-SNP guest is running. This file is only present
+		      when running as an SEV-SNP guest.
 
 
 What:		/sys/devices/system/cpu/svm
arch/x86/coco/sev/core.c +19 -10
index ecd77d3217f3..5770b99feb9b 100644
@@ -1433,14 +1433,7 @@ static ssize_t vmpl_show(struct kobject *kobj,
 
 static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl);
 
-static struct attribute *vmpl_attrs[] = {
-	&vmpl_attr.attr,
-	NULL
-};
-
-static struct attribute_group sev_attr_group = {
-	.attrs = vmpl_attrs,
-};
+static struct attribute_group sev_attr_group = {};
 
 static int __init sev_sysfs_init(void)
 {
@@ -1448,7 +1441,7 @@ static int __init sev_sysfs_init(void)
 	struct device *dev_root;
 	int ret;
 
-	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+	if (!cc_platform_has(CC_ATTR_GUEST_SEV))
 		return -ENODEV;
 
 	dev_root = bus_get_dev_root(&cpu_subsys);
@@ -1463,9 +1456,25 @@ static int __init sev_sysfs_init(void)
 
 	ret = sysfs_create_group(sev_kobj, &sev_attr_group);
 	if (ret)
-		kobject_put(sev_kobj);
+		goto drop_kobj;
+
+	/* Add SEV-SNP specific attributes */
+	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
+		/* VMPL */
+		ret = sysfs_add_file_to_group(sev_kobj, &vmpl_attr.attr, NULL);
+		if (ret)
+			goto drop_sysfs;
+	}
+
+	return 0;
+
+drop_sysfs:
+	sysfs_remove_group(sev_kobj, &sev_attr_group);
+drop_kobj:
+	kobject_put(sev_kobj);
 
 	return ret;
+
 }
 arch_initcall(sev_sysfs_init);
 #endif // CONFIG_SYSFS
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 3/3 x86/coco/sev: Make SEV_STATUS available via SYSFS

In reply to Jörg Rödel

From: Joerg Roedel <[email protected]>

Current user-space tooling which needs access to the SEV_STATUS MSR is using the MSR module. The use of this module poses a security risk in any trusted execution environment and is generally discouraged.

Instead, provide an file in SYSFS in the /sys/devices/system/cpu/sev/ directory to provide the value of the SEV_STATUS MSR to user-space.

---
 .../ABI/testing/sysfs-devices-system-cpu         |  4 ++++
 arch/x86/coco/sev/core.c                         | 16 +++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +4 -0
index 63586e299aa8..cec413a71491 100644
@@ -690,6 +690,7 @@ Description:	Umwait control
 
 What:		/sys/devices/system/cpu/sev
 		/sys/devices/system/cpu/sev/vmpl
+		/sys/devices/system/cpu/sev/sev_status
 Date:		May 2024
 Contact:	Linux kernel mailing list <[email protected]>
 Description:	Secure Encrypted Virtualization (SEV) information
@@ -700,6 +701,9 @@ Description:	Secure Encrypted Virtualization (SEV) information
 		      the SEV-SNP guest is running. This file is only present
 		      when running as an SEV-SNP guest.
 
+		sev_status: Reports the value of the SEV_STATUS MSR which
+			    enumerates the enabled features of an SEV
+			    environment.
 
 What:		/sys/devices/system/cpu/svm
 Date:		August 2019
arch/x86/coco/sev/core.c +15 -1
index 5770b99feb9b..4532ffc27041 100644
@@ -1431,9 +1431,23 @@ static ssize_t vmpl_show(struct kobject *kobj,
 	return sysfs_emit(buf, "%d\n", snp_vmpl);
 }
 
+static ssize_t sev_status_show(struct kobject *kobj,
+			       struct kobj_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "0x%llx\n", sev_status);
+}
+
 static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl);
+static struct kobj_attribute sev_status_attr = __ATTR_RO(sev_status);
 
-static struct attribute_group sev_attr_group = {};
+static struct attribute *sev_status_attrs[] = {
+	&sev_status_attr.attr,
+	NULL
+};
+
+static struct attribute_group sev_attr_group = {
+	.attrs = sev_status_attrs,
+};
 
 static int __init sev_sysfs_init(void)
 {
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH 1/3] x86/coco: Add CC_ATTR_GUEST_SEV

In reply to Jörg Rödel
Quoted 6 lines · Jörg Rödel

From: Joerg Roedel <[email protected]>

Add a new attribute to the CC platform code to check whether the kernel runs in an environment using SEV memory encryption. This will be used to establish the an sev/ directory in SYSFS (which is currently SEV-SNP-only).

Yeah, the initial intention for cc_plaform_has() is to use it in arch-agnostic code and for something which you're going to use in coco code itself, you're better off simply doing:

if (sev_status & MSR_AMD64_SEV_ENABLED)

I can fix that up when applying if you prefer - it is easy enough.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#3 lore raw

Re: [PATCH 1/3] x86/coco: Add CC_ATTR_GUEST_SEV

Quoted 8 lines · Borislav Petkov
Quoted 6 lines · Jörg Rödel

From: Joerg Roedel <[email protected]>

Add a new attribute to the CC platform code to check whether the kernel runs in an environment using SEV memory encryption. This will be used to establish the an sev/ directory in SYSFS (which is currently SEV-SNP-only).

Quoted 7 lines

Yeah, the initial intention for cc_plaform_has() is to use it in arch-agnostic code and for something which you're going to use in coco code itself, you're better off simply doing:

if (sev_status & MSR_AMD64_SEV_ENABLED)

I can fix that up when applying if you prefer - it is easy enough.

IOW, I merged them all three into this simple one.

Author: Joerg Roedel <[email protected]>
Date:   Mon Aug 3 14:58:25 2026 +0200

x86/sev: Report MSR_AMD64_SEV in sysfs

Add SEV_STATUS to the cpu/sev/ sysfs hierarchy and report it for all SEV guest types.

[ bp: Merge into a single patch, simplify. ]

Documentation/ABI/testing/sysfs-devices-system-cpu +7 -2
index 82d10d556cc8..cec413a71491 100644
@@ -690,15 +690,20 @@ Description:	Umwait control
 
 What:		/sys/devices/system/cpu/sev
 		/sys/devices/system/cpu/sev/vmpl
+		/sys/devices/system/cpu/sev/sev_status
 Date:		May 2024
 Contact:	Linux kernel mailing list <[email protected]>
 Description:	Secure Encrypted Virtualization (SEV) information
 
-		This directory is only present when running as an SEV-SNP guest.
+		This directory is only present when running as an SEV guest.
 
 		vmpl: Reports the Virtual Machine Privilege Level (VMPL) at which
-		      the SEV-SNP guest is running.
+		      the SEV-SNP guest is running. This file is only present
+		      when running as an SEV-SNP guest.
 
+		sev_status: Reports the value of the SEV_STATUS MSR which
+			    enumerates the enabled features of an SEV
+			    environment.
 
 What:		/sys/devices/system/cpu/svm
 Date:		August 2019
arch/x86/coco/sev/core.c +25 -5
index ec8fc02bd636..4751bfbe694f 100644
@@ -1431,15 +1431,22 @@ static ssize_t vmpl_show(struct kobject *kobj,
 	return sysfs_emit(buf, "%d\n", snp_vmpl);
 }
 
+static ssize_t sev_status_show(struct kobject *kobj,
+			       struct kobj_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "0x%llx\n", sev_status);
+}
+
 static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl);
+static struct kobj_attribute sev_status_attr = __ATTR_RO(sev_status);
 
-static struct attribute *vmpl_attrs[] = {
-	&vmpl_attr.attr,
+static struct attribute *sev_status_attrs[] = {
+	&sev_status_attr.attr,
 	NULL
 };
 
 static struct attribute_group sev_attr_group = {
-	.attrs = vmpl_attrs,
+	.attrs = sev_status_attrs,
 };
 
 static int __init sev_sysfs_init(void)
@@ -1448,7 +1455,7 @@ static int __init sev_sysfs_init(void)
 	struct device *dev_root;
 	int ret;
 
-	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+	if (!(sev_status & MSR_AMD64_SEV_ENABLED))
 		return -ENODEV;
 
 	dev_root = bus_get_dev_root(&cpu_subsys);
@@ -1463,7 +1470,20 @@ static int __init sev_sysfs_init(void)
 
 	ret = sysfs_create_group(sev_kobj, &sev_attr_group);
 	if (ret)
-		kobject_put(sev_kobj);
+		goto drop_kobj;
+
+	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
+		ret = sysfs_add_file_to_group(sev_kobj, &vmpl_attr.attr, NULL);
+		if (ret)
+			goto drop_sysfs;
+	}
+
+	return 0;
+
+drop_sysfs:
+	sysfs_remove_group(sev_kobj, &sev_attr_group);
+drop_kobj:
+	kobject_put(sev_kobj);
 
 	return ret;
 }
Quoted 6 lines
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Reviewed-by
#4 lore raw

Re: [PATCH 1/3] x86/coco: Add CC_ATTR_GUEST_SEV

Quoted 24 lines · Borislav Petkov
Quoted 8 lines · Borislav Petkov
Quoted 6 lines · Jörg Rödel

From: Joerg Roedel <[email protected]>

Add a new attribute to the CC platform code to check whether the kernel runs in an environment using SEV memory encryption. This will be used to establish the an sev/ directory in SYSFS (which is currently SEV-SNP-only).

Quoted 7 lines

Yeah, the initial intention for cc_plaform_has() is to use it in arch-agnostic code and for something which you're going to use in coco code itself, you're better off simply doing:

if (sev_status & MSR_AMD64_SEV_ENABLED)

I can fix that up when applying if you prefer - it is easy enough.

IOW, I merged them all three into this simple one.

Author: Joerg Roedel <[email protected]>
Date:   Mon Aug 3 14:58:25 2026 +0200

x86/sev: Report MSR_AMD64_SEV in sysfs

Quoted 8 lines

Add SEV_STATUS to the cpu/sev/ sysfs hierarchy and report it for all SEV guest types.

[ bp: Merge into a single patch, simplify. ]

Minor nit below, up to you.

Documentation/ABI/testing/sysfs-devices-system-cpu Description: Umwait control · line 693

Quoted 10 lines
Documentation/ABI/testing/sysfs-devices-system-cpu +1 -0
index 82d10d556cc8..cec413a71491 100644
@@ -690,15 +690,20 @@ Description:	Umwait control
 
 What:		/sys/devices/system/cpu/sev
 		/sys/devices/system/cpu/sev/vmpl
+		/sys/devices/system/cpu/sev/sev_status

See below...

Quoted 5 lines
Documentation/ABI/testing/sysfs-devices-system-cpu +0 -1
 Date:		May 2024
 Contact:	Linux kernel mailing list <[email protected]>
 Description:	Secure Encrypted Virtualization (SEV) information
 
-		This directory is only present when running as an SEV-SNP guest.

Documentation/ABI/testing/sysfs-devices-system-cpu Description: Umwait control · line 706

Quoted 10 lines
Documentation/ABI/testing/sysfs-devices-system-cpu +6 -1
+		This directory is only present when running as an SEV guest.
 
 		vmpl: Reports the Virtual Machine Privilege Level (VMPL) at which
-		      the SEV-SNP guest is running.
+		      the SEV-SNP guest is running. This file is only present
+		      when running as an SEV-SNP guest.
 
+		sev_status: Reports the value of the SEV_STATUS MSR which
+			    enumerates the enabled features of an SEV
+			    environment.

Maybe try to keep in alphabetical order and move above the vmpl entry?

Thanks,
Tom

Quoted 72 lines
Documentation/ABI/testing/sysfs-devices-system-cpu +0 -0
 What:		/sys/devices/system/cpu/svm
 Date:		August 2019
arch/x86/coco/sev/core.c +25 -5
index ec8fc02bd636..4751bfbe694f 100644
@@ -1431,15 +1431,22 @@ static ssize_t vmpl_show(struct kobject *kobj,
 	return sysfs_emit(buf, "%d\n", snp_vmpl);
 }
 
+static ssize_t sev_status_show(struct kobject *kobj,
+			       struct kobj_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "0x%llx\n", sev_status);
+}
+
 static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl);
+static struct kobj_attribute sev_status_attr = __ATTR_RO(sev_status);
 
-static struct attribute *vmpl_attrs[] = {
-	&vmpl_attr.attr,
+static struct attribute *sev_status_attrs[] = {
+	&sev_status_attr.attr,
 	NULL
 };
 
 static struct attribute_group sev_attr_group = {
-	.attrs = vmpl_attrs,
+	.attrs = sev_status_attrs,
 };
 
 static int __init sev_sysfs_init(void)
@@ -1448,7 +1455,7 @@ static int __init sev_sysfs_init(void)
 	struct device *dev_root;
 	int ret;
 
-	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
+	if (!(sev_status & MSR_AMD64_SEV_ENABLED))
 		return -ENODEV;
 
 	dev_root = bus_get_dev_root(&cpu_subsys);
@@ -1463,7 +1470,20 @@ static int __init sev_sysfs_init(void)
 
 	ret = sysfs_create_group(sev_kobj, &sev_attr_group);
 	if (ret)
-		kobject_put(sev_kobj);
+		goto drop_kobj;
+
+	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
+		ret = sysfs_add_file_to_group(sev_kobj, &vmpl_attr.attr, NULL);
+		if (ret)
+			goto drop_sysfs;
+	}
+
+	return 0;
+
+drop_sysfs:
+	sysfs_remove_group(sev_kobj, &sev_attr_group);
+drop_kobj:
+	kobject_put(sev_kobj);
 
 	return ret;
 }
Quoted 7 lines
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette