Skip to contentSkip to conversation

x86/cpuid: Extend the cpuid= option to support all named features

PATCH In review Andrew Cooper 2 participants 7 messages Sep 5, 2019 – Sep 25, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/cpuid: Extend the cpuid= option to support all named features

For gen-cpuid.py, fix a comment describing self.names, and generate the reverse mapping in self.values. Write out INIT_FEATURE_NAMES which maps a string name to a bit position.

For parse_cpuid(), use cmdline_strcmp() and perform a binary search over INIT_FEATURE_NAMES. A tweak to cmdline_strcmp() is needed to break at equals signs as well.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* Rebase over cmdline_strcmp()

---
 xen/arch/x86/cpuid.c   | 75 +++++++++++++++++++++++++++++++-------------------
 xen/common/kernel.c    |  6 ++--
 xen/include/xen/lib.h  |  4 +--
 xen/tools/gen-cpuid.py | 22 +++++++++++++--
 4 files changed, 71 insertions(+), 36 deletions(-)
xen/arch/x86/cpuid.c +46 -29
index ab1a48ff90..040c087689 100644
@@ -21,45 +21,62 @@ static const uint32_t deep_features[] = INIT_DEEP_FEATURES;
 
 static int __init parse_xen_cpuid(const char *s)
 {
+    static const struct feature {
+        const char *name;
+        unsigned int bit;
+    } features[] __initconst = INIT_FEATURE_NAMES, *lhs, *mid, *rhs;
     const char *ss;
     int val, rc = 0;
 
     do {
+        const char *feat;
+
         ss = strchr(s, ',');
         if ( !ss )
             ss = strchr(s, '\0');
 
-        if ( (val = parse_boolean("md-clear", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_MD_CLEAR);
-        }
-        else if ( (val = parse_boolean("ibpb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBPB);
-        }
-        else if ( (val = parse_boolean("ibrsb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBRSB);
-        }
-        else if ( (val = parse_boolean("stibp", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_STIBP);
-        }
-        else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_L1D_FLUSH);
-        }
-        else if ( (val = parse_boolean("ssbd", s, ss)) >= 0 )
+        /* Skip the 'no-' prefix for name comparisons. */
+        feat = s;
+        if ( strncmp(s, "no-", 3) == 0 )
+            feat += 3;
+
+        /* (Re)initalise lhs and rhs for binary search. */
+        lhs = features;
+        rhs = features + ARRAY_SIZE(features);
+
+        while ( lhs < rhs )
         {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_SSBD);
+            int res;
+
+            mid = lhs + (rhs - lhs) / 2;
+            res = cmdline_strcmp(feat, mid->name);
+
+            if ( res < 0 )
+            {
+                rhs = mid;
+                continue;
+            }
+            if ( res > 0 )
+            {
+                lhs = mid + 1;
+                continue;
+            }
+
+            if ( (val = parse_boolean(mid->name, s, ss)) >= 0 )
+            {
+                if ( !val )
+                    setup_clear_cpu_cap(mid->bit);
+                mid = NULL;
+            }
+
+            break;
         }
-        else
+
+        /*
+         * Mid being NULL means that the name and boolean were successfully
+         * identified.  Everything else is an error.
+         */
+        if ( mid )
             rc = -EINVAL;
 
         s = ss + 1;
xen/common/kernel.c +3 -3
index f7628d73ce..760917dab5 100644
@@ -309,10 +309,10 @@ int cmdline_strcmp(const char *frag, const char *name)
         if ( res || n == '\0' )
         {
             /*
-             * NUL in 'name' matching a comma, colon or semicolon in 'frag'
-             * implies success.
+             * NUL in 'name' matching a comma, colon, semicolon or equals in
+             * 'frag' implies success.
              */
-            if ( n == '\0' && (f == ',' || f == ':' || f == ';') )
+            if ( n == '\0' && (f == ',' || f == ':' || f == ';' || f == '=') )
                 res = 0;
 
             return res;
xen/include/xen/lib.h +2 -2
index ce231c5f4f..8fbe84032d 100644
@@ -85,8 +85,8 @@ int parse_boolean(const char *name, const char *s, const char *e);
 
 /**
  * Very similar to strcmp(), but will declare a match if the NUL in 'name'
- * lines up with comma, colon or semicolon in 'frag'.  Designed for picking
- * exact string matches out of a delimited command line list.
+ * lines up with comma, colon, semicolon or equals in 'frag'.  Designed for
+ * picking exact string matches out of a delimited command line list.
  */
 int cmdline_strcmp(const char *frag, const char *name);
 
xen/tools/gen-cpuid.py +20 -2
index 836b010751..f76e80d690 100755
@@ -19,7 +19,8 @@ def __init__(self, input, output):
         self.output = open_file_or_fd(output, "w", 2)
 
         # State parsed from input
-        self.names = {} # Name => value mapping
+        self.names = {}  # Value => Name mapping
+        self.values = {} # Name => Value mapping
         self.raw_special = set()
         self.raw_pv = set()
         self.raw_hvm_shadow = set()
@@ -76,8 +77,9 @@ def parse_definitions(state):
             this_name = name
         setattr(this, this_name, val)
 
-        # Construct a reverse mapping of value to name
+        # Construct forward and reverse mappings between name and value
         state.names[val] = name
+        state.values[name.lower().replace("_", "-")] = val
 
         for a in attr:
 
@@ -403,6 +405,22 @@ def write_results(state):
     state.output.write(
 """}
 
+#define INIT_FEATURE_NAMES { \\
+""")
+
+    try:
+        _tmp = state.values.iteritems()
+    except AttributeError:
+        _tmp = state.values.items()
+
+    for name, bit in sorted(_tmp):
+        state.output.write(
+            '    { "%s", %sU },\\\n' % (name, bit)
+            )
+
+    state.output.write(
+"""}
+
 """)
 
     for idx, text in enumerate(state.bitfields):
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

xen/arch/x86/cpuid.c static const uint32_t deep_features[] = INIT_DEEP_FEATURES; · line 27

Quoted 10 lines · Andrew Cooper
xen/arch/x86/cpuid.c +4 -0
@@ -21,45 +21,62 @@ static const uint32_t deep_features[] = INIT_DEEP_FEATURES;
 
 static int __init parse_xen_cpuid(const char *s)
 {
+    static const struct feature {
+        const char *name;
+        unsigned int bit;
+    } features[] __initconst = INIT_FEATURE_NAMES, *lhs, *mid, *rhs;

The pointer field want this to use __initconstrel. And I don't think you mean lhs, mid, and rhs to also be static? Albeit ...

Quoted 34 lines
xen/arch/x86/cpuid.c +2 -24
     const char *ss;
     int val, rc = 0;
 
     do {
+        const char *feat;
+
         ss = strchr(s, ',');
         if ( !ss )
             ss = strchr(s, '\0');
 
-        if ( (val = parse_boolean("md-clear", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_MD_CLEAR);
-        }
-        else if ( (val = parse_boolean("ibpb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBPB);
-        }
-        else if ( (val = parse_boolean("ibrsb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBRSB);
-        }
-        else if ( (val = parse_boolean("stibp", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_STIBP);
-        }
-        else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_L1D_FLUSH);

xen/arch/x86/cpuid.c static const uint32_t deep_features[] = INIT_DEEP_FEATURES; · line 45

Quoted 10 lines
xen/arch/x86/cpuid.c +8 -2
-        }
-        else if ( (val = parse_boolean("ssbd", s, ss)) >= 0 )
+        /* Skip the 'no-' prefix for name comparisons. */
+        feat = s;
+        if ( strncmp(s, "no-", 3) == 0 )
+            feat += 3;
+
+        /* (Re)initalise lhs and rhs for binary search. */
+        lhs = features;
+        rhs = features + ARRAY_SIZE(features);

... the comment here suggests you do, but I don't currently see why. I'd like to understand this though before giving an ack.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
xen/arch/x86/cpuid.c +0 -0
@@ -21,45 +21,62 @@ static const uint32_t deep_features[] = INIT_DEEP_FEATURES;
 
Quoted 7 lines

xen/arch/x86/cpuid.c static const uint32_t deep_features[] = INIT_DEEP_FEATURES; · line 27

Quoted 6 lines
xen/arch/x86/cpuid.c +4 -0
 static int __init parse_xen_cpuid(const char *s)
 {
+    static const struct feature {
+        const char *name;
+        unsigned int bit;
+    } features[] __initconst = INIT_FEATURE_NAMES, *lhs, *mid, *rhs;

The pointer field want this to use __initconstrel.

Ok.

Quoted 1 line

And I don't think you mean lhs, mid, and rhs to also be static?

No - not intentional.

Quoted 39 lines

Albeit ...

Quoted 37 lines
xen/arch/x86/cpuid.c +3 -26
     const char *ss;
     int val, rc = 0;
 
     do {
+        const char *feat;
+
         ss = strchr(s, ',');
         if ( !ss )
             ss = strchr(s, '\0');
 
-        if ( (val = parse_boolean("md-clear", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_MD_CLEAR);
-        }
-        else if ( (val = parse_boolean("ibpb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBPB);
-        }
-        else if ( (val = parse_boolean("ibrsb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBRSB);
-        }
-        else if ( (val = parse_boolean("stibp", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_STIBP);
-        }
-        else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_L1D_FLUSH);
-        }
-        else if ( (val = parse_boolean("ssbd", s, ss)) >= 0 )
+        /* Skip the 'no-' prefix for name comparisons. */
Quoted 8 lines

xen/arch/x86/cpuid.c static const uint32_t deep_features[] = INIT_DEEP_FEATURES; · line 45

Quoted 7 lines
xen/arch/x86/cpuid.c +7 -0
+        feat = s;
+        if ( strncmp(s, "no-", 3) == 0 )
+            feat += 3;
+
+        /* (Re)initalise lhs and rhs for binary search. */
+        lhs = features;
+        rhs = features + ARRAY_SIZE(features);

... the comment here suggests you do, but I don't currently see why.

We are inside a do { } () while loop, parsing something such as cpuid=avx512,ss,smx

The binary search over the feature names needs to start again from scratch for each new cpuid= list item.

Otherwise, the while ( lhs < rhs ) binary search will never be entered for the second cpuid= item.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#3 lore raw
Quoted 68 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
xen/arch/x86/cpuid.c +0 -0
@@ -21,45 +21,62 @@ static const uint32_t deep_features[] = INIT_DEEP_FEATURES;
 
Quoted 7 lines

xen/arch/x86/cpuid.c static const uint32_t deep_features[] = INIT_DEEP_FEATURES; · line 27

Quoted 6 lines
xen/arch/x86/cpuid.c +4 -0
 static int __init parse_xen_cpuid(const char *s)
 {
+    static const struct feature {
+        const char *name;
+        unsigned int bit;
+    } features[] __initconst = INIT_FEATURE_NAMES, *lhs, *mid, *rhs;

The pointer field want this to use __initconstrel.

Ok.

Quoted 1 line

And I don't think you mean lhs, mid, and rhs to also be static?

No - not intentional.

Quoted 39 lines

Albeit ...

Quoted 37 lines
xen/arch/x86/cpuid.c +3 -26
     const char *ss;
     int val, rc = 0;
 
     do {
+        const char *feat;
+
         ss = strchr(s, ',');
         if ( !ss )
             ss = strchr(s, '\0');
 
-        if ( (val = parse_boolean("md-clear", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_MD_CLEAR);
-        }
-        else if ( (val = parse_boolean("ibpb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBPB);
-        }
-        else if ( (val = parse_boolean("ibrsb", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_IBRSB);
-        }
-        else if ( (val = parse_boolean("stibp", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_STIBP);
-        }
-        else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 )
-        {
-            if ( !val )
-                setup_clear_cpu_cap(X86_FEATURE_L1D_FLUSH);
-        }
-        else if ( (val = parse_boolean("ssbd", s, ss)) >= 0 )
+        /* Skip the 'no-' prefix for name comparisons. */
Quoted 8 lines

xen/arch/x86/cpuid.c static const uint32_t deep_features[] = INIT_DEEP_FEATURES; · line 45

Quoted 7 lines
xen/arch/x86/cpuid.c +7 -0
+        feat = s;
+        if ( strncmp(s, "no-", 3) == 0 )
+            feat += 3;
+
+        /* (Re)initalise lhs and rhs for binary search. */
+        lhs = features;
+        rhs = features + ARRAY_SIZE(features);

... the comment here suggests you do, but I don't currently see why.

Quoted 8 lines

We are inside a do { } () while loop, parsing something such as cpuid=avx512,ss,smx

The binary search over the feature names needs to start again from scratch for each new cpuid= list item.

Otherwise, the while ( lhs < rhs ) binary search will never be entered for the second cpuid= item.

In which case, why don't you move the three variables into the do/while scope? Anyway, with the annotation correction and the variables non- static (in whichever scope)

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Andrew Cooper
Quoted 14 lines · Andrew Cooper

For gen-cpuid.py, fix a comment describing self.names, and generate the reverse mapping in self.values. Write out INIT_FEATURE_NAMES which maps a string name to a bit position.

For parse_cpuid(), use cmdline_strcmp() and perform a binary search over INIT_FEATURE_NAMES. A tweak to cmdline_strcmp() is needed to break at equals signs as well.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

Quoted 8 lines

v2:
* Rebase over cmdline_strcmp()

---
 xen/arch/x86/cpuid.c   | 75 +++++++++++++++++++++++++++++++-------------------
 xen/common/kernel.c    |  6 ++--
 xen/include/xen/lib.h  |  4 +--
 xen/tools/gen-cpuid.py | 22 +++++++++++++--
 4 files changed, 71 insertions(+), 36 deletions(-)

I've noticed this only now: This change would have wanted to be accompanied by an adjustment to the command line doc.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 23 lines · Jan Beulich
Quoted 14 lines · Andrew Cooper

For gen-cpuid.py, fix a comment describing self.names, and generate the reverse mapping in self.values. Write out INIT_FEATURE_NAMES which maps a string name to a bit position.

For parse_cpuid(), use cmdline_strcmp() and perform a binary search over INIT_FEATURE_NAMES. A tweak to cmdline_strcmp() is needed to break at equals signs as well.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

Quoted 8 lines

v2:
* Rebase over cmdline_strcmp()

---
 xen/arch/x86/cpuid.c   | 75 +++++++++++++++++++++++++++++++-------------------
 xen/common/kernel.c    |  6 ++--
 xen/include/xen/lib.h  |  4 +--
 xen/tools/gen-cpuid.py | 22 +++++++++++++--
 4 files changed, 71 insertions(+), 36 deletions(-)
Quoted 2 lines

I've noticed this only now: This change would have wanted to be accompanied by an adjustment to the command line doc.

I debated that, but I don't want to give people the idea that using cpuid=no-lm might be a sensible thing to do.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrew Cooper

For gen-cpuid.py, fix a comment describing self.names, and generate the reverse mapping in self.values. Write out INIT_FEATURE_NAMES which maps a string name to a bit position.

For parse_cpuid(), use cmdline_strcmp() and perform a binary search over INIT_FEATURE_NAMES. A tweak to cmdline_strcmp() is needed to break at equals signs as well.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* Rebase over cmdline_strcmp()

---
 xen/arch/x86/cpuid.c   | 75 +++++++++++++++++++++++++++++++-------------------
 xen/common/kernel.c    |  6 ++--
 xen/include/xen/lib.h  |  4 +--
 xen/tools/gen-cpuid.py | 22 +++++++++++++--
 4 files changed, 71 insertions(+), 36 deletions(-)
Quoted 5 lines
Quoted 2 lines

I've noticed this only now: This change would have wanted to be accompanied by an adjustment to the command line doc.

I debated that, but I don't want to give people the idea that using cpuid=no-lm might be a sensible thing to do.

Well, this is a particularly bad example. There are surely more sensible forms of this option now which weren't previously supported, and which aren't mentioned by the existing doc text. The example you give suggests that we also want to have a "minimal" policy, disabling of elements of which we should reject when given to "cpuid=".

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel