* [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
@ 2026-11-24 15:28 Usama Arif
2026-11-25 17:14 ` Leonardo Bras
2026-11-26 12:56 ` Marc Zyngier
0 siblings, 2 replies; 17+ messages in thread
From: Usama Arif @ 2026-11-24 15:28 UTC (permalink / raw)
To: maz, oupton, fuad.tabba, joey.gouly, seiden, suzuki.poulose,
yuzenghui, catalin.marinas, will, mark.rutland, kvmarm,
linux-arm-kernel, linux-kernel, riel, shakeel.butt, kernel-team,
rkeilty, atish.patra, dgreid
Cc: Usama Arif
When a stage-2 fault may need new page tables, KVM first tops up the
vCPU's page cache: if it holds fewer pages than the fault might need, it
is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
40. Unused pages stay in the cache until the vCPU is destroyed.
With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
used. Stage-2 has at most three levels and the root is preallocated, so
a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
table pages to map all of its memory, while its four vCPU caches get
filled with 160.
On a production host with 64 KiB pages running 101 VMs with four vCPUs
each, the caches were still 86% full. The memory used:
before after
per-vCPU capacity 2.5 MB 512 KB
all 404 vCPUs 869 MB <= 202 MB
Let's lower the capacity to 8 with 64 KiB pages, In the production
usecase above, it frees at least 667 MB. That still covers the two-page
minimum and leaves room for several table allocations per refill. 4 KB
and 16 KB pages keep 40.
The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
but it is topped up before every page it maps, so 8 is sufficient there
as well. The pKVM hypervisor memcache and the eager split cache don't
use this capacity.
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
arch/arm64/include/asm/kvm_types.h | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
index 9a126b9e2d7c99..28568da9ea4a7b 100644
--- a/arch/arm64/include/asm/kvm_types.h
+++ b/arch/arm64/include/asm/kvm_types.h
@@ -2,7 +2,12 @@
#ifndef _ASM_ARM64_KVM_TYPES_H
#define _ASM_ARM64_KVM_TYPES_H
+/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
+#ifdef CONFIG_ARM64_64K_PAGES
+#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
+#else
#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
+#endif
#endif /* _ASM_ARM64_KVM_TYPES_H */
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-24 15:28 [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts Usama Arif
@ 2026-11-25 17:14 ` Leonardo Bras
2026-11-25 17:55 ` Usama Arif
2026-11-26 12:56 ` Marc Zyngier
1 sibling, 1 reply; 17+ messages in thread
From: Leonardo Bras @ 2026-11-25 17:14 UTC (permalink / raw)
To: Usama Arif
Cc: Leonardo Bras, maz, oupton, fuad.tabba, joey.gouly, seiden,
suzuki.poulose, yuzenghui, catalin.marinas, will, mark.rutland,
kvmarm, linux-arm-kernel, linux-kernel, riel, shakeel.butt,
kernel-team, rkeilty, atish.patra, dgreid, ARM ljs@kernel.org
On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> When a stage-2 fault may need new page tables, KVM first tops up the
> vCPU's page cache: if it holds fewer pages than the fault might need, it
> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> 40. Unused pages stay in the cache until the vCPU is destroyed.
>
> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> used. Stage-2 has at most three levels and the root is preallocated, so
> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> table pages to map all of its memory, while its four vCPU caches get
> filled with 160.
>
> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> each, the caches were still 86% full. The memory used:
>
> before after
> per-vCPU capacity 2.5 MB 512 KB
> all 404 vCPUs 869 MB <= 202 MB
>
> Let's lower the capacity to 8 with 64 KiB pages, In the production
> usecase above, it frees at least 667 MB. That still covers the two-page
> minimum and leaves room for several table allocations per refill. 4 KB
> and 16 KB pages keep 40.
>
> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> but it is topped up before every page it maps, so 8 is sufficient there
> as well. The pKVM hypervisor memcache and the eager split cache don't
> use this capacity.
>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> arch/arm64/include/asm/kvm_types.h | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> --- a/arch/arm64/include/asm/kvm_types.h
> +++ b/arch/arm64/include/asm/kvm_types.h
> @@ -2,7 +2,12 @@
> #ifndef _ASM_ARM64_KVM_TYPES_H
> #define _ASM_ARM64_KVM_TYPES_H
>
> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> +#ifdef CONFIG_ARM64_64K_PAGES
> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> +#else
> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> +#endif
>
> #endif /* _ASM_ARM64_KVM_TYPES_H */
>
> --
> 2.53.1-Meta
>
Hi Usama,
Have you tried to reproduce this issue in v7.4-rc1 or newer?
IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
skip topup if the cache contains a minimum of ~4 pages, so it can possibly
improve that issue.
Although I can see that when it's below that minimum it fills the whole
capacity (40) of the cache.
In any case, Lorenzo, could you advise?
CC:Lorenzo Stoakes (ARM) (ljs@kernel.org)
Thanks,
Leo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-25 17:14 ` Leonardo Bras
@ 2026-11-25 17:55 ` Usama Arif
2026-11-25 18:25 ` Leonardo Bras
0 siblings, 1 reply; 17+ messages in thread
From: Usama Arif @ 2026-11-25 17:55 UTC (permalink / raw)
To: Leonardo Bras, Lorenzo Stoakes
Cc: maz, oupton, fuad.tabba, joey.gouly, seiden, suzuki.poulose,
yuzenghui, catalin.marinas, will, mark.rutland, kvmarm,
linux-arm-kernel, linux-kernel, riel, shakeel.butt, kernel-team,
rkeilty, atish.patra, dgreid
On 25/11/2026 09:14, Leonardo Bras wrote:
> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
>> When a stage-2 fault may need new page tables, KVM first tops up the
>> vCPU's page cache: if it holds fewer pages than the fault might need, it
>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
>> 40. Unused pages stay in the cache until the vCPU is destroyed.
>>
>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
>> used. Stage-2 has at most three levels and the root is preallocated, so
>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
>> table pages to map all of its memory, while its four vCPU caches get
>> filled with 160.
>>
>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
>> each, the caches were still 86% full. The memory used:
>>
>> before after
>> per-vCPU capacity 2.5 MB 512 KB
>> all 404 vCPUs 869 MB <= 202 MB
>>
>> Let's lower the capacity to 8 with 64 KiB pages, In the production
>> usecase above, it frees at least 667 MB. That still covers the two-page
>> minimum and leaves room for several table allocations per refill. 4 KB
>> and 16 KB pages keep 40.
>>
>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
>> but it is topped up before every page it maps, so 8 is sufficient there
>> as well. The pKVM hypervisor memcache and the eager split cache don't
>> use this capacity.
>>
>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
>> ---
>> arch/arm64/include/asm/kvm_types.h | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
>> --- a/arch/arm64/include/asm/kvm_types.h
>> +++ b/arch/arm64/include/asm/kvm_types.h
>> @@ -2,7 +2,12 @@
>> #ifndef _ASM_ARM64_KVM_TYPES_H
>> #define _ASM_ARM64_KVM_TYPES_H
>>
>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
>> +#ifdef CONFIG_ARM64_64K_PAGES
>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
>> +#else
>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
>> +#endif
>>
>> #endif /* _ASM_ARM64_KVM_TYPES_H */
>>
>> --
>> 2.53.1-Meta
>>
>
> Hi Usama,
>
Hello Leonardo,
> Have you tried to reproduce this issue in v7.4-rc1 or newer?
Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
Lorenzo's pre-fault series, and I A/B tested it there.
On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
in one page needs a single stage-2 table page, yet the vCPU caches hold
640 pages (40 MiB). With the patch they hold 127 pages.
>
> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> improve that issue.
I don't think that part is new. __kvm_mmu_topup_memory_cache() has
returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
guest abort that is the same MMU, so nothing changes here.
With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
>
> Although I can see that when it's below that minimum it fills the whole
> capacity (40) of the cache.
>
Right, and that is the problem. Skipping the top-up avoids refilling
the cache, but nothing ever drains it. A vCPU's first fault fills its
cache to 40. Each last-level table covers 512 MiB, so a guest needs very
few table pages, and most vCPUs never drop below min. The remaining
pages stay allocated until the vCPU is destroyed, which is why the
production caches were still 86% full.
The patch keeps the early return and only lowers how far a refill goes.
Thanks,
Usama
> In any case, Lorenzo, could you advise?
>
> CC:Lorenzo Stoakes (ARM) (ljs@kernel.org)
>
> Thanks,
> Leo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-25 17:55 ` Usama Arif
@ 2026-11-25 18:25 ` Leonardo Bras
2026-11-25 21:42 ` Usama Arif
0 siblings, 1 reply; 17+ messages in thread
From: Leonardo Bras @ 2026-11-25 18:25 UTC (permalink / raw)
To: Usama Arif
Cc: Leonardo Bras, Lorenzo Stoakes, maz, oupton, fuad.tabba,
joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, mark.rutland, kvmarm, linux-arm-kernel, linux-kernel, riel,
shakeel.butt, kernel-team, rkeilty, atish.patra, dgreid
On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
>
>
> On 25/11/2026 09:14, Leonardo Bras wrote:
> > On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> >> When a stage-2 fault may need new page tables, KVM first tops up the
> >> vCPU's page cache: if it holds fewer pages than the fault might need, it
> >> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> >> 40. Unused pages stay in the cache until the vCPU is destroyed.
> >>
> >> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> >> used. Stage-2 has at most three levels and the root is preallocated, so
> >> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> >> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> >> table pages to map all of its memory, while its four vCPU caches get
> >> filled with 160.
> >>
> >> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> >> each, the caches were still 86% full. The memory used:
> >>
> >> before after
> >> per-vCPU capacity 2.5 MB 512 KB
> >> all 404 vCPUs 869 MB <= 202 MB
> >>
> >> Let's lower the capacity to 8 with 64 KiB pages, In the production
> >> usecase above, it frees at least 667 MB. That still covers the two-page
> >> minimum and leaves room for several table allocations per refill. 4 KB
> >> and 16 KB pages keep 40.
> >>
> >> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> >> but it is topped up before every page it maps, so 8 is sufficient there
> >> as well. The pKVM hypervisor memcache and the eager split cache don't
> >> use this capacity.
> >>
> >> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> >> ---
> >> arch/arm64/include/asm/kvm_types.h | 5 +++++
> >> 1 file changed, 5 insertions(+)
> >>
> >> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> >> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> >> --- a/arch/arm64/include/asm/kvm_types.h
> >> +++ b/arch/arm64/include/asm/kvm_types.h
> >> @@ -2,7 +2,12 @@
> >> #ifndef _ASM_ARM64_KVM_TYPES_H
> >> #define _ASM_ARM64_KVM_TYPES_H
> >>
> >> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> >> +#ifdef CONFIG_ARM64_64K_PAGES
> >> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> >> +#else
> >> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> >> +#endif
> >>
> >> #endif /* _ASM_ARM64_KVM_TYPES_H */
> >>
> >> --
> >> 2.53.1-Meta
> >>
> >
> > Hi Usama,
> >
>
> Hello Leonardo,
>
> > Have you tried to reproduce this issue in v7.4-rc1 or newer?
>
> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
> Lorenzo's pre-fault series, and I A/B tested it there.
>
> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
> in one page needs a single stage-2 table page, yet the vCPU caches hold
> 640 pages (40 MiB). With the patch they hold 127 pages.
>
> >
> > IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> > skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> > improve that issue.
>
> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
> guest abort that is the same MMU, so nothing changes here.
>
> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
>
> >
> > Although I can see that when it's below that minimum it fills the whole
> > capacity (40) of the cache.
> >
>
> Right, and that is the problem. Skipping the top-up avoids refilling
> the cache, but nothing ever drains it. A vCPU's first fault fills its
> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> few table pages, and most vCPUs never drop below min. The remaining
> pages stay allocated until the vCPU is destroyed, which is why the
> production caches were still 86% full.
>
> The patch keeps the early return and only lowers how far a refill goes.
Right,
I took a look in the cache workings, and it does not seem to receive freed
pages back in. It gets filled by topup and used by map, so IIUC it is used
only to avoid failing an allocation during the mapping walk.
Maybe it could be the case to simplify this cache to have the capacity
about the same as the minimum (since it always topup before mapping) and
avoid the waste altogether.
Maymbe having something like this should be enough?
#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
Thanks!
Leo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-25 18:25 ` Leonardo Bras
@ 2026-11-25 21:42 ` Usama Arif
2026-11-26 8:06 ` Wei-Lin Chang
0 siblings, 1 reply; 17+ messages in thread
From: Usama Arif @ 2026-11-25 21:42 UTC (permalink / raw)
To: Leonardo Bras
Cc: Lorenzo Stoakes, maz, oupton, fuad.tabba, joey.gouly, seiden,
suzuki.poulose, yuzenghui, catalin.marinas, will, mark.rutland,
kvmarm, linux-arm-kernel, linux-kernel, riel, shakeel.butt,
kernel-team, rkeilty, atish.patra, dgreid
On 25/11/2026 10:25, Leonardo Bras wrote:
> On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
>>
>>
>> On 25/11/2026 09:14, Leonardo Bras wrote:
>>> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
>>>> When a stage-2 fault may need new page tables, KVM first tops up the
>>>> vCPU's page cache: if it holds fewer pages than the fault might need, it
>>>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
>>>> 40. Unused pages stay in the cache until the vCPU is destroyed.
>>>>
>>>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
>>>> used. Stage-2 has at most three levels and the root is preallocated, so
>>>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
>>>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
>>>> table pages to map all of its memory, while its four vCPU caches get
>>>> filled with 160.
>>>>
>>>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
>>>> each, the caches were still 86% full. The memory used:
>>>>
>>>> before after
>>>> per-vCPU capacity 2.5 MB 512 KB
>>>> all 404 vCPUs 869 MB <= 202 MB
>>>>
>>>> Let's lower the capacity to 8 with 64 KiB pages, In the production
>>>> usecase above, it frees at least 667 MB. That still covers the two-page
>>>> minimum and leaves room for several table allocations per refill. 4 KB
>>>> and 16 KB pages keep 40.
>>>>
>>>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
>>>> but it is topped up before every page it maps, so 8 is sufficient there
>>>> as well. The pKVM hypervisor memcache and the eager split cache don't
>>>> use this capacity.
>>>>
>>>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
>>>> ---
>>>> arch/arm64/include/asm/kvm_types.h | 5 +++++
>>>> 1 file changed, 5 insertions(+)
>>>>
>>>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
>>>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
>>>> --- a/arch/arm64/include/asm/kvm_types.h
>>>> +++ b/arch/arm64/include/asm/kvm_types.h
>>>> @@ -2,7 +2,12 @@
>>>> #ifndef _ASM_ARM64_KVM_TYPES_H
>>>> #define _ASM_ARM64_KVM_TYPES_H
>>>>
>>>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
>>>> +#ifdef CONFIG_ARM64_64K_PAGES
>>>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
>>>> +#else
>>>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
>>>> +#endif
>>>>
>>>> #endif /* _ASM_ARM64_KVM_TYPES_H */
>>>>
>>>> --
>>>> 2.53.1-Meta
>>>>
>>>
>>> Hi Usama,
>>>
>>
>> Hello Leonardo,
>>
>>> Have you tried to reproduce this issue in v7.4-rc1 or newer?
>>
>> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
>> Lorenzo's pre-fault series, and I A/B tested it there.
>>
>> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
>> in one page needs a single stage-2 table page, yet the vCPU caches hold
>> 640 pages (40 MiB). With the patch they hold 127 pages.
>>
>>>
>>> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
>>> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
>>> improve that issue.
>>
>> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
>> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
>> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
>> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
>> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
>> guest abort that is the same MMU, so nothing changes here.
>>
>> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
>>
>>>
>>> Although I can see that when it's below that minimum it fills the whole
>>> capacity (40) of the cache.
>>>
>>
>> Right, and that is the problem. Skipping the top-up avoids refilling
>> the cache, but nothing ever drains it. A vCPU's first fault fills its
>> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
>> few table pages, and most vCPUs never drop below min. The remaining
>> pages stay allocated until the vCPU is destroyed, which is why the
>> production caches were still 86% full.
>>
>> The patch keeps the early return and only lowers how far a refill goes.
>
> Right,
>
> I took a look in the cache workings, and it does not seem to receive freed
> pages back in. It gets filled by topup and used by map, so IIUC it is used
> only to avoid failing an allocation during the mapping walk.
>
> Maybe it could be the case to simplify this cache to have the capacity
> about the same as the minimum (since it always topup before mapping) and
> avoid the waste altogether.
>
> Maymbe having something like this should be enough?
>
> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
>
That sounds good to me. I tested and it works (and releases even more memory :))
I had to move #defines around to make it work, the current patch sits
like below. If this seems acceptable, I can send it as v2 for review.
From 9b044379f470625adb031a0ee7529e054d9ec219 Mon Sep 17 00:00:00 2001
From: Usama Arif <usama.arif@linux.dev>
Date: Wed, 25 Nov 2026 12:55:15 -0800
Subject: [PATCH] KVM: arm64: Shrink the stage-2 memcache to the page-table
depth
When a stage-2 fault may need new page tables, KVM first tops up the
vCPU's memcache: if it holds fewer pages than the fault might need, it
is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
40. Pages never return to the cache, and unused ones stay there until
the vCPU is destroyed.
The cache only has to cover a single map: every fault that can allocate
tables tops it up first, and a map allocates at most one table per
level below the preallocated root (kvm_mmu_cache_min_pages()). That is
at most three pages with 4 KiB and 16 KiB pages, and two with 64 KiB
pages.
With 64 KiB pages, the 40-page cache is 2.5 MiB per vCPU. As every
last-level table covers 512 MiB, an 8 GiB guest needs just 17 table
pages to map all of its memory, while its four vCPU caches get filled
with 160. On a production host with 64 KiB pages running 101 VMs with
four vCPUs each, the caches were still 86% full. The memory used:
before after
per-vCPU capacity 2.5 MiB 256 KiB
all 404 vCPUs 869 MiB <= 101 MiB
Let's size the cache to KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL,
which is 4 pages and covers the deepest walk with one page to spare. On
the host above, it frees at least 768 MiB. Refills get more frequent,
but no more than four pages per vCPU ever sit unused. With 4 KiB pages,
the cache shrinks from 160 KiB to 16 KiB per vCPU.
Move the two level definitions to asm/kvm_types.h, so that the generic
top-up code in kvm_main.c can see them. kvm_pgtable.h still gets them
through linux/kvm_types.h.
The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
but it is topped up before every page it maps. The pKVM hypervisor
memcache and the eager split cache don't use this capacity.
Suggested-by: Leonardo Bras <leo.bras@arm.com>
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
arch/arm64/include/asm/kvm_pgtable.h | 3 ---
arch/arm64/include/asm/kvm_types.h | 7 ++++++-
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h
index eda440354a55ec..691e2eb63939f5 100644
--- a/arch/arm64/include/asm/kvm_pgtable.h
+++ b/arch/arm64/include/asm/kvm_pgtable.h
@@ -19,9 +19,6 @@
struct kvm_s2_mmu;
-#define KVM_PGTABLE_FIRST_LEVEL -1
-#define KVM_PGTABLE_LAST_LEVEL 3
-
/*
* The largest supported block sizes for KVM (no 52-bit PA support):
* - 4K (level 1): 1GB
diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
index 9a126b9e2d7c99..5b61285d317e28 100644
--- a/arch/arm64/include/asm/kvm_types.h
+++ b/arch/arm64/include/asm/kvm_types.h
@@ -2,7 +2,12 @@
#ifndef _ASM_ARM64_KVM_TYPES_H
#define _ASM_ARM64_KVM_TYPES_H
-#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
+#define KVM_PGTABLE_FIRST_LEVEL -1
+#define KVM_PGTABLE_LAST_LEVEL 3
+
+/* One page per level below the root covers kvm_mmu_cache_min_pages(). */
+#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE \
+ (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
#endif /* _ASM_ARM64_KVM_TYPES_H */
--
2.53.1-Meta
> Thanks!
> Leo
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-25 21:42 ` Usama Arif
@ 2026-11-26 8:06 ` Wei-Lin Chang
2026-11-26 15:19 ` Leonardo Bras
2026-11-26 16:30 ` Lorenzo Stoakes (ARM)
0 siblings, 2 replies; 17+ messages in thread
From: Wei-Lin Chang @ 2026-11-26 8:06 UTC (permalink / raw)
To: Usama Arif, Leonardo Bras
Cc: Lorenzo Stoakes, maz, oupton, fuad.tabba, joey.gouly, seiden,
suzuki.poulose, yuzenghui, catalin.marinas, will, mark.rutland,
kvmarm, linux-arm-kernel, linux-kernel, riel, shakeel.butt,
kernel-team, rkeilty, atish.patra, dgreid
On Wed, Nov 25, 2026 at 09:42:33PM +0000, Usama Arif wrote:
>
>
> On 25/11/2026 10:25, Leonardo Bras wrote:
> > On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
> >>
> >>
> >> On 25/11/2026 09:14, Leonardo Bras wrote:
> >>> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> >>>> When a stage-2 fault may need new page tables, KVM first tops up the
> >>>> vCPU's page cache: if it holds fewer pages than the fault might need, it
> >>>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> >>>> 40. Unused pages stay in the cache until the vCPU is destroyed.
> >>>>
> >>>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> >>>> used. Stage-2 has at most three levels and the root is preallocated, so
> >>>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> >>>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> >>>> table pages to map all of its memory, while its four vCPU caches get
> >>>> filled with 160.
> >>>>
> >>>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> >>>> each, the caches were still 86% full. The memory used:
> >>>>
> >>>> before after
> >>>> per-vCPU capacity 2.5 MB 512 KB
> >>>> all 404 vCPUs 869 MB <= 202 MB
> >>>>
> >>>> Let's lower the capacity to 8 with 64 KiB pages, In the production
> >>>> usecase above, it frees at least 667 MB. That still covers the two-page
> >>>> minimum and leaves room for several table allocations per refill. 4 KB
> >>>> and 16 KB pages keep 40.
> >>>>
> >>>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> >>>> but it is topped up before every page it maps, so 8 is sufficient there
> >>>> as well. The pKVM hypervisor memcache and the eager split cache don't
> >>>> use this capacity.
> >>>>
> >>>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> >>>> ---
> >>>> arch/arm64/include/asm/kvm_types.h | 5 +++++
> >>>> 1 file changed, 5 insertions(+)
> >>>>
> >>>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> >>>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> >>>> --- a/arch/arm64/include/asm/kvm_types.h
> >>>> +++ b/arch/arm64/include/asm/kvm_types.h
> >>>> @@ -2,7 +2,12 @@
> >>>> #ifndef _ASM_ARM64_KVM_TYPES_H
> >>>> #define _ASM_ARM64_KVM_TYPES_H
> >>>>
> >>>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> >>>> +#ifdef CONFIG_ARM64_64K_PAGES
> >>>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> >>>> +#else
> >>>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> >>>> +#endif
> >>>>
> >>>> #endif /* _ASM_ARM64_KVM_TYPES_H */
> >>>>
> >>>> --
> >>>> 2.53.1-Meta
> >>>>
> >>>
> >>> Hi Usama,
> >>>
> >>
> >> Hello Leonardo,
> >>
> >>> Have you tried to reproduce this issue in v7.4-rc1 or newer?
> >>
> >> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
> >> Lorenzo's pre-fault series, and I A/B tested it there.
> >>
> >> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
> >> in one page needs a single stage-2 table page, yet the vCPU caches hold
> >> 640 pages (40 MiB). With the patch they hold 127 pages.
> >>
> >>>
> >>> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> >>> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> >>> improve that issue.
> >>
> >> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
> >> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
> >> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
> >> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
> >> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
> >> guest abort that is the same MMU, so nothing changes here.
> >>
> >> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
> >>
> >>>
> >>> Although I can see that when it's below that minimum it fills the whole
> >>> capacity (40) of the cache.
> >>>
> >>
> >> Right, and that is the problem. Skipping the top-up avoids refilling
> >> the cache, but nothing ever drains it. A vCPU's first fault fills its
> >> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> >> few table pages, and most vCPUs never drop below min. The remaining
> >> pages stay allocated until the vCPU is destroyed, which is why the
> >> production caches were still 86% full.
> >>
> >> The patch keeps the early return and only lowers how far a refill goes.
> >
> > Right,
> >
> > I took a look in the cache workings, and it does not seem to receive freed
> > pages back in. It gets filled by topup and used by map, so IIUC it is used
> > only to avoid failing an allocation during the mapping walk.
> >
> > Maybe it could be the case to simplify this cache to have the capacity
> > about the same as the minimum (since it always topup before mapping) and
> > avoid the waste altogether.
Well the more you reserve the less frequent you'd need to actually allocate at
topup, which saves time, so there's this trade-off to consider.
Thanks,
Wei-Lin Chang
> >
> > Maymbe having something like this should be enough?
> >
> > #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
> >
>
> That sounds good to me. I tested and it works (and releases even more memory :))
>
> I had to move #defines around to make it work, the current patch sits
> like below. If this seems acceptable, I can send it as v2 for review.
>
>
> >From 9b044379f470625adb031a0ee7529e054d9ec219 Mon Sep 17 00:00:00 2001
> From: Usama Arif <usama.arif@linux.dev>
> Date: Wed, 25 Nov 2026 12:55:15 -0800
> Subject: [PATCH] KVM: arm64: Shrink the stage-2 memcache to the page-table
> depth
>
> When a stage-2 fault may need new page tables, KVM first tops up the
> vCPU's memcache: if it holds fewer pages than the fault might need, it
> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> 40. Pages never return to the cache, and unused ones stay there until
> the vCPU is destroyed.
>
> The cache only has to cover a single map: every fault that can allocate
> tables tops it up first, and a map allocates at most one table per
> level below the preallocated root (kvm_mmu_cache_min_pages()). That is
> at most three pages with 4 KiB and 16 KiB pages, and two with 64 KiB
> pages.
>
> With 64 KiB pages, the 40-page cache is 2.5 MiB per vCPU. As every
> last-level table covers 512 MiB, an 8 GiB guest needs just 17 table
> pages to map all of its memory, while its four vCPU caches get filled
> with 160. On a production host with 64 KiB pages running 101 VMs with
> four vCPUs each, the caches were still 86% full. The memory used:
>
> before after
> per-vCPU capacity 2.5 MiB 256 KiB
> all 404 vCPUs 869 MiB <= 101 MiB
>
> Let's size the cache to KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL,
> which is 4 pages and covers the deepest walk with one page to spare. On
> the host above, it frees at least 768 MiB. Refills get more frequent,
> but no more than four pages per vCPU ever sit unused. With 4 KiB pages,
> the cache shrinks from 160 KiB to 16 KiB per vCPU.
>
> Move the two level definitions to asm/kvm_types.h, so that the generic
> top-up code in kvm_main.c can see them. kvm_pgtable.h still gets them
> through linux/kvm_types.h.
>
> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> but it is topped up before every page it maps. The pKVM hypervisor
> memcache and the eager split cache don't use this capacity.
>
> Suggested-by: Leonardo Bras <leo.bras@arm.com>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> arch/arm64/include/asm/kvm_pgtable.h | 3 ---
> arch/arm64/include/asm/kvm_types.h | 7 ++++++-
> 2 files changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h
> index eda440354a55ec..691e2eb63939f5 100644
> --- a/arch/arm64/include/asm/kvm_pgtable.h
> +++ b/arch/arm64/include/asm/kvm_pgtable.h
> @@ -19,9 +19,6 @@
>
> struct kvm_s2_mmu;
>
> -#define KVM_PGTABLE_FIRST_LEVEL -1
> -#define KVM_PGTABLE_LAST_LEVEL 3
> -
> /*
> * The largest supported block sizes for KVM (no 52-bit PA support):
> * - 4K (level 1): 1GB
> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> index 9a126b9e2d7c99..5b61285d317e28 100644
> --- a/arch/arm64/include/asm/kvm_types.h
> +++ b/arch/arm64/include/asm/kvm_types.h
> @@ -2,7 +2,12 @@
> #ifndef _ASM_ARM64_KVM_TYPES_H
> #define _ASM_ARM64_KVM_TYPES_H
>
> -#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> +#define KVM_PGTABLE_FIRST_LEVEL -1
> +#define KVM_PGTABLE_LAST_LEVEL 3
> +
> +/* One page per level below the root covers kvm_mmu_cache_min_pages(). */
> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE \
> + (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
>
> #endif /* _ASM_ARM64_KVM_TYPES_H */
>
> --
> 2.53.1-Meta
>
>
>
> > Thanks!
> > Leo
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-24 15:28 [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts Usama Arif
2026-11-25 17:14 ` Leonardo Bras
@ 2026-11-26 12:56 ` Marc Zyngier
2026-11-26 14:32 ` Usama Arif
1 sibling, 1 reply; 17+ messages in thread
From: Marc Zyngier @ 2026-11-26 12:56 UTC (permalink / raw)
To: Usama Arif
Cc: oupton, fuad.tabba, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will, mark.rutland, kvmarm, linux-arm-kernel,
linux-kernel, riel, shakeel.butt, kernel-team, rkeilty,
atish.patra, dgreid
On Tue, 24 Nov 2026 15:28:42 +0000,
Usama Arif <usama.arif@linux.dev> wrote:
>
> When a stage-2 fault may need new page tables, KVM first tops up the
> vCPU's page cache: if it holds fewer pages than the fault might need, it
> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> 40. Unused pages stay in the cache until the vCPU is destroyed.
>
> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> used. Stage-2 has at most three levels and the root is preallocated, so
> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> table pages to map all of its memory, while its four vCPU caches get
> filled with 160.
>
> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> each, the caches were still 86% full. The memory used:
>
> before after
> per-vCPU capacity 2.5 MB 512 KB
> all 404 vCPUs 869 MB <= 202 MB
>
> Let's lower the capacity to 8 with 64 KiB pages, In the production
> usecase above, it frees at least 667 MB. That still covers the two-page
> minimum and leaves room for several table allocations per refill. 4 KB
> and 16 KB pages keep 40.
I'll glance over the use of 64kB pages, because that's definitely a
niche configuration that hardly anyone uses. But the scale of the
numbers strike me a bit: 808GB worth of guest memory, and you're
complaining about 869MB? That's roughly 0.1% of the total, and the
savings you're gaining are even less... I know memory is expensive,
but you'd save a lot more than that by using a realistic page size.
Just saying...
I also question the assumption you are making that the guest is
strictly using IPA contiguous memory, and that therefore a very small
set of pages are all you need to map a guest. Guests with multiple,
disjoint memslots are (sadly) extremely common, specially in these
days of tiered memory and other random horrors.
>
> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> but it is topped up before every page it maps, so 8 is sufficient there
> as well. The pKVM hypervisor memcache and the eager split cache don't
> use this capacity.
>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> arch/arm64/include/asm/kvm_types.h | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> --- a/arch/arm64/include/asm/kvm_types.h
> +++ b/arch/arm64/include/asm/kvm_types.h
> @@ -2,7 +2,12 @@
> #ifndef _ASM_ARM64_KVM_TYPES_H
> #define _ASM_ARM64_KVM_TYPES_H
>
> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> +#ifdef CONFIG_ARM64_64K_PAGES
> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> +#else
> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> +#endif
Now, to the patch itself: I see no reason to special-case this. If
kvm_mmu_cache_min_pages() is an indication of what is needed, then
this macro should be driven by it, or the whole allocator take it into
account. And then you adjust it by whatever fudge factor seems
appropriate.
Thanks,
M.
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 12:56 ` Marc Zyngier
@ 2026-11-26 14:32 ` Usama Arif
2026-11-26 15:58 ` Marc Zyngier
0 siblings, 1 reply; 17+ messages in thread
From: Usama Arif @ 2026-11-26 14:32 UTC (permalink / raw)
To: Marc Zyngier
Cc: oupton, fuad.tabba, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will, mark.rutland, kvmarm, linux-arm-kernel,
linux-kernel, riel, shakeel.butt, kernel-team, rkeilty,
atish.patra, dgreid, Lorenzo Stoakes
On 26/11/2026 04:56, Marc Zyngier wrote:
> On Tue, 24 Nov 2026 15:28:42 +0000,
> Usama Arif <usama.arif@linux.dev> wrote:
>>
>> When a stage-2 fault may need new page tables, KVM first tops up the
>> vCPU's page cache: if it holds fewer pages than the fault might need, it
>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
>> 40. Unused pages stay in the cache until the vCPU is destroyed.
>>
>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
>> used. Stage-2 has at most three levels and the root is preallocated, so
>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
>> table pages to map all of its memory, while its four vCPU caches get
>> filled with 160.
>>
>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
>> each, the caches were still 86% full. The memory used:
>>
>> before after
>> per-vCPU capacity 2.5 MB 512 KB
>> all 404 vCPUs 869 MB <= 202 MB
>>
>> Let's lower the capacity to 8 with 64 KiB pages, In the production
>> usecase above, it frees at least 667 MB. That still covers the two-page
>> minimum and leaves room for several table allocations per refill. 4 KB
>> and 16 KB pages keep 40.
>
> I'll glance over the use of 64kB pages, because that's definitely a
> niche configuration that hardly anyone uses. But the scale of the
> numbers strike me a bit: 808GB worth of guest memory, and you're
> complaining about 869MB? That's roughly 0.1% of the total, and the
> savings you're gaining are even less... I know memory is expensive,
> but you'd save a lot more than that by using a realistic page size.
> Just saying...
>
Hello Marc,
Thanks for the review!
The 808 GB is what the guests were sized to, not what the host had.
Guest memory there is overcommitted (with ballooning): the host has
254 GB of RAM. v2 from [1] gives back at least 768 MB. The usecase
is very memory intensive, so saving more than 0.5G is a very good win.
The cost also scales with vCPUs, not with guest memory: up to 2.5 MB
per vCPU, whatever the guest size. On a test host of the same type
running 101 VMs with 4 vCPUs and 1 GB each, the caches held 986 MB,
while the stage-2 page tables they exist to feed took 25 MB. That is
39 times more memory held in reserve for page tables than used for
them, and 89% of all kernel memory charged to the VMs' cgroup
(1106 MB). With v2, the caches take 84 MB and the total is 204 MB.
As for the page size, 64K is the default NVIDIA recommends for Grace,
and both RHEL and Ubuntu ship 64K arm64 kernels. We use 64K base
page size for all ARM hosts in the meta fleet and see regression in
workload performance for 4K. I do know of other hyperscalers that
use it as well, so this is not a niche in server world IMO.
> I also question the assumption you are making that the guest is
> strictly using IPA contiguous memory, and that therefore a very small
> set of pages are all you need to map a guest. Guests with multiple,
> disjoint memslots are (sadly) extremely common, specially in these
> days of tiered memory and other random horrors.
>
The patch doesn't rely on that. The 8 GB guest needing 17 table pages
was only an example of the scale. The capacity bounds how many pages
are allocated ahead of time, not how many tables a guest can have:
every translation fault tops the cache up to kvm_mmu_cache_min_pages()
before taking mmu_lock, and a single map never needs more than that.
Disjoint memslots mean more refills, not more allocations. The top-up
allocates one page per iteration either way, so a 40-page refill is
40 calls to __get_free_page(), with nothing amortised. With 4, the
total is the same or lower, and the most a single fault allocates
drops from 40 pages to 4. That also answers Wei-Lin's point.
>>
>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
>> but it is topped up before every page it maps, so 8 is sufficient there
>> as well. The pKVM hypervisor memcache and the eager split cache don't
>> use this capacity.
>>
>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
>> ---
>> arch/arm64/include/asm/kvm_types.h | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
>> --- a/arch/arm64/include/asm/kvm_types.h
>> +++ b/arch/arm64/include/asm/kvm_types.h
>> @@ -2,7 +2,12 @@
>> #ifndef _ASM_ARM64_KVM_TYPES_H
>> #define _ASM_ARM64_KVM_TYPES_H
>>
>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
>> +#ifdef CONFIG_ARM64_64K_PAGES
>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
>> +#else
>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
>> +#endif
>
> Now, to the patch itself: I see no reason to special-case this. If
> kvm_mmu_cache_min_pages() is an indication of what is needed, then
> this macro should be driven by it, or the whole allocator take it into
> account. And then you adjust it by whatever fudge factor seems
> appropriate.
>
Completely agree! [1] does that: no page-size special case, and
a capacity of KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL for every
page size. It can't track kvm_mmu_cache_min_pages()
at runtime: the vCPU's cache is shared by the canonical and NV shadow
MMUs, and its capacity must stay fixed. So it has to cover the largest
minimum, which stage-2 caps at 3, and 4 is that plus one page of fudge.
For what it's worth, my understanding is that 40 currently used here was
never derived from stage-2, it was copied from x86's KVM_NR_MEM_OBJS by the
initial port.
Thanks!
Usama
> Thanks,
>
> M.
[1] https://lore.kernel.org/all/e7bdcfa9-bbd8-4649-8318-2f6dc236db15@linux.dev/
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 8:06 ` Wei-Lin Chang
@ 2026-11-26 15:19 ` Leonardo Bras
2026-11-26 15:34 ` Wei-Lin Chang
2026-11-26 16:30 ` Lorenzo Stoakes (ARM)
1 sibling, 1 reply; 17+ messages in thread
From: Leonardo Bras @ 2026-11-26 15:19 UTC (permalink / raw)
To: Wei-Lin Chang
Cc: Leonardo Bras, Usama Arif, Lorenzo Stoakes, maz, oupton,
fuad.tabba, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will, mark.rutland, kvmarm, linux-arm-kernel,
linux-kernel, riel, shakeel.butt, kernel-team, rkeilty,
atish.patra, dgreid
On Thu, Nov 26, 2026 at 08:06:41AM +0000, Wei-Lin Chang wrote:
> On Wed, Nov 25, 2026 at 09:42:33PM +0000, Usama Arif wrote:
> >
> >
> > On 25/11/2026 10:25, Leonardo Bras wrote:
> > > On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
> > >>
> > >>
> > >> On 25/11/2026 09:14, Leonardo Bras wrote:
> > >>> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> > >>>> When a stage-2 fault may need new page tables, KVM first tops up the
> > >>>> vCPU's page cache: if it holds fewer pages than the fault might need, it
> > >>>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> > >>>> 40. Unused pages stay in the cache until the vCPU is destroyed.
> > >>>>
> > >>>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> > >>>> used. Stage-2 has at most three levels and the root is preallocated, so
> > >>>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> > >>>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> > >>>> table pages to map all of its memory, while its four vCPU caches get
> > >>>> filled with 160.
> > >>>>
> > >>>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> > >>>> each, the caches were still 86% full. The memory used:
> > >>>>
> > >>>> before after
> > >>>> per-vCPU capacity 2.5 MB 512 KB
> > >>>> all 404 vCPUs 869 MB <= 202 MB
> > >>>>
> > >>>> Let's lower the capacity to 8 with 64 KiB pages, In the production
> > >>>> usecase above, it frees at least 667 MB. That still covers the two-page
> > >>>> minimum and leaves room for several table allocations per refill. 4 KB
> > >>>> and 16 KB pages keep 40.
> > >>>>
> > >>>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> > >>>> but it is topped up before every page it maps, so 8 is sufficient there
> > >>>> as well. The pKVM hypervisor memcache and the eager split cache don't
> > >>>> use this capacity.
> > >>>>
> > >>>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > >>>> ---
> > >>>> arch/arm64/include/asm/kvm_types.h | 5 +++++
> > >>>> 1 file changed, 5 insertions(+)
> > >>>>
> > >>>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> > >>>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> > >>>> --- a/arch/arm64/include/asm/kvm_types.h
> > >>>> +++ b/arch/arm64/include/asm/kvm_types.h
> > >>>> @@ -2,7 +2,12 @@
> > >>>> #ifndef _ASM_ARM64_KVM_TYPES_H
> > >>>> #define _ASM_ARM64_KVM_TYPES_H
> > >>>>
> > >>>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> > >>>> +#ifdef CONFIG_ARM64_64K_PAGES
> > >>>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> > >>>> +#else
> > >>>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> > >>>> +#endif
> > >>>>
> > >>>> #endif /* _ASM_ARM64_KVM_TYPES_H */
> > >>>>
> > >>>> --
> > >>>> 2.53.1-Meta
> > >>>>
> > >>>
> > >>> Hi Usama,
> > >>>
> > >>
> > >> Hello Leonardo,
> > >>
> > >>> Have you tried to reproduce this issue in v7.4-rc1 or newer?
> > >>
> > >> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
> > >> Lorenzo's pre-fault series, and I A/B tested it there.
> > >>
> > >> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
> > >> in one page needs a single stage-2 table page, yet the vCPU caches hold
> > >> 640 pages (40 MiB). With the patch they hold 127 pages.
> > >>
> > >>>
> > >>> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> > >>> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> > >>> improve that issue.
> > >>
> > >> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
> > >> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
> > >> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
> > >> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
> > >> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
> > >> guest abort that is the same MMU, so nothing changes here.
> > >>
> > >> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
> > >>
> > >>>
> > >>> Although I can see that when it's below that minimum it fills the whole
> > >>> capacity (40) of the cache.
> > >>>
> > >>
> > >> Right, and that is the problem. Skipping the top-up avoids refilling
> > >> the cache, but nothing ever drains it. A vCPU's first fault fills its
> > >> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> > >> few table pages, and most vCPUs never drop below min. The remaining
> > >> pages stay allocated until the vCPU is destroyed, which is why the
> > >> production caches were still 86% full.
> > >>
> > >> The patch keeps the early return and only lowers how far a refill goes.
> > >
> > > Right,
> > >
> > > I took a look in the cache workings, and it does not seem to receive freed
> > > pages back in. It gets filled by topup and used by map, so IIUC it is used
> > > only to avoid failing an allocation during the mapping walk.
> > >
> > > Maybe it could be the case to simplify this cache to have the capacity
> > > about the same as the minimum (since it always topup before mapping) and
> > > avoid the waste altogether.
>
> Well the more you reserve the less frequent you'd need to actually allocate at
> topup, which saves time, so there's this trade-off to consider.
>
> Thanks,
> Wei-Lin Chang
>
Hi Wei-Lin,
IIUC, every time we topup, we call a mmu_memory_cache_alloc_obj() for every
one of the objects in the array. So as it is today it runs it 40 times, and
waits until it runs down to allocate up to 40 again.
With the change, it will run 4, then up to 4 how many times are needed.
So having a smaller cache can potentially save both cputime and memory if
the cache is not that oftenly used. That being said, allocating 40 in
sequence would be faster than 4 * 10 because the instruction / data cache
is hot.
I think the only way to evaluate the performance impact is by running some
benchmarks.
> > >
> > > Maymbe having something like this should be enough?
> > >
> > > #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
> > >
> >
> > That sounds good to me. I tested and it works (and releases even more memory :))
Out of curiosity, have you tested it in all pagesize scenarios (4k, 16k,
64k) in both host and guest?
> >
> > I had to move #defines around to make it work, the current patch sits
> > like below. If this seems acceptable, I can send it as v2 for review.
> >
> >
> > >From 9b044379f470625adb031a0ee7529e054d9ec219 Mon Sep 17 00:00:00 2001
> > From: Usama Arif <usama.arif@linux.dev>
> > Date: Wed, 25 Nov 2026 12:55:15 -0800
> > Subject: [PATCH] KVM: arm64: Shrink the stage-2 memcache to the page-table
> > depth
> >
> > When a stage-2 fault may need new page tables, KVM first tops up the
> > vCPU's memcache: if it holds fewer pages than the fault might need, it
> > is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> > 40. Pages never return to the cache, and unused ones stay there until
> > the vCPU is destroyed.
> >
> > The cache only has to cover a single map: every fault that can allocate
> > tables tops it up first, and a map allocates at most one table per
> > level below the preallocated root (kvm_mmu_cache_min_pages()). That is
> > at most three pages with 4 KiB and 16 KiB pages, and two with 64 KiB
> > pages.
> >
> > With 64 KiB pages, the 40-page cache is 2.5 MiB per vCPU. As every
> > last-level table covers 512 MiB, an 8 GiB guest needs just 17 table
> > pages to map all of its memory, while its four vCPU caches get filled
> > with 160. On a production host with 64 KiB pages running 101 VMs with
> > four vCPUs each, the caches were still 86% full. The memory used:
> >
> > before after
> > per-vCPU capacity 2.5 MiB 256 KiB
> > all 404 vCPUs 869 MiB <= 101 MiB
> >
> > Let's size the cache to KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL,
> > which is 4 pages and covers the deepest walk with one page to spare. On
> > the host above, it frees at least 768 MiB. Refills get more frequent,
> > but no more than four pages per vCPU ever sit unused. With 4 KiB pages,
> > the cache shrinks from 160 KiB to 16 KiB per vCPU.
> >
> > Move the two level definitions to asm/kvm_types.h, so that the generic
> > top-up code in kvm_main.c can see them. kvm_pgtable.h still gets them
> > through linux/kvm_types.h.
It looks odd to have the PGTABLE level defines moved to kvm_types.h, but on
first glance I could not see a better place to put it.
Thanks!
Leo
> >
> > The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> > but it is topped up before every page it maps. The pKVM hypervisor
> > memcache and the eager split cache don't use this capacity.
> >
> > Suggested-by: Leonardo Bras <leo.bras@arm.com>
> > Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > ---
> > arch/arm64/include/asm/kvm_pgtable.h | 3 ---
> > arch/arm64/include/asm/kvm_types.h | 7 ++++++-
> > 2 files changed, 6 insertions(+), 4 deletions(-)
> >
> > diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h
> > index eda440354a55ec..691e2eb63939f5 100644
> > --- a/arch/arm64/include/asm/kvm_pgtable.h
> > +++ b/arch/arm64/include/asm/kvm_pgtable.h
> > @@ -19,9 +19,6 @@
> >
> > struct kvm_s2_mmu;
> >
> > -#define KVM_PGTABLE_FIRST_LEVEL -1
> > -#define KVM_PGTABLE_LAST_LEVEL 3
> > -
> > /*
> > * The largest supported block sizes for KVM (no 52-bit PA support):
> > * - 4K (level 1): 1GB
> > diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> > index 9a126b9e2d7c99..5b61285d317e28 100644
> > --- a/arch/arm64/include/asm/kvm_types.h
> > +++ b/arch/arm64/include/asm/kvm_types.h
> > @@ -2,7 +2,12 @@
> > #ifndef _ASM_ARM64_KVM_TYPES_H
> > #define _ASM_ARM64_KVM_TYPES_H
> >
> > -#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> > +#define KVM_PGTABLE_FIRST_LEVEL -1
> > +#define KVM_PGTABLE_LAST_LEVEL 3
> > +
> > +/* One page per level below the root covers kvm_mmu_cache_min_pages(). */
> > +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE \
> > + (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
> >
> > #endif /* _ASM_ARM64_KVM_TYPES_H */
> >
> > --
> > 2.53.1-Meta
> >
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 15:19 ` Leonardo Bras
@ 2026-11-26 15:34 ` Wei-Lin Chang
2026-11-26 15:55 ` Leonardo Bras
0 siblings, 1 reply; 17+ messages in thread
From: Wei-Lin Chang @ 2026-11-26 15:34 UTC (permalink / raw)
To: Leonardo Bras
Cc: Usama Arif, Lorenzo Stoakes, maz, oupton, fuad.tabba, joey.gouly,
seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
mark.rutland, kvmarm, linux-arm-kernel, linux-kernel, riel,
shakeel.butt, kernel-team, rkeilty, atish.patra, dgreid
On Thu, Nov 26, 2026 at 03:19:41PM +0000, Leonardo Bras wrote:
> On Thu, Nov 26, 2026 at 08:06:41AM +0000, Wei-Lin Chang wrote:
> > On Wed, Nov 25, 2026 at 09:42:33PM +0000, Usama Arif wrote:
> > >
> > >
> > > On 25/11/2026 10:25, Leonardo Bras wrote:
> > > > On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
> > > >>
> > > >>
> > > >> On 25/11/2026 09:14, Leonardo Bras wrote:
> > > >>> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> > > >>>> When a stage-2 fault may need new page tables, KVM first tops up the
> > > >>>> vCPU's page cache: if it holds fewer pages than the fault might need, it
> > > >>>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> > > >>>> 40. Unused pages stay in the cache until the vCPU is destroyed.
> > > >>>>
> > > >>>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> > > >>>> used. Stage-2 has at most three levels and the root is preallocated, so
> > > >>>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> > > >>>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> > > >>>> table pages to map all of its memory, while its four vCPU caches get
> > > >>>> filled with 160.
> > > >>>>
> > > >>>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> > > >>>> each, the caches were still 86% full. The memory used:
> > > >>>>
> > > >>>> before after
> > > >>>> per-vCPU capacity 2.5 MB 512 KB
> > > >>>> all 404 vCPUs 869 MB <= 202 MB
> > > >>>>
> > > >>>> Let's lower the capacity to 8 with 64 KiB pages, In the production
> > > >>>> usecase above, it frees at least 667 MB. That still covers the two-page
> > > >>>> minimum and leaves room for several table allocations per refill. 4 KB
> > > >>>> and 16 KB pages keep 40.
> > > >>>>
> > > >>>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> > > >>>> but it is topped up before every page it maps, so 8 is sufficient there
> > > >>>> as well. The pKVM hypervisor memcache and the eager split cache don't
> > > >>>> use this capacity.
> > > >>>>
> > > >>>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > > >>>> ---
> > > >>>> arch/arm64/include/asm/kvm_types.h | 5 +++++
> > > >>>> 1 file changed, 5 insertions(+)
> > > >>>>
> > > >>>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> > > >>>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> > > >>>> --- a/arch/arm64/include/asm/kvm_types.h
> > > >>>> +++ b/arch/arm64/include/asm/kvm_types.h
> > > >>>> @@ -2,7 +2,12 @@
> > > >>>> #ifndef _ASM_ARM64_KVM_TYPES_H
> > > >>>> #define _ASM_ARM64_KVM_TYPES_H
> > > >>>>
> > > >>>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> > > >>>> +#ifdef CONFIG_ARM64_64K_PAGES
> > > >>>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> > > >>>> +#else
> > > >>>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> > > >>>> +#endif
> > > >>>>
> > > >>>> #endif /* _ASM_ARM64_KVM_TYPES_H */
> > > >>>>
> > > >>>> --
> > > >>>> 2.53.1-Meta
> > > >>>>
> > > >>>
> > > >>> Hi Usama,
> > > >>>
> > > >>
> > > >> Hello Leonardo,
> > > >>
> > > >>> Have you tried to reproduce this issue in v7.4-rc1 or newer?
> > > >>
> > > >> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
> > > >> Lorenzo's pre-fault series, and I A/B tested it there.
> > > >>
> > > >> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
> > > >> in one page needs a single stage-2 table page, yet the vCPU caches hold
> > > >> 640 pages (40 MiB). With the patch they hold 127 pages.
> > > >>
> > > >>>
> > > >>> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> > > >>> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> > > >>> improve that issue.
> > > >>
> > > >> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
> > > >> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
> > > >> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
> > > >> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
> > > >> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
> > > >> guest abort that is the same MMU, so nothing changes here.
> > > >>
> > > >> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
> > > >>
> > > >>>
> > > >>> Although I can see that when it's below that minimum it fills the whole
> > > >>> capacity (40) of the cache.
> > > >>>
> > > >>
> > > >> Right, and that is the problem. Skipping the top-up avoids refilling
> > > >> the cache, but nothing ever drains it. A vCPU's first fault fills its
> > > >> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> > > >> few table pages, and most vCPUs never drop below min. The remaining
> > > >> pages stay allocated until the vCPU is destroyed, which is why the
> > > >> production caches were still 86% full.
> > > >>
> > > >> The patch keeps the early return and only lowers how far a refill goes.
> > > >
> > > > Right,
> > > >
> > > > I took a look in the cache workings, and it does not seem to receive freed
> > > > pages back in. It gets filled by topup and used by map, so IIUC it is used
> > > > only to avoid failing an allocation during the mapping walk.
> > > >
> > > > Maybe it could be the case to simplify this cache to have the capacity
> > > > about the same as the minimum (since it always topup before mapping) and
> > > > avoid the waste altogether.
> >
> > Well the more you reserve the less frequent you'd need to actually allocate at
> > topup, which saves time, so there's this trade-off to consider.
> >
> > Thanks,
> > Wei-Lin Chang
> >
>
> Hi Wei-Lin,
>
> IIUC, every time we topup, we call a mmu_memory_cache_alloc_obj() for every
> one of the objects in the array. So as it is today it runs it 40 times, and
Not necessarily, I meant with 40,
if (mc->nobjs >= min)
return 0;
happens way more often.
> waits until it runs down to allocate up to 40 again.
>
> With the change, it will run 4, then up to 4 how many times are needed.
>
> So having a smaller cache can potentially save both cputime and memory if
> the cache is not that oftenly used. That being said, allocating 40 in
> sequence would be faster than 4 * 10 because the instruction / data cache
> is hot.
I agree here.
Thanks,
Wei-Lin Chang
>
> I think the only way to evaluate the performance impact is by running some
> benchmarks.
>
[...]
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 15:34 ` Wei-Lin Chang
@ 2026-11-26 15:55 ` Leonardo Bras
2026-11-26 16:45 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 17+ messages in thread
From: Leonardo Bras @ 2026-11-26 15:55 UTC (permalink / raw)
To: Wei-Lin Chang
Cc: Leonardo Bras, Usama Arif, Lorenzo Stoakes, maz, oupton,
fuad.tabba, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will, mark.rutland, kvmarm, linux-arm-kernel,
linux-kernel, riel, shakeel.butt, kernel-team, rkeilty,
atish.patra, dgreid
On Thu, Nov 26, 2026 at 03:34:19PM +0000, Wei-Lin Chang wrote:
> On Thu, Nov 26, 2026 at 03:19:41PM +0000, Leonardo Bras wrote:
> > On Thu, Nov 26, 2026 at 08:06:41AM +0000, Wei-Lin Chang wrote:
> > > On Wed, Nov 25, 2026 at 09:42:33PM +0000, Usama Arif wrote:
> > > >
> > > >
> > > > On 25/11/2026 10:25, Leonardo Bras wrote:
> > > > > On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
> > > > >>
> > > > >>
> > > > >> On 25/11/2026 09:14, Leonardo Bras wrote:
> > > > >>> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> > > > >>>> When a stage-2 fault may need new page tables, KVM first tops up the
> > > > >>>> vCPU's page cache: if it holds fewer pages than the fault might need, it
> > > > >>>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> > > > >>>> 40. Unused pages stay in the cache until the vCPU is destroyed.
> > > > >>>>
> > > > >>>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> > > > >>>> used. Stage-2 has at most three levels and the root is preallocated, so
> > > > >>>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> > > > >>>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> > > > >>>> table pages to map all of its memory, while its four vCPU caches get
> > > > >>>> filled with 160.
> > > > >>>>
> > > > >>>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> > > > >>>> each, the caches were still 86% full. The memory used:
> > > > >>>>
> > > > >>>> before after
> > > > >>>> per-vCPU capacity 2.5 MB 512 KB
> > > > >>>> all 404 vCPUs 869 MB <= 202 MB
> > > > >>>>
> > > > >>>> Let's lower the capacity to 8 with 64 KiB pages, In the production
> > > > >>>> usecase above, it frees at least 667 MB. That still covers the two-page
> > > > >>>> minimum and leaves room for several table allocations per refill. 4 KB
> > > > >>>> and 16 KB pages keep 40.
> > > > >>>>
> > > > >>>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> > > > >>>> but it is topped up before every page it maps, so 8 is sufficient there
> > > > >>>> as well. The pKVM hypervisor memcache and the eager split cache don't
> > > > >>>> use this capacity.
> > > > >>>>
> > > > >>>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > > > >>>> ---
> > > > >>>> arch/arm64/include/asm/kvm_types.h | 5 +++++
> > > > >>>> 1 file changed, 5 insertions(+)
> > > > >>>>
> > > > >>>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> > > > >>>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> > > > >>>> --- a/arch/arm64/include/asm/kvm_types.h
> > > > >>>> +++ b/arch/arm64/include/asm/kvm_types.h
> > > > >>>> @@ -2,7 +2,12 @@
> > > > >>>> #ifndef _ASM_ARM64_KVM_TYPES_H
> > > > >>>> #define _ASM_ARM64_KVM_TYPES_H
> > > > >>>>
> > > > >>>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> > > > >>>> +#ifdef CONFIG_ARM64_64K_PAGES
> > > > >>>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> > > > >>>> +#else
> > > > >>>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> > > > >>>> +#endif
> > > > >>>>
> > > > >>>> #endif /* _ASM_ARM64_KVM_TYPES_H */
> > > > >>>>
> > > > >>>> --
> > > > >>>> 2.53.1-Meta
> > > > >>>>
> > > > >>>
> > > > >>> Hi Usama,
> > > > >>>
> > > > >>
> > > > >> Hello Leonardo,
> > > > >>
> > > > >>> Have you tried to reproduce this issue in v7.4-rc1 or newer?
> > > > >>
> > > > >> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
> > > > >> Lorenzo's pre-fault series, and I A/B tested it there.
> > > > >>
> > > > >> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
> > > > >> in one page needs a single stage-2 table page, yet the vCPU caches hold
> > > > >> 640 pages (40 MiB). With the patch they hold 127 pages.
> > > > >>
> > > > >>>
> > > > >>> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> > > > >>> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> > > > >>> improve that issue.
> > > > >>
> > > > >> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
> > > > >> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
> > > > >> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
> > > > >> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
> > > > >> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
> > > > >> guest abort that is the same MMU, so nothing changes here.
> > > > >>
> > > > >> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
> > > > >>
> > > > >>>
> > > > >>> Although I can see that when it's below that minimum it fills the whole
> > > > >>> capacity (40) of the cache.
> > > > >>>
> > > > >>
> > > > >> Right, and that is the problem. Skipping the top-up avoids refilling
> > > > >> the cache, but nothing ever drains it. A vCPU's first fault fills its
> > > > >> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> > > > >> few table pages, and most vCPUs never drop below min. The remaining
> > > > >> pages stay allocated until the vCPU is destroyed, which is why the
> > > > >> production caches were still 86% full.
> > > > >>
> > > > >> The patch keeps the early return and only lowers how far a refill goes.
> > > > >
> > > > > Right,
> > > > >
> > > > > I took a look in the cache workings, and it does not seem to receive freed
> > > > > pages back in. It gets filled by topup and used by map, so IIUC it is used
> > > > > only to avoid failing an allocation during the mapping walk.
> > > > >
> > > > > Maybe it could be the case to simplify this cache to have the capacity
> > > > > about the same as the minimum (since it always topup before mapping) and
> > > > > avoid the waste altogether.
> > >
> > > Well the more you reserve the less frequent you'd need to actually allocate at
> > > topup, which saves time, so there's this trade-off to consider.
> > >
> > > Thanks,
> > > Wei-Lin Chang
> > >
> >
> > Hi Wei-Lin,
> >
> > IIUC, every time we topup, we call a mmu_memory_cache_alloc_obj() for every
> > one of the objects in the array. So as it is today it runs it 40 times, and
>
> Not necessarily, I meant with 40,
>
> if (mc->nobjs >= min)
> return 0;
>
> happens way more often.
That's what I meant with 'waits until it runs down'. Basically we have
if (mc->nobjs >= min)
return 0;
while (mc->nobjs < mc->capacity) {
obj = mmu_memory_cache_alloc_obj(mc, gfp);
...
So, first use it allocates mc->capacity entries, and every further topup
will return early unless it goes below min, when it populates again. The
only reason for a larger cache being more performant would be to use
that cache locality, which could indeed have a large impact, if we use this
cache a lot.
Thanks!
Leo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 14:32 ` Usama Arif
@ 2026-11-26 15:58 ` Marc Zyngier
2026-11-26 16:52 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 17+ messages in thread
From: Marc Zyngier @ 2026-11-26 15:58 UTC (permalink / raw)
To: Usama Arif
Cc: oupton, fuad.tabba, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will, mark.rutland, kvmarm, linux-arm-kernel,
linux-kernel, riel, shakeel.butt, kernel-team, rkeilty,
atish.patra, dgreid, Lorenzo Stoakes
On Thu, 26 Nov 2026 14:32:04 +0000,
Usama Arif <usama.arif@linux.dev> wrote:
[nonsense deleted]
> > Now, to the patch itself: I see no reason to special-case this. If
> > kvm_mmu_cache_min_pages() is an indication of what is needed, then
> > this macro should be driven by it, or the whole allocator take it into
> > account. And then you adjust it by whatever fudge factor seems
> > appropriate.
> >
>
> Completely agree! [1] does that: no page-size special case, and
> a capacity of KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL for every
> page size. It can't track kvm_mmu_cache_min_pages()
> at runtime: the vCPU's cache is shared by the canonical and NV shadow
> MMUs, and its capacity must stay fixed. So it has to cover the largest
> minimum, which stage-2 caps at 3, and 4 is that plus one page of fudge.
Of course it can. That's not exactly rock science to add, rather than
relying on another set of bad heuristics. And this must cover more
than a single mapping per top-up, because that has a ripple effect on
the speed at which we can split large blocks (funnily enough, people
care about that stuff).
M.
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 8:06 ` Wei-Lin Chang
2026-11-26 15:19 ` Leonardo Bras
@ 2026-11-26 16:30 ` Lorenzo Stoakes (ARM)
2026-11-26 16:53 ` Lorenzo Stoakes (ARM)
1 sibling, 1 reply; 17+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-26 16:30 UTC (permalink / raw)
To: Wei-Lin Chang
Cc: Usama Arif, Leonardo Bras, maz, oupton, fuad.tabba, joey.gouly,
seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
mark.rutland, kvmarm, linux-arm-kernel, linux-kernel, riel,
shakeel.butt, kernel-team, rkeilty, atish.patra, dgreid
On Thu, Nov 26, 2026 at 08:06:41AM +0000, Wei-Lin Chang wrote:
> On Wed, Nov 25, 2026 at 09:42:33PM +0000, Usama Arif wrote:
> > On 25/11/2026 10:25, Leonardo Bras wrote:
> > > On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
> > >> Right, and that is the problem. Skipping the top-up avoids refilling
> > >> the cache, but nothing ever drains it. A vCPU's first fault fills its
> > >> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> > >> few table pages, and most vCPUs never drop below min. The remaining
> > >> pages stay allocated until the vCPU is destroyed, which is why the
> > >> production caches were still 86% full.
> > >>
> > >> The patch keeps the early return and only lowers how far a refill goes.
> > >
> > > Right,
> > >
> > > I took a look in the cache workings, and it does not seem to receive freed
> > > pages back in. It gets filled by topup and used by map, so IIUC it is used
> > > only to avoid failing an allocation during the mapping walk.
> > >
> > > Maybe it could be the case to simplify this cache to have the capacity
> > > about the same as the minimum (since it always topup before mapping) and
> > > avoid the waste altogether.
Yeah, I really feel this 40 number (derived from x86 as double some other magic
number :) is bogus here and a historic artifact.
I think this is all a great chance to simplify here :) we can address Usama's
concerns at the same time as making all this actually make sense.
>
> Well the more you reserve the less frequent you'd need to actually allocate at
> topup, which saves time, so there's this trade-off to consider.
Hmm, but the topup is allocating anyway either way no matter the reserve right?
kvm_mmu_memory_cache_alloc() grabs from mc->objects and decrements mc->nobjs for
every allocation (N objects in total), and __kvm_mmu_topup_memory_cache() tops
up N objects back up to the reserve, wherever that should be.
So the only question really is what can the maximum N be? And right now this is
guesstimated at by a magic number.
Also, if it turns out it does:
[ topup X pages ] [ use X pages ] [ topup X pages ]
And you have X + Y pages in the cache, that actually means you have uselessly
allocated Y pages sat around doing nothing that are leaked until the vCPU is
destroyed AFAICT.
I think though as per Leo, we should avoid using a magic number.
And as per Marc, we should calculate what this number should be sensibly.
So something like what Leo suggested is the right way to go!
> > >
> > > Maymbe having something like this should be enough?
> > >
> > > #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
> > >
I think this is definitely the right approach!
> >
> > That sounds good to me. I tested and it works (and releases even more memory :))
> >
> > I had to move #defines around to make it work, the current patch sits
> > like below. If this seems acceptable, I can send it as v2 for review.
> >
> >
> > >From 9b044379f470625adb031a0ee7529e054d9ec219 Mon Sep 17 00:00:00 2001
> > From: Usama Arif <usama.arif@linux.dev>
> > Date: Wed, 25 Nov 2026 12:55:15 -0800
> > Subject: [PATCH] KVM: arm64: Shrink the stage-2 memcache to the page-table
> > depth
> >
> > When a stage-2 fault may need new page tables, KVM first tops up the
> > vCPU's memcache: if it holds fewer pages than the fault might need, it
> > is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> > 40. Pages never return to the cache, and unused ones stay there until
> > the vCPU is destroyed.
> >
> > The cache only has to cover a single map: every fault that can allocate
> > tables tops it up first, and a map allocates at most one table per
> > level below the preallocated root (kvm_mmu_cache_min_pages()). That is
> > at most three pages with 4 KiB and 16 KiB pages, and two with 64 KiB
> > pages.
Yes this seems correct.
> >
> > With 64 KiB pages, the 40-page cache is 2.5 MiB per vCPU. As every
> > last-level table covers 512 MiB, an 8 GiB guest needs just 17 table
> > pages to map all of its memory, while its four vCPU caches get filled
> > with 160. On a production host with 64 KiB pages running 101 VMs with
> > four vCPUs each, the caches were still 86% full. The memory used:
> >
> > before after
> > per-vCPU capacity 2.5 MiB 256 KiB
> > all 404 vCPUs 869 MiB <= 101 MiB
> >
> > Let's size the cache to KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL,
> > which is 4 pages and covers the deepest walk with one page to spare. On
Hmm why would you need 1 page to spare?
> > the host above, it frees at least 768 MiB. Refills get more frequent,
> > but no more than four pages per vCPU ever sit unused. With 4 KiB pages,
> > the cache shrinks from 160 KiB to 16 KiB per vCPU.
> >
> > Move the two level definitions to asm/kvm_types.h, so that the generic
> > top-up code in kvm_main.c can see them. kvm_pgtable.h still gets them
> > through linux/kvm_types.h.
> >
> > The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> > but it is topped up before every page it maps. The pKVM hypervisor
> > memcache and the eager split cache don't use this capacity.
> >
> > Suggested-by: Leonardo Bras <leo.bras@arm.com>
> > Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > ---
> > arch/arm64/include/asm/kvm_pgtable.h | 3 ---
> > arch/arm64/include/asm/kvm_types.h | 7 ++++++-
> > 2 files changed, 6 insertions(+), 4 deletions(-)
> >
> > diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h
> > index eda440354a55ec..691e2eb63939f5 100644
> > --- a/arch/arm64/include/asm/kvm_pgtable.h
> > +++ b/arch/arm64/include/asm/kvm_pgtable.h
> > @@ -19,9 +19,6 @@
> >
> > struct kvm_s2_mmu;
> >
> > -#define KVM_PGTABLE_FIRST_LEVEL -1
> > -#define KVM_PGTABLE_LAST_LEVEL 3
> > -
> > /*
> > * The largest supported block sizes for KVM (no 52-bit PA support):
> > * - 4K (level 1): 1GB
> > diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> > index 9a126b9e2d7c99..5b61285d317e28 100644
> > --- a/arch/arm64/include/asm/kvm_types.h
> > +++ b/arch/arm64/include/asm/kvm_types.h
> > @@ -2,7 +2,12 @@
> > #ifndef _ASM_ARM64_KVM_TYPES_H
> > #define _ASM_ARM64_KVM_TYPES_H
> >
> > -#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> > +#define KVM_PGTABLE_FIRST_LEVEL -1
> > +#define KVM_PGTABLE_LAST_LEVEL 3
> > +
The -1 isn't needed here at all right? So why not just import
KVM_PGTABLE_LAST_LEVEL and do:
#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE KVM_PGTABLE_LAST_LEVEL
?
> > +/* One page per level below the root covers kvm_mmu_cache_min_pages(). */
> > +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE \
> > + (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
> >
> > #endif /* _ASM_ARM64_KVM_TYPES_H */
> >
> > --
> > 2.53.1-Meta
> >
> >
> >
> > > Thanks!
> > > Leo
> >
>
I also think actually there's an existing problem with the top-up right now in
user_mem_abort() that needs addressing too, actually, that this change exposes
and will cause bugs if done as above.
So right now top-up is conditional there:
if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
is_protected_kvm_enabled()) {
ret = topup_mmu_memcache(s2fd->mmu, memcache);
if (ret)
return ret;
}
Inverted is 'permission fault && !dirty-logging && !pKVM'. So the question is
whether a permission fault not needing a new page table is correct.
So working through, the actual decision about a page table allocation is done
in:
user_mem_abort()
-> kvm_s2_fault_map()
-> kvm_pgtable_stage2_map()
-> ... -> stage2_map_walk_leaf()
-> stage2_map_walker_try_leaf()
-> stage2_leaf_mapping_allowed()
-> kvm_block_mapping_supported() [ if false, allocate a page table ]
static bool kvm_block_mapping_supported(const struct kvm_pgtable_visit_ctx *ctx, u64 phys)
{
u64 granule = kvm_granule_size(ctx->level);
...
if (granule > (ctx->end - ctx->addr))
return false;
...
}
But page table size can vary in NV between host and guest (guest must be >=
host) and it gets clamped to the smaller of the two in kvm_s2_resolve_vma_size():
static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd,
struct kvm_s2_fault_vma_info *s2vi,
struct vm_area_struct *vma)
{
...
if (s2fd->nested) {
...
/*
* If we're about to create a shadow stage 2 entry, then we
* can only create a block mapping if the guest stage 2 page
* table uses at least as big a mapping.
*/
max_map_size = min(kvm_s2_trans_size(s2fd->nested), max_map_size);
...
}
}
This sets s2vi->vma_pagesize based on this value:
static int kvm_s2_fault_get_vma_info(const struct kvm_s2_fault_desc *s2fd,
struct kvm_s2_fault_vma_info *s2vi)
{
...
s2vi->vma_pagesize = BIT(kvm_s2_resolve_vma_size(s2fd, s2vi, vma));
...
}
And the fault map uses this in the mapping size, which defines ctx->addr, end
above in kvm_block_mapping_supported():
static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
const struct kvm_s2_fault_vma_info *s2vi,
enum kvm_pgtable_prot prot,
void *memcache,
struct kvm_s2_fault_result *result)
{
...
mapping_size = s2vi->vma_pagesize;
...
ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, gfn_to_gpa(gfn), mapping_size,
__pfn_to_phys(pfn), prot,
memcache, flags);
...
}
So basically, if the host has smaller page table size than the guest, you need
an extra page table level.
So simple solution is to add nested to the check:
if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
is_protected_kvm_enabled() || s2fd->nested) {
ret = topup_mmu_memcache(s2fd->mmu, memcache);
if (ret)
return ret;
}
But honestly I wonder at this point if it's truly worth doing all these checks?
Maybe safer to just topup unconditionally.
It'll only top up once before the cache is full anyway, so it's low cost. And
would make more consistent with other fault paths too.
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 15:55 ` Leonardo Bras
@ 2026-11-26 16:45 ` Lorenzo Stoakes (ARM)
2026-11-26 17:07 ` Leonardo Bras
0 siblings, 1 reply; 17+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-26 16:45 UTC (permalink / raw)
To: Leonardo Bras
Cc: Wei-Lin Chang, Usama Arif, maz, oupton, fuad.tabba, joey.gouly,
seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
mark.rutland, kvmarm, linux-arm-kernel, linux-kernel, riel,
shakeel.butt, kernel-team, rkeilty, atish.patra, dgreid
On Thu, Nov 26, 2026 at 03:55:13PM +0000, Leonardo Bras wrote:
> On Thu, Nov 26, 2026 at 03:34:19PM +0000, Wei-Lin Chang wrote:
> > On Thu, Nov 26, 2026 at 03:19:41PM +0000, Leonardo Bras wrote:
> > > On Thu, Nov 26, 2026 at 08:06:41AM +0000, Wei-Lin Chang wrote:
> > > > On Wed, Nov 25, 2026 at 09:42:33PM +0000, Usama Arif wrote:
> > > > >
> > > > >
> > > > > On 25/11/2026 10:25, Leonardo Bras wrote:
> > > > > > On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
> > > > > >>
> > > > > >>
> > > > > >> On 25/11/2026 09:14, Leonardo Bras wrote:
> > > > > >>> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> > > > > >>>> When a stage-2 fault may need new page tables, KVM first tops up the
> > > > > >>>> vCPU's page cache: if it holds fewer pages than the fault might need, it
> > > > > >>>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> > > > > >>>> 40. Unused pages stay in the cache until the vCPU is destroyed.
> > > > > >>>>
> > > > > >>>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> > > > > >>>> used. Stage-2 has at most three levels and the root is preallocated, so
> > > > > >>>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> > > > > >>>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> > > > > >>>> table pages to map all of its memory, while its four vCPU caches get
> > > > > >>>> filled with 160.
> > > > > >>>>
> > > > > >>>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> > > > > >>>> each, the caches were still 86% full. The memory used:
> > > > > >>>>
> > > > > >>>> before after
> > > > > >>>> per-vCPU capacity 2.5 MB 512 KB
> > > > > >>>> all 404 vCPUs 869 MB <= 202 MB
> > > > > >>>>
> > > > > >>>> Let's lower the capacity to 8 with 64 KiB pages, In the production
> > > > > >>>> usecase above, it frees at least 667 MB. That still covers the two-page
> > > > > >>>> minimum and leaves room for several table allocations per refill. 4 KB
> > > > > >>>> and 16 KB pages keep 40.
> > > > > >>>>
> > > > > >>>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> > > > > >>>> but it is topped up before every page it maps, so 8 is sufficient there
> > > > > >>>> as well. The pKVM hypervisor memcache and the eager split cache don't
> > > > > >>>> use this capacity.
> > > > > >>>>
> > > > > >>>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > > > > >>>> ---
> > > > > >>>> arch/arm64/include/asm/kvm_types.h | 5 +++++
> > > > > >>>> 1 file changed, 5 insertions(+)
> > > > > >>>>
> > > > > >>>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> > > > > >>>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> > > > > >>>> --- a/arch/arm64/include/asm/kvm_types.h
> > > > > >>>> +++ b/arch/arm64/include/asm/kvm_types.h
> > > > > >>>> @@ -2,7 +2,12 @@
> > > > > >>>> #ifndef _ASM_ARM64_KVM_TYPES_H
> > > > > >>>> #define _ASM_ARM64_KVM_TYPES_H
> > > > > >>>>
> > > > > >>>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> > > > > >>>> +#ifdef CONFIG_ARM64_64K_PAGES
> > > > > >>>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> > > > > >>>> +#else
> > > > > >>>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> > > > > >>>> +#endif
> > > > > >>>>
> > > > > >>>> #endif /* _ASM_ARM64_KVM_TYPES_H */
> > > > > >>>>
> > > > > >>>> --
> > > > > >>>> 2.53.1-Meta
> > > > > >>>>
> > > > > >>>
> > > > > >>> Hi Usama,
> > > > > >>>
> > > > > >>
> > > > > >> Hello Leonardo,
> > > > > >>
> > > > > >>> Have you tried to reproduce this issue in v7.4-rc1 or newer?
> > > > > >>
> > > > > >> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
> > > > > >> Lorenzo's pre-fault series, and I A/B tested it there.
> > > > > >>
> > > > > >> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
> > > > > >> in one page needs a single stage-2 table page, yet the vCPU caches hold
> > > > > >> 640 pages (40 MiB). With the patch they hold 127 pages.
> > > > > >>
> > > > > >>>
> > > > > >>> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> > > > > >>> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> > > > > >>> improve that issue.
> > > > > >>
> > > > > >> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
> > > > > >> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
> > > > > >> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
> > > > > >> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
> > > > > >> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
> > > > > >> guest abort that is the same MMU, so nothing changes here.
> > > > > >>
> > > > > >> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
> > > > > >>
> > > > > >>>
> > > > > >>> Although I can see that when it's below that minimum it fills the whole
> > > > > >>> capacity (40) of the cache.
> > > > > >>>
> > > > > >>
> > > > > >> Right, and that is the problem. Skipping the top-up avoids refilling
> > > > > >> the cache, but nothing ever drains it. A vCPU's first fault fills its
> > > > > >> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> > > > > >> few table pages, and most vCPUs never drop below min. The remaining
> > > > > >> pages stay allocated until the vCPU is destroyed, which is why the
> > > > > >> production caches were still 86% full.
> > > > > >>
> > > > > >> The patch keeps the early return and only lowers how far a refill goes.
> > > > > >
> > > > > > Right,
> > > > > >
> > > > > > I took a look in the cache workings, and it does not seem to receive freed
> > > > > > pages back in. It gets filled by topup and used by map, so IIUC it is used
> > > > > > only to avoid failing an allocation during the mapping walk.
> > > > > >
> > > > > > Maybe it could be the case to simplify this cache to have the capacity
> > > > > > about the same as the minimum (since it always topup before mapping) and
> > > > > > avoid the waste altogether.
> > > >
> > > > Well the more you reserve the less frequent you'd need to actually allocate at
> > > > topup, which saves time, so there's this trade-off to consider.
> > > >
> > > > Thanks,
> > > > Wei-Lin Chang
> > > >
> > >
> > > Hi Wei-Lin,
> > >
> > > IIUC, every time we topup, we call a mmu_memory_cache_alloc_obj() for every
> > > one of the objects in the array. So as it is today it runs it 40 times, and
> >
> > Not necessarily, I meant with 40,
> >
> > if (mc->nobjs >= min)
> > return 0;
> >
> > happens way more often.
>
> That's what I meant with 'waits until it runs down'. Basically we have
>
> if (mc->nobjs >= min)
> return 0;
>
> while (mc->nobjs < mc->capacity) {
> obj = mmu_memory_cache_alloc_obj(mc, gfp);
> ...
>
> So, first use it allocates mc->capacity entries, and every further topup
> will return early unless it goes below min, when it populates again. The
> only reason for a larger cache being more performant would be to use
> that cache locality, which could indeed have a large impact, if we use this
> cache a lot.
I think there's confusion because of this being called a cache, this is more
pre-allocation than anything it seems. As per the comment above struct
kvm_mmu_memory_cache in include/linux/kvm_types.h:
* Memory caches are used to preallocate memory ahead of various MMU flows,
* e.g. page fault handlers. Gracefully handling allocation failures deep in
* MMU flows is problematic, as is triggering reclaim, I/O, etc... while
* holding MMU locks. Note, these caches act more like prefetch buffers than
* classical caches, i.e. objects are not returned to the cache on being freed.
The question is would going from 40 to 3 slow things down?
At 40 you do 40 __get_free_page() calls on topup for the _first_ fault that hits
it, which adds latency to a random s2 fault (not great).
(If the fault doesn't in fact use a page table, this is only done once until a
page table is next needed).
And then as 37 page tables are mapped, those ones don't get allocations at 38
you go again...
With 3 in the cache you allocate up to 3 for each fault.
So in both cases you allocate the same amount of pages but in the 40 case it's
all bunched up on one s2 fault with a jump in latency, in the 3 case it's spread
out.
I'd say 3 is therefore the better choice.
Also, as mentioned elsewhere, you end up leaking pages once the S2 page tables
are setup until the vCPU is destroyed, as nothing clears down the cache
AFAICT. With 40 that's way more than with 3.
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 15:58 ` Marc Zyngier
@ 2026-11-26 16:52 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 17+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-26 16:52 UTC (permalink / raw)
To: Marc Zyngier
Cc: Usama Arif, oupton, fuad.tabba, joey.gouly, seiden,
suzuki.poulose, yuzenghui, catalin.marinas, will, mark.rutland,
kvmarm, linux-arm-kernel, linux-kernel, riel, shakeel.butt,
kernel-team, rkeilty, atish.patra, dgreid
On Thu, Nov 26, 2026 at 03:58:11PM +0000, Marc Zyngier wrote:
> On Thu, 26 Nov 2026 14:32:04 +0000,
> Usama Arif <usama.arif@linux.dev> wrote:
>
> [nonsense deleted]
>
> > > Now, to the patch itself: I see no reason to special-case this. If
> > > kvm_mmu_cache_min_pages() is an indication of what is needed, then
> > > this macro should be driven by it, or the whole allocator take it into
> > > account. And then you adjust it by whatever fudge factor seems
> > > appropriate.
> > >
> >
> > Completely agree! [1] does that: no page-size special case, and
> > a capacity of KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL for every
> > page size. It can't track kvm_mmu_cache_min_pages()
> > at runtime: the vCPU's cache is shared by the canonical and NV shadow
> > MMUs, and its capacity must stay fixed. So it has to cover the largest
> > minimum, which stage-2 caps at 3, and 4 is that plus one page of fudge.
>
> Of course it can. That's not exactly rock science to add, rather than
> relying on another set of bad heuristics. And this must cover more
> than a single mapping per top-up, because that has a ripple effect on
> the speed at which we can split large blocks (funnily enough, people
> care about that stuff).
Hmm is splitting a factor here re: the cache?
In kvm_mmu_split_huge_pages() it is using kvm->arch.mmu.split_page_cache which
is a separate cache altogether right?
And splitting on fault is via e.g. kvm_vm_mem_abort() -> user_mem_abort() which
tops up anyway? (Minus an NV case which is a bug I think, I can send that as a
patch separately? I mention in another reply.)
Maybe I missed a case?
Re: the speed, having bigger cache size doesn't speed anything up as the refill
refills to the max each time, so you're doing the same # of allocations, just
maybe with a much bigger latency on one fault (seems bad to me)?
>
> M.
>
> --
> Without deviation from the norm, progress is not possible.
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 16:30 ` Lorenzo Stoakes (ARM)
@ 2026-11-26 16:53 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 17+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-26 16:53 UTC (permalink / raw)
To: Wei-Lin Chang
Cc: Usama Arif, Leonardo Bras, maz, oupton, fuad.tabba, joey.gouly,
seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
mark.rutland, kvmarm, linux-arm-kernel, linux-kernel, riel,
shakeel.butt, kernel-team, rkeilty, atish.patra, dgreid
On Thu, Nov 26, 2026 at 04:30:24PM +0000, Lorenzo Stoakes (ARM) wrote:
> I also think actually there's an existing problem with the top-up right now in
> user_mem_abort() that needs addressing too, actually, that this change exposes
> and will cause bugs if done as above.
>
> So right now top-up is conditional there:
>
> if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
> is_protected_kvm_enabled()) {
> ret = topup_mmu_memcache(s2fd->mmu, memcache);
> if (ret)
> return ret;
> }
>
> Inverted is 'permission fault && !dirty-logging && !pKVM'. So the question is
> whether a permission fault not needing a new page table is correct.
>
> So working through, the actual decision about a page table allocation is done
> in:
>
> user_mem_abort()
> -> kvm_s2_fault_map()
> -> kvm_pgtable_stage2_map()
> -> ... -> stage2_map_walk_leaf()
> -> stage2_map_walker_try_leaf()
> -> stage2_leaf_mapping_allowed()
> -> kvm_block_mapping_supported() [ if false, allocate a page table ]
>
> static bool kvm_block_mapping_supported(const struct kvm_pgtable_visit_ctx *ctx, u64 phys)
> {
> u64 granule = kvm_granule_size(ctx->level);
>
> ...
>
> if (granule > (ctx->end - ctx->addr))
> return false;
>
> ...
> }
>
> But page table size can vary in NV between host and guest (guest must be >=
> host) and it gets clamped to the smaller of the two in kvm_s2_resolve_vma_size():
>
> static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd,
> struct kvm_s2_fault_vma_info *s2vi,
> struct vm_area_struct *vma)
> {
> ...
> if (s2fd->nested) {
> ...
> /*
> * If we're about to create a shadow stage 2 entry, then we
> * can only create a block mapping if the guest stage 2 page
> * table uses at least as big a mapping.
> */
> max_map_size = min(kvm_s2_trans_size(s2fd->nested), max_map_size);
> ...
> }
> }
>
> This sets s2vi->vma_pagesize based on this value:
>
> static int kvm_s2_fault_get_vma_info(const struct kvm_s2_fault_desc *s2fd,
> struct kvm_s2_fault_vma_info *s2vi)
> {
> ...
> s2vi->vma_pagesize = BIT(kvm_s2_resolve_vma_size(s2fd, s2vi, vma));
> ...
> }
>
> And the fault map uses this in the mapping size, which defines ctx->addr, end
> above in kvm_block_mapping_supported():
>
> static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
> const struct kvm_s2_fault_vma_info *s2vi,
> enum kvm_pgtable_prot prot,
> void *memcache,
> struct kvm_s2_fault_result *result)
> {
> ...
> mapping_size = s2vi->vma_pagesize;
> ...
> ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, gfn_to_gpa(gfn), mapping_size,
> __pfn_to_phys(pfn), prot,
> memcache, flags);
> ...
> }
>
> So basically, if the host has smaller page table size than the guest, you need
> an extra page table level.
>
> So simple solution is to add nested to the check:
>
> if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
> is_protected_kvm_enabled() || s2fd->nested) {
> ret = topup_mmu_memcache(s2fd->mmu, memcache);
> if (ret)
> return ret;
> }
>
> But honestly I wonder at this point if it's truly worth doing all these checks?
> Maybe safer to just topup unconditionally.
>
> It'll only top up once before the cache is full anyway, so it's low cost. And
> would make more consistent with other fault paths too.
Given this is pre-existing, let me send a patch to fix this separately.
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
2026-11-26 16:45 ` Lorenzo Stoakes (ARM)
@ 2026-11-26 17:07 ` Leonardo Bras
0 siblings, 0 replies; 17+ messages in thread
From: Leonardo Bras @ 2026-11-26 17:07 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Leonardo Bras, Wei-Lin Chang, Usama Arif, maz, oupton, fuad.tabba,
joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, mark.rutland, kvmarm, linux-arm-kernel, linux-kernel, riel,
shakeel.butt, kernel-team, rkeilty, atish.patra, dgreid
On Thu, Nov 26, 2026 at 04:45:35PM +0000, Lorenzo Stoakes (ARM) wrote:
> On Thu, Nov 26, 2026 at 03:55:13PM +0000, Leonardo Bras wrote:
> > On Thu, Nov 26, 2026 at 03:34:19PM +0000, Wei-Lin Chang wrote:
> > > On Thu, Nov 26, 2026 at 03:19:41PM +0000, Leonardo Bras wrote:
> > > > On Thu, Nov 26, 2026 at 08:06:41AM +0000, Wei-Lin Chang wrote:
> > > > > On Wed, Nov 25, 2026 at 09:42:33PM +0000, Usama Arif wrote:
> > > > > >
> > > > > >
> > > > > > On 25/11/2026 10:25, Leonardo Bras wrote:
> > > > > > > On Wed, Nov 25, 2026 at 05:55:29PM +0000, Usama Arif wrote:
> > > > > > >>
> > > > > > >>
> > > > > > >> On 25/11/2026 09:14, Leonardo Bras wrote:
> > > > > > >>> On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> > > > > > >>>> When a stage-2 fault may need new page tables, KVM first tops up the
> > > > > > >>>> vCPU's page cache: if it holds fewer pages than the fault might need, it
> > > > > > >>>> is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
> > > > > > >>>> 40. Unused pages stay in the cache until the vCPU is destroyed.
> > > > > > >>>>
> > > > > > >>>> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> > > > > > >>>> used. Stage-2 has at most three levels and the root is preallocated, so
> > > > > > >>>> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> > > > > > >>>> As every last-level table covers 512 MiB, an 8 GiB guest needs just 17
> > > > > > >>>> table pages to map all of its memory, while its four vCPU caches get
> > > > > > >>>> filled with 160.
> > > > > > >>>>
> > > > > > >>>> On a production host with 64 KiB pages running 101 VMs with four vCPUs
> > > > > > >>>> each, the caches were still 86% full. The memory used:
> > > > > > >>>>
> > > > > > >>>> before after
> > > > > > >>>> per-vCPU capacity 2.5 MB 512 KB
> > > > > > >>>> all 404 vCPUs 869 MB <= 202 MB
> > > > > > >>>>
> > > > > > >>>> Let's lower the capacity to 8 with 64 KiB pages, In the production
> > > > > > >>>> usecase above, it frees at least 667 MB. That still covers the two-page
> > > > > > >>>> minimum and leaves room for several table allocations per refill. 4 KB
> > > > > > >>>> and 16 KB pages keep 40.
> > > > > > >>>>
> > > > > > >>>> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> > > > > > >>>> but it is topped up before every page it maps, so 8 is sufficient there
> > > > > > >>>> as well. The pKVM hypervisor memcache and the eager split cache don't
> > > > > > >>>> use this capacity.
> > > > > > >>>>
> > > > > > >>>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > > > > > >>>> ---
> > > > > > >>>> arch/arm64/include/asm/kvm_types.h | 5 +++++
> > > > > > >>>> 1 file changed, 5 insertions(+)
> > > > > > >>>>
> > > > > > >>>> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> > > > > > >>>> index 9a126b9e2d7c99..28568da9ea4a7b 100644
> > > > > > >>>> --- a/arch/arm64/include/asm/kvm_types.h
> > > > > > >>>> +++ b/arch/arm64/include/asm/kvm_types.h
> > > > > > >>>> @@ -2,7 +2,12 @@
> > > > > > >>>> #ifndef _ASM_ARM64_KVM_TYPES_H
> > > > > > >>>> #define _ASM_ARM64_KVM_TYPES_H
> > > > > > >>>>
> > > > > > >>>> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> > > > > > >>>> +#ifdef CONFIG_ARM64_64K_PAGES
> > > > > > >>>> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> > > > > > >>>> +#else
> > > > > > >>>> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> > > > > > >>>> +#endif
> > > > > > >>>>
> > > > > > >>>> #endif /* _ASM_ARM64_KVM_TYPES_H */
> > > > > > >>>>
> > > > > > >>>> --
> > > > > > >>>> 2.53.1-Meta
> > > > > > >>>>
> > > > > > >>>
> > > > > > >>> Hi Usama,
> > > > > > >>>
> > > > > > >>
> > > > > > >> Hello Leonardo,
> > > > > > >>
> > > > > > >>> Have you tried to reproduce this issue in v7.4-rc1 or newer?
> > > > > > >>
> > > > > > >> Yes. The patch is against kvmarm/next (v7.4-rc3), which includes
> > > > > > >> Lorenzo's pre-fault series, and I A/B tested it there.
> > > > > > >>
> > > > > > >> On unpatched v7.4-rc3 with 64 KiB pages, a VM whose 16 vCPUs each fault
> > > > > > >> in one page needs a single stage-2 table page, yet the vCPU caches hold
> > > > > > >> 640 pages (40 MiB). With the patch they hold 127 pages.
> > > > > > >>
> > > > > > >>>
> > > > > > >>> IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
> > > > > > >>> skip topup if the cache contains a minimum of ~4 pages, so it can possibly
> > > > > > >>> improve that issue.
> > > > > > >>
> > > > > > >> I don't think that part is new. __kvm_mmu_topup_memory_cache() has
> > > > > > >> returned early when mc->nobjs >= min since commit 6926f95accee ("KVM:
> > > > > > >> Move x86's MMU memory cache helpers to common KVM code"). Lorenzo's
> > > > > > >> commit d27acc152c62 ("KVM: arm64: Size the stage-2 memcache from the
> > > > > > >> fault MMU") takes min from s2fd->mmu instead of vcpu->arch.hw_mmu. For a
> > > > > > >> guest abort that is the same MMU, so nothing changes here.
> > > > > > >>
> > > > > > >> With 64 KiB pages, min is kvm_mmu_cache_min_pages(), which is at most 2.
> > > > > > >>
> > > > > > >>>
> > > > > > >>> Although I can see that when it's below that minimum it fills the whole
> > > > > > >>> capacity (40) of the cache.
> > > > > > >>>
> > > > > > >>
> > > > > > >> Right, and that is the problem. Skipping the top-up avoids refilling
> > > > > > >> the cache, but nothing ever drains it. A vCPU's first fault fills its
> > > > > > >> cache to 40. Each last-level table covers 512 MiB, so a guest needs very
> > > > > > >> few table pages, and most vCPUs never drop below min. The remaining
> > > > > > >> pages stay allocated until the vCPU is destroyed, which is why the
> > > > > > >> production caches were still 86% full.
> > > > > > >>
> > > > > > >> The patch keeps the early return and only lowers how far a refill goes.
> > > > > > >
> > > > > > > Right,
> > > > > > >
> > > > > > > I took a look in the cache workings, and it does not seem to receive freed
> > > > > > > pages back in. It gets filled by topup and used by map, so IIUC it is used
> > > > > > > only to avoid failing an allocation during the mapping walk.
> > > > > > >
> > > > > > > Maybe it could be the case to simplify this cache to have the capacity
> > > > > > > about the same as the minimum (since it always topup before mapping) and
> > > > > > > avoid the waste altogether.
> > > > >
> > > > > Well the more you reserve the less frequent you'd need to actually allocate at
> > > > > topup, which saves time, so there's this trade-off to consider.
> > > > >
> > > > > Thanks,
> > > > > Wei-Lin Chang
> > > > >
> > > >
> > > > Hi Wei-Lin,
> > > >
> > > > IIUC, every time we topup, we call a mmu_memory_cache_alloc_obj() for every
> > > > one of the objects in the array. So as it is today it runs it 40 times, and
> > >
> > > Not necessarily, I meant with 40,
> > >
> > > if (mc->nobjs >= min)
> > > return 0;
> > >
> > > happens way more often.
> >
> > That's what I meant with 'waits until it runs down'. Basically we have
> >
> > if (mc->nobjs >= min)
> > return 0;
> >
> > while (mc->nobjs < mc->capacity) {
> > obj = mmu_memory_cache_alloc_obj(mc, gfp);
> > ...
> >
> > So, first use it allocates mc->capacity entries, and every further topup
> > will return early unless it goes below min, when it populates again. The
> > only reason for a larger cache being more performant would be to use
> > that cache locality, which could indeed have a large impact, if we use this
> > cache a lot.
>
> I think there's confusion because of this being called a cache, this is more
> pre-allocation than anything it seems. As per the comment above struct
> kvm_mmu_memory_cache in include/linux/kvm_types.h:
>
> * Memory caches are used to preallocate memory ahead of various MMU flows,
> * e.g. page fault handlers. Gracefully handling allocation failures deep in
> * MMU flows is problematic, as is triggering reclaim, I/O, etc... while
> * holding MMU locks. Note, these caches act more like prefetch buffers than
> * classical caches, i.e. objects are not returned to the cache on being freed.
>
> The question is would going from 40 to 3 slow things down?
>
> At 40 you do 40 __get_free_page() calls on topup for the _first_ fault that hits
> it, which adds latency to a random s2 fault (not great).
>
> (If the fault doesn't in fact use a page table, this is only done once until a
> page table is next needed).
>
> And then as 37 page tables are mapped, those ones don't get allocations at 38
> you go again...
>
> With 3 in the cache you allocate up to 3 for each fault.
>
> So in both cases you allocate the same amount of pages but in the 40 case it's
> all bunched up on one s2 fault with a jump in latency, in the 3 case it's spread
> out.
>
> I'd say 3 is therefore the better choice.
>
> Also, as mentioned elsewhere, you end up leaking pages once the S2 page tables
> are setup until the vCPU is destroyed, as nothing clears down the cache
> AFAICT. With 40 that's way more than with 3.
>
> --
> Cheers, Lorenzo
Hi Lorenzo,
When I mention cache locality above, I mean the cpu cache. Running the same
routine 40 times in a row can be faster since all instructions are already
cached, and most of the data acessed by it could be local as well.
If you run it more often, it could be that something else used the
cachelines and the cpu has to fetch them again from lower level caches or
memory.
That being said, that's the only reason batch allocating 40 would be faster
than 4 in the long run. For capacity=4, the number of
mmu_memory_cache_alloc_obj() calls can be even smaller than capacity=40,
since we don't leave unused pages in the kvm_mmu_memory_cache.
Thanks!
Leo
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2026-11-26 17:07 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 15:28 [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts Usama Arif
2026-11-25 17:14 ` Leonardo Bras
2026-11-25 17:55 ` Usama Arif
2026-11-25 18:25 ` Leonardo Bras
2026-11-25 21:42 ` Usama Arif
2026-11-26 8:06 ` Wei-Lin Chang
2026-11-26 15:19 ` Leonardo Bras
2026-11-26 15:34 ` Wei-Lin Chang
2026-11-26 15:55 ` Leonardo Bras
2026-11-26 16:45 ` Lorenzo Stoakes (ARM)
2026-11-26 17:07 ` Leonardo Bras
2026-11-26 16:30 ` Lorenzo Stoakes (ARM)
2026-11-26 16:53 ` Lorenzo Stoakes (ARM)
2026-11-26 12:56 ` Marc Zyngier
2026-11-26 14:32 ` Usama Arif
2026-11-26 15:58 ` Marc Zyngier
2026-11-26 16:52 ` Lorenzo Stoakes (ARM)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).