From: Nikita Kalyazin <kalyazin@amazon.com>
To: <pbonzini@redhat.com>, <shuah@kernel.org>, <kvm@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <linux-mm@kvack.org>, <michael.day@amd.com>, <david@redhat.com>,
<quic_eberman@quicinc.com>, <jthoughton@google.com>,
<brijesh.singh@amd.com>, <michael.roth@amd.com>, <graf@amazon.de>,
<jgowans@amazon.com>, <roypat@amazon.co.uk>, <derekmn@amazon.com>,
<nsaenz@amazon.es>, <xmarcalx@amazon.com>, <kalyazin@amazon.com>
Subject: [RFC PATCH v2 2/2] KVM: selftests: update guest_memfd write tests
Date: Fri, 29 Nov 2024 12:39:29 +0000 [thread overview]
Message-ID: <20241129123929.64790-3-kalyazin@amazon.com> (raw)
In-Reply-To: <20241129123929.64790-1-kalyazin@amazon.com>
This is to reflect that the write syscall is now implemented for
guest_memfd.
Signed-off-by: Nikita Kalyazin <kalyazin@amazon.com>
---
.../testing/selftests/kvm/guest_memfd_test.c | 85 +++++++++++++++++--
1 file changed, 79 insertions(+), 6 deletions(-)
diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
index ce687f8d248f..e10d0f51da93 100644
--- a/tools/testing/selftests/kvm/guest_memfd_test.c
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -20,18 +20,90 @@
#include "kvm_util.h"
#include "test_util.h"
-static void test_file_read_write(int fd)
+static void test_file_read(int fd)
{
char buf[64];
TEST_ASSERT(read(fd, buf, sizeof(buf)) < 0,
"read on a guest_mem fd should fail");
- TEST_ASSERT(write(fd, buf, sizeof(buf)) < 0,
- "write on a guest_mem fd should fail");
TEST_ASSERT(pread(fd, buf, sizeof(buf), 0) < 0,
"pread on a guest_mem fd should fail");
- TEST_ASSERT(pwrite(fd, buf, sizeof(buf), 0) < 0,
- "pwrite on a guest_mem fd should fail");
+}
+
+static void test_file_write(int fd, size_t total_size)
+{
+ size_t page_size = getpagesize();
+ void *buf = NULL;
+ int ret;
+
+ ret = posix_memalign(&buf, page_size, total_size);
+ TEST_ASSERT_EQ(ret, 0);
+
+ /* Check arguments correctness checks work as expected */
+
+ ret = pwrite(fd, buf, page_size - 1, 0);
+ TEST_ASSERT(ret == -1, "write unaligned count on a guest_mem fd should fail");
+ TEST_ASSERT_EQ(errno, EINVAL);
+
+ ret = pwrite(fd, buf, page_size, 1);
+ TEST_ASSERT(ret == -1, "write unaligned offset on a guest_mem fd should fail");
+ TEST_ASSERT_EQ(errno, EINVAL);
+
+ ret = pwrite(fd, buf, page_size, total_size);
+ TEST_ASSERT(ret == -1, "writing past the file size on a guest_mem fd should fail");
+ TEST_ASSERT_EQ(errno, EINVAL);
+
+ ret = pwrite(fd, NULL, page_size, 0);
+ TEST_ASSERT(ret == -1, "supplying a NULL buffer when writing a guest_mem fd should fail");
+ TEST_ASSERT_EQ(errno, EINVAL);
+
+ /* Check double population is not allowed */
+
+ ret = pwrite(fd, buf, page_size, 0);
+ TEST_ASSERT(ret == page_size, "page-aligned write on a guest_mem fd should succeed");
+
+ ret = pwrite(fd, buf, page_size, 0);
+ TEST_ASSERT(ret == -1, "write on already populated guest_mem fd should fail");
+ TEST_ASSERT_EQ(errno, ENOSPC);
+
+ ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size);
+ TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed");
+
+ /* Check population is allowed again after punching a hole */
+
+ ret = pwrite(fd, buf, page_size, 0);
+ TEST_ASSERT(ret == page_size, "page-aligned write on a punched guest_mem fd should succeed");
+
+ ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size);
+ TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed");
+
+ /* Check population of already allocated memory is allowed */
+
+ ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, page_size);
+ TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed");
+
+ ret = pwrite(fd, buf, page_size, 0);
+ TEST_ASSERT(ret == page_size, "write on a preallocated guest_mem fd should succeed");
+
+ ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size);
+ TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed");
+
+ /* Check population works until an already populated page is encountered */
+
+ ret = pwrite(fd, buf, total_size, 0);
+ TEST_ASSERT(ret == total_size, "page-aligned write on a guest_mem fd should succeed");
+
+ ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size);
+ TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed");
+
+ ret = pwrite(fd, buf, total_size, 0);
+ TEST_ASSERT(ret == page_size, "write on a guest_mem fd should not overwrite data");
+
+ ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, total_size);
+ TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed");
+
+
+ free(buf);
}
static void test_mmap(int fd, size_t page_size)
@@ -189,7 +261,8 @@ int main(int argc, char *argv[])
fd = vm_create_guest_memfd(vm, total_size, 0);
- test_file_read_write(fd);
+ test_file_read(fd);
+ test_file_write(fd, total_size);
test_mmap(fd, page_size);
test_file_size(fd, page_size, total_size);
test_fallocate(fd, page_size, total_size);
--
2.40.1
prev parent reply other threads:[~2024-11-29 12:40 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-29 12:39 [RFC PATCH v2 0/2] KVM: guest_memfd: use write for population Nikita Kalyazin
2024-11-29 12:39 ` [RFC PATCH v2 1/2] KVM: guest_memfd: add generic population via write Nikita Kalyazin
2024-12-03 14:55 ` Mike Day
2024-11-29 12:39 ` Nikita Kalyazin [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=20241129123929.64790-3-kalyazin@amazon.com \
--to=kalyazin@amazon.com \
--cc=brijesh.singh@amd.com \
--cc=david@redhat.com \
--cc=derekmn@amazon.com \
--cc=graf@amazon.de \
--cc=jgowans@amazon.com \
--cc=jthoughton@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=michael.day@amd.com \
--cc=michael.roth@amd.com \
--cc=nsaenz@amazon.es \
--cc=pbonzini@redhat.com \
--cc=quic_eberman@quicinc.com \
--cc=roypat@amazon.co.uk \
--cc=shuah@kernel.org \
--cc=xmarcalx@amazon.com \
/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