malloc: enable callbacks on alloc/free and mp sync
[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_atomic.h>
24 #include <rte_fbarray.h>
25
26 #include "eal_internal_cfg.h"
27 #include "eal_memalloc.h"
28 #include "malloc_elem.h"
29 #include "malloc_heap.h"
30 #include "malloc_mp.h"
31
32 static unsigned
33 check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
34 {
35         unsigned check_flag = 0;
36
37         if (!(flags & ~RTE_MEMZONE_SIZE_HINT_ONLY))
38                 return 1;
39
40         switch (hugepage_sz) {
41         case RTE_PGSIZE_256K:
42                 check_flag = RTE_MEMZONE_256KB;
43                 break;
44         case RTE_PGSIZE_2M:
45                 check_flag = RTE_MEMZONE_2MB;
46                 break;
47         case RTE_PGSIZE_16M:
48                 check_flag = RTE_MEMZONE_16MB;
49                 break;
50         case RTE_PGSIZE_256M:
51                 check_flag = RTE_MEMZONE_256MB;
52                 break;
53         case RTE_PGSIZE_512M:
54                 check_flag = RTE_MEMZONE_512MB;
55                 break;
56         case RTE_PGSIZE_1G:
57                 check_flag = RTE_MEMZONE_1GB;
58                 break;
59         case RTE_PGSIZE_4G:
60                 check_flag = RTE_MEMZONE_4GB;
61                 break;
62         case RTE_PGSIZE_16G:
63                 check_flag = RTE_MEMZONE_16GB;
64         }
65
66         return check_flag & flags;
67 }
68
69 /*
70  * Expand the heap with a memory area.
71  */
72 static struct malloc_elem *
73 malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
74                 void *start, size_t len)
75 {
76         struct malloc_elem *elem = start;
77
78         malloc_elem_init(elem, heap, msl, len);
79
80         malloc_elem_insert(elem);
81
82         elem = malloc_elem_join_adjacent_free(elem);
83
84         malloc_elem_free_list_insert(elem);
85
86         return elem;
87 }
88
89 static int
90 malloc_add_seg(const struct rte_memseg_list *msl,
91                 const struct rte_memseg *ms, size_t len, void *arg __rte_unused)
92 {
93         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
94         struct rte_memseg_list *found_msl;
95         struct malloc_heap *heap;
96         int msl_idx;
97
98         heap = &mcfg->malloc_heaps[msl->socket_id];
99
100         /* msl is const, so find it */
101         msl_idx = msl - mcfg->memsegs;
102         found_msl = &mcfg->memsegs[msl_idx];
103
104         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
105                 return -1;
106
107         malloc_heap_add_memory(heap, found_msl, ms->addr, len);
108
109         RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
110                         msl->socket_id);
111         return 0;
112 }
113
114 /*
115  * Iterates through the freelist for a heap to find a free element
116  * which can store data of the required size and with the requested alignment.
117  * If size is 0, find the biggest available elem.
118  * Returns null on failure, or pointer to element on success.
119  */
120 static struct malloc_elem *
121 find_suitable_element(struct malloc_heap *heap, size_t size,
122                 unsigned int flags, size_t align, size_t bound, bool contig)
123 {
124         size_t idx;
125         struct malloc_elem *elem, *alt_elem = NULL;
126
127         for (idx = malloc_elem_free_list_index(size);
128                         idx < RTE_HEAP_NUM_FREELISTS; idx++) {
129                 for (elem = LIST_FIRST(&heap->free_head[idx]);
130                                 !!elem; elem = LIST_NEXT(elem, free_list)) {
131                         if (malloc_elem_can_hold(elem, size, align, bound,
132                                         contig)) {
133                                 if (check_hugepage_sz(flags,
134                                                 elem->msl->page_sz))
135                                         return elem;
136                                 if (alt_elem == NULL)
137                                         alt_elem = elem;
138                         }
139                 }
140         }
141
142         if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
143                 return alt_elem;
144
145         return NULL;
146 }
147
148 /*
149  * Main function to allocate a block of memory from the heap.
150  * It locks the free list, scans it, and adds a new memseg if the
151  * scan fails. Once the new memseg is added, it re-scans and should return
152  * the new element after releasing the lock.
153  */
154 static void *
155 heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
156                 unsigned int flags, size_t align, size_t bound, bool contig)
157 {
158         struct malloc_elem *elem;
159
160         size = RTE_CACHE_LINE_ROUNDUP(size);
161         align = RTE_CACHE_LINE_ROUNDUP(align);
162
163         elem = find_suitable_element(heap, size, flags, align, bound, contig);
164         if (elem != NULL) {
165                 elem = malloc_elem_alloc(elem, size, align, bound, contig);
166
167                 /* increase heap's count of allocated elements */
168                 heap->alloc_count++;
169         }
170
171         return elem == NULL ? NULL : (void *)(&elem[1]);
172 }
173
174 /* this function is exposed in malloc_mp.h */
175 void
176 rollback_expand_heap(struct rte_memseg **ms, int n_segs,
177                 struct malloc_elem *elem, void *map_addr, size_t map_len)
178 {
179         if (elem != NULL) {
180                 malloc_elem_free_list_remove(elem);
181                 malloc_elem_hide_region(elem, map_addr, map_len);
182         }
183
184         eal_memalloc_free_seg_bulk(ms, n_segs);
185 }
186
187 /* this function is exposed in malloc_mp.h */
188 struct malloc_elem *
189 alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
190                 int socket, unsigned int flags, size_t align, size_t bound,
191                 bool contig, struct rte_memseg **ms, int n_segs)
192 {
193         struct rte_memseg_list *msl;
194         struct malloc_elem *elem = NULL;
195         size_t alloc_sz;
196         int allocd_pages;
197         void *ret, *map_addr;
198
199         allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
200                         socket, true);
201
202         /* make sure we've allocated our pages... */
203         if (allocd_pages < 0)
204                 return NULL;
205
206         map_addr = ms[0]->addr;
207         msl = rte_mem_virt2memseg_list(map_addr);
208         alloc_sz = (size_t)msl->page_sz * allocd_pages;
209
210         /* check if we wanted contiguous memory but didn't get it */
211         if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
212                 RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
213                                 __func__);
214                 goto fail;
215         }
216
217         /* add newly minted memsegs to malloc heap */
218         elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
219
220         /* try once more, as now we have allocated new memory */
221         ret = find_suitable_element(heap, elt_size, flags, align, bound,
222                         contig);
223
224         if (ret == NULL)
225                 goto fail;
226
227         return elem;
228
229 fail:
230         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
231         return NULL;
232 }
233
234 static int
235 try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
236                 size_t elt_size, int socket, unsigned int flags, size_t align,
237                 size_t bound, bool contig)
238 {
239         struct malloc_elem *elem;
240         struct rte_memseg **ms;
241         void *map_addr;
242         size_t alloc_sz;
243         int n_segs;
244         bool callback_triggered = false;
245
246         alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
247                         MALLOC_ELEM_TRAILER_LEN, pg_sz);
248         n_segs = alloc_sz / pg_sz;
249
250         /* we can't know in advance how many pages we'll need, so we malloc */
251         ms = malloc(sizeof(*ms) * n_segs);
252
253         memset(ms, 0, sizeof(*ms) * n_segs);
254
255         if (ms == NULL)
256                 return -1;
257
258         elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
259                         bound, contig, ms, n_segs);
260
261         if (elem == NULL)
262                 goto free_ms;
263
264         map_addr = ms[0]->addr;
265
266         /* notify user about changes in memory map */
267         eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
268
269         /* notify other processes that this has happened */
270         if (request_sync()) {
271                 /* we couldn't ensure all processes have mapped memory,
272                  * so free it back and notify everyone that it's been
273                  * freed back.
274                  *
275                  * technically, we could've avoided adding memory addresses to
276                  * the map, but that would've led to inconsistent behavior
277                  * between primary and secondary processes, as those get
278                  * callbacks during sync. therefore, force primary process to
279                  * do alloc-and-rollback syncs as well.
280                  */
281                 callback_triggered = true;
282                 goto free_elem;
283         }
284         heap->total_size += alloc_sz;
285
286         RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
287                 socket, alloc_sz >> 20ULL);
288
289         free(ms);
290
291         return 0;
292
293 free_elem:
294         if (callback_triggered)
295                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
296                                 map_addr, alloc_sz);
297
298         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
299
300         request_sync();
301 free_ms:
302         free(ms);
303
304         return -1;
305 }
306
307 static int
308 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
309                 size_t elt_size, int socket, unsigned int flags, size_t align,
310                 size_t bound, bool contig)
311 {
312         struct malloc_mp_req req;
313         int req_result;
314
315         memset(&req, 0, sizeof(req));
316
317         req.t = REQ_TYPE_ALLOC;
318         req.alloc_req.align = align;
319         req.alloc_req.bound = bound;
320         req.alloc_req.contig = contig;
321         req.alloc_req.flags = flags;
322         req.alloc_req.elt_size = elt_size;
323         req.alloc_req.page_sz = pg_sz;
324         req.alloc_req.socket = socket;
325         req.alloc_req.heap = heap; /* it's in shared memory */
326
327         req_result = request_to_primary(&req);
328
329         if (req_result != 0)
330                 return -1;
331
332         if (req.result != REQ_RESULT_SUCCESS)
333                 return -1;
334
335         return 0;
336 }
337
338 static int
339 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
340                 int socket, unsigned int flags, size_t align, size_t bound,
341                 bool contig)
342 {
343         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
344         int ret;
345
346         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
347
348         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
349                 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
350                                 flags, align, bound, contig);
351         } else {
352                 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
353                                 flags, align, bound, contig);
354         }
355
356         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
357         return ret;
358 }
359
360 static int
361 compare_pagesz(const void *a, const void *b)
362 {
363         const struct rte_memseg_list * const*mpa = a;
364         const struct rte_memseg_list * const*mpb = b;
365         const struct rte_memseg_list *msla = *mpa;
366         const struct rte_memseg_list *mslb = *mpb;
367         uint64_t pg_sz_a = msla->page_sz;
368         uint64_t pg_sz_b = mslb->page_sz;
369
370         if (pg_sz_a < pg_sz_b)
371                 return -1;
372         if (pg_sz_a > pg_sz_b)
373                 return 1;
374         return 0;
375 }
376
377 static int
378 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
379                 unsigned int flags, size_t align, size_t bound, bool contig)
380 {
381         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
382         struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
383         struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
384         uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
385         uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
386         uint64_t prev_pg_sz;
387         int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
388         bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
389         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
390         void *ret;
391
392         memset(requested_msls, 0, sizeof(requested_msls));
393         memset(other_msls, 0, sizeof(other_msls));
394         memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
395         memset(other_pg_sz, 0, sizeof(other_pg_sz));
396
397         /*
398          * go through memseg list and take note of all the page sizes available,
399          * and if any of them were specifically requested by the user.
400          */
401         n_requested_msls = 0;
402         n_other_msls = 0;
403         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
404                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
405
406                 if (msl->socket_id != socket)
407                         continue;
408
409                 if (msl->base_va == NULL)
410                         continue;
411
412                 /* if pages of specific size were requested */
413                 if (size_flags != 0 && check_hugepage_sz(size_flags,
414                                 msl->page_sz))
415                         requested_msls[n_requested_msls++] = msl;
416                 else if (size_flags == 0 || size_hint)
417                         other_msls[n_other_msls++] = msl;
418         }
419
420         /* sort the lists, smallest first */
421         qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
422                         compare_pagesz);
423         qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
424                         compare_pagesz);
425
426         /* now, extract page sizes we are supposed to try */
427         prev_pg_sz = 0;
428         n_requested_pg_sz = 0;
429         for (i = 0; i < n_requested_msls; i++) {
430                 uint64_t pg_sz = requested_msls[i]->page_sz;
431
432                 if (prev_pg_sz != pg_sz) {
433                         requested_pg_sz[n_requested_pg_sz++] = pg_sz;
434                         prev_pg_sz = pg_sz;
435                 }
436         }
437         prev_pg_sz = 0;
438         n_other_pg_sz = 0;
439         for (i = 0; i < n_other_msls; i++) {
440                 uint64_t pg_sz = other_msls[i]->page_sz;
441
442                 if (prev_pg_sz != pg_sz) {
443                         other_pg_sz[n_other_pg_sz++] = pg_sz;
444                         prev_pg_sz = pg_sz;
445                 }
446         }
447
448         /* finally, try allocating memory of specified page sizes, starting from
449          * the smallest sizes
450          */
451         for (i = 0; i < n_requested_pg_sz; i++) {
452                 uint64_t pg_sz = requested_pg_sz[i];
453
454                 /*
455                  * do not pass the size hint here, as user expects other page
456                  * sizes first, before resorting to best effort allocation.
457                  */
458                 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
459                                 align, bound, contig))
460                         return 0;
461         }
462         if (n_other_pg_sz == 0)
463                 return -1;
464
465         /* now, check if we can reserve anything with size hint */
466         ret = find_suitable_element(heap, size, flags, align, bound, contig);
467         if (ret != NULL)
468                 return 0;
469
470         /*
471          * we still couldn't reserve memory, so try expanding heap with other
472          * page sizes, if there are any
473          */
474         for (i = 0; i < n_other_pg_sz; i++) {
475                 uint64_t pg_sz = other_pg_sz[i];
476
477                 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
478                                 align, bound, contig))
479                         return 0;
480         }
481         return -1;
482 }
483
484 /* this will try lower page sizes first */
485 static void *
486 heap_alloc_on_socket(const char *type, size_t size, int socket,
487                 unsigned int flags, size_t align, size_t bound, bool contig)
488 {
489         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
490         struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
491         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
492         void *ret;
493
494         rte_spinlock_lock(&(heap->lock));
495
496         align = align == 0 ? 1 : align;
497
498         /* for legacy mode, try once and with all flags */
499         if (internal_config.legacy_mem) {
500                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
501                 goto alloc_unlock;
502         }
503
504         /*
505          * we do not pass the size hint here, because even if allocation fails,
506          * we may still be able to allocate memory from appropriate page sizes,
507          * we just need to request more memory first.
508          */
509         ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
510         if (ret != NULL)
511                 goto alloc_unlock;
512
513         if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
514                         contig)) {
515                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
516
517                 /* this should have succeeded */
518                 if (ret == NULL)
519                         RTE_LOG(ERR, EAL, "Error allocating from heap\n");
520         }
521 alloc_unlock:
522         rte_spinlock_unlock(&(heap->lock));
523         return ret;
524 }
525
526 void *
527 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
528                 unsigned int flags, size_t align, size_t bound, bool contig)
529 {
530         int socket, i, cur_socket;
531         void *ret;
532
533         /* return NULL if size is 0 or alignment is not power-of-2 */
534         if (size == 0 || (align && !rte_is_power_of_2(align)))
535                 return NULL;
536
537         if (!rte_eal_has_hugepages())
538                 socket_arg = SOCKET_ID_ANY;
539
540         if (socket_arg == SOCKET_ID_ANY)
541                 socket = malloc_get_numa_socket();
542         else
543                 socket = socket_arg;
544
545         /* Check socket parameter */
546         if (socket >= RTE_MAX_NUMA_NODES)
547                 return NULL;
548
549         ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
550                         contig);
551         if (ret != NULL || socket_arg != SOCKET_ID_ANY)
552                 return ret;
553
554         /* try other heaps */
555         for (i = 0; i < (int) rte_socket_count(); i++) {
556                 cur_socket = rte_socket_id_by_idx(i);
557                 if (cur_socket == socket)
558                         continue;
559                 ret = heap_alloc_on_socket(type, size, cur_socket, flags,
560                                 align, bound, contig);
561                 if (ret != NULL)
562                         return ret;
563         }
564         return NULL;
565 }
566
567 /* this function is exposed in malloc_mp.h */
568 int
569 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
570 {
571         int n_segs, seg_idx, max_seg_idx;
572         struct rte_memseg_list *msl;
573         size_t page_sz;
574
575         msl = rte_mem_virt2memseg_list(aligned_start);
576         if (msl == NULL)
577                 return -1;
578
579         page_sz = (size_t)msl->page_sz;
580         n_segs = aligned_len / page_sz;
581         seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
582         max_seg_idx = seg_idx + n_segs;
583
584         for (; seg_idx < max_seg_idx; seg_idx++) {
585                 struct rte_memseg *ms;
586
587                 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
588                 eal_memalloc_free_seg(ms);
589         }
590         return 0;
591 }
592
593 int
594 malloc_heap_free(struct malloc_elem *elem)
595 {
596         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
597         struct malloc_heap *heap;
598         void *start, *aligned_start, *end, *aligned_end;
599         size_t len, aligned_len, page_sz;
600         struct rte_memseg_list *msl;
601         int ret;
602
603         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
604                 return -1;
605
606         /* elem may be merged with previous element, so keep heap address */
607         heap = elem->heap;
608         msl = elem->msl;
609         page_sz = (size_t)msl->page_sz;
610
611         rte_spinlock_lock(&(heap->lock));
612
613         /* mark element as free */
614         elem->state = ELEM_FREE;
615
616         elem = malloc_elem_free(elem);
617
618         /* anything after this is a bonus */
619         ret = 0;
620
621         /* ...of which we can't avail if we are in legacy mode */
622         if (internal_config.legacy_mem)
623                 goto free_unlock;
624
625         /* check if we can free any memory back to the system */
626         if (elem->size < page_sz)
627                 goto free_unlock;
628
629         /* probably, but let's make sure, as we may not be using up full page */
630         start = elem;
631         len = elem->size;
632         aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
633         end = RTE_PTR_ADD(elem, len);
634         aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
635
636         aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
637
638         /* can't free anything */
639         if (aligned_len < page_sz)
640                 goto free_unlock;
641
642         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
643
644         /*
645          * we allow secondary processes to clear the heap of this allocated
646          * memory because it is safe to do so, as even if notifications about
647          * unmapped pages don't make it to other processes, heap is shared
648          * across all processes, and will become empty of this memory anyway,
649          * and nothing can allocate it back unless primary process will be able
650          * to deliver allocation message to every single running process.
651          */
652
653         malloc_elem_free_list_remove(elem);
654
655         malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
656
657         heap->total_size -= aligned_len;
658
659         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
660                 /* notify user about changes in memory map */
661                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
662                                 aligned_start, aligned_len);
663
664                 /* don't care if any of this fails */
665                 malloc_heap_free_pages(aligned_start, aligned_len);
666
667                 request_sync();
668         } else {
669                 struct malloc_mp_req req;
670
671                 memset(&req, 0, sizeof(req));
672
673                 req.t = REQ_TYPE_FREE;
674                 req.free_req.addr = aligned_start;
675                 req.free_req.len = aligned_len;
676
677                 /*
678                  * we request primary to deallocate pages, but we don't do it
679                  * in this thread. instead, we notify primary that we would like
680                  * to deallocate pages, and this process will receive another
681                  * request (in parallel) that will do it for us on another
682                  * thread.
683                  *
684                  * we also don't really care if this succeeds - the data is
685                  * already removed from the heap, so it is, for all intents and
686                  * purposes, hidden from the rest of DPDK even if some other
687                  * process (including this one) may have these pages mapped.
688                  *
689                  * notifications about deallocated memory happen during sync.
690                  */
691                 request_to_primary(&req);
692         }
693
694         RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
695                 msl->socket_id, aligned_len >> 20ULL);
696
697         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
698 free_unlock:
699         rte_spinlock_unlock(&(heap->lock));
700         return ret;
701 }
702
703 int
704 malloc_heap_resize(struct malloc_elem *elem, size_t size)
705 {
706         int ret;
707
708         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
709                 return -1;
710
711         rte_spinlock_lock(&(elem->heap->lock));
712
713         ret = malloc_elem_resize(elem, size);
714
715         rte_spinlock_unlock(&(elem->heap->lock));
716
717         return ret;
718 }
719
720 /*
721  * Function to retrieve data for heap on given socket
722  */
723 int
724 malloc_heap_get_stats(struct malloc_heap *heap,
725                 struct rte_malloc_socket_stats *socket_stats)
726 {
727         size_t idx;
728         struct malloc_elem *elem;
729
730         rte_spinlock_lock(&heap->lock);
731
732         /* Initialise variables for heap */
733         socket_stats->free_count = 0;
734         socket_stats->heap_freesz_bytes = 0;
735         socket_stats->greatest_free_size = 0;
736
737         /* Iterate through free list */
738         for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
739                 for (elem = LIST_FIRST(&heap->free_head[idx]);
740                         !!elem; elem = LIST_NEXT(elem, free_list))
741                 {
742                         socket_stats->free_count++;
743                         socket_stats->heap_freesz_bytes += elem->size;
744                         if (elem->size > socket_stats->greatest_free_size)
745                                 socket_stats->greatest_free_size = elem->size;
746                 }
747         }
748         /* Get stats on overall heap and allocated memory on this heap */
749         socket_stats->heap_totalsz_bytes = heap->total_size;
750         socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
751                         socket_stats->heap_freesz_bytes);
752         socket_stats->alloc_count = heap->alloc_count;
753
754         rte_spinlock_unlock(&heap->lock);
755         return 0;
756 }
757
758 /*
759  * Function to retrieve data for heap on given socket
760  */
761 void
762 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
763 {
764         struct malloc_elem *elem;
765
766         rte_spinlock_lock(&heap->lock);
767
768         fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
769         fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
770
771         elem = heap->first;
772         while (elem) {
773                 malloc_elem_dump(elem, f);
774                 elem = elem->next;
775         }
776
777         rte_spinlock_unlock(&heap->lock);
778 }
779
780 int
781 rte_eal_malloc_heap_init(void)
782 {
783         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
784
785         if (register_mp_requests()) {
786                 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
787                 return -1;
788         }
789
790         /* unlock mem hotplug here. it's safe for primary as no requests can
791          * even come before primary itself is fully initialized, and secondaries
792          * do not need to initialize the heap.
793          */
794         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
795
796         /* secondary process does not need to initialize anything */
797         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
798                 return 0;
799
800         /* add all IOVA-contiguous areas to the heap */
801         return rte_memseg_contig_walk(malloc_add_seg, NULL);
802 }