KVM: arm64: nv: Implement nested stage-2 reverse map (new data structure)
v5 cover KVM: arm64: nv: Implement nested stage-2 reverse map (new data structure)
Hi,
This is v5 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
This time, a major overhaul is done to the implementation. After receiving some suggestions from Marc, I have identified that using the interval tree to store the guest stage-2 mappings solves many problems compared to using the maple tree.
Interval Tree vs Maple Tree
===========================
First of all, interval trees are capable of storing overlapping ranges, which is helpful when the L1 hypervisor maps something like:
nested IPA [x, x+4K) -> canonical IPA [a, a+4K)
nested IPA [y, y+2M) -> canonical IPA [a, a+2M)
No problems with storing that in the interval tree with different nodes. We can avoid the maple tree UNKNOWN_IPA mechanism as a compromise.
Second, ideally we would want to save the canonical IPA <-> nested IPA mapping in both directions to allow MMU notifier unmap speed up, and stale shadow mapping removals. If we use the maple tree, we'll have to have 2 separate trees, and make sure they store the same mappings, which isn't simple given the first point.
On the other hand, by using this pattern:
/* Record of a guest stage-2 mapping. */
struct kvm_guest_s2_mapping {
struct interval_tree_node canonical; // CIPA range of the mapping
struct interval_tree_node nested; // NIPA range of the mapping
struct kvm_s2_mmu *nested_mmu; // mmu of the NIPA space
};
and equip each mmu with an interval tree storing mapping records corresponding to the IPA space it represents, we can insert the respective nodes into the canonical IPA tree, and the corresponding nested IPA tree. This makes it trivial to find the range of the other IPA space from a range in one IPA space.
Diagram to help understanding:
struct kvm_guest_s2_mapping mapping1, mapping2;
---------------------> mapping2.canonical
| mapping1.canonical
| ^ (both stored in canonical mmu's tree)
| |
--*****-----------------------*****----------- CIPA
\\\\\ ||||| mapping1.nested_mmu
\\\\\ \\\\\ |
\\\\\ \\\\\ v
------\\\\\---------------------*****--------- NIPA #1 (nested mmu #1)
\\\\\ |
\\\\\ -> mapping1.nested
\\\\\ (stored in nested mmu #1's tree)
\\\\\-----------*****------------------------------ NIPA #2 (nested mmu #2)
| ^
-> mapping2.nested |
(stored in nested mmu #2's tree) mapping2.nested_mmu
Third, maple tree does its own memory allocation. In the KVM stage-2 fault path we only find out what the mapping ranges are after taking the KVM MMU lock, and the maple tree has to know the range and entry to be stored to preallocate, therefore in our case the maple tree is forced to only use GFP_NOWAIT, which isn't the best. With the interval tree the user does the memory management, and we can just allocate before taking the locks.
Locking
=======
The guest_s2_tracking_lock serializes accesses to the tracking interval trees. It is taken after the mmu_lock. However in reality it is only taken after we take the read mmu_lock in the stage-2 fault path, as other accesses have the write mmu_lock already. This saves us some manual lock/unlocks.
vCPU Stage-2 Fault Scalability Reduction
========================================
KVM/arm64 is able to handle stage-2 faults from multiple vCPUs in parallel, thanks to the engineering done to the s2 pgtable code. However to safely insert mappings into the interval trees we have to serialize using the guest_s2_tracking_lock. We trade some performance in stage-2 fault for faster MMU notifier unmaps, and keeping the unaffected shadow mappings.
Memory Usage
============
Each interval tree node is 48 bytes, and a kvm_guest_s2_mapping is 104 bytes, residing in 128-byte slab objects. Each shadow stage-2 fault requires one kvm_guest_s2_mapping instance. This is 32MB for a fully 4KB mapped 1GB region, and 64KB for a 2MB mapped 1GB region.
Series Structure
================
Patch 1: Preparatory refactoring. Patch 2: Introduce data structures for guest stage-2 tracking. Patch 3-4: Guest stage-2 tracking addition and removal Patch 5: Avoid full unmap during MMU notifier unmap using the tracked
guest stage-2 mapping information.
Patch 6: Minor clean up.
As this is a complete rework, I will omit the change log this time. Series is based on v7.2-rc5.
Thanks!
Link to v4: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (6):
KVM: arm64: Use a variable for the canonical IPA in kvm_s2_fault_map()
KVM: arm64: nv: Introduce guest stage-2 tracking structures
KVM: arm64: nv: Track guest stage-2 mapping creation
KVM: arm64: nv: Track guest stage-2 mapping removal
KVM: arm64: nv: Avoid full shadow stage-2 unmap
KVM: arm64: Refactor kvm_unmap_gfn_range() with common variables
arch/arm64/include/asm/kvm_host.h | 20 ++++++ arch/arm64/include/asm/kvm_nested.h | 7 ++ arch/arm64/kvm/mmu.c | 105 ++++++++++++++++++++++++---- arch/arm64/kvm/nested.c | 95 +++++++++++++++++++++++++ 4 files changed, 215 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0
PATCH v5 1/6 KVM: arm64: Use a variable for the canonical IPA in kvm_s2_fault_map()
Create a variable to store the canonical IPA, instead of calculating it when needed. This will be useful when we need to use the canonical IPA for guest stage-2 tracking later.
--- arch/arm64/kvm/mmu.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
arch/arm64/kvm/mmu.c +6 -5
@@ -2021,6 +2021,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ;bool writable = prot &KVM_PGTABLE_PROT_W ;struct kvm *kvm = s2fd->vcpu->kvm; +phys_addr_t canonical_ipa;struct kvm_pgtable *pgt;long perm_fault_granule;long mapping_size;
@@ -2039,6 +2040,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_ipa =gfn_to_gpa (get_canonical_gfn (s2fd, s2vi));/* * If we are not forced to use page mapping, check if we are
@@ -2057,6 +2059,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, goto out_unlock; } } + canonical_ipa =ALIGN_DOWN (canonical_ipa, mapping_size); }if (!perm_fault_granule && !s2vi->map_non_cacheable &&kvm_has_mte (kvm))
@@ -2090,11 +2093,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_ipa));if (ret != -EAGAIN )return ret;
Signature · 1 line
2.43.0
PATCH v5 2/6 KVM: arm64: nv: Introduce guest stage-2 tracking structures
In reply to Wei-Lin ChangIn order to avoid unmapping all shadow stage-2 mappings when KVM receives a MMU notifier unmap call, we have to keep track of the canonical IPA -> nested IPA relationship of the shadow mappings created. This essentially means tracking the guest's stage-2.
To do this, represent each mapping by struct kvm_guest_s2_mapping. It stores the mapping's canonical IPA range and the nested IPA range using two interval tree nodes. Both nodes will be inserted into their respective interval trees called guest_s2_mappings. The canonical IPA ranges will be stored in the tree within the canonical MMU, and the nested IPA ranges will be stored in the corresponding nested MMU's tree.
For example:
struct kvm_guest_s2_mapping mapping1, mapping2;
---------------------> mapping2.canonical
| mapping1.canonical
| ^ (both stored in canonical mmu's tree)
| |
--*****-----------------------*****----------- CIPA
\\\\\ ||||| mapping1.nested_mmu
\\\\\ \\\\\ |
\\\\\ \\\\\ v
------\\\\\---------------------*****--------- NIPA #1 (nested mmu #1)
\\\\\ |
\\\\\ -> mapping1.nested
\\\\\ (stored in nested mmu #1's tree)
\\\\\-----------*****------------------------------ NIPA #2 (nested mmu #2)
| ^
-> mapping2.nested |
(stored in nested mmu #2's tree) mapping2.nested_mmu
Using the trees we can look up nodes in either of the IPA spaces, and for each node, find the corresponding range in the other IPA space from the other node in the enclosing kvm_guest_s2_mapping.
Define kvm_guest_s2_mapping and the interval tree here. Guest stage-2 mapping tracking will come in subsequent patches.
--- arch/arm64/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/arm64/kvm/mmu.c | 30 ++++++++++++++++++++++++++++++ arch/arm64/kvm/nested.c | 1 + 3 files changed, 48 insertions(+)
arch/arm64/include/asm/kvm_host.h +17 -0
@@ -14,6 +14,7 @@#include <linux/arm-smccc.h> #include <linux/bitmap.h> #include <linux/types.h> +#include <linux/interval_tree.h> #include <linux/jump_label.h> #include <linux/kvm_types.h> #include <linux/maple_tree.h>
@@ -150,6 +151,16 @@ struct kvm_vmid {atomic64_t id; }; +/* +* Record of a guest stage-2 mapping, storing canonical and nested IPA +* ranges. Both ranges have the same size. +*/ +struct kvm_guest_s2_mapping { +struct interval_tree_node canonical; +struct interval_tree_node nested; +struct kvm_s2_mmu *nested_mmu; +}; +struct kvm_s2_mmu {struct kvm_vmid vmid;
@@ -227,6 +238,9 @@ struct kvm_s2_mmu {*/ bool pending_unmap; +/* Guest s2 mapping records indexed in this MMU's IPA space. */ +struct rb_root_cached guest_s2_mappings; +/* * 0: Nobody is currently using this, check vttbr for validity * >0: Somebody is actively using this.
@@ -326,6 +340,9 @@ struct kvm_arch {size_t nested_mmus_size;int nested_mmus_next; +/* Guest s2 tracking trees access serialization. */ +spinlock_t guest_s2_tracking_lock; +/* Interrupt controller */ struct vgic_dist vgic;
arch/arm64/kvm/mmu.c +30 -0
@@ -7,6 +7,7 @@#include <linux/acpi.h> #include <linux/mman.h> #include <linux/kvm_host.h> +#include <linux/interval_tree.h> #include <linux/io.h> #include <linux/hugetlb.h> #include <linux/sched/signal.h>
@@ -1033,6 +1034,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t mmu->pgd_phys =__pa (pgt->pgd); + mmu->guest_s2_mappings =RB_ROOT_CACHED ; +if (kvm_is_nested_s2_mmu (kvm, mmu))kvm_init_nested_s2_mmu (mmu);
@@ -1122,10 +1125,32 @@ void stage2_unmap_vm(struct kvm *kvm)srcu_read_unlock (&kvm->srcu, idx); } +static void guest_s2_tracking_destroy (struct kvm_s2_mmu *mmu, +struct rb_root_cached *tree) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); +struct kvm_guest_s2_mapping *mapping; +struct interval_tree_node *node; + +while ((node =interval_tree_iter_first (tree,0 ,ULONG_MAX ))) { +interval_tree_remove (node, tree); + +if (!kvm_is_nested_s2_mmu (kvm, mmu)) { + mapping = container_of(node, struct kvm_guest_s2_mapping, + canonical); +/* The canonical MMU is destroyed after the nested MMUs. */ +kfree (mapping); + } + +cond_resched (); + } +} +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 rb_root_cached mappings_tree;write_lock (&kvm->mmu_lock); pgt = mmu->pgt;
@@ -1138,12 +1163,17 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)if (kvm_is_nested_s2_mmu (kvm, mmu))kvm_init_nested_s2_mmu (mmu); + mappings_tree = mmu->guest_s2_mappings; + mmu->guest_s2_mappings =RB_ROOT_CACHED ; +write_unlock (&kvm->mmu_lock);if (pgt) {kvm_stage2_destroy (pgt);kfree (pgt); } + +guest_s2_tracking_destroy (mmu, &mappings_tree); }static void hyp_mc_free_fn (void *addr,void *mc)
arch/arm64/kvm/nested.c +1 -0
@@ -49,6 +49,7 @@ void kvm_init_nested(struct kvm *kvm) kvm->arch.nested_mmus =NULL ; kvm->arch.nested_mmus_size =0 ;atomic_set (&kvm->arch.vncr_map_count,0 ); +spin_lock_init (&kvm->arch.guest_s2_tracking_lock); }static int init_nested_s2_mmu (struct kvm *kvm,struct kvm_s2_mmu *mmu)
Signature · 1 line
2.43.0
Re: [PATCH v5 2/6] KVM: arm64: nv: Introduce guest stage-2 tracking structures
Quoted 63 lines · Wei-Lin Chang
In order to avoid unmapping all shadow stage-2 mappings when KVM receives a MMU notifier unmap call, we have to keep track of the canonical IPA -> nested IPA relationship of the shadow mappings created. This essentially means tracking the guest's stage-2.
To do this, represent each mapping by struct
kvm_guest_s2_mapping. It stores the mapping's canonical IPA range and the nested IPA range using two interval tree nodes. Both nodes will be inserted into their respective interval trees calledguest_s2_mappings. The canonical IPA ranges will be stored in the tree within the canonical MMU, and the nested IPA ranges will be stored in the corresponding nested MMU's tree.For example:
struct
kvm_guest_s2_mappingmapping1, mapping2;---------------------> mapping2.canonical
| mapping1.canonical
| ^ (both stored in canonical mmu's tree)
| |
--*****-----------------------*****----------- CIPA
\\\\\ ||||| mapping1.nested_mmu
\\\\\ \\\\\ |
\\\\\ \\\\\ v
------\\\\\---------------------*****--------- NIPA #1 (nested mmu #1)\\\\\ | \\\\\ -> mapping1.nested \\\\\ (stored in nested mmu #1's tree) \\\\\-----------*****------------------------------ NIPA #2 (nested mmu #2)
| ^
-> mapping2.nested |
(stored in nested mmu #2's tree) mapping2.nested_mmuUsing the trees we can look up nodes in either of the IPA spaces, and for each node, find the corresponding range in the other IPA space from the other node in the enclosing
kvm_guest_s2_mapping.Define
kvm_guest_s2_mappingand the interval tree here. Guest stage-2 mapping tracking will come in subsequent patches.Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/arm64/kvm/mmu.c | 30 ++++++++++++++++++++++++++++++ arch/arm64/kvm/nested.c | 1 + 3 files changed, 48 insertions(+)arch/arm64/include/asm/kvm_host.h +1
-0@@ -14,6 +14,7 @@#include <linux/arm-smccc.h> #include <linux/bitmap.h> #include <linux/types.h> +#include <linux/interval_tree.h> #include <linux/jump_label.h> #include <linux/kvm_types.h> #include <linux/maple_tree.h> @@ -150,6 +151,16 @@ struct kvm_vmid {atomic64_t id; };
arch/arm64/include/asm/kvm_host.h struct kvm_vmid { · line 162
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +9
-0+/* +* Record of a guest stage-2 mapping, storing canonical and nested IPA +* ranges. Both ranges have the same size. +*/ +struct kvm_guest_s2_mapping { +struct interval_tree_node canonical; +struct interval_tree_node nested; +struct kvm_s2_mmu *nested_mmu; +};
Is this to be used for normal (L1) guests? I guess this series is for shadow stage 2 unmapping optimization, so not sure.
Thanks,
Itaru.
Quoted 111 lines
arch/arm64/include/asm/kvm_host.h +7
-0+struct kvm_s2_mmu {struct kvm_vmid vmid;@@ -227,6 +238,9 @@ struct kvm_s2_mmu {*/ bool pending_unmap; +/* Guest s2 mapping records indexed in this MMU's IPA space. */ +struct rb_root_cached guest_s2_mappings; +/* * 0: Nobody is currently using this, check vttbr for validity * >0: Somebody is actively using this. @@ -326,6 +340,9 @@ struct kvm_arch {size_t nested_mmus_size;int nested_mmus_next; +/* Guest s2 tracking trees access serialization. */ +spinlock_t guest_s2_tracking_lock; +/* Interrupt controller */ struct vgic_dist vgic;arch/arm64/kvm/mmu.c +30
-0@@ -7,6 +7,7 @@#include <linux/acpi.h> #include <linux/mman.h> #include <linux/kvm_host.h> +#include <linux/interval_tree.h> #include <linux/io.h> #include <linux/hugetlb.h> #include <linux/sched/signal.h> @@ -1033,6 +1034,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t mmu->pgd_phys =__pa (pgt->pgd); + mmu->guest_s2_mappings =RB_ROOT_CACHED ; +if (kvm_is_nested_s2_mmu (kvm, mmu))kvm_init_nested_s2_mmu (mmu);@@ -1122,10 +1125,32 @@ void stage2_unmap_vm(struct kvm *kvm)srcu_read_unlock (&kvm->srcu, idx); } +static void guest_s2_tracking_destroy (struct kvm_s2_mmu *mmu, +struct rb_root_cached *tree) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); +struct kvm_guest_s2_mapping *mapping; +struct interval_tree_node *node; + +while ((node =interval_tree_iter_first (tree,0 ,ULONG_MAX ))) { +interval_tree_remove (node, tree); + +if (!kvm_is_nested_s2_mmu (kvm, mmu)) { + mapping = container_of(node, struct kvm_guest_s2_mapping, + canonical); +/* The canonical MMU is destroyed after the nested MMUs. */ +kfree (mapping); + } + +cond_resched (); + } +} +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 rb_root_cached mappings_tree;write_lock (&kvm->mmu_lock); pgt = mmu->pgt;@@ -1138,12 +1163,17 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)if (kvm_is_nested_s2_mmu (kvm, mmu))kvm_init_nested_s2_mmu (mmu); + mappings_tree = mmu->guest_s2_mappings; + mmu->guest_s2_mappings =RB_ROOT_CACHED ; +write_unlock (&kvm->mmu_lock);if (pgt) {kvm_stage2_destroy (pgt);kfree (pgt); } + +guest_s2_tracking_destroy (mmu, &mappings_tree); }static void hyp_mc_free_fn (void *addr,void *mc)arch/arm64/kvm/nested.c +1
-0@@ -49,6 +49,7 @@ void kvm_init_nested(struct kvm *kvm) kvm->arch.nested_mmus =NULL ; kvm->arch.nested_mmus_size =0 ;atomic_set (&kvm->arch.vncr_map_count,0 ); +spin_lock_init (&kvm->arch.guest_s2_tracking_lock); }static int init_nested_s2_mmu (struct kvm *kvm,struct kvm_s2_mmu *mmu)Signature · 1 line
2.43.0
Re: [PATCH v5 2/6] KVM: arm64: nv: Introduce guest stage-2 tracking structures
Hi,
Quoted 44 lines · Itaru Kitayama
Quoted 43 lines · Wei-Lin Chang
In order to avoid unmapping all shadow stage-2 mappings when KVM receives a MMU notifier unmap call, we have to keep track of the canonical IPA -> nested IPA relationship of the shadow mappings created. This essentially means tracking the guest's stage-2.
To do this, represent each mapping by struct
kvm_guest_s2_mapping. It stores the mapping's canonical IPA range and the nested IPA range using two interval tree nodes. Both nodes will be inserted into their respective interval trees calledguest_s2_mappings. The canonical IPA ranges will be stored in the tree within the canonical MMU, and the nested IPA ranges will be stored in the corresponding nested MMU's tree.For example:
struct
kvm_guest_s2_mappingmapping1, mapping2;---------------------> mapping2.canonical
| mapping1.canonical
| ^ (both stored in canonical mmu's tree)
| |
--*****-----------------------*****----------- CIPA
\\\\\ ||||| mapping1.nested_mmu
\\\\\ \\\\\ |
\\\\\ \\\\\ v
------\\\\\---------------------*****--------- NIPA #1 (nested mmu #1)\\\\\ | \\\\\ -> mapping1.nested \\\\\ (stored in nested mmu #1's tree) \\\\\-----------*****------------------------------ NIPA #2 (nested mmu #2)
| ^
-> mapping2.nested |
(stored in nested mmu #2's tree) mapping2.nested_mmuUsing the trees we can look up nodes in either of the IPA spaces, and for each node, find the corresponding range in the other IPA space from the other node in the enclosing
kvm_guest_s2_mapping.Define
kvm_guest_s2_mappingand the interval tree here. Guest stage-2 mapping tracking will come in subsequent patches.Signed-off-by Wei-Lin Chang <[email protected]>---
[...]
Quoted 13 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vmid { · line 162
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +9
-0+/* +* Record of a guest stage-2 mapping, storing canonical and nested IPA +* ranges. Both ranges have the same size. +*/ +struct kvm_guest_s2_mapping { +struct interval_tree_node canonical; +struct interval_tree_node nested; +struct kvm_s2_mmu *nested_mmu; +};Is this to be used for normal (L1) guests? I guess this series is for shadow stage 2 unmapping optimization, so not sure.
Sorry I don't totally understand your question. Yes this series is optimizing cases where a GPA (L1's PA) range have to be unmapped, from an MMU notifier unmap call.
When we want to unmap a GPA range, the corresponding L2PAs must also be unmapped from the shadow page tables. Before this series there is no way of knowing what L2PAs are affected, so the code just calls kvm_nested_s2_unmap() to unmap all shadow mappings.
Each struct kvm_guest_s2_mapping instance keeps one L1PA <-> L2PA mapping record. For example:
canonical (L1PA space): [x, x+4K) <- node stored in canonical MMU tree nested (L2PA space): [y, y+4K) <- node stored in nested MMU tree
Does this make sense?
Thanks,
Wei-Lin Chang
Quoted 4 lines
Thanks,
Itaru.
[...]
Re: [PATCH v5 2/6] KVM: arm64: nv: Introduce guest stage-2 tracking structures
Quoted 73 lines · Wei-Lin Chang
Hi,
Quoted 44 lines · Itaru Kitayama
Quoted 43 lines · Wei-Lin Chang
In order to avoid unmapping all shadow stage-2 mappings when KVM receives a MMU notifier unmap call, we have to keep track of the canonical IPA -> nested IPA relationship of the shadow mappings created. This essentially means tracking the guest's stage-2.
To do this, represent each mapping by struct
kvm_guest_s2_mapping. It stores the mapping's canonical IPA range and the nested IPA range using two interval tree nodes. Both nodes will be inserted into their respective interval trees calledguest_s2_mappings. The canonical IPA ranges will be stored in the tree within the canonical MMU, and the nested IPA ranges will be stored in the corresponding nested MMU's tree.For example:
struct
kvm_guest_s2_mappingmapping1, mapping2;---------------------> mapping2.canonical
| mapping1.canonical
| ^ (both stored in canonical mmu's tree)
| |
--*****-----------------------*****----------- CIPA
\\\\\ ||||| mapping1.nested_mmu
\\\\\ \\\\\ |
\\\\\ \\\\\ v
------\\\\\---------------------*****--------- NIPA #1 (nested mmu #1)\\\\\ | \\\\\ -> mapping1.nested \\\\\ (stored in nested mmu #1's tree) \\\\\-----------*****------------------------------ NIPA #2 (nested mmu #2)
| ^
-> mapping2.nested |
(stored in nested mmu #2's tree) mapping2.nested_mmuUsing the trees we can look up nodes in either of the IPA spaces, and for each node, find the corresponding range in the other IPA space from the other node in the enclosing
kvm_guest_s2_mapping.Define
kvm_guest_s2_mappingand the interval tree here. Guest stage-2 mapping tracking will come in subsequent patches.Signed-off-by Wei-Lin Chang <[email protected]>---
[...]
Quoted 13 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vmid { · line 162
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +9
-0+/* +* Record of a guest stage-2 mapping, storing canonical and nested IPA +* ranges. Both ranges have the same size. +*/ +struct kvm_guest_s2_mapping { +struct interval_tree_node canonical; +struct interval_tree_node nested; +struct kvm_s2_mmu *nested_mmu; +};Is this to be used for normal (L1) guests? I guess this series is for shadow stage 2 unmapping optimization, so not sure.
Sorry I don't totally understand your question. Yes this series is optimizing cases where a GPA (L1's PA) range have to be unmapped, from an MMU notifier unmap call.
When we want to unmap a GPA range, the corresponding L2PAs must also be unmapped from the shadow page tables. Before this series there is no way of knowing what L2PAs are affected, so the code just calls
kvm_nested_s2_unmap()to unmap all shadow mappings.
Quoted 7 lines
Each struct
kvm_guest_s2_mappinginstance keeps one L1PA <-> L2PA mapping record. For example:canonical (L1PA space): [x, x+4K) <- node stored in canonical MMU tree nested (L2PA space): [y, y+4K) <- node stored in nested MMU treeDoes this make sense?
Yes, makes sense. I was mostly wondering about the sturct name, kvm_guest_s2_mapping you introduced, since this is only needed for a guest acting as a hypervisor. No strong opinion.
Thanks,
Itaru.
Quoted 10 lines
Thanks,
Wei-Lin ChangQuoted 4 lines
Thanks,
Itaru.[...]
Marc Zyngier
maintainerRe: [PATCH v5 2/6] KVM: arm64: nv: Introduce guest stage-2 tracking structures
In reply to Wei-Lin ChangQuoted 65 lines · Wei-Lin Chang
In order to avoid unmapping all shadow stage-2 mappings when KVM receives a MMU notifier unmap call, we have to keep track of the canonical IPA -> nested IPA relationship of the shadow mappings created. This essentially means tracking the guest's stage-2.
To do this, represent each mapping by struct
kvm_guest_s2_mapping. It stores the mapping's canonical IPA range and the nested IPA range using two interval tree nodes. Both nodes will be inserted into their respective interval trees calledguest_s2_mappings. The canonical IPA ranges will be stored in the tree within the canonical MMU, and the nested IPA ranges will be stored in the corresponding nested MMU's tree.For example:
struct
kvm_guest_s2_mappingmapping1, mapping2;---------------------> mapping2.canonical
| mapping1.canonical
| ^ (both stored in canonical mmu's tree)
| |
--*****-----------------------*****----------- CIPA
\\\\\ ||||| mapping1.nested_mmu
\\\\\ \\\\\ |
\\\\\ \\\\\ v
------\\\\\---------------------*****--------- NIPA #1 (nested mmu #1)\\\\\ | \\\\\ -> mapping1.nested \\\\\ (stored in nested mmu #1's tree) \\\\\-----------*****------------------------------ NIPA #2 (nested mmu #2)
| ^
-> mapping2.nested |
(stored in nested mmu #2's tree) mapping2.nested_mmuUsing the trees we can look up nodes in either of the IPA spaces, and for each node, find the corresponding range in the other IPA space from the other node in the enclosing
kvm_guest_s2_mapping.Define
kvm_guest_s2_mappingand the interval tree here. Guest stage-2 mapping tracking will come in subsequent patches.Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/arm64/kvm/mmu.c | 30 ++++++++++++++++++++++++++++++ arch/arm64/kvm/nested.c | 1 + 3 files changed, 48 insertions(+)arch/arm64/include/asm/kvm_host.h +1
-0@@ -14,6 +14,7 @@#include <linux/arm-smccc.h> #include <linux/bitmap.h> #include <linux/types.h> +#include <linux/interval_tree.h> #include <linux/jump_label.h> #include <linux/kvm_types.h> #include <linux/maple_tree.h> @@ -150,6 +151,16 @@ struct kvm_vmid {atomic64_t id; };
arch/arm64/include/asm/kvm_host.h struct kvm_vmid { · line 163
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10
-0+/* +* Record of a guest stage-2 mapping, storing canonical and nested IPA +* ranges. Both ranges have the same size. +*/ +struct kvm_guest_s2_mapping { +struct interval_tree_node canonical; +struct interval_tree_node nested; +struct kvm_s2_mmu *nested_mmu; +}; +
I'm trying hard to find a way to reduce the size of this structure, because this is IMO the only real problem with this approach.
Obviously, the only thing we could kill is this nested_mmu field, as everything else is used by the interval trees. I can see two ugly ways to do that:
- either we iterate over all shadow MMUs to find the corresponding 'nested' node: really costly if we have a lot of mappings and/or a lot of shadow MMUs
- or we steal bits from the interval_tree_node to encode extra information. One realisation is that all addresses are PAGE_SIZE aligned, meaning that we have at least 12 bits that are always 0. We could, for example, encode an index in the bottom bits of the nested.start field. Probably easy enough, but may require some careful masking (and the addition of an index in the s2_mmu structure).
The result would be significant, as we could then use a 96 byte slab, which means the cost of a 1GB @4k granularity could fall to 24MB.
I don't think this is an immediate blocker for this series, but I'd like to at least have a plan...
Thoughts?
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Re: [PATCH v5 2/6] KVM: arm64: nv: Introduce guest stage-2 tracking structures
On Sun, Sep 06, 2026 at 04:47:02PM +0100, Marc Zyngier wrote:
[...]
Quoted 30 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vmid { · line 163
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10
-0+/* +* Record of a guest stage-2 mapping, storing canonical and nested IPA +* ranges. Both ranges have the same size. +*/ +struct kvm_guest_s2_mapping { +struct interval_tree_node canonical; +struct interval_tree_node nested; +struct kvm_s2_mmu *nested_mmu; +}; +I'm trying hard to find a way to reduce the size of this structure, because this is IMO the only real problem with this approach.
Obviously, the only thing we could kill is this
nested_mmufield, as everything else is used by the interval trees. I can see two ugly ways to do that:- either we iterate over all shadow MMUs to find the corresponding 'nested' node: really costly if we have a lot of mappings and/or a lot of shadow MMUs
- or we steal bits from the
interval_tree_nodeto encode extra information. One realisation is that all addresses arePAGE_SIZEaligned, meaning that we have at least 12 bits that are always 0. We could, for example, encode an index in the bottom bits of the nested.start field. Probably easy enough, but may require some careful masking (and the addition of an index in thes2_mmustructure).
Quoted 2 lines
The result would be significant, as we could then use a 96 byte slab, which means the cost of a 1GB @4k granularity could fall to 24MB.
One way of meeting in the middle is to keep the nested_mmu pointer, and use a dedicated kmem cache. That cache will then hand out exact 104-byte objects.
That's 26MB for 1GB @4K.
This is a nice improvement from the current 128-byte allocations. Although it's not as good as 96-byte allocations, the code logic can remain simple. Would you take this approach?
Thanks,
Wei-Lin Chang
[...]
Marc Zyngier
maintainerRe: [PATCH v5 2/6] KVM: arm64: nv: Introduce guest stage-2 tracking structures
Quoted 42 lines · Wei-Lin Chang
On Sun, Sep 06, 2026 at 04:47:02PM +0100, Marc Zyngier wrote:
[...]
Quoted 30 lines
Review
Quoted 10 lines
(quoted diff) +10
-0+/* + * Record of a guest stage-2 mapping, storing canonical and nested IPA + * ranges. Both ranges have the same size. + */ +struct kvm_guest_s2_mapping { + struct interval_tree_node canonical; + struct interval_tree_node nested; + struct kvm_s2_mmu *nested_mmu; +}; +I'm trying hard to find a way to reduce the size of this structure, because this is IMO the only real problem with this approach.
Obviously, the only thing we could kill is this
nested_mmufield, as everything else is used by the interval trees. I can see two ugly ways to do that:- either we iterate over all shadow MMUs to find the corresponding 'nested' node: really costly if we have a lot of mappings and/or a lot of shadow MMUs
- or we steal bits from the
interval_tree_nodeto encode extra information. One realisation is that all addresses arePAGE_SIZEaligned, meaning that we have at least 12 bits that are always 0. We could, for example, encode an index in the bottom bits of the nested.start field. Probably easy enough, but may require some careful masking (and the addition of an index in thes2_mmustructure).Quoted 2 lines
The result would be significant, as we could then use a 96 byte slab, which means the cost of a 1GB @4k granularity could fall to 24MB.
One way of meeting in the middle is to keep the
nested_mmupointer, and use a dedicated kmem cache. That cache will then hand out exact 104-byte objects.
Quoted 5 lines
That's 26MB for 1GB @4K.
This is a nice improvement from the current 128-byte allocations. Although it's not as good as 96-byte allocations, the code logic can remain simple. Would you take this approach?
I don't think the logic has to be complicated to go all the way to 96 bytes, see the patch below. I hope we can further trim it over time, but this would be a good start.
Thanks,
M.
From 39e3bdaa1b02c460a1001a23a20dccca5501eb6d Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Tue, 8 Sep 2026 17:07:50 +0100
Subject: [PATCH] KVM: arm64: Drop kvm_s2_mmu pointer from kvm_guest_s2_mapping
As it appears that the kvm_guest_s2_mapping structure is quite large, and results in a 128 byte slab allocation, there is some insentive to shrink a bit.
For this, replace the S2 MMU back-pointer with an index tucked into the low bits of the nested.start field. This allows us to shrink the structure by 8 bytes, and therefore to fit in a 96 byte slab.
The index is also stored in the S2 MMU structure itself, which comes for free as it fits in an existing hole. The structure is also repacked to avoid other disgracious holes.
--- arch/arm64/include/asm/kvm_host.h | 16 ++++++----- arch/arm64/kvm/nested.c | 44 +++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_host.h +9 -7
@@ -158,7 +158,6 @@ struct kvm_vmid {struct kvm_guest_s2_mapping {struct interval_tree_node canonical;struct interval_tree_node nested; -struct kvm_s2_mmu *nested_mmu; };struct kvm_s2_mmu {
@@ -222,30 +221,33 @@ struct kvm_s2_mmu {u64 tlb_vttbr;u64 tlb_vtcr; +/* Guest s2 mapping records indexed in this MMU's IPA space. */ +struct rb_root_cached guest_s2_mappings; +/* * true when this represents a nested context where virtual * HCR_EL2.VM == 1 */ 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; -/* Guest s2 mapping records indexed in this MMU's IPA space. */ -struct rb_root_cached guest_s2_mappings; +/* Index in the S2 MMU array, only valid for a shadow S2 */ +u16 s2_mmu_idx;/* * 0: Nobody is currently using this, check vttbr for validity * >0: Somebody is actively using this. */ atomic_t refcnt; + +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +struct dentry *shadow_pt_debugfs_dentry; +#endif };struct kvm_arch_memory_slot {
arch/arm64/kvm/nested.c +36 -8
85 lines not on this page · raw message
Signature · 4 lines
2.47.3 -- Without deviation from the norm, progress is not possible.
PATCH v5 3/6 KVM: arm64: nv: Track guest stage-2 mapping creation
In reply to Wei-Lin ChangDuring shadow stage-2 faults, in addition to creating mappings in the shadow page tables, also allocate kvm_guest_s2_mapping objects, record the mapping ranges, and insert them into the canonical and nested mmu's guest_s2_mappings tree.
Note that because we allow parallel faulting, the interval trees could store mappings that are not live in the shadow page tables. Storing a superset of the live mappings is fine because we will only over-unmap when we use this information later to do the targeted MMU notifier unmap.
--- arch/arm64/include/asm/kvm_nested.h | 3 +++ arch/arm64/kvm/mmu.c | 29 +++++++++++++++++++++++++++++ arch/arm64/kvm/nested.c | 25 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+)
arch/arm64/include/asm/kvm_nested.h +3 -0
@@ -77,6 +77,9 @@ 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_guest_s2_mapping (struct kvm_s2_mmu *mmu,gpa_t canonical_ipa, +gpa_t nested_ipa,size_t map_size, +struct kvm_guest_s2_mapping *mapping);extern void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu);extern void kvm_vcpu_put_hw_mmu (struct kvm_vcpu *vcpu);
arch/arm64/kvm/mmu.c +29 -0
@@ -1642,6 +1642,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ;enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ;struct kvm_pgtable *pgt = s2fd->vcpu->arch.hw_mmu->pgt; +struct kvm_guest_s2_mapping *mapping =NULL ;unsigned long mmu_seq;struct page *page;struct kvm *kvm = s2fd->vcpu->kvm;
@@ -1655,6 +1656,11 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) ret =topup_mmu_memcache (s2fd->vcpu, memcache);if (ret)return ret; +if (kvm_is_nested_s2_mmu (kvm, pgt->mmu)) { + mapping = kmalloc_obj(struct kvm_guest_s2_mapping, GFP_KERNEL_ACCOUNT); +if (!mapping) +return -ENOMEM ; + } }if (s2fd->nested)
@@ -1675,6 +1681,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)if (ret) {kvm_prepare_memory_fault_exit (s2fd->vcpu, s2fd->fault_ipa,PAGE_SIZE , write_fault, exec_fault, false); +kfree (mapping);return ret; }
@@ -1708,11 +1715,17 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt, s2fd->fault_ipa,PAGE_SIZE ,__pfn_to_phys (pfn), prot, memcache, flags); +if (!ret &&kvm_is_nested_s2_mmu (kvm, pgt->mmu)) { +kvm_record_guest_s2_mapping (pgt->mmu, gfn <<PAGE_SHIFT , + s2fd->fault_ipa,PAGE_SIZE , mapping); + mapping =NULL ; + } } out_unlock:kvm_release_faultin_page (kvm, page, !!ret, prot &KVM_PGTABLE_PROT_W );kvm_fault_unlock (kvm); +kfree (mapping);if ((prot &KVM_PGTABLE_PROT_W ) && !ret)mark_page_dirty_in_slot (kvm, s2fd->memslot, gfn);
@@ -2049,6 +2062,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, void *memcache) {enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ; +struct kvm_guest_s2_mapping *mapping =NULL ;bool writable = prot &KVM_PGTABLE_PROT_W ;struct kvm *kvm = s2fd->vcpu->kvm;phys_addr_t canonical_ipa;
@@ -2059,6 +2073,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,gfn_t gfn;int ret; +if (kvm_is_nested_s2_mmu (kvm, s2fd->vcpu->arch.hw_mmu)) { + mapping = kmalloc_obj(struct kvm_guest_s2_mapping, +GFP_KERNEL_ACCOUNT ); +if (!mapping) { +kvm_release_page_unused (s2vi->page); +return -ENOMEM ; + } + } +kvm_fault_lock (kvm); pgt = s2fd->vcpu->arch.hw_mmu->pgt; ret = -EAGAIN ;
@@ -2112,11 +2135,17 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, ret =KVM_PGT_FN (kvm_pgtable_stage2_map)(pgt,gfn_to_gpa (gfn), mapping_size,__pfn_to_phys (pfn), prot, memcache, flags); +if (!ret &&kvm_is_nested_s2_mmu (kvm, pgt->mmu)) { +kvm_record_guest_s2_mapping (pgt->mmu, canonical_ipa, +gfn_to_gpa (gfn), mapping_size, mapping); + mapping =NULL ; + } } out_unlock:kvm_release_faultin_page (kvm, s2vi->page, !!ret, writable);kvm_fault_unlock (kvm); +kfree (mapping);/* * Mark the page dirty only if the fault is handled successfully,
arch/arm64/kvm/nested.c +25 -0
@@ -5,6 +5,7 @@*/ #include <linux/bitfield.h> +#include <linux/interval_tree.h> #include <linux/kvm.h> #include <linux/kvm_host.h>
@@ -852,6 +853,30 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)return s2_mmu; } +void kvm_record_guest_s2_mapping (struct kvm_s2_mmu *mmu,gpa_t canonical_ipa, +gpa_t nested_ipa,size_t map_size, +struct kvm_guest_s2_mapping *mapping) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); + +lockdep_assert_held_read (&kvm->mmu_lock); + +if (WARN_ON (!IS_ALIGNED (canonical_ipa, map_size))) + canonical_ipa =ALIGN_DOWN (canonical_ipa, map_size); + + mapping->canonical.start = canonical_ipa; + mapping->canonical.last = canonical_ipa + map_size -1 ; + + mapping->nested.start = nested_ipa; + mapping->nested.last = nested_ipa + map_size -1 ; + + mapping->nested_mmu = mmu; + +guard (spinlock)(&kvm->arch.guest_s2_tracking_lock); +interval_tree_insert (&mapping->nested, &mmu->guest_s2_mappings); +interval_tree_insert (&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings); +} +void kvm_init_nested_s2_mmu (struct kvm_s2_mmu *mmu) {/* CnP being set denotes an invalid entry */
Signature · 1 line
2.43.0
PATCH v5 4/6 KVM: arm64: nv: Track guest stage-2 mapping removal
In reply to Wei-Lin Changkvm_stage2_unmap_range() is the helper to remove mappings from the stage-2 page tables. It is called during guest TLBI handling, memslot removal, nested mmu reuse, etc.
Teach it about the guest stage-2 tracking trees and remove mappings from there when shadow mappings are removed. This keeps the tracking trees from having stale mappings pile up.
--- arch/arm64/include/asm/kvm_host.h | 5 ++++- arch/arm64/include/asm/kvm_nested.h | 2 ++ arch/arm64/kvm/mmu.c | 23 +++++++++++++++++++++-- arch/arm64/kvm/nested.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_host.h +4 -1
@@ -340,7 +340,10 @@ struct kvm_arch {size_t nested_mmus_size;int nested_mmus_next; -/* Guest s2 tracking trees access serialization. */ +/* +* Serializes guest s2 tracking trees access when the mmu_lock +* is only held for read. +*/ spinlock_t guest_s2_tracking_lock;/* Interrupt controller */
arch/arm64/include/asm/kvm_nested.h +2 -0
@@ -80,6 +80,8 @@ extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,extern void kvm_record_guest_s2_mapping (struct kvm_s2_mmu *mmu,gpa_t canonical_ipa,gpa_t nested_ipa,size_t map_size,struct kvm_guest_s2_mapping *mapping); +extern void kvm_remove_guest_s2_mappings (struct kvm_s2_mmu *mmu, +gpa_t nipa,size_t size);extern void kvm_vcpu_load_hw_mmu (struct kvm_vcpu *vcpu);extern void kvm_vcpu_put_hw_mmu (struct kvm_vcpu *vcpu);
arch/arm64/kvm/mmu.c +21 -2
@@ -314,6 +314,19 @@ static void invalidate_icache_guest_page(void *va, size_t size)* we then fully enforce cacheability of RAM, no matter what the guest * does. */ + +static int kvm_pgtable_stage2_unmap_tracked (struct kvm_pgtable *pgt,u64 addr,u64 size) +{ +int ret; + + ret =kvm_pgtable_stage2_unmap (pgt, addr, size); +if (ret) +return ret; + +kvm_remove_guest_s2_mappings (pgt->mmu, addr, size); +return 0 ; +} +/** * __unmap_stage2_range -- Clear stage2 page table entries to unmap a range * @mmu: The KVM stage-2 MMU pointer
@@ -331,11 +344,17 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);phys_addr_t end = start + size; +int (*fn)(struct kvm_pgtable *,u64 ,u64 );lockdep_assert_held_write (&kvm->mmu_lock);WARN_ON (size & ~PAGE_MASK ); -WARN_ON (stage2_apply_range (mmu, start, end,KVM_PGT_FN (kvm_pgtable_stage2_unmap), - may_block)); + +if (kvm_is_nested_s2_mmu (kvm, mmu)) + fn = kvm_pgtable_stage2_unmap_tracked; +else + fn =KVM_PGT_FN (kvm_pgtable_stage2_unmap); + +WARN_ON (stage2_apply_range (mmu, start, end, fn, may_block)); } void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
arch/arm64/kvm/nested.c +28 -0
@@ -877,6 +877,34 @@ void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,interval_tree_insert (&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings); } +void kvm_remove_guest_s2_mappings (struct kvm_s2_mmu *mmu,gpa_t nipa, +size_t size) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); +struct interval_tree_node *node, *next; +struct kvm_guest_s2_mapping *mapping; +gpa_t nipa_end = nipa + size -1 ; + +/* +* Guest s2 tracking interval trees are only accessed while holding the +* mmu_lock, hence we don't have to take guest_s2_tracking_lock if the +* mmu_lock is held for write. +*/ +lockdep_assert_held_write (&kvm_s2_mmu_to_kvm (mmu)->mmu_lock); + + node =interval_tree_iter_first (&mmu->guest_s2_mappings, nipa, nipa_end); +while (node) { + next =interval_tree_iter_next (node, nipa, nipa_end); + mapping = container_of(node, struct kvm_guest_s2_mapping, + nested); +interval_tree_remove (&mapping->nested, &mmu->guest_s2_mappings); +interval_tree_remove (&mapping->canonical, + &kvm->arch.mmu.guest_s2_mappings); +kfree (mapping); + node = next; + } +} +void kvm_init_nested_s2_mmu (struct kvm_s2_mmu *mmu) {/* CnP being set denotes an invalid entry */
Signature · 1 line
2.43.0
PATCH v5 5/6 KVM: arm64: nv: Avoid full shadow stage-2 unmap
In reply to Wei-Lin ChangWith guest stage-2 tracking in place, we can improve MMU notifier unmaps from unmapping all existing shadow stage-2 mappings to only unmapping the ones affected by the given canonical IPA range.
--- arch/arm64/include/asm/kvm_nested.h | 2 ++ arch/arm64/kvm/mmu.c | 7 +++-- arch/arm64/kvm/nested.c | 47 +++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_nested.h +2 -0
@@ -170,6 +170,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_nested_unmap_cipa_range (struct kvm *kvm,gpa_t cipa, +size_t unmap_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 +4 -3
@@ -2518,8 +2518,9 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)__unmap_stage2_range (&kvm->arch.mmu, range->start <<PAGE_SHIFT , (range->end - range->start) <<PAGE_SHIFT , range->may_block); - -kvm_nested_s2_unmap (kvm, range->may_block); +kvm_nested_unmap_cipa_range (kvm, range->start <<PAGE_SHIFT , + (range->end - range->start) <<PAGE_SHIFT , + range->may_block);return false; }
@@ -2797,7 +2798,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_nested_unmap_cipa_range (kvm, gpa, size, true);write_unlock (&kvm->mmu_lock); }
arch/arm64/kvm/nested.c +44 -3
59 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v5 6/6 KVM: arm64: Refactor kvm_unmap_gfn_range() with common variables
In reply to Wei-Lin Chang__unmap_stage2_range() and kvm_nested_unmap_cipa_range() are using the same arguments. Clean this up by using local variables.
--- arch/arm64/kvm/mmu.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +7 -6
@@ -2512,15 +2512,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); -kvm_nested_unmap_cipa_range (kvm, range->start <<PAGE_SHIFT , - (range->end - range->start) <<PAGE_SHIFT , - range->may_block); +__unmap_stage2_range (&kvm->arch.mmu, gpa, size, may_block); +kvm_nested_unmap_cipa_range (kvm, gpa, size, may_block); +return false; }
Signature · 1 line
2.43.0
Quoted 4 lines · Wei-Lin Chang
Hi,
This is v5 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
I've tested your series v5 on a Grace system. L2 booted into prompt with the Ubuntu filesystem image, your two kvm selftest for nested virtualization ran fine in L1, and also did stress-ng in L1:
projects $ sudo stress-ng --kvm 16 --cpu 16 --vm 8 --vma 8 --fork 8 --timeout 10m --verify --metrics-brief
stress-ng: info: [1053] setting to a 10 mins run per stressor
stress-ng: info: [1053] dispatching hogs: 16 kvm, 16 cpu, 8 vm, 8 vma, 8 fork
stress-ng: info: [1087] vm: using 32MB per stressor instance (total 256MB of 2.75GB available memory)
stress-ng: metrc: [1053] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s
stress-ng: metrc: [1053] (secs) (secs) (secs) (real time) (usr+sys time)
stress-ng: metrc: [1053] kvm 81 602.31 250.98 357.55 0.13 0.13
stress-ng: metrc: [1053] cpu 63310 595.70 166.40 0.60 106.28 379.12
stress-ng: metrc: [1053] vm 4252063 600.90 38.98 56.59 7076.16 44491.45
stress-ng: metrc: [1053] vma 56633 601.76 12.64 157.96 94.11 331.97
stress-ng: metrc: [1053] fork 149 600.98 0.03 0.45 0.25 314.59
stress-ng: info: [1053] skipped: 0
stress-ng: info: [1053] passed: 56: kvm (16) cpu (16) vm (8) vma (8) fork (8)
stress-ng: info: [1053] failed: 0
stress-ng: info: [1053] metrics untrustworthy: 0
stress-ng: info: [1053] successful run completed in 10 mins 8.62 secs
Thanks,
Itaru.
Quoted 130 lines
This time, a major overhaul is done to the implementation. After receiving some suggestions from Marc, I have identified that using the interval tree to store the guest stage-2 mappings solves many problems compared to using the maple tree.
Interval Tree vs Maple Tree
===========================First of all, interval trees are capable of storing overlapping ranges, which is helpful when the L1 hypervisor maps something like:
nested IPA [x, x+4K) -> canonical IPA [a, a+4K)
nested IPA [y, y+2M) -> canonical IPA [a, a+2M)No problems with storing that in the interval tree with different nodes. We can avoid the maple tree
UNKNOWN_IPAmechanism as a compromise.Second, ideally we would want to save the canonical IPA <-> nested IPA mapping in both directions to allow MMU notifier unmap speed up, and stale shadow mapping removals. If we use the maple tree, we'll have to have 2 separate trees, and make sure they store the same mappings, which isn't simple given the first point.
On the other hand, by using this pattern:
/* Record of a guest stage-2 mapping. */
structkvm_guest_s2_mapping{
structinterval_tree_nodecanonical; // CIPA range of the mapping
structinterval_tree_nodenested; // NIPA range of the mapping
structkvm_s2_mmu*nested_mmu; // mmu of the NIPA space
};and equip each mmu with an interval tree storing mapping records corresponding to the IPA space it represents, we can insert the respective nodes into the canonical IPA tree, and the corresponding nested IPA tree. This makes it trivial to find the range of the other IPA space from a range in one IPA space.
Diagram to help understanding:
struct
kvm_guest_s2_mappingmapping1, mapping2;---------------------> mapping2.canonical
| mapping1.canonical
| ^ (both stored in canonical mmu's tree)
| |
--*****-----------------------*****----------- CIPA
\\\\\ ||||| mapping1.nested_mmu
\\\\\ \\\\\ |
\\\\\ \\\\\ v
------\\\\\---------------------*****--------- NIPA #1 (nested mmu #1)\\\\\ | \\\\\ -> mapping1.nested \\\\\ (stored in nested mmu #1's tree) \\\\\-----------*****------------------------------ NIPA #2 (nested mmu #2)
| ^
-> mapping2.nested |
(stored in nested mmu #2's tree) mapping2.nested_mmuThird, maple tree does its own memory allocation. In the KVM stage-2 fault path we only find out what the mapping ranges are after taking the KVM MMU lock, and the maple tree has to know the range and entry to be stored to preallocate, therefore in our case the maple tree is forced to only use
GFP_NOWAIT, which isn't the best. With the interval tree the user does the memory management, and we can just allocate before taking the locks.Locking
=======The
guest_s2_tracking_lockserializes accesses to the tracking interval trees. It is taken after themmu_lock. However in reality it is only taken after we take the readmmu_lockin the stage-2 fault path, as other accesses have the writemmu_lockalready. This saves us some manual lock/unlocks.vCPU Stage-2 Fault Scalability Reduction
========================================KVM/arm64 is able to handle stage-2 faults from multiple vCPUs in parallel, thanks to the engineering done to the s2 pgtable code. However to safely insert mappings into the interval trees we have to serialize using the
guest_s2_tracking_lock. We trade some performance in stage-2 fault for faster MMU notifier unmaps, and keeping the unaffected shadow mappings.Memory Usage
============Each interval tree node is 48 bytes, and a
kvm_guest_s2_mappingis 104 bytes, residing in 128-byte slab objects. Each shadow stage-2 fault requires onekvm_guest_s2_mappinginstance. This is 32MB for a fully 4KB mapped 1GB region, and 64KB for a 2MB mapped 1GB region.Series Structure
================Patch 1: Preparatory refactoring. Patch 2: Introduce data structures for guest stage-2 tracking. Patch 3-4: Guest stage-2 tracking addition and removal Patch 5: Avoid full unmap during MMU notifier unmap using the trackedguest stage-2 mapping information.
Patch 6: Minor clean up.As this is a complete rework, I will omit the change log this time. Series is based on v7.2-rc5.
Thanks!
Link to v4: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (6):
KVM: arm64: Use a variable for the canonical IPA inkvm_s2_fault_map()
KVM: arm64: nv: Introduce guest stage-2 tracking structures
KVM: arm64: nv: Track guest stage-2 mapping creation
KVM: arm64: nv: Track guest stage-2 mapping removal
KVM: arm64: nv: Avoid full shadow stage-2 unmap
KVM: arm64: Refactorkvm_unmap_gfn_range()with common variablesarch/arm64/include/asm/kvm_host.h | 20 ++++++ arch/arm64/include/asm/kvm_nested.h | 7 ++ arch/arm64/kvm/mmu.c | 105 ++++++++++++++++++++++++---- arch/arm64/kvm/nested.c | 95 +++++++++++++++++++++++++ 4 files changed, 215 insertions(+), 12 deletions(-)Signature · 1 line
2.43.0
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------
L0 kernel: Linux v7.2-rc6
L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64)
QEMU: 10.2.3
L0 NUMA balancing was enabled (/proc/sys/kernel/numa_balancing=1). The host was booted with kvm_arm.mode=nested.
This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
qemu-system-aarch64 -smp 8 -m 32G \
-machine virt,accel=kvm,gic-version=3,virtualization=on \
-cpu host -nographic -enable-kvm \
-drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \
-drive if=pflash,format=raw,file=pflash1_bak.img \
-drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \
-nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \
-serial mon:stdioWith upstream v7.2-rc6 (075b74841bd0065a3bda3440873c747938e69b68), L0 NUMA balancing enabled, and the same QEMU configuration, the L1 guest hung during boot. The original L1 console reported:
[ 76.595764] watchdog: BUG: soft lockup - CPU#1 stuck for 45s! [k8s-dqlite:3068] [ 76.595973] watchdog: BUG: soft lockup - CPU#0 stuck for 38s! [rs:main Q:Reg:1602] [ 76.596181] watchdog: BUG: soft lockup - CPU#7 stuck for 45s! [kubelite:2672] [ 76.596334] watchdog: BUG: soft lockup - CPU#5 stuck for 38s! [containerd:2375]
The corresponding L0 hung-task report was:
[Wed Sep 2 22:49:08 2026] INFO: task qemu-system-aar:14681 blocked in I/O wait for more than 120 seconds. [Wed Sep 2 22:49:08 2026] Tainted: G E N 7.2.0-rc6-poluted_opt-7-2-numa #10.al8 [Wed Sep 2 22:49:08 2026] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [Wed Sep 2 22:49:08 2026] task:qemu-system-aar state:D stack:0 pid:14681 tgid:14681 ppid:14680 task_flags:0x8400080 flags:0x00800000 [Wed Sep 2 22:49:08 2026] Call trace: [Wed Sep 2 22:49:08 2026] __switch_to+0x128/0x168 (T) [Wed Sep 2 22:49:08 2026] __schedule+0x278/0x910 [Wed Sep 2 22:49:08 2026] schedule+0x3c/0xe8 [Wed Sep 2 22:49:08 2026] io_schedule+0x44/0x68 [Wed Sep 2 22:49:08 2026] softleaf_entry_wait_on_locked+0x280/0x2d0 [Wed Sep 2 22:49:08 2026] migration_entry_wait+0xdc/0x140 [Wed Sep 2 22:49:08 2026] do_swap_page+0x834/0xd80 [Wed Sep 2 22:49:08 2026] handle_pte_fault+0x208/0x2b8 [Wed Sep 2 22:49:08 2026] __handle_mm_fault+0x228/0x528 [Wed Sep 2 22:49:08 2026] handle_mm_fault+0xdc/0x2d8 [Wed Sep 2 22:49:08 2026] do_page_fault+0x388/0x790 [Wed Sep 2 22:49:08 2026] do_translation_fault+0x4c/0x88 [Wed Sep 2 22:49:08 2026] do_mem_abort+0x4c/0xa0 [Wed Sep 2 22:49:08 2026] el0_da+0x54/0x178 [Wed Sep 2 22:49:08 2026] el0t_64_sync_handler+0xd0/0xe8 [Wed Sep 2 22:49:08 2026] el0t_64_sync+0x1ac/0x1b0
The same wait and stack were observed repeatedly; the hung-task report recurred after 241 and 362 seconds.
I applied the v5 series to the same v7.2-rc6 baseline. With L0 NUMA balancing still enabled and the same QEMU command line, the L1 guest booted normally and could be operated through the serial console. QEMU no longer hung. No new soft-lockup, hung-task, blocked-I/O, migration-entry-wait, or softleaf-entry-wait message was observed in the patched boot log.
As a control, the unmodified v7.2-rc6 kernel with /proc/sys/kernel/numa_balancing=0 also booted the L1 with the same QEMU configuration. This confirms that the patch removes the NUMA-balancing failure mode in this setup rather than merely changing the guest setup.
Thanks,
Wang Han
Marc Zyngier
maintainerQuoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6
L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64)
QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \
-machine virt,accel=kvm,gic-version=3,virtualization=on \
-cpu host -nographic -enable-kvm \
-drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \
-drive if=pflash,format=raw,file=pflash1_bak.img \
-drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \
-nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \
-serial mon:stdioPuzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
So what are your actual test conditions?
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6
L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64)
QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdio
Quoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes kvm_nested_s2_unmap() to iterate through the entire IPA space 8 times (-smp 8).
What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?
Thanks,
Wei-Lin Chang
Quoted 7 lines
So what are your actual test conditions?
M.Signature · 1 line
Jazz isn't dead. It just smells funny.
Quoted 57 lines · Wei-Lin Chang
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6 L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64) QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?
Quoted 8 lines
Thanks,
Wei-Lin ChangQuoted 5 lines
So what are your actual test conditions?
M.
Hi, Wei-Lin and Marc,
I was able to reproduce this issue and capture ftrace evidence that confirms the root cause. Below is the analysis, trace log, and timing data.
## Problem
Environment:
- Host (L0): ARM64, KVM with virtualization=on (nested virtualization)
- Guest (L1): Ubuntu 26.04, 8 vCPUs / 32 GB
- Host NUMA balancing enabled, numad active
When booting the L1 QEMU guest, the L1 kernel hits a soft lockup during early boot (~45 s):
[ 45.646468] watchdog: BUG: soft lockup - CPU#0 stuck for 32s! [kworker/0:2:330] [ 45.646882] watchdog: BUG: soft lockup - CPU#2 stuck for 29s! [snap:1146] [ 45.647093] watchdog: BUG: soft lockup - CPU#3 stuck for 29s! [snap:1141] [ 45.647242] watchdog: BUG: soft lockup - CPU#7 stuck for 29s! [snap:1144]
At the same time, L0 dmesg reports the QEMU main thread blocked in D-state for more than 120 s:
[11239.341817] INFO: task qemu-system-aar:170799 blocked in I/O wait for more than 120 seconds. ... softleaf_entry_wait_on_locked+0x280/0x2d0 migration_entry_wait+0xdc/0x140 do_swap_page+0x834/0xd80 handle_pte_fault+0x208/0x2b8 __handle_mm_fault+0x228/0x528 handle_mm_fault+0xdc/0x2d8 do_page_fault+0x244/0x790 do_translation_fault+0x4c/0x88 do_mem_abort+0x4c/0xa0 el1_abort+0x50/0x80 el1h_64_sync_handler+0x50/0x108 el1h_64_sync+0x80/0x88 do_sys_poll+0x224/0x290 __arm64_sys_ppoll+0xa4/0x130
Note: This is not a 100% reproducible failure. In my automated loop the hang reproduced on the 2nd boot attempt, but other attempts ran for 6–9 minutes without hitting it. The bug is clearly timing-dependent on NUMA migration activity during the L1 boot window.
## Root cause
The issue is in arch/arm64/kvm/nested.c:
void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block)
{
int i;
lockdep_assert_held_write(&kvm->mmu_lock);
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))
kvm_stage2_unmap_range(mmu, 0, kvm_phys_size(mmu), may_block);
}
kvm_invalidate_vncr_ipa(kvm, 0, BIT(kvm->arch.mmu.pgt->ia_bits));}
When L0 NUMA balancing migrates a page belonging to the QEMU process, the MMU notifier path calls kvm_unmap_gfn_range():
bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
{
...
__unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
range->may_block);
kvm_nested_s2_unmap(kvm, range->may_block); /* full unmap */
return false;}
kvm_handle_hva_range() takes kvm->mmu_lock for writing before invoking the handler and releases it only after the handler returns (virt/kvm/kvm_main.c:622-642). Therefore, the entire kvm_nested_s2_unmap() runs under kvm->mmu_lock.
The problem is that kvm_nested_s2_unmap() does not unmap only the affected GPA range. Instead, it unmaps the entire IPA space (0 to kvm_phys_size(mmu)) for every nested S2 MMU. With nested virtualization enabled, this is very expensive.
## Trace evidence
I captured function_graph traces for kvm_unmap_gfn_range, kvm_nested_s2_unmap, and kvm_stage2_unmap_range, plus mm_migrate_pages tracepoints.
1. numad-triggered full unmap
64) numad-1888 | | /* set_migration_pte:
addr=fff790579000, pte=3040c319d9680 order=0 */
64) numad-1888 | | kvm_unmap_gfn_range() {
64) numad-1888 | | kvm_nested_s2_unmap() {
64) numad-1888 | @ 877415.7 us | kvm_stage2_unmap_range();
64) numad-1888 | @ 877418.2 us | }
64) numad-1888 | @ 877422.0 us | }Each set_migration_pte line is a single-page NUMA migration. Yet each migration triggers one full kvm_nested_s2_unmap() that takes 877 ms.
Subsequent calls show per-page unmap durations between 841 ms and 1.16 s.
2. QEMU threads blocked as well
23) qemu-sy-227844 | | kvm_unmap_gfn_range() {
23) qemu-sy-227844 | | kvm_nested_s2_unmap() {
23) qemu-sy-227844 | $ 1170521 us | kvm_stage2_unmap_range();
23) qemu-sy-227844 | $ 1170529 us | }
23) qemu-sy-227844 | $ 1170542 us | }QEMU's own threads also get stuck in the same full unmap, with one call reaching 6.67 s.
3. Statistics
┌──────────────┬───────┬──────────────────────────────────────┐ │ Thread │ Calls │ kvm_nested_s2_unmap duration │ ├──────────────┼───────┼──────────────────────────────────────┤ │ numad-1888 │ 59 │ min 0.84 s / avg 1.02 s / max 1.16 s │ ├──────────────┼───────┼──────────────────────────────────────┤ │ QEMU threads │ 34 │ min 1.14 s / avg 5.14 s / max 6.67 s │ └──────────────┴───────┴──────────────────────────────────────┘
numad migrates pages one after another; each page holds kvm->mmu_lock for about one second. QEMU vCPU threads cannot acquire mmu_lock and also block on migration_entry_wait. The L1 guest vCPUs make no forward progress, and the watchdog fires.
Soft lockup threshold check
Read directly inside the L1 guest:
root@ubuntu-vm:~# cat /proc/sys/kernel/watchdog_thresh 10
So the soft lockup threshold is 2 * watchdog_thresh = 20 s.
The L1 guest reported stuck times of 29~32 s, which exceeds the 20 s threshold.
## Conclusion
The root cause is confirmed: kvm_nested_s2_unmap() performs a full IPA space unmap in the MMU notifier path instead of unmapping only the affected GPA/CPAI range. The interval-tree-based precise range unmap approach is the right fix.
Please consider applying the patch that replaces the full unmap with kvm_nested_unmap_cipa_range() to avoid scanning the entire nested stage-2 page table on every NUMA migration.
Thanks,
Shuai
Marc Zyngier
maintainerQuoted 74 lines · Shuai Xue
Quoted 57 lines · Wei-Lin Chang
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6 L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64) QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?Quoted 8 lines
Thanks,
Wei-Lin ChangQuoted 5 lines
So what are your actual test conditions?
M.Hi, Wei-Lin and Marc,
I was able to reproduce this issue and capture ftrace evidence that confirms the root cause. Below is the analysis, trace log, and timing data.
[...]
Quoted 2 lines
Each
set_migration_pteline is a single-page NUMA migration. Yet each migration triggers one fullkvm_nested_s2_unmap()that takes 877 ms.
And why is it taking so long? It should be *empty* after the first iteration.
[...]
Quoted 6 lines
## Conclusion
The root cause is confirmed:
kvm_nested_s2_unmap()performs a full IPA space unmap in the MMU notifier path instead of unmapping only the affected GPA/CPAI range. The interval-tree-based precise range unmap approach is the right fix.
No. This just indicates that this is papering over a bigger problem, and your AI is jumping to conclusions.
Quoted 3 lines
Please consider applying the patch that replaces the full unmap with
kvm_nested_unmap_cipa_range()to avoid scanning the entire nested stage-2 page table on every NUMA migration.
Not until we get to the bottom of this issue.
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Quoted 77 lines · Marc Zyngier
Quoted 67 lines · Shuai Xue
Quoted 63 lines · Wei-Lin Chang
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6 L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64) QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?Thanks,
Wei-Lin ChangQuoted 3 lines
So what are your actual test conditions?
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
M.Hi, Wei-Lin and Marc,
I was able to reproduce this issue and capture ftrace evidence that confirms the root cause. Below is the analysis, trace log, and timing data.
Quoted 7 lines
[...]
Quoted 2 lines
Each
set_migration_pteline is a single-page NUMA migration. Yet each migration triggers one fullkvm_nested_s2_unmap()that takes 877 ms.And why is it taking so long? It should be *empty* after the first iteration.
Good question.
After dive into the details trace, let to try to answer the question.
Yes, it is empty -- and that is exactly the point: the 877ms is paid *for* an empty table.
The cost is not in the walk and not in clearing PTEs;
**it is 262,144 broadcast TLB invalidations**, one at the end of every 1GB chunk, each ~3.3us. Since v6.6 the cost of an unmap is proportional to the size of the IPA range, not to the number of mappings; an empty table pays in full.
Quoted 5 lines
[...]
Quoted 2 lines
## Conclusion
Quoted 7 lines
Quoted 4 lines
The root cause is confirmed:
kvm_nested_s2_unmap()performs a full IPA space unmap in the MMU notifier path instead of unmapping only the affected GPA/CPAI range. The interval-tree-based precise range unmap approach is the right fix.No. This just indicates that this is papering over a bigger problem, and your AI is jumping to conclusions.
Sorry for the jumping up.
The culprit is 7657ea920c54 ("KVM: arm64: Use TLBI range-based instructions for unmap", v6.6). kvm_pgtable_stage2_unmap() ends *every* call with an unconditional kvm_tlb_flush_vmid_range(), whether or not the walk cleared a single PTE:
ret = kvm_pgtable_walk(pgt, addr, size, &walker);
if (stage2_unmap_defer_tlb_flush(pgt))
/* Perform the deferred TLB invalidations */
kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);stage2_apply_range() calls it once per 1GB chunk, and the nested MMU covers the guest PARange -- 48 bits here, so 262,144 calls per kvm_nested_s2_unmap(). Each broadcast is one IPAS2E1IS (range) plus one VMALLE1IS plus two DSB(ish), ~3.2-3.4us without ftrace:
877ms / 262,144 chunks = 3.35us per chunk
which is exactly the per-broadcast cost.
All numbers below are from
ftrace function_graph on the same workload as Wang Han reported (Yitian 710, 128 CPUs, Neoverse-N2, L0 v7.2-rc6 with kvm_arm.mode=nested, QEMU virt machine, 8 vCPUs / 32G, 4K host pages), and can be checked against the trace (pre-change kernel, ~90s window):
$ grep -c 'kvm_nested_s2_unmap() {' trace.txt
31 # callbacks in the window
$ grep -c 'kvm_pgtable_stage2_unmap() {' trace.txt
8476033 # one call per 1GB chunk
$ grep -c '__kvm_tlb_flush_vmid_range();' trace.txt
8476033 # one broadcast per chunkThe chunk count and the broadcast count are *equal*: every chunk call ends in a broadcast, mapped or not. Per chunk, instrumented:
kvm_pgtable_stage2_unmap() median 5.097us
kvm_tlb_flush_vmid_range() median 4.645us
__kvm_tlb_flush_vmid_range() p50 4.0us / p90 5.0 / p99 6.25usso ~4.6us of every ~5.1us chunk is the flush; the walk of the empty chunk is ~0.4us. A representative uncontended episode, from the numad thread that executes the unmap itself: 1312ms total, of which 1147ms (87%) inside kvm_tlb_flush_vmid_range() and 106ms (8%) walking. And only 289 of the 8,476,033 chunk calls (0.003%) exceed 20us -- there is nothing in this table.
Raw sample, pre-change kernel (every chunk is a frame, because it contains the traced flush):
13) numad-1888 | | kvm_pgtable_stage2_unmap() {
13) numad-1888 | | kvm_tlb_flush_vmid_range() {
13) numad-1888 | 3.240 us | __kvm_tlb_flush_vmid_range();
13) numad-1888 | 3.660 us | }
13) numad-1888 | 4.060 us | }To confirm that the table is empty, we made the flush conditional on the walk having actually cleared a valid leaf PTE (candidate fix below; nothing else changes). The flush count then becomes a direct detector of "did this chunk clear anything?":
- The first full-IPA unmap of the boot issued 2 flushes -- the boot-time EL1-period mappings, the few dozen pages you predicted, living in two 1GB chunks.
- All 35 subsequent full-IPA unmaps in that run issued *zero* flushes. So yes: empty after the first iteration, exactly as you say. Before the change, each of those empty iterations still cost 877ms, because the broadcast does not depend on the walk having cleared anything.
- The same empty walk now measures 0.208us per chunk (median), i.e. ~55ms of traced chunk time for a full 48-bit sweep:
24) qemu-sy-25907 | | kvm_nested_s2_unmap() {
24) qemu-sy-25907 | | kvm_stage2_unmap_range() {
24) qemu-sy-25907 | 2.740 us | kvm_pgtable_stage2_unmap();
24) qemu-sy-25907 | 0.300 us | kvm_pgtable_stage2_unmap();
24) qemu-sy-25907 | 0.260 us | kvm_pgtable_stage2_unmap();
24) qemu-sy-25907 | 0.240 us | kvm_pgtable_stage2_unmap();while a canonical-S2 chunk that really does clear the migrated page still flushes:
13) qemu-sy-25900 | | kvm_stage2_unmap_range() {
13) qemu-sy-25900 | | kvm_pgtable_stage2_unmap() {
13) qemu-sy-25900 | | kvm_tlb_flush_vmid_range() {
13) qemu-sy-25900 | 4.180 us | __kvm_tlb_flush_vmid_range();
13) qemu-sy-25900 | 4.780 us | }The candidate fix is below. Table entries (KVM_PGTABLE_WALK_TABLE_POST) are unaffected: stage2_unmap_put_pte() keeps issuing the immediate __kvm_tlb_flush_vmid_ipa() for them, and their child leaves are counted as leaves within the same walk, so any call that clears something still flushes a superset of what it cleared. This restores the pre-v6.6 semantics -- cost proportional to what is mapped. With it, an empty nested unmap costs ~110ms instrumented (the pure walk); getting to "exactly zero" would additionally require kvm_nested_s2_unmap() to skip nested MMUs that are valid but empty.
Not proposing to apply anything as-is -- this is the measurement that answers "why is it taking so long", plus the minimal change that confirms it.
Happy to share the full traces if that is useful.
Thanks,
Shuai
---8<---
Subject: [PATCH] KVM: arm64: Skip deferred TLB invalidation for empty unmaps
kvm_pgtable_stage2_unmap() issues kvm_tlb_flush_vmid_range() unconditionally at the end of every call, whether or not the walk cleared any PTE. As stage2_apply_range() calls it once per 1GB chunk, unmapping a sparse (or empty) range costs one broadcast per chunk:
for a 48-bit IPA space that is 262,144 broadcasts, ~0.9s on a Neoverse-N2, paid by every mmu-notifier callback. This is the dominant cost of kvm_nested_s2_unmap() in the NUMA-balancing workload, where the nested shadow S2 is all but empty.
Only perform the deferred invalidation when the walk actually cleared a valid leaf PTE, restoring the pre-v6.6 semantics of the cost being proportional to what is mapped. Table entries keep the immediate __kvm_tlb_flush_vmid_ipa() issued from stage2_unmap_put_pte(), and their child leaves are counted within the same walk, so any call that clears anything still flushes a superset of the range it cleared.
arch/arm64/kvm/hyp/pgtable.c +18 -4
@@ -891,11 +891,17 @@ static bool stage2_unmap_defer_tlb_flush(struct kvm_pgtable *pgt)return system_supports_tlb_range () &&cpus_have_final_cap (ARM64_HAS_STAGE2_FWB ); } +struct stage2_unmap_data { +struct kvm_pgtable *pgt; +u64 unmapped_leaves; +}; +static void stage2_unmap_put_pte (const struct kvm_pgtable_visit_ctx *ctx,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops) { -struct kvm_pgtable *pgt = ctx->arg; +struct stage2_unmap_data *data = ctx->arg; +struct kvm_pgtable *pgt = data->pgt;/* * Clear the existing PTE, and perform break-before-make if it was
@@ -1155,7 +1161,8 @@ int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size,static int stage2_unmap_walker (const struct kvm_pgtable_visit_ctx *ctx,enum kvm_pgtable_walk_flags visit) { -struct kvm_pgtable *pgt = ctx->arg; +struct stage2_unmap_data *data = ctx->arg; +struct kvm_pgtable *pgt = data->pgt;struct kvm_s2_mmu *mmu = pgt->mmu;struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;kvm_pte_t *childp =NULL ;
@@ -1183,6 +1190,9 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,* block entry and rely on the remaining portions being faulted * back lazily. */ +if (kvm_pte_valid (ctx->old) && !kvm_pte_table (ctx->old, ctx->level)) + data->unmapped_leaves++; +stage2_unmap_put_pte (ctx, mmu, mm_ops);if (need_flush && mm_ops->dcache_clean_inval_poc)
@@ -1198,14 +1208,18 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,int kvm_pgtable_stage2_unmap (struct kvm_pgtable *pgt,u64 addr,u64 size) {int ret; +struct stage2_unmap_data data = { + .pgt = pgt, + .unmapped_leaves =0 , + };struct kvm_pgtable_walker walker = { .cb = stage2_unmap_walker, - .arg = pgt, + .arg = &data, .flags =KVM_PGTABLE_WALK_LEAF |KVM_PGTABLE_WALK_TABLE_POST , }; ret =kvm_pgtable_walk (pgt, addr, size, &walker); -if (stage2_unmap_defer_tlb_flush (pgt)) +if (stage2_unmap_defer_tlb_flush (pgt) && data.unmapped_leaves)/* Perform the deferred TLB invalidations */ kvm_tlb_flush_vmid_range (pgt->mmu, addr, size);
Marc Zyngier
maintainerQuoted 127 lines · Shuai Xue
Quoted 77 lines · Marc Zyngier
Quoted 67 lines · Shuai Xue
Quoted 63 lines · Wei-Lin Chang
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6 L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64) QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?Thanks,
Wei-Lin ChangQuoted 3 lines
So what are your actual test conditions?
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
M.Hi, Wei-Lin and Marc,
I was able to reproduce this issue and capture ftrace evidence that confirms the root cause. Below is the analysis, trace log, and timing data.
Quoted 7 lines
[...]
Quoted 2 lines
Each
set_migration_pteline is a single-page NUMA migration. Yet each migration triggers one fullkvm_nested_s2_unmap()that takes 877 ms.And why is it taking so long? It should be *empty* after the first iteration.
Good question.
After dive into the details trace, let to try to answer the question.
Yes, it is empty -- and that is exactly the point: the 877ms is paid *for* an empty table.
The cost is not in the walk and not in clearing PTEs;
**it is 262,144 broadcast TLB invalidations**, one at the end of every 1GB chunk, each ~3.3us. Since v6.6 the cost of an unmap is proportional to the size of the IPA range, not to the number of mappings; an empty table pays in full.Quoted 5 lines
[...]
Quoted 2 lines
## Conclusion
Quoted 7 lines
Quoted 4 lines
The root cause is confirmed:
kvm_nested_s2_unmap()performs a full IPA space unmap in the MMU notifier path instead of unmapping only the affected GPA/CPAI range. The interval-tree-based precise range unmap approach is the right fix.No. This just indicates that this is papering over a bigger problem, and your AI is jumping to conclusions.
Sorry for the jumping up.
The culprit is
7657ea920c54("KVM: arm64: Use TLBI range-based instructions for unmap", v6.6).kvm_pgtable_stage2_unmap()ends *every* call with an unconditionalkvm_tlb_flush_vmid_range(), whether or not the walk cleared a single PTE:ret = kvm_pgtable_walk(pgt, addr, size, &walker); if (stage2_unmap_defer_tlb_flush(pgt)) /* Perform the deferred TLB invalidations */ kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);
Quoted 8 lines
stage2_apply_range()calls it once per 1GB chunk, and the nested MMU covers the guest PARange -- 48 bits here, so 262,144 calls perkvm_nested_s2_unmap(). Each broadcast is one IPAS2E1IS (range) plus one VMALLE1IS plus two DSB(ish), ~3.2-3.4us without ftrace:877ms / 262,144 chunks = 3.35us per chunkwhich is exactly the per-broadcast cost.
Right. That's pretty compelling, thanks for digging into this. Your proposed approach (counting the invalidated regions) is interesting, but I don't think it is the correct one.
The real issue here is that we treat a full S2 unmap as if it was a set of ranges. This is what needs fixing, because we can invalidate the whole thing with exactly *ONE* TLBI.
This is even more important once you run an L2, as L1 will also perform its own TLB invalidation, and we want to avoid having trapping pointlessly. This also propagates in the way we handle TLB emulation.
[...]
Quoted 10 lines
The candidate fix is below. Table entries (
KVM_PGTABLE_WALK_TABLE_POST) are unaffected:stage2_unmap_put_pte()keeps issuing the immediate__kvm_tlb_flush_vmid_ipa()for them, and their child leaves are counted as leaves within the same walk, so any call that clears something still flushes a superset of what it cleared. This restores the pre-v6.6 semantics -- cost proportional to what is mapped. With it, an empty nested unmap costs ~110ms instrumented (the pure walk); getting to "exactly zero" would additionally requirekvm_nested_s2_unmap()to skip nested MMUs that are valid but empty.
That's an interesting remark. I guess we could add some extra tracking for that, but let's see what we can do about the above first.
I've hacked something together and pushed the result at [1] (compile-tested only). I'd appreciate it if you could put it to the test with your setup.
Thanks,
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Quoted 138 lines · Marc Zyngier
Quoted 127 lines · Shuai Xue
Quoted 77 lines · Marc Zyngier
Quoted 67 lines · Shuai Xue
Quoted 63 lines · Wei-Lin Chang
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6 L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64) QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?Thanks,
Wei-Lin ChangQuoted 3 lines
So what are your actual test conditions?
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
M.Hi, Wei-Lin and Marc,
I was able to reproduce this issue and capture ftrace evidence that confirms the root cause. Below is the analysis, trace log, and timing data.
Quoted 7 lines
[...]
Quoted 2 lines
Each
set_migration_pteline is a single-page NUMA migration. Yet each migration triggers one fullkvm_nested_s2_unmap()that takes 877 ms.And why is it taking so long? It should be *empty* after the first iteration.
Good question.
After dive into the details trace, let to try to answer the question.
Yes, it is empty -- and that is exactly the point: the 877ms is paid *for* an empty table.
The cost is not in the walk and not in clearing PTEs;
**it is 262,144 broadcast TLB invalidations**, one at the end of every 1GB chunk, each ~3.3us. Since v6.6 the cost of an unmap is proportional to the size of the IPA range, not to the number of mappings; an empty table pays in full.Quoted 5 lines
[...]
Quoted 2 lines
## Conclusion
Quoted 7 lines
Quoted 4 lines
The root cause is confirmed:
kvm_nested_s2_unmap()performs a full IPA space unmap in the MMU notifier path instead of unmapping only the affected GPA/CPAI range. The interval-tree-based precise range unmap approach is the right fix.No. This just indicates that this is papering over a bigger problem, and your AI is jumping to conclusions.
Sorry for the jumping up.
The culprit is
7657ea920c54("KVM: arm64: Use TLBI range-based instructions for unmap", v6.6).kvm_pgtable_stage2_unmap()ends *every* call with an unconditionalkvm_tlb_flush_vmid_range(), whether or not the walk cleared a single PTE:ret = kvm_pgtable_walk(pgt, addr, size, &walker); if (stage2_unmap_defer_tlb_flush(pgt)) /* Perform the deferred TLB invalidations */ kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);Quoted 8 lines
stage2_apply_range()calls it once per 1GB chunk, and the nested MMU covers the guest PARange -- 48 bits here, so 262,144 calls perkvm_nested_s2_unmap(). Each broadcast is one IPAS2E1IS (range) plus one VMALLE1IS plus two DSB(ish), ~3.2-3.4us without ftrace:877ms / 262,144 chunks = 3.35us per chunkwhich is exactly the per-broadcast cost.
Quoted 7 lines
Right. That's pretty compelling, thanks for digging into this. Your proposed approach (counting the invalidated regions) is interesting, but I don't think it is the correct one.
The real issue here is that we treat a full S2 unmap as if it was a set of ranges. This is what needs fixing, because we can invalidate the whole thing with exactly *ONE* TLBI.
Aha, I see your point. Nice insight.
Quoted 4 lines
This is even more important once you run an L2, as L1 will also perform its own TLB invalidation, and we want to avoid having trapping pointlessly. This also propagates in the way we handle TLB emulation.
Quite agree.
Quoted 21 lines
[...]
Quoted 10 lines
The candidate fix is below. Table entries (
KVM_PGTABLE_WALK_TABLE_POST) are unaffected:stage2_unmap_put_pte()keeps issuing the immediate__kvm_tlb_flush_vmid_ipa()for them, and their child leaves are counted as leaves within the same walk, so any call that clears something still flushes a superset of what it cleared. This restores the pre-v6.6 semantics -- cost proportional to what is mapped. With it, an empty nested unmap costs ~110ms instrumented (the pure walk); getting to "exactly zero" would additionally requirekvm_nested_s2_unmap()to skip nested MMUs that are valid but empty.That's an interesting remark. I guess we could add some extra tracking for that, but let's see what we can do about the above first.
I've hacked something together and pushed the result at [1] (compile-tested only). I'd appreciate it if you could put it to the test with your setup.
Quoted 5 lines
Thanks,
M.
Sure, I will test your proposed approach and update latter.
Thanks.
Shuai
Quoted 170 lines · Marc Zyngier
Quoted 127 lines · Shuai Xue
Quoted 77 lines · Marc Zyngier
Quoted 67 lines · Shuai Xue
Quoted 63 lines · Wei-Lin Chang
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6 L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64) QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?Thanks,
Wei-Lin ChangQuoted 3 lines
So what are your actual test conditions?
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
M.Hi, Wei-Lin and Marc,
I was able to reproduce this issue and capture ftrace evidence that confirms the root cause. Below is the analysis, trace log, and timing data.
Quoted 7 lines
[...]
Quoted 2 lines
Each
set_migration_pteline is a single-page NUMA migration. Yet each migration triggers one fullkvm_nested_s2_unmap()that takes 877 ms.And why is it taking so long? It should be *empty* after the first iteration.
Good question.
After dive into the details trace, let to try to answer the question.
Yes, it is empty -- and that is exactly the point: the 877ms is paid *for* an empty table.
The cost is not in the walk and not in clearing PTEs;
**it is 262,144 broadcast TLB invalidations**, one at the end of every 1GB chunk, each ~3.3us. Since v6.6 the cost of an unmap is proportional to the size of the IPA range, not to the number of mappings; an empty table pays in full.Quoted 5 lines
[...]
Quoted 2 lines
## Conclusion
Quoted 7 lines
Quoted 4 lines
The root cause is confirmed:
kvm_nested_s2_unmap()performs a full IPA space unmap in the MMU notifier path instead of unmapping only the affected GPA/CPAI range. The interval-tree-based precise range unmap approach is the right fix.No. This just indicates that this is papering over a bigger problem, and your AI is jumping to conclusions.
Sorry for the jumping up.
The culprit is
7657ea920c54("KVM: arm64: Use TLBI range-based instructions for unmap", v6.6).kvm_pgtable_stage2_unmap()ends *every* call with an unconditionalkvm_tlb_flush_vmid_range(), whether or not the walk cleared a single PTE:ret = kvm_pgtable_walk(pgt, addr, size, &walker); if (stage2_unmap_defer_tlb_flush(pgt)) /* Perform the deferred TLB invalidations */ kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);Quoted 8 lines
stage2_apply_range()calls it once per 1GB chunk, and the nested MMU covers the guest PARange -- 48 bits here, so 262,144 calls perkvm_nested_s2_unmap(). Each broadcast is one IPAS2E1IS (range) plus one VMALLE1IS plus two DSB(ish), ~3.2-3.4us without ftrace:877ms / 262,144 chunks = 3.35us per chunkwhich is exactly the per-broadcast cost.
Right. That's pretty compelling, thanks for digging into this. Your proposed approach (counting the invalidated regions) is interesting, but I don't think it is the correct one.
The real issue here is that we treat a full S2 unmap as if it was a set of ranges. This is what needs fixing, because we can invalidate the whole thing with exactly *ONE* TLBI.
This is even more important once you run an L2, as L1 will also perform its own TLB invalidation, and we want to avoid having trapping pointlessly. This also propagates in the way we handle TLB emulation.
[...]
Quoted 10 lines
The candidate fix is below. Table entries (
KVM_PGTABLE_WALK_TABLE_POST) are unaffected:stage2_unmap_put_pte()keeps issuing the immediate__kvm_tlb_flush_vmid_ipa()for them, and their child leaves are counted as leaves within the same walk, so any call that clears something still flushes a superset of what it cleared. This restores the pre-v6.6 semantics -- cost proportional to what is mapped. With it, an empty nested unmap costs ~110ms instrumented (the pure walk); getting to "exactly zero" would additionally requirekvm_nested_s2_unmap()to skip nested MMUs that are valid but empty.That's an interesting remark. I guess we could add some extra tracking for that, but let's see what we can do about the above first.
I've hacked something together and pushed the result at [1] (compile-tested only). I'd appreciate it if you could put it to the test with your setup.
Quoted 5 lines
Thanks,
M.
Hi Marc, Wei-Lin,
I have completed a controlled A/B test of Marc's kvm-arm64/unmap-vmall branch and a local port of Wei-Lin's v5 interval-tree series on top of it.
TL;DR
=====
Marc's series successfully removes the per-chunk TLBI amplification:
no range TLBI is issued while walking the shadow stage-2, and each completed full unmap ends with a single VMID-wide invalidation.
However, the reported soft lockup still reproduced in 13 out of 15 Marc-only rounds.
The remaining problem is that each small canonical MMU-notifier invalidation is still promoted to a full-IPA software iteration. On this 48-bit/4KB system, this means 262,144 no-TLBI chunks per valid shadow MMU. Under frequent NUMA migrations, multiple full-unmap calls become outstanding and are interleaved as stage2_apply_range() periodically drops and reacquires mmu_lock.
With Wei-Lin's precise cIPA-to-NIPA lookup enabled, the same test completed 15 out of 15 rounds without a soft lockup, while exercising more than four million cIPA notifier calls.
Test setup
==========
Both variants used:
- the same read-only golden L1 image;
- a fresh qcow2 overlay and writable pflash copy for every round;
- the same kernel configuration;
- QEMU 10.2.3;
- an 8-vCPU / 32-GiB L1;
- automatic NUMA balancing enabled;
- numad active;
- no additional memory-pressure workload;
- 15 rounds of 600 seconds per variant.
Every round verified the kernel commit, config and installed Image, QEMU version, golden-image manifest, guest login, trace accounting, BPF stderr, disk integrity and result manifest.
The tested revisions were:
Marc-only:
3b46d91c9aac2f9e8f0be58d0834b9932667af92
Marc + Wei-Lin:
d3fca181b5fadb86aeb35437c359dc09ad233f05
The latter is a local conflict-resolved validation port of Wei-Lin's v5 interval-tree series on top of Marc's branch.
A/B results
===========
Metric Marc-only Marc + Wei-Lin
------------------------------------------------------------------
Test rounds 15 15 Soft-lockup rounds 13 0 Guest soft-lockup lines 71 0 Guest RCU-stall lines 5 0 L0 hung-task/soft-lockup lines 0 0
kvm_nested_s2_unmap calls 776,896 0 kvm_stage2_unmap_all calls 775,067 0 Precise cIPA notifier calls 0 4,176,635
Completed full-unmap intervals 773,911 0 Total VMID-wide flush calls 773,937 0 Range TLBI inside full unmap 0 (*) N/A
Successfully migrated pages 2,411,259 5,342,515 Harness/manifest failures 0 0
(*) The absence of range TLBI inside Marc's full-unmap path was
confirmed separately with function-graph tracing. The formal low-overhead batch correlated full-unmap completion with the final VMID-wide flush instead of probing the hot range-TLBI symbol.
Marc-only full-unmap behaviour
==============================
The formal trace records kvm_stage2_unmap_all() entry and correlates it with the final __kvm_tlb_flush_vmid() on the same TID. Only successfully paired intervals are used for latency and concurrency calculations.
The minimum per-round completion-pairing coverage was 99.3175%, and all 15 rounds passed the trace-accounting gate.
The following comparison separates the 13 lockup rounds from the two rounds that completed 600 seconds without a lockup:
Metric 13 lockup rounds 2 non-lockup rounds
-----------------------------------------------------------------------
Completed full unmaps 551,786 222,125
Peak outstanding unmaps 5--9 2--4 Median peak 7 3
Time with >=2 outstanding 1,115.3 s 5.94 s Fraction of activity window 37.4% 0.49%
Time with >=4 outstanding 356.4 s 0.06 s Fraction of activity window 11.9% ~0.005%
Time with >=6 outstanding 34.74 s 0
Mean completed duration 9.91 ms 5.46 ms Completed unmaps >=100 ms 8,514 (1.543%) 14 (0.006%) Completed unmaps >=250 ms 5,372 (0.974%) 0 Completed unmaps >=500 ms 135 0 Maximum completed duration 946.97 ms 179.12 ms
In 12 of the 13 lockup rounds, the peak outstanding set included both numad and QEMU threads. One of those rounds also included kcompactd1. In the remaining lockup round, the peak consisted of six QEMU threads.
"Outstanding" does not mean that these threads held mmu_lock simultaneously. kvm_stage2_unmap_all() is entered with the write lock held, but stage2_apply_range() calls cond_resched_rwlock_write() between chunks. This permits another waiting invocation to acquire the lock and start or continue its own full-IPA operation before the previous invocation has completed.
The trace therefore shows a full-unmap hand-off convoy, rather than a single kvm_stage2_unmap_all() invocation remaining stuck for the entire watchdog interval. This also explains why removing the expensive per-chunk TLBIs substantially reduces individual work but does not eliminate the soft lockup under a sufficiently high notifier rate.
Combined-kernel behaviour
=========================
With Wei-Lin's interval-tree path, every observed canonical notifier invalidation used kvm_nested_unmap_cipa_range(). None entered kvm_nested_s2_unmap(), kvm_stage2_unmap_all(), or the final VMALL path.
All 15 rounds completed without a guest soft lockup or RCU stall. Three rounds successfully migrated approximately 2.106 million, 2.091 million and 1.130 million pages respectively, so the notifier path was heavily exercised.
My interpretation
=================
The results distinguish two separate amplification problems:
1. A true full shadow-stage-2 invalidation was implemented as many range TLBIs.
Marc's series fixes this as intended by clearing the page tables without per-chunk TLBI and issuing one final VMID-wide invalidation.
2. A small canonical MMU-notifier invalidation is promoted to a full invalidation of every valid shadow stage-2.
Marc's series does not change this policy. After the TLBI cost is removed, the repeated full-IPA software iteration and resulting overlap are still sufficient to reproduce the soft lockup.
For this workload, Wei-Lin's reverse map removes the second amplification by translating the invalidated canonical IPA range into only the affected nested IPA ranges.
I therefore think the two approaches are complementary:
- use precise reverse-map invalidation for canonical MMU-notifier ranges; and
- retain Marc's no-TLBI walk plus one VMALL for operations that genuinely require the complete shadow stage-2 to be invalidated.
The combined runs did not exercise the full-unmap/MMU-reuse paths, so I am treating them as evidence for the notifier-path behaviour, not as complete validation of every interaction between the two series.
I can provide the per-round summaries, manifests and raw BPF timelines if useful.
Thanks,
Shuai
Quoted 7 lines · Marc Zyngier
That's an interesting remark. I guess we could add some extra tracking for that, but let's see what we can do about the above first.
I've hacked something together and pushed the result at [1] (compile-tested only). I'd appreciate it if you could put it to the test with your setup.
Quoted 5 lines
Thanks,
M.
On Neoverse-V2 though, with NUMA balancing on, the WIP kernel from yesterday brought up the Ubuntu 7.0.0-31-generic kernel online without an issue. I will test the host kernel again when the new code is merged to kvmarm/next.
Thanks,
Itaru.
Quoted 3 lines
Signature · 2 lines
-- Jazz isn't dead. It just smells funny.
Marc Zyngier
maintainerQuoted 41 lines · Wei-Lin Chang
Quoted 35 lines · Marc Zyngier
Quoted 23 lines · Wang Han
Hi Wei-Lin,
I tested this series on a Yitian 710 system with an ARM Neoverse-N2 CPU (128 CPUs, 2 NUMA nodes).
Test environment
----------------L0 kernel: Linux v7.2-rc6
L1 guest: Ubuntu 26.04 LTS, kernel 7.0.0-27-generic (aarch64)
QEMU: 10.2.3L0 NUMA balancing was enabled (
/proc/sys/kernel/numa_balancing=1). The host was booted withkvm_arm.mode=nested.This series fixes a functional hang that is exposed when NUMA balancing is enabled. The previous nested stage-2 unmap path is too slow for this workload, making the performance problem user-visible: NUMA balancing can leave the L1 guest unable to make progress and eventually hang during boot.
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
Quoted 9 lines
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
Quoted 5 lines
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).
It should be one nested MMU for the whole VM, not one per vcpu. That's assuming they share the same VMID+VTCR.
Quoted 10 lines
What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?
This should be close to being at zero cost, so something else is amiss.
Could you please have a look?
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
On Fri, Sep 04, 2026 at 08:49:14AM +0100, Marc Zyngier wrote:
[...]
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 12 lines
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioPuzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
Quoted 8 lines
Quoted 5 lines
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).It should be one nested MMU for the whole VM, not one per vcpu. That's assuming they share the same VMID+VTCR.
(sigh) Yes, rookie mistake from me as always.
Quoted 12 lines
Quoted 11 lines
What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?
Quoted 4 lines
This should be close to being at zero cost, so something else is amiss.
Could you please have a look?
Of course.
Thanks,
Wei-Lin Chang
Quoted 5 lines
M.Signature · 1 line
Jazz isn't dead. It just smells funny.
On Fri, Sep 04, 2026 at 08:49:14AM +0100, Marc Zyngier wrote:
[...]
Quoted 37 lines
Quoted 17 lines
Quoted 13 lines
Quoted 12 lines
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
Quoted 5 lines
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).It should be one nested MMU for the whole VM, not one per vcpu. That's assuming they share the same VMID+VTCR.
Quoted 10 lines
What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?
Quoted 3 lines
This should be close to being at zero cost, so something else is amiss.
Guess what? I am completely wrong once again.
The complete range of the nested mmus isn't about the value supplied by KVM_VM_TYPE_ARM_IPA_SIZE. Instead it is the PARange exposed to the guest. That's because the guest hypervisor is allowed to expose whatever memory map it likes to its nested guests, as long as it is within the guest's PARange. This is explained clearly in init_nested_s2_mmu().
Now the PARange exposed to guests should be much larger than 40bits. On the neoverse-n2 it should be 48bits (256TB), therefore the iterations required for each unmapping would be 256x:
262144 (4KB pages, unmapping 1GB per iteration) 8388608 (16KB pages, unmapping 32MB per iteration) 524288 (64KB pages, unmapping 512MB per iteration)
I think these numbers match the hang time Shuai have shown.
Just to demonstrate the difference I have tried manually limiting kvm_ipa_limit to 40, and tested with software emulation. Before the manual change a kvm_nested_s2_unmap() finished after 42 seconds, after limiting kvm_ipa_limit kvm_nested_s2_unmap() can finish in less than a second.
With this, I think there aren't other underlying issues, it just is that slow unfortunately..
Thanks,
Wei-Lin Chang
Quoted 6 lines
Could you please have a look?
M.Signature · 1 line
Jazz isn't dead. It just smells funny.
Marc Zyngier
maintainerQuoted 65 lines · Wei-Lin Chang
On Fri, Sep 04, 2026 at 08:49:14AM +0100, Marc Zyngier wrote:
[...]
Quoted 37 lines
Quoted 17 lines
Quoted 13 lines
Quoted 12 lines
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
Quoted 5 lines
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).It should be one nested MMU for the whole VM, not one per vcpu. That's assuming they share the same VMID+VTCR.
Quoted 10 lines
What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?Quoted 3 lines
This should be close to being at zero cost, so something else is amiss.
Guess what? I am completely wrong once again.
The complete range of the nested mmus isn't about the value supplied by
KVM_VM_TYPE_ARM_IPA_SIZE. Instead it is the PARange exposed to the guest. That's because the guest hypervisor is allowed to expose whatever memory map it likes to its nested guests, as long as it is within the guest's PARange. This is explained clearly ininit_nested_s2_mmu().Now the PARange exposed to guests should be much larger than 40bits. On the neoverse-n2 it should be 48bits (256TB), therefore the iterations required for each unmapping would be 256x:
262144 (4KB pages, unmapping 1GB per iteration) 8388608 (16KB pages, unmapping 32MB per iteration) 524288 (64KB pages, unmapping 512MB per iteration)
I think these numbers match the hang time Shuai have shown.
Quoted 8 lines
Just to demonstrate the difference I have tried manually limiting
kvm_ipa_limitto 40, and tested with software emulation. Before the manual change akvm_nested_s2_unmap()finished after 42 seconds, after limitingkvm_ipa_limitkvm_nested_s2_unmap()can finish in less than a second.With this, I think there aren't other underlying issues, it just is that slow unfortunately..
I don't buy this. There should be very little mapped in the shadow S2, a few dozen pages at most. As you noticed, there is very little the kernel executes at EL1 before upgrading to EL2. Invalidating this should be cheap. Furthermore, once invalidated after the last vcpu has booted, the cost should be exactly zero.
It seems that these two assumptions are not holding up, and I wonder why.
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Quoted 62 lines · Wei-Lin Chang
On Fri, Sep 04, 2026 at 08:49:14AM +0100, Marc Zyngier wrote:
[...]
Quoted 37 lines
Quoted 17 lines
Quoted 13 lines
Quoted 12 lines
The L1 was started with 8 vCPUs and 32 GiB of RAM using:
qemu-system-aarch64 -smp 8 -m 32G \ -machine virt,accel=kvm,gic-version=3,virtualization=on \ -cpu host -nographic -enable-kvm \ -drive if=pflash,format=raw,readonly=on,file=pflash0_bak.img \ -drive if=pflash,format=raw,file=pflash1_bak.img \ -drive file=./ubuntu-vm.qcow2,format=qcow2,if=virtio,cache=none,aio=native \ -nic user,model=virtio-net-pci,hostfwd=tcp::11234-:22 \ -serial mon:stdioQuoted 3 lines
Puzzling. If you are only running an L1 in VHE mode, there is no shadow S2, and therefore nothing to unmap. For shadow S2s to be built and affect the MMU notifiers, you need to run an L2.
Quoted 5 lines
I was thinking the same at first, but realized even with L1 in VHE mode there is a small period of time where L1 runs in its EL1 during boot, so one nested MMU will become valid for each vCPU. That causes
kvm_nested_s2_unmap()to iterate through the entire IPA space 8 times (-smp 8).It should be one nested MMU for the whole VM, not one per vcpu. That's assuming they share the same VMID+VTCR.
Quoted 10 lines
What I am curious about is whether one single notifier unmap is enough to hang L1, or were there multiple notifier unmaps.
QEMU with -machine virt uses 40 IPA bits only, unmapping that takes:
1024 (4KB pages, unmapping 1GB per iteration)
32768 (16KB pages, unmapping 32MB per iteration)
2048 (64KB pages, unmapping 512MB per iteration)
iterations for each page size. There aren't many mappings in each iteration too. Does this really take that long on real hardware (even if this must be done 8 times)?Quoted 3 lines
This should be close to being at zero cost, so something else is amiss.
Guess what? I am completely wrong once again.
The complete range of the nested mmus isn't about the value supplied by
KVM_VM_TYPE_ARM_IPA_SIZE. Instead it is the PARange exposed to the guest. That's because the guest hypervisor is allowed to expose whatever memory map it likes to its nested guests, as long as it is within the guest's PARange. This is explained clearly ininit_nested_s2_mmu().Now the PARange exposed to guests should be much larger than 40bits. On the neoverse-n2 it should be 48bits (256TB), therefore the iterations required for each unmapping would be 256x:
262144 (4KB pages, unmapping 1GB per iteration) 8388608 (16KB pages, unmapping 32MB per iteration) 524288 (64KB pages, unmapping 512MB per iteration)
Quoted 7 lines
I think these numbers match the hang time Shuai have shown.
Just to demonstrate the difference I have tried manually limiting
kvm_ipa_limitto 40, and tested with software emulation. Before the manual change akvm_nested_s2_unmap()finished after 42 seconds, after limitingkvm_ipa_limitkvm_nested_s2_unmap()can finish in less than a second.
Hi Wei-Lin,
Your PARange analysis is what this whole diagnosis rests on -- the 48-bit/256x arithmetic is exactly right, and our ftrace data confirms it to the chunk: every full-IPA kvm_nested_s2_unmap() in our traces walks precisely 262,144 1GB chunks, the number from your table. The kvm_ipa_limit=40 experiment also pointed straight at the answer. I'd only push back on the last sentence:
Quoted 3 lines
With this, I think there aren't other underlying issues, it just is that slow unfortunately..
I don't think it is "just that slow" -- I think it is a specific regression, and your own data localizes it better than the conclusion suggests.
The thing is, kvm_ipa_limit=40 cannot tell *what* each chunk spends its ~3.3us on, because the walk is also per-chunk: cutting the chunk count by 256x shrinks both terms together, whichever one dominates. The experiment that separates them is one where the chunk count does not change at all.
We ran that experiment. Details are in my reply to Marc, but the short version:
- Before: 877ms per callback, 262,144 chunk calls, 262,144 broadcasts. Chunk count and broadcast count are exactly equal (8,476,033 = 8,476,033 over the trace window) -- every chunk ends in a broadcast, mapped or not. Per chunk, ~4.6us of the ~5.1us is inside kvm_tlb_flush_vmid_range(); the walk of an empty chunk is ~0.2us. In an uncontended numad episode, 87% of the 1312ms is flush time and 8% is walk time.
- After conditioning the flush on the walk having actually cleared a valid leaf (the candidate fix in my other mail): same 262,144 chunks, same walk, ~110ms instrumented. Nothing about the walk changed -- ~3.1us of the per-chunk 3.3us was the broadcast.
- And the table really is empty, as Marc says: with the flush count acting as a detector, the first full-IPA unmap of the boot issues 2 flushes (the EL1-boot-period mappings, a few dozen pages in two 1GB chunks); all 35 subsequent ones issue zero.
The code history agrees. Before 7657ea920c54, the TLBI lived inside stage2_put_pte()'s 'if (kvm_pte_valid(ctx->old))' -- an empty walk issued zero invalidations, ever. v6.6 hoisted the invalidation out to the end of kvm_pgtable_stage2_unmap() to batch it into one range TLBI per call, but the condition got dropped on the way. So "since v6.6, the cost is proportional to the IPA range, not to the number of mappings" -- which is precisely why the empty table pays in full, and why I'd frame it as a fixable regression rather than an inherent cost.
None of this subtracts from your PARange finding -- it depends on it. The 42s -> <1s result was the 256x chunk scaling; the remaining <1s is what the conditional flush removes.
Thanks,
Shuai
On Sat, Sep 05, 2026 at 11:49:32PM +0800, Shuai Xue wrote:
[...]
Quoted 56 lines
Hi Wei-Lin,
Your PARange analysis is what this whole diagnosis rests on -- the 48-bit/256x arithmetic is exactly right, and our ftrace data confirms it to the chunk: every full-IPA
kvm_nested_s2_unmap()in our traces walks precisely 262,144 1GB chunks, the number from your table. The kvm_ipa_limit=40 experiment also pointed straight at the answer. I'd only push back on the last sentence:Quoted 3 lines
With this, I think there aren't other underlying issues, it just is that slow unfortunately..
I don't think it is "just that slow" -- I think it is a specific regression, and your own data localizes it better than the conclusion suggests.
The thing is, kvm_ipa_limit=40 cannot tell *what* each chunk spends its ~3.3us on, because the walk is also per-chunk: cutting the chunk count by 256x shrinks both terms together, whichever one dominates. The experiment that separates them is one where the chunk count does not change at all.
We ran that experiment. Details are in my reply to Marc, but the short version:
- Before: 877ms per callback, 262,144 chunk calls, 262,144 broadcasts. Chunk count and broadcast count are exactly equal (8,476,033 = 8,476,033 over the trace window) -- every chunk ends in a broadcast, mapped or not. Per chunk, ~4.6us of the ~5.1us is inside
kvm_tlb_flush_vmid_range(); the walk of an empty chunk is ~0.2us. In an uncontended numad episode, 87% of the 1312ms is flush time and 8% is walk time.- After conditioning the flush on the walk having actually cleared a valid leaf (the candidate fix in my other mail): same 262,144 chunks, same walk, ~110ms instrumented. Nothing about the walk changed -- ~3.1us of the per-chunk 3.3us was the broadcast.
- And the table really is empty, as Marc says: with the flush count acting as a detector, the first full-IPA unmap of the boot issues 2 flushes (the EL1-boot-period mappings, a few dozen pages in two 1GB chunks); all 35 subsequent ones issue zero.
The code history agrees. Before
7657ea920c54, the TLBI lived inside stage2_put_pte()'s 'if (kvm_pte_valid(ctx->old))' -- an empty walk issued zero invalidations, ever. v6.6 hoisted the invalidation out to the end ofkvm_pgtable_stage2_unmap()to batch it into one range TLBI per call, but the condition got dropped on the way. So "since v6.6, the cost is proportional to the IPA range, not to the number of mappings" -- which is precisely why the empty table pays in full, and why I'd frame it as a fixable regression rather than an inherent cost.
Quoted 3 lines
None of this subtracts from your PARange finding -- it depends on it. The 42s -> <1s result was the 256x chunk scaling; the remaining <1s is what the conditional flush removes.
Thanks for the analysis!
I completely agree. My finding was only a small part of the full picture. I stopped at finding out the real iteration count and didn't realize 3-5us is a long time for a kvm_pgtable_stage2_unmap() on a empty table (thanks for the learning opportunity).
So, because there are no relevant tlb entries created in the first place, the per iteration TLBIs and barriers are mostly doing nothing useful and cause a 10x slowdown.
Thanks,
Wei-Lin Chang
Quoted 5 lines
Thanks,
Shuai
Quoted 66 lines · Wei-Lin Chang
On Sat, Sep 05, 2026 at 11:49:32PM +0800, Shuai Xue wrote:
[...]
Quoted 56 lines
Hi Wei-Lin,
Your PARange analysis is what this whole diagnosis rests on -- the 48-bit/256x arithmetic is exactly right, and our ftrace data confirms it to the chunk: every full-IPA
kvm_nested_s2_unmap()in our traces walks precisely 262,144 1GB chunks, the number from your table. The kvm_ipa_limit=40 experiment also pointed straight at the answer. I'd only push back on the last sentence:Quoted 3 lines
With this, I think there aren't other underlying issues, it just is that slow unfortunately..
I don't think it is "just that slow" -- I think it is a specific regression, and your own data localizes it better than the conclusion suggests.
The thing is, kvm_ipa_limit=40 cannot tell *what* each chunk spends its ~3.3us on, because the walk is also per-chunk: cutting the chunk count by 256x shrinks both terms together, whichever one dominates. The experiment that separates them is one where the chunk count does not change at all.
We ran that experiment. Details are in my reply to Marc, but the short version:
- Before: 877ms per callback, 262,144 chunk calls, 262,144 broadcasts. Chunk count and broadcast count are exactly equal (8,476,033 = 8,476,033 over the trace window) -- every chunk ends in a broadcast, mapped or not. Per chunk, ~4.6us of the ~5.1us is inside
kvm_tlb_flush_vmid_range(); the walk of an empty chunk is ~0.2us. In an uncontended numad episode, 87% of the 1312ms is flush time and 8% is walk time.- After conditioning the flush on the walk having actually cleared a valid leaf (the candidate fix in my other mail): same 262,144 chunks, same walk, ~110ms instrumented. Nothing about the walk changed -- ~3.1us of the per-chunk 3.3us was the broadcast.
- And the table really is empty, as Marc says: with the flush count acting as a detector, the first full-IPA unmap of the boot issues 2 flushes (the EL1-boot-period mappings, a few dozen pages in two 1GB chunks); all 35 subsequent ones issue zero.
The code history agrees. Before
7657ea920c54, the TLBI lived inside stage2_put_pte()'s 'if (kvm_pte_valid(ctx->old))' -- an empty walk issued zero invalidations, ever. v6.6 hoisted the invalidation out to the end ofkvm_pgtable_stage2_unmap()to batch it into one range TLBI per call, but the condition got dropped on the way. So "since v6.6, the cost is proportional to the IPA range, not to the number of mappings" -- which is precisely why the empty table pays in full, and why I'd frame it as a fixable regression rather than an inherent cost.Quoted 3 lines
None of this subtracts from your PARange finding -- it depends on it. The 42s -> <1s result was the 256x chunk scaling; the remaining <1s is what the conditional flush removes.
Thanks for the analysis!
Quoted 8 lines
I completely agree. My finding was only a small part of the full picture. I stopped at finding out the real iteration count and didn't realize 3-5us is a long time for a
kvm_pgtable_stage2_unmap()on a empty table (thanks for the learning opportunity).So, because there are no relevant tlb entries created in the first place, the per iteration TLBIs and barriers are mostly doing nothing useful and cause a 10x slowdown.
Hi Marc, Wei-Lin,
Thanks for pushing back on this. Let me try to give the full picture (Wei-Lin, please correct me if I get any of it wrong).
## 1. The cost of one kvm_nested_s2_unmap()
per-callback cost = (IPA range / 1GB) x per-chunk cost
With numbers:
chunks (range) per-chunk cost per callback
current 262,144 ~3.55us (3.35 broadcast ~877ms
(48-bit IPA) + 0.2 walk, always)
The first factor is what Wei-Lin identified: the nested MMU covers the guest PARange -- 48 bits here, not the 40 bits QEMU asked for -- so stage2_apply_range() calls kvm_pgtable_stage2_unmap() once per 1GB chunk. The second factor is where the surprise was hiding: the broadcast is unconditional, whether or not the chunk contained anything.
Since both factors are large, there are two independent ways to bring the product down:
chunks (range) per-chunk cost per callback current 262,144 ~3.55us (always) ~877ms precise unmap ~1 ~3.55us (always) ~us
(nested layer,
Wei-Lin's
interval-tree)
conditional 262,144 ~0.2us when empty ~110ms
flush (~3.55us if it
(pgtable layer, cleared something)
below)
Each attack a different factor. Wei-Lin's patch shrinks the range to the single chunk containing the migrated page; the conditional flush keeps the full-range walk but stops paying the broadcast for chunks that cleared nothing. Either one alone brings the per-callback cost far below the point where consecutive callbacks can accumulate a watchdog-relevant stall; they also compose.
## 2. Conditioning the flush: the how and the why
We tried the pgtable-layer variant: the walker counts valid leaf PTEs it actually clears, and the deferred flush fires only if that count is non-zero:
ret = kvm_pgtable_walk(pgt, addr, size, &walker);
if (stage2_unmap_defer_tlb_flush(pgt) && data.unmapped_leaves)
kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);Correctness: a non-zero count means at least one valid leaf was cleared, and the flush covers the whole [addr, addr+size) range, a superset of everything this call cleared. A zero count means no valid PTE was cleared, so no core can hold a stale translation this call is responsible for. Table entries keep their immediate __kvm_tlb_flush_vmid_ipa() from stage2_unmap_put_pte(), and their child leaves are counted as leaves within the same walk.
Same L1 QEMU bootup test, five 6-minute runs: per-callback 877ms -> ~110ms instrumented (~40-80ms uninstrumented, the pure walk); broadcasts 8,476,033 -> 221 in total, of which 2 were for actual nested mappings and the rest canonical flushes of the pages NUMA balancing really migrated; the soft lockups are gone (0/5 runs). And it is a pgtable-layer change, so every other sparse-range unmap caller -- canonical S2, pKVM -- stops paying the per-chunk broadcast whenever the range is much larger than what is mapped in it.
## 3. Where the unconditional broadcast came from
The condition was not dropped deliberately; it is a casualty of 7657ea920c54 ("KVM: arm64: Use TLBI range-based instructions for unmap", v6.6). That commit batched the per-PTE TLBI into one range TLBI at the end of each kvm_pgtable_stage2_unmap() call. But in the old stage2_put_pte(), the "only if a valid PTE was cleared" condition was simply the *position* of the TLBI inside the 'if (kvm_pte_valid(ctx->old))' block -- there was nothing to move, so nothing was moved:
dense chunk empty chunk pre-v6.6 262,144 TLBIs 0 TLBIs since v6.6 1 range TLBI 1 range TLBI
The dense column is a 260,000x win -- the case the commit was written for (unmapping a fully populated memslot). The empty column is a 0 -> 1 regression per chunk, invisible as long as nobody unmaps ranges that are simultaneously huge and empty. Before v6.6, the cost of an unmap was proportional to the number of mappings; since, it is proportional to the size of the IPA range. Nested virtualisation plus NUMA balancing is, as far as we know, the first caller to do exactly that, per notifier event, while holding the mmu_lock for write -- 262,144 x 3.35us is the ~877ms we measured, and the vCPUs' stall accumulates across consecutive callbacks until it crosses the guest watchdog threshold (one event: 7 of 8 guest CPUs reporting "stuck for 24-28s" within the same millisecond).
So the conditional flush is not an optimisation so much as restoring the lost condition -- making empty unmaps behave like pre-v6.6 again.
## 4. The question
Do you think restoring the lost condition -- i.e. the pgtable-layer fix above, conditioning the deferred flush on the walk having actually cleared a valid leaf -- is worth submitting as a proper fix for 7657ea920c54?
As far as we can tell it is a ~20-line change that restores the pre-v6.6 semantics with no new ones: empty and sparse unmaps cost the walk again, dense unmaps keep the range-TLBI win.
Thanks,
Shuai
Quoted 9 lines
Thanks,
Wei-Lin ChangQuoted 5 lines
Thanks,
Shuai
Quoted 4 lines · Wei-Lin Chang
Hi,
This is v5 of optimizing the shadow s2 mmu unmapping during MMU notifiers.
I did a more targetted test of this series in a (ad-hoc) way similar to pkvm_ownership_selftest(). I allocated and initialized an in kernel VM, its locks, 2 nested_mmus, and a memcache.
By calling kvm_pgtable_stage2_map(), kvm_record_guest_s2_mapping() together I create mappings and track them, then kvm_nested_unmap_cipa_range() simulates mmu notifier unmaps, and kvm_stage2_unmap_range() simulates L1 TLBIs.
Combining these I tried a few scenarios:
1. Mapping the same CIPA in two nested mmus, and checked that unmapping the CIPA it removes both mappings, but keeps unrelated mappings.
2. Mapping a block and a page to an overlapping CIPA region, checked that unmapping the overlapping CIPA regions removes both mappings.
3. Unmapping CIPA of the last page of a block, checking that the block is removed, but keeps unrelated mappings.
4. Unmapping a CIPA range that starts in an empty region, checking that it finds and removes the mappings inside the range, keeps unrelated mappings.
5. First map NIPA -> CIPA1, unmap that NIPA, then create NIPA -> CIPA2, and check that unmapping the CIPA1 leaves the new mapping alone.
6. Creating multiple identical mappings, and checked unmapping the CIPA once removes the mapping.
The dumped shadow page tables all contain the expected mappings.
Thanks,
Wei-Lin Chang
[...]