Skip to contentSkip to conversation

sysfs/cpu: Implement generic vulnerabilites directory

PATCH Superseded Thomas Gleixner 2 participants 5 messages Jan 7, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover sysfs/cpu: Implement generic vulnerabilites directory

The meltdown/spectre vulnerabilities affect several architectures and people are asking for a common way to figure out whether a system is affected or not.

Create

/sys/devices/system/cpu/vulnerabilites

and the files

/sys/devices/system/cpu/vulnerabilites/meltdown /sys/devices/system/cpu/vulnerabilites/spectre_v1 /sys/devices/system/cpu/vulnerabilites/spectre_v2

Add the x86 implementation which shows:

meltdown    Mitigation: PTI
spectre_v1  Vulnerable
sepctre_v1  Vulnerable

On an AMD CPU the output of meltdown is: Not affected.

If PTI is turned off and the CPU is affected of meltdown the output
becomes: Vulnerable

That series applies on top of

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86/pti

Thanks,

tglx

#1 lore raw

PATCH 1/2 sysfs/cpu: Add vulnerability folder

As the meltdown/spectre problem affects several CPU architectures, it makes sense to have common way to express whether a system is affected by a particular vulnerability or not. If affected the way to express the mitigation should be common as well.

Create /sys/devices/system/cpu/vulnerabilities folder and files for meltdown, spectre_v1 and spectre_v2.

Allow architextures to override the show function.

---
 drivers/base/Kconfig |    3 +++
 drivers/base/cpu.c   |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/cpu.h  |    7 +++++++
 3 files changed, 58 insertions(+)
drivers/base/Kconfig +3 -0
@@ -235,6 +235,9 @@ config GENERIC_CPU_DEVICES
 config GENERIC_CPU_AUTOPROBE
 	bool
 
+config GENERIC_CPU_VULNERABILITIES
+	bool
+
 config SOC_BUS
 	bool
 	select GLOB
drivers/base/cpu.c +48 -0

58 lines not on this page · raw message

include/linux/cpu.h +7 -0
@@ -47,6 +47,13 @@ extern void cpu_remove_dev_attr(struct d
 extern int cpu_add_dev_attr_group(struct attribute_group *attrs);
 extern void cpu_remove_dev_attr_group(struct attribute_group *attrs);
 
+extern ssize_t cpu_show_meltdown(struct device *dev,
+				 struct device_attribute *attr, char *buf);
+extern ssize_t cpu_show_spectre_v1(struct device *dev,
+				   struct device_attribute *attr, char *buf);
+extern ssize_t cpu_show_spectre_v2(struct device *dev,
+				   struct device_attribute *attr, char *buf);
+
 extern __printf(4, 5)
 struct device *cpu_device_create(struct device *parent, void *drvdata,
 				 const struct attribute_group **groups,
#2 lore raw

Re: [patch 1/2] sysfs/cpu: Add vulnerability folder

Quoted 7 lines · Thomas Gleixner

As the meltdown/spectre problem affects several CPU architectures, it makes sense to have common way to express whether a system is affected by a particular vulnerability or not. If affected the way to express the mitigation should be common as well.

Create /sys/devices/system/cpu/vulnerabilities folder and files for meltdown, spectre_v1 and spectre_v2.

I like this, minor nits below:

Quoted 9 lines

Allow architextures to override the show function.

---
 drivers/base/Kconfig |    3 +++
 drivers/base/cpu.c   |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/cpu.h  |    7 +++++++
 3 files changed, 58 insertions(+)

A Documentation/ABI/ update is needed for the new sysfs files.

drivers/base/cpu.c static void __init cpu_dev_register_gene · line 509

Quoted 6 lines
drivers/base/cpu.c +6 -0
+#ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
+
+ssize_t __weak cpu_show_meltdown(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE - 2, "Not affected\n");

sysfs is one-value-per-file, so you never need to care about the page size, a simple sprintf() is fine. No need to change if you don't want to, your call.

Quoted 7 lines
drivers/base/cpu.c +7 -0
+}
+
+ssize_t __weak cpu_show_spectre_v1(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE - 2, "Not affected\n");
+}

drivers/base/cpu.c static void __init cpu_dev_register_gene · line 526

Quoted 10 lines
drivers/base/cpu.c +10 -0
+
+ssize_t __weak cpu_show_spectre_v2(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE - 2, "Not affected\n");
+}
+
+static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
+static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
+static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);

DEVICE_ATTR_RO() please.

Yeah, that does make the global symbols a bit different, meltdown_show() and the like. Hm, I guess this is ok, given that it's ment to be overridden.

Oh, nevermind. So, just a documentation update please, that can always be an add-on patch if you promise to do it :)

thanks,

greg k-h

#3 lore raw

Re: [patch 1/2] sysfs/cpu: Add vulnerability folder

Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines
 drivers/base/Kconfig |    3 +++
 drivers/base/cpu.c   |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/cpu.h  |    7 +++++++
 3 files changed, 58 insertions(+)

A Documentation/ABI/ update is needed for the new sysfs files.

Sure.

Quoted 10 lines

drivers/base/cpu.c static void __init cpu_dev_register_gene · line 509

Quoted 6 lines
drivers/base/cpu.c +6 -0
+#ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
+
+ssize_t __weak cpu_show_meltdown(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE - 2, "Not affected\n");

sysfs is one-value-per-file, so you never need to care about the page size, a simple sprintf() is fine. No need to change if you don't want to, your call.

Done.

Quoted 7 lines
Quoted 1 line

+static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);

DEVICE_ATTR_RO() please.

Yeah, that does make the global symbols a bit different, meltdown_show() and the like. Hm, I guess this is ok, given that it's ment to be overridden.

That and I expect that in the not so distant future we'll see write functions as well.

Quoted 2 lines

Oh, nevermind. So, just a documentation update please, that can always be an add-on patch if you promise to do it :)

You should never make such offers. These promises land on that growth only thingy, aka. todo list :)

Thanks,

tglx

#4 lore raw

PATCH 2/2 x86/cpu: Implement CPU vulnerabilites sysfs functions

In reply to Thomas Gleixner

Implement the CPU vulnerabilty show functions for meltdown, spectre_v1 and spectre_v2.

---
 arch/x86/Kconfig           |    1 +
 arch/x86/kernel/cpu/bugs.c |   29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
arch/x86/Kconfig +1 -0
@@ -89,6 +89,7 @@ config X86
 	select GENERIC_CLOCKEVENTS_MIN_ADJUST
 	select GENERIC_CMOS_UPDATE
 	select GENERIC_CPU_AUTOPROBE
+	select GENERIC_CPU_VULNERABILITIES
 	select GENERIC_EARLY_IOREMAP
 	select GENERIC_FIND_FIRST_BIT
 	select GENERIC_IOMAP
arch/x86/kernel/cpu/bugs.c +29 -0
@@ -10,6 +10,7 @@
  */
 #include <linux/init.h>
 #include <linux/utsname.h>
+#include <linux/cpu.h>
 #include <asm/bugs.h>
 #include <asm/processor.h>
 #include <asm/processor-flags.h>
@@ -60,3 +61,31 @@ void __init check_bugs(void)
 		set_memory_4k((unsigned long)__va(0), 1);
 #endif
 }
+
+#ifdef CONFIG_SYSFS
+ssize_t cpu_show_meltdown(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
+		return snprintf(buf, PAGE_SIZE - 2, "Not affected\n");
+	if (boot_cpu_has(X86_FEATURE_PTI))
+		return snprintf(buf, PAGE_SIZE - 2, "Mitigation: PTI\n");
+	return snprintf(buf, PAGE_SIZE - 2, "Vulnerable\n");
+}
+
+ssize_t cpu_show_spectre_v1(struct device *dev,
+			    struct device_attribute *attr, char *buf)
+{
+	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
+		return snprintf(buf, PAGE_SIZE - 2, "Not affected\n");
+	return snprintf(buf, PAGE_SIZE - 2, "Vulnerable\n");
+}
+
+ssize_t cpu_show_spectre_v2(struct device *dev,
+			    struct device_attribute *attr, char *buf)
+{
+	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
+		return snprintf(buf, PAGE_SIZE - 2, "Not affected\n");
+	return snprintf(buf, PAGE_SIZE - 2, "Vulnerable\n");
+}
+#endif