Skip to content

Commit b5c3c1b

Browse files
Sean Christophersonbonzini
authored andcommitted
KVM: x86/mmu: Micro-optimize nEPT's bad memptype/XWR checks
Rework the handling of nEPT's bad memtype/XWR checks to micro-optimize the checks as much as possible. Move the check to a separate helper, __is_bad_mt_xwr(), which allows the guest_rsvd_check usage in paging_tmpl.h to omit the check entirely for paging32/64 (bad_mt_xwr is always zero for non-nEPT) while retaining the bitwise-OR of the current code for the shadow_zero_check in walk_shadow_page_get_mmio_spte(). Add a comment for the bitwise-OR usage in the mmio spte walk to avoid future attempts to "fix" the code, which is what prompted this optimization in the first place[*]. Opportunistically remove the superfluous '!= 0' and parantheses, and use BIT_ULL() instead of open coding its equivalent. The net effect is that code generation is largely unchanged for walk_shadow_page_get_mmio_spte(), marginally better for ept_prefetch_invalid_gpte(), and significantly improved for paging32/64_prefetch_invalid_gpte(). Note, walk_shadow_page_get_mmio_spte() can't use a templated version of the memtype/XRW as it works on the host's shadow PTEs, e.g. checks that KVM hasn't borked its EPT tables. Even if it could be templated, the benefits of having a single implementation far outweight the few uops that would be saved for NPT or non-TDP paging, e.g. most compilers inline it all the way to up kvm_mmu_page_fault(). [*] https://lkml.kernel.org/r/[email protected] Cc: Jim Mattson <[email protected]> Cc: David Laight <[email protected]> Cc: Arvind Sankar <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Reviewed-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent f8052a0 commit b5c3c1b

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,20 +3968,14 @@ static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gpa_t vaddr,
39683968
static bool
39693969
__is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
39703970
{
3971-
int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f;
3971+
int bit7 = (pte >> 7) & 1;
39723972

3973-
return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) |
3974-
((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0);
3973+
return pte & rsvd_check->rsvd_bits_mask[bit7][level-1];
39753974
}
39763975

3977-
static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
3976+
static bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check, u64 pte)
39783977
{
3979-
return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level);
3980-
}
3981-
3982-
static bool is_shadow_zero_bits_set(struct kvm_mmu *mmu, u64 spte, int level)
3983-
{
3984-
return __is_rsvd_bits_set(&mmu->shadow_zero_check, spte, level);
3978+
return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f);
39853979
}
39863980

39873981
static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
@@ -4005,9 +3999,12 @@ walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
40053999
{
40064000
struct kvm_shadow_walk_iterator iterator;
40074001
u64 sptes[PT64_ROOT_MAX_LEVEL], spte = 0ull;
4002+
struct rsvd_bits_validate *rsvd_check;
40084003
int root, leaf;
40094004
bool reserved = false;
40104005

4006+
rsvd_check = &vcpu->arch.mmu->shadow_zero_check;
4007+
40114008
walk_shadow_page_lockless_begin(vcpu);
40124009

40134010
for (shadow_walk_init(&iterator, vcpu, addr),
@@ -4022,8 +4019,13 @@ walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
40224019
if (!is_shadow_present_pte(spte))
40234020
break;
40244021

4025-
reserved |= is_shadow_zero_bits_set(vcpu->arch.mmu, spte,
4026-
iterator.level);
4022+
/*
4023+
* Use a bitwise-OR instead of a logical-OR to aggregate the
4024+
* reserved bit and EPT's invalid memtype/XWR checks to avoid
4025+
* adding a Jcc in the loop.
4026+
*/
4027+
reserved |= __is_bad_mt_xwr(rsvd_check, spte) |
4028+
__is_rsvd_bits_set(rsvd_check, spte, iterator.level);
40274029
}
40284030

40294031
walk_shadow_page_lockless_end(vcpu);

arch/x86/kvm/mmu/paging_tmpl.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ static inline int FNAME(is_present_gpte)(unsigned long pte)
128128
#endif
129129
}
130130

131+
static bool FNAME(is_bad_mt_xwr)(struct rsvd_bits_validate *rsvd_check, u64 gpte)
132+
{
133+
#if PTTYPE != PTTYPE_EPT
134+
return false;
135+
#else
136+
return __is_bad_mt_xwr(rsvd_check, gpte);
137+
#endif
138+
}
139+
140+
static bool FNAME(is_rsvd_bits_set)(struct kvm_mmu *mmu, u64 gpte, int level)
141+
{
142+
return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level) ||
143+
FNAME(is_bad_mt_xwr)(&mmu->guest_rsvd_check, gpte);
144+
}
145+
131146
static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
132147
pt_element_t __user *ptep_user, unsigned index,
133148
pt_element_t orig_pte, pt_element_t new_pte)
@@ -183,7 +198,7 @@ static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
183198
!(gpte & PT_GUEST_ACCESSED_MASK))
184199
goto no_present;
185200

186-
if (is_rsvd_bits_set(vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL))
201+
if (FNAME(is_rsvd_bits_set)(vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL))
187202
goto no_present;
188203

189204
return false;
@@ -400,7 +415,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
400415
if (unlikely(!FNAME(is_present_gpte)(pte)))
401416
goto error;
402417

403-
if (unlikely(is_rsvd_bits_set(mmu, pte, walker->level))) {
418+
if (unlikely(FNAME(is_rsvd_bits_set)(mmu, pte, walker->level))) {
404419
errcode = PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
405420
goto error;
406421
}

0 commit comments

Comments
 (0)