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