locking.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =================
  3. KVM Lock Overview
  4. =================
  5. 1. Acquisition Orders
  6. ---------------------
  7. The acquisition orders for mutexes are as follows:
  8. - cpus_read_lock() is taken outside kvm_lock
  9. - kvm_usage_lock is taken outside cpus_read_lock()
  10. - kvm->lock is taken outside vcpu->mutex
  11. - kvm->lock is taken outside kvm->slots_lock and kvm->irq_lock
  12. - kvm->slots_lock is taken outside kvm->irq_lock, though acquiring
  13. them together is quite rare.
  14. - kvm->mn_active_invalidate_count ensures that pairs of
  15. invalidate_range_start() and invalidate_range_end() callbacks
  16. use the same memslots array. kvm->slots_lock and kvm->slots_arch_lock
  17. are taken on the waiting side when modifying memslots, so MMU notifiers
  18. must not take either kvm->slots_lock or kvm->slots_arch_lock.
  19. cpus_read_lock() vs kvm_lock:
  20. - Taking cpus_read_lock() outside of kvm_lock is problematic, despite that
  21. being the official ordering, as it is quite easy to unknowingly trigger
  22. cpus_read_lock() while holding kvm_lock. Use caution when walking vm_list,
  23. e.g. avoid complex operations when possible.
  24. For SRCU:
  25. - ``synchronize_srcu(&kvm->srcu)`` is called inside critical sections
  26. for kvm->lock, vcpu->mutex and kvm->slots_lock. These locks _cannot_
  27. be taken inside a kvm->srcu read-side critical section; that is, the
  28. following is broken::
  29. srcu_read_lock(&kvm->srcu);
  30. mutex_lock(&kvm->slots_lock);
  31. - kvm->slots_arch_lock instead is released before the call to
  32. ``synchronize_srcu()``. It _can_ therefore be taken inside a
  33. kvm->srcu read-side critical section, for example while processing
  34. a vmexit.
  35. On x86:
  36. - vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock and kvm->arch.xen.xen_lock
  37. - kvm->arch.mmu_lock is an rwlock; critical sections for
  38. kvm->arch.tdp_mmu_pages_lock and kvm->arch.mmu_unsync_pages_lock must
  39. also take kvm->arch.mmu_lock
  40. Everything else is a leaf: no other lock is taken inside the critical
  41. sections.
  42. 2. Exception
  43. ------------
  44. Fast page fault:
  45. Fast page fault is the fast path which fixes the guest page fault out of
  46. the mmu-lock on x86. Currently, the page fault can be fast in one of the
  47. following two cases:
  48. 1. Access Tracking: The SPTE is not present, but it is marked for access
  49. tracking. That means we need to restore the saved R/X bits. This is
  50. described in more detail later below.
  51. 2. Write-Protection: The SPTE is present and the fault is caused by
  52. write-protect. That means we just need to change the W bit of the spte.
  53. What we use to avoid all the races is the Host-writable bit and MMU-writable bit
  54. on the spte:
  55. - Host-writable means the gfn is writable in the host kernel page tables and in
  56. its KVM memslot.
  57. - MMU-writable means the gfn is writable in the guest's mmu and it is not
  58. write-protected by shadow page write-protection.
  59. On fast page fault path, we will use cmpxchg to atomically set the spte W
  60. bit if spte.HOST_WRITEABLE = 1 and spte.WRITE_PROTECT = 1, to restore the saved
  61. R/X bits if for an access-traced spte, or both. This is safe because whenever
  62. changing these bits can be detected by cmpxchg.
  63. But we need carefully check these cases:
  64. 1) The mapping from gfn to pfn
  65. The mapping from gfn to pfn may be changed since we can only ensure the pfn
  66. is not changed during cmpxchg. This is a ABA problem, for example, below case
  67. will happen:
  68. +------------------------------------------------------------------------+
  69. | At the beginning:: |
  70. | |
  71. | gpte = gfn1 |
  72. | gfn1 is mapped to pfn1 on host |
  73. | spte is the shadow page table entry corresponding with gpte and |
  74. | spte = pfn1 |
  75. +------------------------------------------------------------------------+
  76. | On fast page fault path: |
  77. +------------------------------------+-----------------------------------+
  78. | CPU 0: | CPU 1: |
  79. +------------------------------------+-----------------------------------+
  80. | :: | |
  81. | | |
  82. | old_spte = *spte; | |
  83. +------------------------------------+-----------------------------------+
  84. | | pfn1 is swapped out:: |
  85. | | |
  86. | | spte = 0; |
  87. | | |
  88. | | pfn1 is re-alloced for gfn2. |
  89. | | |
  90. | | gpte is changed to point to |
  91. | | gfn2 by the guest:: |
  92. | | |
  93. | | spte = pfn1; |
  94. +------------------------------------+-----------------------------------+
  95. | :: |
  96. | |
  97. | if (cmpxchg(spte, old_spte, old_spte+W) |
  98. | mark_page_dirty(vcpu->kvm, gfn1) |
  99. | OOPS!!! |
  100. +------------------------------------------------------------------------+
  101. We dirty-log for gfn1, that means gfn2 is lost in dirty-bitmap.
  102. For direct sp, we can easily avoid it since the spte of direct sp is fixed
  103. to gfn. For indirect sp, we disabled fast page fault for simplicity.
  104. A solution for indirect sp could be to pin the gfn, for example via
  105. gfn_to_pfn_memslot_atomic, before the cmpxchg. After the pinning:
  106. - We have held the refcount of pfn; that means the pfn can not be freed and
  107. be reused for another gfn.
  108. - The pfn is writable and therefore it cannot be shared between different gfns
  109. by KSM.
  110. Then, we can ensure the dirty bitmaps is correctly set for a gfn.
  111. 2) Dirty bit tracking
  112. In the origin code, the spte can be fast updated (non-atomically) if the
  113. spte is read-only and the Accessed bit has already been set since the
  114. Accessed bit and Dirty bit can not be lost.
  115. But it is not true after fast page fault since the spte can be marked
  116. writable between reading spte and updating spte. Like below case:
  117. +------------------------------------------------------------------------+
  118. | At the beginning:: |
  119. | |
  120. | spte.W = 0 |
  121. | spte.Accessed = 1 |
  122. +------------------------------------+-----------------------------------+
  123. | CPU 0: | CPU 1: |
  124. +------------------------------------+-----------------------------------+
  125. | In mmu_spte_clear_track_bits():: | |
  126. | | |
  127. | old_spte = *spte; | |
  128. | | |
  129. | | |
  130. | /* 'if' condition is satisfied. */| |
  131. | if (old_spte.Accessed == 1 && | |
  132. | old_spte.W == 0) | |
  133. | spte = 0ull; | |
  134. +------------------------------------+-----------------------------------+
  135. | | on fast page fault path:: |
  136. | | |
  137. | | spte.W = 1 |
  138. | | |
  139. | | memory write on the spte:: |
  140. | | |
  141. | | spte.Dirty = 1 |
  142. +------------------------------------+-----------------------------------+
  143. | :: | |
  144. | | |
  145. | else | |
  146. | old_spte = xchg(spte, 0ull) | |
  147. | if (old_spte.Accessed == 1) | |
  148. | kvm_set_pfn_accessed(spte.pfn);| |
  149. | if (old_spte.Dirty == 1) | |
  150. | kvm_set_pfn_dirty(spte.pfn); | |
  151. | OOPS!!! | |
  152. +------------------------------------+-----------------------------------+
  153. The Dirty bit is lost in this case.
  154. In order to avoid this kind of issue, we always treat the spte as "volatile"
  155. if it can be updated out of mmu-lock [see spte_has_volatile_bits()]; it means
  156. the spte is always atomically updated in this case.
  157. 3) flush tlbs due to spte updated
  158. If the spte is updated from writable to read-only, we should flush all TLBs,
  159. otherwise rmap_write_protect will find a read-only spte, even though the
  160. writable spte might be cached on a CPU's TLB.
  161. As mentioned before, the spte can be updated to writable out of mmu-lock on
  162. fast page fault path. In order to easily audit the path, we see if TLBs needing
  163. to be flushed caused this reason in mmu_spte_update() since this is a common
  164. function to update spte (present -> present).
  165. Since the spte is "volatile" if it can be updated out of mmu-lock, we always
  166. atomically update the spte and the race caused by fast page fault can be avoided.
  167. See the comments in spte_has_volatile_bits() and mmu_spte_update().
  168. Lockless Access Tracking:
  169. This is used for Intel CPUs that are using EPT but do not support the EPT A/D
  170. bits. In this case, PTEs are tagged as A/D disabled (using ignored bits), and
  171. when the KVM MMU notifier is called to track accesses to a page (via
  172. kvm_mmu_notifier_clear_flush_young), it marks the PTE not-present in hardware
  173. by clearing the RWX bits in the PTE and storing the original R & X bits in more
  174. unused/ignored bits. When the VM tries to access the page later on, a fault is
  175. generated and the fast page fault mechanism described above is used to
  176. atomically restore the PTE to a Present state. The W bit is not saved when the
  177. PTE is marked for access tracking and during restoration to the Present state,
  178. the W bit is set depending on whether or not it was a write access. If it
  179. wasn't, then the W bit will remain clear until a write access happens, at which
  180. time it will be set using the Dirty tracking mechanism described above.
  181. 3. Reference
  182. ------------
  183. ``kvm_lock``
  184. ^^^^^^^^^^^^
  185. :Type: mutex
  186. :Arch: any
  187. :Protects: - vm_list
  188. ``kvm_usage_lock``
  189. ^^^^^^^^^^^^^^^^^^
  190. :Type: mutex
  191. :Arch: any
  192. :Protects: - kvm_usage_count
  193. - hardware virtualization enable/disable
  194. :Comment: Exists to allow taking cpus_read_lock() while kvm_usage_count is
  195. protected, which simplifies the virtualization enabling logic.
  196. ``kvm->mn_invalidate_lock``
  197. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  198. :Type: spinlock_t
  199. :Arch: any
  200. :Protects: mn_active_invalidate_count, mn_memslots_update_rcuwait
  201. ``kvm_arch::tsc_write_lock``
  202. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  203. :Type: raw_spinlock_t
  204. :Arch: x86
  205. :Protects: - kvm_arch::{last_tsc_write,last_tsc_nsec,last_tsc_offset}
  206. - tsc offset in vmcb
  207. :Comment: 'raw' because updating the tsc offsets must not be preempted.
  208. ``kvm->mmu_lock``
  209. ^^^^^^^^^^^^^^^^^
  210. :Type: spinlock_t or rwlock_t
  211. :Arch: any
  212. :Protects: -shadow page/shadow tlb entry
  213. :Comment: it is a spinlock since it is used in mmu notifier.
  214. ``kvm->srcu``
  215. ^^^^^^^^^^^^^
  216. :Type: srcu lock
  217. :Arch: any
  218. :Protects: - kvm->memslots
  219. - kvm->buses
  220. :Comment: The srcu read lock must be held while accessing memslots (e.g.
  221. when using gfn_to_* functions) and while accessing in-kernel
  222. MMIO/PIO address->device structure mapping (kvm->buses).
  223. The srcu index can be stored in kvm_vcpu->srcu_idx per vcpu
  224. if it is needed by multiple functions.
  225. ``kvm->slots_arch_lock``
  226. ^^^^^^^^^^^^^^^^^^^^^^^^
  227. :Type: mutex
  228. :Arch: any (only needed on x86 though)
  229. :Protects: any arch-specific fields of memslots that have to be modified
  230. in a ``kvm->srcu`` read-side critical section.
  231. :Comment: must be held before reading the pointer to the current memslots,
  232. until after all changes to the memslots are complete
  233. ``wakeup_vcpus_on_cpu_lock``
  234. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  235. :Type: spinlock_t
  236. :Arch: x86
  237. :Protects: wakeup_vcpus_on_cpu
  238. :Comment: This is a per-CPU lock and it is used for VT-d posted-interrupts.
  239. When VT-d posted-interrupts are supported and the VM has assigned
  240. devices, we put the blocked vCPU on the list blocked_vcpu_on_cpu
  241. protected by blocked_vcpu_on_cpu_lock. When VT-d hardware issues
  242. wakeup notification event since external interrupts from the
  243. assigned devices happens, we will find the vCPU on the list to
  244. wakeup.
  245. ``vendor_module_lock``
  246. ^^^^^^^^^^^^^^^^^^^^^^
  247. :Type: mutex
  248. :Arch: x86
  249. :Protects: loading a vendor module (kvm_amd or kvm_intel)
  250. :Comment: Exists because using kvm_lock leads to deadlock. kvm_lock is taken
  251. in notifiers, e.g. __kvmclock_cpufreq_notifier(), that may be invoked while
  252. cpu_hotplug_lock is held, e.g. from cpufreq_boost_trigger_state(), and many
  253. operations need to take cpu_hotplug_lock when loading a vendor module, e.g.
  254. updating static calls.