1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2017, Anshuman Khandual, IBM Corp.
4 *
5 * Works on architectures which support 128TB virtual
6 * address range and beyond.
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <sys/prctl.h>
14 #include <sys/mman.h>
15 #include <sys/time.h>
16 #include <fcntl.h>
17
18 #include "vm_util.h"
19 #include "../kselftest.h"
20
21 /*
22 * Maximum address range mapped with a single mmap()
23 * call is little bit more than 1GB. Hence 1GB is
24 * chosen as the single chunk size for address space
25 * mapping.
26 */
27
28 #define SZ_1GB (1024 * 1024 * 1024UL)
29 #define SZ_1TB (1024 * 1024 * 1024 * 1024UL)
30
31 #define MAP_CHUNK_SIZE SZ_1GB
32
33 /*
34 * Address space till 128TB is mapped without any hint
35 * and is enabled by default. Address space beyond 128TB
36 * till 512TB is obtained by passing hint address as the
37 * first argument into mmap() system call.
38 *
39 * The process heap address space is divided into two
40 * different areas one below 128TB and one above 128TB
41 * till it reaches 512TB. One with size 128TB and the
42 * other being 384TB.
43 *
44 * On Arm64 the address space is 256TB and support for
45 * high mappings up to 4PB virtual address space has
46 * been added.
47 */
48
49 #define NR_CHUNKS_128TB ((128 * SZ_1TB) / MAP_CHUNK_SIZE) /* Number of chunks for 128TB */
50 #define NR_CHUNKS_256TB (NR_CHUNKS_128TB * 2UL)
51 #define NR_CHUNKS_384TB (NR_CHUNKS_128TB * 3UL)
52 #define NR_CHUNKS_3840TB (NR_CHUNKS_128TB * 30UL)
53
54 #define ADDR_MARK_128TB (1UL << 47) /* First address beyond 128TB */
55 #define ADDR_MARK_256TB (1UL << 48) /* First address beyond 256TB */
56
57 #ifdef __aarch64__
58 #define HIGH_ADDR_MARK ADDR_MARK_256TB
59 #define HIGH_ADDR_SHIFT 49
60 #define NR_CHUNKS_LOW NR_CHUNKS_256TB
61 #define NR_CHUNKS_HIGH NR_CHUNKS_3840TB
62 #else
63 #define HIGH_ADDR_MARK ADDR_MARK_128TB
64 #define HIGH_ADDR_SHIFT 48
65 #define NR_CHUNKS_LOW NR_CHUNKS_128TB
66 #define NR_CHUNKS_HIGH NR_CHUNKS_384TB
67 #endif
68
hint_addr(void)69 static char *hint_addr(void)
70 {
71 int bits = HIGH_ADDR_SHIFT + rand() % (63 - HIGH_ADDR_SHIFT);
72
73 return (char *) (1UL << bits);
74 }
75
validate_addr(char * ptr,int high_addr)76 static void validate_addr(char *ptr, int high_addr)
77 {
78 unsigned long addr = (unsigned long) ptr;
79
80 if (high_addr) {
81 if (addr < HIGH_ADDR_MARK)
82 ksft_exit_fail_msg("Bad address %lx\n", addr);
83 return;
84 }
85
86 if (addr > HIGH_ADDR_MARK)
87 ksft_exit_fail_msg("Bad address %lx\n", addr);
88 }
89
mark_range(char * ptr,size_t size)90 static void mark_range(char *ptr, size_t size)
91 {
92 if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, "virtual_address_range") == -1) {
93 if (errno == EINVAL) {
94 /* Depends on CONFIG_ANON_VMA_NAME */
95 ksft_test_result_skip("prctl(PR_SET_VMA_ANON_NAME) not supported\n");
96 ksft_finished();
97 } else {
98 ksft_exit_fail_perror("prctl(PR_SET_VMA_ANON_NAME) failed\n");
99 }
100 }
101 }
102
is_marked_vma(const char * vma_name)103 static int is_marked_vma(const char *vma_name)
104 {
105 return vma_name && !strcmp(vma_name, "[anon:virtual_address_range]\n");
106 }
107
validate_lower_address_hint(void)108 static int validate_lower_address_hint(void)
109 {
110 char *ptr;
111
112 ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
113 PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
114
115 if (ptr == MAP_FAILED)
116 return 0;
117
118 return 1;
119 }
120
validate_complete_va_space(void)121 static int validate_complete_va_space(void)
122 {
123 unsigned long start_addr, end_addr, prev_end_addr;
124 char line[400];
125 char prot[6];
126 FILE *file;
127 int fd;
128
129 fd = open("va_dump", O_CREAT | O_WRONLY, 0600);
130 unlink("va_dump");
131 if (fd < 0) {
132 ksft_test_result_skip("cannot create or open dump file\n");
133 ksft_finished();
134 }
135
136 file = fopen("/proc/self/maps", "r");
137 if (file == NULL)
138 ksft_exit_fail_msg("cannot open /proc/self/maps\n");
139
140 prev_end_addr = 0;
141 while (fgets(line, sizeof(line), file)) {
142 const char *vma_name = NULL;
143 int vma_name_start = 0;
144 unsigned long hop;
145
146 if (sscanf(line, "%lx-%lx %4s %*s %*s %*s %n",
147 &start_addr, &end_addr, prot, &vma_name_start) != 3)
148 ksft_exit_fail_msg("cannot parse /proc/self/maps\n");
149
150 if (vma_name_start)
151 vma_name = line + vma_name_start;
152
153 /* end of userspace mappings; ignore vsyscall mapping */
154 if (start_addr & (1UL << 63))
155 return 0;
156
157 /* /proc/self/maps must have gaps less than MAP_CHUNK_SIZE */
158 if (start_addr - prev_end_addr >= MAP_CHUNK_SIZE)
159 return 1;
160
161 prev_end_addr = end_addr;
162
163 if (prot[0] != 'r')
164 continue;
165
166 if (check_vmflag_io((void *)start_addr))
167 continue;
168
169 /*
170 * Confirm whether MAP_CHUNK_SIZE chunk can be found or not.
171 * If write succeeds, no need to check MAP_CHUNK_SIZE - 1
172 * addresses after that. If the address was not held by this
173 * process, write would fail with errno set to EFAULT.
174 * Anyways, if write returns anything apart from 1, exit the
175 * program since that would mean a bug in /proc/self/maps.
176 */
177 hop = 0;
178 while (start_addr + hop < end_addr) {
179 if (write(fd, (void *)(start_addr + hop), 1) != 1)
180 return 1;
181 lseek(fd, 0, SEEK_SET);
182
183 if (is_marked_vma(vma_name))
184 munmap((char *)(start_addr + hop), MAP_CHUNK_SIZE);
185
186 hop += MAP_CHUNK_SIZE;
187 }
188 }
189 return 0;
190 }
191
main(int argc,char * argv[])192 int main(int argc, char *argv[])
193 {
194 char *ptr[NR_CHUNKS_LOW];
195 char **hptr;
196 char *hint;
197 unsigned long i, lchunks, hchunks;
198
199 ksft_print_header();
200 ksft_set_plan(1);
201
202 for (i = 0; i < NR_CHUNKS_LOW; i++) {
203 ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ,
204 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
205
206 if (ptr[i] == MAP_FAILED) {
207 if (validate_lower_address_hint())
208 ksft_exit_fail_msg("mmap unexpectedly succeeded with hint\n");
209 break;
210 }
211
212 mark_range(ptr[i], MAP_CHUNK_SIZE);
213 validate_addr(ptr[i], 0);
214 }
215 lchunks = i;
216 hptr = (char **) calloc(NR_CHUNKS_HIGH, sizeof(char *));
217 if (hptr == NULL) {
218 ksft_test_result_skip("Memory constraint not fulfilled\n");
219 ksft_finished();
220 }
221
222 for (i = 0; i < NR_CHUNKS_HIGH; i++) {
223 hint = hint_addr();
224 hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ,
225 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
226
227 if (hptr[i] == MAP_FAILED)
228 break;
229
230 mark_range(ptr[i], MAP_CHUNK_SIZE);
231 validate_addr(hptr[i], 1);
232 }
233 hchunks = i;
234 if (validate_complete_va_space()) {
235 ksft_test_result_fail("BUG in mmap() or /proc/self/maps\n");
236 ksft_finished();
237 }
238
239 for (i = 0; i < lchunks; i++)
240 munmap(ptr[i], MAP_CHUNK_SIZE);
241
242 for (i = 0; i < hchunks; i++)
243 munmap(hptr[i], MAP_CHUNK_SIZE);
244
245 free(hptr);
246
247 ksft_test_result_pass("Test\n");
248 ksft_finished();
249 }
250