From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407450; b=nnHW4i1Z5gEA/hTHe9W/GItTfhWw3Vz43HpuL5dSkvVCrRbMNZK8+MG/8SlX2wGc7ROU u0kl4LfPVyK07dY2QI6b744AZePd44LVAfbOl8OgVe2Fa6Ge01vNEXbVhBWjPHPxQRK+2 DYBvZZ+Eom94GuQNgj/Uo9B67z5dsW6uyEX/iBzpV3VVcEg12C5oFHpb6iumv2UJwOOST oFDXvYjN05ya3vu2AeH/+i0nbsUpOhuWw7G/pQlKQAW52oYOischcPGEbZ7THJ8VMZw/m Lzw4ojTorZfjteIzojcnD/ZI9+LhyopC3ZawQL4SnQp+Sn1gAhWlAHnxXIIlTEXk1eg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407450; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=oOxyZnX0TVy86L5YeLZGgUAYPjyl6kpdGa3CzdTeSzc=; b=3vuC6/E10AOFogVq1+Iv+L/hjjncZLs8J9wvq9n0QOSTMN7w0z3RV+6DVAh18XZvxQlR AfV+xvxckXkw1tHU+C5+CusWb+qa5U44bXs4ELFBwEas2JK8djmyPf4PFWpTAQ/yM+TEg rMJuar0OeCIBQCOiOH2IXkwBjKpKLVXototRc3d95p4xqkdiV4/l2OLoiFRyGJ9MDSY71 4ehrwsMqEmi9fRsfBodfyUViPFqW1oOrPU9kk+VaoySkk8kp5EBmdg+NKBKF2+krtxG5t DBAMA7bgZDIftaKlnGgaL2GmcvOwj059Hm66TZtbLgjvgmbNbD4iBnALe4sVqVBsbvQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=agBc0aD0; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E792F606B025 for ; Fri, 4 Dec 2026 18:04:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E792F606B025 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=agBc0aD0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407450; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oOxyZnX0TVy86L5YeLZGgUAYPjyl6kpdGa3CzdTeSzc=; b=agBc0aD0UmLv/W1GkZ3atJonwtVkCRUAS89rydGLM70C8zBoU5yiJc6QBreeqaoc70ZIYi yAh2Eb0quvT4p66mgYTtQrf6vBzPAKXyoCE6BpMfDu0ps9rnpjpUgcGmXpTFkgqyerUh8h Kac+IX7MNMaxGKc+QavEw2nauOagY38= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-413-FnrS1yBTNAi5ai4KHwUThw-1; Fri, 4 Dec 2026 18:04:04 +0000 X-MC-Unique: FnrS1yBTNAi5ai4KHwUThw-1 X-Mimecast-MFC-AGG-ID: FnrS1yBTNAi5ai4KHwUThw_1796407442 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9AC3F189C2D0; Fri, 4 Dec 2026 18:04:02 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 58F6218019CC; Fri, 4 Dec 2026 18:04:01 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , stable@vger.kernel.org Subject: [PATCH 6.6.y 12/17] KVM: Set mn_active_invalidate_count when the VM's mm is released Date: Fri, 4 Dec 2026 19:03:32 +0100 Message-ID: <20261204180337.2168031-13-pbonzini@redhat.com> In-Reply-To: <20261204180337.2168031-1-pbonzini@redhat.com> References: <20261204180337.2168031-1-pbonzini@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 From: Sean Christopherson [ Upstream commit fca5e4804655f39e40cf302ca37f6bd35445881b ] Set mn_active_invalidate_count permanently (until the VM is destroyed) when the VM's mmu_notifier release() hook is called, i.e. when the VM and/or its process is dying. This will allow using gfn_to_pfn_cache_invalidate_start() more or less verbatim to invalidate all GPCs on .release(), without having to play games with the sequence count. As pointed out by Sashiko, a racing hva_to_pfn_retry() could set its GPC valid after re-acquiring gpc->lock if it's not forced to retry. Isolate the mn_active_invalidate_count change on the off chance it causes problems. Elevating mn_active_invalidate_count should be a-ok because it's already possible for kvm_mmu_notifier_release() to run while the count is elevated *and* cause it to remain elevated, as .release() can be called between .invalidate_range_start() and .invalidate_range_end(). If that happens, the matching end() will never occur, but kvm_destroy_vm() will squash the count before freeing memslots. Note, there can't be any in-flight memslot updates when .release() runs, as KVM only does memslot updates from process context (except for the kvm_destroy_vm() case). Adjust the sanity check on in-progress invalidations to account for an invalidate count of '1' now being the effective baseline when the VM is being destroyed. Opportunistically tweak the comment to call out that unregistering the MMU notifier between start() and end() doesn't *guarantee* KVM should observe an in-progress invalidation, because KVM tracks those invalidations if and only if there is a relevant memslot. Opportunistically add a TODO to call out that KVM can stop squashing the invalidate count once x86 stops abusing internal memslot updates to unmap VMAs for KVM-internal memslots[*]. Link: https://lore.kernel.org/all/20261030212112.2598640-7-seanjc@google.com [*] Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261120195742.55768-5-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 42e147975795..193c0dfea928 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -923,6 +923,16 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn, */ WARN_ON_ONCE(rcuwait_active(&kvm->mn_memslots_update_rcuwait)); + /* + * Elevate the invalidate count to prevent concurrent walks of the user + * page tables from fully succeeding if they race with teardown of the + * page tables. The elevated count will force lookups to always retry, + * until eventually the lookup fails outright after teardown completes. + */ + spin_lock(&kvm->mn_invalidate_lock); + kvm->mn_active_invalidate_count++; + spin_unlock(&kvm->mn_invalidate_lock); + idx = srcu_read_lock(&kvm->srcu); kvm_arch_flush_shadow_all(kvm); kvm_arch_guest_memory_reclaimed(kvm); @@ -1334,22 +1344,26 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm_coalesced_mmio_free(kvm); #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); + /* - * At this point, pending calls to invalidate_range_start() - * have completed but no more MMU notifiers will run, so - * mn_active_invalidate_count may remain unbalanced. - * No threads can be waiting in kvm_swap_active_memslots() as the - * last reference on KVM has been dropped, but freeing - * memslots would deadlock without this manual intervention. - * - * If the count isn't unbalanced, i.e. KVM did NOT unregister its MMU - * notifier between a start() and end(), then there shouldn't be any - * in-progress invalidations. + * Sanity check KVM's tracking and internal use of start()+end(), e.g. + * to detect a unpaired start() or end(). KVM increments the count in + * kvm_mmu_notifier_release(), so the count must be at least 1; and + * if kvm->mmu_invalidate_in_progress is set, then KVM unregistered + * its MMU notifier between kvm_mmu_invalidate_range_start() and + * kvm_mmu_invalidate_range_end(), so kvm->mn_active_invalidate_count must + * have been elevated further by kvm_mmu_notifier_invalidate_range_start(). */ - if (kvm->mn_active_invalidate_count) - kvm->mn_active_invalidate_count = 0; - else - WARN_ON(kvm->mmu_invalidate_in_progress); + WARN_ON_ONCE(kvm->mn_active_invalidate_count < 1 + !!kvm->mmu_invalidate_in_progress); + + /* + * Squash the invalidate count so that arch code can update memslots + * as part of VM destruction. + * + * TODO: Drop this once KVM x86 no longer relies on setting internal + * memslots to munmap() the corresponding VMAs. + */ + kvm->mn_active_invalidate_count = 0; #else kvm_flush_shadow_all(kvm); #endif -- 2.56.1