1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/queue.h>
12 #include <rte_memory.h>
14 #include <rte_launch.h>
15 #include <rte_per_lcore.h>
16 #include <rte_lcore.h>
17 #include <rte_debug.h>
18 #include <rte_common.h>
19 #include <rte_spinlock.h>
21 #include "eal_internal_cfg.h"
22 #include "eal_memalloc.h"
23 #include "malloc_elem.h"
24 #include "malloc_heap.h"
27 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align)
29 void *cur_page, *contig_seg_start, *page_end, *cur_seg_end;
30 void *data_start, *data_end;
31 rte_iova_t expected_iova;
32 struct rte_memseg *ms;
33 size_t page_sz, cur, max;
35 page_sz = (size_t)elem->msl->page_sz;
36 data_start = RTE_PTR_ADD(elem, MALLOC_ELEM_HEADER_LEN);
37 data_end = RTE_PTR_ADD(elem, elem->size - MALLOC_ELEM_TRAILER_LEN);
38 /* segment must start after header and with specified alignment */
39 contig_seg_start = RTE_PTR_ALIGN_CEIL(data_start, align);
41 /* if we're in IOVA as VA mode, or if we're in legacy mode with
42 * hugepages, all elements are IOVA-contiguous. however, we can only
43 * make these assumptions about internal memory - externally allocated
44 * segments have to be checked.
46 if (!elem->msl->external &&
47 (rte_eal_iova_mode() == RTE_IOVA_VA ||
48 (internal_config.legacy_mem &&
49 rte_eal_has_hugepages())))
50 return RTE_PTR_DIFF(data_end, contig_seg_start);
52 cur_page = RTE_PTR_ALIGN_FLOOR(contig_seg_start, page_sz);
53 ms = rte_mem_virt2memseg(cur_page, elem->msl);
55 /* do first iteration outside the loop */
56 page_end = RTE_PTR_ADD(cur_page, page_sz);
57 cur_seg_end = RTE_MIN(page_end, data_end);
58 cur = RTE_PTR_DIFF(cur_seg_end, contig_seg_start) -
59 MALLOC_ELEM_TRAILER_LEN;
61 expected_iova = ms->iova + page_sz;
62 /* memsegs are contiguous in memory */
65 cur_page = RTE_PTR_ADD(cur_page, page_sz);
67 while (cur_page < data_end) {
68 page_end = RTE_PTR_ADD(cur_page, page_sz);
69 cur_seg_end = RTE_MIN(page_end, data_end);
71 /* reset start of contiguous segment if unexpected iova */
72 if (ms->iova != expected_iova) {
73 /* next contiguous segment must start at specified
76 contig_seg_start = RTE_PTR_ALIGN(cur_page, align);
77 /* new segment start may be on a different page, so find
78 * the page and skip to next iteration to make sure
79 * we're not blowing past data end.
81 ms = rte_mem_virt2memseg(contig_seg_start, elem->msl);
83 /* don't trigger another recalculation */
84 expected_iova = ms->iova;
87 /* cur_seg_end ends on a page boundary or on data end. if we're
88 * looking at data end, then malloc trailer is already included
89 * in the calculations. if we're looking at page end, then we
90 * know there's more data past this page and thus there's space
91 * for malloc element trailer, so don't count it here.
93 cur = RTE_PTR_DIFF(cur_seg_end, contig_seg_start);
94 /* update max if cur value is bigger */
98 /* move to next page */
100 expected_iova = ms->iova + page_sz;
101 /* memsegs are contiguous in memory */
109 * Initialize a general malloc_elem header structure
112 malloc_elem_init(struct malloc_elem *elem, struct malloc_heap *heap,
113 struct rte_memseg_list *msl, size_t size)
119 memset(&elem->free_list, 0, sizeof(elem->free_list));
120 elem->state = ELEM_FREE;
128 malloc_elem_insert(struct malloc_elem *elem)
130 struct malloc_elem *prev_elem, *next_elem;
131 struct malloc_heap *heap = elem->heap;
133 /* first and last elements must be both NULL or both non-NULL */
134 if ((heap->first == NULL) != (heap->last == NULL)) {
135 RTE_LOG(ERR, EAL, "Heap is probably corrupt\n");
139 if (heap->first == NULL && heap->last == NULL) {
145 } else if (elem < heap->first) {
146 /* if lower than start */
148 next_elem = heap->first;
150 } else if (elem > heap->last) {
151 /* if higher than end */
152 prev_elem = heap->last;
156 /* the new memory is somewhere inbetween start and end */
157 uint64_t dist_from_start, dist_from_end;
159 dist_from_end = RTE_PTR_DIFF(heap->last, elem);
160 dist_from_start = RTE_PTR_DIFF(elem, heap->first);
162 /* check which is closer, and find closest list entries */
163 if (dist_from_start < dist_from_end) {
164 prev_elem = heap->first;
165 while (prev_elem->next < elem)
166 prev_elem = prev_elem->next;
167 next_elem = prev_elem->next;
169 next_elem = heap->last;
170 while (next_elem->prev > elem)
171 next_elem = next_elem->prev;
172 prev_elem = next_elem->prev;
176 /* insert new element */
177 elem->prev = prev_elem;
178 elem->next = next_elem;
180 prev_elem->next = elem;
182 next_elem->prev = elem;
186 * Attempt to find enough physically contiguous memory in this block to store
187 * our data. Assume that element has at least enough space to fit in the data,
188 * so we just check the page addresses.
191 elem_check_phys_contig(const struct rte_memseg_list *msl,
192 void *start, size_t size)
194 return eal_memalloc_is_contig(msl, start, size);
198 * calculate the starting point of where data of the requested size
199 * and alignment would fit in the current element. If the data doesn't
203 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
204 size_t bound, bool contig)
206 size_t elem_size = elem->size;
209 * we're allocating from the end, so adjust the size of element by
212 while (elem_size >= size) {
213 const size_t bmask = ~(bound - 1);
214 uintptr_t end_pt = (uintptr_t)elem +
215 elem_size - MALLOC_ELEM_TRAILER_LEN;
216 uintptr_t new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
218 uintptr_t new_elem_start;
221 if ((new_data_start & bmask) != ((end_pt - 1) & bmask)) {
222 end_pt = RTE_ALIGN_FLOOR(end_pt, bound);
223 new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
225 end_pt = new_data_start + size;
227 if (((end_pt - 1) & bmask) != (new_data_start & bmask))
231 new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
233 /* if the new start point is before the exist start,
236 if (new_elem_start < (uintptr_t)elem)
240 size_t new_data_size = end_pt - new_data_start;
243 * if physical contiguousness was requested and we
244 * couldn't fit all data into one physically contiguous
245 * block, try again with lower addresses.
247 if (!elem_check_phys_contig(elem->msl,
248 (void *)new_data_start,
254 return (void *)new_elem_start;
260 * use elem_start_pt to determine if we get meet the size and
261 * alignment request from the current element
264 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align,
265 size_t bound, bool contig)
267 return elem_start_pt(elem, size, align, bound, contig) != NULL;
271 * split an existing element into two smaller elements at the given
272 * split_pt parameter.
275 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
277 struct malloc_elem *next_elem = elem->next;
278 const size_t old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
279 const size_t new_elem_size = elem->size - old_elem_size;
281 malloc_elem_init(split_pt, elem->heap, elem->msl, new_elem_size);
282 split_pt->prev = elem;
283 split_pt->next = next_elem;
285 next_elem->prev = split_pt;
287 elem->heap->last = split_pt;
288 elem->next = split_pt;
289 elem->size = old_elem_size;
294 * our malloc heap is a doubly linked list, so doubly remove our element.
296 static void __rte_unused
297 remove_elem(struct malloc_elem *elem)
299 struct malloc_elem *next, *prev;
306 elem->heap->last = prev;
310 elem->heap->first = next;
317 next_elem_is_adjacent(struct malloc_elem *elem)
319 return elem->next == RTE_PTR_ADD(elem, elem->size) &&
320 elem->next->msl == elem->msl;
324 prev_elem_is_adjacent(struct malloc_elem *elem)
326 return elem == RTE_PTR_ADD(elem->prev, elem->prev->size) &&
327 elem->prev->msl == elem->msl;
331 * Given an element size, compute its freelist index.
332 * We free an element into the freelist containing similarly-sized elements.
333 * We try to allocate elements starting with the freelist containing
334 * similarly-sized elements, and if necessary, we search freelists
335 * containing larger elements.
337 * Example element size ranges for a heap with five free lists:
338 * heap->free_head[0] - (0 , 2^8]
339 * heap->free_head[1] - (2^8 , 2^10]
340 * heap->free_head[2] - (2^10 ,2^12]
341 * heap->free_head[3] - (2^12, 2^14]
342 * heap->free_head[4] - (2^14, MAX_SIZE]
345 malloc_elem_free_list_index(size_t size)
347 #define MALLOC_MINSIZE_LOG2 8
348 #define MALLOC_LOG2_INCREMENT 2
353 if (size <= (1UL << MALLOC_MINSIZE_LOG2))
356 /* Find next power of 2 >= size. */
357 log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
359 /* Compute freelist index, based on log2(size). */
360 index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
361 MALLOC_LOG2_INCREMENT;
363 return index <= RTE_HEAP_NUM_FREELISTS-1?
364 index: RTE_HEAP_NUM_FREELISTS-1;
368 * Add the specified element to its heap's free list.
371 malloc_elem_free_list_insert(struct malloc_elem *elem)
375 idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
376 elem->state = ELEM_FREE;
377 LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
381 * Remove the specified element from its heap's free list.
384 malloc_elem_free_list_remove(struct malloc_elem *elem)
386 LIST_REMOVE(elem, free_list);
390 * reserve a block of data in an existing malloc_elem. If the malloc_elem
391 * is much larger than the data block requested, we split the element in two.
392 * This function is only called from malloc_heap_alloc so parameter checking
393 * is not done here, as it's done there previously.
396 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
397 size_t bound, bool contig)
399 struct malloc_elem *new_elem = elem_start_pt(elem, size, align, bound,
401 const size_t old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
402 const size_t trailer_size = elem->size - old_elem_size - size -
403 MALLOC_ELEM_OVERHEAD;
405 malloc_elem_free_list_remove(elem);
407 if (trailer_size > MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
408 /* split it, too much free space after elem */
409 struct malloc_elem *new_free_elem =
410 RTE_PTR_ADD(new_elem, size + MALLOC_ELEM_OVERHEAD);
412 split_elem(elem, new_free_elem);
413 malloc_elem_free_list_insert(new_free_elem);
415 if (elem == elem->heap->last)
416 elem->heap->last = new_free_elem;
419 if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
420 /* don't split it, pad the element instead */
421 elem->state = ELEM_BUSY;
422 elem->pad = old_elem_size;
424 /* put a dummy header in padding, to point to real element header */
425 if (elem->pad > 0) { /* pad will be at least 64-bytes, as everything
426 * is cache-line aligned */
427 new_elem->pad = elem->pad;
428 new_elem->state = ELEM_PAD;
429 new_elem->size = elem->size - elem->pad;
430 set_header(new_elem);
436 /* we are going to split the element in two. The original element
437 * remains free, and the new element is the one allocated.
438 * Re-insert original element, in case its new size makes it
439 * belong on a different list.
441 split_elem(elem, new_elem);
442 new_elem->state = ELEM_BUSY;
443 malloc_elem_free_list_insert(elem);
449 * join two struct malloc_elem together. elem1 and elem2 must
450 * be contiguous in memory.
453 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
455 struct malloc_elem *next = elem2->next;
456 elem1->size += elem2->size;
460 elem1->heap->last = elem1;
465 malloc_elem_join_adjacent_free(struct malloc_elem *elem)
468 * check if next element exists, is adjacent and is free, if so join
469 * with it, need to remove from free list.
471 if (elem->next != NULL && elem->next->state == ELEM_FREE &&
472 next_elem_is_adjacent(elem)) {
476 /* we will want to erase the trailer and header */
477 erase = RTE_PTR_SUB(elem->next, MALLOC_ELEM_TRAILER_LEN);
478 erase_len = MALLOC_ELEM_OVERHEAD + elem->next->pad;
480 /* remove from free list, join to this one */
481 malloc_elem_free_list_remove(elem->next);
482 join_elem(elem, elem->next);
484 /* erase header, trailer and pad */
485 memset(erase, 0, erase_len);
489 * check if prev element exists, is adjacent and is free, if so join
490 * with it, need to remove from free list.
492 if (elem->prev != NULL && elem->prev->state == ELEM_FREE &&
493 prev_elem_is_adjacent(elem)) {
494 struct malloc_elem *new_elem;
498 /* we will want to erase trailer and header */
499 erase = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
500 erase_len = MALLOC_ELEM_OVERHEAD + elem->pad;
502 /* remove from free list, join to this one */
503 malloc_elem_free_list_remove(elem->prev);
505 new_elem = elem->prev;
506 join_elem(new_elem, elem);
508 /* erase header, trailer and pad */
509 memset(erase, 0, erase_len);
518 * free a malloc_elem block by adding it to the free list. If the
519 * blocks either immediately before or immediately after newly freed block
520 * are also free, the blocks are merged together.
523 malloc_elem_free(struct malloc_elem *elem)
528 ptr = RTE_PTR_ADD(elem, MALLOC_ELEM_HEADER_LEN);
529 data_len = elem->size - MALLOC_ELEM_OVERHEAD;
531 elem = malloc_elem_join_adjacent_free(elem);
533 malloc_elem_free_list_insert(elem);
537 /* decrease heap's count of allocated elements */
538 elem->heap->alloc_count--;
540 memset(ptr, 0, data_len);
545 /* assume all checks were already done */
547 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)
549 struct malloc_elem *hide_start, *hide_end, *prev, *next;
550 size_t len_before, len_after;
553 hide_end = RTE_PTR_ADD(start, len);
558 /* we cannot do anything with non-adjacent elements */
559 if (next && next_elem_is_adjacent(elem)) {
560 len_after = RTE_PTR_DIFF(next, hide_end);
561 if (len_after >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
563 split_elem(elem, hide_end);
565 malloc_elem_free_list_insert(hide_end);
566 } else if (len_after > 0) {
567 RTE_LOG(ERR, EAL, "Unaligned element, heap is probably corrupt\n");
572 /* we cannot do anything with non-adjacent elements */
573 if (prev && prev_elem_is_adjacent(elem)) {
574 len_before = RTE_PTR_DIFF(hide_start, elem);
575 if (len_before >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
577 split_elem(elem, hide_start);
582 malloc_elem_free_list_insert(prev);
583 } else if (len_before > 0) {
584 RTE_LOG(ERR, EAL, "Unaligned element, heap is probably corrupt\n");
593 * attempt to resize a malloc_elem by expanding into any free space
594 * immediately after it in memory.
597 malloc_elem_resize(struct malloc_elem *elem, size_t size)
599 const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
601 /* if we request a smaller size, then always return ok */
602 if (elem->size >= new_size)
605 /* check if there is a next element, it's free and adjacent */
606 if (!elem->next || elem->next->state != ELEM_FREE ||
607 !next_elem_is_adjacent(elem))
609 if (elem->size + elem->next->size < new_size)
612 /* we now know the element fits, so remove from free list,
615 malloc_elem_free_list_remove(elem->next);
616 join_elem(elem, elem->next);
618 if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
619 /* now we have a big block together. Lets cut it down a bit, by splitting */
620 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
621 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
622 split_elem(elem, split_pt);
623 malloc_elem_free_list_insert(split_pt);
628 static inline const char *
629 elem_state_to_str(enum elem_state state)
643 malloc_elem_dump(const struct malloc_elem *elem, FILE *f)
645 fprintf(f, "Malloc element at %p (%s)\n", elem,
646 elem_state_to_str(elem->state));
647 fprintf(f, " len: 0x%zx pad: 0x%" PRIx32 "\n", elem->size, elem->pad);
648 fprintf(f, " prev: %p next: %p\n", elem->prev, elem->next);