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
115 #elif defined(RTE_ARCH_PPC_64)
116 #define ASAN_SHADOW_OFFSET 0x020000000000
119 #define ASAN_SHADOW_GRAIN_SIZE 8
120 #define ASAN_MEM_FREE_FLAG 0xfd
121 #define ASAN_MEM_REDZONE_FLAG 0xfa
122 #define ASAN_SHADOW_SCALE 3
124 #define ASAN_MEM_SHIFT(mem) ((void *)((uintptr_t)(mem) >> ASAN_SHADOW_SCALE))
125 #define ASAN_MEM_TO_SHADOW(mem) \
126 RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
128 #if defined(__clang__)
129 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
131 #define __rte_no_asan __attribute__((no_sanitize_address))
136 asan_set_shadow(void *addr, char val)
142 asan_set_zone(void *ptr, size_t len, uint32_t val)
146 size_t zone_len = len / ASAN_SHADOW_GRAIN_SIZE;
147 if (len % ASAN_SHADOW_GRAIN_SIZE != 0)
150 for (i = 0; i < zone_len; i++) {
151 offset = i * ASAN_SHADOW_GRAIN_SIZE;
152 shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
153 asan_set_shadow(shadow, val);
158 * When the memory is released, the release mark is
159 * set in the corresponding range of the shadow area.
162 asan_set_freezone(void *ptr, size_t size)
164 asan_set_zone(ptr, size, ASAN_MEM_FREE_FLAG);
168 * When the memory is allocated, memory state must set as accessible.
171 asan_clear_alloczone(struct malloc_elem *elem)
173 asan_set_zone((void *)elem, elem->size, 0x0);
177 asan_clear_split_alloczone(struct malloc_elem *elem)
179 void *ptr = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
180 asan_set_zone(ptr, MALLOC_ELEM_OVERHEAD, 0x0);
184 * When the memory is allocated, the memory boundary is
185 * marked in the corresponding range of the shadow area.
186 * Requirement: redzone >= 16, is a power of two.
189 asan_set_redzone(struct malloc_elem *elem, size_t user_size)
191 uintptr_t head_redzone;
192 uintptr_t tail_redzone;
198 if (elem->state != ELEM_PAD)
199 elem = RTE_PTR_ADD(elem, elem->pad);
201 elem->user_size = user_size;
203 /* Set mark before the start of the allocated memory */
204 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
205 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
206 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
207 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
208 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone
209 - ASAN_SHADOW_GRAIN_SIZE);
210 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
212 /* Set mark after the end of the allocated memory */
213 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
214 MALLOC_ELEM_HEADER_LEN
216 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
217 val = (tail_redzone % ASAN_SHADOW_GRAIN_SIZE);
218 val = (val == 0) ? ASAN_MEM_REDZONE_FLAG : val;
219 asan_set_shadow(tail_shadow, val);
220 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
221 + ASAN_SHADOW_GRAIN_SIZE);
222 asan_set_shadow(tail_shadow, ASAN_MEM_REDZONE_FLAG);
227 * When the memory is released, the mark of the memory boundary
228 * in the corresponding range of the shadow area is cleared.
229 * Requirement: redzone >= 16, is a power of two.
232 asan_clear_redzone(struct malloc_elem *elem)
234 uintptr_t head_redzone;
235 uintptr_t tail_redzone;
240 elem = RTE_PTR_ADD(elem, elem->pad);
242 /* Clear mark before the start of the allocated memory */
243 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
244 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
245 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
246 asan_set_shadow(head_shadow, 0x00);
247 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone
248 - ASAN_SHADOW_GRAIN_SIZE);
249 asan_set_shadow(head_shadow, 0x00);
251 /* Clear mark after the end of the allocated memory */
252 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
253 MALLOC_ELEM_HEADER_LEN + elem->user_size);
254 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
255 asan_set_shadow(tail_shadow, 0x00);
256 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
257 + ASAN_SHADOW_GRAIN_SIZE);
258 asan_set_shadow(tail_shadow, 0x00);
263 old_malloc_size(struct malloc_elem *elem)
265 if (elem->state != ELEM_PAD)
266 elem = RTE_PTR_ADD(elem, elem->pad);
268 return elem->user_size;
271 #else /* !RTE_MALLOC_ASAN */
273 #define __rte_no_asan
276 asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { }
279 asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { }
282 asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { }
285 asan_set_redzone(struct malloc_elem *elem __rte_unused,
286 size_t user_size __rte_unused) { }
289 asan_clear_redzone(struct malloc_elem *elem __rte_unused) { }
292 old_malloc_size(struct malloc_elem *elem)
294 return elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
296 #endif /* !RTE_MALLOC_ASAN */
299 * Given a pointer to the start of a memory block returned by malloc, get
300 * the actual malloc_elem header for that block.
302 static inline struct malloc_elem *
303 malloc_elem_from_data(const void *data)
308 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
309 if (!malloc_elem_cookies_ok(elem))
311 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
315 * initialise a malloc_elem header
318 malloc_elem_init(struct malloc_elem *elem,
319 struct malloc_heap *heap,
320 struct rte_memseg_list *msl,
322 struct malloc_elem *orig_elem,
326 malloc_elem_insert(struct malloc_elem *elem);
329 * return true if the current malloc_elem can hold a block of data
330 * of the requested size and with the requested alignment
333 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
334 unsigned int align, size_t bound, bool contig);
337 * reserve a block of data in an existing malloc_elem. If the malloc_elem
338 * is much larger than the data block requested, we split the element in two.
341 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
342 unsigned int align, size_t bound, bool contig);
345 * free a malloc_elem block by adding it to the free list. If the
346 * blocks either immediately before or immediately after newly freed block
347 * are also free, the blocks are merged together.
350 malloc_elem_free(struct malloc_elem *elem);
353 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
356 * attempt to resize a malloc_elem by expanding into any free space
357 * immediately after it in memory.
360 malloc_elem_resize(struct malloc_elem *elem, size_t size);
363 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
366 malloc_elem_free_list_remove(struct malloc_elem *elem);
369 * dump contents of malloc elem to a file.
372 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
375 * Given an element size, compute its freelist index.
378 malloc_elem_free_list_index(size_t size);
381 * Add element to its heap's free list.
384 malloc_elem_free_list_insert(struct malloc_elem *elem);
387 * Find biggest IOVA-contiguous zone within an element with specified alignment.
390 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
392 #endif /* MALLOC_ELEM_H_ */