1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
17 #include <sys/queue.h>
20 #include <rte_memory.h>
22 #include <rte_launch.h>
23 #include <rte_per_lcore.h>
24 #include <rte_lcore.h>
25 #include <rte_debug.h>
27 #include <rte_common.h>
28 #include "rte_string_fns.h"
29 #include "eal_internal_cfg.h"
30 #include "eal_hugepages.h"
31 #include "eal_filesystem.h"
33 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
35 /* this function is only called from eal_hugepage_info_init which itself
36 * is only called from a primary process */
38 get_num_hugepages(const char *subdir)
41 long unsigned resv_pages, num_pages = 0;
42 const char *nr_hp_file = "free_hugepages";
43 const char *nr_rsvd_file = "resv_hugepages";
45 /* first, check how many reserved pages kernel reports */
46 snprintf(path, sizeof(path), "%s/%s/%s",
47 sys_dir_path, subdir, nr_rsvd_file);
48 if (eal_parse_sysfs_value(path, &resv_pages) < 0)
51 snprintf(path, sizeof(path), "%s/%s/%s",
52 sys_dir_path, subdir, nr_hp_file);
53 if (eal_parse_sysfs_value(path, &num_pages) < 0)
57 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
60 /* adjust num_pages */
61 if (num_pages >= resv_pages)
62 num_pages -= resv_pages;
66 /* we want to return a uint32_t and more than this looks suspicious
68 if (num_pages > UINT32_MAX)
69 num_pages = UINT32_MAX;
75 get_default_hp_size(void)
77 const char proc_meminfo[] = "/proc/meminfo";
78 const char str_hugepagesz[] = "Hugepagesize:";
79 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
81 unsigned long long size = 0;
83 FILE *fd = fopen(proc_meminfo, "r");
85 rte_panic("Cannot open %s\n", proc_meminfo);
86 while(fgets(buffer, sizeof(buffer), fd)){
87 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
88 size = rte_str_to_size(&buffer[hugepagesz_len]);
94 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
99 get_hugepage_dir(uint64_t hugepage_sz)
101 enum proc_mount_fieldnames {
108 static uint64_t default_size = 0;
109 const char proc_mounts[] = "/proc/mounts";
110 const char hugetlbfs_str[] = "hugetlbfs";
111 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
112 const char pagesize_opt[] = "pagesize=";
113 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
114 const char split_tok = ' ';
115 char *splitstr[_FIELDNAME_MAX];
119 FILE *fd = fopen(proc_mounts, "r");
121 rte_panic("Cannot open %s\n", proc_mounts);
123 if (default_size == 0)
124 default_size = get_default_hp_size();
126 while (fgets(buf, sizeof(buf), fd)){
127 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
128 split_tok) != _FIELDNAME_MAX) {
129 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
130 break; /* return NULL */
133 /* we have a specified --huge-dir option, only examine that dir */
134 if (internal_config.hugepage_dir != NULL &&
135 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
138 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
139 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
141 /* if no explicit page size, the default page size is compared */
142 if (pagesz_str == NULL){
143 if (hugepage_sz == default_size){
144 retval = strdup(splitstr[MOUNTPT]);
148 /* there is an explicit page size, so check it */
150 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
151 if (pagesz == hugepage_sz) {
152 retval = strdup(splitstr[MOUNTPT]);
156 } /* end if strncmp hugetlbfs */
157 } /* end while fgets */
164 * uses fstat to report the size of a file on disk
167 get_file_size(int fd)
170 if (fstat(fd, &st) < 0)
176 * Clear the hugepage directory of whatever hugepage files
177 * there are. Checks if the file is locked (i.e.
178 * if it's in use by another DPDK process).
181 clear_hugedir(const char * hugedir)
184 struct dirent *dirent;
185 int dir_fd, fd, lck_result;
186 const char filter[] = "*map_*"; /* matches hugepage files */
189 dir = opendir(hugedir);
191 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
197 dirent = readdir(dir);
199 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
204 while(dirent != NULL){
205 struct flock lck = {0};
207 /* skip files that don't match the hugepage pattern */
208 if (fnmatch(filter, dirent->d_name, 0) > 0) {
209 dirent = readdir(dir);
213 /* try and lock the file */
214 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
216 /* skip to next file */
218 dirent = readdir(dir);
222 /* non-blocking lock */
223 lck.l_type = F_RDLCK;
224 lck.l_whence = SEEK_SET;
226 lck.l_len = get_file_size(fd);
228 lck_result = fcntl(fd, F_SETLK, &lck);
230 /* if lock succeeds, unlock and remove the file */
231 if (lck_result != -1) {
232 lck.l_type = F_UNLCK;
233 fcntl(fd, F_SETLK, &lck);
234 unlinkat(dir_fd, dirent->d_name, 0);
237 dirent = readdir(dir);
247 RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
254 compare_hpi(const void *a, const void *b)
256 const struct hugepage_info *hpi_a = a;
257 const struct hugepage_info *hpi_b = b;
259 return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
263 * when we initialize the hugepage info, everything goes
264 * to socket 0 by default. it will later get sorted by memory
265 * initialization procedure.
268 eal_hugepage_info_init(void)
270 const char dirent_start_text[] = "hugepages-";
271 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
272 unsigned i, num_sizes = 0;
274 struct dirent *dirent;
276 dir = opendir(sys_dir_path);
279 "Cannot open directory %s to read system hugepage info\n",
284 for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
285 struct hugepage_info *hpi;
287 if (strncmp(dirent->d_name, dirent_start_text,
288 dirent_start_len) != 0)
291 if (num_sizes >= MAX_HUGEPAGE_SIZES)
294 hpi = &internal_config.hugepage_info[num_sizes];
296 rte_str_to_size(&dirent->d_name[dirent_start_len]);
297 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
299 /* first, check if we have a mountpoint */
300 if (hpi->hugedir == NULL) {
303 num_pages = get_num_hugepages(dirent->d_name);
306 "%" PRIu32 " hugepages of size "
307 "%" PRIu64 " reserved, but no mounted "
308 "hugetlbfs found for that size\n",
309 num_pages, hpi->hugepage_sz);
313 /* try to obtain a writelock */
314 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
316 /* if blocking lock failed */
317 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
319 "Failed to lock hugepage directory!\n");
322 /* clear out the hugepages dir from unused pages */
323 if (clear_hugedir(hpi->hugedir) == -1)
326 /* for now, put all pages into socket 0,
327 * later they will be sorted */
328 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
331 /* for 32-bit systems, limit number of hugepages to
332 * 1GB per page size */
333 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
334 RTE_PGSIZE_1G / hpi->hugepage_sz);
341 /* something went wrong, and we broke from the for loop above */
345 internal_config.num_hugepage_sizes = num_sizes;
347 /* sort the page directory entries by size, largest to smallest */
348 qsort(&internal_config.hugepage_info[0], num_sizes,
349 sizeof(internal_config.hugepage_info[0]), compare_hpi);
351 /* now we have all info, check we have at least one valid size */
352 for (i = 0; i < num_sizes; i++)
353 if (internal_config.hugepage_info[i].hugedir != NULL &&
354 internal_config.hugepage_info[i].num_pages[0] > 0)
357 /* no valid hugepage mounts available, return error */