malloc: enable memory hotplug support
[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 /* assume all checks were already done */
451 void
452 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)
453 {
454         struct malloc_elem *hide_start, *hide_end, *prev, *next;
455         size_t len_before, len_after;
456
457         hide_start = start;
458         hide_end = RTE_PTR_ADD(start, len);
459
460         prev = elem->prev;
461         next = elem->next;
462
463         /* we cannot do anything with non-adjacent elements */
464         if (next && next_elem_is_adjacent(elem)) {
465                 len_after = RTE_PTR_DIFF(next, hide_end);
466                 if (len_after >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
467                         /* split after */
468                         split_elem(elem, hide_end);
469
470                         malloc_elem_free_list_insert(hide_end);
471                 } else if (len_after >= MALLOC_ELEM_HEADER_LEN) {
472                         /* shrink current element */
473                         elem->size -= len_after;
474                         memset(hide_end, 0, sizeof(*hide_end));
475
476                         /* copy next element's data to our pad */
477                         memcpy(hide_end, next, sizeof(*hide_end));
478
479                         /* pad next element */
480                         next->state = ELEM_PAD;
481                         next->pad = len_after;
482                         next->size -= len_after;
483
484                         /* next element busy, would've been merged otherwise */
485                         hide_end->pad = len_after;
486                         hide_end->size += len_after;
487
488                         /* adjust pointers to point to our new pad */
489                         if (next->next)
490                                 next->next->prev = hide_end;
491                         elem->next = hide_end;
492                 } else if (len_after > 0) {
493                         RTE_LOG(ERR, EAL, "Unaligned element, heap is probably corrupt\n");
494                         return;
495                 }
496         }
497
498         /* we cannot do anything with non-adjacent elements */
499         if (prev && prev_elem_is_adjacent(elem)) {
500                 len_before = RTE_PTR_DIFF(hide_start, elem);
501                 if (len_before >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
502                         /* split before */
503                         split_elem(elem, hide_start);
504
505                         prev = elem;
506                         elem = hide_start;
507
508                         malloc_elem_free_list_insert(prev);
509                 } else if (len_before > 0) {
510                         /*
511                          * unlike with elements after current, here we don't
512                          * need to pad elements, but rather just increase the
513                          * size of previous element, copy the old header and set
514                          * up trailer.
515                          */
516                         void *trailer = RTE_PTR_ADD(prev,
517                                         prev->size - MALLOC_ELEM_TRAILER_LEN);
518
519                         memcpy(hide_start, elem, sizeof(*elem));
520                         hide_start->size = len;
521
522                         prev->size += len_before;
523                         set_trailer(prev);
524
525                         /* update pointers */
526                         prev->next = hide_start;
527                         if (next)
528                                 next->prev = hide_start;
529
530                         /* erase old trailer */
531                         memset(trailer, 0, MALLOC_ELEM_TRAILER_LEN);
532                         /* erase old header */
533                         memset(elem, 0, sizeof(*elem));
534
535                         elem = hide_start;
536                 }
537         }
538
539         remove_elem(elem);
540 }
541
542 /*
543  * attempt to resize a malloc_elem by expanding into any free space
544  * immediately after it in memory.
545  */
546 int
547 malloc_elem_resize(struct malloc_elem *elem, size_t size)
548 {
549         const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
550
551         /* if we request a smaller size, then always return ok */
552         if (elem->size >= new_size)
553                 return 0;
554
555         /* check if there is a next element, it's free and adjacent */
556         if (!elem->next || elem->next->state != ELEM_FREE ||
557                         !next_elem_is_adjacent(elem))
558                 return -1;
559         if (elem->size + elem->next->size < new_size)
560                 return -1;
561
562         /* we now know the element fits, so remove from free list,
563          * join the two
564          */
565         malloc_elem_free_list_remove(elem->next);
566         join_elem(elem, elem->next);
567
568         if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
569                 /* now we have a big block together. Lets cut it down a bit, by splitting */
570                 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
571                 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
572                 split_elem(elem, split_pt);
573                 malloc_elem_free_list_insert(split_pt);
574         }
575         return 0;
576 }
577
578 static inline const char *
579 elem_state_to_str(enum elem_state state)
580 {
581         switch (state) {
582         case ELEM_PAD:
583                 return "PAD";
584         case ELEM_BUSY:
585                 return "BUSY";
586         case ELEM_FREE:
587                 return "FREE";
588         }
589         return "ERROR";
590 }
591
592 void
593 malloc_elem_dump(const struct malloc_elem *elem, FILE *f)
594 {
595         fprintf(f, "Malloc element at %p (%s)\n", elem,
596                         elem_state_to_str(elem->state));
597         fprintf(f, "  len: 0x%zx pad: 0x%" PRIx32 "\n", elem->size, elem->pad);
598         fprintf(f, "  prev: %p next: %p\n", elem->prev, elem->next);
599 }