Skip to contentSkip to conversation

KVM: s390: Enable 4k granularity for memslots

PATCH Superseded Claudio Imbrenda 1 participant 6 messages Apr 1

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Claudio Imbrenda

author maintainer
#0 lore raw

v2 cover KVM: s390: Enable 4k granularity for memslots

Currently the memslot granularity for s390 VMs is 1M. Both the s390 core architecture and virtio-mem only support a 1M granularity for guest memory.

Notwithstanding the official architectural limits, it can be beneficial to support memslots with 4k granularity. With the new gmap code, this is a quite simple change. This allows us to enable more existing selftests and makes KVM on s390 more future proof.

(Please note that v1 of this series was not sent publicly)

v1->v2
* Fixed the first patch (thanks Christian)

Claudio Imbrenda (5):
KVM: s390: Add some useful mask macros
KVM: s390: Add alignment checks for hugepages
KVM: s390: Allow 4k granularity for memslots
KVM: selftests: Remove 1M alignment requirement for s390
KVM: s390: selftests: enable some common memory-related tests

 arch/s390/kvm/dat.h                           |  5 +++
 arch/s390/kvm/faultin.c                       |  2 +-
 arch/s390/kvm/gaccess.c                       |  2 +-
 arch/s390/kvm/gmap.c                          | 32 +++++++++++++++----
 arch/s390/kvm/gmap.h                          |  3 +-
 arch/s390/kvm/kvm-s390.c                      | 20 ++++--------
 tools/testing/selftests/kvm/Makefile.kvm      | 11 ++-----
 .../testing/selftests/kvm/include/kvm_util.h  |  4 ---
 tools/testing/selftests/kvm/lib/kvm_util.c    |  9 +-----
 9 files changed, 45 insertions(+), 43 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH v2 1/5 KVM: s390: Add some useful mask macros

Add _{SEGMENT,REGION3}_FR_MASK, similar to _{SEGMENT,REGION3}_MASK, but working on gfn/pfn instead of addresses. Use them in gaccess.c instead of using the normal masks plus gpa_to_gfn().

Also add _PAGES_PER_{SEGMENT,REGION3} to make future code more readable.

---
 arch/s390/kvm/dat.h     | 5 +++++
 arch/s390/kvm/gaccess.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)
arch/s390/kvm/dat.h +5 -0
index 123e11dcd70d..809cd7a8adb7 100644
@@ -104,6 +104,11 @@ union pte {
 	} tok;
 };
 
+#define _SEGMENT_FR_MASK	(_SEGMENT_MASK >> PAGE_SHIFT)
+#define _REGION3_FR_MASK	(_REGION3_MASK >> PAGE_SHIFT)
+#define _PAGES_PER_SEGMENT	_PAGE_ENTRIES
+#define _PAGES_PER_REGION3	(_PAGES_PER_SEGMENT * _CRST_ENTRIES)
+
 /* Soft dirty, needed as macro for atomic operations on ptes */
 #define _PAGE_SD 0x002
 
arch/s390/kvm/gaccess.c +1 -1
index 4630b2a067ea..a2ad11e2bf61 100644
@@ -1461,7 +1461,7 @@ static int _do_shadow_crste(struct gmap *sg, gpa_t raddr, union crste *host, uni
 	lockdep_assert_held(&sg->kvm->mmu_lock);
 	lockdep_assert_held(&sg->parent->children_lock);
 
-	gfn = f->gfn & gpa_to_gfn(is_pmd(*table) ? _SEGMENT_MASK : _REGION3_MASK);
+	gfn = f->gfn & (is_pmd(*table) ? _SEGMENT_FR_MASK : _REGION3_FR_MASK);
 	scoped_guard(spinlock, &sg->host_to_rmap_lock)
 		rc = gmap_insert_rmap(sg, gfn, gpa_to_gfn(raddr), host->h.tt);
 	if (rc)
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH v2 2/5 KVM: s390: Add alignment checks for hugepages

In reply to Claudio Imbrenda

When backing a guest page with a large page, check that the alignment of the guest page matches the alignment of the host physical page backing it within the large page.

Also check that the memslot is large enough to fit the large page.

Those checks are currently not needed, because memslots are guaranteed to be 1m-aligned, but this will change.

---
 arch/s390/kvm/faultin.c |  2 +-
 arch/s390/kvm/gmap.c    | 32 ++++++++++++++++++++++++++------
 arch/s390/kvm/gmap.h    |  3 ++-
 3 files changed, 29 insertions(+), 8 deletions(-)
arch/s390/kvm/faultin.c +1 -1
index e37cd18200f5..ddf0ca71f374 100644
@@ -109,7 +109,7 @@ int kvm_s390_faultin_gfn(struct kvm_vcpu *vcpu, struct kvm *kvm, struct guest_fa
 		scoped_guard(read_lock, &kvm->mmu_lock) {
 			if (!mmu_invalidate_retry_gfn(kvm, inv_seq, f->gfn)) {
 				f->valid = true;
-				rc = gmap_link(mc, kvm->arch.gmap, f);
+				rc = gmap_link(mc, kvm->arch.gmap, f, slot);
 				kvm_release_faultin_page(kvm, f->page, !!rc, f->write_attempt);
 				f->page = NULL;
 			}
arch/s390/kvm/gmap.c +26 -6
index ef0c6ebfdde2..e3c1b070a11d 100644
@@ -613,17 +613,37 @@ int gmap_try_fixup_minor(struct gmap *gmap, struct guest_fault *fault)
 	return rc;
 }
 
-static inline bool gmap_2g_allowed(struct gmap *gmap, gfn_t gfn)
+static inline bool gmap_2g_allowed(struct gmap *gmap, struct guest_fault *f,
+				   struct kvm_memory_slot *slot)
 {
 	return false;
 }
 
-static inline bool gmap_1m_allowed(struct gmap *gmap, gfn_t gfn)
+/**
+ * gmap_1m_allowed() - Check whether a 1M hugepage is allowed.
+ * @gmap: The gmap of the guest.
+ * @f: Describes the fault that is being resolved.
+ * @slot: The memslot the faulting address belongs to.
+ *
+ * The function checks whether the GMAP_FLAG_ALLOW_HPAGE_1M flag is set for
+ * @gmap, whether the offset of the address in the 1M virtual frame is the
+ * same as the offset in the physical 1M frame, and finally whether the whole
+ * 1M page would fit in the given memslot.
+ *
+ * Return: true if a 1M hugepage is allowed to back the faulting address, false
+ *         otherwise.
+ */
+static inline bool gmap_1m_allowed(struct gmap *gmap, struct guest_fault *f,
+				   struct kvm_memory_slot *slot)
 {
-	return test_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &gmap->flags);
+	return test_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &gmap->flags) &&
+	       !((f->gfn ^ f->pfn) & ~_SEGMENT_FR_MASK) &&
+	       slot->base_gfn <= ALIGN_DOWN(f->gfn, _PAGES_PER_SEGMENT) &&
+	       slot->base_gfn + slot->npages >= ALIGN(f->gfn + 1, _PAGES_PER_SEGMENT);
 }
 
-int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *f)
+int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *f,
+	      struct kvm_memory_slot *slot)
 {
 	unsigned int order;
 	int rc, level;
@@ -633,9 +653,9 @@ int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fau
 	level = TABLE_TYPE_PAGE_TABLE;
 	if (f->page) {
 		order = folio_order(page_folio(f->page));
-		if (order >= get_order(_REGION3_SIZE) && gmap_2g_allowed(gmap, f->gfn))
+		if (order >= get_order(_REGION3_SIZE) && gmap_2g_allowed(gmap, f, slot))
 			level = TABLE_TYPE_REGION3;
-		else if (order >= get_order(_SEGMENT_SIZE) && gmap_1m_allowed(gmap, f->gfn))
+		else if (order >= get_order(_SEGMENT_SIZE) && gmap_1m_allowed(gmap, f, slot))
 			level = TABLE_TYPE_SEGMENT;
 	}
 	rc = dat_link(mc, gmap->asce, level, uses_skeys(gmap), f);
arch/s390/kvm/gmap.h +2 -1
index ccb5cd751e31..a2f74587ddbf 100644
@@ -90,7 +90,8 @@ struct gmap *gmap_new(struct kvm *kvm, gfn_t limit);
 struct gmap *gmap_new_child(struct gmap *parent, gfn_t limit);
 void gmap_remove_child(struct gmap *child);
 void gmap_dispose(struct gmap *gmap);
-int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *fault);
+int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *fault,
+	      struct kvm_memory_slot *slot);
 void gmap_sync_dirty_log(struct gmap *gmap, gfn_t start, gfn_t end);
 int gmap_set_limit(struct gmap *gmap, gfn_t limit);
 int gmap_ucas_translate(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, gpa_t *gaddr);
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH v2 3/5 KVM: s390: Allow 4k granularity for memslots

In reply to Claudio Imbrenda

Until now memslots on s390 needed to have 1M granularity and be 1M aligned. Since the new gmap code can handle memslots with 4k granularity and alignment, remove the restrictions.

---
 arch/s390/kvm/kvm-s390.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
arch/s390/kvm/kvm-s390.c +6 -14
index bc7d6fa66eaf..be2a721e2574 100644
@@ -5636,8 +5636,6 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 				   struct kvm_memory_slot *new,
 				   enum kvm_mr_change change)
 {
-	gpa_t size;
-
 	if (kvm_is_ucontrol(kvm) && new->id < KVM_USER_MEM_SLOTS)
 		return -EINVAL;
 
@@ -5647,20 +5645,14 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 
 	if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) {
 		/*
-		 * A few sanity checks. We can have memory slots which have to be
-		 * located/ended at a segment boundary (1MB). The memory in userland is
-		 * ok to be fragmented into various different vmas. It is okay to mmap()
-		 * and munmap() stuff in this slot after doing this call at any time
+		 * A few sanity checks. The memory in userland is ok to be
+		 * fragmented into various different vmas. It is okay to mmap()
+		 * and munmap() stuff in this slot after doing this call at any
+		 * time.
 		 */
-
-		if (new->userspace_addr & 0xffffful)
+		if (new->userspace_addr & ~PAGE_MASK)
 			return -EINVAL;
-
-		size = new->npages * PAGE_SIZE;
-		if (size & 0xffffful)
-			return -EINVAL;
-
-		if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit)
+		if ((new->base_gfn + new->npages) * PAGE_SIZE > kvm->arch.mem_limit)
 			return -EINVAL;
 	}
 
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH v2 4/5 KVM: selftests: Remove 1M alignment requirement for s390

In reply to Claudio Imbrenda

Remove the 1M memslot alignment requirement for s390, since it is not needed anymore.

---
 tools/testing/selftests/kvm/include/kvm_util.h | 4 ----
 tools/testing/selftests/kvm/lib/kvm_util.c     | 9 +--------
 2 files changed, 1 insertion(+), 12 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +0 -4
index 8b39cb919f4f..f861242b4ae8 100644
@@ -1127,10 +1127,6 @@ vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
 {
 	unsigned int n;
 	n = vm_num_guest_pages(mode, vm_num_host_pages(mode, num_guest_pages));
-#ifdef __s390x__
-	/* s390 requires 1M aligned guest sizes */
-	n = (n + 255) & ~255;
-#endif
 	return n;
 }
 
tools/testing/selftests/kvm/lib/kvm_util.c +1 -8
index 1959bf556e88..f5e076591c64 100644
@@ -985,7 +985,7 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
 	struct userspace_mem_region *region;
 	size_t backing_src_pagesz = get_backing_src_pagesz(src_type);
 	size_t mem_size = npages * vm->page_size;
-	size_t alignment;
+	size_t alignment = 1;
 
 	TEST_REQUIRE_SET_USER_MEMORY_REGION2();
 
@@ -1039,13 +1039,6 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
 	TEST_ASSERT(region != NULL, "Insufficient Memory");
 	region->mmap_size = mem_size;
 
-#ifdef __s390x__
-	/* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
-	alignment = 0x100000;
-#else
-	alignment = 1;
-#endif
-
 	/*
 	 * When using THP mmap is not guaranteed to returned a hugepage aligned
 	 * address so we have to pad the mmap. Padding is not needed for HugeTLB
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH v2 5/5 KVM: s390: selftests: enable some common memory-related tests

In reply to Claudio Imbrenda

Enable the following tests on s390:
* memslot_modification_stress_test
* memslot_perf_test
* mmu_stress_test

Since the first two tests are now supported on all architectures, move them into TEST_GEN_PROGS_COMMON and out of the indiviual architectures.

---
 tools/testing/selftests/kvm/Makefile.kvm | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/Makefile.kvm +3 -8
index fdec90e85467..765a51599668 100644
@@ -64,6 +64,8 @@ TEST_GEN_PROGS_COMMON += kvm_binary_stats_test
 TEST_GEN_PROGS_COMMON += kvm_create_max_vcpus
 TEST_GEN_PROGS_COMMON += kvm_page_table_test
 TEST_GEN_PROGS_COMMON += set_memory_region_test
+TEST_GEN_PROGS_COMMON += memslot_modification_stress_test
+TEST_GEN_PROGS_COMMON += memslot_perf_test
 
 # Compiled test targets
 TEST_GEN_PROGS_x86 = $(TEST_GEN_PROGS_COMMON)
@@ -147,8 +149,6 @@ TEST_GEN_PROGS_x86 += coalesced_io_test
 TEST_GEN_PROGS_x86 += dirty_log_perf_test
 TEST_GEN_PROGS_x86 += guest_memfd_test
 TEST_GEN_PROGS_x86 += hardware_disable_test
-TEST_GEN_PROGS_x86 += memslot_modification_stress_test
-TEST_GEN_PROGS_x86 += memslot_perf_test
 TEST_GEN_PROGS_x86 += mmu_stress_test
 TEST_GEN_PROGS_x86 += rseq_test
 TEST_GEN_PROGS_x86 += steal_time
@@ -186,8 +186,6 @@ TEST_GEN_PROGS_arm64 += coalesced_io_test
 TEST_GEN_PROGS_arm64 += dirty_log_perf_test
 TEST_GEN_PROGS_arm64 += get-reg-list
 TEST_GEN_PROGS_arm64 += guest_memfd_test
-TEST_GEN_PROGS_arm64 += memslot_modification_stress_test
-TEST_GEN_PROGS_arm64 += memslot_perf_test
 TEST_GEN_PROGS_arm64 += mmu_stress_test
 TEST_GEN_PROGS_arm64 += rseq_test
 TEST_GEN_PROGS_arm64 += steal_time
@@ -205,6 +203,7 @@ TEST_GEN_PROGS_s390 += s390/ucontrol_test
 TEST_GEN_PROGS_s390 += s390/user_operexec
 TEST_GEN_PROGS_s390 += s390/keyop
 TEST_GEN_PROGS_s390 += rseq_test
+TEST_GEN_PROGS_s390 += mmu_stress_test
 
 TEST_GEN_PROGS_riscv = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_riscv += riscv/sbi_pmu_test
@@ -214,8 +213,6 @@ TEST_GEN_PROGS_riscv += arch_timer
 TEST_GEN_PROGS_riscv += coalesced_io_test
 TEST_GEN_PROGS_riscv += dirty_log_perf_test
 TEST_GEN_PROGS_riscv += get-reg-list
-TEST_GEN_PROGS_riscv += memslot_modification_stress_test
-TEST_GEN_PROGS_riscv += memslot_perf_test
 TEST_GEN_PROGS_riscv += mmu_stress_test
 TEST_GEN_PROGS_riscv += rseq_test
 TEST_GEN_PROGS_riscv += steal_time
@@ -230,8 +227,6 @@ TEST_GEN_PROGS_loongarch += hardware_disable_test
 TEST_GEN_PROGS_loongarch += kvm_binary_stats_test
 TEST_GEN_PROGS_loongarch += kvm_create_max_vcpus
 TEST_GEN_PROGS_loongarch += kvm_page_table_test
-TEST_GEN_PROGS_loongarch += memslot_modification_stress_test
-TEST_GEN_PROGS_loongarch += memslot_perf_test
 TEST_GEN_PROGS_loongarch += set_memory_region_test
 TEST_GEN_PROGS_loongarch += steal_time
 
Signature · 1 line
2.53.0