Skip to content

Commit fc4d182

Browse files
x-y-zakpm00
authored andcommitted
mm: huge_memory: enable debugfs to split huge pages to any order
It is used to test split_huge_page_to_list_to_order for pagecache THPs. Also add test cases for split_huge_page_to_list_to_order via both debugfs. [[email protected]: fix issue discovered with NFS] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Zi Yan <[email protected]> Tested-by: Aishwarya TCV <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Luis Chamberlain <[email protected]> Cc: "Matthew Wilcox (Oracle)" <[email protected]> Cc: Michal Koutny <[email protected]> Cc: Roman Gushchin <[email protected]> Cc: Ryan Roberts <[email protected]> Cc: Yang Shi <[email protected]> Cc: Yu Zhao <[email protected]> Cc: Zach O'Keefe <[email protected]> Cc: Aishwarya TCV <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent c010d47 commit fc4d182

File tree

3 files changed

+205
-19
lines changed

3 files changed

+205
-19
lines changed

mm/huge_memory.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,7 +3419,7 @@ static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma)
34193419
}
34203420

34213421
static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
3422-
unsigned long vaddr_end)
3422+
unsigned long vaddr_end, unsigned int new_order)
34233423
{
34243424
int ret = 0;
34253425
struct task_struct *task;
@@ -3483,13 +3483,19 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
34833483
goto next;
34843484

34853485
total++;
3486-
if (!can_split_folio(folio, NULL))
3486+
/*
3487+
* For folios with private, split_huge_page_to_list_to_order()
3488+
* will try to drop it before split and then check if the folio
3489+
* can be split or not. So skip the check here.
3490+
*/
3491+
if (!folio_test_private(folio) &&
3492+
!can_split_folio(folio, NULL))
34873493
goto next;
34883494

34893495
if (!folio_trylock(folio))
34903496
goto next;
34913497

3492-
if (!split_folio(folio))
3498+
if (!split_folio_to_order(folio, new_order))
34933499
split++;
34943500

34953501
folio_unlock(folio);
@@ -3507,7 +3513,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
35073513
}
35083514

35093515
static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
3510-
pgoff_t off_end)
3516+
pgoff_t off_end, unsigned int new_order)
35113517
{
35123518
struct filename *file;
35133519
struct file *candidate;
@@ -3546,7 +3552,7 @@ static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
35463552
if (!folio_trylock(folio))
35473553
goto next;
35483554

3549-
if (!split_folio(folio))
3555+
if (!split_folio_to_order(folio, new_order))
35503556
split++;
35513557

35523558
folio_unlock(folio);
@@ -3571,10 +3577,14 @@ static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
35713577
{
35723578
static DEFINE_MUTEX(split_debug_mutex);
35733579
ssize_t ret;
3574-
/* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */
3580+
/*
3581+
* hold pid, start_vaddr, end_vaddr, new_order or
3582+
* file_path, off_start, off_end, new_order
3583+
*/
35753584
char input_buf[MAX_INPUT_BUF_SZ];
35763585
int pid;
35773586
unsigned long vaddr_start, vaddr_end;
3587+
unsigned int new_order = 0;
35783588

35793589
ret = mutex_lock_interruptible(&split_debug_mutex);
35803590
if (ret)
@@ -3603,29 +3613,29 @@ static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
36033613
goto out;
36043614
}
36053615

3606-
ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end);
3607-
if (ret != 2) {
3616+
ret = sscanf(buf, "0x%lx,0x%lx,%d", &off_start, &off_end, &new_order);
3617+
if (ret != 2 && ret != 3) {
36083618
ret = -EINVAL;
36093619
goto out;
36103620
}
3611-
ret = split_huge_pages_in_file(file_path, off_start, off_end);
3621+
ret = split_huge_pages_in_file(file_path, off_start, off_end, new_order);
36123622
if (!ret)
36133623
ret = input_len;
36143624

36153625
goto out;
36163626
}
36173627

3618-
ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end);
3628+
ret = sscanf(input_buf, "%d,0x%lx,0x%lx,%d", &pid, &vaddr_start, &vaddr_end, &new_order);
36193629
if (ret == 1 && pid == 1) {
36203630
split_huge_pages_all();
36213631
ret = strlen(input_buf);
36223632
goto out;
3623-
} else if (ret != 3) {
3633+
} else if (ret != 3 && ret != 4) {
36243634
ret = -EINVAL;
36253635
goto out;
36263636
}
36273637

3628-
ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end);
3638+
ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order);
36293639
if (!ret)
36303640
ret = strlen(input_buf);
36313641
out:

tools/testing/selftests/mm/run_vmtests.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,27 @@ CATEGORY="thp" run_test ./khugepaged -s 2
399399

400400
CATEGORY="thp" run_test ./transhuge-stress -d 20
401401

402-
CATEGORY="thp" run_test ./split_huge_page_test
402+
# Try to create XFS if not provided
403+
if [ -z "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then
404+
if test_selected "thp"; then
405+
if grep xfs /proc/filesystems &>/dev/null; then
406+
XFS_IMG=$(mktemp /tmp/xfs_img_XXXXXX)
407+
SPLIT_HUGE_PAGE_TEST_XFS_PATH=$(mktemp -d /tmp/xfs_dir_XXXXXX)
408+
truncate -s 314572800 ${XFS_IMG}
409+
mkfs.xfs -q ${XFS_IMG}
410+
mount -o loop ${XFS_IMG} ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
411+
MOUNTED_XFS=1
412+
fi
413+
fi
414+
fi
415+
416+
CATEGORY="thp" run_test ./split_huge_page_test ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
417+
418+
if [ -n "${MOUNTED_XFS}" ]; then
419+
umount ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
420+
rmdir ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
421+
rm -f ${XFS_IMG}
422+
fi
403423

404424
CATEGORY="migration" run_test ./migration
405425

tools/testing/selftests/mm/split_huge_page_test.c

Lines changed: 162 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <sys/mount.h>
1717
#include <malloc.h>
1818
#include <stdbool.h>
19+
#include <time.h>
1920
#include "vm_util.h"
2021
#include "../kselftest.h"
2122

@@ -24,10 +25,11 @@ unsigned int pageshift;
2425
uint64_t pmd_pagesize;
2526

2627
#define SPLIT_DEBUGFS "/sys/kernel/debug/split_huge_pages"
28+
#define SMAP_PATH "/proc/self/smaps"
2729
#define INPUT_MAX 80
2830

29-
#define PID_FMT "%d,0x%lx,0x%lx"
30-
#define PATH_FMT "%s,0x%lx,0x%lx"
31+
#define PID_FMT "%d,0x%lx,0x%lx,%d"
32+
#define PATH_FMT "%s,0x%lx,0x%lx,%d"
3133

3234
#define PFN_MASK ((1UL<<55)-1)
3335
#define KPF_THP (1UL<<22)
@@ -102,7 +104,7 @@ void split_pmd_thp(void)
102104

103105
/* split all THPs */
104106
write_debugfs(PID_FMT, getpid(), (uint64_t)one_page,
105-
(uint64_t)one_page + len);
107+
(uint64_t)one_page + len, 0);
106108

107109
for (i = 0; i < len; i++)
108110
if (one_page[i] != (char)i)
@@ -177,7 +179,7 @@ void split_pte_mapped_thp(void)
177179

178180
/* split all remapped THPs */
179181
write_debugfs(PID_FMT, getpid(), (uint64_t)pte_mapped,
180-
(uint64_t)pte_mapped + pagesize * 4);
182+
(uint64_t)pte_mapped + pagesize * 4, 0);
181183

182184
/* smap does not show THPs after mremap, use kpageflags instead */
183185
thp_size = 0;
@@ -237,7 +239,7 @@ void split_file_backed_thp(void)
237239
}
238240

239241
/* split the file-backed THP */
240-
write_debugfs(PATH_FMT, testfile, pgoff_start, pgoff_end);
242+
write_debugfs(PATH_FMT, testfile, pgoff_start, pgoff_end, 0);
241243

242244
status = unlink(testfile);
243245
if (status) {
@@ -265,26 +267,180 @@ void split_file_backed_thp(void)
265267
ksft_exit_fail_msg("Error occurred\n");
266268
}
267269

270+
bool prepare_thp_fs(const char *xfs_path, char *thp_fs_template,
271+
const char **thp_fs_loc)
272+
{
273+
if (xfs_path) {
274+
*thp_fs_loc = xfs_path;
275+
return false;
276+
}
277+
278+
*thp_fs_loc = mkdtemp(thp_fs_template);
279+
280+
if (!*thp_fs_loc)
281+
ksft_exit_fail_msg("cannot create temp folder\n");
282+
283+
return true;
284+
}
285+
286+
void cleanup_thp_fs(const char *thp_fs_loc, bool created_tmp)
287+
{
288+
int status;
289+
290+
if (!created_tmp)
291+
return;
292+
293+
status = rmdir(thp_fs_loc);
294+
if (status)
295+
ksft_exit_fail_msg("cannot remove tmp dir: %s\n",
296+
strerror(errno));
297+
}
298+
299+
int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
300+
char **addr)
301+
{
302+
size_t i;
303+
int dummy;
304+
305+
srand(time(NULL));
306+
307+
*fd = open(testfile, O_CREAT | O_RDWR, 0664);
308+
if (*fd == -1)
309+
ksft_exit_fail_msg("Failed to create a file at %s\n", testfile);
310+
311+
for (i = 0; i < fd_size; i++) {
312+
unsigned char byte = (unsigned char)i;
313+
314+
write(*fd, &byte, sizeof(byte));
315+
}
316+
close(*fd);
317+
sync();
318+
*fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
319+
if (*fd == -1) {
320+
ksft_perror("open drop_caches");
321+
goto err_out_unlink;
322+
}
323+
if (write(*fd, "3", 1) != 1) {
324+
ksft_perror("write to drop_caches");
325+
goto err_out_unlink;
326+
}
327+
close(*fd);
328+
329+
*fd = open(testfile, O_RDWR);
330+
if (*fd == -1) {
331+
ksft_perror("Failed to open testfile\n");
332+
goto err_out_unlink;
333+
}
334+
335+
*addr = mmap(NULL, fd_size, PROT_READ|PROT_WRITE, MAP_SHARED, *fd, 0);
336+
if (*addr == (char *)-1) {
337+
ksft_perror("cannot mmap");
338+
goto err_out_close;
339+
}
340+
madvise(*addr, fd_size, MADV_HUGEPAGE);
341+
342+
for (size_t i = 0; i < fd_size; i++)
343+
dummy += *(*addr + i);
344+
345+
if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) {
346+
ksft_print_msg("No large pagecache folio generated, please provide a filesystem supporting large folio\n");
347+
munmap(*addr, fd_size);
348+
close(*fd);
349+
unlink(testfile);
350+
ksft_test_result_skip("Pagecache folio split skipped\n");
351+
return -2;
352+
}
353+
return 0;
354+
err_out_close:
355+
close(*fd);
356+
err_out_unlink:
357+
unlink(testfile);
358+
ksft_exit_fail_msg("Failed to create large pagecache folios\n");
359+
return -1;
360+
}
361+
362+
void split_thp_in_pagecache_to_order(size_t fd_size, int order, const char *fs_loc)
363+
{
364+
int fd;
365+
char *addr;
366+
size_t i;
367+
char testfile[INPUT_MAX];
368+
int err = 0;
369+
370+
err = snprintf(testfile, INPUT_MAX, "%s/test", fs_loc);
371+
372+
if (err < 0)
373+
ksft_exit_fail_msg("cannot generate right test file name\n");
374+
375+
err = create_pagecache_thp_and_fd(testfile, fd_size, &fd, &addr);
376+
if (err)
377+
return;
378+
err = 0;
379+
380+
write_debugfs(PID_FMT, getpid(), (uint64_t)addr, (uint64_t)addr + fd_size, order);
381+
382+
for (i = 0; i < fd_size; i++)
383+
if (*(addr + i) != (char)i) {
384+
ksft_print_msg("%lu byte corrupted in the file\n", i);
385+
err = EXIT_FAILURE;
386+
goto out;
387+
}
388+
389+
if (!check_huge_file(addr, 0, pmd_pagesize)) {
390+
ksft_print_msg("Still FilePmdMapped not split\n");
391+
err = EXIT_FAILURE;
392+
goto out;
393+
}
394+
395+
out:
396+
munmap(addr, fd_size);
397+
close(fd);
398+
unlink(testfile);
399+
if (err)
400+
ksft_exit_fail_msg("Split PMD-mapped pagecache folio to order %d failed\n", order);
401+
ksft_test_result_pass("Split PMD-mapped pagecache folio to order %d passed\n", order);
402+
}
403+
268404
int main(int argc, char **argv)
269405
{
406+
int i;
407+
size_t fd_size;
408+
char *optional_xfs_path = NULL;
409+
char fs_loc_template[] = "/tmp/thp_fs_XXXXXX";
410+
const char *fs_loc;
411+
bool created_tmp;
412+
270413
ksft_print_header();
271414

272415
if (geteuid() != 0) {
273416
ksft_print_msg("Please run the benchmark as root\n");
274417
ksft_finished();
275418
}
276419

277-
ksft_set_plan(3);
420+
if (argc > 1)
421+
optional_xfs_path = argv[1];
422+
423+
ksft_set_plan(3+9);
278424

279425
pagesize = getpagesize();
280426
pageshift = ffs(pagesize) - 1;
281427
pmd_pagesize = read_pmd_pagesize();
282428
if (!pmd_pagesize)
283429
ksft_exit_fail_msg("Reading PMD pagesize failed\n");
284430

431+
fd_size = 2 * pmd_pagesize;
432+
285433
split_pmd_thp();
286434
split_pte_mapped_thp();
287435
split_file_backed_thp();
288436

437+
created_tmp = prepare_thp_fs(optional_xfs_path, fs_loc_template,
438+
&fs_loc);
439+
for (i = 8; i >= 0; i--)
440+
split_thp_in_pagecache_to_order(fd_size, i, fs_loc);
441+
cleanup_thp_fs(fs_loc, created_tmp);
442+
289443
ksft_finished();
444+
445+
return 0;
290446
}

0 commit comments

Comments
 (0)