[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220408210545.3915712-5-vannapurve@google.com>
Date: Fri, 8 Apr 2022 21:05:44 +0000
From: Vishal Annapurve <vannapurve@...gle.com>
To: x86@...nel.org, kvm@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-kselftest@...r.kernel.org
Cc: pbonzini@...hat.com, vkuznets@...hat.com, wanpengli@...cent.com,
jmattson@...gle.com, joro@...tes.org, tglx@...utronix.de,
mingo@...hat.com, bp@...en8.de, dave.hansen@...ux.intel.com,
hpa@...or.com, shauh@...nel.org, yang.zhong@...el.com,
drjones@...hat.com, ricarkol@...gle.com, aaronlewis@...gle.com,
wei.w.wang@...el.com, kirill.shutemov@...ux.intel.com,
corbet@....net, hughd@...gle.com, jlayton@...nel.org,
bfields@...ldses.org, akpm@...ux-foundation.org,
chao.p.peng@...ux.intel.com, yu.c.zhang@...ux.intel.com,
jun.nakajima@...el.com, dave.hansen@...el.com,
michael.roth@....com, qperret@...gle.com, steven.price@....com,
ak@...ux.intel.com, david@...hat.com, luto@...nel.org,
vbabka@...e.cz, marcorr@...gle.com, erdemaktas@...gle.com,
pgonda@...gle.com, seanjc@...gle.com, diviness@...gle.com,
Vishal Annapurve <vannapurve@...gle.com>
Subject: [RFC V1 PATCH 4/5] selftests: kvm: priv_memfd_test: Add support for
memory conversion
Add handling of explicit private/shared memory conversion using
KVM_HC_MAP_GPA_RANGE and implicit memory conversion by handling
KVM_EXIT_MEMORY_ERROR.
Signed-off-by: Vishal Annapurve <vannapurve@...gle.com>
---
tools/testing/selftests/kvm/priv_memfd_test.c | 87 +++++++++++++++++++
1 file changed, 87 insertions(+)
diff --git a/tools/testing/selftests/kvm/priv_memfd_test.c b/tools/testing/selftests/kvm/priv_memfd_test.c
index 11ccdb853a84..0e6c19501f27 100644
--- a/tools/testing/selftests/kvm/priv_memfd_test.c
+++ b/tools/testing/selftests/kvm/priv_memfd_test.c
@@ -129,6 +129,83 @@ static struct test_run_helper priv_memfd_testsuite[] = {
},
};
+static void handle_vm_exit_hypercall(struct kvm_run *run,
+ uint32_t test_id)
+{
+ uint64_t gpa, npages, attrs;
+ int priv_memfd =
+ priv_memfd_testsuite[test_id].priv_memfd;
+ int ret;
+ int fallocate_mode;
+
+ if (run->hypercall.nr != KVM_HC_MAP_GPA_RANGE) {
+ TEST_FAIL("Unhandled Hypercall %lld\n",
+ run->hypercall.nr);
+ }
+
+ gpa = run->hypercall.args[0];
+ npages = run->hypercall.args[1];
+ attrs = run->hypercall.args[2];
+
+ if ((gpa >= TEST_MEM_GPA) && ((gpa +
+ (npages << MIN_PAGE_SHIFT)) <= TEST_MEM_END)) {
+ TEST_FAIL("Unhandled gpa 0x%lx npages %ld\n",
+ gpa, npages);
+ }
+
+ if (attrs & KVM_MAP_GPA_RANGE_ENCRYPTED)
+ fallocate_mode = 0;
+ else {
+ fallocate_mode = (FALLOC_FL_PUNCH_HOLE |
+ FALLOC_FL_KEEP_SIZE);
+ }
+ pr_info("Converting off 0x%lx pages 0x%lx to %s\n",
+ (gpa - TEST_MEM_GPA), npages,
+ fallocate_mode ?
+ "shared" : "private");
+ ret = fallocate(priv_memfd, fallocate_mode,
+ (gpa - TEST_MEM_GPA),
+ npages << MIN_PAGE_SHIFT);
+ TEST_ASSERT(ret != -1,
+ "fallocate failed in hc handling");
+ run->hypercall.ret = 0;
+}
+
+static void handle_vm_exit_memory_error(struct kvm_run *run,
+ uint32_t test_id)
+{
+ uint64_t gpa, size, flags;
+ int ret;
+ int priv_memfd =
+ priv_memfd_testsuite[test_id].priv_memfd;
+ int fallocate_mode;
+
+ gpa = run->memory.gpa;
+ size = run->memory.size;
+ flags = run->memory.flags;
+
+ if ((gpa < TEST_MEM_GPA) || ((gpa + size)
+ > TEST_MEM_END)) {
+ TEST_FAIL("Unhandled gpa 0x%lx size 0x%lx\n",
+ gpa, size);
+ }
+
+ if (flags & KVM_MEMORY_EXIT_FLAG_PRIVATE)
+ fallocate_mode = 0;
+ else {
+ fallocate_mode = (FALLOC_FL_PUNCH_HOLE |
+ FALLOC_FL_KEEP_SIZE);
+ }
+ pr_info("Converting off 0x%lx size 0x%lx to %s\n",
+ (gpa - TEST_MEM_GPA), size,
+ fallocate_mode ?
+ "shared" : "private");
+ ret = fallocate(priv_memfd, fallocate_mode,
+ (gpa - TEST_MEM_GPA), size);
+ TEST_ASSERT(ret != -1,
+ "fallocate failed in memory error handling");
+}
+
static void vcpu_work(struct kvm_vm *vm, uint32_t test_id)
{
struct kvm_run *run;
@@ -155,6 +232,16 @@ static void vcpu_work(struct kvm_vm *vm, uint32_t test_id)
continue;
}
+ if (run->exit_reason == KVM_EXIT_HYPERCALL) {
+ handle_vm_exit_hypercall(run, test_id);
+ continue;
+ }
+
+ if (run->exit_reason == KVM_EXIT_MEMORY_ERROR) {
+ handle_vm_exit_memory_error(run, test_id);
+ continue;
+ }
+
TEST_FAIL("Unhandled VCPU exit reason %d\n", run->exit_reason);
break;
}
--
2.35.1.1178.g4f1659d476-goog
Powered by blists - more mailing lists