mirror of
https://github.com/torvalds/linux.git
synced 2026-07-27 09:36:22 +02:00
hugetlb-vmemmap test fails if there are no free huge pages prepared by a wrapper script. Add setup of HugeTLB pages to the test and make sure that the original settings are restored on the test exit. Link: https://lore.kernel.org/20260511162840.375890-45-rppt@kernel.org Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Tested-by: Luiz Capitulino <luizcap@redhat.com> Tested-by: Sarthak Sharma <sarthak.sharma@arm.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Barry Song <baohua@kernel.org> Cc: David Hildenbrand <david@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: Donet Tom <donettom@linux.ibm.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Lance Yang <lance.yang@linux.dev> Cc: Leon Romanovsky <leon@kernel.org> Cc: Liam Howlett <liam@infradead.org> Cc: Li Wang <li.wang@linux.dev> Cc: Lorenzo Stoakes <ljs@kernel.org> Cc: Mark Brown <broonie@kernel.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Nico Pache <npache@redhat.com> Cc: Peter Xu <peterx@redhat.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@kernel.org> Cc: Zi Yan <ziy@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
133 lines
3.1 KiB
C
133 lines
3.1 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* A test case of using hugepage memory in a user application using the
|
|
* mmap system call with MAP_HUGETLB flag. Before running this program
|
|
* make sure the administrator has allocated enough default sized huge
|
|
* pages to cover the 2 MB allocation.
|
|
*/
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <sys/mman.h>
|
|
#include <fcntl.h>
|
|
#include "vm_util.h"
|
|
#include "hugepage_settings.h"
|
|
|
|
#define PAGE_COMPOUND_HEAD (1UL << 15)
|
|
#define PAGE_COMPOUND_TAIL (1UL << 16)
|
|
#define PAGE_HUGE (1UL << 17)
|
|
|
|
#define HEAD_PAGE_FLAGS (PAGE_COMPOUND_HEAD | PAGE_HUGE)
|
|
#define TAIL_PAGE_FLAGS (PAGE_COMPOUND_TAIL | PAGE_HUGE)
|
|
|
|
#define PM_PFRAME_BITS 55
|
|
#define PM_PFRAME_MASK ~((1UL << PM_PFRAME_BITS) - 1)
|
|
|
|
static size_t pagesize;
|
|
static size_t maplength;
|
|
|
|
static void write_bytes(char *addr, size_t length)
|
|
{
|
|
unsigned long i;
|
|
|
|
for (i = 0; i < length; i++)
|
|
*(addr + i) = (char)i;
|
|
}
|
|
|
|
static unsigned long virt_to_pfn(void *addr)
|
|
{
|
|
int fd;
|
|
unsigned long pagemap;
|
|
|
|
fd = open("/proc/self/pagemap", O_RDONLY);
|
|
if (fd < 0)
|
|
return -1UL;
|
|
|
|
lseek(fd, (unsigned long)addr / pagesize * sizeof(pagemap), SEEK_SET);
|
|
read(fd, &pagemap, sizeof(pagemap));
|
|
close(fd);
|
|
|
|
return pagemap & ~PM_PFRAME_MASK;
|
|
}
|
|
|
|
static int check_page_flags(unsigned long pfn)
|
|
{
|
|
int fd, i;
|
|
unsigned long pageflags;
|
|
|
|
fd = open("/proc/kpageflags", O_RDONLY);
|
|
if (fd < 0)
|
|
return -1;
|
|
|
|
lseek(fd, pfn * sizeof(pageflags), SEEK_SET);
|
|
|
|
read(fd, &pageflags, sizeof(pageflags));
|
|
if ((pageflags & HEAD_PAGE_FLAGS) != HEAD_PAGE_FLAGS) {
|
|
close(fd);
|
|
ksft_print_msg("Head page flags (%lx) is invalid\n", pageflags);
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* pages other than the first page must be tail and shouldn't be head;
|
|
* this also verifies kernel has correctly set the fake page_head to tail
|
|
* while hugetlb_free_vmemmap is enabled.
|
|
*/
|
|
for (i = 1; i < maplength / pagesize; i++) {
|
|
read(fd, &pageflags, sizeof(pageflags));
|
|
if ((pageflags & TAIL_PAGE_FLAGS) != TAIL_PAGE_FLAGS ||
|
|
(pageflags & HEAD_PAGE_FLAGS) == HEAD_PAGE_FLAGS) {
|
|
close(fd);
|
|
ksft_print_msg("Tail page flags (%lx) is invalid\n", pageflags);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
void *addr;
|
|
unsigned long pfn;
|
|
int ret;
|
|
|
|
ksft_print_header();
|
|
ksft_set_plan(1);
|
|
|
|
if (!hugetlb_setup_default(1))
|
|
ksft_exit_skip("Not enough free huge pages\n");
|
|
|
|
pagesize = psize();
|
|
maplength = default_huge_page_size();
|
|
if (!maplength)
|
|
ksft_exit_skip("Unable to determine huge page size\n");
|
|
|
|
addr = mmap(NULL, maplength, PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
|
|
if (addr == MAP_FAILED)
|
|
ksft_exit_fail_perror("mmap");
|
|
|
|
/* Trigger allocation of HugeTLB page. */
|
|
write_bytes(addr, maplength);
|
|
|
|
pfn = virt_to_pfn(addr);
|
|
if (pfn == -1UL) {
|
|
ksft_perror("virt_to_pfn");
|
|
munmap(addr, maplength);
|
|
ksft_exit_fail();
|
|
}
|
|
|
|
ksft_print_msg("Returned address is %p whose pfn is %lx\n", addr, pfn);
|
|
|
|
ret = check_page_flags(pfn);
|
|
|
|
if (munmap(addr, maplength))
|
|
ksft_exit_fail_perror("munmap");
|
|
|
|
ksft_test_result(!ret, "HugeTLB vmemmap page flags\n");
|
|
ksft_finished();
|
|
}
|