| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _LARGEFILE64_SOURCE |
| 21 |
#define _GNU_SOURCE |
| 22 |
#include <dlfcn.h> |
| 23 |
#include <features.h> |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdint.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <limits.h> |
| 30 |
#include <string.h> |
| 31 |
#include <ctype.h> |
| 32 |
#include <signal.h> |
| 33 |
#include <dirent.h> |
| 34 |
|
| 35 |
#include <unistd.h> |
| 36 |
#include <fcntl.h> |
| 37 |
#include <sys/vfs.h> |
| 38 |
#include <sys/statfs.h> |
| 39 |
#include <sys/types.h> |
| 40 |
#include <sys/mman.h> |
| 41 |
#include <sys/file.h> |
| 42 |
#include <sys/uio.h> |
| 43 |
#include <sys/syscall.h> |
| 44 |
#include <linux/types.h> |
| 45 |
#include <linux/unistd.h> |
| 46 |
#include <dirent.h> |
| 47 |
|
| 48 |
#include "libhugetlbfs_internal.h" |
| 49 |
#include "hugetlbfs.h" |
| 50 |
|
| 51 |
struct libhugeopts_t __hugetlb_opts; |
| 52 |
|
| 53 |
static int hugepagesize_errno; |
| 54 |
|
| 55 |
#define MAX_HPAGE_SIZES 10 |
| 56 |
static struct hpage_size hpage_sizes[MAX_HPAGE_SIZES]; |
| 57 |
static int nr_hpage_sizes; |
| 58 |
static int hpage_sizes_default_idx = -1; |
| 59 |
|
| 60 |
static long default_size; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
long kernel_default_hugepage_size() |
| 70 |
{ |
| 71 |
if (default_size == 0) { |
| 72 |
default_size = file_read_ulong(MEMINFO, "Hugepagesize:"); |
| 73 |
default_size = size_to_smaller_unit(default_size); } |
| 74 |
return default_size; |
| 75 |
} |
| 76 |
void kernel_default_hugepage_size_reset(void) |
| 77 |
{ |
| 78 |
default_size = 0; |
| 79 |
} |
| 80 |
|
| 81 |
#define BUF_SZ 256 |
| 82 |
#define MEMINFO_SIZE 2048 |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
unsigned long long size_to_smaller_unit(unsigned long long size) |
| 91 |
{ |
| 92 |
if (size * 1024 < size) |
| 93 |
return -1; |
| 94 |
else |
| 95 |
return size * 1024; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
long parse_page_size(const char *str) |
| 107 |
{ |
| 108 |
char *pos; |
| 109 |
long size; |
| 110 |
|
| 111 |
errno = 0; |
| 112 |
size = strtol(str, &pos, 0); |
| 113 |
|
| 114 |
if (errno || str == pos || size <= 0) { |
| 115 |
if (errno == ERANGE) |
| 116 |
errno = EOVERFLOW; |
| 117 |
else |
| 118 |
errno = EINVAL; |
| 119 |
return -1; |
| 120 |
} |
| 121 |
|
| 122 |
switch (*pos) { |
| 123 |
case 'G': |
| 124 |
case 'g': |
| 125 |
size = size_to_smaller_unit(size); |
| 126 |
case 'M': |
| 127 |
case 'm': |
| 128 |
size = size_to_smaller_unit(size); |
| 129 |
case 'K': |
| 130 |
case 'k': |
| 131 |
size = size_to_smaller_unit(size); |
| 132 |
} |
| 133 |
|
| 134 |
if (size < 0) |
| 135 |
errno = EOVERFLOW; |
| 136 |
return size; |
| 137 |
} |
| 138 |
|
| 139 |
struct hugetlb_pool_counter_info_t { |
| 140 |
char *meminfo_key; |
| 141 |
char *sysfs_file; |
| 142 |
}; |
| 143 |
|
| 144 |
static struct hugetlb_pool_counter_info_t hugetlb_counter_info[] = { |
| 145 |
[HUGEPAGES_TOTAL] = { |
| 146 |
.meminfo_key = "HugePages_Total:", |
| 147 |
.sysfs_file = "nr_hugepages", |
| 148 |
}, |
| 149 |
[HUGEPAGES_TOTAL_MEMPOL] = { |
| 150 |
.meminfo_key = "HugePages_Total:", |
| 151 |
.sysfs_file = "nr_hugepages_mempolicy", |
| 152 |
}, |
| 153 |
[HUGEPAGES_FREE] = { |
| 154 |
.meminfo_key = "HugePages_Free:", |
| 155 |
.sysfs_file = "free_hugepages", |
| 156 |
}, |
| 157 |
[HUGEPAGES_RSVD] = { |
| 158 |
.meminfo_key = "HugePages_Rsvd:", |
| 159 |
.sysfs_file = "resv_hugepages", |
| 160 |
}, |
| 161 |
[HUGEPAGES_SURP] = { |
| 162 |
.meminfo_key = "HugePages_Surp:", |
| 163 |
.sysfs_file = "surplus_hugepages", |
| 164 |
}, |
| 165 |
[HUGEPAGES_OC] = { |
| 166 |
.meminfo_key = NULL, |
| 167 |
.sysfs_file = "nr_overcommit_hugepages" |
| 168 |
}, |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
long file_read_ulong(char *file, const char *tag) |
| 176 |
{ |
| 177 |
int fd; |
| 178 |
char buf[MEMINFO_SIZE]; |
| 179 |
int len, readerr; |
| 180 |
char *p, *q; |
| 181 |
long val; |
| 182 |
|
| 183 |
fd = open(file, O_RDONLY); |
| 184 |
if (fd < 0) { |
| 185 |
ERROR("Couldn't open %s: %s\n", file, strerror(errno)); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
len = read(fd, buf, sizeof(buf)); |
| 190 |
readerr = errno; |
| 191 |
close(fd); |
| 192 |
if (len < 0) { |
| 193 |
ERROR("Error reading %s: %s\n", file, strerror(readerr)); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
if (len == sizeof(buf)) { |
| 197 |
ERROR("%s is too large\n", file); |
| 198 |
return -1; |
| 199 |
} |
| 200 |
buf[len] = '\0'; |
| 201 |
|
| 202 |
|
| 203 |
if (tag) { |
| 204 |
p = strstr(buf, tag); |
| 205 |
if (!p) |
| 206 |
return -1; |
| 207 |
p += strlen(tag); |
| 208 |
} else |
| 209 |
p = buf; |
| 210 |
|
| 211 |
val = strtol(p, &q, 0); |
| 212 |
if (! isspace(*q)) { |
| 213 |
ERROR("Couldn't parse %s value\n", file); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
|
| 217 |
return val; |
| 218 |
} |
| 219 |
|
| 220 |
int file_write_ulong(char *file, unsigned long val) |
| 221 |
{ |
| 222 |
int fd, ret, buflen; |
| 223 |
char buf[20]; |
| 224 |
|
| 225 |
fd = open(file, O_WRONLY); |
| 226 |
if (fd < 0) { |
| 227 |
ERROR("Couldn't open %s: %s\n", file, strerror(errno)); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
buflen = sprintf(buf, "%lu", val); |
| 232 |
ret = write(fd, buf, buflen); |
| 233 |
close(fd); |
| 234 |
return ret > 0 ? 0 : -1; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
#define MAX_EXE 4096 |
| 242 |
static char *get_exe_name(char *buf, int size) |
| 243 |
{ |
| 244 |
char *p; |
| 245 |
int fd; |
| 246 |
ssize_t nread; |
| 247 |
|
| 248 |
buf[0] = 0; |
| 249 |
fd = open("/proc/self/cmdline", O_RDONLY); |
| 250 |
if (fd < 0) { |
| 251 |
WARNING("Unable to open cmdline, no exe name\n"); |
| 252 |
return buf; |
| 253 |
} |
| 254 |
nread = read(fd, buf, size-1); |
| 255 |
close(fd); |
| 256 |
|
| 257 |
if (nread < 0) { |
| 258 |
WARNING("Error %d reading cmdline, no exe name\n", errno); |
| 259 |
return buf; |
| 260 |
} |
| 261 |
if (nread == 0) { |
| 262 |
WARNING("Read zero bytes from cmdline, no exe name\n"); |
| 263 |
return buf; |
| 264 |
} |
| 265 |
|
| 266 |
buf[nread] = 0; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
p = strrchr(buf, '/'); |
| 277 |
if (!p) |
| 278 |
return buf; |
| 279 |
return p + 1; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
void hugetlbfs_setup_env() |
| 288 |
{ |
| 289 |
char *env; |
| 290 |
|
| 291 |
__hugetlb_opts.min_copy = true; |
| 292 |
|
| 293 |
env = getenv("HUGETLB_VERBOSE"); |
| 294 |
if (env) |
| 295 |
__hugetlbfs_verbose = atoi(env); |
| 296 |
|
| 297 |
env = getenv("HUGETLB_DEBUG"); |
| 298 |
if (env) { |
| 299 |
__hugetlbfs_debug = true; |
| 300 |
__hugetlbfs_verbose = VERBOSE_DEBUG; |
| 301 |
} |
| 302 |
|
| 303 |
env = getenv("HUGETLB_RESTRICT_EXE"); |
| 304 |
if (env) { |
| 305 |
char *p, *tok, *exe, buf[MAX_EXE+1], restriction[MAX_EXE]; |
| 306 |
int found = 0; |
| 307 |
|
| 308 |
exe = get_exe_name(buf, sizeof buf); |
| 309 |
DEBUG("Found HUGETLB_RESTRICT_EXE, this exe is \"%s\"\n", exe); |
| 310 |
strncpy(restriction, env, sizeof restriction); |
| 311 |
restriction[sizeof(restriction)-1] = 0; |
| 312 |
for (p = restriction; (tok = strtok(p, ":")) != NULL; p = NULL) { |
| 313 |
DEBUG(" ...check exe match for \"%s\"\n", tok); |
| 314 |
if (strcmp(tok, exe) == 0) { |
| 315 |
found = 1; |
| 316 |
DEBUG("exe match - libhugetlbfs is active for this exe\n"); |
| 317 |
break; |
| 318 |
} |
| 319 |
} |
| 320 |
if (!found) { |
| 321 |
DEBUG("No exe match - libhugetlbfs is inactive for this exe\n"); |
| 322 |
return; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
env = getenv("HUGETLB_NO_PREFAULT"); |
| 327 |
if (env) |
| 328 |
__hugetlbfs_prefault = false; |
| 329 |
|
| 330 |
__hugetlb_opts.share_path = getenv("HUGETLB_SHARE_PATH"); |
| 331 |
__hugetlb_opts.elfmap = getenv("HUGETLB_ELFMAP"); |
| 332 |
__hugetlb_opts.ld_preload = getenv("LD_PRELOAD"); |
| 333 |
__hugetlb_opts.def_page_size = getenv("HUGETLB_DEFAULT_PAGE_SIZE"); |
| 334 |
__hugetlb_opts.path = getenv("HUGETLB_PATH"); |
| 335 |
__hugetlb_opts.features = getenv("HUGETLB_FEATURES"); |
| 336 |
__hugetlb_opts.morecore = getenv("HUGETLB_MORECORE"); |
| 337 |
__hugetlb_opts.heapbase = getenv("HUGETLB_MORECORE_HEAPBASE"); |
| 338 |
|
| 339 |
if (__hugetlb_opts.morecore) |
| 340 |
__hugetlb_opts.thp_morecore = |
| 341 |
(strcasecmp(__hugetlb_opts.morecore, "thp") == 0); |
| 342 |
|
| 343 |
if (__hugetlb_opts.thp_morecore && __hugetlb_opts.heapbase) { |
| 344 |
DEBUG("Heapbase specified with THP for morecore, ignoring heapbase\n"); |
| 345 |
__hugetlb_opts.heapbase = NULL; |
| 346 |
} |
| 347 |
|
| 348 |
env = getenv("HUGETLB_FORCE_ELFMAP"); |
| 349 |
if (env && (strcasecmp(env, "yes") == 0)) |
| 350 |
__hugetlb_opts.force_elfmap = 1; |
| 351 |
|
| 352 |
env = getenv("HUGETLB_MINIMAL_COPY"); |
| 353 |
if (__hugetlb_opts.min_copy && env && (strcasecmp(env, "no") == 0)) { |
| 354 |
INFO("HUGETLB_MINIMAL_COPY=%s, disabling filesz copy " |
| 355 |
"optimization\n", env); |
| 356 |
__hugetlb_opts.min_copy = false; |
| 357 |
} |
| 358 |
|
| 359 |
env = getenv("HUGETLB_SHARE"); |
| 360 |
if (env) |
| 361 |
__hugetlb_opts.sharing = atoi(env); |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
env = getenv("HUGETLB_MORECORE_SHRINK"); |
| 379 |
if (env && strcasecmp(env, "yes") == 0) |
| 380 |
__hugetlb_opts.shrink_ok = true; |
| 381 |
|
| 382 |
|
| 383 |
env = getenv("HUGETLB_SHM"); |
| 384 |
if (env && !strcasecmp(env, "yes")) |
| 385 |
__hugetlb_opts.shm_enabled = true; |
| 386 |
|
| 387 |
|
| 388 |
env = getenv("HUGETLB_NO_RESERVE"); |
| 389 |
if (env && !strcasecmp(env, "yes")) |
| 390 |
__hugetlb_opts.no_reserve = true; |
| 391 |
} |
| 392 |
|
| 393 |
void hugetlbfs_setup_kernel_page_size() |
| 394 |
{ |
| 395 |
long page_size = kernel_default_hugepage_size(); |
| 396 |
|
| 397 |
if (page_size <= 0) { |
| 398 |
WARNING("Unable to find default kernel huge page size\n"); |
| 399 |
return; |
| 400 |
} |
| 401 |
|
| 402 |
INFO("Found pagesize %ld kB\n", page_size / 1024); |
| 403 |
hpage_sizes[0].pagesize = page_size; |
| 404 |
|
| 405 |
nr_hpage_sizes = 1; |
| 406 |
} |
| 407 |
|
| 408 |
void hugetlbfs_check_priv_resv() |
| 409 |
{ |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if (hugetlbfs_test_feature(HUGETLB_FEATURE_PRIVATE_RESV) > 0) { |
| 416 |
INFO("Kernel has MAP_PRIVATE reservations. Disabling " |
| 417 |
"heap prefaulting.\n"); |
| 418 |
__hugetlbfs_prefault = false; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
void hugetlbfs_check_safe_noreserve() |
| 423 |
{ |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
if (__hugetlb_opts.no_reserve && |
| 430 |
hugetlbfs_test_feature(HUGETLB_FEATURE_SAFE_NORESERVE) <= 0) { |
| 431 |
INFO("Kernel is not safe for MAP_NORESERVE. Forcing " |
| 432 |
"use of reservations.\n"); |
| 433 |
__hugetlb_opts.no_reserve = false; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
void hugetlbfs_check_map_hugetlb() |
| 438 |
{ |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
#ifdef MAP_HUGETLB |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
if (hugetlbfs_test_feature(HUGETLB_FEATURE_MAP_HUGETLB) > 0) { |
| 452 |
INFO("Kernel supports MAP_HUGETLB\n"); |
| 453 |
__hugetlb_opts.map_hugetlb = true; |
| 454 |
} |
| 455 |
#endif |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
static int select_pool_counter(unsigned int counter, unsigned long pagesize, |
| 466 |
char *filename, char **key) |
| 467 |
{ |
| 468 |
long default_size; |
| 469 |
char *meminfo_key; |
| 470 |
char *sysfs_file; |
| 471 |
|
| 472 |
if (counter >= HUGEPAGES_MAX_COUNTERS) { |
| 473 |
ERROR("Invalid counter specified\n"); |
| 474 |
return -1; |
| 475 |
} |
| 476 |
|
| 477 |
meminfo_key = hugetlb_counter_info[counter].meminfo_key; |
| 478 |
sysfs_file = hugetlb_counter_info[counter].sysfs_file; |
| 479 |
if (key) |
| 480 |
*key = NULL; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
default_size = kernel_default_hugepage_size(); |
| 489 |
if (default_size < 0) { |
| 490 |
ERROR("Cannot determine the default page size\n"); |
| 491 |
return -1; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
if (pagesize == default_size) { |
| 496 |
if (meminfo_key && key) { |
| 497 |
strcpy(filename, MEMINFO); |
| 498 |
*key = meminfo_key; |
| 499 |
} else |
| 500 |
sprintf(filename, PROC_HUGEPAGES_DIR "%s", sysfs_file); |
| 501 |
} else |
| 502 |
sprintf(filename, SYSFS_HUGEPAGES_DIR "hugepages-%lukB/%s", |
| 503 |
pagesize / 1024, sysfs_file); |
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
static int hpage_size_to_index(unsigned long size) |
| 508 |
{ |
| 509 |
int i; |
| 510 |
|
| 511 |
for (i = 0; i < nr_hpage_sizes; i++) |
| 512 |
if (hpage_sizes[i].pagesize == size) |
| 513 |
return i; |
| 514 |
return -1; |
| 515 |
} |
| 516 |
|
| 517 |
void probe_default_hpage_size(void) |
| 518 |
{ |
| 519 |
long size; |
| 520 |
int index; |
| 521 |
int default_overrided; |
| 522 |
|
| 523 |
if (nr_hpage_sizes == 0) { |
| 524 |
INFO("No configured huge page sizes\n"); |
| 525 |
hpage_sizes_default_idx = -1; |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
default_overrided = (__hugetlb_opts.def_page_size && |
| 534 |
strlen(__hugetlb_opts.def_page_size) > 0); |
| 535 |
if (default_overrided) |
| 536 |
size = parse_page_size(__hugetlb_opts.def_page_size); |
| 537 |
else { |
| 538 |
size = kernel_default_hugepage_size(); |
| 539 |
} |
| 540 |
|
| 541 |
if (size >= 0) { |
| 542 |
index = hpage_size_to_index(size); |
| 543 |
if (index >= 0) |
| 544 |
hpage_sizes_default_idx = index; |
| 545 |
else { |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
char msg[] = "No mount point found for default huge " \ |
| 553 |
"page size. Using first available mount " |
| 554 |
"point.\n"; |
| 555 |
if (default_overrided) |
| 556 |
WARNING("%s", msg); |
| 557 |
else |
| 558 |
INFO("%s", msg); |
| 559 |
hpage_sizes_default_idx = 0; |
| 560 |
} |
| 561 |
} else { |
| 562 |
ERROR("Unable to determine default huge page size\n"); |
| 563 |
hpage_sizes_default_idx = -1; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
static void add_hugetlbfs_mount(char *path, int user_mount) |
| 568 |
{ |
| 569 |
int idx; |
| 570 |
long size; |
| 571 |
|
| 572 |
if (strlen(path) > PATH_MAX) |
| 573 |
return; |
| 574 |
|
| 575 |
if (!hugetlbfs_test_path(path)) { |
| 576 |
WARNING("%s is not a hugetlbfs mount point, ignoring\n", path); |
| 577 |
return; |
| 578 |
} |
| 579 |
|
| 580 |
size = hugetlbfs_test_pagesize(path); |
| 581 |
if (size < 0) { |
| 582 |
WARNING("Unable to detect page size for path %s\n", path); |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
idx = hpage_size_to_index(size); |
| 587 |
if (idx < 0) { |
| 588 |
if (nr_hpage_sizes >= MAX_HPAGE_SIZES) { |
| 589 |
WARNING("Maximum number of huge page sizes exceeded, " |
| 590 |
"ignoring %lukB page size\n", size); |
| 591 |
return; |
| 592 |
} |
| 593 |
|
| 594 |
idx = nr_hpage_sizes; |
| 595 |
hpage_sizes[nr_hpage_sizes++].pagesize = size; |
| 596 |
} |
| 597 |
|
| 598 |
if (strlen(hpage_sizes[idx].mount)) { |
| 599 |
if (user_mount) |
| 600 |
WARNING("Mount point already defined for size %li, " |
| 601 |
"ignoring %s\n", size, path); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
strcpy(hpage_sizes[idx].mount, path); |
| 606 |
} |
| 607 |
|
| 608 |
void debug_show_page_sizes(void) |
| 609 |
{ |
| 610 |
int i; |
| 611 |
|
| 612 |
INFO("Detected page sizes:\n"); |
| 613 |
for (i = 0; i < nr_hpage_sizes; i++) |
| 614 |
INFO(" Size: %li kB %s Mount: %s\n", |
| 615 |
hpage_sizes[i].pagesize / 1024, |
| 616 |
i == hpage_sizes_default_idx ? "(default)" : "", |
| 617 |
hpage_sizes[i].mount); |
| 618 |
} |
| 619 |
|
| 620 |
#define LINE_MAXLEN 2048 |
| 621 |
static void find_mounts(void) |
| 622 |
{ |
| 623 |
int fd; |
| 624 |
char path[PATH_MAX+1]; |
| 625 |
char line[LINE_MAXLEN + 1]; |
| 626 |
char *eol; |
| 627 |
char *match; |
| 628 |
char *end; |
| 629 |
int bytes; |
| 630 |
off_t offset; |
| 631 |
|
| 632 |
fd = open("/proc/mounts", O_RDONLY); |
| 633 |
if (fd < 0) { |
| 634 |
fd = open("/etc/mtab", O_RDONLY); |
| 635 |
if (fd < 0) { |
| 636 |
ERROR("Couldn't open /proc/mounts or /etc/mtab (%s)\n", |
| 637 |
strerror(errno)); |
| 638 |
return; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
while ((bytes = read(fd, line, LINE_MAXLEN)) > 0) { |
| 643 |
line[LINE_MAXLEN] = '\0'; |
| 644 |
eol = strchr(line, '\n'); |
| 645 |
if (!eol) { |
| 646 |
ERROR("Line too long when parsing mounts\n"); |
| 647 |
break; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
*eol = '\0'; |
| 655 |
offset = bytes - (eol + 1 - line); |
| 656 |
lseek(fd, -offset, SEEK_CUR); |
| 657 |
|
| 658 |
|
| 659 |
match = strstr(line, " hugetlbfs "); |
| 660 |
if (match) { |
| 661 |
match = strchr(line, '/'); |
| 662 |
if (!match) |
| 663 |
continue; |
| 664 |
end = strchr(match, ' '); |
| 665 |
if (!end) |
| 666 |
continue; |
| 667 |
|
| 668 |
strncpy(path, match, end - match); |
| 669 |
path[end - match] = '\0'; |
| 670 |
if ((hugetlbfs_test_path(path) == 1) && |
| 671 |
!(access(path, R_OK | W_OK | X_OK))) |
| 672 |
add_hugetlbfs_mount(path, 0); |
| 673 |
} |
| 674 |
} |
| 675 |
close(fd); |
| 676 |
} |
| 677 |
|
| 678 |
void setup_mounts(void) |
| 679 |
{ |
| 680 |
int do_scan = 1; |
| 681 |
|
| 682 |
|
| 683 |
while (__hugetlb_opts.path) { |
| 684 |
char path[PATH_MAX + 1]; |
| 685 |
char *next = strchrnul(__hugetlb_opts.path, ':'); |
| 686 |
|
| 687 |
do_scan = 0; |
| 688 |
if (next - __hugetlb_opts.path > PATH_MAX) { |
| 689 |
ERROR("Path too long in HUGETLB_PATH -- " |
| 690 |
"ignoring environment\n"); |
| 691 |
break; |
| 692 |
} |
| 693 |
|
| 694 |
strncpy(path, __hugetlb_opts.path, next - __hugetlb_opts.path); |
| 695 |
path[next - __hugetlb_opts.path] = '\0'; |
| 696 |
add_hugetlbfs_mount(path, 1); |
| 697 |
|
| 698 |
|
| 699 |
__hugetlb_opts.path = *next == '\0' ? NULL : next + 1; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
if (do_scan) |
| 704 |
find_mounts(); |
| 705 |
} |
| 706 |
|
| 707 |
int get_pool_size(long size, struct hpage_pool *pool) |
| 708 |
{ |
| 709 |
long nr_over = 0; |
| 710 |
long nr_used = 0; |
| 711 |
long nr_surp = 0; |
| 712 |
long nr_resv = 0; |
| 713 |
long nr_static = 0; |
| 714 |
|
| 715 |
long it_used = -1; |
| 716 |
long it_surp = -1; |
| 717 |
long it_resv = -1; |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
nr_over = get_huge_page_counter(size, HUGEPAGES_OC); |
| 728 |
if (nr_over < 0) |
| 729 |
nr_over = 0; |
| 730 |
|
| 731 |
|
| 732 |
while (nr_used != it_used || nr_surp != it_surp || nr_resv != it_resv) { |
| 733 |
nr_used = it_used; |
| 734 |
nr_surp = it_surp; |
| 735 |
nr_resv = it_resv; |
| 736 |
|
| 737 |
it_used = get_huge_page_counter(size, HUGEPAGES_TOTAL); |
| 738 |
it_surp = get_huge_page_counter(size, HUGEPAGES_SURP); |
| 739 |
it_resv = get_huge_page_counter(size, HUGEPAGES_RSVD); |
| 740 |
} |
| 741 |
if (nr_surp < 0) |
| 742 |
nr_surp = 0; |
| 743 |
if (nr_resv < 0) |
| 744 |
nr_resv = 0; |
| 745 |
|
| 746 |
nr_static = nr_used - nr_surp; |
| 747 |
|
| 748 |
if (nr_static >= 0) { |
| 749 |
DEBUG("pagesize<%ld> min<%ld> max<%ld> " |
| 750 |
"in-use<%ld>\n", |
| 751 |
size, nr_static, nr_static + nr_over, |
| 752 |
nr_used); |
| 753 |
pool->pagesize = size; |
| 754 |
pool->minimum = nr_static; |
| 755 |
pool->maximum = nr_static + nr_over; |
| 756 |
pool->size = nr_used; |
| 757 |
pool->is_default = 0; |
| 758 |
|
| 759 |
return 1; |
| 760 |
} |
| 761 |
|
| 762 |
return 0; |
| 763 |
} |
| 764 |
|
| 765 |
int hpool_sizes(struct hpage_pool *pools, int pcnt) |
| 766 |
{ |
| 767 |
long default_size; |
| 768 |
int which = 0; |
| 769 |
DIR *dir; |
| 770 |
struct dirent *entry; |
| 771 |
|
| 772 |
default_size = kernel_default_hugepage_size(); |
| 773 |
if (default_size >= 0 && which < pcnt) |
| 774 |
if (get_pool_size(default_size, &pools[which])) { |
| 775 |
pools[which].is_default = 1; |
| 776 |
which++; |
| 777 |
} |
| 778 |
|
| 779 |
dir = opendir(SYSFS_HUGEPAGES_DIR); |
| 780 |
if (dir) { |
| 781 |
while ((entry = readdir(dir))) { |
| 782 |
char *name = entry->d_name; |
| 783 |
long size; |
| 784 |
|
| 785 |
DEBUG("parsing<%s>\n", name); |
| 786 |
if (strncmp(name, "hugepages-", 10) != 0) |
| 787 |
continue; |
| 788 |
name += 10; |
| 789 |
|
| 790 |
size = size_to_smaller_unit(atol(name)); |
| 791 |
if (size < 0 || size == default_size) |
| 792 |
continue; |
| 793 |
|
| 794 |
if (get_pool_size(size, &pools[which])) |
| 795 |
which++; |
| 796 |
} |
| 797 |
closedir(dir); |
| 798 |
} |
| 799 |
|
| 800 |
return (which < pcnt) ? which : -1; |
| 801 |
} |
| 802 |
|
| 803 |
int arch_has_slice_support(void) |
| 804 |
{ |
| 805 |
#ifdef __powerpc64__ |
| 806 |
char mmu_type[16]; |
| 807 |
FILE *fp; |
| 808 |
|
| 809 |
fp = popen("cat /proc/cpuinfo | grep MMU | awk '{ print $3}'", "r"); |
| 810 |
if (!fp || fscanf(fp, "%s", mmu_type) < 0) { |
| 811 |
ERROR("Failed to determine MMU type\n"); |
| 812 |
abort(); |
| 813 |
} |
| 814 |
|
| 815 |
pclose(fp); |
| 816 |
return strcmp(mmu_type, "Hash") == 0; |
| 817 |
#elif defined(__powerpc__) && !defined(PPC_NO_SEGMENTS) |
| 818 |
return 1; |
| 819 |
#else |
| 820 |
return 0; |
| 821 |
#endif |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
int kernel_has_hugepages(void) |
| 828 |
{ |
| 829 |
long default_size = kernel_default_hugepage_size(); |
| 830 |
if (default_size < 0) |
| 831 |
return 0; |
| 832 |
|
| 833 |
return 1; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
int kernel_has_overcommit(void) |
| 841 |
{ |
| 842 |
long default_size = kernel_default_hugepage_size(); |
| 843 |
if (default_size < 0) |
| 844 |
return 0; |
| 845 |
|
| 846 |
if (get_huge_page_counter(default_size, HUGEPAGES_OC) < 0) |
| 847 |
return 0; |
| 848 |
|
| 849 |
return 1; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
long gethugepagesize(void) |
| 867 |
{ |
| 868 |
long hpage_size; |
| 869 |
|
| 870 |
|
| 871 |
if (hpage_sizes_default_idx == -1) { |
| 872 |
errno = hugepagesize_errno = ENOSYS; |
| 873 |
return -1; |
| 874 |
} |
| 875 |
|
| 876 |
errno = 0; |
| 877 |
hpage_size = hpage_sizes[hpage_sizes_default_idx].pagesize; |
| 878 |
return hpage_size; |
| 879 |
} |
| 880 |
|
| 881 |
int gethugepagesizes(long pagesizes[], int n_elem) |
| 882 |
{ |
| 883 |
long default_size; |
| 884 |
DIR *sysfs; |
| 885 |
struct dirent *ent; |
| 886 |
int nr_sizes = 0; |
| 887 |
|
| 888 |
if (n_elem < 0) { |
| 889 |
errno = EINVAL; |
| 890 |
return -1; |
| 891 |
} |
| 892 |
|
| 893 |
if (n_elem > 0 && pagesizes == NULL) { |
| 894 |
errno = EINVAL; |
| 895 |
return -1; |
| 896 |
} |
| 897 |
|
| 898 |
errno = 0; |
| 899 |
|
| 900 |
|
| 901 |
default_size = kernel_default_hugepage_size(); |
| 902 |
if (default_size < 0) |
| 903 |
return 0; |
| 904 |
|
| 905 |
if (pagesizes && (nr_sizes == n_elem)) |
| 906 |
return nr_sizes; |
| 907 |
if (pagesizes) |
| 908 |
pagesizes[nr_sizes] = default_size; |
| 909 |
nr_sizes++; |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
sysfs = opendir(SYSFS_HUGEPAGES_DIR); |
| 916 |
if (!sysfs) { |
| 917 |
if (errno == ENOENT) { |
| 918 |
errno = 0; |
| 919 |
return nr_sizes; |
| 920 |
} else |
| 921 |
return -1; |
| 922 |
} |
| 923 |
while ((ent = readdir(sysfs))) { |
| 924 |
long size; |
| 925 |
|
| 926 |
if (strncmp(ent->d_name, "hugepages-", 10)) |
| 927 |
continue; |
| 928 |
|
| 929 |
size = strtol(ent->d_name + 10, NULL, 10); |
| 930 |
if (size == LONG_MIN || size == LONG_MAX) |
| 931 |
continue; |
| 932 |
size = size_to_smaller_unit(size); |
| 933 |
|
| 934 |
if (size < 0 || size == default_size) |
| 935 |
continue; |
| 936 |
if (pagesizes && (nr_sizes == n_elem)) { |
| 937 |
closedir(sysfs); |
| 938 |
return nr_sizes; |
| 939 |
} |
| 940 |
if (pagesizes) |
| 941 |
pagesizes[nr_sizes] = size; |
| 942 |
nr_sizes++; |
| 943 |
} |
| 944 |
closedir(sysfs); |
| 945 |
|
| 946 |
return nr_sizes; |
| 947 |
} |
| 948 |
|
| 949 |
int getpagesizes(long pagesizes[], int n_elem) |
| 950 |
{ |
| 951 |
int ret; |
| 952 |
|
| 953 |
if (n_elem < 0 || (n_elem > 0 && pagesizes == NULL)) { |
| 954 |
errno = EINVAL; |
| 955 |
return -1; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
if (pagesizes == NULL && n_elem == 0) { |
| 960 |
ret = gethugepagesizes(pagesizes, n_elem); |
| 961 |
} else { |
| 962 |
|
| 963 |
if (pagesizes && n_elem == 0) |
| 964 |
return 0; |
| 965 |
if (pagesizes) |
| 966 |
pagesizes[0] = sysconf(_SC_PAGESIZE); |
| 967 |
|
| 968 |
ret = gethugepagesizes(pagesizes + 1, n_elem - 1); |
| 969 |
} |
| 970 |
if (ret < 0) |
| 971 |
return ret; |
| 972 |
return ret + 1; |
| 973 |
} |
| 974 |
|
| 975 |
int hugetlbfs_test_path(const char *mount) |
| 976 |
{ |
| 977 |
struct statfs64 sb; |
| 978 |
int err; |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
err = statfs64(mount, &sb); |
| 984 |
if (err) |
| 985 |
return -1; |
| 986 |
|
| 987 |
return (sb.f_type == HUGETLBFS_MAGIC); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
long hugetlbfs_test_pagesize(const char *mount) |
| 992 |
{ |
| 993 |
struct statfs64 sb; |
| 994 |
int err; |
| 995 |
|
| 996 |
err = statfs64(mount, &sb); |
| 997 |
if (err) |
| 998 |
return -1; |
| 999 |
|
| 1000 |
if ((sb.f_bsize <= 0) || (sb.f_bsize > LONG_MAX)) |
| 1001 |
return -1; |
| 1002 |
|
| 1003 |
return sb.f_bsize; |
| 1004 |
} |
| 1005 |
|
| 1006 |
const char *hugetlbfs_find_path_for_size(long page_size) |
| 1007 |
{ |
| 1008 |
char *path; |
| 1009 |
int idx; |
| 1010 |
|
| 1011 |
idx = hpage_size_to_index(page_size); |
| 1012 |
if (idx >= 0) { |
| 1013 |
path = hpage_sizes[idx].mount; |
| 1014 |
if (strlen(path)) |
| 1015 |
return path; |
| 1016 |
} |
| 1017 |
return NULL; |
| 1018 |
} |
| 1019 |
|
| 1020 |
const char *hugetlbfs_find_path(void) |
| 1021 |
{ |
| 1022 |
long hpage_size = gethugepagesize(); |
| 1023 |
if (hpage_size > 0) |
| 1024 |
return hugetlbfs_find_path_for_size(hpage_size); |
| 1025 |
else |
| 1026 |
return NULL; |
| 1027 |
} |
| 1028 |
|
| 1029 |
int hugetlbfs_unlinked_fd_for_size(long page_size) |
| 1030 |
{ |
| 1031 |
const char *path; |
| 1032 |
char name[PATH_MAX+1]; |
| 1033 |
int fd; |
| 1034 |
|
| 1035 |
path = hugetlbfs_find_path_for_size(page_size); |
| 1036 |
if (!path) |
| 1037 |
return -1; |
| 1038 |
|
| 1039 |
name[sizeof(name)-1] = '\0'; |
| 1040 |
|
| 1041 |
strcpy(name, path); |
| 1042 |
strncat(name, "/libhugetlbfs.tmp.XXXXXX", sizeof(name)-1); |
| 1043 |
|
| 1044 |
|
| 1045 |
fd = mkstemp64(name); |
| 1046 |
|
| 1047 |
if (fd < 0) { |
| 1048 |
ERROR("mkstemp() failed: %s\n", strerror(errno)); |
| 1049 |
return -1; |
| 1050 |
} |
| 1051 |
|
| 1052 |
unlink(name); |
| 1053 |
|
| 1054 |
return fd; |
| 1055 |
} |
| 1056 |
|
| 1057 |
int hugetlbfs_unlinked_fd(void) |
| 1058 |
{ |
| 1059 |
long hpage_size = gethugepagesize(); |
| 1060 |
if (hpage_size > 0) |
| 1061 |
return hugetlbfs_unlinked_fd_for_size(hpage_size); |
| 1062 |
else |
| 1063 |
return -1; |
| 1064 |
} |
| 1065 |
|
| 1066 |
#define IOV_LEN 64 |
| 1067 |
int hugetlbfs_prefault(void *addr, size_t length) |
| 1068 |
{ |
| 1069 |
size_t offset; |
| 1070 |
struct iovec iov[IOV_LEN]; |
| 1071 |
int ret; |
| 1072 |
int i; |
| 1073 |
int fd; |
| 1074 |
|
| 1075 |
if (!__hugetlbfs_prefault) |
| 1076 |
return 0; |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
fd = open("/dev/zero", O_RDONLY); |
| 1096 |
if (fd < 0) { |
| 1097 |
ERROR("Failed to open /dev/zero for reading\n"); |
| 1098 |
return -ENOMEM; |
| 1099 |
} |
| 1100 |
|
| 1101 |
for (offset = 0; offset < length; ) { |
| 1102 |
for (i = 0; i < IOV_LEN && offset < length; i++) { |
| 1103 |
iov[i].iov_base = addr + offset; |
| 1104 |
iov[i].iov_len = 1; |
| 1105 |
offset += gethugepagesize(); |
| 1106 |
} |
| 1107 |
ret = readv(fd, iov, i); |
| 1108 |
if (ret != i) { |
| 1109 |
DEBUG("Got %d of %d requested; err=%d\n", ret, |
| 1110 |
i, ret < 0 ? errno : 0); |
| 1111 |
WARNING("Failed to reserve %ld huge pages " |
| 1112 |
"for new region\n", |
| 1113 |
length / gethugepagesize()); |
| 1114 |
close(fd); |
| 1115 |
return -ENOMEM; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
close(fd); |
| 1120 |
return 0; |
| 1121 |
} |
| 1122 |
|
| 1123 |
long get_huge_page_counter(long pagesize, unsigned int counter) |
| 1124 |
{ |
| 1125 |
char file[PATH_MAX+1]; |
| 1126 |
char *key; |
| 1127 |
|
| 1128 |
if (select_pool_counter(counter, pagesize, file, &key)) |
| 1129 |
return -1; |
| 1130 |
|
| 1131 |
if (access(file, O_RDONLY)) |
| 1132 |
return -1; |
| 1133 |
|
| 1134 |
return file_read_ulong(file, key); |
| 1135 |
} |
| 1136 |
|
| 1137 |
int set_huge_page_counter(long pagesize, unsigned int counter, |
| 1138 |
unsigned long val) |
| 1139 |
{ |
| 1140 |
char file[PATH_MAX+1]; |
| 1141 |
|
| 1142 |
if (select_pool_counter(counter, pagesize, file, NULL)) |
| 1143 |
return -1; |
| 1144 |
|
| 1145 |
return file_write_ulong(file, val); |
| 1146 |
} |
| 1147 |
|
| 1148 |
int set_nr_hugepages(long pagesize, unsigned long val) |
| 1149 |
{ |
| 1150 |
return set_huge_page_counter(pagesize, HUGEPAGES_TOTAL, val); |
| 1151 |
} |
| 1152 |
|
| 1153 |
int set_nr_overcommit_hugepages(long pagesize, unsigned long val) |
| 1154 |
{ |
| 1155 |
DEBUG("setting HUGEPAGES_OC to %ld\n", val); |
| 1156 |
return set_huge_page_counter(pagesize, HUGEPAGES_OC, val); |
| 1157 |
} |
| 1158 |
|
| 1159 |
long read_nr_overcommit(long page_size) |
| 1160 |
{ |
| 1161 |
if (!kernel_has_overcommit()) |
| 1162 |
return -1; |
| 1163 |
|
| 1164 |
return get_huge_page_counter(page_size, HUGEPAGES_OC); |
| 1165 |
} |
| 1166 |
|
| 1167 |
void restore_overcommit_pages(long page_size, long oc_pool) |
| 1168 |
{ |
| 1169 |
if (!kernel_has_overcommit()) |
| 1170 |
return; |
| 1171 |
|
| 1172 |
set_nr_overcommit_hugepages(page_size, oc_pool); |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
#define MAPS_BUF_SZ 4096 |
| 1180 |
long dump_proc_pid_maps() |
| 1181 |
{ |
| 1182 |
FILE *f; |
| 1183 |
char line[MAPS_BUF_SZ]; |
| 1184 |
size_t ret; |
| 1185 |
long result = -1; |
| 1186 |
|
| 1187 |
f = fopen("/proc/self/maps", "r"); |
| 1188 |
if (!f) { |
| 1189 |
ERROR("Failed to open /proc/self/maps\n"); |
| 1190 |
return -1; |
| 1191 |
} |
| 1192 |
|
| 1193 |
while (1) { |
| 1194 |
ret = fread(line, sizeof(char), MAPS_BUF_SZ, f); |
| 1195 |
if (ret < MAPS_BUF_SZ) { |
| 1196 |
if (ferror(f)) { |
| 1197 |
ERROR("Failed to read /proc/self/maps\n"); |
| 1198 |
goto out; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
break; |
| 1203 |
} |
| 1204 |
|
| 1205 |
ret = fwrite(line, sizeof(char), ret, stderr); |
| 1206 |
if (ret == 0) { |
| 1207 |
if (ferror(stderr)) { |
| 1208 |
ERROR("Failed to write /proc/self/maps to stderr\n"); |
| 1209 |
goto out; |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
ERROR("Cannot write /proc/self/maps because stderr reached EOF, " |
| 1216 |
"was it closed?\n"); |
| 1217 |
goto out; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
result = 0; |
| 1222 |
out: |
| 1223 |
fclose(f); |
| 1224 |
return result; |
| 1225 |
} |
| 1226 |
|
| 1227 |
long read_meminfo(const char *tag) |
| 1228 |
{ |
| 1229 |
return file_read_ulong(MEMINFO, tag); |
| 1230 |
} |