locking.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. For spinlocks, kvm_lock is taken outside kvm->mmu_lock. Everything
  11. else is a leaf: no other lock is taken inside the critical sections.
  12. 2: Exception
  13. ------------
  14. Fast page fault:
  15. Fast page fault is the fast path which fixes the guest page fault out of
  16. the mmu-lock on x86. Currently, the page fault can be fast only if the
  17. shadow page table is present and it is caused by write-protect, that means
  18. we just need change the W bit of the spte.
  19. What we use to avoid all the race is the SPTE_HOST_WRITEABLE bit and
  20. SPTE_MMU_WRITEABLE bit on the spte:
  21. - SPTE_HOST_WRITEABLE means the gfn is writable on host.
  22. - SPTE_MMU_WRITEABLE means the gfn is writable on mmu. The bit is set when
  23. the gfn is writable on guest mmu and it is not write-protected by shadow
  24. page write-protection.
  25. On fast page fault path, we will use cmpxchg to atomically set the spte W
  26. bit if spte.SPTE_HOST_WRITEABLE = 1 and spte.SPTE_WRITE_PROTECT = 1, this
  27. is safe because whenever changing these bits can be detected by cmpxchg.
  28. But we need carefully check these cases:
  29. 1): The mapping from gfn to pfn
  30. The mapping from gfn to pfn may be changed since we can only ensure the pfn
  31. is not changed during cmpxchg. This is a ABA problem, for example, below case
  32. will happen:
  33. At the beginning:
  34. gpte = gfn1
  35. gfn1 is mapped to pfn1 on host
  36. spte is the shadow page table entry corresponding with gpte and
  37. spte = pfn1
  38. VCPU 0 VCPU0
  39. on fast page fault path:
  40. old_spte = *spte;
  41. pfn1 is swapped out:
  42. spte = 0;
  43. pfn1 is re-alloced for gfn2.
  44. gpte is changed to point to
  45. gfn2 by the guest:
  46. spte = pfn1;
  47. if (cmpxchg(spte, old_spte, old_spte+W)
  48. mark_page_dirty(vcpu->kvm, gfn1)
  49. OOPS!!!
  50. We dirty-log for gfn1, that means gfn2 is lost in dirty-bitmap.
  51. For direct sp, we can easily avoid it since the spte of direct sp is fixed
  52. to gfn. For indirect sp, before we do cmpxchg, we call gfn_to_pfn_atomic()
  53. to pin gfn to pfn, because after gfn_to_pfn_atomic():
  54. - We have held the refcount of pfn that means the pfn can not be freed and
  55. be reused for another gfn.
  56. - The pfn is writable that means it can not be shared between different gfns
  57. by KSM.
  58. Then, we can ensure the dirty bitmaps is correctly set for a gfn.
  59. Currently, to simplify the whole things, we disable fast page fault for
  60. indirect shadow page.
  61. 2): Dirty bit tracking
  62. In the origin code, the spte can be fast updated (non-atomically) if the
  63. spte is read-only and the Accessed bit has already been set since the
  64. Accessed bit and Dirty bit can not be lost.
  65. But it is not true after fast page fault since the spte can be marked
  66. writable between reading spte and updating spte. Like below case:
  67. At the beginning:
  68. spte.W = 0
  69. spte.Accessed = 1
  70. VCPU 0 VCPU0
  71. In mmu_spte_clear_track_bits():
  72. old_spte = *spte;
  73. /* 'if' condition is satisfied. */
  74. if (old_spte.Accessed == 1 &&
  75. old_spte.W == 0)
  76. spte = 0ull;
  77. on fast page fault path:
  78. spte.W = 1
  79. memory write on the spte:
  80. spte.Dirty = 1
  81. else
  82. old_spte = xchg(spte, 0ull)
  83. if (old_spte.Accessed == 1)
  84. kvm_set_pfn_accessed(spte.pfn);
  85. if (old_spte.Dirty == 1)
  86. kvm_set_pfn_dirty(spte.pfn);
  87. OOPS!!!
  88. The Dirty bit is lost in this case.
  89. In order to avoid this kind of issue, we always treat the spte as "volatile"
  90. if it can be updated out of mmu-lock, see spte_has_volatile_bits(), it means,
  91. the spte is always atomically updated in this case.
  92. 3): flush tlbs due to spte updated
  93. If the spte is updated from writable to readonly, we should flush all TLBs,
  94. otherwise rmap_write_protect will find a read-only spte, even though the
  95. writable spte might be cached on a CPU's TLB.
  96. As mentioned before, the spte can be updated to writable out of mmu-lock on
  97. fast page fault path, in order to easily audit the path, we see if TLBs need
  98. be flushed caused by this reason in mmu_spte_update() since this is a common
  99. function to update spte (present -> present).
  100. Since the spte is "volatile" if it can be updated out of mmu-lock, we always
  101. atomically update the spte, the race caused by fast page fault can be avoided,
  102. See the comments in spte_has_volatile_bits() and mmu_spte_update().
  103. 3. Reference
  104. ------------
  105. Name: kvm_lock
  106. Type: spinlock_t
  107. Arch: any
  108. Protects: - vm_list
  109. Name: kvm_count_lock
  110. Type: raw_spinlock_t
  111. Arch: any
  112. Protects: - hardware virtualization enable/disable
  113. Comment: 'raw' because hardware enabling/disabling must be atomic /wrt
  114. migration.
  115. Name: kvm_arch::tsc_write_lock
  116. Type: raw_spinlock
  117. Arch: x86
  118. Protects: - kvm_arch::{last_tsc_write,last_tsc_nsec,last_tsc_offset}
  119. - tsc offset in vmcb
  120. Comment: 'raw' because updating the tsc offsets must not be preempted.
  121. Name: kvm->mmu_lock
  122. Type: spinlock_t
  123. Arch: any
  124. Protects: -shadow page/shadow tlb entry
  125. Comment: it is a spinlock since it is used in mmu notifier.
  126. Name: kvm->srcu
  127. Type: srcu lock
  128. Arch: any
  129. Protects: - kvm->memslots
  130. - kvm->buses
  131. Comment: The srcu read lock must be held while accessing memslots (e.g.
  132. when using gfn_to_* functions) and while accessing in-kernel
  133. MMIO/PIO address->device structure mapping (kvm->buses).
  134. The srcu index can be stored in kvm_vcpu->srcu_idx per vcpu
  135. if it is needed by multiple functions.
  136. Name: blocked_vcpu_on_cpu_lock
  137. Type: spinlock_t
  138. Arch: x86
  139. Protects: blocked_vcpu_on_cpu
  140. Comment: This is a per-CPU lock and it is used for VT-d posted-interrupts.
  141. When VT-d posted-interrupts is supported and the VM has assigned
  142. devices, we put the blocked vCPU on the list blocked_vcpu_on_cpu
  143. protected by blocked_vcpu_on_cpu_lock, when VT-d hardware issues
  144. wakeup notification event since external interrupts from the
  145. assigned devices happens, we will find the vCPU on the list to
  146. wakeup.