KVM MMU: don't write-protect if have new mapping to unsync page
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v4 1/9 KVM MMU: split kvm_sync_page() function
Split kvm_sync_page() into kvm_sync_page() and kvm_sync_page_transient() to clarify the code address Avi's suggestion
kvm_sync_page_transient() function only update shadow page but not mark it sync and not write protect sp->gfn. it will be used by later patch
--- arch/x86/kvm/mmu.c | 28 ++++++++++++++++++++++++---- 1 files changed, 24 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu.c +24 -4
@@ -1196,16 +1196,20 @@ static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)static int kvm_mmu_zap_page (struct kvm *kvm,struct kvm_mmu_page *sp); -static int kvm_sync_page (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) +static int __kvm_sync_page (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp, +bool clear_unsync) {if (sp->role.cr4_pae != !!is_pae (vcpu)) {kvm_mmu_zap_page (vcpu->kvm, sp);return 1 ; } -if (rmap_write_protect (vcpu->kvm, sp->gfn)) -kvm_flush_remote_tlbs (vcpu->kvm); -kvm_unlink_unsync_page (vcpu->kvm, sp); +if (clear_unsync) { +if (rmap_write_protect (vcpu->kvm, sp->gfn)) +kvm_flush_remote_tlbs (vcpu->kvm); +kvm_unlink_unsync_page (vcpu->kvm, sp); + } +if (vcpu->arch.mmu.sync_page (vcpu, sp)) {kvm_mmu_zap_page (vcpu->kvm, sp);return 1 ;
@@ -1215,6 +1219,22 @@ static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)return 0 ; } +static void mmu_convert_notrap (struct kvm_mmu_page *sp); +static int kvm_sync_page_transient (struct kvm_vcpu *vcpu, +struct kvm_mmu_page *sp) +{ +int ret; + + ret =__kvm_sync_page (vcpu, sp, false); +mmu_convert_notrap (sp); +return ret; +} + +static int kvm_sync_page (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) +{ +return __kvm_sync_page (vcpu, sp, true); +} +struct mmu_page_path {struct kvm_mmu_page *parent[PT64_ROOT_LEVEL -1 ];unsigned int idx[PT64_ROOT_LEVEL -1 ];
Signature · 1 line
1.6.1.2
PATCH v4 2/9 KVM MMU: don't write-protect if have new mapping to unsync page
In reply to a message not in the archiveTwo cases maybe happen in kvm_mmu_get_page() function:
- one case is, the goal sp is already in cache, if the sp is unsync, we only need update it to assure this mapping is valid, but not mark it sync and not write-protect sp->gfn since it not broke unsync rule(one shadow page for a gfn)
- another case is, the goal sp not existed, we need create a new sp for gfn, i.e, gfn (may)has another shadow page, to keep unsync rule, we should sync(mark sync and write-protect) gfn's unsync shadow page. After enabling multiple unsync shadows, we sync those shadow pages only when the new sp not allow to become unsync(also for the unsyc rule, the new rule is: allow all pte page become unsync)
Changlog:
- fix for forget to mark parent's unsync_children bit when mapping a new parent to unsync shadow page
--- arch/x86/kvm/mmu.c | 18 ++++++++++++++---- 1 files changed, 14 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu.c +14 -4
@@ -1333,7 +1333,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,unsigned index;unsigned quadrant;struct hlist_head *bucket; -struct kvm_mmu_page *sp; +struct kvm_mmu_page *sp, *unsync_sp =NULL ;struct hlist_node *node, *tmp; role = vcpu->arch.mmu.base_role;
@@ -1352,20 +1352,30 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,hlist_for_each_entry_safe (sp, node, tmp, bucket, hash_link)if (sp->gfn == gfn) {if (sp->unsync) -if (kvm_sync_page (vcpu, sp)) -continue ; + unsync_sp = sp;if (sp->role.word != role.word)continue ; +if (!direct && unsync_sp && +kvm_sync_page_transient (vcpu, unsync_sp)) { + unsync_sp =NULL ; +break ; + } +mmu_page_add_parent_pte (vcpu, sp, parent_pte);if (sp->unsync_children) {set_bit (KVM_REQ_MMU_SYNC , &vcpu->requests);kvm_mmu_mark_parents_unsync (sp); - } + }else if (sp->unsync) +kvm_mmu_mark_parents_unsync (sp); +trace_kvm_mmu_get_page (sp, false);return sp; } +if (!direct && unsync_sp) +kvm_sync_page (vcpu, unsync_sp); + ++vcpu->kvm->stat.mmu_cache_miss; sp =kvm_mmu_alloc_page (vcpu, parent_pte);if (!sp)
Signature · 1 line
1.6.1.2
PATCH v4 3/9 KVM MMU: allow more page become unsync at gfn mapping time
In reply to a message not in the archiveIn current code, shadow page can become asynchronous only if one shadow page for a gfn, this rule is too strict, in fact, we can let all last mapping page(i.e, it's the pte page) become unsync, and sync them at invlpg or flush tlb time.
This patch allow more page become asynchronous at gfn mapping time
--- arch/x86/kvm/mmu.c | 81 +++++++++++++++++++++++---------------------------- 1 files changed, 37 insertions(+), 44 deletions(-)
arch/x86/kvm/mmu.c +37 -44
@@ -1166,26 +1166,6 @@ static int mmu_unsync_walk(struct kvm_mmu_page *sp,return __mmu_unsync_walk (sp, pvec); } -static struct kvm_mmu_page *kvm_mmu_lookup_page (struct kvm *kvm,gfn_t gfn) -{ -unsigned index; -struct hlist_head *bucket; -struct kvm_mmu_page *sp; -struct hlist_node *node; - -pgprintk ("%s: looking for gfn %lx\n" , __func__, gfn); - index =kvm_page_table_hashfn (gfn); - bucket = &kvm->arch.mmu_page_hash[index]; -hlist_for_each_entry (sp, node, bucket, hash_link) -if (sp->gfn == gfn && !sp->role.direct - && !sp->role.invalid) { -pgprintk ("%s: found role %x\n" , - __func__, sp->role.word); -return sp; - } -return NULL ; -} -static void kvm_unlink_unsync_page (struct kvm *kvm,struct kvm_mmu_page *sp) {WARN_ON (!sp->unsync);
@@ -1753,47 +1733,60 @@ u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) }EXPORT_SYMBOL_GPL (kvm_get_guest_memory_type); -static int kvm_unsync_page (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) +static void __kvm_unsync_page (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) +{ +trace_kvm_mmu_unsync_page (sp); + ++vcpu->kvm->stat.mmu_unsync; + sp->unsync =1 ; + +kvm_mmu_mark_parents_unsync (sp); +mmu_convert_notrap (sp); +} + +static void kvm_unsync_pages (struct kvm_vcpu *vcpu,gfn_t gfn) { -unsigned index;struct hlist_head *bucket;struct kvm_mmu_page *s;struct hlist_node *node, *n; +unsigned index; - index =kvm_page_table_hashfn (sp->gfn); + index =kvm_page_table_hashfn (gfn); bucket = &vcpu->kvm->arch.mmu_page_hash[index]; -/* don't unsync if pagetable is shadowed with multiple roles */ +hlist_for_each_entry_safe (s, node, n, bucket, hash_link) { -if (s->gfn != sp->gfn || s->role.direct) +if (s->gfn != gfn || s->role.direct || s->unsync)continue ; -if (s->role.word != sp->role.word) -return 1 ; +WARN_ON (s->role.level !=PT_PAGE_TABLE_LEVEL ); +__kvm_unsync_page (vcpu, s); } -trace_kvm_mmu_unsync_page (sp); - ++vcpu->kvm->stat.mmu_unsync; - sp->unsync =1 ; - -kvm_mmu_mark_parents_unsync (sp); - -mmu_convert_notrap (sp); -return 0 ; }static int mmu_need_write_protect (struct kvm_vcpu *vcpu,gfn_t gfn,bool can_unsync) { -struct kvm_mmu_page *shadow; +unsigned index; +struct hlist_head *bucket; +struct kvm_mmu_page *s; +struct hlist_node *node, *n; +bool need_unsync = false; + + index =kvm_page_table_hashfn (gfn); + bucket = &vcpu->kvm->arch.mmu_page_hash[index]; +hlist_for_each_entry_safe (s, node, n, bucket, hash_link) { +if (s->gfn != gfn || s->role.direct) +continue ; - shadow =kvm_mmu_lookup_page (vcpu->kvm, gfn); -if (shadow) { -if (shadow->role.level !=PT_PAGE_TABLE_LEVEL ) +if (s->role.level !=PT_PAGE_TABLE_LEVEL )return 1 ; -if (shadow->unsync) -return 0 ; -if (can_unsync && oos_shadow) -return kvm_unsync_page (vcpu, shadow); -return 1 ; + +if (!need_unsync && !s->unsync) { +if (!can_unsync || !oos_shadow) +return 1 ; + need_unsync = true; + } } +if (need_unsync) +kvm_unsync_pages (vcpu, gfn);return 0 ; }
Signature · 1 line
1.6.1.2
PATCH v4 4/9 KVM MMU: allow more page become unsync at getting sp time
In reply to a message not in the archiveAllow more page become asynchronous at getting sp time, if need create new shadow page for gfn but it not allow unsync(level > 1), we should unsync all gfn's unsync page
--- arch/x86/kvm/mmu.c | 47 +++++++++++++++++++++++++++++++++++++---------- 1 files changed, 37 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu.c +37 -10
@@ -1215,6 +1215,35 @@ static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)return __kvm_sync_page (vcpu, sp, true); } +/* @gfn should be write-protected at the call site */ +static void kvm_sync_pages (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +struct hlist_head *bucket; +struct kvm_mmu_page *s; +struct hlist_node *node, *n; +unsigned index; +bool flush = false; + + index =kvm_page_table_hashfn (gfn); + bucket = &vcpu->kvm->arch.mmu_page_hash[index]; +hlist_for_each_entry_safe (s, node, n, bucket, hash_link) { +if (s->gfn != gfn || !s->unsync) +continue ; + +WARN_ON (s->role.level !=PT_PAGE_TABLE_LEVEL ); +if ((s->role.cr4_pae != !!is_pae (vcpu)) || + (vcpu->arch.mmu.sync_page (vcpu, s))) { +kvm_mmu_zap_page (vcpu->kvm, s); +continue ; + } +kvm_unlink_unsync_page (vcpu->kvm, s); + flush = true; + } + +if (flush) +kvm_mmu_flush_tlb (vcpu); +} +struct mmu_page_path {struct kvm_mmu_page *parent[PT64_ROOT_LEVEL -1 ];unsigned int idx[PT64_ROOT_LEVEL -1 ];
@@ -1313,8 +1342,9 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,unsigned index;unsigned quadrant;struct hlist_head *bucket; -struct kvm_mmu_page *sp, *unsync_sp =NULL ; +struct kvm_mmu_page *sp;struct hlist_node *node, *tmp; +bool need_sync = false; role = vcpu->arch.mmu.base_role; role.level = level;
@@ -1331,17 +1361,14 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, bucket = &vcpu->kvm->arch.mmu_page_hash[index];hlist_for_each_entry_safe (sp, node, tmp, bucket, hash_link)if (sp->gfn == gfn) { -if (sp->unsync) - unsync_sp = sp; +if (!need_sync && sp->unsync) + need_sync = true;if (sp->role.word != role.word)continue ; -if (!direct && unsync_sp && -kvm_sync_page_transient (vcpu, unsync_sp)) { - unsync_sp =NULL ; +if (sp->unsync &&kvm_sync_page_transient (vcpu, sp))break ; - }mmu_page_add_parent_pte (vcpu, sp, parent_pte);if (sp->unsync_children) {
@@ -1353,9 +1380,6 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,trace_kvm_mmu_get_page (sp, false);return sp; } -if (!direct && unsync_sp) -kvm_sync_page (vcpu, unsync_sp); - ++vcpu->kvm->stat.mmu_cache_miss; sp =kvm_mmu_alloc_page (vcpu, parent_pte);if (!sp)
@@ -1366,6 +1390,9 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,if (!direct) {if (rmap_write_protect (vcpu->kvm, gfn))kvm_flush_remote_tlbs (vcpu->kvm); +if (level >PT_PAGE_TABLE_LEVEL && need_sync) +kvm_sync_pages (vcpu, gfn); +account_shadowed (vcpu->kvm, gfn); }if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
Signature · 1 line
1.6.1.2
PATCH v4 5/9 KVM MMU: rename 'root_count' to 'active_count'
In reply to a message not in the archiveRename 'root_count' to 'active_count' in kvm_mmu_page, since the unsync pages also will use it in later patch
--- arch/x86/include/asm/kvm_host.h | 7 ++++++- arch/x86/kvm/mmu.c | 14 +++++++------- arch/x86/kvm/mmutrace.h | 6 +++--- 3 files changed, 16 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -1
@@ -203,7 +203,12 @@ struct kvm_mmu_page {DECLARE_BITMAP (slot_bitmap,KVM_MEMORY_SLOTS +KVM_PRIVATE_MEM_SLOTS );bool multimapped;/* More than one parent_pte? */ bool unsync; -int root_count;/* Currently serving as active root */ +/* +* if active_count > 0, it means that this page is not freed +* immediately, it's used by active root and unsync pages which +* out of kvm->mmu_lock's protection currently. +*/ +int active_count;unsigned int unsync_children;union {u64 *parent_pte;/* !multimapped */
arch/x86/kvm/mmu.c +7 -7
@@ -1539,7 +1539,7 @@ static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)unaccount_shadowed (kvm, sp->gfn);if (sp->unsync)kvm_unlink_unsync_page (kvm, sp); -if (!sp->root_count) { +if (!sp->active_count) {hlist_del (&sp->hash_link);kvm_mmu_free_page (kvm, sp); }else {
@@ -2060,8 +2060,8 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)hpa_t root = vcpu->arch.mmu.root_hpa; sp =page_header (root); - --sp->root_count; -if (!sp->root_count && sp->role.invalid) + --sp->active_count; +if (!sp->active_count && sp->role.invalid)kvm_mmu_zap_page (vcpu->kvm, sp); vcpu->arch.mmu.root_hpa =INVALID_PAGE ;spin_unlock (&vcpu->kvm->mmu_lock);
@@ -2073,8 +2073,8 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)if (root) { root &=PT64_BASE_ADDR_MASK ; sp =page_header (root); - --sp->root_count; -if (!sp->root_count && sp->role.invalid) + --sp->active_count; +if (!sp->active_count && sp->role.invalid)kvm_mmu_zap_page (vcpu->kvm, sp); } vcpu->arch.mmu.pae_root[i] =INVALID_PAGE ;
@@ -2120,7 +2120,7 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)PT64_ROOT_LEVEL , direct, ACC_ALL, NULL); root =__pa (sp->spt); - ++sp->root_count; + ++sp->active_count;spin_unlock (&vcpu->kvm->mmu_lock); vcpu->arch.mmu.root_hpa = root;return 0 ;
@@ -2150,7 +2150,7 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)PT32_ROOT_LEVEL , direct, ACC_ALL, NULL); root =__pa (sp->spt); - ++sp->root_count; + ++sp->active_count;spin_unlock (&vcpu->kvm->mmu_lock); vcpu->arch.mmu.pae_root[i] = root |PT_PRESENT_MASK ;
arch/x86/kvm/mmutrace.h +3 -3
@@ -10,13 +10,13 @@#define KVM_MMU_PAGE_FIELDS \ __field(__u64, gfn) \ __field(__u32, role) \ - __field(__u32, root_count) \ + __field(__u32, active_count) \ __field(bool, unsync)#define KVM_MMU_PAGE_ASSIGN (sp) \ __entry->gfn = sp->gfn; \ __entry->role = sp->role.word; \ - __entry->root_count = sp->root_count; \ + __entry->active_count = sp->active_count; \ __entry->unsync = sp->unsync;#define KVM_MMU_PAGE_PRINTK () ({ \
@@ -37,7 +37,7 @@ access_str[role.access], \ role.invalid ?" invalid" :"" , \ role.nxe ?"" :"!" , \ - __entry->root_count, \ + __entry->active_count, \ __entry->unsync ? "unsync" : "sync", 0); \ ret; \ })
Signature · 1 line
1.6.1.2
PATCH v5 5/9 KVM MMU: rename 'root_count' to 'active_count'
Rename 'root_count' to 'active_count' in kvm_mmu_page, since the unsync pages also will use it in later patch
--- arch/x86/include/asm/kvm_host.h | 8 +++++++- arch/x86/kvm/mmu.c | 14 +++++++------- arch/x86/kvm/mmutrace.h | 6 +++--- 3 files changed, 17 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -1
@@ -203,7 +203,13 @@ struct kvm_mmu_page {DECLARE_BITMAP (slot_bitmap,KVM_MEMORY_SLOTS +KVM_PRIVATE_MEM_SLOTS );bool multimapped;/* More than one parent_pte? */ bool unsync; -int root_count;/* Currently serving as active root */ +/* +* if active_count > 0, it means that this page is not freed +* immediately, it's used by active root and unsync pages which +* out of kvm->mmu_lock's protection currently. +*/ +int active_count; +unsigned int unsync_children;union {u64 *parent_pte;/* !multimapped */
arch/x86/kvm/mmu.c +7 -7
@@ -1539,7 +1539,7 @@ static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)unaccount_shadowed (kvm, sp->gfn);if (sp->unsync)kvm_unlink_unsync_page (kvm, sp); -if (!sp->root_count) { +if (!sp->active_count) {/* Count self */ ret++;hlist_del (&sp->hash_link);
@@ -2061,8 +2061,8 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)hpa_t root = vcpu->arch.mmu.root_hpa; sp =page_header (root); - --sp->root_count; -if (!sp->root_count && sp->role.invalid) + --sp->active_count; +if (!sp->active_count && sp->role.invalid)kvm_mmu_zap_page (vcpu->kvm, sp); vcpu->arch.mmu.root_hpa =INVALID_PAGE ;spin_unlock (&vcpu->kvm->mmu_lock);
@@ -2074,8 +2074,8 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)if (root) { root &=PT64_BASE_ADDR_MASK ; sp =page_header (root); - --sp->root_count; -if (!sp->root_count && sp->role.invalid) + --sp->active_count; +if (!sp->active_count && sp->role.invalid)kvm_mmu_zap_page (vcpu->kvm, sp); } vcpu->arch.mmu.pae_root[i] =INVALID_PAGE ;
@@ -2121,7 +2121,7 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)PT64_ROOT_LEVEL , direct, ACC_ALL, NULL); root =__pa (sp->spt); - ++sp->root_count; + ++sp->active_count;spin_unlock (&vcpu->kvm->mmu_lock); vcpu->arch.mmu.root_hpa = root;return 0 ;
@@ -2151,7 +2151,7 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)PT32_ROOT_LEVEL , direct, ACC_ALL, NULL); root =__pa (sp->spt); - ++sp->root_count; + ++sp->active_count;spin_unlock (&vcpu->kvm->mmu_lock); vcpu->arch.mmu.pae_root[i] = root |PT_PRESENT_MASK ;
arch/x86/kvm/mmutrace.h +3 -3
@@ -10,13 +10,13 @@#define KVM_MMU_PAGE_FIELDS \ __field(__u64, gfn) \ __field(__u32, role) \ - __field(__u32, root_count) \ + __field(__u32, active_count) \ __field(bool, unsync)#define KVM_MMU_PAGE_ASSIGN (sp) \ __entry->gfn = sp->gfn; \ __entry->role = sp->role.word; \ - __entry->root_count = sp->root_count; \ + __entry->active_count = sp->active_count; \ __entry->unsync = sp->unsync;#define KVM_MMU_PAGE_PRINTK () ({ \
@@ -37,7 +37,7 @@ access_str[role.access], \ role.invalid ?" invalid" :"" , \ role.nxe ?"" :"!" , \ - __entry->root_count, \ + __entry->active_count, \ __entry->unsync ? "unsync" : "sync", 0); \ ret; \ })
Signature · 1 line
1.6.1.2
PATCH v4 6/9 KVM MMU: support keeping sp live while it's out of protection
In reply to a message not in the archiveIf we want to keep sp live while it it's out of kvm->mmu_lock protection, we can increase sp->active_count.
Then, the invalid page is not only for active root but also unsync sp, we should filter those out when we make a page to unsync.
And move 'hlist_del(&sp->hash_link)' into kvm_mmu_free_page() then we can free the invalid unsync page to call kvm_mmu_free_page directly.
--- arch/x86/kvm/mmu.c | 15 +++++++++------ 1 files changed, 9 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu.c +9 -6
@@ -894,6 +894,7 @@ static int is_empty_shadow_page(u64 *spt)static void kvm_mmu_free_page (struct kvm *kvm,struct kvm_mmu_page *sp) {ASSERT (is_empty_shadow_page (sp->spt)); +hlist_del (&sp->hash_link);list_del (&sp->link);__free_page (virt_to_page (sp->spt));__free_page (virt_to_page (sp->gfns));
@@ -1539,13 +1540,14 @@ static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)unaccount_shadowed (kvm, sp->gfn);if (sp->unsync)kvm_unlink_unsync_page (kvm, sp); -if (!sp->active_count) { -hlist_del (&sp->hash_link); +if (!sp->active_count)kvm_mmu_free_page (kvm, sp); - }else { +else { sp->role.invalid =1 ;list_move (&sp->link, &kvm->arch.active_mmu_pages); -kvm_reload_remote_mmus (kvm); +/* No need reload mmu if it's unsync page zapped */ +if (sp->role.level !=PT_PAGE_TABLE_LEVEL ) +kvm_reload_remote_mmus (kvm); }kvm_mmu_reset_last_pte_updated (kvm);return ret;
@@ -1781,7 +1783,8 @@ static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) bucket = &vcpu->kvm->arch.mmu_page_hash[index];hlist_for_each_entry_safe (s, node, n, bucket, hash_link) { -if (s->gfn != gfn || s->role.direct || s->unsync) +if (s->gfn != gfn || s->role.direct || s->unsync || + s->role.invalid)continue ;WARN_ON (s->role.level !=PT_PAGE_TABLE_LEVEL );__kvm_unsync_page (vcpu, s);
@@ -1806,7 +1809,7 @@ static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,if (s->role.level !=PT_PAGE_TABLE_LEVEL )return 1 ; -if (!need_unsync && !s->unsync) { +if (!need_unsync && !s->unsync && !s->role.invalid) {if (!can_unsync || !oos_shadow)return 1 ; need_unsync = true;
Signature · 1 line
1.6.1.2
PATCH v5 6/9 KVM MMU: support keeping sp live while it's out of protection
If we want to keep sp live while it it's out of kvm->mmu_lock protection, we can increase sp->active_count.
Then, the invalid page is not only for active root but also unsync sp, we should filter those out when we make a page to unsync.
And move 'hlist_del(&sp->hash_link)' into kvm_mmu_free_page() then we can free the invalid unsync page to call kvm_mmu_free_page directly.
--- arch/x86/kvm/mmu.c | 11 +++++++---- 1 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu.c +7 -4
@@ -894,6 +894,7 @@ static int is_empty_shadow_page(u64 *spt)static void kvm_mmu_free_page (struct kvm *kvm,struct kvm_mmu_page *sp) {ASSERT (is_empty_shadow_page (sp->spt)); +hlist_del (&sp->hash_link);list_del (&sp->link);__free_page (virt_to_page (sp->spt));__free_page (virt_to_page (sp->gfns));
@@ -1542,12 +1543,13 @@ static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)if (!sp->active_count) {/* Count self */ ret++; -hlist_del (&sp->hash_link);kvm_mmu_free_page (kvm, sp); }else { sp->role.invalid =1 ;list_move (&sp->link, &kvm->arch.active_mmu_pages); -kvm_reload_remote_mmus (kvm); +/* No need reload mmu if it's unsync page zapped */ +if (sp->role.level !=PT_PAGE_TABLE_LEVEL ) +kvm_reload_remote_mmus (kvm); }kvm_mmu_reset_last_pte_updated (kvm);return ret;
@@ -1782,7 +1784,8 @@ static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) bucket = &vcpu->kvm->arch.mmu_page_hash[index];hlist_for_each_entry_safe (s, node, n, bucket, hash_link) { -if (s->gfn != gfn || s->role.direct || s->unsync) +if (s->gfn != gfn || s->role.direct || s->unsync || + s->role.invalid)continue ;WARN_ON (s->role.level !=PT_PAGE_TABLE_LEVEL );__kvm_unsync_page (vcpu, s);
@@ -1807,7 +1810,7 @@ static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,if (s->role.level !=PT_PAGE_TABLE_LEVEL )return 1 ; -if (!need_unsync && !s->unsync) { +if (!need_unsync && !s->unsync && !s->role.invalid) {if (!can_unsync || !oos_shadow)return 1 ; need_unsync = true;
Signature · 1 line
1.6.1.2
PATCH v4 7/9 KVM MMU: separate invlpg code form kvm_mmu_pte_write()
In reply to a message not in the archiveLet invlpg not depends on kvm_mmu_pte_write path, later patch will need this feature
--- arch/x86/kvm/mmu.c | 22 +++++++++++++--------- arch/x86/kvm/paging_tmpl.h | 44 +++++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 22 deletions(-)
arch/x86/kvm/mmu.c +13 -9
@@ -2318,6 +2318,10 @@ static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1 ]) !=0 ; } +static void mmu_guess_page_from_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, +u64 gpte); +static void mmu_release_page_from_pte_write (struct kvm_vcpu *vcpu); +#define PTTYPE 64#include "paging_tmpl.h" #undef PTTYPE
@@ -2631,6 +2635,14 @@ static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, vcpu->arch.update_pte.pfn = pfn; } +static void mmu_release_page_from_pte_write (struct kvm_vcpu *vcpu) +{ +if (!is_error_pfn (vcpu->arch.update_pte.pfn)) { +kvm_release_pfn_clean (vcpu->arch.update_pte.pfn); + vcpu->arch.update_pte.pfn = bad_pfn; + } +} +static void kvm_mmu_access_page (struct kvm_vcpu *vcpu,gfn_t gfn) {u64 *spte = vcpu->arch.last_pte_updated;
@@ -2663,12 +2675,9 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,int flooded =0 ;int npte;int r; -int invlpg_counter;pgprintk ("%s: gpa %llx bytes %d\n" , __func__, gpa, bytes); - invlpg_counter =atomic_read (&vcpu->kvm->arch.invlpg_counter); -/* * Assume that the pte write on a page table of the same type * as the current vcpu paging mode. This is nearly always true
@@ -2701,8 +2710,6 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,mmu_guess_page_from_pte_write (vcpu, gpa, gentry);spin_lock (&vcpu->kvm->mmu_lock); -if (atomic_read (&vcpu->kvm->arch.invlpg_counter) != invlpg_counter) - gentry =0 ;kvm_mmu_access_page (vcpu, gfn);kvm_mmu_free_some_pages (vcpu); ++vcpu->kvm->stat.mmu_pte_write;
@@ -2779,10 +2786,7 @@ restart: }kvm_mmu_audit (vcpu,"post pte write" );spin_unlock (&vcpu->kvm->mmu_lock); -if (!is_error_pfn (vcpu->arch.update_pte.pfn)) { -kvm_release_pfn_clean (vcpu->arch.update_pte.pfn); - vcpu->arch.update_pte.pfn = bad_pfn; - } +mmu_release_page_from_pte_write (vcpu); }int kvm_mmu_unprotect_page_virt (struct kvm_vcpu *vcpu,gva_t gva)
arch/x86/kvm/paging_tmpl.h +31 -13
79 lines not on this page · raw message
Signature · 1 line
1.6.1.2
PATCH v4 8/9 KVM MMU: no need atomic operation for 'invlpg_counter'
In reply to a message not in the archive'invlpg_counter' is protected by 'kvm->mmu_lock', no need atomic operation anymore
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/paging_tmpl.h | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -387,7 +387,7 @@ struct kvm_arch {unsigned int n_free_mmu_pages;unsigned int n_requested_mmu_pages;unsigned int n_alloc_mmu_pages; -atomic_t invlpg_counter; +unsigned int invlpg_counter;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];/* * Hash table of struct kvm_mmu_page.
arch/x86/kvm/paging_tmpl.h +4 -3
@@ -466,7 +466,8 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)struct kvm_shadow_walk_iterator iterator;gfn_t gfn =-1 ;u64 *sptep =NULL , gentry; -int invlpg_counter, level, offset =0 , need_flush =0 ; +unsigned int invlpg_counter; +int level, offset =0 , need_flush =0 ;spin_lock (&vcpu->kvm->mmu_lock);
@@ -502,7 +503,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)if (need_flush)kvm_flush_remote_tlbs (vcpu->kvm); - invlpg_counter =atomic_add_return (1 , &vcpu->kvm->arch.invlpg_counter); + invlpg_counter = ++vcpu->kvm->arch.invlpg_counter;spin_unlock (&vcpu->kvm->mmu_lock);if (gfn ==-1 )
@@ -522,7 +523,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)kvm_mmu_free_page (vcpu->kvm, sp); goto unlock_exit; } -if (atomic_read (&vcpu->kvm->arch.invlpg_counter) == invlpg_counter && +if (vcpu->kvm->arch.invlpg_counter == invlpg_counter && sp->role.level ==PT_PAGE_TABLE_LEVEL ) { ++vcpu->kvm->stat.mmu_pte_updated;FNAME (update_pte)(vcpu, sp, sptep, &gentry);
Signature · 1 line
1.6.1.2
PATCH v4 9/9 KVM MMU: optimize sync/update unsync-page
In reply to a message not in the archiveinvlpg only need update unsync page, sp->unsync and sp->unsync_children can help us to find it
Now, a gfn may have many shadow pages, when one sp need be synced, we write protect sp->gfn and sync this sp but we keep other shadow pages asynchronous
So, while gfn happen page fault, let it not touch unsync page, the unsync page only updated at invlpg/flush TLB time
--- arch/x86/kvm/mmu.c | 3 ++- arch/x86/kvm/paging_tmpl.h | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu.c +2 -1
@@ -2731,7 +2731,8 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, restart:hlist_for_each_entry_safe (sp, node, n, bucket, hash_link) { -if (sp->gfn != gfn || sp->role.direct || sp->role.invalid) +if (sp->gfn != gfn || sp->role.direct || sp->role.invalid || + sp->unsync)continue ; pte_size = sp->role.cr4_pae ?8 :4 ; misaligned = (offset ^ (offset + bytes -1 )) & ~(pte_size -1 );
arch/x86/kvm/paging_tmpl.h +8 -4
@@ -475,10 +475,15 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva) level = iterator.level; sptep = iterator.sptep; + sp =page_header (__pa (sptep));if (is_last_spte (*sptep, level)) {int shift; - sp =page_header (__pa (sptep)); +if (!sp->unsync) +break ; + +WARN_ON (level !=PT_PAGE_TABLE_LEVEL ); + shift =PAGE_SHIFT - (PT_LEVEL_BITS -PT64_LEVEL_BITS ) * level; gfn = sp->gfn;
@@ -496,7 +501,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)break ; } -if (!is_shadow_present_pte (*sptep)) +if (!is_shadow_present_pte (*sptep) || !sp->unsync_children)break ; }
@@ -523,8 +528,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)kvm_mmu_free_page (vcpu->kvm, sp); goto unlock_exit; } -if (vcpu->kvm->arch.invlpg_counter == invlpg_counter && - sp->role.level ==PT_PAGE_TABLE_LEVEL ) { +if (vcpu->kvm->arch.invlpg_counter == invlpg_counter) { ++vcpu->kvm->stat.mmu_pte_updated;FNAME (update_pte)(vcpu, sp, sptep, &gentry); }
Signature · 1 line
1.6.1.2