* [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()
@ 2026-11-30 14:30 Usama Arif
2026-11-30 15:26 ` Leonardo Bras
0 siblings, 1 reply; 4+ messages in thread
From: Usama Arif @ 2026-11-30 14:30 UTC (permalink / raw)
To: ljs, leo.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
Cc: Usama Arif
When fewer than kvm_mmu_cache_min_pages() pages are left, a stage-2
fault that may need new tables refills the vCPU's memcache to
KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, 40 on arm64. A map allocates at most
one table per level below the preallocated root, which is
kvm_mmu_cache_min_pages() and at most 3 pages. Only vCPU destruction
frees the memcache, so whatever a refill leaves unused stays allocated:
up to 2.5 MiB per vCPU with 64 KiB pages. On a 64 KiB host running 101
VMs with 4 vCPUs and 1 GiB each, the memcaches held 986 MiB, against
25 MiB of stage-2 tables in use.
Size the memcache at twice the largest kvm_mmu_cache_min_pages() of the
VM's stage-2s instead, so that each refill covers at least two
worst-case maps. With NV, the vCPU's memcache also feeds the shadow
stage-2s, and a memcache's capacity can't change once used, so record
the largest value as each stage-2 is initialised. The VM's stage-2 is
set up at VM creation and the first shadow stage-2s by the first
KVM_ARM_VCPU_INIT, before any vCPU can fault, and later shadow stage-2s
have the same size. kvm_phys_addr_ioremap()'s temporary memcache uses
the same capacity, so arm64 no longer uses the value of
KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE.
With a 48-bit IPA, the capacity is 4 pages with 16 KiB and 64 KiB pages
and 6 with 4 KiB pages; with the default 40-bit IPA, 2 and 4. On the
host above and on a production host also running 101 VMs with 4 vCPUs
each, both with 64 KiB pages and 48-bit IPA VMs:
memcaches saved
before after
host above 986 MiB 81 MiB 905 MiB
production host 869 MiB <= 101 MiB >= 768 MiB
A memcache now refills at most once every two tables, as a capacity of
4 does with 4 KiB pages and a 48-bit IPA. Measured that way, per fault
for the median vCPU, one VM at a time:
40 4
2M blocks split under dirty logging 4.9-5.1 us 4.9-5.0 us
1G block split, then refaulted 2.7-2.8 us 2.8 us
every fault allocating a table 4.0-4.1 us 3.7-4.1 us
Suggested-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
arch/arm64/include/asm/kvm_host.h | 2 ++
arch/arm64/kvm/mmu.c | 19 +++++++++++++++----
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 97332ffcb1300e..e96c6208686240 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -413,6 +413,8 @@ enum kvm_arm_vm_flavor {
struct kvm_arch {
struct kvm_s2_mmu mmu;
+ /* Capacity of the vCPU stage-2 memcaches */
+ int mmu_page_cache_capacity;
const struct kvm_vm_s2_ops *vm_s2_ops;
enum kvm_arm_vm_flavor vm_flavor;
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 042dc53267cb36..cd0605cce45d2a 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1045,7 +1045,7 @@ static void kvm_stage2_destroy(struct kvm_pgtable *pgt)
*/
int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type)
{
- int cpu, err;
+ int cpu, err, capacity;
struct kvm_pgtable *pgt;
/* Initialize the VM ops for the VM instance for the first time */
@@ -1086,6 +1086,15 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
goto out_free_pgtable;
mmu->pgt = pgt;
+
+ /*
+ * The vCPU memcaches serve all of the VM's stage-2s and can't be
+ * resized once used, so make them cover two maps on the deepest one.
+ */
+ capacity = 2 * kvm_mmu_cache_min_pages(mmu);
+ if (capacity > kvm->arch.mmu_page_cache_capacity)
+ kvm->arch.mmu_page_cache_capacity = capacity;
+
if (is_protected_kvm_enabled())
return 0;
@@ -1317,8 +1326,9 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
guest_ipa &= PAGE_MASK;
for (addr = guest_ipa; addr < guest_ipa + size; addr += PAGE_SIZE) {
- ret = kvm_mmu_topup_memory_cache(&cache,
- kvm_mmu_cache_min_pages(mmu));
+ ret = __kvm_mmu_topup_memory_cache(&cache,
+ kvm->arch.mmu_page_cache_capacity,
+ kvm_mmu_cache_min_pages(mmu));
if (ret)
break;
@@ -1669,10 +1679,11 @@ static void *get_mmu_memcache(struct kvm_vcpu *vcpu)
static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache)
{
+ int capacity = kvm_s2_mmu_to_kvm(mmu)->arch.mmu_page_cache_capacity;
int min_pages = kvm_mmu_cache_min_pages(mmu);
if (!is_protected_kvm_enabled())
- return kvm_mmu_topup_memory_cache(memcache, min_pages);
+ return __kvm_mmu_topup_memory_cache(memcache, capacity, min_pages);
return topup_hyp_memcache(memcache, min_pages);
}
--
2.53.1-Meta
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()
2026-11-30 14:30 [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages() Usama Arif
@ 2026-11-30 15:26 ` Leonardo Bras
2026-11-30 17:31 ` Usama Arif
0 siblings, 1 reply; 4+ messages in thread
From: Leonardo Bras @ 2026-11-30 15:26 UTC (permalink / raw)
To: Usama Arif
Cc: Leonardo Bras, ljs, 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 Mon, Nov 30, 2026 at 06:30:39AM -0800, Usama Arif wrote:
> When fewer than kvm_mmu_cache_min_pages() pages are left, a stage-2
> fault that may need new tables refills the vCPU's memcache to
> KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, 40 on arm64. A map allocates at most
> one table per level below the preallocated root, which is
> kvm_mmu_cache_min_pages() and at most 3 pages. Only vCPU destruction
> frees the memcache, so whatever a refill leaves unused stays allocated:
> up to 2.5 MiB per vCPU with 64 KiB pages. On a 64 KiB host running 101
> VMs with 4 vCPUs and 1 GiB each, the memcaches held 986 MiB, against
> 25 MiB of stage-2 tables in use.
>
> Size the memcache at twice the largest kvm_mmu_cache_min_pages() of the
> VM's stage-2s instead,
Why 2? New heuristic to replace the current one? :P
But really, do you have any performance numbers for 1, 2 and 4+ worst-case
block split maps? That would be good to have as a reason for picking a
multiplier.
> so that each refill covers at least two
> worst-case maps. With NV, the vCPU's memcache also feeds the shadow
> stage-2s, and a memcache's capacity can't change once used, so record
> the largest value as each stage-2 is initialised. The VM's stage-2 is
> set up at VM creation and the first shadow stage-2s by the first
> KVM_ARM_VCPU_INIT, before any vCPU can fault, and later shadow stage-2s
> have the same size. kvm_phys_addr_ioremap()'s temporary memcache uses
> the same capacity, so arm64 no longer uses the value of
> KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE.
>
> With a 48-bit IPA, the capacity is 4 pages with 16 KiB and 64 KiB pages
> and 6 with 4 KiB pages; with the default 40-bit IPA, 2 and 4. On the
> host above and on a production host also running 101 VMs with 4 vCPUs
> each, both with 64 KiB pages and 48-bit IPA VMs:
>
> memcaches saved
> before after
> host above 986 MiB 81 MiB 905 MiB
> production host 869 MiB <= 101 MiB >= 768 MiB
>
> A memcache now refills at most once every two tables, as a capacity of
> 4 does with 4 KiB pages and a 48-bit IPA. Measured that way, per fault
> for the median vCPU, one VM at a time:
>
> 40 4
> 2M blocks split under dirty logging 4.9-5.1 us 4.9-5.0 us
> 1G block split, then refaulted 2.7-2.8 us 2.8 us
> every fault allocating a table 4.0-4.1 us 3.7-4.1 us
>
Would be nice to have numbers for 16k and 64k as well, as longer tables
may just in case longer tables have different impact. If you run them,
please share the numbers for the whole operation, instead of a per-fault
version, as well as which code you used to test this, so people can
reproduce if they want.
> Suggested-by: Marc Zyngier <maz@kernel.org>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> arch/arm64/include/asm/kvm_host.h | 2 ++
> arch/arm64/kvm/mmu.c | 19 +++++++++++++++----
> 2 files changed, 17 insertions(+), 4 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> index 97332ffcb1300e..e96c6208686240 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -413,6 +413,8 @@ enum kvm_arm_vm_flavor {
>
> struct kvm_arch {
> struct kvm_s2_mmu mmu;
> + /* Capacity of the vCPU stage-2 memcaches */
> + int mmu_page_cache_capacity;
Why add a new struct member here, if you could instead use your
(MULTIPLIER * kvm_mmu_cache_min_pages(mmu) when you need it?
You are already calculating kvm_mmu_cache_min_pages() anyway, so it's just
one extra operation, see below...
> const struct kvm_vm_s2_ops *vm_s2_ops;
>
> enum kvm_arm_vm_flavor vm_flavor;
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index 042dc53267cb36..cd0605cce45d2a 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -1045,7 +1045,7 @@ static void kvm_stage2_destroy(struct kvm_pgtable *pgt)
> */
> int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type)
> {
> - int cpu, err;
> + int cpu, err, capacity;
> struct kvm_pgtable *pgt;
>
> /* Initialize the VM ops for the VM instance for the first time */
> @@ -1086,6 +1086,15 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
> goto out_free_pgtable;
>
> mmu->pgt = pgt;
> +
> + /*
> + * The vCPU memcaches serve all of the VM's stage-2s and can't be
> + * resized once used, so make them cover two maps on the deepest one.
> + */
> + capacity = 2 * kvm_mmu_cache_min_pages(mmu);
> + if (capacity > kvm->arch.mmu_page_cache_capacity)
> + kvm->arch.mmu_page_cache_capacity = capacity;
> +
This would not be needed
> if (is_protected_kvm_enabled())
> return 0;
>
> @@ -1317,8 +1326,9 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
> guest_ipa &= PAGE_MASK;
>
> for (addr = guest_ipa; addr < guest_ipa + size; addr += PAGE_SIZE) {
> - ret = kvm_mmu_topup_memory_cache(&cache,
> - kvm_mmu_cache_min_pages(mmu));
> + ret = __kvm_mmu_topup_memory_cache(&cache,
> + kvm->arch.mmu_page_cache_capacity,
> + kvm_mmu_cache_min_pages(mmu));
This neither
> if (ret)
> break;
>
> @@ -1669,10 +1679,11 @@ static void *get_mmu_memcache(struct kvm_vcpu *vcpu)
>
> static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache)
> {
> + int capacity = kvm_s2_mmu_to_kvm(mmu)->arch.mmu_page_cache_capacity;
You could have, instead:
int min_pages = kvm_mmu_cache_min_pages(mmu);
+ int capacity = MULTIPLIER * min_pages;
Which would even make it more clear to the reader that you are using a
multiple instead.
>
> if (!is_protected_kvm_enabled())
> - return kvm_mmu_topup_memory_cache(memcache, min_pages);
> + return __kvm_mmu_topup_memory_cache(memcache, capacity, min_pages);
>
> return topup_hyp_memcache(memcache, min_pages);
It could even be an one-line change if you want :)
Thanks!
Leo
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()
2026-11-30 15:26 ` Leonardo Bras
@ 2026-11-30 17:31 ` Usama Arif
2026-11-30 18:29 ` Leonardo Bras
0 siblings, 1 reply; 4+ messages in thread
From: Usama Arif @ 2026-11-30 17:31 UTC (permalink / raw)
To: Leonardo Bras
Cc: ljs, 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 30/11/2026 07:26, Leonardo Bras wrote:
> On Mon, Nov 30, 2026 at 06:30:39AM -0800, Usama Arif wrote:
>> When fewer than kvm_mmu_cache_min_pages() pages are left, a stage-2
>> fault that may need new tables refills the vCPU's memcache to
>> KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, 40 on arm64. A map allocates at most
>> one table per level below the preallocated root, which is
>> kvm_mmu_cache_min_pages() and at most 3 pages. Only vCPU destruction
>> frees the memcache, so whatever a refill leaves unused stays allocated:
>> up to 2.5 MiB per vCPU with 64 KiB pages. On a 64 KiB host running 101
>> VMs with 4 vCPUs and 1 GiB each, the memcaches held 986 MiB, against
>> 25 MiB of stage-2 tables in use.
>>
>> Size the memcache at twice the largest kvm_mmu_cache_min_pages() of the
>> VM's stage-2s instead,
>
> Why 2? New heuristic to replace the current one? :P
>
> But really, do you have any performance numbers for 1, 2 and 4+ worst-case
> block split maps? That would be good to have as a reason for picking a
> multiplier.
>
2 is the smallest multiple that does what Marc asked for on v1, a
capacity that covers more than one mapping per top-up [1].
I put numbers in the commit message below and in [2] for using
a fixed value of 40 vs 2 x kvm_mmu_cache_min_pages() and didnt see a
regression so didnt see a reason to test other multipliers, as that would
just be wasting memory without any performance benefits?
>> so that each refill covers at least two
>> worst-case maps. With NV, the vCPU's memcache also feeds the shadow
>> stage-2s, and a memcache's capacity can't change once used, so record
>> the largest value as each stage-2 is initialised. The VM's stage-2 is
>> set up at VM creation and the first shadow stage-2s by the first
>> KVM_ARM_VCPU_INIT, before any vCPU can fault, and later shadow stage-2s
>> have the same size. kvm_phys_addr_ioremap()'s temporary memcache uses
>> the same capacity, so arm64 no longer uses the value of
>> KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE.
>>
>> With a 48-bit IPA, the capacity is 4 pages with 16 KiB and 64 KiB pages
>> and 6 with 4 KiB pages; with the default 40-bit IPA, 2 and 4. On the
>> host above and on a production host also running 101 VMs with 4 vCPUs
>> each, both with 64 KiB pages and 48-bit IPA VMs:
>>
>> memcaches saved
>> before after
>> host above 986 MiB 81 MiB 905 MiB
>> production host 869 MiB <= 101 MiB >= 768 MiB
>>
>> A memcache now refills at most once every two tables, as a capacity of
>> 4 does with 4 KiB pages and a 48-bit IPA. Measured that way, per fault
>> for the median vCPU, one VM at a time:
>>
>> 40 4
>> 2M blocks split under dirty logging 4.9-5.1 us 4.9-5.0 us
>> 1G block split, then refaulted 2.7-2.8 us 2.8 us
>> every fault allocating a table 4.0-4.1 us 3.7-4.1 us
>>
>
> Would be nice to have numbers for 16k and 64k as well, as longer tables
> may just in case longer tables have different impact. If you run them,
> please share the numbers for the whole operation, instead of a per-fault
> version, as well as which code you used to test this, so people can
> reproduce if they want.
>
I tested above with 4K, 16K and 64K base pages and the numbers are
similar, i.e. no regression found.
Full disclosure, the benchmark was generated by LLM and I didn't
want to put LLM crap in reply, but I can share the raw results and
benchmark if you would like.
>> Suggested-by: Marc Zyngier <maz@kernel.org>
>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
>> ---
>> arch/arm64/include/asm/kvm_host.h | 2 ++
>> arch/arm64/kvm/mmu.c | 19 +++++++++++++++----
>> 2 files changed, 17 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
>> index 97332ffcb1300e..e96c6208686240 100644
>> --- a/arch/arm64/include/asm/kvm_host.h
>> +++ b/arch/arm64/include/asm/kvm_host.h
>> @@ -413,6 +413,8 @@ enum kvm_arm_vm_flavor {
>>
>> struct kvm_arch {
>> struct kvm_s2_mmu mmu;
>> + /* Capacity of the vCPU stage-2 memcaches */
>> + int mmu_page_cache_capacity;
>
> Why add a new struct member here, if you could instead use your
> (MULTIPLIER * kvm_mmu_cache_min_pages(mmu) when you need it?
>
> You are already calculating kvm_mmu_cache_min_pages() anyway, so it's just
> one extra operation, see below...
Because a memcache has to be topped up with the same capacity every
time, and with NV the vCPU's memcache is topped up for more than one
stage-2. __kvm_mmu_topup_memory_cache() has:
/* It is illegal to request a different capacity across topups. */
if (WARN_ON_ONCE(mc->capacity != capacity))
return -EIO;
topup_mmu_memcache() gets the stage-2 that faulted: the VM's own for
L1, a shadow one for L2. The shadow stage-2s are sized from
kvm_get_pa_bits(), which is kvm_ipa_limit, so they can have more levels
than the VM's stage-2. With 4K pages, a VM with the default 40-bit IPA
has a 3-level stage-2 and its shadow stage-2s on a 48-bit host have 4,
so 2 * kvm_mmu_cache_min_pages(mmu) would be 4 for L1's faults and 6
for L2's.
I tried your MULTIPLIER * min_pages version under QEMU with
kvm-arm.mode=nested: 4K pages, an IPA limit of 52 bits, and a 40-bit
IPA VM whose L1 runs an L2. In all six runs, across two tests, L2's
first fault that needs a refill hits that WARN_ON_ONCE() and KVM_RUN
fails with EIO.
>
>> const struct kvm_vm_s2_ops *vm_s2_ops;
>>
>> enum kvm_arm_vm_flavor vm_flavor;
>> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
>> index 042dc53267cb36..cd0605cce45d2a 100644
>> --- a/arch/arm64/kvm/mmu.c
>> +++ b/arch/arm64/kvm/mmu.c
>> @@ -1045,7 +1045,7 @@ static void kvm_stage2_destroy(struct kvm_pgtable *pgt)
>> */
>> int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type)
>> {
>> - int cpu, err;
>> + int cpu, err, capacity;
>> struct kvm_pgtable *pgt;
>>
>> /* Initialize the VM ops for the VM instance for the first time */
>> @@ -1086,6 +1086,15 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
>> goto out_free_pgtable;
>>
>> mmu->pgt = pgt;
>> +
>> + /*
>> + * The vCPU memcaches serve all of the VM's stage-2s and can't be
>> + * resized once used, so make them cover two maps on the deepest one.
>> + */
>> + capacity = 2 * kvm_mmu_cache_min_pages(mmu);
>> + if (capacity > kvm->arch.mmu_page_cache_capacity)
>> + kvm->arch.mmu_page_cache_capacity = capacity;
>> +
>
> This would not be needed
>
>> if (is_protected_kvm_enabled())
>> return 0;
>>
>> @@ -1317,8 +1326,9 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
>> guest_ipa &= PAGE_MASK;
>>
>> for (addr = guest_ipa; addr < guest_ipa + size; addr += PAGE_SIZE) {
>> - ret = kvm_mmu_topup_memory_cache(&cache,
>> - kvm_mmu_cache_min_pages(mmu));
>> + ret = __kvm_mmu_topup_memory_cache(&cache,
>> + kvm->arch.mmu_page_cache_capacity,
>> + kvm_mmu_cache_min_pages(mmu));
>
> This neither
>
>> if (ret)
>> break;
>>
>> @@ -1669,10 +1679,11 @@ static void *get_mmu_memcache(struct kvm_vcpu *vcpu)
>>
>> static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache)
>> {
>> + int capacity = kvm_s2_mmu_to_kvm(mmu)->arch.mmu_page_cache_capacity;
>
> You could have, instead:
>
> int min_pages = kvm_mmu_cache_min_pages(mmu);
> + int capacity = MULTIPLIER * min_pages;
>
> Which would even make it more clear to the reader that you are using a
> multiple instead.
>
>>
>> if (!is_protected_kvm_enabled())
>> - return kvm_mmu_topup_memory_cache(memcache, min_pages);
>> + return __kvm_mmu_topup_memory_cache(memcache, capacity, min_pages);
>>
>> return topup_hyp_memcache(memcache, min_pages);
>
>
> It could even be an one-line change if you want :)
>
> Thanks!
> Leo
[1] https://lore.kernel.org/all/86v75jy48c.wl-maz@kernel.org/
[2] https://lore.kernel.org/all/d2b6e735-a685-4e79-bb39-2fb75ebecd09@linux.dev/#t
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()
2026-11-30 17:31 ` Usama Arif
@ 2026-11-30 18:29 ` Leonardo Bras
0 siblings, 0 replies; 4+ messages in thread
From: Leonardo Bras @ 2026-11-30 18:29 UTC (permalink / raw)
To: Usama Arif
Cc: Leonardo Bras, ljs, 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 Mon, Nov 30, 2026 at 05:31:19PM +0000, Usama Arif wrote:
>
>
> On 30/11/2026 07:26, Leonardo Bras wrote:
> > On Mon, Nov 30, 2026 at 06:30:39AM -0800, Usama Arif wrote:
> >> When fewer than kvm_mmu_cache_min_pages() pages are left, a stage-2
> >> fault that may need new tables refills the vCPU's memcache to
> >> KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, 40 on arm64. A map allocates at most
> >> one table per level below the preallocated root, which is
> >> kvm_mmu_cache_min_pages() and at most 3 pages. Only vCPU destruction
> >> frees the memcache, so whatever a refill leaves unused stays allocated:
> >> up to 2.5 MiB per vCPU with 64 KiB pages. On a 64 KiB host running 101
> >> VMs with 4 vCPUs and 1 GiB each, the memcaches held 986 MiB, against
> >> 25 MiB of stage-2 tables in use.
> >>
> >> Size the memcache at twice the largest kvm_mmu_cache_min_pages() of the
> >> VM's stage-2s instead,
> >
> > Why 2? New heuristic to replace the current one? :P
> >
> > But really, do you have any performance numbers for 1, 2 and 4+ worst-case
> > block split maps? That would be good to have as a reason for picking a
> > multiplier.
> >
>
> 2 is the smallest multiple that does what Marc asked for on v1, a
> capacity that covers more than one mapping per top-up [1].
>
> I put numbers in the commit message below and in [2] for using
> a fixed value of 40 vs 2 x kvm_mmu_cache_min_pages() and didnt see a
> regression so didnt see a reason to test other multipliers, as that would
> just be wasting memory without any performance benefits?
You tested for 2x, I asked if you also tested 1x, 2x, 4x, and so on.
I am not 100% sure Marc asked for a multiple like that, but even if it was,
he seems to be worried about performance on block splitting and re-faulting
(as you tested). Having 1x, 2x and 4x at least would help us better
understand the kind of impact that would have.
>
> >> so that each refill covers at least two
> >> worst-case maps. With NV, the vCPU's memcache also feeds the shadow
> >> stage-2s, and a memcache's capacity can't change once used, so record
> >> the largest value as each stage-2 is initialised. The VM's stage-2 is
> >> set up at VM creation and the first shadow stage-2s by the first
> >> KVM_ARM_VCPU_INIT, before any vCPU can fault, and later shadow stage-2s
> >> have the same size. kvm_phys_addr_ioremap()'s temporary memcache uses
> >> the same capacity, so arm64 no longer uses the value of
> >> KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE.
> >>
> >> With a 48-bit IPA, the capacity is 4 pages with 16 KiB and 64 KiB pages
> >> and 6 with 4 KiB pages; with the default 40-bit IPA, 2 and 4. On the
> >> host above and on a production host also running 101 VMs with 4 vCPUs
> >> each, both with 64 KiB pages and 48-bit IPA VMs:
> >>
> >> memcaches saved
> >> before after
> >> host above 986 MiB 81 MiB 905 MiB
> >> production host 869 MiB <= 101 MiB >= 768 MiB
> >>
> >> A memcache now refills at most once every two tables, as a capacity of
> >> 4 does with 4 KiB pages and a 48-bit IPA. Measured that way, per fault
> >> for the median vCPU, one VM at a time:
> >>
> >> 40 4
> >> 2M blocks split under dirty logging 4.9-5.1 us 4.9-5.0 us
> >> 1G block split, then refaulted 2.7-2.8 us 2.8 us
> >> every fault allocating a table 4.0-4.1 us 3.7-4.1 us
> >>
> >
> > Would be nice to have numbers for 16k and 64k as well, as longer tables
> > may just in case longer tables have different impact. If you run them,
> > please share the numbers for the whole operation, instead of a per-fault
> > version, as well as which code you used to test this, so people can
> > reproduce if they want.
> >
>
> I tested above with 4K, 16K and 64K base pages and the numbers are
> similar, i.e. no regression found.
>
> Full disclosure, the benchmark was generated by LLM and I didn't
> want to put LLM crap in reply, but I can share the raw results and
> benchmark if you would like.
I think it's now even more important that we see the tests, as the
LLM-generated one could be measuring stuff wrong.
Also, the results are important, mainly if they are composed with the
multiplier in the previous comment.
>
> >> Suggested-by: Marc Zyngier <maz@kernel.org>
> >> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> >> ---
> >> arch/arm64/include/asm/kvm_host.h | 2 ++
> >> arch/arm64/kvm/mmu.c | 19 +++++++++++++++----
> >> 2 files changed, 17 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> >> index 97332ffcb1300e..e96c6208686240 100644
> >> --- a/arch/arm64/include/asm/kvm_host.h
> >> +++ b/arch/arm64/include/asm/kvm_host.h
> >> @@ -413,6 +413,8 @@ enum kvm_arm_vm_flavor {
> >>
> >> struct kvm_arch {
> >> struct kvm_s2_mmu mmu;
> >> + /* Capacity of the vCPU stage-2 memcaches */
> >> + int mmu_page_cache_capacity;
> >
> > Why add a new struct member here, if you could instead use your
> > (MULTIPLIER * kvm_mmu_cache_min_pages(mmu) when you need it?
> >
> > You are already calculating kvm_mmu_cache_min_pages() anyway, so it's just
> > one extra operation, see below...
>
> Because a memcache has to be topped up with the same capacity every
> time, and with NV the vCPU's memcache is topped up for more than one
> stage-2.
Ah, I see, the cache is the same based on a vcpu, and the S2_mmu depend on
it being either the canonical mmu or a nested one. I had previously misread
and thought that you were saving the capacity at the s2_mmu instead of
kvm_arch.
I still think there is a better way of doing this, maybe calculating the
maximum levels a current pagesize could have.
Somethink like:
capacity = VTCR_EL2_SL0_TO_LVLS(0b10);
Since a guest never have a smaller pagesize, this could be safe, and ends
up being compile-time as well.
[...]
>
> I tried your MULTIPLIER * min_pages version under QEMU with
> kvm-arm.mode=nested: 4K pages, an IPA limit of 52 bits, and a 40-bit
> IPA VM whose L1 runs an L2. In all six runs, across two tests, L2's
> first fault that needs a refill hits that WARN_ON_ONCE() and KVM_RUN
> fails with EIO.
Out of curiosity, is it fully KVM, or are you using QEMU to emulate a
4k-pagesize host?
Is it hitting with which value for MULTIPLIER?
Ah, you mean based in the version that multiplies
kvm_mmu_cache_min_pages(), yeah, as I mentioned above, you were right in
not using the min for that.
Thanks!
Leo
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-30 18:30 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 14:30 [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages() Usama Arif
2026-11-30 15:26 ` Leonardo Bras
2026-11-30 17:31 ` Usama Arif
2026-11-30 18:29 ` Leonardo Bras
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®