Check that paging out a whole PMD-mapped THP swaps it without corrupting it, and that pages outside the range stay mapped after paging out a base page or half of a PMD- or PTE-mapped THP.
Also check that MADV_COLD on half of either THP preserves its contents. Tests skip when PMD-sized THPs cannot be allocated, and the MADV_PAGEOUT tests also when MADV_PAGEOUT cannot swap. With /proc/kpageflags the PTE-mapped cases also confirm that PTE-mapping left the folio large. Signed-off-by: Gregory Price (Meta) <[email protected]> Reviewed-by: Sarthak Sharma <[email protected]> --- v3: Kept Sarthak's Reviewed-by; the MADV_PAGEOUT tests now skip only if MADV_PAGEOUT cannot swap, and fail if reclaim then does not happen, as he suggested for v2 7/8. --- .../testing/selftests/mm/madv_cold_pageout.c | 238 ++++++++++++++++++ 1 file changed, 238 insertions(+) diff --git a/tools/testing/selftests/mm/madv_cold_pageout.c b/tools/testing/selftests/mm/madv_cold_pageout.c index 7d92f0ecbd27..b5eec490fb68 100644 --- a/tools/testing/selftests/mm/madv_cold_pageout.c +++ b/tools/testing/selftests/mm/madv_cold_pageout.c @@ -8,6 +8,7 @@ #include <sched.h> #include <stdbool.h> #include <stdint.h> +#include <string.h> #include <sys/ipc.h> #include <sys/mman.h> #include <sys/shm.h> @@ -17,10 +18,15 @@ #include "kselftest_harness.h" #include "vm_util.h" +#ifndef MADV_COLLAPSE +#define MADV_COLLAPSE 25 +#endif + /* Reclaim can fail transiently, so retry for up to a second. */ #define PAGEOUT_RETRIES 100 static size_t page_size; +static size_t pmd_size; /* Whether MADV_PAGEOUT can swap out anonymous memory, see main(). */ static bool can_swap; @@ -92,6 +98,111 @@ FIXTURE_TEARDOWN(madv_cold_pageout) close(self->pagemap_fd); } +/* + * Reserve a PMD-aligned range, so that memory mapped over it with MAP_FIXED + * can be backed by PMD-mapped THPs. + */ +static char *reserve_aligned(struct __test_metadata *_metadata, size_t size) +{ + char *reservation, *aligned; + + reservation = mmap(NULL, size + pmd_size, PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(reservation, MAP_FAILED); + + aligned = (char *)(((uintptr_t)reservation + pmd_size - 1) & + ~(pmd_size - 1)); + if (aligned != reservation) + ASSERT_EQ(munmap(reservation, aligned - reservation), 0); + ASSERT_EQ(munmap(aligned + size, reservation + pmd_size - aligned), 0); + + return aligned; +} + +/* Map PMD-aligned anonymous memory filled with 1s. */ +static char *map_aligned_pages(struct __test_metadata *_metadata, size_t size) +{ + char *mapping = reserve_aligned(_metadata, size); + + ASSERT_EQ(mmap(mapping, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0), + mapping); + memset(mapping, 1, size); + + return mapping; +} + +/* + * Collapse a PMD-aligned range into PMD-mapped THPs, and confirm the result + * with check_huge(). MADV_COLLAPSE may fail transiently with EAGAIN. + */ +static bool collapse(char *mapping, size_t size, + bool (*check_huge)(void *, size_t, int, uint64_t)) +{ + int nr_hpages = size / pmd_size; + int retry; + + for (retry = 0; retry < 10; retry++) { + if (madvise(mapping, size, MADV_COLLAPSE)) { + if (errno != EAGAIN) + return false; + } else if (check_huge(mapping, size, nr_hpages, pmd_size)) { + return true; + } + usleep(10000); + } + + return false; +} + +/* Map a PMD-mapped anonymous THP filled with 1s, or return NULL. */ +static char *map_thp(struct __test_metadata *_metadata) +{ + char *mapping; + + if (!pmd_size) + return NULL; + mapping = map_aligned_pages(_metadata, pmd_size); + if (collapse(mapping, pmd_size, check_huge_anon)) + return mapping; + ASSERT_EQ(munmap(mapping, pmd_size), 0); + return NULL; +} + +/* + * Changing the protection of one page PTE-maps a THP. When /proc/kpageflags + * is readable, also check that the folio is still large. + */ +static void pte_map_thp(struct __test_metadata *_metadata, char *mapping, + int pagemap_fd, int kpageflags_fd) +{ + unsigned long pfn; + uint64_t flags; + + ASSERT_EQ(mprotect(mapping + page_size, page_size, PROT_READ), 0); + ASSERT_EQ(mprotect(mapping + page_size, page_size, + PROT_READ | PROT_WRITE), 0); + ASSERT_TRUE(check_huge_anon(mapping, pmd_size, 0, pmd_size)); + if (kpageflags_fd < 0) + return; + + pfn = pagemap_get_pfn(pagemap_fd, mapping); + ASSERT_NE(pfn, -1UL); + ASSERT_EQ(pageflags_get(pfn, kpageflags_fd, &flags), 0); + ASSERT_TRUE(flags & KPF_COMPOUND_HEAD); +} + +static void assert_filled(struct __test_metadata *_metadata, + const char *mapping, size_t size) +{ + size_t offset; + + for (offset = 0; offset < size; offset += page_size) { + ASSERT_EQ(mapping[offset], 1) + TH_LOG("memory changed at offset %zu", offset); + } +} + /* Page out the range until its first page is swapped, or madvise() fails. */ static bool pageout_until_swapped(char *mapping, size_t size, int pagemap_fd) { @@ -124,6 +235,11 @@ static bool pageout_until_unmapped(char *mapping, size_t size, int pagemap_fd) return false; } +static bool page_is_present(int pagemap_fd, char *addr) +{ + return pagemap_get_entry(pagemap_fd, addr) & PM_PRESENT; +} + /* Create an unlinked, zero-filled temporary file, or return -1. */ static int create_temp_file(size_t size) { @@ -452,6 +568,127 @@ TEST_F(madv_cold_pageout, pageout_skips_unevictable_page) ASSERT_EQ(shmdt(mapping), 0); } +/* MADV_PAGEOUT of a whole THP must swap it without corrupting it. */ +TEST_F(madv_cold_pageout, pageout_swaps_thp) +{ + char *mapping; + + if (!can_swap) + SKIP(return, "MADV_PAGEOUT cannot swap"); + mapping = map_thp(_metadata); + if (!mapping) + SKIP(return, "could not allocate a PMD-sized THP"); + + ASSERT_TRUE(pageout_until_swapped(mapping, pmd_size, self->pagemap_fd)); + assert_filled(_metadata, mapping, pmd_size); + + ASSERT_EQ(munmap(mapping, pmd_size), 0); +} + +/* + * MADV_PAGEOUT must not reclaim pages outside the advised range. kswapd or + * page migration could also unmap them, but neither should happen when the + * selftests run by themselves. + */ +TEST_F(madv_cold_pageout, pageout_stays_within_range) +{ + char *mapping; + + if (!can_swap) + SKIP(return, "MADV_PAGEOUT cannot swap"); + mapping = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(mapping, MAP_FAILED); + memset(mapping, 1, 3 * page_size); + ASSERT_TRUE(pageout_until_swapped(mapping + page_size, page_size, + self->pagemap_fd)); + + ASSERT_TRUE(page_is_present(self->pagemap_fd, mapping)); + ASSERT_TRUE(page_is_present(self->pagemap_fd, mapping + 2 * page_size)); + assert_filled(_metadata, mapping, 3 * page_size); + + ASSERT_EQ(munmap(mapping, 3 * page_size), 0); +} + +/* + * MADV_PAGEOUT of half of a THP must split it, and reclaim only the advised + * half. See pageout_stays_within_range about other unmapping. + */ +static void pageout_half_thp(struct __test_metadata *_metadata, char *mapping, + int pagemap_fd) +{ + size_t offset; + + ASSERT_TRUE(pageout_until_swapped(mapping, pmd_size / 2, pagemap_fd)); + + for (offset = pmd_size / 2; offset < pmd_size; offset += page_size) { + ASSERT_TRUE(page_is_present(pagemap_fd, mapping + offset)) + TH_LOG("page at offset %zu was unmapped", offset); + } + assert_filled(_metadata, mapping, pmd_size); +} + +TEST_F(madv_cold_pageout, pageout_stays_within_pmd_mapped_thp) +{ + char *mapping; + + if (!can_swap) + SKIP(return, "MADV_PAGEOUT cannot swap"); + mapping = map_thp(_metadata); + if (!mapping) + SKIP(return, "could not allocate a PMD-sized THP"); + + pageout_half_thp(_metadata, mapping, self->pagemap_fd); + + ASSERT_EQ(munmap(mapping, pmd_size), 0); +} + +TEST_F(madv_cold_pageout, pageout_stays_within_pte_mapped_thp) +{ + char *mapping; + + if (!can_swap) + SKIP(return, "MADV_PAGEOUT cannot swap"); + mapping = map_thp(_metadata); + if (!mapping) + SKIP(return, "could not allocate a PMD-sized THP"); + pte_map_thp(_metadata, mapping, self->pagemap_fd, self->kpageflags_fd); + + pageout_half_thp(_metadata, mapping, self->pagemap_fd); + + ASSERT_EQ(munmap(mapping, pmd_size), 0); +} + +/* MADV_COLD on part of a THP may split it, but must not change it. */ +TEST_F(madv_cold_pageout, cold_preserves_half_of_pmd_mapped_thp) +{ + char *mapping; + + mapping = map_thp(_metadata); + if (!mapping) + SKIP(return, "could not allocate a PMD-sized THP"); + + ASSERT_EQ(madvise(mapping, pmd_size / 2, MADV_COLD), 0); + assert_filled(_metadata, mapping, pmd_size); + + ASSERT_EQ(munmap(mapping, pmd_size), 0); +} + +TEST_F(madv_cold_pageout, cold_preserves_half_of_pte_mapped_thp) +{ + char *mapping; + + mapping = map_thp(_metadata); + if (!mapping) + SKIP(return, "could not allocate a PMD-sized THP"); + pte_map_thp(_metadata, mapping, self->pagemap_fd, self->kpageflags_fd); + + ASSERT_EQ(madvise(mapping, pmd_size / 2, MADV_COLD), 0); + assert_filled(_metadata, mapping, pmd_size); + + ASSERT_EQ(munmap(mapping, pmd_size), 0); +} + /* Swap out an anonymous page, to see whether swap works at all. */ static bool probe_swap(int pagemap_fd) { @@ -474,6 +711,7 @@ int main(int argc, char **argv) int pagemap_fd; page_size = getpagesize(); + pmd_size = read_pmd_pagesize(); /* The harness forks each test, which inherits this. */ pin_to_current_cpu(); /* Only root can reserve the page that hugetlb_vma_is_rejected maps. */ -- 2.56.0
