KVM: arm64: Unconditionally top up the mmu memcache on user fault
Subsystems (3)
PATCH v2 1/1 KVM: arm64: Unconditionally top up the mmu memcache on user fault
The S2 user fault path tops up the MMU memcache (used for page table pre-allocation as the point at which page tables are installed may not allocate) except when:
* It's a permission fault AND
* dirty page logging is disabled AND
* pKVM is disabled
At which point it is assumed no page table will be allocated.
However this is not safe - if the mapping size differs from that at which the permission fault was taken, we must install a new mapping, which may require a page table. This can happen when:
* A guest hypervisor replaces a block in its own S2 with a table without issuing a TLBI (likely due to a bug) - the shadow S2 mapping size is clamped to that of the guest's S2, so the shadow block has to be collapsed.
* A guest hypervisor does issue the TLBI, but races with another vCPU which has already taken a permission fault on the shadow block.
* Nested virtualisation isn't in use at all, but the VMM replaces a THP with 64K hugetlb pages while a permission fault on it is in flight.
With the memcache empty, kvm_mmu_memory_cache_alloc() hits its guest-triggerable WARN_ON(!nobjs) and falls back to a GFP_ATOMIC allocation under mmu_lock, with a BUG_ON() if that fails.
Therefore, top up the memcache unconditionally. This is cheap, as the top up does nothing if the cache already holds enough objects.
Note that dirty logging had a similar issue, addressed in commit 56fd1fc526e6 ("KVM: arm64: Top up stage-2 memcache for dirty logging faults").
Local testing with dirty logging disabled indicated no performance impact.
kvm_page_table_test was modified to take 65,536 permission faults with dirty logging disabled, and run on an Apple M2 with the kernel under test running as an NV guest. The median time across ~70 runs was 1.059s without this patch and 1.062s with it.
A control stage in the same runs, whose code this patch does not touch, showed a similar level of variation so this is clearly within noise levels.
---
v2:
* Switched to always topping up as per Usama.
* Got performance numbers as per Leo.
* Tweaked commit message to be more succinct.
* Retitled the patch as it is no longer NV-specific.
* Added a Fixes tag for the non-NV case, which predates NV support.
v1:
https://lore.kernel.org/r/[email protected]
--- arch/arm64/kvm/mmu.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)
arch/arm64/kvm/mmu.c +3 -16
@@ -2248,28 +2248,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,static int user_mem_abort (const struct kvm_s2_fault_desc *s2fd,struct kvm_s2_fault_result *result) { -bool perm_fault =kvm_s2_fault_is_perm (s2fd);struct kvm_s2_fault_vma_info s2vi = {};enum kvm_pgtable_prot prot;void *memcache;int ret; -/* -* Permission faults just need to update the existing leaf entry, -* and so normally don't require allocations from the memcache. The -* only exception to this is when dirty logging is enabled at runtime -* and a fault needs to collapse a block entry into a table. -* Under pKVM a permission fault can also collapse pages into a block, -* which needs a fresh mapping object, and the hypervisor requires the -* min-pages memcache even when the install allocates nothing. -*/ memcache =get_mmu_memcache (s2fd->vcpu); -if (!perm_fault ||memslot_is_logging (s2fd->memslot) || -is_protected_kvm_enabled ()) { - ret =topup_mmu_memcache (s2fd->mmu, memcache); -if (ret) -return ret; - } + ret =topup_mmu_memcache (s2fd->mmu, memcache); +if (ret) +return ret; ret =kvm_s2_fault_pin_pfn (s2fd, &s2vi);if (ret == -EHWPOISON ) {
---
base-commit: 012e4bc01e339a65dc7c59181b5efb33b171d12c
change-id: 20261126-fix-nv-cache-topup-9ab1fd829014
Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Quoted 36 lines · Lorenzo Stoakes (ARM)
The S2 user fault path tops up the MMU memcache (used for page table pre-allocation as the point at which page tables are installed may not allocate) except when:
* It's a permission fault AND
* dirty page logging is disabled AND
* pKVM is disabledAt which point it is assumed no page table will be allocated.
However this is not safe - if the mapping size differs from that at which the permission fault was taken, we must install a new mapping, which may require a page table. This can happen when:
* A guest hypervisor replaces a block in its own S2 with a table without issuing a TLBI (likely due to a bug) - the shadow S2 mapping size is clamped to that of the guest's S2, so the shadow block has to be collapsed.
* A guest hypervisor does issue the TLBI, but races with another vCPU which has already taken a permission fault on the shadow block.
* Nested virtualisation isn't in use at all, but the VMM replaces a THP with 64K hugetlb pages while a permission fault on it is in flight.
With the memcache empty,
kvm_mmu_memory_cache_alloc()hits its guest-triggerableWARN_ON(!nobjs)and falls back to aGFP_ATOMICallocation undermmu_lock, with aBUG_ON()if that fails.Therefore, top up the memcache unconditionally. This is cheap, as the top up does nothing if the cache already holds enough objects.
Note that dirty logging had a similar issue, addressed in commit
56fd1fc526e6("KVM: arm64: Top up stage-2 memcache for dirty logging faults").
Quoted 7 lines
Local testing with dirty logging disabled indicated no performance impact.
kvm_page_table_testwas modified to take 65,536 permission faults with dirty logging disabled, and run on an Apple M2 with the kernel under test running as an NV guest. The median time across ~70 runs was 1.059s without this patch and 1.062s with it.
Cool!
I wonder how that will impact with Usama's patch prepended.
Thanks!
Leo
Quoted 90 lines · Lorenzo Stoakes (ARM)
The S2 user fault path tops up the MMU memcache (used for page table pre-allocation as the point at which page tables are installed may not allocate) except when:
* It's a permission fault AND
* dirty page logging is disabled AND
* pKVM is disabledAt which point it is assumed no page table will be allocated.
However this is not safe - if the mapping size differs from that at which the permission fault was taken, we must install a new mapping, which may require a page table. This can happen when:
* A guest hypervisor replaces a block in its own S2 with a table without issuing a TLBI (likely due to a bug) - the shadow S2 mapping size is clamped to that of the guest's S2, so the shadow block has to be collapsed.
* A guest hypervisor does issue the TLBI, but races with another vCPU which has already taken a permission fault on the shadow block.
* Nested virtualisation isn't in use at all, but the VMM replaces a THP with 64K hugetlb pages while a permission fault on it is in flight.
With the memcache empty,
kvm_mmu_memory_cache_alloc()hits its guest-triggerableWARN_ON(!nobjs)and falls back to aGFP_ATOMICallocation undermmu_lock, with aBUG_ON()if that fails.Therefore, top up the memcache unconditionally. This is cheap, as the top up does nothing if the cache already holds enough objects.
Note that dirty logging had a similar issue, addressed in commit
56fd1fc526e6("KVM: arm64: Top up stage-2 memcache for dirty logging faults").Local testing with dirty logging disabled indicated no performance impact.
kvm_page_table_testwas modified to take 65,536 permission faults with dirty logging disabled, and run on an Apple M2 with the kernel under test running as an NV guest. The median time across ~70 runs was 1.059s without this patch and 1.062s with it.A control stage in the same runs, whose code this patch does not touch, showed a similar level of variation so this is clearly within noise levels.
Fixes 7d894834a305 ("KVM: arm64: Add usage of stage 2 fault lookup level in user_mem_abort()")Fixes fd276e71d1e7 ("KVM: arm64: nv: Handle shadow stage 2 page faults")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>Reviewed-by Leonardo Bras <[email protected]>---
v2:
* Switched to always topping up as per Usama.
* Got performance numbers as per Leo.
* Tweaked commit message to be more succinct.
* Retitled the patch as it is no longer NV-specific.
* Added a Fixes tag for the non-NV case, which predates NV support.v1:
https://lore.kernel.org/r/[email protected]--- arch/arm64/kvm/mmu.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)arch/arm64/kvm/mmu.c +0
-10@@ -2248,28 +2248,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,static int user_mem_abort (const struct kvm_s2_fault_desc *s2fd,struct kvm_s2_fault_result *result) { -bool perm_fault =kvm_s2_fault_is_perm (s2fd);struct kvm_s2_fault_vma_info s2vi = {};enum kvm_pgtable_prot prot;void *memcache;int ret; -/* -* Permission faults just need to update the existing leaf entry, -* and so normally don't require allocations from the memcache. The -* only exception to this is when dirty logging is enabled at runtime -* and a fault needs to collapse a block entry into a table. -* Under pKVM a permission fault can also collapse pages into a block, -* which needs a fresh mapping object, and the hypervisor requires the -* min-pages memcache even when the install allocates nothing. -*/ memcache =get_mmu_memcache (s2fd->vcpu);
arch/arm64/kvm/mmu.c static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, · line 2260
Quoted 10 lines
arch/arm64/kvm/mmu.c +3
-6-if (!perm_fault ||memslot_is_logging (s2fd->memslot) || -is_protected_kvm_enabled ()) { - ret =topup_mmu_memcache (s2fd->mmu, memcache); -if (ret) -return ret; - } + ret =topup_mmu_memcache (s2fd->mmu, memcache); +if (ret) +return ret;
LGTM! Thanks for the fix!
Quoted 8 lines
arch/arm64/kvm/mmu.c +0
-1ret =kvm_s2_fault_pin_pfn (s2fd, &s2vi);if (ret == -EHWPOISON ) { ---base-commit:
012e4bc01e339a65dc7c59181b5efb33b171d12c
change-id: 20261126-fix-nv-cache-topup-9ab1fd829014Cheers,