Skip to contentSkip to conversation

x86/bugs: default spectre_v2_user_cmd depends on Kconfig

PATCH Superseded Breno Leitao 2 participants 8 messages Oct 15, 2024 – Oct 31, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/bugs: default spectre_v2_user_cmd depends on Kconfig

This series contains two patches that improve the handling of Spectre v2 user mitigations in the x86 architecture:

1. Make spectre_v2_user_cmd default depend on Kconfig
2. Simplify spectre_v2_user_select_mitigation() using cpu_smt_possible()

The first patch was originally part of a larger patchset [1] but was removed in version 3 [2] to simplify the revision process. Now that the original patchset has been merged, I am resubmitting this patch separately.

This patch aligns the default configuration of spectre_v2_user_cmd with CONFIG_MITIGATION_SPECTRE_V2, consistent with other user-selectable mitigations.

The other patch is a simple optimization that leverages the cpu_smt_possible() helper function to simplify the code in spectre_v2_user_select_mitigation().

Together, these changes improve code consistency and simplify the configuration and selection of Spectre v2 user mitigations.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Breno Leitao (2):
x86/bugs: Use cpu_smt_possible helper
x86/bugs: spectre user default must depend on MITIGATION_SPECTRE_V2

 arch/x86/kernel/cpu/bugs.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.5
#1 lore raw

PATCH 1/2 x86/bugs: Use cpu_smt_possible helper

There is a helper function to check if SMT is available. Use this helper instead of performing the check manually.

The helper function cpu_smt_possible() does exactly the same thing as was being done manually inside spectre_v2_user_select_mitigation(). Specifically, it returns false if CONFIG_SMP is disabled, otherwise it checks the cpu_smt_control global variable.

This change improves code consistency and reduces duplication.

---
 arch/x86/kernel/cpu/bugs.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
arch/x86/kernel/cpu/bugs.c +1 -6
index d1915427b4ff..6d3b61d7be9c 100644
@@ -1315,16 +1315,11 @@ static void __init
 spectre_v2_user_select_mitigation(void)
 {
 	enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
-	bool smt_possible = IS_ENABLED(CONFIG_SMP);
 	enum spectre_v2_user_cmd cmd;
 
 	if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
 		return;
 
-	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
-	    cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
-		smt_possible = false;
-
 	cmd = spectre_v2_parse_user_cmdline();
 	switch (cmd) {
 	case SPECTRE_V2_USER_CMD_NONE:
@@ -1385,7 +1380,7 @@ spectre_v2_user_select_mitigation(void)
 	 * so allow for STIBP to be selected in those cases.
 	 */
 	if (!boot_cpu_has(X86_FEATURE_STIBP) ||
-	    !smt_possible ||
+	    !cpu_smt_possible() ||
 	    (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
 	     !boot_cpu_has(X86_FEATURE_AUTOIBRS)))
 		return;
Signature · 1 line
2.43.5
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] x86/bugs: Use cpu_smt_possible helper

Quoted 11 lines · Breno Leitao

There is a helper function to check if SMT is available. Use this helper instead of performing the check manually.

The helper function cpu_smt_possible() does exactly the same thing as was being done manually inside spectre_v2_user_select_mitigation(). Specifically, it returns false if CONFIG_SMP is disabled, otherwise it checks the cpu_smt_control global variable.

This change improves code consistency and reduces duplication.

#3 lore raw

PATCH 2/2 x86/bugs: spectre user default must depend on MITIGATION_SPECTRE_V2

In reply to Breno Leitao

Change the default value of spectre v2 in user mode to respect the CONFIG_MITIGATION_SPECTRE_V2 config option.

Currently, user mode spectre v2 is set to auto
(SPECTRE_V2_USER_CMD_AUTO) by default, even if
CONFIG_MITIGATION_SPECTRE_V2 is disabled.

Set the spectre_v2 value to auto (SPECTRE_V2_USER_CMD_AUTO) if the Spectre v2 config (CONFIG_MITIGATION_SPECTRE_V2) is enabled, otherwise set the value to none (SPECTRE_V2_USER_CMD_NONE).

Important to say the command line argument "spectre_v2_user" overwrites the default value in both cases.

---
 arch/x86/kernel/cpu/bugs.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/bugs.c +7 -3
index 6d3b61d7be9c..c21ff072e048 100644
@@ -1277,9 +1277,13 @@ static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
 static enum spectre_v2_user_cmd __init
 spectre_v2_parse_user_cmdline(void)
 {
+	enum spectre_v2_user_cmd mode;
 	char arg[20];
 	int ret, i;
 
+	mode = IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2) ?
+		SPECTRE_V2_USER_CMD_AUTO : SPECTRE_V2_USER_CMD_NONE;
+
 	switch (spectre_v2_cmd) {
 	case SPECTRE_V2_CMD_NONE:
 		return SPECTRE_V2_USER_CMD_NONE;
@@ -1292,7 +1296,7 @@ spectre_v2_parse_user_cmdline(void)
 	ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
 				  arg, sizeof(arg));
 	if (ret < 0)
-		return SPECTRE_V2_USER_CMD_AUTO;
+		return mode;
 
 	for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
 		if (match_option(arg, ret, v2_user_options[i].option)) {
@@ -1302,8 +1306,8 @@ spectre_v2_parse_user_cmdline(void)
 		}
 	}
 
-	pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
-	return SPECTRE_V2_USER_CMD_AUTO;
+	pr_err("Unknown user space protection option (%s). Switching to default\n", arg);
+	return mode;
 }
 
 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
Signature · 1 line
2.43.5
reviewer
#4 lore raw

Re: [PATCH 2/2] x86/bugs: spectre user default must depend on MITIGATION_SPECTRE_V2

Quoted 15 lines · Breno Leitao
arch/x86/kernel/cpu/bugs.c +4 -0
@@ -1277,9 +1277,13 @@ static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
 static enum spectre_v2_user_cmd __init
 spectre_v2_parse_user_cmdline(void)
 {
+	enum spectre_v2_user_cmd mode;
 	char arg[20];
 	int ret, i;
 
+	mode = IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2) ?
+		SPECTRE_V2_USER_CMD_AUTO : SPECTRE_V2_USER_CMD_NONE;
+
 	switch (spectre_v2_cmd) {
 	case SPECTRE_V2_CMD_NONE:
 		return SPECTRE_V2_USER_CMD_NONE;

arch/x86/kernel/cpu/bugs.c spectre_v2_parse_user_cmdline(void) · line 1299

Quoted 5 lines
arch/x86/kernel/cpu/bugs.c +1 -1
@@ -1292,7 +1296,7 @@ spectre_v2_parse_user_cmdline(void)
 	ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
 				  arg, sizeof(arg));
 	if (ret < 0)
-		return SPECTRE_V2_USER_CMD_AUTO;
+		return mode;

This doesn't look right to me, spectre_v2=eibrs|retpoline... will override CONFIG_MITIGATION_SPECTRE_V2=n and enable the kernel mitigation, but the user mitigation will stay disabled. If this is the intention it should be clearly documented that enabling kernel mitigation does not enable user mitigation. And an explicit spectre_v2_user= is required to enable user mitigation.

#5 lore raw

Re: [PATCH 2/2] x86/bugs: spectre user default must depend on MITIGATION_SPECTRE_V2

Hello Pawan,

Quoted 9 lines · Pawan Gupta
Quoted 8 lines · Breno Leitao
arch/x86/kernel/cpu/bugs.c +1 -0
@@ -1277,9 +1277,13 @@ static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
 static enum spectre_v2_user_cmd __init
 spectre_v2_parse_user_cmdline(void)
 {
+	enum spectre_v2_user_cmd mode;
 	char arg[20];
 	int ret, i;
 
Quoted 16 lines

arch/x86/kernel/cpu/bugs.c spectre_v2_parse_user_cmdline(void) · line 1299

Quoted 12 lines
arch/x86/kernel/cpu/bugs.c +4 -1
+	mode = IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2) ?
+		SPECTRE_V2_USER_CMD_AUTO : SPECTRE_V2_USER_CMD_NONE;
+
 	switch (spectre_v2_cmd) {
 	case SPECTRE_V2_CMD_NONE:
 		return SPECTRE_V2_USER_CMD_NONE;
@@ -1292,7 +1296,7 @@ spectre_v2_parse_user_cmdline(void)
 	ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
 				  arg, sizeof(arg));
 	if (ret < 0)
-		return SPECTRE_V2_USER_CMD_AUTO;
+		return mode;

This doesn't look right to me, spectre_v2=eibrs|retpoline... will override CONFIG_MITIGATION_SPECTRE_V2=n and enable the kernel mitigation, but the user mitigation will stay disabled.

Correct. In the current configuration, if the user compiles the kernel with CONFIG_MITIGATION_SPECTRE_V2=n explict, then the kernel will have all spectre v2 mitigations disable by default, and can opt-in using command line arguments.

In other words, if the user compiles the kernel with CONFIG_MITIGATION_SPECTRE_V2=n, then it means all spectre v2 mitigations will be disable. the user wants to opt-in mitigation by mitigation, tho.

For instance, if user sets spectre_v2=eibrs|retpoline in cmdline, then it must not set spectre_v2_user automatically. User can decide to do enable userspace mitigation so as well by passing userspace another command line spectre_v2_user=on|prctl|... I.e, they are independent selectable.

Quoted 4 lines

If this is the intention it should be
clearly documented that enabling kernel mitigation does not enable user mitigation. And an explicit spectre_v2_user= is required to enable user mitigation.

That is fair. I didn't find a place where to document about diferent behavior when CONFIG_MITIGATION_X is disabled. What would you suggest?

Thanks for review and good points.
--breno

reviewer Reviewed-by
#6 lore raw

Re: [PATCH 2/2] x86/bugs: spectre user default must depend on MITIGATION_SPECTRE_V2

Quoted 7 lines · Breno Leitao
Quoted 4 lines

If this is the intention it should be
clearly documented that enabling kernel mitigation does not enable user mitigation. And an explicit spectre_v2_user= is required to enable user mitigation.

That is fair. I didn't find a place where to document about diferent behavior when CONFIG_MITIGATION_X is disabled. What would you suggest?

You could describe the behavior in the commit message and update kernel parameter documentation.

With that:

---

Documentation/admin-guide/kernel-parameters.txt +2 -0
index 1518343bbe22..f8bc02cd10ec 100644
@@ -6241,6 +6241,8 @@
 
 			Selecting 'on' will also enable the mitigation
 			against user space to user space task attacks.
+			Selecting specific mitigation does not force enable
+			user mitigations.
 
 			Selecting 'off' will disable both the kernel and
 			the user space protections.
#7 lore raw

Re: [PATCH 2/2] x86/bugs: spectre user default must depend on MITIGATION_SPECTRE_V2

Hello Pawan,

Quoted 22 lines · Pawan Gupta
Quoted 7 lines · Breno Leitao
Quoted 4 lines

If this is the intention it should be
clearly documented that enabling kernel mitigation does not enable user mitigation. And an explicit spectre_v2_user= is required to enable user mitigation.

That is fair. I didn't find a place where to document about diferent behavior when CONFIG_MITIGATION_X is disabled. What would you suggest?

You could describe the behavior in the commit message and update kernel parameter documentation.

With that:

---

Documentation/admin-guide/kernel-parameters.txt +0 -0
index 1518343bbe22..f8bc02cd10ec 100644

Documentation/admin-guide/kernel-parameters.txt line 6248

Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +2 -0
@@ -6241,6 +6241,8 @@
 
 			Selecting 'on' will also enable the mitigation
 			against user space to user space task attacks.
+			Selecting specific mitigation does not force enable
+			user mitigations.
 
 			Selecting 'off' will disable both the kernel and
 			the user space protections.

Sure, I will update it.

Thanks for the review!
--breno