From: Yan Zhao <yan.y.zhao@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: <linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>,
<isaku.yamahata@intel.com>, <binbin.wu@linux.intel.com>,
<rick.p.edgecombe@intel.com>
Subject: Re: [PATCH v6 01/18] KVM: x86/mmu: Zap invalid roots with mmu_lock held for write at uninit
Date: Mon, 23 Dec 2024 15:58:14 +0800 [thread overview]
Message-ID: <Z2kYFs4nYVIbqA4t@yzhao56-desk.sh.intel.com> (raw)
In-Reply-To: <20241222193445.349800-2-pbonzini@redhat.com>
On Sun, Dec 22, 2024 at 02:34:28PM -0500, Paolo Bonzini wrote:
> From: Rick Edgecombe <rick.p.edgecombe@intel.com>
>
> Prepare for a future TDX patch which asserts that atomic zapping
> (i.e. zapping with mmu_lock taken for read) don't operate on mirror roots.
> When tearing down a VM, all roots have to be zapped (including mirro
s/mirro/mirror
> roots once they're in place) so do that with the mmu_lock taken for werite.
s/werite/write
> kvm_mmu_uninit_tdp_mmu() is invoked either before or after executing any
> atomic operations on SPTEs by vCPU threads. Therefore, it will not impact
> vCPU threads performance if kvm_tdp_mmu_zap_invalidated_roots() acquires
> mmu_lock for write to zap invalid roots.
>
> Co-developed-by: Yan Zhao <yan.y.zhao@intel.com>
> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
> Message-ID: <20240718211230.1492011-2-rick.p.edgecombe@intel.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> arch/x86/kvm/mmu/mmu.c | 2 +-
> arch/x86/kvm/mmu/tdp_mmu.c | 16 +++++++++++-----
> arch/x86/kvm/mmu/tdp_mmu.h | 2 +-
> 3 files changed, 13 insertions(+), 7 deletions(-)
>
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 2401606db260..3f749fb5ec6c 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -6467,7 +6467,7 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)
> * lead to use-after-free.
> */
> if (tdp_mmu_enabled)
> - kvm_tdp_mmu_zap_invalidated_roots(kvm);
> + kvm_tdp_mmu_zap_invalidated_roots(kvm, true);
> }
>
> void kvm_mmu_init_vm(struct kvm *kvm)
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index 2f15e0e33903..1054ccd9b861 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -38,7 +38,7 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
> * ultimately frees all roots.
> */
> kvm_tdp_mmu_invalidate_all_roots(kvm);
> - kvm_tdp_mmu_zap_invalidated_roots(kvm);
> + kvm_tdp_mmu_zap_invalidated_roots(kvm, false);
>
> WARN_ON(atomic64_read(&kvm->arch.tdp_mmu_pages));
> WARN_ON(!list_empty(&kvm->arch.tdp_mmu_roots));
> @@ -883,11 +883,14 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm)
> * Zap all invalidated roots to ensure all SPTEs are dropped before the "fast
> * zap" completes.
> */
> -void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)
> +void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm, bool shared)
> {
> struct kvm_mmu_page *root;
>
> - read_lock(&kvm->mmu_lock);
> + if (shared)
> + read_lock(&kvm->mmu_lock);
> + else
> + write_lock(&kvm->mmu_lock);
>
> for_each_tdp_mmu_root_yield_safe(kvm, root) {
> if (!root->tdp_mmu_scheduled_root_to_zap)
> @@ -905,7 +908,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)
> * that may be zapped, as such entries are associated with the
> * ASID on both VMX and SVM.
> */
> - tdp_mmu_zap_root(kvm, root, true);
> + tdp_mmu_zap_root(kvm, root, shared);
>
> /*
> * The referenced needs to be put *after* zapping the root, as
> @@ -915,7 +918,10 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)
> kvm_tdp_mmu_put_root(kvm, root);
> }
>
> - read_unlock(&kvm->mmu_lock);
> + if (shared)
> + read_unlock(&kvm->mmu_lock);
> + else
> + write_unlock(&kvm->mmu_lock);
> }
>
> /*
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.h b/arch/x86/kvm/mmu/tdp_mmu.h
> index f03ca0dd13d9..6d7cdc462f58 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.h
> +++ b/arch/x86/kvm/mmu/tdp_mmu.h
> @@ -23,7 +23,7 @@ bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, gfn_t start, gfn_t end, bool flush);
> bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp);
> void kvm_tdp_mmu_zap_all(struct kvm *kvm);
> void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm);
> -void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm);
> +void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm, bool shared);
>
> int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
>
> --
> 2.43.5
>
>
next prev parent reply other threads:[~2024-12-23 8:32 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-22 19:34 [PATCH v6 00/18] TDX MMU prep series part 1 Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 01/18] KVM: x86/mmu: Zap invalid roots with mmu_lock held for write at uninit Paolo Bonzini
2024-12-23 7:58 ` Yan Zhao [this message]
2024-12-22 19:34 ` [PATCH v6 02/18] KVM: Add member to struct kvm_gfn_range to indicate private/shared Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 03/18] KVM: x86: Add a VM type define for TDX Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 04/18] KVM: x86/mmu: Add an external pointer to struct kvm_mmu_page Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 05/18] KVM: x86/mmu: Add an is_mirror member for union kvm_mmu_page_role Paolo Bonzini
2024-12-23 11:23 ` Yan Zhao
2024-12-22 19:34 ` [PATCH v6 06/18] KVM: x86/mmu: Make kvm_tdp_mmu_alloc_root() return void Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 07/18] KVM: x86/tdp_mmu: Take struct kvm in iter loops Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 08/18] KVM: x86/mmu: Support GFN direct bits Paolo Bonzini
2025-01-18 1:03 ` Sean Christopherson
2024-12-22 19:34 ` [PATCH v6 09/18] KVM: x86/tdp_mmu: Extract root invalid check from tdx_mmu_next_root() Paolo Bonzini
2025-01-18 1:05 ` Sean Christopherson
2025-01-22 18:20 ` Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 10/18] KVM: x86/tdp_mmu: Introduce KVM MMU root types to specify page table type Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 11/18] KVM: x86/tdp_mmu: Take root in tdp_mmu_for_each_pte() Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 12/18] KVM: x86/tdp_mmu: Support mirror root for TDP MMU Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 13/18] KVM: x86/tdp_mmu: Propagate attr_filter to MMU notifier callbacks Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 14/18] KVM: x86/tdp_mmu: Propagate building mirror page tables Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 15/18] KVM: x86/tdp_mmu: Propagate tearing down " Paolo Bonzini
2025-01-18 1:09 ` Sean Christopherson
2024-12-22 19:34 ` [PATCH v6 16/18] KVM: x86/tdp_mmu: Take root types for kvm_tdp_mmu_invalidate_all_roots() Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 17/18] KVM: x86/tdp_mmu: Don't zap valid mirror roots in kvm_tdp_mmu_zap_all() Paolo Bonzini
2024-12-22 19:34 ` [PATCH v6 18/18] KVM: x86/mmu: Prevent aliased memslot GFNs Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z2kYFs4nYVIbqA4t@yzhao56-desk.sh.intel.com \
--to=yan.y.zhao@intel.com \
--cc=binbin.wu@linux.intel.com \
--cc=isaku.yamahata@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=rick.p.edgecombe@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®