Skip to contentSkip to conversation

s390x: Cleanup virtualization library

PATCH In review Janosch Frank 3 participants 12 messages Apr 20 – Apr 22

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

v2 cover s390x: Cleanup virtualization library

In order to support sae virtualization we need to cleanup the library a bit and make it more flexible when it comes to guest creation and allocation.

v2:
- Renamed get_page_root() to get_primary_page_root()
- Reworked patch 3 into a general sie memory improvement patch
- Removed the sie struct rename patch

Janosch Frank (4):
lib: s390x: Add function to get page root
lib: s390x: sie: Allocate physical guest memory via memalign
lib: s390x: sie: Memory rework
lib: s390x: snippet: Add function to create a guest of specific length

 lib/s390x/mmu.h     |  7 ++++
 lib/s390x/sie.c     | 89 ++++++++++++++++++++++++---------------------
 lib/s390x/sie.h     |  2 +-
 lib/s390x/snippet.h | 19 ++++++++--
 s390x/diag258.c     |  4 +-
 s390x/edat.c        |  5 ++-
 s390x/mvpg-sie.c    |  4 +-
 s390x/pv-diags.c    |  2 +-
 s390x/pv-edat1.c    |  9 ++---
 s390x/pv-icptcode.c |  4 +-
 s390x/pv-ipl.c      |  2 +-
 s390x/sie-dat.c     |  4 +-
 s390x/sie.c         | 13 ++-----
 s390x/skey.c        |  7 ++--
 s390x/spec_ex-sie.c |  2 +
 s390x/stfle-sie.c   |  2 +
 16 files changed, 97 insertions(+), 78 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/4 lib: s390x: Add function to get page root

It's time to hide the access to cr1 behind a function and add typing to the tests that used void* instead of pgdt_t*.

---
 lib/s390x/mmu.h  | 7 +++++++
 lib/s390x/sie.c  | 3 ++-
 s390x/diag258.c  | 4 ++--
 s390x/edat.c     | 5 +++--
 s390x/mvpg-sie.c | 2 +-
 s390x/pv-edat1.c | 4 ++--
 s390x/sie-dat.c  | 2 +-
 s390x/skey.c     | 7 +++----
 8 files changed, 21 insertions(+), 13 deletions(-)
lib/s390x/mmu.h +7 -0
index dadc2e60..19c46c58 100644
@@ -10,6 +10,8 @@
 #ifndef _S390X_MMU_H_
 #define _S390X_MMU_H_
 
+#include <asm/arch_def.h>
+
 enum pgt_level {
 	pgtable_level_pgd = 1,
 	pgtable_level_p4d,
@@ -95,4 +97,9 @@ static inline void unprotect_page(void *vaddr, unsigned long prot)
 
 void *get_dat_entry(pgd_t *pgtable, void *vaddr, enum pgt_level level);
 
+static inline pgd_t *get_primary_page_root(void)
+{
+	return (pgd_t *)(stctg(1) & PAGE_MASK);
+}
+
 #endif /* _ASMS390X_MMU_H_ */
lib/s390x/sie.c +2 -1
index 0fa915cf..47d4cdde 100644
@@ -17,6 +17,7 @@
 #include <alloc_page.h>
 #include <vmalloc.h>
 #include <sclp.h>
+#include <mmu.h>
 
 void sie_expect_validity(struct vm *vm)
 {
@@ -156,7 +157,7 @@ uint8_t *sie_guest_alloc(uint64_t guest_size)
 	pgd_t *root;
 
 	setup_vm();
-	root = (pgd_t *)(stctg(1) & PAGE_MASK);
+	root = get_primary_page_root();
 
 	/*
 	 * Start of guest memory in host virtual space needs to be aligned to
s390x/diag258.c +2 -2
index 8ba75a72..2482ecf3 100644
@@ -82,7 +82,7 @@ static void test_priv(void)
 
 static void *page_map_outside_real_space(phys_addr_t page_real)
 {
-	pgd_t *root = (pgd_t *)(stctg(1) & PAGE_MASK);
+	pgd_t *root = get_primary_page_root();
 	void *vaddr = alloc_vpage();
 
 	install_page(root, page_real, vaddr);
@@ -109,7 +109,7 @@ static void test_refbk_real(void)
 	refbk_page = alloc_page();
 
 	/* Map refblk page outside of physical memory identity mapping */
-	root = (pgd_t *)(stctg(1) & PAGE_MASK);
+	root = get_primary_page_root();
 	refbk = page_map_outside_real_space(virt_to_pte_phys(root, refbk_page));
 
 	/* Assert the mapping really is outside identity mapping */
s390x/edat.c +3 -2
index 89b9c2d3..e54cd642 100644
@@ -21,7 +21,8 @@
 
 static uint8_t prefix_buf[LC_SIZE] __attribute__((aligned(LC_SIZE)));
 static unsigned int tmp[1024] __attribute__((aligned(PAGE_SIZE)));
-static void *root, *mem, *m;
+static void *mem, *m;
+static pgd_t *root;
 volatile unsigned int *p;
 
 /*
@@ -237,7 +238,7 @@ static unsigned int setup(void)
 
 	/* Setup DAT 1:1 mapping and memory management */
 	setup_vm();
-	root = (void *)(stctg(1) & PAGE_MASK);
+	root = get_primary_page_root();
 
 	/*
 	 * Get a pgd worth of virtual memory, so we can test things later
s390x/mvpg-sie.c +1 -1
index 893de2cf..3050a8bb 100644
@@ -89,7 +89,7 @@ static void setup_guest(void)
 	pgd_t *root;
 
 	setup_vm();
-	root = (pgd_t *)(stctg(1) & PAGE_MASK);
+	root = get_primary_page_root();
 
 	snippet_setup_guest(&vm, false);
 	snippet_init(&vm, SNIPPET_NAME_START(c, mvpg_snippet),
s390x/pv-edat1.c +2 -2
index 6acfe6c4..e423f2fc 100644
@@ -34,7 +34,7 @@
 #define PARAM(n, step)	(((unsigned long)(n) << 32) | (step))
 
 static struct vm vm;
-static void *root;
+static pgd_t *root;
 
 extern const char SNIPPET_NAME_START(c, pv_memhog)[];
 extern const char SNIPPET_NAME_END(c, pv_memhog)[];
@@ -441,7 +441,7 @@ static void init(void)
 
 	setup_vm();
 
-	root = (void *)(stctg(1) & PAGE_MASK);
+	root = get_primary_page_root();
 	ctl_set_bit(0, CTL0_EDAT);
 
 	guest_memory = alloc_pages(GUEST_ORDER - PAGE_SHIFT);
s390x/sie-dat.c +1 -1
index 44bf29fe..e40e348f 100644
@@ -69,7 +69,7 @@ static void setup_guest(void)
 	pgd_t *root;
 
 	setup_vm();
-	root = (pgd_t *)(stctg(1) & PAGE_MASK);
+	root = get_primary_page_root();
 
 	snippet_setup_guest(&vm, false);
 
s390x/skey.c +3 -4
index 7c7a8090..bb769730 100644
@@ -13,6 +13,7 @@
 #include <asm/interrupt.h>
 #include <vmalloc.h>
 #include <css.h>
+#include <mmu.h>
 #include <asm/page.h>
 #include <asm/facility.h>
 #include <asm/mem.h>
@@ -465,10 +466,9 @@ static void test_set_prefix(void)
 	uint32_t *prefix_ptr = (uint32_t *)pagebuf;
 	uint32_t *no_override_prefix_ptr;
 	uint32_t old_prefix;
-	pgd_t *root;
+	pgd_t *root = get_primary_page_root();
 
 	report_prefix_push("SET PREFIX");
-	root = (pgd_t *)(stctg(1) & PAGE_MASK);
 	old_prefix = get_prefix();
 	memcpy(lowcore_tmp, 0, sizeof(lowcore_tmp));
 	assert(((uint64_t)&lowcore_tmp >> 31) == 0);
@@ -583,11 +583,10 @@ static void test_msch(void)
 	struct schib *schib = (struct schib *)pagebuf;
 	struct schib *no_override_schib;
 	int test_device_sid;
-	pgd_t *root;
+	pgd_t *root = get_primary_page_root();
 	int cc;
 
 	report_prefix_push("MSCH");
-	root = (pgd_t *)(stctg(1) & PAGE_MASK);
 	test_device_sid = css_enumerate();
 
 	if (!(test_device_sid & SCHID_ONE)) {
Signature · 1 line
2.51.0
Reviewed-by on v2 1/4
#2 lore raw

Re: [kvm-unit-tests PATCH v2 1/4] lib: s390x: Add function to get page root

Quoted 4 lines · Janosch Frank

It's time to hide the access to cr1 behind a function and add typing to the tests that used void* instead of pgdt_t*.

Quoted 10 lines
---
 lib/s390x/mmu.h  | 7 +++++++
 lib/s390x/sie.c  | 3 ++-
 s390x/diag258.c  | 4 ++--
 s390x/edat.c     | 5 +++--
 s390x/mvpg-sie.c | 2 +-
 s390x/pv-edat1.c | 4 ++--
 s390x/sie-dat.c  | 2 +-
 s390x/skey.c     | 7 +++----
 8 files changed, 21 insertions(+), 13 deletions(-)
#4 lore raw

PATCH v2 2/4 lib: s390x: sie: Allocate physical guest memory via memalign

In reply to Janosch Frank

alloc_pages_flags() alignes the allocation on the same order as its requested size. Since we use virtual memory for SIE we can instead align to 1MB by using memalign() which is less wasteful.

---
 lib/s390x/sie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/s390x/sie.c +2 -1
index 47d4cdde..b1b0a8a0 100644
@@ -171,7 +171,8 @@ uint8_t *sie_guest_alloc(uint64_t guest_size)
 	guest_virt = (uint8_t *)ALIGN(get_ram_size() + guest_counter * 4UL * SZ_1G, SZ_2G);
 	guest_counter++;
 
-	guest_phys = alloc_pages(get_order(guest_size) - 12);
+	guest_phys = memalign_pages_flags(SZ_1M, guest_size, 0);
+	assert(guest_phys);
 	/*
 	 * Establish a new mapping of the guest memory so it can be 2GB aligned
 	 * without actually requiring 2GB physical memory.
Signature · 1 line
2.51.0
#5 lore raw

Re: [kvm-unit-tests PATCH v2 2/4] lib: s390x: sie: Allocate physical guest memory via memalign

Quoted 14 lines · Janosch Frank

alloc_pages_flags() alignes the allocation on the same order as its requested size. Since we use virtual memory for SIE we can instead align to 1MB by using memalign() which is less wasteful.

---
 lib/s390x/sie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/s390x/sie.c +0 -0
index 47d4cdde..b1b0a8a0 100644

lib/s390x/sie.c uint8_t *sie_guest_alloc(uint64_t guest_size) · line 174

Quoted 5 lines
lib/s390x/sie.c +1 -1
@@ -171,7 +171,8 @@ uint8_t *sie_guest_alloc(uint64_t guest_size)
 	guest_virt = (uint8_t *)ALIGN(get_ram_size() + guest_counter * 4UL * SZ_1G, SZ_2G);
 	guest_counter++;
 
-	guest_phys = alloc_pages(get_order(guest_size) - 12);
+	guest_phys = memalign_pages_flags(SZ_1M, guest_size, 0);

There is a const for that:

guest_phys = memalign_pages_flags(SZ_1M, guest_size, AREA_ANY);

Quoted 4 lines
lib/s390x/sie.c +1 -0
+	assert(guest_phys);
 	/*
 	 * Establish a new mapping of the guest memory so it can be 2GB aligned
 	 * without actually requiring 2GB physical memory.
#6 lore raw

Re: [kvm-unit-tests PATCH v2 2/4] lib: s390x: sie: Allocate physical guest memory via memalign

Quoted 18 lines · Christoph Schlameuss
Quoted 17 lines · Janosch Frank

alloc_pages_flags() alignes the allocation on the same order as its requested size. Since we use virtual memory for SIE we can instead align to 1MB by using memalign() which is less wasteful.

---
 lib/s390x/sie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/s390x/sie.c +0 -0
index 47d4cdde..b1b0a8a0 100644
@@ -171,7 +171,8 @@ uint8_t *sie_guest_alloc(uint64_t guest_size)
 	guest_virt = (uint8_t *)ALIGN(get_ram_size() + guest_counter * 4UL * SZ_1G, SZ_2G);
 	guest_counter++;
 
Quoted 7 lines

lib/s390x/sie.c uint8_t *sie_guest_alloc(uint64_t guest_size) · line 174

Quoted 2 lines
lib/s390x/sie.c +1 -1
-	guest_phys = alloc_pages(get_order(guest_size) - 12);
+	guest_phys = memalign_pages_flags(SZ_1M, guest_size, 0);

There is a const for that:

guest_phys = memalign_pages_flags(SZ_1M, guest_size, AREA_ANY);

Oh, and with that fixed:

Quoted 4 lines
Quoted 4 lines
lib/s390x/sie.c +1 -0
+	assert(guest_phys);
 	/*
 	 * Establish a new mapping of the guest memory so it can be 2GB aligned
 	 * without actually requiring 2GB physical memory.
Reviewed-by
#7 lore raw

Re: [kvm-unit-tests PATCH v2 2/4] lib: s390x: sie: Allocate physical guest memory via memalign

In reply to Christoph Schlameuss
Quoted 18 lines · Christoph Schlameuss
Quoted 17 lines · Janosch Frank

alloc_pages_flags() alignes the allocation on the same order as its requested size. Since we use virtual memory for SIE we can instead align to 1MB by using memalign() which is less wasteful.

---
 lib/s390x/sie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/s390x/sie.c +0 -0
index 47d4cdde..b1b0a8a0 100644
@@ -171,7 +171,8 @@ uint8_t *sie_guest_alloc(uint64_t guest_size)
 	guest_virt = (uint8_t *)ALIGN(get_ram_size() + guest_counter * 4UL * SZ_1G, SZ_2G);
 	guest_counter++;
 
Quoted 6 lines

lib/s390x/sie.c uint8_t *sie_guest_alloc(uint64_t guest_size) · line 174

Quoted 2 lines
lib/s390x/sie.c +1 -1
-	guest_phys = alloc_pages(get_order(guest_size) - 12);
+	guest_phys = memalign_pages_flags(SZ_1M, guest_size, 0);

There is a const for that:

guest_phys = memalign_pages_flags(SZ_1M, guest_size, AREA_ANY);

Or just

guest_phys = memalign_pages(SZ_1M, guest_size);

#8 lore raw

PATCH v2 3/4 lib: s390x: sie: Memory rework

In reply to Janosch Frank

Make sie_guest_create() directly alloc the guest's memory.

Also we never freed the memory that the sie library allocates as the guest ram on destruction of the VM. Most tests reuse the VM or just leak the memory since the standard allocation is one megabyte and tests only use single digit numbers of VMs.

It's time to add automatic freeing to the sie library when a VM is destroyed.

---
 lib/s390x/sie.c     | 49 ++++++++++++++++++++++++---------------------
 lib/s390x/sie.h     |  2 +-
 lib/s390x/snippet.h |  9 +++++----
 s390x/mvpg-sie.c    |  2 +-
 s390x/pv-diags.c    |  2 +-
 s390x/pv-edat1.c    |  5 +----
 s390x/pv-icptcode.c |  4 ++--
 s390x/pv-ipl.c      |  2 +-
 s390x/sie-dat.c     |  2 +-
 s390x/sie.c         | 13 ++++--------
 s390x/spec_ex-sie.c |  2 ++
 s390x/stfle-sie.c   |  2 ++
 12 files changed, 47 insertions(+), 47 deletions(-)
lib/s390x/sie.c +26 -23
index b1b0a8a0..f0e1c41b 100644
@@ -121,29 +121,6 @@ void sie_guest_sca_create(struct vm *vm)
 	vm->sca->cpu[0].sda = (uint64_t)vm->sblk;
 }
 
-/* Initializes the struct vm members like the SIE control block. */
-void sie_guest_create(struct vm *vm, uint64_t guest_mem, uint64_t guest_mem_len)
-{
-	vm->sblk = alloc_page();
-	memset(vm->sblk, 0, PAGE_SIZE);
-	vm->sblk->cpuflags = CPUSTAT_ZARCH | CPUSTAT_RUNNING;
-	vm->sblk->ihcpu = 0xffff;
-	vm->sblk->prefix = 0;
-
-	/* Guest memory chunks are always 1MB */
-	assert(!(guest_mem_len & ~HPAGE_MASK));
-	vm->guest_mem = (uint8_t *)guest_mem;
-	/* For non-PV guests we re-use the host's ASCE for ease of use */
-	vm->save_area.guest.asce = stctg(1);
-	/* Currently MSO/MSL is the easiest option */
-	vm->sblk->mso = (uint64_t)guest_mem;
-	vm->sblk->msl = (uint64_t)guest_mem + ((guest_mem_len - 1) & HPAGE_MASK);
-
-	/* CRYCB needs to be in the first 2GB */
-	vm->crycb = alloc_pages_flags(0, AREA_DMA31);
-	vm->sblk->crycbd = (uint32_t)(uintptr_t)vm->crycb;
-}
-
 /**
  * sie_guest_alloc() - Allocate memory for a guest and map it in virtual address
  * space such that it is properly aligned.
@@ -185,6 +162,31 @@ uint8_t *sie_guest_alloc(uint64_t guest_size)
 	return guest_virt;
 }
 
+/* Initializes the struct vm members like the SIE control block. */
+void sie_guest_create(struct vm *vm, uint64_t guest_mem_len)
+{
+	void *guest_mem = sie_guest_alloc(guest_mem_len);
+
+	vm->sblk = alloc_page();
+	memset(vm->sblk, 0, PAGE_SIZE);
+	vm->sblk->cpuflags = CPUSTAT_ZARCH | CPUSTAT_RUNNING;
+	vm->sblk->ihcpu = 0xffff;
+	vm->sblk->prefix = 0;
+
+	/* Guest memory chunks are always 1MB */
+	assert(!(guest_mem_len & ~HPAGE_MASK));
+	vm->guest_mem = (uint8_t *)guest_mem;
+	/* For non-PV guests we re-use the host's ASCE for ease of use */
+	vm->save_area.guest.asce = stctg(1);
+	/* Currently MSO/MSL is the easiest option */
+	vm->sblk->mso = (uint64_t)guest_mem;
+	vm->sblk->msl = (uint64_t)guest_mem + ((guest_mem_len - 1) & HPAGE_MASK);
+
+	/* CRYCB needs to be in the first 2GB */
+	vm->crycb = alloc_pages_flags(0, AREA_DMA31);
+	vm->sblk->crycbd = (uint32_t)(uintptr_t)vm->crycb;
+}
+
 /* Frees the memory that was gathered on initialization */
 void sie_guest_destroy(struct vm *vm)
 {
@@ -192,4 +194,5 @@ void sie_guest_destroy(struct vm *vm)
 	free_page(vm->sblk);
 	if (vm->sblk->ecb2 & ECB2_ESCA)
 		free_page(vm->sca);
+	free_pages((void *)virt_to_pte_phys(get_primary_page_root(), vm->guest_mem));
 }
lib/s390x/sie.h +1 -1
index 3ec49ed0..85d691d5 100644
@@ -59,7 +59,7 @@ static inline bool sie_is_pv(struct vm *vm)
 }
 
 void sie_guest_sca_create(struct vm *vm);
-void sie_guest_create(struct vm *vm, uint64_t guest_mem, uint64_t guest_mem_len);
+void sie_guest_create(struct vm *vm, uint64_t guest_mem_len);
 void sie_guest_destroy(struct vm *vm);
 
 uint8_t *sie_guest_alloc(uint64_t guest_size);
lib/s390x/snippet.h +5 -4
index 910849aa..94688f49 100644
@@ -125,11 +125,8 @@ static inline void snippet_pv_init(struct vm *vm, const char *gbin,
 /* Allocates and sets up a snippet based guest */
 static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
 {
-	const unsigned long guest_size = SZ_1M;
-	uint8_t *guest_start = sie_guest_alloc(guest_size);
-
 	/* Initialize the vm struct and allocate control blocks */
-	sie_guest_create(vm, (uint64_t)guest_start, guest_size);
+	sie_guest_create(vm, SZ_1M);
 
 	if (is_pv) {
 		/* FMT4 needs a ESCA */
@@ -144,4 +141,8 @@ static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
 	}
 }
 
+static inline void snippet_destroy_guest(struct vm *vm)
+{
+	sie_guest_destroy(vm);
+}
 #endif
s390x/mvpg-sie.c +1 -1
index 3050a8bb..21082c53 100644
@@ -118,7 +118,7 @@ int main(void)
 	setup_guest();
 	test_mvpg();
 	test_mvpg_pei();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/pv-diags.c +1 -1
index 09b83d59..b6f08dd2 100644
@@ -156,7 +156,7 @@ int main(void)
 	test_diag_yield();
 	test_diag_288();
 	test_diag_500();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/pv-edat1.c +1 -4
index e423f2fc..ff762c75 100644
@@ -437,15 +437,12 @@ static bool check_facilities(void)
 
 static void init(void)
 {
-	uint8_t *guest_memory;
-
 	setup_vm();
 
 	root = get_primary_page_root();
 	ctl_set_bit(0, CTL0_EDAT);
 
-	guest_memory = alloc_pages(GUEST_ORDER - PAGE_SHIFT);
-	sie_guest_create(&vm, (uint64_t)guest_memory, GUEST_SIZE);
+	sie_guest_create(&vm, GUEST_SIZE);
 	sie_guest_sca_create(&vm);
 	uv_init();
 	uv_setup_asces();
s390x/pv-icptcode.c +2 -2
index 5293306b..bdef3a05 100644
@@ -164,7 +164,7 @@ static void test_validity_handle_not_in_config(void)
 
 	/* Destroy the second vm, since we don't need it for further tests */
 	uv_destroy_guest(&vm2);
-	sie_guest_destroy(&vm2);
+	snippet_destroy_guest(&vm2);
 
 	uv_destroy_guest(&vm);
 	report_prefix_pop();
@@ -368,7 +368,7 @@ int main(void)
 	test_validity_handle_not_in_config();
 	test_validity_already_running();
 	test_validity_timing();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/pv-ipl.c +1 -1
index 61a1e0c0..1219573f 100644
@@ -135,7 +135,7 @@ int main(void)
 	snippet_setup_guest(&vm, true);
 	test_diag_308(0);
 	test_diag_308(1);
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/sie-dat.c +1 -1
index e40e348f..a5ff0872 100644
@@ -101,7 +101,7 @@ int main(void)
 
 	setup_guest();
 	test_sie_dat();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/sie.c +4 -9
index ce5b6069..f08564b1 100644
@@ -20,7 +20,6 @@
 #include <sclp.h>
 #include <sie.h>
 
-static u8 *guest;
 static u8 *guest_instr;
 static struct vm vm;
 
@@ -70,7 +69,7 @@ static void test_epoch_ext(void)
 		return;
 	}
 
-	guest[0] = 0x00;
+	vm.guest_mem[0] = 0x00;
 	memcpy(guest_instr, instr, sizeof(instr));
 
 	vm.sblk->gpsw.addr = PAGE_SIZE * 2;
@@ -82,19 +81,15 @@ static void test_epoch_ext(void)
 	sie(&vm);
 
 	/* ... should result in the same epoch extension here: */
-	report(guest[0] == 0x47, "epdx: different epoch is visible in the guest");
+	report(vm.guest_mem[0] == 0x47, "epdx: different epoch is visible in the guest");
 }
 
 static void setup_guest(void)
 {
-	setup_vm();
-
-	guest = sie_guest_alloc(SZ_1M);
+	sie_guest_create(&vm, HPAGE_SIZE);
 
 	/* The first two pages are the lowcore */
-	guest_instr = guest + PAGE_SIZE * 2;
-
-	sie_guest_create(&vm, (uint64_t)guest, HPAGE_SIZE);
+	guest_instr = vm.guest_mem + PAGE_SIZE * 2;
 }
 
 int main(void)
s390x/spec_ex-sie.c +2 -0
index fe2f23ee..6ab4144c 100644
@@ -71,6 +71,8 @@ static void test_spec_ex_sie(void)
 		report_info("%s", msg);
 	else
 		report_info("Did not interpret initial exception");
+
+	snippet_destroy_guest(&vm);
 	report_prefix_pop();
 	report_prefix_pop();
 }
s390x/stfle-sie.c +2 -0
index 21cf8ff8..8df1185c 100644
@@ -133,6 +133,8 @@ int main(int argc, char **argv)
 	setup_guest();
 	if (run_format_0)
 		test_stfle_format_0();
+
+	snippet_destroy_guest(&vm);
 out:
 	return report_summary();
 }
Signature · 1 line
2.51.0
Reviewed-by on v2 3/4
#9 lore raw

Re: [kvm-unit-tests PATCH v2 3/4] lib: s390x: sie: Memory rework

Quoted 11 lines · Janosch Frank

Make sie_guest_create() directly alloc the guest's memory.

Also we never freed the memory that the sie library allocates as the guest ram on destruction of the VM. Most tests reuse the VM or just leak the memory since the standard allocation is one megabyte and tests only use single digit numbers of VMs.

It's time to add automatic freeing to the sie library when a VM is destroyed.

with nit fixed:

Quoted 68 lines
---
 lib/s390x/sie.c     | 49 ++++++++++++++++++++++++---------------------
 lib/s390x/sie.h     |  2 +-
 lib/s390x/snippet.h |  9 +++++----
 s390x/mvpg-sie.c    |  2 +-
 s390x/pv-diags.c    |  2 +-
 s390x/pv-edat1.c    |  5 +----
 s390x/pv-icptcode.c |  4 ++--
 s390x/pv-ipl.c      |  2 +-
 s390x/sie-dat.c     |  2 +-
 s390x/sie.c         | 13 ++++--------
 s390x/spec_ex-sie.c |  2 ++
 s390x/stfle-sie.c   |  2 ++
 12 files changed, 47 insertions(+), 47 deletions(-)
lib/s390x/sie.c +15 -23
index b1b0a8a0..f0e1c41b 100644
@@ -121,29 +121,6 @@ void sie_guest_sca_create(struct vm *vm)
 	vm->sca->cpu[0].sda = (uint64_t)vm->sblk;
 }
 
-/* Initializes the struct vm members like the SIE control block. */
-void sie_guest_create(struct vm *vm, uint64_t guest_mem, uint64_t guest_mem_len)
-{
-	vm->sblk = alloc_page();
-	memset(vm->sblk, 0, PAGE_SIZE);
-	vm->sblk->cpuflags = CPUSTAT_ZARCH | CPUSTAT_RUNNING;
-	vm->sblk->ihcpu = 0xffff;
-	vm->sblk->prefix = 0;
-
-	/* Guest memory chunks are always 1MB */
-	assert(!(guest_mem_len & ~HPAGE_MASK));
-	vm->guest_mem = (uint8_t *)guest_mem;
-	/* For non-PV guests we re-use the host's ASCE for ease of use */
-	vm->save_area.guest.asce = stctg(1);
-	/* Currently MSO/MSL is the easiest option */
-	vm->sblk->mso = (uint64_t)guest_mem;
-	vm->sblk->msl = (uint64_t)guest_mem + ((guest_mem_len - 1) & HPAGE_MASK);
-
-	/* CRYCB needs to be in the first 2GB */
-	vm->crycb = alloc_pages_flags(0, AREA_DMA31);
-	vm->sblk->crycbd = (uint32_t)(uintptr_t)vm->crycb;
-}
-
 /**
  * sie_guest_alloc() - Allocate memory for a guest and map it in virtual address
  * space such that it is properly aligned.
@@ -185,6 +162,31 @@ uint8_t *sie_guest_alloc(uint64_t guest_size)
 	return guest_virt;
 }
 
+/* Initializes the struct vm members like the SIE control block. */
+void sie_guest_create(struct vm *vm, uint64_t guest_mem_len)
+{
+	void *guest_mem = sie_guest_alloc(guest_mem_len);
+
+	vm->sblk = alloc_page();
+	memset(vm->sblk, 0, PAGE_SIZE);
+	vm->sblk->cpuflags = CPUSTAT_ZARCH | CPUSTAT_RUNNING;
+	vm->sblk->ihcpu = 0xffff;
+	vm->sblk->prefix = 0;
+
+	/* Guest memory chunks are always 1MB */
+	assert(!(guest_mem_len & ~HPAGE_MASK));
+	vm->guest_mem = (uint8_t *)guest_mem;
+	/* For non-PV guests we re-use the host's ASCE for ease of use */

lib/s390x/sie.c uint8_t *sie_guest_alloc(uint64_t guest_size) · line 189

Quoted 10 lines
lib/s390x/sie.c +10 -0
+	vm->save_area.guest.asce = stctg(1);
+	/* Currently MSO/MSL is the easiest option */
+	vm->sblk->mso = (uint64_t)guest_mem;
+	vm->sblk->msl = (uint64_t)guest_mem + ((guest_mem_len - 1) & HPAGE_MASK);
+
+	/* CRYCB needs to be in the first 2GB */
+	vm->crycb = alloc_pages_flags(0, AREA_DMA31);
+	vm->sblk->crycbd = (uint32_t)(uintptr_t)vm->crycb;
+}
+

nit:
This looks like you are moving that unchanged. Is that intended? If it is please add to the commit comment that this is moved unchanged at least or dont move it.

Quoted 213 lines
lib/s390x/sie.c +1 -0
 /* Frees the memory that was gathered on initialization */
 void sie_guest_destroy(struct vm *vm)
 {
@@ -192,4 +194,5 @@ void sie_guest_destroy(struct vm *vm)
 	free_page(vm->sblk);
 	if (vm->sblk->ecb2 & ECB2_ESCA)
 		free_page(vm->sca);
+	free_pages((void *)virt_to_pte_phys(get_primary_page_root(), vm->guest_mem));
 }
lib/s390x/sie.h +1 -1
index 3ec49ed0..85d691d5 100644
@@ -59,7 +59,7 @@ static inline bool sie_is_pv(struct vm *vm)
 }
 
 void sie_guest_sca_create(struct vm *vm);
-void sie_guest_create(struct vm *vm, uint64_t guest_mem, uint64_t guest_mem_len);
+void sie_guest_create(struct vm *vm, uint64_t guest_mem_len);
 void sie_guest_destroy(struct vm *vm);
 
 uint8_t *sie_guest_alloc(uint64_t guest_size);
lib/s390x/snippet.h +5 -4
index 910849aa..94688f49 100644
@@ -125,11 +125,8 @@ static inline void snippet_pv_init(struct vm *vm, const char *gbin,
 /* Allocates and sets up a snippet based guest */
 static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
 {
-	const unsigned long guest_size = SZ_1M;
-	uint8_t *guest_start = sie_guest_alloc(guest_size);
-
 	/* Initialize the vm struct and allocate control blocks */
-	sie_guest_create(vm, (uint64_t)guest_start, guest_size);
+	sie_guest_create(vm, SZ_1M);
 
 	if (is_pv) {
 		/* FMT4 needs a ESCA */
@@ -144,4 +141,8 @@ static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
 	}
 }
 
+static inline void snippet_destroy_guest(struct vm *vm)
+{
+	sie_guest_destroy(vm);
+}
 #endif
s390x/mvpg-sie.c +1 -1
index 3050a8bb..21082c53 100644
@@ -118,7 +118,7 @@ int main(void)
 	setup_guest();
 	test_mvpg();
 	test_mvpg_pei();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/pv-diags.c +1 -1
index 09b83d59..b6f08dd2 100644
@@ -156,7 +156,7 @@ int main(void)
 	test_diag_yield();
 	test_diag_288();
 	test_diag_500();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/pv-edat1.c +1 -4
index e423f2fc..ff762c75 100644
@@ -437,15 +437,12 @@ static bool check_facilities(void)
 
 static void init(void)
 {
-	uint8_t *guest_memory;
-
 	setup_vm();
 
 	root = get_primary_page_root();
 	ctl_set_bit(0, CTL0_EDAT);
 
-	guest_memory = alloc_pages(GUEST_ORDER - PAGE_SHIFT);
-	sie_guest_create(&vm, (uint64_t)guest_memory, GUEST_SIZE);
+	sie_guest_create(&vm, GUEST_SIZE);
 	sie_guest_sca_create(&vm);
 	uv_init();
 	uv_setup_asces();
s390x/pv-icptcode.c +2 -2
index 5293306b..bdef3a05 100644
@@ -164,7 +164,7 @@ static void test_validity_handle_not_in_config(void)
 
 	/* Destroy the second vm, since we don't need it for further tests */
 	uv_destroy_guest(&vm2);
-	sie_guest_destroy(&vm2);
+	snippet_destroy_guest(&vm2);
 
 	uv_destroy_guest(&vm);
 	report_prefix_pop();
@@ -368,7 +368,7 @@ int main(void)
 	test_validity_handle_not_in_config();
 	test_validity_already_running();
 	test_validity_timing();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/pv-ipl.c +1 -1
index 61a1e0c0..1219573f 100644
@@ -135,7 +135,7 @@ int main(void)
 	snippet_setup_guest(&vm, true);
 	test_diag_308(0);
 	test_diag_308(1);
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/sie-dat.c +1 -1
index e40e348f..a5ff0872 100644
@@ -101,7 +101,7 @@ int main(void)
 
 	setup_guest();
 	test_sie_dat();
-	sie_guest_destroy(&vm);
+	snippet_destroy_guest(&vm);
 
 done:
 	report_prefix_pop();
s390x/sie.c +4 -9
index ce5b6069..f08564b1 100644
@@ -20,7 +20,6 @@
 #include <sclp.h>
 #include <sie.h>
 
-static u8 *guest;
 static u8 *guest_instr;
 static struct vm vm;
 
@@ -70,7 +69,7 @@ static void test_epoch_ext(void)
 		return;
 	}
 
-	guest[0] = 0x00;
+	vm.guest_mem[0] = 0x00;
 	memcpy(guest_instr, instr, sizeof(instr));
 
 	vm.sblk->gpsw.addr = PAGE_SIZE * 2;
@@ -82,19 +81,15 @@ static void test_epoch_ext(void)
 	sie(&vm);
 
 	/* ... should result in the same epoch extension here: */
-	report(guest[0] == 0x47, "epdx: different epoch is visible in the guest");
+	report(vm.guest_mem[0] == 0x47, "epdx: different epoch is visible in the guest");
 }
 
 static void setup_guest(void)
 {
-	setup_vm();
-
-	guest = sie_guest_alloc(SZ_1M);
+	sie_guest_create(&vm, HPAGE_SIZE);
 
 	/* The first two pages are the lowcore */
-	guest_instr = guest + PAGE_SIZE * 2;
-
-	sie_guest_create(&vm, (uint64_t)guest, HPAGE_SIZE);
+	guest_instr = vm.guest_mem + PAGE_SIZE * 2;
 }
 
 int main(void)
s390x/spec_ex-sie.c +2 -0
index fe2f23ee..6ab4144c 100644
@@ -71,6 +71,8 @@ static void test_spec_ex_sie(void)
 		report_info("%s", msg);
 	else
 		report_info("Did not interpret initial exception");
+
+	snippet_destroy_guest(&vm);
 	report_prefix_pop();
 	report_prefix_pop();
 }
s390x/stfle-sie.c +2 -0
index 21cf8ff8..8df1185c 100644
@@ -133,6 +133,8 @@ int main(int argc, char **argv)
 	setup_guest();
 	if (run_format_0)
 		test_stfle_format_0();
+
+	snippet_destroy_guest(&vm);
 out:
 	return report_summary();
 }
#10 lore raw

PATCH v2 4/4 lib: s390x: snippet: Add function to create a guest of specific length

In reply to Janosch Frank

While 1MB is certainly enough to store the guest code, it's often not enough for memory tests. Let's add a separate function to allow arbitrary guest sizes.

---
 lib/s390x/snippet.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/s390x/snippet.h +12 -2
index 94688f49..05c9a2d4 100644
@@ -123,10 +123,14 @@ static inline void snippet_pv_init(struct vm *vm, const char *gbin,
 }
 
 /* Allocates and sets up a snippet based guest */
-static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
+static inline void snippet_setup_guest_len(struct vm *vm, bool is_pv,
+					   unsigned long len)
 {
+	/* Guest sizes are specified in megabyte chunks */
+	assert(!(len & ~HPAGE_MASK));
+
 	/* Initialize the vm struct and allocate control blocks */
-	sie_guest_create(vm, SZ_1M);
+	sie_guest_create(vm, len);
 
 	if (is_pv) {
 		/* FMT4 needs a ESCA */
@@ -141,6 +145,12 @@ static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
 	}
 }
 
+/* Allocates and sets up a snippet based guest */
+static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
+{
+	snippet_setup_guest_len(vm, is_pv, SZ_1M);
+}
+
 static inline void snippet_destroy_guest(struct vm *vm)
 {
 	sie_guest_destroy(vm);
Signature · 1 line
2.51.0
Reviewed-by on v2 4/4
#11 lore raw

Re: [kvm-unit-tests PATCH v2 4/4] lib: s390x: snippet: Add function to create a guest of specific length

Quoted 6 lines · Janosch Frank

While 1MB is certainly enough to store the guest code, it's often not enough for memory tests. Let's add a separate function to allow arbitrary guest sizes.

Quoted 38 lines
---
 lib/s390x/snippet.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/s390x/snippet.h +12 -2
index 94688f49..05c9a2d4 100644
@@ -123,10 +123,14 @@ static inline void snippet_pv_init(struct vm *vm, const char *gbin,
 }
 
 /* Allocates and sets up a snippet based guest */
-static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
+static inline void snippet_setup_guest_len(struct vm *vm, bool is_pv,
+					   unsigned long len)
 {
+	/* Guest sizes are specified in megabyte chunks */
+	assert(!(len & ~HPAGE_MASK));
+
 	/* Initialize the vm struct and allocate control blocks */
-	sie_guest_create(vm, SZ_1M);
+	sie_guest_create(vm, len);
 
 	if (is_pv) {
 		/* FMT4 needs a ESCA */
@@ -141,6 +145,12 @@ static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
 	}
 }
 
+/* Allocates and sets up a snippet based guest */
+static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
+{
+	snippet_setup_guest_len(vm, is_pv, SZ_1M);
+}
+
 static inline void snippet_destroy_guest(struct vm *vm)
 {
 	sie_guest_destroy(vm);