tdp_iter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef __KVM_X86_MMU_TDP_ITER_H
  3. #define __KVM_X86_MMU_TDP_ITER_H
  4. #include <linux/kvm_host.h>
  5. #include "mmu.h"
  6. #include "spte.h"
  7. /*
  8. * TDP MMU SPTEs are RCU protected to allow paging structures (non-leaf SPTEs)
  9. * to be zapped while holding mmu_lock for read, and to allow TLB flushes to be
  10. * batched without having to collect the list of zapped SPs. Flows that can
  11. * remove SPs must service pending TLB flushes prior to dropping RCU protection.
  12. */
  13. static inline u64 kvm_tdp_mmu_read_spte(tdp_ptep_t sptep)
  14. {
  15. return READ_ONCE(*rcu_dereference(sptep));
  16. }
  17. static inline u64 kvm_tdp_mmu_write_spte_atomic(tdp_ptep_t sptep, u64 new_spte)
  18. {
  19. return xchg(rcu_dereference(sptep), new_spte);
  20. }
  21. static inline void __kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 new_spte)
  22. {
  23. WRITE_ONCE(*rcu_dereference(sptep), new_spte);
  24. }
  25. static inline u64 kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 old_spte,
  26. u64 new_spte, int level)
  27. {
  28. /*
  29. * Atomically write the SPTE if it is a shadow-present, leaf SPTE with
  30. * volatile bits, i.e. has bits that can be set outside of mmu_lock.
  31. * The Writable bit can be set by KVM's fast page fault handler, and
  32. * Accessed and Dirty bits can be set by the CPU.
  33. *
  34. * Note, non-leaf SPTEs do have Accessed bits and those bits are
  35. * technically volatile, but KVM doesn't consume the Accessed bit of
  36. * non-leaf SPTEs, i.e. KVM doesn't care if it clobbers the bit. This
  37. * logic needs to be reassessed if KVM were to use non-leaf Accessed
  38. * bits, e.g. to skip stepping down into child SPTEs when aging SPTEs.
  39. */
  40. if (is_shadow_present_pte(old_spte) && is_last_spte(old_spte, level) &&
  41. spte_has_volatile_bits(old_spte))
  42. return kvm_tdp_mmu_write_spte_atomic(sptep, new_spte);
  43. __kvm_tdp_mmu_write_spte(sptep, new_spte);
  44. return old_spte;
  45. }
  46. /*
  47. * A TDP iterator performs a pre-order walk over a TDP paging structure.
  48. */
  49. struct tdp_iter {
  50. /*
  51. * The iterator will traverse the paging structure towards the mapping
  52. * for this GFN.
  53. */
  54. gfn_t next_last_level_gfn;
  55. /*
  56. * The next_last_level_gfn at the time when the thread last
  57. * yielded. Only yielding when the next_last_level_gfn !=
  58. * yielded_gfn helps ensure forward progress.
  59. */
  60. gfn_t yielded_gfn;
  61. /* Pointers to the page tables traversed to reach the current SPTE */
  62. tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL];
  63. /* A pointer to the current SPTE */
  64. tdp_ptep_t sptep;
  65. /* The lowest GFN mapped by the current SPTE */
  66. gfn_t gfn;
  67. /* The level of the root page given to the iterator */
  68. int root_level;
  69. /* The lowest level the iterator should traverse to */
  70. int min_level;
  71. /* The iterator's current level within the paging structure */
  72. int level;
  73. /* The address space ID, i.e. SMM vs. regular. */
  74. int as_id;
  75. /* A snapshot of the value at sptep */
  76. u64 old_spte;
  77. /*
  78. * Whether the iterator has a valid state. This will be false if the
  79. * iterator walks off the end of the paging structure.
  80. */
  81. bool valid;
  82. /*
  83. * True if KVM dropped mmu_lock and yielded in the middle of a walk, in
  84. * which case tdp_iter_next() needs to restart the walk at the root
  85. * level instead of advancing to the next entry.
  86. */
  87. bool yielded;
  88. };
  89. /*
  90. * Iterates over every SPTE mapping the GFN range [start, end) in a
  91. * preorder traversal.
  92. */
  93. #define for_each_tdp_pte_min_level(iter, root, min_level, start, end) \
  94. for (tdp_iter_start(&iter, root, min_level, start); \
  95. iter.valid && iter.gfn < end; \
  96. tdp_iter_next(&iter))
  97. #define for_each_tdp_pte(iter, root, start, end) \
  98. for_each_tdp_pte_min_level(iter, root, PG_LEVEL_4K, start, end)
  99. tdp_ptep_t spte_to_child_pt(u64 pte, int level);
  100. void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
  101. int min_level, gfn_t next_last_level_gfn);
  102. void tdp_iter_next(struct tdp_iter *iter);
  103. void tdp_iter_restart(struct tdp_iter *iter);
  104. #endif /* __KVM_X86_MMU_TDP_ITER_H */