1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
12 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
18 ELEM_PAD /* element is a padding-only header */
22 struct malloc_heap *heap;
23 struct malloc_elem *volatile prev;
24 /**< points to prev elem in memseg */
25 struct malloc_elem *volatile next;
26 /**< points to next elem in memseg */
27 LIST_ENTRY(malloc_elem) free_list;
28 /**< list of free elements in heap */
29 struct rte_memseg_list *msl;
30 volatile enum elem_state state;
33 struct malloc_elem *orig_elem;
35 #ifdef RTE_MALLOC_DEBUG
36 uint64_t header_cookie; /* Cookie marking start of data */
37 /* trailer cookie at start + size */
39 #ifdef RTE_MALLOC_ASAN
41 uint64_t asan_cookie[2]; /* must be next to header_cookie */
43 } __rte_cache_aligned;
45 static const unsigned int MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
47 #ifndef RTE_MALLOC_DEBUG
48 #ifdef RTE_MALLOC_ASAN
49 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
51 static const unsigned int MALLOC_ELEM_TRAILER_LEN;
54 /* dummy function - just check if pointer is non-null */
56 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
58 /* dummy function - no header if malloc_debug is not enabled */
60 set_header(struct malloc_elem *elem __rte_unused){ }
62 /* dummy function - no trailer if malloc_debug is not enabled */
64 set_trailer(struct malloc_elem *elem __rte_unused){ }
68 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
70 #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
71 #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
73 /* define macros to make referencing the header and trailer cookies easier */
74 #define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
75 elem->size - MALLOC_ELEM_TRAILER_LEN)))
76 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
79 set_header(struct malloc_elem *elem)
82 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
86 set_trailer(struct malloc_elem *elem)
89 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
92 /* check that the header and trailer cookies are set correctly */
94 malloc_elem_cookies_ok(const struct malloc_elem *elem)
96 return elem != NULL &&
97 MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
98 MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
103 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
105 #ifdef RTE_MALLOC_ASAN
108 * ASAN_SHADOW_OFFSET should match to the corresponding
109 * value defined in gcc/libsanitizer/asan/asan_mapping.h
111 #ifdef RTE_ARCH_X86_64
112 #define ASAN_SHADOW_OFFSET 0x00007fff8000
113 #elif defined(RTE_ARCH_ARM64)
114 #define ASAN_SHADOW_OFFSET 0x001000000000
117 #define ASAN_SHADOW_GRAIN_SIZE 8
118 #define ASAN_MEM_FREE_FLAG 0xfd
119 #define ASAN_MEM_REDZONE_FLAG 0xfa
120 #define ASAN_SHADOW_SCALE 3
122 #define ASAN_MEM_SHIFT(mem) ((void *)((uintptr_t)(mem) >> ASAN_SHADOW_SCALE))
123 #define ASAN_MEM_TO_SHADOW(mem) \
124 RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
126 #if defined(__clang__)
127 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
129 #define __rte_no_asan __attribute__((no_sanitize_address))
134 asan_set_shadow(void *addr, char val)
140 asan_set_zone(void *ptr, size_t len, uint32_t val)
144 size_t zone_len = len / ASAN_SHADOW_GRAIN_SIZE;
145 if (len % ASAN_SHADOW_GRAIN_SIZE != 0)
148 for (i = 0; i < zone_len; i++) {
149 offset = i * ASAN_SHADOW_GRAIN_SIZE;
150 shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
151 asan_set_shadow(shadow, val);
156 * When the memory is released, the release mark is
157 * set in the corresponding range of the shadow area.
160 asan_set_freezone(void *ptr, size_t size)
162 asan_set_zone(ptr, size, ASAN_MEM_FREE_FLAG);
166 * When the memory is allocated, memory state must set as accessible.
169 asan_clear_alloczone(struct malloc_elem *elem)
171 asan_set_zone((void *)elem, elem->size, 0x0);
175 asan_clear_split_alloczone(struct malloc_elem *elem)
177 void *ptr = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
178 asan_set_zone(ptr, MALLOC_ELEM_OVERHEAD, 0x0);
182 * When the memory is allocated, the memory boundary is
183 * marked in the corresponding range of the shadow area.
184 * Requirement: redzone >= 16, is a power of two.
187 asan_set_redzone(struct malloc_elem *elem, size_t user_size)
189 uintptr_t head_redzone;
190 uintptr_t tail_redzone;
196 if (elem->state != ELEM_PAD)
197 elem = RTE_PTR_ADD(elem, elem->pad);
199 elem->user_size = user_size;
201 /* Set mark before the start of the allocated memory */
202 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
203 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
204 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
205 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
206 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone
207 - ASAN_SHADOW_GRAIN_SIZE);
208 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
210 /* Set mark after the end of the allocated memory */
211 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
212 MALLOC_ELEM_HEADER_LEN
214 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
215 val = (tail_redzone % ASAN_SHADOW_GRAIN_SIZE);
216 val = (val == 0) ? ASAN_MEM_REDZONE_FLAG : val;
217 asan_set_shadow(tail_shadow, val);
218 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
219 + ASAN_SHADOW_GRAIN_SIZE);
220 asan_set_shadow(tail_shadow, ASAN_MEM_REDZONE_FLAG);
225 * When the memory is released, the mark of the memory boundary
226 * in the corresponding range of the shadow area is cleared.
227 * Requirement: redzone >= 16, is a power of two.
230 asan_clear_redzone(struct malloc_elem *elem)
232 uintptr_t head_redzone;
233 uintptr_t tail_redzone;
238 elem = RTE_PTR_ADD(elem, elem->pad);
240 /* Clear mark before the start of the allocated memory */
241 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
242 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
243 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
244 asan_set_shadow(head_shadow, 0x00);
245 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone
246 - ASAN_SHADOW_GRAIN_SIZE);
247 asan_set_shadow(head_shadow, 0x00);
249 /* Clear mark after the end of the allocated memory */
250 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
251 MALLOC_ELEM_HEADER_LEN + elem->user_size);
252 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
253 asan_set_shadow(tail_shadow, 0x00);
254 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
255 + ASAN_SHADOW_GRAIN_SIZE);
256 asan_set_shadow(tail_shadow, 0x00);
261 old_malloc_size(struct malloc_elem *elem)
263 if (elem->state != ELEM_PAD)
264 elem = RTE_PTR_ADD(elem, elem->pad);
266 return elem->user_size;
269 #else /* !RTE_MALLOC_ASAN */
271 #define __rte_no_asan
274 asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { }
277 asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { }
280 asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { }
283 asan_set_redzone(struct malloc_elem *elem __rte_unused,
284 size_t user_size __rte_unused) { }
287 asan_clear_redzone(struct malloc_elem *elem __rte_unused) { }
290 old_malloc_size(struct malloc_elem *elem)
292 return elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
294 #endif /* !RTE_MALLOC_ASAN */
297 * Given a pointer to the start of a memory block returned by malloc, get
298 * the actual malloc_elem header for that block.
300 static inline struct malloc_elem *
301 malloc_elem_from_data(const void *data)
306 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
307 if (!malloc_elem_cookies_ok(elem))
309 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
313 * initialise a malloc_elem header
316 malloc_elem_init(struct malloc_elem *elem,
317 struct malloc_heap *heap,
318 struct rte_memseg_list *msl,
320 struct malloc_elem *orig_elem,
324 malloc_elem_insert(struct malloc_elem *elem);
327 * return true if the current malloc_elem can hold a block of data
328 * of the requested size and with the requested alignment
331 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
332 unsigned int align, size_t bound, bool contig);
335 * reserve a block of data in an existing malloc_elem. If the malloc_elem
336 * is much larger than the data block requested, we split the element in two.
339 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
340 unsigned int align, size_t bound, bool contig);
343 * free a malloc_elem block by adding it to the free list. If the
344 * blocks either immediately before or immediately after newly freed block
345 * are also free, the blocks are merged together.
348 malloc_elem_free(struct malloc_elem *elem);
351 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
354 * attempt to resize a malloc_elem by expanding into any free space
355 * immediately after it in memory.
358 malloc_elem_resize(struct malloc_elem *elem, size_t size);
361 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
364 malloc_elem_free_list_remove(struct malloc_elem *elem);
367 * dump contents of malloc elem to a file.
370 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
373 * Given an element size, compute its freelist index.
376 malloc_elem_free_list_index(size_t size);
379 * Add element to its heap's free list.
382 malloc_elem_free_list_insert(struct malloc_elem *elem);
385 * Find biggest IOVA-contiguous zone within an element with specified alignment.
388 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
390 #endif /* MALLOC_ELEM_H_ */