xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Wei Liu" <wei.liu2@citrix.com>,
	"Jan Beulich" <JBeulich@suse.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Subject: [PATCH] libx86: Elide more empty CPUID leaves when serialising a policy
Date: Wed, 22 May 2019 16:50:30 +0100	[thread overview]
Message-ID: <1558540230-26612-1-git-send-email-andrew.cooper3@citrix.com> (raw)

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 resutling 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>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Wei Liu <wei.liu2@citrix.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
---
 tools/tests/cpu-policy/test-cpu-policy.c | 71 ++++++++++++++++++++++++++++++++
 xen/lib/x86/cpuid.c                      | 24 ++++++++++-
 2 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/tools/tests/cpu-policy/test-cpu-policy.c b/tools/tests/cpu-policy/test-cpu-policy.c
index beced5e..fd96c0b 100644
--- a/tools/tests/cpu-policy/test-cpu-policy.c
+++ b/tools/tests/cpu-policy/test-cpu-policy.c
@@ -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");
diff --git a/xen/lib/x86/cpuid.c b/xen/lib/x86/cpuid.c
index 23619c7..dcab1e7 100644
--- a/xen/lib/x86/cpuid.c
+++ b/xen/lib/x86/cpuid.c
@@ -242,7 +242,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:
@@ -254,13 +259,30 @@ 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;
+
+            COPY_LEAF(leaf, 0, &p->xstate.raw[0]);
+            COPY_LEAF(leaf, 1, &p->xstate.raw[1]);
+
+            xstates  = ((uint64_t)(p->xstate.xcr0_high | p->xstate.xss_high) << 32);
+            xstates |=            (p->xstate.xcr0_low  | p->xstate.xss_low);
+
+            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]);
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

WARNING: multiple messages have this Message-ID (diff)
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Wei Liu" <wei.liu2@citrix.com>,
	"Jan Beulich" <JBeulich@suse.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Subject: [Xen-devel] [PATCH] libx86: Elide more empty CPUID leaves when serialising a policy
Date: Wed, 22 May 2019 16:50:30 +0100	[thread overview]
Message-ID: <1558540230-26612-1-git-send-email-andrew.cooper3@citrix.com> (raw)
Message-ID: <20190522155030.m6W1GeugvjnR9jBZfhGkl8hOlySIMYz6wmkbeFAgMgw@z> (raw)

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 resutling 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>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Wei Liu <wei.liu2@citrix.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
---
 tools/tests/cpu-policy/test-cpu-policy.c | 71 ++++++++++++++++++++++++++++++++
 xen/lib/x86/cpuid.c                      | 24 ++++++++++-
 2 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/tools/tests/cpu-policy/test-cpu-policy.c b/tools/tests/cpu-policy/test-cpu-policy.c
index beced5e..fd96c0b 100644
--- a/tools/tests/cpu-policy/test-cpu-policy.c
+++ b/tools/tests/cpu-policy/test-cpu-policy.c
@@ -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");
diff --git a/xen/lib/x86/cpuid.c b/xen/lib/x86/cpuid.c
index 23619c7..dcab1e7 100644
--- a/xen/lib/x86/cpuid.c
+++ b/xen/lib/x86/cpuid.c
@@ -242,7 +242,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:
@@ -254,13 +259,30 @@ 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;
+
+            COPY_LEAF(leaf, 0, &p->xstate.raw[0]);
+            COPY_LEAF(leaf, 1, &p->xstate.raw[1]);
+
+            xstates  = ((uint64_t)(p->xstate.xcr0_high | p->xstate.xss_high) << 32);
+            xstates |=            (p->xstate.xcr0_low  | p->xstate.xss_low);
+
+            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]);
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

             reply	other threads:[~2019-05-22 15:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-22 15:50 Andrew Cooper [this message]
2019-05-22 15:50 ` [Xen-devel] [PATCH] libx86: Elide more empty CPUID leaves when serialising a policy Andrew Cooper
2019-05-23  8:33 ` Jan Beulich
2019-05-23  8:33   ` [Xen-devel] " Jan Beulich
2019-05-23 10:38   ` Andrew Cooper
2019-05-23 10:38     ` [Xen-devel] " Andrew Cooper
2019-05-23 10:27 ` [PATCH] libx86: Introduce wrappers for extracting XCR0/XSS from a cpuid policy Andrew Cooper
2019-05-23 10:27   ` [Xen-devel] " Andrew Cooper
2019-05-23 11:52   ` Jan Beulich
2019-05-23 11:52     ` [Xen-devel] " Jan Beulich
2019-05-23 11:59     ` Andrew Cooper
2019-05-23 11:59       ` [Xen-devel] " Andrew Cooper
2019-05-23 12:08       ` Jan Beulich
2019-05-23 12:08         ` [Xen-devel] " Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1558540230-26612-1-git-send-email-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=roger.pau@citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).