Debian Codegraph @

morecore.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, 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 <malloc.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <errno.h>
28 #include <dlfcn.h>
29 #include <string.h>
30 #include <fcntl.h>
31
32 #include "hugetlbfs.h"
33
34 #include "libhugetlbfs_internal.h"
35
36 #ifndef HAS_MORECORE
37 void hugetlbfs_setup_morecore(void)
38 {
39 INFO("Not setting up morecore because it's not available (see issue #52).\n");
40 }
41 #else
42
43 static int heap_fd;
44
45 static void *heapbase;
46 static void *heaptop;
47 static long mapsize;
48 static long hpage_size;
49
50 static long hugetlbfs_next_addr(long addr)
51 {
52 #if defined(__powerpc64__)
53 return ALIGN(addr, 1L << SLICE_HIGH_SHIFT);
54 #elif defined(__powerpc__) && !defined(PPC_NO_SEGMENTS)
55 return ALIGN(addr, 1L << SLICE_LOW_SHIFT);
56 #elif defined(__ia64__)
57 if (addr < (1UL << SLICE_HIGH_SHIFT))
58 return ALIGN(addr, 1UL << SLICE_HIGH_SHIFT);
59 else
60 return ALIGN(addr, hpage_size);
61 #else
62 return ALIGN(addr, hpage_size);
63 #endif
64 }
65
66 /*
67 * Our plan is to ask for pages 'roughly' at the BASE. We expect and
68 * require the kernel to offer us sequential pages from wherever it
69 * first gave us a page. If it does not do so, we return the page and
70 * pretend there are none this covers us for the case where another
71 * map is in the way. This is required because 'morecore' must have
72 * 'sbrk' semantics, ie. return sequential, contigious memory blocks.
73 * Luckily, if it does not do so and we error out malloc will happily
74 * go back to small pages and use mmap to get them. Hurrah.
75 */
76 static void *hugetlbfs_morecore(ptrdiff_t increment)
77 {
78 int ret;
79 void *p;
80 long delta;
81 int mmap_reserve = __hugetlb_opts.no_reserve ? MAP_NORESERVE : 0;
82 int mmap_hugetlb = 0;
83 int using_default_pagesize =
84 (hpage_size == kernel_default_hugepage_size());
85
86 INFO("hugetlbfs_morecore(%ld) = ...\n", (long)increment);
87
88 /*
89 * how much to grow the heap by =
90 * (size of heap) + malloc request - mmap'd space
91 */
92 delta = (heaptop-heapbase) + increment - mapsize;
93
94 INFO("heapbase = %p, heaptop = %p, mapsize = %lx, delta=%ld\n",
95 heapbase, heaptop, mapsize, delta);
96
97 /* align to multiple of hugepagesize. */
98 delta = ALIGN(delta, hpage_size);
99
100 #ifdef MAP_HUGETLB
101 mmap_hugetlb = MAP_HUGETLB;
102 #endif
103
104 if (delta > 0) {
105 /* growing the heap */
106
107 INFO("Attempting to map %ld bytes\n", delta);
108
109 /* map in (extend) more of the file at the end of our last map */
110 if (__hugetlb_opts.map_hugetlb && using_default_pagesize)
111 p = mmap(heapbase + mapsize, delta, PROT_READ|PROT_WRITE,
112 mmap_hugetlb|MAP_ANONYMOUS|MAP_PRIVATE|mmap_reserve,
113 heap_fd, mapsize);
114 else
115 p = mmap(heapbase + mapsize, delta, PROT_READ|PROT_WRITE,
116 MAP_PRIVATE|mmap_reserve, heap_fd, mapsize);
117
118 if (p == MAP_FAILED) {
119 WARNING("New heap segment map at %p failed: %s\n",
120 heapbase+mapsize, strerror(errno));
121 return NULL;
122 }
123
124 /* if this is the first map */
125 if (! mapsize) {
126 if (heapbase && (heapbase != p)) {
127 WARNING("Heap originates at %p instead of %p\n",
128 p, heapbase);
129 if (__hugetlbfs_debug)
130 dump_proc_pid_maps();
131 }
132 /* then setup the heap variables */
133 heapbase = heaptop = p;
134 } else if (p != (heapbase + mapsize)) {
135 /* Couldn't get the mapping where we wanted */
136 munmap(p, delta);
137 WARNING("New heap segment mapped at %p instead of %p\n",
138 p, heapbase + mapsize);
139 if (__hugetlbfs_debug)
140 dump_proc_pid_maps();
141 return NULL;
142 }
143
144 /* Fault the region to ensure accesses succeed */
145 if (hugetlbfs_prefault(p, delta) != 0) {
146 munmap(p, delta);
147 return NULL;
148 }
149
150 /* we now have mmap'd further */
151 mapsize += delta;
152 } else if (delta < 0) {
153 /* shrinking the heap */
154
155 if (!__hugetlb_opts.shrink_ok) {
156 /* shouldn't ever get here */
157 WARNING("Heap shrinking is turned off\n");
158 return NULL;
159 }
160
161 if (!mapsize) {
162 WARNING("Can't shrink empty heap!\n");
163 return NULL;
164 }
165
166 /*
167 * If we are forced to change the heapaddr from the
168 * original brk() value we have violated brk semantics
169 * (which we are not supposed to do). This shouldn't
170 * pose a problem until glibc tries to trim the heap to an
171 * address lower than what we aligned heapaddr to. At that
172 * point the alignment "gap" causes heap corruption.
173 * So we don't allow the heap to shrink below heapbase.
174 */
175 if (mapsize + delta < 0) { /* remember: delta is negative */
176 WARNING("Unable to shrink heap below %p\n", heapbase);
177 /* unmap just what is currently mapped */
178 delta = -mapsize;
179 /* we need heaptop + increment == heapbase, so: */
180 increment = heapbase - heaptop;
181 }
182 INFO("Attempting to unmap %ld bytes @ %p\n", -delta,
183 heapbase + mapsize + delta);
184 ret = munmap(heapbase + mapsize + delta, -delta);
185 if (ret) {
186 WARNING("Unmapping failed while shrinking heap: "
187 "%s\n", strerror(errno));
188 } else {
189 mapsize += delta;
190 /*
191 * the glibc assumes by default that newly allocated
192 * memory by morecore() will be zeroed. It would be
193 * wasteful to do it for allocation so we only shrink
194 * the top by the size of a page.
195 */
196 increment = heapbase - heaptop + mapsize;
197
198 if (!__hugetlb_opts.map_hugetlb && !using_default_pagesize){
199
200 /*
201 * Now shrink the hugetlbfs file.
202 */
203 ret = ftruncate(heap_fd, mapsize);
204 if (ret) {
205 WARNING("Could not truncate hugetlbfs file to "
206 "shrink heap: %s\n", strerror(errno));
207 }
208 }
209 }
210
211 }
212 else if (increment < 0) {
213 /* Don't shrink by less than a page to avoid having to zero
214 * the memory. There is no point in lying to glibc since
215 * we're not freeing any memory.
216 */
217 increment = 0;
218 }
219
220 /* heap is continuous */
221 p = heaptop;
222 /* and we now have added this much more space to the heap */
223 heaptop = heaptop + increment;
224
225 INFO("... = %p\n", p);
226 return p;
227 }
228
229 static void *thp_morecore(ptrdiff_t increment)
230 {
231 void *p;
232 long delta;
233
234 INFO("thp_morecore(%ld) = ...\n", (long)increment);
235
236 delta = (heaptop - heapbase) + increment - mapsize;
237 delta = ALIGN(delta, hpage_size);
238
239 if (delta > 0) {
240 /*
241 * This first time we expand the mapping we need to account for
242 * the initial heap mapping not necessarily being huge page
243 * aligned
244 */
245 if (!mapsize)
246 delta = hugetlbfs_next_addr((long)heapbase + delta) -
247 (unsigned long)heapbase;
248
249 INFO("Adding %ld bytes to heap\n", delta);
250
251 p = sbrk(delta);
252 if (p == (void *)-1) {
253 WARNING("sbrk returned ENOMEM\n");
254 return NULL;
255 }
256
257 if (!mapsize) {
258 if (heapbase && (heapbase != p)) {
259 WARNING("Heap was expected at %p instead of %p, "
260 "heap has been modified by someone else!\n",
261 heapbase, p);
262 if (__hugetlbfs_debug)
263 dump_proc_pid_maps();
264 }
265 heapbase = heaptop = p;
266 }
267
268 mapsize += delta;
269 #ifdef MADV_HUGEPAGE
270 madvise(p, delta, MADV_HUGEPAGE);
271 #endif
272 } else if (delta < 0) {
273 /* shrinking the heap */
274 if (!mapsize) {
275 WARNING("Can't shrink an empty heap\n");
276 return NULL;
277 }
278
279 INFO("Attempting to shrink heap by %ld bytes with sbrk\n",
280 -delta);
281 p = sbrk(delta);
282 if (p == (void *)-1) {
283 WARNING("Unable to shrink heap\n");
284 return heaptop;
285 }
286
287 mapsize += delta;
288 }
289
290 p = heaptop;
291 heaptop += increment;
292 INFO("... = %p\n", p);
293 return p;
294 }
295
296 void hugetlbfs_setup_morecore(void)
297 {
298 char *ep;
299 unsigned long heapaddr;
300
301 if (! __hugetlb_opts.morecore)
302 return;
303 if (strcasecmp(__hugetlb_opts.morecore, "no") == 0) {
304 INFO("HUGETLB_MORECORE=%s, not setting up morecore\n",
305 __hugetlb_opts.morecore);
306 return;
307 }
308
309 /*
310 * Determine the page size that will be used for the heap.
311 * This can be set explicitly by setting HUGETLB_MORECORE to a valid
312 * page size string or by setting HUGETLB_DEFAULT_PAGE_SIZE.
313 */
314 if (strncasecmp(__hugetlb_opts.morecore, "y", 1) == 0)
315 hpage_size = gethugepagesize();
316 else if (__hugetlb_opts.thp_morecore)
317 hpage_size = kernel_default_hugepage_size();
318 else
319 hpage_size = parse_page_size(__hugetlb_opts.morecore);
320
321 if (hpage_size <= 0) {
322 if (errno == ENOSYS)
323 WARNING("Hugepages unavailable\n");
324 else if (errno == EOVERFLOW || errno == ERANGE)
325 WARNING("Hugepage size too large\n");
326 else if (errno == EINVAL)
327 WARNING("Invalid huge page size\n");
328 else
329 WARNING("Hugepage size (%s)\n", strerror(errno));
330 return;
331 }
332
333 /*
334 * We won't need an fd for the heap mmaps if we are using MAP_HUGETLB
335 * or we are depending on transparent huge pages
336 */
337 if(__hugetlb_opts.thp_morecore || (__hugetlb_opts.map_hugetlb &&
338 hpage_size == kernel_default_hugepage_size())) {
339 heap_fd = -1;
340 } else {
341 if (!hugetlbfs_find_path_for_size(hpage_size)) {
342 WARNING("Hugepage size %li unavailable", hpage_size);
343 return;
344 }
345
346 heap_fd = hugetlbfs_unlinked_fd_for_size(hpage_size);
347 if (heap_fd < 0) {
348 WARNING("Couldn't open hugetlbfs file for morecore\n");
349 return;
350 }
351 }
352
353 /*
354 * THP morecore uses sbrk to allocate more heap space, counting on the
355 * kernel to back the area with THP. So setting heapbase is
356 * meaningless if thp_morecore is used.
357 */
358 if (!__hugetlb_opts.thp_morecore && __hugetlb_opts.heapbase) {
359 heapaddr = strtoul(__hugetlb_opts.heapbase, &ep, 16);
360 if (*ep != '\0') {
361 WARNING("Can't parse HUGETLB_MORECORE_HEAPBASE: %s\n",
362 __hugetlb_opts.heapbase);
363 return;
364 }
365 } else {
366 heapaddr = (unsigned long)sbrk(0);
367 if (!__hugetlb_opts.thp_morecore)
368 heapaddr = hugetlbfs_next_addr(heapaddr);
369 }
370
371 INFO("setup_morecore(): heapaddr = 0x%lx\n", heapaddr);
372
373 heaptop = heapbase = (void *)heapaddr;
374 if (__hugetlb_opts.thp_morecore)
375 __morecore = &thp_morecore;
376 else
377 __morecore = &hugetlbfs_morecore;
378
379 /* Set some allocator options more appropriate for hugepages */
380
381 if (__hugetlb_opts.shrink_ok)
382 mallopt(M_TRIM_THRESHOLD, hpage_size + hpage_size / 2);
383 else
384 mallopt(M_TRIM_THRESHOLD, -1);
385 mallopt(M_TOP_PAD, hpage_size / 2);
386 /* we always want to use our morecore, not ordinary mmap().
387 * This doesn't appear to prohibit malloc() from falling back
388 * to mmap() if we run out of hugepages. */
389 mallopt(M_MMAP_MAX, 0);
390 }
391 #endif /* HAS_MORECORE */