Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1660805rdf; Sun, 5 Nov 2023 08:32:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtB+IjMXZX2dhleEg64Z5VvNpAHGZd5ZEaZz6PWhsGEa+4M+8qOyT4q4s5CERe0IKzdfSM X-Received: by 2002:a05:6871:514:b0:1e9:c1e0:5a97 with SMTP id s20-20020a056871051400b001e9c1e05a97mr28879617oal.13.1699201972379; Sun, 05 Nov 2023 08:32:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699201972; cv=none; d=google.com; s=arc-20160816; b=KDpFU43+AZ4nrwFpVSstlFXsqCD4zl+OcLT2DKZ9hodFhxCQu9iUij6knBrfqjPZuh GSD7g1HnXIuP/ipMiobIqbB31SgIbXJowq/MTCrdVowTk7zabCHYvccih3ewbBmrIAP6 vM8qTYdy6T8wTHiAQNvscJzO3ox0MkCDiElunfwuLkQSb3MfjMlDhREgNLi6rR9BIZ10 785wvmL7r3n3lVJvyUZewdczjEVVP7R1tOmLwY9NnVO6iF436Y2TNnJyXbVSdtjLcgWZ lqbkZdtf3u76EPqc9VU7oU0Kag4E1QxSKUx7hI+N26WSNLgM5jb+t64e3YNL5y/l1ycc HzQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ts/ge04a2OEPPQrJdr5ialSNwsy6vefm6EL3ylmuQZA=; fh=HrMuB0qA2qw4RqBwyiyN+RBlGw+Ob1hcALwHW/PphXs=; b=ugwMX1KKlUDJMAEwXad12AuFS+KgWI1X1bDLkJze7dzcJE8De+0cHmTGG3l9DhhVTd nNulu/y2a6oheaRUr89gjajocyxu+WbANSFsQAjgcip+L9uxuVmJow6He8txGvf3aq7X Ge4uXmYELoiIPR/YglHieJ4EaeWL0ggOPWs1xWA2al2M4irvmRC7GvWDX0mTV6uBemOd 7ZHaVASMt9xP9zThBPv7ckYejDalhjzUsbhfs9HGl81uKHhTft0T0SvbcgiDqjxYLMwN eiCF0mGVm+yA8K8ojZRQMbr3yAa+sV071/7sTZCyOY+s7ddqkpwbLF4HIAdIlW3tT4sN 8emQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XPLcUrwy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id o128-20020a634186000000b005777bea0b6asi5684924pga.859.2023.11.05.08.32.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 08:32:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XPLcUrwy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 091848057B02; Sun, 5 Nov 2023 08:32:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229998AbjKEQcS (ORCPT + 99 others); Sun, 5 Nov 2023 11:32:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229669AbjKEQcJ (ORCPT ); Sun, 5 Nov 2023 11:32:09 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DD66100 for ; Sun, 5 Nov 2023 08:31:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699201884; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ts/ge04a2OEPPQrJdr5ialSNwsy6vefm6EL3ylmuQZA=; b=XPLcUrwy2KTfH1NMMrnmlvV+iklSb8p+AHM6jEnH1eV9ICTFOey3lmbYxgnst7jM0qhij/ wpRBGmbSSySkYV41dYjDKvY+X5ETW7wPb1unUNoqe1gahUyAO4GLPCjjNaMi4XR+t4b5j3 BTkfh1cw3lSMLHBEfaQeCxGFMSp7zJU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-66-o8kgVV1dP5GILnrn4TVhRA-1; Sun, 05 Nov 2023 11:31:21 -0500 X-MC-Unique: o8kgVV1dP5GILnrn4TVhRA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3AD20101A529; Sun, 5 Nov 2023 16:31:19 +0000 (UTC) Received: from avogadro.redhat.com (unknown [10.39.192.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6E18E2166B26; Sun, 5 Nov 2023 16:31:12 +0000 (UTC) From: Paolo Bonzini To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A. Shutemov" Subject: [PATCH 04/34] KVM: WARN if there are dangling MMU invalidations at VM destruction Date: Sun, 5 Nov 2023 17:30:07 +0100 Message-ID: <20231105163040.14904-5-pbonzini@redhat.com> In-Reply-To: <20231105163040.14904-1-pbonzini@redhat.com> References: <20231105163040.14904-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Spam-Status: No, score=-1.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sun, 05 Nov 2023 08:32:48 -0800 (PST) From: Sean Christopherson Add an assertion that there are no in-progress MMU invalidations when a VM is being destroyed, with the exception of the scenario where KVM unregisters its MMU notifier between an .invalidate_range_start() call and the corresponding .invalidate_range_end(). KVM can't detect unpaired calls from the mmu_notifier due to the above exception waiver, but the assertion can detect KVM bugs, e.g. such as the bug that *almost* escaped initial guest_memfd development. Link: https://lore.kernel.org/all/e397d30c-c6af-e68f-d18e-b4e3739c5389@linux.intel.com Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba Message-Id: <20231027182217.3615211-5-seanjc@google.com> Signed-off-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9cc57b23ec81..5422ce20dcba 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1358,9 +1358,16 @@ static void kvm_destroy_vm(struct kvm *kvm) * 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. */ WARN_ON(rcuwait_active(&kvm->mn_memslots_update_rcuwait)); - kvm->mn_active_invalidate_count = 0; + if (kvm->mn_active_invalidate_count) + kvm->mn_active_invalidate_count = 0; + else + WARN_ON(kvm->mmu_invalidate_in_progress); #else kvm_flush_shadow_all(kvm); #endif -- 2.39.1