Skip to content

Commit 9cfd4a5

Browse files
chao-psean-jc
authored andcommitted
KVM: selftests: Add basic selftest for guest_memfd()
Add a selftest to verify the basic functionality of guest_memfd(): + file descriptor created with the guest_memfd() ioctl does not allow read/write/mmap operations + file size and block size as returned from fstat are as expected + fallocate on the fd checks that offset/length on fallocate(FALLOC_FL_PUNCH_HOLE) should be page aligned + invalid inputs (misaligned size, invalid flags) are rejected + file size and inode are unique (the innocuous-sounding anon_inode_getfile() backs all files with a single inode...) Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com> Co-developed-by: Ackerley Tng <ackerleytng@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Co-developed-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent d1c371b commit 9cfd4a5

2 files changed

Lines changed: 222 additions & 0 deletions

File tree

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
124124
TEST_GEN_PROGS_x86_64 += demand_paging_test
125125
TEST_GEN_PROGS_x86_64 += dirty_log_test
126126
TEST_GEN_PROGS_x86_64 += dirty_log_perf_test
127+
TEST_GEN_PROGS_x86_64 += guest_memfd_test
127128
TEST_GEN_PROGS_x86_64 += guest_print_test
128129
TEST_GEN_PROGS_x86_64 += hardware_disable_test
129130
TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright Intel Corporation, 2023
4+
*
5+
* Author: Chao Peng <chao.p.peng@linux.intel.com>
6+
*/
7+
8+
#define _GNU_SOURCE
9+
#include "test_util.h"
10+
#include "kvm_util_base.h"
11+
#include <linux/bitmap.h>
12+
#include <linux/falloc.h>
13+
#include <sys/mman.h>
14+
#include <sys/types.h>
15+
#include <sys/stat.h>
16+
17+
#include <stdlib.h>
18+
#include <string.h>
19+
#include <unistd.h>
20+
#include <errno.h>
21+
#include <stdio.h>
22+
#include <fcntl.h>
23+
24+
static void test_file_read_write(int fd)
25+
{
26+
char buf[64];
27+
28+
TEST_ASSERT(read(fd, buf, sizeof(buf)) < 0,
29+
"read on a guest_mem fd should fail");
30+
TEST_ASSERT(write(fd, buf, sizeof(buf)) < 0,
31+
"write on a guest_mem fd should fail");
32+
TEST_ASSERT(pread(fd, buf, sizeof(buf), 0) < 0,
33+
"pread on a guest_mem fd should fail");
34+
TEST_ASSERT(pwrite(fd, buf, sizeof(buf), 0) < 0,
35+
"pwrite on a guest_mem fd should fail");
36+
}
37+
38+
static void test_mmap(int fd, size_t page_size)
39+
{
40+
char *mem;
41+
42+
mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
43+
TEST_ASSERT_EQ(mem, MAP_FAILED);
44+
}
45+
46+
static void test_file_size(int fd, size_t page_size, size_t total_size)
47+
{
48+
struct stat sb;
49+
int ret;
50+
51+
ret = fstat(fd, &sb);
52+
TEST_ASSERT(!ret, "fstat should succeed");
53+
TEST_ASSERT_EQ(sb.st_size, total_size);
54+
TEST_ASSERT_EQ(sb.st_blksize, page_size);
55+
}
56+
57+
static void test_fallocate(int fd, size_t page_size, size_t total_size)
58+
{
59+
int ret;
60+
61+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, total_size);
62+
TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed");
63+
64+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
65+
page_size - 1, page_size);
66+
TEST_ASSERT(ret, "fallocate with unaligned offset should fail");
67+
68+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, page_size);
69+
TEST_ASSERT(ret, "fallocate beginning at total_size should fail");
70+
71+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + page_size, page_size);
72+
TEST_ASSERT(ret, "fallocate beginning after total_size should fail");
73+
74+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
75+
total_size, page_size);
76+
TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at total_size should succeed");
77+
78+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
79+
total_size + page_size, page_size);
80+
TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) after total_size should succeed");
81+
82+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
83+
page_size, page_size - 1);
84+
TEST_ASSERT(ret, "fallocate with unaligned size should fail");
85+
86+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
87+
page_size, page_size);
88+
TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) with aligned offset and size should succeed");
89+
90+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size);
91+
TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
92+
}
93+
94+
static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)
95+
{
96+
struct {
97+
off_t offset;
98+
off_t len;
99+
} testcases[] = {
100+
{0, 1},
101+
{0, page_size - 1},
102+
{0, page_size + 1},
103+
104+
{1, 1},
105+
{1, page_size - 1},
106+
{1, page_size},
107+
{1, page_size + 1},
108+
109+
{page_size, 1},
110+
{page_size, page_size - 1},
111+
{page_size, page_size + 1},
112+
};
113+
int ret, i;
114+
115+
for (i = 0; i < ARRAY_SIZE(testcases); i++) {
116+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
117+
testcases[i].offset, testcases[i].len);
118+
TEST_ASSERT(ret == -1 && errno == EINVAL,
119+
"PUNCH_HOLE with !PAGE_SIZE offset (%lx) and/or length (%lx) should fail",
120+
testcases[i].offset, testcases[i].len);
121+
}
122+
}
123+
124+
static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
125+
{
126+
uint64_t valid_flags = 0;
127+
size_t page_size = getpagesize();
128+
uint64_t flag;
129+
size_t size;
130+
int fd;
131+
132+
for (size = 1; size < page_size; size++) {
133+
fd = __vm_create_guest_memfd(vm, size, 0);
134+
TEST_ASSERT(fd == -1 && errno == EINVAL,
135+
"guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL",
136+
size);
137+
}
138+
139+
if (thp_configured()) {
140+
for (size = page_size * 2; size < get_trans_hugepagesz(); size += page_size) {
141+
fd = __vm_create_guest_memfd(vm, size, KVM_GUEST_MEMFD_ALLOW_HUGEPAGE);
142+
TEST_ASSERT(fd == -1 && errno == EINVAL,
143+
"guest_memfd() with non-hugepage-aligned page size '0x%lx' should fail with EINVAL",
144+
size);
145+
}
146+
147+
valid_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE;
148+
}
149+
150+
for (flag = 1; flag; flag <<= 1) {
151+
uint64_t bit;
152+
153+
if (flag & valid_flags)
154+
continue;
155+
156+
fd = __vm_create_guest_memfd(vm, page_size, flag);
157+
TEST_ASSERT(fd == -1 && errno == EINVAL,
158+
"guest_memfd() with flag '0x%lx' should fail with EINVAL",
159+
flag);
160+
161+
for_each_set_bit(bit, &valid_flags, 64) {
162+
fd = __vm_create_guest_memfd(vm, page_size, flag | BIT_ULL(bit));
163+
TEST_ASSERT(fd == -1 && errno == EINVAL,
164+
"guest_memfd() with flags '0x%llx' should fail with EINVAL",
165+
flag | BIT_ULL(bit));
166+
}
167+
}
168+
}
169+
170+
static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
171+
{
172+
int fd1, fd2, ret;
173+
struct stat st1, st2;
174+
175+
fd1 = __vm_create_guest_memfd(vm, 4096, 0);
176+
TEST_ASSERT(fd1 != -1, "memfd creation should succeed");
177+
178+
ret = fstat(fd1, &st1);
179+
TEST_ASSERT(ret != -1, "memfd fstat should succeed");
180+
TEST_ASSERT(st1.st_size == 4096, "memfd st_size should match requested size");
181+
182+
fd2 = __vm_create_guest_memfd(vm, 8192, 0);
183+
TEST_ASSERT(fd2 != -1, "memfd creation should succeed");
184+
185+
ret = fstat(fd2, &st2);
186+
TEST_ASSERT(ret != -1, "memfd fstat should succeed");
187+
TEST_ASSERT(st2.st_size == 8192, "second memfd st_size should match requested size");
188+
189+
ret = fstat(fd1, &st1);
190+
TEST_ASSERT(ret != -1, "memfd fstat should succeed");
191+
TEST_ASSERT(st1.st_size == 4096, "first memfd st_size should still match requested size");
192+
TEST_ASSERT(st1.st_ino != st2.st_ino, "different memfd should have different inode numbers");
193+
}
194+
195+
int main(int argc, char *argv[])
196+
{
197+
size_t page_size;
198+
size_t total_size;
199+
int fd;
200+
struct kvm_vm *vm;
201+
202+
TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
203+
204+
page_size = getpagesize();
205+
total_size = page_size * 4;
206+
207+
vm = vm_create_barebones();
208+
209+
test_create_guest_memfd_invalid(vm);
210+
test_create_guest_memfd_multiple(vm);
211+
212+
fd = vm_create_guest_memfd(vm, total_size, 0);
213+
214+
test_file_read_write(fd);
215+
test_mmap(fd, page_size);
216+
test_file_size(fd, page_size, total_size);
217+
test_fallocate(fd, page_size, total_size);
218+
test_invalid_punch_hole(fd, page_size, total_size);
219+
220+
close(fd);
221+
}

0 commit comments

Comments
 (0)