kvm: x86: export maximum number of mmu_page_hash collisions
PATCH 1/2 kvm: x86: export maximum number of mmu_page_hash collisions
Report the maximum number of mmu_page_hash collisions as a per-VM stat. This will make it easy to identify problems with the mmu_page_hash in the future.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 31 +++++++++++++++++++++---------- arch/x86/kvm/x86.c | 2 ++ 3 files changed, 24 insertions(+), 10 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -815,6 +815,7 @@ struct kvm_vm_stat {ulong mmu_unsync;ulong remote_tlb_flush;ulong lpages; +ulong max_mmu_page_hash_collisions; };struct kvm_vcpu_stat {
arch/x86/kvm/mmu.c +21 -10
@@ -1904,17 +1904,17 @@ static void kvm_mmu_commit_zap_page(struct kvm *kvm,* since it has been deleted from active_mmu_pages but still can be found * at hast list. * -* for_each_gfn_valid_sp() has skipped that kind of pages. +* for_each_valid_sp() has skipped that kind of pages. */ -#define for_each_gfn_valid_sp (_kvm, _sp, _gfn) \ +#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) \ - if ((_sp)->gfn != (_gfn) || is_obsolete_sp((_kvm), (_sp)) \ - || (_sp)->role.invalid) {} else + if (is_obsolete_sp((_kvm), (_sp)) || (_sp)->role.invalid) { \ + } else#define for_each_gfn_indirect_valid_sp (_kvm, _sp, _gfn) \ - for_each_gfn_valid_sp(_kvm, _sp, _gfn) \ - if ((_sp)->role.direct) {} else + for_each_valid_sp(_kvm, _sp, _gfn) \ + if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} else/* @sp->gfn should be write-protected at the call site */ static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
@@ -2116,6 +2116,8 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp;bool need_sync = false;bool flush = false; +int collisions =0 ; +bool created = true;LIST_HEAD (invalid_list); role = vcpu->arch.mmu.base_role;
@@ -2130,7 +2132,12 @@ 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_gfn_valid_sp (vcpu->kvm, sp, gfn) { +for_each_valid_sp (vcpu->kvm, sp, gfn) { +if (sp->gfn != gfn) { + collisions++; +continue ; + } +if (!need_sync && sp->unsync) need_sync = true;
@@ -2152,8 +2159,8 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,kvm_make_request (KVM_REQ_MMU_SYNC , vcpu);__clear_sp_write_flooding_count (sp); -trace_kvm_mmu_get_page (sp, false); -return sp; + created = false; + goto out; } ++vcpu->kvm->stat.mmu_cache_miss;
@@ -2164,6 +2171,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, sp->role = role;hlist_add_head (&sp->hash_link, &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn (gfn)]); +if (!direct) {/* * we should do write protection before syncing pages
@@ -2180,9 +2188,12 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, } sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;clear_page (sp->spt); -trace_kvm_mmu_get_page (sp, true);kvm_mmu_flush_or_zap (vcpu, &invalid_list, false, flush); +out: +if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions) + vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions; +trace_kvm_mmu_get_page (sp, created);return sp; }
arch/x86/kvm/x86.c +2 -0
@@ -190,6 +190,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] = { { "mmu_unsync", VM_STAT(mmu_unsync) }, { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, { "largepages", VM_STAT(lpages) }, + { "max_mmu_page_hash_collisions", + VM_STAT(max_mmu_page_hash_collisions) }, { NULL } };
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 2/2 kvm: x86: reduce collisions in mmu_page_hash
When using two-dimensional paging, the mmu_page_hash (which provides lookups for existing kvm_mmu_page structs), becomes imbalanced; with too many collisions in buckets 0 and 512. This has been seen to cause mmu_lock to be held for multiple milliseconds in kvm_mmu_get_page on VMs with a large amount of RAM mapped with 4K pages.
The current hash function uses the lower 10 bits of gfn to index into mmu_page_hash. When doing shadow paging, gfn is the address of the guest page table being shadow. These tables are 4K-aligned, which makes the low bits of gfn a good hash. However, with two-dimensional paging, no guest page tables are being shadowed, so gfn is the base address that is mapped by the table. Thus page tables (level=1) have a 2MB aligned gfn, page directories (level=2) have a 1GB aligned gfn, etc. This means hashes will only differ in their 10th bit.
hash_64() provides a better hash. For example, on a VM with ~200G (99458 direct=1 kvm_mmu_page structs):
hash max_mmu_page_hash_collisions
--------------------------------------------
low 10 bits 49847 hash_64 105 perfect 97
While we're changing the hash, increase the table size by 4x to better support large VMs (further reduces number of collisions in 200G VM to 29).
Note that hash_64() does not provide a good distribution prior to commit ef703f49a6c5 ("Eliminate bad hash multipliers from hash_32() and hash_64()").
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -115,7 +115,7 @@ static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)#define KVM_PERMILLE_MMU_PAGES 20#define KVM_MIN_ALLOC_MMU_PAGES 64 -#define KVM_MMU_HASH_SHIFT 10 +#define KVM_MMU_HASH_SHIFT 12#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)#define KVM_MIN_FREE_MMU_PAGES 5#define KVM_REFILL_PAGES 25
arch/x86/kvm/mmu.c +1 -1
@@ -1713,7 +1713,7 @@ static void kvm_mmu_free_page(struct kvm_mmu_page *sp)static unsigned kvm_page_table_hashfn (gfn_t gfn) { -return gfn & ((1 <<KVM_MMU_HASH_SHIFT ) -1 ); +return hash_64 (gfn,KVM_MMU_HASH_SHIFT ); } static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Signature · 1 line
2.8.0.rc3.226.g39d4020
Re: [PATCH 2/2] kvm: x86: reduce collisions in mmu_page_hash
Quoted 7 lines
arch/x86/kvm/mmu.c +0
-0
arch/x86/kvm/mmu.c static void kvm_mmu_free_page(struct kvm_mmu_page *sp) · line 1719
Quoted 8 lines
arch/x86/kvm/mmu.c +1
-1@@ -1713,7 +1713,7 @@ static void kvm_mmu_free_page(struct kvm_mmu_page *sp)static unsigned kvm_page_table_hashfn (gfn_t gfn) { -return gfn & ((1 <<KVM_MMU_HASH_SHIFT ) -1 ); +return hash_64 (gfn,KVM_MMU_HASH_SHIFT ); } static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
hash_64() might be more expensive to calculate, as it involves one multiplication. Not sure if that might be a problem.
Looks good to me!
Signature · 1 line
David
Paolo Bonzini
maintainerRe: [PATCH 2/2] kvm: x86: reduce collisions in mmu_page_hash
In reply to David MatlackQuoted 3 lines · David Matlack
When using two-dimensional paging, the
mmu_page_hash(which provides lookups for existingkvm_mmu_pagestructs), becomes imbalanced; with too many collisions in buckets 0 and 512.
Ouch, nice catch. I'll apply this patch tomorrow since it's pretty much independent of patch 1. If you want to send out a revision of the statistics patch, please do.
Thanks,
Paolo
This has been seen to cause
Quoted 63 lines
mmu_lockto be held for multiple milliseconds inkvm_mmu_get_pageon VMs with a large amount of RAM mapped with 4K pages.The current hash function uses the lower 10 bits of gfn to index into
mmu_page_hash. When doing shadow paging, gfn is the address of the guest page table being shadow. These tables are 4K-aligned, which makes the low bits of gfn a good hash. However, with two-dimensional paging, no guest page tables are being shadowed, so gfn is the base address that is mapped by the table. Thus page tables (level=1) have a 2MB aligned gfn, page directories (level=2) have a 1GB aligned gfn, etc. This means hashes will only differ in their 10th bit.
hash_64()provides a better hash. For example, on a VM with ~200G (99458 direct=1kvm_mmu_pagestructs):hash
max_mmu_page_hash_collisions
--------------------------------------------low 10 bits 49847 hash_64 105 perfect 97While we're changing the hash, increase the table size by 4x to better support large VMs (further reduces number of collisions in 200G VM to 29).
Note that
hash_64()does not provide a good distribution prior to commitef703f49a6c5("Eliminate bad hash multipliers fromhash_32()andhash_64()").Signed-off-by David Matlack <[email protected]>Change-Id I5aa6b13c834722813c6cca46b8b1ed6f53368ade--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm_host.h +1
-1@@ -115,7 +115,7 @@ static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)#define KVM_PERMILLE_MMU_PAGES 20#define KVM_MIN_ALLOC_MMU_PAGES 64 -#define KVM_MMU_HASH_SHIFT 10 +#define KVM_MMU_HASH_SHIFT 12#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)#define KVM_MIN_FREE_MMU_PAGES 5#define KVM_REFILL_PAGES 25arch/x86/kvm/mmu.c +1
-1@@ -1713,7 +1713,7 @@ static void kvm_mmu_free_page(struct kvm_mmu_page *sp)static unsigned kvm_page_table_hashfn (gfn_t gfn) { -return gfn & ((1 <<KVM_MMU_HASH_SHIFT ) -1 ); +return hash_64 (gfn,KVM_MMU_HASH_SHIFT ); } static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Re: [PATCH 2/2] kvm: x86: reduce collisions in mmu_page_hash
Quoted 8 lines · Paolo Bonzini
Quoted 3 lines · David Matlack
When using two-dimensional paging, the
mmu_page_hash(which provides lookups for existingkvm_mmu_pagestructs), becomes imbalanced; with too many collisions in buckets 0 and 512.Ouch, nice catch. I'll apply this patch tomorrow since it's pretty much independent of patch 1. If you want to send out a revision of the statistics patch, please do.
SG. I'll send v2 of the stats patch by itself. Thanks!
Quoted 12 lines
arch/x86/kvm/mmu.c +6
-1-for_each_gfn_valid_sp (vcpu->kvm, sp, gfn) { +for_each_valid_sp (vcpu->kvm, sp, gfn) { +if (sp->gfn != gfn) { + collisions++; +continue ; + } +if (!need_sync && sp->unsync) need_sync = true;
arch/x86/kvm/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2162
Quoted 6 lines
arch/x86/kvm/mmu.c +1
-2@@ -2152,8 +2159,8 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,kvm_make_request (KVM_REQ_MMU_SYNC , vcpu);__clear_sp_write_flooding_count (sp); -trace_kvm_mmu_get_page (sp, false); -return sp; + created = false;
Simply doing
if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions)
vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;
at this point saves a local variable, a jump label and keeps this patch minimal.
arch/x86/kvm/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2174
Quoted 9 lines
arch/x86/kvm/mmu.c +2
-0+ goto out; } ++vcpu->kvm->stat.mmu_cache_miss;@@ -2164,6 +2171,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, sp->role = role;hlist_add_head (&sp->hash_link, &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn (gfn)]); +
unrelated change.
Quoted 16 lines
arch/x86/kvm/mmu.c +4
-1if (!direct) {/* * we should do write protection before syncing pages @@ -2180,9 +2188,12 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, } sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;clear_page (sp->spt); -trace_kvm_mmu_get_page (sp, true);kvm_mmu_flush_or_zap (vcpu, &invalid_list, false, flush); +out: +if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions) + vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions; +trace_kvm_mmu_get_page (sp, created);return sp; }
Signature · 1 line
David
Paolo Bonzini
maintainerQuoted 6 lines · David Hildenbrand
arch/x86/kvm/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2162
Quoted 5 lines
__clear_sp_write_flooding_count(sp);arch/x86/kvm/mmu.c +1
-2-trace_kvm_mmu_get_page (sp, false); -return sp; + created = false;
Quoted 7 lines
Simply doing
if (collisions >
vcpu->kvm->stat.max_mmu_page_hash_collisions)vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;at this point saves a local variable, a jump label and keeps this patch minimal.
How so? There is a "break" above.
Paolo
Am 20.12.2016 um 10:55 schrieb Paolo Bonzini:
Quoted 11 lines · David Hildenbrand
Quoted 8 lines
arch/x86/kvm/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2162
Quoted 5 lines
__clear_sp_write_flooding_count(sp);arch/x86/kvm/mmu.c +1
-2-trace_kvm_mmu_get_page (sp, false); -return sp; + created = false;Simply doing
Quoted 8 lines
Quoted 5 lines
if (collisions >
vcpu->kvm->stat.max_mmu_page_hash_collisions)vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;at this point saves a local variable, a jump label and keeps this patch minimal.
How so? There is a "break" above.
Keeping the same at the end of the function / (even better directly after the for loop) of course.
Quoted 2 lines
Paolo
Signature · 1 line
David
Paolo Bonzini
maintainerQuoted 15 lines · David Hildenbrand
Am 20.12.2016 um 10:55 schrieb Paolo Bonzini:
Quoted 14 lines · David Hildenbrand
Quoted 11 lines
arch/x86/kvm/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2162
Quoted 5 lines
__clear_sp_write_flooding_count(sp);arch/x86/kvm/mmu.c +1
-2-trace_kvm_mmu_get_page (sp, false); -return sp; + created = false;Simply doing
if (collisions >
vcpu->kvm->stat.max_mmu_page_hash_collisions)vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
at this point saves a local variable, a jump label and keeps this patch minimal.
How so? There is a "break" above.
Keeping the same at the end of the function / (even better directly after the for loop) of course.
Then the question is whether a goto is better or worse than code duplication. Here I don't see the point in introducing the created bool and factoring the call to trace_kvm_mmu_get_page, but on the other hand a single "out" label makes sense.
Paolo
Quoted 6 lines
Then the question is whether a goto is better or worse than code duplication. Here I don't see the point in introducing the created bool and factoring the call to
trace_kvm_mmu_get_page, but on the other hand a single "out" label makes sense.
Agreed.
Signature · 1 line
David