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 C34D9C021B8 for ; Tue, 25 Feb 2025 16:41:35 +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:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To: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=u3OyWMfMyDgx+55Ind0kZ2lpQA8/mHOXTjN2aQVmO4M=; b=J3x72XXohRv7T5FZAgq/BnfAiX t9oyZQHIcbPffTmvVkhFPg943shOzKNdVOvUKzzbz4hk09H3NFLmKnEw3ShWe7ugvzbDeNe5jfnLk XkIX/BQEB16+qqu+nVghWNMFanjEjnEtqxoxuj2km7OagPN360DwrTYW3Y038qkzCPVtODUAwIfzu j0RYpLmUvWFv9jcneoj4/yVQIy2IqzQhQWU04xmv/FHrGTsC2jTRN1RKYH956p232MIoZjkFcLn5K 64b75fzlo+nTKOLu28bodf/f/yxDaQJcxUhs0yMT/AKEWlWAhoHo7cD52A99DIQoPw80BVTnO7Iu4 UyJ0MKRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tmxzn-00000000Jok-2aYo; Tue, 25 Feb 2025 16:41:23 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tmw2B-0000000HXro-2qBM for linux-arm-kernel@lists.infradead.org; Tue, 25 Feb 2025 14:35:45 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2fc0bc05bb5so11866480a91.2 for ; Tue, 25 Feb 2025 06:35:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740494142; x=1741098942; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=u3OyWMfMyDgx+55Ind0kZ2lpQA8/mHOXTjN2aQVmO4M=; b=S9kfpvdNGpzyzi3j8R6RiHsIS3qNbJzRjEC3VVt6zCOkmyZF8ocM2Av+nhSICpb3T8 GT8VeRUR9XyMCKFY9e1k41t2gFscKLJqwpWh6Ap4bdFRuFuk4Ed5DasZGJAUF1hBq/4X OjO0faTNHKHygp5zYjMPFaXoSVHbnLlltWEmaMroYgTuxclxJQ7L6IwjgCWSMjORylOq t5e5bdP2NTAz2/NXzIQqC4U+ADhn44G56vzVuJp3kw1xEm8xMCICDyPuIvb+9ZudaMlD 7WBqXC3Pm6WSQwdnrsBe2gbj7Wf/Gw1VuwRjZu5MCvPQeAYKaMQsWGSncmv05xa9FSC3 6L+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740494142; x=1741098942; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=u3OyWMfMyDgx+55Ind0kZ2lpQA8/mHOXTjN2aQVmO4M=; b=CzAYQntCnURZwdTE3dfjeLJrdQSgCXeP7Ah1bhbbHjl/ij7eVNfQZxAMtSCOXPOipL z6Ecf36y7Es/symIsPiRHFXCbFNdmcQd17o1H5yywSn540LxjFBG1bwroLsNeEIMt8/b 1Luk8dAfbyhTPiAUtpPsTx9msoAb69YkuWGIuzqADHNheJEaWOO6M/ABGYkyvFrfgYqp UJ+ELPQ0G6MJ2RSHmJWtkeX2ydFHWF+rPZ+4u0whtM1Ctc/as31yismHYC8hFSFsEhRr Sfy285WTub8D+7P7GfrvXVXPuTcvmPaiqXFV3rcR0lyAGXNbIcLx0GzISTCmzGUVC56b szPg== X-Forwarded-Encrypted: i=1; AJvYcCUH1uAV9N9mcJ5lDUQrRoTPMQMR+XH1FBiQY3FUujf+7Lljz3ZW4t69/vB+PRG6VD+bDoZh8jxeIOhqfGXa13mm@lists.infradead.org X-Gm-Message-State: AOJu0YwNEnxNBHxkUp2C22OJ2FAv7s4Do9ygeSFokoLhXlw/Ym+1mBBf W3nOny/h3Jdss+ji1I1eHVhZGrK0JTXbW8YXnRusPzmUJHTEYH847OvqPh9nGBT7SaqSLbLmXoe RLw== X-Google-Smtp-Source: AGHT+IFXh/XOoHWCgjyYmqDQFrw6VQ+lwV1XpaGwsHvB9OE+S78BXcvOxN0p4LNeKFLBgsLwKA4Y+N0kitA= X-Received: from pjbdj6.prod.google.com ([2002:a17:90a:d2c6:b0:2fa:1fac:269c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f8e:b0:2ee:c9b6:c26a with SMTP id 98e67ed59e1d1-2fe68ada3bemr5889698a91.11.1740494141828; Tue, 25 Feb 2025 06:35:41 -0800 (PST) Date: Tue, 25 Feb 2025 06:35:40 -0800 In-Reply-To: Mime-Version: 1.0 References: <20250224235542.2562848-1-seanjc@google.com> <20250224235542.2562848-4-seanjc@google.com> Message-ID: Subject: Re: [PATCH 3/7] KVM: Assert that a destroyed/freed vCPU is no longer visible From: Sean Christopherson To: Yan Zhao Cc: Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Madhavan Srinivasan , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Paolo Bonzini , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Aaron Lewis , Jim Mattson , Rick P Edgecombe , Kai Huang , Isaku Yamahata Content-Type: text/plain; charset="us-ascii" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250225_063543_711343_2401F073 X-CRM114-Status: GOOD ( 23.92 ) 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 Tue, Feb 25, 2025, Yan Zhao wrote: > On Mon, Feb 24, 2025 at 03:55:38PM -0800, Sean Christopherson wrote: > > After freeing a vCPU, assert that it is no longer reachable, and that > > kvm_get_vcpu() doesn't return garbage or a pointer to some other vCPU. > > While KVM obviously shouldn't be attempting to access a freed vCPU, it's > > all too easy for KVM to make a VM-wide request, e.g. via KVM_BUG_ON() or > > kvm_flush_remote_tlbs(). > > > > Alternatively, KVM could short-circuit problematic paths if the VM's > > refcount has gone to zero, e.g. in kvm_make_all_cpus_request(), or KVM > > could try disallow making global requests during teardown. But given that > > deleting the vCPU from the array Just Works, adding logic to the requests > > path is unnecessary, and trying to make requests illegal during teardown > > would be a fool's errand. > > > > Signed-off-by: Sean Christopherson > > --- > > virt/kvm/kvm_main.c | 8 ++++++++ > > 1 file changed, 8 insertions(+) > > > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > > index 201c14ff476f..991e8111e88b 100644 > > --- a/virt/kvm/kvm_main.c > > +++ b/virt/kvm/kvm_main.c > > @@ -489,6 +489,14 @@ void kvm_destroy_vcpus(struct kvm *kvm) > > kvm_for_each_vcpu(i, vcpu, kvm) { > > kvm_vcpu_destroy(vcpu); > > xa_erase(&kvm->vcpu_array, i); > > + > > + /* > > + * Assert that the vCPU isn't visible in any way, to ensure KVM > > + * doesn't trigger a use-after-free if destroying vCPUs results > > + * in VM-wide request, e.g. to flush remote TLBs when tearing > > + * down MMUs, or to mark the VM dead if a KVM_BUG_ON() fires. > > + */ > > + WARN_ON_ONCE(xa_load(&kvm->vcpu_array, i) || kvm_get_vcpu(kvm, i)); > As xa_erase() says "After this function returns, loading from @index will return > %NULL", is this checking of xa_load() necessary? None of this is "necessary". My goal with the assert is to (a) document that KVM relies the vCPU to be NULL/unreachable and (b) to help ensure that doesn't change in the future. Checking xa_load() is mostly about (a). That said, I agree checking xa_load() is more than a bit gratuitous. I have no objection to checking only kvm_get_vcpu().