xref: /dpdk/lib/eal/common/malloc_elem.h (revision 64be0e77)
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 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
11 
12 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
13 struct malloc_heap;
14 
15 enum elem_state {
16 	ELEM_FREE = 0,
17 	ELEM_BUSY,
18 	ELEM_PAD  /* element is a padding-only header */
19 };
20 
21 struct malloc_elem {
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;
31 	uint32_t pad;
32 	size_t size;
33 	struct malloc_elem *orig_elem;
34 	size_t orig_size;
35 #ifdef RTE_MALLOC_DEBUG
36 	uint64_t header_cookie;         /* Cookie marking start of data */
37 	                                /* trailer cookie at start + size */
38 #endif
39 #ifdef RTE_MALLOC_ASAN
40 	size_t user_size;
41 	uint64_t asan_cookie[2]; /* must be next to header_cookie */
42 #endif
43 } __rte_cache_aligned;
44 
45 static const unsigned int MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
46 
47 #ifndef RTE_MALLOC_DEBUG
48 #ifdef RTE_MALLOC_ASAN
49 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
50 #else
51 static const unsigned int MALLOC_ELEM_TRAILER_LEN;
52 #endif
53 
54 /* dummy function - just check if pointer is non-null */
55 static inline int
56 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
57 
58 /* dummy function - no header if malloc_debug is not enabled */
59 static inline void
60 set_header(struct malloc_elem *elem __rte_unused){ }
61 
62 /* dummy function - no trailer if malloc_debug is not enabled */
63 static inline void
64 set_trailer(struct malloc_elem *elem __rte_unused){ }
65 
66 
67 #else
68 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
69 
70 #define MALLOC_HEADER_COOKIE   0xbadbadbadadd2e55ULL /**< Header cookie. */
71 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
72 
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)
77 
78 static inline void
79 set_header(struct malloc_elem *elem)
80 {
81 	if (elem != NULL)
82 		MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
83 }
84 
85 static inline void
86 set_trailer(struct malloc_elem *elem)
87 {
88 	if (elem != NULL)
89 		MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
90 }
91 
92 /* check that the header and trailer cookies are set correctly */
93 static inline int
94 malloc_elem_cookies_ok(const struct malloc_elem *elem)
95 {
96 	return elem != NULL &&
97 			MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
98 			MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
99 }
100 
101 #endif
102 
103 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
104 
105 #ifdef RTE_MALLOC_ASAN
106 
107 /*
108  * ASAN_SHADOW_OFFSET should match to the corresponding
109  * value defined in gcc/libsanitizer/asan/asan_mapping.h
110  */
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 #endif
116 
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
121 
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)
125 
126 #if defined(__clang__)
127 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
128 #else
129 #define __rte_no_asan __attribute__((no_sanitize_address))
130 #endif
131 
132 __rte_no_asan
133 static inline void
134 asan_set_shadow(void *addr, char val)
135 {
136 	*(char *)addr = val;
137 }
138 
139 static inline void
140 asan_set_zone(void *ptr, size_t len, uint32_t val)
141 {
142 	size_t offset, i;
143 	void *shadow;
144 	size_t zone_len = len / ASAN_SHADOW_GRAIN_SIZE;
145 	if (len % ASAN_SHADOW_GRAIN_SIZE != 0)
146 		zone_len += 1;
147 
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);
152 	}
153 }
154 
155 /*
156  * When the memory is released, the release mark is
157  * set in the corresponding range of the shadow area.
158  */
159 static inline void
160 asan_set_freezone(void *ptr, size_t size)
161 {
162 	asan_set_zone(ptr, size, ASAN_MEM_FREE_FLAG);
163 }
164 
165 /*
166  * When the memory is allocated, memory state must set as accessible.
167  */
168 static inline void
169 asan_clear_alloczone(struct malloc_elem *elem)
170 {
171 	asan_set_zone((void *)elem, elem->size, 0x0);
172 }
173 
174 static inline void
175 asan_clear_split_alloczone(struct malloc_elem *elem)
176 {
177 	void *ptr = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
178 	asan_set_zone(ptr, MALLOC_ELEM_OVERHEAD, 0x0);
179 }
180 
181 /*
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.
185  */
186 static inline void
187 asan_set_redzone(struct malloc_elem *elem, size_t user_size)
188 {
189 	uintptr_t head_redzone;
190 	uintptr_t tail_redzone;
191 	void *front_shadow;
192 	void *tail_shadow;
193 	uint32_t val;
194 
195 	if (elem != NULL) {
196 		if (elem->state != ELEM_PAD)
197 			elem = RTE_PTR_ADD(elem, elem->pad);
198 
199 		elem->user_size = user_size;
200 
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);
209 
210 		/* Set mark after the end of the allocated memory */
211 		tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
212 			MALLOC_ELEM_HEADER_LEN
213 			+ elem->user_size);
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);
221 	}
222 }
223 
224 /*
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.
228  */
229 static inline void
230 asan_clear_redzone(struct malloc_elem *elem)
231 {
232 	uintptr_t head_redzone;
233 	uintptr_t tail_redzone;
234 	void *head_shadow;
235 	void *tail_shadow;
236 
237 	if (elem != NULL) {
238 		elem = RTE_PTR_ADD(elem, elem->pad);
239 
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);
248 
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);
257 	}
258 }
259 
260 static inline size_t
261 old_malloc_size(struct malloc_elem *elem)
262 {
263 	if (elem->state != ELEM_PAD)
264 		elem = RTE_PTR_ADD(elem, elem->pad);
265 
266 	return elem->user_size;
267 }
268 
269 #else /* !RTE_MALLOC_ASAN */
270 
271 #define __rte_no_asan
272 
273 static inline void
274 asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { }
275 
276 static inline void
277 asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { }
278 
279 static inline void
280 asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { }
281 
282 static inline void
283 asan_set_redzone(struct malloc_elem *elem __rte_unused,
284 					size_t user_size __rte_unused) { }
285 
286 static inline void
287 asan_clear_redzone(struct malloc_elem *elem __rte_unused) { }
288 
289 static inline size_t
290 old_malloc_size(struct malloc_elem *elem)
291 {
292 	return elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
293 }
294 #endif /* !RTE_MALLOC_ASAN */
295 
296 /*
297  * Given a pointer to the start of a memory block returned by malloc, get
298  * the actual malloc_elem header for that block.
299  */
300 static inline struct malloc_elem *
301 malloc_elem_from_data(const void *data)
302 {
303 	if (data == NULL)
304 		return NULL;
305 
306 	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
307 	if (!malloc_elem_cookies_ok(elem))
308 		return NULL;
309 	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
310 }
311 
312 /*
313  * initialise a malloc_elem header
314  */
315 void
316 malloc_elem_init(struct malloc_elem *elem,
317 		struct malloc_heap *heap,
318 		struct rte_memseg_list *msl,
319 		size_t size,
320 		struct malloc_elem *orig_elem,
321 		size_t orig_size);
322 
323 void
324 malloc_elem_insert(struct malloc_elem *elem);
325 
326 /*
327  * return true if the current malloc_elem can hold a block of data
328  * of the requested size and with the requested alignment
329  */
330 int
331 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
332 		unsigned int align, size_t bound, bool contig);
333 
334 /*
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.
337  */
338 struct malloc_elem *
339 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
340 		unsigned int align, size_t bound, bool contig);
341 
342 /*
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.
346  */
347 struct malloc_elem *
348 malloc_elem_free(struct malloc_elem *elem);
349 
350 struct malloc_elem *
351 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
352 
353 /*
354  * attempt to resize a malloc_elem by expanding into any free space
355  * immediately after it in memory.
356  */
357 int
358 malloc_elem_resize(struct malloc_elem *elem, size_t size);
359 
360 void
361 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
362 
363 void
364 malloc_elem_free_list_remove(struct malloc_elem *elem);
365 
366 /*
367  * dump contents of malloc elem to a file.
368  */
369 void
370 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
371 
372 /*
373  * Given an element size, compute its freelist index.
374  */
375 size_t
376 malloc_elem_free_list_index(size_t size);
377 
378 /*
379  * Add element to its heap's free list.
380  */
381 void
382 malloc_elem_free_list_insert(struct malloc_elem *elem);
383 
384 /*
385  * Find biggest IOVA-contiguous zone within an element with specified alignment.
386  */
387 size_t
388 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
389 
390 #endif /* MALLOC_ELEM_H_ */
391