Commit b3e4e63f authored by Marcelo Tosatti's avatar Marcelo Tosatti Committed by Avi Kivity

KVM: MMU: Use cmpxchg for pte updates on walk_addr()

In preparation for multi-threaded guest pte walking, use cmpxchg()
when updating guest pte's. This guarantees that the assignment of the
dirty bit can't be lost if two CPU's are faulting the same address
simultaneously.

[avi: fix kunmap_atomic() parameters]
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: default avatarAvi Kivity <avi@qumranet.com>
parent 80a8119c
...@@ -34,7 +34,9 @@ ...@@ -34,7 +34,9 @@
#define PT_LEVEL_BITS PT64_LEVEL_BITS #define PT_LEVEL_BITS PT64_LEVEL_BITS
#ifdef CONFIG_X86_64 #ifdef CONFIG_X86_64
#define PT_MAX_FULL_LEVELS 4 #define PT_MAX_FULL_LEVELS 4
#define CMPXCHG cmpxchg
#else #else
#define CMPXCHG cmpxchg64
#define PT_MAX_FULL_LEVELS 2 #define PT_MAX_FULL_LEVELS 2
#endif #endif
#elif PTTYPE == 32 #elif PTTYPE == 32
...@@ -48,6 +50,7 @@ ...@@ -48,6 +50,7 @@
#define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level) #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
#define PT_LEVEL_BITS PT32_LEVEL_BITS #define PT_LEVEL_BITS PT32_LEVEL_BITS
#define PT_MAX_FULL_LEVELS 2 #define PT_MAX_FULL_LEVELS 2
#define CMPXCHG cmpxchg
#else #else
#error Invalid PTTYPE value #error Invalid PTTYPE value
#endif #endif
...@@ -78,6 +81,26 @@ static gfn_t gpte_to_gfn_pde(pt_element_t gpte) ...@@ -78,6 +81,26 @@ static gfn_t gpte_to_gfn_pde(pt_element_t gpte)
return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT; return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
} }
static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
gfn_t table_gfn, unsigned index,
pt_element_t orig_pte, pt_element_t new_pte)
{
pt_element_t ret;
pt_element_t *table;
struct page *page;
page = gfn_to_page(kvm, table_gfn);
table = kmap_atomic(page, KM_USER0);
ret = CMPXCHG(&table[index], orig_pte, new_pte);
kunmap_atomic(table, KM_USER0);
kvm_release_page_dirty(page);
return (ret != orig_pte);
}
/* /*
* Fetch a guest pte for a guest virtual address * Fetch a guest pte for a guest virtual address
*/ */
...@@ -91,6 +114,7 @@ static int FNAME(walk_addr)(struct guest_walker *walker, ...@@ -91,6 +114,7 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
gpa_t pte_gpa; gpa_t pte_gpa;
pgprintk("%s: addr %lx\n", __FUNCTION__, addr); pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
walk:
walker->level = vcpu->mmu.root_level; walker->level = vcpu->mmu.root_level;
pte = vcpu->cr3; pte = vcpu->cr3;
#if PTTYPE == 64 #if PTTYPE == 64
...@@ -135,8 +159,10 @@ static int FNAME(walk_addr)(struct guest_walker *walker, ...@@ -135,8 +159,10 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
if (!(pte & PT_ACCESSED_MASK)) { if (!(pte & PT_ACCESSED_MASK)) {
mark_page_dirty(vcpu->kvm, table_gfn); mark_page_dirty(vcpu->kvm, table_gfn);
if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
index, pte, pte|PT_ACCESSED_MASK))
goto walk;
pte |= PT_ACCESSED_MASK; pte |= PT_ACCESSED_MASK;
kvm_write_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
} }
if (walker->level == PT_PAGE_TABLE_LEVEL) { if (walker->level == PT_PAGE_TABLE_LEVEL) {
...@@ -159,9 +185,14 @@ static int FNAME(walk_addr)(struct guest_walker *walker, ...@@ -159,9 +185,14 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
} }
if (write_fault && !is_dirty_pte(pte)) { if (write_fault && !is_dirty_pte(pte)) {
bool ret;
mark_page_dirty(vcpu->kvm, table_gfn); mark_page_dirty(vcpu->kvm, table_gfn);
ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
pte|PT_DIRTY_MASK);
if (ret)
goto walk;
pte |= PT_DIRTY_MASK; pte |= PT_DIRTY_MASK;
kvm_write_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte)); kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte));
} }
...@@ -484,3 +515,4 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu, ...@@ -484,3 +515,4 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
#undef PT_MAX_FULL_LEVELS #undef PT_MAX_FULL_LEVELS
#undef gpte_to_gfn #undef gpte_to_gfn
#undef gpte_to_gfn_pde #undef gpte_to_gfn_pde
#undef CMPXCHG
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