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 08/12] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types
Date: Fri, 21 Feb 2025 16:07:21 +0000 [thread overview]
Message-ID: <20250221160728.1584559-9-roypat@amazon.co.uk> (raw)
In-Reply-To: <20250221160728.1584559-1-roypat@amazon.co.uk>
Allow selftests to configure their memslots such that userspace_addr is
set to a MAP_SHARED mapping of the guest_memfd that's associated with
the memslot. This setup is the configuration for non-CoCo VMs, where all
guest memory is backed by a guest_memfd whose folios are all marked
shared, but KVM is still able to access guest memory to provide
functionality such as MMIO emulation on x86.
Add backing types for normal guest_memfd, as well as direct map removed
guest_memfd.
If KVM_CAP_MEMORY_ATTRIBUTES is available, explicitly set gmem-enabled
memslots to private, as otherwise guest page faults will be resolved by
GUP-ing the guest_memfd VMA (instead of using the special VMA-less
guest_memfd fault code in the KVM MMU), but this is not always supported
(e.g. if direct map entries are not available).
Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
---
.../testing/selftests/kvm/include/kvm_util.h | 10 +++
.../testing/selftests/kvm/include/test_util.h | 7 ++
tools/testing/selftests/kvm/lib/kvm_util.c | 67 ++++++++++---------
tools/testing/selftests/kvm/lib/test_util.c | 8 +++
4 files changed, 62 insertions(+), 30 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 4c4e5a847f67..baeddec7c264 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -544,6 +544,16 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
void vm_create_irqchip(struct kvm_vm *vm);
+static inline bool backing_src_guest_memfd_flags(enum vm_mem_backing_src_type t)
+{
+ switch (t) {
+ case VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP:
+ return KVM_GMEM_NO_DIRECT_MAP;
+ default:
+ return 0;
+ }
+}
+
static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
uint64_t flags)
{
diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index 51f34c34b5a2..2469df886d7a 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -133,6 +133,8 @@ enum vm_mem_backing_src_type {
VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
VM_MEM_SRC_SHMEM,
VM_MEM_SRC_SHARED_HUGETLB,
+ VM_MEM_SRC_GUEST_MEMFD,
+ VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP,
NUM_SRC_TYPES,
};
@@ -164,6 +166,11 @@ static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
}
+static inline bool backing_src_is_guest_memfd(enum vm_mem_backing_src_type t)
+{
+ return t == VM_MEM_SRC_GUEST_MEMFD || t == VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP;
+}
+
static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t)
{
return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM;
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index ebdf38e2983b..0900809bf6ac 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -970,6 +970,34 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
alignment = 1;
#endif
+ if (guest_memfd < 0) {
+ if ((flags & KVM_MEM_GUEST_MEMFD) || backing_src_is_guest_memfd(src_type)) {
+ uint32_t guest_memfd_flags = backing_src_guest_memfd_flags(src_type);
+
+ TEST_ASSERT(!guest_memfd_offset,
+ "Offset must be zero when creating new guest_memfd");
+ guest_memfd = vm_create_guest_memfd(vm, mem_size, guest_memfd_flags);
+ }
+ } else {
+ /*
+ * Install a unique fd for each memslot so that the fd
+ * can be closed when the region is deleted without
+ * needing to track if the fd is owned by the framework
+ * or by the caller.
+ */
+ guest_memfd = dup(guest_memfd);
+ TEST_ASSERT(guest_memfd >= 0, __KVM_SYSCALL_ERROR("dup()", guest_memfd));
+ }
+
+ if (guest_memfd > 0) {
+ flags |= KVM_MEM_GUEST_MEMFD;
+
+ region->region.guest_memfd = guest_memfd;
+ region->region.guest_memfd_offset = guest_memfd_offset;
+ } else {
+ region->region.guest_memfd = -1;
+ }
+
/*
* 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
@@ -985,10 +1013,13 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
if (alignment > 1)
region->mmap_size += alignment;
- region->fd = -1;
- if (backing_src_is_shared(src_type))
+ if (backing_src_is_guest_memfd(src_type))
+ region->fd = guest_memfd;
+ else if (backing_src_is_guest_memfd(src_type))
region->fd = kvm_memfd_alloc(region->mmap_size,
src_type == VM_MEM_SRC_SHARED_HUGETLB);
+ else
+ region->fd = -1;
region->mmap_start = mmap(NULL, region->mmap_size,
PROT_READ | PROT_WRITE,
@@ -1016,34 +1047,6 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
}
region->backing_src_type = src_type;
-
- if (guest_memfd < 0) {
- if (flags & KVM_MEM_GUEST_MEMFD) {
- uint32_t guest_memfd_flags = 0;
- TEST_ASSERT(!guest_memfd_offset,
- "Offset must be zero when creating new guest_memfd");
- guest_memfd = vm_create_guest_memfd(vm, mem_size, guest_memfd_flags);
- }
- } else {
- /*
- * Install a unique fd for each memslot so that the fd
- * can be closed when the region is deleted without
- * needing to track if the fd is owned by the framework
- * or by the caller.
- */
- guest_memfd = dup(guest_memfd);
- TEST_ASSERT(guest_memfd >= 0, __KVM_SYSCALL_ERROR("dup()", guest_memfd));
- }
-
- if (guest_memfd > 0) {
- flags |= KVM_MEM_GUEST_MEMFD;
-
- region->region.guest_memfd = guest_memfd;
- region->region.guest_memfd_offset = guest_memfd_offset;
- } else {
- region->region.guest_memfd = -1;
- }
-
region->unused_phy_pages = sparsebit_alloc();
if (vm_arch_has_protected_memory(vm))
region->protected_phy_pages = sparsebit_alloc();
@@ -1063,6 +1066,10 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
guest_paddr, (uint64_t) region->region.memory_size,
region->region.guest_memfd);
+ if (region->region.guest_memfd != -1 && kvm_has_cap(KVM_CAP_MEMORY_ATTRIBUTES))
+ vm_set_memory_attributes(vm, region->region.guest_phys_addr,
+ region->region.memory_size, KVM_MEMORY_ATTRIBUTE_PRIVATE);
+
/* Add to quick lookup data structures */
vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region);
vm_userspace_mem_region_hva_insert(&vm->regions.hva_tree, region);
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index 8ed0b74ae837..1a5b0d5d5f91 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -279,6 +279,14 @@ const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
*/
.flag = MAP_SHARED,
},
+ [VM_MEM_SRC_GUEST_MEMFD] = {
+ .name = "guest_memfd",
+ .flag = MAP_SHARED,
+ },
+ [VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP] = {
+ .name = "guest_memfd_no_direct_map",
+ .flag = MAP_SHARED,
+ }
};
_Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
"Missing new backing src types?");
--
2.48.1
next 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 ` Patrick Roy [this message]
2025-02-25 14:12 ` [PATCH v4 08/12] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types 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 ` [PATCH v4 12/12] KVM: selftests: Test guest execution from direct map removed gmem Patrick Roy
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-9-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