From: Paolo Bonzini <pbonzini@redhat.com>
To: Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
"Yan Zhao" <yan.y.zhao@intel.com>,
"Sagi Shahar" <sagis@google.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"David Matlack" <dmatlack@google.com>,
"James Houghton" <jthoughton@google.com>
Subject: Re: [PATCH 14/18] KVM: x86/mmu: Stop processing TDP MMU roots for test_age if young SPTE found
Date: Thu, 17 Oct 2024 18:52:33 +0200 [thread overview]
Message-ID: <12ad52ec-f5cc-43ff-9051-040fceeed68b@redhat.com> (raw)
In-Reply-To: <20241011021051.1557902-15-seanjc@google.com>
On 10/11/24 04:10, Sean Christopherson wrote:
> Return immediately if a young SPTE is found when testing, but not updating,
> SPTEs. The return value is a boolean, i.e. whether there is one young SPTE
> or fifty is irrelevant (ignoring the fact that it's impossible for there to
> be fifty SPTEs, as KVM has a hard limit on the number of valid TDP MMU
> roots).
>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
> arch/x86/kvm/mmu/tdp_mmu.c | 84 ++++++++++++++++++--------------------
> 1 file changed, 40 insertions(+), 44 deletions(-)
>
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index e8c061bf94ec..f412bca206c5 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -1192,35 +1192,6 @@ bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range,
> return flush;
> }
>
> -typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter,
> - struct kvm_gfn_range *range);
> -
> -static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,
> - struct kvm_gfn_range *range,
> - tdp_handler_t handler)
> -{
> - struct kvm_mmu_page *root;
> - struct tdp_iter iter;
> - bool ret = false;
> -
> - /*
> - * Don't support rescheduling, none of the MMU notifiers that funnel
> - * into this helper allow blocking; it'd be dead, wasteful code. Note,
> - * this helper must NOT be used to unmap GFNs, as it processes only
> - * valid roots!
> - */
> - for_each_valid_tdp_mmu_root(kvm, root, range->slot->as_id) {
> - rcu_read_lock();
> -
> - tdp_root_for_each_leaf_pte(iter, root, range->start, range->end)
> - ret |= handler(kvm, &iter, range);
> -
> - rcu_read_unlock();
> - }
> -
> - return ret;
> -}
> -
> /*
> * Mark the SPTEs range of GFNs [start, end) unaccessed and return non-zero
> * if any of the GFNs in the range have been accessed.
> @@ -1229,15 +1200,10 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,
> * from the clear_young() or clear_flush_young() notifier, which uses the
> * return value to determine if the page has been accessed.
> */
> -static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,
> - struct kvm_gfn_range *range)
> +static void kvm_tdp_mmu_age_spte(struct tdp_iter *iter)
> {
> u64 new_spte;
>
> - /* If we have a non-accessed entry we don't need to change the pte. */
> - if (!is_accessed_spte(iter->old_spte))
> - return false;
> -
> if (spte_ad_enabled(iter->old_spte)) {
> iter->old_spte = tdp_mmu_clear_spte_bits(iter->sptep,
> iter->old_spte,
> @@ -1253,23 +1219,53 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,
>
> trace_kvm_tdp_mmu_spte_changed(iter->as_id, iter->gfn, iter->level,
> iter->old_spte, new_spte);
> - return true;
> +}
> +
> +static bool __kvm_tdp_mmu_age_gfn_range(struct kvm *kvm,
> + struct kvm_gfn_range *range,
> + bool test_only)
> +{
> + struct kvm_mmu_page *root;
> + struct tdp_iter iter;
> + bool ret = false;
> +
> + /*
> + * Don't support rescheduling, none of the MMU notifiers that funnel
> + * into this helper allow blocking; it'd be dead, wasteful code. Note,
> + * this helper must NOT be used to unmap GFNs, as it processes only
> + * valid roots!
> + */
> + for_each_valid_tdp_mmu_root(kvm, root, range->slot->as_id) {
> + rcu_read_lock();
> +
> + tdp_root_for_each_leaf_pte(iter, root, range->start, range->end) {
> + if (!is_accessed_spte(iter.old_spte))
> + continue;
> +
> + ret = true;
> + if (test_only)
> + break;
> +
> + kvm_tdp_mmu_age_spte(&iter);
> + }
> +
> + rcu_read_unlock();
> +
> + if (ret && test_only)
> + break;
> + }
> +
> + return ret;
> }
If you use guard(rcu)() you can avoid the repeated breaks:
for_each_valid_tdp_mmu_root(kvm, root, range->slot->as_id) {
guard(rcu)();
tdp_root_for_each_leaf_pte(iter, root, range->start, range->end) {
if (!is_accessed_spte(iter.old_spte))
continue;
ret = true;
if (test_only)
return ret;
kvm_tdp_mmu_age_spte(&iter);
}
}
return ret;
Paolo
next prev parent reply other threads:[~2024-10-17 16:52 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-11 2:10 [PATCH 00/18] KVM: x86/mmu: A/D cleanups (on top of kvm_follow_pfn) Sean Christopherson
2024-10-11 2:10 ` [PATCH 01/18] KVM: x86/mmu: Flush remote TLBs iff MMU-writable flag is cleared from RO SPTE Sean Christopherson
2024-10-11 2:10 ` [PATCH 02/18] KVM: x86/mmu: Always set SPTE's dirty bit if it's created as writable Sean Christopherson
2024-10-11 2:10 ` [PATCH 03/18] KVM: x86/mmu: Fold all of make_spte()'s writable handling into one if-else Sean Christopherson
2024-10-11 2:10 ` [PATCH 04/18] KVM: x86/mmu: Don't force flush if SPTE update clears Accessed bit Sean Christopherson
2024-10-11 2:10 ` [PATCH 05/18] KVM: x86/mmu: Don't flush TLBs when clearing Dirty bit in shadow MMU Sean Christopherson
2024-10-11 2:10 ` [PATCH 06/18] KVM: x86/mmu: Drop ignored return value from kvm_tdp_mmu_clear_dirty_slot() Sean Christopherson
2024-10-11 2:10 ` [PATCH 07/18] KVM: x86/mmu: Fold mmu_spte_update_no_track() into mmu_spte_update() Sean Christopherson
2024-10-11 2:10 ` [PATCH 08/18] KVM: x86/mmu: WARN and flush if resolving a TDP MMU fault clears MMU-writable Sean Christopherson
2024-10-11 2:10 ` [PATCH 09/18] KVM: x86/mmu: Add a dedicated flag to track if A/D bits are globally enabled Sean Christopherson
2024-10-11 2:10 ` [PATCH 10/18] KVM: x86/mmu: Set shadow_accessed_mask for EPT even if A/D bits disabled Sean Christopherson
2024-10-11 2:10 ` [PATCH 11/18] KVM: x86/mmu: Set shadow_dirty_mask " Sean Christopherson
2024-10-11 2:10 ` [PATCH 12/18] KVM: x86/mmu: Use Accessed bit even when _hardware_ A/D bits are disabled Sean Christopherson
2024-10-11 2:10 ` [PATCH 13/18] KVM: x86/mmu: Process only valid TDP MMU roots when aging a gfn range Sean Christopherson
2024-10-11 2:10 ` [PATCH 14/18] KVM: x86/mmu: Stop processing TDP MMU roots for test_age if young SPTE found Sean Christopherson
2024-10-17 16:52 ` Paolo Bonzini [this message]
2024-10-11 2:10 ` [PATCH 15/18] KVM: x86/mmu: Dedup logic for detecting TLB flushes on leaf SPTE changes Sean Christopherson
2024-10-17 16:53 ` Paolo Bonzini
2024-10-11 2:10 ` [PATCH 16/18] KVM: x86/mmu: Set Dirty bit for new SPTEs, even if _hardware_ A/D bits are disabled Sean Christopherson
2024-10-11 2:10 ` [PATCH 17/18] KVM: Allow arch code to elide TLB flushes when aging a young page Sean Christopherson
2024-10-11 2:10 ` [PATCH 18/18] KVM: x86: Don't emit TLB flushes when aging SPTEs for mmu_notifiers Sean Christopherson
2024-10-17 16:55 ` [PATCH 00/18] KVM: x86/mmu: A/D cleanups (on top of kvm_follow_pfn) Paolo Bonzini
2024-10-31 19:51 ` Sean Christopherson
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=12ad52ec-f5cc-43ff-9051-040fceeed68b@redhat.com \
--to=pbonzini@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=dmatlack@google.com \
--cc=jthoughton@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sagis@google.com \
--cc=seanjc@google.com \
--cc=yan.y.zhao@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