mem: fix undefined behavior in NUMA-aware mapping
[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/mman.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20
21 #include <linux/mman.h> /* for hugetlb-related flags */
22
23 #include <rte_memory.h>
24 #include <rte_eal.h>
25 #include <rte_launch.h>
26 #include <rte_per_lcore.h>
27 #include <rte_lcore.h>
28 #include <rte_debug.h>
29 #include <rte_log.h>
30 #include <rte_common.h>
31 #include "rte_string_fns.h"
32 #include "eal_internal_cfg.h"
33 #include "eal_hugepages.h"
34 #include "eal_filesystem.h"
35
36 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
37 static const char sys_pages_numa_dir_path[] = "/sys/devices/system/node";
38
39 /*
40  * Uses mmap to create a shared memory area for storage of data
41  * Used in this file to store the hugepage file map on disk
42  */
43 static void *
44 map_shared_memory(const char *filename, const size_t mem_size, int flags)
45 {
46         void *retval;
47         int fd = open(filename, flags, 0666);
48         if (fd < 0)
49                 return NULL;
50         if (ftruncate(fd, mem_size) < 0) {
51                 close(fd);
52                 return NULL;
53         }
54         retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
55                         MAP_SHARED, fd, 0);
56         close(fd);
57         return retval;
58 }
59
60 static void *
61 open_shared_memory(const char *filename, const size_t mem_size)
62 {
63         return map_shared_memory(filename, mem_size, O_RDWR);
64 }
65
66 static void *
67 create_shared_memory(const char *filename, const size_t mem_size)
68 {
69         return map_shared_memory(filename, mem_size, O_RDWR | O_CREAT);
70 }
71
72 /* this function is only called from eal_hugepage_info_init which itself
73  * is only called from a primary process */
74 static uint32_t
75 get_num_hugepages(const char *subdir)
76 {
77         char path[PATH_MAX];
78         long unsigned resv_pages, num_pages = 0;
79         const char *nr_hp_file = "free_hugepages";
80         const char *nr_rsvd_file = "resv_hugepages";
81
82         /* first, check how many reserved pages kernel reports */
83         snprintf(path, sizeof(path), "%s/%s/%s",
84                         sys_dir_path, subdir, nr_rsvd_file);
85         if (eal_parse_sysfs_value(path, &resv_pages) < 0)
86                 return 0;
87
88         snprintf(path, sizeof(path), "%s/%s/%s",
89                         sys_dir_path, subdir, nr_hp_file);
90         if (eal_parse_sysfs_value(path, &num_pages) < 0)
91                 return 0;
92
93         if (num_pages == 0)
94                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
95                                 subdir);
96
97         /* adjust num_pages */
98         if (num_pages >= resv_pages)
99                 num_pages -= resv_pages;
100         else if (resv_pages)
101                 num_pages = 0;
102
103         /* we want to return a uint32_t and more than this looks suspicious
104          * anyway ... */
105         if (num_pages > UINT32_MAX)
106                 num_pages = UINT32_MAX;
107
108         return num_pages;
109 }
110
111 static uint32_t
112 get_num_hugepages_on_node(const char *subdir, unsigned int socket)
113 {
114         char path[PATH_MAX], socketpath[PATH_MAX];
115         DIR *socketdir;
116         unsigned long num_pages = 0;
117         const char *nr_hp_file = "free_hugepages";
118
119         snprintf(socketpath, sizeof(socketpath), "%s/node%u/hugepages",
120                 sys_pages_numa_dir_path, socket);
121
122         socketdir = opendir(socketpath);
123         if (socketdir) {
124                 /* Keep calm and carry on */
125                 closedir(socketdir);
126         } else {
127                 /* Can't find socket dir, so ignore it */
128                 return 0;
129         }
130
131         snprintf(path, sizeof(path), "%s/%s/%s",
132                         socketpath, subdir, nr_hp_file);
133         if (eal_parse_sysfs_value(path, &num_pages) < 0)
134                 return 0;
135
136         if (num_pages == 0)
137                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
138                                 subdir);
139
140         /*
141          * we want to return a uint32_t and more than this looks suspicious
142          * anyway ...
143          */
144         if (num_pages > UINT32_MAX)
145                 num_pages = UINT32_MAX;
146
147         return num_pages;
148 }
149
150 static uint64_t
151 get_default_hp_size(void)
152 {
153         const char proc_meminfo[] = "/proc/meminfo";
154         const char str_hugepagesz[] = "Hugepagesize:";
155         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
156         char buffer[256];
157         unsigned long long size = 0;
158
159         FILE *fd = fopen(proc_meminfo, "r");
160         if (fd == NULL)
161                 rte_panic("Cannot open %s\n", proc_meminfo);
162         while(fgets(buffer, sizeof(buffer), fd)){
163                 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
164                         size = rte_str_to_size(&buffer[hugepagesz_len]);
165                         break;
166                 }
167         }
168         fclose(fd);
169         if (size == 0)
170                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
171         return size;
172 }
173
174 static int
175 get_hugepage_dir(uint64_t hugepage_sz, char *hugedir, int len)
176 {
177         enum proc_mount_fieldnames {
178                 DEVICE = 0,
179                 MOUNTPT,
180                 FSTYPE,
181                 OPTIONS,
182                 _FIELDNAME_MAX
183         };
184         static uint64_t default_size = 0;
185         const char proc_mounts[] = "/proc/mounts";
186         const char hugetlbfs_str[] = "hugetlbfs";
187         const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
188         const char pagesize_opt[] = "pagesize=";
189         const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
190         const char split_tok = ' ';
191         char *splitstr[_FIELDNAME_MAX];
192         char buf[BUFSIZ];
193         int retval = -1;
194
195         FILE *fd = fopen(proc_mounts, "r");
196         if (fd == NULL)
197                 rte_panic("Cannot open %s\n", proc_mounts);
198
199         if (default_size == 0)
200                 default_size = get_default_hp_size();
201
202         while (fgets(buf, sizeof(buf), fd)){
203                 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
204                                 split_tok) != _FIELDNAME_MAX) {
205                         RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
206                         break; /* return NULL */
207                 }
208
209                 /* we have a specified --huge-dir option, only examine that dir */
210                 if (internal_config.hugepage_dir != NULL &&
211                                 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
212                         continue;
213
214                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
215                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
216
217                         /* if no explicit page size, the default page size is compared */
218                         if (pagesz_str == NULL){
219                                 if (hugepage_sz == default_size){
220                                         strlcpy(hugedir, splitstr[MOUNTPT], len);
221                                         retval = 0;
222                                         break;
223                                 }
224                         }
225                         /* there is an explicit page size, so check it */
226                         else {
227                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
228                                 if (pagesz == hugepage_sz) {
229                                         strlcpy(hugedir, splitstr[MOUNTPT], len);
230                                         retval = 0;
231                                         break;
232                                 }
233                         }
234                 } /* end if strncmp hugetlbfs */
235         } /* end while fgets */
236
237         fclose(fd);
238         return retval;
239 }
240
241 /*
242  * Clear the hugepage directory of whatever hugepage files
243  * there are. Checks if the file is locked (i.e.
244  * if it's in use by another DPDK process).
245  */
246 static int
247 clear_hugedir(const char * hugedir)
248 {
249         DIR *dir;
250         struct dirent *dirent;
251         int dir_fd, fd, lck_result;
252         const char filter[] = "*map_*"; /* matches hugepage files */
253
254         /* open directory */
255         dir = opendir(hugedir);
256         if (!dir) {
257                 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
258                                 hugedir);
259                 goto error;
260         }
261         dir_fd = dirfd(dir);
262
263         dirent = readdir(dir);
264         if (!dirent) {
265                 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
266                                 hugedir);
267                 goto error;
268         }
269
270         while(dirent != NULL){
271                 /* skip files that don't match the hugepage pattern */
272                 if (fnmatch(filter, dirent->d_name, 0) > 0) {
273                         dirent = readdir(dir);
274                         continue;
275                 }
276
277                 /* try and lock the file */
278                 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
279
280                 /* skip to next file */
281                 if (fd == -1) {
282                         dirent = readdir(dir);
283                         continue;
284                 }
285
286                 /* non-blocking lock */
287                 lck_result = flock(fd, LOCK_EX | LOCK_NB);
288
289                 /* if lock succeeds, remove the file */
290                 if (lck_result != -1)
291                         unlinkat(dir_fd, dirent->d_name, 0);
292                 close (fd);
293                 dirent = readdir(dir);
294         }
295
296         closedir(dir);
297         return 0;
298
299 error:
300         if (dir)
301                 closedir(dir);
302
303         RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
304                 strerror(errno));
305
306         return -1;
307 }
308
309 static int
310 compare_hpi(const void *a, const void *b)
311 {
312         const struct hugepage_info *hpi_a = a;
313         const struct hugepage_info *hpi_b = b;
314
315         return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
316 }
317
318 static void
319 calc_num_pages(struct hugepage_info *hpi, struct dirent *dirent)
320 {
321         uint64_t total_pages = 0;
322         unsigned int i;
323
324         /*
325          * first, try to put all hugepages into relevant sockets, but
326          * if first attempts fails, fall back to collecting all pages
327          * in one socket and sorting them later
328          */
329         total_pages = 0;
330         /* we also don't want to do this for legacy init */
331         if (!internal_config.legacy_mem)
332                 for (i = 0; i < rte_socket_count(); i++) {
333                         int socket = rte_socket_id_by_idx(i);
334                         unsigned int num_pages =
335                                         get_num_hugepages_on_node(
336                                                 dirent->d_name, socket);
337                         hpi->num_pages[socket] = num_pages;
338                         total_pages += num_pages;
339                 }
340         /*
341          * we failed to sort memory from the get go, so fall
342          * back to old way
343          */
344         if (total_pages == 0) {
345                 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
346
347 #ifndef RTE_ARCH_64
348                 /* for 32-bit systems, limit number of hugepages to
349                  * 1GB per page size */
350                 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
351                                 RTE_PGSIZE_1G / hpi->hugepage_sz);
352 #endif
353         }
354 }
355
356 static int
357 hugepage_info_init(void)
358 {       const char dirent_start_text[] = "hugepages-";
359         const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
360         unsigned int i, num_sizes = 0;
361         DIR *dir;
362         struct dirent *dirent;
363
364         dir = opendir(sys_dir_path);
365         if (dir == NULL) {
366                 RTE_LOG(ERR, EAL,
367                         "Cannot open directory %s to read system hugepage info\n",
368                         sys_dir_path);
369                 return -1;
370         }
371
372         for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
373                 struct hugepage_info *hpi;
374
375                 if (strncmp(dirent->d_name, dirent_start_text,
376                             dirent_start_len) != 0)
377                         continue;
378
379                 if (num_sizes >= MAX_HUGEPAGE_SIZES)
380                         break;
381
382                 hpi = &internal_config.hugepage_info[num_sizes];
383                 hpi->hugepage_sz =
384                         rte_str_to_size(&dirent->d_name[dirent_start_len]);
385
386                 /* first, check if we have a mountpoint */
387                 if (get_hugepage_dir(hpi->hugepage_sz,
388                         hpi->hugedir, sizeof(hpi->hugedir)) < 0) {
389                         uint32_t num_pages;
390
391                         num_pages = get_num_hugepages(dirent->d_name);
392                         if (num_pages > 0)
393                                 RTE_LOG(NOTICE, EAL,
394                                         "%" PRIu32 " hugepages of size "
395                                         "%" PRIu64 " reserved, but no mounted "
396                                         "hugetlbfs found for that size\n",
397                                         num_pages, hpi->hugepage_sz);
398                         /* if we have kernel support for reserving hugepages
399                          * through mmap, and we're in in-memory mode, treat this
400                          * page size as valid. we cannot be in legacy mode at
401                          * this point because we've checked this earlier in the
402                          * init process.
403                          */
404 #ifdef MAP_HUGE_SHIFT
405                         if (internal_config.in_memory) {
406                                 RTE_LOG(DEBUG, EAL, "In-memory mode enabled, "
407                                         "hugepages of size %" PRIu64 " bytes "
408                                         "will be allocated anonymously\n",
409                                         hpi->hugepage_sz);
410                                 calc_num_pages(hpi, dirent);
411                                 num_sizes++;
412                         }
413 #endif
414                         continue;
415                 }
416
417                 /* try to obtain a writelock */
418                 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
419
420                 /* if blocking lock failed */
421                 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
422                         RTE_LOG(CRIT, EAL,
423                                 "Failed to lock hugepage directory!\n");
424                         break;
425                 }
426                 /* clear out the hugepages dir from unused pages */
427                 if (clear_hugedir(hpi->hugedir) == -1)
428                         break;
429
430                 calc_num_pages(hpi, dirent);
431
432                 num_sizes++;
433         }
434         closedir(dir);
435
436         /* something went wrong, and we broke from the for loop above */
437         if (dirent != NULL)
438                 return -1;
439
440         internal_config.num_hugepage_sizes = num_sizes;
441
442         /* sort the page directory entries by size, largest to smallest */
443         qsort(&internal_config.hugepage_info[0], num_sizes,
444               sizeof(internal_config.hugepage_info[0]), compare_hpi);
445
446         /* now we have all info, check we have at least one valid size */
447         for (i = 0; i < num_sizes; i++) {
448                 /* pages may no longer all be on socket 0, so check all */
449                 unsigned int j, num_pages = 0;
450                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
451
452                 for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
453                         num_pages += hpi->num_pages[j];
454                 if (num_pages > 0)
455                         return 0;
456         }
457
458         /* no valid hugepage mounts available, return error */
459         return -1;
460 }
461
462 /*
463  * when we initialize the hugepage info, everything goes
464  * to socket 0 by default. it will later get sorted by memory
465  * initialization procedure.
466  */
467 int
468 eal_hugepage_info_init(void)
469 {
470         struct hugepage_info *hpi, *tmp_hpi;
471         unsigned int i;
472
473         if (hugepage_info_init() < 0)
474                 return -1;
475
476         /* for no shared files mode, we're done */
477         if (internal_config.no_shconf)
478                 return 0;
479
480         hpi = &internal_config.hugepage_info[0];
481
482         tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
483                         sizeof(internal_config.hugepage_info));
484         if (tmp_hpi == NULL) {
485                 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
486                 return -1;
487         }
488
489         memcpy(tmp_hpi, hpi, sizeof(internal_config.hugepage_info));
490
491         /* we've copied file descriptors along with everything else, but they
492          * will be invalid in secondary process, so overwrite them
493          */
494         for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
495                 struct hugepage_info *tmp = &tmp_hpi[i];
496                 tmp->lock_descriptor = -1;
497         }
498
499         if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
500                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
501                 return -1;
502         }
503         return 0;
504 }
505
506 int eal_hugepage_info_read(void)
507 {
508         struct hugepage_info *hpi = &internal_config.hugepage_info[0];
509         struct hugepage_info *tmp_hpi;
510
511         tmp_hpi = open_shared_memory(eal_hugepage_info_path(),
512                                   sizeof(internal_config.hugepage_info));
513         if (tmp_hpi == NULL) {
514                 RTE_LOG(ERR, EAL, "Failed to open shared memory!\n");
515                 return -1;
516         }
517
518         memcpy(hpi, tmp_hpi, sizeof(internal_config.hugepage_info));
519
520         if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
521                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
522                 return -1;
523         }
524         return 0;
525 }