libx86: Elide more empty CPUID leaves when serialising a policy
authorAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 21 May 2019 17:19:33 +0000 (18:19 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 23 May 2019 14:54:06 +0000 (15:54 +0100)
x86_cpuid_copy_to_buffer() currently serialises the full content of the
various subleaf unions.  While leaves 4, 0xb and 0xd don't have a concrete
max_subleaf field, they do have well defined upper bounds.

Diffing the results of `xen-cpuid -p` shows the resulting saving:

  @@ -1,5 +1,5 @@
   Xen reports there are maximum 114 leaves and 1 MSRs
  -Raw policy: 93 leaves, 1 MSRs
  +Raw policy: 38 leaves, 1 MSRs
    CPUID:
     leaf     subleaf  -> eax      ebx      ecx      edx
     00000000:ffffffff -> 00000016:756e6547:6c65746e:49656e69
  @@ -32,7 +32,7 @@ Raw policy: 93 leaves, 1 MSRs
    MSRs:
     index    -> value
     000000ce -> 0000000080000000
  -Host policy: 93 leaves, 1 MSRs
  +Host policy: 33 leaves, 1 MSRs
    CPUID:
     leaf     subleaf  -> eax      ebx      ecx      edx
     00000000:ffffffff -> 0000000d:756e6547:6c65746e:49656e69

which is mostly due to no longer writing out 64 leaves for xstate when (on
this CoffeeLake system) 8 will do.

Extend the unit tests to cover empty and partially filled subleaf unions.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
tools/tests/cpu-policy/test-cpu-policy.c
xen/lib/x86/cpuid.c

index beced5e9a60a58034e6c67b60708b7af0fad7fc6..fd96c0b896fd1fbb73c1c2e0eeb5a4f7a6248559 100644 (file)
@@ -65,6 +65,77 @@ static void test_cpuid_serialise_success(void)
             .name = "empty policy",
             .nr_leaves = 4,
         },
+
+        /* Leaf 4 serialisation stops at the first subleaf with type 0. */
+        {
+            .name = "empty leaf 4",
+            .p = {
+                .basic.max_leaf = 4,
+            },
+            .nr_leaves = 4 + 4,
+        },
+        {
+            .name = "partial leaf 4",
+            .p = {
+                .basic.max_leaf = 4,
+                .cache.subleaf[0].type = 1,
+            },
+            .nr_leaves = 4 + 4 + 1,
+        },
+
+        /* Leaf 7 serialisation stops at max_subleaf. */
+        {
+            .name = "empty leaf 7",
+            .p = {
+                .basic.max_leaf = 7,
+            },
+            .nr_leaves = 4 + 7,
+        },
+        {
+            .name = "partial leaf 7",
+            .p = {
+                .basic.max_leaf = 7,
+                .feat.max_subleaf = 1,
+            },
+            .nr_leaves = 4 + 7 + 1,
+        },
+
+        /* Leaf 0xb serialisation stops at the first subleaf with type 0. */
+        {
+            .name = "empty leaf 0xb",
+            .p = {
+                .basic.max_leaf = 0xb,
+            },
+            .nr_leaves = 4 + 0xb,
+        },
+        {
+            .name = "partial leaf 0xb",
+            .p = {
+                .basic.max_leaf = 0xb,
+                .topo.subleaf[0].type = 1,
+            },
+            .nr_leaves = 4 + 0xb + 1,
+        },
+
+        /*
+         * Leaf 0xd serialisation automatically has two leaves, and stops the
+         * highest bit set in {xcr0,xss}_{high,low}.
+         */
+        {
+            .name = "empty leaf 0xd",
+            .p = {
+                .basic.max_leaf = 0xd,
+            },
+            .nr_leaves = 4 + 0xd + 1,
+        },
+        {
+            .name = "partial 0xd",
+            .p = {
+                .basic.max_leaf = 0xd,
+                .xstate.xcr0_low = 7,
+            },
+            .nr_leaves = 4 + 0xd + 1 + 1,
+        },
     };
 
     printf("Testing CPUID serialise success:\n");
index 74c5b18ac780de5017e2f94991650e33e72e49d2..a82cdb27aa226b044869861812acfd1fc760a0fa 100644 (file)
@@ -241,7 +241,12 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,
         {
         case 0x4:
             for ( subleaf = 0; subleaf < ARRAY_SIZE(p->cache.raw); ++subleaf )
+            {
                 COPY_LEAF(leaf, subleaf, &p->cache.raw[subleaf]);
+
+                if ( p->cache.subleaf[subleaf].type == 0 )
+                    break;
+            }
             break;
 
         case 0x7:
@@ -253,13 +258,27 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,
 
         case 0xb:
             for ( subleaf = 0; subleaf < ARRAY_SIZE(p->topo.raw); ++subleaf )
+            {
                 COPY_LEAF(leaf, subleaf, &p->topo.raw[subleaf]);
+
+                if ( p->topo.subleaf[subleaf].type == 0 )
+                    break;
+            }
             break;
 
         case 0xd:
-            for ( subleaf = 0; subleaf < ARRAY_SIZE(p->xstate.raw); ++subleaf )
+        {
+            uint64_t xstates = cpuid_policy_xstates(p);
+
+            COPY_LEAF(leaf, 0, &p->xstate.raw[0]);
+            COPY_LEAF(leaf, 1, &p->xstate.raw[1]);
+
+            for ( xstates >>= 2, subleaf = 2;
+                  xstates && subleaf < ARRAY_SIZE(p->xstate.raw);
+                  xstates >>= 1, ++subleaf )
                 COPY_LEAF(leaf, subleaf, &p->xstate.raw[subleaf]);
             break;
+        }
 
         default:
             COPY_LEAF(leaf, XEN_CPUID_NO_SUBLEAF, &p->basic.raw[leaf]);