From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 79CFA1DE2DC; Thu, 26 Jun 2025 11:26:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750937189; cv=none; b=PZtGJ/h2f6T4aS/klqpO9rEmYAicj4M3Z0oKhvinaXjwZ2GaUhQ55h0WA/5pFfHfn43ulnSn4t2+wvsF9ScLHaASTqotYs36DHyhYk4ywxUfsZqzquzR4YMzA0dCYuo9ApDePiSwuktsE3hzQf3rHKibjhwbXBPIeQvl8UDEHEg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750937189; c=relaxed/simple; bh=dQypl9dyKKw9crO92Tcl/v2GMju0CN2R0bSYOIYtRcw=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=bB2iHSN8wPjD0MTs5UW5oAfuIJQvI97R+Fl3aVCEujSUnZ6U4qMSybKnlUnp6p2s/5igZBRTZlR9kHJZp2szbW+yiRrY4GBX6Vp6y2hiSxJ6fJkykG5qsoBzonpw2mAGCfxMiwaFclZcGpTYhXzGuaRqsAB6oLYp8pAvn/V1nU8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NuRL+5z+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NuRL+5z+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00C5AC4CEEB; Thu, 26 Jun 2025 11:26:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750937189; bh=dQypl9dyKKw9crO92Tcl/v2GMju0CN2R0bSYOIYtRcw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=NuRL+5z+Ye4uqQI6ye13Pv6+z4FoeqNJgLV6gOtgDdTPjv0l9G5Av1JPxfDoy7tBh AbKfaEvf176MJ9ED+xg6/HzhOMdv/cjBqI59J874t+37TC713qQotYZC9HR4bB7U11 9Kbdiivxczwel6B9Q+9eQ5iEKMGh7Rq7ROOZHo8CC8a/dsd6qfOR0kDucLdwRCGXbw 7YF/R8U4XsSSilWk6Kxwdjxt8TIEZPE1f7FNLVTz7XhKRROA05i+H+vvqqojVX5/cZ uFPnpmyVztEz+zW37Mi4kfxHvJMFepUl5M5jepnJWi1bDc3NklUi9LzdxPOlp9ZcVy WPXAObI1mUQDw== 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 1uUkkM-00ACiY-G9; Thu, 26 Jun 2025 12:26:26 +0100 Date: Thu, 26 Jun 2025 12:26:25 +0100 Message-ID: <86y0tebv6m.wl-maz@kernel.org> From: Marc Zyngier To: Quentin Perret Cc: Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mostafa Saleh , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: arm64: Don't free hyp pages with pKVM on GICv2 In-Reply-To: <20250626101014.1519345-1-qperret@google.com> References: <20250626101014.1519345-1-qperret@google.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/30.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: qperret@google.com, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, smostafa@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@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 On Thu, 26 Jun 2025 11:10:14 +0100, Quentin Perret wrote: > > Marc reported that enabling protected mode on a device with GICv2 > doesn't fail gracefully as one would expect, and leads to a host > kernel crash. > > As it turns out, the first half of pKVM init happens before the vgic > probe, and so by the time we find out we have a GICv2 we're already > committed to keeping the pKVM vectors installed at EL2 -- pKVM rejects > stub HVCs for obvious security reasons. However, the error path on KVM > init leads to teardown_hyp_mode() which unconditionally frees hypervisor > allocations (including the EL2 stacks and per-cpu pages) under the > assumption that a previous cpu_hyp_uninit() execution has reset the > vectors back to the stubs, which is false with pKVM. > > Interestingly, host stage-2 protection is not enabled yet at this point, > so this use-after-free may go unnoticed for a while. The issue becomes > more obvious after the finalize_pkvm() call. > > Fix this by keeping track of the CPUs on which pKVM is initialized in > the kvm_hyp_initialized per-cpu variable, and use it from > teardown_hyp_mode() to skip freeing pages that are in fact used. > > Fixes: a770ee80e662 ("KVM: arm64: pkvm: Disable GICv2 support") > Reported-by: Marc Zyngier > Signed-off-by: Quentin Perret I just gave it a go on a Juno-r1, and it didn't die! Must be perfect! :) I'll stash it on top of Mostafa's patch. Thanks! M. -- Without deviation from the norm, progress is not possible.