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 /** Element state, @c dirty and @c pad validity depends on it. */
31 /* An extra bit is needed to represent enum elem_state as signed int. */
32 enum elem_state state : 3;
33 /** If state == ELEM_FREE: the memory is not filled with zeroes. */
35 /** Reserved for future use. */
36 uint32_t reserved : 28;
39 struct malloc_elem *orig_elem;
41 #ifdef RTE_MALLOC_DEBUG
42 uint64_t header_cookie; /* Cookie marking start of data */
43 /* trailer cookie at start + size */
45 #ifdef RTE_MALLOC_ASAN
47 uint64_t asan_cookie[2]; /* must be next to header_cookie */
49 } __rte_cache_aligned;
51 static const unsigned int MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
53 #ifndef RTE_MALLOC_DEBUG
54 #ifdef RTE_MALLOC_ASAN
55 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
57 static const unsigned int MALLOC_ELEM_TRAILER_LEN;
60 /* dummy function - just check if pointer is non-null */
62 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
64 /* dummy function - no header if malloc_debug is not enabled */
66 set_header(struct malloc_elem *elem __rte_unused){ }
68 /* dummy function - no trailer if malloc_debug is not enabled */
70 set_trailer(struct malloc_elem *elem __rte_unused){ }
74 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
76 #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
77 #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
79 /* define macros to make referencing the header and trailer cookies easier */
80 #define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
81 elem->size - MALLOC_ELEM_TRAILER_LEN)))
82 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
85 set_header(struct malloc_elem *elem)
88 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
92 set_trailer(struct malloc_elem *elem)
95 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
98 /* check that the header and trailer cookies are set correctly */
100 malloc_elem_cookies_ok(const struct malloc_elem *elem)
102 return elem != NULL &&
103 MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
104 MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
109 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
111 #ifdef RTE_MALLOC_ASAN
114 * ASAN_SHADOW_OFFSET should match to the corresponding
115 * value defined in gcc/libsanitizer/asan/asan_mapping.h
117 #ifdef RTE_ARCH_X86_64
118 #define ASAN_SHADOW_OFFSET 0x00007fff8000
119 #elif defined(RTE_ARCH_ARM64)
120 #define ASAN_SHADOW_OFFSET 0x001000000000
121 #elif defined(RTE_ARCH_PPC_64)
122 #define ASAN_SHADOW_OFFSET 0x020000000000
125 #define ASAN_SHADOW_GRAIN_SIZE 8
126 #define ASAN_MEM_FREE_FLAG 0xfd
127 #define ASAN_MEM_REDZONE_FLAG 0xfa
128 #define ASAN_SHADOW_SCALE 3
130 #define ASAN_MEM_SHIFT(mem) ((void *)((uintptr_t)(mem) >> ASAN_SHADOW_SCALE))
131 #define ASAN_MEM_TO_SHADOW(mem) \
132 RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
134 #if defined(__clang__)
135 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
137 #define __rte_no_asan __attribute__((no_sanitize_address))
142 asan_set_shadow(void *addr, char val)
148 asan_set_zone(void *ptr, size_t len, uint32_t val)
152 size_t zone_len = len / ASAN_SHADOW_GRAIN_SIZE;
153 if (len % ASAN_SHADOW_GRAIN_SIZE != 0)
156 for (i = 0; i < zone_len; i++) {
157 offset = i * ASAN_SHADOW_GRAIN_SIZE;
158 shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
159 asan_set_shadow(shadow, val);
164 * When the memory is released, the release mark is
165 * set in the corresponding range of the shadow area.
168 asan_set_freezone(void *ptr, size_t size)
170 asan_set_zone(ptr, size, ASAN_MEM_FREE_FLAG);
174 * When the memory is allocated, memory state must set as accessible.
177 asan_clear_alloczone(struct malloc_elem *elem)
179 asan_set_zone((void *)elem, elem->size, 0x0);
183 asan_clear_split_alloczone(struct malloc_elem *elem)
185 void *ptr = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
186 asan_set_zone(ptr, MALLOC_ELEM_OVERHEAD, 0x0);
190 * When the memory is allocated, the memory boundary is
191 * marked in the corresponding range of the shadow area.
192 * Requirement: redzone >= 16, is a power of two.
195 asan_set_redzone(struct malloc_elem *elem, size_t user_size)
197 uintptr_t head_redzone;
198 uintptr_t tail_redzone;
204 if (elem->state != ELEM_PAD)
205 elem = RTE_PTR_ADD(elem, elem->pad);
207 elem->user_size = user_size;
209 /* Set mark before the start of the allocated memory */
210 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
211 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
212 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
213 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
214 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone
215 - ASAN_SHADOW_GRAIN_SIZE);
216 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
218 /* Set mark after the end of the allocated memory */
219 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
220 MALLOC_ELEM_HEADER_LEN
222 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
223 val = (tail_redzone % ASAN_SHADOW_GRAIN_SIZE);
224 val = (val == 0) ? ASAN_MEM_REDZONE_FLAG : val;
225 asan_set_shadow(tail_shadow, val);
226 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
227 + ASAN_SHADOW_GRAIN_SIZE);
228 asan_set_shadow(tail_shadow, ASAN_MEM_REDZONE_FLAG);
233 * When the memory is released, the mark of the memory boundary
234 * in the corresponding range of the shadow area is cleared.
235 * Requirement: redzone >= 16, is a power of two.
238 asan_clear_redzone(struct malloc_elem *elem)
240 uintptr_t head_redzone;
241 uintptr_t tail_redzone;
246 elem = RTE_PTR_ADD(elem, elem->pad);
248 /* Clear mark before the start of the allocated memory */
249 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
250 MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
251 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
252 asan_set_shadow(head_shadow, 0x00);
253 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone
254 - ASAN_SHADOW_GRAIN_SIZE);
255 asan_set_shadow(head_shadow, 0x00);
257 /* Clear mark after the end of the allocated memory */
258 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
259 MALLOC_ELEM_HEADER_LEN + elem->user_size);
260 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
261 asan_set_shadow(tail_shadow, 0x00);
262 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
263 + ASAN_SHADOW_GRAIN_SIZE);
264 asan_set_shadow(tail_shadow, 0x00);
269 old_malloc_size(struct malloc_elem *elem)
271 if (elem->state != ELEM_PAD)
272 elem = RTE_PTR_ADD(elem, elem->pad);
274 return elem->user_size;
277 #else /* !RTE_MALLOC_ASAN */
279 #define __rte_no_asan
282 asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { }
285 asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { }
288 asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { }
291 asan_set_redzone(struct malloc_elem *elem __rte_unused,
292 size_t user_size __rte_unused) { }
295 asan_clear_redzone(struct malloc_elem *elem __rte_unused) { }
298 old_malloc_size(struct malloc_elem *elem)
300 return elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
302 #endif /* !RTE_MALLOC_ASAN */
305 * Given a pointer to the start of a memory block returned by malloc, get
306 * the actual malloc_elem header for that block.
308 static inline struct malloc_elem *
309 malloc_elem_from_data(const void *data)
314 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
315 if (!malloc_elem_cookies_ok(elem))
317 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
321 * initialise a malloc_elem header
324 malloc_elem_init(struct malloc_elem *elem,
325 struct malloc_heap *heap,
326 struct rte_memseg_list *msl,
328 struct malloc_elem *orig_elem,
333 malloc_elem_insert(struct malloc_elem *elem);
336 * return true if the current malloc_elem can hold a block of data
337 * of the requested size and with the requested alignment
340 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
341 unsigned int align, size_t bound, bool contig);
344 * reserve a block of data in an existing malloc_elem. If the malloc_elem
345 * is much larger than the data block requested, we split the element in two.
348 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
349 unsigned int align, size_t bound, bool contig);
352 * free a malloc_elem block by adding it to the free list. If the
353 * blocks either immediately before or immediately after newly freed block
354 * are also free, the blocks are merged together.
357 malloc_elem_free(struct malloc_elem *elem);
360 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
363 * attempt to resize a malloc_elem by expanding into any free space
364 * immediately after it in memory.
367 malloc_elem_resize(struct malloc_elem *elem, size_t size);
370 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
373 malloc_elem_free_list_remove(struct malloc_elem *elem);
376 * dump contents of malloc elem to a file.
379 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
382 * Given an element size, compute its freelist index.
385 malloc_elem_free_list_index(size_t size);
388 * Add element to its heap's free list.
391 malloc_elem_free_list_insert(struct malloc_elem *elem);
394 * Find biggest IOVA-contiguous zone within an element with specified alignment.
397 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
399 #endif /* MALLOC_ELEM_H_ */