eal: make the global configuration private
[dpdk.git] / lib / librte_eal / common / malloc_heap.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <stdint.h>
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <sys/queue.h>
11
12 #include <rte_memory.h>
13 #include <rte_errno.h>
14 #include <rte_eal.h>
15 #include <rte_eal_memconfig.h>
16 #include <rte_launch.h>
17 #include <rte_per_lcore.h>
18 #include <rte_lcore.h>
19 #include <rte_common.h>
20 #include <rte_string_fns.h>
21 #include <rte_spinlock.h>
22 #include <rte_memcpy.h>
23 #include <rte_memzone.h>
24 #include <rte_atomic.h>
25 #include <rte_fbarray.h>
26
27 #include "eal_internal_cfg.h"
28 #include "eal_memalloc.h"
29 #include "eal_memcfg.h"
30 #include "eal_private.h"
31 #include "malloc_elem.h"
32 #include "malloc_heap.h"
33 #include "malloc_mp.h"
34
35 /* start external socket ID's at a very high number */
36 #define CONST_MAX(a, b) (a > b ? a : b) /* RTE_MAX is not a constant */
37 #define EXTERNAL_HEAP_MIN_SOCKET_ID (CONST_MAX((1 << 8), RTE_MAX_NUMA_NODES))
38
39 static unsigned
40 check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
41 {
42         unsigned check_flag = 0;
43
44         if (!(flags & ~RTE_MEMZONE_SIZE_HINT_ONLY))
45                 return 1;
46
47         switch (hugepage_sz) {
48         case RTE_PGSIZE_256K:
49                 check_flag = RTE_MEMZONE_256KB;
50                 break;
51         case RTE_PGSIZE_2M:
52                 check_flag = RTE_MEMZONE_2MB;
53                 break;
54         case RTE_PGSIZE_16M:
55                 check_flag = RTE_MEMZONE_16MB;
56                 break;
57         case RTE_PGSIZE_256M:
58                 check_flag = RTE_MEMZONE_256MB;
59                 break;
60         case RTE_PGSIZE_512M:
61                 check_flag = RTE_MEMZONE_512MB;
62                 break;
63         case RTE_PGSIZE_1G:
64                 check_flag = RTE_MEMZONE_1GB;
65                 break;
66         case RTE_PGSIZE_4G:
67                 check_flag = RTE_MEMZONE_4GB;
68                 break;
69         case RTE_PGSIZE_16G:
70                 check_flag = RTE_MEMZONE_16GB;
71         }
72
73         return check_flag & flags;
74 }
75
76 int
77 malloc_socket_to_heap_id(unsigned int socket_id)
78 {
79         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
80         int i;
81
82         for (i = 0; i < RTE_MAX_HEAPS; i++) {
83                 struct malloc_heap *heap = &mcfg->malloc_heaps[i];
84
85                 if (heap->socket_id == socket_id)
86                         return i;
87         }
88         return -1;
89 }
90
91 /*
92  * Expand the heap with a memory area.
93  */
94 static struct malloc_elem *
95 malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
96                 void *start, size_t len)
97 {
98         struct malloc_elem *elem = start;
99
100         malloc_elem_init(elem, heap, msl, len, elem, len);
101
102         malloc_elem_insert(elem);
103
104         elem = malloc_elem_join_adjacent_free(elem);
105
106         malloc_elem_free_list_insert(elem);
107
108         return elem;
109 }
110
111 static int
112 malloc_add_seg(const struct rte_memseg_list *msl,
113                 const struct rte_memseg *ms, size_t len, void *arg __rte_unused)
114 {
115         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
116         struct rte_memseg_list *found_msl;
117         struct malloc_heap *heap;
118         int msl_idx, heap_idx;
119
120         if (msl->external)
121                 return 0;
122
123         heap_idx = malloc_socket_to_heap_id(msl->socket_id);
124         if (heap_idx < 0) {
125                 RTE_LOG(ERR, EAL, "Memseg list has invalid socket id\n");
126                 return -1;
127         }
128         heap = &mcfg->malloc_heaps[heap_idx];
129
130         /* msl is const, so find it */
131         msl_idx = msl - mcfg->memsegs;
132
133         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
134                 return -1;
135
136         found_msl = &mcfg->memsegs[msl_idx];
137
138         malloc_heap_add_memory(heap, found_msl, ms->addr, len);
139
140         heap->total_size += len;
141
142         RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
143                         msl->socket_id);
144         return 0;
145 }
146
147 /*
148  * Iterates through the freelist for a heap to find a free element
149  * which can store data of the required size and with the requested alignment.
150  * If size is 0, find the biggest available elem.
151  * Returns null on failure, or pointer to element on success.
152  */
153 static struct malloc_elem *
154 find_suitable_element(struct malloc_heap *heap, size_t size,
155                 unsigned int flags, size_t align, size_t bound, bool contig)
156 {
157         size_t idx;
158         struct malloc_elem *elem, *alt_elem = NULL;
159
160         for (idx = malloc_elem_free_list_index(size);
161                         idx < RTE_HEAP_NUM_FREELISTS; idx++) {
162                 for (elem = LIST_FIRST(&heap->free_head[idx]);
163                                 !!elem; elem = LIST_NEXT(elem, free_list)) {
164                         if (malloc_elem_can_hold(elem, size, align, bound,
165                                         contig)) {
166                                 if (check_hugepage_sz(flags,
167                                                 elem->msl->page_sz))
168                                         return elem;
169                                 if (alt_elem == NULL)
170                                         alt_elem = elem;
171                         }
172                 }
173         }
174
175         if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
176                 return alt_elem;
177
178         return NULL;
179 }
180
181 /*
182  * Iterates through the freelist for a heap to find a free element with the
183  * biggest size and requested alignment. Will also set size to whatever element
184  * size that was found.
185  * Returns null on failure, or pointer to element on success.
186  */
187 static struct malloc_elem *
188 find_biggest_element(struct malloc_heap *heap, size_t *size,
189                 unsigned int flags, size_t align, bool contig)
190 {
191         struct malloc_elem *elem, *max_elem = NULL;
192         size_t idx, max_size = 0;
193
194         for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
195                 for (elem = LIST_FIRST(&heap->free_head[idx]);
196                                 !!elem; elem = LIST_NEXT(elem, free_list)) {
197                         size_t cur_size;
198                         if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY) == 0 &&
199                                         !check_hugepage_sz(flags,
200                                                 elem->msl->page_sz))
201                                 continue;
202                         if (contig) {
203                                 cur_size =
204                                         malloc_elem_find_max_iova_contig(elem,
205                                                         align);
206                         } else {
207                                 void *data_start = RTE_PTR_ADD(elem,
208                                                 MALLOC_ELEM_HEADER_LEN);
209                                 void *data_end = RTE_PTR_ADD(elem, elem->size -
210                                                 MALLOC_ELEM_TRAILER_LEN);
211                                 void *aligned = RTE_PTR_ALIGN_CEIL(data_start,
212                                                 align);
213                                 /* check if aligned data start is beyond end */
214                                 if (aligned >= data_end)
215                                         continue;
216                                 cur_size = RTE_PTR_DIFF(data_end, aligned);
217                         }
218                         if (cur_size > max_size) {
219                                 max_size = cur_size;
220                                 max_elem = elem;
221                         }
222                 }
223         }
224
225         *size = max_size;
226         return max_elem;
227 }
228
229 /*
230  * Main function to allocate a block of memory from the heap.
231  * It locks the free list, scans it, and adds a new memseg if the
232  * scan fails. Once the new memseg is added, it re-scans and should return
233  * the new element after releasing the lock.
234  */
235 static void *
236 heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
237                 unsigned int flags, size_t align, size_t bound, bool contig)
238 {
239         struct malloc_elem *elem;
240
241         size = RTE_CACHE_LINE_ROUNDUP(size);
242         align = RTE_CACHE_LINE_ROUNDUP(align);
243
244         elem = find_suitable_element(heap, size, flags, align, bound, contig);
245         if (elem != NULL) {
246                 elem = malloc_elem_alloc(elem, size, align, bound, contig);
247
248                 /* increase heap's count of allocated elements */
249                 heap->alloc_count++;
250         }
251
252         return elem == NULL ? NULL : (void *)(&elem[1]);
253 }
254
255 static void *
256 heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,
257                 unsigned int flags, size_t align, bool contig)
258 {
259         struct malloc_elem *elem;
260         size_t size;
261
262         align = RTE_CACHE_LINE_ROUNDUP(align);
263
264         elem = find_biggest_element(heap, &size, flags, align, contig);
265         if (elem != NULL) {
266                 elem = malloc_elem_alloc(elem, size, align, 0, contig);
267
268                 /* increase heap's count of allocated elements */
269                 heap->alloc_count++;
270         }
271
272         return elem == NULL ? NULL : (void *)(&elem[1]);
273 }
274
275 /* this function is exposed in malloc_mp.h */
276 void
277 rollback_expand_heap(struct rte_memseg **ms, int n_segs,
278                 struct malloc_elem *elem, void *map_addr, size_t map_len)
279 {
280         if (elem != NULL) {
281                 malloc_elem_free_list_remove(elem);
282                 malloc_elem_hide_region(elem, map_addr, map_len);
283         }
284
285         eal_memalloc_free_seg_bulk(ms, n_segs);
286 }
287
288 /* this function is exposed in malloc_mp.h */
289 struct malloc_elem *
290 alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
291                 int socket, unsigned int flags, size_t align, size_t bound,
292                 bool contig, struct rte_memseg **ms, int n_segs)
293 {
294         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
295         struct rte_memseg_list *msl;
296         struct malloc_elem *elem = NULL;
297         size_t alloc_sz;
298         int allocd_pages;
299         void *ret, *map_addr;
300
301         alloc_sz = (size_t)pg_sz * n_segs;
302
303         /* first, check if we're allowed to allocate this memory */
304         if (eal_memalloc_mem_alloc_validate(socket,
305                         heap->total_size + alloc_sz) < 0) {
306                 RTE_LOG(DEBUG, EAL, "User has disallowed allocation\n");
307                 return NULL;
308         }
309
310         allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
311                         socket, true);
312
313         /* make sure we've allocated our pages... */
314         if (allocd_pages < 0)
315                 return NULL;
316
317         map_addr = ms[0]->addr;
318         msl = rte_mem_virt2memseg_list(map_addr);
319
320         /* check if we wanted contiguous memory but didn't get it */
321         if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
322                 RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
323                                 __func__);
324                 goto fail;
325         }
326
327         /*
328          * Once we have all the memseg lists configured, if there is a dma mask
329          * set, check iova addresses are not out of range. Otherwise the device
330          * setting the dma mask could have problems with the mapped memory.
331          *
332          * There are two situations when this can happen:
333          *      1) memory initialization
334          *      2) dynamic memory allocation
335          *
336          * For 1), an error when checking dma mask implies app can not be
337          * executed. For 2) implies the new memory can not be added.
338          */
339         if (mcfg->dma_maskbits &&
340             rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
341                 /*
342                  * Currently this can only happen if IOMMU is enabled
343                  * and the address width supported by the IOMMU hw is
344                  * not enough for using the memory mapped IOVAs.
345                  *
346                  * If IOVA is VA, advice to try with '--iova-mode pa'
347                  * which could solve some situations when IOVA VA is not
348                  * really needed.
349                  */
350                 RTE_LOG(ERR, EAL,
351                         "%s(): couldn't allocate memory due to IOVA exceeding limits of current DMA mask\n",
352                         __func__);
353
354                 /*
355                  * If IOVA is VA and it is possible to run with IOVA PA,
356                  * because user is root, give and advice for solving the
357                  * problem.
358                  */
359                 if ((rte_eal_iova_mode() == RTE_IOVA_VA) &&
360                      rte_eal_using_phys_addrs())
361                         RTE_LOG(ERR, EAL,
362                                 "%s(): Please try initializing EAL with --iova-mode=pa parameter\n",
363                                 __func__);
364                 goto fail;
365         }
366
367         /* add newly minted memsegs to malloc heap */
368         elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
369
370         /* try once more, as now we have allocated new memory */
371         ret = find_suitable_element(heap, elt_size, flags, align, bound,
372                         contig);
373
374         if (ret == NULL)
375                 goto fail;
376
377         return elem;
378
379 fail:
380         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
381         return NULL;
382 }
383
384 static int
385 try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
386                 size_t elt_size, int socket, unsigned int flags, size_t align,
387                 size_t bound, bool contig)
388 {
389         struct malloc_elem *elem;
390         struct rte_memseg **ms;
391         void *map_addr;
392         size_t alloc_sz;
393         int n_segs;
394         bool callback_triggered = false;
395
396         alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
397                         MALLOC_ELEM_TRAILER_LEN, pg_sz);
398         n_segs = alloc_sz / pg_sz;
399
400         /* we can't know in advance how many pages we'll need, so we malloc */
401         ms = malloc(sizeof(*ms) * n_segs);
402         if (ms == NULL)
403                 return -1;
404         memset(ms, 0, sizeof(*ms) * n_segs);
405
406         elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
407                         bound, contig, ms, n_segs);
408
409         if (elem == NULL)
410                 goto free_ms;
411
412         map_addr = ms[0]->addr;
413
414         /* notify user about changes in memory map */
415         eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
416
417         /* notify other processes that this has happened */
418         if (request_sync()) {
419                 /* we couldn't ensure all processes have mapped memory,
420                  * so free it back and notify everyone that it's been
421                  * freed back.
422                  *
423                  * technically, we could've avoided adding memory addresses to
424                  * the map, but that would've led to inconsistent behavior
425                  * between primary and secondary processes, as those get
426                  * callbacks during sync. therefore, force primary process to
427                  * do alloc-and-rollback syncs as well.
428                  */
429                 callback_triggered = true;
430                 goto free_elem;
431         }
432         heap->total_size += alloc_sz;
433
434         RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
435                 socket, alloc_sz >> 20ULL);
436
437         free(ms);
438
439         return 0;
440
441 free_elem:
442         if (callback_triggered)
443                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
444                                 map_addr, alloc_sz);
445
446         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
447
448         request_sync();
449 free_ms:
450         free(ms);
451
452         return -1;
453 }
454
455 static int
456 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
457                 size_t elt_size, int socket, unsigned int flags, size_t align,
458                 size_t bound, bool contig)
459 {
460         struct malloc_mp_req req;
461         int req_result;
462
463         memset(&req, 0, sizeof(req));
464
465         req.t = REQ_TYPE_ALLOC;
466         req.alloc_req.align = align;
467         req.alloc_req.bound = bound;
468         req.alloc_req.contig = contig;
469         req.alloc_req.flags = flags;
470         req.alloc_req.elt_size = elt_size;
471         req.alloc_req.page_sz = pg_sz;
472         req.alloc_req.socket = socket;
473         req.alloc_req.heap = heap; /* it's in shared memory */
474
475         req_result = request_to_primary(&req);
476
477         if (req_result != 0)
478                 return -1;
479
480         if (req.result != REQ_RESULT_SUCCESS)
481                 return -1;
482
483         return 0;
484 }
485
486 static int
487 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
488                 int socket, unsigned int flags, size_t align, size_t bound,
489                 bool contig)
490 {
491         int ret;
492
493         rte_mcfg_mem_write_lock();
494
495         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
496                 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
497                                 flags, align, bound, contig);
498         } else {
499                 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
500                                 flags, align, bound, contig);
501         }
502
503         rte_mcfg_mem_write_unlock();
504         return ret;
505 }
506
507 static int
508 compare_pagesz(const void *a, const void *b)
509 {
510         const struct rte_memseg_list * const*mpa = a;
511         const struct rte_memseg_list * const*mpb = b;
512         const struct rte_memseg_list *msla = *mpa;
513         const struct rte_memseg_list *mslb = *mpb;
514         uint64_t pg_sz_a = msla->page_sz;
515         uint64_t pg_sz_b = mslb->page_sz;
516
517         if (pg_sz_a < pg_sz_b)
518                 return -1;
519         if (pg_sz_a > pg_sz_b)
520                 return 1;
521         return 0;
522 }
523
524 static int
525 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
526                 unsigned int flags, size_t align, size_t bound, bool contig)
527 {
528         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
529         struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
530         struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
531         uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
532         uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
533         uint64_t prev_pg_sz;
534         int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
535         bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
536         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
537         void *ret;
538
539         memset(requested_msls, 0, sizeof(requested_msls));
540         memset(other_msls, 0, sizeof(other_msls));
541         memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
542         memset(other_pg_sz, 0, sizeof(other_pg_sz));
543
544         /*
545          * go through memseg list and take note of all the page sizes available,
546          * and if any of them were specifically requested by the user.
547          */
548         n_requested_msls = 0;
549         n_other_msls = 0;
550         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
551                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
552
553                 if (msl->socket_id != socket)
554                         continue;
555
556                 if (msl->base_va == NULL)
557                         continue;
558
559                 /* if pages of specific size were requested */
560                 if (size_flags != 0 && check_hugepage_sz(size_flags,
561                                 msl->page_sz))
562                         requested_msls[n_requested_msls++] = msl;
563                 else if (size_flags == 0 || size_hint)
564                         other_msls[n_other_msls++] = msl;
565         }
566
567         /* sort the lists, smallest first */
568         qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
569                         compare_pagesz);
570         qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
571                         compare_pagesz);
572
573         /* now, extract page sizes we are supposed to try */
574         prev_pg_sz = 0;
575         n_requested_pg_sz = 0;
576         for (i = 0; i < n_requested_msls; i++) {
577                 uint64_t pg_sz = requested_msls[i]->page_sz;
578
579                 if (prev_pg_sz != pg_sz) {
580                         requested_pg_sz[n_requested_pg_sz++] = pg_sz;
581                         prev_pg_sz = pg_sz;
582                 }
583         }
584         prev_pg_sz = 0;
585         n_other_pg_sz = 0;
586         for (i = 0; i < n_other_msls; i++) {
587                 uint64_t pg_sz = other_msls[i]->page_sz;
588
589                 if (prev_pg_sz != pg_sz) {
590                         other_pg_sz[n_other_pg_sz++] = pg_sz;
591                         prev_pg_sz = pg_sz;
592                 }
593         }
594
595         /* finally, try allocating memory of specified page sizes, starting from
596          * the smallest sizes
597          */
598         for (i = 0; i < n_requested_pg_sz; i++) {
599                 uint64_t pg_sz = requested_pg_sz[i];
600
601                 /*
602                  * do not pass the size hint here, as user expects other page
603                  * sizes first, before resorting to best effort allocation.
604                  */
605                 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
606                                 align, bound, contig))
607                         return 0;
608         }
609         if (n_other_pg_sz == 0)
610                 return -1;
611
612         /* now, check if we can reserve anything with size hint */
613         ret = find_suitable_element(heap, size, flags, align, bound, contig);
614         if (ret != NULL)
615                 return 0;
616
617         /*
618          * we still couldn't reserve memory, so try expanding heap with other
619          * page sizes, if there are any
620          */
621         for (i = 0; i < n_other_pg_sz; i++) {
622                 uint64_t pg_sz = other_pg_sz[i];
623
624                 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
625                                 align, bound, contig))
626                         return 0;
627         }
628         return -1;
629 }
630
631 /* this will try lower page sizes first */
632 static void *
633 malloc_heap_alloc_on_heap_id(const char *type, size_t size,
634                 unsigned int heap_id, unsigned int flags, size_t align,
635                 size_t bound, bool contig)
636 {
637         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
638         struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
639         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
640         int socket_id;
641         void *ret;
642
643         rte_spinlock_lock(&(heap->lock));
644
645         align = align == 0 ? 1 : align;
646
647         /* for legacy mode, try once and with all flags */
648         if (internal_config.legacy_mem) {
649                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
650                 goto alloc_unlock;
651         }
652
653         /*
654          * we do not pass the size hint here, because even if allocation fails,
655          * we may still be able to allocate memory from appropriate page sizes,
656          * we just need to request more memory first.
657          */
658
659         socket_id = rte_socket_id_by_idx(heap_id);
660         /*
661          * if socket ID is negative, we cannot find a socket ID for this heap -
662          * which means it's an external heap. those can have unexpected page
663          * sizes, so if the user asked to allocate from there - assume user
664          * knows what they're doing, and allow allocating from there with any
665          * page size flags.
666          */
667         if (socket_id < 0)
668                 size_flags |= RTE_MEMZONE_SIZE_HINT_ONLY;
669
670         ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
671         if (ret != NULL)
672                 goto alloc_unlock;
673
674         /* if socket ID is invalid, this is an external heap */
675         if (socket_id < 0)
676                 goto alloc_unlock;
677
678         if (!alloc_more_mem_on_socket(heap, size, socket_id, flags, align,
679                         bound, contig)) {
680                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
681
682                 /* this should have succeeded */
683                 if (ret == NULL)
684                         RTE_LOG(ERR, EAL, "Error allocating from heap\n");
685         }
686 alloc_unlock:
687         rte_spinlock_unlock(&(heap->lock));
688         return ret;
689 }
690
691 void *
692 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
693                 unsigned int flags, size_t align, size_t bound, bool contig)
694 {
695         int socket, heap_id, i;
696         void *ret;
697
698         /* return NULL if size is 0 or alignment is not power-of-2 */
699         if (size == 0 || (align && !rte_is_power_of_2(align)))
700                 return NULL;
701
702         if (!rte_eal_has_hugepages() && socket_arg < RTE_MAX_NUMA_NODES)
703                 socket_arg = SOCKET_ID_ANY;
704
705         if (socket_arg == SOCKET_ID_ANY)
706                 socket = malloc_get_numa_socket();
707         else
708                 socket = socket_arg;
709
710         /* turn socket ID into heap ID */
711         heap_id = malloc_socket_to_heap_id(socket);
712         /* if heap id is negative, socket ID was invalid */
713         if (heap_id < 0)
714                 return NULL;
715
716         ret = malloc_heap_alloc_on_heap_id(type, size, heap_id, flags, align,
717                         bound, contig);
718         if (ret != NULL || socket_arg != SOCKET_ID_ANY)
719                 return ret;
720
721         /* try other heaps. we are only iterating through native DPDK sockets,
722          * so external heaps won't be included.
723          */
724         for (i = 0; i < (int) rte_socket_count(); i++) {
725                 if (i == heap_id)
726                         continue;
727                 ret = malloc_heap_alloc_on_heap_id(type, size, i, flags, align,
728                                 bound, contig);
729                 if (ret != NULL)
730                         return ret;
731         }
732         return NULL;
733 }
734
735 static void *
736 heap_alloc_biggest_on_heap_id(const char *type, unsigned int heap_id,
737                 unsigned int flags, size_t align, bool contig)
738 {
739         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
740         struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
741         void *ret;
742
743         rte_spinlock_lock(&(heap->lock));
744
745         align = align == 0 ? 1 : align;
746
747         ret = heap_alloc_biggest(heap, type, flags, align, contig);
748
749         rte_spinlock_unlock(&(heap->lock));
750
751         return ret;
752 }
753
754 void *
755 malloc_heap_alloc_biggest(const char *type, int socket_arg, unsigned int flags,
756                 size_t align, bool contig)
757 {
758         int socket, i, cur_socket, heap_id;
759         void *ret;
760
761         /* return NULL if align is not power-of-2 */
762         if ((align && !rte_is_power_of_2(align)))
763                 return NULL;
764
765         if (!rte_eal_has_hugepages())
766                 socket_arg = SOCKET_ID_ANY;
767
768         if (socket_arg == SOCKET_ID_ANY)
769                 socket = malloc_get_numa_socket();
770         else
771                 socket = socket_arg;
772
773         /* turn socket ID into heap ID */
774         heap_id = malloc_socket_to_heap_id(socket);
775         /* if heap id is negative, socket ID was invalid */
776         if (heap_id < 0)
777                 return NULL;
778
779         ret = heap_alloc_biggest_on_heap_id(type, heap_id, flags, align,
780                         contig);
781         if (ret != NULL || socket_arg != SOCKET_ID_ANY)
782                 return ret;
783
784         /* try other heaps */
785         for (i = 0; i < (int) rte_socket_count(); i++) {
786                 cur_socket = rte_socket_id_by_idx(i);
787                 if (cur_socket == socket)
788                         continue;
789                 ret = heap_alloc_biggest_on_heap_id(type, i, flags, align,
790                                 contig);
791                 if (ret != NULL)
792                         return ret;
793         }
794         return NULL;
795 }
796
797 /* this function is exposed in malloc_mp.h */
798 int
799 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
800 {
801         int n_segs, seg_idx, max_seg_idx;
802         struct rte_memseg_list *msl;
803         size_t page_sz;
804
805         msl = rte_mem_virt2memseg_list(aligned_start);
806         if (msl == NULL)
807                 return -1;
808
809         page_sz = (size_t)msl->page_sz;
810         n_segs = aligned_len / page_sz;
811         seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
812         max_seg_idx = seg_idx + n_segs;
813
814         for (; seg_idx < max_seg_idx; seg_idx++) {
815                 struct rte_memseg *ms;
816
817                 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
818                 eal_memalloc_free_seg(ms);
819         }
820         return 0;
821 }
822
823 int
824 malloc_heap_free(struct malloc_elem *elem)
825 {
826         struct malloc_heap *heap;
827         void *start, *aligned_start, *end, *aligned_end;
828         size_t len, aligned_len, page_sz;
829         struct rte_memseg_list *msl;
830         unsigned int i, n_segs, before_space, after_space;
831         int ret;
832
833         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
834                 return -1;
835
836         /* elem may be merged with previous element, so keep heap address */
837         heap = elem->heap;
838         msl = elem->msl;
839         page_sz = (size_t)msl->page_sz;
840
841         rte_spinlock_lock(&(heap->lock));
842
843         /* mark element as free */
844         elem->state = ELEM_FREE;
845
846         elem = malloc_elem_free(elem);
847
848         /* anything after this is a bonus */
849         ret = 0;
850
851         /* ...of which we can't avail if we are in legacy mode, or if this is an
852          * externally allocated segment.
853          */
854         if (internal_config.legacy_mem || (msl->external > 0))
855                 goto free_unlock;
856
857         /* check if we can free any memory back to the system */
858         if (elem->size < page_sz)
859                 goto free_unlock;
860
861         /* if user requested to match allocations, the sizes must match - if not,
862          * we will defer freeing these hugepages until the entire original allocation
863          * can be freed
864          */
865         if (internal_config.match_allocations && elem->size != elem->orig_size)
866                 goto free_unlock;
867
868         /* probably, but let's make sure, as we may not be using up full page */
869         start = elem;
870         len = elem->size;
871         aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
872         end = RTE_PTR_ADD(elem, len);
873         aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
874
875         aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
876
877         /* can't free anything */
878         if (aligned_len < page_sz)
879                 goto free_unlock;
880
881         /* we can free something. however, some of these pages may be marked as
882          * unfreeable, so also check that as well
883          */
884         n_segs = aligned_len / page_sz;
885         for (i = 0; i < n_segs; i++) {
886                 const struct rte_memseg *tmp =
887                                 rte_mem_virt2memseg(aligned_start, msl);
888
889                 if (tmp->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
890                         /* this is an unfreeable segment, so move start */
891                         aligned_start = RTE_PTR_ADD(tmp->addr, tmp->len);
892                 }
893         }
894
895         /* recalculate length and number of segments */
896         aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
897         n_segs = aligned_len / page_sz;
898
899         /* check if we can still free some pages */
900         if (n_segs == 0)
901                 goto free_unlock;
902
903         /* We're not done yet. We also have to check if by freeing space we will
904          * be leaving free elements that are too small to store new elements.
905          * Check if we have enough space in the beginning and at the end, or if
906          * start/end are exactly page aligned.
907          */
908         before_space = RTE_PTR_DIFF(aligned_start, elem);
909         after_space = RTE_PTR_DIFF(end, aligned_end);
910         if (before_space != 0 &&
911                         before_space < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
912                 /* There is not enough space before start, but we may be able to
913                  * move the start forward by one page.
914                  */
915                 if (n_segs == 1)
916                         goto free_unlock;
917
918                 /* move start */
919                 aligned_start = RTE_PTR_ADD(aligned_start, page_sz);
920                 aligned_len -= page_sz;
921                 n_segs--;
922         }
923         if (after_space != 0 && after_space <
924                         MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
925                 /* There is not enough space after end, but we may be able to
926                  * move the end backwards by one page.
927                  */
928                 if (n_segs == 1)
929                         goto free_unlock;
930
931                 /* move end */
932                 aligned_end = RTE_PTR_SUB(aligned_end, page_sz);
933                 aligned_len -= page_sz;
934                 n_segs--;
935         }
936
937         /* now we can finally free us some pages */
938
939         rte_mcfg_mem_write_lock();
940
941         /*
942          * we allow secondary processes to clear the heap of this allocated
943          * memory because it is safe to do so, as even if notifications about
944          * unmapped pages don't make it to other processes, heap is shared
945          * across all processes, and will become empty of this memory anyway,
946          * and nothing can allocate it back unless primary process will be able
947          * to deliver allocation message to every single running process.
948          */
949
950         malloc_elem_free_list_remove(elem);
951
952         malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
953
954         heap->total_size -= aligned_len;
955
956         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
957                 /* notify user about changes in memory map */
958                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
959                                 aligned_start, aligned_len);
960
961                 /* don't care if any of this fails */
962                 malloc_heap_free_pages(aligned_start, aligned_len);
963
964                 request_sync();
965         } else {
966                 struct malloc_mp_req req;
967
968                 memset(&req, 0, sizeof(req));
969
970                 req.t = REQ_TYPE_FREE;
971                 req.free_req.addr = aligned_start;
972                 req.free_req.len = aligned_len;
973
974                 /*
975                  * we request primary to deallocate pages, but we don't do it
976                  * in this thread. instead, we notify primary that we would like
977                  * to deallocate pages, and this process will receive another
978                  * request (in parallel) that will do it for us on another
979                  * thread.
980                  *
981                  * we also don't really care if this succeeds - the data is
982                  * already removed from the heap, so it is, for all intents and
983                  * purposes, hidden from the rest of DPDK even if some other
984                  * process (including this one) may have these pages mapped.
985                  *
986                  * notifications about deallocated memory happen during sync.
987                  */
988                 request_to_primary(&req);
989         }
990
991         RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
992                 msl->socket_id, aligned_len >> 20ULL);
993
994         rte_mcfg_mem_write_unlock();
995 free_unlock:
996         rte_spinlock_unlock(&(heap->lock));
997         return ret;
998 }
999
1000 int
1001 malloc_heap_resize(struct malloc_elem *elem, size_t size)
1002 {
1003         int ret;
1004
1005         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
1006                 return -1;
1007
1008         rte_spinlock_lock(&(elem->heap->lock));
1009
1010         ret = malloc_elem_resize(elem, size);
1011
1012         rte_spinlock_unlock(&(elem->heap->lock));
1013
1014         return ret;
1015 }
1016
1017 /*
1018  * Function to retrieve data for a given heap
1019  */
1020 int
1021 malloc_heap_get_stats(struct malloc_heap *heap,
1022                 struct rte_malloc_socket_stats *socket_stats)
1023 {
1024         size_t idx;
1025         struct malloc_elem *elem;
1026
1027         rte_spinlock_lock(&heap->lock);
1028
1029         /* Initialise variables for heap */
1030         socket_stats->free_count = 0;
1031         socket_stats->heap_freesz_bytes = 0;
1032         socket_stats->greatest_free_size = 0;
1033
1034         /* Iterate through free list */
1035         for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
1036                 for (elem = LIST_FIRST(&heap->free_head[idx]);
1037                         !!elem; elem = LIST_NEXT(elem, free_list))
1038                 {
1039                         socket_stats->free_count++;
1040                         socket_stats->heap_freesz_bytes += elem->size;
1041                         if (elem->size > socket_stats->greatest_free_size)
1042                                 socket_stats->greatest_free_size = elem->size;
1043                 }
1044         }
1045         /* Get stats on overall heap and allocated memory on this heap */
1046         socket_stats->heap_totalsz_bytes = heap->total_size;
1047         socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
1048                         socket_stats->heap_freesz_bytes);
1049         socket_stats->alloc_count = heap->alloc_count;
1050
1051         rte_spinlock_unlock(&heap->lock);
1052         return 0;
1053 }
1054
1055 /*
1056  * Function to retrieve data for a given heap
1057  */
1058 void
1059 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
1060 {
1061         struct malloc_elem *elem;
1062
1063         rte_spinlock_lock(&heap->lock);
1064
1065         fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
1066         fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
1067
1068         elem = heap->first;
1069         while (elem) {
1070                 malloc_elem_dump(elem, f);
1071                 elem = elem->next;
1072         }
1073
1074         rte_spinlock_unlock(&heap->lock);
1075 }
1076
1077 static int
1078 destroy_elem(struct malloc_elem *elem, size_t len)
1079 {
1080         struct malloc_heap *heap = elem->heap;
1081
1082         /* notify all subscribers that a memory area is going to be removed */
1083         eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE, elem, len);
1084
1085         /* this element can be removed */
1086         malloc_elem_free_list_remove(elem);
1087         malloc_elem_hide_region(elem, elem, len);
1088
1089         heap->total_size -= len;
1090
1091         memset(elem, 0, sizeof(*elem));
1092
1093         return 0;
1094 }
1095
1096 struct rte_memseg_list *
1097 malloc_heap_create_external_seg(void *va_addr, rte_iova_t iova_addrs[],
1098                 unsigned int n_pages, size_t page_sz, const char *seg_name,
1099                 unsigned int socket_id)
1100 {
1101         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1102         char fbarray_name[RTE_FBARRAY_NAME_LEN];
1103         struct rte_memseg_list *msl = NULL;
1104         struct rte_fbarray *arr;
1105         size_t seg_len = n_pages * page_sz;
1106         unsigned int i;
1107
1108         /* first, find a free memseg list */
1109         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
1110                 struct rte_memseg_list *tmp = &mcfg->memsegs[i];
1111                 if (tmp->base_va == NULL) {
1112                         msl = tmp;
1113                         break;
1114                 }
1115         }
1116         if (msl == NULL) {
1117                 RTE_LOG(ERR, EAL, "Couldn't find empty memseg list\n");
1118                 rte_errno = ENOSPC;
1119                 return NULL;
1120         }
1121
1122         snprintf(fbarray_name, sizeof(fbarray_name), "%s_%p",
1123                         seg_name, va_addr);
1124
1125         /* create the backing fbarray */
1126         if (rte_fbarray_init(&msl->memseg_arr, fbarray_name, n_pages,
1127                         sizeof(struct rte_memseg)) < 0) {
1128                 RTE_LOG(ERR, EAL, "Couldn't create fbarray backing the memseg list\n");
1129                 return NULL;
1130         }
1131         arr = &msl->memseg_arr;
1132
1133         /* fbarray created, fill it up */
1134         for (i = 0; i < n_pages; i++) {
1135                 struct rte_memseg *ms;
1136
1137                 rte_fbarray_set_used(arr, i);
1138                 ms = rte_fbarray_get(arr, i);
1139                 ms->addr = RTE_PTR_ADD(va_addr, i * page_sz);
1140                 ms->iova = iova_addrs == NULL ? RTE_BAD_IOVA : iova_addrs[i];
1141                 ms->hugepage_sz = page_sz;
1142                 ms->len = page_sz;
1143                 ms->nchannel = rte_memory_get_nchannel();
1144                 ms->nrank = rte_memory_get_nrank();
1145                 ms->socket_id = socket_id;
1146         }
1147
1148         /* set up the memseg list */
1149         msl->base_va = va_addr;
1150         msl->page_sz = page_sz;
1151         msl->socket_id = socket_id;
1152         msl->len = seg_len;
1153         msl->version = 0;
1154         msl->external = 1;
1155
1156         return msl;
1157 }
1158
1159 struct extseg_walk_arg {
1160         void *va_addr;
1161         size_t len;
1162         struct rte_memseg_list *msl;
1163 };
1164
1165 static int
1166 extseg_walk(const struct rte_memseg_list *msl, void *arg)
1167 {
1168         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1169         struct extseg_walk_arg *wa = arg;
1170
1171         if (msl->base_va == wa->va_addr && msl->len == wa->len) {
1172                 unsigned int found_idx;
1173
1174                 /* msl is const */
1175                 found_idx = msl - mcfg->memsegs;
1176                 wa->msl = &mcfg->memsegs[found_idx];
1177                 return 1;
1178         }
1179         return 0;
1180 }
1181
1182 struct rte_memseg_list *
1183 malloc_heap_find_external_seg(void *va_addr, size_t len)
1184 {
1185         struct extseg_walk_arg wa;
1186         int res;
1187
1188         wa.va_addr = va_addr;
1189         wa.len = len;
1190
1191         res = rte_memseg_list_walk_thread_unsafe(extseg_walk, &wa);
1192
1193         if (res != 1) {
1194                 /* 0 means nothing was found, -1 shouldn't happen */
1195                 if (res == 0)
1196                         rte_errno = ENOENT;
1197                 return NULL;
1198         }
1199         return wa.msl;
1200 }
1201
1202 int
1203 malloc_heap_destroy_external_seg(struct rte_memseg_list *msl)
1204 {
1205         /* destroy the fbarray backing this memory */
1206         if (rte_fbarray_destroy(&msl->memseg_arr) < 0)
1207                 return -1;
1208
1209         /* reset the memseg list */
1210         memset(msl, 0, sizeof(*msl));
1211
1212         return 0;
1213 }
1214
1215 int
1216 malloc_heap_add_external_memory(struct malloc_heap *heap,
1217                 struct rte_memseg_list *msl)
1218 {
1219         /* erase contents of new memory */
1220         memset(msl->base_va, 0, msl->len);
1221
1222         /* now, add newly minted memory to the malloc heap */
1223         malloc_heap_add_memory(heap, msl, msl->base_va, msl->len);
1224
1225         heap->total_size += msl->len;
1226
1227         /* all done! */
1228         RTE_LOG(DEBUG, EAL, "Added segment for heap %s starting at %p\n",
1229                         heap->name, msl->base_va);
1230
1231         /* notify all subscribers that a new memory area has been added */
1232         eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
1233                         msl->base_va, msl->len);
1234
1235         return 0;
1236 }
1237
1238 int
1239 malloc_heap_remove_external_memory(struct malloc_heap *heap, void *va_addr,
1240                 size_t len)
1241 {
1242         struct malloc_elem *elem = heap->first;
1243
1244         /* find element with specified va address */
1245         while (elem != NULL && elem != va_addr) {
1246                 elem = elem->next;
1247                 /* stop if we've blown past our VA */
1248                 if (elem > (struct malloc_elem *)va_addr) {
1249                         rte_errno = ENOENT;
1250                         return -1;
1251                 }
1252         }
1253         /* check if element was found */
1254         if (elem == NULL || elem->msl->len != len) {
1255                 rte_errno = ENOENT;
1256                 return -1;
1257         }
1258         /* if element's size is not equal to segment len, segment is busy */
1259         if (elem->state == ELEM_BUSY || elem->size != len) {
1260                 rte_errno = EBUSY;
1261                 return -1;
1262         }
1263         return destroy_elem(elem, len);
1264 }
1265
1266 int
1267 malloc_heap_create(struct malloc_heap *heap, const char *heap_name)
1268 {
1269         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1270         uint32_t next_socket_id = mcfg->next_socket_id;
1271
1272         /* prevent overflow. did you really create 2 billion heaps??? */
1273         if (next_socket_id > INT32_MAX) {
1274                 RTE_LOG(ERR, EAL, "Cannot assign new socket ID's\n");
1275                 rte_errno = ENOSPC;
1276                 return -1;
1277         }
1278
1279         /* initialize empty heap */
1280         heap->alloc_count = 0;
1281         heap->first = NULL;
1282         heap->last = NULL;
1283         LIST_INIT(heap->free_head);
1284         rte_spinlock_init(&heap->lock);
1285         heap->total_size = 0;
1286         heap->socket_id = next_socket_id;
1287
1288         /* we hold a global mem hotplug writelock, so it's safe to increment */
1289         mcfg->next_socket_id++;
1290
1291         /* set up name */
1292         strlcpy(heap->name, heap_name, RTE_HEAP_NAME_MAX_LEN);
1293         return 0;
1294 }
1295
1296 int
1297 malloc_heap_destroy(struct malloc_heap *heap)
1298 {
1299         if (heap->alloc_count != 0) {
1300                 RTE_LOG(ERR, EAL, "Heap is still in use\n");
1301                 rte_errno = EBUSY;
1302                 return -1;
1303         }
1304         if (heap->first != NULL || heap->last != NULL) {
1305                 RTE_LOG(ERR, EAL, "Heap still contains memory segments\n");
1306                 rte_errno = EBUSY;
1307                 return -1;
1308         }
1309         if (heap->total_size != 0)
1310                 RTE_LOG(ERR, EAL, "Total size not zero, heap is likely corrupt\n");
1311
1312         /* after this, the lock will be dropped */
1313         memset(heap, 0, sizeof(*heap));
1314
1315         return 0;
1316 }
1317
1318 int
1319 rte_eal_malloc_heap_init(void)
1320 {
1321         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1322         unsigned int i;
1323
1324         if (internal_config.match_allocations) {
1325                 RTE_LOG(DEBUG, EAL, "Hugepages will be freed exactly as allocated.\n");
1326         }
1327
1328         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1329                 /* assign min socket ID to external heaps */
1330                 mcfg->next_socket_id = EXTERNAL_HEAP_MIN_SOCKET_ID;
1331
1332                 /* assign names to default DPDK heaps */
1333                 for (i = 0; i < rte_socket_count(); i++) {
1334                         struct malloc_heap *heap = &mcfg->malloc_heaps[i];
1335                         char heap_name[RTE_HEAP_NAME_MAX_LEN];
1336                         int socket_id = rte_socket_id_by_idx(i);
1337
1338                         snprintf(heap_name, sizeof(heap_name),
1339                                         "socket_%i", socket_id);
1340                         strlcpy(heap->name, heap_name, RTE_HEAP_NAME_MAX_LEN);
1341                         heap->socket_id = socket_id;
1342                 }
1343         }
1344
1345
1346         if (register_mp_requests()) {
1347                 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
1348                 rte_mcfg_mem_read_unlock();
1349                 return -1;
1350         }
1351
1352         /* unlock mem hotplug here. it's safe for primary as no requests can
1353          * even come before primary itself is fully initialized, and secondaries
1354          * do not need to initialize the heap.
1355          */
1356         rte_mcfg_mem_read_unlock();
1357
1358         /* secondary process does not need to initialize anything */
1359         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1360                 return 0;
1361
1362         /* add all IOVA-contiguous areas to the heap */
1363         return rte_memseg_contig_walk(malloc_add_seg, NULL);
1364 }