New upstream version 18.02
[deb_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
19 #include <rte_memory.h>
20 #include <rte_eal.h>
21 #include <rte_launch.h>
22 #include <rte_per_lcore.h>
23 #include <rte_lcore.h>
24 #include <rte_debug.h>
25 #include <rte_log.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"
31
32 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
33
34 /* this function is only called from eal_hugepage_info_init which itself
35  * is only called from a primary process */
36 static uint32_t
37 get_num_hugepages(const char *subdir)
38 {
39         char path[PATH_MAX];
40         long unsigned resv_pages, num_pages = 0;
41         const char *nr_hp_file = "free_hugepages";
42         const char *nr_rsvd_file = "resv_hugepages";
43
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)
48                 return 0;
49
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)
53                 return 0;
54
55         if (num_pages == 0)
56                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
57                                 subdir);
58
59         /* adjust num_pages */
60         if (num_pages >= resv_pages)
61                 num_pages -= resv_pages;
62         else if (resv_pages)
63                 num_pages = 0;
64
65         /* we want to return a uint32_t and more than this looks suspicious
66          * anyway ... */
67         if (num_pages > UINT32_MAX)
68                 num_pages = UINT32_MAX;
69
70         return num_pages;
71 }
72
73 static uint64_t
74 get_default_hp_size(void)
75 {
76         const char proc_meminfo[] = "/proc/meminfo";
77         const char str_hugepagesz[] = "Hugepagesize:";
78         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
79         char buffer[256];
80         unsigned long long size = 0;
81
82         FILE *fd = fopen(proc_meminfo, "r");
83         if (fd == NULL)
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]);
88                         break;
89                 }
90         }
91         fclose(fd);
92         if (size == 0)
93                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
94         return size;
95 }
96
97 static const char *
98 get_hugepage_dir(uint64_t hugepage_sz)
99 {
100         enum proc_mount_fieldnames {
101                 DEVICE = 0,
102                 MOUNTPT,
103                 FSTYPE,
104                 OPTIONS,
105                 _FIELDNAME_MAX
106         };
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];
115         char buf[BUFSIZ];
116         char *retval = NULL;
117
118         FILE *fd = fopen(proc_mounts, "r");
119         if (fd == NULL)
120                 rte_panic("Cannot open %s\n", proc_mounts);
121
122         if (default_size == 0)
123                 default_size = get_default_hp_size();
124
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 */
130                 }
131
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)
135                         continue;
136
137                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
138                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
139
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]);
144                                         break;
145                                 }
146                         }
147                         /* there is an explicit page size, so check it */
148                         else {
149                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
150                                 if (pagesz == hugepage_sz) {
151                                         retval = strdup(splitstr[MOUNTPT]);
152                                         break;
153                                 }
154                         }
155                 } /* end if strncmp hugetlbfs */
156         } /* end while fgets */
157
158         fclose(fd);
159         return retval;
160 }
161
162 /*
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).
166  */
167 static int
168 clear_hugedir(const char * hugedir)
169 {
170         DIR *dir;
171         struct dirent *dirent;
172         int dir_fd, fd, lck_result;
173         const char filter[] = "*map_*"; /* matches hugepage files */
174
175         /* open directory */
176         dir = opendir(hugedir);
177         if (!dir) {
178                 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
179                                 hugedir);
180                 goto error;
181         }
182         dir_fd = dirfd(dir);
183
184         dirent = readdir(dir);
185         if (!dirent) {
186                 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
187                                 hugedir);
188                 goto error;
189         }
190
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);
195                         continue;
196                 }
197
198                 /* try and lock the file */
199                 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
200
201                 /* skip to next file */
202                 if (fd == -1) {
203                         dirent = readdir(dir);
204                         continue;
205                 }
206
207                 /* non-blocking lock */
208                 lck_result = flock(fd, LOCK_EX | LOCK_NB);
209
210                 /* if lock succeeds, unlock and remove the file */
211                 if (lck_result != -1) {
212                         flock(fd, LOCK_UN);
213                         unlinkat(dir_fd, dirent->d_name, 0);
214                 }
215                 close (fd);
216                 dirent = readdir(dir);
217         }
218
219         closedir(dir);
220         return 0;
221
222 error:
223         if (dir)
224                 closedir(dir);
225
226         RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
227                 strerror(errno));
228
229         return -1;
230 }
231
232 static int
233 compare_hpi(const void *a, const void *b)
234 {
235         const struct hugepage_info *hpi_a = a;
236         const struct hugepage_info *hpi_b = b;
237
238         return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
239 }
240
241 /*
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.
245  */
246 int
247 eal_hugepage_info_init(void)
248 {
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;
252         DIR *dir;
253         struct dirent *dirent;
254
255         dir = opendir(sys_dir_path);
256         if (dir == NULL) {
257                 RTE_LOG(ERR, EAL,
258                         "Cannot open directory %s to read system hugepage info\n",
259                         sys_dir_path);
260                 return -1;
261         }
262
263         for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
264                 struct hugepage_info *hpi;
265
266                 if (strncmp(dirent->d_name, dirent_start_text,
267                             dirent_start_len) != 0)
268                         continue;
269
270                 if (num_sizes >= MAX_HUGEPAGE_SIZES)
271                         break;
272
273                 hpi = &internal_config.hugepage_info[num_sizes];
274                 hpi->hugepage_sz =
275                         rte_str_to_size(&dirent->d_name[dirent_start_len]);
276                 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
277
278                 /* first, check if we have a mountpoint */
279                 if (hpi->hugedir == NULL) {
280                         uint32_t num_pages;
281
282                         num_pages = get_num_hugepages(dirent->d_name);
283                         if (num_pages > 0)
284                                 RTE_LOG(NOTICE, EAL,
285                                         "%" PRIu32 " hugepages of size "
286                                         "%" PRIu64 " reserved, but no mounted "
287                                         "hugetlbfs found for that size\n",
288                                         num_pages, hpi->hugepage_sz);
289                         continue;
290                 }
291
292                 /* try to obtain a writelock */
293                 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
294
295                 /* if blocking lock failed */
296                 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
297                         RTE_LOG(CRIT, EAL,
298                                 "Failed to lock hugepage directory!\n");
299                         break;
300                 }
301                 /* clear out the hugepages dir from unused pages */
302                 if (clear_hugedir(hpi->hugedir) == -1)
303                         break;
304
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);
308
309 #ifndef RTE_ARCH_64
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);
314 #endif
315
316                 num_sizes++;
317         }
318         closedir(dir);
319
320         /* something went wrong, and we broke from the for loop above */
321         if (dirent != NULL)
322                 return -1;
323
324         internal_config.num_hugepage_sizes = num_sizes;
325
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);
329
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)
334                         return 0;
335
336         /* no valid hugepage mounts available, return error */
337         return -1;
338 }