They are useful information for debugging split huge page tests. Signed-off-by: Zi Yan --- mm/huge_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2b4ea5a2ce7d..8a11c2d402d4 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4327,8 +4327,8 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, goto out; } - pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx]\n", - pid, vaddr_start, vaddr_end); + pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx], new_order: %u, in_folio_offset %ld\n", + pid, vaddr_start, vaddr_end, new_order, in_folio_offset); mmap_read_lock(mm); /* -- 2.47.2 Current behavior is to move to next PAGE_SIZE and split, but that makes it hard to check after-split folio orders. This is a preparation patch to allow more precise split_huge_page_test check in an upcoming commit. split_folio_to_order() part is not changed, since split_pte_mapped_thp test relies on its current behavior. Signed-off-by: Zi Yan --- mm/huge_memory.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 8a11c2d402d4..b2ce8ac0c5a9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4341,6 +4341,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, struct folio *folio; struct address_space *mapping; unsigned int target_order = new_order; + long nr_pages; if (!vma) break; @@ -4358,6 +4359,8 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, if (!is_transparent_hugepage(folio)) goto next; + nr_pages = folio_nr_pages(folio); + if (!folio_test_anon(folio)) { mapping = folio->mapping; target_order = max(new_order, @@ -4385,15 +4388,16 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, if (!folio_test_anon(folio) && folio->mapping != mapping) goto unlock; - if (in_folio_offset < 0 || - in_folio_offset >= folio_nr_pages(folio)) { + if (in_folio_offset < 0 || in_folio_offset >= nr_pages) { if (!split_folio_to_order(folio, target_order)) split++; } else { - struct page *split_at = folio_page(folio, - in_folio_offset); - if (!folio_split(folio, target_order, split_at, NULL)) + struct page *split_at = + folio_page(folio, in_folio_offset); + if (!folio_split(folio, target_order, split_at, NULL)) { split++; + addr += PAGE_SIZE * nr_pages; + } } unlock: @@ -4438,8 +4442,8 @@ static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, if (IS_ERR(candidate)) goto out; - pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx]\n", - file_path, off_start, off_end); + pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx], new_order %u in_folio_offset %ld\n", + file_path, off_start, off_end, new_order, in_folio_offset); mapping = candidate->f_mapping; min_order = mapping_min_folio_order(mapping); -- 2.47.2 The helper gathers an folio order statistics of folios within a virtual address range and checks it against a given order list. It aims to provide a more precise folio order check instead of just checking the existence of PMD folios. Signed-off-by: Zi Yan --- tools/testing/selftests/mm/vm_util.c | 139 +++++++++++++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 2 + 2 files changed, 141 insertions(+) diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 9dafa7669ef9..373621145b2a 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -17,6 +17,12 @@ #define STATUS_FILE_PATH "/proc/self/status" #define MAX_LINE_LENGTH 500 +#define PGMAP_PRESENT (1UL << 63) +#define KPF_COMPOUND_HEAD (1UL << 15) +#define KPF_COMPOUND_TAIL (1UL << 16) +#define KPF_THP (1UL << 22) +#define PFN_MASK ((1UL<<55)-1) + unsigned int __page_size; unsigned int __page_shift; @@ -338,6 +344,139 @@ int detect_hugetlb_page_sizes(size_t sizes[], int max) return count; } +static int get_page_flags(uint64_t vpn, int pagemap_file, int kpageflags_file, + uint64_t *flags) +{ + uint64_t pfn; + size_t count; + + count = pread(pagemap_file, &pfn, sizeof(pfn), + vpn * sizeof(pfn)); + + if (count != sizeof(pfn)) + return -1; + + /* + * Treat non-present page as a page without any flag, so that + * gather_folio_orders() just record the current folio order. + */ + if (!(pfn & PGMAP_PRESENT)) { + *flags = 0; + return 0; + } + + count = pread(kpageflags_file, flags, sizeof(*flags), + (pfn & PFN_MASK) * sizeof(*flags)); + + if (count != sizeof(*flags)) + return -1; + + return 0; +} + +static int gather_folio_orders(uint64_t vpn_start, size_t nr_pages, + int pagemap_file, int kpageflags_file, + int orders[], int nr_orders) +{ + uint64_t page_flags = 0; + int cur_order = -1; + uint64_t vpn; + + if (!pagemap_file || !kpageflags_file) + return -1; + if (nr_orders <= 0) + return -1; + + for (vpn = vpn_start; vpn < vpn_start + nr_pages; ) { + uint64_t next_folio_vpn; + int status; + + if (get_page_flags(vpn, pagemap_file, kpageflags_file, &page_flags)) + return -1; + + /* all order-0 pages with possible false postive (non folio) */ + if (!(page_flags & (KPF_COMPOUND_HEAD | KPF_COMPOUND_TAIL))) { + orders[0]++; + vpn++; + continue; + } + + /* skip non thp compound pages */ + if (!(page_flags & KPF_THP)) { + vpn++; + continue; + } + + /* vpn points to part of a THP at this point */ + if (page_flags & KPF_COMPOUND_HEAD) + cur_order = 1; + else { + /* not a head nor a tail in a THP? */ + if (!(page_flags & KPF_COMPOUND_TAIL)) + return -1; + continue; + } + + next_folio_vpn = vpn + (1 << cur_order); + + if (next_folio_vpn >= vpn_start + nr_pages) + break; + + while (!(status = get_page_flags(next_folio_vpn, pagemap_file, + kpageflags_file, + &page_flags))) { + /* next compound head page or order-0 page */ + if ((page_flags & KPF_COMPOUND_HEAD) || + !(page_flags & (KPF_COMPOUND_HEAD | + KPF_COMPOUND_TAIL))) { + if (cur_order < nr_orders) { + orders[cur_order]++; + cur_order = -1; + vpn = next_folio_vpn; + } + break; + } + + /* not a head nor a tail in a THP? */ + if (!(page_flags & KPF_COMPOUND_TAIL)) + return -1; + + cur_order++; + next_folio_vpn = vpn + (1 << cur_order); + } + + if (status) + return status; + } + if (cur_order > 0 && cur_order < nr_orders) + orders[cur_order]++; + return 0; +} + +int check_folio_orders(uint64_t vpn_start, size_t nr_pages, int pagemap_file, + int kpageflags_file, int orders[], int nr_orders) +{ + int vpn_orders[nr_orders]; + int status; + int i; + + memset(vpn_orders, 0, sizeof(int) * nr_orders); + status = gather_folio_orders(vpn_start, nr_pages, pagemap_file, + kpageflags_file, vpn_orders, nr_orders); + if (status) + return status; + + status = 0; + for (i = 0; i < nr_orders; i++) + if (vpn_orders[i] != orders[i]) { + ksft_print_msg("order %d: expected: %d got %d\n", i, + orders[i], vpn_orders[i]); + status = -1; + } + + return status; +} + /* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */ int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, bool miss, bool wp, bool minor, uint64_t *ioctls) diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index b55d1809debc..dee9504a6129 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -85,6 +85,8 @@ bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size); int64_t allocate_transhuge(void *ptr, int pagemap_fd); unsigned long default_huge_page_size(void); int detect_hugetlb_page_sizes(size_t sizes[], int max); +int check_folio_orders(uint64_t vpn_start, size_t nr_pages, int pagemap_file, + int kpageflags_file, int orders[], int nr_orders); int uffd_register(int uffd, void *addr, uint64_t len, bool miss, bool wp, bool minor); -- 2.47.2 Instead of just checking the existence of PMD folios before and after folio split tests, use check_folio_orders() to check after-split folio orders. The following tests are not changed: 1. split_pte_mapped_thp: the test already uses kpageflags to check; 2. split_file_backed_thp: no vaddr available. Signed-off-by: Zi Yan --- .../selftests/mm/split_huge_page_test.c | 67 +++++++++++++------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c index 3c761228e451..cd34000279fe 100644 --- a/tools/testing/selftests/mm/split_huge_page_test.c +++ b/tools/testing/selftests/mm/split_huge_page_test.c @@ -25,6 +25,10 @@ uint64_t pagesize; unsigned int pageshift; uint64_t pmd_pagesize; +unsigned int pmd_order; +unsigned int max_order; + +#define NR_ORDERS (max_order + 1) #define SPLIT_DEBUGFS "/sys/kernel/debug/split_huge_pages" #define SMAP_PATH "/proc/self/smaps" @@ -38,6 +42,11 @@ uint64_t pmd_pagesize; #define KPF_THP (1UL<<22) #define GET_ORDER(nr_pages) (31 - __builtin_clz(nr_pages)) +const char *pagemap_proc = "/proc/self/pagemap"; +const char *kpageflags_proc = "/proc/kpageflags"; +int pagemap_fd; +int kpageflags_fd; + int is_backed_by_thp(char *vaddr, int pagemap_file, int kpageflags_file) { uint64_t paddr; @@ -153,6 +162,7 @@ void split_pmd_thp_to_order(int order) char *one_page; size_t len = 4 * pmd_pagesize; size_t i; + int orders[NR_ORDERS]; one_page = memalign(pmd_pagesize, len); if (!one_page) @@ -174,6 +184,12 @@ void split_pmd_thp_to_order(int order) if (one_page[i] != (char)i) ksft_exit_fail_msg("%ld byte corrupted\n", i); + memset(orders, 0, sizeof(int) * NR_ORDERS); + orders[order] = 4 << (pmd_order - order); + if (check_folio_orders((uint64_t)one_page >> pageshift, + len >> pageshift, pagemap_fd, kpageflags_fd, + orders, NR_ORDERS)) + ksft_exit_fail_msg("Unexpected THP split\n"); if (!check_huge_anon(one_page, 0, pmd_pagesize)) ksft_exit_fail_msg("Still AnonHugePages not split\n"); @@ -188,22 +204,6 @@ void split_pte_mapped_thp(void) size_t len = 4 * pmd_pagesize; uint64_t thp_size; size_t i; - const char *pagemap_template = "/proc/%d/pagemap"; - const char *kpageflags_proc = "/proc/kpageflags"; - char pagemap_proc[255]; - int pagemap_fd; - int kpageflags_fd; - - if (snprintf(pagemap_proc, 255, pagemap_template, getpid()) < 0) - ksft_exit_fail_msg("get pagemap proc error: %s\n", strerror(errno)); - - pagemap_fd = open(pagemap_proc, O_RDONLY); - if (pagemap_fd == -1) - ksft_exit_fail_msg("read pagemap: %s\n", strerror(errno)); - - kpageflags_fd = open(kpageflags_proc, O_RDONLY); - if (kpageflags_fd == -1) - ksft_exit_fail_msg("read kpageflags: %s\n", strerror(errno)); one_page = mmap((void *)(1UL << 30), len, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); @@ -261,8 +261,6 @@ void split_pte_mapped_thp(void) ksft_test_result_pass("Split PTE-mapped huge pages successful\n"); munmap(one_page, len); - close(pagemap_fd); - close(kpageflags_fd); } void split_file_backed_thp(int order) @@ -471,6 +469,7 @@ void split_thp_in_pagecache_to_order_at(size_t fd_size, const char *fs_loc, size_t i; char testfile[INPUT_MAX]; int err = 0; + int orders[NR_ORDERS]; err = snprintf(testfile, INPUT_MAX, "%s/test", fs_loc); @@ -482,12 +481,21 @@ void split_thp_in_pagecache_to_order_at(size_t fd_size, const char *fs_loc, return; err = 0; - if (offset == -1) + memset(orders, 0, sizeof(int) * NR_ORDERS); + if (offset == -1) { write_debugfs(PID_FMT, getpid(), (uint64_t)addr, (uint64_t)addr + fd_size, order); - else + orders[order] = fd_size / (pagesize << order); + } else { + int times = fd_size / pmd_pagesize; + write_debugfs(PID_FMT_OFFSET, getpid(), (uint64_t)addr, (uint64_t)addr + fd_size, order, offset); + for (i = order + 1; i < pmd_order; i++) + orders[i] = times; + + orders[order] = 2 * times; + } for (i = 0; i < fd_size; i++) if (*(addr + i) != (char)i) { @@ -496,6 +504,12 @@ void split_thp_in_pagecache_to_order_at(size_t fd_size, const char *fs_loc, goto out; } + if (check_folio_orders((uint64_t)addr >> pageshift, + fd_size >> pageshift, pagemap_fd, kpageflags_fd, + orders, NR_ORDERS)) + ksft_exit_fail_msg("Unexpected THP split\n"); + + if (!check_huge_file(addr, 0, pmd_pagesize)) { ksft_print_msg("Still FilePmdMapped not split\n"); err = EXIT_FAILURE; @@ -526,7 +540,6 @@ int main(int argc, char **argv) const char *fs_loc; bool created_tmp; int offset; - unsigned int max_order; unsigned int nr_pages; unsigned int tests; @@ -543,6 +556,7 @@ int main(int argc, char **argv) pagesize = getpagesize(); pageshift = ffs(pagesize) - 1; pmd_pagesize = read_pmd_pagesize(); + pmd_order = GET_ORDER(pmd_pagesize / pagesize); if (!pmd_pagesize) ksft_exit_fail_msg("Reading PMD pagesize failed\n"); @@ -551,6 +565,14 @@ int main(int argc, char **argv) tests = 2 + (max_order - 1) + (2 * max_order) + (max_order - 1) * 4 + 2; ksft_set_plan(tests); + pagemap_fd = open(pagemap_proc, O_RDONLY); + if (pagemap_fd == -1) + ksft_exit_fail_msg("read pagemap: %s\n", strerror(errno)); + + kpageflags_fd = open(kpageflags_proc, O_RDONLY); + if (kpageflags_fd == -1) + ksft_exit_fail_msg("read kpageflags: %s\n", strerror(errno)); + fd_size = 2 * pmd_pagesize; split_pmd_zero_pages(); @@ -575,6 +597,9 @@ int main(int argc, char **argv) split_thp_in_pagecache_to_order_at(fd_size, fs_loc, i, offset); cleanup_thp_fs(fs_loc, created_tmp); + close(pagemap_fd); + close(kpageflags_fd); + ksft_finished(); return 0; -- 2.47.2