Debian Codegraph @

tests/mremap-expand-slice-collision.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2009 David Gibson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26
27 #include <hugetlbfs.h>
28
29 #include "hugetests.h"
30
31 #define RANDOM_CONSTANT 0x1234ABCD
32
33 unsigned long slice_boundary;
34 long hpage_size, page_size;
35
36 void init_slice_boundary(int fd)
37 {
38 unsigned long slice_size;
39 void *p, *heap;
40 int i, rc;
41 #if defined(__LP64__) && !defined(__aarch64__)
42 /* powerpc: 1TB slices starting at 1 TB */
43 slice_boundary = 0x10000000000;
44 slice_size = 0x10000000000;
45 #else
46 /* powerpc: 256MB slices up to 4GB */
47 slice_boundary = 0x00000000;
48 slice_size = 0x10000000;
49 #endif
50
51 /* dummy malloc so we know where is heap */
52 heap = malloc(1);
53 free(heap);
54
55 /* Find 2 neighbour slices with couple huge pages free
56 * around slice boundary.
57 * 16 is the maximum number of slices (low/high) */
58 for (i = 0; i < 16-1; i++) {
59 slice_boundary += slice_size;
60 p = mmap((void *)(slice_boundary-2*hpage_size), 4*hpage_size,
61 PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0);
62 if (p == MAP_FAILED) {
63 verbose_printf("can't use slice_boundary: 0x%lx\n",
64 slice_boundary);
65 } else {
66 rc = munmap(p, 4*hpage_size);
67 if (rc != 0)
68 FAIL("munmap(p1): %s", strerror(errno));
69 break;
70 }
71 }
72
73 if (p == MAP_FAILED)
74 FAIL("couldn't find 2 free neighbour slices");
75 verbose_printf("using slice_boundary: 0x%lx\n", slice_boundary);
76 }
77
78 void do_readback(void *p, size_t size, const char *stage)
79 {
80 unsigned int *q = p;
81 int i;
82
83 verbose_printf("do_readback(%p, 0x%lx, \"%s\")\n", p,
84 (unsigned long)size, stage);
85
86 for (i = 0; i < (size / sizeof(*q)); i++) {
87 q[i] = RANDOM_CONSTANT ^ i;
88 }
89
90 for (i = 0; i < (size / sizeof(*q)); i++) {
91 if (q[i] != (RANDOM_CONSTANT ^ i))
92 FAIL("Stage \"%s\": Mismatch at offset 0x%x: 0x%x instead of 0x%x",
93 stage, i, q[i], RANDOM_CONSTANT ^ i);
94 }
95 }
96
97 void do_remap(int fd, void *target)
98 {
99 void *a, *b;
100 int rc;
101
102 a = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
103 if (a == MAP_FAILED)
104 FAIL("mmap(huge page): %s", strerror(errno));
105
106 verbose_printf("Huge base mapping at %p\n", a);
107
108 do_readback(a, hpage_size, "base huge");
109
110 verbose_printf("Attempting mremap(MAYMOVE|FIXED) to %p...", target);
111
112 b = mremap(a, hpage_size, hpage_size, MREMAP_MAYMOVE | MREMAP_FIXED,
113 target);
114
115 if (b != MAP_FAILED) {
116 verbose_printf("testing...");
117 do_readback(b, hpage_size, "remapped");
118 verbose_printf("ok\n");
119 } else {
120 verbose_printf("disallowed (%s)\n", strerror(errno));
121 }
122
123 rc = munmap(b, hpage_size);
124 if (rc != 0)
125 FAIL("munmap(after remap): %s", strerror(errno));
126 }
127
128 int main(int argc, char *argv[])
129 {
130 int fd, rc;
131 void *p, *q, *r;
132
133 test_init(argc, argv);
134
135 hpage_size = check_hugepagesize();
136 page_size = getpagesize();
137
138
139 fd = hugetlbfs_unlinked_fd();
140 if (fd < 0)
141 FAIL("hugetlbfs_unlinked_fd()");
142 init_slice_boundary(fd);
143
144 /* First, hugepages above, normal below */
145 p = mmap((void *)(slice_boundary + hpage_size), hpage_size,
146 PROT_READ | PROT_WRITE,
147 MAP_SHARED | MAP_FIXED, fd, 0);
148 if (p == MAP_FAILED)
149 FAIL("mmap(huge above): %s", strerror(errno));
150
151 do_readback(p, hpage_size, "huge above");
152
153 q = mmap((void *)(slice_boundary - page_size), page_size,
154 PROT_READ | PROT_WRITE,
155 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
156 if (q == MAP_FAILED)
157 FAIL("mmap(normal below): %s", strerror(errno));
158
159 do_readback(q, page_size, "normal below");
160
161 verbose_printf("Attempting to remap...");
162
163 r = mremap(q, page_size, 2*page_size, 0);
164 if (r == MAP_FAILED) {
165 verbose_printf("disallowed\n");
166 rc = munmap(q, page_size);
167 if (rc != 0)
168 FAIL("munmap(normal below): %s", strerror(errno));
169 } else {
170 if (r != q)
171 FAIL("mremap() moved without MREMAP_MAYMOVE!?");
172
173 verbose_printf("testing...");
174 do_readback(q, 2*page_size, "normal below expanded");
175 rc = munmap(q, 2*page_size);
176 if (rc != 0)
177 FAIL("munmap(normal below expanded): %s", strerror(errno));
178 }
179
180 rc = munmap(p, hpage_size);
181 if (rc != 0)
182 FAIL("munmap(huge above)");
183
184 /* Next, normal pages above, huge below */
185 p = mmap((void *)(slice_boundary + hpage_size), page_size,
186 PROT_READ|PROT_WRITE,
187 MAP_SHARED | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
188 if (p == MAP_FAILED)
189 FAIL("mmap(normal above): %s", strerror(errno));
190
191 do_readback(p, page_size, "normal above");
192
193 q = mmap((void *)(slice_boundary - hpage_size),
194 hpage_size, PROT_READ | PROT_WRITE,
195 MAP_SHARED | MAP_FIXED, fd, 0);
196 if (q == MAP_FAILED)
197 FAIL("mmap(huge below): %s", strerror(errno));
198
199 do_readback(q, hpage_size, "huge below");
200
201 verbose_printf("Attempting to remap...");
202
203 r = mremap(q, hpage_size, 2*hpage_size, 0);
204 if (r == MAP_FAILED) {
205 verbose_printf("disallowed\n");
206 rc = munmap(q, hpage_size);
207 if (rc != 0)
208 FAIL("munmap(huge below): %s", strerror(errno));
209 } else {
210 if (r != q)
211 FAIL("mremap() moved without MREMAP_MAYMOVE!?");
212
213 verbose_printf("testing...");
214 do_readback(q, 2*hpage_size, "huge below expanded");
215 rc = munmap(q, 2*hpage_size);
216 if (rc != 0)
217 FAIL("munmap(huge below expanded): %s", strerror(errno));
218 }
219
220 rc = munmap(p, page_size);
221 if (rc != 0)
222 FAIL("munmap(normal above)");
223
224
225 PASS();
226 }