mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Kaplan <david.kaplan@amd.com>
To: Thomas Gleixner <tglx@linutronix.de>,
	Borislav Petkov <bp@alien8.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>
Cc: <linux-kernel@vger.kernel.org>,
	Brendan Jackman <jackmanb@google.com>,
	Derek Manwaring <derekmn@amazon.com>
Subject: [PATCH v4 18/36] cpu: Define attack vectors
Date: Mon, 10 Mar 2025 11:40:05 -0500	[thread overview]
Message-ID: <20250310164023.779191-19-david.kaplan@amd.com> (raw)
In-Reply-To: <20250310164023.779191-1-david.kaplan@amd.com>

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 |  20 +++++++
 kernel/cpu.c        | 129 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 138 insertions(+), 11 deletions(-)

diff --git a/include/linux/cpu.h b/include/linux/cpu.h
index 6a0a8f1c7c90..476c6dfe2c03 100644
--- a/include/linux/cpu.h
+++ b/include/linux/cpu.h
@@ -190,9 +190,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_mitigate_attack_vector(enum cpu_attack_vectors v);
+extern enum smt_mitigations smt_mitigations;
 #else
 static inline bool cpu_mitigations_off(void)
 {
@@ -202,6 +218,10 @@ static inline bool cpu_mitigations_auto_nosmt(void)
 {
 	return false;
 }
+static inline bool cpu_mitigate_attack_vector(enum cpu_attack_vectors v)
+{
+	return false;
+}
 #endif
 
 #endif /* _LINUX_CPU_H_ */
diff --git a/kernel/cpu.c b/kernel/cpu.c
index ad755db29efd..fe00ab81d682 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
@@ -3178,8 +3179,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 too expensive
+ * to be 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_mitigate_attack_vector(enum cpu_attack_vectors v)
+{
+	if (v < NR_CPU_ATTACK_VECTORS)
+		return attack_vectors[v];
+
+	WARN_ON_ONCE(v >= NR_CPU_ATTACK_VECTORS);
+	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,
@@ -3187,19 +3218,95 @@ 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


  parent reply	other threads:[~2025-03-10 16:41 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-10 16:39 [PATCH v4 00/36] Attack vector controls David Kaplan
2025-03-10 16:39 ` [PATCH v4 01/36] x86/bugs: Restructure mds mitigation David Kaplan
2025-03-10 16:39 ` [PATCH v4 02/36] x86/bugs: Restructure taa mitigation David Kaplan
2025-03-10 16:39 ` [PATCH v4 03/36] x86/bugs: Restructure mmio mitigation David Kaplan
2025-03-13  9:36   ` Borislav Petkov
2025-03-13 14:19     ` Kaplan, David
2025-03-13 19:26     ` Pawan Gupta
2025-03-18 14:16       ` MMIO and VERW Borislav Petkov
2025-03-18 16:25         ` Pawan Gupta
2025-03-18 16:34           ` Borislav Petkov
2025-03-18 16:56             ` Pawan Gupta
2025-03-18 17:37               ` Borislav Petkov
2025-03-18 18:15                 ` Pawan Gupta
2025-03-18 18:55                   ` Borislav Petkov
2025-03-24  9:29       ` [PATCH v4 03/36] x86/bugs: Restructure mmio mitigation Borislav Petkov
2025-03-24 17:41         ` Pawan Gupta
2025-03-25 11:31           ` Borislav Petkov
2025-03-10 16:39 ` [PATCH v4 04/36] x86/bugs: Restructure rfds mitigation David Kaplan
2025-04-10 16:24   ` Josh Poimboeuf
2025-04-14 19:10     ` Kaplan, David
2025-03-10 16:39 ` [PATCH v4 05/36] x86/bugs: Remove md_clear_*_mitigation() David Kaplan
2025-03-10 16:39 ` [PATCH v4 06/36] x86/bugs: Restructure srbds mitigation David Kaplan
2025-03-10 16:39 ` [PATCH v4 07/36] x86/bugs: Restructure gds mitigation David Kaplan
2025-03-10 16:39 ` [PATCH v4 08/36] x86/bugs: Restructure spectre_v1 mitigation David Kaplan
2025-03-10 16:39 ` [PATCH v4 09/36] x86/bugs: Only allow retbleed=stuff on Intel David Kaplan
2025-03-10 16:39 ` [PATCH v4 10/36] x86/bugs: Restructure retbleed mitigation David Kaplan
2025-03-10 16:39 ` [PATCH v4 11/36] x86/bugs: Restructure spectre_v2_user mitigation David Kaplan
2025-03-12  9:49   ` kernel test robot
2025-04-10 16:26   ` Josh Poimboeuf
2025-04-14 19:20     ` Kaplan, David
2025-04-10 16:41   ` Josh Poimboeuf
2025-04-14 19:20     ` Kaplan, David
2025-04-14 23:48       ` Josh Poimboeuf
2025-04-15 14:56         ` Kaplan, David
2025-03-10 16:39 ` [PATCH v4 12/36] x86/bugs: Restructure bhi mitigation David Kaplan
2025-03-10 16:40 ` [PATCH v4 13/36] x86/bugs: Restructure spectre_v2 mitigation David Kaplan
2025-04-10 17:08   ` Josh Poimboeuf
2025-04-14 19:25     ` Kaplan, David
2025-03-10 16:40 ` [PATCH v4 14/36] x86/bugs: Restructure ssb mitigation David Kaplan
2025-03-10 16:40 ` [PATCH v4 15/36] x86/bugs: Restructure l1tf mitigation David Kaplan
2025-03-10 16:40 ` [PATCH v4 16/36] x86/bugs: Restructure srso mitigation David Kaplan
2025-04-10 17:38   ` Josh Poimboeuf
2025-04-14 21:12     ` Kaplan, David
2025-03-10 16:40 ` [PATCH v4 17/36] Documentation/x86: Document the new attack vector controls David Kaplan
2025-04-10 18:14   ` Josh Poimboeuf
2025-04-14 21:15     ` Kaplan, David
2025-04-14 23:51       ` Josh Poimboeuf
2025-04-15 14:59         ` Kaplan, David
2025-04-15 15:32           ` Josh Poimboeuf
2025-04-15 16:10             ` Kaplan, David
2025-04-15 16:47               ` Josh Poimboeuf
2025-04-15 17:06                 ` Kaplan, David
2025-03-10 16:40 ` David Kaplan [this message]
2025-04-10 18:12   ` [PATCH v4 18/36] cpu: Define attack vectors Josh Poimboeuf
2025-04-14 21:20     ` Kaplan, David
2025-03-10 16:40 ` [PATCH v4 19/36] x86/Kconfig: Arch attack vector support David Kaplan
2025-03-10 16:40 ` [PATCH v4 20/36] x86/bugs: Determine relevant vulnerabilities based on attack vector controls David Kaplan
2025-03-12 11:32   ` kernel test robot
2025-03-10 16:40 ` [PATCH v4 21/36] x86/bugs: Add attack vector controls for mds David Kaplan
2025-03-10 16:40 ` [PATCH v4 22/36] x86/bugs: Add attack vector controls for taa David Kaplan
2025-03-10 16:40 ` [PATCH v4 23/36] x86/bugs: Add attack vector controls for mmio David Kaplan
2025-03-10 16:40 ` [PATCH v4 24/36] x86/bugs: Add attack vector controls for rfds David Kaplan
2025-03-10 16:40 ` [PATCH v4 25/36] x86/bugs: Add attack vector controls for srbds David Kaplan
2025-03-10 16:40 ` [PATCH v4 26/36] x86/bugs: Add attack vector controls for gds David Kaplan
2025-03-10 16:40 ` [PATCH v4 27/36] x86/bugs: Add attack vector controls for spectre_v1 David Kaplan
2025-03-10 16:40 ` [PATCH v4 28/36] x86/bugs: Add attack vector controls for retbleed David Kaplan
2025-03-10 16:40 ` [PATCH v4 29/36] x86/bugs: Add attack vector controls for spectre_v2_user David Kaplan
2025-03-10 16:40 ` [PATCH v4 30/36] x86/bugs: Add attack vector controls for bhi David Kaplan
2025-03-10 16:40 ` [PATCH v4 31/36] x86/bugs: Add attack vector controls for spectre_v2 David Kaplan
2025-03-10 16:40 ` [PATCH v4 32/36] x86/bugs: Add attack vector controls for l1tf David Kaplan
2025-03-10 16:40 ` [PATCH v4 33/36] x86/bugs: Add attack vector controls for srso David Kaplan
2025-03-10 16:40 ` [PATCH v4 34/36] x86/pti: Add attack vector controls for pti David Kaplan
2025-03-10 16:40 ` [PATCH v4 35/36] x86/bugs: Print enabled attack vectors David Kaplan
2025-03-10 16:40 ` [PATCH v4 36/36] cpu: Show attack vectors in sysfs David Kaplan
2025-03-10 18:45 ` [PATCH v4 00/36] Attack vector controls Ingo Molnar
2025-03-10 20:46   ` Kaplan, David

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250310164023.779191-19-david.kaplan@amd.com \
    --to=david.kaplan@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=derekmn@amazon.com \
    --cc=hpa@zytor.com \
    --cc=jackmanb@google.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®