1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_common.h>
12 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
14 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
20 ELEM_PAD /* element is a padding-only header */
24 struct malloc_heap *heap;
25 struct malloc_elem *volatile prev;
26 /**< points to prev elem in memseg */
27 struct malloc_elem *volatile next;
28 /**< points to next elem in memseg */
29 LIST_ENTRY(malloc_elem) free_list;
30 /**< list of free elements in heap */
31 struct rte_memseg_list *msl;
32 /** Element state, @c dirty and @c pad validity depends on it. */
33 /* An extra bit is needed to represent enum elem_state as signed int. */
34 enum elem_state state : 3;
35 /** If state == ELEM_FREE: the memory is not filled with zeroes. */
37 /** Reserved for future use. */
38 uint32_t reserved : 28;
41 struct malloc_elem *orig_elem;
43 #ifdef RTE_MALLOC_DEBUG
44 uint64_t header_cookie; /* Cookie marking start of data */
45 /* trailer cookie at start + size */
47 #ifdef RTE_MALLOC_ASAN
49 uint64_t asan_cookie[2]; /* must be next to header_cookie */
51 } __rte_cache_aligned;
53 static const unsigned int MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
55 #ifndef RTE_MALLOC_DEBUG
56 #ifdef RTE_MALLOC_ASAN
57 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
59 static const unsigned int MALLOC_ELEM_TRAILER_LEN;
62 /* dummy function - just check if pointer is non-null */
64 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
66 /* dummy function - no header if malloc_debug is not enabled */
68 set_header(struct malloc_elem *elem __rte_unused){ }
70 /* dummy function - no trailer if malloc_debug is not enabled */
72 set_trailer(struct malloc_elem *elem __rte_unused){ }
76 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
78 #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
79 #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
81 /* define macros to make referencing the header and trailer cookies easier */
82 #define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
83 elem->size - MALLOC_ELEM_TRAILER_LEN)))
84 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
87 set_header(struct malloc_elem *elem)
90 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
94 set_trailer(struct malloc_elem *elem)
97 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
100 /* check that the header and trailer cookies are set correctly */
102 malloc_elem_cookies_ok(const struct malloc_elem *elem)
104 return elem != NULL &&
105 MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
106 MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
111 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
113 #ifdef RTE_MALLOC_ASAN
116 * ASAN_SHADOW_OFFSET should match to the corresponding
117 * value defined in gcc/libsanitizer/asan/asan_mapping.h
119 #ifdef RTE_ARCH_X86_64
120 #define ASAN_SHADOW_OFFSET 0x00007fff8000
121 #elif defined(RTE_ARCH_ARM64)
122 #define ASAN_SHADOW_OFFSET 0x001000000000
123 #elif defined(RTE_ARCH_PPC_64)
124 #define ASAN_SHADOW_OFFSET 0x020000000000
127 #define ASAN_SHADOW_GRAIN_SIZE 8
128 #define ASAN_MEM_FREE_FLAG 0xfd
129 #define ASAN_MEM_REDZONE_FLAG 0xfa
130 #define ASAN_SHADOW_SCALE 3
132 #define ASAN_MEM_SHIFT(mem) ((void *)((uintptr_t)(mem) >> ASAN_SHADOW_SCALE))
133 #define ASAN_MEM_TO_SHADOW(mem) \
134 RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
138 asan_set_shadow(void *addr, char val)
144 asan_set_zone(void *ptr, size_t len, uint32_t val)
148 size_t zone_len = len / ASAN_SHADOW_GRAIN_SIZE;
149 if (len % ASAN_SHADOW_GRAIN_SIZE != 0)
152 for (i = 0; i < zone_len; i++) {
153 offset = i * ASAN_SHADOW_GRAIN_SIZE;
154 shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
155 asan_set_shadow(shadow, val);
160 * When the memory is released, the release mark is
161 * set in the corresponding range of the shadow area.
164 asan_set_freezone(void *ptr, size_t size)
166 asan_set_zone(ptr, size, ASAN_MEM_FREE_FLAG);
170 * When the memory is allocated, memory state must set as accessible.
173 asan_clear_alloczone(struct malloc_elem *elem)
175 asan_set_zone((void *)elem, elem->size, 0x0);
179 asan_clear_split_alloczone(struct malloc_elem *elem)
181 void *ptr = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
182 asan_set_zone(ptr, MALLOC_ELEM_OVERHEAD, 0x0);
186 * When the memory is allocated, the memory boundary is
187 * marked in the corresponding range of the shadow area.
188 * Requirement: redzone >= 16, is a power of two.
191 asan_set_redzone(struct malloc_elem *elem, size_t user_size)
193 uintptr_t head_redzone;
194 uintptr_t tail_redzone;
200 if (elem->state != ELEM_PAD)
201 elem = RTE_PTR_ADD(elem, elem->pad);
203 elem->user_size = user_size;
205 /* Set mark before the start of the allocated memory */
206 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
207 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
208 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
209 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
210 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone
211 - ASAN_SHADOW_GRAIN_SIZE);
212 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
214 /* Set mark after the end of the allocated memory */
215 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
216 MALLOC_ELEM_HEADER_LEN
218 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
219 val = (tail_redzone % ASAN_SHADOW_GRAIN_SIZE);
220 val = (val == 0) ? ASAN_MEM_REDZONE_FLAG : val;
221 asan_set_shadow(tail_shadow, val);
222 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
223 + ASAN_SHADOW_GRAIN_SIZE);
224 asan_set_shadow(tail_shadow, ASAN_MEM_REDZONE_FLAG);
229 * When the memory is released, the mark of the memory boundary
230 * in the corresponding range of the shadow area is cleared.
231 * Requirement: redzone >= 16, is a power of two.
234 asan_clear_redzone(struct malloc_elem *elem)
236 uintptr_t head_redzone;
237 uintptr_t tail_redzone;
242 elem = RTE_PTR_ADD(elem, elem->pad);
244 /* Clear mark before the start of the allocated memory */
245 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
246 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
247 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
248 asan_set_shadow(head_shadow, 0x00);
249 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone
250 - ASAN_SHADOW_GRAIN_SIZE);
251 asan_set_shadow(head_shadow, 0x00);
253 /* Clear mark after the end of the allocated memory */
254 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
255 MALLOC_ELEM_HEADER_LEN + elem->user_size);
256 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
257 asan_set_shadow(tail_shadow, 0x00);
258 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
259 + ASAN_SHADOW_GRAIN_SIZE);
260 asan_set_shadow(tail_shadow, 0x00);
265 old_malloc_size(struct malloc_elem *elem)
267 if (elem->state != ELEM_PAD)
268 elem = RTE_PTR_ADD(elem, elem->pad);
270 return elem->user_size;
273 #else /* !RTE_MALLOC_ASAN */
276 asan_set_zone(void *ptr __rte_unused, size_t len __rte_unused,
277 uint32_t val __rte_unused) { }
280 asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { }
283 asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { }
286 asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { }
289 asan_set_redzone(struct malloc_elem *elem __rte_unused,
290 size_t user_size __rte_unused) { }
293 asan_clear_redzone(struct malloc_elem *elem __rte_unused) { }
296 old_malloc_size(struct malloc_elem *elem)
298 return elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
300 #endif /* !RTE_MALLOC_ASAN */
303 * Given a pointer to the start of a memory block returned by malloc, get
304 * the actual malloc_elem header for that block.
306 static inline struct malloc_elem *
307 malloc_elem_from_data(const void *data)
312 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
313 if (!malloc_elem_cookies_ok(elem))
315 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
319 * initialise a malloc_elem header
322 malloc_elem_init(struct malloc_elem *elem,
323 struct malloc_heap *heap,
324 struct rte_memseg_list *msl,
326 struct malloc_elem *orig_elem,
331 malloc_elem_insert(struct malloc_elem *elem);
334 * return true if the current malloc_elem can hold a block of data
335 * of the requested size and with the requested alignment
338 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
339 unsigned int align, size_t bound, bool contig);
342 * reserve a block of data in an existing malloc_elem. If the malloc_elem
343 * is much larger than the data block requested, we split the element in two.
346 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
347 unsigned int align, size_t bound, bool contig);
350 * free a malloc_elem block by adding it to the free list. If the
351 * blocks either immediately before or immediately after newly freed block
352 * are also free, the blocks are merged together.
355 malloc_elem_free(struct malloc_elem *elem);
358 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
361 * attempt to resize a malloc_elem by expanding into any free space
362 * immediately after it in memory.
365 malloc_elem_resize(struct malloc_elem *elem, size_t size);
368 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
371 malloc_elem_free_list_remove(struct malloc_elem *elem);
374 * dump contents of malloc elem to a file.
377 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
380 * Given an element size, compute its freelist index.
383 malloc_elem_free_list_index(size_t size);
386 * Add element to its heap's free list.
389 malloc_elem_free_list_insert(struct malloc_elem *elem);
392 * Find biggest IOVA-contiguous zone within an element with specified alignment.
395 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
397 #endif /* MALLOC_ELEM_H_ */