KVM: arm64: Top up the mmu memcache on user fault for NV
Subsystems (3)
PATCH 1/1 KVM: arm64: Top up the mmu memcache on user fault for NV
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 nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at PAGE_SIZE, which requires a page table to collapse the block into.
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, also top up the memcache for permission faults taken on a shadow S2.
Note that dirty logging had a similar issue, addressed in commit 56fd1fc526e6 ("KVM: arm64: Top up stage-2 memcache for dirty logging faults").
--- arch/arm64/kvm/mmu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +5 -3
@@ -2256,16 +2256,18 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,/* * 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 so normally don't require allocations from the memcache. One +* 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. +* With nested, a shadow stage-2 block may also need to be collapsed +* into a table if the guest's own stage-2 mapping has become smaller. */ memcache =get_mmu_memcache (s2fd->vcpu);if (!perm_fault ||memslot_is_logging (s2fd->memslot) || -is_protected_kvm_enabled ()) { +is_protected_kvm_enabled () || s2fd->nested) { ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret;
---
base-commit: 012e4bc01e339a65dc7c59181b5efb33b171d12c
change-id: 20261126-fix-nv-cache-topup-9ab1fd829014
Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Quoted 8 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 disabled
Quoted 7 lines
At which point it is assumed no page table will be allocated.
However nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at
PAGE_SIZE, which requires a page table to collapse the block into.
Can you please expand on how would that happen?
IIUC when a guest hypervisor replaces a block mapping with a table, it calls a tlbi in the range, and L0 will trap and remove the Shadow S2 entry, then next access shouldn't be a permission but a translation fault.
What am I missing?
Thanks!
Leo
Quoted 54 lines
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, also top up the memcache for permission faults taken on a shadow S2.
Note that dirty logging had a similar issue, addressed in commit
56fd1fc526e6("KVM: arm64: Top up stage-2 memcache for dirty logging faults").Fixes fd276e71d1e7 ("KVM: arm64: nv: Handle shadow stage 2 page faults")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/arm64/kvm/mmu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)arch/arm64/kvm/mmu.c +5
-4@@ -2256,16 +2256,18 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,/* * 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 so normally don't require allocations from the memcache. One +* 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. +* With nested, a shadow stage-2 block may also need to be collapsed +* into a table if the guest's own stage-2 mapping has become smaller. */ memcache =get_mmu_memcache (s2fd->vcpu);if (!perm_fault ||memslot_is_logging (s2fd->memslot) || -is_protected_kvm_enabled ()) { +is_protected_kvm_enabled () || s2fd->nested) { ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret; ---base-commit:
012e4bc01e339a65dc7c59181b5efb33b171d12c
change-id: 20261126-fix-nv-cache-topup-9ab1fd829014Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Quoted 17 lines · Leonardo Bras
Quoted 8 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 disabledQuoted 7 lines
At which point it is assumed no page table will be allocated.
However nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at
PAGE_SIZE, which requires a page table to collapse the block into.
Quoted 7 lines
Can you please expand on how would that happen?
IIUC when a guest hypervisor replaces a block mapping with a table, it calls a tlbi in the range, and L0 will trap and remove the Shadow S2 entry, then next access shouldn't be a permission but a translation fault.
What am I missing?
That's a great point, actually :)
But nothing really enforces that L1 does that for sure, even a bug could produce that, and you really want to avoid the host getting in bother in that case.
(The host kernel has to behave properly no matter what guests do).
I was able to (get claude to) repro this easily.
But actually I think there's another case too, even assuming everything's well-behaved:
* vCPU B: Writes to a read-only shadow block -> permission fault, exits to the host.
* vCPU A: Concurrently does its break/TLBI/make on the L1 S2, replacing the block with writable pages. The TLBI clears the shadow block.
* vCPU B: Now handles the fault. It's still a permission fault as far as the ESR is concerned so we skip the top-up, but the L1 S2 now says page rather than block, so we go to map a page and have to allocate page table(s) for it.
This needs the mapping size to have changed (otherwise we just try to relax permissions, which doesn't allocate) which can happen when splitting, replacing pages with a THP page, etc.
In general I think just topping up regardless is the better route, but for a backport this is the least big change to fix this.
Quoted 3 lines
Thanks!
Leo
--
Cheers, Lorenzo
Leonardo Bras
Reviewed-byQuoted 24 lines · Lorenzo Stoakes (ARM)
Quoted 23 lines · Leonardo Bras
Quoted 8 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 disabledQuoted 7 lines
At which point it is assumed no page table will be allocated.
However nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at
PAGE_SIZE, which requires a page table to collapse the block into.Can you please expand on how would that happen?
IIUC when a guest hypervisor replaces a block mapping with a table, it calls a tlbi in the range, and L0 will trap and remove the Shadow S2 entry, then next access shouldn't be a permission but a translation fault.
Quoted 8 lines
Quoted 1 line
What am I missing?
That's a great point, actually :)
But nothing really enforces that L1 does that for sure, even a bug could produce that, and you really want to avoid the host getting in bother in that case.
(The host kernel has to behave properly no matter what guests do).
Oh right, there can be malfunctioning guests :)
Quoted 17 lines
I was able to (get claude to) repro this easily.
But actually I think there's another case too, even assuming everything's well-behaved:
* vCPU B: Writes to a read-only shadow block -> permission fault, exits to the host.
* vCPU A: Concurrently does its break/TLBI/make on the L1 S2, replacing the block with writable pages. The TLBI clears the shadow block.
* vCPU B: Now handles the fault. It's still a permission fault as far as the ESR is concerned so we skip the top-up, but the L1 S2 now says page rather than block, so we go to map a page and have to allocate page table(s) for it.
Quoted 7 lines
This needs the mapping size to have changed (otherwise we just try to relax permissions, which doesn't allocate) which can happen when splitting, replacing pages with a THP page, etc.
In general I think just topping up regardless is the better route, but for a backport this is the least big change to fix this.
Ok, fair enough.
FWIW:
Thanks!
Leo
Quoted 57 lines · Leonardo Bras
Quoted 24 lines · Lorenzo Stoakes (ARM)
Quoted 23 lines · Leonardo Bras
Quoted 8 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 disabledQuoted 7 lines
At which point it is assumed no page table will be allocated.
However nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at
PAGE_SIZE, which requires a page table to collapse the block into.Can you please expand on how would that happen?
IIUC when a guest hypervisor replaces a block mapping with a table, it calls a tlbi in the range, and L0 will trap and remove the Shadow S2 entry, then next access shouldn't be a permission but a translation fault.
Quoted 8 lines
Quoted 1 line
What am I missing?
That's a great point, actually :)
But nothing really enforces that L1 does that for sure, even a bug could produce that, and you really want to avoid the host getting in bother in that case.
(The host kernel has to behave properly no matter what guests do).
Oh right, there can be malfunctioning guests :)
Quoted 21 lines
I was able to (get claude to) repro this easily.
But actually I think there's another case too, even assuming everything's well-behaved:
* vCPU B: Writes to a read-only shadow block -> permission fault, exits to the host.
* vCPU A: Concurrently does its break/TLBI/make on the L1 S2, replacing the block with writable pages. The TLBI clears the shadow block.
* vCPU B: Now handles the fault. It's still a permission fault as far as the ESR is concerned so we skip the top-up, but the L1 S2 now says page rather than block, so we go to map a page and have to allocate page table(s) for it.
This needs the mapping size to have changed (otherwise we just try to relax permissions, which doesn't allocate) which can happen when splitting, replacing pages with a THP page, etc.
Quoted 8 lines
Quoted 3 lines
In general I think just topping up regardless is the better route, but for a backport this is the least big change to fix this.
Ok, fair enough.
FWIW:
Reviewed-by Leonardo Bras <[email protected]>
Thanks! I plan to send a v2 with unconditional topup as per the discussion with Usama, would you be OK with me propagating the tag to that?
Quoted 3 lines
Thanks!
Leo
--
Cheers, Lorenzo
Quoted 62 lines · Lorenzo Stoakes (ARM)
Quoted 61 lines · Leonardo Bras
Quoted 24 lines · Lorenzo Stoakes (ARM)
Quoted 23 lines · Leonardo Bras
Quoted 8 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 disabledQuoted 7 lines
At which point it is assumed no page table will be allocated.
However nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at
PAGE_SIZE, which requires a page table to collapse the block into.Can you please expand on how would that happen?
IIUC when a guest hypervisor replaces a block mapping with a table, it calls a tlbi in the range, and L0 will trap and remove the Shadow S2 entry, then next access shouldn't be a permission but a translation fault.
Quoted 8 lines
Quoted 1 line
What am I missing?
That's a great point, actually :)
But nothing really enforces that L1 does that for sure, even a bug could produce that, and you really want to avoid the host getting in bother in that case.
(The host kernel has to behave properly no matter what guests do).
Oh right, there can be malfunctioning guests :)
Quoted 24 lines
I was able to (get claude to) repro this easily.
But actually I think there's another case too, even assuming everything's well-behaved:
* vCPU B: Writes to a read-only shadow block -> permission fault, exits to the host.
* vCPU A: Concurrently does its break/TLBI/make on the L1 S2, replacing the block with writable pages. The TLBI clears the shadow block.
* vCPU B: Now handles the fault. It's still a permission fault as far as the ESR is concerned so we skip the top-up, but the L1 S2 now says page rather than block, so we go to map a page and have to allocate page table(s) for it.
This needs the mapping size to have changed (otherwise we just try to relax permissions, which doesn't allocate) which can happen when splitting, replacing pages with a THP page, etc.
In general I think just topping up regardless is the better route, but for a backport this is the least big change to fix this.
Quoted 8 lines
Quoted 4 lines
Ok, fair enough.
FWIW:
Reviewed-by Leonardo Bras <[email protected]>Thanks! I plan to send a v2 with unconditional topup as per the discussion with Usama, would you be OK with me propagating the tag to that?
Sure, an even better idea, as it simplifies things :)
I suppose it won't negatively impact throughput, but it would be nice to see some numbers. (it can be even faster :))
Thanks!
Leo
Quoted 1 line · Leonardo Bras
Sure, an even better idea, as it simplifies things :)
Thanks :)
Quoted 2 lines
I suppose it won't negatively impact throughput, but it would be nice to see some numbers. (it can be even faster :))
I think in general we're fairly safe in terms of any perf impact since the topup only tops up if you need to, and assuming further page table mapping that is going to happen at some point in any case.
And in any case, the conditional is now wrong (it was hidden in the 40 magic number before), and I'm not sure it's possible to reasonably add another expression to the if().
I will try to get some numbers though regardless and see if there's anything impactful there!
--
Cheers, Lorenzo
Quoted 12 lines · Lorenzo Stoakes (ARM)
Quoted 1 line · Leonardo Bras
Sure, an even better idea, as it simplifies things :)
Thanks :)
Quoted 2 lines
I suppose it won't negatively impact throughput, but it would be nice to see some numbers. (it can be even faster :))
I think in general we're fairly safe in terms of any perf impact since the topup only tops up if you need to, and assuming further page table mapping that is going to happen at some point in any case.
Quoted 6 lines
And in any case, the conditional is now wrong (it was hidden in the 40 magic number before), and I'm not sure it's possible to reasonably add another expression to the
if().I will try to get some numbers though regardless and see if there's anything impactful there!
Since it will be testing for less stuff, I suppose it will be actually faster :P
Thanks!
Leo
Quoted 51 lines · On Thu, 26 Nov 2026 17:28:29 +0000 "Lorenzo Stoakes (ARM)" <[email protected]>…
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 nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at
PAGE_SIZE, which requires a page table to collapse the block into.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, also top up the memcache for permission faults taken on a shadow S2.
Note that dirty logging had a similar issue, addressed in commit
56fd1fc526e6("KVM: arm64: Top up stage-2 memcache for dirty logging faults").Fixes fd276e71d1e7 ("KVM: arm64: nv: Handle shadow stage 2 page faults")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/arm64/kvm/mmu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)arch/arm64/kvm/mmu.c +2
-2@@ -2256,16 +2256,18 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,/* * 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 so normally don't require allocations from the memcache. One +* 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.
arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, · line 2273
Quoted 10 lines
arch/arm64/kvm/mmu.c +3
-1+* With nested, a shadow stage-2 block may also need to be collapsed +* into a table if the guest's own stage-2 mapping has become smaller. */ memcache =get_mmu_memcache (s2fd->vcpu);if (!perm_fault ||memslot_is_logging (s2fd->memslot) || -is_protected_kvm_enabled ()) { +is_protected_kvm_enabled () || s2fd->nested) { ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret;
Thanks for the fix!
On the narrow condition being the smaller backport: I don't think it is. The hunk already conflicts on 7.3, where topup_mmu_memcache() still takes the vCPU, and the condition looks different on 7.2 and older, so every stable tree needs a small manual backport either way.
A permission fault can also allocate outside NV. For example, take an exec permission fault on a THP block, where the VMM replaces that memory with 64K hugetlb pages before the fault looks up the VMA:
- the MMU notifier unmaps the block, and mmu_seq is read afterwards
- kvm_s2_resolve_vma_size() forces PAGE_SIZE for the CONT_PTE hugetlb
- PAGE_SIZE != perm_fault_granule, so kvm_pgtable_stage2_map() runs and allocates a table, with no top-up
That one is narrow and VMM-driven, but topping up for every fault would cover it too, and it costs a single comparison when the cache already holds enough pages.
Quoted 9 lines
---
base-commit:012e4bc01e339a65dc7c59181b5efb33b171d12c
change-id: 20261126-fix-nv-cache-topup-9ab1fd829014Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Quoted 75 lines · Usama Arif
Quoted 51 lines · On Thu, 26 Nov 2026 17:28:29 +0000 "Lorenzo Stoakes (ARM)" <[email protected]>…
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 nested virtualisation adds another edge case - the mapping size of a shadow S2 is clamped to that of the guest's own S2. If the guest hypervisor replaces a block mapping with a table, a permission fault on the shadow block has to be mapped at
PAGE_SIZE, which requires a page table to collapse the block into.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, also top up the memcache for permission faults taken on a shadow S2.
Note that dirty logging had a similar issue, addressed in commit
56fd1fc526e6("KVM: arm64: Top up stage-2 memcache for dirty logging faults").Fixes fd276e71d1e7 ("KVM: arm64: nv: Handle shadow stage 2 page faults")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/arm64/kvm/mmu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)arch/arm64/kvm/mmu.c +2
-2@@ -2256,16 +2256,18 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,/* * 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 so normally don't require allocations from the memcache. One +* 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. arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, · line 2273
Quoted 10 lines
arch/arm64/kvm/mmu.c +3
-1+* With nested, a shadow stage-2 block may also need to be collapsed +* into a table if the guest's own stage-2 mapping has become smaller. */ memcache =get_mmu_memcache (s2fd->vcpu);if (!perm_fault ||memslot_is_logging (s2fd->memslot) || -is_protected_kvm_enabled ()) { +is_protected_kvm_enabled () || s2fd->nested) { ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret;Thanks for the fix!
On the narrow condition being the smaller backport: I don't think it is. The hunk already conflicts on 7.3, where
topup_mmu_memcache()still takes the vCPU, and the condition looks different on 7.2 and older, so every stable tree needs a small manual backport either way.A permission fault can also allocate outside NV. For example, take an exec permission fault on a THP block, where the VMM replaces that memory with 64K hugetlb pages before the fault looks up the VMA:
Quoted 9 lines
- the MMU notifier unmaps the block, and
mmu_seqis read afterwards
-kvm_s2_resolve_vma_size()forcesPAGE_SIZEfor theCONT_PTEhugetlb
-PAGE_SIZE!=perm_fault_granule, sokvm_pgtable_stage2_map()runs and allocates a table, with no top-upThat one is narrow and VMM-driven, but topping up for every fault would cover it too, and it costs a single comparison when the cache already holds enough pages.
Reads a little schloppy ;) But I think you're right yeah.
Good bloody lord @ 64 KiB hugetlb pages being a thing... obscene :)
Anyway, I'll send an unconditional v2, it's really not that much delta in general.
--
Cheers, Lorenzo