Skip to content

Commit 6fd01b7

Browse files
committed
KVM: MMU: Optimize is_last_gpte()
Instead 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. Reviewed-by: Xiao Guangrong <[email protected]> Signed-off-by: Avi Kivity <[email protected]>
1 parent 13d22b6 commit 6fd01b7

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ struct kvm_mmu {
298298
u64 *lm_root;
299299
u64 rsvd_bits_mask[2][4];
300300

301+
/*
302+
* Bitmap: bit set = last pte in walk
303+
* index[0:1]: level (zero-based)
304+
* index[2]: pte.ps
305+
*/
306+
u8 last_pte_bitmap;
307+
301308
bool nx;
302309

303310
u64 pdptrs[4]; /* pae */

arch/x86/kvm/mmu.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,6 +3447,15 @@ static inline unsigned gpte_access(struct kvm_vcpu *vcpu, u64 gpte)
34473447
return access;
34483448
}
34493449

3450+
static inline bool is_last_gpte(struct kvm_mmu *mmu, unsigned level, unsigned gpte)
3451+
{
3452+
unsigned index;
3453+
3454+
index = level - 1;
3455+
index |= (gpte & PT_PAGE_SIZE_MASK) >> (PT_PAGE_SIZE_SHIFT - 2);
3456+
return mmu->last_pte_bitmap & (1 << index);
3457+
}
3458+
34503459
#define PTTYPE 64
34513460
#include "paging_tmpl.h"
34523461
#undef PTTYPE
@@ -3548,6 +3557,24 @@ static void update_permission_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu
35483557
}
35493558
}
35503559

3560+
static void update_last_pte_bitmap(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
3561+
{
3562+
u8 map;
3563+
unsigned level, root_level = mmu->root_level;
3564+
const unsigned ps_set_index = 1 << 2; /* bit 2 of index: ps */
3565+
3566+
if (root_level == PT32E_ROOT_LEVEL)
3567+
--root_level;
3568+
/* PT_PAGE_TABLE_LEVEL always terminates */
3569+
map = 1 | (1 << ps_set_index);
3570+
for (level = PT_DIRECTORY_LEVEL; level <= root_level; ++level) {
3571+
if (level <= PT_PDPE_LEVEL
3572+
&& (mmu->root_level >= PT32E_ROOT_LEVEL || is_pse(vcpu)))
3573+
map |= 1 << (ps_set_index | (level - 1));
3574+
}
3575+
mmu->last_pte_bitmap = map;
3576+
}
3577+
35513578
static int paging64_init_context_common(struct kvm_vcpu *vcpu,
35523579
struct kvm_mmu *context,
35533580
int level)
@@ -3557,6 +3584,7 @@ static int paging64_init_context_common(struct kvm_vcpu *vcpu,
35573584

35583585
reset_rsvds_bits_mask(vcpu, context);
35593586
update_permission_bitmask(vcpu, context);
3587+
update_last_pte_bitmap(vcpu, context);
35603588

35613589
ASSERT(is_pae(vcpu));
35623590
context->new_cr3 = paging_new_cr3;
@@ -3586,6 +3614,7 @@ static int paging32_init_context(struct kvm_vcpu *vcpu,
35863614

35873615
reset_rsvds_bits_mask(vcpu, context);
35883616
update_permission_bitmask(vcpu, context);
3617+
update_last_pte_bitmap(vcpu, context);
35893618

35903619
context->new_cr3 = paging_new_cr3;
35913620
context->page_fault = paging32_page_fault;
@@ -3647,6 +3676,7 @@ static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
36473676
}
36483677

36493678
update_permission_bitmask(vcpu, context);
3679+
update_last_pte_bitmap(vcpu, context);
36503680

36513681
return 0;
36523682
}
@@ -3724,6 +3754,7 @@ static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
37243754
}
37253755

37263756
update_permission_bitmask(vcpu, g_context);
3757+
update_last_pte_bitmap(vcpu, g_context);
37273758

37283759
return 0;
37293760
}

arch/x86/kvm/mmu.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#define PT_ACCESSED_MASK (1ULL << PT_ACCESSED_SHIFT)
2121
#define PT_DIRTY_SHIFT 6
2222
#define PT_DIRTY_MASK (1ULL << PT_DIRTY_SHIFT)
23-
#define PT_PAGE_SIZE_MASK (1ULL << 7)
23+
#define PT_PAGE_SIZE_SHIFT 7
24+
#define PT_PAGE_SIZE_MASK (1ULL << PT_PAGE_SIZE_SHIFT)
2425
#define PT_PAT_MASK (1ULL << 7)
2526
#define PT_GLOBAL_MASK (1ULL << 8)
2627
#define PT64_NX_SHIFT 63

arch/x86/kvm/paging_tmpl.h

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,6 @@ static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
103103
return (ret != orig_pte);
104104
}
105105

106-
static bool FNAME(is_last_gpte)(struct guest_walker *walker,
107-
struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
108-
pt_element_t gpte)
109-
{
110-
if (walker->level == PT_PAGE_TABLE_LEVEL)
111-
return true;
112-
113-
if ((walker->level == PT_DIRECTORY_LEVEL) && is_large_pte(gpte) &&
114-
(PTTYPE == 64 || is_pse(vcpu)))
115-
return true;
116-
117-
if ((walker->level == PT_PDPE_LEVEL) && is_large_pte(gpte) &&
118-
(mmu->root_level == PT64_ROOT_LEVEL))
119-
return true;
120-
121-
return false;
122-
}
123-
124106
static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,
125107
struct kvm_mmu *mmu,
126108
struct guest_walker *walker,
@@ -247,7 +229,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
247229
pte_access = pt_access & gpte_access(vcpu, pte);
248230

249231
walker->ptes[walker->level - 1] = pte;
250-
} while (!FNAME(is_last_gpte)(walker, vcpu, mmu, pte));
232+
} while (!is_last_gpte(mmu, walker->level, pte));
251233

252234
eperm |= permission_fault(mmu, pte_access, access);
253235
if (unlikely(eperm)) {

0 commit comments

Comments
 (0)