locking.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. KVM Lock Overview
  2. =================
  3. 1. Acquisition Orders
  4. ---------------------
  5. The acquisition orders for mutexes are as follows:
  6. - kvm->lock is taken outside vcpu->mutex
  7. - kvm->lock is taken outside kvm->slots_lock and kvm->irq_lock
  8. - kvm->slots_lock is taken outside kvm->irq_lock, though acquiring
  9. them together is quite rare.
  10. On x86, vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock.
  11. Everything else is a leaf: no other lock is taken inside the critical
  12. sections.
  13. 2: Exception
  14. ------------
  15. Fast page fault:
  16. Fast page fault is the fast path which fixes the guest page fault out of
  17. the mmu-lock on x86. Currently, the page fault can be fast in one of the
  18. following two cases:
  19. 1. Access Tracking: The SPTE is not present, but it is marked for access
  20. tracking i.e. the SPTE_SPECIAL_MASK is set. That means we need to
  21. restore the saved R/X bits. This is described in more detail later below.
  22. 2. Write-Protection: The SPTE is present and the fault is
  23. caused by write-protect. That means we just need to change the W bit of the
  24. spte.
  25. What we use to avoid all the race is the SPTE_HOST_WRITEABLE bit and
  26. SPTE_MMU_WRITEABLE bit on the spte:
  27. - SPTE_HOST_WRITEABLE means the gfn is writable on host.
  28. - SPTE_MMU_WRITEABLE means the gfn is writable on mmu. The bit is set when
  29. the gfn is writable on guest mmu and it is not write-protected by shadow
  30. page write-protection.
  31. On fast page fault path, we will use cmpxchg to atomically set the spte W
  32. bit if spte.SPTE_HOST_WRITEABLE = 1 and spte.SPTE_WRITE_PROTECT = 1, or
  33. restore the saved R/X bits if VMX_EPT_TRACK_ACCESS mask is set, or both. This
  34. is safe because whenever changing these bits can be detected by cmpxchg.
  35. But we need carefully check these cases:
  36. 1): The mapping from gfn to pfn
  37. The mapping from gfn to pfn may be changed since we can only ensure the pfn
  38. is not changed during cmpxchg. This is a ABA problem, for example, below case
  39. will happen:
  40. At the beginning:
  41. gpte = gfn1
  42. gfn1 is mapped to pfn1 on host
  43. spte is the shadow page table entry corresponding with gpte and
  44. spte = pfn1
  45. VCPU 0 VCPU0
  46. on fast page fault path:
  47. old_spte = *spte;
  48. pfn1 is swapped out:
  49. spte = 0;
  50. pfn1 is re-alloced for gfn2.
  51. gpte is changed to point to
  52. gfn2 by the guest:
  53. spte = pfn1;
  54. if (cmpxchg(spte, old_spte, old_spte+W)
  55. mark_page_dirty(vcpu->kvm, gfn1)
  56. OOPS!!!
  57. We dirty-log for gfn1, that means gfn2 is lost in dirty-bitmap.
  58. For direct sp, we can easily avoid it since the spte of direct sp is fixed
  59. to gfn. For indirect sp, before we do cmpxchg, we call gfn_to_pfn_atomic()
  60. to pin gfn to pfn, because after gfn_to_pfn_atomic():
  61. - We have held the refcount of pfn that means the pfn can not be freed and
  62. be reused for another gfn.
  63. - The pfn is writable that means it can not be shared between different gfns
  64. by KSM.
  65. Then, we can ensure the dirty bitmaps is correctly set for a gfn.
  66. Currently, to simplify the whole things, we disable fast page fault for
  67. indirect shadow page.
  68. 2): Dirty bit tracking
  69. In the origin code, the spte can be fast updated (non-atomically) if the
  70. spte is read-only and the Accessed bit has already been set since the
  71. Accessed bit and Dirty bit can not be lost.
  72. But it is not true after fast page fault since the spte can be marked
  73. writable between reading spte and updating spte. Like below case:
  74. At the beginning:
  75. spte.W = 0
  76. spte.Accessed = 1
  77. VCPU 0 VCPU0
  78. In mmu_spte_clear_track_bits():
  79. old_spte = *spte;
  80. /* 'if' condition is satisfied. */
  81. if (old_spte.Accessed == 1 &&
  82. old_spte.W == 0)
  83. spte = 0ull;
  84. on fast page fault path:
  85. spte.W = 1
  86. memory write on the spte:
  87. spte.Dirty = 1
  88. else
  89. old_spte = xchg(spte, 0ull)
  90. if (old_spte.Accessed == 1)
  91. kvm_set_pfn_accessed(spte.pfn);
  92. if (old_spte.Dirty == 1)
  93. kvm_set_pfn_dirty(spte.pfn);
  94. OOPS!!!
  95. The Dirty bit is lost in this case.
  96. In order to avoid this kind of issue, we always treat the spte as "volatile"
  97. if it can be updated out of mmu-lock, see spte_has_volatile_bits(), it means,
  98. the spte is always atomically updated in this case.
  99. 3): flush tlbs due to spte updated
  100. If the spte is updated from writable to readonly, we should flush all TLBs,
  101. otherwise rmap_write_protect will find a read-only spte, even though the
  102. writable spte might be cached on a CPU's TLB.
  103. As mentioned before, the spte can be updated to writable out of mmu-lock on
  104. fast page fault path, in order to easily audit the path, we see if TLBs need
  105. be flushed caused by this reason in mmu_spte_update() since this is a common
  106. function to update spte (present -> present).
  107. Since the spte is "volatile" if it can be updated out of mmu-lock, we always
  108. atomically update the spte, the race caused by fast page fault can be avoided,
  109. See the comments in spte_has_volatile_bits() and mmu_spte_update().
  110. Lockless Access Tracking:
  111. This is used for Intel CPUs that are using EPT but do not support the EPT A/D
  112. bits. In this case, when the KVM MMU notifier is called to track accesses to a
  113. page (via kvm_mmu_notifier_clear_flush_young), it marks the PTE as not-present
  114. by clearing the RWX bits in the PTE and storing the original R & X bits in
  115. some unused/ignored bits. In addition, the SPTE_SPECIAL_MASK is also set on the
  116. PTE (using the ignored bit 62). When the VM tries to access the page later on,
  117. a fault is generated and the fast page fault mechanism described above is used
  118. to atomically restore the PTE to a Present state. The W bit is not saved when
  119. the PTE is marked for access tracking and during restoration to the Present
  120. state, the W bit is set depending on whether or not it was a write access. If
  121. it wasn't, then the W bit will remain clear until a write access happens, at
  122. which time it will be set using the Dirty tracking mechanism described above.
  123. 3. Reference
  124. ------------
  125. Name: kvm_lock
  126. Type: mutex
  127. Arch: any
  128. Protects: - vm_list
  129. Name: kvm_count_lock
  130. Type: raw_spinlock_t
  131. Arch: any
  132. Protects: - hardware virtualization enable/disable
  133. Comment: 'raw' because hardware enabling/disabling must be atomic /wrt
  134. migration.
  135. Name: kvm_arch::tsc_write_lock
  136. Type: raw_spinlock
  137. Arch: x86
  138. Protects: - kvm_arch::{last_tsc_write,last_tsc_nsec,last_tsc_offset}
  139. - tsc offset in vmcb
  140. Comment: 'raw' because updating the tsc offsets must not be preempted.
  141. Name: kvm->mmu_lock
  142. Type: spinlock_t
  143. Arch: any
  144. Protects: -shadow page/shadow tlb entry
  145. Comment: it is a spinlock since it is used in mmu notifier.
  146. Name: kvm->srcu
  147. Type: srcu lock
  148. Arch: any
  149. Protects: - kvm->memslots
  150. - kvm->buses
  151. Comment: The srcu read lock must be held while accessing memslots (e.g.
  152. when using gfn_to_* functions) and while accessing in-kernel
  153. MMIO/PIO address->device structure mapping (kvm->buses).
  154. The srcu index can be stored in kvm_vcpu->srcu_idx per vcpu
  155. if it is needed by multiple functions.
  156. Name: blocked_vcpu_on_cpu_lock
  157. Type: spinlock_t
  158. Arch: x86
  159. Protects: blocked_vcpu_on_cpu
  160. Comment: This is a per-CPU lock and it is used for VT-d posted-interrupts.
  161. When VT-d posted-interrupts is supported and the VM has assigned
  162. devices, we put the blocked vCPU on the list blocked_vcpu_on_cpu
  163. protected by blocked_vcpu_on_cpu_lock, when VT-d hardware issues
  164. wakeup notification event since external interrupts from the
  165. assigned devices happens, we will find the vCPU on the list to
  166. wakeup.