1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
19 #include <sys/queue.h>
22 #include <linux/mman.h> /* for hugetlb-related flags */
24 #include <rte_memory.h>
26 #include <rte_launch.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_debug.h>
31 #include <rte_common.h>
32 #include "rte_string_fns.h"
34 #include "eal_private.h"
35 #include "eal_internal_cfg.h"
36 #include "eal_hugepages.h"
37 #include "eal_filesystem.h"
39 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
40 static const char sys_pages_numa_dir_path[] = "/sys/devices/system/node";
43 * Uses mmap to create a shared memory area for storage of data
44 * Used in this file to store the hugepage file map on disk
47 map_shared_memory(const char *filename, const size_t mem_size, int flags)
50 int fd = open(filename, flags, 0600);
53 if (ftruncate(fd, mem_size) < 0) {
57 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
64 open_shared_memory(const char *filename, const size_t mem_size)
66 return map_shared_memory(filename, mem_size, O_RDWR);
70 create_shared_memory(const char *filename, const size_t mem_size)
72 return map_shared_memory(filename, mem_size, O_RDWR | O_CREAT);
75 static int get_hp_sysfs_value(const char *subdir, const char *file, unsigned long *val)
79 snprintf(path, sizeof(path), "%s/%s/%s",
80 sys_dir_path, subdir, file);
81 return eal_parse_sysfs_value(path, val);
84 /* this function is only called from eal_hugepage_info_init which itself
85 * is only called from a primary process */
87 get_num_hugepages(const char *subdir, size_t sz, unsigned int reusable_pages)
89 unsigned long resv_pages, num_pages, over_pages, surplus_pages;
90 const char *nr_hp_file = "free_hugepages";
91 const char *nr_rsvd_file = "resv_hugepages";
92 const char *nr_over_file = "nr_overcommit_hugepages";
93 const char *nr_splus_file = "surplus_hugepages";
95 /* first, check how many reserved pages kernel reports */
96 if (get_hp_sysfs_value(subdir, nr_rsvd_file, &resv_pages) < 0)
99 if (get_hp_sysfs_value(subdir, nr_hp_file, &num_pages) < 0)
102 if (get_hp_sysfs_value(subdir, nr_over_file, &over_pages) < 0)
105 if (get_hp_sysfs_value(subdir, nr_splus_file, &surplus_pages) < 0)
108 /* adjust num_pages */
109 if (num_pages >= resv_pages)
110 num_pages -= resv_pages;
114 if (over_pages >= surplus_pages)
115 over_pages -= surplus_pages;
119 if (num_pages == 0 && over_pages == 0 && reusable_pages)
120 RTE_LOG(WARNING, EAL, "No available %zu kB hugepages reported\n",
123 num_pages += over_pages;
124 if (num_pages < over_pages) /* overflow */
125 num_pages = UINT32_MAX;
127 num_pages += reusable_pages;
128 if (num_pages < reusable_pages) /* overflow */
129 num_pages = UINT32_MAX;
131 /* we want to return a uint32_t and more than this looks suspicious
133 if (num_pages > UINT32_MAX)
134 num_pages = UINT32_MAX;
140 get_num_hugepages_on_node(const char *subdir, unsigned int socket, size_t sz)
142 char path[PATH_MAX], socketpath[PATH_MAX];
144 unsigned long num_pages = 0;
145 const char *nr_hp_file = "free_hugepages";
147 snprintf(socketpath, sizeof(socketpath), "%s/node%u/hugepages",
148 sys_pages_numa_dir_path, socket);
150 socketdir = opendir(socketpath);
152 /* Keep calm and carry on */
155 /* Can't find socket dir, so ignore it */
159 snprintf(path, sizeof(path), "%s/%s/%s",
160 socketpath, subdir, nr_hp_file);
161 if (eal_parse_sysfs_value(path, &num_pages) < 0)
165 RTE_LOG(WARNING, EAL, "No free %zu kB hugepages reported on node %u\n",
169 * we want to return a uint32_t and more than this looks suspicious
172 if (num_pages > UINT32_MAX)
173 num_pages = UINT32_MAX;
179 get_default_hp_size(void)
181 const char proc_meminfo[] = "/proc/meminfo";
182 const char str_hugepagesz[] = "Hugepagesize:";
183 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
185 unsigned long long size = 0;
187 FILE *fd = fopen(proc_meminfo, "r");
189 rte_panic("Cannot open %s\n", proc_meminfo);
190 while(fgets(buffer, sizeof(buffer), fd)){
191 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
192 size = rte_str_to_size(&buffer[hugepagesz_len]);
198 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
203 get_hugepage_dir(uint64_t hugepage_sz, char *hugedir, int len)
205 enum proc_mount_fieldnames {
212 static uint64_t default_size = 0;
213 const char proc_mounts[] = "/proc/mounts";
214 const char hugetlbfs_str[] = "hugetlbfs";
215 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
216 const char pagesize_opt[] = "pagesize=";
217 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
218 const char split_tok = ' ';
219 char *splitstr[_FIELDNAME_MAX];
220 char found[PATH_MAX] = "";
222 const struct internal_config *internal_conf =
223 eal_get_internal_configuration();
227 * If the specified dir doesn't exist, we can't match it.
229 if (internal_conf->hugepage_dir != NULL &&
230 stat(internal_conf->hugepage_dir, &st) != 0) {
234 FILE *fd = fopen(proc_mounts, "r");
236 rte_panic("Cannot open %s\n", proc_mounts);
238 if (default_size == 0)
239 default_size = get_default_hp_size();
241 while (fgets(buf, sizeof(buf), fd)){
242 const char *pagesz_str;
244 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
245 split_tok) != _FIELDNAME_MAX) {
246 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
247 break; /* return NULL */
250 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) != 0)
253 pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
255 /* if no explicit page size, the default page size is compared */
256 if (pagesz_str == NULL) {
257 if (hugepage_sz != default_size)
260 /* there is an explicit page size, so check it */
262 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
263 if (pagesz != hugepage_sz)
268 * If no --huge-dir option has been given, we're done.
270 if (internal_conf->hugepage_dir == NULL) {
271 strlcpy(found, splitstr[MOUNTPT], len);
276 * Ignore any mount that doesn't contain the --huge-dir
279 if (strncmp(internal_conf->hugepage_dir, splitstr[MOUNTPT],
280 strlen(splitstr[MOUNTPT])) != 0) {
285 * We found a match, but only prefer it if it's a longer match
286 * (so /mnt/1 is preferred over /mnt for matching /mnt/1/2)).
288 if (strlen(splitstr[MOUNTPT]) > strlen(found))
289 strlcpy(found, splitstr[MOUNTPT], len);
290 } /* end while fgets */
294 if (found[0] != '\0') {
295 /* If needed, return the requested dir, not the mount point. */
296 strlcpy(hugedir, internal_conf->hugepage_dir != NULL ?
297 internal_conf->hugepage_dir : found, len);
304 struct walk_hugedir_data {
307 const char *file_name;
311 typedef void (walk_hugedir_t)(const struct walk_hugedir_data *whd);
314 * Search the hugepage directory for whatever hugepage files there are.
315 * Check if the file is in use by another DPDK process.
316 * If not, execute a callback on it.
319 walk_hugedir(const char *hugedir, walk_hugedir_t *cb, void *user_data)
322 struct dirent *dirent;
323 int dir_fd, fd, lck_result;
324 const char filter[] = "*map_*"; /* matches hugepage files */
326 dir = opendir(hugedir);
328 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
334 dirent = readdir(dir);
336 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
341 while (dirent != NULL) {
342 /* skip files that don't match the hugepage pattern */
343 if (fnmatch(filter, dirent->d_name, 0) > 0) {
344 dirent = readdir(dir);
348 /* try and lock the file */
349 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
351 /* skip to next file */
353 dirent = readdir(dir);
357 /* non-blocking lock */
358 lck_result = flock(fd, LOCK_EX | LOCK_NB);
360 /* if lock succeeds, execute callback */
361 if (lck_result != -1)
362 cb(&(struct walk_hugedir_data){
365 .file_name = dirent->d_name,
366 .user_data = user_data,
370 dirent = readdir(dir);
380 RTE_LOG(ERR, EAL, "Error while walking hugepage dir: %s\n",
387 clear_hugedir_cb(const struct walk_hugedir_data *whd)
389 unlinkat(whd->dir_fd, whd->file_name, 0);
392 /* Remove hugepage files not used by other DPDK processes from a directory. */
394 clear_hugedir(const char *hugedir)
396 return walk_hugedir(hugedir, clear_hugedir_cb, NULL);
400 inspect_hugedir_cb(const struct walk_hugedir_data *whd)
402 uint64_t *total_size = whd->user_data;
405 if (fstat(whd->file_fd, &st) < 0)
406 RTE_LOG(DEBUG, EAL, "%s(): stat(\"%s\") failed: %s",
407 __func__, whd->file_name, strerror(errno));
409 (*total_size) += st.st_size;
413 * Count the total size in bytes of all files in the directory
414 * not mapped by other DPDK process.
417 inspect_hugedir(const char *hugedir, uint64_t *total_size)
419 return walk_hugedir(hugedir, inspect_hugedir_cb, total_size);
423 compare_hpi(const void *a, const void *b)
425 const struct hugepage_info *hpi_a = a;
426 const struct hugepage_info *hpi_b = b;
428 return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
432 calc_num_pages(struct hugepage_info *hpi, struct dirent *dirent,
433 unsigned int reusable_pages)
435 uint64_t total_pages = 0;
437 const struct internal_config *internal_conf =
438 eal_get_internal_configuration();
441 * first, try to put all hugepages into relevant sockets, but
442 * if first attempts fails, fall back to collecting all pages
443 * in one socket and sorting them later
448 * We also don't want to do this for legacy init.
449 * When there are hugepage files to reuse it is unknown
450 * what NUMA node the pages are on.
451 * This could be determined by mapping,
452 * but it is precisely what hugepage file reuse is trying to avoid.
454 if (!internal_conf->legacy_mem && reusable_pages == 0)
455 for (i = 0; i < rte_socket_count(); i++) {
456 int socket = rte_socket_id_by_idx(i);
457 unsigned int num_pages =
458 get_num_hugepages_on_node(
459 dirent->d_name, socket,
461 hpi->num_pages[socket] = num_pages;
462 total_pages += num_pages;
465 * we failed to sort memory from the get go, so fall
468 if (total_pages == 0) {
469 hpi->num_pages[0] = get_num_hugepages(dirent->d_name,
470 hpi->hugepage_sz, reusable_pages);
473 /* for 32-bit systems, limit number of hugepages to
474 * 1GB per page size */
475 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
476 RTE_PGSIZE_1G / hpi->hugepage_sz);
482 hugepage_info_init(void)
483 { const char dirent_start_text[] = "hugepages-";
484 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
485 unsigned int i, num_sizes = 0;
486 uint64_t reusable_bytes;
487 unsigned int reusable_pages;
489 struct dirent *dirent;
490 struct internal_config *internal_conf =
491 eal_get_internal_configuration();
493 dir = opendir(sys_dir_path);
496 "Cannot open directory %s to read system hugepage info\n",
501 for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
502 struct hugepage_info *hpi;
504 if (strncmp(dirent->d_name, dirent_start_text,
505 dirent_start_len) != 0)
508 if (num_sizes >= MAX_HUGEPAGE_SIZES)
511 hpi = &internal_conf->hugepage_info[num_sizes];
513 rte_str_to_size(&dirent->d_name[dirent_start_len]);
515 /* first, check if we have a mountpoint */
516 if (get_hugepage_dir(hpi->hugepage_sz,
517 hpi->hugedir, sizeof(hpi->hugedir)) < 0) {
520 num_pages = get_num_hugepages(dirent->d_name,
521 hpi->hugepage_sz, 0);
524 "%" PRIu32 " hugepages of size "
525 "%" PRIu64 " reserved, but no mounted "
526 "hugetlbfs found for that size\n",
527 num_pages, hpi->hugepage_sz);
528 /* if we have kernel support for reserving hugepages
529 * through mmap, and we're in in-memory mode, treat this
530 * page size as valid. we cannot be in legacy mode at
531 * this point because we've checked this earlier in the
534 #ifdef MAP_HUGE_SHIFT
535 if (internal_conf->in_memory) {
536 RTE_LOG(DEBUG, EAL, "In-memory mode enabled, "
537 "hugepages of size %" PRIu64 " bytes "
538 "will be allocated anonymously\n",
540 calc_num_pages(hpi, dirent, 0);
547 /* try to obtain a writelock */
548 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
550 /* if blocking lock failed */
551 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
553 "Failed to lock hugepage directory!\n");
558 * Check for existing hugepage files and either remove them
559 * or count how many of them can be reused.
562 if (!internal_conf->hugepage_file.unlink_existing) {
564 if (inspect_hugedir(hpi->hugedir,
565 &reusable_bytes) < 0)
567 RTE_ASSERT(reusable_bytes % hpi->hugepage_sz == 0);
568 reusable_pages = reusable_bytes / hpi->hugepage_sz;
569 } else if (clear_hugedir(hpi->hugedir) < 0) {
572 calc_num_pages(hpi, dirent, reusable_pages);
578 /* something went wrong, and we broke from the for loop above */
582 internal_conf->num_hugepage_sizes = num_sizes;
584 /* sort the page directory entries by size, largest to smallest */
585 qsort(&internal_conf->hugepage_info[0], num_sizes,
586 sizeof(internal_conf->hugepage_info[0]), compare_hpi);
588 /* now we have all info, check we have at least one valid size */
589 for (i = 0; i < num_sizes; i++) {
590 /* pages may no longer all be on socket 0, so check all */
591 unsigned int j, num_pages = 0;
592 struct hugepage_info *hpi = &internal_conf->hugepage_info[i];
594 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
595 num_pages += hpi->num_pages[j];
600 /* no valid hugepage mounts available, return error */
605 * when we initialize the hugepage info, everything goes
606 * to socket 0 by default. it will later get sorted by memory
607 * initialization procedure.
610 eal_hugepage_info_init(void)
612 struct hugepage_info *hpi, *tmp_hpi;
614 struct internal_config *internal_conf =
615 eal_get_internal_configuration();
617 if (hugepage_info_init() < 0)
620 /* for no shared files mode, we're done */
621 if (internal_conf->no_shconf)
624 hpi = &internal_conf->hugepage_info[0];
626 tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
627 sizeof(internal_conf->hugepage_info));
628 if (tmp_hpi == NULL) {
629 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
633 memcpy(tmp_hpi, hpi, sizeof(internal_conf->hugepage_info));
635 /* we've copied file descriptors along with everything else, but they
636 * will be invalid in secondary process, so overwrite them
638 for (i = 0; i < RTE_DIM(internal_conf->hugepage_info); i++) {
639 struct hugepage_info *tmp = &tmp_hpi[i];
640 tmp->lock_descriptor = -1;
643 if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
644 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
650 int eal_hugepage_info_read(void)
652 struct internal_config *internal_conf =
653 eal_get_internal_configuration();
654 struct hugepage_info *hpi = &internal_conf->hugepage_info[0];
655 struct hugepage_info *tmp_hpi;
657 tmp_hpi = open_shared_memory(eal_hugepage_info_path(),
658 sizeof(internal_conf->hugepage_info));
659 if (tmp_hpi == NULL) {
660 RTE_LOG(ERR, EAL, "Failed to open shared memory!\n");
664 memcpy(hpi, tmp_hpi, sizeof(internal_conf->hugepage_info));
666 if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
667 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");