KVM: arm64: nv: Implement nested stage-2 reverse map
v3 cover KVM: arm64: nv: Implement nested stage-2 reverse map
Hi,
This is v3 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
Two new preparatory patches are added, one reduces a hole in kvm_s2_mmu and another refactors the code a bit during s2 faults. Other changes are listed below.
* Changes from v2 [1]:
- Removed "polluted" teminology.
- Use xa_{mk, to}_value() when storing and retriving values from maple trees.
- Avoid using the 63rd bit in maple tree values so that xa_{mk, to}_value() does not lose us a bit.
- Added reverse map removal during TLBI handling.
- Other suggested refactorings.
Thanks!
[1]: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (5):
KVM: arm64: Use a variable for the canonical GPA in kvm_s2_fault_map()
KVM: arm64: Move shadow_pt_debugfs_dentry to reduce holes in
kvm_s2_mmu
KVM: arm64: nv: Avoid full shadow s2 unmap
KVM: arm64: nv: Remove reverse map entries during TLBI handling
KVM: arm64: nv: Create nested IPA direct map to speed up reverse map
removal
arch/arm64/include/asm/kvm_host.h | 17 +- arch/arm64/include/asm/kvm_nested.h | 6 + arch/arm64/kvm/mmu.c | 43 +++-- arch/arm64/kvm/nested.c | 238 +++++++++++++++++++++++++++- arch/arm64/kvm/sys_regs.c | 3 + 5 files changed, 290 insertions(+), 17 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/5 KVM: arm64: Use a variable for the canonical GPA in kvm_s2_fault_map()
Create a variable to store the canonical GPA, instead of calculating it when needed. This will be useful when we need to use the canonical GPA for the nested reverse map later.
--- arch/arm64/kvm/mmu.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
arch/arm64/kvm/mmu.c +6 -5
@@ -1981,6 +1981,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,long mapping_size;kvm_pfn_t pfn;gfn_t gfn; +phys_addr_t canonical_gpa;int ret;kvm_fault_lock (kvm);
@@ -1994,6 +1995,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, mapping_size = s2vi->vma_pagesize; pfn = s2vi->pfn; gfn = s2vi->gfn; + canonical_gpa =gfn_to_gpa (get_canonical_gfn (s2fd, s2vi));/* * If we are not forced to use page mapping, check if we are
@@ -2012,6 +2014,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, goto out_unlock; } } + canonical_gpa =ALIGN_DOWN (canonical_gpa, mapping_size); }if (!perm_fault_granule && !s2vi->map_non_cacheable &&kvm_has_mte (kvm))
@@ -2045,11 +2048,9 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,* making sure we adjust the canonical IPA if the mapping size has * been updated (via a THP upgrade, for example). */ -if (writable && !ret) { -phys_addr_t ipa =gfn_to_gpa (get_canonical_gfn (s2fd, s2vi)); - ipa &= ~(mapping_size -1 ); -mark_page_dirty_in_slot (kvm, s2fd->memslot,gpa_to_gfn (ipa)); - } +if (writable && !ret) +mark_page_dirty_in_slot (kvm, s2fd->memslot, +gpa_to_gfn (canonical_gpa));if (ret != -EAGAIN )return ret;
Signature · 1 line
2.43.0
PATCH v3 2/5 KVM: arm64: Move shadow_pt_debugfs_dentry to reduce holes in kvm_s2_mmu
In reply to Wei-Lin Changdentry pointer shadow_pt_debugfs_dentry was placed between two booleans in kvm_s2_mmu, which created unnecessary holes in the struct. Move it so the two booleans connect.
--- arch/arm64/include/asm/kvm_host.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +4 -4
@@ -217,16 +217,16 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; -#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS -struct dentry *shadow_pt_debugfs_dentry; -#endif -/* * true when this MMU needs to be unmapped before being used for a new * purpose. */ bool pending_unmap; +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +struct dentry *shadow_pt_debugfs_dentry; +#endif +/* * 0: Nobody is currently using this, check vttbr for validity * >0: Somebody is actively using this.
Signature · 1 line
2.43.0
PATCH v3 3/5 KVM: arm64: nv: Avoid full shadow s2 unmap
In reply to Wei-Lin ChangCurrently we are forced to fully unmap all shadow stage-2 for a VM when unmapping a page from the canonical stage-2, for example during an MMU notifier call. This is because we are not tracking what canonical IPA are mapped in the shadow stage-2 page tables hence there is no way to know what to unmap.
Create a per kvm_s2_mmu maple tree to track canonical IPA range -> nested IPA range, so that it is possible to partially unmap shadow stage-2 when a canonical IPA range is unmapped. The algorithm is simple and conservative:
At each shadow stage-2 map, insert the nested IPA range into the maple tree, with the canonical IPA range as the key. If the canonical IPA range doesn't overlap with existing ranges in the tree, insert as is, and a reverse mapping for this range is established. But if the canonical IPA range overlaps with any existing ranges in the tree, create a new range that spans all the overlapping ranges including the input range and replace those existing ranges. In the mean time, mark this new spanning canonical IPA range with an "UNKNOWN_IPA" bit, indicating we give up tracking the nested IPA ranges that map to this canonical IPA range.
The maple tree's 64 bit entry is enough to store the nested IPA and the UNKNOWN_IPA status, therefore besides maple tree's internal operation, memory allocation is avoided.
Example:
|||| means existing range, ---- means empty range
input: $$$$$$$$$$$$$$$$$$$$$$$$$$ tree: --||||-----|||||||---------||||||||||-----------
insert spanning range and replace overlapping ones:
--||||-----||||||||||||||||||||||||||-----------
^^^^marked UNKNOWN_IPA^^^^With the reverse map created, when a canonical IPA range gets unmapped, look into each s2 mmu's maple tree and look for canonical IPA ranges affected, and base on their UNKNOWN_IPA status:
UNKNOWN_IPA -> fall back and fully unmap the current shadow
stage-2, also clear the tree
not UNKNOWN_IPA -> unmap the nested IPA range, and remove the reverse
map entry
--- arch/arm64/include/asm/kvm_host.h | 4 + arch/arm64/include/asm/kvm_nested.h | 4 + arch/arm64/kvm/mmu.c | 27 ++++-- arch/arm64/kvm/nested.c | 140 +++++++++++++++++++++++++++- 4 files changed, 167 insertions(+), 8 deletions(-)
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -223,6 +223,10 @@ struct kvm_s2_mmu {*/ bool pending_unmap; +bool nested_revmap_broken; +/* canonical IPA to nested IPA range lookup */ +struct maple_tree nested_revmap_mt; +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS struct dentry *shadow_pt_debugfs_dentry;#endif
arch/arm64/include/asm/kvm_nested.h +4 -0
@@ -76,6 +76,8 @@ extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,const union tlbi_info *info, void (*)(struct kvm_s2_mmu *, const union tlbi_info *)); +extern void kvm_record_nested_revmap (gpa_t gpa,struct kvm_s2_mmu *mmu, +gpa_t fault_ipa,size_t map_size);extern void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu);extern void kvm_vcpu_put_hw_mmu (struct kvm_vcpu *vcpu);
@@ -164,6 +166,8 @@ extern int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu, struct kvm_s2_trans *trans);extern int kvm_inject_s2_fault (struct kvm_vcpu *vcpu,u64 esr_el2);extern void kvm_nested_s2_wp (struct kvm *kvm); +extern void kvm_unmap_gfn_range_nested (struct kvm *kvm,gpa_t gpa,size_t size, +bool may_block);extern void kvm_nested_s2_unmap (struct kvm *kvm,bool may_block);extern void kvm_nested_s2_flush (struct kvm *kvm);
arch/arm64/kvm/mmu.c +21 -6
@@ -5,6 +5,7 @@*/ #include <linux/acpi.h> +#include <linux/maple_tree.h> #include <linux/mman.h> #include <linux/kvm_host.h> #include <linux/io.h>
@@ -1099,6 +1100,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_pgtable *pgt =NULL ; +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt;write_lock (&kvm->mmu_lock); pgt = mmu->pgt;
@@ -1108,8 +1110,11 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)free_percpu (mmu->last_vcpu_ran); } -if (kvm_is_nested_s2_mmu (kvm, mmu)) +if (kvm_is_nested_s2_mmu (kvm, mmu)) { +if (!mtree_empty (revmap_mt)) +mtree_destroy (revmap_mt);kvm_init_nested_s2_mmu (mmu); + }write_unlock (&kvm->mmu_lock);
@@ -1631,6 +1636,10 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) goto out_unlock; } +if (s2fd->nested) +kvm_record_nested_revmap (gfn <<PAGE_SHIFT , pgt->mmu, + s2fd->fault_ipa,PAGE_SIZE ); + ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt, s2fd->fault_ipa,PAGE_SIZE ,__pfn_to_phys (pfn), prot, memcache, flags);
@@ -2034,6 +2043,10 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, ret =KVM_PGT_FN (kvm_pgtable_stage2_relax_perms)(pgt,gfn_to_gpa (gfn), prot, flags); }else { +if (s2fd->nested) +kvm_record_nested_revmap (canonical_gpa, pgt->mmu, +gfn_to_gpa (gfn), mapping_size); + ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt,gfn_to_gpa (gfn), mapping_size,__pfn_to_phys (pfn), prot, memcache, flags);
@@ -2389,14 +2402,16 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) { +gpa_t gpa = range->start <<PAGE_SHIFT ; +size_t size = (range->end - range->start) <<PAGE_SHIFT ; +bool may_block = range->may_block; +if (!kvm->arch.mmu.pgt ||kvm_vm_is_protected (kvm))return false; -__unmap_stage2_range (&kvm->arch.mmu, range->start <<PAGE_SHIFT , - (range->end - range->start) <<PAGE_SHIFT , - range->may_block); +__unmap_stage2_range (&kvm->arch.mmu, gpa, size, may_block); +kvm_unmap_gfn_range_nested (kvm, gpa, size, may_block); -kvm_nested_s2_unmap (kvm, range->may_block);return false; }
@@ -2674,7 +2689,7 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,write_lock (&kvm->mmu_lock);kvm_stage2_unmap_range (&kvm->arch.mmu, gpa, size, true); -kvm_nested_s2_unmap (kvm, true); +kvm_unmap_gfn_range_nested (kvm, gpa, size, true);write_unlock (&kvm->mmu_lock); }
arch/arm64/kvm/nested.c +138 -2
182 lines not on this page · raw message
Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v3 3/5] KVM: arm64: nv: Avoid full shadow s2 unmap
Quoted 153 lines · Wei-Lin Chang
Currently we are forced to fully unmap all shadow stage-2 for a VM when unmapping a page from the canonical stage-2, for example during an MMU notifier call. This is because we are not tracking what canonical IPA are mapped in the shadow stage-2 page tables hence there is no way to know what to unmap.
Create a per
kvm_s2_mmumaple tree to track canonical IPA range -> nested IPA range, so that it is possible to partially unmap shadow stage-2 when a canonical IPA range is unmapped. The algorithm is simple and conservative:At each shadow stage-2 map, insert the nested IPA range into the maple tree, with the canonical IPA range as the key. If the canonical IPA range doesn't overlap with existing ranges in the tree, insert as is, and a reverse mapping for this range is established. But if the canonical IPA range overlaps with any existing ranges in the tree, create a new range that spans all the overlapping ranges including the input range and replace those existing ranges. In the mean time, mark this new spanning canonical IPA range with an "
UNKNOWN_IPA" bit, indicating we give up tracking the nested IPA ranges that map to this canonical IPA range.The maple tree's 64 bit entry is enough to store the nested IPA and the
UNKNOWN_IPAstatus, therefore besides maple tree's internal operation, memory allocation is avoided.Example:
|||| means existing range, ---- means empty rangeinput: $$$$$$$$$$$$$$$$$$$$$$$$$$ tree: --||||-----|||||||---------||||||||||-----------insert spanning range and replace overlapping ones:
--||||-----||||||||||||||||||||||||||----------- ^^^^marked UNKNOWN_IPA^^^^With the reverse map created, when a canonical IPA range gets unmapped, look into each s2 mmu's maple tree and look for canonical IPA ranges affected, and base on their
UNKNOWN_IPAstatus:
UNKNOWN_IPA-> fall back and fully unmap the current shadow
stage-2, also clear the treenot
UNKNOWN_IPA-> unmap the nested IPA range, and remove the reverse
map entrySuggested-by Marc Zyngier <[email protected]>Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 4 + arch/arm64/include/asm/kvm_nested.h | 4 + arch/arm64/kvm/mmu.c | 27 ++++-- arch/arm64/kvm/nested.c | 140 +++++++++++++++++++++++++++- 4 files changed, 167 insertions(+), 8 deletions(-)arch/arm64/include/asm/kvm_host.h +4
-0@@ -223,6 +223,10 @@ struct kvm_s2_mmu {*/ bool pending_unmap; +bool nested_revmap_broken; +/* canonical IPA to nested IPA range lookup */ +struct maple_tree nested_revmap_mt; +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS struct dentry *shadow_pt_debugfs_dentry;#endif arch/arm64/include/asm/kvm_nested.h +4
-0@@ -76,6 +76,8 @@ extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,const union tlbi_info *info, void (*)(struct kvm_s2_mmu *, const union tlbi_info *)); +extern void kvm_record_nested_revmap (gpa_t gpa,struct kvm_s2_mmu *mmu, +gpa_t fault_ipa,size_t map_size);extern void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu);extern void kvm_vcpu_put_hw_mmu (struct kvm_vcpu *vcpu);@@ -164,6 +166,8 @@ extern int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu, struct kvm_s2_trans *trans);extern int kvm_inject_s2_fault (struct kvm_vcpu *vcpu,u64 esr_el2);extern void kvm_nested_s2_wp (struct kvm *kvm); +extern void kvm_unmap_gfn_range_nested (struct kvm *kvm,gpa_t gpa,size_t size, +bool may_block);extern void kvm_nested_s2_unmap (struct kvm *kvm,bool may_block);extern void kvm_nested_s2_flush (struct kvm *kvm);arch/arm64/kvm/mmu.c +15
-1@@ -5,6 +5,7 @@*/ #include <linux/acpi.h> +#include <linux/maple_tree.h> #include <linux/mman.h> #include <linux/kvm_host.h> #include <linux/io.h> @@ -1099,6 +1100,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_pgtable *pgt =NULL ; +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt;write_lock (&kvm->mmu_lock); pgt = mmu->pgt;@@ -1108,8 +1110,11 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)free_percpu (mmu->last_vcpu_ran); } -if (kvm_is_nested_s2_mmu (kvm, mmu)) +if (kvm_is_nested_s2_mmu (kvm, mmu)) { +if (!mtree_empty (revmap_mt)) +mtree_destroy (revmap_mt);kvm_init_nested_s2_mmu (mmu); + }write_unlock (&kvm->mmu_lock);@@ -1631,6 +1636,10 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) goto out_unlock; } +if (s2fd->nested) +kvm_record_nested_revmap (gfn <<PAGE_SHIFT , pgt->mmu, + s2fd->fault_ipa,PAGE_SIZE ); + ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt, s2fd->fault_ipa,PAGE_SIZE ,__pfn_to_phys (pfn), prot, memcache, flags);@@ -2034,6 +2043,10 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, ret =KVM_PGT_FN (kvm_pgtable_stage2_relax_perms)(pgt,gfn_to_gpa (gfn), prot, flags); }else { +if (s2fd->nested) +kvm_record_nested_revmap (canonical_gpa, pgt->mmu, +gfn_to_gpa (gfn), mapping_size); + ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt,gfn_to_gpa (gfn), mapping_size,__pfn_to_phys (pfn), prot, memcache, flags);@@ -2389,14 +2402,16 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) { +gpa_t gpa = range->start <<PAGE_SHIFT ;
arch/arm64/kvm/mmu.c int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) · line 2412
Quoted 10 lines
arch/arm64/kvm/mmu.c +4
-3+size_t size = (range->end - range->start) <<PAGE_SHIFT ; +bool may_block = range->may_block; +if (!kvm->arch.mmu.pgt ||kvm_vm_is_protected (kvm))return false; -__unmap_stage2_range (&kvm->arch.mmu, range->start <<PAGE_SHIFT , - (range->end - range->start) <<PAGE_SHIFT , - range->may_block); +__unmap_stage2_range (&kvm->arch.mmu, gpa, size, may_block);
This sort of cleanups could be in a separate patch.
Quoted 49 lines
arch/arm64/kvm/mmu.c +2
-2+kvm_unmap_gfn_range_nested (kvm, gpa, size, may_block); -kvm_nested_s2_unmap (kvm, range->may_block);return false; }@@ -2674,7 +2689,7 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,write_lock (&kvm->mmu_lock);kvm_stage2_unmap_range (&kvm->arch.mmu, gpa, size, true); -kvm_nested_s2_unmap (kvm, true); +kvm_unmap_gfn_range_nested (kvm, gpa, size, true);write_unlock (&kvm->mmu_lock); }arch/arm64/kvm/nested.c +15
-0@@ -7,6 +7,7 @@#include <linux/bitfield.h> #include <linux/kvm.h> #include <linux/kvm_host.h> +#include <linux/maple_tree.h> #include <asm/fixmap.h> #include <asm/kvm_arm.h> @@ -43,6 +44,20 @@ struct vncr_tlb {*/ #define S2_MMU_PER_VCPU 2 +/* +* Per shadow S2 reverse map (IPA -> nested IPA range) maple tree payload +* layout: +* +* bit 62: valid, prevents the case where the nested IPA is 0 and turning +* the whole value to 0 +* bits 55-12: nested IPA bits 55-12 +* bit 0: UNKNOWN_IPA bit, 1 indicates we give up on tracking what nested +* IPA maps to this canonical IPA in the shadow stage-2 +*/ +#define VALID_ENTRY BIT(62) +#define ADDR_MASK GENMASK_ULL(55, 12) +#define UNKNOWN_IPA BIT(0) +void kvm_init_nested (struct kvm *kvm) { kvm->arch.nested_mmus =NULL ;
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 788
Quoted 5 lines
arch/arm64/kvm/nested.c +2
-0@@ -769,12 +784,57 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)return s2_mmu; } +void kvm_record_nested_revmap (gpa_t ipa,struct kvm_s2_mmu *mmu, +gpa_t fault_ipa,size_t map_size)
The name fault_ipa doesn't really make sense here. This is the IPA as seen from L1 (the input to the L1 S2 tables). We indeed obtain it from a fault, but that should not influence the naming here.
Similarly, 'ipa' should be qualified a bit better to reflect that this is the L0 IPA.
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 791
Quoted 3 lines
arch/arm64/kvm/nested.c +3
-0+{ +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +gpa_t ipa_end = ipa + map_size -1 ;
Are you always guaranteed that ipa is aligned on map_size?
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 803
Quoted 12 lines
arch/arm64/kvm/nested.c +12
-0+u64 entry, new_entry =0 ; +MA_STATE (mas_rev, revmap_mt, ipa, ipa_end); + +if (mmu->nested_revmap_broken) +return ; + +mtree_lock (revmap_mt); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + +if (entry) { +/* maybe just a perm update... */ + if (!(entry & UNKNOWN_IPA) && mas_rev.index == ipa &&
Shouldn't you check that VALID_ENTRY is set? Is the index guaranteed to match the L0 IPA?
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 805
Quoted 2 lines
arch/arm64/kvm/nested.c +2
-0+ mas_rev.last == ipa_end && + fault_ipa == (entry & ADDR_MASK))
Again, I think there is a potential alignment issue here.
Quoted 10 lines
arch/arm64/kvm/nested.c +10
-0+ goto unlock; +/* +* Create a "UNKNOWN_IPA" range that spans all the overlapping +* ranges and store it. +*/ +while (entry && mas_rev.index <= ipa_end) { + ipa =min (mas_rev.index, ipa); + ipa_end =max (mas_rev.last, ipa_end); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + }
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 825
Quoted 10 lines
arch/arm64/kvm/nested.c +10
-0+ new_entry |=UNKNOWN_IPA ; + }else { + new_entry |= fault_ipa; + new_entry |=VALID_ENTRY ; + } + +mas_set_range (&mas_rev, ipa, ipa_end); +if (mas_store_gfp (&mas_rev,xa_mk_value (new_entry), +GFP_NOWAIT | __GFP_ACCOUNT)) + mmu->nested_revmap_broken = true;
I really think we ought to track this event happening. Maybe a trace point.
Quoted 18 lines
arch/arm64/kvm/nested.c +6
-0+unlock: +mtree_unlock (revmap_mt); +} +void kvm_init_nested_s2_mmu (struct kvm_s2_mmu *mmu) {/* CnP being set denotes an invalid entry */ mmu->tlb_vttbr =VTTBR_CNP_BIT ; mmu->nested_stage2_enabled = false;atomic_set (&mmu->refcnt,0 ); +mt_init (&mmu->nested_revmap_mt); + mmu->nested_revmap_broken = false; }void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu)@@ -1150,6 +1210,82 @@ void kvm_nested_s2_wp(struct kvm *kvm)kvm_invalidate_vncr_ipa (kvm,0 ,BIT (kvm->arch.mmu.pgt->ia_bits)); }
arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1221
Quoted 10 lines
arch/arm64/kvm/nested.c +9
-0+static void reset_revmap_and_unmap (struct kvm_s2_mmu *mmu,bool may_block) +{ +mtree_destroy (&mmu->nested_revmap_mt); + mmu->nested_revmap_broken = false; +kvm_stage2_unmap_range (mmu,0 ,kvm_phys_size (mmu), may_block); +} + +static void unmap_mmu_ipa_range (struct kvm_s2_mmu *mmu,gpa_t gpa, +size_t unmap_size,bool may_block)
Same comment as above about the nature of 'gpa'. I *think* this is the L0 IPA, but please clarify.
arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1225
Quoted 4 lines
arch/arm64/kvm/nested.c +4
-0+{ +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +gpa_t ipa = gpa; +gpa_t ipa_end = gpa + unmap_size -1 ;
Similar concerns about alignments.
arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1238
Quoted 13 lines
arch/arm64/kvm/nested.c +13
-0+u64 entry; +size_t entry_size; +MA_STATE (mas_rev, revmap_mt, gpa, ipa_end); + +if (mmu->nested_revmap_broken) { +reset_revmap_and_unmap (mmu, may_block); +return ; + } + +mtree_lock (revmap_mt); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + +while (entry && mas_rev.index <= ipa_end) {
I'm again concerned that the VALID bit is never checked.
Quoted 38 lines
arch/arm64/kvm/nested.c +38
-0+ ipa = mas_rev.last +1 ; + entry_size = mas_rev.last - mas_rev.index +1 ; +/* +* Give up and invalidate this s2 mmu if the unmap range +* touches any UNKNOWN_IPA range. +*/ +if (entry &UNKNOWN_IPA ) { +mtree_unlock (revmap_mt); +reset_revmap_and_unmap (mmu, may_block); +return ; + } + +/* +* Ignore result, it is okay if a reverse mapping erase +* fails. +*/ +mas_store_gfp (&mas_rev,NULL ,GFP_NOWAIT | __GFP_ACCOUNT); + +mtree_unlock (revmap_mt); +kvm_stage2_unmap_range (mmu, entry &ADDR_MASK , entry_size, + may_block); +mtree_lock (revmap_mt); +/* +* Other maple tree operations during preemption could render +* this ma_state invalid, so reset it. +*/ +mas_set_range (&mas_rev, ipa, ipa_end); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + } +mtree_unlock (revmap_mt); +} + +void kvm_unmap_gfn_range_nested (struct kvm *kvm,gpa_t gpa,size_t size, +bool may_block) +{ +int i; + +if (!kvm->arch.nested_mmus_size)
arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1286
Quoted 10 lines
arch/arm64/kvm/nested.c +10
-0+return ; + +for (i =0 ; i < kvm->arch.nested_mmus_size; i++) { +struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i]; + +if (kvm_s2_mmu_valid (mmu)) +unmap_mmu_ipa_range (mmu, gpa, size, may_block); + } + +kvm_invalidate_vncr_ipa (kvm, gpa, gpa + size);
I'm not overly fond of propagating the VNCR invalidation in all the S2 manipulations. I understand why you are doing it here, but I think we need to have a better solution.
Fundamentally, VNCR invalidation has nothing to do with S2. This really is a EL2 S1 thing. And given that you have a reverse map per s2_mmu, it would be easy enough to track VNCR TLBs through that.
It doesn't have to be part of this patch, but that would be a good thing to disentangle as a subsequent patch.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v3 3/5] KVM: arm64: nv: Avoid full shadow s2 unmap
Hi Marc,
Sorry for the late reply.
Quoted 161 lines · Marc Zyngier
Quoted 159 lines · Wei-Lin Chang
Currently we are forced to fully unmap all shadow stage-2 for a VM when unmapping a page from the canonical stage-2, for example during an MMU notifier call. This is because we are not tracking what canonical IPA are mapped in the shadow stage-2 page tables hence there is no way to know what to unmap.
Create a per
kvm_s2_mmumaple tree to track canonical IPA range -> nested IPA range, so that it is possible to partially unmap shadow stage-2 when a canonical IPA range is unmapped. The algorithm is simple and conservative:At each shadow stage-2 map, insert the nested IPA range into the maple tree, with the canonical IPA range as the key. If the canonical IPA range doesn't overlap with existing ranges in the tree, insert as is, and a reverse mapping for this range is established. But if the canonical IPA range overlaps with any existing ranges in the tree, create a new range that spans all the overlapping ranges including the input range and replace those existing ranges. In the mean time, mark this new spanning canonical IPA range with an "
UNKNOWN_IPA" bit, indicating we give up tracking the nested IPA ranges that map to this canonical IPA range.The maple tree's 64 bit entry is enough to store the nested IPA and the
UNKNOWN_IPAstatus, therefore besides maple tree's internal operation, memory allocation is avoided.Example:
|||| means existing range, ---- means empty rangeinput: $$$$$$$$$$$$$$$$$$$$$$$$$$ tree: --||||-----|||||||---------||||||||||-----------insert spanning range and replace overlapping ones:
--||||-----||||||||||||||||||||||||||----------- ^^^^marked UNKNOWN_IPA^^^^With the reverse map created, when a canonical IPA range gets unmapped, look into each s2 mmu's maple tree and look for canonical IPA ranges affected, and base on their
UNKNOWN_IPAstatus:
UNKNOWN_IPA-> fall back and fully unmap the current shadow
stage-2, also clear the treenot
UNKNOWN_IPA-> unmap the nested IPA range, and remove the reverse
map entrySuggested-by Marc Zyngier <[email protected]>Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 4 + arch/arm64/include/asm/kvm_nested.h | 4 + arch/arm64/kvm/mmu.c | 27 ++++-- arch/arm64/kvm/nested.c | 140 +++++++++++++++++++++++++++- 4 files changed, 167 insertions(+), 8 deletions(-)arch/arm64/include/asm/kvm_host.h +4
-0@@ -223,6 +223,10 @@ struct kvm_s2_mmu {*/ bool pending_unmap; +bool nested_revmap_broken; +/* canonical IPA to nested IPA range lookup */ +struct maple_tree nested_revmap_mt; +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS struct dentry *shadow_pt_debugfs_dentry;#endif arch/arm64/include/asm/kvm_nested.h +4
-0@@ -76,6 +76,8 @@ extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,const union tlbi_info *info, void (*)(struct kvm_s2_mmu *, const union tlbi_info *)); +extern void kvm_record_nested_revmap (gpa_t gpa,struct kvm_s2_mmu *mmu, +gpa_t fault_ipa,size_t map_size);extern void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu);extern void kvm_vcpu_put_hw_mmu (struct kvm_vcpu *vcpu);@@ -164,6 +166,8 @@ extern int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu, struct kvm_s2_trans *trans);extern int kvm_inject_s2_fault (struct kvm_vcpu *vcpu,u64 esr_el2);extern void kvm_nested_s2_wp (struct kvm *kvm); +extern void kvm_unmap_gfn_range_nested (struct kvm *kvm,gpa_t gpa,size_t size, +bool may_block);extern void kvm_nested_s2_unmap (struct kvm *kvm,bool may_block);extern void kvm_nested_s2_flush (struct kvm *kvm);arch/arm64/kvm/mmu.c +18
-1@@ -5,6 +5,7 @@*/ #include <linux/acpi.h> +#include <linux/maple_tree.h> #include <linux/mman.h> #include <linux/kvm_host.h> #include <linux/io.h> @@ -1099,6 +1100,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_pgtable *pgt =NULL ; +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt;write_lock (&kvm->mmu_lock); pgt = mmu->pgt;@@ -1108,8 +1110,11 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)free_percpu (mmu->last_vcpu_ran); } -if (kvm_is_nested_s2_mmu (kvm, mmu)) +if (kvm_is_nested_s2_mmu (kvm, mmu)) { +if (!mtree_empty (revmap_mt)) +mtree_destroy (revmap_mt);kvm_init_nested_s2_mmu (mmu); + }write_unlock (&kvm->mmu_lock);@@ -1631,6 +1636,10 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) goto out_unlock; } +if (s2fd->nested) +kvm_record_nested_revmap (gfn <<PAGE_SHIFT , pgt->mmu, + s2fd->fault_ipa,PAGE_SIZE ); + ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt, s2fd->fault_ipa,PAGE_SIZE ,__pfn_to_phys (pfn), prot, memcache, flags);@@ -2034,6 +2043,10 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, ret =KVM_PGT_FN (kvm_pgtable_stage2_relax_perms)(pgt,gfn_to_gpa (gfn), prot, flags); }else { +if (s2fd->nested) +kvm_record_nested_revmap (canonical_gpa, pgt->mmu, +gfn_to_gpa (gfn), mapping_size); + ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt,gfn_to_gpa (gfn), mapping_size,__pfn_to_phys (pfn), prot, memcache, flags);@@ -2389,14 +2402,16 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) { +gpa_t gpa = range->start <<PAGE_SHIFT ; +size_t size = (range->end - range->start) <<PAGE_SHIFT ; +bool may_block = range->may_block; +if (!kvm->arch.mmu.pgt ||kvm_vm_is_protected (kvm))return false;
Quoted 6 lines
arch/arm64/kvm/mmu.c int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) · line 2412
Quoted 4 lines
arch/arm64/kvm/mmu.c +1
-3-__unmap_stage2_range (&kvm->arch.mmu, range->start <<PAGE_SHIFT , - (range->end - range->start) <<PAGE_SHIFT , - range->may_block); +__unmap_stage2_range (&kvm->arch.mmu, gpa, size, may_block);This sort of cleanups could be in a separate patch.
Ack.
Quoted 60 lines
Quoted 50 lines
arch/arm64/kvm/mmu.c +2
-2+kvm_unmap_gfn_range_nested (kvm, gpa, size, may_block); -kvm_nested_s2_unmap (kvm, range->may_block);return false; }@@ -2674,7 +2689,7 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,write_lock (&kvm->mmu_lock);kvm_stage2_unmap_range (&kvm->arch.mmu, gpa, size, true); -kvm_nested_s2_unmap (kvm, true); +kvm_unmap_gfn_range_nested (kvm, gpa, size, true);write_unlock (&kvm->mmu_lock); }arch/arm64/kvm/nested.c +15
-0@@ -7,6 +7,7 @@#include <linux/bitfield.h> #include <linux/kvm.h> #include <linux/kvm_host.h> +#include <linux/maple_tree.h> #include <asm/fixmap.h> #include <asm/kvm_arm.h> @@ -43,6 +44,20 @@ struct vncr_tlb {*/ #define S2_MMU_PER_VCPU 2 +/* +* Per shadow S2 reverse map (IPA -> nested IPA range) maple tree payload +* layout: +* +* bit 62: valid, prevents the case where the nested IPA is 0 and turning +* the whole value to 0 +* bits 55-12: nested IPA bits 55-12 +* bit 0: UNKNOWN_IPA bit, 1 indicates we give up on tracking what nested +* IPA maps to this canonical IPA in the shadow stage-2 +*/ +#define VALID_ENTRY BIT(62) +#define ADDR_MASK GENMASK_ULL(55, 12) +#define UNKNOWN_IPA BIT(0) +void kvm_init_nested (struct kvm *kvm) { kvm->arch.nested_mmus =NULL ;arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 788
Quoted 5 lines
arch/arm64/kvm/nested.c +2
-0@@ -769,12 +784,57 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)return s2_mmu; } +void kvm_record_nested_revmap (gpa_t ipa,struct kvm_s2_mmu *mmu, +gpa_t fault_ipa,size_t map_size)The name
fault_ipadoesn't really make sense here. This is the IPA as seen from L1 (the input to the L1 S2 tables). We indeed obtain it from a fault, but that should not influence the naming here.
Quoted 2 lines
Similarly, 'ipa' should be qualified a bit better to reflect that this is the L0 IPA.
Yeah these names aren't very clear. I'll change to use 'canonical IPA', and 'nested IPA'.
Quoted 6 lines
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 791
Quoted 4 lines
arch/arm64/kvm/nested.c +3
-0+{ +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +gpa_t ipa_end = ipa + map_size -1 ;Are you always guaranteed that ipa is aligned on
map_size?
From what I see in kvm_s2_fault_map() and gmem_abort(), yes, the ipa is aligned to map_size.
What about adding
if (WARN_ON(!IS_ALIGNED(canonical_ipa, map_size))) {
ALIGN_DOWN(canonical_ipa, map_size);
canonical_ipa_end = canonical_ipa + map_size;
}to be safe?
Quoted 16 lines
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 803
Quoted 13 lines
arch/arm64/kvm/nested.c +12
-0+u64 entry, new_entry =0 ; +MA_STATE (mas_rev, revmap_mt, ipa, ipa_end); + +if (mmu->nested_revmap_broken) +return ; + +mtree_lock (revmap_mt); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + +if (entry) { +/* maybe just a perm update... */ + if (!(entry & UNKNOWN_IPA) && mas_rev.index == ipa &&Shouldn't you check that
VALID_ENTRYis set? Is the index guaranteed to match the L0 IPA?
Right now either VALID_ENTRY or UNKNOWN_IPA is set for an exising entry in the tree, having both set is not expected. I think we can turn VALID_ENTRY and UNKNOWN_IPA tests into helpers and warn if they are both set in the helpers?
Quoted 21 lines
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 805
Quoted 3 lines
arch/arm64/kvm/nested.c +2
-0+ mas_rev.last == ipa_end && + fault_ipa == (entry & ADDR_MASK))Again, I think there is a potential alignment issue here.
Quoted 15 lines
arch/arm64/kvm/nested.c +15
-0+ goto unlock; +/* +* Create a "UNKNOWN_IPA" range that spans all the overlapping +* ranges and store it. +*/ +while (entry && mas_rev.index <= ipa_end) { + ipa =min (mas_rev.index, ipa); + ipa_end =max (mas_rev.last, ipa_end); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + } + new_entry |=UNKNOWN_IPA ; + }else { + new_entry |= fault_ipa; + new_entry |=VALID_ENTRY ; + }
Quoted 8 lines
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 825
Quoted 5 lines
arch/arm64/kvm/nested.c +5
-0+ +mas_set_range (&mas_rev, ipa, ipa_end); +if (mas_store_gfp (&mas_rev,xa_mk_value (new_entry), +GFP_NOWAIT | __GFP_ACCOUNT)) + mmu->nested_revmap_broken = true;I really think we ought to track this event happening. Maybe a trace point.
Will do.
Quoted 20 lines
Quoted 20 lines
arch/arm64/kvm/nested.c +6
-0+unlock: +mtree_unlock (revmap_mt); +} +void kvm_init_nested_s2_mmu (struct kvm_s2_mmu *mmu) {/* CnP being set denotes an invalid entry */ mmu->tlb_vttbr =VTTBR_CNP_BIT ; mmu->nested_stage2_enabled = false;atomic_set (&mmu->refcnt,0 ); +mt_init (&mmu->nested_revmap_mt); + mmu->nested_revmap_broken = false; }void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu)@@ -1150,6 +1210,82 @@ void kvm_nested_s2_wp(struct kvm *kvm)kvm_invalidate_vncr_ipa (kvm,0 ,BIT (kvm->arch.mmu.pgt->ia_bits)); }
Quoted 12 lines
arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1221
Quoted 9 lines
arch/arm64/kvm/nested.c +9
-0+static void reset_revmap_and_unmap (struct kvm_s2_mmu *mmu,bool may_block) +{ +mtree_destroy (&mmu->nested_revmap_mt); + mmu->nested_revmap_broken = false; +kvm_stage2_unmap_range (mmu,0 ,kvm_phys_size (mmu), may_block); +} + +static void unmap_mmu_ipa_range (struct kvm_s2_mmu *mmu,gpa_t gpa, +size_t unmap_size,bool may_block)Same comment as above about the nature of 'gpa'. I *think* this is the L0 IPA, but please clarify.
Will use clearer names from now.
Quoted 7 lines
arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1225
Quoted 5 lines
arch/arm64/kvm/nested.c +4
-0+{ +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +gpa_t ipa = gpa; +gpa_t ipa_end = gpa + unmap_size -1 ;Similar concerns about alignments.
For this function, I don't think there are any guarantees on alignment for sizes > PAGE_SIZE, but I think if we make sure the range we unmap completely covers the input range then things should be fine.
Quoted 61 lines
Quoted 4 lines
arch/arm64/kvm/nested.c +3
-0+u64 entry; +size_t entry_size; +MA_STATE (mas_rev, revmap_mt, gpa, ipa_end);arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1238
Quoted 10 lines
arch/arm64/kvm/nested.c +10
-0+ +if (mmu->nested_revmap_broken) { +reset_revmap_and_unmap (mmu, may_block); +return ; + } + +mtree_lock (revmap_mt); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + +while (entry && mas_rev.index <= ipa_end) {I'm again concerned that the VALID bit is never checked.
Quoted 44 lines
arch/arm64/kvm/nested.c +44
-0+ ipa = mas_rev.last +1 ; + entry_size = mas_rev.last - mas_rev.index +1 ; +/* +* Give up and invalidate this s2 mmu if the unmap range +* touches any UNKNOWN_IPA range. +*/ +if (entry &UNKNOWN_IPA ) { +mtree_unlock (revmap_mt); +reset_revmap_and_unmap (mmu, may_block); +return ; + } + +/* +* Ignore result, it is okay if a reverse mapping erase +* fails. +*/ +mas_store_gfp (&mas_rev,NULL ,GFP_NOWAIT | __GFP_ACCOUNT); + +mtree_unlock (revmap_mt); +kvm_stage2_unmap_range (mmu, entry &ADDR_MASK , entry_size, + may_block); +mtree_lock (revmap_mt); +/* +* Other maple tree operations during preemption could render +* this ma_state invalid, so reset it. +*/ +mas_set_range (&mas_rev, ipa, ipa_end); + entry =xa_to_value (mas_find_range (&mas_rev, ipa_end)); + } +mtree_unlock (revmap_mt); +} + +void kvm_unmap_gfn_range_nested (struct kvm *kvm,gpa_t gpa,size_t size, +bool may_block) +{ +int i; + +if (!kvm->arch.nested_mmus_size) +return ; + +for (i =0 ; i < kvm->arch.nested_mmus_size; i++) { +struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i]; + +if (kvm_s2_mmu_valid (mmu))
Quoted 8 lines
arch/arm64/kvm/nested.c void kvm_nested_s2_wp(struct kvm *kvm) · line 1286
Quoted 4 lines
arch/arm64/kvm/nested.c +4
-0+unmap_mmu_ipa_range (mmu, gpa, size, may_block); + } + +kvm_invalidate_vncr_ipa (kvm, gpa, gpa + size);I'm not overly fond of propagating the VNCR invalidation in all the S2 manipulations. I understand why you are doing it here, but I think we need to have a better solution.
I agree. I think moving kvm_invalidate_vncr_ipa() up one level, to the places where canonical IPA are being invalidated, is slightly more sensible.
Quoted 7 lines
Fundamentally, VNCR invalidation has nothing to do with S2. This really is a EL2 S1 thing. And given that you have a reverse map per
s2_mmu, it would be easy enough to track VNCR TLBs through that.It doesn't have to be part of this patch, but that would be a good thing to disentangle as a subsequent patch.
I'll have a think, thanks!
Thanks,
Wei-Lin Chang
Quoted 7 lines
Thanks,
M.Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v3 4/5 KVM: arm64: nv: Remove reverse map entries during TLBI handling
In reply to Wei-Lin ChangWhen a guest hypervisor issues a TLBI for a specific IPA range, KVM unmaps that range from all the effected shadow stage-2s. During this we get the opportunity to remove the reverse map, and lower the probability of creating UNKNOWN_IPA reverse map ranges at subsequent stage-2 faults.
However, the TLBI ranges are specified in nested IPA, so in order to locate the affected ranges in the reverse map maple tree, which is a mapping from canonical IPA to nested IPA, we can only iterate through the entire tree and check each entry.
--- arch/arm64/include/asm/kvm_nested.h | 2 ++ arch/arm64/kvm/nested.c | 38 +++++++++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 3 +++ 3 files changed, 43 insertions(+)
arch/arm64/include/asm/kvm_nested.h +2 -0
@@ -76,6 +76,8 @@ extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,const union tlbi_info *info, void (*)(struct kvm_s2_mmu *, const union tlbi_info *)); +extern void kvm_remove_nested_revmap (struct kvm_s2_mmu *mmu,u64 nested_ipa, +size_t size);extern void kvm_record_nested_revmap (gpa_t gpa,struct kvm_s2_mmu *mmu,gpa_t fault_ipa,size_t map_size);extern void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu);
arch/arm64/kvm/nested.c +38 -0
@@ -784,6 +784,44 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)return s2_mmu; } +void kvm_remove_nested_revmap (struct kvm_s2_mmu *mmu,u64 nested_ipa,size_t size) +{ +/* +* Iterate through the mt of this mmu, remove all canonical ipa ranges +* with !UNKNOWN_IPA that maps to ranges that are strictly within +* [addr, addr + size). +*/ +struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +void *entry; +u64 entry_val, nested_ipa_end = nested_ipa + size; +u64 this_nested_ipa, this_nested_ipa_end; +size_t revmap_size; + +MA_STATE (mas_rev, revmap_mt,0 ,ULONG_MAX ); + +mtree_lock (revmap_mt); +mas_for_each (&mas_rev, entry,ULONG_MAX ) { + entry_val =xa_to_value (entry); +if (entry_val &UNKNOWN_IPA ) +continue ; + + revmap_size = mas_rev.last - mas_rev.index +1 ; + this_nested_ipa = entry_val &ADDR_MASK ; + this_nested_ipa_end = this_nested_ipa + revmap_size; + +if (this_nested_ipa >= nested_ipa && + this_nested_ipa_end <= nested_ipa_end) { +/* +* As the shadow stage-2 is about to be unmapped +* after this function, it doesn't matter whether the +* removal of the reverse map failed or not. +*/ +mas_store_gfp (&mas_rev,NULL ,GFP_NOWAIT | __GFP_ACCOUNT); + } + } +mtree_unlock (revmap_mt); +} +void kvm_record_nested_revmap (gpa_t ipa,struct kvm_s2_mmu *mmu,gpa_t fault_ipa,size_t map_size) {
arch/arm64/kvm/sys_regs.c +3 -0
@@ -4006,6 +4006,7 @@ union tlbi_info {static void s2_mmu_unmap_range (struct kvm_s2_mmu *mmu,const union tlbi_info *info) { +kvm_remove_nested_revmap (mmu, info->range.start, info->range.size);/* * The unmap operation is allowed to drop the MMU lock and block, which * means that @mmu could be used for a different context than the one
@@ -4104,6 +4105,8 @@ static void s2_mmu_unmap_ipa(struct kvm_s2_mmu *mmu, max_size =compute_tlb_inval_range (mmu, info->ipa.addr); base_addr &= ~(max_size -1 ); +kvm_remove_nested_revmap (mmu, base_addr, max_size); +/* * See comment in s2_mmu_unmap_range() for why this is allowed to * reschedule.
Signature · 1 line
2.43.0
PATCH v3 5/5 KVM: arm64: nv: Create nested IPA direct map to speed up reverse map removal
In reply to Wei-Lin ChangIterating through the whole reverse map to find which entries to remove when handling guest hypervisor TLBIs is not efficient. Create a direct map that goes from nested IPA to canonical IPA so that the canonical IPA range affected by the TLBI can be quickly determined, then remove the entries in the reverse map accordingly.
--- arch/arm64/include/asm/kvm_host.h | 5 ++ arch/arm64/kvm/mmu.c | 9 ++- arch/arm64/kvm/nested.c | 124 ++++++++++++++++++++++-------- 3 files changed, 104 insertions(+), 34 deletions(-)
arch/arm64/include/asm/kvm_host.h +5 -0
@@ -226,6 +226,11 @@ struct kvm_s2_mmu {bool nested_revmap_broken;/* canonical IPA to nested IPA range lookup */ struct maple_tree nested_revmap_mt; +/* +* Nested IPA to canonical IPA range lookup, essentially a cache of +* the guest's stage-2. +*/ +struct maple_tree nested_direct_mt;#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS struct dentry *shadow_pt_debugfs_dentry;
arch/arm64/kvm/mmu.c +7 -2
@@ -1101,6 +1101,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_pgtable *pgt =NULL ;struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +struct maple_tree *direct_mt = &mmu->nested_direct_mt;write_lock (&kvm->mmu_lock); pgt = mmu->pgt;
@@ -1111,8 +1112,12 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) }if (kvm_is_nested_s2_mmu (kvm, mmu)) { -if (!mtree_empty (revmap_mt)) -mtree_destroy (revmap_mt); +if (!mtree_empty (revmap_mt) || !mtree_empty (direct_mt)) { +mtree_lock (revmap_mt); +__mt_destroy (revmap_mt); +__mt_destroy (direct_mt); +mtree_unlock (revmap_mt); + }kvm_init_nested_s2_mmu (mmu); }
arch/arm64/kvm/nested.c +92 -32
188 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 5/5] KVM: arm64: nv: Create nested IPA direct map to speed up reverse map removal
Quoted 35 lines · Wei-Lin Chang
Iterating through the whole reverse map to find which entries to remove when handling guest hypervisor TLBIs is not efficient. Create a direct map that goes from nested IPA to canonical IPA so that the canonical IPA range affected by the TLBI can be quickly determined, then remove the entries in the reverse map accordingly.
Suggested-by Marc Zyngier <[email protected]>Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 5 ++ arch/arm64/kvm/mmu.c | 9 ++- arch/arm64/kvm/nested.c | 124 ++++++++++++++++++++++-------- 3 files changed, 104 insertions(+), 34 deletions(-)arch/arm64/include/asm/kvm_host.h +5
-0@@ -226,6 +226,11 @@ struct kvm_s2_mmu {bool nested_revmap_broken;/* canonical IPA to nested IPA range lookup */ struct maple_tree nested_revmap_mt; +/* +* Nested IPA to canonical IPA range lookup, essentially a cache of +* the guest's stage-2. +*/ +struct maple_tree nested_direct_mt;#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS struct dentry *shadow_pt_debugfs_dentry;arch/arm64/kvm/mmu.c +0
-0
arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1103
Quoted 3 lines
arch/arm64/kvm/mmu.c +0
-0@@ -1101,6 +1101,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_pgtable *pgt =NULL ;struct maple_tree *revmap_mt = &mmu->nested_revmap_mt;
Naming, do you prefer revmap over rmap?
Quoted 74 lines
arch/arm64/kvm/mmu.c +7
-2+struct maple_tree *direct_mt = &mmu->nested_direct_mt;write_lock (&kvm->mmu_lock); pgt = mmu->pgt;@@ -1111,8 +1112,12 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) }if (kvm_is_nested_s2_mmu (kvm, mmu)) { -if (!mtree_empty (revmap_mt)) -mtree_destroy (revmap_mt); +if (!mtree_empty (revmap_mt) || !mtree_empty (direct_mt)) { +mtree_lock (revmap_mt); +__mt_destroy (revmap_mt); +__mt_destroy (direct_mt); +mtree_unlock (revmap_mt); + }kvm_init_nested_s2_mmu (mmu); }arch/arm64/kvm/nested.c +23
-12@@ -45,14 +45,14 @@ struct vncr_tlb {#define S2_MMU_PER_VCPU 2/* -* Per shadow S2 reverse map (IPA -> nested IPA range) maple tree payload -* layout: +* Per shadow S2 reverse & direct map maple tree payload layout: * -* bit 62: valid, prevents the case where the nested IPA is 0 and turning +* bit 62: valid, prevents the case where the address is 0 and turning * the whole value to 0 -* bits 55-12: nested IPA bits 55-12 +* bits 55-12: {nested, canonical} IPA bits 55-12 * bit 0: UNKNOWN_IPA bit, 1 indicates we give up on tracking what nested -* IPA maps to this canonical IPA in the shadow stage-2 +* IPA maps to this canonical IPA in the shadow stage-2, only used +* in reverse map */ #define VALID_ENTRY BIT(62)#define ADDR_MASK GENMASK_ULL(55, 12)@@ -787,37 +787,67 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)void kvm_remove_nested_revmap (struct kvm_s2_mmu *mmu,u64 nested_ipa,size_t size) {/* -* Iterate through the mt of this mmu, remove all canonical ipa ranges -* with !UNKNOWN_IPA that maps to ranges that are strictly within -* [addr, addr + size). +* For all ranges in direct_mt that are completely covered by the range +* we are TLBIing [gpa, gpa + size), remove the reverse map and its +* corresponding direct map together, when these conditions are met: +* +* 1. The reverse map is not UNKNOWN_IPA. +* 2. The reverse map is completely covered by the TLBI range. +* 3. The reverse map and the direct map are symmetric i.e. they map to +* each other, with the same size. +* +* Symmetry must be checked because there are three places where the +* direct map could become inconsistent: +* +* 1. Direct map removal failure during an mmu notifier in +* unmap_mmu_ipa_range(). +* 2. Direct map insertion failure during an s2 fault in +* kvm_record_nested_revmap(). +* 3. Direct map removal failure during a previous call of this very +* function. */ struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; -void *entry; -u64 entry_val, nested_ipa_end = nested_ipa + size; -u64 this_nested_ipa, this_nested_ipa_end; -size_t revmap_size;
arch/arm64/kvm/nested.c static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) · line 816
Quoted 10 lines
arch/arm64/kvm/nested.c +7
-3- -MA_STATE (mas_rev, revmap_mt,0 ,ULONG_MAX ); - +struct maple_tree *direct_mt = &mmu->nested_direct_mt; +gpa_t nested_ipa_end = nested_ipa + size -1 ; +u64 entry_dir; +struct mapping { +u64 from; +u64 to; +size_t size;
entry_dir sounds like a directory related one, if entry_drect is too long, perhaps entry_d? But it's up to you and Marc and Oliver.
Thanks,
Itaru.
Quoted 138 lines
arch/arm64/kvm/nested.c +62
-17+ }; + +MA_STATE (mas_dir, direct_mt, nested_ipa, nested_ipa_end);mtree_lock (revmap_mt); -mas_for_each (&mas_rev, entry,ULONG_MAX ) { - entry_val =xa_to_value (entry); -if (entry_val &UNKNOWN_IPA ) -continue ; - - revmap_size = mas_rev.last - mas_rev.index +1 ; - this_nested_ipa = entry_val &ADDR_MASK ; - this_nested_ipa_end = this_nested_ipa + revmap_size; - -if (this_nested_ipa >= nested_ipa && - this_nested_ipa_end <= nested_ipa_end) { -/* -* As the shadow stage-2 is about to be unmapped -* after this function, it doesn't matter whether the -* removal of the reverse map failed or not. -*/ + entry_dir =xa_to_value (mas_find_range (&mas_dir, nested_ipa_end)); + +while (entry_dir && mas_dir.index <= nested_ipa_end) { +struct mapping dir, rev; +u64 entry_rev; + + dir.from = mas_dir.index; + dir.to = entry_dir &ADDR_MASK ; + dir.size = mas_dir.last - mas_dir.index +1 ; + +/* Use ipa range to find the corresponding entry in revmap. */ +MA_STATE (mas_rev, revmap_mt, dir.to, dir.to + dir.size -1 ); + entry_rev =xa_to_value (mas_find_range (&mas_rev, + dir.to + dir.size -1 )); + + rev.from = mas_rev.index; + rev.to = entry_rev &ADDR_MASK ; + rev.size = mas_rev.last - mas_rev.index +1 ; + +/* The three conditions outlined above. */ +if (entry_rev && !(entry_rev &UNKNOWN_IPA ) && + dir.from >= nested_ipa && + dir.from + dir.size -1 <= nested_ipa_end && + dir.from == rev.to && + rev.from == dir.to && + dir.size == rev.size) { +mas_store_gfp (&mas_dir,NULL ,GFP_NOWAIT | __GFP_ACCOUNT);mas_store_gfp (&mas_rev,NULL ,GFP_NOWAIT | __GFP_ACCOUNT); } + entry_dir =xa_to_value (mas_find_range (&mas_dir, nested_ipa_end)); }mtree_unlock (revmap_mt); }@@ -826,9 +856,12 @@ void kvm_record_nested_revmap(gpa_t ipa, struct kvm_s2_mmu *mmu, gpa_t fault_ipa, size_t map_size) {struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +struct maple_tree *direct_mt = &mmu->nested_direct_mt;gpa_t ipa_end = ipa + map_size -1 ; +gpa_t fault_ipa_end = fault_ipa + map_size -1 ;u64 entry, new_entry =0 ;MA_STATE (mas_rev, revmap_mt, ipa, ipa_end); +MA_STATE (mas_dir, direct_mt, fault_ipa, fault_ipa_end);if (mmu->nested_revmap_broken)return ;@@ -861,6 +894,15 @@ void kvm_record_nested_revmap(gpa_t ipa, struct kvm_s2_mmu *mmu,if (mas_store_gfp (&mas_rev,xa_mk_value (new_entry),GFP_NOWAIT | __GFP_ACCOUNT)) mmu->nested_revmap_broken = true; + +/* +* Add direct map but ignore the result, missing a direct map does not +* affect correctness. +*/ +if (new_entry &VALID_ENTRY && !mmu->nested_revmap_broken) +mas_store_gfp (&mas_dir,xa_mk_value (ipa |VALID_ENTRY ), +GFP_NOWAIT | __GFP_ACCOUNT); + unlock:mtree_unlock (revmap_mt); }@@ -872,6 +914,8 @@ void kvm_init_nested_s2_mmu(struct kvm_s2_mmu *mmu) mmu->nested_stage2_enabled = false;atomic_set (&mmu->refcnt,0 );mt_init (&mmu->nested_revmap_mt); +mt_init_flags (&mmu->nested_direct_mt,MT_FLAGS_LOCK_EXTERN ); +mt_set_external_lock (&mmu->nested_direct_mt, &mmu->nested_revmap_mt.ma_lock); mmu->nested_revmap_broken = false; }@@ -1250,7 +1294,10 @@ void kvm_nested_s2_wp(struct kvm *kvm)static void reset_revmap_and_unmap (struct kvm_s2_mmu *mmu,bool may_block) { -mtree_destroy (&mmu->nested_revmap_mt); +mtree_lock (&mmu->nested_revmap_mt); +__mt_destroy (&mmu->nested_revmap_mt); +__mt_destroy (&mmu->nested_direct_mt); +mtree_unlock (&mmu->nested_revmap_mt); mmu->nested_revmap_broken = false;kvm_stage2_unmap_range (mmu,0 ,kvm_phys_size (mmu), may_block); }@@ -1259,11 +1306,14 @@ static void unmap_mmu_ipa_range(struct kvm_s2_mmu *mmu, gpa_t gpa, size_t unmap_size, bool may_block) {struct maple_tree *revmap_mt = &mmu->nested_revmap_mt; +struct maple_tree *direct_mt = &mmu->nested_direct_mt;gpa_t ipa = gpa;gpa_t ipa_end = gpa + unmap_size -1 ; +gpa_t nested_ipa, nested_ipa_end;u64 entry;size_t entry_size;MA_STATE (mas_rev, revmap_mt, gpa, ipa_end); +MA_STATE (mas_dir, direct_mt,0 ,ULONG_MAX );if (mmu->nested_revmap_broken) {reset_revmap_and_unmap (mmu, may_block);@@ -1292,6 +1342,16 @@ static void unmap_mmu_ipa_range(struct kvm_s2_mmu *mmu, gpa_t gpa,*/ mas_store_gfp (&mas_rev,NULL ,GFP_NOWAIT | __GFP_ACCOUNT); +/* +* Try to also remove the direct map, it is okay if this fails, +* as we check for direct map consistency in +* kvm_remove_nested_revmap(). +*/ + nested_ipa = entry &ADDR_MASK ; + nested_ipa_end = nested_ipa + entry_size -1 ; +mas_set_range (&mas_dir, nested_ipa, nested_ipa_end); +mas_store_gfp (&mas_dir,NULL ,GFP_NOWAIT | __GFP_ACCOUNT); +mtree_unlock (revmap_mt);kvm_stage2_unmap_range (mmu, entry &ADDR_MASK , entry_size, may_block);Signature · 1 line
2.43.0
Hi Wei Lin,
Quoted 16 lines · Wei-Lin Chang
Hi,
This is v3 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
Two new preparatory patches are added, one reduces a hole in
kvm_s2_mmuand another refactors the code a bit during s2 faults. Other changes are listed below.* Changes from v2 [1]:
- Removed "polluted" teminology.
- Use xa_{mk, to}_value() when storing and retriving values from maple trees.
Quoted 8 lines
- Avoid using the 63rd bit in maple tree values so that xa_{mk, to}_value() does not lose us a bit.
- Added reverse map removal during TLBI handling.
- Other suggested refactorings.
Thanks!
With this series applied on the recent kvmarm/fixes (minor local modification), your v3 hello_nested runs fine on all three page granules; without this 16KB kernel can't handle stage 2 unmapping paths. I tested using the latest QEMU only so wonder how you're testing.
Thanks,
Itaru.
Quoted 22 lines
[1]: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (5):
KVM: arm64: Use a variable for the canonical GPA inkvm_s2_fault_map()
KVM: arm64: Moveshadow_pt_debugfs_dentryto reduce holes inkvm_s2_mmuKVM: arm64: nv: Avoid full shadow s2 unmap
KVM: arm64: nv: Remove reverse map entries during TLBI handling
KVM: arm64: nv: Create nested IPA direct map to speed up reverse map
removalarch/arm64/include/asm/kvm_host.h | 17 +- arch/arm64/include/asm/kvm_nested.h | 6 + arch/arm64/kvm/mmu.c | 43 +++-- arch/arm64/kvm/nested.c | 238 +++++++++++++++++++++++++++- arch/arm64/kvm/sys_regs.c | 3 + 5 files changed, 290 insertions(+), 17 deletions(-)Signature · 1 line
2.43.0
Marc Zyngier
maintainerQuoted 25 lines · Itaru Kitayama
Hi Wei Lin,
Quoted 21 lines · Wei-Lin Chang
Hi,
This is v3 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
Two new preparatory patches are added, one reduces a hole in
kvm_s2_mmuand another refactors the code a bit during s2 faults. Other changes are listed below.* Changes from v2 [1]:
- Removed "polluted" teminology.
- Use xa_{mk, to}_value() when storing and retriving values from maple trees.
- Avoid using the 63rd bit in maple tree values so that xa_{mk, to}_value() does not lose us a bit.
- Added reverse map removal during TLBI handling.
Quoted 8 lines
Quoted 3 lines
- Other suggested refactorings.
Thanks!
With this series applied on the recent kvmarm/fixes (minor local modification), your v3
hello_nestedruns fine on all three page granules; without this 16KB kernel can't handle stage 2 unmapping paths. I tested using the latest QEMU only so wonder how you're testing.
Can you clarify this statement?
Are you saying that 16kB S2 unmap do not work with upstream? That seems odd, as it would mean NV wouldn't work on Apple HW, which represents two thirds of my development boxes...
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 36 lines · Marc Zyngier
Quoted 25 lines · Itaru Kitayama
Hi Wei Lin,
Quoted 21 lines · Wei-Lin Chang
Hi,
This is v3 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
Two new preparatory patches are added, one reduces a hole in
kvm_s2_mmuand another refactors the code a bit during s2 faults. Other changes are listed below.* Changes from v2 [1]:
- Removed "polluted" teminology.
- Use xa_{mk, to}_value() when storing and retriving values from maple trees.
- Avoid using the 63rd bit in maple tree values so that xa_{mk, to}_value() does not lose us a bit.
- Added reverse map removal during TLBI handling.
Quoted 8 lines
Quoted 3 lines
- Other suggested refactorings.
Thanks!
With this series applied on the recent kvmarm/fixes (minor local modification), your v3
hello_nestedruns fine on all three page granules; without this 16KB kernel can't handle stage 2 unmapping paths. I tested using the latest QEMU only so wonder how you're testing.
Quoted 5 lines
Can you clarify this statement?
Are you saying that 16kB S2 unmap do not work with upstream? That seems odd, as it would mean NV wouldn't work on Apple HW, which represents two thirds of my development boxes...
Yes, I built this morning's upstream kvmarm/fixes with 16KB granule fresh, and tested Wei Lin's propsed KVM selftest, hello_nested on QEMU with the small buildroot filesystem and saw the above i.e., the stage 2 unmapping paths never finish.
I thought it was due to the Ubuntu userspace I was using for testing, but not sure now.
Thanks,
Itaru.
Quoted 7 lines
Thanks,
M.Signature · 1 line
Without deviation from the norm, progress is not possible.
Hi Wei-Lin,
Quoted 36 lines · Wei-Lin Chang
Hi,
This is v3 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
Two new preparatory patches are added, one reduces a hole in
kvm_s2_mmuand another refactors the code a bit during s2 faults. Other changes are listed below.* Changes from v2 [1]:
- Removed "polluted" teminology.
- Use xa_{mk, to}_value() when storing and retriving values from maple trees.
- Avoid using the 63rd bit in maple tree values so that xa_{mk, to}_value() does not lose us a bit.
- Added reverse map removal during TLBI handling.
- Other suggested refactorings.
Thanks!
[1]: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (5):
KVM: arm64: Use a variable for the canonical GPA inkvm_s2_fault_map()
KVM: arm64: Moveshadow_pt_debugfs_dentryto reduce holes inkvm_s2_mmuKVM: arm64: nv: Avoid full shadow s2 unmap
KVM: arm64: nv: Remove reverse map entries during TLBI handling
KVM: arm64: nv: Create nested IPA direct map to speed up reverse map
removal
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h | 17 +- arch/arm64/include/asm/kvm_nested.h | 6 + arch/arm64/kvm/mmu.c | 43 +++-- arch/arm64/kvm/nested.c | 238 +++++++++++++++++++++++++++- arch/arm64/kvm/sys_regs.c | 3 + 5 files changed, 290 insertions(+), 17 deletions(-)
While investigating the long VM teardown time on QEMU in the nested shadow stage 2 selftest, I instrumented a few functions with bpftrace.
The same kselftest workload produced the following counts:
Without this series (today's kvmarm/fixes):
kvm_nested_s2_unmap: 12kvm_pgtable_stage2_unmap: 8,388,623kvm_tlb_flush_vmid_range: 8,388,623exit_vmid_context: 8,388,639
With this series:
kvm_nested_s2_unmap: 12kvm_pgtable_stage2_unmap: 18kvm_tlb_flush_vmid_range: 18exit_vmid_context: 34
The bpftrace script used was:
#!/usr/bin/env bpftrace
fentry:kvm_nested_s2_unmap
{
@nested = count();
}fentry:kvm_pgtable_stage2_unmap
{
@pgunmap = count();
}fentry:kvm_tlb_flush_vmid_range
{
@flush = count();
}fentry:exit_vmid_context
{
@exit = count();
}END
{
print(@nested);
print(@pgunmap);
print(@flush);
print(@exit);
}Is this consistent with what you evaluated during the development? I wonder if this is good for nested guest boot paths too.
Thanks,
Itaru.
Quoted 4 lines
Signature · 2 lines
-- 2.43.0
Quoted 16 lines · Wei-Lin Chang
Hi,
This is v3 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
Two new preparatory patches are added, one reduces a hole in
kvm_s2_mmuand another refactors the code a bit during s2 faults. Other changes are listed below.* Changes from v2 [1]:
- Removed "polluted" teminology.
- Use xa_{mk, to}_value() when storing and retriving values from maple trees.
Quoted 8 lines
- Avoid using the 63rd bit in maple tree values so that xa_{mk, to}_value() does not lose us a bit.
- Added reverse map removal during TLBI handling.
- Other suggested refactorings.
Thanks!
Hi Wei-Lin
As this series is no longer cleanly applied on top of the recent kvmarm/fixes, I wonder if you could update the series; I can wait if you're in the works of preparing v4. I am planning to test this series on HW with your proposed nested virtualization KVM selftests.
Thanks,
Itaru.
Quoted 22 lines
[1]: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (5):
KVM: arm64: Use a variable for the canonical GPA inkvm_s2_fault_map()
KVM: arm64: Moveshadow_pt_debugfs_dentryto reduce holes inkvm_s2_mmuKVM: arm64: nv: Avoid full shadow s2 unmap
KVM: arm64: nv: Remove reverse map entries during TLBI handling
KVM: arm64: nv: Create nested IPA direct map to speed up reverse map
removalarch/arm64/include/asm/kvm_host.h | 17 +- arch/arm64/include/asm/kvm_nested.h | 6 + arch/arm64/kvm/mmu.c | 43 +++-- arch/arm64/kvm/nested.c | 238 +++++++++++++++++++++++++++- arch/arm64/kvm/sys_regs.c | 3 + 5 files changed, 290 insertions(+), 17 deletions(-)Signature · 1 line
2.43.0
Quoted 24 lines · Itaru Kitayama
Quoted 23 lines · Wei-Lin Chang
Hi,
This is v3 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
Two new preparatory patches are added, one reduces a hole in
kvm_s2_mmuand another refactors the code a bit during s2 faults. Other changes are listed below.* Changes from v2 [1]:
- Removed "polluted" teminology.
- Use xa_{mk, to}_value() when storing and retriving values from maple trees.
- Avoid using the 63rd bit in maple tree values so that xa_{mk, to}_value() does not lose us a bit.
- Added reverse map removal during TLBI handling.
- Other suggested refactorings.
Quoted 7 lines
Quoted 1 line
Thanks!
Hi Wei-Lin
As this series is no longer cleanly applied on top of the recent kvmarm/fixes, I wonder if you could update the series; I can wait if you're in the works of preparing v4. I am planning to test this series on HW with your proposed nested virtualization KVM selftests.
Hey Itaru,
Sorry for the delay. I have been working on something else in parallel hence I haven't updated this series for a while. Yes I am almost done preparing v4, it should be posted sometime this week.
Thanks for your feedback and testing!
Thanks,
Wei-Lin Chang
Quoted 26 lines
Thanks,
Itaru.Quoted 22 lines
[1]: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (5):
KVM: arm64: Use a variable for the canonical GPA inkvm_s2_fault_map()
KVM: arm64: Moveshadow_pt_debugfs_dentryto reduce holes inkvm_s2_mmuKVM: arm64: nv: Avoid full shadow s2 unmap
KVM: arm64: nv: Remove reverse map entries during TLBI handling
KVM: arm64: nv: Create nested IPA direct map to speed up reverse map
removalarch/arm64/include/asm/kvm_host.h | 17 +- arch/arm64/include/asm/kvm_nested.h | 6 + arch/arm64/kvm/mmu.c | 43 +++-- arch/arm64/kvm/nested.c | 238 +++++++++++++++++++++++++++- arch/arm64/kvm/sys_regs.c | 3 + 5 files changed, 290 insertions(+), 17 deletions(-)Signature · 1 line
2.43.0