From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30E35490C1F; Thu, 15 Oct 2026 10:05:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792058746; cv=none; b=XrhoeyNpCwMU9opPg2tDi2b/JkNhgK6K0Sme2f2Shb/W15wo+Jos+QO4mSFeVH+8Wy/0Q+TM1lgPc5o7838mWEvwmEAvLUFXC9hQT43u08XJKuWFAFcDg2fB+HtBeX1Uq5gTt51VWNMNaDl1KYmXczkS3I5IWP1XGucQtLEujGQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792058746; c=relaxed/simple; bh=QAbsovvmTlS08Yzo8+/BsbNLLnA0d3u4080EI3GLOiA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZWJyLcOv23rTMJUZs9ZzKkr4T9gOfzpq4JjodE0XP+knWrdCP4SetwqZbcu5dMQUK81TsDSHSjNzq1mzkedZqutRgmDWq2Ur1Tne6dTDGix3fTAbSdbOErBoIqurxwLpPnJqDH/syJoSm8dtskA8QXX+A0AmEIcI3VQLD7LiXtU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EALL3TSv; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EALL3TSv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF63E1F000C7; Thu, 15 Oct 2026 10:05:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1792058745; bh=IcXE+QKAM3zp7ruNKnZ0gCHpYWLhruwBMjwK4wN4Xro=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=EALL3TSvwfzPr/49BtYM6eTll4svhaR+YZNuW3DSFW+25zjyTO+CJRFsyGN9y+ir6 R1Fv56fgLplr3Q2Ks4FYKxO4Ad8j90nWav+WXtmHDG7Q/TKOhn6/avgD7XFEI67oD9 JhWZzxnZ7N4I0YAHiRbeoU2R5lq7liW9/GVOr6EnbAU/G0ppT2/POJqs0DRu2uzGkr 0NVaRGFslPuN7ZKHmB6RnJeu1qKPHxHRCJ0IHG2xOTqfM98agO1lUu9Jz9iiTtkYlc 2HqylAPqQ1HYnXIkh+sl0oPF4SvF8DLbqOuUW71VHwBCWcFvzRI1vpqnDf24U+c4OG FXCkWSd69IEEQ== Received: from sofa.misterjones.org ([185.219.108.64] helo=valley-girl.lan) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHILH-00000003EfK-0s3N; Thu, 15 Oct 2026 10:05:43 +0000 From: Marc Zyngier To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org Cc: Steffen Eiden , Joey Gouly , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Fuad Tabba , stable@vger.kernel.org Subject: [PATCH 3/7] KVM: arm64: Move IN_GUEST_MODE ahead of state being loaded Date: Thu, 15 Oct 2026 11:05:35 +0100 Message-ID: <20261015100539.1011760-4-maz@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261015100539.1011760-1-maz@kernel.org> References: <20261015100539.1011760-1-maz@kernel.org> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, seiden@linux.ibm.com, joey.gouly@arm.com, suzuki.poulose@arm.com, oupton@kernel.org, yuzenghui@huawei.com, fuad.tabba@linux.dev, 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 IN_GUEST_MODE is advertised too late, when state has already been moved onto CPU registers and cannot be observed by another CPU anymore. Move the IN_GUEST_MODE publication ahead of the state being loaded, and immediately check for pending request. This ensures that there is no window of opportunity for another CPU to observe OUTSIDE_GUEST_MODE and yet for state to be loaded. This also simplifies the "state rollback", which solely amounts to synchronising the timer when no irqchip is present. Signed-off-by: Marc Zyngier Cc: stable@vger.kernel.org --- arch/arm64/kvm/arm.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 8b46fafdd3494..9956049e592ab 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1321,31 +1321,33 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) local_irq_disable(); - kvm_vgic_flush_hwstate(vcpu); - - kvm_pmu_update_vcpu_events(vcpu); - /* * Ensure we set mode to IN_GUEST_MODE after we disable * interrupts and before the final VCPU requests check. * See the comment in kvm_vcpu_exiting_guest_mode() and * Documentation/virt/kvm/vcpu-requests.rst + * + * Requests to self must be posted before this point, or + * won't be taken into account until the next exit. No state + * loading other than what happens at vcpu_load() time can + * be done before we have checked for pending requests. */ smp_store_mb(vcpu->mode, IN_GUEST_MODE); if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) { - isb(); /* Ensure work in x_flush_hwstate is committed */ - if (kvm_vcpu_has_pmu(vcpu)) - kvm_pmu_sync_hwstate(vcpu); if (unlikely(!irqchip_in_kernel(vcpu->kvm))) kvm_timer_sync_user(vcpu); - kvm_vgic_sync_hwstate(vcpu); smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE); local_irq_enable(); preempt_enable(); continue; } + /* From here onwards, we are commited to entering the guest */ + kvm_vgic_flush_hwstate(vcpu); + + kvm_pmu_update_vcpu_events(vcpu); + kvm_arch_vcpu_ctxflush_fp(vcpu); /************************************************************** -- 2.47.3