Skip to contentSkip to conversation

KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()

PATCH In review Usama Arif 3 participants 13 messages Nov 30 – Dec 2

Subsystems (2)
lore
Share & tools
lore

Usama Arif

author
#0 lore raw

PATCH v2 1/1 KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()

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
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index 97332ffcb1300e..e96c6208686240 100644
@@ -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;
arch/arm64/kvm/mmu.c +15 -4
index 042dc53267cb36..cd0605cce45d2a 100644
@@ -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);
 }
Signature · 1 line
2.53.1-Meta
#1 lore raw
Quoted 10 lines · 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.

Quoted 2 lines

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.

Quoted 20 lines

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
Quoted 9 lines

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.

Quoted 12 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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...

Quoted 19 lines
arch/arm64/include/asm/kvm_host.h +0 -0
 	const struct kvm_vm_s2_ops *vm_s2_ops;
 
 	enum kvm_arm_vm_flavor vm_flavor;
arch/arm64/kvm/mmu.c +1 -1
index 042dc53267cb36..cd0605cce45d2a 100644
@@ -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;
 

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1097

Quoted 10 lines
arch/arm64/kvm/mmu.c +9 -0
 	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

arch/arm64/kvm/mmu.c int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, · line 1331

Quoted 12 lines
arch/arm64/kvm/mmu.c +3 -2
 	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

arch/arm64/kvm/mmu.c static void *get_mmu_memcache(struct kvm_vcpu *vcpu) · line 1682

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -0
 		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.

arch/arm64/kvm/mmu.c static void *get_mmu_memcache(struct kvm_vcpu *vcpu) · line 1688

Quoted 6 lines
arch/arm64/kvm/mmu.c +1 -1
 	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

author
#2 lore raw
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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?

Quoted 30 lines
Quoted 20 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 21 lines
Quoted 12 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 2 lines

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.

Quoted 70 lines
Quoted 20 lines
arch/arm64/include/asm/kvm_host.h +0 -0
 	const struct kvm_vm_s2_ops *vm_s2_ops;
 
 	enum kvm_arm_vm_flavor vm_flavor;
arch/arm64/kvm/mmu.c +1 -1
index 042dc53267cb36..cd0605cce45d2a 100644
@@ -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;
 

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1097

Quoted 10 lines
arch/arm64/kvm/mmu.c +9 -0
 	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

arch/arm64/kvm/mmu.c int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, · line 1331

Quoted 12 lines
arch/arm64/kvm/mmu.c +3 -2
 	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

arch/arm64/kvm/mmu.c static void *get_mmu_memcache(struct kvm_vcpu *vcpu) · line 1682

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -0
 		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);
(quoted diff) +1 -0
+	int capacity = MULTIPLIER * min_pages;

Which would even make it more clear to the reader that you are using a multiple instead.

Quoted 5 lines
arch/arm64/kvm/mmu.c +1 -1
 	if (!is_protected_kvm_enabled())
-		return kvm_mmu_topup_memory_cache(memcache, min_pages);
+		return __kvm_mmu_topup_memory_cache(memcache, capacity, min_pages);
 
Quoted 7 lines

arch/arm64/kvm/mmu.c static void *get_mmu_memcache(struct kvm_vcpu *vcpu) · line 1688

Quoted 1 line
arch/arm64/kvm/mmu.c +0 -0
 	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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/#t

#3 lore raw
Quoted 24 lines · Usama Arif
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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.

Quoted 7 lines

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.

Quoted 38 lines
Quoted 31 lines
Quoted 21 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 6 lines

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.

Quoted 22 lines
Quoted 22 lines
Quoted 13 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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.

[...]

Quoted 6 lines

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

author
#4 lore raw
Quoted 28 lines · Leonardo Bras
Quoted 27 lines · Usama Arif
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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].

Quoted 6 lines
Quoted 4 lines

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.

Ah sorry, maybe I am confused.

So the reason I am saying I only did 2x is because Marc asked for a capacity that covers more than one map per top-up (so that ruled out 1x), and using 2x didnt show any regression for block splitting and faults, so I didnt see a reason to test for 4x as thats just allocating more memory without any benefits.

Quoted 2 lines

I am not 100% sure Marc asked for a multiple like that, but even if it was,

Ah no not at all, Marc just said he wants more than one map per top-up.

2x was something I did. I asked if 2x made sense in [1].

Quoted 4 lines

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.

Sure! I have attached it below.

Quoted 45 lines
Quoted 38 lines
Quoted 31 lines
Quoted 21 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 6 lines

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.

Quoted 6 lines

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.

Sure, the tests are at [2] and [3]. You need [4] if you want to iterate quickly.

I ran them on one Grace host (72 CPU). The minimum is the 3 pages with 4K and 2 with 16K and 64K, so the capacities are:

40 4x 2x 1x

  4K           40    12     6     3
  16K, 64K     40     8     4     2

Each run creates 16 VMs with 4 vCPUs, one VM at a time. Each vCPU times its KVM_RUN with CLOCK_MONOTONIC until the guest exits through MMIO at the end of the phase, and the programs check that every store landed. Each cell is the median of the 16 VMs, given as the range over 3 rounds.

split kvmsplit. Each vCPU writes one page of every huge page in its
1 GiB share, so stage-2 maps them as blocks. Then dirty logging is enabled (no eager splitting) and each vCPU writes another page of every block, so each write splits a block and allocates a table: 512 2M blocks per vCPU with 4K, 32 32M blocks with 16K, 2 512M blocks with 64K. Time per VM: the slowest vCPU's split.
sparse kvmsparse. 8000 one-page memslots per vCPU, one last-level
table span apart, so every fault takes a table and the memcache refills as often as it can. Time per VM: from the first KVM_RUN to the last vCPU's exit.

  ms per VM          40           4x           2x           1x
  4K   split         2.59-2.82    2.60-2.68    2.60-2.66    2.65-2.68
       sparse        31.4-32.5    30.4-31.5    30.2-31.9    30.3-32.1
  16K  split         0.157-0.184  0.197-0.205  0.198-0.206  0.197-0.205
       sparse        36.1-37.4    36.0-36.6    35.6-37.9    35.6-36.5
  64K  split         0.025-0.026  0.026        0.026-0.027  0.028-0.029
       sparse        51.9-52.3    50.2-51.6    49.8-50.8    50.4-52.4

The capacity makes no difference beyond the noise, down to 1x.

Quoted 34 lines
Quoted 22 lines
Quoted 22 lines
Quoted 13 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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.

Quoted 9 lines

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.

Agree that a host-wide value would be nicer: it drops the struct member and the reasoning about when the shadow stage-2s are initialised, and it can be compile-time. SL0 = 0b10 isn't the deepest stage-2 with 16K pages, though: with LPA2 and a 52-bit IPA it has 4 levels (SL0 = 0b11).

How about we size the cache for the deepest stage-2 KVM can create, the one for a 52-bit IPA? No stage-2 can be deeper: neither a VM's IPA nor the shadow stage-2s' can exceed kvm_ipa_limit, and kvm_ipa_limit is at most 52 bits.

Something like below maybe?

arch/arm64/kvm/mmu.c +6 -3
diff -urN a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
@@ -1289,6 +1289,9 @@
 				    kvm_host_pa, mc);
 }
 
+/* Two maps on the deepest stage-2, which has a 52-bit IPA */
+#define KVM_MMU_CACHE_CAPACITY	(2 * (stage2_pgtable_levels(52) - 1))
+
 /**
  * kvm_phys_addr_ioremap - map a device range to guest IPA
  *
@@ -1317,8 +1320,8 @@
 	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_MMU_CACHE_CAPACITY,
+						   kvm_mmu_cache_min_pages(mmu));
 		if (ret)
 			break;
 
@@ -1672,7 +1675,7 @@
 	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, KVM_MMU_CACHE_CAPACITY, min_pages);
 
 	return topup_hyp_memcache(memcache, min_pages);
 }

That's 6 with 4K and 16K pages and 4 with 64K. VMs whose stage-2 has fewer levels keep a few more pages per vCPU than this current revision though.

Quoted 10 lines

[...]

Quoted 6 lines

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?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Quoted 5 lines

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.

Any multiplier hits it, because the VM's stage-2 and the shadow ones have different kvm_mmu_cache_min_pages(), 2 and 3 there.

Quoted 3 lines

Thanks!
Leo

[1] https://lore.kernel.org/all/[email protected]/
[2] https://gist.github.com/uarif1/8c52fd4c1fc6f82e8fb5eb1a1ff25411
[3] https://gist.github.com/uarif1/43164a1c3767aefff38ab5ed558919e6
[4] https://gist.github.com/uarif1/ca0edf254a28ca1b246a6054c40016e6

#5 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] KVM: arm64: missing Signed-off-by trailer in commit message - [Medium] KVM: arm64: unedited email…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] KVM: arm64: missing Signed-off-by trailer in commit message
- [Medium] KVM: arm64: unedited email reply in changelog and mismatched commit title
- [Low] KVM: arm64: redundant KVM_MMU_CACHE_CAPACITY leaves arch macro stale at 40
--

commit 0cb2b4c3c55d9c290742d9fed15dbc30c21664cb
Author: Usama Arif <[email protected]>

Quoted 199 lines

KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()

Quoted 28 lines · Leonardo Bras
Quoted 27 lines · Usama Arif
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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].

Quoted 6 lines
Quoted 4 lines

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.

Ah sorry, maybe I am confused.

So the reason I am saying I only did 2x is because Marc asked for a capacity that covers more than one map per top-up (so that ruled out 1x), and using 2x didnt show any regression for block splitting and faults, so I didnt see a reason to test for 4x as thats just allocating more memory without any benefits.

Quoted 2 lines

I am not 100% sure Marc asked for a multiple like that, but even if it was,

Ah no not at all, Marc just said he wants more than one map per top-up.

2x was something I did. I asked if 2x made sense in [1].

Quoted 4 lines

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.

Sure! I have attached it below.

Quoted 45 lines
Quoted 38 lines
Quoted 31 lines
Quoted 21 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 6 lines

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.

Quoted 6 lines

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.

Sure, the tests are at [2] and [3]. You need [4] if you want to iterate quickly.

I ran them on one Grace host (72 CPU). The minimum is the 3 pages with 4K and 2 with 16K and 64K, so the capacities are:

40 4x 2x 1x

  4K           40    12     6     3
  16K, 64K     40     8     4     2

Each run creates 16 VMs with 4 vCPUs, one VM at a time. Each vCPU times its KVM_RUN with CLOCK_MONOTONIC until the guest exits through MMIO at the end of the phase, and the programs check that every store landed. Each cell is the median of the 16 VMs, given as the range over 3 rounds.

split kvmsplit. Each vCPU writes one page of every huge page in its
1 GiB share, so stage-2 maps them as blocks. Then dirty logging is enabled (no eager splitting) and each vCPU writes another page of every block, so each write splits a block and allocates a table: 512 2M blocks per vCPU with 4K, 32 32M blocks with 16K, 2 512M blocks with 64K. Time per VM: the slowest vCPU's split.
sparse kvmsparse. 8000 one-page memslots per vCPU, one last-level
table span apart, so every fault takes a table and the memcache refills as often as it can. Time per VM: from the first KVM_RUN to the last vCPU's exit.

  ms per VM          40           4x           2x           1x
  4K   split         2.59-2.82    2.60-2.68    2.60-2.66    2.65-2.68
       sparse        31.4-32.5    30.4-31.5    30.2-31.9    30.3-32.1
  16K  split         0.157-0.184  0.197-0.205  0.198-0.206  0.197-0.205
       sparse        36.1-37.4    36.0-36.6    35.6-37.9    35.6-36.5
  64K  split         0.025-0.026  0.026        0.026-0.027  0.028-0.029
       sparse        51.9-52.3    50.2-51.6    49.8-50.8    50.4-52.4

The capacity makes no difference beyond the noise, down to 1x.

Quoted 34 lines
Quoted 22 lines
Quoted 22 lines
Quoted 13 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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.

Quoted 9 lines

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.

Agree that a host-wide value would be nicer: it drops the struct member and the reasoning about when the shadow stage-2s are initialised, and it can be compile-time. SL0 = 0b10 isn't the deepest stage-2 with 16K pages, though: with LPA2 and a 52-bit IPA it has 4 levels (SL0 = 0b11).

Quoted 6 lines

How about we size the cache for the deepest stage-2 KVM can create, the one for a 52-bit IPA? No stage-2 can be deeper: neither a VM's IPA nor the shadow stage-2s' can exceed kvm_ipa_limit, and kvm_ipa_limit is at most 52 bits.

Something like below maybe?

[Severity: High]
The commit message is missing a top-level Signed-off-by: trailer from the patch author (the only Signed-off-by: line is inside a nested email quote from an earlier version).

[Severity: Medium]
The commit message is an unedited mailing-list reply containing quoted thread history and an obsolete inline diff. In addition, the commit subject and quoted changelog still describe sizing the cache per-VM from kvm_mmu_cache_min_pages(), whereas the patch sizes it from a host-wide 52-bit IPA compile-time constant (2 * (stage2_pgtable_levels(52) - 1)).

Quoted 26 lines
arch/arm64/kvm/mmu.c +5 -2
index 1db10d8d6db73..f95131280c04f 100644
@@ -1287,6 +1287,9 @@ int topup_hyp_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages)
 				    kvm_host_pa, mc);
 }
 
+/* Two maps on the deepest stage-2, which has a 52-bit IPA */
+#define KVM_MMU_CACHE_CAPACITY	(2 * (stage2_pgtable_levels(52) - 1))
+
 /**
  * kvm_phys_addr_ioremap - map a device range to guest IPA
  *
@@ -1315,8 +1318,8 @@ 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_MMU_CACHE_CAPACITY,
+						   kvm_mmu_cache_min_pages(mmu));
 		if (ret)
 			break;
 

arch/arm64/kvm/mmu.c static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache) · line 1673

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1664,7 +1667,7 @@ static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache)
 	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, KVM_MMU_CACHE_CAPACITY, min_pages);
 
 	return topup_hyp_memcache(memcache, min_pages);
 }

[Severity: Low]
This isn't a bug, but now that the capacity is a host-wide compile-time constant rather than a per-VM value, could KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE in arch/arm64/include/asm/kvm_types.h be updated instead of adding KVM_MMU_CACHE_CAPACITY? kvm_phys_addr_ioremap() and topup_mmu_memcache() are the only arm64 callers of kvm_mmu_topup_memory_cache(), so bypassing it leaves KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE unused at its stale value of 40.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#6 lore raw
In reply to Usama Arif
Quoted 38 lines · Usama Arif
Quoted 28 lines · Leonardo Bras
Quoted 27 lines · Usama Arif
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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].

Quoted 6 lines
Quoted 4 lines

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.

Quoted 8 lines

Ah sorry, maybe I am confused.

So the reason I am saying I only did 2x is because Marc asked for a capacity that covers more than one map per top-up (so that ruled out 1x), and using 2x didnt show any regression for block splitting and faults, so I didnt see a reason to test for 4x as thats just allocating more memory without any benefits.

Okay, but if he was worried about performance regression, I think it would be nice to have the results for 1x as well, just to evaluate how much of a regression that is.

Quoted 102 lines
Quoted 2 lines

I am not 100% sure Marc asked for a multiple like that, but even if it was,

Ah no not at all, Marc just said he wants more than one map per top-up.

2x was something I did. I asked if 2x made sense in [1].

Quoted 4 lines

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.

Sure! I have attached it below.

Quoted 45 lines
Quoted 38 lines
Quoted 31 lines
Quoted 21 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 6 lines

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.

Quoted 6 lines

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.

Sure, the tests are at [2] and [3]. You need [4] if you want to iterate quickly.

I ran them on one Grace host (72 CPU). The minimum is the 3 pages with 4K and 2 with 16K and 64K, so the capacities are:

40 4x 2x 1x

  4K           40    12     6     3
  16K, 64K     40     8     4     2

Each run creates 16 VMs with 4 vCPUs, one VM at a time. Each vCPU times its KVM_RUN with CLOCK_MONOTONIC until the guest exits through MMIO at the end of the phase, and the programs check that every store landed. Each cell is the median of the 16 VMs, given as the range over 3 rounds.

split kvmsplit. Each vCPU writes one page of every huge page in its
1 GiB share, so stage-2 maps them as blocks. Then dirty logging is enabled (no eager splitting) and each vCPU writes another page of every block, so each write splits a block and allocates a table: 512 2M blocks per vCPU with 4K, 32 32M blocks with 16K, 2 512M blocks with 64K. Time per VM: the slowest vCPU's split.
sparse kvmsparse. 8000 one-page memslots per vCPU, one last-level
table span apart, so every fault takes a table and the memcache refills as often as it can. Time per VM: from the first KVM_RUN to the last vCPU's exit.

  ms per VM          40           4x           2x           1x
  4K   split         2.59-2.82    2.60-2.68    2.60-2.66    2.65-2.68
       sparse        31.4-32.5    30.4-31.5    30.2-31.9    30.3-32.1
  16K  split         0.157-0.184  0.197-0.205  0.198-0.206  0.197-0.205
       sparse        36.1-37.4    36.0-36.6    35.6-37.9    35.6-36.5
  64K  split         0.025-0.026  0.026        0.026-0.027  0.028-0.029
       sparse        51.9-52.3    50.2-51.6    49.8-50.8    50.4-52.4
Quoted 2 lines

The capacity makes no difference beyond the noise, down to 1x.

Cool!
Just in case, I would also retest 16k split for 40, as it's the only one that differs significantly from the others.

I also recommend repeating the tests for a guest memory backed by 4k level-1 blocks (1G), as it would stress a bit more the cache.

Quoted 51 lines
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
Quoted 14 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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.

Quoted 9 lines

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.

Agree that a host-wide value would be nicer: it drops the struct member and the reasoning about when the shadow stage-2s are initialised, and it can be compile-time. SL0 = 0b10 isn't the deepest stage-2 with 16K pages, though: with LPA2 and a 52-bit IPA it has 4 levels (SL0 = 0b11).

Quoted 4 lines

How about we size the cache for the deepest stage-2 KVM can create, the one for a 52-bit IPA? No stage-2 can be deeper: neither a VM's IPA nor the shadow stage-2s' can exceed kvm_ipa_limit, and kvm_ipa_limit is at most 52 bits.

I think it makes sense, but using plain 52 here would not be great if this get to increase in the future. What if we use get_kvm_ipa_limit() instead? It's not built-time, but it's __ro_after_init.

arch/arm64/kvm/mmu.c line 1294

Quoted 13 lines

Something like below maybe?

diff -urN a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c

arch/arm64/kvm/mmu.c +3 -0
@@ -1289,6 +1289,9 @@
 				    kvm_host_pa, mc);
 }
 
+/* Two maps on the deepest stage-2, which has a 52-bit IPA */
+#define KVM_MMU_CACHE_CAPACITY	(2 * (stage2_pgtable_levels(52) - 1))
+

If your results show the same performance for 1x, why we care about using 2x here?

Maybe something like
#define KVM_MMU_CACHE_CAPACITY() (stage2_pgtable_levels(get_kvm_ipa_limit()) - 1)

Adding a multiplier here would be ok if it has better performance.

Quoted 15 lines
arch/arm64/kvm/mmu.c +2 -2
 /**
  * kvm_phys_addr_ioremap - map a device range to guest IPA
  *
@@ -1317,8 +1320,8 @@
 	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_MMU_CACHE_CAPACITY,
+						   kvm_mmu_cache_min_pages(mmu));
 		if (ret)
 			break;
 

arch/arm64/kvm/mmu.c line 1681

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1672,7 +1675,7 @@
 	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, KVM_MMU_CACHE_CAPACITY, min_pages);
 
 	return topup_hyp_memcache(memcache, min_pages);
 }

Yeah, that's the idea :)

Quoted 14 lines

That's 6 with 4K and 16K pages and 4 with 64K. VMs whose stage-2 has fewer levels keep a few more pages per vCPU than this current revision though.

Quoted 8 lines

[...]

Quoted 6 lines

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.

Quoted 9 lines
Quoted 2 lines

Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Ah, I see your point.
The issue is that QEMU TCG is not really great for gathering performance numbers :(

I can try running the tests for you in a NV2-enabled machine, if that's ok :)

Quoted 8 lines
Quoted 5 lines

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.

Any multiplier hits it, because the VM's stage-2 and the shadow ones have different kvm_mmu_cache_min_pages(), 2 and 3 there.

All right, let's instead use the stage2_pgtable_levels approach from above, with

Quoted 11 lines
Quoted 4 lines

Thanks!
Leo

[1] https://lore.kernel.org/all/[email protected]/
[2] https://gist.github.com/uarif1/8c52fd4c1fc6f82e8fb5eb1a1ff25411
[3] https://gist.github.com/uarif1/43164a1c3767aefff38ab5ed558919e6
[4] https://gist.github.com/uarif1/ca0edf254a28ca1b246a6054c40016e6

Thanks!
Leo

#7 lore raw
Quoted 1 line · Leonardo Bras

On Mon, Nov 30, 2026 at 08:05:32PM +0000, Usama Arif wrote:

[...]

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

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.

Quoted 9 lines
Quoted 2 lines

Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Quoted 7 lines

Ah, I see your point.
The issue is that QEMU TCG is not really great for gathering performance numbers :(

I can try running the tests for you in a NV2-enabled machine, if that's ok :)

Humm, but again, why do you need NV2? That path is tested on both user_mem_abort and gmem_abort, it could easily be tested by replacing the host kernel.

Do you have any limitation replacing the host kernel?

Thanks!
Leo

author
#10 lore raw
In reply to Leonardo Bras
Quoted 154 lines · On Tue, 1 Dec 2026 12:46:22 +0000 Leonardo Bras <[email protected]> wrote:
Quoted 38 lines · Usama Arif
Quoted 28 lines · Leonardo Bras
Quoted 27 lines · Usama Arif
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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].

Quoted 6 lines
Quoted 4 lines

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.

Quoted 8 lines

Ah sorry, maybe I am confused.

So the reason I am saying I only did 2x is because Marc asked for a capacity that covers more than one map per top-up (so that ruled out 1x), and using 2x didnt show any regression for block splitting and faults, so I didnt see a reason to test for 4x as thats just allocating more memory without any benefits.

Okay, but if he was worried about performance regression, I think it would be nice to have the results for 1x as well, just to evaluate how much of a regression that is.

Quoted 102 lines
Quoted 2 lines

I am not 100% sure Marc asked for a multiple like that, but even if it was,

Ah no not at all, Marc just said he wants more than one map per top-up.

2x was something I did. I asked if 2x made sense in [1].

Quoted 4 lines

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.

Sure! I have attached it below.

Quoted 45 lines
Quoted 38 lines
Quoted 31 lines
Quoted 21 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 6 lines

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.

Quoted 6 lines

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.

Sure, the tests are at [2] and [3]. You need [4] if you want to iterate quickly.

I ran them on one Grace host (72 CPU). The minimum is the 3 pages with 4K and 2 with 16K and 64K, so the capacities are:

40 4x 2x 1x

  4K           40    12     6     3
  16K, 64K     40     8     4     2

Each run creates 16 VMs with 4 vCPUs, one VM at a time. Each vCPU times its KVM_RUN with CLOCK_MONOTONIC until the guest exits through MMIO at the end of the phase, and the programs check that every store landed. Each cell is the median of the 16 VMs, given as the range over 3 rounds.

split kvmsplit. Each vCPU writes one page of every huge page in its
1 GiB share, so stage-2 maps them as blocks. Then dirty logging is enabled (no eager splitting) and each vCPU writes another page of every block, so each write splits a block and allocates a table: 512 2M blocks per vCPU with 4K, 32 32M blocks with 16K, 2 512M blocks with 64K. Time per VM: the slowest vCPU's split.
sparse kvmsparse. 8000 one-page memslots per vCPU, one last-level
table span apart, so every fault takes a table and the memcache refills as often as it can. Time per VM: from the first KVM_RUN to the last vCPU's exit.

  ms per VM          40           4x           2x           1x
  4K   split         2.59-2.82    2.60-2.68    2.60-2.66    2.65-2.68
       sparse        31.4-32.5    30.4-31.5    30.2-31.9    30.3-32.1
  16K  split         0.157-0.184  0.197-0.205  0.198-0.206  0.197-0.205
       sparse        36.1-37.4    36.0-36.6    35.6-37.9    35.6-36.5
  64K  split         0.025-0.026  0.026        0.026-0.027  0.028-0.029
       sparse        51.9-52.3    50.2-51.6    49.8-50.8    50.4-52.4
Quoted 6 lines
Quoted 2 lines

The capacity makes no difference beyond the noise, down to 1x.

Cool!
Just in case, I would also retest 16k split for 40, as it's the only one that differs significantly from the others.

I tried again and it reproduces on a separate boot with 128 blocks per vCPU (0.71-0.76 ms per VM with 40, 0.75-0.79 with 1x, 2x and 4x), so very slightly higher. but nothing is lost by going below 40. With 40, the split's tables had already been allocated while the blocks were mapped, so the split only used
them: the allocations moved earlier, they didn't go away. So we should still lower it below 40.

Quoted 4 lines

I also recommend repeating the tests for a guest memory backed by 4k level-1 blocks (1G), as it would stress a bit more the cache.

Thanks! I tested this now, with one 1G block per vCPU, then dirty logging and a write every 2M. No difference there either:

  ms per VM          40           4x           2x           1x
  4K   1G split      1.47-1.51    1.50         1.51-1.53    1.49-1.51

Only the first write takes two tables, though. But with 1x the memcache already refills after every table it hands out, in all the tests.

Quoted 51 lines
Quoted 51 lines
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
Quoted 14 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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.

Quoted 9 lines

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.

Agree that a host-wide value would be nicer: it drops the struct member and the reasoning about when the shadow stage-2s are initialised, and it can be compile-time. SL0 = 0b10 isn't the deepest stage-2 with 16K pages, though: with LPA2 and a 52-bit IPA it has 4 levels (SL0 = 0b11).

Quoted 9 lines
Quoted 4 lines

How about we size the cache for the deepest stage-2 KVM can create, the one for a 52-bit IPA? No stage-2 can be deeper: neither a VM's IPA nor the shadow stage-2s' can exceed kvm_ipa_limit, and kvm_ipa_limit is at most 52 bits.

I think it makes sense, but using plain 52 here would not be great if this get to increase in the future. What if we use get_kvm_ipa_limit() instead? It's not built-time, but it's __ro_after_init.

Sure, that also gives hosts with a lower limit a smaller capacity. It needs kvm_get_vtcr()'s minimum of 2 levels, though: with 64K pages and a 32-bit limit, stage2_pgtable_levels() gives 1, so the capacity would be 0 and every top-up would fail. Computed once, after kvm_set_ipa_limit():

	int levels = max_t(int, stage2_pgtable_levels(get_kvm_ipa_limit()), 2);

	kvm_mmu_cache_capacity = 2 * (levels - 1);
Quoted 18 lines
Quoted 8 lines

Something like below maybe?

diff -urN a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c

arch/arm64/kvm/mmu.c line 1294

Quoted 6 lines
arch/arm64/kvm/mmu.c +3 -0
@@ -1289,6 +1289,9 @@
 				    kvm_host_pa, mc);
 }
 
+/* Two maps on the deepest stage-2, which has a 52-bit IPA */
+#define KVM_MMU_CACHE_CAPACITY	(2 * (stage2_pgtable_levels(52) - 1))
+

If your results show the same performance for 1x, why we care about using 2x here?

Quoted 4 lines

Maybe something like
#define KVM_MMU_CACHE_CAPACITY() (stage2_pgtable_levels(get_kvm_ipa_limit()) - 1)

Adding a multiplier here would be ok if it has better performance.

So mainly added a multiplier as I though Marc had something in mind as he said the new implementation "must cover more than a single mapping per top-up" [1]. I dont see a performance difference between 1x, 2x, 4x...

Quoted 64 lines
Quoted 16 lines
arch/arm64/kvm/mmu.c +2 -2
 /**
  * kvm_phys_addr_ioremap - map a device range to guest IPA
  *
@@ -1317,8 +1320,8 @@
 	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_MMU_CACHE_CAPACITY,
+						   kvm_mmu_cache_min_pages(mmu));
 		if (ret)
 			break;
 

arch/arm64/kvm/mmu.c line 1681

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1672,7 +1675,7 @@
 	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, KVM_MMU_CACHE_CAPACITY, min_pages);
 
 	return topup_hyp_memcache(memcache, min_pages);
 }

Yeah, that's the idea :)

Quoted 14 lines

That's 6 with 4K and 16K pages and 4 with 64K. VMs whose stage-2 has fewer levels keep a few more pages per vCPU than this current revision though.

Quoted 8 lines

[...]

Quoted 6 lines

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.

Quoted 9 lines
Quoted 2 lines

Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Ah, I see your point.
The issue is that QEMU TCG is not really great for gathering performance numbers :(

I can try running the tests for you in a NV2-enabled machine, if that's ok :)

Quoted 6 lines
Quoted 5 lines

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.

Quoted 6 lines
Quoted 2 lines

Any multiplier hits it, because the VM's stage-2 and the shadow ones have different kvm_mmu_cache_min_pages(), 2 and 3 there.

All right, let's instead use the stage2_pgtable_levels approach from above, with

This got cut off after "with"..

Would you be ok with something like below as next revision?

arch/arm64/include/asm/kvm_mmu.h +1 -0
index 32392ab..ca2827a 100644
@@ -200,6 +200,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
 phys_addr_t kvm_mmu_get_httbr(void);
 phys_addr_t kvm_get_idmap_vector(void);
 int __init kvm_mmu_init(u32 hyp_va_bits);
+void __init kvm_mmu_init_cache_capacity(void);
 
 static inline void *__kvm_vector_slot2addr(void *base,
 					   enum arm64_hyp_spectre_vector slot)
arch/arm64/kvm/arm.c +2 -0
index 157bf4b..3f5424c 100644
@@ -3278,6 +3278,8 @@ static __init int kvm_arm_init(void)
 	if (err)
 		return err;
 
+	kvm_mmu_init_cache_capacity();
+
 	err = kvm_arm_init_sve();
 	if (err)
 		return err;
arch/arm64/kvm/mmu.c +19 -3
index 042dc53..0432129 100644
@@ -37,6 +37,9 @@ u32 __ro_after_init __hyp_va_bits;
 
 static unsigned long __ro_after_init io_map_base;
 
+/* Capacity of the stage-2 memcaches */
+static int __ro_after_init kvm_mmu_cache_capacity;
+
 #define KVM_PGT_FN(fn)		(!is_protected_kvm_enabled() ? fn : p ## fn)
 
 static int kvm_vm_init_vm_s2_ops(struct kvm *kvm);
@@ -994,6 +997,18 @@ static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
 	return 0;
 }
 
+/*
+ * Cover the deepest stage-2 the host supports, which is the size
+ * of the shadow stage-2s that share a vCPU's memcache with the VM's stage-2.
+ * Like kvm_get_vtcr(), use at least 2 levels.
+ */
+void __init kvm_mmu_init_cache_capacity(void)
+{
+	int levels = max_t(int, stage2_pgtable_levels(get_kvm_ipa_limit()), 2);
+
+	kvm_mmu_cache_capacity = levels - 1;
+}
+
 /*
  * Assume that @pgt is valid and unlinked from the KVM MMU to free the
  * page-table without taking the kvm_mmu_lock and without performing any
@@ -1317,8 +1332,8 @@ 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_mmu_cache_capacity,
+						   kvm_mmu_cache_min_pages(mmu));
 		if (ret)
 			break;
 
@@ -1672,7 +1687,8 @@ static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache)
 	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, kvm_mmu_cache_capacity,
+						    min_pages);
 
 	return topup_hyp_memcache(memcache, min_pages);
 }
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Thanks!
Leo

Quoted 8 lines
Quoted 5 lines

[1] https://lore.kernel.org/all/[email protected]/
[2] https://gist.github.com/uarif1/8c52fd4c1fc6f82e8fb5eb1a1ff25411
[3] https://gist.github.com/uarif1/43164a1c3767aefff38ab5ed558919e6
[4] https://gist.github.com/uarif1/ca0edf254a28ca1b246a6054c40016e6

Thanks!
Leo

[1] https://lore.kernel.org/all/[email protected]/

author
#8 lore raw
In reply to Leonardo Bras
Quoted 27 lines · On Tue, 1 Dec 2026 18:19:42 +0000 Leonardo Bras <[email protected]> wrote:
Quoted 1 line · Leonardo Bras

On Mon, Nov 30, 2026 at 08:05:32PM +0000, Usama Arif wrote:

[...]

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

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.

Quoted 9 lines
Quoted 2 lines

Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Quoted 7 lines

Ah, I see your point.
The issue is that QEMU TCG is not really great for gathering performance numbers :(

I can try running the tests for you in a NV2-enabled machine, if that's ok :)

Quoted 6 lines

Humm, but again, why do you need NV2? That path is tested on both user_mem_abort and gmem_abort, it could easily be tested by replacing the host kernel.

Do you have any limitation replacing the host kernel?

(Replying here instead of the previous email for this part of the thread.)

Ah no limitation, and all the numbers are from a patched host kernel on the Grace host. Only the NV tests ran under TCG, and they only check for warnings. None of the performance numbers are from TCG.

It's the same code, but only with NV does the vCPU's memcache also feed shadow stage-2s, which can be deeper than the VM's own stage-2. That's why a multiple of the VM's kvm_mmu_cache_min_pages() hits the WARN_ON_ONCE() with NV. IMO performance numbers are needed from NV for this, but if they are, could you help as I dont have hardware for it.

Thanks!
Usama

Quoted 3 lines

Thanks!
Leo

#11 lore raw
In reply to Usama Arif
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] KVM: arm64: missing Signed-off-by trailer in commit message - [Medium] KVM: arm64: raw email thread…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] KVM: arm64: missing Signed-off-by trailer in commit message
- [Medium] KVM: arm64: raw email thread in changelog and mismatch with code
--

commit 0dff0c4a1daf3eb6dfab9d765a11f06387e84824
Author: Usama Arif <[email protected]>

Quoted 352 lines

KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages()

Quoted 154 lines · On Tue, 1 Dec 2026 12:46:22 +0000 Leonardo Bras <[email protected]> wrote:
Quoted 38 lines · Usama Arif
Quoted 28 lines · Leonardo Bras
Quoted 27 lines · Usama Arif
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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].

Quoted 6 lines
Quoted 4 lines

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.

Quoted 8 lines

Ah sorry, maybe I am confused.

So the reason I am saying I only did 2x is because Marc asked for a capacity that covers more than one map per top-up (so that ruled out 1x), and using 2x didnt show any regression for block splitting and faults, so I didnt see a reason to test for 4x as thats just allocating more memory without any benefits.

Okay, but if he was worried about performance regression, I think it would be nice to have the results for 1x as well, just to evaluate how much of a regression that is.

Quoted 102 lines
Quoted 2 lines

I am not 100% sure Marc asked for a multiple like that, but even if it was,

Ah no not at all, Marc just said he wants more than one map per top-up.

2x was something I did. I asked if 2x made sense in [1].

Quoted 4 lines

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.

Sure! I have attached it below.

Quoted 45 lines
Quoted 38 lines
Quoted 31 lines
Quoted 21 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 6 lines

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.

Quoted 6 lines

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.

Sure, the tests are at [2] and [3]. You need [4] if you want to iterate quickly.

I ran them on one Grace host (72 CPU). The minimum is the 3 pages with 4K and 2 with 16K and 64K, so the capacities are:

40 4x 2x 1x

  4K           40    12     6     3
  16K, 64K     40     8     4     2

Each run creates 16 VMs with 4 vCPUs, one VM at a time. Each vCPU times its KVM_RUN with CLOCK_MONOTONIC until the guest exits through MMIO at the end of the phase, and the programs check that every store landed. Each cell is the median of the 16 VMs, given as the range over 3 rounds.

split kvmsplit. Each vCPU writes one page of every huge page in its
1 GiB share, so stage-2 maps them as blocks. Then dirty logging is enabled (no eager splitting) and each vCPU writes another page of every block, so each write splits a block and allocates a table: 512 2M blocks per vCPU with 4K, 32 32M blocks with 16K, 2 512M blocks with 64K. Time per VM: the slowest vCPU's split.
sparse kvmsparse. 8000 one-page memslots per vCPU, one last-level
table span apart, so every fault takes a table and the memcache refills as often as it can. Time per VM: from the first KVM_RUN to the last vCPU's exit.

  ms per VM          40           4x           2x           1x
  4K   split         2.59-2.82    2.60-2.68    2.60-2.66    2.65-2.68
       sparse        31.4-32.5    30.4-31.5    30.2-31.9    30.3-32.1
  16K  split         0.157-0.184  0.197-0.205  0.198-0.206  0.197-0.205
       sparse        36.1-37.4    36.0-36.6    35.6-37.9    35.6-36.5
  64K  split         0.025-0.026  0.026        0.026-0.027  0.028-0.029
       sparse        51.9-52.3    50.2-51.6    49.8-50.8    50.4-52.4
Quoted 6 lines
Quoted 2 lines

The capacity makes no difference beyond the noise, down to 1x.

Cool!
Just in case, I would also retest 16k split for 40, as it's the only one that differs significantly from the others.

I tried again and it reproduces on a separate boot with 128 blocks per vCPU (0.71-0.76 ms per VM with 40, 0.75-0.79 with 1x, 2x and 4x), so very slightly higher. but nothing is lost by going below 40. With 40, the split's tables had already been allocated while the blocks were mapped, so the split only used
them: the allocations moved earlier, they didn't go away. So we should still lower it below 40.

Quoted 4 lines

I also recommend repeating the tests for a guest memory backed by 4k level-1 blocks (1G), as it would stress a bit more the cache.

Thanks! I tested this now, with one 1G block per vCPU, then dirty logging and a write every 2M. No difference there either:

  ms per VM          40           4x           2x           1x
  4K   1G split      1.47-1.51    1.50         1.51-1.53    1.49-1.51

Only the first write takes two tables, though. But with 1x the memcache already refills after every table it hands out, in all the tests.

Quoted 51 lines
Quoted 51 lines
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
Quoted 14 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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.

Quoted 9 lines

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.

Agree that a host-wide value would be nicer: it drops the struct member and the reasoning about when the shadow stage-2s are initialised, and it can be compile-time. SL0 = 0b10 isn't the deepest stage-2 with 16K pages, though: with LPA2 and a 52-bit IPA it has 4 levels (SL0 = 0b11).

Quoted 9 lines
Quoted 4 lines

How about we size the cache for the deepest stage-2 KVM can create, the one for a 52-bit IPA? No stage-2 can be deeper: neither a VM's IPA nor the shadow stage-2s' can exceed kvm_ipa_limit, and kvm_ipa_limit is at most 52 bits.

I think it makes sense, but using plain 52 here would not be great if this get to increase in the future. What if we use get_kvm_ipa_limit() instead? It's not built-time, but it's __ro_after_init.

Sure, that also gives hosts with a lower limit a smaller capacity. It needs kvm_get_vtcr()'s minimum of 2 levels, though: with 64K pages and a 32-bit limit, stage2_pgtable_levels() gives 1, so the capacity would be 0 and every top-up would fail. Computed once, after kvm_set_ipa_limit():

        int levels = max_t(int, stage2_pgtable_levels(get_kvm_ipa_limit()), 2);

        kvm_mmu_cache_capacity = 2 * (levels - 1);
Quoted 18 lines
Quoted 8 lines

Something like below maybe?

diff -urN a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c

arch/arm64/kvm/mmu.c 2026-11-30 11:39:13.490652336 -0800 line 1294

Quoted 6 lines
arch/arm64/kvm/mmu.c 2026-11-30 11:39:13.490652336 -0800 renamed from arch/arm64/kvm/mmu.c 2026-11-17 16:28:54.000000000 -0800 +3 -0
@@ -1289,6 +1289,9 @@
                                kvm_host_pa, mc);
 }
 
+/* Two maps on the deepest stage-2, which has a 52-bit IPA */
+#define KVM_MMU_CACHE_CAPACITY     (2 * (stage2_pgtable_levels(52) - 1))
+

If your results show the same performance for 1x, why we care about using 2x here?

Quoted 4 lines

Maybe something like
#define KVM_MMU_CACHE_CAPACITY() (stage2_pgtable_levels(get_kvm_ipa_limit()) - 1)

Adding a multiplier here would be ok if it has better performance.

So mainly added a multiplier as I though Marc had something in mind as he said the new implementation "must cover more than a single mapping per top-up" [1]. I dont see a performance difference between 1x, 2x, 4x...

Quoted 67 lines
Quoted 16 lines
arch/arm64/kvm/mmu.c 2026-11-30 11:39:13.490652336 -0800 renamed from arch/arm64/kvm/mmu.c 2026-11-17 16:28:54.000000000 -0800 +2 -2
 /**
  * kvm_phys_addr_ioremap - map a device range to guest IPA
  *
@@ -1317,8 +1320,8 @@
    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_MMU_CACHE_CAPACITY,
+                                              kvm_mmu_cache_min_pages(mmu));
            if (ret)
                    break;
 

arch/arm64/kvm/mmu.c 2026-11-30 11:39:13.490652336 -0800 line 1681

Quoted 8 lines
arch/arm64/kvm/mmu.c 2026-11-30 11:39:13.490652336 -0800 renamed from arch/arm64/kvm/mmu.c 2026-11-17 16:28:54.000000000 -0800 +1 -1
@@ -1672,7 +1675,7 @@
    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, KVM_MMU_CACHE_CAPACITY, min_pages);
 
    return topup_hyp_memcache(memcache, min_pages);
 }

Yeah, that's the idea :)

Quoted 14 lines

That's 6 with 4K and 16K pages and 4 with 64K. VMs whose stage-2 has fewer levels keep a few more pages per vCPU than this current revision though.

Quoted 8 lines

[...]

Quoted 6 lines

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.

Quoted 9 lines
Quoted 2 lines

Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Ah, I see your point.
The issue is that QEMU TCG is not really great for gathering performance numbers :(

I can try running the tests for you in a NV2-enabled machine, if that's ok :)

Quoted 8 lines
Quoted 5 lines

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.

Any multiplier hits it, because the VM's stage-2 and the shadow ones have different kvm_mmu_cache_min_pages(), 2 and 3 there.

Quoted 7 lines
Quoted 3 lines

All right, let's instead use the stage2_pgtable_levels approach from above, with

This got cut off after "with"..

Would you be ok with something like below as next revision?

[Severity: High]
The commit message is missing a top-level Signed-off-by: trailer (the only one present is nested inside the quoted email thread from an earlier revision).

[Severity: Medium]
The commit message body is a raw mailing-list reply thread containing nested quotes and obsolete inline diffs rather than a standalone changelog, and both the subject line and the reply text mismatch the implementation:

arch/arm64/kvm/mmu.c:kvm_mmu_init_cache_capacity() {
	int levels = max_t(int, stage2_pgtable_levels(get_kvm_ipa_limit()), 2);

	kvm_mmu_cache_capacity = levels - 1;
}

Rather than sizing the cache per-VM from kvm_mmu_cache_min_pages() or setting kvm_mmu_cache_capacity = 2 * (levels - 1) as shown in the reply text, the code computes a host-wide kvm_mmu_cache_capacity of levels - 1 once from stage2_pgtable_levels(get_kvm_ipa_limit()).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#9 lore raw
In reply to Usama Arif
Quoted 42 lines · Usama Arif
Quoted 27 lines · On Tue, 1 Dec 2026 18:19:42 +0000 Leonardo Bras <[email protected]> wrote:
Quoted 1 line · Leonardo Bras

On Mon, Nov 30, 2026 at 08:05:32PM +0000, Usama Arif wrote:

[...]

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

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.

Quoted 9 lines
Quoted 2 lines

Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Quoted 7 lines

Ah, I see your point.
The issue is that QEMU TCG is not really great for gathering performance numbers :(

I can try running the tests for you in a NV2-enabled machine, if that's ok :)

Quoted 6 lines

Humm, but again, why do you need NV2? That path is tested on both user_mem_abort and gmem_abort, it could easily be tested by replacing the host kernel.

Do you have any limitation replacing the host kernel?

(Replying here instead of the previous email for this part of the thread.)

Ah no limitation, and all the numbers are from a patched host kernel on the Grace host. Only the NV tests ran under TCG, and they only check for warnings. None of the performance numbers are from TCG.

Quoted 7 lines

It's the same code, but only with NV does the vCPU's memcache also feed shadow stage-2s, which can be deeper than the VM's own stage-2. That's why a multiple of the VM's kvm_mmu_cache_min_pages() hits the WARN_ON_ONCE() with NV. IMO performance numbers are needed from NV for this, but if they are, could you help as I dont have hardware for it.

Ah, I get it now.
I think as long as your non-NV perf numbers are from real HW, it should be fine. But sure, I can test it if it's relevant to anyone.

Thanks!
Leo

#12 lore raw
In reply to Usama Arif
Quoted 350 lines · Usama Arif
Quoted 154 lines · On Tue, 1 Dec 2026 12:46:22 +0000 Leonardo Bras <[email protected]> wrote:
Quoted 38 lines · Usama Arif
Quoted 28 lines · Leonardo Bras
Quoted 27 lines · Usama Arif
Quoted 11 lines · Leonardo Bras
Quoted 10 lines · 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.

Quoted 9 lines
Quoted 2 lines

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].

Quoted 6 lines
Quoted 4 lines

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.

Quoted 8 lines

Ah sorry, maybe I am confused.

So the reason I am saying I only did 2x is because Marc asked for a capacity that covers more than one map per top-up (so that ruled out 1x), and using 2x didnt show any regression for block splitting and faults, so I didnt see a reason to test for 4x as thats just allocating more memory without any benefits.

Okay, but if he was worried about performance regression, I think it would be nice to have the results for 1x as well, just to evaluate how much of a regression that is.

Quoted 102 lines
Quoted 2 lines

I am not 100% sure Marc asked for a multiple like that, but even if it was,

Ah no not at all, Marc just said he wants more than one map per top-up.

2x was something I did. I asked if 2x made sense in [1].

Quoted 4 lines

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.

Sure! I have attached it below.

Quoted 45 lines
Quoted 38 lines
Quoted 31 lines
Quoted 21 lines

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
Quoted 9 lines

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
Quoted 6 lines

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.

Quoted 6 lines

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.

Quoted 6 lines

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.

Sure, the tests are at [2] and [3]. You need [4] if you want to iterate quickly.

I ran them on one Grace host (72 CPU). The minimum is the 3 pages with 4K and 2 with 16K and 64K, so the capacities are:

40 4x 2x 1x

  4K           40    12     6     3
  16K, 64K     40     8     4     2

Each run creates 16 VMs with 4 vCPUs, one VM at a time. Each vCPU times its KVM_RUN with CLOCK_MONOTONIC until the guest exits through MMIO at the end of the phase, and the programs check that every store landed. Each cell is the median of the 16 VMs, given as the range over 3 rounds.

split kvmsplit. Each vCPU writes one page of every huge page in its
1 GiB share, so stage-2 maps them as blocks. Then dirty logging is enabled (no eager splitting) and each vCPU writes another page of every block, so each write splits a block and allocates a table: 512 2M blocks per vCPU with 4K, 32 32M blocks with 16K, 2 512M blocks with 64K. Time per VM: the slowest vCPU's split.
sparse kvmsparse. 8000 one-page memslots per vCPU, one last-level
table span apart, so every fault takes a table and the memcache refills as often as it can. Time per VM: from the first KVM_RUN to the last vCPU's exit.

  ms per VM          40           4x           2x           1x
  4K   split         2.59-2.82    2.60-2.68    2.60-2.66    2.65-2.68
       sparse        31.4-32.5    30.4-31.5    30.2-31.9    30.3-32.1
  16K  split         0.157-0.184  0.197-0.205  0.198-0.206  0.197-0.205
       sparse        36.1-37.4    36.0-36.6    35.6-37.9    35.6-36.5
  64K  split         0.025-0.026  0.026        0.026-0.027  0.028-0.029
       sparse        51.9-52.3    50.2-51.6    49.8-50.8    50.4-52.4
Quoted 6 lines
Quoted 2 lines

The capacity makes no difference beyond the noise, down to 1x.

Cool!
Just in case, I would also retest 16k split for 40, as it's the only one that differs significantly from the others.

I tried again and it reproduces on a separate boot with 128 blocks per vCPU (0.71-0.76 ms per VM with 40, 0.75-0.79 with 1x, 2x and 4x), so very slightly higher. but nothing is lost by going below 40. With 40, the split's tables had already been allocated while the blocks were mapped, so the split only used
them: the allocations moved earlier, they didn't go away. So we should still lower it below 40.

Quoted 4 lines

I also recommend repeating the tests for a guest memory backed by 4k level-1 blocks (1G), as it would stress a bit more the cache.

Thanks! I tested this now, with one 1G block per vCPU, then dirty logging and a write every 2M. No difference there either:

  ms per VM          40           4x           2x           1x
  4K   1G split      1.47-1.51    1.50         1.51-1.53    1.49-1.51

Only the first write takes two tables, though. But with 1x the memcache already refills after every table it hands out, in all the tests.

Quoted 51 lines
Quoted 51 lines
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
Quoted 14 lines
---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 97332ffcb1300e..e96c6208686240 100644

arch/arm64/include/asm/kvm_host.h enum kvm_arm_vm_flavor { · line 417

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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.

Quoted 9 lines

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.

Agree that a host-wide value would be nicer: it drops the struct member and the reasoning about when the shadow stage-2s are initialised, and it can be compile-time. SL0 = 0b10 isn't the deepest stage-2 with 16K pages, though: with LPA2 and a 52-bit IPA it has 4 levels (SL0 = 0b11).

Quoted 9 lines
Quoted 4 lines

How about we size the cache for the deepest stage-2 KVM can create, the one for a 52-bit IPA? No stage-2 can be deeper: neither a VM's IPA nor the shadow stage-2s' can exceed kvm_ipa_limit, and kvm_ipa_limit is at most 52 bits.

I think it makes sense, but using plain 52 here would not be great if this get to increase in the future. What if we use get_kvm_ipa_limit() instead? It's not built-time, but it's __ro_after_init.

Sure, that also gives hosts with a lower limit a smaller capacity. It needs kvm_get_vtcr()'s minimum of 2 levels, though: with 64K pages and a 32-bit limit, stage2_pgtable_levels() gives 1, so the capacity would be 0 and every top-up would fail. Computed once, after kvm_set_ipa_limit():

	int levels = max_t(int, stage2_pgtable_levels(get_kvm_ipa_limit()), 2);

	kvm_mmu_cache_capacity = 2 * (levels - 1);
Quoted 18 lines
Quoted 8 lines

Something like below maybe?

diff -urN a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c

arch/arm64/kvm/mmu.c line 1294

Quoted 6 lines
arch/arm64/kvm/mmu.c +3 -0
@@ -1289,6 +1289,9 @@
 				    kvm_host_pa, mc);
 }
 
+/* Two maps on the deepest stage-2, which has a 52-bit IPA */
+#define KVM_MMU_CACHE_CAPACITY	(2 * (stage2_pgtable_levels(52) - 1))
+

If your results show the same performance for 1x, why we care about using 2x here?

Quoted 4 lines

Maybe something like
#define KVM_MMU_CACHE_CAPACITY() (stage2_pgtable_levels(get_kvm_ipa_limit()) - 1)

Adding a multiplier here would be ok if it has better performance.

So mainly added a multiplier as I though Marc had something in mind as he said the new implementation "must cover more than a single mapping per top-up" [1]. I dont see a performance difference between 1x, 2x, 4x...

Quoted 67 lines
Quoted 16 lines
arch/arm64/kvm/mmu.c +2 -2
 /**
  * kvm_phys_addr_ioremap - map a device range to guest IPA
  *
@@ -1317,8 +1320,8 @@
 	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_MMU_CACHE_CAPACITY,
+						   kvm_mmu_cache_min_pages(mmu));
 		if (ret)
 			break;
 

arch/arm64/kvm/mmu.c line 1681

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1672,7 +1675,7 @@
 	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, KVM_MMU_CACHE_CAPACITY, min_pages);
 
 	return topup_hyp_memcache(memcache, min_pages);
 }

Yeah, that's the idea :)

Quoted 14 lines

That's 6 with 4K and 16K pages and 4 with 64K. VMs whose stage-2 has fewer levels keep a few more pages per vCPU than this current revision though.

Quoted 8 lines

[...]

Quoted 6 lines

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.

Quoted 9 lines
Quoted 2 lines

Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host?

QEMU emulates the host. I don't have hardware with FEAT_NV2, so L0 is a 4K kernel booted with kvm-arm.mode=nested in qemu-system-aarch64 with TCG (-cpu max, virtualization=on). Only the NV tests ran that way, the numbers above are from the Grace host.

Ah, I see your point.
The issue is that QEMU TCG is not really great for gathering performance numbers :(

I can try running the tests for you in a NV2-enabled machine, if that's ok :)

Quoted 8 lines
Quoted 5 lines

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.

Any multiplier hits it, because the VM's stage-2 and the shadow ones have different kvm_mmu_cache_min_pages(), 2 and 3 there.

Quoted 7 lines
Quoted 3 lines

All right, let's instead use the stage2_pgtable_levels approach from above, with

This got cut off after "with"..

Ah, sorry. I think I was going to mention a possible multiplier, if performance numbers show any difference.

Quoted 43 lines

Would you be ok with something like below as next revision?

arch/arm64/include/asm/kvm_mmu.h +1 -0
index 32392ab..ca2827a 100644
@@ -200,6 +200,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
 phys_addr_t kvm_mmu_get_httbr(void);
 phys_addr_t kvm_get_idmap_vector(void);
 int __init kvm_mmu_init(u32 hyp_va_bits);
+void __init kvm_mmu_init_cache_capacity(void);
 
 static inline void *__kvm_vector_slot2addr(void *base,
 					   enum arm64_hyp_spectre_vector slot)
arch/arm64/kvm/arm.c +2 -0
index 157bf4b..3f5424c 100644
@@ -3278,6 +3278,8 @@ static __init int kvm_arm_init(void)
 	if (err)
 		return err;
 
+	kvm_mmu_init_cache_capacity();
+
 	err = kvm_arm_init_sve();
 	if (err)
 		return err;
arch/arm64/kvm/mmu.c +3 -0
index 042dc53..0432129 100644
@@ -37,6 +37,9 @@ u32 __ro_after_init __hyp_va_bits;
 
 static unsigned long __ro_after_init io_map_base;
 
+/* Capacity of the stage-2 memcaches */
+static int __ro_after_init kvm_mmu_cache_capacity;
+
 #define KVM_PGT_FN(fn)		(!is_protected_kvm_enabled() ? fn : p ## fn)
 
 static int kvm_vm_init_vm_s2_ops(struct kvm *kvm);

arch/arm64/kvm/mmu.c static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type) · line 1004

Quoted 8 lines
arch/arm64/kvm/mmu.c +5 -0
@@ -994,6 +997,18 @@ static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
 	return 0;
 }
 
+/*
+ * Cover the deepest stage-2 the host supports, which is the size
+ * of the shadow stage-2s that share a vCPU's memcache with the VM's stage-2.
+ * Like kvm_get_vtcr(), use at least 2 levels.
+ */

Ah, nice catch :)

Quoted 22 lines
arch/arm64/kvm/mmu.c +9 -2
+void __init kvm_mmu_init_cache_capacity(void)
+{
+	int levels = max_t(int, stage2_pgtable_levels(get_kvm_ipa_limit()), 2);
+
+	kvm_mmu_cache_capacity = levels - 1;
+}
+
 /*
  * Assume that @pgt is valid and unlinked from the KVM MMU to free the
  * page-table without taking the kvm_mmu_lock and without performing any
@@ -1317,8 +1332,8 @@ 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_mmu_cache_capacity,
+						   kvm_mmu_cache_min_pages(mmu));
 		if (ret)
 			break;
 

arch/arm64/kvm/mmu.c static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache) · line 1694

Quoted 9 lines
arch/arm64/kvm/mmu.c +2 -1
@@ -1672,7 +1687,8 @@ static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache)
 	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, kvm_mmu_cache_capacity,
+						    min_pages);
 
 	return topup_hyp_memcache(memcache, min_pages);
 }

Humm, seems correct.
I think that's the best approach for not having to compute as much stuff in runtime, as it will basically become a load from a _ro* variable.

It's just my personal taste, but I would go with something like below[1], which does some extra arithm instructions at top up, but does not require an extra static variable or init function.

But in any case, if we have good test results with this approach, it would be a good time to get maintainers opinion on that.

Thanks!
Leo

[1]

arch/arm64/kvm/mmu.c

Quoted 2 lines
arch/arm64/kvm/mmu.c +0 -0

@@
+static int kvm_mmu_cache_capacity(void)
+{
+ int levels = stage2_pgtable_levels(get_kvm_ipa_limit());
+
+ if (levels < 2)
+ levels = 2;
+
+ return levels - 1;
+
+}

@@ -1317,8 +1332,8 @@ 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_mmu_cache_capacity(),
+ kvm_mmu_cache_min_pages(mmu));
if (ret)
break;

@@ -1672,7 +1687,8 @@ static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache)
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, kvm_mmu_cache_capacity(),
+ min_pages);

return topup_hyp_memcache(memcache, min_pages);
}