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