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;
102 found_msl = &mcfg->memsegs[msl_idx];
104 if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
107 malloc_heap_add_memory(heap, found_msl, ms->addr, len);
109 RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
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.
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)
125 struct malloc_elem *elem, *alt_elem = NULL;
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,
133 if (check_hugepage_sz(flags,
136 if (alt_elem == NULL)
142 if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
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.
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)
158 struct malloc_elem *elem;
160 size = RTE_CACHE_LINE_ROUNDUP(size);
161 align = RTE_CACHE_LINE_ROUNDUP(align);
163 elem = find_suitable_element(heap, size, flags, align, bound, contig);
165 elem = malloc_elem_alloc(elem, size, align, bound, contig);
167 /* increase heap's count of allocated elements */
171 return elem == NULL ? NULL : (void *)(&elem[1]);
174 /* this function is exposed in malloc_mp.h */
176 rollback_expand_heap(struct rte_memseg **ms, int n_segs,
177 struct malloc_elem *elem, void *map_addr, size_t map_len)
180 malloc_elem_free_list_remove(elem);
181 malloc_elem_hide_region(elem, map_addr, map_len);
184 eal_memalloc_free_seg_bulk(ms, n_segs);
187 /* this function is exposed in malloc_mp.h */
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)
193 struct rte_memseg_list *msl;
194 struct malloc_elem *elem = NULL;
197 void *ret, *map_addr;
199 allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
202 /* make sure we've allocated our pages... */
203 if (allocd_pages < 0)
206 map_addr = ms[0]->addr;
207 msl = rte_mem_virt2memseg_list(map_addr);
208 alloc_sz = (size_t)msl->page_sz * allocd_pages;
210 /* check if we wanted contiguous memory but didn't get it */
211 if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
212 RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
217 /* add newly minted memsegs to malloc heap */
218 elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
220 /* try once more, as now we have allocated new memory */
221 ret = find_suitable_element(heap, elt_size, flags, align, bound,
230 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
235 try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
236 size_t elt_size, int socket, unsigned int flags, size_t align,
237 size_t bound, bool contig)
239 struct malloc_elem *elem;
240 struct rte_memseg **ms;
244 bool callback_triggered = false;
246 alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
247 MALLOC_ELEM_TRAILER_LEN, pg_sz);
248 n_segs = alloc_sz / pg_sz;
250 /* we can't know in advance how many pages we'll need, so we malloc */
251 ms = malloc(sizeof(*ms) * n_segs);
253 memset(ms, 0, sizeof(*ms) * n_segs);
258 elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
259 bound, contig, ms, n_segs);
264 map_addr = ms[0]->addr;
266 /* notify user about changes in memory map */
267 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
269 /* notify other processes that this has happened */
270 if (request_sync()) {
271 /* we couldn't ensure all processes have mapped memory,
272 * so free it back and notify everyone that it's been
275 * technically, we could've avoided adding memory addresses to
276 * the map, but that would've led to inconsistent behavior
277 * between primary and secondary processes, as those get
278 * callbacks during sync. therefore, force primary process to
279 * do alloc-and-rollback syncs as well.
281 callback_triggered = true;
284 heap->total_size += alloc_sz;
286 RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
287 socket, alloc_sz >> 20ULL);
294 if (callback_triggered)
295 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
298 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
308 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
309 size_t elt_size, int socket, unsigned int flags, size_t align,
310 size_t bound, bool contig)
312 struct malloc_mp_req req;
315 memset(&req, 0, sizeof(req));
317 req.t = REQ_TYPE_ALLOC;
318 req.alloc_req.align = align;
319 req.alloc_req.bound = bound;
320 req.alloc_req.contig = contig;
321 req.alloc_req.flags = flags;
322 req.alloc_req.elt_size = elt_size;
323 req.alloc_req.page_sz = pg_sz;
324 req.alloc_req.socket = socket;
325 req.alloc_req.heap = heap; /* it's in shared memory */
327 req_result = request_to_primary(&req);
332 if (req.result != REQ_RESULT_SUCCESS)
339 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
340 int socket, unsigned int flags, size_t align, size_t bound,
343 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
346 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
348 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
349 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
350 flags, align, bound, contig);
352 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
353 flags, align, bound, contig);
356 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
361 compare_pagesz(const void *a, const void *b)
363 const struct rte_memseg_list * const*mpa = a;
364 const struct rte_memseg_list * const*mpb = b;
365 const struct rte_memseg_list *msla = *mpa;
366 const struct rte_memseg_list *mslb = *mpb;
367 uint64_t pg_sz_a = msla->page_sz;
368 uint64_t pg_sz_b = mslb->page_sz;
370 if (pg_sz_a < pg_sz_b)
372 if (pg_sz_a > pg_sz_b)
378 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
379 unsigned int flags, size_t align, size_t bound, bool contig)
381 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
382 struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
383 struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
384 uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
385 uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
387 int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
388 bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
389 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
392 memset(requested_msls, 0, sizeof(requested_msls));
393 memset(other_msls, 0, sizeof(other_msls));
394 memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
395 memset(other_pg_sz, 0, sizeof(other_pg_sz));
398 * go through memseg list and take note of all the page sizes available,
399 * and if any of them were specifically requested by the user.
401 n_requested_msls = 0;
403 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
404 struct rte_memseg_list *msl = &mcfg->memsegs[i];
406 if (msl->socket_id != socket)
409 if (msl->base_va == NULL)
412 /* if pages of specific size were requested */
413 if (size_flags != 0 && check_hugepage_sz(size_flags,
415 requested_msls[n_requested_msls++] = msl;
416 else if (size_flags == 0 || size_hint)
417 other_msls[n_other_msls++] = msl;
420 /* sort the lists, smallest first */
421 qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
423 qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
426 /* now, extract page sizes we are supposed to try */
428 n_requested_pg_sz = 0;
429 for (i = 0; i < n_requested_msls; i++) {
430 uint64_t pg_sz = requested_msls[i]->page_sz;
432 if (prev_pg_sz != pg_sz) {
433 requested_pg_sz[n_requested_pg_sz++] = pg_sz;
439 for (i = 0; i < n_other_msls; i++) {
440 uint64_t pg_sz = other_msls[i]->page_sz;
442 if (prev_pg_sz != pg_sz) {
443 other_pg_sz[n_other_pg_sz++] = pg_sz;
448 /* finally, try allocating memory of specified page sizes, starting from
451 for (i = 0; i < n_requested_pg_sz; i++) {
452 uint64_t pg_sz = requested_pg_sz[i];
455 * do not pass the size hint here, as user expects other page
456 * sizes first, before resorting to best effort allocation.
458 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
459 align, bound, contig))
462 if (n_other_pg_sz == 0)
465 /* now, check if we can reserve anything with size hint */
466 ret = find_suitable_element(heap, size, flags, align, bound, contig);
471 * we still couldn't reserve memory, so try expanding heap with other
472 * page sizes, if there are any
474 for (i = 0; i < n_other_pg_sz; i++) {
475 uint64_t pg_sz = other_pg_sz[i];
477 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
478 align, bound, contig))
484 /* this will try lower page sizes first */
486 heap_alloc_on_socket(const char *type, size_t size, int socket,
487 unsigned int flags, size_t align, size_t bound, bool contig)
489 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
490 struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
491 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
494 rte_spinlock_lock(&(heap->lock));
496 align = align == 0 ? 1 : align;
498 /* for legacy mode, try once and with all flags */
499 if (internal_config.legacy_mem) {
500 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
505 * we do not pass the size hint here, because even if allocation fails,
506 * we may still be able to allocate memory from appropriate page sizes,
507 * we just need to request more memory first.
509 ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
513 if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
515 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
517 /* this should have succeeded */
519 RTE_LOG(ERR, EAL, "Error allocating from heap\n");
522 rte_spinlock_unlock(&(heap->lock));
527 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
528 unsigned int flags, size_t align, size_t bound, bool contig)
530 int socket, i, cur_socket;
533 /* return NULL if size is 0 or alignment is not power-of-2 */
534 if (size == 0 || (align && !rte_is_power_of_2(align)))
537 if (!rte_eal_has_hugepages())
538 socket_arg = SOCKET_ID_ANY;
540 if (socket_arg == SOCKET_ID_ANY)
541 socket = malloc_get_numa_socket();
545 /* Check socket parameter */
546 if (socket >= RTE_MAX_NUMA_NODES)
549 ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
551 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
554 /* try other heaps */
555 for (i = 0; i < (int) rte_socket_count(); i++) {
556 cur_socket = rte_socket_id_by_idx(i);
557 if (cur_socket == socket)
559 ret = heap_alloc_on_socket(type, size, cur_socket, flags,
560 align, bound, contig);
567 /* this function is exposed in malloc_mp.h */
569 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
571 int n_segs, seg_idx, max_seg_idx;
572 struct rte_memseg_list *msl;
575 msl = rte_mem_virt2memseg_list(aligned_start);
579 page_sz = (size_t)msl->page_sz;
580 n_segs = aligned_len / page_sz;
581 seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
582 max_seg_idx = seg_idx + n_segs;
584 for (; seg_idx < max_seg_idx; seg_idx++) {
585 struct rte_memseg *ms;
587 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
588 eal_memalloc_free_seg(ms);
594 malloc_heap_free(struct malloc_elem *elem)
596 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
597 struct malloc_heap *heap;
598 void *start, *aligned_start, *end, *aligned_end;
599 size_t len, aligned_len, page_sz;
600 struct rte_memseg_list *msl;
603 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
606 /* elem may be merged with previous element, so keep heap address */
609 page_sz = (size_t)msl->page_sz;
611 rte_spinlock_lock(&(heap->lock));
613 /* mark element as free */
614 elem->state = ELEM_FREE;
616 elem = malloc_elem_free(elem);
618 /* anything after this is a bonus */
621 /* ...of which we can't avail if we are in legacy mode */
622 if (internal_config.legacy_mem)
625 /* check if we can free any memory back to the system */
626 if (elem->size < page_sz)
629 /* probably, but let's make sure, as we may not be using up full page */
632 aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
633 end = RTE_PTR_ADD(elem, len);
634 aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
636 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
638 /* can't free anything */
639 if (aligned_len < page_sz)
642 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
645 * we allow secondary processes to clear the heap of this allocated
646 * memory because it is safe to do so, as even if notifications about
647 * unmapped pages don't make it to other processes, heap is shared
648 * across all processes, and will become empty of this memory anyway,
649 * and nothing can allocate it back unless primary process will be able
650 * to deliver allocation message to every single running process.
653 malloc_elem_free_list_remove(elem);
655 malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
657 heap->total_size -= aligned_len;
659 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
660 /* notify user about changes in memory map */
661 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
662 aligned_start, aligned_len);
664 /* don't care if any of this fails */
665 malloc_heap_free_pages(aligned_start, aligned_len);
669 struct malloc_mp_req req;
671 memset(&req, 0, sizeof(req));
673 req.t = REQ_TYPE_FREE;
674 req.free_req.addr = aligned_start;
675 req.free_req.len = aligned_len;
678 * we request primary to deallocate pages, but we don't do it
679 * in this thread. instead, we notify primary that we would like
680 * to deallocate pages, and this process will receive another
681 * request (in parallel) that will do it for us on another
684 * we also don't really care if this succeeds - the data is
685 * already removed from the heap, so it is, for all intents and
686 * purposes, hidden from the rest of DPDK even if some other
687 * process (including this one) may have these pages mapped.
689 * notifications about deallocated memory happen during sync.
691 request_to_primary(&req);
694 RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
695 msl->socket_id, aligned_len >> 20ULL);
697 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
699 rte_spinlock_unlock(&(heap->lock));
704 malloc_heap_resize(struct malloc_elem *elem, size_t size)
708 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
711 rte_spinlock_lock(&(elem->heap->lock));
713 ret = malloc_elem_resize(elem, size);
715 rte_spinlock_unlock(&(elem->heap->lock));
721 * Function to retrieve data for heap on given socket
724 malloc_heap_get_stats(struct malloc_heap *heap,
725 struct rte_malloc_socket_stats *socket_stats)
728 struct malloc_elem *elem;
730 rte_spinlock_lock(&heap->lock);
732 /* Initialise variables for heap */
733 socket_stats->free_count = 0;
734 socket_stats->heap_freesz_bytes = 0;
735 socket_stats->greatest_free_size = 0;
737 /* Iterate through free list */
738 for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
739 for (elem = LIST_FIRST(&heap->free_head[idx]);
740 !!elem; elem = LIST_NEXT(elem, free_list))
742 socket_stats->free_count++;
743 socket_stats->heap_freesz_bytes += elem->size;
744 if (elem->size > socket_stats->greatest_free_size)
745 socket_stats->greatest_free_size = elem->size;
748 /* Get stats on overall heap and allocated memory on this heap */
749 socket_stats->heap_totalsz_bytes = heap->total_size;
750 socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
751 socket_stats->heap_freesz_bytes);
752 socket_stats->alloc_count = heap->alloc_count;
754 rte_spinlock_unlock(&heap->lock);
759 * Function to retrieve data for heap on given socket
762 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
764 struct malloc_elem *elem;
766 rte_spinlock_lock(&heap->lock);
768 fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
769 fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
773 malloc_elem_dump(elem, f);
777 rte_spinlock_unlock(&heap->lock);
781 rte_eal_malloc_heap_init(void)
783 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
785 if (register_mp_requests()) {
786 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
790 /* unlock mem hotplug here. it's safe for primary as no requests can
791 * even come before primary itself is fully initialized, and secondaries
792 * do not need to initialize the heap.
794 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
796 /* secondary process does not need to initialize anything */
797 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
800 /* add all IOVA-contiguous areas to the heap */
801 return rte_memseg_contig_walk(malloc_add_seg, NULL);