Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kvm: x86: mmu: Update documentation for fast page fault mechanism

Add a brief description of the lockless access tracking mechanism
to the documentation of fast page faults in locking.txt.

Signed-off-by: Junaid Shahid <junaids@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Junaid Shahid and committed by
Radim Krčmář
63dbe14d f160c7b7

+27 -4
+27 -4
Documentation/virtual/kvm/locking.txt
··· 26 26 Fast page fault: 27 27 28 28 Fast page fault is the fast path which fixes the guest page fault out of 29 - the mmu-lock on x86. Currently, the page fault can be fast only if the 30 - shadow page table is present and it is caused by write-protect, that means 31 - we just need change the W bit of the spte. 29 + the mmu-lock on x86. Currently, the page fault can be fast in one of the 30 + following two cases: 31 + 32 + 1. Access Tracking: The SPTE is not present, but it is marked for access 33 + tracking i.e. the SPTE_SPECIAL_MASK is set. That means we need to 34 + restore the saved R/X bits. This is described in more detail later below. 35 + 36 + 2. Write-Protection: The SPTE is present and the fault is 37 + caused by write-protect. That means we just need to change the W bit of the 38 + spte. 32 39 33 40 What we use to avoid all the race is the SPTE_HOST_WRITEABLE bit and 34 41 SPTE_MMU_WRITEABLE bit on the spte: ··· 45 38 page write-protection. 46 39 47 40 On fast page fault path, we will use cmpxchg to atomically set the spte W 48 - bit if spte.SPTE_HOST_WRITEABLE = 1 and spte.SPTE_WRITE_PROTECT = 1, this 41 + bit if spte.SPTE_HOST_WRITEABLE = 1 and spte.SPTE_WRITE_PROTECT = 1, or 42 + restore the saved R/X bits if VMX_EPT_TRACK_ACCESS mask is set, or both. This 49 43 is safe because whenever changing these bits can be detected by cmpxchg. 50 44 51 45 But we need carefully check these cases: ··· 149 141 Since the spte is "volatile" if it can be updated out of mmu-lock, we always 150 142 atomically update the spte, the race caused by fast page fault can be avoided, 151 143 See the comments in spte_has_volatile_bits() and mmu_spte_update(). 144 + 145 + Lockless Access Tracking: 146 + 147 + This is used for Intel CPUs that are using EPT but do not support the EPT A/D 148 + bits. In this case, when the KVM MMU notifier is called to track accesses to a 149 + page (via kvm_mmu_notifier_clear_flush_young), it marks the PTE as not-present 150 + by clearing the RWX bits in the PTE and storing the original R & X bits in 151 + some unused/ignored bits. In addition, the SPTE_SPECIAL_MASK is also set on the 152 + PTE (using the ignored bit 62). When the VM tries to access the page later on, 153 + a fault is generated and the fast page fault mechanism described above is used 154 + to atomically restore the PTE to a Present state. The W bit is not saved when 155 + the PTE is marked for access tracking and during restoration to the Present 156 + state, the W bit is set depending on whether or not it was a write access. If 157 + it wasn't, then the W bit will remain clear until a write access happens, at 158 + which time it will be set using the Dirty tracking mechanism described above. 152 159 153 160 3. Reference 154 161 ------------