mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5 00/20] Attack vector controls (part 2)
@ 2025-05-09 16:28 David Kaplan
  2025-05-09 16:28 ` [PATCH v5 01/20] Documentation/x86: Document new attack vector controls David Kaplan
                   ` (20 more replies)
  0 siblings, 21 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

This is an updated version of the second half of the attack vector
series which adds new attack vector command line options designed to make
it easier to control which CPU mitigations are enabled.

The first half of this series focused on bugs.c restructuring and was
merged on May 2.  Link:
https://lore.kernel.org/all/20250418161721.1855190-1-david.kaplan@amd.com/

Attack vector options are designed to make it easier to select appropriate
mitigations based on the usage of the system.  While many users may not be
intimately familiar with the details of these CPU vulnerabilities, they are
likely better able to understand the intended usage of their system.  As a
result, unneeded mitigations may be disabled, allowing users to recoup more
performance.  New documentation is included with recommendations on what to
consider when choosing which attack vectors to enable/disable.

In this series, attack vector options are chosen using the mitigations=
command line.  Attack vectors may be individually disabled such as
'mitigations=auto;no_user_kernel,no_user_user'.  The 'mitigations=off'
option is equivalent to disabling all attack vectors.  'mitigations=off'
therefore disables all mitigations, unless bug-specific command line
options are used to re-enable some.

Note that this patch series does not change any of the existing
mitigation defaults.

Changes in v5:
   - Updated table layout in documentation file
   - Minor clean up

David Kaplan (20):
  Documentation/x86: Document new attack vector controls
  cpu: Define attack vectors
  x86/Kconfig: Add arch attack vector support
  x86/bugs: Define attack vectors relevant for each bug
  x86/bugs: Add attack vector controls for MDS
  x86/bugs: Add attack vector controls for TAA
  x86/bugs: Add attack vector controls for MMIO
  x86/bugs: Add attack vector controls for RFDS
  x86/bugs: Add attack vector controls for SRBDS
  x86/bugs: Add attack vector controls for GDS
  x86/bugs: Add attack vector controls for spectre_v1
  x86/bugs: Add attack vector controls for retbleed
  x86/bugs: Add attack vector controls for spectre_v2_user
  x86/bugs: Add attack vector controls for BHI
  x86/bugs: Add attack vector controls for spectre_v2
  x86/bugs: Add attack vector controls for L1TF
  x86/bugs: Add attack vector controls for SRSO
  x86/pti: Add attack vector controls for PTI
  x86/bugs: Print enabled attack vectors
  cpu: Show attack vectors in sysfs

 .../hw-vuln/attack_vector_controls.rst        | 236 +++++++++++++++
 Documentation/admin-guide/hw-vuln/index.rst   |   1 +
 .../admin-guide/kernel-parameters.txt         |   4 +
 arch/Kconfig                                  |   3 +
 arch/x86/Kconfig                              |   1 +
 arch/x86/kernel/cpu/bugs.c                    | 278 ++++++++++++++----
 arch/x86/mm/pti.c                             |   4 +-
 drivers/base/cpu.c                            |  67 +++++
 include/linux/cpu.h                           |  21 ++
 kernel/cpu.c                                  | 130 +++++++-
 10 files changed, 672 insertions(+), 73 deletions(-)
 create mode 100644 Documentation/admin-guide/hw-vuln/attack_vector_controls.rst


base-commit: fd569ffb9ea03da78b1719ccee5bce34fa130fa7
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 01/20] Documentation/x86: Document new attack vector controls
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 02/20] cpu: Define attack vectors David Kaplan
                   ` (19 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Document the 5 new attack vector command line options, how they
interact with existing vulnerability controls, and recommendations on when
they can be disabled.

Note that while mitigating against untrusted userspace requires both
user-to-kernel and user-to-user protection, these are kept separate.  The
kernel can control what code executes inside of it and that may affect the
risk associated with vulnerabilities especially if new kernel mitigations
are implemented.  The same isn't typically true of userspace.

In other words, the risk associated with user-to-user or guest-to-guest
attacks is unlikely to change over time.  While the risk associated with
user-to-kernel or guest-to-host attacks may change.  Therefore, these
controls are separated.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 .../hw-vuln/attack_vector_controls.rst        | 236 ++++++++++++++++++
 Documentation/admin-guide/hw-vuln/index.rst   |   1 +
 .../admin-guide/kernel-parameters.txt         |   4 +
 3 files changed, 241 insertions(+)
 create mode 100644 Documentation/admin-guide/hw-vuln/attack_vector_controls.rst

diff --git a/Documentation/admin-guide/hw-vuln/attack_vector_controls.rst b/Documentation/admin-guide/hw-vuln/attack_vector_controls.rst
new file mode 100644
index 000000000000..d4ab8b3fe52e
--- /dev/null
+++ b/Documentation/admin-guide/hw-vuln/attack_vector_controls.rst
@@ -0,0 +1,236 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Attack Vector Controls
+======================
+
+Attack vector controls provide a simple method to configure only the mitigations
+for CPU vulnerabilities which are relevant given the intended use of a system.
+Administrators are encouraged to consider which attack vectors are relevant and
+disable all others in order to recoup system performance.
+
+When new relevant CPU vulnerabilities are found, they will be added to these
+attack vector controls so administrators will likely not need to reconfigure
+their command line parameters as mitigations will continue to be correctly
+applied based on the chosen attack vector controls.
+
+Attack Vectors
+--------------
+
+There are 5 sets of attack-vector mitigations currently supported by the kernel:
+
+#. :ref:`user_kernel`
+#. :ref:`user_user`
+#. :ref:`guest_host`
+#. :ref:`guest_guest`
+#. :ref:`smt`
+
+To control the enabled attack vectors, see :ref:`cmdline`.
+
+.. _user_kernel:
+
+User-to-Kernel
+^^^^^^^^^^^^^^
+
+The user-to-kernel attack vector involves a malicious userspace program
+attempting to leak kernel data into userspace by exploiting a CPU vulnerability.
+The kernel data involved might be limited to certain kernel memory, or include
+all memory in the system, depending on the vulnerability exploited.
+
+If no untrusted userspace applications are being run, such as with single-user
+systems, consider disabling user-to-kernel mitigations.
+
+Note that the CPU vulnerabilities mitigated by Linux have generally not been
+shown to be exploitable from browser-based sandboxes.  User-to-kernel
+mitigations are therefore mostly relevant if unknown userspace applications may
+be run by untrusted users.
+
+*user-to-kernel mitigations are enabled by default*
+
+.. _user_user:
+
+User-to-User
+^^^^^^^^^^^^
+
+The user-to-user attack vector involves a malicious userspace program attempting
+to influence the behavior of another unsuspecting userspace program in order to
+exfiltrate data.  The vulnerability of a userspace program is based on the
+program itself and the interfaces it provides.
+
+If no untrusted userspace applications are being run, consider disabling
+user-to-user mitigations.
+
+Note that because the Linux kernel contains a mapping of all physical memory,
+preventing a malicious userspace program from leaking data from another
+userspace program requires mitigating user-to-kernel attacks as well for
+complete protection.
+
+*user-to-user mitigations are enabled by default*
+
+.. _guest_host:
+
+Guest-to-Host
+^^^^^^^^^^^^^
+
+The guest-to-host attack vector involves a malicious VM attempting to leak
+hypervisor data into the VM.  The data involved may be limited, or may
+potentially include all memory in the system, depending on the vulnerability
+exploited.
+
+If no untrusted VMs are being run, consider disabling guest-to-host mitigations.
+
+*guest-to-host mitigations are enabled by default if KVM support is present*
+
+.. _guest_guest:
+
+Guest-to-Guest
+^^^^^^^^^^^^^^
+
+The guest-to-guest attack vector involves a malicious VM attempting to influence
+the behavior of another unsuspecting VM in order to exfiltrate data.  The
+vulnerability of a VM is based on the code inside the VM itself and the
+interfaces it provides.
+
+If no untrusted VMs, or only a single VM is being run, consider disabling
+guest-to-guest mitigations.
+
+Similar to the user-to-user attack vector, preventing a malicious VM from
+leaking data from another VM requires mitigating guest-to-host attacks as well
+due to the Linux kernel phys map.
+
+*guest-to-guest mitigations are enabled by default if KVM support is present*
+
+.. _smt:
+
+Cross-Thread
+^^^^^^^^^^^^
+
+The cross-thread attack vector involves a malicious userspace program or
+malicious VM either observing or attempting to influence the behavior of code
+running on the SMT sibling thread in order to exfiltrate data.
+
+Many cross-thread attacks can only be mitigated if SMT is disabled, which will
+result in reduced CPU core count and reduced performance.
+
+If cross-thread mitigations are fully enabled ('auto,nosmt'), all mitigations
+for cross-thread attacks will be enabled.  SMT may be disabled depending on
+which vulnerabilities are present in the CPU.
+
+If cross-thread mitigations are partially enabled ('auto'), mitigations for
+cross-thread attacks will be enabled but SMT will not be disabled.
+
+If cross-thread mitigations are disabled, no mitigations for cross-thread
+attacks will be enabled.
+
+Cross-thread mitigation may not be required if core-scheduling or similar
+techniques are used to prevent untrusted workloads from running on SMT siblings.
+
+*cross-thread mitigations default to partially enabled*
+
+.. _cmdline:
+
+Command Line Controls
+---------------------
+
+Attack vectors are controlled through the mitigations= command line option.  The
+value provided begins with a global option and then may optionally include one
+or more options to disable various attack vectors.
+
+Format:
+	| ``mitigations=[global]``
+	| ``mitigations=[global];[attack vectors]``
+
+Global options:
+
+============ =============================================================
+Option       Description
+============ =============================================================
+'off'        All attack vectors disabled.
+'auto'       All attack vectors enabled, partial cross-thread mitigations.
+'auto,nosmt' All attack vectors enabled, full cross-thread mitigations.
+============ =============================================================
+
+Attack vector options:
+
+================= =======================================
+Option            Description
+================= =======================================
+'no_user_kernel'  Disables user-to-kernel mitigations.
+'no_user_user'    Disables user-to-user mitigations.
+'no_guest_host'   Disables guest-to-host mitigations.
+'no_guest_guest'  Disables guest-to-guest mitigations
+'no_cross_thread' Disables all cross-thread mitigations.
+================= =======================================
+
+Multiple attack vector options may be specified in a comma-separated list.  If
+the global option is not specified, it defaults to 'auto'.  The global option
+'off' is equivalent to disabling all attack vectors.
+
+Examples:
+	| ``mitigations=auto;no_user_kernel``
+
+	Enable all attack vectors except user-to-kernel.  Partial cross-thread
+	mitigations.
+
+	| ``mitigations=auto,nosmt;no_guest_host,no_guest_guest``
+
+	Enable all attack vectors and cross-thread mitigations except for
+	guest-to-host and guest-to-guest mitigations.
+
+	| ``mitigations=;no_cross_thread``
+
+	Enable all attack vectors but not cross-thread mitigations.
+
+Interactions with command-line options
+--------------------------------------
+
+Vulnerability-specific controls (e.g. "retbleed=off") take precedence over all
+attack vector controls.  Mitigations for individual vulnerabilities may be
+turned on or off via their command-line options regardless of the attack vector
+controls.
+
+Summary of attack-vector mitigations
+------------------------------------
+
+When a vulnerability is mitigated due to an attack-vector control, the default
+mitigation option for that particular vulnerability is used.  To use a different
+mitigation, please use the vulnerability-specific command line option.
+
+The table below summarizes which vulnerabilities are mitigated when different
+attack vectors are enabled and assuming the CPU is vulnerable.
+
+=============== ============== ============ ============= ============== ============ ========
+Vulnerability   User-to-Kernel User-to-User Guest-to-Host Guest-to-Guest Cross-Thread Notes
+=============== ============== ============ ============= ============== ============ ========
+BHI                   X                           X
+GDS                   X              X            X              X            *       (Note 1)
+L1TF                  X                           X                           *       (Note 2)
+MDS                   X              X            X              X            *       (Note 2)
+MMIO                  X              X            X              X            *       (Note 2)
+Meltdown              X
+Retbleed              X                           X                           *       (Note 3)
+RFDS                  X              X            X              X
+Spectre_v1            X
+Spectre_v2            X                           X
+Spectre_v2_user                      X                           X            *       (Note 1)
+SRBDS                 X              X            X              X
+SRSO                  X                           X
+SSB                                                                                   (Note 4)
+TAA                   X              X            X              X            *       (Note 2)
+=============== ============== ============ ============= ============== ============ ========
+
+Notes:
+   1 --  Can be mitigated without disabling SMT.
+
+   2 --  Disables SMT if cross-thread mitigations are fully enabled  and the CPU
+   is vulnerable
+
+   3 --  Disables SMT if cross-thread mitigations are fully enabled, the CPU is
+   vulnerable, and STIBP is not supported
+
+   4 --  Speculative store bypass is always enabled by default (no kernel
+   mitigation applied) unless overridden with spec_store_bypass_disable option
+
+When an attack-vector is disabled, all mitigations for the vulnerabilities
+listed in the above table are disabled, unless mitigation is required for a
+different enabled attack-vector or a mitigation is explicitly selected via a
+vulnerability-specific command line option.
diff --git a/Documentation/admin-guide/hw-vuln/index.rst b/Documentation/admin-guide/hw-vuln/index.rst
index cf1511145927..11a3d0240c66 100644
--- a/Documentation/admin-guide/hw-vuln/index.rst
+++ b/Documentation/admin-guide/hw-vuln/index.rst
@@ -9,6 +9,7 @@ are configurable at compile, boot or run time.
 .. toctree::
    :maxdepth: 1
 
+   attack_vector_controls
    spectre
    l1tf
    mds
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 4568572205ee..f02993bc5b02 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3737,6 +3737,10 @@
 					       mmio_stale_data=full,nosmt [X86]
 					       retbleed=auto,nosmt [X86]
 
+			[X86] After one of the above options, additionally
+			supports attack-vector based controls as documented in
+			Documentation/admin-guide/hw-vuln/attack_vector_controls.rst
+
 	mminit_loglevel=
 			[KNL,EARLY] When CONFIG_DEBUG_MEMORY_INIT is set, this
 			parameter allows control of the logging verbosity for
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 02/20] cpu: Define attack vectors
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
  2025-05-09 16:28 ` [PATCH v5 01/20] Documentation/x86: Document new attack vector controls David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-21 15:28   ` Borislav Petkov
  2025-05-09 16:28 ` [PATCH v5 03/20] x86/Kconfig: Add arch attack vector support David Kaplan
                   ` (18 subsequent siblings)
  20 siblings, 1 reply; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Define 4 new attack vectors that are used for controlling CPU speculation
mitigations.  These may be individually disabled as part of the
mitigations= command line.  Attack vector controls are combined with global
options like 'auto' or 'auto,nosmt' like 'mitigations=auto;no_user_kernel'.

Cross-thread mitigations can either remain enabled fully, including
potentially disabling SMT ('auto,nosmt'), remain enabled except for
disabling SMT ('auto'), or entirely disabled through the new
'no_cross_thread' attack vector option.

The default settings for these attack vectors are consistent with existing
kernel defaults, other than the automatic disabling of VM-based attack
vectors if KVM support is not present.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 include/linux/cpu.h |  21 +++++++
 kernel/cpu.c        | 130 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 140 insertions(+), 11 deletions(-)

diff --git a/include/linux/cpu.h b/include/linux/cpu.h
index 1f5cfc4cc04f..ce98c66a7fc0 100644
--- a/include/linux/cpu.h
+++ b/include/linux/cpu.h
@@ -192,9 +192,25 @@ void cpuhp_report_idle_dead(void);
 static inline void cpuhp_report_idle_dead(void) { }
 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
 
+enum cpu_attack_vectors {
+	CPU_MITIGATE_USER_KERNEL,
+	CPU_MITIGATE_USER_USER,
+	CPU_MITIGATE_GUEST_HOST,
+	CPU_MITIGATE_GUEST_GUEST,
+	NR_CPU_ATTACK_VECTORS,
+};
+
+enum smt_mitigations {
+	SMT_MITIGATIONS_OFF,
+	SMT_MITIGATIONS_AUTO,
+	SMT_MITIGATIONS_ON,
+};
+
 #ifdef CONFIG_CPU_MITIGATIONS
 extern bool cpu_mitigations_off(void);
 extern bool cpu_mitigations_auto_nosmt(void);
+extern bool cpu_attack_vector_mitigated(enum cpu_attack_vectors v);
+extern enum smt_mitigations smt_mitigations;
 #else
 static inline bool cpu_mitigations_off(void)
 {
@@ -204,6 +220,11 @@ static inline bool cpu_mitigations_auto_nosmt(void)
 {
 	return false;
 }
+static inline bool cpu_attack_vector_mitigated(enum cpu_attack_vectors v)
+{
+	return false;
+}
+#define smt_mitigations SMT_MITIGATIONS_OFF
 #endif
 
 #endif /* _LINUX_CPU_H_ */
diff --git a/kernel/cpu.c b/kernel/cpu.c
index a59e009e0be4..be25543567c0 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -37,6 +37,7 @@
 #include <linux/cpuset.h>
 #include <linux/random.h>
 #include <linux/cc_platform.h>
+#include <linux/parser.h>
 
 #include <trace/events/power.h>
 #define CREATE_TRACE_POINTS
@@ -3174,8 +3175,38 @@ void __init boot_cpu_hotplug_init(void)
 
 #ifdef CONFIG_CPU_MITIGATIONS
 /*
- * These are used for a global "mitigations=" cmdline option for toggling
- * optional CPU mitigations.
+ * All except the cross-thread attack vector are mitigated by default.
+ * Cross-thread mitigation often requires disabling SMT which is expensive
+ * so cross-thread mitigations are only partially enabled by default.
+ *
+ * Guest-to-Host and Guest-to-Guest vectors are only needed if KVM support is
+ * present.
+ */
+static bool attack_vectors[NR_CPU_ATTACK_VECTORS] __ro_after_init = {
+	[CPU_MITIGATE_USER_KERNEL] = true,
+	[CPU_MITIGATE_USER_USER] = true,
+	[CPU_MITIGATE_GUEST_HOST] = IS_ENABLED(CONFIG_KVM),
+	[CPU_MITIGATE_GUEST_GUEST] = IS_ENABLED(CONFIG_KVM),
+};
+
+bool cpu_attack_vector_mitigated(enum cpu_attack_vectors v)
+{
+	if (v < NR_CPU_ATTACK_VECTORS)
+		return attack_vectors[v];
+
+	WARN_ONCE(1, "Invalid attack vector %d\n", v);
+	return false;
+}
+
+/*
+ * There are 3 global options, 'off', 'auto', 'auto,nosmt'.
+ * These may optionally be combined with attack-vector disables after a ';'.
+ *
+ * Examples:
+ *   mitigations=auto;no_user_kernel,no_user_user,no_cross_thread
+ *   mitigations=auto,nosmt;no_guest_host,no_guest_guest
+ *
+ * mitigations=off is equivalent to disabling all attack vectors.
  */
 enum cpu_mitigations {
 	CPU_MITIGATIONS_OFF,
@@ -3183,19 +3214,96 @@ enum cpu_mitigations {
 	CPU_MITIGATIONS_AUTO_NOSMT,
 };
 
+enum {
+	NO_USER_KERNEL,
+	NO_USER_USER,
+	NO_GUEST_HOST,
+	NO_GUEST_GUEST,
+	NO_CROSS_THREAD,
+	NR_VECTOR_PARAMS,
+};
+
+enum smt_mitigations smt_mitigations __ro_after_init = SMT_MITIGATIONS_AUTO;
 static enum cpu_mitigations cpu_mitigations __ro_after_init = CPU_MITIGATIONS_AUTO;
 
+static const match_table_t global_mitigations = {
+	{ CPU_MITIGATIONS_AUTO_NOSMT,	"auto,nosmt"},
+	{ CPU_MITIGATIONS_AUTO,		"auto"},
+	{ CPU_MITIGATIONS_OFF,		"off"},
+};
+
+static const match_table_t vector_mitigations = {
+	{ NO_USER_KERNEL,	"no_user_kernel"},
+	{ NO_USER_USER,		"no_user_user"},
+	{ NO_GUEST_HOST,	"no_guest_host"},
+	{ NO_GUEST_GUEST,	"no_guest_guest"},
+	{ NO_CROSS_THREAD,	"no_cross_thread"},
+	{ NR_VECTOR_PARAMS,	NULL},
+};
+
+static int __init mitigations_parse_global_opt(char *arg)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(global_mitigations); i++) {
+		const char *pattern = global_mitigations[i].pattern;
+
+		if (!strncmp(arg, pattern, strlen(pattern))) {
+			cpu_mitigations = global_mitigations[i].token;
+			return strlen(pattern);
+		}
+	}
+
+	return 0;
+}
+
 static int __init mitigations_parse_cmdline(char *arg)
 {
-	if (!strcmp(arg, "off"))
-		cpu_mitigations = CPU_MITIGATIONS_OFF;
-	else if (!strcmp(arg, "auto"))
-		cpu_mitigations = CPU_MITIGATIONS_AUTO;
-	else if (!strcmp(arg, "auto,nosmt"))
-		cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
-	else
-		pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
-			arg);
+	char *s, *p;
+	int len;
+
+	len = mitigations_parse_global_opt(arg);
+
+	if (cpu_mitigations_off()) {
+		memset(attack_vectors, 0, sizeof(attack_vectors));
+		smt_mitigations = SMT_MITIGATIONS_OFF;
+	} else if (cpu_mitigations_auto_nosmt()) {
+		smt_mitigations = SMT_MITIGATIONS_ON;
+	}
+
+	p = arg + len;
+
+	if (!*p)
+		return 0;
+
+	/* Attack vector controls may come after a ';' */
+	if (*p++ != ';' || !IS_ENABLED(CONFIG_ARCH_HAS_CPU_ATTACK_VECTORS)) {
+		pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",	arg);
+		return 0;
+	}
+
+	while ((s = strsep(&p, ",")) != NULL) {
+		switch (match_token(s, vector_mitigations, NULL)) {
+		case NO_USER_KERNEL:
+			attack_vectors[CPU_MITIGATE_USER_KERNEL] = false;
+			break;
+		case NO_USER_USER:
+			attack_vectors[CPU_MITIGATE_USER_USER] = false;
+			break;
+		case NO_GUEST_HOST:
+			attack_vectors[CPU_MITIGATE_GUEST_HOST] = false;
+			break;
+		case NO_GUEST_GUEST:
+			attack_vectors[CPU_MITIGATE_GUEST_GUEST] = false;
+			break;
+		case NO_CROSS_THREAD:
+			smt_mitigations = SMT_MITIGATIONS_OFF;
+			break;
+		default:
+			pr_crit("Unsupported mitigations options %s\n",	s);
+			return 0;
+		}
+	}
 
 	return 0;
 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 03/20] x86/Kconfig: Add arch attack vector support
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
  2025-05-09 16:28 ` [PATCH v5 01/20] Documentation/x86: Document new attack vector controls David Kaplan
  2025-05-09 16:28 ` [PATCH v5 02/20] cpu: Define attack vectors David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 04/20] x86/bugs: Define attack vectors relevant for each bug David Kaplan
                   ` (17 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

ARCH_HAS_CPU_ATTACK_VECTORS should be set for architectures which implement
the new attack-vector based controls for CPU mitigations.  If an arch does
not support attack-vector based controls then an attempt to use them
results in a warning.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/Kconfig     | 3 +++
 arch/x86/Kconfig | 1 +
 2 files changed, 4 insertions(+)

diff --git a/arch/Kconfig b/arch/Kconfig
index b0adb665041f..a900f7a5baae 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -1755,4 +1755,7 @@ config ARCH_WANTS_PRE_LINK_VMLINUX
 	  An architecture can select this if it provides arch/<arch>/tools/Makefile
 	  with .arch.vmlinux.o target to be linked into vmlinux.
 
+config ARCH_HAS_CPU_ATTACK_VECTORS
+	bool
+
 endmenu
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 4c33c644b92d..af6350f056ae 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -74,6 +74,7 @@ config X86
 	select ARCH_ENABLE_SPLIT_PMD_PTLOCK if (PGTABLE_LEVELS > 2) && (X86_64 || X86_PAE)
 	select ARCH_ENABLE_THP_MIGRATION if X86_64 && TRANSPARENT_HUGEPAGE
 	select ARCH_HAS_ACPI_TABLE_UPGRADE	if ACPI
+	select ARCH_HAS_CPU_ATTACK_VECTORS	if CPU_MITIGATIONS
 	select ARCH_HAS_CACHE_LINE_SIZE
 	select ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
 	select ARCH_HAS_CPU_FINALIZE_INIT
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 04/20] x86/bugs: Define attack vectors relevant for each bug
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (2 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 03/20] x86/Kconfig: Add arch attack vector support David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 05/20] x86/bugs: Add attack vector controls for MDS David Kaplan
                   ` (16 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Add a function which defines which vulnerabilities should be mitigated
based on the selected attack vector controls.  The selections here are
based on the individual characteristics of each vulnerability.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 55 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index a938fb4add65..3dce7c3126ea 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -311,6 +311,61 @@ static void x86_amd_ssb_disable(void)
 #undef pr_fmt
 #define pr_fmt(fmt)	"MDS: " fmt
 
+/*
+ * Returns true if vulnerability should be mitigated based on the
+ * selected attack vector controls.
+ *
+ * See Documentation/admin-guide/hw-vuln/attack_vector_controls.rst
+ */
+static bool __init should_mitigate_vuln(unsigned int bug)
+{
+	switch (bug) {
+	/*
+	 * The only runtime-selected spectre_v1 mitigations in the kernel are
+	 * related to SWAPGS protection on kernel entry.  Therefore, protection
+	 * is only required for the user->kernel attack vector.
+	 */
+	case X86_BUG_SPECTRE_V1:
+		return cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL);
+
+	case X86_BUG_SPECTRE_V2:
+	case X86_BUG_RETBLEED:
+	case X86_BUG_SRSO:
+	case X86_BUG_L1TF:
+		return cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST);
+
+	case X86_BUG_SPECTRE_V2_USER:
+		return cpu_attack_vector_mitigated(CPU_MITIGATE_USER_USER) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_GUEST);
+
+	/*
+	 * All the vulnerabilities below allow potentially leaking data
+	 * across address spaces.  Therefore, mitigation is required for
+	 * any of these 4 attack vectors.
+	 */
+	case X86_BUG_MDS:
+	case X86_BUG_TAA:
+	case X86_BUG_MMIO_STALE_DATA:
+	case X86_BUG_RFDS:
+	case X86_BUG_SRBDS:
+		return cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_USER_USER) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_GUEST);
+
+	case X86_BUG_GDS:
+		return cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_USER_USER) ||
+		       cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_GUEST) ||
+		       (smt_mitigations != SMT_MITIGATIONS_OFF);
+	default:
+		WARN(1, "Unknown bug %x\n", bug);
+		return false;
+	}
+}
+
 /* Default mitigation for MDS-affected CPUs */
 static enum mds_mitigations mds_mitigation __ro_after_init =
 	IS_ENABLED(CONFIG_MITIGATION_MDS) ? MDS_MITIGATION_AUTO : MDS_MITIGATION_OFF;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 05/20] x86/bugs: Add attack vector controls for MDS
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (3 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 04/20] x86/bugs: Define attack vectors relevant for each bug David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 06/20] x86/bugs: Add attack vector controls for TAA David Kaplan
                   ` (15 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if MDS mitigation is required.
The global mitigations=off command now simply disables all attack vectors
so explicit checking of mitigations=off is no longer needed.

If cross-thread attack mitigations are required, disable SMT.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 3dce7c3126ea..97d152c33e9e 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -419,13 +419,17 @@ static bool verw_clear_cpu_buf_mitigation_selected __ro_after_init;
 
 static void __init mds_select_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
+	if (!boot_cpu_has_bug(X86_BUG_MDS)) {
 		mds_mitigation = MDS_MITIGATION_OFF;
 		return;
 	}
 
-	if (mds_mitigation == MDS_MITIGATION_AUTO)
-		mds_mitigation = MDS_MITIGATION_FULL;
+	if (mds_mitigation == MDS_MITIGATION_AUTO) {
+		if (should_mitigate_vuln(X86_BUG_MDS))
+			mds_mitigation = MDS_MITIGATION_FULL;
+		else
+			mds_mitigation = MDS_MITIGATION_OFF;
+	}
 
 	if (mds_mitigation == MDS_MITIGATION_OFF)
 		return;
@@ -435,7 +439,7 @@ static void __init mds_select_mitigation(void)
 
 static void __init mds_update_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off())
+	if (!boot_cpu_has_bug(X86_BUG_MDS))
 		return;
 
 	/* If TAA, MMIO, or RFDS are being mitigated, MDS gets mitigated too. */
@@ -456,7 +460,7 @@ static void __init mds_apply_mitigation(void)
 	    mds_mitigation == MDS_MITIGATION_VMWERV) {
 		setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
 		if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
-		    (mds_nosmt || cpu_mitigations_auto_nosmt()))
+		    (mds_nosmt || smt_mitigations == SMT_MITIGATIONS_ON))
 			cpu_smt_disable(false);
 	}
 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 06/20] x86/bugs: Add attack vector controls for TAA
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (4 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 05/20] x86/bugs: Add attack vector controls for MDS David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 07/20] x86/bugs: Add attack vector controls for MMIO David Kaplan
                   ` (14 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if TAA mitigation is required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 97d152c33e9e..d57132cdbc21 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -516,12 +516,13 @@ static void __init taa_select_mitigation(void)
 		return;
 	}
 
-	if (cpu_mitigations_off())
-		taa_mitigation = TAA_MITIGATION_OFF;
-
 	/* Microcode will be checked in taa_update_mitigation(). */
-	if (taa_mitigation == TAA_MITIGATION_AUTO)
-		taa_mitigation = TAA_MITIGATION_VERW;
+	if (taa_mitigation == TAA_MITIGATION_AUTO) {
+		if (should_mitigate_vuln(X86_BUG_TAA))
+			taa_mitigation = TAA_MITIGATION_VERW;
+		else
+			taa_mitigation = TAA_MITIGATION_OFF;
+	}
 
 	if (taa_mitigation != TAA_MITIGATION_OFF)
 		verw_clear_cpu_buf_mitigation_selected = true;
@@ -529,7 +530,7 @@ static void __init taa_select_mitigation(void)
 
 static void __init taa_update_mitigation(void)
 {
-	if (!taa_vulnerable() || cpu_mitigations_off())
+	if (!taa_vulnerable())
 		return;
 
 	if (verw_clear_cpu_buf_mitigation_selected)
@@ -570,7 +571,7 @@ static void __init taa_apply_mitigation(void)
 		 */
 		setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
 
-		if (taa_nosmt || cpu_mitigations_auto_nosmt())
+		if (taa_nosmt || smt_mitigations == SMT_MITIGATIONS_ON)
 			cpu_smt_disable(false);
 	}
 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 07/20] x86/bugs: Add attack vector controls for MMIO
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (5 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 06/20] x86/bugs: Add attack vector controls for TAA David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 08/20] x86/bugs: Add attack vector controls for RFDS David Kaplan
                   ` (13 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vectors controls to determine if MMIO mitigation is required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index d57132cdbc21..bb5d58abaab6 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -617,8 +617,12 @@ static void __init mmio_select_mitigation(void)
 	}
 
 	/* Microcode will be checked in mmio_update_mitigation(). */
-	if (mmio_mitigation == MMIO_MITIGATION_AUTO)
-		mmio_mitigation = MMIO_MITIGATION_VERW;
+	if (mmio_mitigation == MMIO_MITIGATION_AUTO) {
+		if (should_mitigate_vuln(X86_BUG_MMIO_STALE_DATA))
+			mmio_mitigation = MMIO_MITIGATION_VERW;
+		else
+			mmio_mitigation = MMIO_MITIGATION_OFF;
+	}
 
 	if (mmio_mitigation == MMIO_MITIGATION_OFF)
 		return;
@@ -633,7 +637,7 @@ static void __init mmio_select_mitigation(void)
 
 static void __init mmio_update_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) || cpu_mitigations_off())
+	if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
 		return;
 
 	if (verw_clear_cpu_buf_mitigation_selected)
@@ -681,7 +685,7 @@ static void __init mmio_apply_mitigation(void)
 	if (!(x86_arch_cap_msr & ARCH_CAP_FBSDP_NO))
 		static_branch_enable(&mds_idle_clear);
 
-	if (mmio_nosmt || cpu_mitigations_auto_nosmt())
+	if (mmio_nosmt || smt_mitigations == SMT_MITIGATIONS_ON)
 		cpu_smt_disable(false);
 }
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 08/20] x86/bugs: Add attack vector controls for RFDS
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (6 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 07/20] x86/bugs: Add attack vector controls for MMIO David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 09/20] x86/bugs: Add attack vector controls for SRBDS David Kaplan
                   ` (12 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if RFDS mitigation is required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index bb5d58abaab6..e807176bc845 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -726,13 +726,17 @@ static inline bool __init verw_clears_cpu_reg_file(void)
 
 static void __init rfds_select_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_RFDS) || cpu_mitigations_off()) {
+	if (!boot_cpu_has_bug(X86_BUG_RFDS)) {
 		rfds_mitigation = RFDS_MITIGATION_OFF;
 		return;
 	}
 
-	if (rfds_mitigation == RFDS_MITIGATION_AUTO)
-		rfds_mitigation = RFDS_MITIGATION_VERW;
+	if (rfds_mitigation == RFDS_MITIGATION_AUTO) {
+		if (should_mitigate_vuln(X86_BUG_RFDS))
+			rfds_mitigation = RFDS_MITIGATION_VERW;
+		else
+			rfds_mitigation = RFDS_MITIGATION_OFF;
+	}
 
 	if (rfds_mitigation == RFDS_MITIGATION_OFF)
 		return;
@@ -743,7 +747,7 @@ static void __init rfds_select_mitigation(void)
 
 static void __init rfds_update_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_RFDS) || cpu_mitigations_off())
+	if (!boot_cpu_has_bug(X86_BUG_RFDS))
 		return;
 
 	if (verw_clear_cpu_buf_mitigation_selected)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 09/20] x86/bugs: Add attack vector controls for SRBDS
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (7 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 08/20] x86/bugs: Add attack vector controls for RFDS David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 10/20] x86/bugs: Add attack vector controls for GDS David Kaplan
                   ` (11 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if SRBDS mitigation is required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index e807176bc845..237ef8d0980a 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -848,13 +848,19 @@ void update_srbds_msr(void)
 
 static void __init srbds_select_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_SRBDS) || cpu_mitigations_off()) {
+	if (!boot_cpu_has_bug(X86_BUG_SRBDS)) {
 		srbds_mitigation = SRBDS_MITIGATION_OFF;
 		return;
 	}
 
-	if (srbds_mitigation == SRBDS_MITIGATION_AUTO)
-		srbds_mitigation = SRBDS_MITIGATION_FULL;
+	if (srbds_mitigation == SRBDS_MITIGATION_AUTO) {
+		if (should_mitigate_vuln(X86_BUG_SRBDS))
+			srbds_mitigation = SRBDS_MITIGATION_FULL;
+		else {
+			srbds_mitigation = SRBDS_MITIGATION_OFF;
+			return;
+		}
+	}
 
 	/*
 	 * Check to see if this is one of the MDS_NO systems supporting TSX that
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 10/20] x86/bugs: Add attack vector controls for GDS
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (8 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 09/20] x86/bugs: Add attack vector controls for SRBDS David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 11/20] x86/bugs: Add attack vector controls for spectre_v1 David Kaplan
                   ` (10 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if GDS mitigation is required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 237ef8d0980a..659455c5b38f 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1008,12 +1008,15 @@ static void __init gds_select_mitigation(void)
 		return;
 	}
 
-	if (cpu_mitigations_off())
-		gds_mitigation = GDS_MITIGATION_OFF;
 	/* Will verify below that mitigation _can_ be disabled */
-
-	if (gds_mitigation == GDS_MITIGATION_AUTO)
-		gds_mitigation = GDS_MITIGATION_FULL;
+	if (gds_mitigation == GDS_MITIGATION_AUTO) {
+		if (should_mitigate_vuln(X86_BUG_GDS))
+			gds_mitigation = GDS_MITIGATION_FULL;
+		else {
+			gds_mitigation = GDS_MITIGATION_OFF;
+			return;
+		}
+	}
 
 	/* No microcode */
 	if (!(x86_arch_cap_msr & ARCH_CAP_GDS_CTRL)) {
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 11/20] x86/bugs: Add attack vector controls for spectre_v1
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (9 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 10/20] x86/bugs: Add attack vector controls for GDS David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 12/20] x86/bugs: Add attack vector controls for retbleed David Kaplan
                   ` (9 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if spectre_v1 mitigation is
required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 659455c5b38f..ad68130707c2 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1122,13 +1122,16 @@ static bool smap_works_speculatively(void)
 
 static void __init spectre_v1_select_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off())
+	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
+		spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
+
+	if (!should_mitigate_vuln(X86_BUG_SPECTRE_V1))
 		spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
 }
 
 static void __init spectre_v1_apply_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off())
+	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
 		return;
 
 	if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 12/20] x86/bugs: Add attack vector controls for retbleed
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (10 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 11/20] x86/bugs: Add attack vector controls for spectre_v1 David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 13/20] x86/bugs: Add attack vector controls for spectre_v2_user David Kaplan
                   ` (8 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if retbleed mitigation is
required.

Disable SMT if cross-thread protection is desired and STIBP is not
available.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index ad68130707c2..160dcaa18b04 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1251,7 +1251,7 @@ early_param("retbleed", retbleed_parse_cmdline);
 
 static void __init retbleed_select_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off()) {
+	if (!boot_cpu_has_bug(X86_BUG_RETBLEED)) {
 		retbleed_mitigation = RETBLEED_MITIGATION_NONE;
 		return;
 	}
@@ -1288,6 +1288,11 @@ static void __init retbleed_select_mitigation(void)
 	if (retbleed_mitigation != RETBLEED_MITIGATION_AUTO)
 		return;
 
+	if (!should_mitigate_vuln(X86_BUG_RETBLEED)) {
+		retbleed_mitigation = RETBLEED_MITIGATION_NONE;
+		return;
+	}
+
 	/* Intel mitigation selected in retbleed_update_mitigation() */
 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
 	    boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
@@ -1303,7 +1308,7 @@ static void __init retbleed_select_mitigation(void)
 
 static void __init retbleed_update_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
+	if (!boot_cpu_has_bug(X86_BUG_RETBLEED))
 		return;
 
 	if (retbleed_mitigation == RETBLEED_MITIGATION_NONE)
@@ -1401,7 +1406,7 @@ static void __init retbleed_apply_mitigation(void)
 	}
 
 	if (mitigate_smt && !boot_cpu_has(X86_FEATURE_STIBP) &&
-	    (retbleed_nosmt || cpu_mitigations_auto_nosmt()))
+	    (retbleed_nosmt || smt_mitigations == SMT_MITIGATIONS_ON))
 		cpu_smt_disable(false);
 }
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 13/20] x86/bugs: Add attack vector controls for spectre_v2_user
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (11 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 12/20] x86/bugs: Add attack vector controls for retbleed David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI David Kaplan
                   ` (7 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if spectre_v2_user mitigation is
required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 160dcaa18b04..305a11fa9521 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1532,7 +1532,7 @@ static enum spectre_v2_user_cmd __init spectre_v2_parse_user_cmdline(void)
 	char arg[20];
 	int ret, i;
 
-	if (cpu_mitigations_off() || !IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2))
+	if (!IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2))
 		return SPECTRE_V2_USER_CMD_NONE;
 
 	ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
@@ -1570,6 +1570,13 @@ static void __init spectre_v2_user_select_mitigation(void)
 		spectre_v2_user_stibp = SPECTRE_V2_USER_STRICT;
 		break;
 	case SPECTRE_V2_USER_CMD_AUTO:
+		if (!should_mitigate_vuln(X86_BUG_SPECTRE_V2_USER))
+			break;
+		spectre_v2_user_ibpb = SPECTRE_V2_USER_PRCTL;
+		if (smt_mitigations == SMT_MITIGATIONS_OFF)
+			break;
+		spectre_v2_user_stibp = SPECTRE_V2_USER_PRCTL;
+		break;
 	case SPECTRE_V2_USER_CMD_PRCTL:
 		spectre_v2_user_ibpb  = SPECTRE_V2_USER_PRCTL;
 		spectre_v2_user_stibp = SPECTRE_V2_USER_PRCTL;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (12 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 13/20] x86/bugs: Add attack vector controls for spectre_v2_user David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-06-30 12:40   ` Borislav Petkov
  2025-05-09 16:28 ` [PATCH v5 15/20] x86/bugs: Add attack vector controls for spectre_v2 David Kaplan
                   ` (6 subsequent siblings)
  20 siblings, 1 reply; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

There are two BHI mitigations, one for SYSCALL and one for VMEXIT.
Split these up so they can be selected individually based on attack
vector.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 305a11fa9521..667385808400 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1905,8 +1905,9 @@ static bool __init spec_ctrl_bhi_dis(void)
 enum bhi_mitigations {
 	BHI_MITIGATION_OFF,
 	BHI_MITIGATION_AUTO,
-	BHI_MITIGATION_ON,
+	BHI_MITIGATION_FULL,
 	BHI_MITIGATION_VMEXIT_ONLY,
+	BHI_MITIGATION_SYSCALL_ONLY
 };
 
 static enum bhi_mitigations bhi_mitigation __ro_after_init =
@@ -1920,7 +1921,7 @@ static int __init spectre_bhi_parse_cmdline(char *str)
 	if (!strcmp(str, "off"))
 		bhi_mitigation = BHI_MITIGATION_OFF;
 	else if (!strcmp(str, "on"))
-		bhi_mitigation = BHI_MITIGATION_ON;
+		bhi_mitigation = BHI_MITIGATION_FULL;
 	else if (!strcmp(str, "vmexit"))
 		bhi_mitigation = BHI_MITIGATION_VMEXIT_ONLY;
 	else
@@ -1932,11 +1933,22 @@ early_param("spectre_bhi", spectre_bhi_parse_cmdline);
 
 static void __init bhi_select_mitigation(void)
 {
-	if (!boot_cpu_has(X86_BUG_BHI) || cpu_mitigations_off())
+	if (!boot_cpu_has(X86_BUG_BHI))
 		bhi_mitigation = BHI_MITIGATION_OFF;
 
-	if (bhi_mitigation == BHI_MITIGATION_AUTO)
-		bhi_mitigation = BHI_MITIGATION_ON;
+	if (bhi_mitigation != BHI_MITIGATION_AUTO)
+		return;
+
+	if (cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL)) {
+		if (cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST))
+			bhi_mitigation = BHI_MITIGATION_FULL;
+		else
+			bhi_mitigation = BHI_MITIGATION_SYSCALL_ONLY;
+	} else if (cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST)) {
+		bhi_mitigation = BHI_MITIGATION_VMEXIT_ONLY;
+	} else {
+		bhi_mitigation = BHI_MITIGATION_OFF;
+	}
 }
 
 static void __init bhi_update_mitigation(void)
@@ -1969,15 +1981,19 @@ static void __init bhi_apply_mitigation(void)
 	if (!IS_ENABLED(CONFIG_X86_64))
 		return;
 
-	if (bhi_mitigation == BHI_MITIGATION_VMEXIT_ONLY) {
-		pr_info("Spectre BHI mitigation: SW BHB clearing on VM exit only\n");
+	/* Mitigate KVM if guest->host protection is desired */
+	if (bhi_mitigation == BHI_MITIGATION_FULL ||
+	    bhi_mitigation == BHI_MITIGATION_VMEXIT_ONLY) {
 		setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_VMEXIT);
-		return;
+		pr_info("Spectre BHI mitigation: SW BHB clearing on VM exit\n");
 	}
 
-	pr_info("Spectre BHI mitigation: SW BHB clearing on syscall and VM exit\n");
-	setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
-	setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_VMEXIT);
+	/* Mitigate syscalls if user->kernel protection is desired */
+	if (bhi_mitigation == BHI_MITIGATION_FULL ||
+	    bhi_mitigation == BHI_MITIGATION_SYSCALL_ONLY) {
+		setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
+		pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
+	}
 }
 
 static void __init spectre_v2_select_mitigation(void)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 15/20] x86/bugs: Add attack vector controls for spectre_v2
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (13 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 16/20] x86/bugs: Add attack vector controls for L1TF David Kaplan
                   ` (5 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if spectre_v2 mitigation is
required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 667385808400..6a4bc80be1e9 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1728,8 +1728,7 @@ static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
 	int ret, i;
 
 	cmd = IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2) ?  SPECTRE_V2_CMD_AUTO : SPECTRE_V2_CMD_NONE;
-	if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
-	    cpu_mitigations_off())
+	if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
 		return SPECTRE_V2_CMD_NONE;
 
 	ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
@@ -2008,8 +2007,11 @@ static void __init spectre_v2_select_mitigation(void)
 	case SPECTRE_V2_CMD_NONE:
 		return;
 
-	case SPECTRE_V2_CMD_FORCE:
 	case SPECTRE_V2_CMD_AUTO:
+		if (!should_mitigate_vuln(X86_BUG_SPECTRE_V2))
+			break;
+		fallthrough;
+	case SPECTRE_V2_CMD_FORCE:
 		if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
 			spectre_v2_enabled = SPECTRE_V2_EIBRS;
 			break;
@@ -2062,7 +2064,7 @@ static void __init spectre_v2_update_mitigation(void)
 		}
 	}
 
-	if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) && !cpu_mitigations_off())
+	if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
 		pr_info("%s\n", spectre_v2_strings[spectre_v2_enabled]);
 }
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 16/20] x86/bugs: Add attack vector controls for L1TF
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (14 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 15/20] x86/bugs: Add attack vector controls for spectre_v2 David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 17/20] x86/bugs: Add attack vector controls for SRSO David Kaplan
                   ` (4 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if L1TF mitigation is required.

Disable SMT if cross-thread protection is desired.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 6a4bc80be1e9..19a176369b22 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -2697,17 +2697,23 @@ static void override_cache_bits(struct cpuinfo_x86 *c)
 
 static void __init l1tf_select_mitigation(void)
 {
-	if (!boot_cpu_has_bug(X86_BUG_L1TF) || cpu_mitigations_off()) {
+	if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
 		l1tf_mitigation = L1TF_MITIGATION_OFF;
 		return;
 	}
 
-	if (l1tf_mitigation == L1TF_MITIGATION_AUTO) {
-		if (cpu_mitigations_auto_nosmt())
-			l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
-		else
-			l1tf_mitigation = L1TF_MITIGATION_FLUSH;
+	if (l1tf_mitigation != L1TF_MITIGATION_AUTO)
+		return;
+
+	if (!should_mitigate_vuln(X86_BUG_L1TF)) {
+		l1tf_mitigation = L1TF_MITIGATION_OFF;
+		return;
 	}
+
+	if (smt_mitigations == SMT_MITIGATIONS_ON)
+		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
+	else
+		l1tf_mitigation = L1TF_MITIGATION_FLUSH;
 }
 
 static void __init l1tf_apply_mitigation(void)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 17/20] x86/bugs: Add attack vector controls for SRSO
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (15 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 16/20] x86/bugs: Add attack vector controls for L1TF David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 18/20] x86/pti: Add attack vector controls for PTI David Kaplan
                   ` (3 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Use attack vector controls to determine if SRSO mitigation is required.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 19a176369b22..55b0658562c0 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -2840,14 +2840,19 @@ static void __init srso_select_mitigation(void)
 {
 	bool has_microcode;
 
-	if (!boot_cpu_has_bug(X86_BUG_SRSO) || cpu_mitigations_off())
+	if (!boot_cpu_has_bug(X86_BUG_SRSO)) {
 		srso_mitigation = SRSO_MITIGATION_NONE;
-
-	if (srso_mitigation == SRSO_MITIGATION_NONE)
 		return;
+	}
 
-	if (srso_mitigation == SRSO_MITIGATION_AUTO)
-		srso_mitigation = SRSO_MITIGATION_SAFE_RET;
+	if (srso_mitigation == SRSO_MITIGATION_AUTO) {
+		if (should_mitigate_vuln(X86_BUG_SRSO)) {
+			srso_mitigation = SRSO_MITIGATION_SAFE_RET;
+		} else {
+			srso_mitigation = SRSO_MITIGATION_NONE;
+			return;
+		}
+	}
 
 	has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE);
 	if (has_microcode) {
@@ -2909,7 +2914,7 @@ static void __init srso_update_mitigation(void)
 	    boot_cpu_has(X86_FEATURE_IBPB_BRTYPE))
 		srso_mitigation = SRSO_MITIGATION_IBPB;
 
-	if (boot_cpu_has_bug(X86_BUG_SRSO) && !cpu_mitigations_off())
+	if (boot_cpu_has_bug(X86_BUG_SRSO))
 		pr_info("%s\n", srso_strings[srso_mitigation]);
 }
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 18/20] x86/pti: Add attack vector controls for PTI
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (16 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 17/20] x86/bugs: Add attack vector controls for SRSO David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 19/20] x86/bugs: Print enabled attack vectors David Kaplan
                   ` (2 subsequent siblings)
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Disable PTI mitigation if user->kernel attack vector mitigations are
disabled.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/mm/pti.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c
index 190299834011..6dba18f8c715 100644
--- a/arch/x86/mm/pti.c
+++ b/arch/x86/mm/pti.c
@@ -38,6 +38,7 @@
 #include <asm/desc.h>
 #include <asm/sections.h>
 #include <asm/set_memory.h>
+#include <asm/bugs.h>
 
 #undef pr_fmt
 #define pr_fmt(fmt)     "Kernel/User page tables isolation: " fmt
@@ -84,7 +85,8 @@ void __init pti_check_boottime_disable(void)
 		return;
 	}
 
-	if (cpu_mitigations_off())
+	if (pti_mode == PTI_AUTO &&
+	    !cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL))
 		pti_mode = PTI_FORCE_OFF;
 	if (pti_mode == PTI_FORCE_OFF) {
 		pti_print_if_insecure("disabled on command line.");
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 19/20] x86/bugs: Print enabled attack vectors
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (17 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 18/20] x86/pti: Add attack vector controls for PTI David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-05-09 16:28 ` [PATCH v5 20/20] cpu: Show attack vectors in sysfs David Kaplan
  2025-06-30 16:40 ` [PATCH v5 00/20] Attack vector controls (part 2) Borislav Petkov
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Print the status of enabled attack vectors and SMT mitigation status in the
boot log for easier reporting and debugging.  This information will also be
available through sysfs.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 55b0658562c0..4e01903ca6fd 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -177,6 +177,39 @@ DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
 DEFINE_STATIC_KEY_FALSE(cpu_buf_vm_clear);
 EXPORT_SYMBOL_GPL(cpu_buf_vm_clear);
 
+#undef pr_fmt
+#define pr_fmt(fmt)	"mitigations: " fmt
+
+static void __init cpu_print_attack_vectors(void)
+{
+	pr_info("Enabled attack vectors: ");
+
+	if (cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL))
+		pr_cont("user_kernel, ");
+
+	if (cpu_attack_vector_mitigated(CPU_MITIGATE_USER_USER))
+		pr_cont("user_user, ");
+
+	if (cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST))
+		pr_cont("guest_host, ");
+
+	if (cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_GUEST))
+		pr_cont("guest_guest, ");
+
+	pr_cont("SMT mitigations: ");
+
+	switch (smt_mitigations) {
+	case SMT_MITIGATIONS_OFF:
+		pr_cont("off\n");
+		break;
+	case SMT_MITIGATIONS_AUTO:
+		pr_cont("auto\n");
+		break;
+	case SMT_MITIGATIONS_ON:
+		pr_cont("on\n");
+	}
+}
+
 void __init cpu_select_mitigations(void)
 {
 	/*
@@ -197,6 +230,8 @@ void __init cpu_select_mitigations(void)
 
 	x86_arch_cap_msr = x86_read_arch_cap_msr();
 
+	cpu_print_attack_vectors();
+
 	/* Select the proper CPU mitigations before patching alternatives: */
 	spectre_v1_select_mitigation();
 	spectre_v2_select_mitigation();
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5 20/20] cpu: Show attack vectors in sysfs
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (18 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 19/20] x86/bugs: Print enabled attack vectors David Kaplan
@ 2025-05-09 16:28 ` David Kaplan
  2025-06-30 16:40 ` [PATCH v5 00/20] Attack vector controls (part 2) Borislav Petkov
  20 siblings, 0 replies; 29+ messages in thread
From: David Kaplan @ 2025-05-09 16:28 UTC (permalink / raw)
  To: Thomas Gleixner, Borislav Petkov, Peter Zijlstra, Josh Poimboeuf,
	Pawan Gupta, Ingo Molnar, Dave Hansen, x86, H . Peter Anvin
  Cc: linux-kernel

Show the status of currently mitigated attack vectors in
/sys/devices/system/cpu/vector_mitigations/

Note that these files are not under the vulnerabilities directory so they
will not be printed by 'lscpu'.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 drivers/base/cpu.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c
index 1c4359366cd7..991f989f18ff 100644
--- a/drivers/base/cpu.c
+++ b/drivers/base/cpu.c
@@ -20,6 +20,7 @@
 #include <linux/tick.h>
 #include <linux/pm_qos.h>
 #include <linux/delay.h>
+#include <linux/string_choices.h>
 #include <linux/sched/isolation.h>
 
 #include "base.h"
@@ -644,6 +645,70 @@ static const struct attribute_group cpu_root_vulnerabilities_group = {
 	.attrs = cpu_root_vulnerabilities_attrs,
 };
 
+static const char *attack_vector_state(enum cpu_attack_vectors v)
+{
+	return str_on_off(cpu_attack_vector_mitigated(v));
+}
+
+static ssize_t cpu_show_user_kernel_vector(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n",  attack_vector_state(CPU_MITIGATE_USER_KERNEL));
+}
+
+static ssize_t cpu_show_user_user_vector(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n", attack_vector_state(CPU_MITIGATE_USER_USER));
+}
+
+static ssize_t cpu_show_guest_host_vector(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n", attack_vector_state(CPU_MITIGATE_GUEST_HOST));
+}
+
+static ssize_t cpu_show_guest_guest_vector(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n", attack_vector_state(CPU_MITIGATE_GUEST_GUEST));
+}
+
+static ssize_t cpu_show_smt_vector(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	switch (smt_mitigations) {
+	case SMT_MITIGATIONS_OFF:
+		return sysfs_emit(buf, "off\n");
+	case SMT_MITIGATIONS_ON:
+		return sysfs_emit(buf, "on\n");
+	case SMT_MITIGATIONS_AUTO:
+		return sysfs_emit(buf, "auto\n");
+	}
+
+	return 0;
+}
+
+static DEVICE_ATTR(user_kernel, 0444, cpu_show_user_kernel_vector, NULL);
+static DEVICE_ATTR(user_user, 0444, cpu_show_user_user_vector, NULL);
+static DEVICE_ATTR(guest_host, 0444, cpu_show_guest_host_vector, NULL);
+static DEVICE_ATTR(guest_guest, 0444, cpu_show_guest_guest_vector, NULL);
+static DEVICE_ATTR(smt, 0444, cpu_show_smt_vector, NULL);
+
+static struct attribute *cpu_vector_mitigations_attrs[] = {
+	&dev_attr_user_kernel.attr,
+	&dev_attr_user_user.attr,
+	&dev_attr_guest_host.attr,
+	&dev_attr_guest_guest.attr,
+	&dev_attr_smt.attr,
+	NULL
+};
+
+static const struct attribute_group cpu_vector_mitigations_group = {
+	.name  = "vector_mitigations",
+	.attrs = cpu_vector_mitigations_attrs,
+};
+
 static void __init cpu_register_vulnerabilities(void)
 {
 	struct device *dev = bus_get_dev_root(&cpu_subsys);
@@ -651,6 +716,8 @@ static void __init cpu_register_vulnerabilities(void)
 	if (dev) {
 		if (sysfs_create_group(&dev->kobj, &cpu_root_vulnerabilities_group))
 			pr_err("Unable to register CPU vulnerabilities\n");
+		if (sysfs_create_group(&dev->kobj, &cpu_vector_mitigations_group))
+			pr_err("Unable to register CPU attack vectors\n");
 		put_device(dev);
 	}
 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v5 02/20] cpu: Define attack vectors
  2025-05-09 16:28 ` [PATCH v5 02/20] cpu: Define attack vectors David Kaplan
@ 2025-05-21 15:28   ` Borislav Petkov
  0 siblings, 0 replies; 29+ messages in thread
From: Borislav Petkov @ 2025-05-21 15:28 UTC (permalink / raw)
  To: David Kaplan
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

On Fri, May 09, 2025 at 11:28:21AM -0500, David Kaplan wrote:
> +#define smt_mitigations SMT_MITIGATIONS_OFF

Oh fun.

Let's make that explicit before someone goes and shuffles the enums:

diff --git a/include/linux/cpu.h b/include/linux/cpu.h
index 4da2ee8d9144..fdf552fd8a8b 100644
--- a/include/linux/cpu.h
+++ b/include/linux/cpu.h
@@ -203,7 +203,7 @@ enum cpu_attack_vectors {
 };
 
 enum smt_mitigations {
-	SMT_MITIGATIONS_OFF,
+	SMT_MITIGATIONS_OFF = 0,
 	SMT_MITIGATIONS_AUTO,
 	SMT_MITIGATIONS_ON,
 };

-- 
Regards/Gruss,
    Boris.

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

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
  2025-05-09 16:28 ` [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI David Kaplan
@ 2025-06-30 12:40   ` Borislav Petkov
  2025-07-01  3:27     ` Kaplan, David
  0 siblings, 1 reply; 29+ messages in thread
From: Borislav Petkov @ 2025-06-30 12:40 UTC (permalink / raw)
  To: David Kaplan
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

On Fri, May 09, 2025 at 11:28:33AM -0500, David Kaplan wrote:
> There are two BHI mitigations, one for SYSCALL and one for VMEXIT.
> Split these up so they can be selected individually based on attack
> vector.
> 
> Signed-off-by: David Kaplan <david.kaplan@amd.com>
> ---
>  arch/x86/kernel/cpu/bugs.c | 38 +++++++++++++++++++++++++++-----------
>  1 file changed, 27 insertions(+), 11 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
> index 305a11fa9521..667385808400 100644
> --- a/arch/x86/kernel/cpu/bugs.c
> +++ b/arch/x86/kernel/cpu/bugs.c
> @@ -1905,8 +1905,9 @@ static bool __init spec_ctrl_bhi_dis(void)
>  enum bhi_mitigations {
>  	BHI_MITIGATION_OFF,
>  	BHI_MITIGATION_AUTO,
> -	BHI_MITIGATION_ON,
> +	BHI_MITIGATION_FULL,
>  	BHI_MITIGATION_VMEXIT_ONLY,
> +	BHI_MITIGATION_SYSCALL_ONLY

We usually call those USER_KERNEL or so...

But I don't think you need it. The rename to _FULL is ok but the rest could
look like this:

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 32edf0b8a495..624d8d766dca 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -2096,7 +2096,7 @@ static bool __init spec_ctrl_bhi_dis(void)
 enum bhi_mitigations {
 	BHI_MITIGATION_OFF,
 	BHI_MITIGATION_AUTO,
-	BHI_MITIGATION_ON,
+	BHI_MITIGATION_FULL,
 	BHI_MITIGATION_VMEXIT_ONLY,
 };
 
@@ -2111,7 +2111,7 @@ static int __init spectre_bhi_parse_cmdline(char *str)
 	if (!strcmp(str, "off"))
 		bhi_mitigation = BHI_MITIGATION_OFF;
 	else if (!strcmp(str, "on"))
-		bhi_mitigation = BHI_MITIGATION_ON;
+		bhi_mitigation = BHI_MITIGATION_FULL;
 	else if (!strcmp(str, "vmexit"))
 		bhi_mitigation = BHI_MITIGATION_VMEXIT_ONLY;
 	else
@@ -2123,11 +2123,11 @@ early_param("spectre_bhi", spectre_bhi_parse_cmdline);
 
 static void __init bhi_select_mitigation(void)
 {
-	if (!boot_cpu_has(X86_BUG_BHI) || cpu_mitigations_off())
+	if (!boot_cpu_has(X86_BUG_BHI))
 		bhi_mitigation = BHI_MITIGATION_OFF;
 
 	if (bhi_mitigation == BHI_MITIGATION_AUTO)
-		bhi_mitigation = BHI_MITIGATION_ON;
+		bhi_mitigation = BHI_MITIGATION_FULL;
 }
 
 static void __init bhi_update_mitigation(void)
@@ -2160,15 +2160,19 @@ static void __init bhi_apply_mitigation(void)
 	if (spec_ctrl_bhi_dis())
 		return;
 
-	if (bhi_mitigation == BHI_MITIGATION_VMEXIT_ONLY) {
-		pr_info("Spectre BHI mitigation: SW BHB clearing on VM exit only\n");
+	/* Mitigate KVM if guest->host protection is desired */
+	if (bhi_mitigation == BHI_MITIGATION_FULL ||
+	    bhi_mitigation == BHI_MITIGATION_VMEXIT_ONLY) {
 		setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_VMEXIT);
-		return;
+		pr_info("Spectre BHI mitigation: SW BHB clearing on VM exit\n");
 	}
 
-	pr_info("Spectre BHI mitigation: SW BHB clearing on syscall and VM exit\n");
-	setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
-	setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_VMEXIT);
+	/* Mitigate syscalls if user->kernel protection is desired */
+	if (bhi_mitigation == BHI_MITIGATION_FULL ||
+	    cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL)) {
+		setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
+		pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
+	}
 }
 
 static void __init spectre_v2_select_mitigation(void)

-- 
Regards/Gruss,
    Boris.

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

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v5 00/20] Attack vector controls (part 2)
  2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
                   ` (19 preceding siblings ...)
  2025-05-09 16:28 ` [PATCH v5 20/20] cpu: Show attack vectors in sysfs David Kaplan
@ 2025-06-30 16:40 ` Borislav Petkov
  2025-07-01  3:27   ` Kaplan, David
  20 siblings, 1 reply; 29+ messages in thread
From: Borislav Petkov @ 2025-06-30 16:40 UTC (permalink / raw)
  To: David Kaplan
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

On Fri, May 09, 2025 at 11:28:19AM -0500, David Kaplan wrote:
>  .../hw-vuln/attack_vector_controls.rst        | 236 +++++++++++++++
>  Documentation/admin-guide/hw-vuln/index.rst   |   1 +
>  .../admin-guide/kernel-parameters.txt         |   4 +
>  arch/Kconfig                                  |   3 +
>  arch/x86/Kconfig                              |   1 +
>  arch/x86/kernel/cpu/bugs.c                    | 278 ++++++++++++++----
>  arch/x86/mm/pti.c                             |   4 +-
>  drivers/base/cpu.c                            |  67 +++++
>  include/linux/cpu.h                           |  21 ++
>  kernel/cpu.c                                  | 130 +++++++-
>  10 files changed, 672 insertions(+), 73 deletions(-)
>  create mode 100644 Documentation/admin-guide/hw-vuln/attack_vector_controls.rst

Ok, I replied to one message which is needs more discussing but the rest looks
ok to me. Feel free to send the next revision once we've resolved this so that
I can queue it.

Thx.

-- 
Regards/Gruss,
    Boris.

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

^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
  2025-06-30 12:40   ` Borislav Petkov
@ 2025-07-01  3:27     ` Kaplan, David
  2025-07-01 11:45       ` Borislav Petkov
  0 siblings, 1 reply; 29+ messages in thread
From: Kaplan, David @ 2025-07-01  3:27 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

[AMD Official Use Only - AMD Internal Distribution Only]

> -----Original Message-----
> From: Borislav Petkov <bp@alien8.de>
> Sent: Monday, June 30, 2025 7:41 AM
> To: Kaplan, David <David.Kaplan@amd.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>; Peter Zijlstra <peterz@infradead.org>;
> Josh Poimboeuf <jpoimboe@kernel.org>; Pawan Gupta
> <pawan.kumar.gupta@linux.intel.com>; Ingo Molnar <mingo@redhat.com>; Dave
> Hansen <dave.hansen@linux.intel.com>; x86@kernel.org; H . Peter Anvin
> <hpa@zytor.com>; linux-kernel@vger.kernel.org
> Subject: Re: [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
>
> Caution: This message originated from an External Source. Use proper caution
> when opening attachments, clicking links, or responding.
>
>
> On Fri, May 09, 2025 at 11:28:33AM -0500, David Kaplan wrote:
> > There are two BHI mitigations, one for SYSCALL and one for VMEXIT.
> > Split these up so they can be selected individually based on attack
> > vector.
> >
> > Signed-off-by: David Kaplan <david.kaplan@amd.com>
> > ---
> >  arch/x86/kernel/cpu/bugs.c | 38 +++++++++++++++++++++++++++-----------
> >  1 file changed, 27 insertions(+), 11 deletions(-)
> >
> > diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
> > index 305a11fa9521..667385808400 100644
> > --- a/arch/x86/kernel/cpu/bugs.c
> > +++ b/arch/x86/kernel/cpu/bugs.c
> > @@ -1905,8 +1905,9 @@ static bool __init spec_ctrl_bhi_dis(void)
> >  enum bhi_mitigations {
> >       BHI_MITIGATION_OFF,
> >       BHI_MITIGATION_AUTO,
> > -     BHI_MITIGATION_ON,
> > +     BHI_MITIGATION_FULL,
> >       BHI_MITIGATION_VMEXIT_ONLY,
> > +     BHI_MITIGATION_SYSCALL_ONLY
>
> We usually call those USER_KERNEL or so...

Was following the existing pattern with VMEXIT_ONLY.  But I could rename these to USER_KERNEL (for the SYSCALL one) and GUEST_HOST (for the VMEXIT one) if desired...

>
> But I don't think you need it. The rename to _FULL is ok but the rest could
> look like this:
>

Eh, I'm not a big fan of that.  It's basically overloading an existing mitigation setting with a new meaning depending something else.  That's unnecessarily complex imo, and in this case is actually incorrect.  The attack vectors are supposed to be lower priority than bug-specific command line options (as attack vectors are generic).  So if you pass in "spectre_bhi=vmexit" for instance, that should only mitigate bhi for vmexit, even if you have general user->kernel protections enabled.  The code below appears to not observe that correctly.

That issue aside, the enums I believe should ideally map to specific mitigation decisions.  There are two potential mitigations for BHI, so it makes sense to have mitigation choices for all 4 potential outcomes.

--David Kaplan

> diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
> index 32edf0b8a495..624d8d766dca 100644
> --- a/arch/x86/kernel/cpu/bugs.c
> +++ b/arch/x86/kernel/cpu/bugs.c
> @@ -2096,7 +2096,7 @@ static bool __init spec_ctrl_bhi_dis(void)
>  enum bhi_mitigations {
>         BHI_MITIGATION_OFF,
>         BHI_MITIGATION_AUTO,
> -       BHI_MITIGATION_ON,
> +       BHI_MITIGATION_FULL,
>         BHI_MITIGATION_VMEXIT_ONLY,
>  };
>
> @@ -2111,7 +2111,7 @@ static int __init spectre_bhi_parse_cmdline(char *str)
>         if (!strcmp(str, "off"))
>                 bhi_mitigation = BHI_MITIGATION_OFF;
>         else if (!strcmp(str, "on"))
> -               bhi_mitigation = BHI_MITIGATION_ON;
> +               bhi_mitigation = BHI_MITIGATION_FULL;
>         else if (!strcmp(str, "vmexit"))
>                 bhi_mitigation = BHI_MITIGATION_VMEXIT_ONLY;
>         else
> @@ -2123,11 +2123,11 @@ early_param("spectre_bhi",
> spectre_bhi_parse_cmdline);
>
>  static void __init bhi_select_mitigation(void)
>  {
> -       if (!boot_cpu_has(X86_BUG_BHI) || cpu_mitigations_off())
> +       if (!boot_cpu_has(X86_BUG_BHI))
>                 bhi_mitigation = BHI_MITIGATION_OFF;
>
>         if (bhi_mitigation == BHI_MITIGATION_AUTO)
> -               bhi_mitigation = BHI_MITIGATION_ON;
> +               bhi_mitigation = BHI_MITIGATION_FULL;
>  }
>
>  static void __init bhi_update_mitigation(void)
> @@ -2160,15 +2160,19 @@ static void __init bhi_apply_mitigation(void)
>         if (spec_ctrl_bhi_dis())
>                 return;
>
> -       if (bhi_mitigation == BHI_MITIGATION_VMEXIT_ONLY) {
> -               pr_info("Spectre BHI mitigation: SW BHB clearing on VM exit only\n");
> +       /* Mitigate KVM if guest->host protection is desired */
> +       if (bhi_mitigation == BHI_MITIGATION_FULL ||
> +           bhi_mitigation == BHI_MITIGATION_VMEXIT_ONLY) {
>                 setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_VMEXIT);
> -               return;
> +               pr_info("Spectre BHI mitigation: SW BHB clearing on VM exit\n");
>         }
>
> -       pr_info("Spectre BHI mitigation: SW BHB clearing on syscall and VM exit\n");
> -       setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
> -       setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_VMEXIT);
> +       /* Mitigate syscalls if user->kernel protection is desired */
> +       if (bhi_mitigation == BHI_MITIGATION_FULL ||
> +           cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL)) {
> +               setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
> +               pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
> +       }
>  }
>
>  static void __init spectre_v2_select_mitigation(void)
>
> --
> Regards/Gruss,
>     Boris.
>
> https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH v5 00/20] Attack vector controls (part 2)
  2025-06-30 16:40 ` [PATCH v5 00/20] Attack vector controls (part 2) Borislav Petkov
@ 2025-07-01  3:27   ` Kaplan, David
  0 siblings, 0 replies; 29+ messages in thread
From: Kaplan, David @ 2025-07-01  3:27 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

[AMD Official Use Only - AMD Internal Distribution Only]

> -----Original Message-----
> From: Borislav Petkov <bp@alien8.de>
> Sent: Monday, June 30, 2025 11:40 AM
> To: Kaplan, David <David.Kaplan@amd.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>; Peter Zijlstra <peterz@infradead.org>;
> Josh Poimboeuf <jpoimboe@kernel.org>; Pawan Gupta
> <pawan.kumar.gupta@linux.intel.com>; Ingo Molnar <mingo@redhat.com>; Dave
> Hansen <dave.hansen@linux.intel.com>; x86@kernel.org; H . Peter Anvin
> <hpa@zytor.com>; linux-kernel@vger.kernel.org
> Subject: Re: [PATCH v5 00/20] Attack vector controls (part 2)
>
> Caution: This message originated from an External Source. Use proper caution
> when opening attachments, clicking links, or responding.
>
>
> On Fri, May 09, 2025 at 11:28:19AM -0500, David Kaplan wrote:
> >  .../hw-vuln/attack_vector_controls.rst        | 236 +++++++++++++++
> >  Documentation/admin-guide/hw-vuln/index.rst   |   1 +
> >  .../admin-guide/kernel-parameters.txt         |   4 +
> >  arch/Kconfig                                  |   3 +
> >  arch/x86/Kconfig                              |   1 +
> >  arch/x86/kernel/cpu/bugs.c                    | 278 ++++++++++++++----
> >  arch/x86/mm/pti.c                             |   4 +-
> >  drivers/base/cpu.c                            |  67 +++++
> >  include/linux/cpu.h                           |  21 ++
> >  kernel/cpu.c                                  | 130 +++++++-
> >  10 files changed, 672 insertions(+), 73 deletions(-)
> >  create mode 100644 Documentation/admin-guide/hw-
> vuln/attack_vector_controls.rst
>
> Ok, I replied to one message which is needs more discussing but the rest looks
> ok to me. Feel free to send the next revision once we've resolved this so that
> I can queue it.
>
> Thx.
>

Ack, thanks.  Note that the next revision will also include a new patch for adding attack vector support for ITS, but that should be pretty straightforward.

--David Kaplan

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
  2025-07-01  3:27     ` Kaplan, David
@ 2025-07-01 11:45       ` Borislav Petkov
  2025-07-02 18:24         ` Kaplan, David
  0 siblings, 1 reply; 29+ messages in thread
From: Borislav Petkov @ 2025-07-01 11:45 UTC (permalink / raw)
  To: Kaplan, David
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

On Tue, Jul 01, 2025 at 03:27:00AM +0000, Kaplan, David wrote:
> Eh, I'm not a big fan of that.  It's basically overloading an existing
> mitigation setting with a new meaning depending something else.  That's
> unnecessarily complex imo, and in this case is actually incorrect.  The
> attack vectors are supposed to be lower priority than bug-specific command
> line options (as attack vectors are generic).  So if you pass in
> "spectre_bhi=vmexit" for instance, that should only mitigate bhi for vmexit,
> even if you have general user->kernel protections enabled.  The code below
> appears to not observe that correctly.
> 
> That issue aside, the enums I believe should ideally map to specific
> mitigation decisions.  There are two potential mitigations for BHI, so it
> makes sense to have mitigation choices for all 4 potential outcomes.

But then having a _FULL enum for both while *also* having USER_KERNEL and
GUEST_HOST which both mean _FULL is kinda confusing, no?

And when you look at the final code, that's kinda not really easy to grok for
something which is actually simple.

So I'm not sure the renaming and adding of another mitigation option is really
needed. If you want to set mitigations based on the vectors, you can simply
do:

	if (cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST)) {
                if (cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL)) {
                        bhi_mitigation = BHI_MITIGATION_ON;
                else    
                        bhi_mitigation = BHI_MITIGATION_VMEXIT_ONLY;
                }
        }

no?

Using the existing switches.

Because you have VMEXIT_ONLY already, so you check that first and only if
USER_KERNEL is also mitigated, you select _ON.

And then you don't need to touch bhi_apply_mitigation() at all.

Or am I missing a corner case?

-- 
Regards/Gruss,
    Boris.

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

^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
  2025-07-01 11:45       ` Borislav Petkov
@ 2025-07-02 18:24         ` Kaplan, David
  2025-07-03 10:01           ` Borislav Petkov
  0 siblings, 1 reply; 29+ messages in thread
From: Kaplan, David @ 2025-07-02 18:24 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

[AMD Official Use Only - AMD Internal Distribution Only]

> -----Original Message-----
> From: Borislav Petkov <bp@alien8.de>
> Sent: Tuesday, July 1, 2025 6:46 AM
> To: Kaplan, David <David.Kaplan@amd.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>; Peter Zijlstra <peterz@infradead.org>;
> Josh Poimboeuf <jpoimboe@kernel.org>; Pawan Gupta
> <pawan.kumar.gupta@linux.intel.com>; Ingo Molnar <mingo@redhat.com>; Dave
> Hansen <dave.hansen@linux.intel.com>; x86@kernel.org; H . Peter Anvin
> <hpa@zytor.com>; linux-kernel@vger.kernel.org
> Subject: Re: [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
>
> Caution: This message originated from an External Source. Use proper caution
> when opening attachments, clicking links, or responding.
>
>
> On Tue, Jul 01, 2025 at 03:27:00AM +0000, Kaplan, David wrote:
> > Eh, I'm not a big fan of that.  It's basically overloading an existing
> > mitigation setting with a new meaning depending something else.  That's
> > unnecessarily complex imo, and in this case is actually incorrect.  The
> > attack vectors are supposed to be lower priority than bug-specific command
> > line options (as attack vectors are generic).  So if you pass in
> > "spectre_bhi=vmexit" for instance, that should only mitigate bhi for vmexit,
> > even if you have general user->kernel protections enabled.  The code below
> > appears to not observe that correctly.
> >
> > That issue aside, the enums I believe should ideally map to specific
> > mitigation decisions.  There are two potential mitigations for BHI, so it
> > makes sense to have mitigation choices for all 4 potential outcomes.
>
> But then having a _FULL enum for both while *also* having USER_KERNEL and
> GUEST_HOST which both mean _FULL is kinda confusing, no?

I guess I don't see that.  Those others don't mean full, they only protect the relevant attack vector.  FULL protects all attack vectors.

>
> And when you look at the final code, that's kinda not really easy to grok for
> something which is actually simple.
>
> So I'm not sure the renaming and adding of another mitigation option is really
> needed. If you want to set mitigations based on the vectors, you can simply
> do:
>
>         if (cpu_attack_vector_mitigated(CPU_MITIGATE_GUEST_HOST)) {
>                 if (cpu_attack_vector_mitigated(CPU_MITIGATE_USER_KERNEL)) {
>                         bhi_mitigation = BHI_MITIGATION_ON;
>                 else
>                         bhi_mitigation = BHI_MITIGATION_VMEXIT_ONLY;
>                 }
>         }
>
> no?
>
> Using the existing switches.
>
> Because you have VMEXIT_ONLY already, so you check that first and only if
> USER_KERNEL is also mitigated, you select _ON.
>
> And then you don't need to touch bhi_apply_mitigation() at all.
>
> Or am I missing a corner case?
>

Yeah, I think my code was doing two things...it was adding the attack vector controls for the existing mitigations, and then also adding a new user->kernel only mitigation.

So probably the right answer here is to split this up.  I'll change the patch to just use the existing mitigations and structure it like your snippet above.  If someone wants to add a new user->kernel only option for BHI, that can be done in a separate patch later.  There's actually probably several other mitigations that could similarly be split up based on attack vector (e.g. have separate controls for VERW in various places), if we wanted to have more mitigation options based on attack vectors (instead of just a simple on/off).

--David Kaplan

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI
  2025-07-02 18:24         ` Kaplan, David
@ 2025-07-03 10:01           ` Borislav Petkov
  0 siblings, 0 replies; 29+ messages in thread
From: Borislav Petkov @ 2025-07-03 10:01 UTC (permalink / raw)
  To: Kaplan, David
  Cc: Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Ingo Molnar, Dave Hansen, x86, H . Peter Anvin, linux-kernel

On Wed, Jul 02, 2025 at 06:24:31PM +0000, Kaplan, David wrote:
> So probably the right answer here is to split this up.  I'll change the
> patch to just use the existing mitigations and structure it like your
> snippet above.  If someone wants to add a new user->kernel only option for
> BHI, that can be done in a separate patch later.  There's actually probably
> several other mitigations that could similarly be split up based on attack
> vector (e.g. have separate controls for VERW in various places), if we
> wanted to have more mitigation options based on attack vectors (instead of
> just a simple on/off).

Yeah, the thought that our mitigations are not really consistent wrt vectors
did cross my mind. We should definitely keep this in mind and perhaps
restructure them into a common pattern later, if it turns beneficial.

Thx.

-- 
Regards/Gruss,
    Boris.

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

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2025-07-03 10:02 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-09 16:28 [PATCH v5 00/20] Attack vector controls (part 2) David Kaplan
2025-05-09 16:28 ` [PATCH v5 01/20] Documentation/x86: Document new attack vector controls David Kaplan
2025-05-09 16:28 ` [PATCH v5 02/20] cpu: Define attack vectors David Kaplan
2025-05-21 15:28   ` Borislav Petkov
2025-05-09 16:28 ` [PATCH v5 03/20] x86/Kconfig: Add arch attack vector support David Kaplan
2025-05-09 16:28 ` [PATCH v5 04/20] x86/bugs: Define attack vectors relevant for each bug David Kaplan
2025-05-09 16:28 ` [PATCH v5 05/20] x86/bugs: Add attack vector controls for MDS David Kaplan
2025-05-09 16:28 ` [PATCH v5 06/20] x86/bugs: Add attack vector controls for TAA David Kaplan
2025-05-09 16:28 ` [PATCH v5 07/20] x86/bugs: Add attack vector controls for MMIO David Kaplan
2025-05-09 16:28 ` [PATCH v5 08/20] x86/bugs: Add attack vector controls for RFDS David Kaplan
2025-05-09 16:28 ` [PATCH v5 09/20] x86/bugs: Add attack vector controls for SRBDS David Kaplan
2025-05-09 16:28 ` [PATCH v5 10/20] x86/bugs: Add attack vector controls for GDS David Kaplan
2025-05-09 16:28 ` [PATCH v5 11/20] x86/bugs: Add attack vector controls for spectre_v1 David Kaplan
2025-05-09 16:28 ` [PATCH v5 12/20] x86/bugs: Add attack vector controls for retbleed David Kaplan
2025-05-09 16:28 ` [PATCH v5 13/20] x86/bugs: Add attack vector controls for spectre_v2_user David Kaplan
2025-05-09 16:28 ` [PATCH v5 14/20] x86/bugs: Add attack vector controls for BHI David Kaplan
2025-06-30 12:40   ` Borislav Petkov
2025-07-01  3:27     ` Kaplan, David
2025-07-01 11:45       ` Borislav Petkov
2025-07-02 18:24         ` Kaplan, David
2025-07-03 10:01           ` Borislav Petkov
2025-05-09 16:28 ` [PATCH v5 15/20] x86/bugs: Add attack vector controls for spectre_v2 David Kaplan
2025-05-09 16:28 ` [PATCH v5 16/20] x86/bugs: Add attack vector controls for L1TF David Kaplan
2025-05-09 16:28 ` [PATCH v5 17/20] x86/bugs: Add attack vector controls for SRSO David Kaplan
2025-05-09 16:28 ` [PATCH v5 18/20] x86/pti: Add attack vector controls for PTI David Kaplan
2025-05-09 16:28 ` [PATCH v5 19/20] x86/bugs: Print enabled attack vectors David Kaplan
2025-05-09 16:28 ` [PATCH v5 20/20] cpu: Show attack vectors in sysfs David Kaplan
2025-06-30 16:40 ` [PATCH v5 00/20] Attack vector controls (part 2) Borislav Petkov
2025-07-01  3:27   ` Kaplan, David

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®