mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2025-08-16 14:45:36 +00:00

- The 4 patch series "mm: ksm: prevent KSM from breaking merging of new VMAs" from Lorenzo Stoakes addresses an issue with KSM's PR_SET_MEMORY_MERGE mode: newly mapped VMAs were not eligible for merging with existing adjacent VMAs. - The 4 patch series "mm/damon: introduce DAMON_STAT for simple and practical access monitoring" from SeongJae Park adds a new kernel module which simplifies the setup and usage of DAMON in production environments. - The 6 patch series "stop passing a writeback_control to swap/shmem writeout" from Christoph Hellwig is a cleanup to the writeback code which removes a couple of pointers from struct writeback_control. - The 7 patch series "drivers/base/node.c: optimization and cleanups" from Donet Tom contains largely uncorrelated cleanups to the NUMA node setup and management code. - The 4 patch series "mm: userfaultfd: assorted fixes and cleanups" from Tal Zussman does some maintenance work on the userfaultfd code. - The 5 patch series "Readahead tweaks for larger folios" from Ryan Roberts implements some tuneups for pagecache readahead when it is reading into order>0 folios. - The 4 patch series "selftests/mm: Tweaks to the cow test" from Mark Brown provides some cleanups and consistency improvements to the selftests code. - The 4 patch series "Optimize mremap() for large folios" from Dev Jain does that. A 37% reduction in execution time was measured in a memset+mremap+munmap microbenchmark. - The 5 patch series "Remove zero_user()" from Matthew Wilcox expunges zero_user() in favor of the more modern memzero_page(). - The 3 patch series "mm/huge_memory: vmf_insert_folio_*() and vmf_insert_pfn_pud() fixes" from David Hildenbrand addresses some warts which David noticed in the huge page code. These were not known to be causing any issues at this time. - The 3 patch series "mm/damon: use alloc_migrate_target() for DAMOS_MIGRATE_{HOT,COLD" from SeongJae Park provides some cleanup and consolidation work in DAMON. - The 3 patch series "use vm_flags_t consistently" from Lorenzo Stoakes uses vm_flags_t in places where we were inappropriately using other types. - The 3 patch series "mm/memfd: Reserve hugetlb folios before allocation" from Vivek Kasireddy increases the reliability of large page allocation in the memfd code. - The 14 patch series "mm: Remove pXX_devmap page table bit and pfn_t type" from Alistair Popple removes several now-unneeded PFN_* flags. - The 5 patch series "mm/damon: decouple sysfs from core" from SeongJae Park implememnts some cleanup and maintainability work in the DAMON sysfs layer. - The 5 patch series "madvise cleanup" from Lorenzo Stoakes does quite a lot of cleanup/maintenance work in the madvise() code. - The 4 patch series "madvise anon_name cleanups" from Vlastimil Babka provides additional cleanups on top or Lorenzo's effort. - The 11 patch series "Implement numa node notifier" from Oscar Salvador creates a standalone notifier for NUMA node memory state changes. Previously these were lumped under the more general memory on/offline notifier. - The 6 patch series "Make MIGRATE_ISOLATE a standalone bit" from Zi Yan cleans up the pageblock isolation code and fixes a potential issue which doesn't seem to cause any problems in practice. - The 5 patch series "selftests/damon: add python and drgn based DAMON sysfs functionality tests" from SeongJae Park adds additional drgn- and python-based DAMON selftests which are more comprehensive than the existing selftest suite. - The 5 patch series "Misc rework on hugetlb faulting path" from Oscar Salvador fixes a rather obscure deadlock in the hugetlb fault code and follows that fix with a series of cleanups. - The 3 patch series "cma: factor out allocation logic from __cma_declare_contiguous_nid" from Mike Rapoport rationalizes and cleans up the highmem-specific code in the CMA allocator. - The 28 patch series "mm/migration: rework movable_ops page migration (part 1)" from David Hildenbrand provides cleanups and future-preparedness to the migration code. - The 2 patch series "mm/damon: add trace events for auto-tuned monitoring intervals and DAMOS quota" from SeongJae Park adds some tracepoints to some DAMON auto-tuning code. - The 6 patch series "mm/damon: fix misc bugs in DAMON modules" from SeongJae Park does that. - The 6 patch series "mm/damon: misc cleanups" from SeongJae Park also does what it claims. - The 4 patch series "mm: folio_pte_batch() improvements" from David Hildenbrand cleans up the large folio PTE batching code. - The 13 patch series "mm/damon/vaddr: Allow interleaving in migrate_{hot,cold} actions" from SeongJae Park facilitates dynamic alteration of DAMON's inter-node allocation policy. - The 3 patch series "Remove unmap_and_put_page()" from Vishal Moola provides a couple of page->folio conversions. - The 4 patch series "mm: per-node proactive reclaim" from Davidlohr Bueso implements a per-node control of proactive reclaim - beyond the current memcg-based implementation. - The 14 patch series "mm/damon: remove damon_callback" from SeongJae Park replaces the damon_callback interface with a more general and powerful damon_call()+damos_walk() interface. - The 10 patch series "mm/mremap: permit mremap() move of multiple VMAs" from Lorenzo Stoakes implements a number of mremap cleanups (of course) in preparation for adding new mremap() functionality: newly permit the remapping of multiple VMAs when the user is specifying MREMAP_FIXED. It still excludes some specialized situations where this cannot be performed reliably. - The 3 patch series "drop hugetlb_free_pgd_range()" from Anthony Yznaga switches some sparc hugetlb code over to the generic version and removes the thus-unneeded hugetlb_free_pgd_range(). - The 4 patch series "mm/damon/sysfs: support periodic and automated stats update" from SeongJae Park augments the present userspace-requested update of DAMON sysfs monitoring files. Automatic update is now provided, along with a tunable to control the update interval. - The 4 patch series "Some randome fixes and cleanups to swapfile" from Kemeng Shi does what is claims. - The 4 patch series "mm: introduce snapshot_page" from Luiz Capitulino and David Hildenbrand provides (and uses) a means by which debug-style functions can grab a copy of a pageframe and inspect it locklessly without tripping over the races inherent in operating on the live pageframe directly. - The 6 patch series "use per-vma locks for /proc/pid/maps reads" from Suren Baghdasaryan addresses the large contention issues which can be triggered by reads from that procfs file. Latencies are reduced by more than half in some situations. The series also introduces several new selftests for the /proc/pid/maps interface. - The 6 patch series "__folio_split() clean up" from Zi Yan cleans up __folio_split()! - The 7 patch series "Optimize mprotect() for large folios" from Dev Jain provides some quite large (>3x) speedups to mprotect() when dealing with large folios. - The 2 patch series "selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));" and some cleanup" from wang lian does some cleanup work in the selftests code. - The 3 patch series "tools/testing: expand mremap testing" from Lorenzo Stoakes extends the mremap() selftest in several ways, including adding more checking of Lorenzo's recently added "permit mremap() move of multiple VMAs" feature. - The 22 patch series "selftests/damon/sysfs.py: test all parameters" from SeongJae Park extends the DAMON sysfs interface selftest so that it tests all possible user-requested parameters. Rather than the present minimal subset. -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCaIqcCgAKCRDdBJ7gKXxA jkVBAQCCn9DR1QP0CRk961ot0cKzOgioSc0aA03DPb2KXRt2kQEAzDAz0ARurFhL 8BzbvI0c+4tntHLXvIlrC33n9KWAOQM= =XsFy -----END PGP SIGNATURE----- Merge tag 'mm-stable-2025-07-30-15-25' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Pull MM updates from Andrew Morton: "As usual, many cleanups. The below blurbiage describes 42 patchsets. 21 of those are partially or fully cleanup work. "cleans up", "cleanup", "maintainability", "rationalizes", etc. I never knew the MM code was so dirty. "mm: ksm: prevent KSM from breaking merging of new VMAs" (Lorenzo Stoakes) addresses an issue with KSM's PR_SET_MEMORY_MERGE mode: newly mapped VMAs were not eligible for merging with existing adjacent VMAs. "mm/damon: introduce DAMON_STAT for simple and practical access monitoring" (SeongJae Park) adds a new kernel module which simplifies the setup and usage of DAMON in production environments. "stop passing a writeback_control to swap/shmem writeout" (Christoph Hellwig) is a cleanup to the writeback code which removes a couple of pointers from struct writeback_control. "drivers/base/node.c: optimization and cleanups" (Donet Tom) contains largely uncorrelated cleanups to the NUMA node setup and management code. "mm: userfaultfd: assorted fixes and cleanups" (Tal Zussman) does some maintenance work on the userfaultfd code. "Readahead tweaks for larger folios" (Ryan Roberts) implements some tuneups for pagecache readahead when it is reading into order>0 folios. "selftests/mm: Tweaks to the cow test" (Mark Brown) provides some cleanups and consistency improvements to the selftests code. "Optimize mremap() for large folios" (Dev Jain) does that. A 37% reduction in execution time was measured in a memset+mremap+munmap microbenchmark. "Remove zero_user()" (Matthew Wilcox) expunges zero_user() in favor of the more modern memzero_page(). "mm/huge_memory: vmf_insert_folio_*() and vmf_insert_pfn_pud() fixes" (David Hildenbrand) addresses some warts which David noticed in the huge page code. These were not known to be causing any issues at this time. "mm/damon: use alloc_migrate_target() for DAMOS_MIGRATE_{HOT,COLD" (SeongJae Park) provides some cleanup and consolidation work in DAMON. "use vm_flags_t consistently" (Lorenzo Stoakes) uses vm_flags_t in places where we were inappropriately using other types. "mm/memfd: Reserve hugetlb folios before allocation" (Vivek Kasireddy) increases the reliability of large page allocation in the memfd code. "mm: Remove pXX_devmap page table bit and pfn_t type" (Alistair Popple) removes several now-unneeded PFN_* flags. "mm/damon: decouple sysfs from core" (SeongJae Park) implememnts some cleanup and maintainability work in the DAMON sysfs layer. "madvise cleanup" (Lorenzo Stoakes) does quite a lot of cleanup/maintenance work in the madvise() code. "madvise anon_name cleanups" (Vlastimil Babka) provides additional cleanups on top or Lorenzo's effort. "Implement numa node notifier" (Oscar Salvador) creates a standalone notifier for NUMA node memory state changes. Previously these were lumped under the more general memory on/offline notifier. "Make MIGRATE_ISOLATE a standalone bit" (Zi Yan) cleans up the pageblock isolation code and fixes a potential issue which doesn't seem to cause any problems in practice. "selftests/damon: add python and drgn based DAMON sysfs functionality tests" (SeongJae Park) adds additional drgn- and python-based DAMON selftests which are more comprehensive than the existing selftest suite. "Misc rework on hugetlb faulting path" (Oscar Salvador) fixes a rather obscure deadlock in the hugetlb fault code and follows that fix with a series of cleanups. "cma: factor out allocation logic from __cma_declare_contiguous_nid" (Mike Rapoport) rationalizes and cleans up the highmem-specific code in the CMA allocator. "mm/migration: rework movable_ops page migration (part 1)" (David Hildenbrand) provides cleanups and future-preparedness to the migration code. "mm/damon: add trace events for auto-tuned monitoring intervals and DAMOS quota" (SeongJae Park) adds some tracepoints to some DAMON auto-tuning code. "mm/damon: fix misc bugs in DAMON modules" (SeongJae Park) does that. "mm/damon: misc cleanups" (SeongJae Park) also does what it claims. "mm: folio_pte_batch() improvements" (David Hildenbrand) cleans up the large folio PTE batching code. "mm/damon/vaddr: Allow interleaving in migrate_{hot,cold} actions" (SeongJae Park) facilitates dynamic alteration of DAMON's inter-node allocation policy. "Remove unmap_and_put_page()" (Vishal Moola) provides a couple of page->folio conversions. "mm: per-node proactive reclaim" (Davidlohr Bueso) implements a per-node control of proactive reclaim - beyond the current memcg-based implementation. "mm/damon: remove damon_callback" (SeongJae Park) replaces the damon_callback interface with a more general and powerful damon_call()+damos_walk() interface. "mm/mremap: permit mremap() move of multiple VMAs" (Lorenzo Stoakes) implements a number of mremap cleanups (of course) in preparation for adding new mremap() functionality: newly permit the remapping of multiple VMAs when the user is specifying MREMAP_FIXED. It still excludes some specialized situations where this cannot be performed reliably. "drop hugetlb_free_pgd_range()" (Anthony Yznaga) switches some sparc hugetlb code over to the generic version and removes the thus-unneeded hugetlb_free_pgd_range(). "mm/damon/sysfs: support periodic and automated stats update" (SeongJae Park) augments the present userspace-requested update of DAMON sysfs monitoring files. Automatic update is now provided, along with a tunable to control the update interval. "Some randome fixes and cleanups to swapfile" (Kemeng Shi) does what is claims. "mm: introduce snapshot_page" (Luiz Capitulino and David Hildenbrand) provides (and uses) a means by which debug-style functions can grab a copy of a pageframe and inspect it locklessly without tripping over the races inherent in operating on the live pageframe directly. "use per-vma locks for /proc/pid/maps reads" (Suren Baghdasaryan) addresses the large contention issues which can be triggered by reads from that procfs file. Latencies are reduced by more than half in some situations. The series also introduces several new selftests for the /proc/pid/maps interface. "__folio_split() clean up" (Zi Yan) cleans up __folio_split()! "Optimize mprotect() for large folios" (Dev Jain) provides some quite large (>3x) speedups to mprotect() when dealing with large folios. "selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));" and some cleanup" (wang lian) does some cleanup work in the selftests code. "tools/testing: expand mremap testing" (Lorenzo Stoakes) extends the mremap() selftest in several ways, including adding more checking of Lorenzo's recently added "permit mremap() move of multiple VMAs" feature. "selftests/damon/sysfs.py: test all parameters" (SeongJae Park) extends the DAMON sysfs interface selftest so that it tests all possible user-requested parameters. Rather than the present minimal subset" * tag 'mm-stable-2025-07-30-15-25' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (370 commits) MAINTAINERS: add missing headers to mempory policy & migration section MAINTAINERS: add missing file to cgroup section MAINTAINERS: add MM MISC section, add missing files to MISC and CORE MAINTAINERS: add missing zsmalloc file MAINTAINERS: add missing files to page alloc section MAINTAINERS: add missing shrinker files MAINTAINERS: move memremap.[ch] to hotplug section MAINTAINERS: add missing mm_slot.h file THP section MAINTAINERS: add missing interval_tree.c to memory mapping section MAINTAINERS: add missing percpu-internal.h file to per-cpu section mm/page_alloc: remove trace_mm_alloc_contig_migrate_range_info() selftests/damon: introduce _common.sh to host shared function selftests/damon/sysfs.py: test runtime reduction of DAMON parameters selftests/damon/sysfs.py: test non-default parameters runtime commit selftests/damon/sysfs.py: generalize DAMON context commit assertion selftests/damon/sysfs.py: generalize monitoring attributes commit assertion selftests/damon/sysfs.py: generalize DAMOS schemes commit assertion selftests/damon/sysfs.py: test DAMOS filters commitment selftests/damon/sysfs.py: generalize DAMOS scheme commit assertion selftests/damon/sysfs.py: test DAMOS destinations commitment ...
572 lines
14 KiB
C
572 lines
14 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* A test of splitting PMD THPs and PTE-mapped THPs from a specified virtual
|
|
* address range in a process via <debugfs>/split_huge_pages interface.
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <unistd.h>
|
|
#include <inttypes.h>
|
|
#include <string.h>
|
|
#include <fcntl.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/mount.h>
|
|
#include <sys/param.h>
|
|
#include <malloc.h>
|
|
#include <stdbool.h>
|
|
#include <time.h>
|
|
#include "vm_util.h"
|
|
#include "../kselftest.h"
|
|
|
|
uint64_t pagesize;
|
|
unsigned int pageshift;
|
|
uint64_t pmd_pagesize;
|
|
|
|
#define SPLIT_DEBUGFS "/sys/kernel/debug/split_huge_pages"
|
|
#define SMAP_PATH "/proc/self/smaps"
|
|
#define INPUT_MAX 80
|
|
|
|
#define PID_FMT "%d,0x%lx,0x%lx,%d"
|
|
#define PID_FMT_OFFSET "%d,0x%lx,0x%lx,%d,%d"
|
|
#define PATH_FMT "%s,0x%lx,0x%lx,%d"
|
|
|
|
#define PFN_MASK ((1UL<<55)-1)
|
|
#define KPF_THP (1UL<<22)
|
|
|
|
int is_backed_by_thp(char *vaddr, int pagemap_file, int kpageflags_file)
|
|
{
|
|
uint64_t paddr;
|
|
uint64_t page_flags;
|
|
|
|
if (pagemap_file) {
|
|
pread(pagemap_file, &paddr, sizeof(paddr),
|
|
((long)vaddr >> pageshift) * sizeof(paddr));
|
|
|
|
if (kpageflags_file) {
|
|
pread(kpageflags_file, &page_flags, sizeof(page_flags),
|
|
(paddr & PFN_MASK) * sizeof(page_flags));
|
|
|
|
return !!(page_flags & KPF_THP);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void write_file(const char *path, const char *buf, size_t buflen)
|
|
{
|
|
int fd;
|
|
ssize_t numwritten;
|
|
|
|
fd = open(path, O_WRONLY);
|
|
if (fd == -1)
|
|
ksft_exit_fail_msg("%s open failed: %s\n", path, strerror(errno));
|
|
|
|
numwritten = write(fd, buf, buflen - 1);
|
|
close(fd);
|
|
if (numwritten < 1)
|
|
ksft_exit_fail_msg("Write failed\n");
|
|
}
|
|
|
|
static void write_debugfs(const char *fmt, ...)
|
|
{
|
|
char input[INPUT_MAX];
|
|
int ret;
|
|
va_list argp;
|
|
|
|
va_start(argp, fmt);
|
|
ret = vsnprintf(input, INPUT_MAX, fmt, argp);
|
|
va_end(argp);
|
|
|
|
if (ret >= INPUT_MAX)
|
|
ksft_exit_fail_msg("%s: Debugfs input is too long\n", __func__);
|
|
|
|
write_file(SPLIT_DEBUGFS, input, ret + 1);
|
|
}
|
|
|
|
static char *allocate_zero_filled_hugepage(size_t len)
|
|
{
|
|
char *result;
|
|
size_t i;
|
|
|
|
result = memalign(pmd_pagesize, len);
|
|
if (!result) {
|
|
printf("Fail to allocate memory\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
madvise(result, len, MADV_HUGEPAGE);
|
|
|
|
for (i = 0; i < len; i++)
|
|
result[i] = (char)0;
|
|
|
|
return result;
|
|
}
|
|
|
|
static void verify_rss_anon_split_huge_page_all_zeroes(char *one_page, int nr_hpages, size_t len)
|
|
{
|
|
unsigned long rss_anon_before, rss_anon_after;
|
|
size_t i;
|
|
|
|
if (!check_huge_anon(one_page, 4, pmd_pagesize))
|
|
ksft_exit_fail_msg("No THP is allocated\n");
|
|
|
|
rss_anon_before = rss_anon();
|
|
if (!rss_anon_before)
|
|
ksft_exit_fail_msg("No RssAnon is allocated before split\n");
|
|
|
|
/* split all THPs */
|
|
write_debugfs(PID_FMT, getpid(), (uint64_t)one_page,
|
|
(uint64_t)one_page + len, 0);
|
|
|
|
for (i = 0; i < len; i++)
|
|
if (one_page[i] != (char)0)
|
|
ksft_exit_fail_msg("%ld byte corrupted\n", i);
|
|
|
|
if (!check_huge_anon(one_page, 0, pmd_pagesize))
|
|
ksft_exit_fail_msg("Still AnonHugePages not split\n");
|
|
|
|
rss_anon_after = rss_anon();
|
|
if (rss_anon_after >= rss_anon_before)
|
|
ksft_exit_fail_msg("Incorrect RssAnon value. Before: %ld After: %ld\n",
|
|
rss_anon_before, rss_anon_after);
|
|
}
|
|
|
|
void split_pmd_zero_pages(void)
|
|
{
|
|
char *one_page;
|
|
int nr_hpages = 4;
|
|
size_t len = nr_hpages * pmd_pagesize;
|
|
|
|
one_page = allocate_zero_filled_hugepage(len);
|
|
verify_rss_anon_split_huge_page_all_zeroes(one_page, nr_hpages, len);
|
|
ksft_test_result_pass("Split zero filled huge pages successful\n");
|
|
free(one_page);
|
|
}
|
|
|
|
void split_pmd_thp_to_order(int order)
|
|
{
|
|
char *one_page;
|
|
size_t len = 4 * pmd_pagesize;
|
|
size_t i;
|
|
|
|
one_page = memalign(pmd_pagesize, len);
|
|
if (!one_page)
|
|
ksft_exit_fail_msg("Fail to allocate memory: %s\n", strerror(errno));
|
|
|
|
madvise(one_page, len, MADV_HUGEPAGE);
|
|
|
|
for (i = 0; i < len; i++)
|
|
one_page[i] = (char)i;
|
|
|
|
if (!check_huge_anon(one_page, 4, pmd_pagesize))
|
|
ksft_exit_fail_msg("No THP is allocated\n");
|
|
|
|
/* split all THPs */
|
|
write_debugfs(PID_FMT, getpid(), (uint64_t)one_page,
|
|
(uint64_t)one_page + len, order);
|
|
|
|
for (i = 0; i < len; i++)
|
|
if (one_page[i] != (char)i)
|
|
ksft_exit_fail_msg("%ld byte corrupted\n", i);
|
|
|
|
|
|
if (!check_huge_anon(one_page, 0, pmd_pagesize))
|
|
ksft_exit_fail_msg("Still AnonHugePages not split\n");
|
|
|
|
ksft_test_result_pass("Split huge pages to order %d successful\n", order);
|
|
free(one_page);
|
|
}
|
|
|
|
void split_pte_mapped_thp(void)
|
|
{
|
|
char *one_page, *pte_mapped, *pte_mapped2;
|
|
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);
|
|
if (one_page == MAP_FAILED)
|
|
ksft_exit_fail_msg("Fail to allocate memory: %s\n", strerror(errno));
|
|
|
|
madvise(one_page, len, MADV_HUGEPAGE);
|
|
|
|
for (i = 0; i < len; i++)
|
|
one_page[i] = (char)i;
|
|
|
|
if (!check_huge_anon(one_page, 4, pmd_pagesize))
|
|
ksft_exit_fail_msg("No THP is allocated\n");
|
|
|
|
/* remap the first pagesize of first THP */
|
|
pte_mapped = mremap(one_page, pagesize, pagesize, MREMAP_MAYMOVE);
|
|
|
|
/* remap the Nth pagesize of Nth THP */
|
|
for (i = 1; i < 4; i++) {
|
|
pte_mapped2 = mremap(one_page + pmd_pagesize * i + pagesize * i,
|
|
pagesize, pagesize,
|
|
MREMAP_MAYMOVE|MREMAP_FIXED,
|
|
pte_mapped + pagesize * i);
|
|
if (pte_mapped2 == MAP_FAILED)
|
|
ksft_exit_fail_msg("mremap failed: %s\n", strerror(errno));
|
|
}
|
|
|
|
/* smap does not show THPs after mremap, use kpageflags instead */
|
|
thp_size = 0;
|
|
for (i = 0; i < pagesize * 4; i++)
|
|
if (i % pagesize == 0 &&
|
|
is_backed_by_thp(&pte_mapped[i], pagemap_fd, kpageflags_fd))
|
|
thp_size++;
|
|
|
|
if (thp_size != 4)
|
|
ksft_exit_fail_msg("Some THPs are missing during mremap\n");
|
|
|
|
/* split all remapped THPs */
|
|
write_debugfs(PID_FMT, getpid(), (uint64_t)pte_mapped,
|
|
(uint64_t)pte_mapped + pagesize * 4, 0);
|
|
|
|
/* smap does not show THPs after mremap, use kpageflags instead */
|
|
thp_size = 0;
|
|
for (i = 0; i < pagesize * 4; i++) {
|
|
if (pte_mapped[i] != (char)i)
|
|
ksft_exit_fail_msg("%ld byte corrupted\n", i);
|
|
|
|
if (i % pagesize == 0 &&
|
|
is_backed_by_thp(&pte_mapped[i], pagemap_fd, kpageflags_fd))
|
|
thp_size++;
|
|
}
|
|
|
|
if (thp_size)
|
|
ksft_exit_fail_msg("Still %ld THPs not split\n", thp_size);
|
|
|
|
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)
|
|
{
|
|
int status;
|
|
int fd;
|
|
char tmpfs_template[] = "/tmp/thp_split_XXXXXX";
|
|
const char *tmpfs_loc = mkdtemp(tmpfs_template);
|
|
char testfile[INPUT_MAX];
|
|
ssize_t num_written, num_read;
|
|
char *file_buf1, *file_buf2;
|
|
uint64_t pgoff_start = 0, pgoff_end = 1024;
|
|
int i;
|
|
|
|
ksft_print_msg("Please enable pr_debug in split_huge_pages_in_file() for more info.\n");
|
|
|
|
file_buf1 = (char *)malloc(pmd_pagesize);
|
|
file_buf2 = (char *)malloc(pmd_pagesize);
|
|
|
|
if (!file_buf1 || !file_buf2) {
|
|
ksft_print_msg("cannot allocate file buffers\n");
|
|
goto out;
|
|
}
|
|
|
|
for (i = 0; i < pmd_pagesize; i++)
|
|
file_buf1[i] = (char)i;
|
|
memset(file_buf2, 0, pmd_pagesize);
|
|
|
|
status = mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=always,size=4m");
|
|
|
|
if (status)
|
|
ksft_exit_fail_msg("Unable to create a tmpfs for testing\n");
|
|
|
|
status = snprintf(testfile, INPUT_MAX, "%s/thp_file", tmpfs_loc);
|
|
if (status >= INPUT_MAX) {
|
|
ksft_exit_fail_msg("Fail to create file-backed THP split testing file\n");
|
|
goto cleanup;
|
|
}
|
|
|
|
fd = open(testfile, O_CREAT|O_RDWR, 0664);
|
|
if (fd == -1) {
|
|
ksft_perror("Cannot open testing file");
|
|
goto cleanup;
|
|
}
|
|
|
|
/* write pmd size data to the file, so a file-backed THP can be allocated */
|
|
num_written = write(fd, file_buf1, pmd_pagesize);
|
|
|
|
if (num_written == -1 || num_written != pmd_pagesize) {
|
|
ksft_perror("Failed to write data to testing file");
|
|
goto close_file;
|
|
}
|
|
|
|
/* split the file-backed THP */
|
|
write_debugfs(PATH_FMT, testfile, pgoff_start, pgoff_end, order);
|
|
|
|
/* check file content after split */
|
|
status = lseek(fd, 0, SEEK_SET);
|
|
if (status == -1) {
|
|
ksft_perror("Cannot lseek file");
|
|
goto close_file;
|
|
}
|
|
|
|
num_read = read(fd, file_buf2, num_written);
|
|
if (num_read == -1 || num_read != num_written) {
|
|
ksft_perror("Cannot read file content back");
|
|
goto close_file;
|
|
}
|
|
|
|
if (strncmp(file_buf1, file_buf2, pmd_pagesize) != 0) {
|
|
ksft_print_msg("File content changed\n");
|
|
goto close_file;
|
|
}
|
|
|
|
close(fd);
|
|
status = unlink(testfile);
|
|
if (status) {
|
|
ksft_perror("Cannot remove testing file");
|
|
goto cleanup;
|
|
}
|
|
|
|
status = umount(tmpfs_loc);
|
|
if (status) {
|
|
rmdir(tmpfs_loc);
|
|
ksft_exit_fail_msg("Unable to umount %s\n", tmpfs_loc);
|
|
}
|
|
|
|
status = rmdir(tmpfs_loc);
|
|
if (status)
|
|
ksft_exit_fail_msg("cannot remove tmp dir: %s\n", strerror(errno));
|
|
|
|
ksft_print_msg("Please check dmesg for more information\n");
|
|
ksft_test_result_pass("File-backed THP split to order %d test done\n", order);
|
|
return;
|
|
|
|
close_file:
|
|
close(fd);
|
|
cleanup:
|
|
umount(tmpfs_loc);
|
|
rmdir(tmpfs_loc);
|
|
out:
|
|
ksft_exit_fail_msg("Error occurred\n");
|
|
}
|
|
|
|
bool prepare_thp_fs(const char *xfs_path, char *thp_fs_template,
|
|
const char **thp_fs_loc)
|
|
{
|
|
if (xfs_path) {
|
|
*thp_fs_loc = xfs_path;
|
|
return false;
|
|
}
|
|
|
|
*thp_fs_loc = mkdtemp(thp_fs_template);
|
|
|
|
if (!*thp_fs_loc)
|
|
ksft_exit_fail_msg("cannot create temp folder\n");
|
|
|
|
return true;
|
|
}
|
|
|
|
void cleanup_thp_fs(const char *thp_fs_loc, bool created_tmp)
|
|
{
|
|
int status;
|
|
|
|
if (!created_tmp)
|
|
return;
|
|
|
|
status = rmdir(thp_fs_loc);
|
|
if (status)
|
|
ksft_exit_fail_msg("cannot remove tmp dir: %s\n",
|
|
strerror(errno));
|
|
}
|
|
|
|
int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
|
|
char **addr)
|
|
{
|
|
size_t i;
|
|
unsigned char buf[1024];
|
|
|
|
srand(time(NULL));
|
|
|
|
*fd = open(testfile, O_CREAT | O_RDWR, 0664);
|
|
if (*fd == -1)
|
|
ksft_exit_fail_msg("Failed to create a file at %s\n", testfile);
|
|
|
|
assert(fd_size % sizeof(buf) == 0);
|
|
for (i = 0; i < sizeof(buf); i++)
|
|
buf[i] = (unsigned char)i;
|
|
for (i = 0; i < fd_size; i += sizeof(buf))
|
|
write(*fd, buf, sizeof(buf));
|
|
|
|
close(*fd);
|
|
sync();
|
|
*fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
|
|
if (*fd == -1) {
|
|
ksft_perror("open drop_caches");
|
|
goto err_out_unlink;
|
|
}
|
|
if (write(*fd, "3", 1) != 1) {
|
|
ksft_perror("write to drop_caches");
|
|
goto err_out_unlink;
|
|
}
|
|
close(*fd);
|
|
|
|
*fd = open(testfile, O_RDWR);
|
|
if (*fd == -1) {
|
|
ksft_perror("Failed to open testfile\n");
|
|
goto err_out_unlink;
|
|
}
|
|
|
|
*addr = mmap(NULL, fd_size, PROT_READ|PROT_WRITE, MAP_SHARED, *fd, 0);
|
|
if (*addr == (char *)-1) {
|
|
ksft_perror("cannot mmap");
|
|
goto err_out_close;
|
|
}
|
|
madvise(*addr, fd_size, MADV_HUGEPAGE);
|
|
|
|
for (size_t i = 0; i < fd_size; i++)
|
|
FORCE_READ((*addr + i));
|
|
|
|
if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) {
|
|
ksft_print_msg("No large pagecache folio generated, please provide a filesystem supporting large folio\n");
|
|
munmap(*addr, fd_size);
|
|
close(*fd);
|
|
unlink(testfile);
|
|
ksft_test_result_skip("Pagecache folio split skipped\n");
|
|
return -2;
|
|
}
|
|
return 0;
|
|
err_out_close:
|
|
close(*fd);
|
|
err_out_unlink:
|
|
unlink(testfile);
|
|
ksft_exit_fail_msg("Failed to create large pagecache folios\n");
|
|
return -1;
|
|
}
|
|
|
|
void split_thp_in_pagecache_to_order_at(size_t fd_size, const char *fs_loc,
|
|
int order, int offset)
|
|
{
|
|
int fd;
|
|
char *addr;
|
|
size_t i;
|
|
char testfile[INPUT_MAX];
|
|
int err = 0;
|
|
|
|
err = snprintf(testfile, INPUT_MAX, "%s/test", fs_loc);
|
|
|
|
if (err < 0)
|
|
ksft_exit_fail_msg("cannot generate right test file name\n");
|
|
|
|
err = create_pagecache_thp_and_fd(testfile, fd_size, &fd, &addr);
|
|
if (err)
|
|
return;
|
|
err = 0;
|
|
|
|
if (offset == -1)
|
|
write_debugfs(PID_FMT, getpid(), (uint64_t)addr,
|
|
(uint64_t)addr + fd_size, order);
|
|
else
|
|
write_debugfs(PID_FMT_OFFSET, getpid(), (uint64_t)addr,
|
|
(uint64_t)addr + fd_size, order, offset);
|
|
|
|
for (i = 0; i < fd_size; i++)
|
|
if (*(addr + i) != (char)i) {
|
|
ksft_print_msg("%lu byte corrupted in the file\n", i);
|
|
err = EXIT_FAILURE;
|
|
goto out;
|
|
}
|
|
|
|
if (!check_huge_file(addr, 0, pmd_pagesize)) {
|
|
ksft_print_msg("Still FilePmdMapped not split\n");
|
|
err = EXIT_FAILURE;
|
|
goto out;
|
|
}
|
|
|
|
out:
|
|
munmap(addr, fd_size);
|
|
close(fd);
|
|
unlink(testfile);
|
|
if (offset == -1) {
|
|
if (err)
|
|
ksft_exit_fail_msg("Split PMD-mapped pagecache folio to order %d failed\n", order);
|
|
ksft_test_result_pass("Split PMD-mapped pagecache folio to order %d passed\n", order);
|
|
} else {
|
|
if (err)
|
|
ksft_exit_fail_msg("Split PMD-mapped pagecache folio to order %d at in-folio offset %d failed\n", order, offset);
|
|
ksft_test_result_pass("Split PMD-mapped pagecache folio to order %d at in-folio offset %d passed\n", order, offset);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int i;
|
|
size_t fd_size;
|
|
char *optional_xfs_path = NULL;
|
|
char fs_loc_template[] = "/tmp/thp_fs_XXXXXX";
|
|
const char *fs_loc;
|
|
bool created_tmp;
|
|
int offset;
|
|
|
|
ksft_print_header();
|
|
|
|
if (geteuid() != 0) {
|
|
ksft_print_msg("Please run the benchmark as root\n");
|
|
ksft_finished();
|
|
}
|
|
|
|
if (argc > 1)
|
|
optional_xfs_path = argv[1];
|
|
|
|
ksft_set_plan(1+8+1+9+9+8*4+2);
|
|
|
|
pagesize = getpagesize();
|
|
pageshift = ffs(pagesize) - 1;
|
|
pmd_pagesize = read_pmd_pagesize();
|
|
if (!pmd_pagesize)
|
|
ksft_exit_fail_msg("Reading PMD pagesize failed\n");
|
|
|
|
fd_size = 2 * pmd_pagesize;
|
|
|
|
split_pmd_zero_pages();
|
|
|
|
for (i = 0; i < 9; i++)
|
|
if (i != 1)
|
|
split_pmd_thp_to_order(i);
|
|
|
|
split_pte_mapped_thp();
|
|
for (i = 0; i < 9; i++)
|
|
split_file_backed_thp(i);
|
|
|
|
created_tmp = prepare_thp_fs(optional_xfs_path, fs_loc_template,
|
|
&fs_loc);
|
|
for (i = 8; i >= 0; i--)
|
|
split_thp_in_pagecache_to_order_at(fd_size, fs_loc, i, -1);
|
|
|
|
for (i = 0; i < 9; i++)
|
|
for (offset = 0;
|
|
offset < pmd_pagesize / pagesize;
|
|
offset += MAX(pmd_pagesize / pagesize / 4, 1 << i))
|
|
split_thp_in_pagecache_to_order_at(fd_size, fs_loc, i, offset);
|
|
cleanup_thp_fs(fs_loc, created_tmp);
|
|
|
|
ksft_finished();
|
|
|
|
return 0;
|
|
}
|