[RFC PATCH 51/56] cpu: Parse string of mitigation options

David Kaplan
2025-10-13T14:36:25Z

Support runtime parsing of a cmdline-like string of mitigation options.
The global 'mitigations' parameter is processed in generic code and all
other parsing is done via the arch-specific function.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
include/linux/cpu.h | 4 ++++
kernel/cpu.c | 32 ++++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+)

diff --git a/include/linux/cpu.h b/include/linux/cpu.h
index 3da629a76a49..6f69344465f1 100644
--- a/include/linux/cpu.h
+++ b/include/linux/cpu.h
@@ -210,6 +210,10 @@ enum smt_mitigations {

void cpu_reset_mitigations(void);
void arch_cpu_reset_mitigations(void);
+bool cpu_is_mitigation_opt(char *param);
+bool arch_is_mitigation_opt(char *param);
+int cpu_parse_mitigation_options(const char *str);
+int arch_parse_mitigation_opt(char *param, char *val);

#ifdef CONFIG_CPU_MITIGATIONS
extern bool cpu_mitigations_off(void);
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 33289405af30..942a200398c9 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -3343,4 +3343,36 @@ void cpu_reset_mitigations(void)
attack_vectors[CPU_MITIGATE_GUEST_GUEST] = IS_ENABLED(CONFIG_KVM);
arch_cpu_reset_mitigations();
}
+
+int __weak arch_parse_mitigation_opt(char *param, char *val) { return 0; }
+bool __weak arch_is_mitigation_opt(char *param) { return false; }
+
+bool cpu_is_mitigation_opt(char *param)
+{
+ if (parameq(param, "mitigations"))
+ return true;
+ else
+ return arch_is_mitigation_opt(param);
+}
+
+static int __cpu_parse_mitigation_options(char *param, char *val,
+ const char *unused, void *arg)
+{
+ if (parameq(param, "mitigations"))
+ return mitigations_parse_cmdline(val);
+ else
+ return arch_parse_mitigation_opt(param, val);
+}
+
+int cpu_parse_mitigation_options(const char *str)
+{
+ char *tmpstr;
+
+ /* Copy the provided string because parse_args will mangle it. */
+ tmpstr = kstrdup(str, GFP_KERNEL);
+ parse_args("dynamic mitigations", tmpstr, NULL, 0, 0, 0, NULL,
+ __cpu_parse_mitigation_options);
+ kfree(tmpstr);
+ return 0;
+}
#endif
--
2.34.1