1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
18 #include <sys/queue.h>
21 #include <rte_memory.h>
23 #include <rte_launch.h>
24 #include <rte_per_lcore.h>
25 #include <rte_lcore.h>
26 #include <rte_debug.h>
28 #include <rte_common.h>
29 #include "rte_string_fns.h"
30 #include "eal_internal_cfg.h"
31 #include "eal_hugepages.h"
32 #include "eal_filesystem.h"
34 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
35 static const char sys_pages_numa_dir_path[] = "/sys/devices/system/node";
38 * Uses mmap to create a shared memory area for storage of data
39 * Used in this file to store the hugepage file map on disk
42 map_shared_memory(const char *filename, const size_t mem_size, int flags)
45 int fd = open(filename, flags, 0666);
48 if (ftruncate(fd, mem_size) < 0) {
52 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
59 open_shared_memory(const char *filename, const size_t mem_size)
61 return map_shared_memory(filename, mem_size, O_RDWR);
65 create_shared_memory(const char *filename, const size_t mem_size)
67 return map_shared_memory(filename, mem_size, O_RDWR | O_CREAT);
70 /* this function is only called from eal_hugepage_info_init which itself
71 * is only called from a primary process */
73 get_num_hugepages(const char *subdir)
76 long unsigned resv_pages, num_pages = 0;
77 const char *nr_hp_file = "free_hugepages";
78 const char *nr_rsvd_file = "resv_hugepages";
80 /* first, check how many reserved pages kernel reports */
81 snprintf(path, sizeof(path), "%s/%s/%s",
82 sys_dir_path, subdir, nr_rsvd_file);
83 if (eal_parse_sysfs_value(path, &resv_pages) < 0)
86 snprintf(path, sizeof(path), "%s/%s/%s",
87 sys_dir_path, subdir, nr_hp_file);
88 if (eal_parse_sysfs_value(path, &num_pages) < 0)
92 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
95 /* adjust num_pages */
96 if (num_pages >= resv_pages)
97 num_pages -= resv_pages;
101 /* we want to return a uint32_t and more than this looks suspicious
103 if (num_pages > UINT32_MAX)
104 num_pages = UINT32_MAX;
110 get_num_hugepages_on_node(const char *subdir, unsigned int socket)
112 char path[PATH_MAX], socketpath[PATH_MAX];
114 unsigned long num_pages = 0;
115 const char *nr_hp_file = "free_hugepages";
117 snprintf(socketpath, sizeof(socketpath), "%s/node%u/hugepages",
118 sys_pages_numa_dir_path, socket);
120 socketdir = opendir(socketpath);
122 /* Keep calm and carry on */
125 /* Can't find socket dir, so ignore it */
129 snprintf(path, sizeof(path), "%s/%s/%s",
130 socketpath, subdir, nr_hp_file);
131 if (eal_parse_sysfs_value(path, &num_pages) < 0)
135 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
139 * we want to return a uint32_t and more than this looks suspicious
142 if (num_pages > UINT32_MAX)
143 num_pages = UINT32_MAX;
149 get_default_hp_size(void)
151 const char proc_meminfo[] = "/proc/meminfo";
152 const char str_hugepagesz[] = "Hugepagesize:";
153 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
155 unsigned long long size = 0;
157 FILE *fd = fopen(proc_meminfo, "r");
159 rte_panic("Cannot open %s\n", proc_meminfo);
160 while(fgets(buffer, sizeof(buffer), fd)){
161 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
162 size = rte_str_to_size(&buffer[hugepagesz_len]);
168 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
173 get_hugepage_dir(uint64_t hugepage_sz)
175 enum proc_mount_fieldnames {
182 static uint64_t default_size = 0;
183 const char proc_mounts[] = "/proc/mounts";
184 const char hugetlbfs_str[] = "hugetlbfs";
185 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
186 const char pagesize_opt[] = "pagesize=";
187 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
188 const char split_tok = ' ';
189 char *splitstr[_FIELDNAME_MAX];
193 FILE *fd = fopen(proc_mounts, "r");
195 rte_panic("Cannot open %s\n", proc_mounts);
197 if (default_size == 0)
198 default_size = get_default_hp_size();
200 while (fgets(buf, sizeof(buf), fd)){
201 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
202 split_tok) != _FIELDNAME_MAX) {
203 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
204 break; /* return NULL */
207 /* we have a specified --huge-dir option, only examine that dir */
208 if (internal_config.hugepage_dir != NULL &&
209 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
212 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
213 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
215 /* if no explicit page size, the default page size is compared */
216 if (pagesz_str == NULL){
217 if (hugepage_sz == default_size){
218 retval = strdup(splitstr[MOUNTPT]);
222 /* there is an explicit page size, so check it */
224 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
225 if (pagesz == hugepage_sz) {
226 retval = strdup(splitstr[MOUNTPT]);
230 } /* end if strncmp hugetlbfs */
231 } /* end while fgets */
238 * uses fstat to report the size of a file on disk
241 get_file_size(int fd)
244 if (fstat(fd, &st) < 0)
250 * Clear the hugepage directory of whatever hugepage files
251 * there are. Checks if the file is locked (i.e.
252 * if it's in use by another DPDK process).
255 clear_hugedir(const char * hugedir)
258 struct dirent *dirent;
259 int dir_fd, fd, lck_result;
260 const char filter[] = "*map_*"; /* matches hugepage files */
263 dir = opendir(hugedir);
265 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
271 dirent = readdir(dir);
273 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
278 while(dirent != NULL){
279 struct flock lck = {0};
281 /* skip files that don't match the hugepage pattern */
282 if (fnmatch(filter, dirent->d_name, 0) > 0) {
283 dirent = readdir(dir);
287 /* try and lock the file */
288 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
290 /* skip to next file */
292 dirent = readdir(dir);
296 /* non-blocking lock */
297 lck.l_type = F_RDLCK;
298 lck.l_whence = SEEK_SET;
300 lck.l_len = get_file_size(fd);
302 lck_result = fcntl(fd, F_SETLK, &lck);
304 /* if lock succeeds, unlock and remove the file */
305 if (lck_result != -1) {
306 lck.l_type = F_UNLCK;
307 fcntl(fd, F_SETLK, &lck);
308 unlinkat(dir_fd, dirent->d_name, 0);
311 dirent = readdir(dir);
321 RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
328 compare_hpi(const void *a, const void *b)
330 const struct hugepage_info *hpi_a = a;
331 const struct hugepage_info *hpi_b = b;
333 return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
337 hugepage_info_init(void)
338 { const char dirent_start_text[] = "hugepages-";
339 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
340 unsigned int i, total_pages, num_sizes = 0;
342 struct dirent *dirent;
344 dir = opendir(sys_dir_path);
347 "Cannot open directory %s to read system hugepage info\n",
352 for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
353 struct hugepage_info *hpi;
356 if (strncmp(dirent->d_name, dirent_start_text,
357 dirent_start_len) != 0)
360 if (num_sizes >= MAX_HUGEPAGE_SIZES)
363 hpi = &internal_config.hugepage_info[num_sizes];
365 rte_str_to_size(&dirent->d_name[dirent_start_len]);
366 hugedir = get_hugepage_dir(hpi->hugepage_sz);
368 /* first, check if we have a mountpoint */
369 if (hugedir == NULL) {
372 num_pages = get_num_hugepages(dirent->d_name);
375 "%" PRIu32 " hugepages of size "
376 "%" PRIu64 " reserved, but no mounted "
377 "hugetlbfs found for that size\n",
378 num_pages, hpi->hugepage_sz);
381 snprintf(hpi->hugedir, sizeof(hpi->hugedir), "%s", hugedir);
383 /* try to obtain a writelock */
384 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
386 /* if blocking lock failed */
387 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
389 "Failed to lock hugepage directory!\n");
392 /* clear out the hugepages dir from unused pages */
393 if (clear_hugedir(hpi->hugedir) == -1)
397 * first, try to put all hugepages into relevant sockets, but
398 * if first attempts fails, fall back to collecting all pages
399 * in one socket and sorting them later
402 /* we also don't want to do this for legacy init */
403 if (!internal_config.legacy_mem)
404 for (i = 0; i < rte_socket_count(); i++) {
405 int socket = rte_socket_id_by_idx(i);
406 unsigned int num_pages =
407 get_num_hugepages_on_node(
408 dirent->d_name, socket);
409 hpi->num_pages[socket] = num_pages;
410 total_pages += num_pages;
413 * we failed to sort memory from the get go, so fall
416 if (total_pages == 0)
417 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
420 /* for 32-bit systems, limit number of hugepages to
421 * 1GB per page size */
422 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
423 RTE_PGSIZE_1G / hpi->hugepage_sz);
430 /* something went wrong, and we broke from the for loop above */
434 internal_config.num_hugepage_sizes = num_sizes;
436 /* sort the page directory entries by size, largest to smallest */
437 qsort(&internal_config.hugepage_info[0], num_sizes,
438 sizeof(internal_config.hugepage_info[0]), compare_hpi);
440 /* now we have all info, check we have at least one valid size */
441 for (i = 0; i < num_sizes; i++) {
442 /* pages may no longer all be on socket 0, so check all */
443 unsigned int j, num_pages = 0;
444 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
446 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
447 num_pages += hpi->num_pages[j];
448 if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0 &&
453 /* no valid hugepage mounts available, return error */
458 * when we initialize the hugepage info, everything goes
459 * to socket 0 by default. it will later get sorted by memory
460 * initialization procedure.
463 eal_hugepage_info_init(void)
465 struct hugepage_info *hpi, *tmp_hpi;
468 if (hugepage_info_init() < 0)
471 hpi = &internal_config.hugepage_info[0];
473 tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
474 sizeof(internal_config.hugepage_info));
475 if (tmp_hpi == NULL) {
476 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
480 memcpy(tmp_hpi, hpi, sizeof(internal_config.hugepage_info));
482 /* we've copied file descriptors along with everything else, but they
483 * will be invalid in secondary process, so overwrite them
485 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
486 struct hugepage_info *tmp = &tmp_hpi[i];
487 tmp->lock_descriptor = -1;
490 if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
491 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
497 int eal_hugepage_info_read(void)
499 struct hugepage_info *hpi = &internal_config.hugepage_info[0];
500 struct hugepage_info *tmp_hpi;
502 tmp_hpi = open_shared_memory(eal_hugepage_info_path(),
503 sizeof(internal_config.hugepage_info));
504 if (tmp_hpi == NULL) {
505 RTE_LOG(ERR, EAL, "Failed to open shared memory!\n");
509 memcpy(hpi, tmp_hpi, sizeof(internal_config.hugepage_info));
511 if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
512 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");