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);
330 memset(ms, 0, sizeof(*ms) * n_segs);
335 elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
336 bound, contig, ms, n_segs);
341 map_addr = ms[0]->addr;
343 /* notify user about changes in memory map */
344 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
346 /* notify other processes that this has happened */
347 if (request_sync()) {
348 /* we couldn't ensure all processes have mapped memory,
349 * so free it back and notify everyone that it's been
352 * technically, we could've avoided adding memory addresses to
353 * the map, but that would've led to inconsistent behavior
354 * between primary and secondary processes, as those get
355 * callbacks during sync. therefore, force primary process to
356 * do alloc-and-rollback syncs as well.
358 callback_triggered = true;
361 heap->total_size += alloc_sz;
363 RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
364 socket, alloc_sz >> 20ULL);
371 if (callback_triggered)
372 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
375 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
385 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
386 size_t elt_size, int socket, unsigned int flags, size_t align,
387 size_t bound, bool contig)
389 struct malloc_mp_req req;
392 memset(&req, 0, sizeof(req));
394 req.t = REQ_TYPE_ALLOC;
395 req.alloc_req.align = align;
396 req.alloc_req.bound = bound;
397 req.alloc_req.contig = contig;
398 req.alloc_req.flags = flags;
399 req.alloc_req.elt_size = elt_size;
400 req.alloc_req.page_sz = pg_sz;
401 req.alloc_req.socket = socket;
402 req.alloc_req.heap = heap; /* it's in shared memory */
404 req_result = request_to_primary(&req);
409 if (req.result != REQ_RESULT_SUCCESS)
416 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
417 int socket, unsigned int flags, size_t align, size_t bound,
420 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
423 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
425 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
426 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
427 flags, align, bound, contig);
429 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
430 flags, align, bound, contig);
433 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
438 compare_pagesz(const void *a, const void *b)
440 const struct rte_memseg_list * const*mpa = a;
441 const struct rte_memseg_list * const*mpb = b;
442 const struct rte_memseg_list *msla = *mpa;
443 const struct rte_memseg_list *mslb = *mpb;
444 uint64_t pg_sz_a = msla->page_sz;
445 uint64_t pg_sz_b = mslb->page_sz;
447 if (pg_sz_a < pg_sz_b)
449 if (pg_sz_a > pg_sz_b)
455 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
456 unsigned int flags, size_t align, size_t bound, bool contig)
458 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
459 struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
460 struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
461 uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
462 uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
464 int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
465 bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
466 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
469 memset(requested_msls, 0, sizeof(requested_msls));
470 memset(other_msls, 0, sizeof(other_msls));
471 memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
472 memset(other_pg_sz, 0, sizeof(other_pg_sz));
475 * go through memseg list and take note of all the page sizes available,
476 * and if any of them were specifically requested by the user.
478 n_requested_msls = 0;
480 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
481 struct rte_memseg_list *msl = &mcfg->memsegs[i];
483 if (msl->socket_id != socket)
486 if (msl->base_va == NULL)
489 /* if pages of specific size were requested */
490 if (size_flags != 0 && check_hugepage_sz(size_flags,
492 requested_msls[n_requested_msls++] = msl;
493 else if (size_flags == 0 || size_hint)
494 other_msls[n_other_msls++] = msl;
497 /* sort the lists, smallest first */
498 qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
500 qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
503 /* now, extract page sizes we are supposed to try */
505 n_requested_pg_sz = 0;
506 for (i = 0; i < n_requested_msls; i++) {
507 uint64_t pg_sz = requested_msls[i]->page_sz;
509 if (prev_pg_sz != pg_sz) {
510 requested_pg_sz[n_requested_pg_sz++] = pg_sz;
516 for (i = 0; i < n_other_msls; i++) {
517 uint64_t pg_sz = other_msls[i]->page_sz;
519 if (prev_pg_sz != pg_sz) {
520 other_pg_sz[n_other_pg_sz++] = pg_sz;
525 /* finally, try allocating memory of specified page sizes, starting from
528 for (i = 0; i < n_requested_pg_sz; i++) {
529 uint64_t pg_sz = requested_pg_sz[i];
532 * do not pass the size hint here, as user expects other page
533 * sizes first, before resorting to best effort allocation.
535 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
536 align, bound, contig))
539 if (n_other_pg_sz == 0)
542 /* now, check if we can reserve anything with size hint */
543 ret = find_suitable_element(heap, size, flags, align, bound, contig);
548 * we still couldn't reserve memory, so try expanding heap with other
549 * page sizes, if there are any
551 for (i = 0; i < n_other_pg_sz; i++) {
552 uint64_t pg_sz = other_pg_sz[i];
554 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
555 align, bound, contig))
561 /* this will try lower page sizes first */
563 heap_alloc_on_socket(const char *type, size_t size, int socket,
564 unsigned int flags, size_t align, size_t bound, bool contig)
566 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
567 struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
568 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
571 rte_spinlock_lock(&(heap->lock));
573 align = align == 0 ? 1 : align;
575 /* for legacy mode, try once and with all flags */
576 if (internal_config.legacy_mem) {
577 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
582 * we do not pass the size hint here, because even if allocation fails,
583 * we may still be able to allocate memory from appropriate page sizes,
584 * we just need to request more memory first.
586 ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
590 if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
592 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
594 /* this should have succeeded */
596 RTE_LOG(ERR, EAL, "Error allocating from heap\n");
599 rte_spinlock_unlock(&(heap->lock));
604 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
605 unsigned int flags, size_t align, size_t bound, bool contig)
607 int socket, i, cur_socket;
610 /* return NULL if size is 0 or alignment is not power-of-2 */
611 if (size == 0 || (align && !rte_is_power_of_2(align)))
614 if (!rte_eal_has_hugepages())
615 socket_arg = SOCKET_ID_ANY;
617 if (socket_arg == SOCKET_ID_ANY)
618 socket = malloc_get_numa_socket();
622 /* Check socket parameter */
623 if (socket >= RTE_MAX_NUMA_NODES)
626 ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
628 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
631 /* try other heaps */
632 for (i = 0; i < (int) rte_socket_count(); i++) {
633 cur_socket = rte_socket_id_by_idx(i);
634 if (cur_socket == socket)
636 ret = heap_alloc_on_socket(type, size, cur_socket, flags,
637 align, bound, contig);
645 heap_alloc_biggest_on_socket(const char *type, int socket, unsigned int flags,
646 size_t align, bool contig)
648 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
649 struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
652 rte_spinlock_lock(&(heap->lock));
654 align = align == 0 ? 1 : align;
656 ret = heap_alloc_biggest(heap, type, flags, align, contig);
658 rte_spinlock_unlock(&(heap->lock));
664 malloc_heap_alloc_biggest(const char *type, int socket_arg, unsigned int flags,
665 size_t align, bool contig)
667 int socket, i, cur_socket;
670 /* return NULL if align is not power-of-2 */
671 if ((align && !rte_is_power_of_2(align)))
674 if (!rte_eal_has_hugepages())
675 socket_arg = SOCKET_ID_ANY;
677 if (socket_arg == SOCKET_ID_ANY)
678 socket = malloc_get_numa_socket();
682 /* Check socket parameter */
683 if (socket >= RTE_MAX_NUMA_NODES)
686 ret = heap_alloc_biggest_on_socket(type, socket, flags, align,
688 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
691 /* try other heaps */
692 for (i = 0; i < (int) rte_socket_count(); i++) {
693 cur_socket = rte_socket_id_by_idx(i);
694 if (cur_socket == socket)
696 ret = heap_alloc_biggest_on_socket(type, cur_socket, flags,
704 /* this function is exposed in malloc_mp.h */
706 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
708 int n_segs, seg_idx, max_seg_idx;
709 struct rte_memseg_list *msl;
712 msl = rte_mem_virt2memseg_list(aligned_start);
716 page_sz = (size_t)msl->page_sz;
717 n_segs = aligned_len / page_sz;
718 seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
719 max_seg_idx = seg_idx + n_segs;
721 for (; seg_idx < max_seg_idx; seg_idx++) {
722 struct rte_memseg *ms;
724 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
725 eal_memalloc_free_seg(ms);
731 malloc_heap_free(struct malloc_elem *elem)
733 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
734 struct malloc_heap *heap;
735 void *start, *aligned_start, *end, *aligned_end;
736 size_t len, aligned_len, page_sz;
737 struct rte_memseg_list *msl;
738 unsigned int i, n_segs, before_space, after_space;
741 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
744 /* elem may be merged with previous element, so keep heap address */
747 page_sz = (size_t)msl->page_sz;
749 rte_spinlock_lock(&(heap->lock));
751 /* mark element as free */
752 elem->state = ELEM_FREE;
754 elem = malloc_elem_free(elem);
756 /* anything after this is a bonus */
759 /* ...of which we can't avail if we are in legacy mode */
760 if (internal_config.legacy_mem)
763 /* check if we can free any memory back to the system */
764 if (elem->size < page_sz)
767 /* probably, but let's make sure, as we may not be using up full page */
770 aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
771 end = RTE_PTR_ADD(elem, len);
772 aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
774 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
776 /* can't free anything */
777 if (aligned_len < page_sz)
780 /* we can free something. however, some of these pages may be marked as
781 * unfreeable, so also check that as well
783 n_segs = aligned_len / page_sz;
784 for (i = 0; i < n_segs; i++) {
785 const struct rte_memseg *tmp =
786 rte_mem_virt2memseg(aligned_start, msl);
788 if (tmp->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
789 /* this is an unfreeable segment, so move start */
790 aligned_start = RTE_PTR_ADD(tmp->addr, tmp->len);
794 /* recalculate length and number of segments */
795 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
796 n_segs = aligned_len / page_sz;
798 /* check if we can still free some pages */
802 /* We're not done yet. We also have to check if by freeing space we will
803 * be leaving free elements that are too small to store new elements.
804 * Check if we have enough space in the beginning and at the end, or if
805 * start/end are exactly page aligned.
807 before_space = RTE_PTR_DIFF(aligned_start, elem);
808 after_space = RTE_PTR_DIFF(end, aligned_end);
809 if (before_space != 0 &&
810 before_space < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
811 /* There is not enough space before start, but we may be able to
812 * move the start forward by one page.
818 aligned_start = RTE_PTR_ADD(aligned_start, page_sz);
819 aligned_len -= page_sz;
822 if (after_space != 0 && after_space <
823 MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
824 /* There is not enough space after end, but we may be able to
825 * move the end backwards by one page.
831 aligned_end = RTE_PTR_SUB(aligned_end, page_sz);
832 aligned_len -= page_sz;
836 /* now we can finally free us some pages */
838 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
841 * we allow secondary processes to clear the heap of this allocated
842 * memory because it is safe to do so, as even if notifications about
843 * unmapped pages don't make it to other processes, heap is shared
844 * across all processes, and will become empty of this memory anyway,
845 * and nothing can allocate it back unless primary process will be able
846 * to deliver allocation message to every single running process.
849 malloc_elem_free_list_remove(elem);
851 malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
853 heap->total_size -= aligned_len;
855 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
856 /* notify user about changes in memory map */
857 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
858 aligned_start, aligned_len);
860 /* don't care if any of this fails */
861 malloc_heap_free_pages(aligned_start, aligned_len);
865 struct malloc_mp_req req;
867 memset(&req, 0, sizeof(req));
869 req.t = REQ_TYPE_FREE;
870 req.free_req.addr = aligned_start;
871 req.free_req.len = aligned_len;
874 * we request primary to deallocate pages, but we don't do it
875 * in this thread. instead, we notify primary that we would like
876 * to deallocate pages, and this process will receive another
877 * request (in parallel) that will do it for us on another
880 * we also don't really care if this succeeds - the data is
881 * already removed from the heap, so it is, for all intents and
882 * purposes, hidden from the rest of DPDK even if some other
883 * process (including this one) may have these pages mapped.
885 * notifications about deallocated memory happen during sync.
887 request_to_primary(&req);
890 RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
891 msl->socket_id, aligned_len >> 20ULL);
893 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
895 rte_spinlock_unlock(&(heap->lock));
900 malloc_heap_resize(struct malloc_elem *elem, size_t size)
904 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
907 rte_spinlock_lock(&(elem->heap->lock));
909 ret = malloc_elem_resize(elem, size);
911 rte_spinlock_unlock(&(elem->heap->lock));
917 * Function to retrieve data for heap on given socket
920 malloc_heap_get_stats(struct malloc_heap *heap,
921 struct rte_malloc_socket_stats *socket_stats)
924 struct malloc_elem *elem;
926 rte_spinlock_lock(&heap->lock);
928 /* Initialise variables for heap */
929 socket_stats->free_count = 0;
930 socket_stats->heap_freesz_bytes = 0;
931 socket_stats->greatest_free_size = 0;
933 /* Iterate through free list */
934 for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
935 for (elem = LIST_FIRST(&heap->free_head[idx]);
936 !!elem; elem = LIST_NEXT(elem, free_list))
938 socket_stats->free_count++;
939 socket_stats->heap_freesz_bytes += elem->size;
940 if (elem->size > socket_stats->greatest_free_size)
941 socket_stats->greatest_free_size = elem->size;
944 /* Get stats on overall heap and allocated memory on this heap */
945 socket_stats->heap_totalsz_bytes = heap->total_size;
946 socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
947 socket_stats->heap_freesz_bytes);
948 socket_stats->alloc_count = heap->alloc_count;
950 rte_spinlock_unlock(&heap->lock);
955 * Function to retrieve data for heap on given socket
958 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
960 struct malloc_elem *elem;
962 rte_spinlock_lock(&heap->lock);
964 fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
965 fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
969 malloc_elem_dump(elem, f);
973 rte_spinlock_unlock(&heap->lock);
977 rte_eal_malloc_heap_init(void)
979 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
981 if (register_mp_requests()) {
982 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
983 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
987 /* unlock mem hotplug here. it's safe for primary as no requests can
988 * even come before primary itself is fully initialized, and secondaries
989 * do not need to initialize the heap.
991 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
993 /* secondary process does not need to initialize anything */
994 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
997 /* add all IOVA-contiguous areas to the heap */
998 return rte_memseg_contig_walk(malloc_add_seg, NULL);