Skip to contentSkip to conversation

KVM: arm64: Stop stage-2 teardown scaling with the IPA size

PATCH Applied Fuad Tabba 2 participants 4 messages Nov 6 – Nov 17

Subsystems (2)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

v3 cover KVM: arm64: Stop stage-2 teardown scaling with the IPA size

Hi folks,

Changes since v2 [1]:
- Return a failed walk's error to the callers and warn there, instead of inside the walk. (Oliver)
- Dropped the comment on the next-mapping lookup. (Vincent)
- Collected Reviewed-by from Vincent on patch 2. (thanks!)

Two patches that make stage-2 teardown take time proportional to what is mapped, rather than to the size of the IPA space as it has since 4ddfab5436b6.

Under QEMU TCG ('-cpu max', 16K pages with LPA2), destroying even an empty VM takes about 7 seconds in pKVM. Without pKVM it takes about 19 seconds if the VM requests a 52-bit IPA (crosvm requests the host's limit). With the series, both take well under a second. Tested only under QEMU.

The first has the page-table walk return where it stopped, so that the teardown steps over an invalid entry larger than a block-sized chunk in one go. The second has pKVM return where its next mapping starts.

Based on Linux 7.4-rc1 (6628e7d4c6b25).

Cheers,
/fuad

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

Fuad Tabba (2):
KVM: arm64: Resume stage-2 teardown where the walk stopped
KVM: arm64: Resume a pKVM stage-2 teardown at the next mapping

 arch/arm64/include/asm/kvm_pgtable.h |  8 ++++++--
 arch/arm64/include/asm/kvm_pkvm.h    |  4 ++--
 arch/arm64/kvm/hyp/pgtable.c         | 19 +++++++++++++++----
 arch/arm64/kvm/mmu.c                 |  6 ++++--
 arch/arm64/kvm/pkvm.c                | 11 ++++++++---
 5 files changed, 35 insertions(+), 13 deletions(-)

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH v3 1/2 KVM: arm64: Resume stage-2 teardown where the walk stopped

Since commit 4ddfab5436b6 ("KVM: arm64: Reschedule as needed when destroying the stage-2 page-tables"), kvm_stage2_destroy() walks the IPA space one block-sized chunk at a time. The walker steps over an invalid entry in one go, but the next walk starts at the following chunk, so an invalid entry larger than a chunk is visited once for every chunk it covers. Destroying a VM with a 52-bit IPA then takes 2^27 walks with 16K pages (2^22 with 4K), however little it maps.

Have kvm_pgtable_stage2_destroy_range() return the address its walk reached, which kvm_pgtable_walk() doesn't expose, and resume from there. A failed walk returns its error instead, and the callers warn about it as the walk used to. stage2_destroy_range() then goes on to the next chunk, as before. pkvm_pgtable_stage2_destroy_range() returns the end of its range.

---
 arch/arm64/include/asm/kvm_pgtable.h |  8 ++++++--
 arch/arm64/include/asm/kvm_pkvm.h    |  4 ++--
 arch/arm64/kvm/hyp/pgtable.c         | 19 +++++++++++++++----
 arch/arm64/kvm/mmu.c                 |  6 ++++--
 arch/arm64/kvm/pkvm.c                |  8 +++++---
 5 files changed, 32 insertions(+), 13 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +6 -2
index 1c9c57cd6ae40..41b137d39a5bb 100644
@@ -599,9 +599,13 @@ void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt);
  *
  * The page-table is assumed to be unreachable by any hardware walkers prior
  * to freeing and therefore no TLB invalidation is performed.
+ *
+ * Return: the address the walk reached, which can be past @addr + @size
+ * when the range ends within a block or an invalid entry, or a negative
+ * error code on failure.
  */
-void kvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
-					u64 addr, u64 size);
+s64 kvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
+				     u64 addr, u64 size);
 
 /**
  * kvm_pgtable_stage2_destroy_pgd() - Destroy the PGD of guest stage-2 page-table.
arch/arm64/include/asm/kvm_pkvm.h +2 -2
index fd042d2b54624..971ab6c0380de 100644
@@ -349,8 +349,8 @@ int __pkvm_handle_smccc_req(struct arm_smccc_res *res);
 
 int pkvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
 			     struct kvm_pgtable_mm_ops *mm_ops);
-void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
-					u64 addr, u64 size);
+s64 pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
+				      u64 addr, u64 size);
 void pkvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt);
 int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
 			    enum kvm_pgtable_prot prot, void *mc,
arch/arm64/kvm/hyp/pgtable.c +15 -4
index 9c83837b184d3..01fc3fc2b6553 100644
@@ -1653,16 +1653,27 @@ static int stage2_free_walker(const struct kvm_pgtable_visit_ctx *ctx,
 	}
 }
 
-void kvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
-				       u64 addr, u64 size)
+s64 kvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
+				     u64 addr, u64 size)
 {
 	struct kvm_pgtable_walker walker = {
 		.cb	= stage2_free_walker,
 		.flags	= KVM_PGTABLE_WALK_LEAF |
 			  KVM_PGTABLE_WALK_TABLE_POST,
 	};
+	struct kvm_pgtable_walk_data data = {
+		.walker	= &walker,
+		.start	= addr,
+		.addr	= addr,
+		.end	= addr + size,
+	};
+	int ret;
 
-	WARN_ON(kvm_pgtable_walk(pgt, addr, size, &walker));
+	ret = _kvm_pgtable_walk(pgt, &data);
+	if (ret)
+		return ret;
+
+	return data.addr;
 }
 
 void kvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt)
@@ -1681,7 +1692,7 @@ void kvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt)
 
 void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt)
 {
-	kvm_pgtable_stage2_destroy_range(pgt, 0, BIT(pgt->ia_bits));
+	WARN_ON(kvm_pgtable_stage2_destroy_range(pgt, 0, BIT(pgt->ia_bits)) < 0);
 	kvm_pgtable_stage2_destroy_pgd(pgt);
 }
 
arch/arm64/kvm/mmu.c +4 -2
index 1db10d8d6db73..372a1c04d3c47 100644
@@ -1008,11 +1008,13 @@ static void stage2_destroy_range(struct kvm_pgtable *pgt, phys_addr_t addr,
 				   phys_addr_t end)
 {
 	u64 next;
+	s64 ret;
 
 	do {
 		next = stage2_range_addr_end(addr, end);
-		KVM_PGT_FN(kvm_pgtable_stage2_destroy_range)(pgt, addr,
-							     next - addr);
+		ret = KVM_PGT_FN(kvm_pgtable_stage2_destroy_range)(pgt, addr, next - addr);
+		if (!WARN_ON(ret < 0))
+			next = min_t(u64, ret, end);
 		if (next != end)
 			cond_resched();
 	} while (addr = next, addr != end);
arch/arm64/kvm/pkvm.c +5 -3
index 016997288fd23..d0a6308008512 100644
@@ -499,14 +499,14 @@ static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64
 	return 0;
 }
 
-void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
-					u64 addr, u64 size)
+s64 pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
+				      u64 addr, u64 size)
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
 	pkvm_handle_t handle = kvm->arch.pkvm.handle;
 
 	if (!handle)
-		return;
+		return addr + size;
 
 	if (pkvm_hyp_vm_is_created(kvm) && !kvm->arch.pkvm.is_dying) {
 		WARN_ON(kvm_call_hyp_nvhe(__pkvm_start_teardown_vm, handle));
@@ -517,6 +517,8 @@ void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
 		__pkvm_pgtable_stage2_reclaim(pgt, addr, addr + size);
 	else
 		__pkvm_pgtable_stage2_unshare(pgt, addr, addr + size);
+
+	return addr + size;
 }
 
 void pkvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt)
Signature · 1 line
2.47.3
author reviewer
#2 lore raw

PATCH v3 2/2 KVM: arm64: Resume a pKVM stage-2 teardown at the next mapping

In reply to Fuad Tabba

pKVM keeps a VM's stage-2 mappings in an interval tree, and pkvm_pgtable_stage2_destroy_range() releases the ones in its range. stage2_destroy_range() still calls it for every block-sized chunk of the IPA space. As pKVM gives every VM the host's IPA limit, a teardown with LPA2 makes 2^27 calls with 16K pages (2^22 with 4K), however little the VM maps.

Return the address of the next mapping instead, or S64_MAX when none is left (U64_MAX would read as an error), so that the teardown skips the space between mappings. No mapping crosses a chunk boundary, as pKVM maps at most PMD_SIZE, so the next one starts at or above the end of the range. A VM without a handle doesn't have anything mapped, so return S64_MAX for it straight away.

---
 arch/arm64/kvm/pkvm.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/arm64/kvm/pkvm.c +5 -2
index d0a6308008512..ef36875e25610 100644
@@ -504,9 +504,10 @@ s64 pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
 	pkvm_handle_t handle = kvm->arch.pkvm.handle;
+	struct pkvm_mapping *mapping;
 
 	if (!handle)
-		return addr + size;
+		return S64_MAX;
 
 	if (pkvm_hyp_vm_is_created(kvm) && !kvm->arch.pkvm.is_dying) {
 		WARN_ON(kvm_call_hyp_nvhe(__pkvm_start_teardown_vm, handle));
@@ -518,7 +519,9 @@ s64 pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
 	else
 		__pkvm_pgtable_stage2_unshare(pgt, addr, addr + size);
 
-	return addr + size;
+	mapping = pkvm_mapping_iter_first(&pgt->pkvm_mappings, addr + size, U64_MAX);
+
+	return mapping ? gfn_to_gpa(mapping->gfn) : S64_MAX;
 }
 
 void pkvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt)
Signature · 1 line
2.47.3
maintainer Applied
#3 lore raw
In reply to Fuad Tabba
Quoted 11 lines · Fuad Tabba

Changes since v2 [1]:
- Return a failed walk's error to the callers and warn there, instead of inside the walk. (Oliver)
- Dropped the comment on the next-mapping lookup. (Vincent)
- Collected Reviewed-by from Vincent on patch 2. (thanks!)

Two patches that make stage-2 teardown take time proportional to what is mapped, rather than to the size of the IPA space as it has since 4ddfab5436b6.

[...]

Applied to next, thanks!

[1/2] KVM: arm64: Resume stage-2 teardown where the walk stopped

      https://git.kernel.org/kvmarm/kvmarm/c/6607becfb034
[2/2] KVM: arm64: Resume a pKVM stage-2 teardown at the next mapping
      https://git.kernel.org/kvmarm/kvmarm/c/737a5ff38ccd

--
Best,
Oliver