From patchwork Fri Oct 27 18:21:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438931 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24FA3C25B70 for ; Fri, 27 Oct 2023 18:22:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE8A68000B; Fri, 27 Oct 2023 14:22:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9784900002; Fri, 27 Oct 2023 14:22:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B74898000B; Fri, 27 Oct 2023 14:22:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9F058900002 for ; Fri, 27 Oct 2023 14:22:36 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 78904A09D1 for ; Fri, 27 Oct 2023 18:22:36 +0000 (UTC) X-FDA: 81392061912.23.7BC16AD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 9796F100013 for ; Fri, 27 Oct 2023 18:22:34 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=w5+VIBQE; spf=pass (imf14.hostedemail.com: domain of 36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430954; h=from:from:sender:reply-to: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QjG+iTAl+MysAoCDX0shLrWGCG7e9aCGlo9+qs2tVnE=; b=z8/ipqdNABnooU1oXL557bhvG85dvcw7IO+eWqPcbIyp/tKqvxWXTZnRqPQ73K7FB9LDiD ci4e4Qlf9/07bW1kmMzWnenOSu8oPs4WhE+W765GfW51J+SGGr/VWKnw6SgnE3y77nW/iw 41iV8dtXoOmKGp356nSX2aS80/0b1sU= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=w5+VIBQE; spf=pass (imf14.hostedemail.com: domain of 36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430954; a=rsa-sha256; cv=none; b=q3cf7C07bDIhZls3pNCdnqSXH66C0vOHGnKTuhdHhyyspjJFV/qGIVlc46AGn84S4hbm1+ RLfz89zkSX5MPm1t11jIE9pLURc/VOTT+eq8FV4ZdPHbMYcbY+0mdMorvbdsFywteZQ8Ub UCt5u/AJH4U4nKYryUjGBzKftAwheL0= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5af9ad9341fso10343367b3.2 for ; Fri, 27 Oct 2023 11:22:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430953; x=1699035753; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=QjG+iTAl+MysAoCDX0shLrWGCG7e9aCGlo9+qs2tVnE=; b=w5+VIBQE9Tx1WO0HRye876yZc9hKO+piTxbvaOVlYmrB/Kz0GuwWN5FwR3He10TnCa PqKjXU2KvxK8zUEO1i2DACiOoABF4AyM9Uv/kn6k0D3rKdYnuIk198p3bVJr15KSTzEI rj6YH5UPB6xRfFtKt5jGlNdwft/qzuWVJH3nd8DIf8JnuNNhmki7i74ize11q2B1dH9H X00wP+bN42ljEoy37DhcGsbYQPalG10tuol+XO4Djw9Q6jIYT3K9EcrTP12G3V+pJQ0j ltS5l6+Az4V7n3r5Ugncc3zKhYFJYoJkFpK6oqhYzpBeA2fidc+Zmj3UumMcv2omYnnM Zbgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430953; x=1699035753; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QjG+iTAl+MysAoCDX0shLrWGCG7e9aCGlo9+qs2tVnE=; b=d+FnabI4ApbTBGl65r1TYcCf6R3A0nBGePL5xIfO25I73k7nGOBEF1yf/E2k0j6Ylc 12NYQvahMvRX1So4IwtrVoSI909ZXmlH+tmSnoR5xSBwof58LwP0hfhdQN3lxH5gyyB1 hgzWuLd7GG+Rx+ixqFTcCdbrmMsFeRQh0v8pP6f4EsJ2XDzQRjDsqKGARq7YscXchKH3 09R+X3EzR7xilXv4Tl1zSOcKuZHZUS5XnkOOCWtLWJnECJ4lIVoWgQ7getcnJ6Q7YQ08 BRDgc1tev43oCgZqlnCPR+6dRUY7Gd/1ksgqUKLvd1bOJtS813KZysEt3C98dj1cuOnL GlkQ== X-Gm-Message-State: AOJu0Yzt3K+e7wApL9DiZImCT8FquktD4mhaaRAnxERmfRmSPdwmPrgb 2oqOFOV/87gkM/MIbdmGSt8PazrLXno= X-Google-Smtp-Source: AGHT+IFjdgmBdyxBQ0zr6Phdzdc3NhfRHFKE/+Wea1IZiVOT7T4IBQHOPkTpo2hU6PU86u42a7hH74NJYwo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:da0:567d:f819 with SMTP id v3-20020a056902108300b00da0567df819mr78680ybu.10.1698430953673; Fri, 27 Oct 2023 11:22:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:46 -0700 In-Reply-To: <20231027182217.3615211-1-seanjc@google.com> Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231027182217.3615211-5-seanjc@google.com> Subject: [PATCH v13 04/35] KVM: WARN if there are dangling MMU invalidations at VM destruction From: Sean Christopherson 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_Sala?= =?utf-8?q?=C3=BCn?= " , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Que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ad+TgzpAD k41dkiiP m5CFwHcsauAemF5fh4bJ305vQ7/VJ9mNNf6SSYSjIRANdtiA6CPCSmMDgUWXtR5V/9vVaUl/cg1Hit6hRNmHgged3t+EDbb5nZcCnOpr1G+lbiBU3UPOCBke/jpQybZiiGP2MrGWCA1C76A8QXGy0YgxWndAIjvybLaLJajZG8PoLlLvgQgpBP80csC6sezFfgoFl/aXI/e9ld9NfAMzLwX++g0AKOkZmced8em55v1ul8HeM5njXnmdHO2MiWxlXSe/ZL/chq6/fSChEYJmHJerDlFv6l64HyxBteHGs3q1A+CqXG23MvdMaT4KbU6st4ZPrqrsxj/rgljMNg7UU9chmHAQYfSstDJEBpCdOQQkelKvo+5edQlD1bdXrJ8WYU5zsk6kAkAuhqUYp7T+4lQ2Z+AJ89BnbBWD+CMf4ishHQkEo2ldRo6nBOjU4OgmLrE9sph0NYNFX2W37xp6UU5PXmy4WwdS5rzQfA2Bc9Lax45BdfzalXSZmw/zhuWkMzpzxNrgrG64nBwz+fA2ixz3ypc5daF13fzplBgu8+6q4zg3/mvdLhP74bJuBaYlFZPZIC/6v8Y9p3qb/7TCvbNZMyZZIyFL3850bSGA4H/emy5pJjVb0nl4tSy21QxHGp/oPJPXP71HsVPf2OqF9h5XiqjWMQKnf85fsxonb30Mv7uWsTtDKWY9WJZSHFbbD6e9HgZzAKMvS70MX4tCRJIzLc3ph6W1YIXZp1ov4oqDnYFOCvH2aQoP9u1PAsUFGElI/MSILFmsKmGL8vPU56TawfJzQfq6NIw2uBHa2OX/1zH6C1oS6+SFk9TTcKILcwsIR7coIFkkusQPVU9IzcK86u9lGwIT2ZRl0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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 1a577a25de47..4dba682586ee 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1356,9 +1356,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