mremap_dontunmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Tests for mremap w/ MREMAP_DONTUNMAP.
  4. *
  5. * Copyright 2020, Brian Geffon <[email protected]>
  6. */
  7. #define _GNU_SOURCE
  8. #include <sys/mman.h>
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include "../kselftest.h"
  15. #ifndef MREMAP_DONTUNMAP
  16. #define MREMAP_DONTUNMAP 4
  17. #endif
  18. unsigned long page_size;
  19. char *page_buffer;
  20. static void dump_maps(void)
  21. {
  22. char cmd[32];
  23. snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid());
  24. system(cmd);
  25. }
  26. #define BUG_ON(condition, description) \
  27. do { \
  28. if (condition) { \
  29. fprintf(stderr, "[FAIL]\t%s():%d\t%s:%s\n", __func__, \
  30. __LINE__, (description), strerror(errno)); \
  31. dump_maps(); \
  32. exit(1); \
  33. } \
  34. } while (0)
  35. // Try a simple operation for to "test" for kernel support this prevents
  36. // reporting tests as failed when it's run on an older kernel.
  37. static int kernel_support_for_mremap_dontunmap()
  38. {
  39. int ret = 0;
  40. unsigned long num_pages = 1;
  41. void *source_mapping = mmap(NULL, num_pages * page_size, PROT_NONE,
  42. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  43. BUG_ON(source_mapping == MAP_FAILED, "mmap");
  44. // This simple remap should only fail if MREMAP_DONTUNMAP isn't
  45. // supported.
  46. void *dest_mapping =
  47. mremap(source_mapping, num_pages * page_size, num_pages * page_size,
  48. MREMAP_DONTUNMAP | MREMAP_MAYMOVE, 0);
  49. if (dest_mapping == MAP_FAILED) {
  50. ret = errno;
  51. } else {
  52. BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
  53. "unable to unmap destination mapping");
  54. }
  55. BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
  56. "unable to unmap source mapping");
  57. return ret;
  58. }
  59. // This helper will just validate that an entire mapping contains the expected
  60. // byte.
  61. static int check_region_contains_byte(void *addr, unsigned long size, char byte)
  62. {
  63. BUG_ON(size & (page_size - 1),
  64. "check_region_contains_byte expects page multiples");
  65. BUG_ON((unsigned long)addr & (page_size - 1),
  66. "check_region_contains_byte expects page alignment");
  67. memset(page_buffer, byte, page_size);
  68. unsigned long num_pages = size / page_size;
  69. unsigned long i;
  70. // Compare each page checking that it contains our expected byte.
  71. for (i = 0; i < num_pages; ++i) {
  72. int ret =
  73. memcmp(addr + (i * page_size), page_buffer, page_size);
  74. if (ret) {
  75. return ret;
  76. }
  77. }
  78. return 0;
  79. }
  80. // this test validates that MREMAP_DONTUNMAP moves the pagetables while leaving
  81. // the source mapping mapped.
  82. static void mremap_dontunmap_simple()
  83. {
  84. unsigned long num_pages = 5;
  85. void *source_mapping =
  86. mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
  87. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  88. BUG_ON(source_mapping == MAP_FAILED, "mmap");
  89. memset(source_mapping, 'a', num_pages * page_size);
  90. // Try to just move the whole mapping anywhere (not fixed).
  91. void *dest_mapping =
  92. mremap(source_mapping, num_pages * page_size, num_pages * page_size,
  93. MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
  94. BUG_ON(dest_mapping == MAP_FAILED, "mremap");
  95. // Validate that the pages have been moved, we know they were moved if
  96. // the dest_mapping contains a's.
  97. BUG_ON(check_region_contains_byte
  98. (dest_mapping, num_pages * page_size, 'a') != 0,
  99. "pages did not migrate");
  100. BUG_ON(check_region_contains_byte
  101. (source_mapping, num_pages * page_size, 0) != 0,
  102. "source should have no ptes");
  103. BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
  104. "unable to unmap destination mapping");
  105. BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
  106. "unable to unmap source mapping");
  107. }
  108. // This test validates that MREMAP_DONTUNMAP on a shared mapping works as expected.
  109. static void mremap_dontunmap_simple_shmem()
  110. {
  111. unsigned long num_pages = 5;
  112. int mem_fd = memfd_create("memfd", MFD_CLOEXEC);
  113. BUG_ON(mem_fd < 0, "memfd_create");
  114. BUG_ON(ftruncate(mem_fd, num_pages * page_size) < 0,
  115. "ftruncate");
  116. void *source_mapping =
  117. mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
  118. MAP_FILE | MAP_SHARED, mem_fd, 0);
  119. BUG_ON(source_mapping == MAP_FAILED, "mmap");
  120. BUG_ON(close(mem_fd) < 0, "close");
  121. memset(source_mapping, 'a', num_pages * page_size);
  122. // Try to just move the whole mapping anywhere (not fixed).
  123. void *dest_mapping =
  124. mremap(source_mapping, num_pages * page_size, num_pages * page_size,
  125. MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
  126. if (dest_mapping == MAP_FAILED && errno == EINVAL) {
  127. // Old kernel which doesn't support MREMAP_DONTUNMAP on shmem.
  128. BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
  129. "unable to unmap source mapping");
  130. return;
  131. }
  132. BUG_ON(dest_mapping == MAP_FAILED, "mremap");
  133. // Validate that the pages have been moved, we know they were moved if
  134. // the dest_mapping contains a's.
  135. BUG_ON(check_region_contains_byte
  136. (dest_mapping, num_pages * page_size, 'a') != 0,
  137. "pages did not migrate");
  138. // Because the region is backed by shmem, we will actually see the same
  139. // memory at the source location still.
  140. BUG_ON(check_region_contains_byte
  141. (source_mapping, num_pages * page_size, 'a') != 0,
  142. "source should have no ptes");
  143. BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
  144. "unable to unmap destination mapping");
  145. BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
  146. "unable to unmap source mapping");
  147. }
  148. // This test validates MREMAP_DONTUNMAP will move page tables to a specific
  149. // destination using MREMAP_FIXED, also while validating that the source
  150. // remains intact.
  151. static void mremap_dontunmap_simple_fixed()
  152. {
  153. unsigned long num_pages = 5;
  154. // Since we want to guarantee that we can remap to a point, we will
  155. // create a mapping up front.
  156. void *dest_mapping =
  157. mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
  158. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  159. BUG_ON(dest_mapping == MAP_FAILED, "mmap");
  160. memset(dest_mapping, 'X', num_pages * page_size);
  161. void *source_mapping =
  162. mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
  163. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  164. BUG_ON(source_mapping == MAP_FAILED, "mmap");
  165. memset(source_mapping, 'a', num_pages * page_size);
  166. void *remapped_mapping =
  167. mremap(source_mapping, num_pages * page_size, num_pages * page_size,
  168. MREMAP_FIXED | MREMAP_DONTUNMAP | MREMAP_MAYMOVE,
  169. dest_mapping);
  170. BUG_ON(remapped_mapping == MAP_FAILED, "mremap");
  171. BUG_ON(remapped_mapping != dest_mapping,
  172. "mremap should have placed the remapped mapping at dest_mapping");
  173. // The dest mapping will have been unmap by mremap so we expect the Xs
  174. // to be gone and replaced with a's.
  175. BUG_ON(check_region_contains_byte
  176. (dest_mapping, num_pages * page_size, 'a') != 0,
  177. "pages did not migrate");
  178. // And the source mapping will have had its ptes dropped.
  179. BUG_ON(check_region_contains_byte
  180. (source_mapping, num_pages * page_size, 0) != 0,
  181. "source should have no ptes");
  182. BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
  183. "unable to unmap destination mapping");
  184. BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
  185. "unable to unmap source mapping");
  186. }
  187. // This test validates that we can MREMAP_DONTUNMAP for a portion of an
  188. // existing mapping.
  189. static void mremap_dontunmap_partial_mapping()
  190. {
  191. /*
  192. * source mapping:
  193. * --------------
  194. * | aaaaaaaaaa |
  195. * --------------
  196. * to become:
  197. * --------------
  198. * | aaaaa00000 |
  199. * --------------
  200. * With the destination mapping containing 5 pages of As.
  201. * ---------
  202. * | aaaaa |
  203. * ---------
  204. */
  205. unsigned long num_pages = 10;
  206. void *source_mapping =
  207. mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
  208. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  209. BUG_ON(source_mapping == MAP_FAILED, "mmap");
  210. memset(source_mapping, 'a', num_pages * page_size);
  211. // We will grab the last 5 pages of the source and move them.
  212. void *dest_mapping =
  213. mremap(source_mapping + (5 * page_size), 5 * page_size,
  214. 5 * page_size,
  215. MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
  216. BUG_ON(dest_mapping == MAP_FAILED, "mremap");
  217. // We expect the first 5 pages of the source to contain a's and the
  218. // final 5 pages to contain zeros.
  219. BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 'a') !=
  220. 0, "first 5 pages of source should have original pages");
  221. BUG_ON(check_region_contains_byte
  222. (source_mapping + (5 * page_size), 5 * page_size, 0) != 0,
  223. "final 5 pages of source should have no ptes");
  224. // Finally we expect the destination to have 5 pages worth of a's.
  225. BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') !=
  226. 0, "dest mapping should contain ptes from the source");
  227. BUG_ON(munmap(dest_mapping, 5 * page_size) == -1,
  228. "unable to unmap destination mapping");
  229. BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
  230. "unable to unmap source mapping");
  231. }
  232. // This test validates that we can remap over only a portion of a mapping.
  233. static void mremap_dontunmap_partial_mapping_overwrite(void)
  234. {
  235. /*
  236. * source mapping:
  237. * ---------
  238. * |aaaaa|
  239. * ---------
  240. * dest mapping initially:
  241. * -----------
  242. * |XXXXXXXXXX|
  243. * ------------
  244. * Source to become:
  245. * ---------
  246. * |00000|
  247. * ---------
  248. * With the destination mapping containing 5 pages of As.
  249. * ------------
  250. * |aaaaaXXXXX|
  251. * ------------
  252. */
  253. void *source_mapping =
  254. mmap(NULL, 5 * page_size, PROT_READ | PROT_WRITE,
  255. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  256. BUG_ON(source_mapping == MAP_FAILED, "mmap");
  257. memset(source_mapping, 'a', 5 * page_size);
  258. void *dest_mapping =
  259. mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE,
  260. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  261. BUG_ON(dest_mapping == MAP_FAILED, "mmap");
  262. memset(dest_mapping, 'X', 10 * page_size);
  263. // We will grab the last 5 pages of the source and move them.
  264. void *remapped_mapping =
  265. mremap(source_mapping, 5 * page_size,
  266. 5 * page_size,
  267. MREMAP_DONTUNMAP | MREMAP_MAYMOVE | MREMAP_FIXED, dest_mapping);
  268. BUG_ON(dest_mapping == MAP_FAILED, "mremap");
  269. BUG_ON(dest_mapping != remapped_mapping, "expected to remap to dest_mapping");
  270. BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 0) !=
  271. 0, "first 5 pages of source should have no ptes");
  272. // Finally we expect the destination to have 5 pages worth of a's.
  273. BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') != 0,
  274. "dest mapping should contain ptes from the source");
  275. // Finally the last 5 pages shouldn't have been touched.
  276. BUG_ON(check_region_contains_byte(dest_mapping + (5 * page_size),
  277. 5 * page_size, 'X') != 0,
  278. "dest mapping should have retained the last 5 pages");
  279. BUG_ON(munmap(dest_mapping, 10 * page_size) == -1,
  280. "unable to unmap destination mapping");
  281. BUG_ON(munmap(source_mapping, 5 * page_size) == -1,
  282. "unable to unmap source mapping");
  283. }
  284. int main(void)
  285. {
  286. page_size = sysconf(_SC_PAGE_SIZE);
  287. // test for kernel support for MREMAP_DONTUNMAP skipping the test if
  288. // not.
  289. if (kernel_support_for_mremap_dontunmap() != 0) {
  290. printf("No kernel support for MREMAP_DONTUNMAP\n");
  291. return KSFT_SKIP;
  292. }
  293. // Keep a page sized buffer around for when we need it.
  294. page_buffer =
  295. mmap(NULL, page_size, PROT_READ | PROT_WRITE,
  296. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  297. BUG_ON(page_buffer == MAP_FAILED, "unable to mmap a page.");
  298. mremap_dontunmap_simple();
  299. mremap_dontunmap_simple_shmem();
  300. mremap_dontunmap_simple_fixed();
  301. mremap_dontunmap_partial_mapping();
  302. mremap_dontunmap_partial_mapping_overwrite();
  303. BUG_ON(munmap(page_buffer, page_size) == -1,
  304. "unable to unmap page buffer");
  305. printf("OK\n");
  306. return 0;
  307. }