kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Radim Krčmář" <rkrcmar@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Nick Desaulniers <nick.desaulniers@gmail.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 v4] KVM: x86: avoid large stack allocations in em_fxrstor
Date: Tue, 30 May 2017 16:05:01 +0200	[thread overview]
Message-ID: <20170530140500.GA22589@potion> (raw)
In-Reply-To: <241100ad-9049-6a4f-078d-c64e0fbba417@redhat.com>

2017-05-30 12:15+0200, Paolo Bonzini:
> On 30/05/2017 00:48, Nick Desaulniers wrote:
>> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
>> @@ -3985,57 +3985,45 @@ static int em_fxsave(struct x86_emulate_ctxt *ctxt)
>>  static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
>>  {
>>  	struct fxregs_state fx_state;
>>  	int rc;
>> +	unsigned int size;
>>  
>>  	rc = check_fxsr(ctxt);
>>  	if (rc != X86EMUL_CONTINUE)
>>  		return rc;
>>  
>> -	rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, 512);
>> -	if (rc != X86EMUL_CONTINUE)
>> -		return rc;
>> +	ctxt->ops->get_fpu(ctxt);
>> +
>> +	if (ctxt->mode < X86EMUL_MODE_PROT64) {
>> +		rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
>> +		if (rc != X86EMUL_CONTINUE)
>> +			return rc;

Please call ctxt->ops->put_fpu() before returning.
(Don't be afraid to use 'goto', the will likely be more of them.)

>> +		/*
>> +		 * Hardware doesn't save and restore XMM 0-7 without
>> +		 * CR4.OSFXSR, but does save and restore MXCSR.
>> +		 */
>> +		if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR)
>> +			size = offsetof(struct fxregs_state, xmm_space[8]);

This should be the size of first 8 XMM registers, but xmm_space is of
type u32, so the correct size is
  xmm_space[8 * 16/sizeof(*fx_state.xmm_space)].

Adding a separate function to compute the size and call it from
em_fxrstor and em_fxsave would make sense at this point.

>> +		else
>> +			size = offsetof(struct fxregs_state, xmm_space[0]);
>> +		rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state,
>> +			size);
>> +		if (rc != X86EMUL_CONTINUE)
>> +			return rc;
>> +	} else if (ctxt->mode == X86EMUL_MODE_PROT64) {
>> +		size = offsetof(struct fxregs_state, xmm_space[16]);
>> +		rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state,
>> +			size);
>> +		if (rc != X86EMUL_CONTINUE)
>> +			return rc;
>> +	}
> 
> You can just remove the "if (ctxt->mode == X86EMUL_MODE_PROT64)".  This
> way, the call of segmented_read_std and the "if (rc !=
> X86EMUL_CONTINUE)" can move outside the conditional.

Good idea.  check_fxsr() doesn't allow X86EMUL_MODE_PROT64 and the
condition was an artefact from older iterations.

I'll refresh the kvm-unit-test ([kvm-unit-tests PATCH] x86: realmode:
add FXSR tests).

  reply	other threads:[~2017-05-30 14:05 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ář [this message]
2017-05-31  3:08                 ` [PATCH v5] " Nick Desaulniers
2017-05-31 11:01                   ` Paolo Bonzini
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=20170530140500.GA22589@potion \
    --to=rkrcmar@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=pbonzini@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 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).