xref: /linux/tools/testing/selftests/mm/virtual_address_range.c (revision ef9226cd56b718c79184a3466d32984a51cb449c)
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/mman.h>
14 #include <sys/time.h>
15 #include "../kselftest.h"
16 
17 /*
18  * Maximum address range mapped with a single mmap()
19  * call is little bit more than 1GB. Hence 1GB is
20  * chosen as the single chunk size for address space
21  * mapping.
22  */
23 
24 #define SZ_1GB	(1024 * 1024 * 1024UL)
25 #define SZ_1TB	(1024 * 1024 * 1024 * 1024UL)
26 
27 #define MAP_CHUNK_SIZE	SZ_1GB
28 
29 /*
30  * Address space till 128TB is mapped without any hint
31  * and is enabled by default. Address space beyond 128TB
32  * till 512TB is obtained by passing hint address as the
33  * first argument into mmap() system call.
34  *
35  * The process heap address space is divided into two
36  * different areas one below 128TB and one above 128TB
37  * till it reaches 512TB. One with size 128TB and the
38  * other being 384TB.
39  *
40  * On Arm64 the address space is 256TB and support for
41  * high mappings up to 4PB virtual address space has
42  * been added.
43  */
44 
45 #define NR_CHUNKS_128TB   ((128 * SZ_1TB) / MAP_CHUNK_SIZE) /* Number of chunks for 128TB */
46 #define NR_CHUNKS_256TB   (NR_CHUNKS_128TB * 2UL)
47 #define NR_CHUNKS_384TB   (NR_CHUNKS_128TB * 3UL)
48 #define NR_CHUNKS_3840TB  (NR_CHUNKS_128TB * 30UL)
49 
50 #define ADDR_MARK_128TB  (1UL << 47) /* First address beyond 128TB */
51 #define ADDR_MARK_256TB  (1UL << 48) /* First address beyond 256TB */
52 
53 #ifdef __aarch64__
54 #define HIGH_ADDR_MARK  ADDR_MARK_256TB
55 #define HIGH_ADDR_SHIFT 49
56 #define NR_CHUNKS_LOW   NR_CHUNKS_256TB
57 #define NR_CHUNKS_HIGH  NR_CHUNKS_3840TB
58 #else
59 #define HIGH_ADDR_MARK  ADDR_MARK_128TB
60 #define HIGH_ADDR_SHIFT 48
61 #define NR_CHUNKS_LOW   NR_CHUNKS_128TB
62 #define NR_CHUNKS_HIGH  NR_CHUNKS_384TB
63 #endif
64 
65 static char *hind_addr(void)
66 {
67 	int bits = HIGH_ADDR_SHIFT + rand() % (63 - HIGH_ADDR_SHIFT);
68 
69 	return (char *) (1UL << bits);
70 }
71 
72 static void validate_addr(char *ptr, int high_addr)
73 {
74 	unsigned long addr = (unsigned long) ptr;
75 
76 	if (high_addr && addr < HIGH_ADDR_MARK)
77 		ksft_exit_fail_msg("Bad address %lx\n", addr);
78 
79 	if (addr > HIGH_ADDR_MARK)
80 		ksft_exit_fail_msg("Bad address %lx\n", addr);
81 }
82 
83 static int validate_lower_address_hint(void)
84 {
85 	char *ptr;
86 
87 	ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
88 			PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
89 
90 	if (ptr == MAP_FAILED)
91 		return 0;
92 
93 	return 1;
94 }
95 
96 int main(int argc, char *argv[])
97 {
98 	char *ptr[NR_CHUNKS_LOW];
99 	char **hptr;
100 	char *hint;
101 	unsigned long i, lchunks, hchunks;
102 
103 	ksft_print_header();
104 	ksft_set_plan(1);
105 
106 	for (i = 0; i < NR_CHUNKS_LOW; i++) {
107 		ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
108 					MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
109 
110 		if (ptr[i] == MAP_FAILED) {
111 			if (validate_lower_address_hint()) {
112 				ksft_test_result_skip("Memory constraint not fulfilled\n");
113 				ksft_finished();
114 			}
115 			break;
116 		}
117 
118 		validate_addr(ptr[i], 0);
119 	}
120 	lchunks = i;
121 	hptr = (char **) calloc(NR_CHUNKS_HIGH, sizeof(char *));
122 	if (hptr == NULL) {
123 		ksft_test_result_skip("Memory constraint not fulfilled\n");
124 		ksft_finished();
125 	}
126 
127 	for (i = 0; i < NR_CHUNKS_HIGH; i++) {
128 		hint = hind_addr();
129 		hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
130 					MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
131 
132 		if (hptr[i] == MAP_FAILED)
133 			break;
134 
135 		validate_addr(hptr[i], 1);
136 	}
137 	hchunks = i;
138 
139 	for (i = 0; i < lchunks; i++)
140 		munmap(ptr[i], MAP_CHUNK_SIZE);
141 
142 	for (i = 0; i < hchunks; i++)
143 		munmap(hptr[i], MAP_CHUNK_SIZE);
144 
145 	free(hptr);
146 
147 	ksft_test_result_pass("Test\n");
148 	ksft_finished();
149 }
150