New upstream version 18.05
[deb_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
103         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
104                 return -1;
105
106         found_msl = &mcfg->memsegs[msl_idx];
107
108         malloc_heap_add_memory(heap, found_msl, ms->addr, len);
109
110         heap->total_size += len;
111
112         RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
113                         msl->socket_id);
114         return 0;
115 }
116
117 /*
118  * Iterates through the freelist for a heap to find a free element
119  * which can store data of the required size and with the requested alignment.
120  * If size is 0, find the biggest available elem.
121  * Returns null on failure, or pointer to element on success.
122  */
123 static struct malloc_elem *
124 find_suitable_element(struct malloc_heap *heap, size_t size,
125                 unsigned int flags, size_t align, size_t bound, bool contig)
126 {
127         size_t idx;
128         struct malloc_elem *elem, *alt_elem = NULL;
129
130         for (idx = malloc_elem_free_list_index(size);
131                         idx < RTE_HEAP_NUM_FREELISTS; idx++) {
132                 for (elem = LIST_FIRST(&heap->free_head[idx]);
133                                 !!elem; elem = LIST_NEXT(elem, free_list)) {
134                         if (malloc_elem_can_hold(elem, size, align, bound,
135                                         contig)) {
136                                 if (check_hugepage_sz(flags,
137                                                 elem->msl->page_sz))
138                                         return elem;
139                                 if (alt_elem == NULL)
140                                         alt_elem = elem;
141                         }
142                 }
143         }
144
145         if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
146                 return alt_elem;
147
148         return NULL;
149 }
150
151 /*
152  * Main function to allocate a block of memory from the heap.
153  * It locks the free list, scans it, and adds a new memseg if the
154  * scan fails. Once the new memseg is added, it re-scans and should return
155  * the new element after releasing the lock.
156  */
157 static void *
158 heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
159                 unsigned int flags, size_t align, size_t bound, bool contig)
160 {
161         struct malloc_elem *elem;
162
163         size = RTE_CACHE_LINE_ROUNDUP(size);
164         align = RTE_CACHE_LINE_ROUNDUP(align);
165
166         elem = find_suitable_element(heap, size, flags, align, bound, contig);
167         if (elem != NULL) {
168                 elem = malloc_elem_alloc(elem, size, align, bound, contig);
169
170                 /* increase heap's count of allocated elements */
171                 heap->alloc_count++;
172         }
173
174         return elem == NULL ? NULL : (void *)(&elem[1]);
175 }
176
177 /* this function is exposed in malloc_mp.h */
178 void
179 rollback_expand_heap(struct rte_memseg **ms, int n_segs,
180                 struct malloc_elem *elem, void *map_addr, size_t map_len)
181 {
182         if (elem != NULL) {
183                 malloc_elem_free_list_remove(elem);
184                 malloc_elem_hide_region(elem, map_addr, map_len);
185         }
186
187         eal_memalloc_free_seg_bulk(ms, n_segs);
188 }
189
190 /* this function is exposed in malloc_mp.h */
191 struct malloc_elem *
192 alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
193                 int socket, unsigned int flags, size_t align, size_t bound,
194                 bool contig, struct rte_memseg **ms, int n_segs)
195 {
196         struct rte_memseg_list *msl;
197         struct malloc_elem *elem = NULL;
198         size_t alloc_sz;
199         int allocd_pages;
200         void *ret, *map_addr;
201
202         alloc_sz = (size_t)pg_sz * n_segs;
203
204         /* first, check if we're allowed to allocate this memory */
205         if (eal_memalloc_mem_alloc_validate(socket,
206                         heap->total_size + alloc_sz) < 0) {
207                 RTE_LOG(DEBUG, EAL, "User has disallowed allocation\n");
208                 return NULL;
209         }
210
211         allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
212                         socket, true);
213
214         /* make sure we've allocated our pages... */
215         if (allocd_pages < 0)
216                 return NULL;
217
218         map_addr = ms[0]->addr;
219         msl = rte_mem_virt2memseg_list(map_addr);
220
221         /* check if we wanted contiguous memory but didn't get it */
222         if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
223                 RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
224                                 __func__);
225                 goto fail;
226         }
227
228         /* add newly minted memsegs to malloc heap */
229         elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
230
231         /* try once more, as now we have allocated new memory */
232         ret = find_suitable_element(heap, elt_size, flags, align, bound,
233                         contig);
234
235         if (ret == NULL)
236                 goto fail;
237
238         return elem;
239
240 fail:
241         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
242         return NULL;
243 }
244
245 static int
246 try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
247                 size_t elt_size, int socket, unsigned int flags, size_t align,
248                 size_t bound, bool contig)
249 {
250         struct malloc_elem *elem;
251         struct rte_memseg **ms;
252         void *map_addr;
253         size_t alloc_sz;
254         int n_segs;
255         bool callback_triggered = false;
256
257         alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
258                         MALLOC_ELEM_TRAILER_LEN, pg_sz);
259         n_segs = alloc_sz / pg_sz;
260
261         /* we can't know in advance how many pages we'll need, so we malloc */
262         ms = malloc(sizeof(*ms) * n_segs);
263
264         memset(ms, 0, sizeof(*ms) * n_segs);
265
266         if (ms == NULL)
267                 return -1;
268
269         elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
270                         bound, contig, ms, n_segs);
271
272         if (elem == NULL)
273                 goto free_ms;
274
275         map_addr = ms[0]->addr;
276
277         /* notify user about changes in memory map */
278         eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
279
280         /* notify other processes that this has happened */
281         if (request_sync()) {
282                 /* we couldn't ensure all processes have mapped memory,
283                  * so free it back and notify everyone that it's been
284                  * freed back.
285                  *
286                  * technically, we could've avoided adding memory addresses to
287                  * the map, but that would've led to inconsistent behavior
288                  * between primary and secondary processes, as those get
289                  * callbacks during sync. therefore, force primary process to
290                  * do alloc-and-rollback syncs as well.
291                  */
292                 callback_triggered = true;
293                 goto free_elem;
294         }
295         heap->total_size += alloc_sz;
296
297         RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
298                 socket, alloc_sz >> 20ULL);
299
300         free(ms);
301
302         return 0;
303
304 free_elem:
305         if (callback_triggered)
306                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
307                                 map_addr, alloc_sz);
308
309         rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
310
311         request_sync();
312 free_ms:
313         free(ms);
314
315         return -1;
316 }
317
318 static int
319 try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
320                 size_t elt_size, int socket, unsigned int flags, size_t align,
321                 size_t bound, bool contig)
322 {
323         struct malloc_mp_req req;
324         int req_result;
325
326         memset(&req, 0, sizeof(req));
327
328         req.t = REQ_TYPE_ALLOC;
329         req.alloc_req.align = align;
330         req.alloc_req.bound = bound;
331         req.alloc_req.contig = contig;
332         req.alloc_req.flags = flags;
333         req.alloc_req.elt_size = elt_size;
334         req.alloc_req.page_sz = pg_sz;
335         req.alloc_req.socket = socket;
336         req.alloc_req.heap = heap; /* it's in shared memory */
337
338         req_result = request_to_primary(&req);
339
340         if (req_result != 0)
341                 return -1;
342
343         if (req.result != REQ_RESULT_SUCCESS)
344                 return -1;
345
346         return 0;
347 }
348
349 static int
350 try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
351                 int socket, unsigned int flags, size_t align, size_t bound,
352                 bool contig)
353 {
354         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
355         int ret;
356
357         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
358
359         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
360                 ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
361                                 flags, align, bound, contig);
362         } else {
363                 ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
364                                 flags, align, bound, contig);
365         }
366
367         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
368         return ret;
369 }
370
371 static int
372 compare_pagesz(const void *a, const void *b)
373 {
374         const struct rte_memseg_list * const*mpa = a;
375         const struct rte_memseg_list * const*mpb = b;
376         const struct rte_memseg_list *msla = *mpa;
377         const struct rte_memseg_list *mslb = *mpb;
378         uint64_t pg_sz_a = msla->page_sz;
379         uint64_t pg_sz_b = mslb->page_sz;
380
381         if (pg_sz_a < pg_sz_b)
382                 return -1;
383         if (pg_sz_a > pg_sz_b)
384                 return 1;
385         return 0;
386 }
387
388 static int
389 alloc_more_mem_on_socket(struct malloc_heap *heap, size_t size, int socket,
390                 unsigned int flags, size_t align, size_t bound, bool contig)
391 {
392         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
393         struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
394         struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
395         uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
396         uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
397         uint64_t prev_pg_sz;
398         int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
399         bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
400         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
401         void *ret;
402
403         memset(requested_msls, 0, sizeof(requested_msls));
404         memset(other_msls, 0, sizeof(other_msls));
405         memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
406         memset(other_pg_sz, 0, sizeof(other_pg_sz));
407
408         /*
409          * go through memseg list and take note of all the page sizes available,
410          * and if any of them were specifically requested by the user.
411          */
412         n_requested_msls = 0;
413         n_other_msls = 0;
414         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
415                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
416
417                 if (msl->socket_id != socket)
418                         continue;
419
420                 if (msl->base_va == NULL)
421                         continue;
422
423                 /* if pages of specific size were requested */
424                 if (size_flags != 0 && check_hugepage_sz(size_flags,
425                                 msl->page_sz))
426                         requested_msls[n_requested_msls++] = msl;
427                 else if (size_flags == 0 || size_hint)
428                         other_msls[n_other_msls++] = msl;
429         }
430
431         /* sort the lists, smallest first */
432         qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
433                         compare_pagesz);
434         qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
435                         compare_pagesz);
436
437         /* now, extract page sizes we are supposed to try */
438         prev_pg_sz = 0;
439         n_requested_pg_sz = 0;
440         for (i = 0; i < n_requested_msls; i++) {
441                 uint64_t pg_sz = requested_msls[i]->page_sz;
442
443                 if (prev_pg_sz != pg_sz) {
444                         requested_pg_sz[n_requested_pg_sz++] = pg_sz;
445                         prev_pg_sz = pg_sz;
446                 }
447         }
448         prev_pg_sz = 0;
449         n_other_pg_sz = 0;
450         for (i = 0; i < n_other_msls; i++) {
451                 uint64_t pg_sz = other_msls[i]->page_sz;
452
453                 if (prev_pg_sz != pg_sz) {
454                         other_pg_sz[n_other_pg_sz++] = pg_sz;
455                         prev_pg_sz = pg_sz;
456                 }
457         }
458
459         /* finally, try allocating memory of specified page sizes, starting from
460          * the smallest sizes
461          */
462         for (i = 0; i < n_requested_pg_sz; i++) {
463                 uint64_t pg_sz = requested_pg_sz[i];
464
465                 /*
466                  * do not pass the size hint here, as user expects other page
467                  * sizes first, before resorting to best effort allocation.
468                  */
469                 if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
470                                 align, bound, contig))
471                         return 0;
472         }
473         if (n_other_pg_sz == 0)
474                 return -1;
475
476         /* now, check if we can reserve anything with size hint */
477         ret = find_suitable_element(heap, size, flags, align, bound, contig);
478         if (ret != NULL)
479                 return 0;
480
481         /*
482          * we still couldn't reserve memory, so try expanding heap with other
483          * page sizes, if there are any
484          */
485         for (i = 0; i < n_other_pg_sz; i++) {
486                 uint64_t pg_sz = other_pg_sz[i];
487
488                 if (!try_expand_heap(heap, pg_sz, size, socket, flags,
489                                 align, bound, contig))
490                         return 0;
491         }
492         return -1;
493 }
494
495 /* this will try lower page sizes first */
496 static void *
497 heap_alloc_on_socket(const char *type, size_t size, int socket,
498                 unsigned int flags, size_t align, size_t bound, bool contig)
499 {
500         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
501         struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
502         unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
503         void *ret;
504
505         rte_spinlock_lock(&(heap->lock));
506
507         align = align == 0 ? 1 : align;
508
509         /* for legacy mode, try once and with all flags */
510         if (internal_config.legacy_mem) {
511                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
512                 goto alloc_unlock;
513         }
514
515         /*
516          * we do not pass the size hint here, because even if allocation fails,
517          * we may still be able to allocate memory from appropriate page sizes,
518          * we just need to request more memory first.
519          */
520         ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
521         if (ret != NULL)
522                 goto alloc_unlock;
523
524         if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
525                         contig)) {
526                 ret = heap_alloc(heap, type, size, flags, align, bound, contig);
527
528                 /* this should have succeeded */
529                 if (ret == NULL)
530                         RTE_LOG(ERR, EAL, "Error allocating from heap\n");
531         }
532 alloc_unlock:
533         rte_spinlock_unlock(&(heap->lock));
534         return ret;
535 }
536
537 void *
538 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
539                 unsigned int flags, size_t align, size_t bound, bool contig)
540 {
541         int socket, i, cur_socket;
542         void *ret;
543
544         /* return NULL if size is 0 or alignment is not power-of-2 */
545         if (size == 0 || (align && !rte_is_power_of_2(align)))
546                 return NULL;
547
548         if (!rte_eal_has_hugepages())
549                 socket_arg = SOCKET_ID_ANY;
550
551         if (socket_arg == SOCKET_ID_ANY)
552                 socket = malloc_get_numa_socket();
553         else
554                 socket = socket_arg;
555
556         /* Check socket parameter */
557         if (socket >= RTE_MAX_NUMA_NODES)
558                 return NULL;
559
560         ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
561                         contig);
562         if (ret != NULL || socket_arg != SOCKET_ID_ANY)
563                 return ret;
564
565         /* try other heaps */
566         for (i = 0; i < (int) rte_socket_count(); i++) {
567                 cur_socket = rte_socket_id_by_idx(i);
568                 if (cur_socket == socket)
569                         continue;
570                 ret = heap_alloc_on_socket(type, size, cur_socket, flags,
571                                 align, bound, contig);
572                 if (ret != NULL)
573                         return ret;
574         }
575         return NULL;
576 }
577
578 /* this function is exposed in malloc_mp.h */
579 int
580 malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
581 {
582         int n_segs, seg_idx, max_seg_idx;
583         struct rte_memseg_list *msl;
584         size_t page_sz;
585
586         msl = rte_mem_virt2memseg_list(aligned_start);
587         if (msl == NULL)
588                 return -1;
589
590         page_sz = (size_t)msl->page_sz;
591         n_segs = aligned_len / page_sz;
592         seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
593         max_seg_idx = seg_idx + n_segs;
594
595         for (; seg_idx < max_seg_idx; seg_idx++) {
596                 struct rte_memseg *ms;
597
598                 ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
599                 eal_memalloc_free_seg(ms);
600         }
601         return 0;
602 }
603
604 int
605 malloc_heap_free(struct malloc_elem *elem)
606 {
607         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
608         struct malloc_heap *heap;
609         void *start, *aligned_start, *end, *aligned_end;
610         size_t len, aligned_len, page_sz;
611         struct rte_memseg_list *msl;
612         unsigned int i, n_segs, before_space, after_space;
613         int ret;
614
615         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
616                 return -1;
617
618         /* elem may be merged with previous element, so keep heap address */
619         heap = elem->heap;
620         msl = elem->msl;
621         page_sz = (size_t)msl->page_sz;
622
623         rte_spinlock_lock(&(heap->lock));
624
625         /* mark element as free */
626         elem->state = ELEM_FREE;
627
628         elem = malloc_elem_free(elem);
629
630         /* anything after this is a bonus */
631         ret = 0;
632
633         /* ...of which we can't avail if we are in legacy mode */
634         if (internal_config.legacy_mem)
635                 goto free_unlock;
636
637         /* check if we can free any memory back to the system */
638         if (elem->size < page_sz)
639                 goto free_unlock;
640
641         /* probably, but let's make sure, as we may not be using up full page */
642         start = elem;
643         len = elem->size;
644         aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
645         end = RTE_PTR_ADD(elem, len);
646         aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
647
648         aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
649
650         /* can't free anything */
651         if (aligned_len < page_sz)
652                 goto free_unlock;
653
654         /* we can free something. however, some of these pages may be marked as
655          * unfreeable, so also check that as well
656          */
657         n_segs = aligned_len / page_sz;
658         for (i = 0; i < n_segs; i++) {
659                 const struct rte_memseg *tmp =
660                                 rte_mem_virt2memseg(aligned_start, msl);
661
662                 if (tmp->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
663                         /* this is an unfreeable segment, so move start */
664                         aligned_start = RTE_PTR_ADD(tmp->addr, tmp->len);
665                 }
666         }
667
668         /* recalculate length and number of segments */
669         aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
670         n_segs = aligned_len / page_sz;
671
672         /* check if we can still free some pages */
673         if (n_segs == 0)
674                 goto free_unlock;
675
676         /* We're not done yet. We also have to check if by freeing space we will
677          * be leaving free elements that are too small to store new elements.
678          * Check if we have enough space in the beginning and at the end, or if
679          * start/end are exactly page aligned.
680          */
681         before_space = RTE_PTR_DIFF(aligned_start, elem);
682         after_space = RTE_PTR_DIFF(end, aligned_end);
683         if (before_space != 0 &&
684                         before_space < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
685                 /* There is not enough space before start, but we may be able to
686                  * move the start forward by one page.
687                  */
688                 if (n_segs == 1)
689                         goto free_unlock;
690
691                 /* move start */
692                 aligned_start = RTE_PTR_ADD(aligned_start, page_sz);
693                 aligned_len -= page_sz;
694                 n_segs--;
695         }
696         if (after_space != 0 && after_space <
697                         MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
698                 /* There is not enough space after end, but we may be able to
699                  * move the end backwards by one page.
700                  */
701                 if (n_segs == 1)
702                         goto free_unlock;
703
704                 /* move end */
705                 aligned_end = RTE_PTR_SUB(aligned_end, page_sz);
706                 aligned_len -= page_sz;
707                 n_segs--;
708         }
709
710         /* now we can finally free us some pages */
711
712         rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
713
714         /*
715          * we allow secondary processes to clear the heap of this allocated
716          * memory because it is safe to do so, as even if notifications about
717          * unmapped pages don't make it to other processes, heap is shared
718          * across all processes, and will become empty of this memory anyway,
719          * and nothing can allocate it back unless primary process will be able
720          * to deliver allocation message to every single running process.
721          */
722
723         malloc_elem_free_list_remove(elem);
724
725         malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
726
727         heap->total_size -= aligned_len;
728
729         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
730                 /* notify user about changes in memory map */
731                 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
732                                 aligned_start, aligned_len);
733
734                 /* don't care if any of this fails */
735                 malloc_heap_free_pages(aligned_start, aligned_len);
736
737                 request_sync();
738         } else {
739                 struct malloc_mp_req req;
740
741                 memset(&req, 0, sizeof(req));
742
743                 req.t = REQ_TYPE_FREE;
744                 req.free_req.addr = aligned_start;
745                 req.free_req.len = aligned_len;
746
747                 /*
748                  * we request primary to deallocate pages, but we don't do it
749                  * in this thread. instead, we notify primary that we would like
750                  * to deallocate pages, and this process will receive another
751                  * request (in parallel) that will do it for us on another
752                  * thread.
753                  *
754                  * we also don't really care if this succeeds - the data is
755                  * already removed from the heap, so it is, for all intents and
756                  * purposes, hidden from the rest of DPDK even if some other
757                  * process (including this one) may have these pages mapped.
758                  *
759                  * notifications about deallocated memory happen during sync.
760                  */
761                 request_to_primary(&req);
762         }
763
764         RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
765                 msl->socket_id, aligned_len >> 20ULL);
766
767         rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
768 free_unlock:
769         rte_spinlock_unlock(&(heap->lock));
770         return ret;
771 }
772
773 int
774 malloc_heap_resize(struct malloc_elem *elem, size_t size)
775 {
776         int ret;
777
778         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
779                 return -1;
780
781         rte_spinlock_lock(&(elem->heap->lock));
782
783         ret = malloc_elem_resize(elem, size);
784
785         rte_spinlock_unlock(&(elem->heap->lock));
786
787         return ret;
788 }
789
790 /*
791  * Function to retrieve data for heap on given socket
792  */
793 int
794 malloc_heap_get_stats(struct malloc_heap *heap,
795                 struct rte_malloc_socket_stats *socket_stats)
796 {
797         size_t idx;
798         struct malloc_elem *elem;
799
800         rte_spinlock_lock(&heap->lock);
801
802         /* Initialise variables for heap */
803         socket_stats->free_count = 0;
804         socket_stats->heap_freesz_bytes = 0;
805         socket_stats->greatest_free_size = 0;
806
807         /* Iterate through free list */
808         for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
809                 for (elem = LIST_FIRST(&heap->free_head[idx]);
810                         !!elem; elem = LIST_NEXT(elem, free_list))
811                 {
812                         socket_stats->free_count++;
813                         socket_stats->heap_freesz_bytes += elem->size;
814                         if (elem->size > socket_stats->greatest_free_size)
815                                 socket_stats->greatest_free_size = elem->size;
816                 }
817         }
818         /* Get stats on overall heap and allocated memory on this heap */
819         socket_stats->heap_totalsz_bytes = heap->total_size;
820         socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
821                         socket_stats->heap_freesz_bytes);
822         socket_stats->alloc_count = heap->alloc_count;
823
824         rte_spinlock_unlock(&heap->lock);
825         return 0;
826 }
827
828 /*
829  * Function to retrieve data for heap on given socket
830  */
831 void
832 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
833 {
834         struct malloc_elem *elem;
835
836         rte_spinlock_lock(&heap->lock);
837
838         fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
839         fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
840
841         elem = heap->first;
842         while (elem) {
843                 malloc_elem_dump(elem, f);
844                 elem = elem->next;
845         }
846
847         rte_spinlock_unlock(&heap->lock);
848 }
849
850 int
851 rte_eal_malloc_heap_init(void)
852 {
853         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
854
855         if (register_mp_requests()) {
856                 RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
857                 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
858                 return -1;
859         }
860
861         /* unlock mem hotplug here. it's safe for primary as no requests can
862          * even come before primary itself is fully initialized, and secondaries
863          * do not need to initialize the heap.
864          */
865         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
866
867         /* secondary process does not need to initialize anything */
868         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
869                 return 0;
870
871         /* add all IOVA-contiguous areas to the heap */
872         return rte_memseg_contig_walk(malloc_add_seg, NULL);
873 }