1 // SPDX-License-Identifier: GPL-2.0
3 * A test of splitting PMD THPs and PTE-mapped THPs from a specified virtual
4 * address range in a process via <debugfs>/split_huge_pages interface.
16 #include <sys/mount.h>
21 #include "../kselftest.h"
24 unsigned int pageshift;
25 uint64_t pmd_pagesize;
27 #define SPLIT_DEBUGFS "/sys/kernel/debug/split_huge_pages"
28 #define SMAP_PATH "/proc/self/smaps"
31 #define PID_FMT "%d,0x%lx,0x%lx,%d"
32 #define PATH_FMT "%s,0x%lx,0x%lx,%d"
34 #define PFN_MASK ((1UL<<55)-1)
35 #define KPF_THP (1UL<<22)
37 int is_backed_by_thp(char *vaddr, int pagemap_file, int kpageflags_file)
43 pread(pagemap_file, &paddr, sizeof(paddr),
44 ((long)vaddr >> pageshift) * sizeof(paddr));
46 if (kpageflags_file) {
47 pread(kpageflags_file, &page_flags, sizeof(page_flags),
48 (paddr & PFN_MASK) * sizeof(page_flags));
50 return !!(page_flags & KPF_THP);
56 static void write_file(const char *path, const char *buf, size_t buflen)
61 fd = open(path, O_WRONLY);
63 ksft_exit_fail_msg("%s open failed: %s\n", path, strerror(errno));
65 numwritten = write(fd, buf, buflen - 1);
68 ksft_exit_fail_msg("Write failed\n");
71 static void write_debugfs(const char *fmt, ...)
73 char input[INPUT_MAX];
78 ret = vsnprintf(input, INPUT_MAX, fmt, argp);
82 ksft_exit_fail_msg("%s: Debugfs input is too long\n", __func__);
84 write_file(SPLIT_DEBUGFS, input, ret + 1);
87 static char *allocate_zero_filled_hugepage(size_t len)
92 result = memalign(pmd_pagesize, len);
94 printf("Fail to allocate memory\n");
98 madvise(result, len, MADV_HUGEPAGE);
100 for (i = 0; i < len; i++)
106 static void verify_rss_anon_split_huge_page_all_zeroes(char *one_page, int nr_hpages, size_t len)
108 unsigned long rss_anon_before, rss_anon_after;
111 if (!check_huge_anon(one_page, 4, pmd_pagesize)) {
112 printf("No THP is allocated\n");
116 rss_anon_before = rss_anon();
117 if (!rss_anon_before) {
118 printf("No RssAnon is allocated before split\n");
123 write_debugfs(PID_FMT, getpid(), (uint64_t)one_page,
124 (uint64_t)one_page + len, 0);
126 for (i = 0; i < len; i++)
127 if (one_page[i] != (char)0) {
128 printf("%ld byte corrupted\n", i);
132 if (!check_huge_anon(one_page, 0, pmd_pagesize)) {
133 printf("Still AnonHugePages not split\n");
137 rss_anon_after = rss_anon();
138 if (rss_anon_after >= rss_anon_before) {
139 printf("Incorrect RssAnon value. Before: %ld After: %ld\n",
140 rss_anon_before, rss_anon_after);
145 void split_pmd_zero_pages(void)
149 size_t len = nr_hpages * pmd_pagesize;
151 one_page = allocate_zero_filled_hugepage(len);
152 verify_rss_anon_split_huge_page_all_zeroes(one_page, nr_hpages, len);
153 printf("Split zero filled huge pages successful\n");
157 void split_pmd_thp(void)
160 size_t len = 4 * pmd_pagesize;
163 one_page = memalign(pmd_pagesize, len);
165 ksft_exit_fail_msg("Fail to allocate memory: %s\n", strerror(errno));
167 madvise(one_page, len, MADV_HUGEPAGE);
169 for (i = 0; i < len; i++)
170 one_page[i] = (char)i;
172 if (!check_huge_anon(one_page, 4, pmd_pagesize))
173 ksft_exit_fail_msg("No THP is allocated\n");
176 write_debugfs(PID_FMT, getpid(), (uint64_t)one_page,
177 (uint64_t)one_page + len, 0);
179 for (i = 0; i < len; i++)
180 if (one_page[i] != (char)i)
181 ksft_exit_fail_msg("%ld byte corrupted\n", i);
184 if (!check_huge_anon(one_page, 0, pmd_pagesize))
185 ksft_exit_fail_msg("Still AnonHugePages not split\n");
187 ksft_test_result_pass("Split huge pages successful\n");
191 void split_pte_mapped_thp(void)
193 char *one_page, *pte_mapped, *pte_mapped2;
194 size_t len = 4 * pmd_pagesize;
197 const char *pagemap_template = "/proc/%d/pagemap";
198 const char *kpageflags_proc = "/proc/kpageflags";
199 char pagemap_proc[255];
203 if (snprintf(pagemap_proc, 255, pagemap_template, getpid()) < 0)
204 ksft_exit_fail_msg("get pagemap proc error: %s\n", strerror(errno));
206 pagemap_fd = open(pagemap_proc, O_RDONLY);
207 if (pagemap_fd == -1)
208 ksft_exit_fail_msg("read pagemap: %s\n", strerror(errno));
210 kpageflags_fd = open(kpageflags_proc, O_RDONLY);
211 if (kpageflags_fd == -1)
212 ksft_exit_fail_msg("read kpageflags: %s\n", strerror(errno));
214 one_page = mmap((void *)(1UL << 30), len, PROT_READ | PROT_WRITE,
215 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
216 if (one_page == MAP_FAILED)
217 ksft_exit_fail_msg("Fail to allocate memory: %s\n", strerror(errno));
219 madvise(one_page, len, MADV_HUGEPAGE);
221 for (i = 0; i < len; i++)
222 one_page[i] = (char)i;
224 if (!check_huge_anon(one_page, 4, pmd_pagesize))
225 ksft_exit_fail_msg("No THP is allocated\n");
227 /* remap the first pagesize of first THP */
228 pte_mapped = mremap(one_page, pagesize, pagesize, MREMAP_MAYMOVE);
230 /* remap the Nth pagesize of Nth THP */
231 for (i = 1; i < 4; i++) {
232 pte_mapped2 = mremap(one_page + pmd_pagesize * i + pagesize * i,
234 MREMAP_MAYMOVE|MREMAP_FIXED,
235 pte_mapped + pagesize * i);
236 if (pte_mapped2 == MAP_FAILED)
237 ksft_exit_fail_msg("mremap failed: %s\n", strerror(errno));
240 /* smap does not show THPs after mremap, use kpageflags instead */
242 for (i = 0; i < pagesize * 4; i++)
243 if (i % pagesize == 0 &&
244 is_backed_by_thp(&pte_mapped[i], pagemap_fd, kpageflags_fd))
248 ksft_exit_fail_msg("Some THPs are missing during mremap\n");
250 /* split all remapped THPs */
251 write_debugfs(PID_FMT, getpid(), (uint64_t)pte_mapped,
252 (uint64_t)pte_mapped + pagesize * 4, 0);
254 /* smap does not show THPs after mremap, use kpageflags instead */
256 for (i = 0; i < pagesize * 4; i++) {
257 if (pte_mapped[i] != (char)i)
258 ksft_exit_fail_msg("%ld byte corrupted\n", i);
260 if (i % pagesize == 0 &&
261 is_backed_by_thp(&pte_mapped[i], pagemap_fd, kpageflags_fd))
266 ksft_exit_fail_msg("Still %ld THPs not split\n", thp_size);
268 ksft_test_result_pass("Split PTE-mapped huge pages successful\n");
269 munmap(one_page, len);
271 close(kpageflags_fd);
274 void split_file_backed_thp(void)
279 char tmpfs_template[] = "/tmp/thp_split_XXXXXX";
280 const char *tmpfs_loc = mkdtemp(tmpfs_template);
281 char testfile[INPUT_MAX];
282 uint64_t pgoff_start = 0, pgoff_end = 1024;
284 ksft_print_msg("Please enable pr_debug in split_huge_pages_in_file() for more info.\n");
286 status = mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=always,size=4m");
289 ksft_exit_fail_msg("Unable to create a tmpfs for testing\n");
291 status = snprintf(testfile, INPUT_MAX, "%s/thp_file", tmpfs_loc);
292 if (status >= INPUT_MAX) {
293 ksft_exit_fail_msg("Fail to create file-backed THP split testing file\n");
296 fd = open(testfile, O_CREAT|O_WRONLY, 0664);
298 ksft_perror("Cannot open testing file");
302 /* write something to the file, so a file-backed THP can be allocated */
303 num_written = write(fd, tmpfs_loc, strlen(tmpfs_loc) + 1);
306 if (num_written < 1) {
307 ksft_perror("Fail to write data to testing file");
311 /* split the file-backed THP */
312 write_debugfs(PATH_FMT, testfile, pgoff_start, pgoff_end, 0);
314 status = unlink(testfile);
316 ksft_perror("Cannot remove testing file");
320 status = umount(tmpfs_loc);
323 ksft_exit_fail_msg("Unable to umount %s\n", tmpfs_loc);
326 status = rmdir(tmpfs_loc);
328 ksft_exit_fail_msg("cannot remove tmp dir: %s\n", strerror(errno));
330 ksft_print_msg("Please check dmesg for more information\n");
331 ksft_test_result_pass("File-backed THP split test done\n");
337 ksft_exit_fail_msg("Error occurred\n");
340 bool prepare_thp_fs(const char *xfs_path, char *thp_fs_template,
341 const char **thp_fs_loc)
344 *thp_fs_loc = xfs_path;
348 *thp_fs_loc = mkdtemp(thp_fs_template);
351 ksft_exit_fail_msg("cannot create temp folder\n");
356 void cleanup_thp_fs(const char *thp_fs_loc, bool created_tmp)
363 status = rmdir(thp_fs_loc);
365 ksft_exit_fail_msg("cannot remove tmp dir: %s\n",
369 int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
377 *fd = open(testfile, O_CREAT | O_RDWR, 0664);
379 ksft_exit_fail_msg("Failed to create a file at %s\n", testfile);
381 for (i = 0; i < fd_size; i++) {
382 unsigned char byte = (unsigned char)i;
384 write(*fd, &byte, sizeof(byte));
388 *fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
390 ksft_perror("open drop_caches");
393 if (write(*fd, "3", 1) != 1) {
394 ksft_perror("write to drop_caches");
399 *fd = open(testfile, O_RDWR);
401 ksft_perror("Failed to open testfile\n");
405 *addr = mmap(NULL, fd_size, PROT_READ|PROT_WRITE, MAP_SHARED, *fd, 0);
406 if (*addr == (char *)-1) {
407 ksft_perror("cannot mmap");
410 madvise(*addr, fd_size, MADV_HUGEPAGE);
412 for (size_t i = 0; i < fd_size; i++)
413 dummy += *(*addr + i);
414 asm volatile("" : "+r" (dummy));
416 if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) {
417 ksft_print_msg("No large pagecache folio generated, please provide a filesystem supporting large folio\n");
418 munmap(*addr, fd_size);
421 ksft_test_result_skip("Pagecache folio split skipped\n");
429 ksft_exit_fail_msg("Failed to create large pagecache folios\n");
433 void split_thp_in_pagecache_to_order(size_t fd_size, int order, const char *fs_loc)
438 char testfile[INPUT_MAX];
441 err = snprintf(testfile, INPUT_MAX, "%s/test", fs_loc);
444 ksft_exit_fail_msg("cannot generate right test file name\n");
446 err = create_pagecache_thp_and_fd(testfile, fd_size, &fd, &addr);
451 write_debugfs(PID_FMT, getpid(), (uint64_t)addr, (uint64_t)addr + fd_size, order);
453 for (i = 0; i < fd_size; i++)
454 if (*(addr + i) != (char)i) {
455 ksft_print_msg("%lu byte corrupted in the file\n", i);
460 if (!check_huge_file(addr, 0, pmd_pagesize)) {
461 ksft_print_msg("Still FilePmdMapped not split\n");
467 munmap(addr, fd_size);
471 ksft_exit_fail_msg("Split PMD-mapped pagecache folio to order %d failed\n", order);
472 ksft_test_result_pass("Split PMD-mapped pagecache folio to order %d passed\n", order);
475 int main(int argc, char **argv)
479 char *optional_xfs_path = NULL;
480 char fs_loc_template[] = "/tmp/thp_fs_XXXXXX";
486 if (geteuid() != 0) {
487 ksft_print_msg("Please run the benchmark as root\n");
492 optional_xfs_path = argv[1];
496 pagesize = getpagesize();
497 pageshift = ffs(pagesize) - 1;
498 pmd_pagesize = read_pmd_pagesize();
500 ksft_exit_fail_msg("Reading PMD pagesize failed\n");
502 fd_size = 2 * pmd_pagesize;
504 split_pmd_zero_pages();
506 split_pte_mapped_thp();
507 split_file_backed_thp();
509 created_tmp = prepare_thp_fs(optional_xfs_path, fs_loc_template,
511 for (i = 8; i >= 0; i--)
512 split_thp_in_pagecache_to_order(fd_size, i, fs_loc);
513 cleanup_thp_fs(fs_loc, created_tmp);