Skip to content

Commit e2bbfd5

Browse files
yamahatasean-jc
authored andcommitted
KVM: selftests: Add negative test cases for punch hole for guest_memfd()
Add test cases to check for punch hole of guest_memfd to reject unaligned offset or size. Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Link: https://lore.kernel.org/r/bf9094185cc93cc1ff0efac0349258cf68046c99.1695327124.git.isaku.yamahata@intel.com [sean: tweak function name, print offset+len on failure] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 6a92dc5 commit e2bbfd5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size)
9191
TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
9292
}
9393

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+
94124
static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
95125
{
96126
uint64_t valid_flags = 0;
@@ -160,6 +190,7 @@ int main(int argc, char *argv[])
160190
test_mmap(fd, page_size);
161191
test_file_size(fd, page_size, total_size);
162192
test_fallocate(fd, page_size, total_size);
193+
test_invalid_punch_hole(fd, page_size, total_size);
163194

164195
close(fd);
165196
}

0 commit comments

Comments
 (0)