1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
17 #include <sys/queue.h>
19 #include <rte_memory.h>
21 #include <rte_launch.h>
22 #include <rte_per_lcore.h>
23 #include <rte_lcore.h>
24 #include <rte_debug.h>
26 #include <rte_common.h>
27 #include "rte_string_fns.h"
28 #include "eal_internal_cfg.h"
29 #include "eal_hugepages.h"
30 #include "eal_filesystem.h"
32 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
34 /* this function is only called from eal_hugepage_info_init which itself
35 * is only called from a primary process */
37 get_num_hugepages(const char *subdir)
40 long unsigned resv_pages, num_pages = 0;
41 const char *nr_hp_file = "free_hugepages";
42 const char *nr_rsvd_file = "resv_hugepages";
44 /* first, check how many reserved pages kernel reports */
45 snprintf(path, sizeof(path), "%s/%s/%s",
46 sys_dir_path, subdir, nr_rsvd_file);
47 if (eal_parse_sysfs_value(path, &resv_pages) < 0)
50 snprintf(path, sizeof(path), "%s/%s/%s",
51 sys_dir_path, subdir, nr_hp_file);
52 if (eal_parse_sysfs_value(path, &num_pages) < 0)
56 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
59 /* adjust num_pages */
60 if (num_pages >= resv_pages)
61 num_pages -= resv_pages;
65 /* we want to return a uint32_t and more than this looks suspicious
67 if (num_pages > UINT32_MAX)
68 num_pages = UINT32_MAX;
74 get_default_hp_size(void)
76 const char proc_meminfo[] = "/proc/meminfo";
77 const char str_hugepagesz[] = "Hugepagesize:";
78 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
80 unsigned long long size = 0;
82 FILE *fd = fopen(proc_meminfo, "r");
84 rte_panic("Cannot open %s\n", proc_meminfo);
85 while(fgets(buffer, sizeof(buffer), fd)){
86 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
87 size = rte_str_to_size(&buffer[hugepagesz_len]);
93 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
98 get_hugepage_dir(uint64_t hugepage_sz)
100 enum proc_mount_fieldnames {
107 static uint64_t default_size = 0;
108 const char proc_mounts[] = "/proc/mounts";
109 const char hugetlbfs_str[] = "hugetlbfs";
110 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
111 const char pagesize_opt[] = "pagesize=";
112 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
113 const char split_tok = ' ';
114 char *splitstr[_FIELDNAME_MAX];
118 FILE *fd = fopen(proc_mounts, "r");
120 rte_panic("Cannot open %s\n", proc_mounts);
122 if (default_size == 0)
123 default_size = get_default_hp_size();
125 while (fgets(buf, sizeof(buf), fd)){
126 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
127 split_tok) != _FIELDNAME_MAX) {
128 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
129 break; /* return NULL */
132 /* we have a specified --huge-dir option, only examine that dir */
133 if (internal_config.hugepage_dir != NULL &&
134 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
137 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
138 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
140 /* if no explicit page size, the default page size is compared */
141 if (pagesz_str == NULL){
142 if (hugepage_sz == default_size){
143 retval = strdup(splitstr[MOUNTPT]);
147 /* there is an explicit page size, so check it */
149 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
150 if (pagesz == hugepage_sz) {
151 retval = strdup(splitstr[MOUNTPT]);
155 } /* end if strncmp hugetlbfs */
156 } /* end while fgets */
163 * Clear the hugepage directory of whatever hugepage files
164 * there are. Checks if the file is locked (i.e.
165 * if it's in use by another DPDK process).
168 clear_hugedir(const char * hugedir)
171 struct dirent *dirent;
172 int dir_fd, fd, lck_result;
173 const char filter[] = "*map_*"; /* matches hugepage files */
176 dir = opendir(hugedir);
178 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
184 dirent = readdir(dir);
186 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
191 while(dirent != NULL){
192 /* skip files that don't match the hugepage pattern */
193 if (fnmatch(filter, dirent->d_name, 0) > 0) {
194 dirent = readdir(dir);
198 /* try and lock the file */
199 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
201 /* skip to next file */
203 dirent = readdir(dir);
207 /* non-blocking lock */
208 lck_result = flock(fd, LOCK_EX | LOCK_NB);
210 /* if lock succeeds, unlock and remove the file */
211 if (lck_result != -1) {
213 unlinkat(dir_fd, dirent->d_name, 0);
216 dirent = readdir(dir);
226 RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
233 compare_hpi(const void *a, const void *b)
235 const struct hugepage_info *hpi_a = a;
236 const struct hugepage_info *hpi_b = b;
238 return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
242 * when we initialize the hugepage info, everything goes
243 * to socket 0 by default. it will later get sorted by memory
244 * initialization procedure.
247 eal_hugepage_info_init(void)
249 const char dirent_start_text[] = "hugepages-";
250 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
251 unsigned i, num_sizes = 0;
253 struct dirent *dirent;
255 dir = opendir(sys_dir_path);
258 "Cannot open directory %s to read system hugepage info\n",
263 for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
264 struct hugepage_info *hpi;
266 if (strncmp(dirent->d_name, dirent_start_text,
267 dirent_start_len) != 0)
270 if (num_sizes >= MAX_HUGEPAGE_SIZES)
273 hpi = &internal_config.hugepage_info[num_sizes];
275 rte_str_to_size(&dirent->d_name[dirent_start_len]);
276 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
278 /* first, check if we have a mountpoint */
279 if (hpi->hugedir == NULL) {
282 num_pages = get_num_hugepages(dirent->d_name);
285 "%" PRIu32 " hugepages of size "
286 "%" PRIu64 " reserved, but no mounted "
287 "hugetlbfs found for that size\n",
288 num_pages, hpi->hugepage_sz);
292 /* try to obtain a writelock */
293 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
295 /* if blocking lock failed */
296 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
298 "Failed to lock hugepage directory!\n");
301 /* clear out the hugepages dir from unused pages */
302 if (clear_hugedir(hpi->hugedir) == -1)
305 /* for now, put all pages into socket 0,
306 * later they will be sorted */
307 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
310 /* for 32-bit systems, limit number of hugepages to
311 * 1GB per page size */
312 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
313 RTE_PGSIZE_1G / hpi->hugepage_sz);
320 /* something went wrong, and we broke from the for loop above */
324 internal_config.num_hugepage_sizes = num_sizes;
326 /* sort the page directory entries by size, largest to smallest */
327 qsort(&internal_config.hugepage_info[0], num_sizes,
328 sizeof(internal_config.hugepage_info[0]), compare_hpi);
330 /* now we have all info, check we have at least one valid size */
331 for (i = 0; i < num_sizes; i++)
332 if (internal_config.hugepage_info[i].hugedir != NULL &&
333 internal_config.hugepage_info[i].num_pages[0] > 0)
336 /* no valid hugepage mounts available, return error */