Re: [PATCH v3] KVM: x86: avoid large stack allocations in em_fxrstor

From: Nick Desaulniers
Date: Mon May 29 2017 - 18:40:27 EST


On Mon, May 29, 2017 at 01:39:08PM -0700, Nick Desaulniers wrote:
> + if (ctxt->mode < X86EMUL_MODE_PROT64) {
> + rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
> + if (rc != X86EMUL_CONTINUE)
> + return rc;
> + /*
> + * 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]);
> + else
> + size = offsetof(struct fxregs_state, xmm_space[0]);
> + } else if (ctxt->mode == X86EMUL_MODE_PROT64)
> + size = offsetof(struct fxregs_state, xmm_space[16]);
> +
> + if (size == 0)
> + return X86EMUL_UNHANDLEABLE;
> +
> + rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
> if (rc != X86EMUL_CONTINUE)
> return rc;

Thinking more about this, I think it may be more elegant to move the
segmented_read_std into the then/else branches above, remove
initialization of size, and remove the size == 0 check. Thoughts?