Skip to content

Commit 41e0766

Browse files
shvipinsean-jc
authored andcommitted
KVM: x86/mmu: Add a helper function to check if an SPTE needs atomic write
Move conditions in kvm_tdp_mmu_write_spte() to check if an SPTE should be written atomically or not to a separate function. This new function, kvm_tdp_mmu_spte_need_atomic_write(), will be used in future commits to optimize clearing bits in SPTEs. Signed-off-by: Vipin Sharma <vipinsh@google.com> Reviewed-by: David Matlack <dmatlack@google.com> Reviewed-by: Ben Gardon <bgardon@google.com> Link: https://lore.kernel.org/r/20230321220021.2119033-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 50f1399 commit 41e0766

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

arch/x86/kvm/mmu/tdp_iter.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,29 @@ static inline void __kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 new_spte)
2929
WRITE_ONCE(*rcu_dereference(sptep), new_spte);
3030
}
3131

32+
/*
33+
* SPTEs must be modified atomically if they are shadow-present, leaf
34+
* SPTEs, and have volatile bits, i.e. has bits that can be set outside
35+
* of mmu_lock. The Writable bit can be set by KVM's fast page fault
36+
* handler, and Accessed and Dirty bits can be set by the CPU.
37+
*
38+
* Note, non-leaf SPTEs do have Accessed bits and those bits are
39+
* technically volatile, but KVM doesn't consume the Accessed bit of
40+
* non-leaf SPTEs, i.e. KVM doesn't care if it clobbers the bit. This
41+
* logic needs to be reassessed if KVM were to use non-leaf Accessed
42+
* bits, e.g. to skip stepping down into child SPTEs when aging SPTEs.
43+
*/
44+
static inline bool kvm_tdp_mmu_spte_need_atomic_write(u64 old_spte, int level)
45+
{
46+
return is_shadow_present_pte(old_spte) &&
47+
is_last_spte(old_spte, level) &&
48+
spte_has_volatile_bits(old_spte);
49+
}
50+
3251
static inline u64 kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 old_spte,
3352
u64 new_spte, int level)
3453
{
35-
/*
36-
* Atomically write the SPTE if it is a shadow-present, leaf SPTE with
37-
* volatile bits, i.e. has bits that can be set outside of mmu_lock.
38-
* The Writable bit can be set by KVM's fast page fault handler, and
39-
* Accessed and Dirty bits can be set by the CPU.
40-
*
41-
* Note, non-leaf SPTEs do have Accessed bits and those bits are
42-
* technically volatile, but KVM doesn't consume the Accessed bit of
43-
* non-leaf SPTEs, i.e. KVM doesn't care if it clobbers the bit. This
44-
* logic needs to be reassessed if KVM were to use non-leaf Accessed
45-
* bits, e.g. to skip stepping down into child SPTEs when aging SPTEs.
46-
*/
47-
if (is_shadow_present_pte(old_spte) && is_last_spte(old_spte, level) &&
48-
spte_has_volatile_bits(old_spte))
54+
if (kvm_tdp_mmu_spte_need_atomic_write(old_spte, level))
4955
return kvm_tdp_mmu_write_spte_atomic(sptep, new_spte);
5056

5157
__kvm_tdp_mmu_write_spte(sptep, new_spte);

0 commit comments

Comments
 (0)