Skip to content

Commit 7ee131e

Browse files
shvipinsean-jc
authored andcommitted
KVM: x86/mmu: Clear only A-bit (if enabled) when aging TDP MMU SPTEs
Use tdp_mmu_clear_spte_bits() when clearing the Accessed bit in TDP MMU SPTEs so as to use an atomic-AND instead of XCHG to clear the A-bit. Similar to the D-bit story, this will allow KVM to bypass __handle_changed_spte() by ensuring only the A-bit is modified. Link: https://lore.kernel.org/all/Y9HcHRBShQgjxsQb@google.com Signed-off-by: Vipin Sharma <vipinsh@google.com> Reviewed-by: David Matlack <dmatlack@google.com> [sean: massage changelog] Link: https://lore.kernel.org/r/20230321220021.2119033-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent e730087 commit 7ee131e

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -756,13 +756,6 @@ static inline void tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,
756756
_tdp_mmu_set_spte(kvm, iter, new_spte, true);
757757
}
758758

759-
static inline void tdp_mmu_set_spte_no_acc_track(struct kvm *kvm,
760-
struct tdp_iter *iter,
761-
u64 new_spte)
762-
{
763-
_tdp_mmu_set_spte(kvm, iter, new_spte, false);
764-
}
765-
766759
#define tdp_root_for_each_pte(_iter, _root, _start, _end) \
767760
for_each_tdp_pte(_iter, _root, _start, _end)
768761

@@ -1248,33 +1241,44 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,
12481241
/*
12491242
* Mark the SPTEs range of GFNs [start, end) unaccessed and return non-zero
12501243
* if any of the GFNs in the range have been accessed.
1244+
*
1245+
* No need to mark the corresponding PFN as accessed as this call is coming
1246+
* from the clear_young() or clear_flush_young() notifier, which uses the
1247+
* return value to determine if the page has been accessed.
12511248
*/
12521249
static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,
12531250
struct kvm_gfn_range *range)
12541251
{
1255-
u64 new_spte = 0;
1252+
u64 new_spte;
12561253

12571254
/* If we have a non-accessed entry we don't need to change the pte. */
12581255
if (!is_accessed_spte(iter->old_spte))
12591256
return false;
12601257

1261-
new_spte = iter->old_spte;
1262-
1263-
if (spte_ad_enabled(new_spte)) {
1264-
new_spte &= ~shadow_accessed_mask;
1258+
if (spte_ad_enabled(iter->old_spte)) {
1259+
iter->old_spte = tdp_mmu_clear_spte_bits(iter->sptep,
1260+
iter->old_spte,
1261+
shadow_accessed_mask,
1262+
iter->level);
1263+
new_spte = iter->old_spte & ~shadow_accessed_mask;
12651264
} else {
12661265
/*
12671266
* Capture the dirty status of the page, so that it doesn't get
12681267
* lost when the SPTE is marked for access tracking.
12691268
*/
1270-
if (is_writable_pte(new_spte))
1271-
kvm_set_pfn_dirty(spte_to_pfn(new_spte));
1269+
if (is_writable_pte(iter->old_spte))
1270+
kvm_set_pfn_dirty(spte_to_pfn(iter->old_spte));
12721271

1273-
new_spte = mark_spte_for_access_track(new_spte);
1272+
new_spte = mark_spte_for_access_track(iter->old_spte);
1273+
iter->old_spte = kvm_tdp_mmu_write_spte(iter->sptep,
1274+
iter->old_spte, new_spte,
1275+
iter->level);
12741276
}
12751277

1276-
tdp_mmu_set_spte_no_acc_track(kvm, iter, new_spte);
1277-
1278+
__handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte,
1279+
new_spte, iter->level, false);
1280+
handle_changed_spte_dirty_log(kvm, iter->as_id, iter->gfn,
1281+
iter->old_spte, new_spte, iter->level);
12781282
return true;
12791283
}
12801284

0 commit comments

Comments
 (0)