Optimize page table walk
v3 cover Optimize page table walk
The page table walk has gotten crufty over the years and is threatening to become even more crufty when SMAP is introduced. Clean it up (and optimize it) somewhat.
v3:
fix another boolflip pointed out by Xiao Guangrong some cosmetics
v2:
fix SMEP false positive by moving checks to the end of the walk fix last_pte_bitmap documentation
fix incorrect SMEP fault permission checks
introduce helper for accessing the permission bitmap
Avi Kivity (10):
KVM: MMU: Push clean gpte write protection out of gpte_access()
KVM: MMU: Optimize gpte_access() slightly
KVM: MMU: Move gpte_access() out of paging_tmpl.h
KVM: MMU: Update accessed and dirty bits after guest pagetable walk
KVM: MMU: Optimize pte permission checks
KVM: MMU: Simplify walk_addr_generic() loop
KVM: MMU: Optimize is_last_gpte()
KVM: MMU: Eliminate eperm temporary
KVM: MMU: Avoid access/dirty update loop if all is well
KVM: MMU: Eliminate pointless temporary 'ac'
arch/x86/include/asm/kvm_host.h | 14 +++ arch/x86/kvm/mmu.c | 91 +++++++++++++++++++ arch/x86/kvm/mmu.h | 25 +++--- arch/x86/kvm/paging_tmpl.h | 191 +++++++++++++++++----------------------- arch/x86/kvm/x86.c | 11 +-- 5 files changed, 202 insertions(+), 130 deletions(-)
Signature · 1 line
1.7.12
PATCH v3 1/10 KVM: MMU: Push clean gpte write protection out of gpte_access()
gpte_access() computes the access permissions of a guest pte and also write-protects clean gptes. This is wrong when we are servicing a write fault (since we'll be setting the dirty bit momentarily) but correct when instantiating a speculative spte, or when servicing a read fault (since we'll want to trap a following write in order to set the dirty bit).
It doesn't seem to hurt in practice, but in order to make the code readable, push the write protection out of gpte_access() and into a new protect_clean_gpte() which is called explicitly when needed.
--- arch/x86/kvm/mmu.c | 12 ++++++++++++ arch/x86/kvm/mmu.h | 3 ++- arch/x86/kvm/paging_tmpl.h | 24 ++++++++++++------------ 3 files changed, 26 insertions(+), 13 deletions(-)
arch/x86/kvm/mmu.c +12 -0
@@ -3408,6 +3408,18 @@ static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)return (gpte & mmu->rsvd_bits_mask[bit7][level-1 ]) !=0 ; } +static inline void protect_clean_gpte (unsigned *access,unsigned gpte) +{ +unsigned mask; + +BUILD_BUG_ON (PT_WRITABLE_MASK !=ACC_WRITE_MASK ); + + mask = (unsigned )~ACC_WRITE_MASK ; +/* Allow write access to dirty gptes */ + mask |= (gpte >> (PT_DIRTY_SHIFT -PT_WRITABLE_SHIFT )) &PT_WRITABLE_MASK ; + *access &= mask; +} +static bool sync_mmio_spte (u64 *sptep,gfn_t gfn,unsigned access,int *nr_present) {
arch/x86/kvm/mmu.h +2 -1
@@ -18,7 +18,8 @@#define PT_PCD_MASK (1ULL << 4)#define PT_ACCESSED_SHIFT 5#define PT_ACCESSED_MASK (1ULL << PT_ACCESSED_SHIFT) -#define PT_DIRTY_MASK (1ULL << 6) +#define PT_DIRTY_SHIFT 6 +#define PT_DIRTY_MASK (1ULL << PT_DIRTY_SHIFT)#define PT_PAGE_SIZE_MASK (1ULL << 7)#define PT_PAT_MASK (1ULL << 7)#define PT_GLOBAL_MASK (1ULL << 8)
arch/x86/kvm/paging_tmpl.h +12 -12
@@ -101,14 +101,11 @@ static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,return (ret != orig_pte); } -static unsigned FNAME (gpte_access )(struct kvm_vcpu *vcpu,pt_element_t gpte, -bool last) +static unsigned FNAME (gpte_access )(struct kvm_vcpu *vcpu,pt_element_t gpte) {unsigned access; access = (gpte & (PT_WRITABLE_MASK |PT_USER_MASK )) |ACC_EXEC_MASK ; -if (last && !is_dirty_gpte (gpte)) - access &= ~ACC_WRITE_MASK ;#if PTTYPE ==64 if (vcpu->arch.mmu.nx)
@@ -222,8 +219,7 @@ retry_walk: last_gpte =FNAME (is_last_gpte)(walker, vcpu, mmu, pte);if (last_gpte) { - pte_access = pt_access & -FNAME (gpte_access)(vcpu, pte, true); + pte_access = pt_access &FNAME (gpte_access)(vcpu, pte);/* check if the kernel is fetching from user page */ if (unlikely (pte_access &PT_USER_MASK ) &&kvm_read_cr4_bits (vcpu,X86_CR4_SMEP ))
@@ -274,7 +270,7 @@ retry_walk:break ; } - pt_access &=FNAME (gpte_access)(vcpu, pte, false); + pt_access &=FNAME (gpte_access)(vcpu, pte); --walker->level; }
@@ -283,7 +279,9 @@ retry_walk: goto error; } -if (write_fault &&unlikely (!is_dirty_gpte (pte))) { +if (!write_fault) +protect_clean_gpte (&pte_access, pte); +else if (unlikely (!is_dirty_gpte (pte))) {int ret;trace_kvm_mmu_set_dirty_bit (table_gfn, index,sizeof (pte));
@@ -368,7 +366,8 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,return ;pgprintk ("%s: gpte %llx spte %p\n" , __func__, (u64 )gpte, spte); - pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte, true); + pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte); +protect_clean_gpte (&pte_access, gpte); pfn =gfn_to_pfn_atomic (vcpu->kvm,gpte_to_gfn (gpte));if (mmu_invalid_pfn (pfn))return ;
@@ -441,8 +440,8 @@ static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,if (FNAME (prefetch_invalid_gpte)(vcpu, sp, spte, gpte))continue ; - pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte, - true); + pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte); +protect_clean_gpte (&pte_access, gpte); gfn =gpte_to_gfn (gpte); pfn =pte_prefetch_gfn_to_pfn (vcpu, gfn, pte_access &ACC_WRITE_MASK );
@@ -794,7 +793,8 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) gfn =gpte_to_gfn (gpte); pte_access = sp->role.access; - pte_access &=FNAME (gpte_access)(vcpu, gpte, true); + pte_access &=FNAME (gpte_access)(vcpu, gpte); +protect_clean_gpte (&pte_access, gpte);if (sync_mmio_spte (&sp->spt[i], gfn, pte_access, &nr_present))continue ;
Signature · 1 line
1.7.12
PATCH v3 2/10 KVM: MMU: Optimize gpte_access() slightly
In reply to Avi KivityIf nx is disabled, then is gpte[63] is set we will hit a reserved bit set fault before checking permissions; so we can ignore the setting of efer.nxe.
--- arch/x86/kvm/paging_tmpl.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/kvm/paging_tmpl.h +1 -3
@@ -106,10 +106,8 @@ static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)unsigned access; access = (gpte & (PT_WRITABLE_MASK |PT_USER_MASK )) |ACC_EXEC_MASK ; -#if PTTYPE ==64 -if (vcpu->arch.mmu.nx) - access &= ~(gpte >>PT64_NX_SHIFT ); + access &= ~(gpte >>PT64_NX_SHIFT );#endif return access; }
Signature · 1 line
1.7.12
PATCH v3 3/10 KVM: MMU: Move gpte_access() out of paging_tmpl.h
In reply to Avi KivityWe no longer rely on paging_tmpl.h defines; so we can move the function to mmu.c.
Rely on zero extension to 64 bits to get the correct nx behaviour.
--- arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/paging_tmpl.h | 21 +++++---------------- 2 files changed, 15 insertions(+), 16 deletions(-)
arch/x86/kvm/mmu.c +10 -0
@@ -3437,6 +3437,16 @@ static bool sync_mmio_spte(u64 *sptep, gfn_t gfn, unsigned access,return false; } +static inline unsigned gpte_access (struct kvm_vcpu *vcpu,u64 gpte) +{ +unsigned access; + + access = (gpte & (PT_WRITABLE_MASK |PT_USER_MASK )) |ACC_EXEC_MASK ; + access &= ~(gpte >>PT64_NX_SHIFT ); + +return access; +} +#define PTTYPE 64#include "paging_tmpl.h" #undef PTTYPE
arch/x86/kvm/paging_tmpl.h +5 -16
@@ -101,17 +101,6 @@ static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,return (ret != orig_pte); } -static unsigned FNAME (gpte_access )(struct kvm_vcpu *vcpu,pt_element_t gpte) -{ -unsigned access; - - access = (gpte & (PT_WRITABLE_MASK |PT_USER_MASK )) |ACC_EXEC_MASK ; -#if PTTYPE ==64 - access &= ~(gpte >>PT64_NX_SHIFT ); -#endif -return access; -} -static bool FNAME (is_last_gpte )(struct guest_walker *walker,struct kvm_vcpu *vcpu,struct kvm_mmu *mmu,pt_element_t gpte)
@@ -217,7 +206,7 @@ retry_walk: last_gpte =FNAME (is_last_gpte)(walker, vcpu, mmu, pte);if (last_gpte) { - pte_access = pt_access &FNAME (gpte_access)(vcpu, pte); + pte_access = pt_access &gpte_access (vcpu, pte);/* check if the kernel is fetching from user page */ if (unlikely (pte_access &PT_USER_MASK ) &&kvm_read_cr4_bits (vcpu,X86_CR4_SMEP ))
@@ -268,7 +257,7 @@ retry_walk:break ; } - pt_access &=FNAME (gpte_access)(vcpu, pte); + pt_access &=gpte_access (vcpu, pte); --walker->level; }
@@ -364,7 +353,7 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,return ;pgprintk ("%s: gpte %llx spte %p\n" , __func__, (u64 )gpte, spte); - pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte); + pte_access = sp->role.access &gpte_access (vcpu, gpte);protect_clean_gpte (&pte_access, gpte); pfn =gfn_to_pfn_atomic (vcpu->kvm,gpte_to_gfn (gpte));if (mmu_invalid_pfn (pfn))
@@ -438,7 +427,7 @@ static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,if (FNAME (prefetch_invalid_gpte)(vcpu, sp, spte, gpte))continue ; - pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte); + pte_access = sp->role.access &gpte_access (vcpu, gpte);protect_clean_gpte (&pte_access, gpte); gfn =gpte_to_gfn (gpte); pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
@@ -791,7 +780,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) gfn =gpte_to_gfn (gpte); pte_access = sp->role.access; - pte_access &=FNAME (gpte_access)(vcpu, gpte); + pte_access &=gpte_access (vcpu, gpte);protect_clean_gpte (&pte_access, gpte);if (sync_mmio_spte (&sp->spt[i], gfn, pte_access, &nr_present))
Signature · 1 line
1.7.12
PATCH v3 4/10 KVM: MMU: Update accessed and dirty bits after guest pagetable walk
In reply to Avi KivityWhile unspecified, the behaviour of Intel processors is to first perform the page table walk, then, if the walk was successful, to atomically update the accessed and dirty bits of walked paging elements.
While we are not required to follow this exactly, doing so will allow us to perform the access permissions check after the walk is complete, rather than after each walk step.
(the tricky case is SMEP: a zero in any pte's U bit makes the referenced page a supervisor page, so we can't fault on a one bit during the walk itself).
--- arch/x86/kvm/paging_tmpl.h | 76 ++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 29 deletions(-)
arch/x86/kvm/paging_tmpl.h +47 -29
@@ -63,10 +63,12 @@*/ struct guest_walker {int level; +unsigned max_level;gfn_t table_gfn[PT_MAX_FULL_LEVELS ];pt_element_t ptes[PT_MAX_FULL_LEVELS ];pt_element_t prefetch_ptes[PTE_PREFETCH_NUM ];gpa_t pte_gpa[PT_MAX_FULL_LEVELS ]; +pt_element_t __user *ptep_user[PT_MAX_FULL_LEVELS ];unsigned pt_access;unsigned pte_access;gfn_t gfn;
@@ -119,6 +121,43 @@ static bool FNAME(is_last_gpte)(struct guest_walker *walker,return false; } +static int FNAME (update_accessed_dirty_bits )(struct kvm_vcpu *vcpu, +struct kvm_mmu *mmu, +struct guest_walker *walker, +int write_fault) +{ +unsigned level, index; +pt_element_t pte, orig_pte; +pt_element_t __user *ptep_user; +gfn_t table_gfn; +int ret; + +for (level = walker->max_level; level >= walker->level; --level) { + pte = orig_pte = walker->ptes[level -1 ]; + table_gfn = walker->table_gfn[level -1 ]; + ptep_user = walker->ptep_user[level -1 ]; + index =offset_in_page (ptep_user) /sizeof (pt_element_t); +if (!(pte &PT_ACCESSED_MASK )) { +trace_kvm_mmu_set_accessed_bit (table_gfn, index,sizeof (pte)); + pte |=PT_ACCESSED_MASK ; + } +if (level == walker->level && write_fault && !is_dirty_gpte (pte)) { +trace_kvm_mmu_set_dirty_bit (table_gfn, index,sizeof (pte)); + pte |=PT_DIRTY_MASK ; + } +if (pte == orig_pte) +continue ; + + ret =FNAME (cmpxchg_gpte)(vcpu, mmu, ptep_user, index, orig_pte, pte); +if (ret) +return ret; + +mark_page_dirty (vcpu->kvm, table_gfn); + walker->ptes[level] = pte; + } +return 0 ; +} +/* * Fetch a guest pte for a guest virtual address */
@@ -126,6 +165,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, gva_t addr, u32 access) { + int ret;pt_element_t pte;pt_element_t __user *uninitialized_var (ptep_user );gfn_t table_gfn;
@@ -153,6 +193,7 @@ retry_walk: --walker->level; }#endif + walker->max_level = walker->level;ASSERT ((!is_long_mode (vcpu) &&is_pae (vcpu)) || (mmu->get_cr3 (vcpu) &CR3_NONPAE_RESERVED_BITS ) ==0 );
@@ -183,6 +224,7 @@ retry_walk: ptep_user = (pt_element_t __user *)((void *)host_addr + offset);if (unlikely (__copy_from_user (&pte, ptep_user,sizeof (pte)))) goto error; + walker->ptep_user[walker->level -1 ] = ptep_user;trace_kvm_mmu_paging_element (pte, walker->level);
@@ -214,21 +256,6 @@ retry_walk: eperm = true; } -if (!eperm &&unlikely (!(pte &PT_ACCESSED_MASK ))) { -int ret; -trace_kvm_mmu_set_accessed_bit (table_gfn, index, -sizeof (pte)); - ret =FNAME (cmpxchg_gpte)(vcpu, mmu, ptep_user, index, - pte, pte|PT_ACCESSED_MASK ); -if (unlikely (ret <0 )) - goto error; -else if (ret) - goto retry_walk; - -mark_page_dirty (vcpu->kvm, table_gfn); - pte |=PT_ACCESSED_MASK ; - } - walker->ptes[walker->level -1 ] = pte;if (last_gpte) {
@@ -268,21 +295,12 @@ retry_walk:if (!write_fault)protect_clean_gpte (&pte_access, pte); -else if (unlikely (!is_dirty_gpte (pte))) { -int ret; -trace_kvm_mmu_set_dirty_bit (table_gfn, index,sizeof (pte)); - ret =FNAME (cmpxchg_gpte)(vcpu, mmu, ptep_user, index, - pte, pte|PT_DIRTY_MASK ); -if (unlikely (ret <0 )) - goto error; -else if (ret) - goto retry_walk; - -mark_page_dirty (vcpu->kvm, table_gfn); - pte |=PT_DIRTY_MASK ; - walker->ptes[walker->level -1 ] = pte; - } + ret =FNAME (update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault); +if (unlikely (ret <0 )) + goto error; +else if (ret) + goto retry_walk; walker->pt_access = pt_access; walker->pte_access = pte_access;
Signature · 1 line
1.7.12
PATCH v3 5/10 KVM: MMU: Optimize pte permission checks
In reply to Avi Kivitywalk_addr_generic() permission checks are a maze of branchy code, which is performed four times per lookup. It depends on the type of access, efer.nxe, cr0.wp, cr4.smep, and in the near future, cr4.smap.
Optimize this away by precalculating all variants and storing them in a bitmap. The bitmap is recalculated when rarely-changing variables change (cr0, cr4) and is indexed by the often-changing variables (page fault error code, pte access permissions).
The permission check is moved to the end of the loop, otherwise an SMEP fault could be reported as a false positive, when PDE.U=1 but PTE.U=0. Noted by Xiao Guangrong.
The result is short, branch-free code.
--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu.c | 38 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu.h | 19 ++++++++----------- arch/x86/kvm/paging_tmpl.h | 22 ++++------------------ arch/x86/kvm/x86.c | 11 ++++------- 5 files changed, 61 insertions(+), 36 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -0
@@ -287,6 +287,13 @@ struct kvm_mmu {union kvm_mmu_page_role base_role;bool direct_map; +/* +* Bitmap; bit set = permission fault +* Byte index: page fault error code [4:1] +* Bit index: pte permissions in ACC_* format +*/ +u8 permissions[16 ]; +u64 *pae_root;u64 *lm_root;u64 rsvd_bits_mask[2 ][4 ];
arch/x86/kvm/mmu.c +38 -0
@@ -3516,6 +3516,38 @@ static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, } } +static void update_permission_bitmask (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu) +{ +unsigned bit, byte, pfec; +u8 map; +bool fault, x, w, u, wf, uf, ff, smep; + + smep =kvm_read_cr4_bits (vcpu,X86_CR4_SMEP ); +for (byte =0 ; byte <ARRAY_SIZE (mmu->permissions); ++byte) { + pfec = byte <<1 ; + map =0 ; + wf = pfec &PFERR_WRITE_MASK ; + uf = pfec &PFERR_USER_MASK ; + ff = pfec &PFERR_FETCH_MASK ; +for (bit =0 ; bit <8 ; ++bit) { + x = bit &ACC_EXEC_MASK ; + w = bit &ACC_WRITE_MASK ; + u = bit &ACC_USER_MASK ; + +/* Not really needed: !nx will cause pte.nx to fault */ + x |= !mmu->nx; +/* Allow supervisor writes if !cr0.wp */ + w |= !is_write_protection (vcpu) && !uf; +/* Disallow supervisor fetches of user code if cr4.smep */ + x &= !(smep && u && !uf); + + fault = (ff && !x) || (uf && !u) || (wf && !w); + map |= fault << bit; + } + mmu->permissions[byte] = map; + } +} +static int paging64_init_context_common (struct kvm_vcpu *vcpu,struct kvm_mmu *context,int level)
@@ -3524,6 +3556,7 @@ static int paging64_init_context_common(struct kvm_vcpu *vcpu, context->root_level = level;reset_rsvds_bits_mask (vcpu, context); +update_permission_bitmask (vcpu, context);ASSERT (is_pae (vcpu)); context->new_cr3 = paging_new_cr3;
@@ -3552,6 +3585,7 @@ static int paging32_init_context(struct kvm_vcpu *vcpu, context->root_level =PT32_ROOT_LEVEL ;reset_rsvds_bits_mask (vcpu, context); +update_permission_bitmask (vcpu, context); context->new_cr3 = paging_new_cr3; context->page_fault = paging32_page_fault;
@@ -3612,6 +3646,8 @@ static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) context->gva_to_gpa = paging32_gva_to_gpa; } +update_permission_bitmask (vcpu, context); +return 0 ; }
@@ -3687,6 +3723,8 @@ static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu) g_context->gva_to_gpa = paging32_gva_to_gpa_nested; } +update_permission_bitmask (vcpu, g_context); +return 0 ; }
arch/x86/kvm/mmu.h +8 -11
@@ -89,17 +89,14 @@ static inline bool is_write_protection(struct kvm_vcpu *vcpu) return kvm_read_cr0_bits(vcpu, X86_CR0_WP); } -static inline bool check_write_user_access (struct kvm_vcpu *vcpu, -bool write_fault,bool user_fault, -unsigned long pte) +/* +* Will a fault with a given page-fault error code (pfec) cause a permission +* fault with the given access (in ACC_* format)? +*/ +static inline bool permission_fault (struct kvm_mmu *mmu,unsigned pte_access, +unsigned pfec) { -if (unlikely (write_fault && !is_writable_pte (pte) - && (user_fault ||is_write_protection (vcpu)))) -return false; - -if (unlikely (user_fault && !(pte &PT_USER_MASK ))) -return false; - -return true; +return (mmu->permissions[pfec >>1 ] >> pte_access) &1 ; } +#endif
arch/x86/kvm/paging_tmpl.h +4 -18
@@ -169,7 +169,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,pt_element_t pte;pt_element_t __user *uninitialized_var (ptep_user );gfn_t table_gfn; -unsigned index, pt_access,uninitialized_var (pte_access ); +unsigned index, pt_access, pte_access;gpa_t pte_gpa;bool eperm, last_gpte;int offset;
@@ -237,24 +237,9 @@ retry_walk: goto error; } -if (!check_write_user_access (vcpu, write_fault, user_fault, - pte)) - eperm = true; - -#if PTTYPE ==64 -if (unlikely (fetch_fault && (pte &PT64_NX_MASK ))) - eperm = true; -#endif + pte_access = pt_access &gpte_access (vcpu, pte); last_gpte =FNAME (is_last_gpte)(walker, vcpu, mmu, pte); -if (last_gpte) { - pte_access = pt_access &gpte_access (vcpu, pte); -/* check if the kernel is fetching from user page */ -if (unlikely (pte_access &PT_USER_MASK ) && -kvm_read_cr4_bits (vcpu,X86_CR4_SMEP )) -if (fetch_fault && !user_fault) - eperm = true; - } walker->ptes[walker->level -1 ] = pte;
@@ -284,10 +269,11 @@ retry_walk:break ; } - pt_access &=gpte_access (vcpu, pte); + pt_access &= pte_access; --walker->level; } + eperm |=permission_fault (mmu, pte_access, access);if (unlikely (eperm)) { errcode |=PFERR_PRESENT_MASK ; goto error;
arch/x86/kvm/x86.c +4 -7
@@ -3672,20 +3672,17 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, gpa_t *gpa, struct x86_exception *exception, bool write) { - u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; + u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0) + | (write ?PFERR_WRITE_MASK :0 ); -if (vcpu_match_mmio_gva (vcpu, gva) && -check_write_user_access (vcpu, write, access, - vcpu->arch.access)) { +if (vcpu_match_mmio_gva (vcpu, gva) + && !permission_fault (vcpu->arch.walk_mmu, vcpu->arch.access, access)) { *gpa = vcpu->arch.mmio_gfn <<PAGE_SHIFT | (gva & (PAGE_SIZE -1 ));trace_vcpu_match_mmio (gva, *gpa, write, false);return 1 ; } -if (write) - access |=PFERR_WRITE_MASK ; - *gpa = vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, exception);if (*gpa ==UNMAPPED_GVA )
Signature · 1 line
1.7.12
PATCH v3 6/10 KVM: MMU: Simplify walk_addr_generic() loop
In reply to Avi KivityThe page table walk is coded as an infinite loop, with a special case on the last pte.
Code it as an ordinary loop with a termination condition on the last pte (large page or walk length exhausted), and put the last pte handling code after the loop where it belongs.
--- arch/x86/kvm/paging_tmpl.h | 60 +++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 35 deletions(-)
arch/x86/kvm/paging_tmpl.h +25 -35
@@ -171,12 +171,15 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,gfn_t table_gfn;unsigned index, pt_access, pte_access;gpa_t pte_gpa; -bool eperm, last_gpte; +bool eperm;int offset;const int write_fault = access &PFERR_WRITE_MASK ;const int user_fault = access &PFERR_USER_MASK ;const int fetch_fault = access &PFERR_FETCH_MASK ;u16 errcode =0 ; +gpa_t real_gpa; +gfn_t gfn; +u32 ac;trace_kvm_mmu_pagetable_walk (addr, access); retry_walk:
@@ -197,12 +200,16 @@ retry_walk:ASSERT ((!is_long_mode (vcpu) &&is_pae (vcpu)) || (mmu->get_cr3 (vcpu) &CR3_NONPAE_RESERVED_BITS ) ==0 ); - pt_access =ACC_ALL ; + pt_access = pte_access =ACC_ALL ; + ++walker->level; -for (;;) { + do { gfn_t real_gfn; unsigned long host_addr; + pt_access &= pte_access; + --walker->level; + index = PT_INDEX(addr, walker->level); table_gfn = gpte_to_gfn(pte);
@@ -239,39 +246,8 @@ retry_walk: pte_access = pt_access &gpte_access (vcpu, pte); - last_gpte =FNAME (is_last_gpte)(walker, vcpu, mmu, pte); - walker->ptes[walker->level -1 ] = pte; - -if (last_gpte) { -int lvl = walker->level; -gpa_t real_gpa; -gfn_t gfn; -u32 ac; - - gfn =gpte_to_gfn_lvl (pte, lvl); - gfn += (addr &PT_LVL_OFFSET_MASK (lvl)) >>PAGE_SHIFT ; - -if (PTTYPE ==32 && - walker->level ==PT_DIRECTORY_LEVEL && -is_cpuid_PSE36 ()) - gfn +=pse36_gfn_delta (pte); - - ac = write_fault | fetch_fault | user_fault; - - real_gpa = mmu->translate_gpa (vcpu,gfn_to_gpa (gfn), - ac); -if (real_gpa ==UNMAPPED_GVA ) -return 0 ; - - walker->gfn = real_gpa >>PAGE_SHIFT ; - -break ; - } - - pt_access &= pte_access; - --walker->level; - } + }while (!FNAME (is_last_gpte)(walker, vcpu, mmu, pte)); eperm |=permission_fault (mmu, pte_access, access);if (unlikely (eperm)) {
@@ -279,6 +255,20 @@ retry_walk: goto error; } + gfn =gpte_to_gfn_lvl (pte, walker->level); + gfn += (addr &PT_LVL_OFFSET_MASK (walker->level)) >>PAGE_SHIFT ; + +if (PTTYPE ==32 && walker->level ==PT_DIRECTORY_LEVEL &&is_cpuid_PSE36 ()) + gfn +=pse36_gfn_delta (pte); + + ac = write_fault | fetch_fault | user_fault; + + real_gpa = mmu->translate_gpa (vcpu,gfn_to_gpa (gfn), ac); +if (real_gpa ==UNMAPPED_GVA ) +return 0 ; + + walker->gfn = real_gpa >>PAGE_SHIFT ; +if (!write_fault)protect_clean_gpte (&pte_access, pte);
Signature · 1 line
1.7.12
PATCH v3 7/10 KVM: MMU: Optimize is_last_gpte()
In reply to Avi KivityInstead of branchy code depending on level, gpte.ps, and mmu configuration, prepare everything in a bitmap during mode changes and look it up during runtime.
--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu.c | 31 +++++++++++++++++++++++++++++++ arch/x86/kvm/mmu.h | 3 ++- arch/x86/kvm/paging_tmpl.h | 20 +------------------- 4 files changed, 41 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -0
@@ -298,6 +298,13 @@ struct kvm_mmu {u64 *lm_root;u64 rsvd_bits_mask[2 ][4 ]; +/* +* Bitmap: bit set = last pte in walk +* index[0:1]: level (zero-based) +* index[2]: pte.ps +*/ +u8 last_pte_bitmap; +bool nx;u64 pdptrs[4 ];/* pae */
arch/x86/kvm/mmu.c +31 -0
@@ -3447,6 +3447,15 @@ static inline unsigned gpte_access(struct kvm_vcpu *vcpu, u64 gpte)return access; } +static inline bool is_last_gpte (struct kvm_mmu *mmu,unsigned level,unsigned gpte) +{ +unsigned index; + + index = level -1 ; + index |= (gpte &PT_PAGE_SIZE_MASK ) >> (PT_PAGE_SIZE_SHIFT -2 ); +return mmu->last_pte_bitmap & (1 << index); +} +#define PTTYPE 64#include "paging_tmpl.h" #undef PTTYPE
@@ -3548,6 +3557,24 @@ static void update_permission_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu } } +static void update_last_pte_bitmap (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu) +{ +u8 map; +unsigned level, root_level = mmu->root_level; +const unsigned ps_set_index =1 <<2 ;/* bit 2 of index: ps */ + +if (root_level ==PT32E_ROOT_LEVEL ) + --root_level; +/* PT_PAGE_TABLE_LEVEL always terminates */ + map =1 | (1 << ps_set_index); +for (level =PT_DIRECTORY_LEVEL ; level <= root_level; ++level) { +if (level <=PT_PDPE_LEVEL + && (mmu->root_level >=PT32E_ROOT_LEVEL ||is_pse (vcpu))) + map |=1 << (ps_set_index | (level -1 )); + } + mmu->last_pte_bitmap = map; +} +static int paging64_init_context_common (struct kvm_vcpu *vcpu,struct kvm_mmu *context,int level)
@@ -3557,6 +3584,7 @@ static int paging64_init_context_common(struct kvm_vcpu *vcpu,reset_rsvds_bits_mask (vcpu, context);update_permission_bitmask (vcpu, context); +update_last_pte_bitmap (vcpu, context);ASSERT (is_pae (vcpu)); context->new_cr3 = paging_new_cr3;
@@ -3586,6 +3614,7 @@ static int paging32_init_context(struct kvm_vcpu *vcpu,reset_rsvds_bits_mask (vcpu, context);update_permission_bitmask (vcpu, context); +update_last_pte_bitmap (vcpu, context); context->new_cr3 = paging_new_cr3; context->page_fault = paging32_page_fault;
@@ -3647,6 +3676,7 @@ static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) }update_permission_bitmask (vcpu, context); +update_last_pte_bitmap (vcpu, context);return 0 ; }
@@ -3724,6 +3754,7 @@ static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu) }update_permission_bitmask (vcpu, g_context); +update_last_pte_bitmap (vcpu, g_context);return 0 ; }
arch/x86/kvm/mmu.h +2 -1
@@ -20,7 +20,8 @@#define PT_ACCESSED_MASK (1ULL << PT_ACCESSED_SHIFT)#define PT_DIRTY_SHIFT 6#define PT_DIRTY_MASK (1ULL << PT_DIRTY_SHIFT) -#define PT_PAGE_SIZE_MASK (1ULL << 7) +#define PT_PAGE_SIZE_SHIFT 7 +#define PT_PAGE_SIZE_MASK (1ULL << PT_PAGE_SIZE_SHIFT)#define PT_PAT_MASK (1ULL << 7)#define PT_GLOBAL_MASK (1ULL << 8)#define PT64_NX_SHIFT 63
arch/x86/kvm/paging_tmpl.h +1 -19
@@ -103,24 +103,6 @@ static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,return (ret != orig_pte); } -static bool FNAME (is_last_gpte )(struct guest_walker *walker, -struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, -pt_element_t gpte) -{ -if (walker->level ==PT_PAGE_TABLE_LEVEL ) -return true; - -if ((walker->level ==PT_DIRECTORY_LEVEL ) &&is_large_pte (gpte) && - (PTTYPE ==64 ||is_pse (vcpu))) -return true; - -if ((walker->level ==PT_PDPE_LEVEL ) &&is_large_pte (gpte) && - (mmu->root_level ==PT64_ROOT_LEVEL )) -return true; - -return false; -} - static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, struct guest_walker *walker,
@@ -247,7 +229,7 @@ retry_walk: pte_access = pt_access &gpte_access (vcpu, pte); walker->ptes[walker->level -1 ] = pte; - }while (!FNAME (is_last_gpte)(walker, vcpu, mmu, pte)); + }while (!is_last_gpte (mmu, walker->level, pte)); eperm |=permission_fault (mmu, pte_access, access);if (unlikely (eperm)) {
Signature · 1 line
1.7.12
PATCH v3 8/10 KVM: MMU: Eliminate eperm temporary
In reply to Avi Kivity'eperm' is no longer used in the walker loop, so we can eliminate it.
--- arch/x86/kvm/paging_tmpl.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
arch/x86/kvm/paging_tmpl.h +1 -4
@@ -153,7 +153,6 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,gfn_t table_gfn;unsigned index, pt_access, pte_access;gpa_t pte_gpa; -bool eperm;int offset;const int write_fault = access &PFERR_WRITE_MASK ;const int user_fault = access &PFERR_USER_MASK ;
@@ -165,7 +164,6 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,trace_kvm_mmu_pagetable_walk (addr, access); retry_walk: - eperm = false; walker->level = mmu->root_level; pte = mmu->get_cr3 (vcpu);
@@ -231,8 +229,7 @@ retry_walk: walker->ptes[walker->level -1 ] = pte; }while (!is_last_gpte (mmu, walker->level, pte)); - eperm |=permission_fault (mmu, pte_access, access); -if (unlikely (eperm)) { +if (unlikely (permission_fault (mmu, pte_access, access))) { errcode |=PFERR_PRESENT_MASK ; goto error; }
Signature · 1 line
1.7.12
PATCH v3 9/10 KVM: MMU: Avoid access/dirty update loop if all is well
In reply to Avi KivityKeep track of accessed/dirty bits; if they are all set, do not enter the accessed/dirty update loop.
--- arch/x86/kvm/paging_tmpl.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
arch/x86/kvm/paging_tmpl.h +20 -6
@@ -151,7 +151,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,pt_element_t pte;pt_element_t __user *uninitialized_var (ptep_user );gfn_t table_gfn; -unsigned index, pt_access, pte_access; +unsigned index, pt_access, pte_access, accessed_dirty, shift;gpa_t pte_gpa;int offset;const int write_fault = access &PFERR_WRITE_MASK ;
@@ -180,6 +180,7 @@ retry_walk:ASSERT ((!is_long_mode (vcpu) &&is_pae (vcpu)) || (mmu->get_cr3 (vcpu) &CR3_NONPAE_RESERVED_BITS ) ==0 ); + accessed_dirty =PT_ACCESSED_MASK ; pt_access = pte_access =ACC_ALL ; ++walker->level;
@@ -224,6 +225,7 @@ retry_walk: goto error; } + accessed_dirty &= pte; pte_access = pt_access &gpte_access (vcpu, pte); walker->ptes[walker->level -1 ] = pte;
@@ -251,11 +253,23 @@ retry_walk:if (!write_fault)protect_clean_gpte (&pte_access, pte); - ret =FNAME (update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault); -if (unlikely (ret <0 )) - goto error; -else if (ret) - goto retry_walk; +/* +* On a write fault, fold the dirty bit into accessed_dirty by shifting it one +* place right. +* +* On a read fault, do nothing. +*/ + shift = write_fault >>ilog2 (PFERR_WRITE_MASK ); + shift *=PT_DIRTY_SHIFT -PT_ACCESSED_SHIFT ; + accessed_dirty &= pte >> shift; + +if (unlikely (!accessed_dirty)) { + ret =FNAME (update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault); +if (unlikely (ret <0 )) + goto error; +else if (ret) + goto retry_walk; + } walker->pt_access = pt_access; walker->pte_access = pte_access;
Signature · 1 line
1.7.12
PATCH v3 10/10 KVM: MMU: Eliminate pointless temporary 'ac'
In reply to Avi Kivity'ac' essentially reconstructs the 'access' variable we already have, except for the PFERR_PRESENT_MASK and PFERR_RSVD_MASK. As these are not used by callees, just use 'access' directly.
--- arch/x86/kvm/paging_tmpl.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
arch/x86/kvm/paging_tmpl.h +1 -4
@@ -160,7 +160,6 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,u16 errcode =0 ;gpa_t real_gpa;gfn_t gfn; -u32 ac;trace_kvm_mmu_pagetable_walk (addr, access); retry_walk:
@@ -242,9 +241,7 @@ retry_walk:if (PTTYPE ==32 && walker->level ==PT_DIRECTORY_LEVEL &&is_cpuid_PSE36 ()) gfn +=pse36_gfn_delta (pte); - ac = write_fault | fetch_fault | user_fault; - - real_gpa = mmu->translate_gpa (vcpu,gfn_to_gpa (gfn), ac); + real_gpa = mmu->translate_gpa (vcpu,gfn_to_gpa (gfn), access);if (real_gpa ==UNMAPPED_GVA )return 0 ;
Signature · 1 line
1.7.12
Re: [PATCH v3 10/10] KVM: MMU: Eliminate pointless temporary 'ac'
Quoted 4 lines · Avi Kivity
'ac' essentially reconstructs the 'access' variable we already have, except for the
PFERR_PRESENT_MASKandPFERR_RSVD_MASK. As these are not used by callees, just use 'access' directly.