From nobody Tue Nov 5 10:32:54 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63C01C25B48 for ; Fri, 27 Oct 2023 18:23:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346377AbjJ0SXs (ORCPT ); Fri, 27 Oct 2023 14:23:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235194AbjJ0SXC (ORCPT ); Fri, 27 Oct 2023 14:23:02 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28D111718 for ; Fri, 27 Oct 2023 11:22:48 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1cc29f3afe0so4664955ad.2 for ; Fri, 27 Oct 2023 11:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430967; x=1699035767; darn=vger.kernel.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=fLrd3AGy66HdihfPmI58EyyG3FCGELBv0TZMEZAIG4Y=; b=3xT/ci44KKzjmmrpAao7ngJthUkPCvXpRnXckArfxBa8Zr2fJkiV/GGCTxm0vyYgnh vv6yfcs4BneSBQUXt/mB0YsqyBZ5wbzwqFoo9Bod2Vk1MyjcsTDxUyOaqvrVCPcPa38Q 32ttnMXcYZbWGrhfz3tqW/C/cXNeWarTxG3N24uN5SnPll/wesBAT3TLmemoDU1LtMo5 WiucU7uTpzEHtyi7rLFqaOet9e3z0o/w1AOA4cn6qjtbOCIhZzZy910UQftKJeSXkd57 vmfr68Fie5ywyab18IL2u0ebB08b0G3NaN81dOAHYtcfnhwyH5rgn4AzwmqQ41JLdjya AovQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430967; x=1699035767; 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=fLrd3AGy66HdihfPmI58EyyG3FCGELBv0TZMEZAIG4Y=; b=CEZYxF9HQKxF0S/Cd0m0eLJIdy0gkqvRIDox5628LahIh1pCNTqDUp5PLHKUDFv5H6 gKvFLRocTYJ5J9yV8JwQVR/jhVJnnrRPfvqNAXJ0Vf7y5ZomnHjXtcoRDjIpuB3janNq ruxBab9QLlL+J0TQTexcX6SQ5UODJgfi8hQQoOqh7P7+Zy5FCC9D6sPKmgFJ9X8V6y0K LyKsfVVIUeN3+V7Vw8A018yqAENPBGb/pmerT/809GzJyLA6ukcYt4SeFPK9GmC5vG1U 4RcPw7o+6f83ou4i4iyT5p18ByX5B7VN7hTx4oEe1HlnVGxSIKBH2V2rEM8Z3fzqnxpt XSrw== X-Gm-Message-State: AOJu0YwLHzOL4c9e6XIiJy3LniYbsYlUPE8BpN3Sk6vKY0E+RJHjym6F QcpZ0SI9ZVrDua1ZshuLu31fMjJvaQE= X-Google-Smtp-Source: AGHT+IFw7CMiVxkdfLmPKgztX5f1RnQlL4BTlZlVKLxMz6aRVtHUrQY578kvMdkjnRTl6BBw2VS25sQYIRo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ee14:b0:1ca:b952:f5fa with SMTP id z20-20020a170902ee1400b001cab952f5famr63161plb.5.1698430967325; Fri, 27 Oct 2023 11:22:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:53 -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-12-seanjc@google.com> Subject: [PATCH v13 11/35] KVM: Drop .on_unlock() mmu_notifier hook 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=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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the .on_unlock() mmu_notifer hook now that it's no longer used for notifying arch code that memory has been reclaimed. Adding .on_unlock() and invoking it *after* dropping mmu_lock was a terrible idea, as doing so resulted in .on_lock() and .on_unlock() having divergent and asymmetric behavior, and set future developers up for failure, i.e. all but asked for bugs where KVM relied on using .on_unlock() to try to run a callback while holding mmu_lock. Opportunistically add a lockdep assertion in kvm_mmu_invalidate_end() to guard against future bugs of this nature. Reported-by: Isaku Yamahata Link: https://lore.kernel.org/all/20230802203119.GB2021422@ls.amr.corp.inte= l.com Signed-off-by: Sean Christopherson Reviewed-by: Fuad Tabba Reviewed-by: Paolo Bonzini Tested-by: Fuad Tabba --- virt/kvm/kvm_main.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 2bc04c8ae1f4..cb9376833c18 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -544,7 +544,6 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mm= u_notifier *mn) typedef bool (*gfn_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range= ); =20 typedef void (*on_lock_fn_t)(struct kvm *kvm); -typedef void (*on_unlock_fn_t)(struct kvm *kvm); =20 struct kvm_mmu_notifier_range { /* @@ -556,7 +555,6 @@ struct kvm_mmu_notifier_range { union kvm_mmu_notifier_arg arg; gfn_handler_t handler; on_lock_fn_t on_lock; - on_unlock_fn_t on_unlock; bool flush_on_ret; bool may_block; }; @@ -663,11 +661,8 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_r= ange(struct kvm *kvm, if (range->flush_on_ret && r.ret) kvm_flush_remote_tlbs(kvm); =20 - if (r.found_memslot) { + if (r.found_memslot) KVM_MMU_UNLOCK(kvm); - if (!IS_KVM_NULL_FN(range->on_unlock)) - range->on_unlock(kvm); - } =20 srcu_read_unlock(&kvm->srcu, idx); =20 @@ -687,7 +682,6 @@ static __always_inline int kvm_handle_hva_range(struct = mmu_notifier *mn, .arg =3D arg, .handler =3D handler, .on_lock =3D (void *)kvm_null_fn, - .on_unlock =3D (void *)kvm_null_fn, .flush_on_ret =3D true, .may_block =3D false, }; @@ -706,7 +700,6 @@ static __always_inline int kvm_handle_hva_range_no_flus= h(struct mmu_notifier *mn .end =3D end, .handler =3D handler, .on_lock =3D (void *)kvm_null_fn, - .on_unlock =3D (void *)kvm_null_fn, .flush_on_ret =3D false, .may_block =3D false, }; @@ -813,7 +806,6 @@ static int kvm_mmu_notifier_invalidate_range_start(stru= ct mmu_notifier *mn, .end =3D range->end, .handler =3D kvm_mmu_unmap_gfn_range, .on_lock =3D kvm_mmu_invalidate_begin, - .on_unlock =3D (void *)kvm_null_fn, .flush_on_ret =3D true, .may_block =3D mmu_notifier_range_blockable(range), }; @@ -858,6 +850,8 @@ static int kvm_mmu_notifier_invalidate_range_start(stru= ct mmu_notifier *mn, =20 void kvm_mmu_invalidate_end(struct kvm *kvm) { + lockdep_assert_held_write(&kvm->mmu_lock); + /* * This sequence increase will notify the kvm page fault that * the page that is going to be mapped in the spte could have @@ -889,7 +883,6 @@ static void kvm_mmu_notifier_invalidate_range_end(struc= t mmu_notifier *mn, .end =3D range->end, .handler =3D (void *)kvm_null_fn, .on_lock =3D kvm_mmu_invalidate_end, - .on_unlock =3D (void *)kvm_null_fn, .flush_on_ret =3D false, .may_block =3D mmu_notifier_range_blockable(range), }; --=20 2.42.0.820.g83a721a137-goog