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 EE77DCEE352 for ; Wed, 9 Oct 2024 21:16:12 +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:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From: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=2Q6tIzhsoWpI8OActtQsa2+WNCC3Oz8pxLk/X6USEkU=; b=IWyRaIpa1jjZERNa+cOG4R/DuT x1zC9bOt8Is2oFLa7BLWprLflU9vuKEvwVSPvPHb7KtLFExBf/FJL8tsTifdZfsQHiUKlik2MuQjm jJZd86Q7J6biCnsMCkJWrjsjdBjz8RE/QemVTWzb5UfGksSmFfapJWIySev6AlU1k7CVhL3bnJwT+ nmTetxjguFJsoJAj7QflumbBiBXh/O7XBHXP4o12NfJ8HOrU4iM3l1wr4ctSDGO7y4rY2284s0qXp YILOuHIQaLVsUGv43vudWdh2FCnXVq6lx9n707+v/GeWvgFCIk2E7TI6RgkpHxTnst/XuvKSkpptG Pzf/0zHQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sye2K-0000000AkLi-1ujn; Wed, 09 Oct 2024 21:16:00 +0000 Received: from out-189.mta0.migadu.com ([2001:41d0:1004:224b::bd]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sycJu-0000000AV6Q-3Ysn for linux-arm-kernel@lists.infradead.org; Wed, 09 Oct 2024 19:26:06 +0000 Date: Wed, 9 Oct 2024 12:25:48 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1728501956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=2Q6tIzhsoWpI8OActtQsa2+WNCC3Oz8pxLk/X6USEkU=; b=Ki0k9YSbRb8hPkAO0EtbW+r+z11wP0Mkn1h3zKB6/Yt1r2h8wsoyzKRFz8d5NVwOKTvs9r nuKoB7eqIH6esAO9KB7bLjRdjSjtAwwLTOXWRwSKBGZBafB6vEKIsg+6v0Oh7v+X/5FIPz VpIHBYoS3XPmsOY27gBS51lyKCk+1Ks= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, Joey Gouly , Suzuki K Poulose , Zenghui Yu , stable@vger.kernel.org, Alexander Potapenko Subject: Re: [PATCH] KVM: arm64: Don't eagerly teardown the vgic on init error Message-ID: References: <20241009183603.3221824-1-maz@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241009183603.3221824-1-maz@kernel.org> X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241009_122603_419127_E0606B22 X-CRM114-Status: GOOD ( 30.16 ) 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 On Wed, Oct 09, 2024 at 07:36:03PM +0100, Marc Zyngier wrote: > As there is very little ordering in the KVM API, userspace can > instanciate a half-baked GIC (missing its memory map, for example) > at almost any time. > > This means that, with the right timing, a thread running vcpu-0 > can enter the kernel without a GIC configured and get a GIC created > behind its back by another thread. Amusingly, it will pick up > that GIC and start messing with the data structures without the > GIC having been fully initialised. Huh, I'm definitely missing something. Could you remind me where we open up this race between KVM_RUN && kvm_vgic_create()? I'd thought the fact that the latter takes all the vCPU mutexes and checks if any vCPU in the VM has run would be enough to guard against such a race, but clearly not... > Similarly, a thread running vcpu-1 can enter the kernel, and try > to init the GIC that was previously created. Since this GIC isn't > properly configured (no memory map), it fails to correctly initialise. > > And that's the point where we decide to teardown the GIC, freeing all > its resources. Behind vcpu-0's back. Things stop pretty abruptly, > with a variety of symptoms. Clearly, this isn't good, we should be > a bit more careful about this. > > It is obvious that this guest is not viable, as it is missing some > important part of its configuration. So instead of trying to tear > bits of it down, let's just mark it as *dead*. It means that any > further interaction from userspace will result in -EIO. The memory > will be released on the "normal" path, when userspace gives up. > > Cc: stable@vger.kernel.org > Reported-by: Alexander Potapenko > Signed-off-by: Marc Zyngier Anyway, regarless of *how* we got here, it is pretty clear that tearing things down on the error path is a bad idea. So: Reviewed-by: Oliver Upton > --- > arch/arm64/kvm/arm.c | 3 +++ > arch/arm64/kvm/vgic/vgic-init.c | 6 +++--- > 2 files changed, 6 insertions(+), 3 deletions(-) > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index a0d01c46e4084..b97ada19f06a7 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -997,6 +997,9 @@ static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu) > static int check_vcpu_requests(struct kvm_vcpu *vcpu) > { > if (kvm_request_pending(vcpu)) { > + if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) > + return -EIO; > + > if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) > kvm_vcpu_sleep(vcpu); > > diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c > index e7c53e8af3d16..c4cbf798e71a4 100644 > --- a/arch/arm64/kvm/vgic/vgic-init.c > +++ b/arch/arm64/kvm/vgic/vgic-init.c > @@ -536,10 +536,10 @@ int kvm_vgic_map_resources(struct kvm *kvm) > out: > mutex_unlock(&kvm->arch.config_lock); > out_slots: > - mutex_unlock(&kvm->slots_lock); > - > if (ret) > - kvm_vgic_destroy(kvm); > + kvm_vm_dead(kvm); > + > + mutex_unlock(&kvm->slots_lock); > > return ret; > } > -- > 2.39.2 > -- Thanks, Oliver