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;
245 alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
246 MALLOC_ELEM_TRAILER_LEN, pg_sz);
247 n_segs = alloc_sz / pg_sz;
249 /* we can't know in advance how many pages we'll need, so we malloc */
250 ms = malloc(sizeof(*ms) * n_segs);
252 memset(ms, 0, sizeof(*ms) * n_segs);
257 elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
258 bound, contig, ms, n_segs);
263 map_addr = ms[0]->addr;
265 /* notify other processes that this has happened */
266 if (request_sync()) {
267 /* we couldn't ensure all processes have mapped memory,
268 * so free it back and notify everyone that it's been
273 heap->total_size += alloc_sz;
275 RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
276 socket, alloc_sz >> 20ULL);
283 rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
293 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
294 size_t elt_size, int socket, unsigned int flags, size_t align,
295 size_t bound, bool contig)
297 struct malloc_mp_req req;
300 memset(&req, 0, sizeof(req));
302 req.t = REQ_TYPE_ALLOC;
303 req.alloc_req.align = align;
304 req.alloc_req.bound = bound;
305 req.alloc_req.contig = contig;
306 req.alloc_req.flags = flags;
307 req.alloc_req.elt_size = elt_size;
308 req.alloc_req.page_sz = pg_sz;
309 req.alloc_req.socket = socket;
310 req.alloc_req.heap = heap; /* it's in shared memory */
312 req_result = request_to_primary(&req);
317 if (req.result != REQ_RESULT_SUCCESS)
324 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
325 int socket, unsigned int flags, size_t align, size_t bound,
328 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
331 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
333 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
334 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
335 flags, align, bound, contig);
337 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
338 flags, align, bound, contig);
341 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
346 compare_pagesz(const void *a, const void *b)
348 const struct rte_memseg_list * const*mpa = a;
349 const struct rte_memseg_list * const*mpb = b;
350 const struct rte_memseg_list *msla = *mpa;
351 const struct rte_memseg_list *mslb = *mpb;
352 uint64_t pg_sz_a = msla->page_sz;
353 uint64_t pg_sz_b = mslb->page_sz;
355 if (pg_sz_a < pg_sz_b)
357 if (pg_sz_a > pg_sz_b)
363 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
364 unsigned int flags, size_t align, size_t bound, bool contig)
366 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
367 struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
368 struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
369 uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
370 uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
372 int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
373 bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
374 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
377 memset(requested_msls, 0, sizeof(requested_msls));
378 memset(other_msls, 0, sizeof(other_msls));
379 memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
380 memset(other_pg_sz, 0, sizeof(other_pg_sz));
383 * go through memseg list and take note of all the page sizes available,
384 * and if any of them were specifically requested by the user.
386 n_requested_msls = 0;
388 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
389 struct rte_memseg_list *msl = &mcfg->memsegs[i];
391 if (msl->socket_id != socket)
394 if (msl->base_va == NULL)
397 /* if pages of specific size were requested */
398 if (size_flags != 0 && check_hugepage_sz(size_flags,
400 requested_msls[n_requested_msls++] = msl;
401 else if (size_flags == 0 || size_hint)
402 other_msls[n_other_msls++] = msl;
405 /* sort the lists, smallest first */
406 qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
408 qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
411 /* now, extract page sizes we are supposed to try */
413 n_requested_pg_sz = 0;
414 for (i = 0; i < n_requested_msls; i++) {
415 uint64_t pg_sz = requested_msls[i]->page_sz;
417 if (prev_pg_sz != pg_sz) {
418 requested_pg_sz[n_requested_pg_sz++] = pg_sz;
424 for (i = 0; i < n_other_msls; i++) {
425 uint64_t pg_sz = other_msls[i]->page_sz;
427 if (prev_pg_sz != pg_sz) {
428 other_pg_sz[n_other_pg_sz++] = pg_sz;
433 /* finally, try allocating memory of specified page sizes, starting from
436 for (i = 0; i < n_requested_pg_sz; i++) {
437 uint64_t pg_sz = requested_pg_sz[i];
440 * do not pass the size hint here, as user expects other page
441 * sizes first, before resorting to best effort allocation.
443 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
444 align, bound, contig))
447 if (n_other_pg_sz == 0)
450 /* now, check if we can reserve anything with size hint */
451 ret = find_suitable_element(heap, size, flags, align, bound, contig);
456 * we still couldn't reserve memory, so try expanding heap with other
457 * page sizes, if there are any
459 for (i = 0; i < n_other_pg_sz; i++) {
460 uint64_t pg_sz = other_pg_sz[i];
462 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
463 align, bound, contig))
469 /* this will try lower page sizes first */
471 heap_alloc_on_socket(const char *type, size_t size, int socket,
472 unsigned int flags, size_t align, size_t bound, bool contig)
474 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
475 struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
476 unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
479 rte_spinlock_lock(&(heap->lock));
481 align = align == 0 ? 1 : align;
483 /* for legacy mode, try once and with all flags */
484 if (internal_config.legacy_mem) {
485 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
490 * we do not pass the size hint here, because even if allocation fails,
491 * we may still be able to allocate memory from appropriate page sizes,
492 * we just need to request more memory first.
494 ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
498 if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
500 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
502 /* this should have succeeded */
504 RTE_LOG(ERR, EAL, "Error allocating from heap\n");
507 rte_spinlock_unlock(&(heap->lock));
512 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
513 unsigned int flags, size_t align, size_t bound, bool contig)
515 int socket, i, cur_socket;
518 /* return NULL if size is 0 or alignment is not power-of-2 */
519 if (size == 0 || (align && !rte_is_power_of_2(align)))
522 if (!rte_eal_has_hugepages())
523 socket_arg = SOCKET_ID_ANY;
525 if (socket_arg == SOCKET_ID_ANY)
526 socket = malloc_get_numa_socket();
530 /* Check socket parameter */
531 if (socket >= RTE_MAX_NUMA_NODES)
534 ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
536 if (ret != NULL || socket_arg != SOCKET_ID_ANY)
539 /* try other heaps */
540 for (i = 0; i < (int) rte_socket_count(); i++) {
541 cur_socket = rte_socket_id_by_idx(i);
542 if (cur_socket == socket)
544 ret = heap_alloc_on_socket(type, size, cur_socket, flags,
545 align, bound, contig);
552 /* this function is exposed in malloc_mp.h */
554 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
556 int n_segs, seg_idx, max_seg_idx;
557 struct rte_memseg_list *msl;
560 msl = rte_mem_virt2memseg_list(aligned_start);
564 page_sz = (size_t)msl->page_sz;
565 n_segs = aligned_len / page_sz;
566 seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
567 max_seg_idx = seg_idx + n_segs;
569 for (; seg_idx < max_seg_idx; seg_idx++) {
570 struct rte_memseg *ms;
572 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
573 eal_memalloc_free_seg(ms);
579 malloc_heap_free(struct malloc_elem *elem)
581 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
582 struct malloc_heap *heap;
583 void *start, *aligned_start, *end, *aligned_end;
584 size_t len, aligned_len, page_sz;
585 struct rte_memseg_list *msl;
588 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
591 /* elem may be merged with previous element, so keep heap address */
594 page_sz = (size_t)msl->page_sz;
596 rte_spinlock_lock(&(heap->lock));
598 /* mark element as free */
599 elem->state = ELEM_FREE;
601 elem = malloc_elem_free(elem);
603 /* anything after this is a bonus */
606 /* ...of which we can't avail if we are in legacy mode */
607 if (internal_config.legacy_mem)
610 /* check if we can free any memory back to the system */
611 if (elem->size < page_sz)
614 /* probably, but let's make sure, as we may not be using up full page */
617 aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
618 end = RTE_PTR_ADD(elem, len);
619 aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
621 aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
623 /* can't free anything */
624 if (aligned_len < page_sz)
627 rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
630 * we allow secondary processes to clear the heap of this allocated
631 * memory because it is safe to do so, as even if notifications about
632 * unmapped pages don't make it to other processes, heap is shared
633 * across all processes, and will become empty of this memory anyway,
634 * and nothing can allocate it back unless primary process will be able
635 * to deliver allocation message to every single running process.
638 malloc_elem_free_list_remove(elem);
640 malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
642 heap->total_size -= aligned_len;
644 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
645 /* don't care if any of this fails */
646 malloc_heap_free_pages(aligned_start, aligned_len);
650 struct malloc_mp_req req;
652 memset(&req, 0, sizeof(req));
654 req.t = REQ_TYPE_FREE;
655 req.free_req.addr = aligned_start;
656 req.free_req.len = aligned_len;
659 * we request primary to deallocate pages, but we don't do it
660 * in this thread. instead, we notify primary that we would like
661 * to deallocate pages, and this process will receive another
662 * request (in parallel) that will do it for us on another
665 * we also don't really care if this succeeds - the data is
666 * already removed from the heap, so it is, for all intents and
667 * purposes, hidden from the rest of DPDK even if some other
668 * process (including this one) may have these pages mapped.
670 request_to_primary(&req);
673 RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
674 msl->socket_id, aligned_len >> 20ULL);
676 rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
678 rte_spinlock_unlock(&(heap->lock));
683 malloc_heap_resize(struct malloc_elem *elem, size_t size)
687 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
690 rte_spinlock_lock(&(elem->heap->lock));
692 ret = malloc_elem_resize(elem, size);
694 rte_spinlock_unlock(&(elem->heap->lock));
700 * Function to retrieve data for heap on given socket
703 malloc_heap_get_stats(struct malloc_heap *heap,
704 struct rte_malloc_socket_stats *socket_stats)
707 struct malloc_elem *elem;
709 rte_spinlock_lock(&heap->lock);
711 /* Initialise variables for heap */
712 socket_stats->free_count = 0;
713 socket_stats->heap_freesz_bytes = 0;
714 socket_stats->greatest_free_size = 0;
716 /* Iterate through free list */
717 for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
718 for (elem = LIST_FIRST(&heap->free_head[idx]);
719 !!elem; elem = LIST_NEXT(elem, free_list))
721 socket_stats->free_count++;
722 socket_stats->heap_freesz_bytes += elem->size;
723 if (elem->size > socket_stats->greatest_free_size)
724 socket_stats->greatest_free_size = elem->size;
727 /* Get stats on overall heap and allocated memory on this heap */
728 socket_stats->heap_totalsz_bytes = heap->total_size;
729 socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
730 socket_stats->heap_freesz_bytes);
731 socket_stats->alloc_count = heap->alloc_count;
733 rte_spinlock_unlock(&heap->lock);
738 * Function to retrieve data for heap on given socket
741 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
743 struct malloc_elem *elem;
745 rte_spinlock_lock(&heap->lock);
747 fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
748 fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
752 malloc_elem_dump(elem, f);
756 rte_spinlock_unlock(&heap->lock);
760 rte_eal_malloc_heap_init(void)
762 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
764 if (register_mp_requests()) {
765 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
769 /* unlock mem hotplug here. it's safe for primary as no requests can
770 * even come before primary itself is fully initialized, and secondaries
771 * do not need to initialize the heap.
773 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
775 /* secondary process does not need to initialize anything */
776 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
779 /* add all IOVA-contiguous areas to the heap */
780 return rte_memseg_contig_walk(malloc_add_seg, NULL);