Skip to contentSkip to conversation

KVM: arm64: Resume stage-2 unmap where the walk stopped

PATCH Superseded Haseeb Ashraf 2 participants 3 messages Nov 10 – Nov 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover KVM: arm64: Resume stage-2 unmap where the walk stopped

Unmapping the whole IPA space of a shadow stage-2, as emulating TLBI VMALLS12E1IS and TLBI ALLE1IS does, walks it 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. Each walk also ends with a range TLBI when FEAT_TLBIRANGE and FWB are available.

This version follows the approach of Fuad's series [2]: the walk reports the address it reached, and the unmap loop resumes from there. The walk already handles every entry it steps over, so nothing has to be looked up beforehand.

An example shows what changes. Take 4K pages and a 48-bit IPA space. The root is a level-0 table with 512 entries of 512GB each, and a chunk is 1GB:

Empty shadow stage-2
before: 2^18 = 262144 walks. Each of the 512 level-0 entries is
visited once for every one of the 512 chunks under it.
after: 512 walks. The first walk under an invalid entry returns
the end of that entry, and the loop resumes there.

One populated 512GB region (RAM and devices low in the IPA space)

    before: 262144 walks.
    after:  at most 512 + 511 = 1023 walks: 512 chunks inside the
            region, and one step over each of the 511 other entries.

The example is derived from the page-table layout. Measurements follow. The machine is an AmpereOne A192-32X (192 CPUs, bare metal, 4K pages, 48-bit IPA, FWB and TLB range maintenance present) running Xen as a KVM guest. The workload is one create and destroy cycle of an 8GB guest under Xen. bpftrace kprobes and kretprobes recorded every full-range unmap (start 0, size 2^48), its walks, its range TLBIs and its duration. "Before" is plain 6.18.29. "After" is 6.18.29 with this patch, adapted to the older unmap loop, and with seven commits that are already in mainline backported: 33346f8960c7 and e5843f4effaa (nested_mmus life cycle), and a6ca6cba5959, c2778d14cd2f, 5491980c1a41, e6084aecaeef and 2064f15b0848 (guest stage-2 tracking).

before after
full-range unmaps 192 128
walks per unmap 262144 512 - 1024
range TLBIs per unmap 262144 same as walks

  unmap duration, mean            907.8 ms        3.77 ms
  unmap duration, median          909.3 ms        2.46 ms
  unmap duration, min             815.5 ms        2.06 ms
  unmap duration, max             937.6 ms       27.8  ms
  VMALLS12E1IS trap, mean         957.0 ms        3.78 ms

VMALLS12E1IS trap, max 1848.6 ms 27.9 ms

Walks per unmap after the change: 512 (10 unmaps), 534 (1), 546 (78), 1023 (38) and 1024 (1), mean 689. The page-table occupancy was not recorded, so I cannot tie each count to a layout. 1024 is one more than a single populated region gives. Before the change nine of the 177 traps unmapped two shadow stage-2s and took about twice as long; after it every trap unmapped one.

Patch 1 adds kvm_pgtable_stage2_unmap_range(), which returns the address its walk reached, and has the unmap loop resume from there. kvm_pgtable_stage2_unmap() keeps its return value for its callers at EL2. The unmap of a shadow stage-2 also drops the guest stage-2 records of the whole span the walk covered. The TLBI VMALLS12E1IS handler, kvm_nested_s2_unmap() and the pending unmap of a recycled shadow stage-2 need no change.

Changes since v1 [1]:
- Reworked as Oliver suggested: the walk reports the next IPA to the outer loop, as in Fuad's series [2]. This replaces kvm_stage2_unmap_all() and its kvm_pgtable_get_leaf() lookup.
- No extra VMID-wide TLBI: each walk keeps its own TLB maintenance.
- Measurements added.
- Based on Linux 7.4-rc1, the same base as [2]. It does not depend on [2]; it applies with and without it.

[1] https://lore.kernel.org/r/cfa77cdbf14d9fd24c22ab1ef9b940a0d7411699.1794229714.git.haseeb.ashraf@siemens.com
[2] https://lore.kernel.org/r/[email protected]

Tooling: the patch was drafted with AI assistance (SiemensGPT). The statistics were collected with bpftrace, using kprobes at function entry and kretprobes at function return.

Testing: the backport of this patch, together with the seven commits listed above, was booted as 6.18.29 on the machine above, and the measurements are from that kernel. The patch for 7.4-rc1 itself was only compile-checked in isolation.

#1 lore raw

PATCH v2 1/1 KVM: arm64: Resume stage-2 unmap where the walk stopped

From: Haseeb Ashraf <[email protected]>

kvm_vm_stage2_unmap_range() unmaps a range 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. Unmapping the whole IPA space of a shadow stage-2, as emulating TLBI VMALLS12E1IS and TLBI ALLE1IS does, takes 2^18 walks with 4K pages and a 48-bit IPA space, however little it maps. Each walk also ends with a range TLBI when FEAT_TLBIRANGE and FWB are available.

On an AmpereOne A192-32X (192 CPUs, Xen on KVM, 4K pages, 48-bit IPA), a full-range unmap of one shadow stage-2 took 0.82-0.94 s (mean 0.91 s over 192 unmaps) on 6.18.29. With this change and the mainline commits it builds on backported, it took 2.1-27.8 ms (mean 3.8 ms over 128 unmaps), with 512 to 1024 walks instead of 262144. A TLBI VMALLS12E1IS trap that unmapped two shadow stage-2s took up to 1.85 s before.

Add kvm_pgtable_stage2_unmap_range(), which returns the address its walk reached, and have the unmap loop resume from there. kvm_pgtable_stage2_unmap() still returns 0 or an error, for its callers at EL2. The walk handles every entry it steps over, including invalid entries that carry state, so nothing has to be looked up beforehand.

The unmap of a shadow stage-2 also drops the guest stage-2 records of the whole span the walk covered: nothing is mapped in the part it stepped over, so any record there is stale. Each walk that remains ends with the same deferred TLB invalidation as before. The spans that were skipped have no valid entry, so there is nothing to invalidate.

---
 arch/arm64/include/asm/kvm_pgtable.h | 18 ++++++++
 arch/arm64/kvm/hyp/pgtable.c         | 22 ++++++++--
 arch/arm64/kvm/mmu.c                 | 63 +++++++++++++++++++++++-----
 3 files changed, 90 insertions(+), 13 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +18 -0
index 1c9c57c..4ed096f 100644
@@ -725,6 +725,24 @@ int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size,
  */
 int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
 
+/**
+ * kvm_pgtable_stage2_unmap_range() - Remove a mapping from a guest stage-2
+ *                                    page-table, and report where the walk
+ *                                    stopped.
+ * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
+ * @addr:	Intermediate physical address from which to remove the mapping.
+ * @size:	Size of the mapping.
+ *
+ * Same as kvm_pgtable_stage2_unmap(), but the walk can end past @addr + @size
+ * when it steps over an invalid entry that extends beyond the range. Nothing
+ * is mapped up to the address that is returned, so a caller that unmaps a
+ * larger range in chunks can resume from it.
+ *
+ * Return: the address the walk reached, which can be past @addr + @size, or a
+ * negative error code on failure.
+ */
+s64 kvm_pgtable_stage2_unmap_range(struct kvm_pgtable *pgt, u64 addr, u64 size);
+
 /**
  * kvm_pgtable_stage2_wrprotect() - Write-protect guest stage-2 address range
  *                                  without TLB invalidation.
arch/arm64/kvm/hyp/pgtable.c +19 -3
index 9c83837..b02f548 100644
@@ -1197,7 +1197,7 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
 	return 0;
 }
 
-int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
+s64 kvm_pgtable_stage2_unmap_range(struct kvm_pgtable *pgt, u64 addr, u64 size)
 {
 	int ret;
 	struct kvm_pgtable_walker walker = {
@@ -1205,13 +1205,29 @@ int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
 		.arg	= pgt,
 		.flags	= KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
 	};
+	struct kvm_pgtable_walk_data data = {
+		.walker	= &walker,
+		.start	= ALIGN_DOWN(addr, PAGE_SIZE),
+		.addr	= ALIGN_DOWN(addr, PAGE_SIZE),
+		.end	= PAGE_ALIGN(data.addr + size),
+	};
 
-	ret = kvm_pgtable_walk(pgt, addr, size, &walker);
+	ret = _kvm_pgtable_walk(pgt, &data);
 	if (stage2_unmap_defer_tlb_flush(pgt))
 		/* Perform the deferred TLB invalidations */
 		kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);
 
-	return ret;
+	if (ret)
+		return ret;
+
+	return data.addr;
+}
+
+int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
+{
+	s64 ret = kvm_pgtable_stage2_unmap_range(pgt, addr, size);
+
+	return ret < 0 ? ret : 0;
 }
 
 struct stage2_attr_data {
arch/arm64/kvm/mmu.c +53 -10

91 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Hi,

Quoted 34 lines · Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

kvm_vm_stage2_unmap_range() unmaps a range 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. Unmapping the whole IPA space of a shadow stage-2, as emulating TLBI VMALLS12E1IS and TLBI ALLE1IS does, takes 2^18 walks with 4K pages and a 48-bit IPA space, however little it maps. Each walk also ends with a range TLBI when FEAT_TLBIRANGE and FWB are available.

On an AmpereOne A192-32X (192 CPUs, Xen on KVM, 4K pages, 48-bit IPA), a full-range unmap of one shadow stage-2 took 0.82-0.94 s (mean 0.91 s over 192 unmaps) on 6.18.29. With this change and the mainline commits it builds on backported, it took 2.1-27.8 ms (mean 3.8 ms over 128 unmaps), with 512 to 1024 walks instead of 262144. A TLBI VMALLS12E1IS trap that unmapped two shadow stage-2s took up to 1.85 s before.

Add kvm_pgtable_stage2_unmap_range(), which returns the address its walk reached, and have the unmap loop resume from there. kvm_pgtable_stage2_unmap() still returns 0 or an error, for its callers at EL2. The walk handles every entry it steps over, including invalid entries that carry state, so nothing has to be looked up beforehand.

The unmap of a shadow stage-2 also drops the guest stage-2 records of the whole span the walk covered: nothing is mapped in the part it stepped over, so any record there is stale. Each walk that remains ends with the same deferred TLB invalidation as before. The spans that were skipped have no valid entry, so there is nothing to invalidate.

---

[...]

Quoted 31 lines
arch/arm64/kvm/mmu.c +31 -0
+/*
+ * Unmap [start, end) one chunk at a time, resuming from the address that each
+ * walk reached. An invalid entry that is larger than a chunk is then stepped
+ * over once, rather than once for every chunk that it covers.
+ */
+static int stage2_apply_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
+				    phys_addr_t end,
+				    s64 (*fn)(struct kvm_pgtable *, u64, u64),
+				    bool resched)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	bool lock_dropped = false;
+	phys_addr_t addr = start;
+	u64 next;
+	s64 ret;
+
+	do {
+		struct kvm_pgtable *pgt = mmu->pgt;
+
+		/*
+		 * We may be raced on PGT teardown when we release the
+		 * kvm->mmu_lock. That's fine as the PGT is legitimately no
+		 * longer present.
+		 */
+		if (!pgt)
+			return lock_dropped ? 0 : -EINVAL;
+
+		next = stage2_range_addr_end(addr, end);
+		ret = fn(pgt, addr, next - addr);
+		if (ret < 0)
+			return ret;

arch/arm64/kvm/mmu.c static void invalidate_icache_guest_page(void *va, size_t size) · line 365

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+		next = min_t(u64, ret, end);
+		if (resched && next != end) {
+			cond_resched_rwlock_write(&kvm->mmu_lock);
+			lock_dropped = true;
+		}
+	} while (addr = next, addr != end);
+
+	return 0;
+}

In my opinion it is worth giving the same treatment to all users of stage2_apply_range(), instead of having this new stage2_apply_unmap_range() that is almost identical.

Thanks,
Wei-Lin Chang

Quoted 19 lines
arch/arm64/kvm/mmu.c +4 -3
+
 static void kvm_vm_stage2_unmap_range(struct kvm_s2_mmu *mmu,
 				      phys_addr_t start,
 				      u64 size, bool may_block)
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
-	int (*fn)(struct kvm_pgtable *, u64, u64);
+	s64 (*fn)(struct kvm_pgtable *pgt, u64 addr, u64 size);
 
 	if (kvm_is_nested_s2_mmu(kvm, mmu))
 		fn = kvm_pgtable_stage2_unmap_tracked;
 	else
-		fn = kvm_pgtable_stage2_unmap;
+		fn = kvm_pgtable_stage2_unmap_range;
 
-	WARN_ON(stage2_apply_range(mmu, start, start + size, fn, may_block));
+	WARN_ON(stage2_apply_unmap_range(mmu, start, start + size, fn, may_block));
 }

[...]