From nobody Tue Nov 5 10:57:09 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 3B2B7C25B47 for ; Fri, 27 Oct 2023 18:23:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346503AbjJ0SXg (ORCPT ); Fri, 27 Oct 2023 14:23:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235148AbjJ0SXC (ORCPT ); Fri, 27 Oct 2023 14:23:02 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7821B1708 for ; Fri, 27 Oct 2023 11:22:46 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5afe220cadeso6517917b3.3 for ; Fri, 27 Oct 2023 11:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430965; x=1699035765; 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=yZ4F+cCxLEA4PAApSP/v5UU01C7c86XBvQlNJK8gQkw=; b=jNz8g3h8naX2HfYcFiDLgvpX0tWkk3a2xdTTOQ4PMqOOuyPdIQN+INWtO/Y3MU1uOA TPApdR6K0cxVH5me7fr8t9/u7KngtbbHTY1qRElGDn2ummZtWW1HRKFrwi/WAaVW2DPT 0M0xImaGKcsjoDfaOKwKBKHkFbvbd08rjQpJbd+mX2B3BuDvcPncPaKObaV+mCTrfEY7 jriMkeR1P+muFZA2clrz+O9sZsEmdfl2TZkB6/XLLftAsura+BykuUYQXnsC7N4cD1W2 jnIdZ/yAKiywIWxEPvhEMjYVEyDzhXo1f5rbT+50aCu/YVBu47mGECveZ/JRjlqq6ZK0 2xyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430965; x=1699035765; 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=yZ4F+cCxLEA4PAApSP/v5UU01C7c86XBvQlNJK8gQkw=; b=T5UwGcRZjdvywD+3ViMGNQn4NoxzeylIHgmvUWdBvDmoz5RW0oQpEF20+U8l8UiGTd xhyFdcBZHkKuy7tyQOqv07qy00sRBuxfV8xgeHRJKs45LHDNJGU7nDy17UNqoyasi8+i atoJ/QU+DfotWrCT3NrSx5vJQLOkrC1G1lBFbtbj2iKG2DyQBuF3Z7Nvf/DhJTyyMdbQ aiHbVP49VabNn8NZlmLFjRXlTFnNjEHnwm/zhmBYZbmV3MRLRIzSgFMuLvJewYO15afg cARAmusLfJDqC3E3ntlHcccNflNhr2C5uAhEx4P+Oj7g0TSRLxPdvcGIylcsUW7ZiXEh Q75Q== X-Gm-Message-State: AOJu0YzynWISiqeCiEHjcUYpzXwdOxffm5pxni3JQ/xROYmnRitUewPA iaxc6aX53PhCBEZ2PC6FN0juezEqUxw= X-Google-Smtp-Source: AGHT+IGwuIwT1CUNj+EGBgkfDLIp57neyP6oJZbU4jqENzEgZhephR9TVXqJjgCFT4zzJuNYuq9lZ329bBo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:a14c:0:b0:59b:e97e:f7e2 with SMTP id y73-20020a81a14c000000b0059be97ef7e2mr73613ywg.4.1698430965278; Fri, 27 Oct 2023 11:22:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:52 -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-11-seanjc@google.com> Subject: [PATCH v13 10/35] KVM: Add a dedicated mmu_notifier flag for reclaiming freed memory 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" Handle AMD SEV's kvm_arch_guest_memory_reclaimed() hook by having __kvm_handle_hva_range() return whether or not an overlapping memslot was found, i.e. mmu_lock was acquired. Using the .on_unlock() hook works, but kvm_arch_guest_memory_reclaimed() needs to run after dropping mmu_lock, which makes .on_lock() and .on_unlock() asymmetrical. Use a small struct to return the tuple of the notifier-specific return, plus whether or not overlap was found. Because the iteration helpers are __always_inlined, practically speaking, the struct will never actually be returned from a function call (not to mention the size of the struct will be two bytes in practice). Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 53 +++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 3f5b7c2c5327..2bc04c8ae1f4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -561,6 +561,19 @@ struct kvm_mmu_notifier_range { bool may_block; }; =20 +/* + * The inner-most helper returns a tuple containing the return value from = the + * arch- and action-specific handler, plus a flag indicating whether or no= t at + * least one memslot was found, i.e. if the handler found guest memory. + * + * Note, most notifiers are averse to booleans, so even though KVM tracks = the + * return from arch code as a bool, outer helpers will cast it to an int. = :-( + */ +typedef struct kvm_mmu_notifier_return { + bool ret; + bool found_memslot; +} kvm_mn_ret_t; + /* * Use a dedicated stub instead of NULL to indicate that there is no callb= ack * function/handler. The compiler technically can't guarantee that a real @@ -582,22 +595,25 @@ static const union kvm_mmu_notifier_arg KVM_MMU_NOTIF= IER_NO_ARG; node; \ node =3D interval_tree_iter_next(node, start, last)) \ =20 -static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, - const struct kvm_mmu_notifier_range *range) +static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, + const struct kvm_mmu_notifier_range *range) { - bool ret =3D false, locked =3D false; + struct kvm_mmu_notifier_return r =3D { + .ret =3D false, + .found_memslot =3D false, + }; struct kvm_gfn_range gfn_range; struct kvm_memory_slot *slot; struct kvm_memslots *slots; int i, idx; =20 if (WARN_ON_ONCE(range->end <=3D range->start)) - return 0; + return r; =20 /* A null handler is allowed if and only if on_lock() is provided. */ if (WARN_ON_ONCE(IS_KVM_NULL_FN(range->on_lock) && IS_KVM_NULL_FN(range->handler))) - return 0; + return r; =20 idx =3D srcu_read_lock(&kvm->srcu); =20 @@ -631,8 +647,8 @@ static __always_inline int __kvm_handle_hva_range(struc= t kvm *kvm, gfn_range.end =3D hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, slot); gfn_range.slot =3D slot; =20 - if (!locked) { - locked =3D true; + if (!r.found_memslot) { + r.found_memslot =3D true; KVM_MMU_LOCK(kvm); if (!IS_KVM_NULL_FN(range->on_lock)) range->on_lock(kvm); @@ -640,14 +656,14 @@ static __always_inline int __kvm_handle_hva_range(str= uct kvm *kvm, if (IS_KVM_NULL_FN(range->handler)) break; } - ret |=3D range->handler(kvm, &gfn_range); + r.ret |=3D range->handler(kvm, &gfn_range); } } =20 - if (range->flush_on_ret && ret) + if (range->flush_on_ret && r.ret) kvm_flush_remote_tlbs(kvm); =20 - if (locked) { + if (r.found_memslot) { KVM_MMU_UNLOCK(kvm); if (!IS_KVM_NULL_FN(range->on_unlock)) range->on_unlock(kvm); @@ -655,8 +671,7 @@ static __always_inline int __kvm_handle_hva_range(struc= t kvm *kvm, =20 srcu_read_unlock(&kvm->srcu, idx); =20 - /* The notifiers are averse to booleans. :-( */ - return (int)ret; + return r; } =20 static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, @@ -677,7 +692,7 @@ static __always_inline int kvm_handle_hva_range(struct = mmu_notifier *mn, .may_block =3D false, }; =20 - return __kvm_handle_hva_range(kvm, &range); + return __kvm_handle_hva_range(kvm, &range).ret; } =20 static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifi= er *mn, @@ -696,7 +711,7 @@ static __always_inline int kvm_handle_hva_range_no_flus= h(struct mmu_notifier *mn .may_block =3D false, }; =20 - return __kvm_handle_hva_range(kvm, &range); + return __kvm_handle_hva_range(kvm, &range).ret; } =20 static bool kvm_change_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *ran= ge) @@ -798,7 +813,7 @@ 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 kvm_arch_guest_memory_reclaimed, + .on_unlock =3D (void *)kvm_null_fn, .flush_on_ret =3D true, .may_block =3D mmu_notifier_range_blockable(range), }; @@ -830,7 +845,13 @@ static int kvm_mmu_notifier_invalidate_range_start(str= uct mmu_notifier *mn, gfn_to_pfn_cache_invalidate_start(kvm, range->start, range->end, hva_range.may_block); =20 - __kvm_handle_hva_range(kvm, &hva_range); + /* + * If one or more memslots were found and thus zapped, notify arch code + * that guest memory has been reclaimed. This needs to be done *after* + * dropping mmu_lock, as x86's reclaim path is slooooow. + */ + if (__kvm_handle_hva_range(kvm, &hva_range).found_memslot) + kvm_arch_guest_memory_reclaimed(kvm); =20 return 0; } --=20 2.42.0.820.g83a721a137-goog