New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_hugepage_info.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/file.h>
37 #include <dirent.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <fnmatch.h>
42 #include <inttypes.h>
43 #include <stdarg.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <sys/queue.h>
47
48 #include <rte_memory.h>
49 #include <rte_eal.h>
50 #include <rte_launch.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_debug.h>
54 #include <rte_log.h>
55 #include <rte_common.h>
56 #include "rte_string_fns.h"
57 #include "eal_internal_cfg.h"
58 #include "eal_hugepages.h"
59 #include "eal_filesystem.h"
60
61 static const char sys_dir_path[] = "/sys/kernel/mm/hugepages";
62
63 /* this function is only called from eal_hugepage_info_init which itself
64  * is only called from a primary process */
65 static uint32_t
66 get_num_hugepages(const char *subdir)
67 {
68         char path[PATH_MAX];
69         long unsigned resv_pages, num_pages = 0;
70         const char *nr_hp_file = "free_hugepages";
71         const char *nr_rsvd_file = "resv_hugepages";
72
73         /* first, check how many reserved pages kernel reports */
74         snprintf(path, sizeof(path), "%s/%s/%s",
75                         sys_dir_path, subdir, nr_rsvd_file);
76         if (eal_parse_sysfs_value(path, &resv_pages) < 0)
77                 return 0;
78
79         snprintf(path, sizeof(path), "%s/%s/%s",
80                         sys_dir_path, subdir, nr_hp_file);
81         if (eal_parse_sysfs_value(path, &num_pages) < 0)
82                 return 0;
83
84         if (num_pages == 0)
85                 RTE_LOG(WARNING, EAL, "No free hugepages reported in %s\n",
86                                 subdir);
87
88         /* adjust num_pages */
89         if (num_pages >= resv_pages)
90                 num_pages -= resv_pages;
91         else if (resv_pages)
92                 num_pages = 0;
93
94         /* we want to return a uint32_t and more than this looks suspicious
95          * anyway ... */
96         if (num_pages > UINT32_MAX)
97                 num_pages = UINT32_MAX;
98
99         return num_pages;
100 }
101
102 static uint64_t
103 get_default_hp_size(void)
104 {
105         const char proc_meminfo[] = "/proc/meminfo";
106         const char str_hugepagesz[] = "Hugepagesize:";
107         unsigned hugepagesz_len = sizeof(str_hugepagesz) - 1;
108         char buffer[256];
109         unsigned long long size = 0;
110
111         FILE *fd = fopen(proc_meminfo, "r");
112         if (fd == NULL)
113                 rte_panic("Cannot open %s\n", proc_meminfo);
114         while(fgets(buffer, sizeof(buffer), fd)){
115                 if (strncmp(buffer, str_hugepagesz, hugepagesz_len) == 0){
116                         size = rte_str_to_size(&buffer[hugepagesz_len]);
117                         break;
118                 }
119         }
120         fclose(fd);
121         if (size == 0)
122                 rte_panic("Cannot get default hugepage size from %s\n", proc_meminfo);
123         return size;
124 }
125
126 static const char *
127 get_hugepage_dir(uint64_t hugepage_sz)
128 {
129         enum proc_mount_fieldnames {
130                 DEVICE = 0,
131                 MOUNTPT,
132                 FSTYPE,
133                 OPTIONS,
134                 _FIELDNAME_MAX
135         };
136         static uint64_t default_size = 0;
137         const char proc_mounts[] = "/proc/mounts";
138         const char hugetlbfs_str[] = "hugetlbfs";
139         const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1;
140         const char pagesize_opt[] = "pagesize=";
141         const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1;
142         const char split_tok = ' ';
143         char *splitstr[_FIELDNAME_MAX];
144         char buf[BUFSIZ];
145         char *retval = NULL;
146
147         FILE *fd = fopen(proc_mounts, "r");
148         if (fd == NULL)
149                 rte_panic("Cannot open %s\n", proc_mounts);
150
151         if (default_size == 0)
152                 default_size = get_default_hp_size();
153
154         while (fgets(buf, sizeof(buf), fd)){
155                 if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX,
156                                 split_tok) != _FIELDNAME_MAX) {
157                         RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts);
158                         break; /* return NULL */
159                 }
160
161                 /* we have a specified --huge-dir option, only examine that dir */
162                 if (internal_config.hugepage_dir != NULL &&
163                                 strcmp(splitstr[MOUNTPT], internal_config.hugepage_dir) != 0)
164                         continue;
165
166                 if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){
167                         const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt);
168
169                         /* if no explicit page size, the default page size is compared */
170                         if (pagesz_str == NULL){
171                                 if (hugepage_sz == default_size){
172                                         retval = strdup(splitstr[MOUNTPT]);
173                                         break;
174                                 }
175                         }
176                         /* there is an explicit page size, so check it */
177                         else {
178                                 uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]);
179                                 if (pagesz == hugepage_sz) {
180                                         retval = strdup(splitstr[MOUNTPT]);
181                                         break;
182                                 }
183                         }
184                 } /* end if strncmp hugetlbfs */
185         } /* end while fgets */
186
187         fclose(fd);
188         return retval;
189 }
190
191 /*
192  * Clear the hugepage directory of whatever hugepage files
193  * there are. Checks if the file is locked (i.e.
194  * if it's in use by another DPDK process).
195  */
196 static int
197 clear_hugedir(const char * hugedir)
198 {
199         DIR *dir;
200         struct dirent *dirent;
201         int dir_fd, fd, lck_result;
202         const char filter[] = "*map_*"; /* matches hugepage files */
203
204         /* open directory */
205         dir = opendir(hugedir);
206         if (!dir) {
207                 RTE_LOG(ERR, EAL, "Unable to open hugepage directory %s\n",
208                                 hugedir);
209                 goto error;
210         }
211         dir_fd = dirfd(dir);
212
213         dirent = readdir(dir);
214         if (!dirent) {
215                 RTE_LOG(ERR, EAL, "Unable to read hugepage directory %s\n",
216                                 hugedir);
217                 goto error;
218         }
219
220         while(dirent != NULL){
221                 /* skip files that don't match the hugepage pattern */
222                 if (fnmatch(filter, dirent->d_name, 0) > 0) {
223                         dirent = readdir(dir);
224                         continue;
225                 }
226
227                 /* try and lock the file */
228                 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
229
230                 /* skip to next file */
231                 if (fd == -1) {
232                         dirent = readdir(dir);
233                         continue;
234                 }
235
236                 /* non-blocking lock */
237                 lck_result = flock(fd, LOCK_EX | LOCK_NB);
238
239                 /* if lock succeeds, unlock and remove the file */
240                 if (lck_result != -1) {
241                         flock(fd, LOCK_UN);
242                         unlinkat(dir_fd, dirent->d_name, 0);
243                 }
244                 close (fd);
245                 dirent = readdir(dir);
246         }
247
248         closedir(dir);
249         return 0;
250
251 error:
252         if (dir)
253                 closedir(dir);
254
255         RTE_LOG(ERR, EAL, "Error while clearing hugepage dir: %s\n",
256                 strerror(errno));
257
258         return -1;
259 }
260
261 static int
262 compare_hpi(const void *a, const void *b)
263 {
264         const struct hugepage_info *hpi_a = a;
265         const struct hugepage_info *hpi_b = b;
266
267         return hpi_b->hugepage_sz - hpi_a->hugepage_sz;
268 }
269
270 /*
271  * when we initialize the hugepage info, everything goes
272  * to socket 0 by default. it will later get sorted by memory
273  * initialization procedure.
274  */
275 int
276 eal_hugepage_info_init(void)
277 {
278         const char dirent_start_text[] = "hugepages-";
279         const size_t dirent_start_len = sizeof(dirent_start_text) - 1;
280         unsigned i, num_sizes = 0;
281         DIR *dir;
282         struct dirent *dirent;
283
284         dir = opendir(sys_dir_path);
285         if (dir == NULL) {
286                 RTE_LOG(ERR, EAL,
287                         "Cannot open directory %s to read system hugepage info\n",
288                         sys_dir_path);
289                 return -1;
290         }
291
292         for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
293                 struct hugepage_info *hpi;
294
295                 if (strncmp(dirent->d_name, dirent_start_text,
296                             dirent_start_len) != 0)
297                         continue;
298
299                 if (num_sizes >= MAX_HUGEPAGE_SIZES)
300                         break;
301
302                 hpi = &internal_config.hugepage_info[num_sizes];
303                 hpi->hugepage_sz =
304                         rte_str_to_size(&dirent->d_name[dirent_start_len]);
305                 hpi->hugedir = get_hugepage_dir(hpi->hugepage_sz);
306
307                 /* first, check if we have a mountpoint */
308                 if (hpi->hugedir == NULL) {
309                         uint32_t num_pages;
310
311                         num_pages = get_num_hugepages(dirent->d_name);
312                         if (num_pages > 0)
313                                 RTE_LOG(NOTICE, EAL,
314                                         "%" PRIu32 " hugepages of size "
315                                         "%" PRIu64 " reserved, but no mounted "
316                                         "hugetlbfs found for that size\n",
317                                         num_pages, hpi->hugepage_sz);
318                         continue;
319                 }
320
321                 /* try to obtain a writelock */
322                 hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
323
324                 /* if blocking lock failed */
325                 if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
326                         RTE_LOG(CRIT, EAL,
327                                 "Failed to lock hugepage directory!\n");
328                         break;
329                 }
330                 /* clear out the hugepages dir from unused pages */
331                 if (clear_hugedir(hpi->hugedir) == -1)
332                         break;
333
334                 /* for now, put all pages into socket 0,
335                  * later they will be sorted */
336                 hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
337
338 #ifndef RTE_ARCH_64
339                 /* for 32-bit systems, limit number of hugepages to
340                  * 1GB per page size */
341                 hpi->num_pages[0] = RTE_MIN(hpi->num_pages[0],
342                                             RTE_PGSIZE_1G / hpi->hugepage_sz);
343 #endif
344
345                 num_sizes++;
346         }
347         closedir(dir);
348
349         /* something went wrong, and we broke from the for loop above */
350         if (dirent != NULL)
351                 return -1;
352
353         internal_config.num_hugepage_sizes = num_sizes;
354
355         /* sort the page directory entries by size, largest to smallest */
356         qsort(&internal_config.hugepage_info[0], num_sizes,
357               sizeof(internal_config.hugepage_info[0]), compare_hpi);
358
359         /* now we have all info, check we have at least one valid size */
360         for (i = 0; i < num_sizes; i++)
361                 if (internal_config.hugepage_info[i].hugedir != NULL &&
362                     internal_config.hugepage_info[i].num_pages[0] > 0)
363                         return 0;
364
365         /* no valid hugepage mounts available, return error */
366         return -1;
367 }