9db416f25e0631b2fa5f85b25f6d4f464102e768
[dpdk.git] / lib / librte_eal / common / malloc_elem.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <inttypes.h>
5 #include <stdint.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/queue.h>
11
12 #include <rte_memory.h>
13 #include <rte_eal.h>
14 #include <rte_launch.h>
15 #include <rte_per_lcore.h>
16 #include <rte_lcore.h>
17 #include <rte_debug.h>
18 #include <rte_common.h>
19 #include <rte_spinlock.h>
20
21 #include "eal_memalloc.h"
22 #include "malloc_elem.h"
23 #include "malloc_heap.h"
24
25 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
26
27 /*
28  * Initialize a general malloc_elem header structure
29  */
30 void
31 malloc_elem_init(struct malloc_elem *elem, struct malloc_heap *heap,
32                 struct rte_memseg_list *msl, size_t size)
33 {
34         elem->heap = heap;
35         elem->msl = msl;
36         elem->prev = NULL;
37         elem->next = NULL;
38         memset(&elem->free_list, 0, sizeof(elem->free_list));
39         elem->state = ELEM_FREE;
40         elem->size = size;
41         elem->pad = 0;
42         set_header(elem);
43         set_trailer(elem);
44 }
45
46 void
47 malloc_elem_insert(struct malloc_elem *elem)
48 {
49         struct malloc_elem *prev_elem, *next_elem;
50         struct malloc_heap *heap = elem->heap;
51
52         if (heap->first == NULL && heap->last == NULL) {
53                 /* if empty heap */
54                 heap->first = elem;
55                 heap->last = elem;
56                 prev_elem = NULL;
57                 next_elem = NULL;
58         } else if (elem < heap->first) {
59                 /* if lower than start */
60                 prev_elem = NULL;
61                 next_elem = heap->first;
62                 heap->first = elem;
63         } else if (elem > heap->last) {
64                 /* if higher than end */
65                 prev_elem = heap->last;
66                 next_elem = NULL;
67                 heap->last = elem;
68         } else {
69                 /* the new memory is somewhere inbetween start and end */
70                 uint64_t dist_from_start, dist_from_end;
71
72                 dist_from_end = RTE_PTR_DIFF(heap->last, elem);
73                 dist_from_start = RTE_PTR_DIFF(elem, heap->first);
74
75                 /* check which is closer, and find closest list entries */
76                 if (dist_from_start < dist_from_end) {
77                         prev_elem = heap->first;
78                         while (prev_elem->next < elem)
79                                 prev_elem = prev_elem->next;
80                         next_elem = prev_elem->next;
81                 } else {
82                         next_elem = heap->last;
83                         while (next_elem->prev > elem)
84                                 next_elem = next_elem->prev;
85                         prev_elem = next_elem->prev;
86                 }
87         }
88
89         /* insert new element */
90         elem->prev = prev_elem;
91         elem->next = next_elem;
92         if (prev_elem)
93                 prev_elem->next = elem;
94         if (next_elem)
95                 next_elem->prev = elem;
96 }
97
98 /*
99  * Attempt to find enough physically contiguous memory in this block to store
100  * our data. Assume that element has at least enough space to fit in the data,
101  * so we just check the page addresses.
102  */
103 static bool
104 elem_check_phys_contig(const struct rte_memseg_list *msl,
105                 void *start, size_t size)
106 {
107         return eal_memalloc_is_contig(msl, start, size);
108 }
109
110 /*
111  * calculate the starting point of where data of the requested size
112  * and alignment would fit in the current element. If the data doesn't
113  * fit, return NULL.
114  */
115 static void *
116 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
117                 size_t bound, bool contig)
118 {
119         size_t elem_size = elem->size;
120
121         /*
122          * we're allocating from the end, so adjust the size of element by
123          * alignment size.
124          */
125         while (elem_size >= size) {
126                 const size_t bmask = ~(bound - 1);
127                 uintptr_t end_pt = (uintptr_t)elem +
128                                 elem_size - MALLOC_ELEM_TRAILER_LEN;
129                 uintptr_t new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
130                                 align);
131                 uintptr_t new_elem_start;
132
133                 /* check boundary */
134                 if ((new_data_start & bmask) != ((end_pt - 1) & bmask)) {
135                         end_pt = RTE_ALIGN_FLOOR(end_pt, bound);
136                         new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
137                                         align);
138                         end_pt = new_data_start + size;
139
140                         if (((end_pt - 1) & bmask) != (new_data_start & bmask))
141                                 return NULL;
142                 }
143
144                 new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
145
146                 /* if the new start point is before the exist start,
147                  * it won't fit
148                  */
149                 if (new_elem_start < (uintptr_t)elem)
150                         return NULL;
151
152                 if (contig) {
153                         size_t new_data_size = end_pt - new_data_start;
154
155                         /*
156                          * if physical contiguousness was requested and we
157                          * couldn't fit all data into one physically contiguous
158                          * block, try again with lower addresses.
159                          */
160                         if (!elem_check_phys_contig(elem->msl,
161                                         (void *)new_data_start,
162                                         new_data_size)) {
163                                 elem_size -= align;
164                                 continue;
165                         }
166                 }
167                 return (void *)new_elem_start;
168         }
169         return NULL;
170 }
171
172 /*
173  * use elem_start_pt to determine if we get meet the size and
174  * alignment request from the current element
175  */
176 int
177 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,     unsigned align,
178                 size_t bound, bool contig)
179 {
180         return elem_start_pt(elem, size, align, bound, contig) != NULL;
181 }
182
183 /*
184  * split an existing element into two smaller elements at the given
185  * split_pt parameter.
186  */
187 static void
188 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
189 {
190         struct malloc_elem *next_elem = elem->next;
191         const size_t old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
192         const size_t new_elem_size = elem->size - old_elem_size;
193
194         malloc_elem_init(split_pt, elem->heap, elem->msl, new_elem_size);
195         split_pt->prev = elem;
196         split_pt->next = next_elem;
197         if (next_elem)
198                 next_elem->prev = split_pt;
199         else
200                 elem->heap->last = split_pt;
201         elem->next = split_pt;
202         elem->size = old_elem_size;
203         set_trailer(elem);
204 }
205
206 /*
207  * our malloc heap is a doubly linked list, so doubly remove our element.
208  */
209 static void __rte_unused
210 remove_elem(struct malloc_elem *elem)
211 {
212         struct malloc_elem *next, *prev;
213         next = elem->next;
214         prev = elem->prev;
215
216         if (next)
217                 next->prev = prev;
218         else
219                 elem->heap->last = prev;
220         if (prev)
221                 prev->next = next;
222         else
223                 elem->heap->first = next;
224
225         elem->prev = NULL;
226         elem->next = NULL;
227 }
228
229 static int
230 next_elem_is_adjacent(struct malloc_elem *elem)
231 {
232         return elem->next == RTE_PTR_ADD(elem, elem->size);
233 }
234
235 static int
236 prev_elem_is_adjacent(struct malloc_elem *elem)
237 {
238         return elem == RTE_PTR_ADD(elem->prev, elem->prev->size);
239 }
240
241 /*
242  * Given an element size, compute its freelist index.
243  * We free an element into the freelist containing similarly-sized elements.
244  * We try to allocate elements starting with the freelist containing
245  * similarly-sized elements, and if necessary, we search freelists
246  * containing larger elements.
247  *
248  * Example element size ranges for a heap with five free lists:
249  *   heap->free_head[0] - (0   , 2^8]
250  *   heap->free_head[1] - (2^8 , 2^10]
251  *   heap->free_head[2] - (2^10 ,2^12]
252  *   heap->free_head[3] - (2^12, 2^14]
253  *   heap->free_head[4] - (2^14, MAX_SIZE]
254  */
255 size_t
256 malloc_elem_free_list_index(size_t size)
257 {
258 #define MALLOC_MINSIZE_LOG2   8
259 #define MALLOC_LOG2_INCREMENT 2
260
261         size_t log2;
262         size_t index;
263
264         if (size <= (1UL << MALLOC_MINSIZE_LOG2))
265                 return 0;
266
267         /* Find next power of 2 >= size. */
268         log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
269
270         /* Compute freelist index, based on log2(size). */
271         index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
272                 MALLOC_LOG2_INCREMENT;
273
274         return index <= RTE_HEAP_NUM_FREELISTS-1?
275                 index: RTE_HEAP_NUM_FREELISTS-1;
276 }
277
278 /*
279  * Add the specified element to its heap's free list.
280  */
281 void
282 malloc_elem_free_list_insert(struct malloc_elem *elem)
283 {
284         size_t idx;
285
286         idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
287         elem->state = ELEM_FREE;
288         LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
289 }
290
291 /*
292  * Remove the specified element from its heap's free list.
293  */
294 void
295 malloc_elem_free_list_remove(struct malloc_elem *elem)
296 {
297         LIST_REMOVE(elem, free_list);
298 }
299
300 /*
301  * reserve a block of data in an existing malloc_elem. If the malloc_elem
302  * is much larger than the data block requested, we split the element in two.
303  * This function is only called from malloc_heap_alloc so parameter checking
304  * is not done here, as it's done there previously.
305  */
306 struct malloc_elem *
307 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
308                 size_t bound, bool contig)
309 {
310         struct malloc_elem *new_elem = elem_start_pt(elem, size, align, bound,
311                         contig);
312         const size_t old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
313         const size_t trailer_size = elem->size - old_elem_size - size -
314                 MALLOC_ELEM_OVERHEAD;
315
316         malloc_elem_free_list_remove(elem);
317
318         if (trailer_size > MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
319                 /* split it, too much free space after elem */
320                 struct malloc_elem *new_free_elem =
321                                 RTE_PTR_ADD(new_elem, size + MALLOC_ELEM_OVERHEAD);
322
323                 split_elem(elem, new_free_elem);
324                 malloc_elem_free_list_insert(new_free_elem);
325
326                 if (elem == elem->heap->last)
327                         elem->heap->last = new_free_elem;
328         }
329
330         if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
331                 /* don't split it, pad the element instead */
332                 elem->state = ELEM_BUSY;
333                 elem->pad = old_elem_size;
334
335                 /* put a dummy header in padding, to point to real element header */
336                 if (elem->pad > 0) { /* pad will be at least 64-bytes, as everything
337                                      * is cache-line aligned */
338                         new_elem->pad = elem->pad;
339                         new_elem->state = ELEM_PAD;
340                         new_elem->size = elem->size - elem->pad;
341                         set_header(new_elem);
342                 }
343
344                 return new_elem;
345         }
346
347         /* we are going to split the element in two. The original element
348          * remains free, and the new element is the one allocated.
349          * Re-insert original element, in case its new size makes it
350          * belong on a different list.
351          */
352         split_elem(elem, new_elem);
353         new_elem->state = ELEM_BUSY;
354         malloc_elem_free_list_insert(elem);
355
356         return new_elem;
357 }
358
359 /*
360  * join two struct malloc_elem together. elem1 and elem2 must
361  * be contiguous in memory.
362  */
363 static inline void
364 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
365 {
366         struct malloc_elem *next = elem2->next;
367         elem1->size += elem2->size;
368         if (next)
369                 next->prev = elem1;
370         else
371                 elem1->heap->last = elem1;
372         elem1->next = next;
373 }
374
375 struct malloc_elem *
376 malloc_elem_join_adjacent_free(struct malloc_elem *elem)
377 {
378         /*
379          * check if next element exists, is adjacent and is free, if so join
380          * with it, need to remove from free list.
381          */
382         if (elem->next != NULL && elem->next->state == ELEM_FREE &&
383                         next_elem_is_adjacent(elem)) {
384                 void *erase;
385
386                 /* we will want to erase the trailer and header */
387                 erase = RTE_PTR_SUB(elem->next, MALLOC_ELEM_TRAILER_LEN);
388
389                 /* remove from free list, join to this one */
390                 malloc_elem_free_list_remove(elem->next);
391                 join_elem(elem, elem->next);
392
393                 /* erase header and trailer */
394                 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
395         }
396
397         /*
398          * check if prev element exists, is adjacent and is free, if so join
399          * with it, need to remove from free list.
400          */
401         if (elem->prev != NULL && elem->prev->state == ELEM_FREE &&
402                         prev_elem_is_adjacent(elem)) {
403                 struct malloc_elem *new_elem;
404                 void *erase;
405
406                 /* we will want to erase trailer and header */
407                 erase = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
408
409                 /* remove from free list, join to this one */
410                 malloc_elem_free_list_remove(elem->prev);
411
412                 new_elem = elem->prev;
413                 join_elem(new_elem, elem);
414
415                 /* erase header and trailer */
416                 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
417
418                 elem = new_elem;
419         }
420
421         return elem;
422 }
423
424 /*
425  * free a malloc_elem block by adding it to the free list. If the
426  * blocks either immediately before or immediately after newly freed block
427  * are also free, the blocks are merged together.
428  */
429 struct malloc_elem *
430 malloc_elem_free(struct malloc_elem *elem)
431 {
432         void *ptr;
433         size_t data_len;
434
435         ptr = RTE_PTR_ADD(elem, sizeof(*elem));
436         data_len = elem->size - MALLOC_ELEM_OVERHEAD;
437
438         elem = malloc_elem_join_adjacent_free(elem);
439
440         malloc_elem_free_list_insert(elem);
441
442         /* decrease heap's count of allocated elements */
443         elem->heap->alloc_count--;
444
445         memset(ptr, 0, data_len);
446
447         return elem;
448 }
449
450 /*
451  * attempt to resize a malloc_elem by expanding into any free space
452  * immediately after it in memory.
453  */
454 int
455 malloc_elem_resize(struct malloc_elem *elem, size_t size)
456 {
457         const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
458
459         /* if we request a smaller size, then always return ok */
460         if (elem->size >= new_size)
461                 return 0;
462
463         /* check if there is a next element, it's free and adjacent */
464         if (!elem->next || elem->next->state != ELEM_FREE ||
465                         !next_elem_is_adjacent(elem))
466                 return -1;
467         if (elem->size + elem->next->size < new_size)
468                 return -1;
469
470         /* we now know the element fits, so remove from free list,
471          * join the two
472          */
473         malloc_elem_free_list_remove(elem->next);
474         join_elem(elem, elem->next);
475
476         if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
477                 /* now we have a big block together. Lets cut it down a bit, by splitting */
478                 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
479                 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
480                 split_elem(elem, split_pt);
481                 malloc_elem_free_list_insert(split_pt);
482         }
483         return 0;
484 }
485
486 static inline const char *
487 elem_state_to_str(enum elem_state state)
488 {
489         switch (state) {
490         case ELEM_PAD:
491                 return "PAD";
492         case ELEM_BUSY:
493                 return "BUSY";
494         case ELEM_FREE:
495                 return "FREE";
496         }
497         return "ERROR";
498 }
499
500 void
501 malloc_elem_dump(const struct malloc_elem *elem, FILE *f)
502 {
503         fprintf(f, "Malloc element at %p (%s)\n", elem,
504                         elem_state_to_str(elem->state));
505         fprintf(f, "  len: 0x%zx pad: 0x%" PRIx32 "\n", elem->size, elem->pad);
506         fprintf(f, "  prev: %p next: %p\n", elem->prev, elem->next);
507 }