Skip to contentSkip to conversation

kvm: Add VM memory access helper functions to host library

PATCH In review Martin Doucha 2 participants 4 messages May 6, 2022 – May 19, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 kvm: Add VM memory access helper functions to host library

---
 testcases/kernel/kvm/include/kvm_host.h |  28 +++++-
 testcases/kernel/kvm/lib_host.c         | 112 +++++++++++++++++++-----
 2 files changed, 117 insertions(+), 23 deletions(-)
testcases/kernel/kvm/include/kvm_host.h +25 -3
index 52cc3f5e9..2359944fd 100644
@@ -45,12 +45,13 @@
 
 #define MIN_FREE_RAM (10 * 1024 * 1024)
 #define DEFAULT_RAM_SIZE (16 * 1024 * 1024)
+#define MAX_KVM_MEMSLOTS 8
 
 struct tst_kvm_instance {
 	int vm_fd, vcpu_fd;
 	struct kvm_run *vcpu_info;
 	size_t vcpu_info_size;
-	void *ram;
+	struct kvm_userspace_memory_region ram[MAX_KVM_MEMSLOTS];
 	struct tst_kvm_result *result;
 };
 
@@ -82,8 +83,29 @@ void tst_kvm_validate_result(int value);
  * to free() it. Any extra space added at the beginning or end for page
  * alignment will be writable.
  */
-void *tst_kvm_alloc_memory(int vm, unsigned int slot, uint64_t baseaddr,
-	size_t size, unsigned int flags);
+void *tst_kvm_alloc_memory(struct tst_kvm_instance *inst, unsigned int slot,
+	uint64_t baseaddr, size_t size, unsigned int flags);
+
+/*
+ * Translate VM virtual memory address to the corresponding physical address.
+ * Returns 0 if the virtual address is unmapped or otherwise invalid.
+ */
+uint64_t tst_kvm_get_phys_address(const struct tst_kvm_instance *inst,
+	uint64_t addr);
+
+/*
+ * Find the struct tst_kvm_instance memory slot ID for the give virtual
+ * or physical VM memory address. Returns -1 if the address is not backed
+ * by any memory buffer.
+ */
+int tst_kvm_find_phys_memslot(const struct tst_kvm_instance *inst,
+	uint64_t paddr);
+int tst_kvm_find_memslot(const struct tst_kvm_instance *inst, uint64_t addr);
+
+/*
+ * Convert VM virtual memory address to a directly usable pointer.
+ */
+void *tst_kvm_get_memptr(const struct tst_kvm_instance *inst, uint64_t addr);
 
 /*
  * Find CPUIDs supported by KVM. x86_64 tests must set non-default CPUID,
testcases/kernel/kvm/lib_host.c +92 -20

163 lines not on this page · raw message

Signature · 5 lines
2.36.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/2 kvm: Allow running multiple iterations of a test

---

This fixes running the test with "-i N" for N>1. It's a lazy fix but the alternative would be saving the initial CPU state in up to 3 separate buffers and then restoring it after each test run. And the number of buffers differs on different archs. And ARM64 has an extra ioctl() instead...

I also wonder whether I should implement LTP library function to free individual guarded buffers because I don't like calling tst_free_all() directly.

 testcases/kernel/kvm/lib_host.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
testcases/kernel/kvm/lib_host.c +5 -1
index b8994f34e..2782e68b0 100644
@@ -269,16 +269,20 @@ void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)
 		SAFE_CLOSE(inst->vcpu_fd);
 
 	SAFE_CLOSE(inst->vm_fd);
+	memset(inst->ram, 0, sizeof(inst->ram));
 }
 
 void tst_kvm_setup(void)
 {
-	tst_kvm_create_instance(&test_vm, DEFAULT_RAM_SIZE);
+
 }
 
 void tst_kvm_run(void)
 {
+	tst_kvm_create_instance(&test_vm, DEFAULT_RAM_SIZE);
 	tst_kvm_run_instance(&test_vm);
+	tst_kvm_destroy_instance(&test_vm);
+	tst_free_all();
 }
 
 void tst_kvm_cleanup(void)
Signature · 5 lines
2.36.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH 2/2] kvm: Allow running multiple iterations of a test

Hi!

Quoted 8 lines

This fixes running the test with "-i N" for N>1. It's a lazy fix but the alternative would be saving the initial CPU state in up to 3 separate buffers and then restoring it after each test run. And the number of buffers differs on different archs. And ARM64 has an extra ioctl() instead...

I also wonder whether I should implement LTP library function to free individual guarded buffers because I don't like calling tst_free_all() directly.

Well I guess that we can do that later on. It should be easy enough if we switch to a double linked list and do a linear search, after all I doubt that we will allocate more than a few buffers this way.

Quoted 17 lines
 testcases/kernel/kvm/lib_host.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
testcases/kernel/kvm/lib_host.c +1 -1
index b8994f34e..2782e68b0 100644
@@ -269,16 +269,20 @@ void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)
 		SAFE_CLOSE(inst->vcpu_fd);
 
 	SAFE_CLOSE(inst->vm_fd);
+	memset(inst->ram, 0, sizeof(inst->ram));
 }
 
 void tst_kvm_setup(void)
 {
-	tst_kvm_create_instance(&test_vm, DEFAULT_RAM_SIZE);

testcases/kernel/kvm/lib_host.c void tst_kvm_destroy_instance(struct tst_kvm_instance *inst) · line 286

Quoted 10 lines
testcases/kernel/kvm/lib_host.c +4 -0
+
 }
 
 void tst_kvm_run(void)
 {
+	tst_kvm_create_instance(&test_vm, DEFAULT_RAM_SIZE);
 	tst_kvm_run_instance(&test_vm);
+	tst_kvm_destroy_instance(&test_vm);
+	tst_free_all();
 }

Pushed, thanks.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp