From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Jan Beulich" <JBeulich@suse.com>,
"Roger Pau Monné" <roger.pau@citrix.com>, "Wei Liu" <wl@xen.org>
Subject: [PATCH 1/5] x86/xstate: Elide redundant writes in set_xcr0()
Date: Mon, 3 May 2021 16:39:34 +0100 [thread overview]
Message-ID: <20210503153938.14109-2-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <20210503153938.14109-1-andrew.cooper3@citrix.com>
XSETBV is an expensive instruction as, amongst other things, it involves
reconfiguring the instruction decode at the frontend of the pipeline.
We have several paths which reconfigure %xcr0 in quick succession (the context
switch path has 5, including the fpu save/restore helpers), and only a single
caller takes any care to try to skip redundant writes.
Update set_xcr0() to perform amortisation automatically, and simplify the
__context_switch() path as a consequence.
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
CC: Wei Liu <wl@xen.org>
---
xen/arch/x86/domain.c | 4 +---
xen/arch/x86/xstate.c | 15 +++++++++++----
2 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index 4dc27f798e..50a27197b5 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -1977,9 +1977,7 @@ static void __context_switch(void)
memcpy(stack_regs, &n->arch.user_regs, CTXT_SWITCH_STACK_BYTES);
if ( cpu_has_xsave )
{
- u64 xcr0 = n->arch.xcr0 ?: XSTATE_FP_SSE;
-
- if ( xcr0 != get_xcr0() && !set_xcr0(xcr0) )
+ if ( !set_xcr0(n->arch.xcr0 ?: XSTATE_FP_SSE) )
BUG();
if ( cpu_has_xsaves && is_hvm_vcpu(n) )
diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c
index 3794d9a5a5..f82dae8053 100644
--- a/xen/arch/x86/xstate.c
+++ b/xen/arch/x86/xstate.c
@@ -55,11 +55,18 @@ static inline bool xsetbv(u32 index, u64 xfeatures)
return lo != 0;
}
-bool set_xcr0(u64 xfeatures)
+bool set_xcr0(u64 val)
{
- if ( !xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures) )
- return false;
- this_cpu(xcr0) = xfeatures;
+ uint64_t *this_xcr0 = &this_cpu(xcr0);
+
+ if ( *this_xcr0 != val )
+ {
+ if ( !xsetbv(XCR_XFEATURE_ENABLED_MASK, val) )
+ return false;
+
+ *this_xcr0 = val;
+ }
+
return true;
}
--
2.11.0
next prev parent reply other threads:[~2021-05-03 15:40 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-03 15:39 [PATCH 0/5] x86/xstate: Fixes to size calculations Andrew Cooper
2021-05-03 15:39 ` Andrew Cooper [this message]
2021-05-04 11:51 ` [PATCH 1/5] x86/xstate: Elide redundant writes in set_xcr0() Jan Beulich
2021-05-03 15:39 ` [PATCH 2/5] x86/xstate: Rename _xstate_ctxt_size() to hw_uncompressed_size() Andrew Cooper
2021-05-04 11:53 ` Jan Beulich
2021-05-03 15:39 ` [PATCH 3/5] x86/xstate: Rework xstate_ctxt_size() as xstate_uncompressed_size() Andrew Cooper
2021-05-03 18:17 ` Andrew Cooper
2021-05-04 12:08 ` Jan Beulich
2021-05-04 12:15 ` Andrew Cooper
2021-05-04 12:20 ` Jan Beulich
2021-05-04 12:22 ` Andrew Cooper
2021-05-04 12:45 ` Jan Beulich
2021-05-03 15:39 ` [PATCH 4/5] x86/cpuid: Simplify recalculate_xstate() Andrew Cooper
2021-05-04 12:43 ` Jan Beulich
2021-05-04 13:58 ` Andrew Cooper
2021-05-05 8:19 ` Jan Beulich
2021-05-05 14:53 ` Andrew Cooper
2021-05-05 15:14 ` Jan Beulich
2021-05-03 15:39 ` [PATCH 5/5] x86/cpuid: Fix handling of xsave dynamic leaves Andrew Cooper
2021-05-04 12:56 ` Jan Beulich
2021-05-04 14:17 ` Andrew Cooper
2021-05-05 8:33 ` Jan Beulich
2021-05-05 16:59 ` Andrew Cooper
2021-05-06 6:17 ` 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=20210503153938.14109-2-andrew.cooper3@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=JBeulich@suse.com \
--cc=roger.pau@citrix.com \
--cc=wl@xen.org \
--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).