From: Paolo Bonzini <pbonzini@redhat.com>
To: Nick Desaulniers <nick.desaulniers@gmail.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ingo Molnar" <mingo@redhat.com>,
"H. Peter Anvin" <hpa@zytor.com>,
x86@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5] KVM: x86: avoid large stack allocations in em_fxrstor
Date: Wed, 31 May 2017 07:01:29 -0400 (EDT) [thread overview]
Message-ID: <1815926933.3699401.1496228489339.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <20170531030838.23182-1-nick.desaulniers@gmail.com>
> + size = offsetof(struct fxregs_state, xmm_space[16]);
This still has the same issue (it should be multiplied by 4). Here's my
take on it; I checked the compiled code and it's pretty good too (the
compiler knows to do the fxsave if and only if ctxt->mode <
X86EMUL_MODE_PROT64, because that's when the size is smaller):
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 55470ad07c2a..b76f19d2684d 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -3944,10 +3944,19 @@ static int check_fxsr(struct x86_emulate_ctxt *ctxt)
* Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
* and restore MXCSR.
*/
-static size_t xmm_offset(struct x86_emulate_ctxt *ctxt)
+static size_t __fxstate_size(int nregs)
{
- bool b = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
- return offsetof(struct fxregs_state, xmm_space[b ? 8 * 16 / 4 : 0]);
+ return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
+}
+
+static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
+{
+ bool cr4_osfxsr;
+ if (ctxt->mode == X86EMUL_MODE_PROT64)
+ return __fxstate_size(16);
+
+ cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
+ return __fxstate_size(cr4_osfxsr ? 8 : 0);
}
/*
@@ -3987,7 +3996,7 @@ static int em_fxsave(struct x86_emulate_ctxt *ctxt)
return rc;
return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
- xmm_offset(ctxt));
+ fxstate_size(ctxt));
}
static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
@@ -4002,13 +4011,11 @@ static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
ctxt->ops->get_fpu(ctxt);
- if (ctxt->mode < X86EMUL_MODE_PROT64) {
+ size = fxstate_size(ctxt);
+ if (size < __fxstate_size(16)) {
rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
if (rc != X86EMUL_CONTINUE)
goto out;
- size = xmm_offset(ctxt);
- } else {
- size = offsetof(struct fxregs_state, xmm_space[16]);
}
rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
Thanks Nick for the patches and Radim for the reviews!
Paolo
next prev parent reply other threads:[~2017-05-31 11:01 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-24 6:24 [PATCH] KVM: x86: dynamically allocate large struct in em_fxrstor Nick Desaulniers
2017-05-24 14:19 ` Radim Krčmář
2017-05-25 1:36 ` Nick Desaulniers
2017-05-25 14:07 ` Paolo Bonzini
2017-05-26 4:13 ` Nick Desaulniers
2017-05-26 7:18 ` Paolo Bonzini
2017-05-29 19:55 ` [PATCH v2] KVM: x86: avoid large stack allocations " Nick Desaulniers
2017-05-29 20:14 ` kbuild test robot
2017-05-29 20:29 ` Nick Desaulniers
2017-05-29 20:39 ` [PATCH v3] " Nick Desaulniers
2017-05-29 22:40 ` Nick Desaulniers
2017-05-29 22:48 ` [PATCH v4] " Nick Desaulniers
2017-05-30 10:15 ` Paolo Bonzini
2017-05-30 14:05 ` Radim Krčmář
2017-05-31 3:08 ` [PATCH v5] " Nick Desaulniers
2017-05-31 11:01 ` Paolo Bonzini [this message]
2017-06-01 1:05 ` Nick Desaulniers
2017-06-01 7:36 ` Paolo Bonzini
2017-06-02 2:10 ` Nick Desaulniers
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=1815926933.3699401.1496228489339.JavaMail.zimbra@redhat.com \
--to=pbonzini@redhat.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=nick.desaulniers@gmail.com \
--cc=rkrcmar@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.