From: "Roy, Patrick" <roypat@amazon.co.uk>
To: "david@redhat.com" <david@redhat.com>,
"seanjc@google.com" <seanjc@google.com>
Cc: "Roy, Patrick" <roypat@amazon.co.uk>,
"tabba@google.com" <tabba@google.com>,
"ackerleytng@google.com" <ackerleytng@google.com>,
"pbonzini@redhat.com" <pbonzini@redhat.com>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"linux-arm-kernel@lists.infradead.org"
<linux-arm-kernel@lists.infradead.org>,
"kvmarm@lists.linux.dev" <kvmarm@lists.linux.dev>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-mm@kvack.org" <linux-mm@kvack.org>,
"rppt@kernel.org" <rppt@kernel.org>,
"will@kernel.org" <will@kernel.org>,
"vbabka@suse.cz" <vbabka@suse.cz>,
"Cali, Marco" <xmarcalx@amazon.co.uk>,
"Kalyazin, Nikita" <kalyazin@amazon.co.uk>,
"Thomson, Jack" <jackabt@amazon.co.uk>,
"Manwaring, Derek" <derekmn@amazon.com>
Subject: [PATCH v5 06/12] KVM: selftests: load elf via bounce buffer
Date: Thu, 28 Aug 2025 09:39:24 +0000 [thread overview]
Message-ID: <20250828093902.2719-7-roypat@amazon.co.uk> (raw)
In-Reply-To: <20250828093902.2719-1-roypat@amazon.co.uk>
If guest memory is backed using a VMA that does not allow GUP (e.g. a
userspace mapping of guest_memfd when the fd was allocated using
KVM_GMEM_NO_DIRECT_MAP), then directly loading the test ELF binary into
it via read(2) potentially does not work. To nevertheless support
loading binaries in this cases, do the read(2) syscall using a bounce
buffer, and then memcpy from the bounce buffer into guest memory.
Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
---
.../testing/selftests/kvm/include/test_util.h | 1 +
tools/testing/selftests/kvm/lib/elf.c | 8 +++----
tools/testing/selftests/kvm/lib/io.c | 23 +++++++++++++++++++
3 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index c6ef895fbd9a..0409b7b96c94 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -46,6 +46,7 @@ do { \
ssize_t test_write(int fd, const void *buf, size_t count);
ssize_t test_read(int fd, void *buf, size_t count);
+ssize_t test_read_bounce(int fd, void *buf, size_t count);
int test_seq_read(const char *path, char **bufp, size_t *sizep);
void __printf(5, 6) test_assert(bool exp, const char *exp_str,
diff --git a/tools/testing/selftests/kvm/lib/elf.c b/tools/testing/selftests/kvm/lib/elf.c
index f34d926d9735..e829fbe0a11e 100644
--- a/tools/testing/selftests/kvm/lib/elf.c
+++ b/tools/testing/selftests/kvm/lib/elf.c
@@ -31,7 +31,7 @@ static void elfhdr_get(const char *filename, Elf64_Ehdr *hdrp)
* the real size of the ELF header.
*/
unsigned char ident[EI_NIDENT];
- test_read(fd, ident, sizeof(ident));
+ test_read_bounce(fd, ident, sizeof(ident));
TEST_ASSERT((ident[EI_MAG0] == ELFMAG0) && (ident[EI_MAG1] == ELFMAG1)
&& (ident[EI_MAG2] == ELFMAG2) && (ident[EI_MAG3] == ELFMAG3),
"ELF MAGIC Mismatch,\n"
@@ -79,7 +79,7 @@ static void elfhdr_get(const char *filename, Elf64_Ehdr *hdrp)
offset_rv = lseek(fd, 0, SEEK_SET);
TEST_ASSERT(offset_rv == 0, "Seek to ELF header failed,\n"
" rv: %zi expected: %i", offset_rv, 0);
- test_read(fd, hdrp, sizeof(*hdrp));
+ test_read_bounce(fd, hdrp, sizeof(*hdrp));
TEST_ASSERT(hdrp->e_phentsize == sizeof(Elf64_Phdr),
"Unexpected physical header size,\n"
" hdrp->e_phentsize: %x\n"
@@ -146,7 +146,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
/* Read in the program header. */
Elf64_Phdr phdr;
- test_read(fd, &phdr, sizeof(phdr));
+ test_read_bounce(fd, &phdr, sizeof(phdr));
/* Skip if this header doesn't describe a loadable segment. */
if (phdr.p_type != PT_LOAD)
@@ -187,7 +187,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
" expected: 0x%jx",
n1, errno, (intmax_t) offset_rv,
(intmax_t) phdr.p_offset);
- test_read(fd, addr_gva2hva(vm, phdr.p_vaddr),
+ test_read_bounce(fd, addr_gva2hva(vm, phdr.p_vaddr),
phdr.p_filesz);
}
}
diff --git a/tools/testing/selftests/kvm/lib/io.c b/tools/testing/selftests/kvm/lib/io.c
index fedb2a741f0b..74419becc8bc 100644
--- a/tools/testing/selftests/kvm/lib/io.c
+++ b/tools/testing/selftests/kvm/lib/io.c
@@ -155,3 +155,26 @@ ssize_t test_read(int fd, void *buf, size_t count)
return num_read;
}
+
+/* Test read via intermediary buffer
+ *
+ * Same as test_read, except read(2)s happen into a bounce buffer that is memcpy'd
+ * to buf. For use with buffers that cannot be GUP'd (e.g. guest_memfd VMAs if
+ * guest_memfd was created with GUEST_MEMFD_FLAG_NO_DIRECT_MAP).
+ */
+ssize_t test_read_bounce(int fd, void *buf, size_t count)
+{
+ void *bounce_buffer;
+ ssize_t num_read;
+
+ TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
+
+ bounce_buffer = malloc(count);
+ TEST_ASSERT(bounce_buffer != NULL, "Failed to allocate bounce buffer");
+
+ num_read = test_read(fd, bounce_buffer, count);
+ memcpy(buf, bounce_buffer, num_read);
+ free(bounce_buffer);
+
+ return num_read;
+}
--
2.50.1
next prev parent reply other threads:[~2025-08-28 9:39 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-28 9:39 [PATCH v5 00/12] Direct Map Removal Support for guest_memfd Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 01/12] filemap: Pass address_space mapping to ->free_folio() Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 02/12] arch: export set_direct_map_valid_noflush to KVM module Roy, Patrick
2025-08-28 10:07 ` Fuad Tabba
2025-09-01 13:47 ` Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 03/12] mm: introduce AS_NO_DIRECT_MAP Roy, Patrick
2025-08-28 10:21 ` Fuad Tabba
2025-09-01 13:54 ` Roy, Patrick
2025-09-01 14:56 ` Roy, Patrick
2025-09-02 7:59 ` Fuad Tabba
2025-09-02 8:46 ` David Hildenbrand
2025-09-02 8:50 ` Fuad Tabba
2025-09-02 9:18 ` Roy, Patrick
2025-09-02 9:21 ` Fuad Tabba
2025-09-02 9:54 ` David Hildenbrand
2025-08-28 14:26 ` Mike Rapoport
2025-09-01 13:56 ` Roy, Patrick
2025-08-28 21:00 ` David Hildenbrand
2025-09-01 14:03 ` Roy, Patrick
2025-08-31 10:26 ` kernel test robot
2025-09-01 14:05 ` Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 04/12] KVM: guest_memfd: Add flag to remove from direct map Roy, Patrick
2025-08-28 14:54 ` Mike Rapoport
2025-09-01 14:22 ` Roy, Patrick
2025-09-01 14:27 ` Mike Rapoport
2025-08-28 9:39 ` [PATCH v5 05/12] KVM: Documentation: describe GUEST_MEMFD_FLAG_NO_DIRECT_MAP Roy, Patrick
2025-08-28 10:27 ` David Hildenbrand
2025-09-01 14:30 ` Roy, Patrick
2025-09-01 14:43 ` David Hildenbrand
2025-08-28 9:39 ` Roy, Patrick [this message]
2025-08-28 9:39 ` [PATCH v5 07/12] KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1 Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 08/12] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 09/12] KVM: selftests: stuff vm_mem_backing_src_type into vm_shape Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 10/12] KVM: selftests: cover GUEST_MEMFD_FLAG_NO_DIRECT_MAP in mem conversion tests Roy, Patrick
2025-08-28 9:39 ` [PATCH v5 11/12] KVM: selftests: cover GUEST_MEMFD_FLAG_NO_DIRECT_MAP in guest_memfd_test.c Roy, Patrick
2025-08-28 10:26 ` David Hildenbrand
2025-08-28 9:39 ` [PATCH v5 12/12] KVM: selftests: Test guest execution from direct map removed gmem Roy, Patrick
2025-08-28 12:50 ` [PATCH v5 00/12] Direct Map Removal Support for guest_memfd David Hildenbrand
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=20250828093902.2719-7-roypat@amazon.co.uk \
--to=roypat@amazon.co.uk \
--cc=ackerleytng@google.com \
--cc=david@redhat.com \
--cc=derekmn@amazon.com \
--cc=jackabt@amazon.co.uk \
--cc=kalyazin@amazon.co.uk \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pbonzini@redhat.com \
--cc=rppt@kernel.org \
--cc=seanjc@google.com \
--cc=tabba@google.com \
--cc=vbabka@suse.cz \
--cc=will@kernel.org \
--cc=xmarcalx@amazon.co.uk \
/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;
as well as URLs for NNTP newsgroup(s).