guest_memfd_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/falloc.h>
  15. #include <sys/mman.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include "kvm_util.h"
  19. #include "test_util.h"
  20. static void test_file_read_write(int fd)
  21. {
  22. char buf[64];
  23. TEST_ASSERT(read(fd, buf, sizeof(buf)) < 0,
  24. "read on a guest_mem fd should fail");
  25. TEST_ASSERT(write(fd, buf, sizeof(buf)) < 0,
  26. "write on a guest_mem fd should fail");
  27. TEST_ASSERT(pread(fd, buf, sizeof(buf), 0) < 0,
  28. "pread on a guest_mem fd should fail");
  29. TEST_ASSERT(pwrite(fd, buf, sizeof(buf), 0) < 0,
  30. "pwrite on a guest_mem fd should fail");
  31. }
  32. static void test_mmap(int fd, size_t page_size)
  33. {
  34. char *mem;
  35. mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  36. TEST_ASSERT_EQ(mem, MAP_FAILED);
  37. }
  38. static void test_file_size(int fd, size_t page_size, size_t total_size)
  39. {
  40. struct stat sb;
  41. int ret;
  42. ret = fstat(fd, &sb);
  43. TEST_ASSERT(!ret, "fstat should succeed");
  44. TEST_ASSERT_EQ(sb.st_size, total_size);
  45. TEST_ASSERT_EQ(sb.st_blksize, page_size);
  46. }
  47. static void test_fallocate(int fd, size_t page_size, size_t total_size)
  48. {
  49. int ret;
  50. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, total_size);
  51. TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed");
  52. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
  53. page_size - 1, page_size);
  54. TEST_ASSERT(ret, "fallocate with unaligned offset should fail");
  55. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, page_size);
  56. TEST_ASSERT(ret, "fallocate beginning at total_size should fail");
  57. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + page_size, page_size);
  58. TEST_ASSERT(ret, "fallocate beginning after total_size should fail");
  59. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
  60. total_size, page_size);
  61. TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at total_size should succeed");
  62. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
  63. total_size + page_size, page_size);
  64. TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) after total_size should succeed");
  65. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
  66. page_size, page_size - 1);
  67. TEST_ASSERT(ret, "fallocate with unaligned size should fail");
  68. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
  69. page_size, page_size);
  70. TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) with aligned offset and size should succeed");
  71. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size);
  72. TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
  73. }
  74. static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)
  75. {
  76. struct {
  77. off_t offset;
  78. off_t len;
  79. } testcases[] = {
  80. {0, 1},
  81. {0, page_size - 1},
  82. {0, page_size + 1},
  83. {1, 1},
  84. {1, page_size - 1},
  85. {1, page_size},
  86. {1, page_size + 1},
  87. {page_size, 1},
  88. {page_size, page_size - 1},
  89. {page_size, page_size + 1},
  90. };
  91. int ret, i;
  92. for (i = 0; i < ARRAY_SIZE(testcases); i++) {
  93. ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
  94. testcases[i].offset, testcases[i].len);
  95. TEST_ASSERT(ret == -1 && errno == EINVAL,
  96. "PUNCH_HOLE with !PAGE_SIZE offset (%lx) and/or length (%lx) should fail",
  97. testcases[i].offset, testcases[i].len);
  98. }
  99. }
  100. static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
  101. {
  102. size_t page_size = getpagesize();
  103. uint64_t flag;
  104. size_t size;
  105. int fd;
  106. for (size = 1; size < page_size; size++) {
  107. fd = __vm_create_guest_memfd(vm, size, 0);
  108. TEST_ASSERT(fd == -1 && errno == EINVAL,
  109. "guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL",
  110. size);
  111. }
  112. for (flag = BIT(0); flag; flag <<= 1) {
  113. fd = __vm_create_guest_memfd(vm, page_size, flag);
  114. TEST_ASSERT(fd == -1 && errno == EINVAL,
  115. "guest_memfd() with flag '0x%lx' should fail with EINVAL",
  116. flag);
  117. }
  118. }
  119. static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
  120. {
  121. int fd1, fd2, ret;
  122. struct stat st1, st2;
  123. fd1 = __vm_create_guest_memfd(vm, 4096, 0);
  124. TEST_ASSERT(fd1 != -1, "memfd creation should succeed");
  125. ret = fstat(fd1, &st1);
  126. TEST_ASSERT(ret != -1, "memfd fstat should succeed");
  127. TEST_ASSERT(st1.st_size == 4096, "memfd st_size should match requested size");
  128. fd2 = __vm_create_guest_memfd(vm, 8192, 0);
  129. TEST_ASSERT(fd2 != -1, "memfd creation should succeed");
  130. ret = fstat(fd2, &st2);
  131. TEST_ASSERT(ret != -1, "memfd fstat should succeed");
  132. TEST_ASSERT(st2.st_size == 8192, "second memfd st_size should match requested size");
  133. ret = fstat(fd1, &st1);
  134. TEST_ASSERT(ret != -1, "memfd fstat should succeed");
  135. TEST_ASSERT(st1.st_size == 4096, "first memfd st_size should still match requested size");
  136. TEST_ASSERT(st1.st_ino != st2.st_ino, "different memfd should have different inode numbers");
  137. close(fd2);
  138. close(fd1);
  139. }
  140. int main(int argc, char *argv[])
  141. {
  142. size_t page_size;
  143. size_t total_size;
  144. int fd;
  145. struct kvm_vm *vm;
  146. TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
  147. page_size = getpagesize();
  148. total_size = page_size * 4;
  149. vm = vm_create_barebones();
  150. test_create_guest_memfd_invalid(vm);
  151. test_create_guest_memfd_multiple(vm);
  152. fd = vm_create_guest_memfd(vm, total_size, 0);
  153. test_file_read_write(fd);
  154. test_mmap(fd, page_size);
  155. test_file_size(fd, page_size, total_size);
  156. test_fallocate(fd, page_size, total_size);
  157. test_invalid_punch_hole(fd, page_size, total_size);
  158. close(fd);
  159. }