From: Patrick Roy <roypat@amazon.co.uk>
To: <rppt@kernel.org>, <david@redhat.com>, <seanjc@google.com>
Cc: Patrick Roy <roypat@amazon.co.uk>, <pbonzini@redhat.com>,
<corbet@lwn.net>, <willy@infradead.org>,
<akpm@linux-foundation.org>, <song@kernel.org>,
<jolsa@kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>,
<andrii@kernel.org>, <martin.lau@linux.dev>, <eddyz87@gmail.com>,
<yonghong.song@linux.dev>, <john.fastabend@gmail.com>,
<kpsingh@kernel.org>, <sdf@fomichev.me>, <haoluo@google.com>,
<Liam.Howlett@oracle.com>, <lorenzo.stoakes@oracle.com>,
<vbabka@suse.cz>, <jannh@google.com>, <shuah@kernel.org>,
<kvm@vger.kernel.org>, <linux-doc@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <linux-fsdevel@vger.kernel.org>,
<linux-mm@kvack.org>, <bpf@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <tabba@google.com>,
<jgowans@amazon.com>, <graf@amazon.com>, <kalyazin@amazon.com>,
<xmarcalx@amazon.com>, <derekmn@amazon.com>,
<jthoughton@google.com>
Subject: [PATCH v4 12/12] KVM: selftests: Test guest execution from direct map removed gmem
Date: Fri, 21 Feb 2025 16:07:25 +0000 [thread overview]
Message-ID: <20250221160728.1584559-13-roypat@amazon.co.uk> (raw)
In-Reply-To: <20250221160728.1584559-1-roypat@amazon.co.uk>
Add a selftest that loads itself into guest_memfd (via
KVM_GMEM_SHARED_MEM) and triggers an MMIO exit when executed. This
exercises x86 MMIO emulation code inside KVM for guest_memfd-backed
memslots where the guest_memfd folios are direct map removed.
Particularly, it validates that x86 MMIO emulation code (guest page
table walks + instruction fetch) correctly accesses gmem through the VMA
that's been reflected into the memslot's userspace_addr field (instead
of trying to do direct map accesses).
Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
---
.../selftests/kvm/set_memory_region_test.c | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
index bc440d5aba57..16bbfe117a17 100644
--- a/tools/testing/selftests/kvm/set_memory_region_test.c
+++ b/tools/testing/selftests/kvm/set_memory_region_test.c
@@ -603,6 +603,42 @@ static void test_mmio_during_vectoring(void)
kvm_vm_free(vm);
}
+
+static void guest_code_trigger_mmio(void)
+{
+ /*
+ * Read some GPA that is not backed by a memslot. KVM consider this
+ * as MMIO and tell userspace to emulate the read.
+ */
+ READ_ONCE(*((uint64_t *)MEM_REGION_GPA));
+
+ GUEST_DONE();
+}
+
+static void test_guest_memfd_mmio(void)
+{
+ struct kvm_vm *vm;
+ struct kvm_vcpu *vcpu;
+ struct vm_shape shape = {
+ .mode = VM_MODE_DEFAULT,
+ .type = KVM_X86_SW_PROTECTED_VM,
+ .src_type = VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP,
+ };
+ pthread_t vcpu_thread;
+
+ pr_info("Testing MMIO emulation for instructions in gmem\n");
+
+ vm = __vm_create_shape_with_one_vcpu(shape, &vcpu, 0, guest_code_trigger_mmio);
+
+ virt_map(vm, MEM_REGION_GPA, MEM_REGION_GPA, 1);
+
+ pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu);
+
+ /* If the MMIO read was successfully emulated, the vcpu thread will exit */
+ pthread_join(vcpu_thread, NULL);
+
+ kvm_vm_free(vm);
+}
#endif
int main(int argc, char *argv[])
@@ -630,6 +666,10 @@ int main(int argc, char *argv[])
(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) {
test_add_private_memory_region();
test_add_overlapping_private_memory_regions();
+ if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM) && kvm_has_cap(KVM_CAP_GMEM_NO_DIRECT_MAP))
+ test_guest_memfd_mmio();
+ else
+ pr_info("Skipping tests requiring KVM_CAP_GMEM_SHARED_MEM | KVM_CAP_GMEM_NO_DIRECT_MAP");
} else {
pr_info("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n");
}
--
2.48.1
prev parent reply other threads:[~2025-02-21 16:10 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-21 16:07 [PATCH v4 00/12] Direct Map Removal for guest_memfd Patrick Roy
2025-02-21 16:07 ` [PATCH v4 01/12] mm: introduce AS_NO_DIRECT_MAP Patrick Roy
2025-02-25 16:52 ` David Hildenbrand
2025-03-03 9:29 ` Vlastimil Babka
2025-02-21 16:07 ` [PATCH v4 02/12] mm/secretmem: set AS_NO_DIRECT_MAP instead of special-casing Patrick Roy
2025-02-25 16:52 ` David Hildenbrand
2025-02-26 8:44 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map Patrick Roy
2025-02-25 16:54 ` David Hildenbrand
2025-02-26 8:48 ` Patrick Roy
2025-02-26 9:08 ` David Hildenbrand
2025-02-26 15:14 ` Patrick Roy
2025-02-26 15:30 ` David Hildenbrand
2025-03-19 7:53 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 04/12] KVM: Add capability to discover KVM_GMEM_NO_DIRECT_MAP support Patrick Roy
2025-02-25 16:55 ` David Hildenbrand
2025-02-26 8:37 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 05/12] KVM: Documentation: document KVM_GMEM_NO_DIRECT_MAP flag Patrick Roy
2025-02-21 16:07 ` [PATCH v4 06/12] KVM: selftests: load elf via bounce buffer Patrick Roy
2025-02-21 16:07 ` [PATCH v4 07/12] KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1 Patrick Roy
2025-02-21 16:07 ` [PATCH v4 08/12] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types Patrick Roy
2025-02-25 14:12 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 09/12] KVM: selftests: stuff vm_mem_backing_src_type into vm_shape Patrick Roy
2025-02-21 16:07 ` [PATCH v4 10/12] KVM: selftests: adjust test_create_guest_memfd_invalid Patrick Roy
2025-02-21 16:07 ` [PATCH v4 11/12] KVM: selftests: set KVM_GMEM_NO_DIRECT_MAP in mem conversion tests Patrick Roy
2025-02-21 16:07 ` Patrick Roy [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250221160728.1584559-13-roypat@amazon.co.uk \
--to=roypat@amazon.co.uk \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=david@redhat.com \
--cc=derekmn@amazon.com \
--cc=eddyz87@gmail.com \
--cc=graf@amazon.com \
--cc=haoluo@google.com \
--cc=jannh@google.com \
--cc=jgowans@amazon.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=jthoughton@google.com \
--cc=kalyazin@amazon.com \
--cc=kpsingh@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=martin.lau@linux.dev \
--cc=pbonzini@redhat.com \
--cc=rppt@kernel.org \
--cc=sdf@fomichev.me \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tabba@google.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
--cc=xmarcalx@amazon.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox