mem: replace memseg with memseg lists
[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
35 /* this function is only called from eal_hugepage_info_init which itself
36  * is only called from a primary process */
37 static uint32_t
38 get_num_hugepages(const char *subdir)
39 {
40         char path[PATH_MAX];
41         long unsigned resv_pages, num_pages = 0;
42         const char *nr_hp_file = "free_hugepages";
43         const char *nr_rsvd_file = "resv_hugepages";
44
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)
49                 return 0;
50
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)
54                 return 0;
55
56         if (num_pages == 0)
57                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
58                                 subdir);
59
60         /* adjust num_pages */
61         if (num_pages >= resv_pages)
62                 num_pages -= resv_pages;
63         else if (resv_pages)
64                 num_pages = 0;
65
66         /* we want to return a uint32_t and more than this looks suspicious
67          * anyway ... */
68         if (num_pages > UINT32_MAX)
69                 num_pages = UINT32_MAX;
70
71         return num_pages;
72 }
73
74 static uint64_t
75 get_default_hp_size(void)
76 {
77         const char proc_meminfo[] = "/proc/meminfo";
78         const char str_hugepagesz[] = "Hugepagesize:";
79         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
80         char buffer[256];
81         unsigned long long size = 0;
82
83         FILE *fd = fopen(proc_meminfo, "r");
84         if (fd == NULL)
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]);
89                         break;
90                 }
91         }
92         fclose(fd);
93         if (size == 0)
94                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
95         return size;
96 }
97
98 static const char *
99 get_hugepage_dir(uint64_t hugepage_sz)
100 {
101         enum proc_mount_fieldnames {
102                 DEVICE = 0,
103                 MOUNTPT,
104                 FSTYPE,
105                 OPTIONS,
106                 _FIELDNAME_MAX
107         };
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];
116         char buf[BUFSIZ];
117         char *retval = NULL;
118
119         FILE *fd = fopen(proc_mounts, "r");
120         if (fd == NULL)
121                 rte_panic("Cannot open %s\n", proc_mounts);
122
123         if (default_size == 0)
124                 default_size = get_default_hp_size();
125
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 */
131                 }
132
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)
136                         continue;
137
138                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
139                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
140
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]);
145                                         break;
146                                 }
147                         }
148                         /* there is an explicit page size, so check it */
149                         else {
150                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
151                                 if (pagesz == hugepage_sz) {
152                                         retval = strdup(splitstr[MOUNTPT]);
153                                         break;
154                                 }
155                         }
156                 } /* end if strncmp hugetlbfs */
157         } /* end while fgets */
158
159         fclose(fd);
160         return retval;
161 }
162
163 /*
164  * uses fstat to report the size of a file on disk
165  */
166 static off_t
167 get_file_size(int fd)
168 {
169         struct stat st;
170         if (fstat(fd, &st) < 0)
171                 return 0;
172         return st.st_size;
173 }
174
175 /*
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).
179  */
180 static int
181 clear_hugedir(const char * hugedir)
182 {
183         DIR *dir;
184         struct dirent *dirent;
185         int dir_fd, fd, lck_result;
186         const char filter[] = "*map_*"; /* matches hugepage files */
187
188         /* open directory */
189         dir = opendir(hugedir);
190         if (!dir) {
191                 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
192                                 hugedir);
193                 goto error;
194         }
195         dir_fd = dirfd(dir);
196
197         dirent = readdir(dir);
198         if (!dirent) {
199                 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
200                                 hugedir);
201                 goto error;
202         }
203
204         while(dirent != NULL){
205                 struct flock lck = {0};
206
207                 /* skip files that don't match the hugepage pattern */
208                 if (fnmatch(filter, dirent->d_name, 0) > 0) {
209                         dirent = readdir(dir);
210                         continue;
211                 }
212
213                 /* try and lock the file */
214                 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
215
216                 /* skip to next file */
217                 if (fd == -1) {
218                         dirent = readdir(dir);
219                         continue;
220                 }
221
222                 /* non-blocking lock */
223                 lck.l_type = F_RDLCK;
224                 lck.l_whence = SEEK_SET;
225                 lck.l_start = 0;
226                 lck.l_len = get_file_size(fd);
227
228                 lck_result = fcntl(fd, F_SETLK, &lck);
229
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);
235                 }
236                 close (fd);
237                 dirent = readdir(dir);
238         }
239
240         closedir(dir);
241         return 0;
242
243 error:
244         if (dir)
245                 closedir(dir);
246
247         RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
248                 strerror(errno));
249
250         return -1;
251 }
252
253 static int
254 compare_hpi(const void *a, const void *b)
255 {
256         const struct hugepage_info *hpi_a = a;
257         const struct hugepage_info *hpi_b = b;
258
259         return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
260 }
261
262 /*
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.
266  */
267 int
268 eal_hugepage_info_init(void)
269 {
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;
273         DIR *dir;
274         struct dirent *dirent;
275
276         dir = opendir(sys_dir_path);
277         if (dir == NULL) {
278                 RTE_LOG(ERR, EAL,
279                         "Cannot open directory %s to read system hugepage info\n",
280                         sys_dir_path);
281                 return -1;
282         }
283
284         for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
285                 struct hugepage_info *hpi;
286
287                 if (strncmp(dirent->d_name, dirent_start_text,
288                             dirent_start_len) != 0)
289                         continue;
290
291                 if (num_sizes >= MAX_HUGEPAGE_SIZES)
292                         break;
293
294                 hpi = &internal_config.hugepage_info[num_sizes];
295                 hpi->hugepage_sz =
296                         rte_str_to_size(&dirent->d_name[dirent_start_len]);
297                 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
298
299                 /* first, check if we have a mountpoint */
300                 if (hpi->hugedir == NULL) {
301                         uint32_t num_pages;
302
303                         num_pages = get_num_hugepages(dirent->d_name);
304                         if (num_pages > 0)
305                                 RTE_LOG(NOTICE, EAL,
306                                         "%" PRIu32 " hugepages of size "
307                                         "%" PRIu64 " reserved, but no mounted "
308                                         "hugetlbfs found for that size\n",
309                                         num_pages, hpi->hugepage_sz);
310                         continue;
311                 }
312
313                 /* try to obtain a writelock */
314                 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
315
316                 /* if blocking lock failed */
317                 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
318                         RTE_LOG(CRIT, EAL,
319                                 "Failed to lock hugepage directory!\n");
320                         break;
321                 }
322                 /* clear out the hugepages dir from unused pages */
323                 if (clear_hugedir(hpi->hugedir) == -1)
324                         break;
325
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);
329
330 #ifndef RTE_ARCH_64
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);
335 #endif
336
337                 num_sizes++;
338         }
339         closedir(dir);
340
341         /* something went wrong, and we broke from the for loop above */
342         if (dirent != NULL)
343                 return -1;
344
345         internal_config.num_hugepage_sizes = num_sizes;
346
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);
350
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)
355                         return 0;
356
357         /* no valid hugepage mounts available, return error */
358         return -1;
359 }