From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16E5DC433ED for ; Mon, 10 May 2021 15:06:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E351F61481 for ; Mon, 10 May 2021 15:06:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238848AbhEJPHR (ORCPT ); Mon, 10 May 2021 11:07:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:54722 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239602AbhEJPGB (ORCPT ); Mon, 10 May 2021 11:06:01 -0400 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6DF146147F; Mon, 10 May 2021 15:04:56 +0000 (UTC) Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1lg7So-000TBy-B7; Mon, 10 May 2021 16:04:54 +0100 Date: Mon, 10 May 2021 16:04:53 +0100 Message-ID: <87v97qociy.wl-maz@kernel.org> From: Marc Zyngier To: Alexandru Elisei Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Zenghui Yu , James Morse , Suzuki K Poulose , kernel-team@android.com, stable@vger.kernel.org Subject: Re: [PATCH 2/2] KVM: arm64: Commit pending PC adjustemnts before returning to userspace In-Reply-To: <7a0f43c8-cc36-810e-0b8e-ffe66672ca82@arm.com> References: <20210510094915.1909484-1-maz@kernel.org> <20210510094915.1909484-3-maz@kernel.org> <7a0f43c8-cc36-810e-0b8e-ffe66672ca82@arm.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 62.31.163.78 X-SA-Exim-Rcpt-To: alexandru.elisei@arm.com, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, yuzenghui@huawei.com, james.morse@arm.com, suzuki.poulose@arm.com, kernel-team@android.com, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org On Mon, 10 May 2021 15:55:28 +0100, Alexandru Elisei wrote: > > Hi Marc, > > On 5/10/21 10:49 AM, Marc Zyngier wrote: > > 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 > > The code seems to handle only the KVM_ARM64_PENDING_EXCEPTION > flag. Is the "PC flag" a reference to the KVM_ARM64_INCREMENT_PC > flag? No, it does handle both exception and PC increment, unless I have completely bodged something (entirely possible). > > > context is wrong, as userpsace doesn't have access to these flags > > s/userpsace/userspace > > > (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 > > Signed-off-by: Marc Zyngier > > Cc: stable@vger.kernel.org # 5.11 > > --- > > arch/arm64/include/asm/kvm_asm.h | 1 + > > arch/arm64/kvm/arm.c | 10 ++++++++++ > > arch/arm64/kvm/hyp/exception.c | 4 ++-- > > arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 ++++++++ > > 4 files changed, 21 insertions(+), 2 deletions(-) > > > > diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h > > index d5b11037401d..5e9b33cbac51 100644 > > --- a/arch/arm64/include/asm/kvm_asm.h > > +++ b/arch/arm64/include/asm/kvm_asm.h > > @@ -63,6 +63,7 @@ > > #define __KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector 18 > > #define __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize 19 > > #define __KVM_HOST_SMCCC_FUNC___pkvm_mark_hyp 20 > > +#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 21 > > > > #ifndef __ASSEMBLY__ > > > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > > index 1cb39c0803a4..d62a7041ebd1 100644 > > --- a/arch/arm64/kvm/arm.c > > +++ b/arch/arm64/kvm/arm.c > > @@ -897,6 +897,16 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > > > > kvm_sigset_deactivate(vcpu); > > > > + /* > > + * In the unlikely event that we are returning to userspace > > + * with pending exceptions or PC adjustment, commit these > > I'm going to assume "PC adjustment" means the KVM_ARM64_INCREMENT_PC > flag. Please correct me if that's not true, but if that's the case, > then the flag isn't handled below. > > > + * adjustments in order to give userspace a consistent view of > > + * the vcpu state. > > + */ > > + if (unlikely(vcpu->arch.flags & (KVM_ARM64_PENDING_EXCEPTION | > > + KVM_ARM64_EXCEPT_MASK))) > > The condition seems to suggest that it is valid to set > KVM_ARM64_EXCEPT_{AA32,AA64}_* without setting > KVM_ARM64_PENDING_EXCEPTION, which looks rather odd to me. > Is that a valid use of the KVM_ARM64_EXCEPT_MASK bits? If it's not > (the existing code always sets the exception type with the > KVM_ARM64_PENDING_EXCEPTION), that I was thinking that checking only > the KVM_ARM64_PENDING_EXCEPTION flag would make the intention > clearer. No, you are missing this (subtle) comment in kvm_host.h: /* * Overlaps with KVM_ARM64_EXCEPT_MASK on purpose so that it can't be * set together with an exception... */ #define KVM_ARM64_INCREMENT_PC (1 << 9) /* Increment PC */ So (KVM_ARM64_PENDING_EXCEPTION | KVM_ARM64_EXCEPT_MASK) checks for *both* an exception and a PC increment. Thanks, M. -- Without deviation from the norm, progress is not possible. From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3CDAC433ED for ; Mon, 10 May 2021 15:05:07 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id 0901261421 for ; Mon, 10 May 2021 15:05:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0901261421 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvmarm-bounces@lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 788F84A483; Mon, 10 May 2021 11:05:06 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yeNDo+N2pL6V; Mon, 10 May 2021 11:05:01 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 45FFD4B32F; Mon, 10 May 2021 11:05:01 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id E43A140630 for ; Mon, 10 May 2021 11:04:59 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id BSOjVeqNmBpX for ; Mon, 10 May 2021 11:04:58 -0400 (EDT) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 5BD5040CF8 for ; Mon, 10 May 2021 11:04:58 -0400 (EDT) Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6DF146147F; Mon, 10 May 2021 15:04:56 +0000 (UTC) Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1lg7So-000TBy-B7; Mon, 10 May 2021 16:04:54 +0100 Date: Mon, 10 May 2021 16:04:53 +0100 Message-ID: <87v97qociy.wl-maz@kernel.org> From: Marc Zyngier To: Alexandru Elisei Subject: Re: [PATCH 2/2] KVM: arm64: Commit pending PC adjustemnts before returning to userspace In-Reply-To: <7a0f43c8-cc36-810e-0b8e-ffe66672ca82@arm.com> References: <20210510094915.1909484-1-maz@kernel.org> <20210510094915.1909484-3-maz@kernel.org> <7a0f43c8-cc36-810e-0b8e-ffe66672ca82@arm.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 62.31.163.78 X-SA-Exim-Rcpt-To: alexandru.elisei@arm.com, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, yuzenghui@huawei.com, james.morse@arm.com, suzuki.poulose@arm.com, kernel-team@android.com, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Cc: kvm@vger.kernel.org, stable@vger.kernel.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On Mon, 10 May 2021 15:55:28 +0100, Alexandru Elisei wrote: > > Hi Marc, > > On 5/10/21 10:49 AM, Marc Zyngier wrote: > > 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 > > The code seems to handle only the KVM_ARM64_PENDING_EXCEPTION > flag. Is the "PC flag" a reference to the KVM_ARM64_INCREMENT_PC > flag? No, it does handle both exception and PC increment, unless I have completely bodged something (entirely possible). > > > context is wrong, as userpsace doesn't have access to these flags > > s/userpsace/userspace > > > (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 > > Signed-off-by: Marc Zyngier > > Cc: stable@vger.kernel.org # 5.11 > > --- > > arch/arm64/include/asm/kvm_asm.h | 1 + > > arch/arm64/kvm/arm.c | 10 ++++++++++ > > arch/arm64/kvm/hyp/exception.c | 4 ++-- > > arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 ++++++++ > > 4 files changed, 21 insertions(+), 2 deletions(-) > > > > diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h > > index d5b11037401d..5e9b33cbac51 100644 > > --- a/arch/arm64/include/asm/kvm_asm.h > > +++ b/arch/arm64/include/asm/kvm_asm.h > > @@ -63,6 +63,7 @@ > > #define __KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector 18 > > #define __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize 19 > > #define __KVM_HOST_SMCCC_FUNC___pkvm_mark_hyp 20 > > +#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 21 > > > > #ifndef __ASSEMBLY__ > > > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > > index 1cb39c0803a4..d62a7041ebd1 100644 > > --- a/arch/arm64/kvm/arm.c > > +++ b/arch/arm64/kvm/arm.c > > @@ -897,6 +897,16 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > > > > kvm_sigset_deactivate(vcpu); > > > > + /* > > + * In the unlikely event that we are returning to userspace > > + * with pending exceptions or PC adjustment, commit these > > I'm going to assume "PC adjustment" means the KVM_ARM64_INCREMENT_PC > flag. Please correct me if that's not true, but if that's the case, > then the flag isn't handled below. > > > + * adjustments in order to give userspace a consistent view of > > + * the vcpu state. > > + */ > > + if (unlikely(vcpu->arch.flags & (KVM_ARM64_PENDING_EXCEPTION | > > + KVM_ARM64_EXCEPT_MASK))) > > The condition seems to suggest that it is valid to set > KVM_ARM64_EXCEPT_{AA32,AA64}_* without setting > KVM_ARM64_PENDING_EXCEPTION, which looks rather odd to me. > Is that a valid use of the KVM_ARM64_EXCEPT_MASK bits? If it's not > (the existing code always sets the exception type with the > KVM_ARM64_PENDING_EXCEPTION), that I was thinking that checking only > the KVM_ARM64_PENDING_EXCEPTION flag would make the intention > clearer. No, you are missing this (subtle) comment in kvm_host.h: /* * Overlaps with KVM_ARM64_EXCEPT_MASK on purpose so that it can't be * set together with an exception... */ #define KVM_ARM64_INCREMENT_PC (1 << 9) /* Increment PC */ So (KVM_ARM64_PENDING_EXCEPTION | KVM_ARM64_EXCEPT_MASK) checks for *both* an exception and a PC increment. Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D45FDC433ED for ; Mon, 10 May 2021 15:06:43 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2642761483 for ; Mon, 10 May 2021 15:06:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2642761483 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Subject:Cc:To: From:Message-ID:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=umafz6LWos5UTV+OfaTxx4Vbwnzhgr2jGGk+TJ0tf5g=; b=VYBChBw4jLvjam37elOwtu+aT s/cs7zEy8VuROjQ9wfhx+scnMSRc8/Pxtn43kSch/tbSQ/HeN9EaEb938X25SCpV4ssrrOzkHbkY3 7en3s3ymli/28JtVeKVCNvZNmdXqi4TzhTBtrIyhKRfknitE4WKR7/c3biLYJPjed1dnjul+/Q9fE Zw8uEmX67pgTeTXUaT5z4xIR4rS7cPxZpS0cQhyFffThAH+L+2jduqbOFpyk6HlBazn12JTGZI+in ThFdl0ESasGQhcc8216D8htZnqjjFnqkJjZheBZHk0ROoZFXMiOBOXtKMorjgvv1Epp8JH9CqzuRR wFK3Hechg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lg7Sx-00EkFn-Tr; Mon, 10 May 2021 15:05:04 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lg7St-00EkF9-P0 for linux-arm-kernel@desiato.infradead.org; Mon, 10 May 2021 15:04:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Type:MIME-Version:References: In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=sQU4MPX8Z6trnlDa/cnTb0TaBYe8q0qDBPC/jxYfq/M=; b=vuszbrcrs7qYELtJtaIhOBqkrM U5A77RHvc7tkB54j/EBdo4AtvO07exOe30NRc8bbM+f+P4HdE1SAe6RiG28Yyo3j1LiD2BIfbO4xg 0Nx4hetPGveceBnP2Uw7K68LWuv4COC3jLqHEtJ1wjwEGiFnlYM7omBChkWJIZBWDAF73GNWg6OBr F27aP/u5iRPksKfytBTeFrAY7Vx5S6pYs+l81+09FlzFEi9hL0joht6cZjbYsFI9pcLXhB8idX02J d5mDWZvkeozggEmmGYNVQpl6CQa1RC8vrAZ2j1wa8aVNGLcMCEFPfeO/X1MM09CnuCtoPnBBC5h2Y PA5S4vJg==; Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lg7Sq-008ql8-TQ for linux-arm-kernel@lists.infradead.org; Mon, 10 May 2021 15:04:58 +0000 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6DF146147F; Mon, 10 May 2021 15:04:56 +0000 (UTC) Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1lg7So-000TBy-B7; Mon, 10 May 2021 16:04:54 +0100 Date: Mon, 10 May 2021 16:04:53 +0100 Message-ID: <87v97qociy.wl-maz@kernel.org> From: Marc Zyngier To: Alexandru Elisei Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Zenghui Yu , James Morse , Suzuki K Poulose , kernel-team@android.com, stable@vger.kernel.org Subject: Re: [PATCH 2/2] KVM: arm64: Commit pending PC adjustemnts before returning to userspace In-Reply-To: <7a0f43c8-cc36-810e-0b8e-ffe66672ca82@arm.com> References: <20210510094915.1909484-1-maz@kernel.org> <20210510094915.1909484-3-maz@kernel.org> <7a0f43c8-cc36-810e-0b8e-ffe66672ca82@arm.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 62.31.163.78 X-SA-Exim-Rcpt-To: alexandru.elisei@arm.com, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, yuzenghui@huawei.com, james.morse@arm.com, suzuki.poulose@arm.com, kernel-team@android.com, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210510_080457_028578_10B75FFE X-CRM114-Status: GOOD ( 38.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, 10 May 2021 15:55:28 +0100, Alexandru Elisei wrote: > > Hi Marc, > > On 5/10/21 10:49 AM, Marc Zyngier wrote: > > 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 > > The code seems to handle only the KVM_ARM64_PENDING_EXCEPTION > flag. Is the "PC flag" a reference to the KVM_ARM64_INCREMENT_PC > flag? No, it does handle both exception and PC increment, unless I have completely bodged something (entirely possible). > > > context is wrong, as userpsace doesn't have access to these flags > > s/userpsace/userspace > > > (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 > > Signed-off-by: Marc Zyngier > > Cc: stable@vger.kernel.org # 5.11 > > --- > > arch/arm64/include/asm/kvm_asm.h | 1 + > > arch/arm64/kvm/arm.c | 10 ++++++++++ > > arch/arm64/kvm/hyp/exception.c | 4 ++-- > > arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 ++++++++ > > 4 files changed, 21 insertions(+), 2 deletions(-) > > > > diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h > > index d5b11037401d..5e9b33cbac51 100644 > > --- a/arch/arm64/include/asm/kvm_asm.h > > +++ b/arch/arm64/include/asm/kvm_asm.h > > @@ -63,6 +63,7 @@ > > #define __KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector 18 > > #define __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize 19 > > #define __KVM_HOST_SMCCC_FUNC___pkvm_mark_hyp 20 > > +#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 21 > > > > #ifndef __ASSEMBLY__ > > > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > > index 1cb39c0803a4..d62a7041ebd1 100644 > > --- a/arch/arm64/kvm/arm.c > > +++ b/arch/arm64/kvm/arm.c > > @@ -897,6 +897,16 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > > > > kvm_sigset_deactivate(vcpu); > > > > + /* > > + * In the unlikely event that we are returning to userspace > > + * with pending exceptions or PC adjustment, commit these > > I'm going to assume "PC adjustment" means the KVM_ARM64_INCREMENT_PC > flag. Please correct me if that's not true, but if that's the case, > then the flag isn't handled below. > > > + * adjustments in order to give userspace a consistent view of > > + * the vcpu state. > > + */ > > + if (unlikely(vcpu->arch.flags & (KVM_ARM64_PENDING_EXCEPTION | > > + KVM_ARM64_EXCEPT_MASK))) > > The condition seems to suggest that it is valid to set > KVM_ARM64_EXCEPT_{AA32,AA64}_* without setting > KVM_ARM64_PENDING_EXCEPTION, which looks rather odd to me. > Is that a valid use of the KVM_ARM64_EXCEPT_MASK bits? If it's not > (the existing code always sets the exception type with the > KVM_ARM64_PENDING_EXCEPTION), that I was thinking that checking only > the KVM_ARM64_PENDING_EXCEPTION flag would make the intention > clearer. No, you are missing this (subtle) comment in kvm_host.h: /* * Overlaps with KVM_ARM64_EXCEPT_MASK on purpose so that it can't be * set together with an exception... */ #define KVM_ARM64_INCREMENT_PC (1 << 9) /* Increment PC */ So (KVM_ARM64_PENDING_EXCEPTION | KVM_ARM64_EXCEPT_MASK) checks for *both* an exception and a PC increment. Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel