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";
34 static const char sys_pages_numa_dir_path[] = "/sys/devices/system/node";
36 /* this function is only called from eal_hugepage_info_init which itself
37 * is only called from a primary process */
39 get_num_hugepages(const char *subdir)
42 long unsigned resv_pages, num_pages = 0;
43 const char *nr_hp_file = "free_hugepages";
44 const char *nr_rsvd_file = "resv_hugepages";
46 /* first, check how many reserved pages kernel reports */
47 snprintf(path, sizeof(path), "%s/%s/%s",
48 sys_dir_path, subdir, nr_rsvd_file);
49 if (eal_parse_sysfs_value(path, &resv_pages) < 0)
52 snprintf(path, sizeof(path), "%s/%s/%s",
53 sys_dir_path, subdir, nr_hp_file);
54 if (eal_parse_sysfs_value(path, &num_pages) < 0)
58 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
61 /* adjust num_pages */
62 if (num_pages >= resv_pages)
63 num_pages -= resv_pages;
67 /* we want to return a uint32_t and more than this looks suspicious
69 if (num_pages > UINT32_MAX)
70 num_pages = UINT32_MAX;
76 get_num_hugepages_on_node(const char *subdir, unsigned int socket)
78 char path[PATH_MAX], socketpath[PATH_MAX];
80 unsigned long num_pages = 0;
81 const char *nr_hp_file = "free_hugepages";
83 snprintf(socketpath, sizeof(socketpath), "%s/node%u/hugepages",
84 sys_pages_numa_dir_path, socket);
86 socketdir = opendir(socketpath);
88 /* Keep calm and carry on */
91 /* Can't find socket dir, so ignore it */
95 snprintf(path, sizeof(path), "%s/%s/%s",
96 socketpath, subdir, nr_hp_file);
97 if (eal_parse_sysfs_value(path, &num_pages) < 0)
101 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
105 * we want to return a uint32_t and more than this looks suspicious
108 if (num_pages > UINT32_MAX)
109 num_pages = UINT32_MAX;
115 get_default_hp_size(void)
117 const char proc_meminfo[] = "/proc/meminfo";
118 const char str_hugepagesz[] = "Hugepagesize:";
119 unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
121 unsigned long long size = 0;
123 FILE *fd = fopen(proc_meminfo, "r");
125 rte_panic("Cannot open %s\n", proc_meminfo);
126 while(fgets(buffer, sizeof(buffer), fd)){
127 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
128 size = rte_str_to_size(&buffer[hugepagesz_len]);
134 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
139 get_hugepage_dir(uint64_t hugepage_sz)
141 enum proc_mount_fieldnames {
148 static uint64_t default_size = 0;
149 const char proc_mounts[] = "/proc/mounts";
150 const char hugetlbfs_str[] = "hugetlbfs";
151 const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
152 const char pagesize_opt[] = "pagesize=";
153 const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
154 const char split_tok = ' ';
155 char *splitstr[_FIELDNAME_MAX];
159 FILE *fd = fopen(proc_mounts, "r");
161 rte_panic("Cannot open %s\n", proc_mounts);
163 if (default_size == 0)
164 default_size = get_default_hp_size();
166 while (fgets(buf, sizeof(buf), fd)){
167 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
168 split_tok) != _FIELDNAME_MAX) {
169 RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
170 break; /* return NULL */
173 /* we have a specified --huge-dir option, only examine that dir */
174 if (internal_config.hugepage_dir != NULL &&
175 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
178 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
179 const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
181 /* if no explicit page size, the default page size is compared */
182 if (pagesz_str == NULL){
183 if (hugepage_sz == default_size){
184 retval = strdup(splitstr[MOUNTPT]);
188 /* there is an explicit page size, so check it */
190 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
191 if (pagesz == hugepage_sz) {
192 retval = strdup(splitstr[MOUNTPT]);
196 } /* end if strncmp hugetlbfs */
197 } /* end while fgets */
204 * uses fstat to report the size of a file on disk
207 get_file_size(int fd)
210 if (fstat(fd, &st) < 0)
216 * Clear the hugepage directory of whatever hugepage files
217 * there are. Checks if the file is locked (i.e.
218 * if it's in use by another DPDK process).
221 clear_hugedir(const char * hugedir)
224 struct dirent *dirent;
225 int dir_fd, fd, lck_result;
226 const char filter[] = "*map_*"; /* matches hugepage files */
229 dir = opendir(hugedir);
231 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
237 dirent = readdir(dir);
239 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
244 while(dirent != NULL){
245 struct flock lck = {0};
247 /* skip files that don't match the hugepage pattern */
248 if (fnmatch(filter, dirent->d_name, 0) > 0) {
249 dirent = readdir(dir);
253 /* try and lock the file */
254 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
256 /* skip to next file */
258 dirent = readdir(dir);
262 /* non-blocking lock */
263 lck.l_type = F_RDLCK;
264 lck.l_whence = SEEK_SET;
266 lck.l_len = get_file_size(fd);
268 lck_result = fcntl(fd, F_SETLK, &lck);
270 /* if lock succeeds, unlock and remove the file */
271 if (lck_result != -1) {
272 lck.l_type = F_UNLCK;
273 fcntl(fd, F_SETLK, &lck);
274 unlinkat(dir_fd, dirent->d_name, 0);
277 dirent = readdir(dir);
287 RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
294 compare_hpi(const void *a, const void *b)
296 const struct hugepage_info *hpi_a = a;
297 const struct hugepage_info *hpi_b = b;
299 return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
303 * when we initialize the hugepage info, everything goes
304 * to socket 0 by default. it will later get sorted by memory
305 * initialization procedure.
308 eal_hugepage_info_init(void)
310 const char dirent_start_text[] = "hugepages-";
311 const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
312 unsigned int i, total_pages, num_sizes = 0;
314 struct dirent *dirent;
316 dir = opendir(sys_dir_path);
319 "Cannot open directory %s to read system hugepage info\n",
324 for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
325 struct hugepage_info *hpi;
327 if (strncmp(dirent->d_name, dirent_start_text,
328 dirent_start_len) != 0)
331 if (num_sizes >= MAX_HUGEPAGE_SIZES)
334 hpi = &internal_config.hugepage_info[num_sizes];
336 rte_str_to_size(&dirent->d_name[dirent_start_len]);
337 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
339 /* first, check if we have a mountpoint */
340 if (hpi->hugedir == NULL) {
343 num_pages = get_num_hugepages(dirent->d_name);
346 "%" PRIu32 " hugepages of size "
347 "%" PRIu64 " reserved, but no mounted "
348 "hugetlbfs found for that size\n",
349 num_pages, hpi->hugepage_sz);
353 /* try to obtain a writelock */
354 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
356 /* if blocking lock failed */
357 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
359 "Failed to lock hugepage directory!\n");
362 /* clear out the hugepages dir from unused pages */
363 if (clear_hugedir(hpi->hugedir) == -1)
367 * first, try to put all hugepages into relevant sockets, but
368 * if first attempts fails, fall back to collecting all pages
369 * in one socket and sorting them later
372 /* we also don't want to do this for legacy init */
373 if (!internal_config.legacy_mem)
374 for (i = 0; i < rte_socket_count(); i++) {
375 int socket = rte_socket_id_by_idx(i);
376 unsigned int num_pages =
377 get_num_hugepages_on_node(
378 dirent->d_name, socket);
379 hpi->num_pages[socket] = num_pages;
380 total_pages += num_pages;
383 * we failed to sort memory from the get go, so fall
386 if (total_pages == 0)
387 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
390 /* for 32-bit systems, limit number of hugepages to
391 * 1GB per page size */
392 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
393 RTE_PGSIZE_1G / hpi->hugepage_sz);
400 /* something went wrong, and we broke from the for loop above */
404 internal_config.num_hugepage_sizes = num_sizes;
406 /* sort the page directory entries by size, largest to smallest */
407 qsort(&internal_config.hugepage_info[0], num_sizes,
408 sizeof(internal_config.hugepage_info[0]), compare_hpi);
410 /* now we have all info, check we have at least one valid size */
411 for (i = 0; i < num_sizes; i++) {
412 /* pages may no longer all be on socket 0, so check all */
413 unsigned int j, num_pages = 0;
415 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
416 struct hugepage_info *hpi =
417 &internal_config.hugepage_info[i];
418 num_pages += hpi->num_pages[j];
420 if (internal_config.hugepage_info[i].hugedir != NULL &&
425 /* no valid hugepage mounts available, return error */