Commit 75e7fcdb authored by Mauro Carvalho Chehab's avatar Mauro Carvalho Chehab Committed by Paolo Bonzini

docs: kvm: Convert locking.txt to ReST format

- Use document title and chapter markups;
- Add markups for literal blocks;
- use :field: for field descriptions;
- Add blank lines and adjust indentation.
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 5a0af480
...@@ -12,6 +12,7 @@ KVM ...@@ -12,6 +12,7 @@ KVM
cpuid cpuid
halt-polling halt-polling
hypercalls hypercalls
locking
msr msr
vcpu-requests vcpu-requests
......
.. SPDX-License-Identifier: GPL-2.0
=================
KVM Lock Overview KVM Lock Overview
================= =================
...@@ -18,7 +21,7 @@ On x86, vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock. ...@@ -18,7 +21,7 @@ On x86, vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock.
Everything else is a leaf: no other lock is taken inside the critical Everything else is a leaf: no other lock is taken inside the critical
sections. sections.
2: Exception 2. Exception
------------ ------------
Fast page fault: Fast page fault:
...@@ -28,59 +31,74 @@ the mmu-lock on x86. Currently, the page fault can be fast in one of the ...@@ -28,59 +31,74 @@ the mmu-lock on x86. Currently, the page fault can be fast in one of the
following two cases: following two cases:
1. Access Tracking: The SPTE is not present, but it is marked for access 1. Access Tracking: The SPTE is not present, but it is marked for access
tracking i.e. the SPTE_SPECIAL_MASK is set. That means we need to tracking i.e. the SPTE_SPECIAL_MASK is set. That means we need to
restore the saved R/X bits. This is described in more detail later below. restore the saved R/X bits. This is described in more detail later below.
2. Write-Protection: The SPTE is present and the fault is 2. Write-Protection: The SPTE is present and the fault is
caused by write-protect. That means we just need to change the W bit of the caused by write-protect. That means we just need to change the W bit of
spte. the spte.
What we use to avoid all the race is the SPTE_HOST_WRITEABLE bit and What we use to avoid all the race is the SPTE_HOST_WRITEABLE bit and
SPTE_MMU_WRITEABLE bit on the spte: SPTE_MMU_WRITEABLE bit on the spte:
- SPTE_HOST_WRITEABLE means the gfn is writable on host. - SPTE_HOST_WRITEABLE means the gfn is writable on host.
- SPTE_MMU_WRITEABLE means the gfn is writable on mmu. The bit is set when - SPTE_MMU_WRITEABLE means the gfn is writable on mmu. The bit is set when
the gfn is writable on guest mmu and it is not write-protected by shadow the gfn is writable on guest mmu and it is not write-protected by shadow
page write-protection. page write-protection.
On fast page fault path, we will use cmpxchg to atomically set the spte W On fast page fault path, we will use cmpxchg to atomically set the spte W
bit if spte.SPTE_HOST_WRITEABLE = 1 and spte.SPTE_WRITE_PROTECT = 1, or bit if spte.SPTE_HOST_WRITEABLE = 1 and spte.SPTE_WRITE_PROTECT = 1, or
restore the saved R/X bits if VMX_EPT_TRACK_ACCESS mask is set, or both. This restore the saved R/X bits if VMX_EPT_TRACK_ACCESS mask is set, or both. This
is safe because whenever changing these bits can be detected by cmpxchg. is safe because whenever changing these bits can be detected by cmpxchg.
But we need carefully check these cases: But we need carefully check these cases:
1): The mapping from gfn to pfn
1) The mapping from gfn to pfn
The mapping from gfn to pfn may be changed since we can only ensure the pfn The mapping from gfn to pfn may be changed since we can only ensure the pfn
is not changed during cmpxchg. This is a ABA problem, for example, below case is not changed during cmpxchg. This is a ABA problem, for example, below case
will happen: will happen:
At the beginning: +------------------------------------------------------------------------+
gpte = gfn1 | At the beginning:: |
gfn1 is mapped to pfn1 on host | |
spte is the shadow page table entry corresponding with gpte and | gpte = gfn1 |
spte = pfn1 | gfn1 is mapped to pfn1 on host |
| spte is the shadow page table entry corresponding with gpte and |
VCPU 0 VCPU0 | spte = pfn1 |
on fast page fault path: +------------------------------------------------------------------------+
| On fast page fault path: |
old_spte = *spte; +------------------------------------+-----------------------------------+
pfn1 is swapped out: | CPU 0: | CPU 1: |
spte = 0; +------------------------------------+-----------------------------------+
| :: | |
pfn1 is re-alloced for gfn2. | | |
| old_spte = *spte; | |
gpte is changed to point to +------------------------------------+-----------------------------------+
gfn2 by the guest: | | pfn1 is swapped out:: |
spte = pfn1; | | |
| | spte = 0; |
if (cmpxchg(spte, old_spte, old_spte+W) | | |
mark_page_dirty(vcpu->kvm, gfn1) | | pfn1 is re-alloced for gfn2. |
OOPS!!! | | |
| | gpte is changed to point to |
| | gfn2 by the guest:: |
| | |
| | spte = pfn1; |
+------------------------------------+-----------------------------------+
| :: |
| |
| if (cmpxchg(spte, old_spte, old_spte+W) |
| mark_page_dirty(vcpu->kvm, gfn1) |
| OOPS!!! |
+------------------------------------------------------------------------+
We dirty-log for gfn1, that means gfn2 is lost in dirty-bitmap. We dirty-log for gfn1, that means gfn2 is lost in dirty-bitmap.
For direct sp, we can easily avoid it since the spte of direct sp is fixed For direct sp, we can easily avoid it since the spte of direct sp is fixed
to gfn. For indirect sp, before we do cmpxchg, we call gfn_to_pfn_atomic() to gfn. For indirect sp, before we do cmpxchg, we call gfn_to_pfn_atomic()
to pin gfn to pfn, because after gfn_to_pfn_atomic(): to pin gfn to pfn, because after gfn_to_pfn_atomic():
- We have held the refcount of pfn that means the pfn can not be freed and - We have held the refcount of pfn that means the pfn can not be freed and
be reused for another gfn. be reused for another gfn.
- The pfn is writable that means it can not be shared between different gfns - The pfn is writable that means it can not be shared between different gfns
...@@ -91,7 +109,8 @@ Then, we can ensure the dirty bitmaps is correctly set for a gfn. ...@@ -91,7 +109,8 @@ Then, we can ensure the dirty bitmaps is correctly set for a gfn.
Currently, to simplify the whole things, we disable fast page fault for Currently, to simplify the whole things, we disable fast page fault for
indirect shadow page. indirect shadow page.
2): Dirty bit tracking 2) Dirty bit tracking
In the origin code, the spte can be fast updated (non-atomically) if the In the origin code, the spte can be fast updated (non-atomically) if the
spte is read-only and the Accessed bit has already been set since the spte is read-only and the Accessed bit has already been set since the
Accessed bit and Dirty bit can not be lost. Accessed bit and Dirty bit can not be lost.
...@@ -99,34 +118,42 @@ Accessed bit and Dirty bit can not be lost. ...@@ -99,34 +118,42 @@ Accessed bit and Dirty bit can not be lost.
But it is not true after fast page fault since the spte can be marked But it is not true after fast page fault since the spte can be marked
writable between reading spte and updating spte. Like below case: writable between reading spte and updating spte. Like below case:
At the beginning: +------------------------------------------------------------------------+
spte.W = 0 | At the beginning:: |
spte.Accessed = 1 | |
| spte.W = 0 |
VCPU 0 VCPU0 | spte.Accessed = 1 |
In mmu_spte_clear_track_bits(): +------------------------------------+-----------------------------------+
| CPU 0: | CPU 1: |
old_spte = *spte; +------------------------------------+-----------------------------------+
| In mmu_spte_clear_track_bits():: | |
/* 'if' condition is satisfied. */ | | |
if (old_spte.Accessed == 1 && | old_spte = *spte; | |
old_spte.W == 0) | | |
spte = 0ull; | | |
on fast page fault path: | /* 'if' condition is satisfied. */| |
spte.W = 1 | if (old_spte.Accessed == 1 && | |
memory write on the spte: | old_spte.W == 0) | |
spte.Dirty = 1 | spte = 0ull; | |
+------------------------------------+-----------------------------------+
| | on fast page fault path:: |
else | | |
old_spte = xchg(spte, 0ull) | | spte.W = 1 |
| | |
| | memory write on the spte:: |
if (old_spte.Accessed == 1) | | |
kvm_set_pfn_accessed(spte.pfn); | | spte.Dirty = 1 |
if (old_spte.Dirty == 1) +------------------------------------+-----------------------------------+
kvm_set_pfn_dirty(spte.pfn); | :: | |
OOPS!!! | | |
| else | |
| old_spte = xchg(spte, 0ull) | |
| if (old_spte.Accessed == 1) | |
| kvm_set_pfn_accessed(spte.pfn);| |
| if (old_spte.Dirty == 1) | |
| kvm_set_pfn_dirty(spte.pfn); | |
| OOPS!!! | |
+------------------------------------+-----------------------------------+
The Dirty bit is lost in this case. The Dirty bit is lost in this case.
...@@ -134,7 +161,8 @@ In order to avoid this kind of issue, we always treat the spte as "volatile" ...@@ -134,7 +161,8 @@ In order to avoid this kind of issue, we always treat the spte as "volatile"
if it can be updated out of mmu-lock, see spte_has_volatile_bits(), it means, if it can be updated out of mmu-lock, see spte_has_volatile_bits(), it means,
the spte is always atomically updated in this case. the spte is always atomically updated in this case.
3): flush tlbs due to spte updated 3) flush tlbs due to spte updated
If the spte is updated from writable to readonly, we should flush all TLBs, If the spte is updated from writable to readonly, we should flush all TLBs,
otherwise rmap_write_protect will find a read-only spte, even though the otherwise rmap_write_protect will find a read-only spte, even though the
writable spte might be cached on a CPU's TLB. writable spte might be cached on a CPU's TLB.
...@@ -160,53 +188,53 @@ a fault is generated and the fast page fault mechanism described above is used ...@@ -160,53 +188,53 @@ a fault is generated and the fast page fault mechanism described above is used
to atomically restore the PTE to a Present state. The W bit is not saved when to atomically restore the PTE to a Present state. The W bit is not saved when
the PTE is marked for access tracking and during restoration to the Present the PTE is marked for access tracking and during restoration to the Present
state, the W bit is set depending on whether or not it was a write access. If state, the W bit is set depending on whether or not it was a write access. If
it wasn't, then the W bit will remain clear until a write access happens, at it wasn't, then the W bit will remain clear until a write access happens, at
which time it will be set using the Dirty tracking mechanism described above. which time it will be set using the Dirty tracking mechanism described above.
3. Reference 3. Reference
------------ ------------
Name: kvm_lock :Name: kvm_lock
Type: mutex :Type: mutex
Arch: any :Arch: any
Protects: - vm_list :Protects: - vm_list
Name: kvm_count_lock :Name: kvm_count_lock
Type: raw_spinlock_t :Type: raw_spinlock_t
Arch: any :Arch: any
Protects: - hardware virtualization enable/disable :Protects: - hardware virtualization enable/disable
Comment: 'raw' because hardware enabling/disabling must be atomic /wrt :Comment: 'raw' because hardware enabling/disabling must be atomic /wrt
migration. migration.
Name: kvm_arch::tsc_write_lock :Name: kvm_arch::tsc_write_lock
Type: raw_spinlock :Type: raw_spinlock
Arch: x86 :Arch: x86
Protects: - kvm_arch::{last_tsc_write,last_tsc_nsec,last_tsc_offset} :Protects: - kvm_arch::{last_tsc_write,last_tsc_nsec,last_tsc_offset}
- tsc offset in vmcb - tsc offset in vmcb
Comment: 'raw' because updating the tsc offsets must not be preempted. :Comment: 'raw' because updating the tsc offsets must not be preempted.
Name: kvm->mmu_lock :Name: kvm->mmu_lock
Type: spinlock_t :Type: spinlock_t
Arch: any :Arch: any
Protects: -shadow page/shadow tlb entry :Protects: -shadow page/shadow tlb entry
Comment: it is a spinlock since it is used in mmu notifier. :Comment: it is a spinlock since it is used in mmu notifier.
Name: kvm->srcu :Name: kvm->srcu
Type: srcu lock :Type: srcu lock
Arch: any :Arch: any
Protects: - kvm->memslots :Protects: - kvm->memslots
- kvm->buses - kvm->buses
Comment: The srcu read lock must be held while accessing memslots (e.g. :Comment: The srcu read lock must be held while accessing memslots (e.g.
when using gfn_to_* functions) and while accessing in-kernel when using gfn_to_* functions) and while accessing in-kernel
MMIO/PIO address->device structure mapping (kvm->buses). MMIO/PIO address->device structure mapping (kvm->buses).
The srcu index can be stored in kvm_vcpu->srcu_idx per vcpu The srcu index can be stored in kvm_vcpu->srcu_idx per vcpu
if it is needed by multiple functions. if it is needed by multiple functions.
Name: blocked_vcpu_on_cpu_lock :Name: blocked_vcpu_on_cpu_lock
Type: spinlock_t :Type: spinlock_t
Arch: x86 :Arch: x86
Protects: blocked_vcpu_on_cpu :Protects: blocked_vcpu_on_cpu
Comment: This is a per-CPU lock and it is used for VT-d posted-interrupts. :Comment: This is a per-CPU lock and it is used for VT-d posted-interrupts.
When VT-d posted-interrupts is supported and the VM has assigned When VT-d posted-interrupts is supported and the VM has assigned
devices, we put the blocked vCPU on the list blocked_vcpu_on_cpu devices, we put the blocked vCPU on the list blocked_vcpu_on_cpu
protected by blocked_vcpu_on_cpu_lock, when VT-d hardware issues protected by blocked_vcpu_on_cpu_lock, when VT-d hardware issues
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment