]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/testing/selftests/vm/virtual_address_range.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[karo-tx-linux.git] / tools / testing / selftests / vm / virtual_address_range.c
1 /*
2  * Copyright 2017, Anshuman Khandual, IBM Corp.
3  * Licensed under GPLv2.
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 <numaif.h>
14 #include <sys/mman.h>
15 #include <sys/time.h>
16
17 /*
18  * Maximum address range mapped with a single mmap()
19  * call is little bit more than 16GB. Hence 16GB is
20  * chosen as the single chunk size for address space
21  * mapping.
22  */
23 #define MAP_CHUNK_SIZE   17179869184UL /* 16GB */
24
25 /*
26  * Address space till 128TB is mapped without any hint
27  * and is enabled by default. Address space beyond 128TB
28  * till 512TB is obtained by passing hint address as the
29  * first argument into mmap() system call.
30  *
31  * The process heap address space is divided into two
32  * different areas one below 128TB and one above 128TB
33  * till it reaches 512TB. One with size 128TB and the
34  * other being 384TB.
35  */
36 #define NR_CHUNKS_128TB   8192UL /* Number of 16GB chunks for 128TB */
37 #define NR_CHUNKS_384TB  24576UL /* Number of 16GB chunks for 384TB */
38
39 #define ADDR_MARK_128TB  (1UL << 47) /* First address beyond 128TB */
40
41 static char *hind_addr(void)
42 {
43         int bits = 48 + rand() % 15;
44
45         return (char *) (1UL << bits);
46 }
47
48 static int validate_addr(char *ptr, int high_addr)
49 {
50         unsigned long addr = (unsigned long) ptr;
51
52         if (high_addr) {
53                 if (addr < ADDR_MARK_128TB) {
54                         printf("Bad address %lx\n", addr);
55                         return 1;
56                 }
57                 return 0;
58         }
59
60         if (addr > ADDR_MARK_128TB) {
61                 printf("Bad address %lx\n", addr);
62                 return 1;
63         }
64         return 0;
65 }
66
67 static int validate_lower_address_hint(void)
68 {
69         char *ptr;
70
71         ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
72                         PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
73
74         if (ptr == MAP_FAILED)
75                 return 0;
76
77         return 1;
78 }
79
80 int main(int argc, char *argv[])
81 {
82         char *ptr[NR_CHUNKS_128TB];
83         char *hptr[NR_CHUNKS_384TB];
84         char *hint;
85         unsigned long i, lchunks, hchunks;
86
87         for (i = 0; i < NR_CHUNKS_128TB; i++) {
88                 ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
89                                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
90
91                 if (ptr[i] == MAP_FAILED) {
92                         if (validate_lower_address_hint())
93                                 return 1;
94                         break;
95                 }
96
97                 if (validate_addr(ptr[i], 0))
98                         return 1;
99         }
100         lchunks = i;
101
102         for (i = 0; i < NR_CHUNKS_384TB; i++) {
103                 hint = hind_addr();
104                 hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
105                                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
106
107                 if (hptr[i] == MAP_FAILED)
108                         break;
109
110                 if (validate_addr(hptr[i], 1))
111                         return 1;
112         }
113         hchunks = i;
114
115         for (i = 0; i < lchunks; i++)
116                 munmap(ptr[i], MAP_CHUNK_SIZE);
117
118         for (i = 0; i < hchunks; i++)
119                 munmap(hptr[i], MAP_CHUNK_SIZE);
120
121         return 0;
122 }