Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp48527pxk; Wed, 30 Sep 2020 17:30:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqlsdzutgGQum3hlxwBrqqvq5lKa6dUL/VxzyHDXcy6hW20Dk+AzO5/i+js60MgWJKcxcl X-Received: by 2002:a17:907:394:: with SMTP id ss20mr5351505ejb.120.1601512201591; Wed, 30 Sep 2020 17:30:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601512201; cv=none; d=google.com; s=arc-20160816; b=KoAlzwi3rSJknf1CUySXEZpuSsqpsyJporLOMF0p9SxIEDtyNA9Lg2loBLDdouzPp7 IjZuv+k2F3nBKCzvN3kf0CtR3C/xpK/plqaDxwPrPgHZLFXKSbnYNl+LDmRBvBw/7/l6 oJS4jym5BKypLUdN8wmm6dXLiyKKmtqe+gUVsI4JX5DNAV9EtXtvHEHAXhm1w4eh7ajK P7UYL8If8ybxh6vlYACv9dnWG/pHMULzDjQPr2twxbd57nu2bX9ZtFDfUM2iDCEISvjG 0NqlbS27ElXAHYWzUiE1BTbNPoZD+UlY9ZiH4KSN8S7tkRJNpyUH75QxAJyfZ7qZ5HSC fw8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ziKIfU8vAVPMot5eNP2rJCYqSKd8W1jYnncCawSVC4k=; b=TKJJ14ujHLtQur0CKR4TGWj0DHRMCqXbcRQ6CuLprTHOBVdVRUoA+SnlLzUJL4MvHu p+Uzvf4Whp71IzdYOvX0+7MfaHLUWk0zGTkRgHSEMKkhXEF6FnkGQUdi7/CeJTP/IIGE wjFOz8BNMFLPPM8hfHfd/9n4QuL8qzRCWmVvOKRuEJNnSazmDyXQ02Ftkq14GyVgQO5c z2we457u3VZnpX76K7hTAWbOIxadxbT3nhZfTSOu2pi/YDtSNAOG4kGWupiYO4zPj+IP EnJ4b/6QbPSxZjYWnl8/aZvhIKxg+3CTBAXOKZjR6I9Mw5s4Q7GWxJ+xUDnh128T7YcV Lbhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=sNIUgBiX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ks27si96191ejb.543.2020.09.30.17.29.39; Wed, 30 Sep 2020 17:30:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=sNIUgBiX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732138AbgI3XPh (ORCPT + 99 others); Wed, 30 Sep 2020 19:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732131AbgI3XPa (ORCPT ); Wed, 30 Sep 2020 19:15:30 -0400 Received: from mail-il1-x141.google.com (mail-il1-x141.google.com [IPv6:2607:f8b0:4864:20::141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84DFCC0613D1 for ; Wed, 30 Sep 2020 16:15:29 -0700 (PDT) Received: by mail-il1-x141.google.com with SMTP id t12so4161229ilh.3 for ; Wed, 30 Sep 2020 16:15:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ziKIfU8vAVPMot5eNP2rJCYqSKd8W1jYnncCawSVC4k=; b=sNIUgBiX8VjUnFjvQu6mWmODrgvc3PS6B3WbHDRu/bvwK6yZkGJB/JE6tTBwBbDdp+ 7PiR/NVNqXNOIy2exNIBm4w0b+jK1AUYkILUiYH7EhuFCbZdAarKCz9Il4cF5pDffG+J iTbd+JhF/vdPZ6JIV9RRcKssPQ+tVYS2Nz0n+ePWkJ1A5z4qr5FSd5RdwPogbJfbhFHa Xqc7G/25yo4GvDiif0dlJtBBPbYPAgpVH5pUh1MIQ3FhvQI4I89Ij2x0W5vKELB6KdOt qdsyxt9d3I525xkjH3r0Vdm9aFjLQf9DkpreLVcaPfHcdpZNYWC45caUCLRPib9fLNbb l0Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ziKIfU8vAVPMot5eNP2rJCYqSKd8W1jYnncCawSVC4k=; b=IwVakpW0Nt22JZcMGUC95czKgwBeLDtkfbmrGjWcl4/Du0QMazdtCoQBVfsaH0F+sT ib/8gYNiJS5yj/ir1cM3I8MHTiK6RTX8BmMV/dZpUJkfB94TK/KncS7VpGkWcSKG0SXx UzA5HEn78Pw5GQohgIgGaUnuF+f/7T3kGVKxY50rj/E1IKlFv5kVydYV/fotFa9A//Yw Nj8Or6iDMLionArMSJTdFL5AaiKwBI6DEbtlllMcENQ1QdfNs03ansQOrEV8OBRmagrw QuBUr1HsUP9iQEWO2xZktNu/oHYedCYxYLO0MiFkNWppLs/0gHTnR7Vs4+qCqqsBsCDw DcMw== X-Gm-Message-State: AOAM532l5pwzThuEMRn+6Tt0+MDyjr6Ej6OXxT8wEGsdcj3DcL08oGqf WdR4kGCkQpCdRyK4kbRJ68Y69KbJlYP0mBitAQD7Rg== X-Received: by 2002:a92:1e07:: with SMTP id e7mr194208ile.154.1601507728477; Wed, 30 Sep 2020 16:15:28 -0700 (PDT) MIME-Version: 1.0 References: <20200925212302.3979661-1-bgardon@google.com> <20200925212302.3979661-14-bgardon@google.com> <20200930170354.GF32672@linux.intel.com> In-Reply-To: <20200930170354.GF32672@linux.intel.com> From: Ben Gardon Date: Wed, 30 Sep 2020 16:15:17 -0700 Message-ID: Subject: Re: [PATCH 13/22] kvm: mmu: Support invalidate range MMU notifier for TDP MMU To: Sean Christopherson Cc: LKML , kvm , Cannon Matthews , Paolo Bonzini , Peter Xu , Peter Shier , Peter Feiner , Junaid Shahid , Jim Mattson , Yulei Zhang , Wanpeng Li , Vitaly Kuznetsov , Xiao Guangrong Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 30, 2020 at 10:04 AM Sean Christopherson wrote: > > On Fri, Sep 25, 2020 at 02:22:53PM -0700, Ben Gardon wrote: > > In order to interoperate correctly with the rest of KVM and other Linux > > subsystems, the TDP MMU must correctly handle various MMU notifiers. Add > > hooks to handle the invalidate range family of MMU notifiers. > > > > Tested by running kvm-unit-tests and KVM selftests on an Intel Haswell > > machine. This series introduced no new failures. > > > > This series can be viewed in Gerrit at: > > https://linux-review.googlesource.com/c/virt/kvm/kvm/+/2538 > > > > Signed-off-by: Ben Gardon > > --- > > arch/x86/kvm/mmu/mmu.c | 9 ++++- > > arch/x86/kvm/mmu/tdp_mmu.c | 80 +++++++++++++++++++++++++++++++++++--- > > arch/x86/kvm/mmu/tdp_mmu.h | 3 ++ > > 3 files changed, 86 insertions(+), 6 deletions(-) > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > index 52d661a758585..0ddfdab942554 100644 > > --- a/arch/x86/kvm/mmu/mmu.c > > +++ b/arch/x86/kvm/mmu/mmu.c > > @@ -1884,7 +1884,14 @@ static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, > > int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end, > > unsigned flags) > > { > > - return kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp); > > + int r; > > + > > + r = kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp); > > + > > + if (kvm->arch.tdp_mmu_enabled) > > + r |= kvm_tdp_mmu_zap_hva_range(kvm, start, end); > > Similar to an earlier question, is this intentionally additive, or can this > instead by: > > if (kvm->arch.tdp_mmu_enabled) > r = kvm_tdp_mmu_zap_hva_range(kvm, start, end); > else > r = kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp); > It is intentionally additive so the legacy/shadow MMU can handle nested. > > + > > + return r; > > } > > > > int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > > index 557e780bdf9f9..1cea58db78a13 100644 > > --- a/arch/x86/kvm/mmu/tdp_mmu.c > > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > > @@ -60,7 +60,7 @@ bool is_tdp_mmu_root(struct kvm *kvm, hpa_t hpa) > > } > > > > static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, > > - gfn_t start, gfn_t end); > > + gfn_t start, gfn_t end, bool can_yield); > > > > static void free_tdp_mmu_root(struct kvm *kvm, struct kvm_mmu_page *root) > > { > > @@ -73,7 +73,7 @@ static void free_tdp_mmu_root(struct kvm *kvm, struct kvm_mmu_page *root) > > > > list_del(&root->link); > > > > - zap_gfn_range(kvm, root, 0, max_gfn); > > + zap_gfn_range(kvm, root, 0, max_gfn, false); > > > > free_page((unsigned long)root->spt); > > kmem_cache_free(mmu_page_header_cache, root); > > @@ -361,9 +361,14 @@ static bool tdp_mmu_iter_cond_resched(struct kvm *kvm, struct tdp_iter *iter) > > * non-root pages mapping GFNs strictly within that range. Returns true if > > * SPTEs have been cleared and a TLB flush is needed before releasing the > > * MMU lock. > > + * If can_yield is true, will release the MMU lock and reschedule if the > > + * scheduler needs the CPU or there is contention on the MMU lock. If this > > + * function cannot yield, it will not release the MMU lock or reschedule and > > + * the caller must ensure it does not supply too large a GFN range, or the > > + * operation can cause a soft lockup. > > */ > > static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, > > - gfn_t start, gfn_t end) > > + gfn_t start, gfn_t end, bool can_yield) > > { > > struct tdp_iter iter; > > bool flush_needed = false; > > @@ -387,7 +392,10 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, > > handle_changed_spte(kvm, as_id, iter.gfn, iter.old_spte, 0, > > iter.level); > > > > - flush_needed = !tdp_mmu_iter_cond_resched(kvm, &iter); > > + if (can_yield) > > + flush_needed = !tdp_mmu_iter_cond_resched(kvm, &iter); > > flush_needed = !can_yield || !tdp_mmu_iter_cond_resched(kvm, &iter); > > > + else > > + flush_needed = true; > > } > > return flush_needed; > > } > > @@ -410,7 +418,7 @@ bool kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, gfn_t start, gfn_t end) > > */ > > get_tdp_mmu_root(kvm, root); > > > > - flush = zap_gfn_range(kvm, root, start, end) || flush; > > + flush = zap_gfn_range(kvm, root, start, end, true) || flush; > > > > put_tdp_mmu_root(kvm, root); > > } > > @@ -551,3 +559,65 @@ int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu, int write, int map_writable, > > > > return ret; > > } > > + > > +static int kvm_tdp_mmu_handle_hva_range(struct kvm *kvm, unsigned long start, > > + unsigned long end, unsigned long data, > > + int (*handler)(struct kvm *kvm, struct kvm_memory_slot *slot, > > + struct kvm_mmu_page *root, gfn_t start, > > + gfn_t end, unsigned long data)) > > +{ > > + struct kvm_memslots *slots; > > + struct kvm_memory_slot *memslot; > > + struct kvm_mmu_page *root; > > + int ret = 0; > > + int as_id; > > + > > + for_each_tdp_mmu_root(kvm, root) { > > + /* > > + * Take a reference on the root so that it cannot be freed if > > + * this thread releases the MMU lock and yields in this loop. > > + */ > > + get_tdp_mmu_root(kvm, root); > > + > > + as_id = kvm_mmu_page_as_id(root); > > + slots = __kvm_memslots(kvm, as_id); > > + kvm_for_each_memslot(memslot, slots) { > > + unsigned long hva_start, hva_end; > > + gfn_t gfn_start, gfn_end; > > + > > + hva_start = max(start, memslot->userspace_addr); > > + hva_end = min(end, memslot->userspace_addr + > > + (memslot->npages << PAGE_SHIFT)); > > + if (hva_start >= hva_end) > > + continue; > > + /* > > + * {gfn(page) | page intersects with [hva_start, hva_end)} = > > + * {gfn_start, gfn_start+1, ..., gfn_end-1}. > > + */ > > + gfn_start = hva_to_gfn_memslot(hva_start, memslot); > > + gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); > > + > > + ret |= handler(kvm, memslot, root, gfn_start, > > + gfn_end, data); > > Eh, I'd say let this one poke out, the above hva_to_gfn_memslot() already > overruns 80 chars. IMO it's more readable without the wraps. Will do. > > > + } > > + > > + put_tdp_mmu_root(kvm, root); > > + } > > + > > + return ret; > > +} > > + > > +static int zap_gfn_range_hva_wrapper(struct kvm *kvm, > > + struct kvm_memory_slot *slot, > > + struct kvm_mmu_page *root, gfn_t start, > > + gfn_t end, unsigned long unused) > > +{ > > + return zap_gfn_range(kvm, root, start, end, false); > > +} > > + > > +int kvm_tdp_mmu_zap_hva_range(struct kvm *kvm, unsigned long start, > > + unsigned long end) > > +{ > > + return kvm_tdp_mmu_handle_hva_range(kvm, start, end, 0, > > + zap_gfn_range_hva_wrapper); > > +} > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.h b/arch/x86/kvm/mmu/tdp_mmu.h > > index abf23dc0ab7ad..ce804a97bfa1d 100644 > > --- a/arch/x86/kvm/mmu/tdp_mmu.h > > +++ b/arch/x86/kvm/mmu/tdp_mmu.h > > @@ -18,4 +18,7 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm); > > int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu, int write, int map_writable, > > int level, gpa_t gpa, kvm_pfn_t pfn, bool prefault, > > bool lpage_disallowed); > > + > > +int kvm_tdp_mmu_zap_hva_range(struct kvm *kvm, unsigned long start, > > + unsigned long end); > > #endif /* __KVM_X86_MMU_TDP_MMU_H */ > > -- > > 2.28.0.709.gb0816b6eb0-goog > >