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)
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)
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 /* we want to return a uint32_t and more than this looks suspicious
129 if (num_pages > UINT32_MAX)
130 num_pages = UINT32_MAX;
136 get_num_hugepages_on_node(const char *subdir, unsigned int socket, size_t sz)
138 char path[PATH_MAX], socketpath[PATH_MAX];
140 unsigned long num_pages = 0;
141 const char *nr_hp_file = "free_hugepages";
143 snprintf(socketpath, sizeof(socketpath), "%s/node%u/hugepages",
144 sys_pages_numa_dir_path, socket);
146 socketdir = opendir(socketpath);
148 /* Keep calm and carry on */
151 /* Can't find socket dir, so ignore it */
155 snprintf(path, sizeof(path), "%s/%s/%s",
156 socketpath, subdir, nr_hp_file);
157 if (eal_parse_sysfs_value(path, &num_pages) < 0)
161 RTE_LOG(WARNING, EAL, "No free %zu kB hugepages reported on node %u\n",
165 * we want to return a uint32_t and more than this looks suspicious
168 if (num_pages > UINT32_MAX)
169 num_pages = UINT32_MAX;
175 get_default_hp_size(void)
177 const char proc_meminfo[] = "/proc/meminfo";
178 const char str_hugepagesz[] = "Hugepagesize:";
179 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
181 unsigned long long size = 0;
183 FILE *fd = fopen(proc_meminfo, "r");
185 rte_panic("Cannot open %s\n", proc_meminfo);
186 while(fgets(buffer, sizeof(buffer), fd)){
187 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
188 size = rte_str_to_size(&buffer[hugepagesz_len]);
194 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
199 get_hugepage_dir(uint64_t hugepage_sz, char *hugedir, int len)
201 enum proc_mount_fieldnames {
208 static uint64_t default_size = 0;
209 const char proc_mounts[] = "/proc/mounts";
210 const char hugetlbfs_str[] = "hugetlbfs";
211 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
212 const char pagesize_opt[] = "pagesize=";
213 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
214 const char split_tok = ' ';
215 char *splitstr[_FIELDNAME_MAX];
218 const struct internal_config *internal_conf =
219 eal_get_internal_configuration();
221 FILE *fd = fopen(proc_mounts, "r");
223 rte_panic("Cannot open %s\n", proc_mounts);
225 if (default_size == 0)
226 default_size = get_default_hp_size();
228 while (fgets(buf, sizeof(buf), fd)){
229 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
230 split_tok) != _FIELDNAME_MAX) {
231 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
232 break; /* return NULL */
235 /* we have a specified --huge-dir option, only examine that dir */
236 if (internal_conf->hugepage_dir != NULL &&
237 strcmp(splitstr[MOUNTPT], internal_conf->hugepage_dir) != 0)
240 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
241 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
243 /* if no explicit page size, the default page size is compared */
244 if (pagesz_str == NULL){
245 if (hugepage_sz == default_size){
246 strlcpy(hugedir, splitstr[MOUNTPT], len);
251 /* there is an explicit page size, so check it */
253 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
254 if (pagesz == hugepage_sz) {
255 strlcpy(hugedir, splitstr[MOUNTPT], len);
260 } /* end if strncmp hugetlbfs */
261 } /* end while fgets */
268 * Clear the hugepage directory of whatever hugepage files
269 * there are. Checks if the file is locked (i.e.
270 * if it's in use by another DPDK process).
273 clear_hugedir(const char * hugedir)
276 struct dirent *dirent;
277 int dir_fd, fd, lck_result;
278 const char filter[] = "*map_*"; /* matches hugepage files */
281 dir = opendir(hugedir);
283 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
289 dirent = readdir(dir);
291 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
296 while(dirent != NULL){
297 /* skip files that don't match the hugepage pattern */
298 if (fnmatch(filter, dirent->d_name, 0) > 0) {
299 dirent = readdir(dir);
303 /* try and lock the file */
304 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
306 /* skip to next file */
308 dirent = readdir(dir);
312 /* non-blocking lock */
313 lck_result = flock(fd, LOCK_EX | LOCK_NB);
315 /* if lock succeeds, remove the file */
316 if (lck_result != -1)
317 unlinkat(dir_fd, dirent->d_name, 0);
319 dirent = readdir(dir);
329 RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
336 compare_hpi(const void *a, const void *b)
338 const struct hugepage_info *hpi_a = a;
339 const struct hugepage_info *hpi_b = b;
341 return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
345 calc_num_pages(struct hugepage_info *hpi, struct dirent *dirent)
347 uint64_t total_pages = 0;
349 const struct internal_config *internal_conf =
350 eal_get_internal_configuration();
353 * first, try to put all hugepages into relevant sockets, but
354 * if first attempts fails, fall back to collecting all pages
355 * in one socket and sorting them later
358 /* we also don't want to do this for legacy init */
359 if (!internal_conf->legacy_mem)
360 for (i = 0; i < rte_socket_count(); i++) {
361 int socket = rte_socket_id_by_idx(i);
362 unsigned int num_pages =
363 get_num_hugepages_on_node(
364 dirent->d_name, socket,
366 hpi->num_pages[socket] = num_pages;
367 total_pages += num_pages;
370 * we failed to sort memory from the get go, so fall
373 if (total_pages == 0) {
374 hpi->num_pages[0] = get_num_hugepages(dirent->d_name,
378 /* for 32-bit systems, limit number of hugepages to
379 * 1GB per page size */
380 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
381 RTE_PGSIZE_1G / hpi->hugepage_sz);
387 hugepage_info_init(void)
388 { const char dirent_start_text[] = "hugepages-";
389 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
390 unsigned int i, num_sizes = 0;
392 struct dirent *dirent;
393 struct internal_config *internal_conf =
394 eal_get_internal_configuration();
396 dir = opendir(sys_dir_path);
399 "Cannot open directory %s to read system hugepage info\n",
404 for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
405 struct hugepage_info *hpi;
407 if (strncmp(dirent->d_name, dirent_start_text,
408 dirent_start_len) != 0)
411 if (num_sizes >= MAX_HUGEPAGE_SIZES)
414 hpi = &internal_conf->hugepage_info[num_sizes];
416 rte_str_to_size(&dirent->d_name[dirent_start_len]);
418 /* first, check if we have a mountpoint */
419 if (get_hugepage_dir(hpi->hugepage_sz,
420 hpi->hugedir, sizeof(hpi->hugedir)) < 0) {
423 num_pages = get_num_hugepages(dirent->d_name,
427 "%" PRIu32 " hugepages of size "
428 "%" PRIu64 " reserved, but no mounted "
429 "hugetlbfs found for that size\n",
430 num_pages, hpi->hugepage_sz);
431 /* if we have kernel support for reserving hugepages
432 * through mmap, and we're in in-memory mode, treat this
433 * page size as valid. we cannot be in legacy mode at
434 * this point because we've checked this earlier in the
437 #ifdef MAP_HUGE_SHIFT
438 if (internal_conf->in_memory) {
439 RTE_LOG(DEBUG, EAL, "In-memory mode enabled, "
440 "hugepages of size %" PRIu64 " bytes "
441 "will be allocated anonymously\n",
443 calc_num_pages(hpi, dirent);
450 /* try to obtain a writelock */
451 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
453 /* if blocking lock failed */
454 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
456 "Failed to lock hugepage directory!\n");
459 /* clear out the hugepages dir from unused pages */
460 if (clear_hugedir(hpi->hugedir) == -1)
463 calc_num_pages(hpi, dirent);
469 /* something went wrong, and we broke from the for loop above */
473 internal_conf->num_hugepage_sizes = num_sizes;
475 /* sort the page directory entries by size, largest to smallest */
476 qsort(&internal_conf->hugepage_info[0], num_sizes,
477 sizeof(internal_conf->hugepage_info[0]), compare_hpi);
479 /* now we have all info, check we have at least one valid size */
480 for (i = 0; i < num_sizes; i++) {
481 /* pages may no longer all be on socket 0, so check all */
482 unsigned int j, num_pages = 0;
483 struct hugepage_info *hpi = &internal_conf->hugepage_info[i];
485 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
486 num_pages += hpi->num_pages[j];
491 /* no valid hugepage mounts available, return error */
496 * when we initialize the hugepage info, everything goes
497 * to socket 0 by default. it will later get sorted by memory
498 * initialization procedure.
501 eal_hugepage_info_init(void)
503 struct hugepage_info *hpi, *tmp_hpi;
505 struct internal_config *internal_conf =
506 eal_get_internal_configuration();
508 if (hugepage_info_init() < 0)
511 /* for no shared files mode, we're done */
512 if (internal_conf->no_shconf)
515 hpi = &internal_conf->hugepage_info[0];
517 tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
518 sizeof(internal_conf->hugepage_info));
519 if (tmp_hpi == NULL) {
520 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
524 memcpy(tmp_hpi, hpi, sizeof(internal_conf->hugepage_info));
526 /* we've copied file descriptors along with everything else, but they
527 * will be invalid in secondary process, so overwrite them
529 for (i = 0; i < RTE_DIM(internal_conf->hugepage_info); i++) {
530 struct hugepage_info *tmp = &tmp_hpi[i];
531 tmp->lock_descriptor = -1;
534 if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
535 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
541 int eal_hugepage_info_read(void)
543 struct internal_config *internal_conf =
544 eal_get_internal_configuration();
545 struct hugepage_info *hpi = &internal_conf->hugepage_info[0];
546 struct hugepage_info *tmp_hpi;
548 tmp_hpi = open_shared_memory(eal_hugepage_info_path(),
549 sizeof(internal_conf->hugepage_info));
550 if (tmp_hpi == NULL) {
551 RTE_LOG(ERR, EAL, "Failed to open shared memory!\n");
555 memcpy(hpi, tmp_hpi, sizeof(internal_conf->hugepage_info));
557 if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
558 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");