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 malloc_socket_to_heap_id(unsigned int socket_id)
72 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
75 for (i = 0; i < RTE_MAX_HEAPS; i++) {
76 struct malloc_heap *heap = &mcfg->malloc_heaps[i];
78 if (heap->socket_id == socket_id)
85 * Expand the heap with a memory area.
87 static struct malloc_elem *
88 malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
89 void *start, size_t len)
91 struct malloc_elem *elem = start;
93 malloc_elem_init(elem, heap, msl, len);
95 malloc_elem_insert(elem);
97 elem = malloc_elem_join_adjacent_free(elem);
99 malloc_elem_free_list_insert(elem);
105 malloc_add_seg(const struct rte_memseg_list *msl,
106 const struct rte_memseg *ms, size_t len, void *arg __rte_unused)
108 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
109 struct rte_memseg_list *found_msl;
110 struct malloc_heap *heap;
111 int msl_idx, heap_idx;
116 heap_idx = malloc_socket_to_heap_id(msl->socket_id);
118 RTE_LOG(ERR, EAL, "Memseg list has invalid socket id\n");
121 heap = &mcfg->malloc_heaps[heap_idx];
123 /* msl is const, so find it */
124 msl_idx = msl - mcfg->memsegs;
126 if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
129 found_msl = &mcfg->memsegs[msl_idx];
131 malloc_heap_add_memory(heap, found_msl, ms->addr, len);
133 heap->total_size += len;
134 heap->socket_id = msl->socket_id;
136 RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
142 * Iterates through the freelist for a heap to find a free element
143 * which can store data of the required size and with the requested alignment.
144 * If size is 0, find the biggest available elem.
145 * Returns null on failure, or pointer to element on success.
147 static struct malloc_elem *
148 find_suitable_element(struct malloc_heap *heap, size_t size,
149 unsigned int flags, size_t align, size_t bound, bool contig)
152 struct malloc_elem *elem, *alt_elem = NULL;
154 for (idx = malloc_elem_free_list_index(size);
155 idx < RTE_HEAP_NUM_FREELISTS; idx++) {
156 for (elem = LIST_FIRST(&heap->free_head[idx]);
157 !!elem; elem = LIST_NEXT(elem, free_list)) {
158 if (malloc_elem_can_hold(elem, size, align, bound,
160 if (check_hugepage_sz(flags,
163 if (alt_elem == NULL)
169 if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
176 * Iterates through the freelist for a heap to find a free element with the
177 * biggest size and requested alignment. Will also set size to whatever element
178 * size that was found.
179 * Returns null on failure, or pointer to element on success.
181 static struct malloc_elem *
182 find_biggest_element(struct malloc_heap *heap, size_t *size,
183 unsigned int flags, size_t align, bool contig)
185 struct malloc_elem *elem, *max_elem = NULL;
186 size_t idx, max_size = 0;
188 for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
189 for (elem = LIST_FIRST(&heap->free_head[idx]);
190 !!elem; elem = LIST_NEXT(elem, free_list)) {
192 if (!check_hugepage_sz(flags, elem->msl->page_sz))
196 malloc_elem_find_max_iova_contig(elem,
199 void *data_start = RTE_PTR_ADD(elem,
200 MALLOC_ELEM_HEADER_LEN);
201 void *data_end = RTE_PTR_ADD(elem, elem->size -
202 MALLOC_ELEM_TRAILER_LEN);
203 void *aligned = RTE_PTR_ALIGN_CEIL(data_start,
205 /* check if aligned data start is beyond end */
206 if (aligned >= data_end)
208 cur_size = RTE_PTR_DIFF(data_end, aligned);
210 if (cur_size > max_size) {
222 * Main function to allocate a block of memory from the heap.
223 * It locks the free list, scans it, and adds a new memseg if the
224 * scan fails. Once the new memseg is added, it re-scans and should return
225 * the new element after releasing the lock.
228 heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
229 unsigned int flags, size_t align, size_t bound, bool contig)
231 struct malloc_elem *elem;
233 size = RTE_CACHE_LINE_ROUNDUP(size);
234 align = RTE_CACHE_LINE_ROUNDUP(align);
236 elem = find_suitable_element(heap, size, flags, align, bound, contig);
238 elem = malloc_elem_alloc(elem, size, align, bound, contig);
240 /* increase heap's count of allocated elements */
244 return elem == NULL ? NULL : (void *)(&elem[1]);
248 heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,
249 unsigned int flags, size_t align, bool contig)
251 struct malloc_elem *elem;
254 align = RTE_CACHE_LINE_ROUNDUP(align);
256 elem = find_biggest_element(heap, &size, flags, align, contig);
258 elem = malloc_elem_alloc(elem, size, align, 0, contig);
260 /* increase heap's count of allocated elements */
264 return elem == NULL ? NULL : (void *)(&elem[1]);
267 /* this function is exposed in malloc_mp.h */
269 rollback_expand_heap(struct rte_memseg **ms, int n_segs,
270 struct malloc_elem *elem, void *map_addr, size_t map_len)
273 malloc_elem_free_list_remove(elem);
274 malloc_elem_hide_region(elem, map_addr, map_len);
277 eal_memalloc_free_seg_bulk(ms, n_segs);
280 /* this function is exposed in malloc_mp.h */
282 alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
283 int socket, unsigned int flags, size_t align, size_t bound,
284 bool contig, struct rte_memseg **ms, int n_segs)
286 struct rte_memseg_list *msl;
287 struct malloc_elem *elem = NULL;
290 void *ret, *map_addr;
292 alloc_sz = (size_t)pg_sz * n_segs;
294 /* first, check if we're allowed to allocate this memory */
295 if (eal_memalloc_mem_alloc_validate(socket,
296 heap->total_size + alloc_sz) < 0) {
297 RTE_LOG(DEBUG, EAL, "User has disallowed allocation\n");
301 allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
304 /* make sure we've allocated our pages... */
305 if (allocd_pages < 0)
308 map_addr = ms[0]->addr;
309 msl = rte_mem_virt2memseg_list(map_addr);
311 /* check if we wanted contiguous memory but didn't get it */
312 if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
313 RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
318 /* add newly minted memsegs to malloc heap */
319 elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
321 /* try once more, as now we have allocated new memory */
322 ret = find_suitable_element(heap, elt_size, flags, align, bound,
331 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
336 try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
337 size_t elt_size, int socket, unsigned int flags, size_t align,
338 size_t bound, bool contig)
340 struct malloc_elem *elem;
341 struct rte_memseg **ms;
345 bool callback_triggered = false;
347 alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
348 MALLOC_ELEM_TRAILER_LEN, pg_sz);
349 n_segs = alloc_sz / pg_sz;
351 /* we can't know in advance how many pages we'll need, so we malloc */
352 ms = malloc(sizeof(*ms) * n_segs);
355 memset(ms, 0, sizeof(*ms) * n_segs);
357 elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
358 bound, contig, ms, n_segs);
363 map_addr = ms[0]->addr;
365 /* notify user about changes in memory map */
366 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
368 /* notify other processes that this has happened */
369 if (request_sync()) {
370 /* we couldn't ensure all processes have mapped memory,
371 * so free it back and notify everyone that it's been
374 * technically, we could've avoided adding memory addresses to
375 * the map, but that would've led to inconsistent behavior
376 * between primary and secondary processes, as those get
377 * callbacks during sync. therefore, force primary process to
378 * do alloc-and-rollback syncs as well.
380 callback_triggered = true;
383 heap->total_size += alloc_sz;
385 RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
386 socket, alloc_sz >> 20ULL);
393 if (callback_triggered)
394 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
397 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
407 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
408 size_t elt_size, int socket, unsigned int flags, size_t align,
409 size_t bound, bool contig)
411 struct malloc_mp_req req;
414 memset(&req, 0, sizeof(req));
416 req.t = REQ_TYPE_ALLOC;
417 req.alloc_req.align = align;
418 req.alloc_req.bound = bound;
419 req.alloc_req.contig = contig;
420 req.alloc_req.flags = flags;
421 req.alloc_req.elt_size = elt_size;
422 req.alloc_req.page_sz = pg_sz;
423 req.alloc_req.socket = socket;
424 req.alloc_req.heap = heap; /* it's in shared memory */
426 req_result = request_to_primary(&req);
431 if (req.result != REQ_RESULT_SUCCESS)
438 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
439 int socket, unsigned int flags, size_t align, size_t bound,
442 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
445 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
447 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
448 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
449 flags, align, bound, contig);
451 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
452 flags, align, bound, contig);
455 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
460 compare_pagesz(const void *a, const void *b)
462 const struct rte_memseg_list * const*mpa = a;
463 const struct rte_memseg_list * const*mpb = b;
464 const struct rte_memseg_list *msla = *mpa;
465 const struct rte_memseg_list *mslb = *mpb;
466 uint64_t pg_sz_a = msla->page_sz;
467 uint64_t pg_sz_b = mslb->page_sz;
469 if (pg_sz_a < pg_sz_b)
471 if (pg_sz_a > pg_sz_b)
477 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
478 unsigned int flags, size_t align, size_t bound, bool contig)
480 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
481 struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
482 struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
483 uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
484 uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
486 int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
487 bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
488 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
491 memset(requested_msls, 0, sizeof(requested_msls));
492 memset(other_msls, 0, sizeof(other_msls));
493 memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
494 memset(other_pg_sz, 0, sizeof(other_pg_sz));
497 * go through memseg list and take note of all the page sizes available,
498 * and if any of them were specifically requested by the user.
500 n_requested_msls = 0;
502 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
503 struct rte_memseg_list *msl = &mcfg->memsegs[i];
505 if (msl->socket_id != socket)
508 if (msl->base_va == NULL)
511 /* if pages of specific size were requested */
512 if (size_flags != 0 && check_hugepage_sz(size_flags,
514 requested_msls[n_requested_msls++] = msl;
515 else if (size_flags == 0 || size_hint)
516 other_msls[n_other_msls++] = msl;
519 /* sort the lists, smallest first */
520 qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
522 qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
525 /* now, extract page sizes we are supposed to try */
527 n_requested_pg_sz = 0;
528 for (i = 0; i < n_requested_msls; i++) {
529 uint64_t pg_sz = requested_msls[i]->page_sz;
531 if (prev_pg_sz != pg_sz) {
532 requested_pg_sz[n_requested_pg_sz++] = pg_sz;
538 for (i = 0; i < n_other_msls; i++) {
539 uint64_t pg_sz = other_msls[i]->page_sz;
541 if (prev_pg_sz != pg_sz) {
542 other_pg_sz[n_other_pg_sz++] = pg_sz;
547 /* finally, try allocating memory of specified page sizes, starting from
550 for (i = 0; i < n_requested_pg_sz; i++) {
551 uint64_t pg_sz = requested_pg_sz[i];
554 * do not pass the size hint here, as user expects other page
555 * sizes first, before resorting to best effort allocation.
557 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
558 align, bound, contig))
561 if (n_other_pg_sz == 0)
564 /* now, check if we can reserve anything with size hint */
565 ret = find_suitable_element(heap, size, flags, align, bound, contig);
570 * we still couldn't reserve memory, so try expanding heap with other
571 * page sizes, if there are any
573 for (i = 0; i < n_other_pg_sz; i++) {
574 uint64_t pg_sz = other_pg_sz[i];
576 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
577 align, bound, contig))
583 /* this will try lower page sizes first */
585 malloc_heap_alloc_on_heap_id(const char *type, size_t size,
586 unsigned int heap_id, unsigned int flags, size_t align,
587 size_t bound, bool contig)
589 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
590 struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
591 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
595 rte_spinlock_lock(&(heap->lock));
597 align = align == 0 ? 1 : align;
599 /* for legacy mode, try once and with all flags */
600 if (internal_config.legacy_mem) {
601 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
606 * we do not pass the size hint here, because even if allocation fails,
607 * we may still be able to allocate memory from appropriate page sizes,
608 * we just need to request more memory first.
611 socket_id = rte_socket_id_by_idx(heap_id);
613 * if socket ID is negative, we cannot find a socket ID for this heap -
614 * which means it's an external heap. those can have unexpected page
615 * sizes, so if the user asked to allocate from there - assume user
616 * knows what they're doing, and allow allocating from there with any
620 size_flags |= RTE_MEMZONE_SIZE_HINT_ONLY;
622 ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
626 /* if socket ID is invalid, this is an external heap */
630 if (!alloc_more_mem_on_socket(heap, size, socket_id, flags, align,
632 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
634 /* this should have succeeded */
636 RTE_LOG(ERR, EAL, "Error allocating from heap\n");
639 rte_spinlock_unlock(&(heap->lock));
644 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
645 unsigned int flags, size_t align, size_t bound, bool contig)
647 int socket, heap_id, i;
650 /* return NULL if size is 0 or alignment is not power-of-2 */
651 if (size == 0 || (align && !rte_is_power_of_2(align)))
654 if (!rte_eal_has_hugepages())
655 socket_arg = SOCKET_ID_ANY;
657 if (socket_arg == SOCKET_ID_ANY)
658 socket = malloc_get_numa_socket();
662 /* turn socket ID into heap ID */
663 heap_id = malloc_socket_to_heap_id(socket);
664 /* if heap id is negative, socket ID was invalid */
668 ret = malloc_heap_alloc_on_heap_id(type, size, heap_id, flags, align,
670 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
673 /* try other heaps. we are only iterating through native DPDK sockets,
674 * so external heaps won't be included.
676 for (i = 0; i < (int) rte_socket_count(); i++) {
679 ret = malloc_heap_alloc_on_heap_id(type, size, i, flags, align,
688 heap_alloc_biggest_on_heap_id(const char *type, unsigned int heap_id,
689 unsigned int flags, size_t align, bool contig)
691 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
692 struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
695 rte_spinlock_lock(&(heap->lock));
697 align = align == 0 ? 1 : align;
699 ret = heap_alloc_biggest(heap, type, flags, align, contig);
701 rte_spinlock_unlock(&(heap->lock));
707 malloc_heap_alloc_biggest(const char *type, int socket_arg, unsigned int flags,
708 size_t align, bool contig)
710 int socket, i, cur_socket, heap_id;
713 /* return NULL if align is not power-of-2 */
714 if ((align && !rte_is_power_of_2(align)))
717 if (!rte_eal_has_hugepages())
718 socket_arg = SOCKET_ID_ANY;
720 if (socket_arg == SOCKET_ID_ANY)
721 socket = malloc_get_numa_socket();
725 /* turn socket ID into heap ID */
726 heap_id = malloc_socket_to_heap_id(socket);
727 /* if heap id is negative, socket ID was invalid */
731 ret = heap_alloc_biggest_on_heap_id(type, heap_id, flags, align,
733 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
736 /* try other heaps */
737 for (i = 0; i < (int) rte_socket_count(); i++) {
738 cur_socket = rte_socket_id_by_idx(i);
739 if (cur_socket == socket)
741 ret = heap_alloc_biggest_on_heap_id(type, i, flags, align,
749 /* this function is exposed in malloc_mp.h */
751 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
753 int n_segs, seg_idx, max_seg_idx;
754 struct rte_memseg_list *msl;
757 msl = rte_mem_virt2memseg_list(aligned_start);
761 page_sz = (size_t)msl->page_sz;
762 n_segs = aligned_len / page_sz;
763 seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
764 max_seg_idx = seg_idx + n_segs;
766 for (; seg_idx < max_seg_idx; seg_idx++) {
767 struct rte_memseg *ms;
769 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
770 eal_memalloc_free_seg(ms);
776 malloc_heap_free(struct malloc_elem *elem)
778 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
779 struct malloc_heap *heap;
780 void *start, *aligned_start, *end, *aligned_end;
781 size_t len, aligned_len, page_sz;
782 struct rte_memseg_list *msl;
783 unsigned int i, n_segs, before_space, after_space;
786 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
789 /* elem may be merged with previous element, so keep heap address */
792 page_sz = (size_t)msl->page_sz;
794 rte_spinlock_lock(&(heap->lock));
796 /* mark element as free */
797 elem->state = ELEM_FREE;
799 elem = malloc_elem_free(elem);
801 /* anything after this is a bonus */
804 /* ...of which we can't avail if we are in legacy mode, or if this is an
805 * externally allocated segment.
807 if (internal_config.legacy_mem || (msl->external > 0))
810 /* check if we can free any memory back to the system */
811 if (elem->size < page_sz)
814 /* probably, but let's make sure, as we may not be using up full page */
817 aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
818 end = RTE_PTR_ADD(elem, len);
819 aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
821 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
823 /* can't free anything */
824 if (aligned_len < page_sz)
827 /* we can free something. however, some of these pages may be marked as
828 * unfreeable, so also check that as well
830 n_segs = aligned_len / page_sz;
831 for (i = 0; i < n_segs; i++) {
832 const struct rte_memseg *tmp =
833 rte_mem_virt2memseg(aligned_start, msl);
835 if (tmp->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
836 /* this is an unfreeable segment, so move start */
837 aligned_start = RTE_PTR_ADD(tmp->addr, tmp->len);
841 /* recalculate length and number of segments */
842 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
843 n_segs = aligned_len / page_sz;
845 /* check if we can still free some pages */
849 /* We're not done yet. We also have to check if by freeing space we will
850 * be leaving free elements that are too small to store new elements.
851 * Check if we have enough space in the beginning and at the end, or if
852 * start/end are exactly page aligned.
854 before_space = RTE_PTR_DIFF(aligned_start, elem);
855 after_space = RTE_PTR_DIFF(end, aligned_end);
856 if (before_space != 0 &&
857 before_space < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
858 /* There is not enough space before start, but we may be able to
859 * move the start forward by one page.
865 aligned_start = RTE_PTR_ADD(aligned_start, page_sz);
866 aligned_len -= page_sz;
869 if (after_space != 0 && after_space <
870 MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
871 /* There is not enough space after end, but we may be able to
872 * move the end backwards by one page.
878 aligned_end = RTE_PTR_SUB(aligned_end, page_sz);
879 aligned_len -= page_sz;
883 /* now we can finally free us some pages */
885 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
888 * we allow secondary processes to clear the heap of this allocated
889 * memory because it is safe to do so, as even if notifications about
890 * unmapped pages don't make it to other processes, heap is shared
891 * across all processes, and will become empty of this memory anyway,
892 * and nothing can allocate it back unless primary process will be able
893 * to deliver allocation message to every single running process.
896 malloc_elem_free_list_remove(elem);
898 malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
900 heap->total_size -= aligned_len;
902 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
903 /* notify user about changes in memory map */
904 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
905 aligned_start, aligned_len);
907 /* don't care if any of this fails */
908 malloc_heap_free_pages(aligned_start, aligned_len);
912 struct malloc_mp_req req;
914 memset(&req, 0, sizeof(req));
916 req.t = REQ_TYPE_FREE;
917 req.free_req.addr = aligned_start;
918 req.free_req.len = aligned_len;
921 * we request primary to deallocate pages, but we don't do it
922 * in this thread. instead, we notify primary that we would like
923 * to deallocate pages, and this process will receive another
924 * request (in parallel) that will do it for us on another
927 * we also don't really care if this succeeds - the data is
928 * already removed from the heap, so it is, for all intents and
929 * purposes, hidden from the rest of DPDK even if some other
930 * process (including this one) may have these pages mapped.
932 * notifications about deallocated memory happen during sync.
934 request_to_primary(&req);
937 RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
938 msl->socket_id, aligned_len >> 20ULL);
940 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
942 rte_spinlock_unlock(&(heap->lock));
947 malloc_heap_resize(struct malloc_elem *elem, size_t size)
951 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
954 rte_spinlock_lock(&(elem->heap->lock));
956 ret = malloc_elem_resize(elem, size);
958 rte_spinlock_unlock(&(elem->heap->lock));
964 * Function to retrieve data for a given heap
967 malloc_heap_get_stats(struct malloc_heap *heap,
968 struct rte_malloc_socket_stats *socket_stats)
971 struct malloc_elem *elem;
973 rte_spinlock_lock(&heap->lock);
975 /* Initialise variables for heap */
976 socket_stats->free_count = 0;
977 socket_stats->heap_freesz_bytes = 0;
978 socket_stats->greatest_free_size = 0;
980 /* Iterate through free list */
981 for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
982 for (elem = LIST_FIRST(&heap->free_head[idx]);
983 !!elem; elem = LIST_NEXT(elem, free_list))
985 socket_stats->free_count++;
986 socket_stats->heap_freesz_bytes += elem->size;
987 if (elem->size > socket_stats->greatest_free_size)
988 socket_stats->greatest_free_size = elem->size;
991 /* Get stats on overall heap and allocated memory on this heap */
992 socket_stats->heap_totalsz_bytes = heap->total_size;
993 socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
994 socket_stats->heap_freesz_bytes);
995 socket_stats->alloc_count = heap->alloc_count;
997 rte_spinlock_unlock(&heap->lock);
1002 * Function to retrieve data for a given heap
1005 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
1007 struct malloc_elem *elem;
1009 rte_spinlock_lock(&heap->lock);
1011 fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
1012 fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
1016 malloc_elem_dump(elem, f);
1020 rte_spinlock_unlock(&heap->lock);
1024 rte_eal_malloc_heap_init(void)
1026 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1028 if (register_mp_requests()) {
1029 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
1030 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
1034 /* unlock mem hotplug here. it's safe for primary as no requests can
1035 * even come before primary itself is fully initialized, and secondaries
1036 * do not need to initialize the heap.
1038 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
1040 /* secondary process does not need to initialize anything */
1041 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1044 /* add all IOVA-contiguous areas to the heap */
1045 return rte_memseg_contig_walk(malloc_add_seg, NULL);