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 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A7EF7C3DA4A for ; Wed, 14 Aug 2024 13:31:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=btWpugW5Ouic4ioe6NoaWA2iRb4D/5SIOR8EwId/kZc=; b=QUC1vC4yc9xirXWfuYpuOoqSD1 2cDAoobnDy4vsKE0L6HjCgbFs6wX22i4l6vpok5PWasTAiH+Ttn495uC1igTXoNVGcIjHrJ9FksvY 5LnNOwlr5gc2XX5tANe8BlAjwoRMmycr5Bt5ur1YxBvccgCTehKl1Rm15Ha8yAXCYm0o78BUBYOJ9 Gm2eiN3z4d2Yye8d17QlN9AS0MoRFfNbWRtYVyzmpZesqAMhHyFdruvE8WtNSicnT1pZtB87rLnob VJUPATSPrqIiTlpS2fLJgCwpJA0p4kaElidsiS5J6IQ3AZfq+Zm/3wQmuNQxhWarsAvGvRuHg/Jqa hwGjLLxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1seE6C-000000076fz-0V81; Wed, 14 Aug 2024 13:31:36 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1seE5Z-000000076an-0lKA for linux-arm-kernel@lists.infradead.org; Wed, 14 Aug 2024 13:30:58 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 2A79D61A2D; Wed, 14 Aug 2024 13:30:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D55CEC32786; Wed, 14 Aug 2024 13:30:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723642255; bh=6PEuPukI+6pCm/WZ+0rZZCOQMHCGiIb3Ln74vcVm85Q=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=i1ubjz1DXBzvMLx2fPT+p/DlJVztCOo4Sw0/J9799UByx24X6P8HBgOYYuBHfTDNv DCWmoq7IF3hDOyrJ3fW3I8DU4WY5OJtgh2U5q8TE6o9noOjqsnltJqOnQQnQYcawCV l7v+Y0EFGtkdkqzDWtxU3MvGqhufJKJ7wHmcTrapiZAZNUdYYd9X4jhKz1yMUgwRzP ksEu8HbAd7d4oeb/CUY9r2DOyOU1STiutF6pBBkrXIsoLdM/McacOzv4rNCUjXisdo WWgaDLGUpftpBa18MJoxTjxbWWxHKDm+fi4HMmvTKDt1GZpAXaPgCYCepJac8K03ua GgMP04vyv9g6w== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1seE5V-003gBe-Q0; Wed, 14 Aug 2024 14:30:53 +0100 Date: Wed, 14 Aug 2024 14:30:53 +0100 Message-ID: <86ttfnz0xe.wl-maz@kernel.org> From: Marc Zyngier To: Will Deacon Cc: linux-arm-kernel@lists.infradead.org, Oliver Upton , Fuad Tabba , kvmarm@lists.linux.dev Subject: Re: [PATCH 2/2] KVM: arm64: Ensure TLBI uses correct VMID after changing context In-Reply-To: <20240814123429.20457-3-will@kernel.org> References: <20240814123429.20457-1-will@kernel.org> <20240814123429.20457-3-will@kernel.org> 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/29.4 (aarch64-unknown-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: 185.219.108.64 X-SA-Exim-Rcpt-To: will@kernel.org, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, tabba@google.com, kvmarm@lists.linux.dev 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-20240814_063057_377100_86BF4AAB X-CRM114-Status: GOOD ( 31.91 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Will, On Wed, 14 Aug 2024 13:34:29 +0100, Will Deacon wrote: > > When the target context passed to enter_vmid_context() matches the > current running context, the function returns early without manipulating > the registers of the stage-2 MMU. This can result in a stale VMID due to > the lack of an ISB instruction in exit_vmid_context() after writing the This refers to the exit_vmid_context() *before* a subsequent enter_vmid_context(), right? > VTTBR when ARM64_WORKAROUND_SPECULATIVE_AT is not enabled. I'm not sure I completely get the failure mode, so please bear with me. > > For example, with pKVM enabled: > > // Initially running in host context > enter_vmid_context(guest); > -> __load_stage2(guest); isb // Writes VTCR & VTTBR > exit_vmid_context(guest); > -> __load_stage2(host); // Restores VTCR & VTTBR > > enter_vmid_context(host); > -> Returns early as we're already in host context Is this the code you are referring to? vcpu = host_ctxt->__hyp_running_vcpu; [...] if (vcpu) { /* * We're in guest context. However, for this to work, this needs * to be called from within __kvm_vcpu_run(), which ensures that * __hyp_running_vcpu is set to the current guest vcpu. */ if (mmu == vcpu->arch.hw_mmu || WARN_ON(mmu != host_s2_mmu)) return; cxt->mmu = vcpu->arch.hw_mmu; } else { /* We're in host context. */ if (mmu == host_s2_mmu) return; cxt->mmu = host_s2_mmu; } So I take it that no vcpu is running at this point, and that we have done a vcpu_put(). Is there an actual path within pKVM that causes a guest TLBI to be followed by a host __kvm_tlb_flush_vmid() *without* a CSE? I can't convinced myself that such a path exist in the current upstream code base. > tlbi vmalls12e1is // !!! Can use the stale VMID as we > // haven't performed context > // synchronisation since restoring > // VTTBR.VMID > > Add an unconditional ISB instruction to exit_vmid_context() after > restoring the VTTBR. This already existed for the > ARM64_WORKAROUND_SPECULATIVE_AT path, so we can simply hoist that onto > the common path. > > Cc: Marc Zyngier > Cc: Oliver Upton > Cc: Fuad Tabba > Fixes: 58f3b0fc3b87 ("KVM: arm64: Support TLB invalidation in guest context") > Signed-off-by: Will Deacon > --- > arch/arm64/kvm/hyp/nvhe/tlb.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/nvhe/tlb.c b/arch/arm64/kvm/hyp/nvhe/tlb.c > index ca3c09df8d7c..48da9ca9763f 100644 > --- a/arch/arm64/kvm/hyp/nvhe/tlb.c > +++ b/arch/arm64/kvm/hyp/nvhe/tlb.c > @@ -132,10 +132,10 @@ static void exit_vmid_context(struct tlb_inv_context *cxt) > else > __load_host_stage2(); > > - if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) { > - /* Ensure write of the old VMID */ > - isb(); > + /* Ensure write of the old VMID */ > + isb(); > > + if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) { > if (!(cxt->sctlr & SCTLR_ELx_M)) { > write_sysreg_el1(cxt->sctlr, SYS_SCTLR); > isb(); The patch itself makes sense to me. I'd just like to understand how we end-up in that situation. Thanks, M. -- Without deviation from the norm, progress is not possible.