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 B1898C021BE for ; Tue, 25 Feb 2025 16:41:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=hNkL+ThQM30VmyB9USmwTf/yWelub296Q7D9em3av/k=; b=eClU0l9nVSYsZPaCCKprkMbn5A gE90qlnn30F7wXciS5fLVtzxnkCN+0pVoEpejvknLNz5fJuj6J1E/zgudCCHTBQDlYPx7+nex83YD e+WGrevWPPwhy8jJcF5lDOLYfZymx3sUhuYjncQDSlwQFxRE55hsXg/9cK1KNZOgz2miL1aa7T4za BhJbB19vipUg5XI8ayEFLHAVjFMKSu5B10xzapLuotlNyrwi/nVYdS2OcQ37a8WwQXcWdR2E1T36c GHgiRJ9RnB2oYvF39xWV6+6lItYs49KZWpm1cqQh1Uzmcqh/D2MZPyQFID7a3cs+CgTTw1nM2Xen4 JseAqGyQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tmxzk-00000000Jjg-1eTt; Tue, 25 Feb 2025 16:41:20 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tmw2B-0000000HXrp-0M5a for kvm-riscv@lists.infradead.org; Tue, 25 Feb 2025 14:35:44 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2fc1a4c14d4so11902096a91.0 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=HFIdGTnM0O94GasDiZINZZwUg+/en1DsnGeLYplvhMtj7rh45HA2x58Mog/HmscB/C GN9f20mGAxFBCP2JzyJsjFgWF6PKZFlp2c9vZb1atG+PcRHWnLYbXrNHoG5+wP4Q+AlJ /VEh2wgYDPnl14IJ9PSnLqFhwE3GyCYeRuPCL+XJLcAZ5NweUXiq76jNRhFcPH7dlWr/ 1v+QzcN4IbahdAIOlP2nXgn+2G7HlM3j/kemUXWfwa0y/VxkkqDqNST6vGc8AqpxH5aY ugAtGmRl6MVqlgH/sWyJOZUtyVyOste2zrN6GJ2nS6YWLG3XRxybglkxQRmrDYon+iyp Y6SA== X-Forwarded-Encrypted: i=1; AJvYcCXLgrWHDJEi+5e3QiHiafB/gVC+6Lu8EZ5zNly1/OC0/9OjMcIxtr83zCcaWIeMiJNv7qsrFE/QCfs=@lists.infradead.org X-Gm-Message-State: AOJu0YxLXGQ/OpsD05DDzxxKzMj+SA37OXhgqRUCSQaXLycj9L5n54Q0 KlMRneZJeYO6/ZPTJBTnfOGZVvSXY2Ea5jtuP4VBgSBxuERuf3so0n4mbXbP8cT8ba0aEpT9mAL Kaw== 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250225_063543_118397_26668313 X-CRM114-Status: GOOD ( 22.36 ) X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+kvm-riscv=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(). -- kvm-riscv mailing list kvm-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kvm-riscv From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4CB5280A2D for ; Tue, 25 Feb 2025 14:35:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740494145; cv=none; b=kgiHkspgoveKmK2pHEo/XyCk8XzpRBR5/pjI/UgPpw0RM9vNXoql07i5biocvjcSBCC0DZ2badbL+mgIgtbEtmqWT4TI3KvfuxZQhu9Um+rARp5mhkJB1Q3ixO3sHL21ZIN4xjtjGhWwxLIN4R59uYU9t+dqL6b4jUYLSH1L8k0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740494145; c=relaxed/simple; bh=9hyuXWtcrt6qYFRuvHO0bnjXrHF/CkXG7yP6ynsJrjs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ik1BWdoE8GrPFiOKFS92bGao08UGyMsjxdDiu8MgWQooaWh0L2aDU32zFzbb4UgDGB0AyL9ETVAl6ifjhAnbhfEQP39jdTYob3jTiLqmo/hy6rlZAev2mvqVSVN/fpygnQf57cUrJdCTLlFl+f3OEbaOtfKVWD0FxxuqnZEhTzQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4TBIAJdx; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4TBIAJdx" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fc1a70935fso11879968a91.1 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=vger.kernel.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=4TBIAJdxZuNNjUSEmpRBFjteVkgFAruncnIrqJ4LUXIwes9texKhNKx3yNMu5nj2Of +1XkL1NFqyOB7QG8mnvtMp3ijTF95zE0RA4z2SvTGB1nlPPiW9hdQLxReFkrEDpbSiSL VYUBQmJ7bUXnfeYuKh4q93nUVzNyWLbYgJ37mfr/ueg8MUL1N/4IODoeGagy715MnVN4 ywbuIQyb3eZM+wwMI/jwVXmUkCe3X3S+L0yifLjE0mVl4BlROlAbA+vAY7JJmp0gOcbA 1RMPsJLfSaT94Kzk61YdJCSnh/K5L0Gs40VHLn8BCvMlLFRT7qR9AJTDLw8cOq6Kw2ii 0lvA== 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=PijLxdvUMt7+u7fSqSp+BZ5n8/3hDgYd/s1arZL/cLKx7zoxIHtvW2WBMRYUFp0L3Y sDq3a/P1CvFE8k63k/Rz3RwcNIG19TfU8igJwl5p4a3T3kGuSjuYnmEND9k+jkGUr29h TETZgvXBdFCJZsCRqgEk5ksO7pndXH+QGUHdxUHvF3u3qcbLhUS0erqV9wlC1w0Cpyok /o0aoM/t+sDgw7aJE10JmAVhf6kmvYWhLww4DpBPAWpMtT8hpCQcnH91pou3DP4iZgr0 ou7PW3j1QLPjQ6FviRy4+/Yv/iRMAkIBYIoalh1ZO33E/iQ32qJaBlj4gdZs4yAhQI0b lrbw== X-Forwarded-Encrypted: i=1; AJvYcCVwW4+MyXHbzHi3r+onQRWUyVFJEPQQqavggyC9o2aWSsz5yI8DDWDYs2NE/bi4L6GM6NE=@vger.kernel.org X-Gm-Message-State: AOJu0YwDzymqIbBVs4+9jd8hIrkIPIQtew+8+IF96IntSep5Z/CHynMA H/7gpBSaTq9ycVFO5bvNKbxGuEmkWBZ1ruRVgqZlY25OcdtXQcMFoyHKLcEb1AZKU+iO/EyxVGq jCw== 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: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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" 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(). 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 14F2BC021B8 for ; Tue, 25 Feb 2025 16:41:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=n0vbRGg6u1bGJYt+j1IgmYnfd2UWZJz4RwcLCLE9jL8=; b=ia9u74kC+0mwXLPiTY7zN9aCye jW09JS2jpZraaQf50NZi9YMLeaL2sSO/H76oiG08L1NbxGF2W0nx2p67sccOg58MpHyk7IEvlKkV3 1wQ7l07xUfwgDO1NE4b/DeYAYIBRIC+wpfGiwGsJiHwepEKRgNaUS23OL3dggZiupNEwicfialUIS aew2T3gLcFviq0MslOc8q/japWHiBrrPi1JRduex2K1ngYkDvCO5eV/yV1cAdZvGKVv7PN9t8eeu8 /YwLk20YXPGmVeDjNNJTJ3rbnEa5AD2k+0y4KKmOwjNnPurmvQ8FXg2YSFQmkLjhEwXGpQG3cpWNI XO5yUnlQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tmxzk-00000000Jkl-3xU1; Tue, 25 Feb 2025 16:41:20 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tmw2B-0000000HXrq-0J1H for linux-riscv@lists.infradead.org; Tue, 25 Feb 2025 14:35:44 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2fc1a70935fso11879973a91.1 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=vRyVk5fh4JxhfKWc1d2NRf4j+S5jCE8vDPyhaKg8lkgeKEVOBGHbMatnn0KmTB10bX hZezweLi46p6GutKf3WraA4GmEJwEW042Bh+kOJ2QgGh2AppiPZXRaJC6HGo0eq3LA0u 1B+2o+AUkwzw4c9uAnM45gtDuBQNnlF93Y+XVhqqYvy8UALndNXc8G5MKlr/S2Jk+fLN +Jxs/XG9zMlriaJXRjy+wmflGumXXsSFH0+X5t3pbbYblr8Xc8PDwc15ZSbACU8chzfl fYKSlWcCVRypFW/TU+G1ai/zPalC2OPqMlr839t8ww1zaF/jml/+ZRN58uZ2CkaqWJDk 5OLw== X-Forwarded-Encrypted: i=1; AJvYcCW06ZVGvmK/UTvcg0HEF5F8BTIKkQmFL2E/xH840wo9mHBylZbA6z9wckKtbQ3lhonJXiRxeMFRDO5iQw==@lists.infradead.org X-Gm-Message-State: AOJu0YwNovchNSEic0gR3ELjcC7IiJ9Pb/RSpYqxSWEDWtRI4PBebgzL AiVjGTsbo96kEpPJP5IChMhOZMaWDUnh7UH2UWaK7AUWefiDz3r5efbfzXbXXw0S7Usxxstdx3+ IHQ== 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250225_063543_118895_D6259AAC X-CRM114-Status: GOOD ( 22.39 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=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(). _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv