199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson #include <inttypes.h>
599a2dd95SBruce Richardson #include <stdint.h>
699a2dd95SBruce Richardson #include <stddef.h>
799a2dd95SBruce Richardson #include <stdio.h>
899a2dd95SBruce Richardson #include <string.h>
999a2dd95SBruce Richardson #include <sys/queue.h>
1099a2dd95SBruce Richardson
1199a2dd95SBruce Richardson #include <rte_memory.h>
1299a2dd95SBruce Richardson #include <rte_eal.h>
1399a2dd95SBruce Richardson #include <rte_common.h>
1499a2dd95SBruce Richardson
1599a2dd95SBruce Richardson #include "eal_private.h"
1699a2dd95SBruce Richardson #include "eal_internal_cfg.h"
1799a2dd95SBruce Richardson #include "eal_memalloc.h"
1899a2dd95SBruce Richardson #include "malloc_elem.h"
1999a2dd95SBruce Richardson #include "malloc_heap.h"
2099a2dd95SBruce Richardson
2199a2dd95SBruce Richardson /*
2299a2dd95SBruce Richardson * If debugging is enabled, freed memory is set to poison value
2399a2dd95SBruce Richardson * to catch buggy programs. Otherwise, freed memory is set to zero
2499a2dd95SBruce Richardson * to avoid having to zero in zmalloc
2599a2dd95SBruce Richardson */
2699a2dd95SBruce Richardson #ifdef RTE_MALLOC_DEBUG
2799a2dd95SBruce Richardson #define MALLOC_POISON 0x6b
2899a2dd95SBruce Richardson #else
2999a2dd95SBruce Richardson #define MALLOC_POISON 0
3099a2dd95SBruce Richardson #endif
3199a2dd95SBruce Richardson
3299a2dd95SBruce Richardson size_t
malloc_elem_find_max_iova_contig(struct malloc_elem * elem,size_t align)3399a2dd95SBruce Richardson malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align)
3499a2dd95SBruce Richardson {
3599a2dd95SBruce Richardson void *cur_page, *contig_seg_start, *page_end, *cur_seg_end;
3699a2dd95SBruce Richardson void *data_start, *data_end;
3799a2dd95SBruce Richardson rte_iova_t expected_iova;
3899a2dd95SBruce Richardson struct rte_memseg *ms;
3999a2dd95SBruce Richardson size_t page_sz, cur, max;
4099a2dd95SBruce Richardson const struct internal_config *internal_conf =
4199a2dd95SBruce Richardson eal_get_internal_configuration();
4299a2dd95SBruce Richardson
4399a2dd95SBruce Richardson page_sz = (size_t)elem->msl->page_sz;
4499a2dd95SBruce Richardson data_start = RTE_PTR_ADD(elem, MALLOC_ELEM_HEADER_LEN);
4599a2dd95SBruce Richardson data_end = RTE_PTR_ADD(elem, elem->size - MALLOC_ELEM_TRAILER_LEN);
4699a2dd95SBruce Richardson /* segment must start after header and with specified alignment */
4799a2dd95SBruce Richardson contig_seg_start = RTE_PTR_ALIGN_CEIL(data_start, align);
4899a2dd95SBruce Richardson
4999a2dd95SBruce Richardson /* return if aligned address is already out of malloc element */
5099a2dd95SBruce Richardson if (contig_seg_start > data_end)
5199a2dd95SBruce Richardson return 0;
5299a2dd95SBruce Richardson
5399a2dd95SBruce Richardson /* if we're in IOVA as VA mode, or if we're in legacy mode with
5499a2dd95SBruce Richardson * hugepages, all elements are IOVA-contiguous. however, we can only
5599a2dd95SBruce Richardson * make these assumptions about internal memory - externally allocated
5699a2dd95SBruce Richardson * segments have to be checked.
5799a2dd95SBruce Richardson */
5899a2dd95SBruce Richardson if (!elem->msl->external &&
5999a2dd95SBruce Richardson (rte_eal_iova_mode() == RTE_IOVA_VA ||
6099a2dd95SBruce Richardson (internal_conf->legacy_mem &&
6199a2dd95SBruce Richardson rte_eal_has_hugepages())))
6299a2dd95SBruce Richardson return RTE_PTR_DIFF(data_end, contig_seg_start);
6399a2dd95SBruce Richardson
6499a2dd95SBruce Richardson cur_page = RTE_PTR_ALIGN_FLOOR(contig_seg_start, page_sz);
6599a2dd95SBruce Richardson ms = rte_mem_virt2memseg(cur_page, elem->msl);
6699a2dd95SBruce Richardson
6799a2dd95SBruce Richardson /* do first iteration outside the loop */
6899a2dd95SBruce Richardson page_end = RTE_PTR_ADD(cur_page, page_sz);
6999a2dd95SBruce Richardson cur_seg_end = RTE_MIN(page_end, data_end);
7099a2dd95SBruce Richardson cur = RTE_PTR_DIFF(cur_seg_end, contig_seg_start) -
7199a2dd95SBruce Richardson MALLOC_ELEM_TRAILER_LEN;
7299a2dd95SBruce Richardson max = cur;
7399a2dd95SBruce Richardson expected_iova = ms->iova + page_sz;
7499a2dd95SBruce Richardson /* memsegs are contiguous in memory */
7599a2dd95SBruce Richardson ms++;
7699a2dd95SBruce Richardson
7799a2dd95SBruce Richardson cur_page = RTE_PTR_ADD(cur_page, page_sz);
7899a2dd95SBruce Richardson
7999a2dd95SBruce Richardson while (cur_page < data_end) {
8099a2dd95SBruce Richardson page_end = RTE_PTR_ADD(cur_page, page_sz);
8199a2dd95SBruce Richardson cur_seg_end = RTE_MIN(page_end, data_end);
8299a2dd95SBruce Richardson
8399a2dd95SBruce Richardson /* reset start of contiguous segment if unexpected iova */
8499a2dd95SBruce Richardson if (ms->iova != expected_iova) {
8599a2dd95SBruce Richardson /* next contiguous segment must start at specified
8699a2dd95SBruce Richardson * alignment.
8799a2dd95SBruce Richardson */
8899a2dd95SBruce Richardson contig_seg_start = RTE_PTR_ALIGN(cur_page, align);
8999a2dd95SBruce Richardson /* new segment start may be on a different page, so find
9099a2dd95SBruce Richardson * the page and skip to next iteration to make sure
9199a2dd95SBruce Richardson * we're not blowing past data end.
9299a2dd95SBruce Richardson */
9399a2dd95SBruce Richardson ms = rte_mem_virt2memseg(contig_seg_start, elem->msl);
9499a2dd95SBruce Richardson cur_page = ms->addr;
9599a2dd95SBruce Richardson /* don't trigger another recalculation */
9699a2dd95SBruce Richardson expected_iova = ms->iova;
9799a2dd95SBruce Richardson continue;
9899a2dd95SBruce Richardson }
9999a2dd95SBruce Richardson /* cur_seg_end ends on a page boundary or on data end. if we're
10099a2dd95SBruce Richardson * looking at data end, then malloc trailer is already included
10199a2dd95SBruce Richardson * in the calculations. if we're looking at page end, then we
10299a2dd95SBruce Richardson * know there's more data past this page and thus there's space
10399a2dd95SBruce Richardson * for malloc element trailer, so don't count it here.
10499a2dd95SBruce Richardson */
10599a2dd95SBruce Richardson cur = RTE_PTR_DIFF(cur_seg_end, contig_seg_start);
10699a2dd95SBruce Richardson /* update max if cur value is bigger */
10799a2dd95SBruce Richardson if (cur > max)
10899a2dd95SBruce Richardson max = cur;
10999a2dd95SBruce Richardson
11099a2dd95SBruce Richardson /* move to next page */
11199a2dd95SBruce Richardson cur_page = page_end;
11299a2dd95SBruce Richardson expected_iova = ms->iova + page_sz;
11399a2dd95SBruce Richardson /* memsegs are contiguous in memory */
11499a2dd95SBruce Richardson ms++;
11599a2dd95SBruce Richardson }
11699a2dd95SBruce Richardson
11799a2dd95SBruce Richardson return max;
11899a2dd95SBruce Richardson }
11999a2dd95SBruce Richardson
12099a2dd95SBruce Richardson /*
12199a2dd95SBruce Richardson * Initialize a general malloc_elem header structure
12299a2dd95SBruce Richardson */
12399a2dd95SBruce Richardson void
malloc_elem_init(struct malloc_elem * elem,struct malloc_heap * heap,struct rte_memseg_list * msl,size_t size,struct malloc_elem * orig_elem,size_t orig_size,bool dirty)12499a2dd95SBruce Richardson malloc_elem_init(struct malloc_elem *elem, struct malloc_heap *heap,
12599a2dd95SBruce Richardson struct rte_memseg_list *msl, size_t size,
1262edd037cSDmitry Kozlyuk struct malloc_elem *orig_elem, size_t orig_size, bool dirty)
12799a2dd95SBruce Richardson {
12899a2dd95SBruce Richardson elem->heap = heap;
12999a2dd95SBruce Richardson elem->msl = msl;
13099a2dd95SBruce Richardson elem->prev = NULL;
13199a2dd95SBruce Richardson elem->next = NULL;
13299a2dd95SBruce Richardson memset(&elem->free_list, 0, sizeof(elem->free_list));
13399a2dd95SBruce Richardson elem->state = ELEM_FREE;
1342edd037cSDmitry Kozlyuk elem->dirty = dirty;
13599a2dd95SBruce Richardson elem->size = size;
13699a2dd95SBruce Richardson elem->pad = 0;
13799a2dd95SBruce Richardson elem->orig_elem = orig_elem;
13899a2dd95SBruce Richardson elem->orig_size = orig_size;
13999a2dd95SBruce Richardson set_header(elem);
14099a2dd95SBruce Richardson set_trailer(elem);
14199a2dd95SBruce Richardson }
14299a2dd95SBruce Richardson
14399a2dd95SBruce Richardson void
malloc_elem_insert(struct malloc_elem * elem)14499a2dd95SBruce Richardson malloc_elem_insert(struct malloc_elem *elem)
14599a2dd95SBruce Richardson {
14699a2dd95SBruce Richardson struct malloc_elem *prev_elem, *next_elem;
14799a2dd95SBruce Richardson struct malloc_heap *heap = elem->heap;
14899a2dd95SBruce Richardson
14999a2dd95SBruce Richardson /* first and last elements must be both NULL or both non-NULL */
15099a2dd95SBruce Richardson if ((heap->first == NULL) != (heap->last == NULL)) {
151*ae67895bSDavid Marchand EAL_LOG(ERR, "Heap is probably corrupt");
15299a2dd95SBruce Richardson return;
15399a2dd95SBruce Richardson }
15499a2dd95SBruce Richardson
15599a2dd95SBruce Richardson if (heap->first == NULL && heap->last == NULL) {
15699a2dd95SBruce Richardson /* if empty heap */
15799a2dd95SBruce Richardson heap->first = elem;
15899a2dd95SBruce Richardson heap->last = elem;
15999a2dd95SBruce Richardson prev_elem = NULL;
16099a2dd95SBruce Richardson next_elem = NULL;
16199a2dd95SBruce Richardson } else if (elem < heap->first) {
16299a2dd95SBruce Richardson /* if lower than start */
16399a2dd95SBruce Richardson prev_elem = NULL;
16499a2dd95SBruce Richardson next_elem = heap->first;
16599a2dd95SBruce Richardson heap->first = elem;
16699a2dd95SBruce Richardson } else if (elem > heap->last) {
16799a2dd95SBruce Richardson /* if higher than end */
16899a2dd95SBruce Richardson prev_elem = heap->last;
16999a2dd95SBruce Richardson next_elem = NULL;
17099a2dd95SBruce Richardson heap->last = elem;
17199a2dd95SBruce Richardson } else {
17299a2dd95SBruce Richardson /* the new memory is somewhere between start and end */
17399a2dd95SBruce Richardson uint64_t dist_from_start, dist_from_end;
17499a2dd95SBruce Richardson
17599a2dd95SBruce Richardson dist_from_end = RTE_PTR_DIFF(heap->last, elem);
17699a2dd95SBruce Richardson dist_from_start = RTE_PTR_DIFF(elem, heap->first);
17799a2dd95SBruce Richardson
17899a2dd95SBruce Richardson /* check which is closer, and find closest list entries */
17999a2dd95SBruce Richardson if (dist_from_start < dist_from_end) {
18099a2dd95SBruce Richardson prev_elem = heap->first;
18199a2dd95SBruce Richardson while (prev_elem->next < elem)
18299a2dd95SBruce Richardson prev_elem = prev_elem->next;
18399a2dd95SBruce Richardson next_elem = prev_elem->next;
18499a2dd95SBruce Richardson } else {
18599a2dd95SBruce Richardson next_elem = heap->last;
18699a2dd95SBruce Richardson while (next_elem->prev > elem)
18799a2dd95SBruce Richardson next_elem = next_elem->prev;
18899a2dd95SBruce Richardson prev_elem = next_elem->prev;
18999a2dd95SBruce Richardson }
19099a2dd95SBruce Richardson }
19199a2dd95SBruce Richardson
19299a2dd95SBruce Richardson /* insert new element */
19399a2dd95SBruce Richardson elem->prev = prev_elem;
19499a2dd95SBruce Richardson elem->next = next_elem;
19599a2dd95SBruce Richardson if (prev_elem)
19699a2dd95SBruce Richardson prev_elem->next = elem;
19799a2dd95SBruce Richardson if (next_elem)
19899a2dd95SBruce Richardson next_elem->prev = elem;
19999a2dd95SBruce Richardson }
20099a2dd95SBruce Richardson
20199a2dd95SBruce Richardson /*
20299a2dd95SBruce Richardson * Attempt to find enough physically contiguous memory in this block to store
20399a2dd95SBruce Richardson * our data. Assume that element has at least enough space to fit in the data,
20499a2dd95SBruce Richardson * so we just check the page addresses.
20599a2dd95SBruce Richardson */
20699a2dd95SBruce Richardson static bool
elem_check_phys_contig(const struct rte_memseg_list * msl,void * start,size_t size)20799a2dd95SBruce Richardson elem_check_phys_contig(const struct rte_memseg_list *msl,
20899a2dd95SBruce Richardson void *start, size_t size)
20999a2dd95SBruce Richardson {
21099a2dd95SBruce Richardson return eal_memalloc_is_contig(msl, start, size);
21199a2dd95SBruce Richardson }
21299a2dd95SBruce Richardson
21399a2dd95SBruce Richardson /*
21499a2dd95SBruce Richardson * calculate the starting point of where data of the requested size
21599a2dd95SBruce Richardson * and alignment would fit in the current element. If the data doesn't
21699a2dd95SBruce Richardson * fit, return NULL.
21799a2dd95SBruce Richardson */
21899a2dd95SBruce Richardson static void *
elem_start_pt(struct malloc_elem * elem,size_t size,unsigned align,size_t bound,bool contig)21999a2dd95SBruce Richardson elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
22099a2dd95SBruce Richardson size_t bound, bool contig)
22199a2dd95SBruce Richardson {
22299a2dd95SBruce Richardson size_t elem_size = elem->size;
22399a2dd95SBruce Richardson
22499a2dd95SBruce Richardson /*
22599a2dd95SBruce Richardson * we're allocating from the end, so adjust the size of element by
22699a2dd95SBruce Richardson * alignment size.
22799a2dd95SBruce Richardson */
22899a2dd95SBruce Richardson while (elem_size >= size) {
22999a2dd95SBruce Richardson const size_t bmask = ~(bound - 1);
23099a2dd95SBruce Richardson uintptr_t end_pt = (uintptr_t)elem +
23199a2dd95SBruce Richardson elem_size - MALLOC_ELEM_TRAILER_LEN;
23299a2dd95SBruce Richardson uintptr_t new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
23399a2dd95SBruce Richardson align);
23499a2dd95SBruce Richardson uintptr_t new_elem_start;
23599a2dd95SBruce Richardson
23699a2dd95SBruce Richardson /* check boundary */
23799a2dd95SBruce Richardson if ((new_data_start & bmask) != ((end_pt - 1) & bmask)) {
23899a2dd95SBruce Richardson end_pt = RTE_ALIGN_FLOOR(end_pt, bound);
23999a2dd95SBruce Richardson new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
24099a2dd95SBruce Richardson align);
24199a2dd95SBruce Richardson end_pt = new_data_start + size;
24299a2dd95SBruce Richardson
24399a2dd95SBruce Richardson if (((end_pt - 1) & bmask) != (new_data_start & bmask))
24499a2dd95SBruce Richardson return NULL;
24599a2dd95SBruce Richardson }
24699a2dd95SBruce Richardson
24799a2dd95SBruce Richardson new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
24899a2dd95SBruce Richardson
24999a2dd95SBruce Richardson /* if the new start point is before the exist start,
25099a2dd95SBruce Richardson * it won't fit
25199a2dd95SBruce Richardson */
25299a2dd95SBruce Richardson if (new_elem_start < (uintptr_t)elem)
25399a2dd95SBruce Richardson return NULL;
25499a2dd95SBruce Richardson
25599a2dd95SBruce Richardson if (contig) {
25699a2dd95SBruce Richardson size_t new_data_size = end_pt - new_data_start;
25799a2dd95SBruce Richardson
25899a2dd95SBruce Richardson /*
25999a2dd95SBruce Richardson * if physical contiguousness was requested and we
26099a2dd95SBruce Richardson * couldn't fit all data into one physically contiguous
26199a2dd95SBruce Richardson * block, try again with lower addresses.
26299a2dd95SBruce Richardson */
26399a2dd95SBruce Richardson if (!elem_check_phys_contig(elem->msl,
26499a2dd95SBruce Richardson (void *)new_data_start,
26599a2dd95SBruce Richardson new_data_size)) {
26699a2dd95SBruce Richardson elem_size -= align;
26799a2dd95SBruce Richardson continue;
26899a2dd95SBruce Richardson }
26999a2dd95SBruce Richardson }
27099a2dd95SBruce Richardson return (void *)new_elem_start;
27199a2dd95SBruce Richardson }
27299a2dd95SBruce Richardson return NULL;
27399a2dd95SBruce Richardson }
27499a2dd95SBruce Richardson
27599a2dd95SBruce Richardson /*
27699a2dd95SBruce Richardson * use elem_start_pt to determine if we get meet the size and
27799a2dd95SBruce Richardson * alignment request from the current element
27899a2dd95SBruce Richardson */
27999a2dd95SBruce Richardson int
malloc_elem_can_hold(struct malloc_elem * elem,size_t size,unsigned align,size_t bound,bool contig)28099a2dd95SBruce Richardson malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align,
28199a2dd95SBruce Richardson size_t bound, bool contig)
28299a2dd95SBruce Richardson {
28399a2dd95SBruce Richardson return elem_start_pt(elem, size, align, bound, contig) != NULL;
28499a2dd95SBruce Richardson }
28599a2dd95SBruce Richardson
28699a2dd95SBruce Richardson /*
28799a2dd95SBruce Richardson * split an existing element into two smaller elements at the given
28899a2dd95SBruce Richardson * split_pt parameter.
28999a2dd95SBruce Richardson */
29099a2dd95SBruce Richardson static void
split_elem(struct malloc_elem * elem,struct malloc_elem * split_pt)29199a2dd95SBruce Richardson split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
29299a2dd95SBruce Richardson {
29399a2dd95SBruce Richardson struct malloc_elem *next_elem = elem->next;
29499a2dd95SBruce Richardson const size_t old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
29599a2dd95SBruce Richardson const size_t new_elem_size = elem->size - old_elem_size;
29699a2dd95SBruce Richardson
29799a2dd95SBruce Richardson malloc_elem_init(split_pt, elem->heap, elem->msl, new_elem_size,
2982edd037cSDmitry Kozlyuk elem->orig_elem, elem->orig_size, elem->dirty);
29999a2dd95SBruce Richardson split_pt->prev = elem;
30099a2dd95SBruce Richardson split_pt->next = next_elem;
30199a2dd95SBruce Richardson if (next_elem)
30299a2dd95SBruce Richardson next_elem->prev = split_pt;
30399a2dd95SBruce Richardson else
30499a2dd95SBruce Richardson elem->heap->last = split_pt;
30599a2dd95SBruce Richardson elem->next = split_pt;
30699a2dd95SBruce Richardson elem->size = old_elem_size;
30799a2dd95SBruce Richardson set_trailer(elem);
30899a2dd95SBruce Richardson if (elem->pad) {
30999a2dd95SBruce Richardson /* Update inner padding inner element size. */
31099a2dd95SBruce Richardson elem = RTE_PTR_ADD(elem, elem->pad);
31199a2dd95SBruce Richardson elem->size = old_elem_size - elem->pad;
31299a2dd95SBruce Richardson }
31399a2dd95SBruce Richardson }
31499a2dd95SBruce Richardson
31599a2dd95SBruce Richardson /*
31699a2dd95SBruce Richardson * our malloc heap is a doubly linked list, so doubly remove our element.
31799a2dd95SBruce Richardson */
31899a2dd95SBruce Richardson static void __rte_unused
remove_elem(struct malloc_elem * elem)31999a2dd95SBruce Richardson remove_elem(struct malloc_elem *elem)
32099a2dd95SBruce Richardson {
32199a2dd95SBruce Richardson struct malloc_elem *next, *prev;
32299a2dd95SBruce Richardson next = elem->next;
32399a2dd95SBruce Richardson prev = elem->prev;
32499a2dd95SBruce Richardson
32599a2dd95SBruce Richardson if (next)
32699a2dd95SBruce Richardson next->prev = prev;
32799a2dd95SBruce Richardson else
32899a2dd95SBruce Richardson elem->heap->last = prev;
32999a2dd95SBruce Richardson if (prev)
33099a2dd95SBruce Richardson prev->next = next;
33199a2dd95SBruce Richardson else
33299a2dd95SBruce Richardson elem->heap->first = next;
33399a2dd95SBruce Richardson
33499a2dd95SBruce Richardson elem->prev = NULL;
33599a2dd95SBruce Richardson elem->next = NULL;
33699a2dd95SBruce Richardson }
33799a2dd95SBruce Richardson
33899a2dd95SBruce Richardson static int
next_elem_is_adjacent(struct malloc_elem * elem)33999a2dd95SBruce Richardson next_elem_is_adjacent(struct malloc_elem *elem)
34099a2dd95SBruce Richardson {
34199a2dd95SBruce Richardson const struct internal_config *internal_conf =
34299a2dd95SBruce Richardson eal_get_internal_configuration();
34399a2dd95SBruce Richardson
34499a2dd95SBruce Richardson return elem->next == RTE_PTR_ADD(elem, elem->size) &&
34599a2dd95SBruce Richardson elem->next->msl == elem->msl &&
34699a2dd95SBruce Richardson (!internal_conf->match_allocations ||
34799a2dd95SBruce Richardson elem->orig_elem == elem->next->orig_elem);
34899a2dd95SBruce Richardson }
34999a2dd95SBruce Richardson
35099a2dd95SBruce Richardson static int
prev_elem_is_adjacent(struct malloc_elem * elem)35199a2dd95SBruce Richardson prev_elem_is_adjacent(struct malloc_elem *elem)
35299a2dd95SBruce Richardson {
35399a2dd95SBruce Richardson const struct internal_config *internal_conf =
35499a2dd95SBruce Richardson eal_get_internal_configuration();
35599a2dd95SBruce Richardson
35699a2dd95SBruce Richardson return elem == RTE_PTR_ADD(elem->prev, elem->prev->size) &&
35799a2dd95SBruce Richardson elem->prev->msl == elem->msl &&
35899a2dd95SBruce Richardson (!internal_conf->match_allocations ||
35999a2dd95SBruce Richardson elem->orig_elem == elem->prev->orig_elem);
36099a2dd95SBruce Richardson }
36199a2dd95SBruce Richardson
36299a2dd95SBruce Richardson /*
36399a2dd95SBruce Richardson * Given an element size, compute its freelist index.
36499a2dd95SBruce Richardson * We free an element into the freelist containing similarly-sized elements.
36599a2dd95SBruce Richardson * We try to allocate elements starting with the freelist containing
36699a2dd95SBruce Richardson * similarly-sized elements, and if necessary, we search freelists
36799a2dd95SBruce Richardson * containing larger elements.
36899a2dd95SBruce Richardson *
36999a2dd95SBruce Richardson * Example element size ranges for a heap with five free lists:
370f62f4a37SFengnan Chang * heap->free_head[0] - (0 , 2^8)
371f62f4a37SFengnan Chang * heap->free_head[1] - [2^8 , 2^10)
372f62f4a37SFengnan Chang * heap->free_head[2] - [2^10 ,2^12)
373f62f4a37SFengnan Chang * heap->free_head[3] - [2^12, 2^14)
374f62f4a37SFengnan Chang * heap->free_head[4] - [2^14, MAX_SIZE]
37599a2dd95SBruce Richardson */
37699a2dd95SBruce Richardson size_t
malloc_elem_free_list_index(size_t size)37799a2dd95SBruce Richardson malloc_elem_free_list_index(size_t size)
37899a2dd95SBruce Richardson {
37999a2dd95SBruce Richardson #define MALLOC_MINSIZE_LOG2 8
38099a2dd95SBruce Richardson #define MALLOC_LOG2_INCREMENT 2
38199a2dd95SBruce Richardson
38299a2dd95SBruce Richardson size_t log2;
38399a2dd95SBruce Richardson size_t index;
38499a2dd95SBruce Richardson
385f881b3e6SRuifeng Wang if (size < (1UL << MALLOC_MINSIZE_LOG2))
38699a2dd95SBruce Richardson return 0;
38799a2dd95SBruce Richardson
388f62f4a37SFengnan Chang /* Find next power of 2 > size. */
38918898c4dSTyler Retzlaff log2 = sizeof(size) * 8 - rte_clz64(size);
39099a2dd95SBruce Richardson
39199a2dd95SBruce Richardson /* Compute freelist index, based on log2(size). */
39299a2dd95SBruce Richardson index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
39399a2dd95SBruce Richardson MALLOC_LOG2_INCREMENT;
39499a2dd95SBruce Richardson
39599a2dd95SBruce Richardson return index <= RTE_HEAP_NUM_FREELISTS - 1 ?
39699a2dd95SBruce Richardson index : RTE_HEAP_NUM_FREELISTS - 1;
39799a2dd95SBruce Richardson }
39899a2dd95SBruce Richardson
39999a2dd95SBruce Richardson /*
40099a2dd95SBruce Richardson * Add the specified element to its heap's free list.
40199a2dd95SBruce Richardson */
40299a2dd95SBruce Richardson void
malloc_elem_free_list_insert(struct malloc_elem * elem)40399a2dd95SBruce Richardson malloc_elem_free_list_insert(struct malloc_elem *elem)
40499a2dd95SBruce Richardson {
40599a2dd95SBruce Richardson size_t idx;
40699a2dd95SBruce Richardson
40799a2dd95SBruce Richardson idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
40899a2dd95SBruce Richardson elem->state = ELEM_FREE;
40999a2dd95SBruce Richardson LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
41099a2dd95SBruce Richardson }
41199a2dd95SBruce Richardson
41299a2dd95SBruce Richardson /*
41399a2dd95SBruce Richardson * Remove the specified element from its heap's free list.
41499a2dd95SBruce Richardson */
41599a2dd95SBruce Richardson void
malloc_elem_free_list_remove(struct malloc_elem * elem)41699a2dd95SBruce Richardson malloc_elem_free_list_remove(struct malloc_elem *elem)
41799a2dd95SBruce Richardson {
41899a2dd95SBruce Richardson LIST_REMOVE(elem, free_list);
41999a2dd95SBruce Richardson }
42099a2dd95SBruce Richardson
42199a2dd95SBruce Richardson /*
42299a2dd95SBruce Richardson * reserve a block of data in an existing malloc_elem. If the malloc_elem
42399a2dd95SBruce Richardson * is much larger than the data block requested, we split the element in two.
42499a2dd95SBruce Richardson * This function is only called from malloc_heap_alloc so parameter checking
42599a2dd95SBruce Richardson * is not done here, as it's done there previously.
42699a2dd95SBruce Richardson */
42799a2dd95SBruce Richardson struct malloc_elem *
malloc_elem_alloc(struct malloc_elem * elem,size_t size,unsigned align,size_t bound,bool contig)42899a2dd95SBruce Richardson malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
42999a2dd95SBruce Richardson size_t bound, bool contig)
43099a2dd95SBruce Richardson {
43199a2dd95SBruce Richardson struct malloc_elem *new_elem = elem_start_pt(elem, size, align, bound,
43299a2dd95SBruce Richardson contig);
43399a2dd95SBruce Richardson const size_t old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
43499a2dd95SBruce Richardson const size_t trailer_size = elem->size - old_elem_size - size -
43599a2dd95SBruce Richardson MALLOC_ELEM_OVERHEAD;
43699a2dd95SBruce Richardson
43799a2dd95SBruce Richardson malloc_elem_free_list_remove(elem);
43899a2dd95SBruce Richardson
43999a2dd95SBruce Richardson if (trailer_size > MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
44099a2dd95SBruce Richardson /* split it, too much free space after elem */
44199a2dd95SBruce Richardson struct malloc_elem *new_free_elem =
44299a2dd95SBruce Richardson RTE_PTR_ADD(new_elem, size + MALLOC_ELEM_OVERHEAD);
44399a2dd95SBruce Richardson
4446cc51b12SZhihong Peng asan_clear_split_alloczone(new_free_elem);
4456cc51b12SZhihong Peng
44699a2dd95SBruce Richardson split_elem(elem, new_free_elem);
44799a2dd95SBruce Richardson malloc_elem_free_list_insert(new_free_elem);
44899a2dd95SBruce Richardson
44999a2dd95SBruce Richardson if (elem == elem->heap->last)
45099a2dd95SBruce Richardson elem->heap->last = new_free_elem;
45199a2dd95SBruce Richardson }
45299a2dd95SBruce Richardson
45399a2dd95SBruce Richardson if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
45499a2dd95SBruce Richardson /* don't split it, pad the element instead */
45599a2dd95SBruce Richardson elem->state = ELEM_BUSY;
45699a2dd95SBruce Richardson elem->pad = old_elem_size;
45799a2dd95SBruce Richardson
4586cc51b12SZhihong Peng asan_clear_alloczone(elem);
4596cc51b12SZhihong Peng
46099a2dd95SBruce Richardson /* put a dummy header in padding, to point to real element header */
46199a2dd95SBruce Richardson if (elem->pad > 0) { /* pad will be at least 64-bytes, as everything
46299a2dd95SBruce Richardson * is cache-line aligned */
46399a2dd95SBruce Richardson new_elem->pad = elem->pad;
46499a2dd95SBruce Richardson new_elem->state = ELEM_PAD;
46599a2dd95SBruce Richardson new_elem->size = elem->size - elem->pad;
46699a2dd95SBruce Richardson set_header(new_elem);
46799a2dd95SBruce Richardson }
46899a2dd95SBruce Richardson
46999a2dd95SBruce Richardson return new_elem;
47099a2dd95SBruce Richardson }
47199a2dd95SBruce Richardson
4726cc51b12SZhihong Peng asan_clear_split_alloczone(new_elem);
4736cc51b12SZhihong Peng
47499a2dd95SBruce Richardson /* we are going to split the element in two. The original element
47599a2dd95SBruce Richardson * remains free, and the new element is the one allocated.
47699a2dd95SBruce Richardson * Re-insert original element, in case its new size makes it
47799a2dd95SBruce Richardson * belong on a different list.
47899a2dd95SBruce Richardson */
4796cc51b12SZhihong Peng
48099a2dd95SBruce Richardson split_elem(elem, new_elem);
4816cc51b12SZhihong Peng
4826cc51b12SZhihong Peng asan_clear_alloczone(new_elem);
4836cc51b12SZhihong Peng
48499a2dd95SBruce Richardson new_elem->state = ELEM_BUSY;
48599a2dd95SBruce Richardson malloc_elem_free_list_insert(elem);
48699a2dd95SBruce Richardson
48799a2dd95SBruce Richardson return new_elem;
48899a2dd95SBruce Richardson }
48999a2dd95SBruce Richardson
49099a2dd95SBruce Richardson /*
49199a2dd95SBruce Richardson * join two struct malloc_elem together. elem1 and elem2 must
49299a2dd95SBruce Richardson * be contiguous in memory.
49399a2dd95SBruce Richardson */
49499a2dd95SBruce Richardson static inline void
join_elem(struct malloc_elem * elem1,struct malloc_elem * elem2)49599a2dd95SBruce Richardson join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
49699a2dd95SBruce Richardson {
49799a2dd95SBruce Richardson struct malloc_elem *next = elem2->next;
49899a2dd95SBruce Richardson elem1->size += elem2->size;
49999a2dd95SBruce Richardson if (next)
50099a2dd95SBruce Richardson next->prev = elem1;
50199a2dd95SBruce Richardson else
50299a2dd95SBruce Richardson elem1->heap->last = elem1;
50399a2dd95SBruce Richardson elem1->next = next;
5042edd037cSDmitry Kozlyuk elem1->dirty |= elem2->dirty;
50599a2dd95SBruce Richardson if (elem1->pad) {
50699a2dd95SBruce Richardson struct malloc_elem *inner = RTE_PTR_ADD(elem1, elem1->pad);
50799a2dd95SBruce Richardson inner->size = elem1->size - elem1->pad;
50899a2dd95SBruce Richardson }
50999a2dd95SBruce Richardson }
51099a2dd95SBruce Richardson
51199a2dd95SBruce Richardson struct malloc_elem *
malloc_elem_join_adjacent_free(struct malloc_elem * elem)51299a2dd95SBruce Richardson malloc_elem_join_adjacent_free(struct malloc_elem *elem)
51399a2dd95SBruce Richardson {
51499a2dd95SBruce Richardson /*
51599a2dd95SBruce Richardson * check if next element exists, is adjacent and is free, if so join
51699a2dd95SBruce Richardson * with it, need to remove from free list.
51799a2dd95SBruce Richardson */
51899a2dd95SBruce Richardson if (elem->next != NULL && elem->next->state == ELEM_FREE &&
51999a2dd95SBruce Richardson next_elem_is_adjacent(elem)) {
52099a2dd95SBruce Richardson void *erase;
52199a2dd95SBruce Richardson size_t erase_len;
52299a2dd95SBruce Richardson
52399a2dd95SBruce Richardson /* we will want to erase the trailer and header */
52499a2dd95SBruce Richardson erase = RTE_PTR_SUB(elem->next, MALLOC_ELEM_TRAILER_LEN);
52599a2dd95SBruce Richardson erase_len = MALLOC_ELEM_OVERHEAD + elem->next->pad;
52699a2dd95SBruce Richardson
52799a2dd95SBruce Richardson /* remove from free list, join to this one */
52899a2dd95SBruce Richardson malloc_elem_free_list_remove(elem->next);
52999a2dd95SBruce Richardson join_elem(elem, elem->next);
53099a2dd95SBruce Richardson
53199a2dd95SBruce Richardson /* erase header, trailer and pad */
53299a2dd95SBruce Richardson memset(erase, MALLOC_POISON, erase_len);
53399a2dd95SBruce Richardson }
53499a2dd95SBruce Richardson
53599a2dd95SBruce Richardson /*
53699a2dd95SBruce Richardson * check if prev element exists, is adjacent and is free, if so join
53799a2dd95SBruce Richardson * with it, need to remove from free list.
53899a2dd95SBruce Richardson */
53999a2dd95SBruce Richardson if (elem->prev != NULL && elem->prev->state == ELEM_FREE &&
54099a2dd95SBruce Richardson prev_elem_is_adjacent(elem)) {
54199a2dd95SBruce Richardson struct malloc_elem *new_elem;
54299a2dd95SBruce Richardson void *erase;
54399a2dd95SBruce Richardson size_t erase_len;
54499a2dd95SBruce Richardson
54599a2dd95SBruce Richardson /* we will want to erase trailer and header */
54699a2dd95SBruce Richardson erase = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
54799a2dd95SBruce Richardson erase_len = MALLOC_ELEM_OVERHEAD + elem->pad;
54899a2dd95SBruce Richardson
54999a2dd95SBruce Richardson /* remove from free list, join to this one */
55099a2dd95SBruce Richardson malloc_elem_free_list_remove(elem->prev);
55199a2dd95SBruce Richardson
55299a2dd95SBruce Richardson new_elem = elem->prev;
55399a2dd95SBruce Richardson join_elem(new_elem, elem);
55499a2dd95SBruce Richardson
55599a2dd95SBruce Richardson /* erase header, trailer and pad */
55699a2dd95SBruce Richardson memset(erase, MALLOC_POISON, erase_len);
55799a2dd95SBruce Richardson
55899a2dd95SBruce Richardson elem = new_elem;
55999a2dd95SBruce Richardson }
56099a2dd95SBruce Richardson
56199a2dd95SBruce Richardson return elem;
56299a2dd95SBruce Richardson }
56399a2dd95SBruce Richardson
56499a2dd95SBruce Richardson /*
56599a2dd95SBruce Richardson * free a malloc_elem block by adding it to the free list. If the
56699a2dd95SBruce Richardson * blocks either immediately before or immediately after newly freed block
56799a2dd95SBruce Richardson * are also free, the blocks are merged together.
56899a2dd95SBruce Richardson */
56999a2dd95SBruce Richardson struct malloc_elem *
malloc_elem_free(struct malloc_elem * elem)57099a2dd95SBruce Richardson malloc_elem_free(struct malloc_elem *elem)
57199a2dd95SBruce Richardson {
57299a2dd95SBruce Richardson void *ptr;
57399a2dd95SBruce Richardson size_t data_len;
57499a2dd95SBruce Richardson
57599a2dd95SBruce Richardson ptr = RTE_PTR_ADD(elem, MALLOC_ELEM_HEADER_LEN);
57699a2dd95SBruce Richardson data_len = elem->size - MALLOC_ELEM_OVERHEAD;
57799a2dd95SBruce Richardson
5782edd037cSDmitry Kozlyuk /*
5792edd037cSDmitry Kozlyuk * Consider the element clean for the purposes of joining.
5802edd037cSDmitry Kozlyuk * If both neighbors are clean or non-existent,
5812edd037cSDmitry Kozlyuk * the joint element will be clean,
5822edd037cSDmitry Kozlyuk * which means the memory should be cleared.
5832edd037cSDmitry Kozlyuk * There is no need to clear the memory if the joint element is dirty.
5842edd037cSDmitry Kozlyuk */
5852edd037cSDmitry Kozlyuk elem->dirty = false;
58699a2dd95SBruce Richardson elem = malloc_elem_join_adjacent_free(elem);
58799a2dd95SBruce Richardson
58899a2dd95SBruce Richardson malloc_elem_free_list_insert(elem);
58999a2dd95SBruce Richardson
59099a2dd95SBruce Richardson elem->pad = 0;
59199a2dd95SBruce Richardson
59299a2dd95SBruce Richardson /* decrease heap's count of allocated elements */
59399a2dd95SBruce Richardson elem->heap->alloc_count--;
59499a2dd95SBruce Richardson
5952edd037cSDmitry Kozlyuk #ifndef RTE_MALLOC_DEBUG
5962edd037cSDmitry Kozlyuk /* Normally clear the memory when needed. */
5972edd037cSDmitry Kozlyuk if (!elem->dirty)
5982edd037cSDmitry Kozlyuk memset(ptr, 0, data_len);
5992edd037cSDmitry Kozlyuk #else
6002edd037cSDmitry Kozlyuk /* Always poison the memory in debug mode. */
60199a2dd95SBruce Richardson memset(ptr, MALLOC_POISON, data_len);
6022edd037cSDmitry Kozlyuk #endif
60399a2dd95SBruce Richardson
60499a2dd95SBruce Richardson return elem;
60599a2dd95SBruce Richardson }
60699a2dd95SBruce Richardson
60799a2dd95SBruce Richardson /* assume all checks were already done */
60899a2dd95SBruce Richardson void
malloc_elem_hide_region(struct malloc_elem * elem,void * start,size_t len)60999a2dd95SBruce Richardson malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)
61099a2dd95SBruce Richardson {
61199a2dd95SBruce Richardson struct malloc_elem *hide_start, *hide_end, *prev, *next;
61299a2dd95SBruce Richardson size_t len_before, len_after;
61399a2dd95SBruce Richardson
61499a2dd95SBruce Richardson hide_start = start;
61599a2dd95SBruce Richardson hide_end = RTE_PTR_ADD(start, len);
61699a2dd95SBruce Richardson
61799a2dd95SBruce Richardson prev = elem->prev;
61899a2dd95SBruce Richardson next = elem->next;
61999a2dd95SBruce Richardson
62099a2dd95SBruce Richardson /* we cannot do anything with non-adjacent elements */
62199a2dd95SBruce Richardson if (next && next_elem_is_adjacent(elem)) {
62299a2dd95SBruce Richardson len_after = RTE_PTR_DIFF(next, hide_end);
62399a2dd95SBruce Richardson if (len_after >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
6246cc51b12SZhihong Peng asan_clear_split_alloczone(hide_end);
6256cc51b12SZhihong Peng
62699a2dd95SBruce Richardson /* split after */
62799a2dd95SBruce Richardson split_elem(elem, hide_end);
62899a2dd95SBruce Richardson
62999a2dd95SBruce Richardson malloc_elem_free_list_insert(hide_end);
63099a2dd95SBruce Richardson } else if (len_after > 0) {
631*ae67895bSDavid Marchand EAL_LOG(ERR, "Unaligned element, heap is probably corrupt");
63299a2dd95SBruce Richardson return;
63399a2dd95SBruce Richardson }
63499a2dd95SBruce Richardson }
63599a2dd95SBruce Richardson
63699a2dd95SBruce Richardson /* we cannot do anything with non-adjacent elements */
63799a2dd95SBruce Richardson if (prev && prev_elem_is_adjacent(elem)) {
63899a2dd95SBruce Richardson len_before = RTE_PTR_DIFF(hide_start, elem);
63999a2dd95SBruce Richardson if (len_before >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
6406cc51b12SZhihong Peng asan_clear_split_alloczone(hide_start);
6416cc51b12SZhihong Peng
64299a2dd95SBruce Richardson /* split before */
64399a2dd95SBruce Richardson split_elem(elem, hide_start);
64499a2dd95SBruce Richardson
64599a2dd95SBruce Richardson prev = elem;
64699a2dd95SBruce Richardson elem = hide_start;
64799a2dd95SBruce Richardson
64899a2dd95SBruce Richardson malloc_elem_free_list_insert(prev);
64999a2dd95SBruce Richardson } else if (len_before > 0) {
650*ae67895bSDavid Marchand EAL_LOG(ERR, "Unaligned element, heap is probably corrupt");
65199a2dd95SBruce Richardson return;
65299a2dd95SBruce Richardson }
65399a2dd95SBruce Richardson }
65499a2dd95SBruce Richardson
6556cc51b12SZhihong Peng asan_clear_alloczone(elem);
6566cc51b12SZhihong Peng
65799a2dd95SBruce Richardson remove_elem(elem);
65899a2dd95SBruce Richardson }
65999a2dd95SBruce Richardson
66099a2dd95SBruce Richardson /*
66199a2dd95SBruce Richardson * attempt to resize a malloc_elem by expanding into any free space
66299a2dd95SBruce Richardson * immediately after it in memory.
66399a2dd95SBruce Richardson */
66499a2dd95SBruce Richardson int
malloc_elem_resize(struct malloc_elem * elem,size_t size)66599a2dd95SBruce Richardson malloc_elem_resize(struct malloc_elem *elem, size_t size)
66699a2dd95SBruce Richardson {
66799a2dd95SBruce Richardson const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
66899a2dd95SBruce Richardson
66999a2dd95SBruce Richardson /* if we request a smaller size, then always return ok */
6706cc51b12SZhihong Peng if (elem->size >= new_size) {
6716cc51b12SZhihong Peng asan_clear_alloczone(elem);
67299a2dd95SBruce Richardson return 0;
6736cc51b12SZhihong Peng }
67499a2dd95SBruce Richardson
67599a2dd95SBruce Richardson /* check if there is a next element, it's free and adjacent */
67699a2dd95SBruce Richardson if (!elem->next || elem->next->state != ELEM_FREE ||
67799a2dd95SBruce Richardson !next_elem_is_adjacent(elem))
67899a2dd95SBruce Richardson return -1;
67999a2dd95SBruce Richardson if (elem->size + elem->next->size < new_size)
68099a2dd95SBruce Richardson return -1;
68199a2dd95SBruce Richardson
68299a2dd95SBruce Richardson /* we now know the element fits, so remove from free list,
68399a2dd95SBruce Richardson * join the two
68499a2dd95SBruce Richardson */
68599a2dd95SBruce Richardson malloc_elem_free_list_remove(elem->next);
68699a2dd95SBruce Richardson join_elem(elem, elem->next);
68799a2dd95SBruce Richardson
68899a2dd95SBruce Richardson if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
68999a2dd95SBruce Richardson /* now we have a big block together. Lets cut it down a bit, by splitting */
69099a2dd95SBruce Richardson struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
69199a2dd95SBruce Richardson split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
6926cc51b12SZhihong Peng
6936cc51b12SZhihong Peng asan_clear_split_alloczone(split_pt);
6946cc51b12SZhihong Peng
69599a2dd95SBruce Richardson split_elem(elem, split_pt);
69699a2dd95SBruce Richardson malloc_elem_free_list_insert(split_pt);
69799a2dd95SBruce Richardson }
6986cc51b12SZhihong Peng
6996cc51b12SZhihong Peng asan_clear_alloczone(elem);
7006cc51b12SZhihong Peng
70199a2dd95SBruce Richardson return 0;
70299a2dd95SBruce Richardson }
70399a2dd95SBruce Richardson
70499a2dd95SBruce Richardson static inline const char *
elem_state_to_str(enum elem_state state)70599a2dd95SBruce Richardson elem_state_to_str(enum elem_state state)
70699a2dd95SBruce Richardson {
70799a2dd95SBruce Richardson switch (state) {
70899a2dd95SBruce Richardson case ELEM_PAD:
70999a2dd95SBruce Richardson return "PAD";
71099a2dd95SBruce Richardson case ELEM_BUSY:
71199a2dd95SBruce Richardson return "BUSY";
71299a2dd95SBruce Richardson case ELEM_FREE:
71399a2dd95SBruce Richardson return "FREE";
71499a2dd95SBruce Richardson }
71599a2dd95SBruce Richardson return "ERROR";
71699a2dd95SBruce Richardson }
71799a2dd95SBruce Richardson
71899a2dd95SBruce Richardson void
malloc_elem_dump(const struct malloc_elem * elem,FILE * f)71999a2dd95SBruce Richardson malloc_elem_dump(const struct malloc_elem *elem, FILE *f)
72099a2dd95SBruce Richardson {
72199a2dd95SBruce Richardson fprintf(f, "Malloc element at %p (%s)\n", elem,
72299a2dd95SBruce Richardson elem_state_to_str(elem->state));
72399a2dd95SBruce Richardson fprintf(f, " len: 0x%zx pad: 0x%" PRIx32 "\n", elem->size, elem->pad);
72499a2dd95SBruce Richardson fprintf(f, " prev: %p next: %p\n", elem->prev, elem->next);
72599a2dd95SBruce Richardson }
726