Skip to contentSkip to conversation

x86/spec: improve command line parsing

PATCH Superseded Roger Pau Monne 2 participants 12 messages Feb 23, 2024 – Feb 26, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86/spec: improve command line parsing

Hello,

A couple of improvements for the spec-ctrl command line parsing, and one bugfix for no_config_param().

Thanks, Roger.

Roger Pau Monne (3):
xen/cmdline: fix printf format specifier in no_config_param() x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled x86/spec: fix INDIRECT_THUNK option to only be set when build-enabled

 xen/arch/x86/spec_ctrl.c | 15 ++++++++++++++-
 xen/include/xen/param.h  |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 xen/cmdline: fix printf format specifier in no_config_param()

'*' sets the width field, which is the minimum number of characters to output, but what we want in no_config_param() is the precision instead, which is '.*' as it imposes a maximum limit on the output.

Fixes 68d757df8dd2 ('x86/pv: Options to disable and/or compile out 32bit PV support')

---
Changes since v1:
- New in this version.

---
 xen/include/xen/param.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/param.h +1 -1
index 9170455cde5c..6442a92aff8e 100644
@@ -191,7 +191,7 @@ static inline void no_config_param(const char *cfg, const char *param,
 {
     int len = e ? ({ ASSERT(e >= s); e - s; }) : strlen(s);
 
-    printk(XENLOG_INFO "CONFIG_%s disabled - ignoring '%s=%*s' setting\n",
+    printk(XENLOG_INFO "CONFIG_%s disabled - ignoring '%s=%.*s' setting\n",
            cfg, param, len, s);
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] xen/cmdline: fix printf format specifier in no_config_param()

Quoted 6 lines · Roger Pau Monne

'*' sets the width field, which is the minimum number of characters to output, but what we want in no_config_param() is the precision instead, which is '.*' as it imposes a maximum limit on the output.

Fixes 68d757df8dd2 ('x86/pv: Options to disable and/or compile out 32bit PV support')
#3 lore raw

PATCH v2 2/3 x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled

In reply to Roger Pau Monne

The current logic to handle the BRANCH_HARDEN option will report it as enabled even when build-time disabled. Fix this by only allowing the option to be set when support for it is built into Xen.

Fixes 2d6f36daa086 ('x86/nospec: Introduce CONFIG_SPECULATIVE_HARDEN_BRANCH')

---
Changes since v1:
- Use no_config_param().

---
 xen/arch/x86/spec_ctrl.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
xen/arch/x86/spec_ctrl.c +9 -1
index 421fe3f640df..4ce8a7a0b8ef 100644
@@ -50,7 +50,8 @@ static int8_t __initdata opt_psfd = -1;
 int8_t __ro_after_init opt_ibpb_ctxt_switch = -1;
 int8_t __read_mostly opt_eager_fpu = -1;
 int8_t __read_mostly opt_l1d_flush = -1;
-static bool __initdata opt_branch_harden = true;
+static bool __initdata opt_branch_harden =
+    IS_ENABLED(CONFIG_SPECULATIVE_HARDEN_BRANCH);
 
 bool __initdata bsp_delay_spec_ctrl;
 uint8_t __read_mostly default_xen_spec_ctrl;
@@ -268,7 +269,14 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
             opt_l1d_flush = val;
         else if ( (val = parse_boolean("branch-harden", s, ss)) >= 0 )
+        {
+#ifdef CONFIG_SPECULATIVE_HARDEN_BRANCH
             opt_branch_harden = val;
+#else
+            no_config_param("SPECULATIVE_HARDEN_BRANCH", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
+        }
         else if ( (val = parse_boolean("srb-lock", s, ss)) >= 0 )
             opt_srb_lock = val;
         else if ( (val = parse_boolean("unpriv-mmio", s, ss)) >= 0 )
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2 2/3] x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled

Quoted 29 lines · Roger Pau Monne

The current logic to handle the BRANCH_HARDEN option will report it as enabled even when build-time disabled. Fix this by only allowing the option to be set when support for it is built into Xen.

Fixes 2d6f36daa086 ('x86/nospec: Introduce CONFIG_SPECULATIVE_HARDEN_BRANCH')

---
Changes since v1:
- Use no_config_param().

---
 xen/arch/x86/spec_ctrl.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
xen/arch/x86/spec_ctrl.c +2 -1
index 421fe3f640df..4ce8a7a0b8ef 100644
@@ -50,7 +50,8 @@ static int8_t __initdata opt_psfd = -1;
 int8_t __ro_after_init opt_ibpb_ctxt_switch = -1;
 int8_t __read_mostly opt_eager_fpu = -1;
 int8_t __read_mostly opt_l1d_flush = -1;
-static bool __initdata opt_branch_harden = true;
+static bool __initdata opt_branch_harden =
+    IS_ENABLED(CONFIG_SPECULATIVE_HARDEN_BRANCH);
 
 bool __initdata bsp_delay_spec_ctrl;
 uint8_t __read_mostly default_xen_spec_ctrl;
@@ -268,7 +269,14 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )

xen/arch/x86/spec_ctrl.c static int __init cf_check parse_spec_ctrl(const char *s) · line 279

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +7 -0
             opt_l1d_flush = val;
         else if ( (val = parse_boolean("branch-harden", s, ss)) >= 0 )
+        {
+#ifdef CONFIG_SPECULATIVE_HARDEN_BRANCH
             opt_branch_harden = val;
+#else
+            no_config_param("SPECULATIVE_HARDEN_BRANCH", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
+        }

If you use #ifdef (rather than IS_ENABLED()) here, the variable probably shouldn't exist at all when the Kconfig option is off (albeit yet, another #ifdef would then be needed higher up in the function). Question is - why don't you use IS_ENABLED() here as well?

Jan

#5 lore raw

Re: [PATCH v2 2/3] x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled

In reply to Roger Pau Monne
Quoted 17 lines · Roger Pau Monne
xen/arch/x86/spec_ctrl.c +3 -1
@@ -50,7 +50,8 @@ static int8_t __initdata opt_psfd = -1;
 int8_t __ro_after_init opt_ibpb_ctxt_switch = -1;
 int8_t __read_mostly opt_eager_fpu = -1;
 int8_t __read_mostly opt_l1d_flush = -1;
-static bool __initdata opt_branch_harden = true;
+static bool __initdata opt_branch_harden =
+    IS_ENABLED(CONFIG_SPECULATIVE_HARDEN_BRANCH);
 
 bool __initdata bsp_delay_spec_ctrl;
 uint8_t __read_mostly default_xen_spec_ctrl;
@@ -268,7 +269,14 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
             opt_l1d_flush = val;
         else if ( (val = parse_boolean("branch-harden", s, ss)) >= 0 )
+        {

xen/arch/x86/spec_ctrl.c static int __init cf_check parse_spec_ctrl(const char *s) · line 282

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +6 -0
+#ifdef CONFIG_SPECULATIVE_HARDEN_BRANCH
             opt_branch_harden = val;
+#else
+            no_config_param("SPECULATIVE_HARDEN_BRANCH", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
+        }
         else if ( (val = parse_boolean("srb-lock", s, ss)) >= 0 )
             opt_srb_lock = val;
         else if ( (val = parse_boolean("unpriv-mmio", s, ss)) >= 0 )

While looking at patch 3 I noticed another inconsistency: Wouldn't "Compiled-in support:" better also enumerate HARDEN_BRANCH then, just like for thunks both CONFIG_* state and actual runtime choice are logged? Or alternatively, should logging of thunk runtime choice be suppressed when the Kconfig setting is off?

Jan

#6 lore raw

Re: [PATCH v2 2/3] x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled

Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monne
xen/arch/x86/spec_ctrl.c +9 -1
@@ -50,7 +50,8 @@ static int8_t __initdata opt_psfd = -1;
 int8_t __ro_after_init opt_ibpb_ctxt_switch = -1;
 int8_t __read_mostly opt_eager_fpu = -1;
 int8_t __read_mostly opt_l1d_flush = -1;
-static bool __initdata opt_branch_harden = true;
+static bool __initdata opt_branch_harden =
+    IS_ENABLED(CONFIG_SPECULATIVE_HARDEN_BRANCH);
 
 bool __initdata bsp_delay_spec_ctrl;
 uint8_t __read_mostly default_xen_spec_ctrl;
@@ -268,7 +269,14 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
             opt_l1d_flush = val;
         else if ( (val = parse_boolean("branch-harden", s, ss)) >= 0 )
+        {
+#ifdef CONFIG_SPECULATIVE_HARDEN_BRANCH
             opt_branch_harden = val;
+#else
+            no_config_param("SPECULATIVE_HARDEN_BRANCH", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
+        }
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static int __init cf_check parse_spec_ctrl(const char *s) · line 282

Quoted 3 lines
xen/arch/x86/spec_ctrl.c +0 -0
         else if ( (val = parse_boolean("srb-lock", s, ss)) >= 0 )
             opt_srb_lock = val;
         else if ( (val = parse_boolean("unpriv-mmio", s, ss)) >= 0 )

While looking at patch 3 I noticed another inconsistency: Wouldn't "Compiled-in support:" better also enumerate HARDEN_BRANCH then, just like for thunks both CONFIG_* state and actual runtime choice are logged?

Yes, I guess we would also need to expand "Compiled-in support:" to include HARDEN_ARRAY and HARDEN_GUEST_ACCESS.

Quoted 2 lines

Or alternatively, should logging of thunk runtime choice be suppressed when the Kconfig setting is off?

Hm, I think printing "BTI-Thunk N/A" is good enough when the thunk has been built time disabled.

Thanks, Roger.

#7 lore raw

Re: [PATCH v2 2/3] x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled

Quoted 35 lines · Roger Pau Monné
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monne
xen/arch/x86/spec_ctrl.c +9 -1
@@ -50,7 +50,8 @@ static int8_t __initdata opt_psfd = -1;
 int8_t __ro_after_init opt_ibpb_ctxt_switch = -1;
 int8_t __read_mostly opt_eager_fpu = -1;
 int8_t __read_mostly opt_l1d_flush = -1;
-static bool __initdata opt_branch_harden = true;
+static bool __initdata opt_branch_harden =
+    IS_ENABLED(CONFIG_SPECULATIVE_HARDEN_BRANCH);
 
 bool __initdata bsp_delay_spec_ctrl;
 uint8_t __read_mostly default_xen_spec_ctrl;
@@ -268,7 +269,14 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
             opt_l1d_flush = val;
         else if ( (val = parse_boolean("branch-harden", s, ss)) >= 0 )
+        {
+#ifdef CONFIG_SPECULATIVE_HARDEN_BRANCH
             opt_branch_harden = val;
+#else
+            no_config_param("SPECULATIVE_HARDEN_BRANCH", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
+        }
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static int __init cf_check parse_spec_ctrl(const char *s) · line 282

Quoted 3 lines
xen/arch/x86/spec_ctrl.c +0 -0
         else if ( (val = parse_boolean("srb-lock", s, ss)) >= 0 )
             opt_srb_lock = val;
         else if ( (val = parse_boolean("unpriv-mmio", s, ss)) >= 0 )

While looking at patch 3 I noticed another inconsistency: Wouldn't "Compiled-in support:" better also enumerate HARDEN_BRANCH then, just like for thunks both CONFIG_* state and actual runtime choice are logged?

Quoted 8 lines

Yes, I guess we would also need to expand "Compiled-in support:" to include HARDEN_ARRAY and HARDEN_GUEST_ACCESS.

Quoted 2 lines

Or alternatively, should logging of thunk runtime choice be suppressed when the Kconfig setting is off?

Hm, I think printing "BTI-Thunk N/A" is good enough when the thunk has been built time disabled.

Good enough - yes. But redundant with the other log line. And since all of this output is getting longer and longer anyway, omitting whatever can sensibly be omitted might be at least worth considering.

Jan

#8 lore raw

Re: [PATCH v2 2/3] x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled

Quoted 42 lines · Jan Beulich
Quoted 41 lines · Roger Pau Monné
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monne
xen/arch/x86/spec_ctrl.c +9 -1
@@ -50,7 +50,8 @@ static int8_t __initdata opt_psfd = -1;
 int8_t __ro_after_init opt_ibpb_ctxt_switch = -1;
 int8_t __read_mostly opt_eager_fpu = -1;
 int8_t __read_mostly opt_l1d_flush = -1;
-static bool __initdata opt_branch_harden = true;
+static bool __initdata opt_branch_harden =
+    IS_ENABLED(CONFIG_SPECULATIVE_HARDEN_BRANCH);
 
 bool __initdata bsp_delay_spec_ctrl;
 uint8_t __read_mostly default_xen_spec_ctrl;
@@ -268,7 +269,14 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
             opt_l1d_flush = val;
         else if ( (val = parse_boolean("branch-harden", s, ss)) >= 0 )
+        {
+#ifdef CONFIG_SPECULATIVE_HARDEN_BRANCH
             opt_branch_harden = val;
+#else
+            no_config_param("SPECULATIVE_HARDEN_BRANCH", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
+        }
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static int __init cf_check parse_spec_ctrl(const char *s) · line 282

Quoted 3 lines
xen/arch/x86/spec_ctrl.c +0 -0
         else if ( (val = parse_boolean("srb-lock", s, ss)) >= 0 )
             opt_srb_lock = val;
         else if ( (val = parse_boolean("unpriv-mmio", s, ss)) >= 0 )

While looking at patch 3 I noticed another inconsistency: Wouldn't "Compiled-in support:" better also enumerate HARDEN_BRANCH then, just like for thunks both CONFIG_* state and actual runtime choice are logged?

Yes, I guess we would also need to expand "Compiled-in support:" to include HARDEN_ARRAY and HARDEN_GUEST_ACCESS.

Quoted 2 lines

Or alternatively, should logging of thunk runtime choice be suppressed when the Kconfig setting is off?

Quoted 6 lines
Quoted 2 lines

Hm, I think printing "BTI-Thunk N/A" is good enough when the thunk has been built time disabled.

Good enough - yes. But redundant with the other log line. And since all of this output is getting longer and longer anyway, omitting whatever can sensibly be omitted might be at least worth considering.

I can add yet another patch to remove that when not built-in, this cleanup is turning into a never-ending exercise I'm afraid.

Thanks, Roger.

#9 lore raw

Re: [PATCH v2 2/3] x86/spec: fix BRANCH_HARDEN option to only be set when build-enabled

Quoted 46 lines · Roger Pau Monné
Quoted 45 lines · Jan Beulich
Quoted 43 lines · Roger Pau Monné
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monne
xen/arch/x86/spec_ctrl.c +9 -1
@@ -50,7 +50,8 @@ static int8_t __initdata opt_psfd = -1;
 int8_t __ro_after_init opt_ibpb_ctxt_switch = -1;
 int8_t __read_mostly opt_eager_fpu = -1;
 int8_t __read_mostly opt_l1d_flush = -1;
-static bool __initdata opt_branch_harden = true;
+static bool __initdata opt_branch_harden =
+    IS_ENABLED(CONFIG_SPECULATIVE_HARDEN_BRANCH);
 
 bool __initdata bsp_delay_spec_ctrl;
 uint8_t __read_mostly default_xen_spec_ctrl;
@@ -268,7 +269,14 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
             opt_l1d_flush = val;
         else if ( (val = parse_boolean("branch-harden", s, ss)) >= 0 )
+        {
+#ifdef CONFIG_SPECULATIVE_HARDEN_BRANCH
             opt_branch_harden = val;
+#else
+            no_config_param("SPECULATIVE_HARDEN_BRANCH", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
+        }
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static int __init cf_check parse_spec_ctrl(const char *s) · line 282

Quoted 3 lines
xen/arch/x86/spec_ctrl.c +0 -0
         else if ( (val = parse_boolean("srb-lock", s, ss)) >= 0 )
             opt_srb_lock = val;
         else if ( (val = parse_boolean("unpriv-mmio", s, ss)) >= 0 )

While looking at patch 3 I noticed another inconsistency: Wouldn't "Compiled-in support:" better also enumerate HARDEN_BRANCH then, just like for thunks both CONFIG_* state and actual runtime choice are logged?

Yes, I guess we would also need to expand "Compiled-in support:" to include HARDEN_ARRAY and HARDEN_GUEST_ACCESS.

Quoted 2 lines

Or alternatively, should logging of thunk runtime choice be suppressed when the Kconfig setting is off?

Hm, I think printing "BTI-Thunk N/A" is good enough when the thunk has been built time disabled.

Quoted 6 lines
Quoted 3 lines

Good enough - yes. But redundant with the other log line. And since all of this output is getting longer and longer anyway, omitting whatever can sensibly be omitted might be at least worth considering.

I can add yet another patch to remove that when not built-in, this cleanup is turning into a never-ending exercise I'm afraid.

Just to be clear: I'm not meaning to insist that you also clean up the thunk part. But I still wanted to mention such as I noticed it.

Jan

#10 lore raw

PATCH v2 3/3 x86/spec: fix INDIRECT_THUNK option to only be set when build-enabled

In reply to Roger Pau Monne

Attempt to provide a more helpful error message when the user attempts to set spec-ctrl=bti-thunk option but the support is build-time disabled.

---
Changes since v1:
- New in this version.

---
 xen/arch/x86/spec_ctrl.c | 5 +++++
 1 file changed, 5 insertions(+)
xen/arch/x86/spec_ctrl.c +5 -0
index 4ce8a7a0b8ef..f3432f1a6c80 100644
@@ -239,6 +239,7 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         /* Xen's speculative sidechannel mitigation settings. */
         else if ( !strncmp(s, "bti-thunk=", 10) )
         {
+#ifdef CONFIG_INDIRECT_THUNK
             s += 10;
 
             if ( !cmdline_strcmp(s, "retpoline") )
@@ -249,6 +250,10 @@ static int __init cf_check parse_spec_ctrl(const char *s)
                 opt_thunk = THUNK_JMP;
             else
                 rc = -EINVAL;
+#else
+            no_config_param("INDIRECT_THUNK", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
         }
 
         /* Bits in MSR_SPEC_CTRL. */
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 3/3] x86/spec: fix INDIRECT_THUNK option to only be set when build-enabled

xen/arch/x86/spec_ctrl.c static int __init cf_check parse_spec_ctrl(const char *s) · line 245

Quoted 10 lines · Roger Pau Monne
xen/arch/x86/spec_ctrl.c +1 -0
@@ -239,6 +239,7 @@ static int __init cf_check parse_spec_ctrl(const char *s)
         /* Xen's speculative sidechannel mitigation settings. */
         else if ( !strncmp(s, "bti-thunk=", 10) )
         {
+#ifdef CONFIG_INDIRECT_THUNK
             s += 10;
 
             if ( !cmdline_strcmp(s, "retpoline") )
              if ( !IS_ENABLED(CONFIG_INDIRECT_THUNK) )
              {
                  no_config_param("INDIRECT_THUNK", "spec-ctrl", s, ss);
                  rc = -EINVAL;
              }
              else if ( !cmdline_strcmp(s, "retpoline") )

?

Also, while touching INDIRECT_THUNK, could I talk you into also adjusting documentation accordingly? It presently mentions INDIRECT_THUNK, when really it means CONFIG_INDIRECT_THUNK.

Jan

Quoted 11 lines
xen/arch/x86/spec_ctrl.c +4 -0
@@ -249,6 +250,10 @@ static int __init cf_check parse_spec_ctrl(const char *s)
                 opt_thunk = THUNK_JMP;
             else
                 rc = -EINVAL;
+#else
+            no_config_param("INDIRECT_THUNK", "spec-ctrl", s, ss);
+            rc = -EINVAL;
+#endif
         }
 
         /* Bits in MSR_SPEC_CTRL. */