Skip to content

Commit 7971801

Browse files
committed
KVM: x86/mmu: Use Accessed bit even when _hardware_ A/D bits are disabled
Use the Accessed bit in SPTEs even when A/D bits are disabled in hardware, i.e. propagate accessed information to SPTE.Accessed even when KVM is doing manual tracking by making SPTEs not-present. In addition to eliminating a small amount of code in is_accessed_spte(), this also paves the way for preserving Accessed information when a SPTE is zapped in response to a mmu_notifier PROTECTION event, e.g. if a SPTE is zapped because NUMA balancing kicks in. Note, EPT is the only flavor of paging in which A/D bits are conditionally enabled, and the Accessed (and Dirty) bit is software-available when A/D bits are disabled. Note #2, there are currently no concrete plans to preserve Accessed information. Explorations on that front were the initial catalyst, but the cleanup is the motivation for the actual commit. Link: https://lore.kernel.org/r/20241011021051.1557902-13-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 53510b9 commit 7971801

3 files changed

Lines changed: 5 additions & 13 deletions

File tree

arch/x86/kvm/mmu/mmu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3493,7 +3493,8 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
34933493
* enabled, the SPTE can't be an access-tracked SPTE.
34943494
*/
34953495
if (unlikely(!kvm_ad_enabled) && is_access_track_spte(spte))
3496-
new_spte = restore_acc_track_spte(new_spte);
3496+
new_spte = restore_acc_track_spte(new_spte) |
3497+
shadow_accessed_mask;
34973498

34983499
/*
34993500
* To keep things simple, only SPTEs that are MMU-writable can

arch/x86/kvm/mmu/spte.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
175175

176176
spte |= shadow_present_mask;
177177
if (!prefetch || synchronizing)
178-
spte |= spte_shadow_accessed_mask(spte);
178+
spte |= shadow_accessed_mask;
179179

180180
/*
181181
* For simplicity, enforce the NX huge page mitigation even if not
@@ -346,7 +346,7 @@ u64 mark_spte_for_access_track(u64 spte)
346346

347347
spte |= (spte & SHADOW_ACC_TRACK_SAVED_BITS_MASK) <<
348348
SHADOW_ACC_TRACK_SAVED_BITS_SHIFT;
349-
spte &= ~shadow_acc_track_mask;
349+
spte &= ~(shadow_acc_track_mask | shadow_accessed_mask);
350350

351351
return spte;
352352
}

arch/x86/kvm/mmu/spte.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,6 @@ static inline bool spte_ad_need_write_protect(u64 spte)
316316
return (spte & SPTE_TDP_AD_MASK) != SPTE_TDP_AD_ENABLED;
317317
}
318318

319-
static inline u64 spte_shadow_accessed_mask(u64 spte)
320-
{
321-
KVM_MMU_WARN_ON(!is_shadow_present_pte(spte));
322-
return spte_ad_enabled(spte) ? shadow_accessed_mask : 0;
323-
}
324-
325319
static inline u64 spte_shadow_dirty_mask(u64 spte)
326320
{
327321
KVM_MMU_WARN_ON(!is_shadow_present_pte(spte));
@@ -355,10 +349,7 @@ static inline kvm_pfn_t spte_to_pfn(u64 pte)
355349

356350
static inline bool is_accessed_spte(u64 spte)
357351
{
358-
u64 accessed_mask = spte_shadow_accessed_mask(spte);
359-
360-
return accessed_mask ? spte & accessed_mask
361-
: !is_access_track_spte(spte);
352+
return spte & shadow_accessed_mask;
362353
}
363354

364355
static inline u64 get_rsvd_bits(struct rsvd_bits_validate *rsvd_check, u64 pte,

0 commit comments

Comments
 (0)