kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandru Elisei <alexandru.elisei@arm.com>
To: Marc Zyngier <maz@kernel.org>,
	linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org,
	kvmarm@lists.cs.columbia.edu
Cc: James Morse <james.morse@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Will Deacon <will@kernel.org>,
	kernel-team@android.com, stable@vger.kernel.org
Subject: Re: [PATCH v2] KVM: arm64: Fully zero the vcpu state on reset
Date: Mon, 12 Apr 2021 17:51:53 +0100	[thread overview]
Message-ID: <977338ef-9b06-1fec-9075-5e9cbdb89bc2@arm.com> (raw)
In-Reply-To: <20210409173257.3031729-1-maz@kernel.org>

Hi Marc,

On 4/9/21 6:32 PM, Marc Zyngier wrote:
> On vcpu reset, we expect all the registers to be brought back
> to their initial state, which happens to be a bunch of zeroes.
>
> However, some recent commit broke this, and is now leaving a bunch
> of registers (such as the FP state) with whatever was left by the
> guest. My bad.
>
> Zero the reset of the state (32bit SPSRs and FPSIMD state).
>
> Cc: stable@vger.kernel.org
> Fixes: e47c2055c68e ("KVM: arm64: Make struct kvm_regs userspace-only")
> Signed-off-by: Marc Zyngier <maz@kernel.org>
> ---
>
> Notes:
>     v2: Only reset the FPSIMD state and the AArch32 SPSRs to avoid
>     corrupting CNTVOFF in creative ways.

I missed that last time, sorry.

>
>  arch/arm64/kvm/reset.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
> index bd354cd45d28..4b5acd84b8c8 100644
> --- a/arch/arm64/kvm/reset.c
> +++ b/arch/arm64/kvm/reset.c
> @@ -242,6 +242,11 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
>  
>  	/* Reset core registers */
>  	memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu)));
> +	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
> +	vcpu->arch.ctxt.spsr_abt = 0;
> +	vcpu->arch.ctxt.spsr_und = 0;
> +	vcpu->arch.ctxt.spsr_irq = 0;
> +	vcpu->arch.ctxt.spsr_fiq = 0;

Checked, and the only fields not touched by the change from struct kvm_cpu_context
are sys_regs and __hyp_running_cpu. __hyp_running_cpu is assumed to be NULL for a
guest and it doesn't change during the lifetime of a VCPU; it is set to NULL when
struct kvm_vcpu is allocated.

CNTVOFF_EL2 is not accessible to userspace (it's not in the sys_reg_descs and in
the invariant_sys_regs arrays), so it's not necessary to reset it in case
userspace changed it. Same for the other registers that are part of the VCPU
context, but are not in sys_reg_descs.

I think it's a good choice to skip zeroing the system registers. I compared
vcpu->arch.ctxt.sys_regs with sys_regs_descs, there were quite a few registers
that were part of the vcpu context (pointer authentication registers, virtual
timer registers, and others) and not part of sys_regs_descs. If someone adds a
register to the VCPU context, but not to sys_regs_descs, I think it would have
been easy to miss the fact that KVM_ARM_VCPU_INIT zeroes it.

Hopefully I haven't missed anything:

Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>

Thanks,

Alex

>  	vcpu_gp_regs(vcpu)->pstate = pstate;
>  
>  	/* Reset system registers */

      reply	other threads:[~2021-04-12 16:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-09 17:32 [PATCH v2] KVM: arm64: Fully zero the vcpu state on reset Marc Zyngier
2021-04-12 16:51 ` Alexandru Elisei [this message]

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=977338ef-9b06-1fec-9075-5e9cbdb89bc2@arm.com \
    --to=alexandru.elisei@arm.com \
    --cc=james.morse@arm.com \
    --cc=kernel-team@android.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=maz@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=will@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).