1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/queue.h>
12 #include <rte_memory.h>
13 #include <rte_errno.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>
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"
33 check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
35 unsigned check_flag = 0;
37 if (!(flags & ~RTE_MEMZONE_SIZE_HINT_ONLY))
40 switch (hugepage_sz) {
42 check_flag = RTE_MEMZONE_256KB;
45 check_flag = RTE_MEMZONE_2MB;
48 check_flag = RTE_MEMZONE_16MB;
51 check_flag = RTE_MEMZONE_256MB;
54 check_flag = RTE_MEMZONE_512MB;
57 check_flag = RTE_MEMZONE_1GB;
60 check_flag = RTE_MEMZONE_4GB;
63 check_flag = RTE_MEMZONE_16GB;
66 return check_flag & flags;
70 * Expand the heap with a memory area.
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)
76 struct malloc_elem *elem = start;
78 malloc_elem_init(elem, heap, msl, len);
80 malloc_elem_insert(elem);
82 elem = malloc_elem_join_adjacent_free(elem);
84 malloc_elem_free_list_insert(elem);
90 malloc_add_seg(const struct rte_memseg_list *msl,
91 const struct rte_memseg *ms, size_t len, void *arg __rte_unused)
93 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
94 struct rte_memseg_list *found_msl;
95 struct malloc_heap *heap;
98 heap = &mcfg->malloc_heaps[msl->socket_id];
100 /* msl is const, so find it */
101 msl_idx = msl - mcfg->memsegs;
103 if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
106 found_msl = &mcfg->memsegs[msl_idx];
108 malloc_heap_add_memory(heap, found_msl, ms->addr, len);
110 heap->total_size += len;
112 RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
118 * Iterates through the freelist for a heap to find a free element
119 * which can store data of the required size and with the requested alignment.
120 * If size is 0, find the biggest available elem.
121 * Returns null on failure, or pointer to element on success.
123 static struct malloc_elem *
124 find_suitable_element(struct malloc_heap *heap, size_t size,
125 unsigned int flags, size_t align, size_t bound, bool contig)
128 struct malloc_elem *elem, *alt_elem = NULL;
130 for (idx = malloc_elem_free_list_index(size);
131 idx < RTE_HEAP_NUM_FREELISTS; idx++) {
132 for (elem = LIST_FIRST(&heap->free_head[idx]);
133 !!elem; elem = LIST_NEXT(elem, free_list)) {
134 if (malloc_elem_can_hold(elem, size, align, bound,
136 if (check_hugepage_sz(flags,
139 if (alt_elem == NULL)
145 if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
152 * Iterates through the freelist for a heap to find a free element with the
153 * biggest size and requested alignment. Will also set size to whatever element
154 * size that was found.
155 * Returns null on failure, or pointer to element on success.
157 static struct malloc_elem *
158 find_biggest_element(struct malloc_heap *heap, size_t *size,
159 unsigned int flags, size_t align, bool contig)
161 struct malloc_elem *elem, *max_elem = NULL;
162 size_t idx, max_size = 0;
164 for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
165 for (elem = LIST_FIRST(&heap->free_head[idx]);
166 !!elem; elem = LIST_NEXT(elem, free_list)) {
168 if (!check_hugepage_sz(flags, elem->msl->page_sz))
172 malloc_elem_find_max_iova_contig(elem,
175 void *data_start = RTE_PTR_ADD(elem,
176 MALLOC_ELEM_HEADER_LEN);
177 void *data_end = RTE_PTR_ADD(elem, elem->size -
178 MALLOC_ELEM_TRAILER_LEN);
179 void *aligned = RTE_PTR_ALIGN_CEIL(data_start,
181 /* check if aligned data start is beyond end */
182 if (aligned >= data_end)
184 cur_size = RTE_PTR_DIFF(data_end, aligned);
186 if (cur_size > max_size) {
198 * Main function to allocate a block of memory from the heap.
199 * It locks the free list, scans it, and adds a new memseg if the
200 * scan fails. Once the new memseg is added, it re-scans and should return
201 * the new element after releasing the lock.
204 heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
205 unsigned int flags, size_t align, size_t bound, bool contig)
207 struct malloc_elem *elem;
209 size = RTE_CACHE_LINE_ROUNDUP(size);
210 align = RTE_CACHE_LINE_ROUNDUP(align);
212 elem = find_suitable_element(heap, size, flags, align, bound, contig);
214 elem = malloc_elem_alloc(elem, size, align, bound, contig);
216 /* increase heap's count of allocated elements */
220 return elem == NULL ? NULL : (void *)(&elem[1]);
224 heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,
225 unsigned int flags, size_t align, bool contig)
227 struct malloc_elem *elem;
230 align = RTE_CACHE_LINE_ROUNDUP(align);
232 elem = find_biggest_element(heap, &size, flags, align, contig);
234 elem = malloc_elem_alloc(elem, size, align, 0, contig);
236 /* increase heap's count of allocated elements */
240 return elem == NULL ? NULL : (void *)(&elem[1]);
243 /* this function is exposed in malloc_mp.h */
245 rollback_expand_heap(struct rte_memseg **ms, int n_segs,
246 struct malloc_elem *elem, void *map_addr, size_t map_len)
249 malloc_elem_free_list_remove(elem);
250 malloc_elem_hide_region(elem, map_addr, map_len);
253 eal_memalloc_free_seg_bulk(ms, n_segs);
256 /* this function is exposed in malloc_mp.h */
258 alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
259 int socket, unsigned int flags, size_t align, size_t bound,
260 bool contig, struct rte_memseg **ms, int n_segs)
262 struct rte_memseg_list *msl;
263 struct malloc_elem *elem = NULL;
266 void *ret, *map_addr;
268 alloc_sz = (size_t)pg_sz * n_segs;
270 /* first, check if we're allowed to allocate this memory */
271 if (eal_memalloc_mem_alloc_validate(socket,
272 heap->total_size + alloc_sz) < 0) {
273 RTE_LOG(DEBUG, EAL, "User has disallowed allocation\n");
277 allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
280 /* make sure we've allocated our pages... */
281 if (allocd_pages < 0)
284 map_addr = ms[0]->addr;
285 msl = rte_mem_virt2memseg_list(map_addr);
287 /* check if we wanted contiguous memory but didn't get it */
288 if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
289 RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
294 /* add newly minted memsegs to malloc heap */
295 elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
297 /* try once more, as now we have allocated new memory */
298 ret = find_suitable_element(heap, elt_size, flags, align, bound,
307 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
312 try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
313 size_t elt_size, int socket, unsigned int flags, size_t align,
314 size_t bound, bool contig)
316 struct malloc_elem *elem;
317 struct rte_memseg **ms;
321 bool callback_triggered = false;
323 alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
324 MALLOC_ELEM_TRAILER_LEN, pg_sz);
325 n_segs = alloc_sz / pg_sz;
327 /* we can't know in advance how many pages we'll need, so we malloc */
328 ms = malloc(sizeof(*ms) * n_segs);
331 memset(ms, 0, sizeof(*ms) * n_segs);
333 elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
334 bound, contig, ms, n_segs);
339 map_addr = ms[0]->addr;
341 /* notify user about changes in memory map */
342 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
344 /* notify other processes that this has happened */
345 if (request_sync()) {
346 /* we couldn't ensure all processes have mapped memory,
347 * so free it back and notify everyone that it's been
350 * technically, we could've avoided adding memory addresses to
351 * the map, but that would've led to inconsistent behavior
352 * between primary and secondary processes, as those get
353 * callbacks during sync. therefore, force primary process to
354 * do alloc-and-rollback syncs as well.
356 callback_triggered = true;
359 heap->total_size += alloc_sz;
361 RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
362 socket, alloc_sz >> 20ULL);
369 if (callback_triggered)
370 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
373 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
383 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
384 size_t elt_size, int socket, unsigned int flags, size_t align,
385 size_t bound, bool contig)
387 struct malloc_mp_req req;
390 memset(&req, 0, sizeof(req));
392 req.t = REQ_TYPE_ALLOC;
393 req.alloc_req.align = align;
394 req.alloc_req.bound = bound;
395 req.alloc_req.contig = contig;
396 req.alloc_req.flags = flags;
397 req.alloc_req.elt_size = elt_size;
398 req.alloc_req.page_sz = pg_sz;
399 req.alloc_req.socket = socket;
400 req.alloc_req.heap = heap; /* it's in shared memory */
402 req_result = request_to_primary(&req);
407 if (req.result != REQ_RESULT_SUCCESS)
414 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
415 int socket, unsigned int flags, size_t align, size_t bound,
418 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
421 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
423 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
424 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
425 flags, align, bound, contig);
427 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
428 flags, align, bound, contig);
431 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
436 compare_pagesz(const void *a, const void *b)
438 const struct rte_memseg_list * const*mpa = a;
439 const struct rte_memseg_list * const*mpb = b;
440 const struct rte_memseg_list *msla = *mpa;
441 const struct rte_memseg_list *mslb = *mpb;
442 uint64_t pg_sz_a = msla->page_sz;
443 uint64_t pg_sz_b = mslb->page_sz;
445 if (pg_sz_a < pg_sz_b)
447 if (pg_sz_a > pg_sz_b)
453 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
454 unsigned int flags, size_t align, size_t bound, bool contig)
456 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
457 struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
458 struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
459 uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
460 uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
462 int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
463 bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
464 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
467 memset(requested_msls, 0, sizeof(requested_msls));
468 memset(other_msls, 0, sizeof(other_msls));
469 memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
470 memset(other_pg_sz, 0, sizeof(other_pg_sz));
473 * go through memseg list and take note of all the page sizes available,
474 * and if any of them were specifically requested by the user.
476 n_requested_msls = 0;
478 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
479 struct rte_memseg_list *msl = &mcfg->memsegs[i];
481 if (msl->socket_id != socket)
484 if (msl->base_va == NULL)
487 /* if pages of specific size were requested */
488 if (size_flags != 0 && check_hugepage_sz(size_flags,
490 requested_msls[n_requested_msls++] = msl;
491 else if (size_flags == 0 || size_hint)
492 other_msls[n_other_msls++] = msl;
495 /* sort the lists, smallest first */
496 qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
498 qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
501 /* now, extract page sizes we are supposed to try */
503 n_requested_pg_sz = 0;
504 for (i = 0; i < n_requested_msls; i++) {
505 uint64_t pg_sz = requested_msls[i]->page_sz;
507 if (prev_pg_sz != pg_sz) {
508 requested_pg_sz[n_requested_pg_sz++] = pg_sz;
514 for (i = 0; i < n_other_msls; i++) {
515 uint64_t pg_sz = other_msls[i]->page_sz;
517 if (prev_pg_sz != pg_sz) {
518 other_pg_sz[n_other_pg_sz++] = pg_sz;
523 /* finally, try allocating memory of specified page sizes, starting from
526 for (i = 0; i < n_requested_pg_sz; i++) {
527 uint64_t pg_sz = requested_pg_sz[i];
530 * do not pass the size hint here, as user expects other page
531 * sizes first, before resorting to best effort allocation.
533 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
534 align, bound, contig))
537 if (n_other_pg_sz == 0)
540 /* now, check if we can reserve anything with size hint */
541 ret = find_suitable_element(heap, size, flags, align, bound, contig);
546 * we still couldn't reserve memory, so try expanding heap with other
547 * page sizes, if there are any
549 for (i = 0; i < n_other_pg_sz; i++) {
550 uint64_t pg_sz = other_pg_sz[i];
552 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
553 align, bound, contig))
559 /* this will try lower page sizes first */
561 heap_alloc_on_socket(const char *type, size_t size, int socket,
562 unsigned int flags, size_t align, size_t bound, bool contig)
564 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
565 struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
566 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
569 rte_spinlock_lock(&(heap->lock));
571 align = align == 0 ? 1 : align;
573 /* for legacy mode, try once and with all flags */
574 if (internal_config.legacy_mem) {
575 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
580 * we do not pass the size hint here, because even if allocation fails,
581 * we may still be able to allocate memory from appropriate page sizes,
582 * we just need to request more memory first.
584 ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
588 if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
590 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
592 /* this should have succeeded */
594 RTE_LOG(ERR, EAL, "Error allocating from heap\n");
597 rte_spinlock_unlock(&(heap->lock));
602 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
603 unsigned int flags, size_t align, size_t bound, bool contig)
605 int socket, i, cur_socket;
608 /* return NULL if size is 0 or alignment is not power-of-2 */
609 if (size == 0 || (align && !rte_is_power_of_2(align)))
612 if (!rte_eal_has_hugepages())
613 socket_arg = SOCKET_ID_ANY;
615 if (socket_arg == SOCKET_ID_ANY)
616 socket = malloc_get_numa_socket();
620 /* Check socket parameter */
621 if (socket >= RTE_MAX_NUMA_NODES)
624 ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
626 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
629 /* try other heaps */
630 for (i = 0; i < (int) rte_socket_count(); i++) {
631 cur_socket = rte_socket_id_by_idx(i);
632 if (cur_socket == socket)
634 ret = heap_alloc_on_socket(type, size, cur_socket, flags,
635 align, bound, contig);
643 heap_alloc_biggest_on_socket(const char *type, int socket, unsigned int flags,
644 size_t align, bool contig)
646 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
647 struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
650 rte_spinlock_lock(&(heap->lock));
652 align = align == 0 ? 1 : align;
654 ret = heap_alloc_biggest(heap, type, flags, align, contig);
656 rte_spinlock_unlock(&(heap->lock));
662 malloc_heap_alloc_biggest(const char *type, int socket_arg, unsigned int flags,
663 size_t align, bool contig)
665 int socket, i, cur_socket;
668 /* return NULL if align is not power-of-2 */
669 if ((align && !rte_is_power_of_2(align)))
672 if (!rte_eal_has_hugepages())
673 socket_arg = SOCKET_ID_ANY;
675 if (socket_arg == SOCKET_ID_ANY)
676 socket = malloc_get_numa_socket();
680 /* Check socket parameter */
681 if (socket >= RTE_MAX_NUMA_NODES)
684 ret = heap_alloc_biggest_on_socket(type, socket, flags, align,
686 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
689 /* try other heaps */
690 for (i = 0; i < (int) rte_socket_count(); i++) {
691 cur_socket = rte_socket_id_by_idx(i);
692 if (cur_socket == socket)
694 ret = heap_alloc_biggest_on_socket(type, cur_socket, flags,
702 /* this function is exposed in malloc_mp.h */
704 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
706 int n_segs, seg_idx, max_seg_idx;
707 struct rte_memseg_list *msl;
710 msl = rte_mem_virt2memseg_list(aligned_start);
714 page_sz = (size_t)msl->page_sz;
715 n_segs = aligned_len / page_sz;
716 seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
717 max_seg_idx = seg_idx + n_segs;
719 for (; seg_idx < max_seg_idx; seg_idx++) {
720 struct rte_memseg *ms;
722 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
723 eal_memalloc_free_seg(ms);
729 malloc_heap_free(struct malloc_elem *elem)
731 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
732 struct malloc_heap *heap;
733 void *start, *aligned_start, *end, *aligned_end;
734 size_t len, aligned_len, page_sz;
735 struct rte_memseg_list *msl;
736 unsigned int i, n_segs, before_space, after_space;
739 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
742 /* elem may be merged with previous element, so keep heap address */
745 page_sz = (size_t)msl->page_sz;
747 rte_spinlock_lock(&(heap->lock));
749 /* mark element as free */
750 elem->state = ELEM_FREE;
752 elem = malloc_elem_free(elem);
754 /* anything after this is a bonus */
757 /* ...of which we can't avail if we are in legacy mode */
758 if (internal_config.legacy_mem)
761 /* check if we can free any memory back to the system */
762 if (elem->size < page_sz)
765 /* probably, but let's make sure, as we may not be using up full page */
768 aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
769 end = RTE_PTR_ADD(elem, len);
770 aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
772 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
774 /* can't free anything */
775 if (aligned_len < page_sz)
778 /* we can free something. however, some of these pages may be marked as
779 * unfreeable, so also check that as well
781 n_segs = aligned_len / page_sz;
782 for (i = 0; i < n_segs; i++) {
783 const struct rte_memseg *tmp =
784 rte_mem_virt2memseg(aligned_start, msl);
786 if (tmp->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
787 /* this is an unfreeable segment, so move start */
788 aligned_start = RTE_PTR_ADD(tmp->addr, tmp->len);
792 /* recalculate length and number of segments */
793 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
794 n_segs = aligned_len / page_sz;
796 /* check if we can still free some pages */
800 /* We're not done yet. We also have to check if by freeing space we will
801 * be leaving free elements that are too small to store new elements.
802 * Check if we have enough space in the beginning and at the end, or if
803 * start/end are exactly page aligned.
805 before_space = RTE_PTR_DIFF(aligned_start, elem);
806 after_space = RTE_PTR_DIFF(end, aligned_end);
807 if (before_space != 0 &&
808 before_space < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
809 /* There is not enough space before start, but we may be able to
810 * move the start forward by one page.
816 aligned_start = RTE_PTR_ADD(aligned_start, page_sz);
817 aligned_len -= page_sz;
820 if (after_space != 0 && after_space <
821 MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
822 /* There is not enough space after end, but we may be able to
823 * move the end backwards by one page.
829 aligned_end = RTE_PTR_SUB(aligned_end, page_sz);
830 aligned_len -= page_sz;
834 /* now we can finally free us some pages */
836 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
839 * we allow secondary processes to clear the heap of this allocated
840 * memory because it is safe to do so, as even if notifications about
841 * unmapped pages don't make it to other processes, heap is shared
842 * across all processes, and will become empty of this memory anyway,
843 * and nothing can allocate it back unless primary process will be able
844 * to deliver allocation message to every single running process.
847 malloc_elem_free_list_remove(elem);
849 malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
851 heap->total_size -= aligned_len;
853 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
854 /* notify user about changes in memory map */
855 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
856 aligned_start, aligned_len);
858 /* don't care if any of this fails */
859 malloc_heap_free_pages(aligned_start, aligned_len);
863 struct malloc_mp_req req;
865 memset(&req, 0, sizeof(req));
867 req.t = REQ_TYPE_FREE;
868 req.free_req.addr = aligned_start;
869 req.free_req.len = aligned_len;
872 * we request primary to deallocate pages, but we don't do it
873 * in this thread. instead, we notify primary that we would like
874 * to deallocate pages, and this process will receive another
875 * request (in parallel) that will do it for us on another
878 * we also don't really care if this succeeds - the data is
879 * already removed from the heap, so it is, for all intents and
880 * purposes, hidden from the rest of DPDK even if some other
881 * process (including this one) may have these pages mapped.
883 * notifications about deallocated memory happen during sync.
885 request_to_primary(&req);
888 RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
889 msl->socket_id, aligned_len >> 20ULL);
891 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
893 rte_spinlock_unlock(&(heap->lock));
898 malloc_heap_resize(struct malloc_elem *elem, size_t size)
902 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
905 rte_spinlock_lock(&(elem->heap->lock));
907 ret = malloc_elem_resize(elem, size);
909 rte_spinlock_unlock(&(elem->heap->lock));
915 * Function to retrieve data for heap on given socket
918 malloc_heap_get_stats(struct malloc_heap *heap,
919 struct rte_malloc_socket_stats *socket_stats)
922 struct malloc_elem *elem;
924 rte_spinlock_lock(&heap->lock);
926 /* Initialise variables for heap */
927 socket_stats->free_count = 0;
928 socket_stats->heap_freesz_bytes = 0;
929 socket_stats->greatest_free_size = 0;
931 /* Iterate through free list */
932 for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
933 for (elem = LIST_FIRST(&heap->free_head[idx]);
934 !!elem; elem = LIST_NEXT(elem, free_list))
936 socket_stats->free_count++;
937 socket_stats->heap_freesz_bytes += elem->size;
938 if (elem->size > socket_stats->greatest_free_size)
939 socket_stats->greatest_free_size = elem->size;
942 /* Get stats on overall heap and allocated memory on this heap */
943 socket_stats->heap_totalsz_bytes = heap->total_size;
944 socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
945 socket_stats->heap_freesz_bytes);
946 socket_stats->alloc_count = heap->alloc_count;
948 rte_spinlock_unlock(&heap->lock);
953 * Function to retrieve data for heap on given socket
956 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
958 struct malloc_elem *elem;
960 rte_spinlock_lock(&heap->lock);
962 fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
963 fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
967 malloc_elem_dump(elem, f);
971 rte_spinlock_unlock(&heap->lock);
975 rte_eal_malloc_heap_init(void)
977 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
979 if (register_mp_requests()) {
980 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
981 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
985 /* unlock mem hotplug here. it's safe for primary as no requests can
986 * even come before primary itself is fully initialized, and secondaries
987 * do not need to initialize the heap.
989 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
991 /* secondary process does not need to initialize anything */
992 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
995 /* add all IOVA-contiguous areas to the heap */
996 return rte_memseg_contig_walk(malloc_add_seg, NULL);