Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41BC7367B78 for ; Sun, 21 Jun 2026 18:38:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782067128; cv=none; b=gj47zZgsKCDM2vlfqTe0Bf8IdVNdY9mXsA9ckVfpkm0qmGd/wzOxWD5lw2IL7MccD3y4gmEWn00khyNpu5SLJkM8wf7cwdbsmpcgeess0YOYc0Ds2TipTcnRluz2Euz7i4MYbZpbg7Pc1L3+LpMajOoUecv9oLpd+tAr/VH42g0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782067128; c=relaxed/simple; bh=GYmhl7NE1jE6Mkhvm1IUmIXeHoEGx1D3NYW0pPGqNsU=; h=Date:To:From:Subject:Message-Id; b=nlucKaRq/QGCIpaGbIm1yoaWtUyg3bL0fOh+wIyng/i3enJUyWeVSjKdh6yGRW4PbBxjS5MJD+r3kyZnN/Cpis0Ak7tLa0ya/h7VTn540+1OhbZqir21Af2ycw+0fwQRbJ16EQmYqsRPC38tmU253S9dAuZAXkg77tHp2o4KWH0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=h+vUKeuh; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="h+vUKeuh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 127B11F00A3D; Sun, 21 Jun 2026 18:38:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1782067126; bh=LVEU9kM8eXDCJQH8ekbjJgZKyp4ZQeKA16dfsF9lsG4=; h=Date:To:From:Subject; b=h+vUKeuhO8XZ4jsjb2wzAEs0EpMfmdlGmPXwow9vYubLo+u5ucFKx5ONHUet1WtZw tH11RnzMenukwDyMoXK13Zl1z2B8w76nHEGzc1JDQOJigJD6NNBLirzzk2x9IYi9JV wOqHhDztdB1hh4WJOqqx93DfQrp7rLTVy5ijPVr4= Date: Sun, 21 Jun 2026 11:38:45 -0700 To: mm-commits@vger.kernel.org,ziy@nvidia.com,vbabka@kernel.org,surenb@google.com,shuah@kernel.org,sarthak.sharma@arm.com,ryan.roberts@arm.com,peterx@redhat.com,npache@redhat.com,mhocko@suse.com,luizcap@redhat.com,ljs@kernel.org,li.wang@linux.dev,liam@infradead.org,leon@kernel.org,lance.yang@linux.dev,jhubbard@nvidia.com,jgg@ziepe.ca,donettom@linux.ibm.com,dev.jain@arm.com,david@kernel.org,broonie@kernel.org,baolin.wang@linux.alibaba.com,baohua@kernel.org,rppt@kernel.org,akpm@linux-foundation.org From: Andrew Morton Subject: [merged mm-stable] selftests-mm-rename-hugepage-tests-to-hugetlb.patch removed from -mm tree Message-Id: <20260621183846.127B11F00A3D@smtp.kernel.org> Precedence: bulk X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The quilt patch titled Subject: selftests/mm: rename hugepage-* tests to hugetlb-* has been removed from the -mm tree. Its filename was selftests-mm-rename-hugepage-tests-to-hugetlb.patch This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm ------------------------------------------------------ From: "Mike Rapoport (Microsoft)" Subject: selftests/mm: rename hugepage-* tests to hugetlb-* Date: Mon, 11 May 2026 19:27:51 +0300 hugepage could mean both THP and HugeTLB these days. Rename hugepage-* tests for HugeTLB to hugetlb-* to avoid confusion. Make sure that Makefile update keeps alphabetical ordering of the TEST_GEN_FILES entries. Keep old binary names in .gitignore because Linus prefers it this way. Link: https://lore.kernel.org/20260511162840.375890-8-rppt@kernel.org Signed-off-by: Mike Rapoport (Microsoft) Reviewed-by: Donet Tom Reviewed-by: Li Wang Tested-by: Sarthak Sharma Tested-by: Luiz Capitulino Cc: Baolin Wang Cc: Barry Song Cc: David Hildenbrand Cc: Dev Jain Cc: Jason Gunthorpe Cc: John Hubbard Cc: Lance Yang Cc: Leon Romanovsky Cc: Liam Howlett Cc: Lorenzo Stoakes Cc: Mark Brown Cc: Michal Hocko Cc: Nico Pache Cc: Peter Xu Cc: Ryan Roberts Cc: Shuah Khan Cc: Suren Baghdasaryan Cc: Vlastimil Babka Cc: Zi Yan Signed-off-by: Andrew Morton --- Documentation/admin-guide/mm/hugetlbpage.rst | 8 tools/testing/selftests/mm/.gitignore | 4 tools/testing/selftests/mm/Makefile | 8 tools/testing/selftests/mm/hugepage-mmap.c | 135 ------------ tools/testing/selftests/mm/hugepage-mremap.c | 178 ---------------- tools/testing/selftests/mm/hugepage-shm.c | 81 ------- tools/testing/selftests/mm/hugepage-vmemmap.c | 134 ------------ tools/testing/selftests/mm/hugetlb-madvise.c | 2 tools/testing/selftests/mm/hugetlb-mmap.c | 135 ++++++++++++ tools/testing/selftests/mm/hugetlb-mremap.c | 178 ++++++++++++++++ tools/testing/selftests/mm/hugetlb-shm.c | 81 +++++++ tools/testing/selftests/mm/hugetlb-vmemmap.c | 134 ++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 8 13 files changed, 545 insertions(+), 541 deletions(-) --- a/Documentation/admin-guide/mm/hugetlbpage.rst~selftests-mm-rename-hugepage-tests-to-hugetlb +++ a/Documentation/admin-guide/mm/hugetlbpage.rst @@ -475,11 +475,11 @@ Examples .. _examples: -``hugepage-shm`` - see tools/testing/selftests/mm/hugepage-shm.c +``hugetlb-shm`` + see tools/testing/selftests/mm/hugetlb-shm.c -``hugepage-mmap`` - see tools/testing/selftests/mm/hugepage-mmap.c +``hugetlb-mmap`` + see tools/testing/selftests/mm/hugetlb-mmap.c The `libhugetlbfs`_ library provides a wide range of userspace tools to help with huge page usability, environment setup, and control. --- a/tools/testing/selftests/mm/.gitignore~selftests-mm-rename-hugepage-tests-to-hugetlb +++ a/tools/testing/selftests/mm/.gitignore @@ -4,6 +4,10 @@ hugepage-mmap hugepage-mremap hugepage-shm hugepage-vmemmap +hugetlb-mmap +hugetlb-mremap +hugetlb-shm +hugetlb-vmemmap hugetlb-madvise hugetlb-read-hwpoison hugetlb-soft-offline diff --git a/tools/testing/selftests/mm/hugepage-mmap.c a/tools/testing/selftests/mm/hugepage-mmap.c deleted file mode 100644 --- a/tools/testing/selftests/mm/hugepage-mmap.c +++ /dev/null @@ -1,135 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * hugepage-mmap: - * - * Example of using huge page memory in a user application using the mmap - * system call. Before running this application, make sure that the - * administrator has mounted the hugetlbfs filesystem (on some directory - * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this - * example, the app is requesting memory of size 256MB that is backed by - * huge pages. - */ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include "vm_util.h" -#include "kselftest.h" - -#define LENGTH (256UL*1024*1024) -#define PROTECTION (PROT_READ | PROT_WRITE) - -static void check_bytes(char *addr) -{ - ksft_print_msg("First hex is %x\n", *((unsigned int *)addr)); -} - -static void write_bytes(char *addr, size_t length) -{ - unsigned long i; - - for (i = 0; i < length; i++) - *(addr + i) = (char)i; -} - -static bool verify_bytes(char *addr, size_t length) -{ - unsigned long i; - - check_bytes(addr); - for (i = 0; i < length; i++) - if (*(addr + i) != (char)i) { - ksft_print_msg("Error: Mismatch at %lu(%p)\n", i, addr); - return false; - } - - return true; -} - -static void test_mmap(size_t length, int mmap_flags, int fd, - const char *test_name) -{ - bool passed = true; - void *addr; - - addr = mmap(NULL, length, PROTECTION, mmap_flags, fd, 0); - if (addr == MAP_FAILED) - ksft_exit_fail_perror("mmap"); - - ksft_print_msg("Returned address is %p\n", addr); - check_bytes(addr); - write_bytes(addr, length); - if (!verify_bytes(addr, length)) - passed = false; - - /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */ - if (munmap(addr, length)) - ksft_exit_fail_perror("munmap"); - - ksft_test_result(passed, "%s\n", test_name); -} - -static void test_anon_mmap(size_t length, int shift) -{ - const char *test_name = "hugetlb anonymous mmap"; - int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB; - - if (shift) - mmap_flags |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT; - - test_mmap(length, mmap_flags, -1, test_name); -} - -static void test_file_mmap(size_t length, int shift) -{ - const char *test_name = "hugetlb file mmap"; - int mfd_flags = MFD_HUGETLB; - int fd; - - if (shift) - mfd_flags |= (shift & MFD_HUGE_MASK) << MFD_HUGE_SHIFT; - - fd = memfd_create("hugetlb-mmap", mfd_flags); - if (fd < 0) - ksft_exit_fail_perror("memfd_create"); - - test_mmap(length, MAP_SHARED, fd, test_name); - close(fd); -} - -int main(int argc, char **argv) -{ - size_t hugepage_size; - size_t length = LENGTH; - int shift = 0; - - ksft_print_header(); - ksft_set_plan(2); - - if (argc > 1) - length = atol(argv[1]) << 20; - if (argc > 2) - shift = atoi(argv[2]); - - if (shift) { - hugepage_size = (1UL << shift); - ksft_print_msg("%lu kB hugepages\n", 1UL << (shift - 10)); - } else { - hugepage_size = default_huge_page_size(); - ksft_print_msg("Default size hugepages (%lu kB)\n", hugepage_size >> 10); - } - - /* munmap will fail if the length is not page aligned */ - if (hugepage_size > length) - length = hugepage_size; - - ksft_print_msg("Mapping %lu Mbytes\n", (unsigned long)length >> 20); - - test_anon_mmap(length, shift); - test_file_mmap(length, shift); - - ksft_finished(); -} diff --git a/tools/testing/selftests/mm/hugepage-mremap.c a/tools/testing/selftests/mm/hugepage-mremap.c deleted file mode 100644 --- a/tools/testing/selftests/mm/hugepage-mremap.c +++ /dev/null @@ -1,178 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * hugepage-mremap: - * - * Example of remapping huge page memory in a user application using the - * mremap system call. The path to a file in a hugetlbfs filesystem must - * be passed as the last argument to this test. The amount of memory used - * by this test in MBs can optionally be passed as an argument. If no memory - * amount is passed, the default amount is 10MB. - * - * To make sure the test triggers pmd sharing and goes through the 'unshare' - * path in the mremap code use 1GB (1024) or more. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include /* Definition of O_* constants */ -#include /* Definition of SYS_* constants */ -#include -#include -#include -#include -#include "kselftest.h" -#include "vm_util.h" - -#define DEFAULT_LENGTH_MB 10UL -#define MB_TO_BYTES(x) (x * 1024 * 1024) - -#define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC) -#define FLAGS (MAP_SHARED | MAP_ANONYMOUS) - -static void check_bytes(char *addr) -{ - ksft_print_msg("First hex is %x\n", *((unsigned int *)addr)); -} - -static void write_bytes(char *addr, size_t len) -{ - unsigned long i; - - for (i = 0; i < len; i++) - *(addr + i) = (char)i; -} - -static int read_bytes(char *addr, size_t len) -{ - unsigned long i; - - check_bytes(addr); - for (i = 0; i < len; i++) - if (*(addr + i) != (char)i) { - ksft_print_msg("Mismatch at %lu\n", i); - return 1; - } - return 0; -} - -static void register_region_with_uffd(char *addr, size_t len) -{ - long uffd; /* userfaultfd file descriptor */ - struct uffdio_api uffdio_api; - - /* Create and enable userfaultfd object. */ - uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); - if (uffd == -1) { - switch (errno) { - case EPERM: - ksft_exit_skip("Insufficient permissions, try running as root.\n"); - break; - case ENOSYS: - ksft_exit_skip("userfaultfd is not supported/not enabled.\n"); - break; - default: - ksft_exit_fail_msg("userfaultfd failed with %s\n", strerror(errno)); - break; - } - } - - uffdio_api.api = UFFD_API; - uffdio_api.features = 0; - if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) - ksft_exit_fail_msg("ioctl-UFFDIO_API: %s\n", strerror(errno)); - - /* Create a private anonymous mapping. The memory will be - * demand-zero paged--that is, not yet allocated. When we - * actually touch the memory, it will be allocated via - * the userfaultfd. - */ - - addr = mmap(NULL, len, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (addr == MAP_FAILED) - ksft_exit_fail_msg("mmap: %s\n", strerror(errno)); - - ksft_print_msg("Address returned by mmap() = %p\n", addr); - - /* Register the memory range of the mapping we just created for - * handling by the userfaultfd object. In mode, we request to track - * missing pages (i.e., pages that have not yet been faulted in). - */ - if (uffd_register(uffd, addr, len, true, false, false)) - ksft_exit_fail_msg("ioctl-UFFDIO_REGISTER: %s\n", strerror(errno)); -} - -int main(int argc, char *argv[]) -{ - size_t length = 0; - int ret = 0, fd; - - ksft_print_header(); - ksft_set_plan(1); - - if (argc >= 2 && !strcmp(argv[1], "-h")) - ksft_exit_fail_msg("Usage: %s [length_in_MB]\n", argv[0]); - - /* Read memory length as the first arg if valid, otherwise fallback to - * the default length. - */ - if (argc >= 2) - length = (size_t)atoi(argv[1]); - else - length = DEFAULT_LENGTH_MB; - - length = MB_TO_BYTES(length); - fd = memfd_create(argv[0], MFD_HUGETLB); - if (fd < 0) - ksft_exit_fail_msg("Open failed: %s\n", strerror(errno)); - - /* mmap to a PUD aligned address to hopefully trigger pmd sharing. */ - unsigned long suggested_addr = 0x7eaa40000000; - void *haddr = mmap((void *)suggested_addr, length, PROTECTION, - MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0); - ksft_print_msg("Map haddr: Returned address is %p\n", haddr); - if (haddr == MAP_FAILED) - ksft_exit_fail_msg("mmap1: %s\n", strerror(errno)); - - /* mmap again to a dummy address to hopefully trigger pmd sharing. */ - suggested_addr = 0x7daa40000000; - void *daddr = mmap((void *)suggested_addr, length, PROTECTION, - MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0); - ksft_print_msg("Map daddr: Returned address is %p\n", daddr); - if (daddr == MAP_FAILED) - ksft_exit_fail_msg("mmap3: %s\n", strerror(errno)); - - suggested_addr = 0x7faa40000000; - void *vaddr = - mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0); - ksft_print_msg("Map vaddr: Returned address is %p\n", vaddr); - if (vaddr == MAP_FAILED) - ksft_exit_fail_msg("mmap2: %s\n", strerror(errno)); - - register_region_with_uffd(haddr, length); - - void *addr = mremap(haddr, length, length, - MREMAP_MAYMOVE | MREMAP_FIXED, vaddr); - if (addr == MAP_FAILED) - ksft_exit_fail_msg("mremap: %s\n", strerror(errno)); - - ksft_print_msg("Mremap: Returned address is %p\n", addr); - check_bytes(addr); - write_bytes(addr, length); - ret = read_bytes(addr, length); - - munmap(addr, length); - - addr = mremap(addr, length, length, 0); - if (addr != MAP_FAILED) - ksft_exit_fail_msg("mremap: Expected failure, but call succeeded\n"); - - close(fd); - - ksft_test_result(!ret, "Read same data\n"); - ksft_exit(!ret); -} diff --git a/tools/testing/selftests/mm/hugepage-shm.c a/tools/testing/selftests/mm/hugepage-shm.c deleted file mode 100644 --- a/tools/testing/selftests/mm/hugepage-shm.c +++ /dev/null @@ -1,81 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * hugepage-shm: - * - * Example of using huge page memory in a user application using Sys V shared - * memory system calls. In this example the app is requesting 256MB of - * memory that is backed by huge pages. The application uses the flag - * SHM_HUGETLB in the shmget system call to inform the kernel that it is - * requesting huge pages. - * - * Note: The default shared memory limit is quite low on many kernels, - * you may need to increase it via: - * - * echo 268435456 > /proc/sys/kernel/shmmax - * - * This will increase the maximum size per shared memory segment to 256MB. - * The other limit that you will hit eventually is shmall which is the - * total amount of shared memory in pages. To set it to 16GB on a system - * with a 4kB pagesize do: - * - * echo 4194304 > /proc/sys/kernel/shmall - */ - -#include -#include -#include -#include -#include -#include - -#define LENGTH (256UL*1024*1024) - -#define dprintf(x) printf(x) - -int main(void) -{ - int shmid; - unsigned long i; - char *shmaddr; - - shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W); - if (shmid < 0) { - perror("shmget"); - exit(1); - } - printf("shmid: 0x%x\n", shmid); - - shmaddr = shmat(shmid, NULL, 0); - if (shmaddr == (char *)-1) { - perror("Shared memory attach failure"); - shmctl(shmid, IPC_RMID, NULL); - exit(2); - } - printf("shmaddr: %p\n", shmaddr); - - dprintf("Starting the writes:\n"); - for (i = 0; i < LENGTH; i++) { - shmaddr[i] = (char)(i); - if (!(i % (1024 * 1024))) - dprintf("."); - } - dprintf("\n"); - - dprintf("Starting the Check..."); - for (i = 0; i < LENGTH; i++) - if (shmaddr[i] != (char)i) { - printf("\nIndex %lu mismatched\n", i); - exit(3); - } - dprintf("Done.\n"); - - if (shmdt((const void *)shmaddr) != 0) { - perror("Detach failure"); - shmctl(shmid, IPC_RMID, NULL); - exit(4); - } - - shmctl(shmid, IPC_RMID, NULL); - - return 0; -} diff --git a/tools/testing/selftests/mm/hugepage-vmemmap.c a/tools/testing/selftests/mm/hugepage-vmemmap.c deleted file mode 100644 --- a/tools/testing/selftests/mm/hugepage-vmemmap.c +++ /dev/null @@ -1,134 +0,0 @@ -// 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 -#include -#include -#include -#include -#include "vm_util.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); - printf("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); - printf("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; - - pagesize = psize(); - maplength = default_huge_page_size(); - if (!maplength) { - printf("Unable to determine huge page size\n"); - exit(1); - } - - addr = mmap(NULL, maplength, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0); - if (addr == MAP_FAILED) { - perror("mmap"); - exit(1); - } - - /* Trigger allocation of HugeTLB page. */ - write_bytes(addr, maplength); - - pfn = virt_to_pfn(addr); - if (pfn == -1UL) { - munmap(addr, maplength); - perror("virt_to_pfn"); - exit(1); - } - - printf("Returned address is %p whose pfn is %lx\n", addr, pfn); - - if (check_page_flags(pfn) < 0) { - munmap(addr, maplength); - perror("check_page_flags"); - exit(1); - } - - /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */ - if (munmap(addr, maplength)) { - perror("munmap"); - exit(1); - } - - return 0; -} --- a/tools/testing/selftests/mm/hugetlb-madvise.c~selftests-mm-rename-hugepage-tests-to-hugetlb +++ a/tools/testing/selftests/mm/hugetlb-madvise.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 /* - * hugepage-madvise: + * hugetlb-madvise: * * Basic functional testing of madvise MADV_DONTNEED and MADV_REMOVE * on hugetlb mappings. diff --git a/tools/testing/selftests/mm/hugetlb-mmap.c a/tools/testing/selftests/mm/hugetlb-mmap.c new file mode 100664 --- /dev/null +++ a/tools/testing/selftests/mm/hugetlb-mmap.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * hugetlb-mmap: + * + * Example of using huge page memory in a user application using the mmap + * system call. Before running this application, make sure that the + * administrator has mounted the hugetlbfs filesystem (on some directory + * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this + * example, the app is requesting memory of size 256MB that is backed by + * huge pages. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "vm_util.h" +#include "kselftest.h" + +#define LENGTH (256UL*1024*1024) +#define PROTECTION (PROT_READ | PROT_WRITE) + +static void check_bytes(char *addr) +{ + ksft_print_msg("First hex is %x\n", *((unsigned int *)addr)); +} + +static void write_bytes(char *addr, size_t length) +{ + unsigned long i; + + for (i = 0; i < length; i++) + *(addr + i) = (char)i; +} + +static bool verify_bytes(char *addr, size_t length) +{ + unsigned long i; + + check_bytes(addr); + for (i = 0; i < length; i++) + if (*(addr + i) != (char)i) { + ksft_print_msg("Error: Mismatch at %lu(%p)\n", i, addr); + return false; + } + + return true; +} + +static void test_mmap(size_t length, int mmap_flags, int fd, + const char *test_name) +{ + bool passed = true; + void *addr; + + addr = mmap(NULL, length, PROTECTION, mmap_flags, fd, 0); + if (addr == MAP_FAILED) + ksft_exit_fail_perror("mmap"); + + ksft_print_msg("Returned address is %p\n", addr); + check_bytes(addr); + write_bytes(addr, length); + if (!verify_bytes(addr, length)) + passed = false; + + /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */ + if (munmap(addr, length)) + ksft_exit_fail_perror("munmap"); + + ksft_test_result(passed, "%s\n", test_name); +} + +static void test_anon_mmap(size_t length, int shift) +{ + const char *test_name = "hugetlb anonymous mmap"; + int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB; + + if (shift) + mmap_flags |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT; + + test_mmap(length, mmap_flags, -1, test_name); +} + +static void test_file_mmap(size_t length, int shift) +{ + const char *test_name = "hugetlb file mmap"; + int mfd_flags = MFD_HUGETLB; + int fd; + + if (shift) + mfd_flags |= (shift & MFD_HUGE_MASK) << MFD_HUGE_SHIFT; + + fd = memfd_create("hugetlb-mmap", mfd_flags); + if (fd < 0) + ksft_exit_fail_perror("memfd_create"); + + test_mmap(length, MAP_SHARED, fd, test_name); + close(fd); +} + +int main(int argc, char **argv) +{ + size_t hugepage_size; + size_t length = LENGTH; + int shift = 0; + + ksft_print_header(); + ksft_set_plan(2); + + if (argc > 1) + length = atol(argv[1]) << 20; + if (argc > 2) + shift = atoi(argv[2]); + + if (shift) { + hugepage_size = (1UL << shift); + ksft_print_msg("%lu kB hugepages\n", 1UL << (shift - 10)); + } else { + hugepage_size = default_huge_page_size(); + ksft_print_msg("Default size hugepages (%lu kB)\n", hugepage_size >> 10); + } + + /* munmap will fail if the length is not page aligned */ + if (hugepage_size > length) + length = hugepage_size; + + ksft_print_msg("Mapping %lu Mbytes\n", (unsigned long)length >> 20); + + test_anon_mmap(length, shift); + test_file_mmap(length, shift); + + ksft_finished(); +} diff --git a/tools/testing/selftests/mm/hugetlb-mremap.c a/tools/testing/selftests/mm/hugetlb-mremap.c new file mode 100664 --- /dev/null +++ a/tools/testing/selftests/mm/hugetlb-mremap.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * hugetlb-mremap: + * + * Example of remapping huge page memory in a user application using the + * mremap system call. The path to a file in a hugetlbfs filesystem must + * be passed as the last argument to this test. The amount of memory used + * by this test in MBs can optionally be passed as an argument. If no memory + * amount is passed, the default amount is 10MB. + * + * To make sure the test triggers pmd sharing and goes through the 'unshare' + * path in the mremap code use 1GB (1024) or more. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include /* Definition of O_* constants */ +#include /* Definition of SYS_* constants */ +#include +#include +#include +#include +#include "kselftest.h" +#include "vm_util.h" + +#define DEFAULT_LENGTH_MB 10UL +#define MB_TO_BYTES(x) (x * 1024 * 1024) + +#define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC) +#define FLAGS (MAP_SHARED | MAP_ANONYMOUS) + +static void check_bytes(char *addr) +{ + ksft_print_msg("First hex is %x\n", *((unsigned int *)addr)); +} + +static void write_bytes(char *addr, size_t len) +{ + unsigned long i; + + for (i = 0; i < len; i++) + *(addr + i) = (char)i; +} + +static int read_bytes(char *addr, size_t len) +{ + unsigned long i; + + check_bytes(addr); + for (i = 0; i < len; i++) + if (*(addr + i) != (char)i) { + ksft_print_msg("Mismatch at %lu\n", i); + return 1; + } + return 0; +} + +static void register_region_with_uffd(char *addr, size_t len) +{ + long uffd; /* userfaultfd file descriptor */ + struct uffdio_api uffdio_api; + + /* Create and enable userfaultfd object. */ + uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); + if (uffd == -1) { + switch (errno) { + case EPERM: + ksft_exit_skip("Insufficient permissions, try running as root.\n"); + break; + case ENOSYS: + ksft_exit_skip("userfaultfd is not supported/not enabled.\n"); + break; + default: + ksft_exit_fail_msg("userfaultfd failed with %s\n", strerror(errno)); + break; + } + } + + uffdio_api.api = UFFD_API; + uffdio_api.features = 0; + if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) + ksft_exit_fail_msg("ioctl-UFFDIO_API: %s\n", strerror(errno)); + + /* Create a private anonymous mapping. The memory will be + * demand-zero paged--that is, not yet allocated. When we + * actually touch the memory, it will be allocated via + * the userfaultfd. + */ + + addr = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (addr == MAP_FAILED) + ksft_exit_fail_msg("mmap: %s\n", strerror(errno)); + + ksft_print_msg("Address returned by mmap() = %p\n", addr); + + /* Register the memory range of the mapping we just created for + * handling by the userfaultfd object. In mode, we request to track + * missing pages (i.e., pages that have not yet been faulted in). + */ + if (uffd_register(uffd, addr, len, true, false, false)) + ksft_exit_fail_msg("ioctl-UFFDIO_REGISTER: %s\n", strerror(errno)); +} + +int main(int argc, char *argv[]) +{ + size_t length = 0; + int ret = 0, fd; + + ksft_print_header(); + ksft_set_plan(1); + + if (argc >= 2 && !strcmp(argv[1], "-h")) + ksft_exit_fail_msg("Usage: %s [length_in_MB]\n", argv[0]); + + /* Read memory length as the first arg if valid, otherwise fallback to + * the default length. + */ + if (argc >= 2) + length = (size_t)atoi(argv[1]); + else + length = DEFAULT_LENGTH_MB; + + length = MB_TO_BYTES(length); + fd = memfd_create(argv[0], MFD_HUGETLB); + if (fd < 0) + ksft_exit_fail_msg("Open failed: %s\n", strerror(errno)); + + /* mmap to a PUD aligned address to hopefully trigger pmd sharing. */ + unsigned long suggested_addr = 0x7eaa40000000; + void *haddr = mmap((void *)suggested_addr, length, PROTECTION, + MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0); + ksft_print_msg("Map haddr: Returned address is %p\n", haddr); + if (haddr == MAP_FAILED) + ksft_exit_fail_msg("mmap1: %s\n", strerror(errno)); + + /* mmap again to a dummy address to hopefully trigger pmd sharing. */ + suggested_addr = 0x7daa40000000; + void *daddr = mmap((void *)suggested_addr, length, PROTECTION, + MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0); + ksft_print_msg("Map daddr: Returned address is %p\n", daddr); + if (daddr == MAP_FAILED) + ksft_exit_fail_msg("mmap3: %s\n", strerror(errno)); + + suggested_addr = 0x7faa40000000; + void *vaddr = + mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0); + ksft_print_msg("Map vaddr: Returned address is %p\n", vaddr); + if (vaddr == MAP_FAILED) + ksft_exit_fail_msg("mmap2: %s\n", strerror(errno)); + + register_region_with_uffd(haddr, length); + + void *addr = mremap(haddr, length, length, + MREMAP_MAYMOVE | MREMAP_FIXED, vaddr); + if (addr == MAP_FAILED) + ksft_exit_fail_msg("mremap: %s\n", strerror(errno)); + + ksft_print_msg("Mremap: Returned address is %p\n", addr); + check_bytes(addr); + write_bytes(addr, length); + ret = read_bytes(addr, length); + + munmap(addr, length); + + addr = mremap(addr, length, length, 0); + if (addr != MAP_FAILED) + ksft_exit_fail_msg("mremap: Expected failure, but call succeeded\n"); + + close(fd); + + ksft_test_result(!ret, "Read same data\n"); + ksft_exit(!ret); +} diff --git a/tools/testing/selftests/mm/hugetlb-shm.c a/tools/testing/selftests/mm/hugetlb-shm.c new file mode 100664 --- /dev/null +++ a/tools/testing/selftests/mm/hugetlb-shm.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * hugetlb-shm: + * + * Example of using huge page memory in a user application using Sys V shared + * memory system calls. In this example the app is requesting 256MB of + * memory that is backed by huge pages. The application uses the flag + * SHM_HUGETLB in the shmget system call to inform the kernel that it is + * requesting huge pages. + * + * Note: The default shared memory limit is quite low on many kernels, + * you may need to increase it via: + * + * echo 268435456 > /proc/sys/kernel/shmmax + * + * This will increase the maximum size per shared memory segment to 256MB. + * The other limit that you will hit eventually is shmall which is the + * total amount of shared memory in pages. To set it to 16GB on a system + * with a 4kB pagesize do: + * + * echo 4194304 > /proc/sys/kernel/shmall + */ + +#include +#include +#include +#include +#include +#include + +#define LENGTH (256UL*1024*1024) + +#define dprintf(x) printf(x) + +int main(void) +{ + int shmid; + unsigned long i; + char *shmaddr; + + shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W); + if (shmid < 0) { + perror("shmget"); + exit(1); + } + printf("shmid: 0x%x\n", shmid); + + shmaddr = shmat(shmid, NULL, 0); + if (shmaddr == (char *)-1) { + perror("Shared memory attach failure"); + shmctl(shmid, IPC_RMID, NULL); + exit(2); + } + printf("shmaddr: %p\n", shmaddr); + + dprintf("Starting the writes:\n"); + for (i = 0; i < LENGTH; i++) { + shmaddr[i] = (char)(i); + if (!(i % (1024 * 1024))) + dprintf("."); + } + dprintf("\n"); + + dprintf("Starting the Check..."); + for (i = 0; i < LENGTH; i++) + if (shmaddr[i] != (char)i) { + printf("\nIndex %lu mismatched\n", i); + exit(3); + } + dprintf("Done.\n"); + + if (shmdt((const void *)shmaddr) != 0) { + perror("Detach failure"); + shmctl(shmid, IPC_RMID, NULL); + exit(4); + } + + shmctl(shmid, IPC_RMID, NULL); + + return 0; +} diff --git a/tools/testing/selftests/mm/hugetlb-vmemmap.c a/tools/testing/selftests/mm/hugetlb-vmemmap.c new file mode 100664 --- /dev/null +++ a/tools/testing/selftests/mm/hugetlb-vmemmap.c @@ -0,0 +1,134 @@ +// 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 +#include +#include +#include +#include +#include "vm_util.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); + printf("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); + printf("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; + + pagesize = psize(); + maplength = default_huge_page_size(); + if (!maplength) { + printf("Unable to determine huge page size\n"); + exit(1); + } + + addr = mmap(NULL, maplength, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0); + if (addr == MAP_FAILED) { + perror("mmap"); + exit(1); + } + + /* Trigger allocation of HugeTLB page. */ + write_bytes(addr, maplength); + + pfn = virt_to_pfn(addr); + if (pfn == -1UL) { + munmap(addr, maplength); + perror("virt_to_pfn"); + exit(1); + } + + printf("Returned address is %p whose pfn is %lx\n", addr, pfn); + + if (check_page_flags(pfn) < 0) { + munmap(addr, maplength); + perror("check_page_flags"); + exit(1); + } + + /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */ + if (munmap(addr, maplength)) { + perror("munmap"); + exit(1); + } + + return 0; +} --- a/tools/testing/selftests/mm/Makefile~selftests-mm-rename-hugepage-tests-to-hugetlb +++ a/tools/testing/selftests/mm/Makefile @@ -61,12 +61,12 @@ TEST_GEN_FILES += gup_longterm TEST_GEN_FILES += gup_test TEST_GEN_FILES += hmm-tests TEST_GEN_FILES += hugetlb-madvise +TEST_GEN_FILES += hugetlb-mmap +TEST_GEN_FILES += hugetlb-mremap TEST_GEN_FILES += hugetlb-read-hwpoison +TEST_GEN_FILES += hugetlb-shm TEST_GEN_FILES += hugetlb-soft-offline -TEST_GEN_FILES += hugepage-mmap -TEST_GEN_FILES += hugepage-mremap -TEST_GEN_FILES += hugepage-shm -TEST_GEN_FILES += hugepage-vmemmap +TEST_GEN_FILES += hugetlb-vmemmap TEST_GEN_FILES += khugepaged TEST_GEN_FILES += madv_populate TEST_GEN_FILES += map_fixed_noreplace --- a/tools/testing/selftests/mm/run_vmtests.sh~selftests-mm-rename-hugepage-tests-to-hugetlb +++ a/tools/testing/selftests/mm/run_vmtests.sh @@ -282,18 +282,18 @@ run_test() { echo "TAP version 13" | tap_output -CATEGORY="hugetlb" run_test ./hugepage-mmap +CATEGORY="hugetlb" run_test ./hugetlb-mmap shmmax=$(cat /proc/sys/kernel/shmmax) shmall=$(cat /proc/sys/kernel/shmall) echo 268435456 > /proc/sys/kernel/shmmax echo 4194304 > /proc/sys/kernel/shmall -CATEGORY="hugetlb" run_test ./hugepage-shm +CATEGORY="hugetlb" run_test ./hugetlb-shm echo "$shmmax" > /proc/sys/kernel/shmmax echo "$shmall" > /proc/sys/kernel/shmall -CATEGORY="hugetlb" run_test ./hugepage-mremap -CATEGORY="hugetlb" run_test ./hugepage-vmemmap +CATEGORY="hugetlb" run_test ./hugetlb-mremap +CATEGORY="hugetlb" run_test ./hugetlb-vmemmap CATEGORY="hugetlb" run_test ./hugetlb-madvise CATEGORY="hugetlb" run_test ./hugetlb_dio _ Patches currently in -mm which might be from rppt@kernel.org are