| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
#include <fcntl.h> |
| 22 |
#include <errno.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <time.h> |
| 28 |
#include <sys/mman.h> |
| 29 |
#include <sys/types.h> |
| 30 |
|
| 31 |
#include "hugetlbfs.h" |
| 32 |
#include "libhugetlbfs_internal.h" |
| 33 |
|
| 34 |
|
| 35 |
static void *fallback_base_pages(size_t len, ghp_t flags) |
| 36 |
{ |
| 37 |
int fd; |
| 38 |
void *buf; |
| 39 |
INFO("get_huge_pages: Falling back to base pages\n"); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
fd = open("/dev/zero", O_RDWR); |
| 49 |
if (fd == -1) { |
| 50 |
ERROR("get_huge_pages: Failed to open /dev/zero for fallback"); |
| 51 |
return NULL; |
| 52 |
} |
| 53 |
|
| 54 |
buf = mmap(NULL, len, |
| 55 |
PROT_READ|PROT_WRITE, |
| 56 |
MAP_PRIVATE, |
| 57 |
fd, 0); |
| 58 |
if (buf == MAP_FAILED) { |
| 59 |
WARNING("Base page fallback failed: %s\n", strerror(errno)); |
| 60 |
buf = NULL; |
| 61 |
} |
| 62 |
close(fd); |
| 63 |
|
| 64 |
return buf; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
void *get_huge_pages(size_t len, ghp_t flags) |
| 78 |
{ |
| 79 |
void *buf; |
| 80 |
int buf_fd = -1; |
| 81 |
int mmap_reserve = __hugetlb_opts.no_reserve ? MAP_NORESERVE : 0; |
| 82 |
int mmap_hugetlb = 0; |
| 83 |
int ret; |
| 84 |
|
| 85 |
|
| 86 |
if (flags & GHR_MASK) |
| 87 |
ERROR("Improper use of GHR_* in get_huge_pages()\n"); |
| 88 |
|
| 89 |
#ifdef MAP_HUGETLB |
| 90 |
mmap_hugetlb = MAP_HUGETLB; |
| 91 |
#endif |
| 92 |
|
| 93 |
if (__hugetlb_opts.map_hugetlb && |
| 94 |
gethugepagesize() == kernel_default_hugepage_size()) { |
| 95 |
|
| 96 |
buf = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 97 |
MAP_PRIVATE|MAP_ANONYMOUS|mmap_hugetlb|mmap_reserve, |
| 98 |
0, 0); |
| 99 |
} else { |
| 100 |
|
| 101 |
buf_fd = hugetlbfs_unlinked_fd(); |
| 102 |
if (buf_fd < 0) { |
| 103 |
WARNING("Couldn't open hugetlbfs file for %zd-sized buffer\n", |
| 104 |
len); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
buf = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 110 |
MAP_PRIVATE|mmap_reserve, buf_fd, 0); |
| 111 |
} |
| 112 |
|
| 113 |
if (buf == MAP_FAILED) { |
| 114 |
if (buf_fd >= 0) |
| 115 |
close(buf_fd); |
| 116 |
|
| 117 |
WARNING("get_huge_pages: New region mapping failed (flags: 0x%lX): %s\n", |
| 118 |
flags, strerror(errno)); |
| 119 |
return NULL; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
ret = hugetlbfs_prefault(buf, len); |
| 124 |
if (ret != 0) { |
| 125 |
munmap(buf, len); |
| 126 |
if (buf_fd >= 0) |
| 127 |
close(buf_fd); |
| 128 |
|
| 129 |
WARNING("get_huge_pages: Prefaulting failed (flags: 0x%lX): %s\n", |
| 130 |
flags, strerror(ret)); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
if (buf_fd >= 0 && close(buf_fd) != 0) { |
| 136 |
WARNING("Failed to close new buffer fd: %s\n", strerror(errno)); |
| 137 |
munmap(buf, len); |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
return buf; |
| 143 |
} |
| 144 |
|
| 145 |
#define MAPS_BUF_SZ 4096 |
| 146 |
static void __free_huge_pages(void *ptr, int aligned) |
| 147 |
{ |
| 148 |
FILE *fd; |
| 149 |
char line[MAPS_BUF_SZ]; |
| 150 |
unsigned long start = 0, end = 0; |
| 151 |
unsigned long palign = 0, hpalign = 0; |
| 152 |
unsigned long hpalign_end = 0; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fd = fopen("/proc/self/maps", "r"); |
| 161 |
if (!fd) { |
| 162 |
ERROR("Failed to open /proc/self/maps\n"); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if (!aligned) { |
| 171 |
palign = ALIGN_DOWN((unsigned long)ptr, getpagesize()); |
| 172 |
hpalign = ALIGN_DOWN((unsigned long)ptr, gethugepagesize()); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
while (!feof(fd)) { |
| 177 |
char *bufptr; |
| 178 |
char *saveptr = NULL; |
| 179 |
|
| 180 |
|
| 181 |
if (fgets(line, MAPS_BUF_SZ, fd) == NULL) |
| 182 |
break; |
| 183 |
|
| 184 |
|
| 185 |
bufptr = strtok_r(line, " ", &saveptr); |
| 186 |
bufptr = strtok_r(bufptr, "-", &saveptr); |
| 187 |
start = strtoull(bufptr, NULL, 16); |
| 188 |
bufptr = strtok_r(NULL, "-", &saveptr); |
| 189 |
|
| 190 |
|
| 191 |
if (start == (unsigned long)ptr) { |
| 192 |
end = strtoull(bufptr, NULL, 16); |
| 193 |
munmap(ptr, end - start); |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
if (aligned) |
| 199 |
continue; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if (start == hpalign) { |
| 206 |
hpalign_end = strtoull(bufptr, NULL, 16); |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
if (start == palign) { |
| 212 |
end = strtoull(bufptr, NULL, 16); |
| 213 |
munmap((void *)start, end - start); |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if (end == 0) { |
| 225 |
if (hpalign_end == 0) |
| 226 |
ERROR("hugepages_free using invalid or double free\n"); |
| 227 |
else |
| 228 |
munmap((void *)hpalign, hpalign_end - hpalign); |
| 229 |
} |
| 230 |
|
| 231 |
fclose(fd); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
void free_huge_pages(void *ptr) |
| 244 |
{ |
| 245 |
__free_huge_pages(ptr, 1); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
void *cachecolor(void *buf, size_t len, size_t color_bytes) |
| 255 |
{ |
| 256 |
static long cacheline_size = 0; |
| 257 |
static int linemod = 0; |
| 258 |
char *bytebuf = (char *)buf; |
| 259 |
int numlines; |
| 260 |
int line = 0; |
| 261 |
|
| 262 |
|
| 263 |
if (cacheline_size == 0) { |
| 264 |
cacheline_size = sysconf(_SC_LEVEL2_CACHE_LINESIZE); |
| 265 |
linemod = time(NULL); |
| 266 |
} |
| 267 |
|
| 268 |
numlines = color_bytes / cacheline_size; |
| 269 |
DEBUG("%d lines of cacheline size %ld due to %zd wastage\n", |
| 270 |
numlines, cacheline_size, color_bytes); |
| 271 |
if (numlines) { |
| 272 |
line = linemod % numlines; |
| 273 |
bytebuf += cacheline_size * line; |
| 274 |
|
| 275 |
|
| 276 |
linemod += len % numlines; |
| 277 |
} |
| 278 |
DEBUG("Using line offset %d from start\n", line); |
| 279 |
|
| 280 |
return bytebuf; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
void *get_hugepage_region(size_t len, ghr_t flags) |
| 296 |
{ |
| 297 |
size_t aligned_len, wastage; |
| 298 |
void *buf; |
| 299 |
|
| 300 |
|
| 301 |
if (flags & GHP_MASK) |
| 302 |
ERROR("Improper use of GHP_* in get_hugepage_region()\n"); |
| 303 |
|
| 304 |
|
| 305 |
aligned_len = ALIGN(len, gethugepagesize()); |
| 306 |
buf = get_huge_pages(aligned_len, GHP_DEFAULT); |
| 307 |
if (buf == NULL) { |
| 308 |
if (flags & GHR_FALLBACK) { |
| 309 |
aligned_len = ALIGN(len, getpagesize()); |
| 310 |
buf = fallback_base_pages(len, flags); |
| 311 |
} else { |
| 312 |
return buf; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
wastage = aligned_len - len; |
| 318 |
if (wastage != 0 && !(flags & GHR_COLOR)) |
| 319 |
DEBUG("get_hugepage_region: Wasted %zd bytes due to alignment\n", |
| 320 |
wastage); |
| 321 |
|
| 322 |
|
| 323 |
if (flags & GHR_COLOR) |
| 324 |
buf = cachecolor(buf, len, wastage); |
| 325 |
|
| 326 |
return buf; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
void free_hugepage_region(void *ptr) |
| 339 |
{ |
| 340 |
__free_huge_pages(ptr, 0); |
| 341 |
} |