Skip to contentSkip to conversation

KVM: Scalable memslots implementation additional patches

PATCH In review Maciej S. Szmigiero 3 participants 9 messages Nov 26, 2021 – Nov 29, 2021

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

cover KVM: Scalable memslots implementation additional patches

From: "Maciej S. Szmigiero" <[email protected]>

While the last "5.5" version of KVM scalable memslots implementation was merged to kvm/queue some changes from its review round are still pending.

This series contains these changes which still apply to the final form of the code.

The changes include switching kvm_for_each_memslot_in_gfn_range() to use iterators.
However, I've dropped kvm_for_each_memslot_in_hva_range() rework to use dedicated iterators since the existing implementation was already returning only strictly overlapping memslots and it was already using interval tree iterators.
The code there is also self-contained and very simple.

The code was tested on x86 with KASAN on and booted various guests successfully (including nested ones; with TDP MMU both enabled and disabled).

There were some VMX APICv warnings during these tests but these look related to the latest VMX PI changes rather than memslots handling code.

 arch/x86/include/asm/kvm_host.h |   2 +-
 arch/x86/kvm/mmu/mmu.c          |  11 ++--
 arch/x86/kvm/x86.c              |   3 +-
 include/linux/kvm_host.h        | 104 ++++++++++++++++++++------------
 virt/kvm/kvm_main.c             |  25 +++-----
 5 files changed, 83 insertions(+), 62 deletions(-)
#1 lore raw

PATCH 1/3 KVM: x86: Avoid overflowing nr_mmu_pages in kvm_arch_commit_memory_region()

From: "Maciej S. Szmigiero" <[email protected]>

With kvm->nr_memslot_pages capped at ULONG_MAX we can't safely multiply it by KVM_PERMILLE_MMU_PAGES (20) since this operation can possibly overflow an unsigned long variable.

Rewrite this "* 20 / 1000" operation as "/ 50" instead to avoid such overflow.

---
 arch/x86/include/asm/kvm_host.h | 2 +-
 arch/x86/kvm/x86.c              | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 1fcb345bc107..8cd1d254c948 100644
@@ -135,7 +135,7 @@
 #define KVM_HPAGE_MASK(x)	(~(KVM_HPAGE_SIZE(x) - 1))
 #define KVM_PAGES_PER_HPAGE(x)	(KVM_HPAGE_SIZE(x) / PAGE_SIZE)
 
-#define KVM_PERMILLE_MMU_PAGES 20
+#define KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO 50
 #define KVM_MIN_ALLOC_MMU_PAGES 64UL
 #define KVM_MMU_HASH_SHIFT 12
 #define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
arch/x86/kvm/x86.c +1 -2
index 04e8dabc187d..69330b395f12 100644
@@ -11753,8 +11753,7 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,
 	    (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) {
 		unsigned long nr_mmu_pages;
 
-		nr_mmu_pages = kvm->nr_memslot_pages * KVM_PERMILLE_MMU_PAGES;
-		nr_mmu_pages /= 1000;
+		nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO;
 		nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES);
 		kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
 	}
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] KVM: x86: Avoid overflowing nr_mmu_pages in kvm_arch_commit_memory_region()

Quoted 11 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

With kvm->nr_memslot_pages capped at ULONG_MAX we can't safely multiply it by KVM_PERMILLE_MMU_PAGES (20) since this operation can possibly overflow an unsigned long variable.

Rewrite this "* 20 / 1000" operation as "/ 50" instead to avoid such overflow.

---

#3 lore raw

PATCH 2/3 KVM: Use atomic_long_cmpxchg() instead of an open-coded variant

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Open-coding a cmpxchg()-like operation is significantly less readable than a direct call.

Also, the open-coded version compiles to multiple instructions with a branch on x86, instead of just a single instruction.

Since technically the open-coded variant didn't guarantee actual atomicity add a comment there, too, that this property isn't strictly required in this case.

---
 virt/kvm/kvm_main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +6 -2
index d4399db06d49..367c1cba26d2 100644
@@ -1378,8 +1378,12 @@ static void kvm_replace_memslot(struct kvm *kvm,
 		hash_del(&old->id_node[idx]);
 		interval_tree_remove(&old->hva_node[idx], &slots->hva_tree);
 
-		if ((long)old == atomic_long_read(&slots->last_used_slot))
-			atomic_long_set(&slots->last_used_slot, (long)new);
+		/*
+		 * The atomicity isn't strictly required here since we are
+		 * operating on an inactive memslots set anyway.
+		 */
+		atomic_long_cmpxchg(&slots->last_used_slot,
+				    (unsigned long)old, (unsigned long)new);
 
 		if (!new) {
 			kvm_erase_gfn_node(slots, old);
maintainer
#4 lore raw

Re: [PATCH 2/3] KVM: Use atomic_long_cmpxchg() instead of an open-coded variant

virt/kvm/kvm_main.c static void kvm_replace_memslot(struct kvm *kvm, · line 1386

Quoted 8 lines · Maciej S. Szmigiero
virt/kvm/kvm_main.c +6 -2
-		if ((long)old == atomic_long_read(&slots->last_used_slot))
-			atomic_long_set(&slots->last_used_slot, (long)new);
+		/*
+		 * The atomicity isn't strictly required here since we are
+		 * operating on an inactive memslots set anyway.
+		 */
+		atomic_long_cmpxchg(&slots->last_used_slot,
+				    (unsigned long)old, (unsigned long)new);

I think using read/set is more readable than a comment saying that atomicity is not required.

It's a fairly common pattern, and while I agree that it's a PITA to write atomic_long_read and atomic_long_set, the person that reads the code is also helped by read/set, because they know they have to think about ownership invariants rather than concurrency invariants.

Paolo

#5 lore raw

PATCH 3/3 KVM: Make kvm_for_each_memslot_in_gfn_range() strict and use iterators

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Make kvm_for_each_memslot_in_gfn_range() return only memslots at least partially intersecting the requested range.
At the same time modify its implementation to use iterators.

This simplifies kvm_check_memslot_overlap() a bit.

---
 arch/x86/kvm/mmu/mmu.c   |  11 ++---
 include/linux/kvm_host.h | 104 +++++++++++++++++++++++++--------------
 virt/kvm/kvm_main.c      |  17 ++-----
 3 files changed, 75 insertions(+), 57 deletions(-)
arch/x86/kvm/mmu/mmu.c +5 -6
index 8f0035517450..009eb27d34d3 100644
@@ -5715,23 +5715,22 @@ static bool __kvm_zap_rmaps(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
 {
 	const struct kvm_memory_slot *memslot;
 	struct kvm_memslots *slots;
-	struct rb_node *node;
+	struct kvm_memslot_iter iter;
 	bool flush = false;
 	gfn_t start, end;
-	int i, idx;
+	int i;
 
 	if (!kvm_memslots_have_rmaps(kvm))
 		return flush;
 
 	for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
 		slots = __kvm_memslots(kvm, i);
-		idx = slots->node_idx;
 
-		kvm_for_each_memslot_in_gfn_range(node, slots, gfn_start, gfn_end) {
-			memslot = container_of(node, struct kvm_memory_slot, gfn_node[idx]);
+		kvm_for_each_memslot_in_gfn_range(&iter, slots, gfn_start, gfn_end) {
+			memslot = kvm_memslot_iter_slot(&iter);
 			start = max(gfn_start, memslot->base_gfn);
 			end = min(gfn_end, memslot->base_gfn + memslot->npages);
-			if (start >= end)
+			if (WARN_ON_ONCE(start >= end))
 				continue;
 
 			flush = slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
include/linux/kvm_host.h +67 -37

141 lines not on this page · raw message

virt/kvm/kvm_main.c +3 -14
index 367c1cba26d2..d6503b92b3cf 100644
@@ -1797,22 +1797,11 @@ static int kvm_set_memslot(struct kvm *kvm,
 static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,
 				      gfn_t start, gfn_t end)
 {
-	int idx = slots->node_idx;
-	struct rb_node *node;
-
-	kvm_for_each_memslot_in_gfn_range(node, slots, start, end) {
-		struct kvm_memory_slot *cslot;
-		gfn_t cend;
+	struct kvm_memslot_iter iter;
 
-		cslot = container_of(node, struct kvm_memory_slot, gfn_node[idx]);
-		cend = cslot->base_gfn + cslot->npages;
-		if (cslot->id == id)
-			continue;
-
-		/* kvm_for_each_in_gfn_no_more() guarantees that cslot->base_gfn < nend */
-		if (cend > start)
+	kvm_for_each_memslot_in_gfn_range(&iter, slots, start, end)
+		if (kvm_memslot_iter_slot(&iter)->id != id)
 			return true;
-	}
 
 	return false;
 }
maintainer
#6 lore raw
In reply to Maciej S. Szmigiero
Quoted 4 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero"<[email protected]>

While the last "5.5" version of KVM scalable memslots implementation was merged to kvm/queue some changes from its review round are still pending.

You can go ahead and post v6, I'll replace. However, note that I would prefer the current form instead of patch 2's atomic_long_cmpxchg.

Paolo

#7 lore raw
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero"<[email protected]>

While the last "5.5" version of KVM scalable memslots implementation was merged to kvm/queue some changes from its review round are still pending.

You can go ahead and post v6, I'll replace.

Which tree should I target then?
kvm/queue already has these commits so git refuses to rebase on top of it.

By the way, v6 will have more changes than this series since there will be patches for intermediate forms of code, too.

Quoted 1 line

However, note that I would prefer the current form instead of patch 2's atomic_long_cmpxchg.

Understood.

Quoted 1 line

Paolo

Thanks,
Maciej

maintainer
#8 lore raw
Quoted 8 lines · Maciej S. Szmigiero
Quoted 2 lines

You can go ahead and post v6, I'll replace.

Which tree should I target then?
kvm/queue already has these commits so git refuses to rebase on top of it.

By the way, v6 will have more changes than this series since there will be patches for intermediate forms of code, too.

I'll undo them from kvm/queue shortly, since I've now updated all my development machines to 5.16-rc2.

Paolo