2e0819ffefae5e9ec8b08d5c81f7e44d798a683b
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_hugepage_info.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/file.h>
8 #include <dirent.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <fnmatch.h>
13 #include <inttypes.h>
14 #include <stdarg.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19
20 #include <rte_memory.h>
21 #include <rte_eal.h>
22 #include <rte_launch.h>
23 #include <rte_per_lcore.h>
24 #include <rte_lcore.h>
25 #include <rte_debug.h>
26 #include <rte_log.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"
32
33 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
34 static const char sys_pages_numa_dir_path[] = "/sys/devices/system/node";
35
36 /* this function is only called from eal_hugepage_info_init which itself
37  * is only called from a primary process */
38 static uint32_t
39 get_num_hugepages(const char *subdir)
40 {
41         char path[PATH_MAX];
42         long unsigned resv_pages, num_pages = 0;
43         const char *nr_hp_file = "free_hugepages";
44         const char *nr_rsvd_file = "resv_hugepages";
45
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)
50                 return 0;
51
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)
55                 return 0;
56
57         if (num_pages == 0)
58                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
59                                 subdir);
60
61         /* adjust num_pages */
62         if (num_pages >= resv_pages)
63                 num_pages -= resv_pages;
64         else if (resv_pages)
65                 num_pages = 0;
66
67         /* we want to return a uint32_t and more than this looks suspicious
68          * anyway ... */
69         if (num_pages > UINT32_MAX)
70                 num_pages = UINT32_MAX;
71
72         return num_pages;
73 }
74
75 static uint32_t
76 get_num_hugepages_on_node(const char *subdir, unsigned int socket)
77 {
78         char path[PATH_MAX], socketpath[PATH_MAX];
79         DIR *socketdir;
80         unsigned long num_pages = 0;
81         const char *nr_hp_file = "free_hugepages";
82
83         snprintf(socketpath, sizeof(socketpath), "%s/node%u/hugepages",
84                 sys_pages_numa_dir_path, socket);
85
86         socketdir = opendir(socketpath);
87         if (socketdir) {
88                 /* Keep calm and carry on */
89                 closedir(socketdir);
90         } else {
91                 /* Can't find socket dir, so ignore it */
92                 return 0;
93         }
94
95         snprintf(path, sizeof(path), "%s/%s/%s",
96                         socketpath, subdir, nr_hp_file);
97         if (eal_parse_sysfs_value(path, &num_pages) < 0)
98                 return 0;
99
100         if (num_pages == 0)
101                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
102                                 subdir);
103
104         /*
105          * we want to return a uint32_t and more than this looks suspicious
106          * anyway ...
107          */
108         if (num_pages > UINT32_MAX)
109                 num_pages = UINT32_MAX;
110
111         return num_pages;
112 }
113
114 static uint64_t
115 get_default_hp_size(void)
116 {
117         const char proc_meminfo[] = "/proc/meminfo";
118         const char str_hugepagesz[] = "Hugepagesize:";
119         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
120         char buffer[256];
121         unsigned long long size = 0;
122
123         FILE *fd = fopen(proc_meminfo, "r");
124         if (fd == NULL)
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]);
129                         break;
130                 }
131         }
132         fclose(fd);
133         if (size == 0)
134                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
135         return size;
136 }
137
138 static const char *
139 get_hugepage_dir(uint64_t hugepage_sz)
140 {
141         enum proc_mount_fieldnames {
142                 DEVICE = 0,
143                 MOUNTPT,
144                 FSTYPE,
145                 OPTIONS,
146                 _FIELDNAME_MAX
147         };
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];
156         char buf[BUFSIZ];
157         char *retval = NULL;
158
159         FILE *fd = fopen(proc_mounts, "r");
160         if (fd == NULL)
161                 rte_panic("Cannot open %s\n", proc_mounts);
162
163         if (default_size == 0)
164                 default_size = get_default_hp_size();
165
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 */
171                 }
172
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)
176                         continue;
177
178                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
179                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
180
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]);
185                                         break;
186                                 }
187                         }
188                         /* there is an explicit page size, so check it */
189                         else {
190                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
191                                 if (pagesz == hugepage_sz) {
192                                         retval = strdup(splitstr[MOUNTPT]);
193                                         break;
194                                 }
195                         }
196                 } /* end if strncmp hugetlbfs */
197         } /* end while fgets */
198
199         fclose(fd);
200         return retval;
201 }
202
203 /*
204  * uses fstat to report the size of a file on disk
205  */
206 static off_t
207 get_file_size(int fd)
208 {
209         struct stat st;
210         if (fstat(fd, &st) < 0)
211                 return 0;
212         return st.st_size;
213 }
214
215 /*
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).
219  */
220 static int
221 clear_hugedir(const char * hugedir)
222 {
223         DIR *dir;
224         struct dirent *dirent;
225         int dir_fd, fd, lck_result;
226         const char filter[] = "*map_*"; /* matches hugepage files */
227
228         /* open directory */
229         dir = opendir(hugedir);
230         if (!dir) {
231                 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
232                                 hugedir);
233                 goto error;
234         }
235         dir_fd = dirfd(dir);
236
237         dirent = readdir(dir);
238         if (!dirent) {
239                 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
240                                 hugedir);
241                 goto error;
242         }
243
244         while(dirent != NULL){
245                 struct flock lck = {0};
246
247                 /* skip files that don't match the hugepage pattern */
248                 if (fnmatch(filter, dirent->d_name, 0) > 0) {
249                         dirent = readdir(dir);
250                         continue;
251                 }
252
253                 /* try and lock the file */
254                 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
255
256                 /* skip to next file */
257                 if (fd == -1) {
258                         dirent = readdir(dir);
259                         continue;
260                 }
261
262                 /* non-blocking lock */
263                 lck.l_type = F_RDLCK;
264                 lck.l_whence = SEEK_SET;
265                 lck.l_start = 0;
266                 lck.l_len = get_file_size(fd);
267
268                 lck_result = fcntl(fd, F_SETLK, &lck);
269
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);
275                 }
276                 close (fd);
277                 dirent = readdir(dir);
278         }
279
280         closedir(dir);
281         return 0;
282
283 error:
284         if (dir)
285                 closedir(dir);
286
287         RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
288                 strerror(errno));
289
290         return -1;
291 }
292
293 static int
294 compare_hpi(const void *a, const void *b)
295 {
296         const struct hugepage_info *hpi_a = a;
297         const struct hugepage_info *hpi_b = b;
298
299         return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
300 }
301
302 /*
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.
306  */
307 int
308 eal_hugepage_info_init(void)
309 {
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;
313         DIR *dir;
314         struct dirent *dirent;
315
316         dir = opendir(sys_dir_path);
317         if (dir == NULL) {
318                 RTE_LOG(ERR, EAL,
319                         "Cannot open directory %s to read system hugepage info\n",
320                         sys_dir_path);
321                 return -1;
322         }
323
324         for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
325                 struct hugepage_info *hpi;
326
327                 if (strncmp(dirent->d_name, dirent_start_text,
328                             dirent_start_len) != 0)
329                         continue;
330
331                 if (num_sizes >= MAX_HUGEPAGE_SIZES)
332                         break;
333
334                 hpi = &internal_config.hugepage_info[num_sizes];
335                 hpi->hugepage_sz =
336                         rte_str_to_size(&dirent->d_name[dirent_start_len]);
337                 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
338
339                 /* first, check if we have a mountpoint */
340                 if (hpi->hugedir == NULL) {
341                         uint32_t num_pages;
342
343                         num_pages = get_num_hugepages(dirent->d_name);
344                         if (num_pages > 0)
345                                 RTE_LOG(NOTICE, EAL,
346                                         "%" PRIu32 " hugepages of size "
347                                         "%" PRIu64 " reserved, but no mounted "
348                                         "hugetlbfs found for that size\n",
349                                         num_pages, hpi->hugepage_sz);
350                         continue;
351                 }
352
353                 /* try to obtain a writelock */
354                 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
355
356                 /* if blocking lock failed */
357                 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
358                         RTE_LOG(CRIT, EAL,
359                                 "Failed to lock hugepage directory!\n");
360                         break;
361                 }
362                 /* clear out the hugepages dir from unused pages */
363                 if (clear_hugedir(hpi->hugedir) == -1)
364                         break;
365
366                 /*
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
370                  */
371                 total_pages = 0;
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;
381                         }
382                 /*
383                  * we failed to sort memory from the get go, so fall
384                  * back to old way
385                  */
386                 if (total_pages == 0)
387                         hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
388
389 #ifndef RTE_ARCH_64
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);
394 #endif
395
396                 num_sizes++;
397         }
398         closedir(dir);
399
400         /* something went wrong, and we broke from the for loop above */
401         if (dirent != NULL)
402                 return -1;
403
404         internal_config.num_hugepage_sizes = num_sizes;
405
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);
409
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;
414
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];
419                 }
420                 if (internal_config.hugepage_info[i].hugedir != NULL &&
421                                 num_pages > 0)
422                         return 0;
423         }
424
425         /* no valid hugepage mounts available, return error */
426         return -1;
427 }