uffd-wp-mremap.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define _GNU_SOURCE
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <fcntl.h>
  6. #include <assert.h>
  7. #include <linux/mman.h>
  8. #include <sys/mman.h>
  9. #include "kselftest.h"
  10. #include "thp_settings.h"
  11. #include "uffd-common.h"
  12. static int pagemap_fd;
  13. static size_t pagesize;
  14. static int nr_pagesizes = 1;
  15. static int nr_thpsizes;
  16. static size_t thpsizes[20];
  17. static int nr_hugetlbsizes;
  18. static size_t hugetlbsizes[10];
  19. static int detect_thp_sizes(size_t sizes[], int max)
  20. {
  21. int count = 0;
  22. unsigned long orders;
  23. size_t kb;
  24. int i;
  25. /* thp not supported at all. */
  26. if (!read_pmd_pagesize())
  27. return 0;
  28. orders = thp_supported_orders();
  29. for (i = 0; orders && count < max; i++) {
  30. if (!(orders & (1UL << i)))
  31. continue;
  32. orders &= ~(1UL << i);
  33. kb = (pagesize >> 10) << i;
  34. sizes[count++] = kb * 1024;
  35. ksft_print_msg("[INFO] detected THP size: %zu KiB\n", kb);
  36. }
  37. return count;
  38. }
  39. static void *mmap_aligned(size_t size, int prot, int flags)
  40. {
  41. size_t mmap_size = size * 2;
  42. char *mmap_mem, *mem;
  43. mmap_mem = mmap(NULL, mmap_size, prot, flags, -1, 0);
  44. if (mmap_mem == MAP_FAILED)
  45. return mmap_mem;
  46. mem = (char *)(((uintptr_t)mmap_mem + size - 1) & ~(size - 1));
  47. munmap(mmap_mem, mem - mmap_mem);
  48. munmap(mem + size, mmap_mem + mmap_size - mem - size);
  49. return mem;
  50. }
  51. static void *alloc_one_folio(size_t size, bool private, bool hugetlb)
  52. {
  53. bool thp = !hugetlb && size > pagesize;
  54. int flags = MAP_ANONYMOUS;
  55. int prot = PROT_READ | PROT_WRITE;
  56. char *mem, *addr;
  57. assert((size & (size - 1)) == 0);
  58. if (private)
  59. flags |= MAP_PRIVATE;
  60. else
  61. flags |= MAP_SHARED;
  62. /*
  63. * For THP, we must explicitly enable the THP size, allocate twice the
  64. * required space then manually align.
  65. */
  66. if (thp) {
  67. struct thp_settings settings = *thp_current_settings();
  68. if (private)
  69. settings.hugepages[sz2ord(size, pagesize)].enabled = THP_ALWAYS;
  70. else
  71. settings.shmem_hugepages[sz2ord(size, pagesize)].enabled = SHMEM_ALWAYS;
  72. thp_push_settings(&settings);
  73. mem = mmap_aligned(size, prot, flags);
  74. } else {
  75. if (hugetlb) {
  76. flags |= MAP_HUGETLB;
  77. flags |= __builtin_ctzll(size) << MAP_HUGE_SHIFT;
  78. }
  79. mem = mmap(NULL, size, prot, flags, -1, 0);
  80. }
  81. if (mem == MAP_FAILED) {
  82. mem = NULL;
  83. goto out;
  84. }
  85. assert(((uintptr_t)mem & (size - 1)) == 0);
  86. /*
  87. * Populate the folio by writing the first byte and check that all pages
  88. * are populated. Finally set the whole thing to non-zero data to avoid
  89. * kernel from mapping it back to the zero page.
  90. */
  91. mem[0] = 1;
  92. for (addr = mem; addr < mem + size; addr += pagesize) {
  93. if (!pagemap_is_populated(pagemap_fd, addr)) {
  94. munmap(mem, size);
  95. mem = NULL;
  96. goto out;
  97. }
  98. }
  99. memset(mem, 1, size);
  100. out:
  101. if (thp)
  102. thp_pop_settings();
  103. return mem;
  104. }
  105. static bool check_uffd_wp_state(void *mem, size_t size, bool expect)
  106. {
  107. uint64_t pte;
  108. void *addr;
  109. for (addr = mem; addr < mem + size; addr += pagesize) {
  110. pte = pagemap_get_entry(pagemap_fd, addr);
  111. if (!!(pte & PM_UFFD_WP) != expect) {
  112. ksft_test_result_fail("uffd-wp not %s for pte %lu!\n",
  113. expect ? "set" : "clear",
  114. (addr - mem) / pagesize);
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. static bool range_is_swapped(void *addr, size_t size)
  121. {
  122. for (; size; addr += pagesize, size -= pagesize)
  123. if (!pagemap_is_swapped(pagemap_fd, addr))
  124. return false;
  125. return true;
  126. }
  127. static void test_one_folio(uffd_global_test_opts_t *gopts, size_t size, bool private,
  128. bool swapout, bool hugetlb)
  129. {
  130. struct uffdio_writeprotect wp_prms;
  131. uint64_t features = 0;
  132. void *addr = NULL;
  133. void *mem = NULL;
  134. assert(!(hugetlb && swapout));
  135. ksft_print_msg("[RUN] %s(size=%zu, private=%s, swapout=%s, hugetlb=%s)\n",
  136. __func__,
  137. size,
  138. private ? "true" : "false",
  139. swapout ? "true" : "false",
  140. hugetlb ? "true" : "false");
  141. /* Allocate a folio of required size and type. */
  142. mem = alloc_one_folio(size, private, hugetlb);
  143. if (!mem) {
  144. ksft_test_result_fail("alloc_one_folio() failed\n");
  145. goto out;
  146. }
  147. /* Register range for uffd-wp. */
  148. if (userfaultfd_open(gopts, &features)) {
  149. if (errno == ENOENT)
  150. ksft_test_result_skip("userfaultfd not available\n");
  151. else
  152. ksft_test_result_fail("userfaultfd_open() failed\n");
  153. goto out;
  154. }
  155. if (uffd_register(gopts->uffd, mem, size, false, true, false)) {
  156. ksft_test_result_fail("uffd_register() failed\n");
  157. goto out;
  158. }
  159. wp_prms.mode = UFFDIO_WRITEPROTECT_MODE_WP;
  160. wp_prms.range.start = (uintptr_t)mem;
  161. wp_prms.range.len = size;
  162. if (ioctl(gopts->uffd, UFFDIO_WRITEPROTECT, &wp_prms)) {
  163. ksft_test_result_fail("ioctl(UFFDIO_WRITEPROTECT) failed\n");
  164. goto out;
  165. }
  166. if (swapout) {
  167. madvise(mem, size, MADV_PAGEOUT);
  168. if (!range_is_swapped(mem, size)) {
  169. ksft_test_result_skip("MADV_PAGEOUT did not work, is swap enabled?\n");
  170. goto out;
  171. }
  172. }
  173. /* Check that uffd-wp is set for all PTEs in range. */
  174. if (!check_uffd_wp_state(mem, size, true))
  175. goto out;
  176. /*
  177. * Move the mapping to a new, aligned location. Since
  178. * UFFD_FEATURE_EVENT_REMAP is not set, we expect the uffd-wp bit for
  179. * each PTE to be cleared in the new mapping.
  180. */
  181. addr = mmap_aligned(size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS);
  182. if (addr == MAP_FAILED) {
  183. ksft_test_result_fail("mmap_aligned() failed\n");
  184. goto out;
  185. }
  186. if (mremap(mem, size, size, MREMAP_FIXED | MREMAP_MAYMOVE, addr) == MAP_FAILED) {
  187. ksft_test_result_fail("mremap() failed\n");
  188. munmap(addr, size);
  189. goto out;
  190. }
  191. mem = addr;
  192. /* Check that uffd-wp is cleared for all PTEs in range. */
  193. if (!check_uffd_wp_state(mem, size, false))
  194. goto out;
  195. ksft_test_result_pass("%s(size=%zu, private=%s, swapout=%s, hugetlb=%s)\n",
  196. __func__,
  197. size,
  198. private ? "true" : "false",
  199. swapout ? "true" : "false",
  200. hugetlb ? "true" : "false");
  201. out:
  202. if (mem)
  203. munmap(mem, size);
  204. if (gopts->uffd >= 0) {
  205. close(gopts->uffd);
  206. gopts->uffd = -1;
  207. }
  208. }
  209. struct testcase {
  210. size_t *sizes;
  211. int *nr_sizes;
  212. bool private;
  213. bool swapout;
  214. bool hugetlb;
  215. };
  216. static const struct testcase testcases[] = {
  217. /* base pages. */
  218. {
  219. .sizes = &pagesize,
  220. .nr_sizes = &nr_pagesizes,
  221. .private = false,
  222. .swapout = false,
  223. .hugetlb = false,
  224. },
  225. {
  226. .sizes = &pagesize,
  227. .nr_sizes = &nr_pagesizes,
  228. .private = true,
  229. .swapout = false,
  230. .hugetlb = false,
  231. },
  232. {
  233. .sizes = &pagesize,
  234. .nr_sizes = &nr_pagesizes,
  235. .private = false,
  236. .swapout = true,
  237. .hugetlb = false,
  238. },
  239. {
  240. .sizes = &pagesize,
  241. .nr_sizes = &nr_pagesizes,
  242. .private = true,
  243. .swapout = true,
  244. .hugetlb = false,
  245. },
  246. /* thp. */
  247. {
  248. .sizes = thpsizes,
  249. .nr_sizes = &nr_thpsizes,
  250. .private = false,
  251. .swapout = false,
  252. .hugetlb = false,
  253. },
  254. {
  255. .sizes = thpsizes,
  256. .nr_sizes = &nr_thpsizes,
  257. .private = true,
  258. .swapout = false,
  259. .hugetlb = false,
  260. },
  261. {
  262. .sizes = thpsizes,
  263. .nr_sizes = &nr_thpsizes,
  264. .private = false,
  265. .swapout = true,
  266. .hugetlb = false,
  267. },
  268. {
  269. .sizes = thpsizes,
  270. .nr_sizes = &nr_thpsizes,
  271. .private = true,
  272. .swapout = true,
  273. .hugetlb = false,
  274. },
  275. /* hugetlb. */
  276. {
  277. .sizes = hugetlbsizes,
  278. .nr_sizes = &nr_hugetlbsizes,
  279. .private = false,
  280. .swapout = false,
  281. .hugetlb = true,
  282. },
  283. {
  284. .sizes = hugetlbsizes,
  285. .nr_sizes = &nr_hugetlbsizes,
  286. .private = true,
  287. .swapout = false,
  288. .hugetlb = true,
  289. },
  290. };
  291. int main(int argc, char **argv)
  292. {
  293. uffd_global_test_opts_t gopts = { 0 };
  294. struct thp_settings settings;
  295. int i, j, plan = 0;
  296. pagesize = getpagesize();
  297. nr_thpsizes = detect_thp_sizes(thpsizes, ARRAY_SIZE(thpsizes));
  298. nr_hugetlbsizes = detect_hugetlb_page_sizes(hugetlbsizes,
  299. ARRAY_SIZE(hugetlbsizes));
  300. /* If THP is supported, save THP settings and initially disable THP. */
  301. if (nr_thpsizes) {
  302. thp_save_settings();
  303. thp_read_settings(&settings);
  304. for (i = 0; i < NR_ORDERS; i++) {
  305. settings.hugepages[i].enabled = THP_NEVER;
  306. settings.shmem_hugepages[i].enabled = SHMEM_NEVER;
  307. }
  308. thp_push_settings(&settings);
  309. }
  310. for (i = 0; i < ARRAY_SIZE(testcases); i++)
  311. plan += *testcases[i].nr_sizes;
  312. ksft_set_plan(plan);
  313. pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
  314. if (pagemap_fd < 0)
  315. ksft_exit_fail_msg("opening pagemap failed\n");
  316. for (i = 0; i < ARRAY_SIZE(testcases); i++) {
  317. const struct testcase *tc = &testcases[i];
  318. for (j = 0; j < *tc->nr_sizes; j++)
  319. test_one_folio(&gopts, tc->sizes[j], tc->private,
  320. tc->swapout, tc->hugetlb);
  321. }
  322. /* If THP is supported, restore original THP settings. */
  323. if (nr_thpsizes)
  324. thp_restore_settings();
  325. i = ksft_get_fail_cnt();
  326. if (i)
  327. ksft_exit_fail_msg("%d out of %d tests failed\n",
  328. i, ksft_test_num());
  329. ksft_exit_pass();
  330. }