From patchwork Fri Oct 27 18:21:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438937 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 5591AC25B6F for ; Fri, 27 Oct 2023 18:22:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6715680011; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CFA68000C; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4269580011; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1B6288000C for ; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E81A0A0749 for ; Fri, 27 Oct 2023 18:22:47 +0000 (UTC) X-FDA: 81392062374.11.1FCBCBB Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf10.hostedemail.com (Postfix) with ESMTP id 209F6C0004 for ; Fri, 27 Oct 2023 18:22:45 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LbCICIAr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430966; 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=yZ4F+cCxLEA4PAApSP/v5UU01C7c86XBvQlNJK8gQkw=; b=5cbFWANCvckOHHc1aPsqflioHUSPf65k8iISZ0QD+zeid5zohQ1AevRfFNqsiYLZkp+KP4 EqBir+dsvPZ8Ea0IE5PMS72pwRrhO6pCw3S9cPMK9oO/J8akNyo0+/ZZdqllYQYZnK6+Xx Q6MY5XF30ywwWhlqjiHCuWZYtGLdEmQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LbCICIAr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430966; a=rsa-sha256; cv=none; b=kj0tF4MSZuUxWjUCxtGep6XrCobqkSzXJsT7oCdLH9WRofkwqs9dk/ObE9RtYHkXf71jTO yGJKmdiTrC/Do7SG+UloRpZPHOYoYUiATkSlZ7X2co2akJAkkmhb3OzemUWJ6P5HSfpFe6 Q/qHw9Ny6pG/Ra+Lbjg+wxcqeh1dCeE= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5af9b0850fdso9978457b3.1 for ; Fri, 27 Oct 2023 11:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430965; x=1699035765; 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=yZ4F+cCxLEA4PAApSP/v5UU01C7c86XBvQlNJK8gQkw=; b=LbCICIArbW1Pb1ffDv3sW5RhuulUO2t5vGWoUvhMlOdAWyqPq+yCb4DKZdfUYfkNH4 Ta2Yfc92yt1jQjRzQYH8pNCcqFF5pmrkO7mXP9ASg5RUSPZp7CgdpqsnJB7CfQmHZUIx 5FO1XtO929Zsk797tF0yXNEh5cHUh2Dstls83nuYG7SL2hOJF4e0j8HgaMBcyaSA6XSa uqu9mTgl0i6/VUZpFUa60Ym/BABr8d6B6Ni4IxTzblU+JZCrtb8qAdS8pWGNBeHJovuj Zw5SMb6W1P90FHf7D/3Psla3gVzuZhuhG6DsnGRtkE7k6IRLmOdVkYZMMyrMtFlZNq5O 2dvw== 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=mLqhVVLf2LoK1VOr7bxf/PnCCL2tS+ra9JeSNYxbzPjSA/kHkkVzsC9t0Ni2DB9/z+ BYxgmLrif2ansHIwLF9Y54l0w5TfGVV7y961NgfRcBc1LaUUiCZZ16YbDSj4LTlqrLmL ARlWRxGdZvsFjCITIFv+NFz6NYeSFyUKhvXGuscTw8bHZb9HEYPjhO9LRfXX200WmZ9v BpdNpaJD90ZrObRjOdiwTXUT2Sj5MMflYE8a4vJjZ9k6+gd0k5YsUYWXjTPElZpnWX2d VbgmHGd/XIVU18d8eqWDKdjoIjcPMe+dmzShyM1yzTFyrIr9+uNXdgIHP7oXduMj/epk rV6g== X-Gm-Message-State: AOJu0YwjGRgtMmt3fdrYuu/c80LQQoPvmXayROUG5N5WuFVix9BzNPRk wMIFz1MyBPEr+rp1b/U1eldAdgex9Po= 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_Sala?= =?utf-8?q?=C3=BCn?= " , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Is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cnt2J3HZ0urD1GqawbgvUByC22JISyPJF2FUFqg2GEtEnZnmU euXTDCge yAq11vLXNoT0fdwJ/QnrePPP2xnGqUQRZ0QlKm+TeRyFQl/6eD0Y0qTY1Fi/I27PJscjgtu6T0jAH3/tFfIKrMPDrecac6vYhp7iY396klzp258UujpKUwUKEVMdrOspCSVIv7f/Yw4GDNfKmP9mu/acqozjAlFOQOcVj8DjYP5QsjMqBQFhjxw+BJIWLRbuRXcQrFLz5Jtq2lsn1YUNrnIMjdjRqqoB6WN5z1jvRlgNGddEJoOTy7GPOlYmq/lDmvnpm8zzX6fW2Q36lqJ5lKZTFzAJQTi5uhVU/bJeHndTlE/CiN16w93aEiI+mtPiUu5nTyx5EbJ/D+EMbJUmPsSNlZlAmxldyVo7LtYf81KHCH+A3EVKNytiPbBdTpW67ox6fL5lKU8HHsKT7kGi7Lk4IcXMJfkRr6lrrMXxEnja/D8xxTIgjwjwCNOL/t8VYJUCzY7xjzytkRRvzI076pec09xrd8s9FmEVLHqnDIQxHFc0vP+8ZqTnXbbQefJQ8dylgvsj0rf84330Vjg+pThzuQhwG4G0SKhhiO/rZJmVjNNtCed6lYgS3PGRPPPaAV9DpnXLQnKerr/2RWaRXq0LizBoYA4DCLpaZrR0/jACNFt4eAqHmVkDOzhnAngJfyuk8yLb6GPkHPCQvCD4IP5My77aYDl4vR+8hU3tRbbG8zB94xe058FJ9DK2b0o0A2WK+XBRuKrMq2woz1kBOy0/gGsQwZMWcjbfw/ISzFK5kTKuGNj00zWd752DgN4yvH8e2/c7CexbkiZAh7WvezZbJiwEynod9XKCE8FIRyFiKQJeWhhfZNRQUqA== 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: 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 Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- 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; }; +/* + * The inner-most helper returns a tuple containing the return value from the + * arch- and action-specific handler, plus a flag indicating whether or not 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 callback * function/handler. The compiler technically can't guarantee that a real @@ -582,22 +595,25 @@ static const union kvm_mmu_notifier_arg KVM_MMU_NOTIFIER_NO_ARG; node; \ node = interval_tree_iter_next(node, start, last)) \ -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 = false, locked = false; + struct kvm_mmu_notifier_return r = { + .ret = false, + .found_memslot = false, + }; struct kvm_gfn_range gfn_range; struct kvm_memory_slot *slot; struct kvm_memslots *slots; int i, idx; if (WARN_ON_ONCE(range->end <= range->start)) - return 0; + return r; /* 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; idx = srcu_read_lock(&kvm->srcu); @@ -631,8 +647,8 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, gfn_range.end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, slot); gfn_range.slot = slot; - if (!locked) { - locked = true; + if (!r.found_memslot) { + r.found_memslot = 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(struct kvm *kvm, if (IS_KVM_NULL_FN(range->handler)) break; } - ret |= range->handler(kvm, &gfn_range); + r.ret |= range->handler(kvm, &gfn_range); } } - if (range->flush_on_ret && ret) + if (range->flush_on_ret && r.ret) kvm_flush_remote_tlbs(kvm); - 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(struct kvm *kvm, srcu_read_unlock(&kvm->srcu, idx); - /* The notifiers are averse to booleans. :-( */ - return (int)ret; + return r; } 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 = false, }; - return __kvm_handle_hva_range(kvm, &range); + return __kvm_handle_hva_range(kvm, &range).ret; } static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn, @@ -696,7 +711,7 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn .may_block = false, }; - return __kvm_handle_hva_range(kvm, &range); + return __kvm_handle_hva_range(kvm, &range).ret; } static bool kvm_change_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) @@ -798,7 +813,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, .end = range->end, .handler = kvm_mmu_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, - .on_unlock = kvm_arch_guest_memory_reclaimed, + .on_unlock = (void *)kvm_null_fn, .flush_on_ret = true, .may_block = mmu_notifier_range_blockable(range), }; @@ -830,7 +845,13 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, gfn_to_pfn_cache_invalidate_start(kvm, range->start, range->end, hva_range.may_block); - __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); return 0; }