Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #ifndef MALLOC_ELEM_H_
6 : : #define MALLOC_ELEM_H_
7 : :
8 : : #include <stdbool.h>
9 : :
10 : : #include <rte_common.h>
11 : :
12 : : #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
13 : :
14 : : /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
15 : : struct malloc_heap;
16 : :
17 : : enum elem_state {
18 : : ELEM_FREE = 0,
19 : : ELEM_BUSY,
20 : : ELEM_PAD /* element is a padding-only header */
21 : : };
22 : :
23 : : struct __rte_cache_aligned malloc_elem {
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. */
36 : : uint32_t dirty : 1;
37 : : /** Reserved for future use. */
38 : : uint32_t reserved : 28;
39 : : uint32_t pad;
40 : : size_t size;
41 : : struct malloc_elem *orig_elem;
42 : : size_t orig_size;
43 : : #ifdef RTE_MALLOC_DEBUG
44 : : uint64_t header_cookie; /* Cookie marking start of data */
45 : : /* trailer cookie at start + size */
46 : : #endif
47 : : #ifdef RTE_MALLOC_ASAN
48 : : size_t user_size;
49 : : uint64_t asan_cookie[2]; /* must be next to header_cookie */
50 : : #endif
51 : : };
52 : :
53 : : static const unsigned int MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
54 : :
55 : : #ifndef RTE_MALLOC_DEBUG
56 : : #ifdef RTE_MALLOC_ASAN
57 : : static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
58 : : #else
59 : : static const unsigned int MALLOC_ELEM_TRAILER_LEN;
60 : : #endif
61 : :
62 : : /* dummy function - just check if pointer is non-null */
63 : : static inline int
64 : : malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
65 : :
66 : : /* dummy function - no header if malloc_debug is not enabled */
67 : : static inline void
68 : 57 : set_header(struct malloc_elem *elem __rte_unused){ }
69 : :
70 : : /* dummy function - no trailer if malloc_debug is not enabled */
71 : : static inline void
72 : : set_trailer(struct malloc_elem *elem __rte_unused){ }
73 : :
74 : :
75 : : #else
76 : : static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
77 : :
78 : : #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
79 : : #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
80 : :
81 : : /* define macros to make referencing the header and trailer cookies easier */
82 : : #define MALLOC_ELEM_TRAILER(elem) \
83 : : /* typeof preserves qualifiers (const/volatile) of elem */ \
84 : : (*(typeof((elem)->header_cookie) *)RTE_PTR_ADD(elem, \
85 : : (elem)->size - MALLOC_ELEM_TRAILER_LEN))
86 : : #define MALLOC_ELEM_HEADER(elem) ((elem)->header_cookie)
87 : :
88 : : static inline void
89 : : set_header(struct malloc_elem *elem)
90 : : {
91 : : if (elem != NULL)
92 : : MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
93 : : }
94 : :
95 : : static inline void
96 : : set_trailer(struct malloc_elem *elem)
97 : : {
98 : : if (elem != NULL)
99 : : MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
100 : : }
101 : :
102 : : /* check that the header and trailer cookies are set correctly */
103 : : static inline int
104 : : malloc_elem_cookies_ok(const struct malloc_elem *elem)
105 : : {
106 : : return elem != NULL &&
107 : : MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
108 : : MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
109 : : }
110 : :
111 : : #endif
112 : :
113 : : #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
114 : :
115 : : #ifdef RTE_MALLOC_ASAN
116 : :
117 : : /*
118 : : * ASAN_SHADOW_OFFSET should match to the corresponding
119 : : * value defined in gcc/libsanitizer/asan/asan_mapping.h
120 : : */
121 : : #ifdef RTE_ARCH_X86_64
122 : : #define ASAN_SHADOW_OFFSET 0x00007fff8000
123 : : #elif defined(RTE_ARCH_ARM64)
124 : : #define ASAN_SHADOW_OFFSET 0x001000000000
125 : : #elif defined(RTE_ARCH_PPC_64)
126 : : #define ASAN_SHADOW_OFFSET 0x020000000000
127 : : #endif
128 : :
129 : : #define ASAN_SHADOW_GRAIN_SIZE 8
130 : : #define ASAN_MEM_FREE_FLAG 0xfd
131 : : #define ASAN_MEM_REDZONE_FLAG 0xfa
132 : : #define ASAN_SHADOW_SCALE 3
133 : :
134 : : #define ASAN_MEM_SHIFT(mem) ((void *)((uintptr_t)(mem) >> ASAN_SHADOW_SCALE))
135 : : #define ASAN_MEM_TO_SHADOW(mem) \
136 : : RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
137 : :
138 : : __rte_no_asan
139 : : static inline void
140 : : asan_set_shadow(void *addr, char val)
141 : : {
142 : : *(char *)addr = val;
143 : : }
144 : :
145 : : static inline void
146 : : asan_set_zone(void *ptr, size_t len, uint32_t val)
147 : : {
148 : : size_t offset, i;
149 : : void *shadow;
150 : : size_t zone_len = len / ASAN_SHADOW_GRAIN_SIZE;
151 : : if (len % ASAN_SHADOW_GRAIN_SIZE != 0)
152 : : zone_len += 1;
153 : :
154 : : for (i = 0; i < zone_len; i++) {
155 : : offset = i * ASAN_SHADOW_GRAIN_SIZE;
156 : : shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
157 : : asan_set_shadow(shadow, val);
158 : : }
159 : : }
160 : :
161 : : /*
162 : : * When the memory is released, the release mark is
163 : : * set in the corresponding range of the shadow area.
164 : : */
165 : : static inline void
166 : : asan_set_freezone(void *ptr, size_t size)
167 : : {
168 : : asan_set_zone(ptr, size, ASAN_MEM_FREE_FLAG);
169 : : }
170 : :
171 : : /*
172 : : * When the memory is allocated, memory state must set as accessible.
173 : : */
174 : : static inline void
175 : : asan_clear_alloczone(struct malloc_elem *elem)
176 : : {
177 : : asan_set_zone((void *)elem, elem->size, 0x0);
178 : : }
179 : :
180 : : static inline void
181 : : asan_clear_split_alloczone(struct malloc_elem *elem)
182 : : {
183 : : void *ptr = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
184 : : asan_set_zone(ptr, MALLOC_ELEM_OVERHEAD, 0x0);
185 : : }
186 : :
187 : : /*
188 : : * When the memory is allocated, the memory boundary is
189 : : * marked in the corresponding range of the shadow area.
190 : : * Requirement: redzone >= 16, is a power of two.
191 : : */
192 : : static inline void
193 : : asan_set_redzone(struct malloc_elem *elem, size_t user_size)
194 : : {
195 : : uintptr_t head_redzone;
196 : : uintptr_t tail_redzone;
197 : : void *front_shadow;
198 : : void *tail_shadow;
199 : : uint32_t val;
200 : :
201 : : if (elem != NULL) {
202 : : if (elem->state != ELEM_PAD)
203 : : elem = RTE_PTR_ADD(elem, elem->pad);
204 : :
205 : : elem->user_size = user_size;
206 : :
207 : : /* Set mark before the start of the allocated memory */
208 : : head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
209 : : MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
210 : : front_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
211 : : asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
212 : : front_shadow = ASAN_MEM_TO_SHADOW(head_redzone
213 : : - ASAN_SHADOW_GRAIN_SIZE);
214 : : asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
215 : :
216 : : /* Set mark after the end of the allocated memory */
217 : : tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
218 : : MALLOC_ELEM_HEADER_LEN
219 : : + elem->user_size);
220 : : tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
221 : : val = (tail_redzone % ASAN_SHADOW_GRAIN_SIZE);
222 : : val = (val == 0) ? ASAN_MEM_REDZONE_FLAG : val;
223 : : asan_set_shadow(tail_shadow, val);
224 : : tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
225 : : + ASAN_SHADOW_GRAIN_SIZE);
226 : : asan_set_shadow(tail_shadow, ASAN_MEM_REDZONE_FLAG);
227 : : }
228 : : }
229 : :
230 : : /*
231 : : * When the memory is released, the mark of the memory boundary
232 : : * in the corresponding range of the shadow area is cleared.
233 : : * Requirement: redzone >= 16, is a power of two.
234 : : */
235 : : static inline void
236 : : asan_clear_redzone(struct malloc_elem *elem)
237 : : {
238 : : uintptr_t head_redzone;
239 : : uintptr_t tail_redzone;
240 : : void *head_shadow;
241 : : void *tail_shadow;
242 : :
243 : : if (elem != NULL) {
244 : : elem = RTE_PTR_ADD(elem, elem->pad);
245 : :
246 : : /* Clear mark before the start of the allocated memory */
247 : : head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
248 : : MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
249 : : head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
250 : : asan_set_shadow(head_shadow, 0x00);
251 : : head_shadow = ASAN_MEM_TO_SHADOW(head_redzone
252 : : - ASAN_SHADOW_GRAIN_SIZE);
253 : : asan_set_shadow(head_shadow, 0x00);
254 : :
255 : : /* Clear mark after the end of the allocated memory */
256 : : tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
257 : : MALLOC_ELEM_HEADER_LEN + elem->user_size);
258 : : tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
259 : : asan_set_shadow(tail_shadow, 0x00);
260 : : tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
261 : : + ASAN_SHADOW_GRAIN_SIZE);
262 : : asan_set_shadow(tail_shadow, 0x00);
263 : : }
264 : : }
265 : :
266 : : static inline size_t
267 : : old_malloc_size(struct malloc_elem *elem)
268 : : {
269 : : if (elem->state != ELEM_PAD)
270 : : elem = RTE_PTR_ADD(elem, elem->pad);
271 : :
272 : : return elem->user_size;
273 : : }
274 : :
275 : : #else /* !RTE_MALLOC_ASAN */
276 : :
277 : : static inline void
278 : : asan_set_zone(void *ptr __rte_unused, size_t len __rte_unused,
279 : : uint32_t val __rte_unused) { }
280 : :
281 : : static inline void
282 : : asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { }
283 : :
284 : : static inline void
285 : : asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { }
286 : :
287 : : static inline void
288 : : asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { }
289 : :
290 : : static inline void
291 : : asan_set_redzone(struct malloc_elem *elem __rte_unused,
292 : 121081 : size_t user_size __rte_unused) { }
293 : :
294 : : static inline void
295 : : asan_clear_redzone(struct malloc_elem *elem __rte_unused) { }
296 : :
297 : : static inline size_t
298 : : old_malloc_size(struct malloc_elem *elem)
299 : : {
300 [ - + ]: 22 : return elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
301 : : }
302 : : #endif /* !RTE_MALLOC_ASAN */
303 : :
304 : : /*
305 : : * Given a pointer to the start of a memory block returned by malloc, get
306 : : * the actual malloc_elem header for that block.
307 : : */
308 : : static inline struct malloc_elem *
309 : : malloc_elem_from_data(const void *data)
310 : : {
311 : : struct malloc_elem *result;
312 : :
313 [ + - + + ]: 109214 : if (data == NULL)
314 : : return NULL;
315 : :
316 : : /* The allocator returns a pointer in the middle of an allocation pool.
317 : : * GCC's interprocedural analysis can't trace this and warns about
318 : : * out-of-bounds access when we do backwards pointer arithmetic to
319 : : * find the malloc_elem header.
320 : : */
321 : : __rte_diagnostic_push
322 : : __rte_diagnostic_ignored_array_bounds
323 : : {
324 : : struct malloc_elem *elem =
325 : 271835 : RTE_PTR_SUB(RTE_PTR_UNQUAL(data), MALLOC_ELEM_HEADER_LEN);
326 : :
327 : : if (!malloc_elem_cookies_ok(elem))
328 : : result = NULL;
329 : : else
330 [ - + + + : 271835 : result = elem->state != ELEM_PAD ? elem :
- + + + +
+ ]
331 : 156 : RTE_PTR_SUB(elem, elem->pad);
332 : : }
333 : : __rte_diagnostic_pop
334 : :
335 : : return result;
336 : : }
337 : :
338 : : /*
339 : : * initialise a malloc_elem header
340 : : */
341 : : void
342 : : malloc_elem_init(struct malloc_elem *elem,
343 : : struct malloc_heap *heap,
344 : : struct rte_memseg_list *msl,
345 : : size_t size,
346 : : struct malloc_elem *orig_elem,
347 : : size_t orig_size,
348 : : bool dirty);
349 : :
350 : : void
351 : : malloc_elem_insert(struct malloc_elem *elem);
352 : :
353 : : /*
354 : : * return true if the current malloc_elem can hold a block of data
355 : : * of the requested size and with the requested alignment
356 : : */
357 : : int
358 : : malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
359 : : unsigned int align, size_t bound, bool contig);
360 : :
361 : : /*
362 : : * reserve a block of data in an existing malloc_elem. If the malloc_elem
363 : : * is much larger than the data block requested, we split the element in two.
364 : : */
365 : : struct malloc_elem *
366 : : malloc_elem_alloc(struct malloc_elem *elem, size_t size,
367 : : unsigned int align, size_t bound, bool contig);
368 : :
369 : : /*
370 : : * free a malloc_elem block by adding it to the free list. If the
371 : : * blocks either immediately before or immediately after newly freed block
372 : : * are also free, the blocks are merged together.
373 : : */
374 : : struct malloc_elem *
375 : : malloc_elem_free(struct malloc_elem *elem);
376 : :
377 : : struct malloc_elem *
378 : : malloc_elem_join_adjacent_free(struct malloc_elem *elem);
379 : :
380 : : /*
381 : : * attempt to resize a malloc_elem by expanding into any free space
382 : : * immediately after it in memory.
383 : : */
384 : : int
385 : : malloc_elem_resize(struct malloc_elem *elem, size_t size);
386 : :
387 : : void
388 : : malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
389 : :
390 : : void
391 : : malloc_elem_free_list_remove(struct malloc_elem *elem);
392 : :
393 : : /*
394 : : * dump contents of malloc elem to a file.
395 : : */
396 : : void
397 : : malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
398 : :
399 : : /*
400 : : * Given an element size, compute its freelist index.
401 : : */
402 : : size_t
403 : : malloc_elem_free_list_index(size_t size);
404 : :
405 : : /*
406 : : * Add element to its heap's free list.
407 : : */
408 : : void
409 : : malloc_elem_free_list_insert(struct malloc_elem *elem);
410 : :
411 : : /*
412 : : * Find biggest IOVA-contiguous zone within an element with specified alignment.
413 : : */
414 : : size_t
415 : : malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
416 : :
417 : : #endif /* MALLOC_ELEM_H_ */
|