RFC: Solve several problems in stage 2 translation
v2 cover RFC: Solve several problems in stage 2 translation
Hi, this is the second version, thanks for reading.
PATCH1/3:
Procedures of hyp stage 1 mapping and guest stage 2 mapping are different, but they are tied closely by function kvm_set_valid_leaf_pte(). So separate them by rewriting kvm_set_valid_leaf_pte().
PATCH2/3:
To avoid unnecessary update and small loops, add prejudgement in the translation fault handler: Skip updating the PTE with break-before-make if we are trying to recreate the exact same mapping or only change the access permissions. Actually, change of permissions will be handled through the relax_perms path next time if necessary.
(1) If there are some vCPUs accessing the same GPA at the same time and the leaf PTE is not set yet, then they will all cause translation faults and the first vCPU holding mmu_lock will set valid leaf PTE, and the others will later update the old PTE with a new one if they are different.
(2) When changing a leaf entry or a table entry with break-before-make, if there are some vCPUs accessing the same GPA just catch the moment when the target PTE is set invalid in a BBM procedure coincidentally, they will all cause translation faults and will later update the old PTE with a new one if they are different.
The worst case can be like this: vCPU A causes a translation fault with RW prot and sets the leaf PTE with RW permissions, and then the next vCPU B with RO prot updates the PTE back to RO permissions with break-before-make. And the BBM-invalid moment may trigger more unnecessary translation faults, then some useless small loops might occur which could lead to vCPU stuck.
PATCH3/3:
We now mark the page dirty and set the bitmap before calling fault handlers in user_mem_abort(), and we might end up having spurious dirty pages if update of permissions or mapping has failed. So, mark the page dirty only if the fault is handled successfully.
Let the guest directly enter again but not return to userspace if we were trying to recreate the same mapping or only change access permissions with BBM, which is not permitted in the mapping path.
Changes from v1:
- Make part of the diff as an independent patch (PATCH1/3),
and add Will's Signed-off-by.
- Use *return -EPERM* way when changing permissions only in the mapping path.
- Add a new patch (PATCH3/3).
Yanan Wang (3):
KVM: arm64: Decouple partial code of hyp stage 1 mapping and guest
stage 2 mapping
KVM: arm64: Add prejudgement for relaxing permissions only case in
stage2 translation fault handler
KVM: arm64: Mark the page dirty only if the fault is handled
successfully
arch/arm64/kvm/hyp/pgtable.c | 78 ++++++++++++++++++++---------------- arch/arm64/kvm/mmu.c | 18 +++++++-- 2 files changed, 58 insertions(+), 38 deletions(-)
Signature · 1 line
2.19.1
PATCH v2 1/3 KVM: arm64: Decouple partial code of hyp stage 1 mapping and guest stage 2 mapping
Procedures of hyp stage 1 mapping and guest stage 2 mapping are different, but they are tied closely by function kvm_set_valid_leaf_pte(). So separate them by rewriting kvm_set_valid_leaf_pte().
--- arch/arm64/kvm/hyp/pgtable.c | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +27 -27
@@ -170,10 +170,9 @@ static void kvm_set_table_pte(kvm_pte_t *ptep, kvm_pte_t *childp)smp_store_release (ptep, pte); } -static bool kvm_set_valid_leaf_pte (kvm_pte_t *ptep,u64 pa,kvm_pte_t attr, -u32 level) +static kvm_pte_t kvm_init_valid_leaf_pte (u64 pa,kvm_pte_t attr,u32 level) { -kvm_pte_t old = *ptep, pte =kvm_phys_to_pte (pa); +kvm_pte_t pte =kvm_phys_to_pte (pa);u64 type = (level ==KVM_PGTABLE_MAX_LEVELS -1 ) ?KVM_PTE_TYPE_PAGE :KVM_PTE_TYPE_BLOCK ;
@@ -181,12 +180,7 @@ static bool kvm_set_valid_leaf_pte(kvm_pte_t *ptep, u64 pa, kvm_pte_t attr, pte |=FIELD_PREP (KVM_PTE_TYPE , type); pte |=KVM_PTE_VALID ; -/* Tolerate KVM recreating the exact same mapping. */ -if (kvm_pte_valid (old)) -return old == pte; - -smp_store_release (ptep, pte); -return true; +return pte; } static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data, u64 addr,
@@ -341,12 +335,17 @@ static int hyp_map_set_prot_attr(enum kvm_pgtable_prot prot,static bool hyp_map_walker_try_leaf (u64 addr,u64 end,u32 level,kvm_pte_t *ptep,struct hyp_map_data *data) { +kvm_pte_t new, old = *ptep;u64 granule =kvm_granule_size (level), phys = data->phys;if (!kvm_block_mapping_supported (addr, end, phys, level))return false; -WARN_ON (!kvm_set_valid_leaf_pte (ptep, phys, data->attr, level)); +/* Tolerate KVM recreating the exact same mapping */ + new =kvm_init_valid_leaf_pte (phys, data->attr, level); +if (old != new && !WARN_ON (kvm_pte_valid (old))) +smp_store_release (ptep, new); + data->phys += granule;return true; }
@@ -465,27 +464,29 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,kvm_pte_t *ptep, struct stage2_map_data *data) { + kvm_pte_t new, old = *ptep;u64 granule =kvm_granule_size (level), phys = data->phys; +struct page *page =virt_to_page (ptep);if (!kvm_block_mapping_supported (addr, end, phys, level))return false; -/* -* If the PTE was already valid, drop the refcount on the table -* early, as it will be bumped-up again in stage2_map_walk_leaf(). -* This ensures that the refcount stays constant across a valid to -* valid PTE update. -*/ -if (kvm_pte_valid (*ptep)) -put_page (virt_to_page (ptep)); + new =kvm_init_valid_leaf_pte (phys, data->attr, level); +if (kvm_pte_valid (old)) { +/* Tolerate KVM recreating the exact same mapping */ +if (old == new) + goto out; -if (kvm_set_valid_leaf_pte (ptep, phys, data->attr, level)) - goto out; +/* There's an existing different valid leaf entry, so perform +* break-before-make. +*/ +kvm_set_invalid_pte (ptep); +kvm_call_hyp (__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level); +put_page (page); + } -/* There's an existing valid leaf entry, so perform break-before-make */ -kvm_set_invalid_pte (ptep); -kvm_call_hyp (__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level); -kvm_set_valid_leaf_pte (ptep, phys, data->attr, level); +smp_store_release (ptep, new); +get_page (page); out: data->phys += granule;return true;
@@ -527,7 +528,7 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, }if (stage2_map_walker_try_leaf (addr, end, level, ptep, data)) - goto out_get_page; +return 0 ;if (WARN_ON (level ==KVM_PGTABLE_MAX_LEVELS -1 ))return -EINVAL ;
@@ -551,9 +552,8 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, }kvm_set_table_pte (ptep, childp); - -out_get_page:get_page (page); +return 0 ; }
Signature · 1 line
2.19.1
PATCH v2 3/3 KVM: arm64: Mark the page dirty only if the fault is handled successfully
In reply to Yanan WangWe now mark the page dirty and set the bitmap before calling fault handlers in user_mem_abort(), and we might end up having spurious dirty pages if update of permissions or mapping has failed.
So, mark the page dirty only if the fault is handled successfully.
Let the guest directly enter again but not return to userspace if we were trying to recreate the same mapping or only change access permissions with BBM, which is not permitted in the mapping path.
--- arch/arm64/kvm/mmu.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +14 -4
@@ -879,11 +879,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,if (vma_pagesize ==PAGE_SIZE && !force_pte) vma_pagesize =transparent_hugepage_adjust (memslot, hva, &pfn, &fault_ipa); -if (writable) { +if (writable) prot |=KVM_PGTABLE_PROT_W ; -kvm_set_pfn_dirty (pfn); -mark_page_dirty (kvm, gfn); - }if (fault_status !=FSC_PERM && !device)clean_dcache_guest_page (pfn, vma_pagesize);
@@ -911,6 +908,19 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, memcache); } +/* Mark the page dirty only if the fault is handled successfully */ +if (writable && !ret) { +kvm_set_pfn_dirty (pfn); +mark_page_dirty (kvm, gfn); + } + +/* Let the guest directly enter again if we were trying to recreate the +* same mapping or only change access permissions with BBM, which is not +* permitted in the mapping path. +*/ +if (ret == -EAGAIN ) + ret =0 ; + out_unlock:spin_unlock (&kvm->mmu_lock);kvm_set_pfn_accessed (pfn);
Signature · 1 line
2.19.1
PATCH v2 2/3 KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler
In reply to Yanan WangIn dirty-logging, or dirty-logging-stopped time, even normal running time of a guest configed with huge mappings and numbers of vCPUs, translation faults by different vCPUs on the same GPA could occur successively almost at the same time. There are two reasons for it.
(1) If there are some vCPUs accessing the same GPA at the same time and the leaf PTE is not set yet, then they will all cause translation faults and the first vCPU holding mmu_lock will set valid leaf PTE, and the others will later update the old PTE with a new one if they are different.
(2) When changing a leaf entry or a table entry with break-before-make, if there are some vCPUs accessing the same GPA just catch the moment when the target PTE is set invalid in a BBM procedure coincidentally, they will all cause translation faults and will later update the old PTE with a new one if they are different.
The worst case can be like this: vCPU A causes a translation fault with RW prot and sets the leaf PTE with RW permissions, and then the next vCPU B with RO prot updates the PTE back to RO permissions with break-before-make. And the BBM-invalid moment may trigger more unnecessary translation faults, then some useless small loops might occur which could lead to vCPU stuck.
To avoid unnecessary update and small loops, add prejudgement in the translation fault handler: Skip updating the PTE with break-before-make if we are trying to recreate the exact same mapping or only change the access permissions. Actually, change of permissions will be handled through the relax_perms path next time if necessary.
--- arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +19 -9
@@ -45,6 +45,10 @@#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) +#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ + KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ + KVM_PTE_LEAF_ATTR_HI_S2_XN) +struct kvm_pgtable_walk_data {struct kvm_pgtable *pgt;struct kvm_pgtable_walker *walker;
@@ -460,7 +464,7 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot,return 0 ; } -static bool stage2_map_walker_try_leaf (u64 addr,u64 end,u32 level, +static int stage2_map_walker_try_leaf (u64 addr,u64 end,u32 level,kvm_pte_t *ptep,struct stage2_map_data *data) {
@@ -469,13 +473,18 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,struct page *page =virt_to_page (ptep);if (!kvm_block_mapping_supported (addr, end, phys, level)) -return false; +return 1 ; new =kvm_init_valid_leaf_pte (phys, data->attr, level);if (kvm_pte_valid (old)) { -/* Tolerate KVM recreating the exact same mapping */ -if (old == new) - goto out; +/* +* Skip updating the PTE with break-before-make if we are trying +* to recreate the exact same mapping or only change the access +* permissions. Actually, change of permissions will be handled +* through the relax_perms path next time if necessary. +*/ +if (!((old ^ new) & (~KVM_PTE_LEAF_ATTR_S2_PERMS ))) +return -EAGAIN ;/* There's an existing different valid leaf entry, so perform * break-before-make.
@@ -487,9 +496,8 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,smp_store_release (ptep, new);get_page (page); -out: data->phys += granule; -return true; +return 0 ; } static int stage2_map_walk_table_pre(u64 addr, u64 end, u32 level,
@@ -517,6 +525,7 @@ static int stage2_map_walk_table_pre(u64 addr, u64 end, u32 level,static int stage2_map_walk_leaf (u64 addr,u64 end,u32 level,kvm_pte_t *ptep,struct stage2_map_data *data) { +int ret;kvm_pte_t *childp, pte = *ptep;struct page *page =virt_to_page (ptep);
@@ -527,8 +536,9 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,return 0 ; } -if (stage2_map_walker_try_leaf (addr, end, level, ptep, data)) -return 0 ; + ret =stage2_map_walker_try_leaf (addr, end, level, ptep, data); +if (ret <=0 ) +return ret;if (WARN_ON (level ==KVM_PGTABLE_MAX_LEVELS -1 ))return -EINVAL ;
Signature · 1 line
2.19.1
Hi Will, Marc,
Gently Ping.
Is there any comments about this V2 series?
Many thanks,
Yanan.
Quoted 58 lines · Yanan Wang
Hi, this is the second version, thanks for reading.
PATCH1/3:
Procedures of hyp stage 1 mapping and guest stage 2 mapping are different, but they are tied closely by functionkvm_set_valid_leaf_pte(). So separate them by rewritingkvm_set_valid_leaf_pte().PATCH2/3:
To avoid unnecessary update and small loops, add prejudgement in the translation fault handler: Skip updating the PTE with break-before-make if we are trying to recreate the exact same mapping or only change the access permissions. Actually, change of permissions will be handled through therelax_permspath next time if necessary.(1) If there are some vCPUs accessing the same GPA at the same time and the leaf PTE is not set yet, then they will all cause translation faults and the first vCPU holding
mmu_lockwill set valid leaf PTE, and the others will later update the old PTE with a new one if they are different.(2) When changing a leaf entry or a table entry with break-before-make, if there are some vCPUs accessing the same GPA just catch the moment when the target PTE is set invalid in a BBM procedure coincidentally, they will all cause translation faults and will later update the old PTE with a new one if they are different.
The worst case can be like this: vCPU A causes a translation fault with RW prot and sets the leaf PTE with RW permissions, and then the next vCPU B with RO prot updates the PTE back to RO permissions with break-before-make. And the BBM-invalid moment may trigger more unnecessary translation faults, then some useless small loops might occur which could lead to vCPU stuck.
PATCH3/3:
We now mark the page dirty and set the bitmap before calling fault handlers inuser_mem_abort(), and we might end up having spurious dirty pages if update of permissions or mapping has failed. So, mark the page dirty only if the fault is handled successfully.Let the guest directly enter again but not return to userspace if we were trying to recreate the same mapping or only change access permissions with BBM, which is not permitted in the mapping path.
Changes from v1:
- Make part of the diff as an independent patch (PATCH1/3),
and add Will's Signed-off-by.
- Use *return -EPERM* way when changing permissions only in the mapping path.
- Add a new patch (PATCH3/3).Yanan Wang (3):
KVM: arm64: Decouple partial code of hyp stage 1 mapping and guest
stage 2 mapping
KVM: arm64: Add prejudgement for relaxing permissions only case in
stage2 translation fault handler
KVM: arm64: Mark the page dirty only if the fault is handled
successfullyarch/arm64/kvm/hyp/pgtable.c | 78 ++++++++++++++++++++---------------- arch/arm64/kvm/mmu.c | 18 +++++++-- 2 files changed, 58 insertions(+), 38 deletions(-)
Will Deacon
maintainerRe: [PATCH v2 2/3] KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler
In reply to Yanan WangQuoted 58 lines · Yanan Wang
In dirty-logging, or dirty-logging-stopped time, even normal running time of a guest configed with huge mappings and numbers of vCPUs, translation faults by different vCPUs on the same GPA could occur successively almost at the same time. There are two reasons for it.
(1) If there are some vCPUs accessing the same GPA at the same time and the leaf PTE is not set yet, then they will all cause translation faults and the first vCPU holding
mmu_lockwill set valid leaf PTE, and the others will later update the old PTE with a new one if they are different.(2) When changing a leaf entry or a table entry with break-before-make, if there are some vCPUs accessing the same GPA just catch the moment when the target PTE is set invalid in a BBM procedure coincidentally, they will all cause translation faults and will later update the old PTE with a new one if they are different.
The worst case can be like this: vCPU A causes a translation fault with RW prot and sets the leaf PTE with RW permissions, and then the next vCPU B with RO prot updates the PTE back to RO permissions with break-before-make. And the BBM-invalid moment may trigger more unnecessary translation faults, then some useless small loops might occur which could lead to vCPU stuck.
To avoid unnecessary update and small loops, add prejudgement in the translation fault handler: Skip updating the PTE with break-before-make if we are trying to recreate the exact same mapping or only change the access permissions. Actually, change of permissions will be handled through the
relax_permspath next time if necessary.Signed-off-by Yanan Wang <[email protected]>--- arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)arch/arm64/kvm/hyp/pgtable.c +5
-1@@ -45,6 +45,10 @@#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) +#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ + KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ + KVM_PTE_LEAF_ATTR_HI_S2_XN) +struct kvm_pgtable_walk_data {struct kvm_pgtable *pgt;struct kvm_pgtable_walker *walker;@@ -460,7 +464,7 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot,return 0 ; } -static bool stage2_map_walker_try_leaf (u64 addr,u64 end,u32 level, +static int stage2_map_walker_try_leaf (u64 addr,u64 end,u32 level,kvm_pte_t *ptep,struct stage2_map_data *data) {
arch/arm64/kvm/hyp/pgtable.c static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, · line 476
Quoted 5 lines
arch/arm64/kvm/hyp/pgtable.c +1
-1@@ -469,13 +473,18 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,struct page *page =virt_to_page (ptep);if (!kvm_block_mapping_supported (addr, end, phys, level)) -return false; +return 1 ;
It would probably be cleaner to return another error code here, as we have failed to install a mapping (e.g. E2BIG or perhaps more perversely, ENOTBLK). Then the caller can decide to install a trable.
Will
Will Deacon
maintainerRe: [PATCH v2 3/3] KVM: arm64: Mark the page dirty only if the fault is handled successfully
In reply to Yanan WangQuoted 37 lines · Yanan Wang
We now mark the page dirty and set the bitmap before calling fault handlers in
user_mem_abort(), and we might end up having spurious dirty pages if update of permissions or mapping has failed.
So, mark the page dirty only if the fault is handled successfully.Let the guest directly enter again but not return to userspace if we were trying to recreate the same mapping or only change access permissions with BBM, which is not permitted in the mapping path.
Signed-off-by Yanan Wang <[email protected]>--- arch/arm64/kvm/mmu.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)arch/arm64/kvm/mmu.c +3
-4@@ -879,11 +879,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,if (vma_pagesize ==PAGE_SIZE && !force_pte) vma_pagesize =transparent_hugepage_adjust (memslot, hva, &pfn, &fault_ipa); -if (writable) { +if (writable) prot |=KVM_PGTABLE_PROT_W ; -kvm_set_pfn_dirty (pfn); -mark_page_dirty (kvm, gfn); - }if (fault_status !=FSC_PERM && !device)clean_dcache_guest_page (pfn, vma_pagesize);@@ -911,6 +908,19 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, memcache); } +/* Mark the page dirty only if the fault is handled successfully */ +if (writable && !ret) {
arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 922
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+kvm_set_pfn_dirty (pfn); +mark_page_dirty (kvm, gfn); + } + +/* Let the guest directly enter again if we were trying to recreate the +* same mapping or only change access permissions with BBM, which is not +* permitted in the mapping path. +*/ +if (ret == -EAGAIN ) + ret =0 ;
Maybe just 'return ret != -EAGAIN ? ret : 0;' at the end of the function?
Will
Re: [PATCH v2 3/3] KVM: arm64: Mark the page dirty only if the fault is handled successfully
Quoted 36 lines · Will Deacon
Quoted 35 lines · Yanan Wang
We now mark the page dirty and set the bitmap before calling fault handlers in
user_mem_abort(), and we might end up having spurious dirty pages if update of permissions or mapping has failed.
So, mark the page dirty only if the fault is handled successfully.Let the guest directly enter again but not return to userspace if we were trying to recreate the same mapping or only change access permissions with BBM, which is not permitted in the mapping path.
Signed-off-by Yanan Wang <[email protected]>--- arch/arm64/kvm/mmu.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)arch/arm64/kvm/mmu.c +1
-4@@ -879,11 +879,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,if (vma_pagesize ==PAGE_SIZE && !force_pte) vma_pagesize =transparent_hugepage_adjust (memslot, hva, &pfn, &fault_ipa); -if (writable) { +if (writable) prot |=KVM_PGTABLE_PROT_W ; -kvm_set_pfn_dirty (pfn); -mark_page_dirty (kvm, gfn); - }if (fault_status !=FSC_PERM && !device)clean_dcache_guest_page (pfn, vma_pagesize);@@ -911,6 +908,19 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, memcache); }
Quoted 13 lines
arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 922
Quoted 12 lines
arch/arm64/kvm/mmu.c +12
-0+/* Mark the page dirty only if the fault is handled successfully */ +if (writable && !ret) { +kvm_set_pfn_dirty (pfn); +mark_page_dirty (kvm, gfn); + } + +/* Let the guest directly enter again if we were trying to recreate the +* same mapping or only change access permissions with BBM, which is not +* permitted in the mapping path. +*/ +if (ret == -EAGAIN ) + ret =0 ;Maybe just 'return ret !=
-EAGAIN? ret : 0;' at the end of the function?
Yes, it's more concise.
Re: [PATCH v2 2/3] KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler
In reply to Will DeaconQuoted 61 lines · Will Deacon
Quoted 60 lines · Yanan Wang
In dirty-logging, or dirty-logging-stopped time, even normal running time of a guest configed with huge mappings and numbers of vCPUs, translation faults by different vCPUs on the same GPA could occur successively almost at the same time. There are two reasons for it.
(1) If there are some vCPUs accessing the same GPA at the same time and the leaf PTE is not set yet, then they will all cause translation faults and the first vCPU holding
mmu_lockwill set valid leaf PTE, and the others will later update the old PTE with a new one if they are different.(2) When changing a leaf entry or a table entry with break-before-make, if there are some vCPUs accessing the same GPA just catch the moment when the target PTE is set invalid in a BBM procedure coincidentally, they will all cause translation faults and will later update the old PTE with a new one if they are different.
The worst case can be like this: vCPU A causes a translation fault with RW prot and sets the leaf PTE with RW permissions, and then the next vCPU B with RO prot updates the PTE back to RO permissions with break-before-make. And the BBM-invalid moment may trigger more unnecessary translation faults, then some useless small loops might occur which could lead to vCPU stuck.
To avoid unnecessary update and small loops, add prejudgement in the translation fault handler: Skip updating the PTE with break-before-make if we are trying to recreate the exact same mapping or only change the access permissions. Actually, change of permissions will be handled through the
relax_permspath next time if necessary.Signed-off-by Yanan Wang <[email protected]>--- arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)arch/arm64/kvm/hyp/pgtable.c +5
-1@@ -45,6 +45,10 @@#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) +#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ + KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ + KVM_PTE_LEAF_ATTR_HI_S2_XN) +struct kvm_pgtable_walk_data {struct kvm_pgtable *pgt;struct kvm_pgtable_walker *walker;@@ -460,7 +464,7 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot,return 0 ; } -static bool stage2_map_walker_try_leaf (u64 addr,u64 end,u32 level, +static int stage2_map_walker_try_leaf (u64 addr,u64 end,u32 level,kvm_pte_t *ptep,struct stage2_map_data *data) {@@ -469,13 +473,18 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,struct page *page =virt_to_page (ptep);
Quoted 7 lines
arch/arm64/kvm/hyp/pgtable.c static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, · line 476
Quoted 3 lines
arch/arm64/kvm/hyp/pgtable.c +1
-1if (!kvm_block_mapping_supported (addr, end, phys, level)) -return false; +return 1 ;It would probably be cleaner to return another error code here, as we have failed to install a mapping (e.g. E2BIG or perhaps more perversely, ENOTBLK). Then the caller can decide to install a trable.
Ok, I will make some adjustment for this and post the v3 soon after.
Thanks,
Yanan.