Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/vm: add thp collapse shmem testing

Add memory operations for shmem (memfd) memory, and reuse existing tests
with the new memory operations.

Shmem tests can be called with "shmem" mem_type, and shmem tests are ran
with "all" mem_type as well.

Link: https://lkml.kernel.org/r/20220907144521.3115321-9-zokeefe@google.com
Link: https://lkml.kernel.org/r/20220922224046.1143204-9-zokeefe@google.com
Signed-off-by: Zach O'Keefe <zokeefe@google.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Chris Kennelly <ckennelly@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: James Houghton <jthoughton@google.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
Cc: SeongJae Park <sj@kernel.org>
Cc: Song Liu <songliubraving@fb.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Yang Shi <shy828301@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Zach O'Keefe and committed by
Andrew Morton
d0d35b60 1b03d0d5

+55 -2
+55 -2
tools/testing/selftests/vm/khugepaged.c
··· 59 59 60 60 static struct mem_ops *file_ops; 61 61 static struct mem_ops *anon_ops; 62 + static struct mem_ops *shmem_ops; 62 63 63 64 struct collapse_context { 64 65 void (*collapse)(const char *msg, char *p, int nr_hpages, ··· 740 739 } 741 740 } 742 741 742 + static void *shmem_setup_area(int nr_hpages) 743 + { 744 + void *p; 745 + unsigned long size = nr_hpages * hpage_pmd_size; 746 + 747 + finfo.fd = memfd_create("khugepaged-selftest-collapse-shmem", 0); 748 + if (finfo.fd < 0) { 749 + perror("memfd_create()"); 750 + exit(EXIT_FAILURE); 751 + } 752 + if (ftruncate(finfo.fd, size)) { 753 + perror("ftruncate()"); 754 + exit(EXIT_FAILURE); 755 + } 756 + p = mmap(BASE_ADDR, size, PROT_READ | PROT_WRITE, MAP_SHARED, finfo.fd, 757 + 0); 758 + if (p != BASE_ADDR) { 759 + perror("mmap()"); 760 + exit(EXIT_FAILURE); 761 + } 762 + return p; 763 + } 764 + 765 + static void shmem_cleanup_area(void *p, unsigned long size) 766 + { 767 + munmap(p, size); 768 + close(finfo.fd); 769 + } 770 + 771 + static bool shmem_check_huge(void *addr, int nr_hpages) 772 + { 773 + return check_huge_shmem(addr, nr_hpages, hpage_pmd_size); 774 + } 775 + 743 776 static struct mem_ops __anon_ops = { 744 777 .setup_area = &anon_setup_area, 745 778 .cleanup_area = &anon_cleanup_area, ··· 788 753 .fault = &file_fault, 789 754 .check_huge = &file_check_huge, 790 755 .name = "file", 756 + }; 757 + 758 + static struct mem_ops __shmem_ops = { 759 + .setup_area = &shmem_setup_area, 760 + .cleanup_area = &shmem_cleanup_area, 761 + .fault = &anon_fault, 762 + .check_huge = &shmem_check_huge, 763 + .name = "shmem", 791 764 }; 792 765 793 766 static void __madvise_collapse(const char *msg, char *p, int nr_hpages, ··· 1358 1315 fprintf(stderr, "\nUsage: ./khugepaged <test type> [dir]\n\n"); 1359 1316 fprintf(stderr, "\t<test type>\t: <context>:<mem_type>\n"); 1360 1317 fprintf(stderr, "\t<context>\t: [all|khugepaged|madvise]\n"); 1361 - fprintf(stderr, "\t<mem_type>\t: [all|anon|file]\n"); 1318 + fprintf(stderr, "\t<mem_type>\t: [all|anon|file|shmem]\n"); 1362 1319 fprintf(stderr, "\n\t\"file,all\" mem_type requires [dir] argument\n"); 1363 1320 fprintf(stderr, "\n\t\"file,all\" mem_type requires kernel built with\n"); 1364 1321 fprintf(stderr, "\tCONFIG_READ_ONLY_THP_FOR_FS=y\n"); ··· 1400 1357 if (!strcmp(buf, "all")) { 1401 1358 file_ops = &__file_ops; 1402 1359 anon_ops = &__anon_ops; 1360 + shmem_ops = &__shmem_ops; 1403 1361 } else if (!strcmp(buf, "anon")) { 1404 1362 anon_ops = &__anon_ops; 1405 1363 } else if (!strcmp(buf, "file")) { 1406 1364 file_ops = &__file_ops; 1365 + } else if (!strcmp(buf, "shmem")) { 1366 + shmem_ops = &__shmem_ops; 1407 1367 } else { 1408 1368 usage(); 1409 1369 } ··· 1423 1377 struct settings default_settings = { 1424 1378 .thp_enabled = THP_MADVISE, 1425 1379 .thp_defrag = THP_DEFRAG_ALWAYS, 1426 - .shmem_enabled = SHMEM_NEVER, 1380 + .shmem_enabled = SHMEM_ADVISE, 1427 1381 .use_zero_page = 0, 1428 1382 .khugepaged = { 1429 1383 .defrag = 1, ··· 1470 1424 1471 1425 TEST(collapse_full, khugepaged_context, anon_ops); 1472 1426 TEST(collapse_full, khugepaged_context, file_ops); 1427 + TEST(collapse_full, khugepaged_context, shmem_ops); 1473 1428 TEST(collapse_full, madvise_context, anon_ops); 1474 1429 TEST(collapse_full, madvise_context, file_ops); 1430 + TEST(collapse_full, madvise_context, shmem_ops); 1475 1431 1476 1432 TEST(collapse_empty, khugepaged_context, anon_ops); 1477 1433 TEST(collapse_empty, madvise_context, anon_ops); 1478 1434 1479 1435 TEST(collapse_single_pte_entry, khugepaged_context, anon_ops); 1480 1436 TEST(collapse_single_pte_entry, khugepaged_context, file_ops); 1437 + TEST(collapse_single_pte_entry, khugepaged_context, shmem_ops); 1481 1438 TEST(collapse_single_pte_entry, madvise_context, anon_ops); 1482 1439 TEST(collapse_single_pte_entry, madvise_context, file_ops); 1440 + TEST(collapse_single_pte_entry, madvise_context, shmem_ops); 1483 1441 1484 1442 TEST(collapse_max_ptes_none, khugepaged_context, anon_ops); 1485 1443 TEST(collapse_max_ptes_none, khugepaged_context, file_ops); ··· 1497 1447 1498 1448 TEST(collapse_full_of_compound, khugepaged_context, anon_ops); 1499 1449 TEST(collapse_full_of_compound, khugepaged_context, file_ops); 1450 + TEST(collapse_full_of_compound, khugepaged_context, shmem_ops); 1500 1451 TEST(collapse_full_of_compound, madvise_context, anon_ops); 1501 1452 TEST(collapse_full_of_compound, madvise_context, file_ops); 1453 + TEST(collapse_full_of_compound, madvise_context, shmem_ops); 1502 1454 1503 1455 TEST(collapse_compound_extreme, khugepaged_context, anon_ops); 1504 1456 TEST(collapse_compound_extreme, madvise_context, anon_ops); ··· 1522 1470 1523 1471 TEST(madvise_collapse_existing_thps, madvise_context, anon_ops); 1524 1472 TEST(madvise_collapse_existing_thps, madvise_context, file_ops); 1473 + TEST(madvise_collapse_existing_thps, madvise_context, shmem_ops); 1525 1474 1526 1475 restore_settings(0); 1527 1476 }