eal: add single file segments option
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_memalloc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Intel Corporation
3  */
4
5 #define _FILE_OFFSET_BITS 64
6 #include <errno.h>
7 #include <stdarg.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13 #include <string.h>
14 #include <sys/mman.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/queue.h>
18 #include <sys/file.h>
19 #include <unistd.h>
20 #include <limits.h>
21 #include <fcntl.h>
22 #include <sys/ioctl.h>
23 #include <sys/time.h>
24 #include <signal.h>
25 #include <setjmp.h>
26 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
27 #include <numa.h>
28 #include <numaif.h>
29 #endif
30 #include <linux/falloc.h>
31
32 #include <rte_common.h>
33 #include <rte_log.h>
34 #include <rte_eal_memconfig.h>
35 #include <rte_eal.h>
36 #include <rte_memory.h>
37 #include <rte_spinlock.h>
38
39 #include "eal_filesystem.h"
40 #include "eal_internal_cfg.h"
41 #include "eal_memalloc.h"
42
43 /*
44  * not all kernel version support fallocate on hugetlbfs, so fall back to
45  * ftruncate and disallow deallocation if fallocate is not supported.
46  */
47 static int fallocate_supported = -1; /* unknown */
48
49 /*
50  * If each page is in a separate file, we can close fd's since we need each fd
51  * only once. However, in single file segments mode, we can get away with using
52  * a single fd for entire segments, but we need to store them somewhere. Each
53  * fd is different within each process, so we'll store them in a local tailq.
54  */
55 struct msl_entry {
56         TAILQ_ENTRY(msl_entry) next;
57         unsigned int msl_idx;
58         int fd;
59 };
60
61 /** Double linked list of memseg list fd's. */
62 TAILQ_HEAD(msl_entry_list, msl_entry);
63
64 static struct msl_entry_list msl_entry_list =
65                 TAILQ_HEAD_INITIALIZER(msl_entry_list);
66 static rte_spinlock_t tailq_lock = RTE_SPINLOCK_INITIALIZER;
67
68 static sigjmp_buf huge_jmpenv;
69
70 static void __rte_unused huge_sigbus_handler(int signo __rte_unused)
71 {
72         siglongjmp(huge_jmpenv, 1);
73 }
74
75 /* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
76  * non-static local variable in the stack frame calling sigsetjmp might be
77  * clobbered by a call to longjmp.
78  */
79 static int __rte_unused huge_wrap_sigsetjmp(void)
80 {
81         return sigsetjmp(huge_jmpenv, 1);
82 }
83
84 static struct sigaction huge_action_old;
85 static int huge_need_recover;
86
87 static void __rte_unused
88 huge_register_sigbus(void)
89 {
90         sigset_t mask;
91         struct sigaction action;
92
93         sigemptyset(&mask);
94         sigaddset(&mask, SIGBUS);
95         action.sa_flags = 0;
96         action.sa_mask = mask;
97         action.sa_handler = huge_sigbus_handler;
98
99         huge_need_recover = !sigaction(SIGBUS, &action, &huge_action_old);
100 }
101
102 static void __rte_unused
103 huge_recover_sigbus(void)
104 {
105         if (huge_need_recover) {
106                 sigaction(SIGBUS, &huge_action_old, NULL);
107                 huge_need_recover = 0;
108         }
109 }
110
111 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
112 static bool
113 check_numa(void)
114 {
115         bool ret = true;
116         /* Check if kernel supports NUMA. */
117         if (numa_available() != 0) {
118                 RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
119                 ret = false;
120         }
121         return ret;
122 }
123
124 static void
125 prepare_numa(int *oldpolicy, struct bitmask *oldmask, int socket_id)
126 {
127         RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
128         if (get_mempolicy(oldpolicy, oldmask->maskp,
129                           oldmask->size + 1, 0, 0) < 0) {
130                 RTE_LOG(ERR, EAL,
131                         "Failed to get current mempolicy: %s. "
132                         "Assuming MPOL_DEFAULT.\n", strerror(errno));
133                 oldpolicy = MPOL_DEFAULT;
134         }
135         RTE_LOG(DEBUG, EAL,
136                 "Setting policy MPOL_PREFERRED for socket %d\n",
137                 socket_id);
138         numa_set_preferred(socket_id);
139 }
140
141 static void
142 resotre_numa(int *oldpolicy, struct bitmask *oldmask)
143 {
144         RTE_LOG(DEBUG, EAL,
145                 "Restoring previous memory policy: %d\n", *oldpolicy);
146         if (oldpolicy == MPOL_DEFAULT) {
147                 numa_set_localalloc();
148         } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
149                                  oldmask->size + 1) < 0) {
150                 RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
151                         strerror(errno));
152                 numa_set_localalloc();
153         }
154         numa_free_cpumask(oldmask);
155 }
156 #endif
157
158 static struct msl_entry *
159 get_msl_entry_by_idx(unsigned int list_idx)
160 {
161         struct msl_entry *te;
162
163         rte_spinlock_lock(&tailq_lock);
164
165         TAILQ_FOREACH(te, &msl_entry_list, next) {
166                 if (te->msl_idx == list_idx)
167                         break;
168         }
169         if (te == NULL) {
170                 /* doesn't exist, so create it and set fd to -1 */
171
172                 te = malloc(sizeof(*te));
173                 if (te == NULL) {
174                         RTE_LOG(ERR, EAL, "%s(): cannot allocate tailq entry for memseg list\n",
175                                 __func__);
176                         goto unlock;
177                 }
178                 te->msl_idx = list_idx;
179                 te->fd = -1;
180                 TAILQ_INSERT_TAIL(&msl_entry_list, te, next);
181         }
182 unlock:
183         rte_spinlock_unlock(&tailq_lock);
184         return te;
185 }
186
187 /*
188  * uses fstat to report the size of a file on disk
189  */
190 static off_t
191 get_file_size(int fd)
192 {
193         struct stat st;
194         if (fstat(fd, &st) < 0)
195                 return 0;
196         return st.st_size;
197 }
198
199 /*
200  * uses fstat to check if file size on disk is zero (regular fstat won't show
201  * true file size due to how fallocate works)
202  */
203 static bool
204 is_zero_length(int fd)
205 {
206         struct stat st;
207         if (fstat(fd, &st) < 0)
208                 return false;
209         return st.st_blocks == 0;
210 }
211
212 static int
213 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
214                 unsigned int list_idx, unsigned int seg_idx)
215 {
216         int fd;
217
218         if (internal_config.single_file_segments) {
219                 /*
220                  * try to find a tailq entry, for this memseg list, or create
221                  * one if it doesn't exist.
222                  */
223                 struct msl_entry *te = get_msl_entry_by_idx(list_idx);
224                 if (te == NULL) {
225                         RTE_LOG(ERR, EAL, "%s(): cannot allocate tailq entry for memseg list\n",
226                                 __func__);
227                         return -1;
228                 } else if (te->fd < 0) {
229                         /* create a hugepage file */
230                         eal_get_hugefile_path(path, buflen, hi->hugedir,
231                                         list_idx);
232                         fd = open(path, O_CREAT | O_RDWR, 0600);
233                         if (fd < 0) {
234                                 RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n",
235                                         __func__, strerror(errno));
236                                 return -1;
237                         }
238                         te->fd = fd;
239                 } else {
240                         fd = te->fd;
241                 }
242         } else {
243                 /* one file per page, just create it */
244                 eal_get_hugefile_path(path, buflen, hi->hugedir,
245                                 list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
246                 fd = open(path, O_CREAT | O_RDWR, 0600);
247                 if (fd < 0) {
248                         RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
249                                         strerror(errno));
250                         return -1;
251                 }
252         }
253         return fd;
254 }
255
256 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
257 static int lock(int fd, uint64_t offset, uint64_t len, int type)
258 {
259         struct flock lck;
260         int ret;
261
262         memset(&lck, 0, sizeof(lck));
263
264         lck.l_type = type;
265         lck.l_whence = SEEK_SET;
266         lck.l_start = offset;
267         lck.l_len = len;
268
269         ret = fcntl(fd, F_SETLK, &lck);
270
271         if (ret && (errno == EAGAIN || errno == EACCES)) {
272                 /* locked by another process, not an error */
273                 return 0;
274         } else if (ret) {
275                 RTE_LOG(ERR, EAL, "%s(): error calling fcntl(): %s\n",
276                         __func__, strerror(errno));
277                 /* we've encountered an unexpected error */
278                 return -1;
279         }
280         return 1;
281 }
282
283 static int
284 resize_hugefile(int fd, uint64_t fa_offset, uint64_t page_sz,
285                 bool grow)
286 {
287         bool again = false;
288         do {
289                 if (fallocate_supported == 0) {
290                         /* we cannot deallocate memory if fallocate() is not
291                          * supported, but locks are still needed to prevent
292                          * primary process' initialization from clearing out
293                          * huge pages used by this process.
294                          */
295
296                         if (!grow) {
297                                 RTE_LOG(DEBUG, EAL, "%s(): fallocate not supported, not freeing page back to the system\n",
298                                         __func__);
299                                 return -1;
300                         }
301                         uint64_t new_size = fa_offset + page_sz;
302                         uint64_t cur_size = get_file_size(fd);
303
304                         /* fallocate isn't supported, fall back to ftruncate */
305                         if (new_size > cur_size &&
306                                         ftruncate(fd, new_size) < 0) {
307                                 RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
308                                         __func__, strerror(errno));
309                                 return -1;
310                         }
311                         /* not being able to take out a read lock is an error */
312                         if (lock(fd, fa_offset, page_sz, F_RDLCK) != 1)
313                                 return -1;
314                 } else {
315                         int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
316                                         FALLOC_FL_KEEP_SIZE;
317                         int ret;
318
319                         /* if fallocate() is supported, we need to take out a
320                          * read lock on allocate (to prevent other processes
321                          * from deallocating this page), and take out a write
322                          * lock on deallocate (to ensure nobody else is using
323                          * this page).
324                          *
325                          * we can't use flock() for this, as we actually need to
326                          * lock part of the file, not the entire file.
327                          */
328
329                         if (!grow) {
330                                 ret = lock(fd, fa_offset, page_sz, F_WRLCK);
331
332                                 if (ret < 0)
333                                         return -1;
334                                 else if (ret == 0)
335                                         /* failed to lock, not an error */
336                                         return 0;
337                         }
338                         if (fallocate(fd, flags, fa_offset, page_sz) < 0) {
339                                 if (fallocate_supported == -1 &&
340                                                 errno == ENOTSUP) {
341                                         RTE_LOG(ERR, EAL, "%s(): fallocate() not supported, hugepage deallocation will be disabled\n",
342                                                 __func__);
343                                         again = true;
344                                         fallocate_supported = 0;
345                                 } else {
346                                         RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
347                                                 __func__,
348                                                 strerror(errno));
349                                         return -1;
350                                 }
351                         } else {
352                                 fallocate_supported = 1;
353
354                                 if (grow) {
355                                         /* if can't read lock, it's an error */
356                                         if (lock(fd, fa_offset, page_sz,
357                                                         F_RDLCK) != 1)
358                                                 return -1;
359                                 } else {
360                                         /* if can't unlock, it's an error */
361                                         if (lock(fd, fa_offset, page_sz,
362                                                         F_UNLCK) != 1)
363                                                 return -1;
364                                 }
365                         }
366                 }
367         } while (again);
368         return 0;
369 }
370
371 static int
372 alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
373                 struct hugepage_info *hi, unsigned int list_idx,
374                 unsigned int seg_idx)
375 {
376 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
377         int cur_socket_id = 0;
378 #endif
379         uint64_t map_offset;
380         char path[PATH_MAX];
381         int ret = 0;
382         int fd;
383         size_t alloc_sz;
384
385         fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
386         if (fd < 0)
387                 return -1;
388
389         alloc_sz = hi->hugepage_sz;
390         if (internal_config.single_file_segments) {
391                 map_offset = seg_idx * alloc_sz;
392                 ret = resize_hugefile(fd, map_offset, alloc_sz, true);
393                 if (ret < 1)
394                         goto resized;
395         } else {
396                 map_offset = 0;
397                 if (ftruncate(fd, alloc_sz) < 0) {
398                         RTE_LOG(DEBUG, EAL, "%s(): ftruncate() failed: %s\n",
399                                 __func__, strerror(errno));
400                         goto resized;
401                 }
402                 /* we've allocated a page - take out a read lock. we're using
403                  * fcntl() locks rather than flock() here because doing that
404                  * gives us one huge advantage - fcntl() locks are per-process,
405                  * not per-file descriptor, which means that we don't have to
406                  * keep the original fd's around to keep a lock on the file.
407                  *
408                  * this is useful, because when it comes to unmapping pages, we
409                  * will have to take out a write lock (to figure out if another
410                  * process still has this page mapped), and to do itwith flock()
411                  * we'll have to use original fd, as lock is associated with
412                  * that particular fd. with fcntl(), this is not necessary - we
413                  * can open a new fd and use fcntl() on that.
414                  */
415                 ret = lock(fd, map_offset, alloc_sz, F_RDLCK);
416
417                 /* this should not fail */
418                 if (ret != 1) {
419                         RTE_LOG(ERR, EAL, "%s(): error locking file: %s\n",
420                                 __func__,
421                                 strerror(errno));
422                         goto resized;
423                 }
424         }
425
426         /*
427          * map the segment, and populate page tables, the kernel fills this
428          * segment with zeros if it's a new page.
429          */
430         void *va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
431                         MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, map_offset);
432         /* for non-single file segments, we can close fd here */
433         if (!internal_config.single_file_segments)
434                 close(fd);
435
436         if (va == MAP_FAILED) {
437                 RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
438                         strerror(errno));
439                 goto resized;
440         }
441         if (va != addr) {
442                 RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
443                 goto mapped;
444         }
445
446         rte_iova_t iova = rte_mem_virt2iova(addr);
447         if (iova == RTE_BAD_PHYS_ADDR) {
448                 RTE_LOG(DEBUG, EAL, "%s(): can't get IOVA addr\n",
449                         __func__);
450                 goto mapped;
451         }
452
453 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
454         move_pages(getpid(), 1, &addr, NULL, &cur_socket_id, 0);
455
456         if (cur_socket_id != socket_id) {
457                 RTE_LOG(DEBUG, EAL,
458                                 "%s(): allocation happened on wrong socket (wanted %d, got %d)\n",
459                         __func__, socket_id, cur_socket_id);
460                 goto mapped;
461         }
462 #endif
463
464         /* In linux, hugetlb limitations, like cgroup, are
465          * enforced at fault time instead of mmap(), even
466          * with the option of MAP_POPULATE. Kernel will send
467          * a SIGBUS signal. To avoid to be killed, save stack
468          * environment here, if SIGBUS happens, we can jump
469          * back here.
470          */
471         if (huge_wrap_sigsetjmp()) {
472                 RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more hugepages of size %uMB\n",
473                         (unsigned int)(alloc_sz >> 20));
474                 goto mapped;
475         }
476         *(int *)addr = *(int *)addr;
477
478         ms->addr = addr;
479         ms->hugepage_sz = alloc_sz;
480         ms->len = alloc_sz;
481         ms->nchannel = rte_memory_get_nchannel();
482         ms->nrank = rte_memory_get_nrank();
483         ms->iova = iova;
484         ms->socket_id = socket_id;
485
486         return 0;
487
488 mapped:
489         munmap(addr, alloc_sz);
490 resized:
491         if (internal_config.single_file_segments) {
492                 resize_hugefile(fd, map_offset, alloc_sz, false);
493                 if (is_zero_length(fd)) {
494                         struct msl_entry *te = get_msl_entry_by_idx(list_idx);
495                         if (te != NULL && te->fd >= 0) {
496                                 close(te->fd);
497                                 te->fd = -1;
498                         }
499                         /* ignore errors, can't make it any worse */
500                         unlink(path);
501                 }
502         } else {
503                 close(fd);
504                 unlink(path);
505         }
506         return -1;
507 }
508
509 static int
510 free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
511                 unsigned int list_idx, unsigned int seg_idx)
512 {
513         uint64_t map_offset;
514         char path[PATH_MAX];
515         int fd, ret;
516
517         /* erase page data */
518         memset(ms->addr, 0, ms->len);
519
520         if (mmap(ms->addr, ms->len, PROT_READ,
521                         MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
522                                 MAP_FAILED) {
523                 RTE_LOG(DEBUG, EAL, "couldn't unmap page\n");
524                 return -1;
525         }
526
527         fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
528         if (fd < 0)
529                 return -1;
530
531         if (internal_config.single_file_segments) {
532                 map_offset = seg_idx * ms->len;
533                 if (resize_hugefile(fd, map_offset, ms->len, false))
534                         return -1;
535                 /* if file is zero-length, we've already shrunk it, so it's
536                  * safe to remove.
537                  */
538                 if (is_zero_length(fd)) {
539                         struct msl_entry *te = get_msl_entry_by_idx(list_idx);
540                         if (te != NULL && te->fd >= 0) {
541                                 close(te->fd);
542                                 te->fd = -1;
543                         }
544                         unlink(path);
545                 }
546                 ret = 0;
547         } else {
548                 /* if we're able to take out a write lock, we're the last one
549                  * holding onto this page.
550                  */
551
552                 ret = lock(fd, 0, ms->len, F_WRLCK);
553                 if (ret >= 0) {
554                         /* no one else is using this page */
555                         if (ret == 1)
556                                 unlink(path);
557                         ret = lock(fd, 0, ms->len, F_UNLCK);
558                         if (ret != 1)
559                                 RTE_LOG(ERR, EAL, "%s(): unable to unlock file %s\n",
560                                         __func__, path);
561                 }
562                 close(fd);
563         }
564
565         memset(ms, 0, sizeof(*ms));
566
567         return ret;
568 }
569
570 struct alloc_walk_param {
571         struct hugepage_info *hi;
572         struct rte_memseg **ms;
573         size_t page_sz;
574         unsigned int segs_allocated;
575         unsigned int n_segs;
576         int socket;
577         bool exact;
578 };
579 static int
580 alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
581 {
582         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
583         struct alloc_walk_param *wa = arg;
584         struct rte_memseg_list *cur_msl;
585         size_t page_sz;
586         int cur_idx, start_idx, j;
587         unsigned int msl_idx, need, i;
588
589         if (msl->page_sz != wa->page_sz)
590                 return 0;
591         if (msl->socket_id != wa->socket)
592                 return 0;
593
594         page_sz = (size_t)msl->page_sz;
595
596         msl_idx = msl - mcfg->memsegs;
597         cur_msl = &mcfg->memsegs[msl_idx];
598
599         need = wa->n_segs;
600
601         /* try finding space in memseg list */
602         cur_idx = rte_fbarray_find_next_n_free(&cur_msl->memseg_arr, 0, need);
603         if (cur_idx < 0)
604                 return 0;
605         start_idx = cur_idx;
606
607         for (i = 0; i < need; i++, cur_idx++) {
608                 struct rte_memseg *cur;
609                 void *map_addr;
610
611                 cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
612                 map_addr = RTE_PTR_ADD(cur_msl->base_va,
613                                 cur_idx * page_sz);
614
615                 if (alloc_seg(cur, map_addr, wa->socket, wa->hi,
616                                 msl_idx, cur_idx)) {
617                         RTE_LOG(DEBUG, EAL, "attempted to allocate %i segments, but only %i were allocated\n",
618                                 need, i);
619
620                         /* if exact number wasn't requested, stop */
621                         if (!wa->exact)
622                                 goto out;
623
624                         /* clean up */
625                         for (j = start_idx; j < cur_idx; j++) {
626                                 struct rte_memseg *tmp;
627                                 struct rte_fbarray *arr =
628                                                 &cur_msl->memseg_arr;
629
630                                 tmp = rte_fbarray_get(arr, j);
631                                 if (free_seg(tmp, wa->hi, msl_idx,
632                                                 start_idx + j)) {
633                                         RTE_LOG(ERR, EAL, "Cannot free page\n");
634                                         continue;
635                                 }
636
637                                 rte_fbarray_set_free(arr, j);
638                         }
639                         /* clear the list */
640                         if (wa->ms)
641                                 memset(wa->ms, 0, sizeof(*wa->ms) * wa->n_segs);
642                         return -1;
643                 }
644                 if (wa->ms)
645                         wa->ms[i] = cur;
646
647                 rte_fbarray_set_used(&cur_msl->memseg_arr, cur_idx);
648         }
649 out:
650         wa->segs_allocated = i;
651         return 1;
652 }
653
654 struct free_walk_param {
655         struct hugepage_info *hi;
656         struct rte_memseg *ms;
657 };
658 static int
659 free_seg_walk(const struct rte_memseg_list *msl, void *arg)
660 {
661         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
662         struct rte_memseg_list *found_msl;
663         struct free_walk_param *wa = arg;
664         uintptr_t start_addr, end_addr;
665         int msl_idx, seg_idx;
666
667         start_addr = (uintptr_t) msl->base_va;
668         end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
669
670         if ((uintptr_t)wa->ms->addr < start_addr ||
671                         (uintptr_t)wa->ms->addr >= end_addr)
672                 return 0;
673
674         msl_idx = msl - mcfg->memsegs;
675         seg_idx = RTE_PTR_DIFF(wa->ms->addr, start_addr) / msl->page_sz;
676
677         /* msl is const */
678         found_msl = &mcfg->memsegs[msl_idx];
679
680         rte_fbarray_set_free(&found_msl->memseg_arr, seg_idx);
681         if (free_seg(wa->ms, wa->hi, msl_idx, seg_idx))
682                 return -1;
683
684         return 1;
685 }
686
687 int
688 eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
689                 int socket, bool exact)
690 {
691         int i, ret = -1;
692 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
693         bool have_numa = false;
694         int oldpolicy;
695         struct bitmask *oldmask;
696 #endif
697         struct alloc_walk_param wa;
698         struct hugepage_info *hi = NULL;
699
700         memset(&wa, 0, sizeof(wa));
701
702         /* dynamic allocation not supported in legacy mode */
703         if (internal_config.legacy_mem)
704                 return -1;
705
706         for (i = 0; i < (int) RTE_DIM(internal_config.hugepage_info); i++) {
707                 if (page_sz ==
708                                 internal_config.hugepage_info[i].hugepage_sz) {
709                         hi = &internal_config.hugepage_info[i];
710                         break;
711                 }
712         }
713         if (!hi) {
714                 RTE_LOG(ERR, EAL, "%s(): can't find relevant hugepage_info entry\n",
715                         __func__);
716                 return -1;
717         }
718
719 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
720         if (check_numa()) {
721                 oldmask = numa_allocate_nodemask();
722                 prepare_numa(&oldpolicy, oldmask, socket);
723                 have_numa = true;
724         }
725 #endif
726
727         wa.exact = exact;
728         wa.hi = hi;
729         wa.ms = ms;
730         wa.n_segs = n_segs;
731         wa.page_sz = page_sz;
732         wa.socket = socket;
733         wa.segs_allocated = 0;
734
735         ret = rte_memseg_list_walk(alloc_seg_walk, &wa);
736         if (ret == 0) {
737                 RTE_LOG(ERR, EAL, "%s(): couldn't find suitable memseg_list\n",
738                         __func__);
739                 ret = -1;
740         } else if (ret > 0) {
741                 ret = (int)wa.segs_allocated;
742         }
743
744 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
745         if (have_numa)
746                 resotre_numa(&oldpolicy, oldmask);
747 #endif
748         return ret;
749 }
750
751 struct rte_memseg *
752 eal_memalloc_alloc_seg(size_t page_sz, int socket)
753 {
754         struct rte_memseg *ms;
755         if (eal_memalloc_alloc_seg_bulk(&ms, 1, page_sz, socket, true) < 0)
756                 return NULL;
757         /* return pointer to newly allocated memseg */
758         return ms;
759 }
760
761 int
762 eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs)
763 {
764         int seg, ret = 0;
765
766         /* dynamic free not supported in legacy mode */
767         if (internal_config.legacy_mem)
768                 return -1;
769
770         for (seg = 0; seg < n_segs; seg++) {
771                 struct rte_memseg *cur = ms[seg];
772                 struct hugepage_info *hi = NULL;
773                 struct free_walk_param wa;
774                 int i, walk_res;
775
776                 memset(&wa, 0, sizeof(wa));
777
778                 for (i = 0; i < (int)RTE_DIM(internal_config.hugepage_info);
779                                 i++) {
780                         hi = &internal_config.hugepage_info[i];
781                         if (cur->hugepage_sz == hi->hugepage_sz)
782                                 break;
783                 }
784                 if (i == (int)RTE_DIM(internal_config.hugepage_info)) {
785                         RTE_LOG(ERR, EAL, "Can't find relevant hugepage_info entry\n");
786                         ret = -1;
787                         continue;
788                 }
789
790                 wa.ms = cur;
791                 wa.hi = hi;
792
793                 walk_res = rte_memseg_list_walk(free_seg_walk, &wa);
794                 if (walk_res == 1)
795                         continue;
796                 if (walk_res == 0)
797                         RTE_LOG(ERR, EAL, "Couldn't find memseg list\n");
798                 ret = -1;
799         }
800         return ret;
801 }
802
803 int
804 eal_memalloc_free_seg(struct rte_memseg *ms)
805 {
806         /* dynamic free not supported in legacy mode */
807         if (internal_config.legacy_mem)
808                 return -1;
809
810         return eal_memalloc_free_seg_bulk(&ms, 1);
811 }