forked from libhugetlbfs/libhugetlbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.c
337 lines (294 loc) · 9.5 KB
/
alloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* libhugetlbfs - Easy use of Linux hugepages
* alloc.c - Simple allocator of regions backed by hugepages
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include "hugetlbfs.h"
#include "libhugetlbfs_internal.h"
/* Allocate base pages if huge page allocation fails */
static void *fallback_base_pages(size_t len, ghp_t flags)
{
int fd;
void *buf;
INFO("get_huge_pages: Falling back to base pages\n");
/*
* Map /dev/zero instead of MAP_ANONYMOUS avoid VMA mergings. Freeing
* pages depends on /proc/pid/maps to find lengths of allocations.
* This is a bit lazy and if found to be costly due to either the
* extra open() or virtual address space usage, we could track active
* mappings in a lock-protected list instead.
*/
fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
ERROR("get_huge_pages: Failed to open /dev/zero for fallback");
return NULL;
}
buf = mmap(NULL, len,
PROT_READ|PROT_WRITE,
MAP_PRIVATE,
fd, 0);
if (buf == MAP_FAILED) {
WARNING("Base page fallback failed: %s\n", strerror(errno));
buf = NULL;
}
close(fd);
return buf;
}
/**
* get_huge_pages - Allocate an amount of memory backed by huge pages
* len: Size of the region to allocate, must be hugepage-aligned
* flags: Flags specifying the behaviour of the function
*
* This function allocates a region of memory that is backed by huge pages
* and hugepage-aligned. This is not a suitable drop-in for malloc() but a
* a malloc library could use this function to create a new fixed-size heap
* similar in principal to what morecore does for glibc malloc.
*/
void *get_huge_pages(size_t len, ghp_t flags)
{
void *buf;
int buf_fd = -1;
int mmap_reserve = __hugetlb_opts.no_reserve ? MAP_NORESERVE : 0;
int mmap_hugetlb = 0;
int ret;
/* Catch an altogether-too easy typo */
if (flags & GHR_MASK)
ERROR("Improper use of GHR_* in get_huge_pages()\n");
#ifdef MAP_HUGETLB
mmap_hugetlb = MAP_HUGETLB;
#endif
if (__hugetlb_opts.map_hugetlb &&
gethugepagesize() == kernel_default_hugepage_size()) {
/* Because we can use MAP_HUGETLB, we simply mmap the region */
buf = mmap(NULL, len, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS|mmap_hugetlb|mmap_reserve,
0, 0);
} else {
/* Create a file descriptor for the new region */
buf_fd = hugetlbfs_unlinked_fd();
if (buf_fd < 0) {
WARNING("Couldn't open hugetlbfs file for %zd-sized buffer\n",
len);
return NULL;
}
/* Map the requested region */
buf = mmap(NULL, len, PROT_READ|PROT_WRITE,
MAP_PRIVATE|mmap_reserve, buf_fd, 0);
}
if (buf == MAP_FAILED) {
if (buf_fd >= 0)
close(buf_fd);
WARNING("get_huge_pages: New region mapping failed (flags: 0x%lX): %s\n",
flags, strerror(errno));
return NULL;
}
/* Fault the region to ensure accesses succeed */
ret = hugetlbfs_prefault(buf, len);
if (ret != 0) {
munmap(buf, len);
if (buf_fd >= 0)
close(buf_fd);
WARNING("get_huge_pages: Prefaulting failed (flags: 0x%lX): %s\n",
flags, strerror(ret));
return NULL;
}
/* Close the file so we do not have to track the descriptor */
if (buf_fd >= 0 && close(buf_fd) != 0) {
WARNING("Failed to close new buffer fd: %s\n", strerror(errno));
munmap(buf, len);
return NULL;
}
/* woo, new buffer of shiny */
return buf;
}
#define MAPS_BUF_SZ 4096
static void __free_huge_pages(void *ptr, int aligned)
{
FILE *fd;
char line[MAPS_BUF_SZ];
unsigned long start = 0, end = 0;
unsigned long palign = 0, hpalign = 0;
unsigned long hpalign_end = 0;
/*
* /proc/self/maps is used to determine the length of the original
* allocation. As mappings are based on different files, we can
* assume that maps will not merge. If the hugepages were truly
* anonymous, this assumption would be broken.
*/
fd = fopen("/proc/self/maps", "r");
if (!fd) {
ERROR("Failed to open /proc/self/maps\n");
return;
}
/*
* An unaligned address allocated by get_hugepage_region()
* could be either page or hugepage aligned
*/
if (!aligned) {
palign = ALIGN_DOWN((unsigned long)ptr, getpagesize());
hpalign = ALIGN_DOWN((unsigned long)ptr, gethugepagesize());
}
/* Parse /proc/maps for address ranges line by line */
while (!feof(fd)) {
char *bufptr;
char *saveptr = NULL;
/* Read a line of input */
if (fgets(line, MAPS_BUF_SZ, fd) == NULL)
break;
/* Parse the line to get the start and end of each mapping */
bufptr = strtok_r(line, " ", &saveptr);
bufptr = strtok_r(bufptr, "-", &saveptr);
start = strtoull(bufptr, NULL, 16);
bufptr = strtok_r(NULL, "-", &saveptr);
/* If the correct mapping is found, remove it */
if (start == (unsigned long)ptr) {
end = strtoull(bufptr, NULL, 16);
munmap(ptr, end - start);
break;
}
/* If the passed address is aligned, just move along */
if (aligned)
continue;
/*
* If an address is hpage-aligned, record it but keep looking.
* We might find a page-aligned or exact address later
*/
if (start == hpalign) {
hpalign_end = strtoull(bufptr, NULL, 16);
continue;
}
/* If an address is page-aligned, free it */
if (start == palign) {
end = strtoull(bufptr, NULL, 16);
munmap((void *)start, end - start);
break;
}
}
/*
* If no exact or page-aligned address was found, check for a
* hpage-aligned address. If found, free it, otherwise warn that
* the ptr pointed nowhere
*/
if (end == 0) {
if (hpalign_end == 0)
ERROR("hugepages_free using invalid or double free\n");
else
munmap((void *)hpalign, hpalign_end - hpalign);
}
fclose(fd);
}
/**
* free_huge_pages - Free a region allocated that was backed by large pages
* ptr - The pointer to the buffer returned by get_huge_pages()
*
* This function finds a region to free based on the contents of
* /proc/pid/maps. The assumption is made that the ptr is the start of
* a hugepage region allocated with free_huge_pages. No checking is made
* that the pointer is to a hugepage backed region.
*/
void free_huge_pages(void *ptr)
{
__free_huge_pages(ptr, 1);
}
/*
* Offset the buffer using bytes wasted due to alignment to avoid using the
* same cache lines for the start of every buffer returned by
* get_huge_pages(). A small effort is made to select a random cacheline
* rather than sequential lines to give decent behaviour on average.
*/
void *cachecolor(void *buf, size_t len, size_t color_bytes)
{
static long cacheline_size = 0;
static int linemod = 0;
char *bytebuf = (char *)buf;
int numlines;
int line = 0;
/* Lookup our cacheline size once */
if (cacheline_size == 0) {
cacheline_size = sysconf(_SC_LEVEL2_CACHE_LINESIZE);
linemod = time(NULL);
}
numlines = color_bytes / cacheline_size;
DEBUG("%d lines of cacheline size %ld due to %zd wastage\n",
numlines, cacheline_size, color_bytes);
if (numlines) {
line = linemod % numlines;
bytebuf += cacheline_size * line;
/* Pseudo-ish random line selection */
linemod += len % numlines;
}
DEBUG("Using line offset %d from start\n", line);
return bytebuf;
}
/**
* get_hugepage_region - Allocate an amount of memory backed by huge pages
*
* len: Size of the region to allocate
* flags: Flags specifying the behaviour of the function
*
* This function allocates a region of memory backed by huge pages. Care should
* be taken when using this function as a drop-in replacement for malloc() as
* memory can be wasted if the length is not hugepage-aligned. This function
* is more relaxed than get_huge_pages() in that it allows fallback to small
* pages when requested.
*/
void *get_hugepage_region(size_t len, ghr_t flags)
{
size_t aligned_len, wastage;
void *buf;
/* Catch an altogether-too easy typo */
if (flags & GHP_MASK)
ERROR("Improper use of GHP_* in get_hugepage_region()\n");
/* Align the len parameter to a hugepage boundary and allocate */
aligned_len = ALIGN(len, gethugepagesize());
buf = get_huge_pages(aligned_len, GHP_DEFAULT);
if (buf == NULL && (flags & GHR_FALLBACK)) {
aligned_len = ALIGN(len, getpagesize());
buf = fallback_base_pages(len, flags);
}
/* Calculate wastage for coloring */
wastage = aligned_len - len;
if (wastage != 0 && !(flags & GHR_COLOR))
DEBUG("get_hugepage_region: Wasted %zd bytes due to alignment\n",
wastage);
/* Only colour if requested */
if (flags & GHR_COLOR)
buf = cachecolor(buf, len, wastage);
return buf;
}
/**
* free_hugepage_region - Free a region allocated by get_hugepage_region
* ptr - The pointer to the buffer returned by get_hugepage_region
*
* This function finds a region to free based on the contents of
* /proc/pid/maps. The assumption is made that the ptr is the start of
* a hugepage region allocated with get_hugepage_region. No checking is made
* that the pointer is to a hugepage backed region.
*/
void free_hugepage_region(void *ptr)
{
__free_huge_pages(ptr, 0);
}