Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4369834pxj; Tue, 8 Jun 2021 12:37:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpnu3NQzBgteg9DHYlwq2lTk57ZCB2nDzid+sZPDsIFOVIpQTnBoSBNLfPQgYRrfIjqgv5 X-Received: by 2002:aa7:c54c:: with SMTP id s12mr14652421edr.374.1623181032726; Tue, 08 Jun 2021 12:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623181032; cv=none; d=google.com; s=arc-20160816; b=RIB+6CItOoknlhWrPjiI3o1euMJpkQ1ir2Vl9jbvv2CJ3EqL6KS7N3F3wY6Ubgscw+ ESjf6fDUQF5gqMRTMT83cVuxapSbJe54CQgiDh5QKZhwvSU+js13IEU5wxLqlpyvwU/U Q/18L0dSp01wnZrWzu20V69c4Y/MgEeNugf2QQKBsi2FEtx6XptZfFrLDOBzozYoWYo1 xkKU1SD0TJzgBpkj2hDHU8iYvEDySb4Fpk+O7JSqROG8wWs60w/ayGF4VbJQHXCP+QV6 HgFfkSbxiiQsdeIwj3KGL+R3UStiv8vw76/rCVmQKBDUgVu0rpn78U9B58DdC5sFvPOh FgUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=iIcVOpJbTRE+/r4KfWMan0H01FbzWoExfZaaIz/Ko18=; b=b2a22Qs+hPI0ggoZNz8m1FKiGC5yvyOcoBiNKCW12UkXmxaENiPx3sbJG3/6kBpnAt jvaTi+oX2mqM/nynNMQZes3anFAP+FkKfm0WbZR5cB4/Cc2kVhqWs29gJK63k/8OV7SQ RgNszec+j0AAqKY8x01QC1Gpglf4XxQgVM30/Ni2MJ0DWkrsQIhE/BF2dP6o4F9zlVc/ Wekl+0RTX5Sb0EK3yegG9m8Q4vsTO2yJD8szy+TrZpDznhA4D0zM/Mzj/SwHMao37S6z UNNuELhuSGjFUSnh0Zn0fbCb3kcN+10zN8JiQvITobXptdlCGjdELTLWKw5CDAZrot2X MuWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VzD9KSFy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c25si474453edy.128.2021.06.08.12.36.48; Tue, 08 Jun 2021 12:37:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VzD9KSFy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238968AbhFHTe3 (ORCPT + 99 others); Tue, 8 Jun 2021 15:34:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:39732 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238002AbhFHTS2 (ORCPT ); Tue, 8 Jun 2021 15:18:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A3553613DC; Tue, 8 Jun 2021 18:51:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623178312; bh=MZRgdcwTak35JCmwkrToVUJ54ErTgwQqssS1fik8Kik=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VzD9KSFytkZ5JgwegBt9Ry45tRIviKh5INS1UDjqktmnx3pbdMZRdL5MqHHwdFvJs KVScOWa/nwXu1RpksNd+cmS1eHRm+HF0nqn5sgQd/EEokkykf2ICBF/wbOo43TgrkZ n3DEQloYC/6rY8WJ/l8gIPynFcMOaM/Pxw5i+iTg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zenghui Yu , Alexandru Elisei , Marc Zyngier Subject: [PATCH 5.12 154/161] KVM: arm64: Commit pending PC adjustemnts before returning to userspace Date: Tue, 8 Jun 2021 20:28:04 +0200 Message-Id: <20210608175950.654918120@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175945.476074951@linuxfoundation.org> References: <20210608175945.476074951@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marc Zyngier commit 26778aaa134a9aefdf5dbaad904054d7be9d656d upstream. KVM currently updates PC (and the corresponding exception state) using a two phase approach: first by setting a set of flags, then by converting these flags into a state update when the vcpu is about to enter the guest. However, this creates a disconnect with userspace if the vcpu thread returns there with any exception/PC flag set. In this case, the exposed context is wrong, as userspace doesn't have access to these flags (they aren't architectural). It also means that these flags are preserved across a reset, which isn't expected. To solve this problem, force an explicit synchronisation of the exception state on vcpu exit to userspace. As an optimisation for nVHE systems, only perform this when there is something pending. Reported-by: Zenghui Yu Reviewed-by: Alexandru Elisei Reviewed-by: Zenghui Yu Tested-by: Zenghui Yu Signed-off-by: Marc Zyngier Cc: stable@vger.kernel.org # 5.11 [yuz: stable-5.12.y backport: allocate a new number (15) for __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc to keep the host_hcall array tightly packed] Signed-off-by: Zenghui Yu Reviewed-by: Marc Zyngier Signed-off-by: Greg Kroah-Hartman --- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/arm.c | 11 +++++++++++ arch/arm64/kvm/hyp/exception.c | 4 ++-- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 ++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -57,6 +57,7 @@ #define __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 12 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs 13 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs 14 +#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 15 #ifndef __ASSEMBLY__ --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -892,6 +892,17 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_v kvm_sigset_deactivate(vcpu); + /* + * In the unlikely event that we are returning to userspace + * with pending exceptions or PC adjustment, commit these + * adjustments in order to give userspace a consistent view of + * the vcpu state. Note that this relies on __kvm_adjust_pc() + * being preempt-safe on VHE. + */ + if (unlikely(vcpu->arch.flags & (KVM_ARM64_PENDING_EXCEPTION | + KVM_ARM64_INCREMENT_PC))) + kvm_call_hyp(__kvm_adjust_pc, vcpu); + vcpu_put(vcpu); return ret; } --- a/arch/arm64/kvm/hyp/exception.c +++ b/arch/arm64/kvm/hyp/exception.c @@ -331,8 +331,8 @@ static void kvm_inject_exception(struct } /* - * Adjust the guest PC on entry, depending on flags provided by EL1 - * for the purpose of emulation (MMIO, sysreg) or exception injection. + * Adjust the guest PC (and potentially exception state) depending on + * flags provided by the emulation code. */ void __kvm_adjust_pc(struct kvm_vcpu *vcpu) { --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -25,6 +25,13 @@ static void handle___kvm_vcpu_run(struct cpu_reg(host_ctxt, 1) = __kvm_vcpu_run(kern_hyp_va(vcpu)); } +static void handle___kvm_adjust_pc(struct kvm_cpu_context *host_ctxt) +{ + DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1); + + __kvm_adjust_pc(kern_hyp_va(vcpu)); +} + static void handle___kvm_flush_vm_context(struct kvm_cpu_context *host_ctxt) { __kvm_flush_vm_context(); @@ -112,6 +119,7 @@ typedef void (*hcall_t)(struct kvm_cpu_c static const hcall_t host_hcall[] = { HANDLE_FUNC(__kvm_vcpu_run), + HANDLE_FUNC(__kvm_adjust_pc), HANDLE_FUNC(__kvm_flush_vm_context), HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa), HANDLE_FUNC(__kvm_tlb_flush_vmid),