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