drm/panel: panel-himax-hx83102: support for csot-pna957qt1-1 MIPI-DSI panel
[drm/drm-misc.git] / tools / testing / selftests / mm / hugepage-mremap.c
blobada9156cc497b3644487ee22dcadfa9b116c8aba
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * hugepage-mremap:
5 * Example of remapping huge page memory in a user application using the
6 * mremap system call. The path to a file in a hugetlbfs filesystem must
7 * be passed as the last argument to this test. The amount of memory used
8 * by this test in MBs can optionally be passed as an argument. If no memory
9 * amount is passed, the default amount is 10MB.
11 * To make sure the test triggers pmd sharing and goes through the 'unshare'
12 * path in the mremap code use 1GB (1024) or more.
15 #define _GNU_SOURCE
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <asm-generic/unistd.h>
19 #include <sys/mman.h>
20 #include <errno.h>
21 #include <fcntl.h> /* Definition of O_* constants */
22 #include <sys/syscall.h> /* Definition of SYS_* constants */
23 #include <linux/userfaultfd.h>
24 #include <sys/ioctl.h>
25 #include <string.h>
26 #include <stdbool.h>
27 #include "../kselftest.h"
28 #include "vm_util.h"
30 #define DEFAULT_LENGTH_MB 10UL
31 #define MB_TO_BYTES(x) (x * 1024 * 1024)
33 #define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC)
34 #define FLAGS (MAP_SHARED | MAP_ANONYMOUS)
36 static void check_bytes(char *addr)
38 ksft_print_msg("First hex is %x\n", *((unsigned int *)addr));
41 static void write_bytes(char *addr, size_t len)
43 unsigned long i;
45 for (i = 0; i < len; i++)
46 *(addr + i) = (char)i;
49 static int read_bytes(char *addr, size_t len)
51 unsigned long i;
53 check_bytes(addr);
54 for (i = 0; i < len; i++)
55 if (*(addr + i) != (char)i) {
56 ksft_print_msg("Mismatch at %lu\n", i);
57 return 1;
59 return 0;
62 static void register_region_with_uffd(char *addr, size_t len)
64 long uffd; /* userfaultfd file descriptor */
65 struct uffdio_api uffdio_api;
67 /* Create and enable userfaultfd object. */
69 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
70 if (uffd == -1)
71 ksft_exit_fail_msg("userfaultfd: %s\n", strerror(errno));
73 uffdio_api.api = UFFD_API;
74 uffdio_api.features = 0;
75 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
76 ksft_exit_fail_msg("ioctl-UFFDIO_API: %s\n", strerror(errno));
78 /* Create a private anonymous mapping. The memory will be
79 * demand-zero paged--that is, not yet allocated. When we
80 * actually touch the memory, it will be allocated via
81 * the userfaultfd.
84 addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
85 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
86 if (addr == MAP_FAILED)
87 ksft_exit_fail_msg("mmap: %s\n", strerror(errno));
89 ksft_print_msg("Address returned by mmap() = %p\n", addr);
91 /* Register the memory range of the mapping we just created for
92 * handling by the userfaultfd object. In mode, we request to track
93 * missing pages (i.e., pages that have not yet been faulted in).
95 if (uffd_register(uffd, addr, len, true, false, false))
96 ksft_exit_fail_msg("ioctl-UFFDIO_REGISTER: %s\n", strerror(errno));
99 int main(int argc, char *argv[])
101 size_t length = 0;
102 int ret = 0, fd;
104 ksft_print_header();
105 ksft_set_plan(1);
107 if (argc >= 2 && !strcmp(argv[1], "-h"))
108 ksft_exit_fail_msg("Usage: %s [length_in_MB]\n", argv[0]);
110 /* Read memory length as the first arg if valid, otherwise fallback to
111 * the default length.
113 if (argc >= 2)
114 length = (size_t)atoi(argv[1]);
115 else
116 length = DEFAULT_LENGTH_MB;
118 length = MB_TO_BYTES(length);
119 fd = memfd_create(argv[0], MFD_HUGETLB);
120 if (fd < 0)
121 ksft_exit_fail_msg("Open failed: %s\n", strerror(errno));
123 /* mmap to a PUD aligned address to hopefully trigger pmd sharing. */
124 unsigned long suggested_addr = 0x7eaa40000000;
125 void *haddr = mmap((void *)suggested_addr, length, PROTECTION,
126 MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
127 ksft_print_msg("Map haddr: Returned address is %p\n", haddr);
128 if (haddr == MAP_FAILED)
129 ksft_exit_fail_msg("mmap1: %s\n", strerror(errno));
131 /* mmap again to a dummy address to hopefully trigger pmd sharing. */
132 suggested_addr = 0x7daa40000000;
133 void *daddr = mmap((void *)suggested_addr, length, PROTECTION,
134 MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
135 ksft_print_msg("Map daddr: Returned address is %p\n", daddr);
136 if (daddr == MAP_FAILED)
137 ksft_exit_fail_msg("mmap3: %s\n", strerror(errno));
139 suggested_addr = 0x7faa40000000;
140 void *vaddr =
141 mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0);
142 ksft_print_msg("Map vaddr: Returned address is %p\n", vaddr);
143 if (vaddr == MAP_FAILED)
144 ksft_exit_fail_msg("mmap2: %s\n", strerror(errno));
146 register_region_with_uffd(haddr, length);
148 void *addr = mremap(haddr, length, length,
149 MREMAP_MAYMOVE | MREMAP_FIXED, vaddr);
150 if (addr == MAP_FAILED)
151 ksft_exit_fail_msg("mremap: %s\n", strerror(errno));
153 ksft_print_msg("Mremap: Returned address is %p\n", addr);
154 check_bytes(addr);
155 write_bytes(addr, length);
156 ret = read_bytes(addr, length);
158 munmap(addr, length);
160 addr = mremap(addr, length, length, 0);
161 if (addr != MAP_FAILED)
162 ksft_exit_fail_msg("mremap: Expected failure, but call succeeded\n");
164 close(fd);
166 ksft_test_result(!ret, "Read same data\n");
167 ksft_exit(!ret);