Skip to content

Commit 697c89b

Browse files
shvipinsean-jc
authored andcommitted
KVM: x86/mmu: Consolidate Dirty vs. Writable clearing logic in TDP MMU
Deduplicate the guts of the TDP MMU's clearing of dirty status by snapshotting whether to check+clear the Dirty bit vs. the Writable bit, which is the only difference between the two flavors of dirty tracking. Note, kvm_ad_enabled() is just a wrapper for shadow_accessed_mask, i.e. is constant after kvm-{intel,amd}.ko is loaded. Link: https://lore.kernel.org/all/Yz4Qi7cn7TWTWQjj@google.com Signed-off-by: Vipin Sharma <vipinsh@google.com> [sean: split to separate patch, apply to dirty log, write changelog] Link: https://lore.kernel.org/r/20230321220021.2119033-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 5982a53 commit 697c89b

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,8 +1607,8 @@ void kvm_tdp_mmu_try_split_huge_pages(struct kvm *kvm,
16071607
static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
16081608
gfn_t start, gfn_t end)
16091609
{
1610+
u64 dbit = kvm_ad_enabled() ? shadow_dirty_mask : PT_WRITABLE_MASK;
16101611
struct tdp_iter iter;
1611-
u64 new_spte;
16121612
bool spte_set = false;
16131613

16141614
rcu_read_lock();
@@ -1624,19 +1624,10 @@ static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
16241624
MMU_WARN_ON(kvm_ad_enabled() &&
16251625
spte_ad_need_write_protect(iter.old_spte));
16261626

1627-
if (!kvm_ad_enabled()) {
1628-
if (is_writable_pte(iter.old_spte))
1629-
new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
1630-
else
1631-
continue;
1632-
} else {
1633-
if (iter.old_spte & shadow_dirty_mask)
1634-
new_spte = iter.old_spte & ~shadow_dirty_mask;
1635-
else
1636-
continue;
1637-
}
1627+
if (!(iter.old_spte & dbit))
1628+
continue;
16381629

1639-
if (tdp_mmu_set_spte_atomic(kvm, &iter, new_spte))
1630+
if (tdp_mmu_set_spte_atomic(kvm, &iter, iter.old_spte & ~dbit))
16401631
goto retry;
16411632

16421633
spte_set = true;
@@ -1678,8 +1669,9 @@ bool kvm_tdp_mmu_clear_dirty_slot(struct kvm *kvm,
16781669
static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root,
16791670
gfn_t gfn, unsigned long mask, bool wrprot)
16801671
{
1672+
u64 dbit = (wrprot || !kvm_ad_enabled()) ? PT_WRITABLE_MASK :
1673+
shadow_dirty_mask;
16811674
struct tdp_iter iter;
1682-
u64 new_spte;
16831675

16841676
rcu_read_lock();
16851677

@@ -1697,19 +1689,10 @@ static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root,
16971689

16981690
mask &= ~(1UL << (iter.gfn - gfn));
16991691

1700-
if (wrprot || !kvm_ad_enabled()) {
1701-
if (is_writable_pte(iter.old_spte))
1702-
new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
1703-
else
1704-
continue;
1705-
} else {
1706-
if (iter.old_spte & shadow_dirty_mask)
1707-
new_spte = iter.old_spte & ~shadow_dirty_mask;
1708-
else
1709-
continue;
1710-
}
1692+
if (!(iter.old_spte & dbit))
1693+
continue;
17111694

1712-
tdp_mmu_set_spte_no_dirty_log(kvm, &iter, new_spte);
1695+
tdp_mmu_set_spte_no_dirty_log(kvm, &iter, iter.old_spte & ~dbit);
17131696
}
17141697

17151698
rcu_read_unlock();

0 commit comments

Comments
 (0)