KVM: x86/mmu: Optimizations for kvm_get_mmu_page()
cover KVM: x86/mmu: Optimizations for kvm_get_mmu_page()
Avoid multiple hash lookups in kvm_get_mmu_page(), and tweak the cache loop to optimize it for TDP.
Sean Christopherson (2):
KVM: x86/mmu: Avoid multiple hash lookups in kvm_get_mmu_page()
KVM: x86/mmu: Optimize MMU page cache lookup for fully direct MMUs
arch/x86/kvm/mmu/mmu.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
Signature · 1 line
2.26.0
PATCH 1/2 KVM: x86/mmu: Avoid multiple hash lookups in kvm_get_mmu_page()
Refactor for_each_valid_sp() to take the list of shadow pages instead of retrieving it from a gfn to avoid doing the gfn->list hash and lookup multiple times during kvm_get_mmu_page().
--- arch/x86/kvm/mmu/mmu.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu/mmu.c +9 -8
@@ -2258,15 +2258,14 @@ static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,static void kvm_mmu_commit_zap_page (struct kvm *kvm,struct list_head *invalid_list); - -#define for_each_valid_sp (_kvm, _sp, _gfn) \ - hlist_for_each_entry(_sp, \ - &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \ +#define for_each_valid_sp (_kvm, _sp, _list) \ + hlist_for_each_entry(_sp, _list, hash_link) \ if (is_obsolete_sp((_kvm), (_sp))) { \ } else#define for_each_gfn_indirect_valid_sp (_kvm, _sp, _gfn) \ - for_each_valid_sp(_kvm, _sp, _gfn) \ + for_each_valid_sp(_kvm, _sp, \ + &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \ if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} elsestatic inline bool is_ept_sp (struct kvm_mmu_page *sp)
@@ -2477,6 +2476,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, unsigned int access) {union kvm_mmu_page_role role; +struct hlist_head *sp_list;unsigned quadrant;struct kvm_mmu_page *sp;bool need_sync = false;
@@ -2496,7 +2496,9 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, quadrant &= (1 << ((PT32_PT_BITS -PT64_PT_BITS ) * level)) -1 ; role.quadrant = quadrant; } -for_each_valid_sp (vcpu->kvm, sp, gfn) { + + sp_list = &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn (gfn)]; +for_each_valid_sp (vcpu->kvm, sp, sp_list) {if (sp->gfn != gfn) { collisions++;continue ;
@@ -2533,8 +2535,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, sp->gfn = gfn; sp->role = role; -hlist_add_head (&sp->hash_link, - &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn (gfn)]); +hlist_add_head (&sp->hash_link, sp_list);if (!direct) {/* * we should do write protection before syncing pages
Signature · 1 line
2.26.0
Re: [PATCH 1/2] KVM: x86/mmu: Avoid multiple hash lookups in kvm_get_mmu_page()
LGTM.
Quoted 69 lines · Sean Christopherson
Refactor
for_each_valid_sp()to take the list of shadow pages instead of retrieving it from a gfn to avoid doing thegfn->listhash and lookup multiple times duringkvm_get_mmu_page().Cc Peter Feiner <[email protected]>Cc Jon Cargille <[email protected]>Cc Jim Mattson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)arch/x86/kvm/mmu/mmu.c +9
-9@@ -2258,15 +2258,14 @@ static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,static void kvm_mmu_commit_zap_page (struct kvm *kvm,struct list_head *invalid_list); - -#define for_each_valid_sp (_kvm, _sp, _gfn) \ - hlist_for_each_entry(_sp, \ - &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \ +#define for_each_valid_sp (_kvm, _sp, _list) \ + hlist_for_each_entry(_sp, _list, hash_link) \ if (is_obsolete_sp((_kvm), (_sp))) { \ } else#define for_each_gfn_indirect_valid_sp (_kvm, _sp, _gfn) \ - for_each_valid_sp(_kvm, _sp, _gfn) \ + for_each_valid_sp(_kvm, _sp, \ + &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \ if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} elsestatic inline bool is_ept_sp (struct kvm_mmu_page *sp)@@ -2477,6 +2476,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, unsigned int access) {union kvm_mmu_page_role role; +struct hlist_head *sp_list;unsigned quadrant;struct kvm_mmu_page *sp;bool need_sync = false;@@ -2496,7 +2496,9 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, quadrant &= (1 << ((PT32_PT_BITS -PT64_PT_BITS ) * level)) -1 ; role.quadrant = quadrant; } -for_each_valid_sp (vcpu->kvm, sp, gfn) { + + sp_list = &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn (gfn)]; +for_each_valid_sp (vcpu->kvm, sp, sp_list) {if (sp->gfn != gfn) { collisions++;continue ;@@ -2533,8 +2535,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, sp->gfn = gfn; sp->role = role; -hlist_add_head (&sp->hash_link, - &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn (gfn)]); +hlist_add_head (&sp->hash_link, sp_list);if (!direct) {/* * we should do write protection before syncing pages -- 2.26.0
PATCH 2/2 KVM: x86/mmu: Optimize MMU page cache lookup for fully direct MMUs
In reply to Sean ChristophersonSkip the unsync checks and the write flooding clearing for fully direct MMUs, which are guaranteed to not have unsync'd or indirect pages (write flooding detection only applies to indirect pages). For TDP, this avoids unnecessary memory reads and writes, and for the write flooding count will also avoid dirtying a cache line (unsync_child_bitmap itself consumes a cache line, i.e. write_flooding_count is guaranteed to be in a different cache line than parent_ptes).
--- arch/x86/kvm/mmu/mmu.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +7 -2
@@ -2475,6 +2475,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,int direct, unsigned int access) { + bool direct_mmu = vcpu->arch.mmu->direct_map;union kvm_mmu_page_role role;struct hlist_head *sp_list;unsigned quadrant;
@@ -2490,8 +2491,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,if (role.direct) role.gpte_is_8_bytes = true; role.access = access; -if (!vcpu->arch.mmu->direct_map - && vcpu->arch.mmu->root_level <=PT32_ROOT_LEVEL ) { +if (!direct_mmu && vcpu->arch.mmu->root_level <=PT32_ROOT_LEVEL ) { quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); quadrant &= (1 << ((PT32_PT_BITS -PT64_PT_BITS ) * level)) -1 ; role.quadrant = quadrant;
@@ -2510,6 +2510,9 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,if (sp->role.word != role.word)continue ; +if (direct_mmu) + goto trace_get_page; +if (sp->unsync) {/* The page is good, but __kvm_sync_page might still end * up zapping it. If so, break in order to rebuild it.
@@ -2525,6 +2528,8 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu);__clear_sp_write_flooding_count (sp); + +trace_get_page:trace_kvm_mmu_get_page (sp, false); goto out; }
Signature · 1 line
2.26.0
Re: [PATCH 2/2] KVM: x86/mmu: Optimize MMU page cache lookup for fully direct MMUs
LGTM.
Quoted 58 lines · Sean Christopherson
Skip the unsync checks and the write flooding clearing for fully direct MMUs, which are guaranteed to not have unsync'd or indirect pages (write flooding detection only applies to indirect pages). For TDP, this avoids unnecessary memory reads and writes, and for the write flooding count will also avoid dirtying a cache line (
unsync_child_bitmapitself consumes a cache line, i.e.write_flooding_countis guaranteed to be in a different cache line thanparent_ptes).Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/mmu/mmu.c +7
-3@@ -2475,6 +2475,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,int direct, unsigned int access) { + bool direct_mmu = vcpu->arch.mmu->direct_map;union kvm_mmu_page_role role;struct hlist_head *sp_list;unsigned quadrant;@@ -2490,8 +2491,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,if (role.direct) role.gpte_is_8_bytes = true; role.access = access; -if (!vcpu->arch.mmu->direct_map - && vcpu->arch.mmu->root_level <=PT32_ROOT_LEVEL ) { +if (!direct_mmu && vcpu->arch.mmu->root_level <=PT32_ROOT_LEVEL ) { quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); quadrant &= (1 << ((PT32_PT_BITS -PT64_PT_BITS ) * level)) -1 ; role.quadrant = quadrant;@@ -2510,6 +2510,9 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,if (sp->role.word != role.word)continue ; +if (direct_mmu) + goto trace_get_page; +if (sp->unsync) {/* The page is good, but __kvm_sync_page might still end * up zapping it. If so, break in order to rebuild it. @@ -2525,6 +2528,8 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu);__clear_sp_write_flooding_count (sp); + +trace_get_page:trace_kvm_mmu_get_page (sp, false); goto out; } --2.26.0
Quoted 10 lines · Sean Christopherson
Avoid multiple hash lookups in
kvm_get_mmu_page(), and tweak the cache loop to optimize it for TDP.Sean Christopherson (2):
KVM: x86/mmu: Avoid multiple hash lookups inkvm_get_mmu_page()
KVM: x86/mmu: Optimize MMU page cache lookup for fully direct MMUsarch/x86/kvm/mmu/mmu.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
Queued, thanks.
Paolo