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