malloc: support multiprocess memory hotplug
[dpdk.git] / lib / librte_eal / common / malloc_heap.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <stdint.h>
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <sys/queue.h>
11
12 #include <rte_memory.h>
13 #include <rte_errno.h>
14 #include <rte_eal.h>
15 #include <rte_eal_memconfig.h>
16 #include <rte_launch.h>
17 #include <rte_per_lcore.h>
18 #include <rte_lcore.h>
19 #include <rte_common.h>
20 #include <rte_string_fns.h>
21 #include <rte_spinlock.h>
22 #include <rte_memcpy.h>
23 #include <rte_atomic.h>
24 #include <rte_fbarray.h>
25
26 #include "eal_internal_cfg.h"
27 #include "eal_memalloc.h"
28 #include "malloc_elem.h"
29 #include "malloc_heap.h"
30 #include "malloc_mp.h"
31
32 static unsigned
33 check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
34 {
35         unsigned check_flag = 0;
36
37         if (!(flags & ~RTE_MEMZONE_SIZE_HINT_ONLY))
38                 return 1;
39
40         switch (hugepage_sz) {
41         case RTE_PGSIZE_256K:
42                 check_flag = RTE_MEMZONE_256KB;
43                 break;
44         case RTE_PGSIZE_2M:
45                 check_flag = RTE_MEMZONE_2MB;
46                 break;
47         case RTE_PGSIZE_16M:
48                 check_flag = RTE_MEMZONE_16MB;
49                 break;
50         case RTE_PGSIZE_256M:
51                 check_flag = RTE_MEMZONE_256MB;
52                 break;
53         case RTE_PGSIZE_512M:
54                 check_flag = RTE_MEMZONE_512MB;
55                 break;
56         case RTE_PGSIZE_1G:
57                 check_flag = RTE_MEMZONE_1GB;
58                 break;
59         case RTE_PGSIZE_4G:
60                 check_flag = RTE_MEMZONE_4GB;
61                 break;
62         case RTE_PGSIZE_16G:
63                 check_flag = RTE_MEMZONE_16GB;
64         }
65
66         return check_flag & flags;
67 }
68
69 /*
70  * Expand the heap with a memory area.
71  */
72 static struct malloc_elem *
73 malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
74                 void *start, size_t len)
75 {
76         struct malloc_elem *elem = start;
77
78         malloc_elem_init(elem, heap, msl, len);
79
80         malloc_elem_insert(elem);
81
82         elem = malloc_elem_join_adjacent_free(elem);
83
84         malloc_elem_free_list_insert(elem);
85
86         return elem;
87 }
88
89 static int
90 malloc_add_seg(const struct rte_memseg_list *msl,
91                 const struct rte_memseg *ms, size_t len, void *arg __rte_unused)
92 {
93         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
94         struct rte_memseg_list *found_msl;
95         struct malloc_heap *heap;
96         int msl_idx;
97
98         heap = &mcfg->malloc_heaps[msl->socket_id];
99
100         /* msl is const, so find it */
101         msl_idx = msl - mcfg->memsegs;
102         found_msl = &mcfg->memsegs[msl_idx];
103
104         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
105                 return -1;
106
107         malloc_heap_add_memory(heap, found_msl, ms->addr, len);
108
109         RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
110                         msl->socket_id);
111         return 0;
112 }
113
114 /*
115  * Iterates through the freelist for a heap to find a free element
116  * which can store data of the required size and with the requested alignment.
117  * If size is 0, find the biggest available elem.
118  * Returns null on failure, or pointer to element on success.
119  */
120 static struct malloc_elem *
121 find_suitable_element(struct malloc_heap *heap, size_t size,
122                 unsigned int flags, size_t align, size_t bound, bool contig)
123 {
124         size_t idx;
125         struct malloc_elem *elem, *alt_elem = NULL;
126
127         for (idx = malloc_elem_free_list_index(size);
128                         idx < RTE_HEAP_NUM_FREELISTS; idx++) {
129                 for (elem = LIST_FIRST(&heap->free_head[idx]);
130                                 !!elem; elem = LIST_NEXT(elem, free_list)) {
131                         if (malloc_elem_can_hold(elem, size, align, bound,
132                                         contig)) {
133                                 if (check_hugepage_sz(flags,
134                                                 elem->msl->page_sz))
135                                         return elem;
136                                 if (alt_elem == NULL)
137                                         alt_elem = elem;
138                         }
139                 }
140         }
141
142         if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
143                 return alt_elem;
144
145         return NULL;
146 }
147
148 /*
149  * Main function to allocate a block of memory from the heap.
150  * It locks the free list, scans it, and adds a new memseg if the
151  * scan fails. Once the new memseg is added, it re-scans and should return
152  * the new element after releasing the lock.
153  */
154 static void *
155 heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
156                 unsigned int flags, size_t align, size_t bound, bool contig)
157 {
158         struct malloc_elem *elem;
159
160         size = RTE_CACHE_LINE_ROUNDUP(size);
161         align = RTE_CACHE_LINE_ROUNDUP(align);
162
163         elem = find_suitable_element(heap, size, flags, align, bound, contig);
164         if (elem != NULL) {
165                 elem = malloc_elem_alloc(elem, size, align, bound, contig);
166
167                 /* increase heap's count of allocated elements */
168                 heap->alloc_count++;
169         }
170
171         return elem == NULL ? NULL : (void *)(&elem[1]);
172 }
173
174 /* this function is exposed in malloc_mp.h */
175 void
176 rollback_expand_heap(struct rte_memseg **ms, int n_segs,
177                 struct malloc_elem *elem, void *map_addr, size_t map_len)
178 {
179         if (elem != NULL) {
180                 malloc_elem_free_list_remove(elem);
181                 malloc_elem_hide_region(elem, map_addr, map_len);
182         }
183
184         eal_memalloc_free_seg_bulk(ms, n_segs);
185 }
186
187 /* this function is exposed in malloc_mp.h */
188 struct malloc_elem *
189 alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
190                 int socket, unsigned int flags, size_t align, size_t bound,
191                 bool contig, struct rte_memseg **ms, int n_segs)
192 {
193         struct rte_memseg_list *msl;
194         struct malloc_elem *elem = NULL;
195         size_t alloc_sz;
196         int allocd_pages;
197         void *ret, *map_addr;
198
199         allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
200                         socket, true);
201
202         /* make sure we've allocated our pages... */
203         if (allocd_pages < 0)
204                 return NULL;
205
206         map_addr = ms[0]->addr;
207         msl = rte_mem_virt2memseg_list(map_addr);
208         alloc_sz = (size_t)msl->page_sz * allocd_pages;
209
210         /* check if we wanted contiguous memory but didn't get it */
211         if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
212                 RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
213                                 __func__);
214                 goto fail;
215         }
216
217         /* add newly minted memsegs to malloc heap */
218         elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
219
220         /* try once more, as now we have allocated new memory */
221         ret = find_suitable_element(heap, elt_size, flags, align, bound,
222                         contig);
223
224         if (ret == NULL)
225                 goto fail;
226
227         return elem;
228
229 fail:
230         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
231         return NULL;
232 }
233
234 static int
235 try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
236                 size_t elt_size, int socket, unsigned int flags, size_t align,
237                 size_t bound, bool contig)
238 {
239         struct malloc_elem *elem;
240         struct rte_memseg **ms;
241         void *map_addr;
242         size_t alloc_sz;
243         int n_segs;
244
245         alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
246                         MALLOC_ELEM_TRAILER_LEN, pg_sz);
247         n_segs = alloc_sz / pg_sz;
248
249         /* we can't know in advance how many pages we'll need, so we malloc */
250         ms = malloc(sizeof(*ms) * n_segs);
251
252         memset(ms, 0, sizeof(*ms) * n_segs);
253
254         if (ms == NULL)
255                 return -1;
256
257         elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
258                         bound, contig, ms, n_segs);
259
260         if (elem == NULL)
261                 goto free_ms;
262
263         map_addr = ms[0]->addr;
264
265         /* notify other processes that this has happened */
266         if (request_sync()) {
267                 /* we couldn't ensure all processes have mapped memory,
268                  * so free it back and notify everyone that it's been
269                  * freed back.
270                  */
271                 goto free_elem;
272         }
273         heap->total_size += alloc_sz;
274
275         RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
276                 socket, alloc_sz >> 20ULL);
277
278         free(ms);
279
280         return 0;
281
282 free_elem:
283         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
284
285         request_sync();
286 free_ms:
287         free(ms);
288
289         return -1;
290 }
291
292 static int
293 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
294                 size_t elt_size, int socket, unsigned int flags, size_t align,
295                 size_t bound, bool contig)
296 {
297         struct malloc_mp_req req;
298         int req_result;
299
300         memset(&req, 0, sizeof(req));
301
302         req.t = REQ_TYPE_ALLOC;
303         req.alloc_req.align = align;
304         req.alloc_req.bound = bound;
305         req.alloc_req.contig = contig;
306         req.alloc_req.flags = flags;
307         req.alloc_req.elt_size = elt_size;
308         req.alloc_req.page_sz = pg_sz;
309         req.alloc_req.socket = socket;
310         req.alloc_req.heap = heap; /* it's in shared memory */
311
312         req_result = request_to_primary(&req);
313
314         if (req_result != 0)
315                 return -1;
316
317         if (req.result != REQ_RESULT_SUCCESS)
318                 return -1;
319
320         return 0;
321 }
322
323 static int
324 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
325                 int socket, unsigned int flags, size_t align, size_t bound,
326                 bool contig)
327 {
328         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
329         int ret;
330
331         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
332
333         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
334                 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
335                                 flags, align, bound, contig);
336         } else {
337                 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
338                                 flags, align, bound, contig);
339         }
340
341         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
342         return ret;
343 }
344
345 static int
346 compare_pagesz(const void *a, const void *b)
347 {
348         const struct rte_memseg_list * const*mpa = a;
349         const struct rte_memseg_list * const*mpb = b;
350         const struct rte_memseg_list *msla = *mpa;
351         const struct rte_memseg_list *mslb = *mpb;
352         uint64_t pg_sz_a = msla->page_sz;
353         uint64_t pg_sz_b = mslb->page_sz;
354
355         if (pg_sz_a < pg_sz_b)
356                 return -1;
357         if (pg_sz_a > pg_sz_b)
358                 return 1;
359         return 0;
360 }
361
362 static int
363 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
364                 unsigned int flags, size_t align, size_t bound, bool contig)
365 {
366         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
367         struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
368         struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
369         uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
370         uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
371         uint64_t prev_pg_sz;
372         int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
373         bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
374         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
375         void *ret;
376
377         memset(requested_msls, 0, sizeof(requested_msls));
378         memset(other_msls, 0, sizeof(other_msls));
379         memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
380         memset(other_pg_sz, 0, sizeof(other_pg_sz));
381
382         /*
383          * go through memseg list and take note of all the page sizes available,
384          * and if any of them were specifically requested by the user.
385          */
386         n_requested_msls = 0;
387         n_other_msls = 0;
388         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
389                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
390
391                 if (msl->socket_id != socket)
392                         continue;
393
394                 if (msl->base_va == NULL)
395                         continue;
396
397                 /* if pages of specific size were requested */
398                 if (size_flags != 0 && check_hugepage_sz(size_flags,
399                                 msl->page_sz))
400                         requested_msls[n_requested_msls++] = msl;
401                 else if (size_flags == 0 || size_hint)
402                         other_msls[n_other_msls++] = msl;
403         }
404
405         /* sort the lists, smallest first */
406         qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
407                         compare_pagesz);
408         qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
409                         compare_pagesz);
410
411         /* now, extract page sizes we are supposed to try */
412         prev_pg_sz = 0;
413         n_requested_pg_sz = 0;
414         for (i = 0; i < n_requested_msls; i++) {
415                 uint64_t pg_sz = requested_msls[i]->page_sz;
416
417                 if (prev_pg_sz != pg_sz) {
418                         requested_pg_sz[n_requested_pg_sz++] = pg_sz;
419                         prev_pg_sz = pg_sz;
420                 }
421         }
422         prev_pg_sz = 0;
423         n_other_pg_sz = 0;
424         for (i = 0; i < n_other_msls; i++) {
425                 uint64_t pg_sz = other_msls[i]->page_sz;
426
427                 if (prev_pg_sz != pg_sz) {
428                         other_pg_sz[n_other_pg_sz++] = pg_sz;
429                         prev_pg_sz = pg_sz;
430                 }
431         }
432
433         /* finally, try allocating memory of specified page sizes, starting from
434          * the smallest sizes
435          */
436         for (i = 0; i < n_requested_pg_sz; i++) {
437                 uint64_t pg_sz = requested_pg_sz[i];
438
439                 /*
440                  * do not pass the size hint here, as user expects other page
441                  * sizes first, before resorting to best effort allocation.
442                  */
443                 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
444                                 align, bound, contig))
445                         return 0;
446         }
447         if (n_other_pg_sz == 0)
448                 return -1;
449
450         /* now, check if we can reserve anything with size hint */
451         ret = find_suitable_element(heap, size, flags, align, bound, contig);
452         if (ret != NULL)
453                 return 0;
454
455         /*
456          * we still couldn't reserve memory, so try expanding heap with other
457          * page sizes, if there are any
458          */
459         for (i = 0; i < n_other_pg_sz; i++) {
460                 uint64_t pg_sz = other_pg_sz[i];
461
462                 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
463                                 align, bound, contig))
464                         return 0;
465         }
466         return -1;
467 }
468
469 /* this will try lower page sizes first */
470 static void *
471 heap_alloc_on_socket(const char *type, size_t size, int socket,
472                 unsigned int flags, size_t align, size_t bound, bool contig)
473 {
474         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
475         struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
476         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
477         void *ret;
478
479         rte_spinlock_lock(&(heap->lock));
480
481         align = align == 0 ? 1 : align;
482
483         /* for legacy mode, try once and with all flags */
484         if (internal_config.legacy_mem) {
485                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
486                 goto alloc_unlock;
487         }
488
489         /*
490          * we do not pass the size hint here, because even if allocation fails,
491          * we may still be able to allocate memory from appropriate page sizes,
492          * we just need to request more memory first.
493          */
494         ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
495         if (ret != NULL)
496                 goto alloc_unlock;
497
498         if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
499                         contig)) {
500                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
501
502                 /* this should have succeeded */
503                 if (ret == NULL)
504                         RTE_LOG(ERR, EAL, "Error allocating from heap\n");
505         }
506 alloc_unlock:
507         rte_spinlock_unlock(&(heap->lock));
508         return ret;
509 }
510
511 void *
512 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
513                 unsigned int flags, size_t align, size_t bound, bool contig)
514 {
515         int socket, i, cur_socket;
516         void *ret;
517
518         /* return NULL if size is 0 or alignment is not power-of-2 */
519         if (size == 0 || (align && !rte_is_power_of_2(align)))
520                 return NULL;
521
522         if (!rte_eal_has_hugepages())
523                 socket_arg = SOCKET_ID_ANY;
524
525         if (socket_arg == SOCKET_ID_ANY)
526                 socket = malloc_get_numa_socket();
527         else
528                 socket = socket_arg;
529
530         /* Check socket parameter */
531         if (socket >= RTE_MAX_NUMA_NODES)
532                 return NULL;
533
534         ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
535                         contig);
536         if (ret != NULL || socket_arg != SOCKET_ID_ANY)
537                 return ret;
538
539         /* try other heaps */
540         for (i = 0; i < (int) rte_socket_count(); i++) {
541                 cur_socket = rte_socket_id_by_idx(i);
542                 if (cur_socket == socket)
543                         continue;
544                 ret = heap_alloc_on_socket(type, size, cur_socket, flags,
545                                 align, bound, contig);
546                 if (ret != NULL)
547                         return ret;
548         }
549         return NULL;
550 }
551
552 /* this function is exposed in malloc_mp.h */
553 int
554 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
555 {
556         int n_segs, seg_idx, max_seg_idx;
557         struct rte_memseg_list *msl;
558         size_t page_sz;
559
560         msl = rte_mem_virt2memseg_list(aligned_start);
561         if (msl == NULL)
562                 return -1;
563
564         page_sz = (size_t)msl->page_sz;
565         n_segs = aligned_len / page_sz;
566         seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
567         max_seg_idx = seg_idx + n_segs;
568
569         for (; seg_idx < max_seg_idx; seg_idx++) {
570                 struct rte_memseg *ms;
571
572                 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
573                 eal_memalloc_free_seg(ms);
574         }
575         return 0;
576 }
577
578 int
579 malloc_heap_free(struct malloc_elem *elem)
580 {
581         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
582         struct malloc_heap *heap;
583         void *start, *aligned_start, *end, *aligned_end;
584         size_t len, aligned_len, page_sz;
585         struct rte_memseg_list *msl;
586         int ret;
587
588         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
589                 return -1;
590
591         /* elem may be merged with previous element, so keep heap address */
592         heap = elem->heap;
593         msl = elem->msl;
594         page_sz = (size_t)msl->page_sz;
595
596         rte_spinlock_lock(&(heap->lock));
597
598         /* mark element as free */
599         elem->state = ELEM_FREE;
600
601         elem = malloc_elem_free(elem);
602
603         /* anything after this is a bonus */
604         ret = 0;
605
606         /* ...of which we can't avail if we are in legacy mode */
607         if (internal_config.legacy_mem)
608                 goto free_unlock;
609
610         /* check if we can free any memory back to the system */
611         if (elem->size < page_sz)
612                 goto free_unlock;
613
614         /* probably, but let's make sure, as we may not be using up full page */
615         start = elem;
616         len = elem->size;
617         aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
618         end = RTE_PTR_ADD(elem, len);
619         aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
620
621         aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
622
623         /* can't free anything */
624         if (aligned_len < page_sz)
625                 goto free_unlock;
626
627         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
628
629         /*
630          * we allow secondary processes to clear the heap of this allocated
631          * memory because it is safe to do so, as even if notifications about
632          * unmapped pages don't make it to other processes, heap is shared
633          * across all processes, and will become empty of this memory anyway,
634          * and nothing can allocate it back unless primary process will be able
635          * to deliver allocation message to every single running process.
636          */
637
638         malloc_elem_free_list_remove(elem);
639
640         malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
641
642         heap->total_size -= aligned_len;
643
644         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
645                 /* don't care if any of this fails */
646                 malloc_heap_free_pages(aligned_start, aligned_len);
647
648                 request_sync();
649         } else {
650                 struct malloc_mp_req req;
651
652                 memset(&req, 0, sizeof(req));
653
654                 req.t = REQ_TYPE_FREE;
655                 req.free_req.addr = aligned_start;
656                 req.free_req.len = aligned_len;
657
658                 /*
659                  * we request primary to deallocate pages, but we don't do it
660                  * in this thread. instead, we notify primary that we would like
661                  * to deallocate pages, and this process will receive another
662                  * request (in parallel) that will do it for us on another
663                  * thread.
664                  *
665                  * we also don't really care if this succeeds - the data is
666                  * already removed from the heap, so it is, for all intents and
667                  * purposes, hidden from the rest of DPDK even if some other
668                  * process (including this one) may have these pages mapped.
669                  */
670                 request_to_primary(&req);
671         }
672
673         RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
674                 msl->socket_id, aligned_len >> 20ULL);
675
676         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
677 free_unlock:
678         rte_spinlock_unlock(&(heap->lock));
679         return ret;
680 }
681
682 int
683 malloc_heap_resize(struct malloc_elem *elem, size_t size)
684 {
685         int ret;
686
687         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
688                 return -1;
689
690         rte_spinlock_lock(&(elem->heap->lock));
691
692         ret = malloc_elem_resize(elem, size);
693
694         rte_spinlock_unlock(&(elem->heap->lock));
695
696         return ret;
697 }
698
699 /*
700  * Function to retrieve data for heap on given socket
701  */
702 int
703 malloc_heap_get_stats(struct malloc_heap *heap,
704                 struct rte_malloc_socket_stats *socket_stats)
705 {
706         size_t idx;
707         struct malloc_elem *elem;
708
709         rte_spinlock_lock(&heap->lock);
710
711         /* Initialise variables for heap */
712         socket_stats->free_count = 0;
713         socket_stats->heap_freesz_bytes = 0;
714         socket_stats->greatest_free_size = 0;
715
716         /* Iterate through free list */
717         for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
718                 for (elem = LIST_FIRST(&heap->free_head[idx]);
719                         !!elem; elem = LIST_NEXT(elem, free_list))
720                 {
721                         socket_stats->free_count++;
722                         socket_stats->heap_freesz_bytes += elem->size;
723                         if (elem->size > socket_stats->greatest_free_size)
724                                 socket_stats->greatest_free_size = elem->size;
725                 }
726         }
727         /* Get stats on overall heap and allocated memory on this heap */
728         socket_stats->heap_totalsz_bytes = heap->total_size;
729         socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
730                         socket_stats->heap_freesz_bytes);
731         socket_stats->alloc_count = heap->alloc_count;
732
733         rte_spinlock_unlock(&heap->lock);
734         return 0;
735 }
736
737 /*
738  * Function to retrieve data for heap on given socket
739  */
740 void
741 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
742 {
743         struct malloc_elem *elem;
744
745         rte_spinlock_lock(&heap->lock);
746
747         fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
748         fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
749
750         elem = heap->first;
751         while (elem) {
752                 malloc_elem_dump(elem, f);
753                 elem = elem->next;
754         }
755
756         rte_spinlock_unlock(&heap->lock);
757 }
758
759 int
760 rte_eal_malloc_heap_init(void)
761 {
762         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
763
764         if (register_mp_requests()) {
765                 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
766                 return -1;
767         }
768
769         /* unlock mem hotplug here. it's safe for primary as no requests can
770          * even come before primary itself is fully initialized, and secondaries
771          * do not need to initialize the heap.
772          */
773         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
774
775         /* secondary process does not need to initialize anything */
776         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
777                 return 0;
778
779         /* add all IOVA-contiguous areas to the heap */
780         return rte_memseg_contig_walk(malloc_add_seg, NULL);
781 }