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 36192CF11E9 for ; Thu, 10 Oct 2024 13:04:41 +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=k9FWqyZ5+Qd8Mv7DhvoiAvsS6GJsJOjjHBFrIN6W7Ro=; b=qjuNekiNGy8UBxBN6G3RzbFzPE 1cnNb6ug206M0BjDLY/jS7gtf9AOgQnCjSlHlUG/DNiBZ1PlfMTNrhQMTb8wh7aYqDFbjgEJahfnR xrPwkcMs95SWcHG85MTKmOdryOYvH+3jejDgNCQOp09r1BWrAlRoWhUDZgMkOFcqUDrkFIQg2/v1+ b3xiT3yby2nJYjVTKCjfAZYkKQtj5UQrbvUH4UJzB4bgGcyQraipgSqvfUaPDijrZYQQjwdxkfxAP 5vPYV32I09Lbb2WlSjeWy27kXjnJ0UYXrH2hKfZycqtTLrK1a0XuC5Aply+kJKpJhp1nRiSJfF5Fs v1EzNGWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sysqF-0000000CqOB-0gay; Thu, 10 Oct 2024 13:04:31 +0000 Received: from nyc.source.kernel.org ([2604:1380:45d1:ec00::3]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sysZR-0000000CmUf-1iWu for linux-arm-kernel@lists.infradead.org; Thu, 10 Oct 2024 12:47:11 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 07E60A444B3; Thu, 10 Oct 2024 12:47:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D5ECC4CEC5; Thu, 10 Oct 2024 12:47:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728564428; bh=SP9MED2OcZY+ELggC9MXpPB7Hg/40uqQ0bfhipI8oN4=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=HnVQ/ZeEHx78m5NLTbAXpSAxfiUxCSrMaAsmyHeZ+JyK6fziJxUQHyjPZ/h7/IhtD zYkxDqG/PMovCrjBYsGZFhtx/2dBeFA581G3SrO/YAiPJJOJ+rG1MOggCqQtWexuW5 8GQSFHErl2RA+Uxc0Rg+qU7ZTjIKSjxuPgiFoOk30rPoszzO4L8B21Y+lSFkagW/dp YNPtmaUpVvm3ysp5ndo+/ud5TQDGgy/my/8hmJx35za/346/GIk13zPUcweSvY7Rfz T0ztAKyGRXEijRglGyjPexUkLBkjADc0lUU686mPUeJmsYhabegL7PYZ8LAo/sZWyC JT9d0gOQFigSw== 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 1sysZO-002BTI-0F; Thu, 10 Oct 2024 13:47:06 +0100 Date: Thu, 10 Oct 2024 13:47:05 +0100 Message-ID: <86plo85dme.wl-maz@kernel.org> From: Marc Zyngier To: Oliver Upton Cc: Sean Christopherson , 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 In-Reply-To: References: <20241009183603.3221824-1-maz@kernel.org> <875xq0v1do.wl-maz@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: oliver.upton@linux.dev, seanjc@google.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, stable@vger.kernel.org, glider@google.com 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-20241010_054709_618302_F177A296 X-CRM114-Status: GOOD ( 38.68 ) 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 Thu, 10 Oct 2024 09:47:04 +0100, Oliver Upton wrote: > > On Thu, Oct 10, 2024 at 08:54:43AM +0100, Marc Zyngier wrote: > > On Thu, 10 Oct 2024 00:27:46 +0100, Oliver Upton wrote: > > > Then if we can't register the MMIO region for the distributor > > > everything comes crashing down and a vCPU has made it into the KVM_RUN > > > loop w/ the VGIC-shaped rug pulled out from under it. There's definitely > > > another functional bug here where a vCPU's attempts to poke the > > > distributor wind up reaching userspace as MMIO exits. But we can worry > > > about that another day. > > > > I don't think that one is that bad. Userspace got us here, and they > > now see an MMIO exit for something that it is not prepared to handle. > > Suck it up and die (on a black size M t-shirt, please). > > LOL, I'll remember that. > > The situation I have in mind is a bit harder to blame on userspace, > though. Supposing that the whole VM was set up correctly, multiple vCPUs > entering KVM_RUN concurrently could cause this race and have 'unexpected' > MMIO exits go out to userspace. > > vcpu-0 vcpu-1 > ====== ====== > kvm_vgic_map_resources() > dist->ready = true > mutex_unlock(config_lock) > kvm_vgic_map_resources() > if (vgic_ready()) > return 0 > > < enter guest > > typer = writel(0, GICD_CTLR) > > < data abort > > kvm_io_bus_write(...) <= No GICD, out to userspace > > vgic_register_dist_iodev() > > A small but stupid window to race with. Ah, gotcha. I guess getting rid of the early-out in kvm_vgic_map_resources() would plug that one. Want to post a fix for that? > > > > If memory serves, kvm_vgic_map_resources() used to do all of this behind > > > the config_lock to cure the race, but that wound up inverting lock > > > ordering on srcu. > > > > Probably something like that. We also used to hold the kvm lock, which > > made everything much simpler, but awfully wrong. > > > > > Note to self: Impose strict ordering on GIC initialization v. vCPU > > > creation if/when we get a new flavor of irqchip. > > > > One of the things we should have done when introducing GICv3 is to > > impose that at KVM_DEV_ARM_VGIC_CTRL_INIT, the GIC memory map is > > final. I remember some push-back on the QEMU side of things, as they > > like to decouple things, but this has proved to be a nightmare. > > Pushing more of the initialization complexity into userspace feels like > the right thing. Since we clearly have no idea what we're doing :) KVM APIv2? > > > > The crappy assumption here is kvm_arch_vcpu_run_pid_change() and its > > > callees are allowed to destroy VM-scoped structures in error handling. > > > > I think this is symptomatic of more general issue: we perform VM-wide > > configuration in the context of a vcpu. We have tons of this stuff to > > paper over the lack of a "this VM is fully configured" barrier. > > > > I wonder whether we could sidestep things by punting the finalisation > > of the VM to a different context (workqueue?) and simply return > > -EAGAIN or -EINTR to userspace while we're processing it. That doesn't > > solve the "I'm missing parts of the address map and I'm going to die" > > part though. > > Throwing it back at userspace would be nice, but unfortunately for ABI I > think we need to block/spin vCPUs in the kernel til the VM is in fully > working condition. A fragile userspace could explode for a 'spurious' > EAGAIN/EINTR where there wasn't one before. EINTR needs to be handled already, as this is how you report preemption by a signal. But yeah, overall, I'm not enthralled with much so far... M. -- Without deviation from the norm, progress is not possible.