7 The acquisition orders for mutexes are as follows:
9 - kvm->lock is taken outside vcpu->mutex
11 - kvm->lock is taken outside kvm->slots_lock and kvm->irq_lock
13 - kvm->slots_lock is taken outside kvm->irq_lock, though acquiring
14 them together is quite rare.
16 For spinlocks, kvm_lock is taken outside kvm->mmu_lock. Everything
17 else is a leaf: no other lock is taken inside the critical sections.
24 Fast page fault is the fast path which fixes the guest page fault out of
25 the mmu-lock on x86. Currently, the page fault can be fast only if the
26 shadow page table is present and it is caused by write-protect, that means
27 we just need change the W bit of the spte.
29 What we use to avoid all the race is the SPTE_HOST_WRITEABLE bit and
30 SPTE_MMU_WRITEABLE bit on the spte:
31 - SPTE_HOST_WRITEABLE means the gfn is writable on host.
32 - SPTE_MMU_WRITEABLE means the gfn is writable on mmu. The bit is set when
33 the gfn is writable on guest mmu and it is not write-protected by shadow
34 page write-protection.
36 On fast page fault path, we will use cmpxchg to atomically set the spte W
37 bit if spte.SPTE_HOST_WRITEABLE = 1 and spte.SPTE_WRITE_PROTECT = 1, this
38 is safe because whenever changing these bits can be detected by cmpxchg.
40 But we need carefully check these cases:
41 1): The mapping from gfn to pfn
42 The mapping from gfn to pfn may be changed since we can only ensure the pfn
43 is not changed during cmpxchg. This is a ABA problem, for example, below case
48 gfn1 is mapped to pfn1 on host
49 spte is the shadow page table entry corresponding with gpte and
53 on fast page fault path:
59 pfn1 is re-alloced for gfn2.
61 gpte is changed to point to
65 if (cmpxchg(spte, old_spte, old_spte+W)
66 mark_page_dirty(vcpu->kvm, gfn1)
69 We dirty-log for gfn1, that means gfn2 is lost in dirty-bitmap.
71 For direct sp, we can easily avoid it since the spte of direct sp is fixed
72 to gfn. For indirect sp, before we do cmpxchg, we call gfn_to_pfn_atomic()
73 to pin gfn to pfn, because after gfn_to_pfn_atomic():
74 - We have held the refcount of pfn that means the pfn can not be freed and
75 be reused for another gfn.
76 - The pfn is writable that means it can not be shared between different gfns
79 Then, we can ensure the dirty bitmaps is correctly set for a gfn.
81 Currently, to simplify the whole things, we disable fast page fault for
84 2): Dirty bit tracking
85 In the origin code, the spte can be fast updated (non-atomically) if the
86 spte is read-only and the Accessed bit has already been set since the
87 Accessed bit and Dirty bit can not be lost.
89 But it is not true after fast page fault since the spte can be marked
90 writable between reading spte and updating spte. Like below case:
97 In mmu_spte_clear_track_bits():
101 /* 'if' condition is satisfied. */
102 if (old_spte.Accessed == 1 &&
105 on fast page fault path:
107 memory write on the spte:
112 old_spte = xchg(spte, 0ull)
115 if (old_spte.Accessed == 1)
116 kvm_set_pfn_accessed(spte.pfn);
117 if (old_spte.Dirty == 1)
118 kvm_set_pfn_dirty(spte.pfn);
121 The Dirty bit is lost in this case.
123 In order to avoid this kind of issue, we always treat the spte as "volatile"
124 if it can be updated out of mmu-lock, see spte_has_volatile_bits(), it means,
125 the spte is always atomically updated in this case.
127 3): flush tlbs due to spte updated
128 If the spte is updated from writable to readonly, we should flush all TLBs,
129 otherwise rmap_write_protect will find a read-only spte, even though the
130 writable spte might be cached on a CPU's TLB.
132 As mentioned before, the spte can be updated to writable out of mmu-lock on
133 fast page fault path, in order to easily audit the path, we see if TLBs need
134 be flushed caused by this reason in mmu_spte_update() since this is a common
135 function to update spte (present -> present).
137 Since the spte is "volatile" if it can be updated out of mmu-lock, we always
138 atomically update the spte, the race caused by fast page fault can be avoided,
139 See the comments in spte_has_volatile_bits() and mmu_spte_update().
152 Protects: - hardware virtualization enable/disable
153 Comment: 'raw' because hardware enabling/disabling must be atomic /wrt
156 Name: kvm_arch::tsc_write_lock
159 Protects: - kvm_arch::{last_tsc_write,last_tsc_nsec,last_tsc_offset}
161 Comment: 'raw' because updating the tsc offsets must not be preempted.
166 Protects: -shadow page/shadow tlb entry
167 Comment: it is a spinlock since it is used in mmu notifier.
172 Protects: - kvm->memslots
174 Comment: The srcu read lock must be held while accessing memslots (e.g.
175 when using gfn_to_* functions) and while accessing in-kernel
176 MMIO/PIO address->device structure mapping (kvm->buses).
177 The srcu index can be stored in kvm_vcpu->srcu_idx per vcpu
178 if it is needed by multiple functions.
180 Name: blocked_vcpu_on_cpu_lock
183 Protects: blocked_vcpu_on_cpu
184 Comment: This is a per-CPU lock and it is used for VT-d posted-interrupts.
185 When VT-d posted-interrupts is supported and the VM has assigned
186 devices, we put the blocked vCPU on the list blocked_vcpu_on_cpu
187 protected by blocked_vcpu_on_cpu_lock, when VT-d hardware issues
188 wakeup notification event since external interrupts from the
189 assigned devices happens, we will find the vCPU on the list to