malloc: enable memory hotplug support
[dpdk.git] / lib / librte_eal / common / malloc_heap.c
index 8861d27..5f8c643 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
  */
 #include <stdint.h>
 #include <stddef.h>
@@ -39,7 +10,6 @@
 #include <sys/queue.h>
 
 #include <rte_memory.h>
-#include <rte_memzone.h>
 #include <rte_eal.h>
 #include <rte_eal_memconfig.h>
 #include <rte_launch.h>
 #include <rte_spinlock.h>
 #include <rte_memcpy.h>
 #include <rte_atomic.h>
+#include <rte_fbarray.h>
 
+#include "eal_internal_cfg.h"
+#include "eal_memalloc.h"
 #include "malloc_elem.h"
 #include "malloc_heap.h"
 
-/* since the memzone size starts with a digit, it will appear unquoted in
- * rte_config.h, so quote it so it can be passed to rte_str_to_size */
-#define MALLOC_MEMZONE_SIZE RTE_STR(RTE_MALLOC_MEMZONE_SIZE)
+static unsigned
+check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
+{
+       unsigned check_flag = 0;
+
+       if (!(flags & ~RTE_MEMZONE_SIZE_HINT_ONLY))
+               return 1;
+
+       switch (hugepage_sz) {
+       case RTE_PGSIZE_256K:
+               check_flag = RTE_MEMZONE_256KB;
+               break;
+       case RTE_PGSIZE_2M:
+               check_flag = RTE_MEMZONE_2MB;
+               break;
+       case RTE_PGSIZE_16M:
+               check_flag = RTE_MEMZONE_16MB;
+               break;
+       case RTE_PGSIZE_256M:
+               check_flag = RTE_MEMZONE_256MB;
+               break;
+       case RTE_PGSIZE_512M:
+               check_flag = RTE_MEMZONE_512MB;
+               break;
+       case RTE_PGSIZE_1G:
+               check_flag = RTE_MEMZONE_1GB;
+               break;
+       case RTE_PGSIZE_4G:
+               check_flag = RTE_MEMZONE_4GB;
+               break;
+       case RTE_PGSIZE_16G:
+               check_flag = RTE_MEMZONE_16GB;
+       }
+
+       return check_flag & flags;
+}
 
 /*
- * returns the configuration setting for the memzone size as a size_t value
+ * Expand the heap with a memory area.
  */
-static inline size_t
-get_malloc_memzone_size(void)
+static struct malloc_elem *
+malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
+               void *start, size_t len)
 {
-       return rte_str_to_size(MALLOC_MEMZONE_SIZE);
+       struct malloc_elem *elem = start;
+
+       malloc_elem_init(elem, heap, msl, len);
+
+       malloc_elem_insert(elem);
+
+       elem = malloc_elem_join_adjacent_free(elem);
+
+       malloc_elem_free_list_insert(elem);
+
+       heap->total_size += len;
+
+       return elem;
 }
 
-/*
- * reserve an extra memory zone and make it available for use by a particular
- * heap. This reserves the zone and sets a dummy malloc_elem header at the end
- * to prevent overflow. The rest of the zone is added to free list as a single
- * large free block
- */
 static int
-malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align)
+malloc_add_seg(const struct rte_memseg_list *msl,
+               const struct rte_memseg *ms, size_t len, void *arg __rte_unused)
 {
-       const unsigned mz_flags = 0;
-       const size_t block_size = get_malloc_memzone_size();
-       /* ensure the data we want to allocate will fit in the memzone */
-       const size_t min_size = size + align + MALLOC_ELEM_OVERHEAD * 2;
-       const struct rte_memzone *mz = NULL;
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
-       unsigned numa_socket = heap - mcfg->malloc_heaps;
+       struct rte_memseg_list *found_msl;
+       struct malloc_heap *heap;
+       int msl_idx;
 
-       size_t mz_size = min_size;
-       if (mz_size < block_size)
-               mz_size = block_size;
+       heap = &mcfg->malloc_heaps[msl->socket_id];
 
-       char mz_name[RTE_MEMZONE_NAMESIZE];
-       snprintf(mz_name, sizeof(mz_name), "MALLOC_S%u_HEAP_%u",
-                    numa_socket, heap->mz_count++);
+       /* msl is const, so find it */
+       msl_idx = msl - mcfg->memsegs;
+       found_msl = &mcfg->memsegs[msl_idx];
 
-       /* try getting a block. if we fail and we don't need as big a block
-        * as given in the config, we can shrink our request and try again
-        */
-       do {
-               mz = rte_memzone_reserve(mz_name, mz_size, numa_socket,
-                                        mz_flags);
-               if (mz == NULL)
-                       mz_size /= 2;
-       } while (mz == NULL && mz_size > min_size);
-       if (mz == NULL)
+       if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
                return -1;
 
-       /* allocate the memory block headers, one at end, one at start */
-       struct malloc_elem *start_elem = (struct malloc_elem *)mz->addr;
-       struct malloc_elem *end_elem = RTE_PTR_ADD(mz->addr,
-                       mz_size - MALLOC_ELEM_OVERHEAD);
-       end_elem = RTE_PTR_ALIGN_FLOOR(end_elem, RTE_CACHE_LINE_SIZE);
-
-       const unsigned elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem;
-       malloc_elem_init(start_elem, heap, mz, elem_size);
-       malloc_elem_mkend(end_elem, start_elem);
-       malloc_elem_free_list_insert(start_elem);
+       malloc_heap_add_memory(heap, found_msl, ms->addr, len);
 
-       /* increase heap total size by size of new memzone */
-       heap->total_size+=mz_size - MALLOC_ELEM_OVERHEAD;
+       RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
+                       msl->socket_id);
        return 0;
 }
 
 /*
  * Iterates through the freelist for a heap to find a free element
  * which can store data of the required size and with the requested alignment.
+ * If size is 0, find the biggest available elem.
  * Returns null on failure, or pointer to element on success.
  */
 static struct malloc_elem *
-find_suitable_element(struct malloc_heap *heap, size_t size, unsigned align)
+find_suitable_element(struct malloc_heap *heap, size_t size,
+               unsigned int flags, size_t align, size_t bound, bool contig)
 {
        size_t idx;
-       struct malloc_elem *elem;
+       struct malloc_elem *elem, *alt_elem = NULL;
 
        for (idx = malloc_elem_free_list_index(size);
-               idx < RTE_HEAP_NUM_FREELISTS; idx++)
-       {
+                       idx < RTE_HEAP_NUM_FREELISTS; idx++) {
                for (elem = LIST_FIRST(&heap->free_head[idx]);
-                       !!elem; elem = LIST_NEXT(elem, free_list))
-               {
-                       if (malloc_elem_can_hold(elem, size, align))
-                               return elem;
+                               !!elem; elem = LIST_NEXT(elem, free_list)) {
+                       if (malloc_elem_can_hold(elem, size, align, bound,
+                                       contig)) {
+                               if (check_hugepage_sz(flags,
+                                               elem->msl->page_sz))
+                                       return elem;
+                               if (alt_elem == NULL)
+                                       alt_elem = elem;
+                       }
                }
        }
+
+       if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
+               return alt_elem;
+
        return NULL;
 }
 
 /*
- * Main function called by malloc to allocate a block of memory from the
- * heap. It locks the free list, scans it, and adds a new memzone if the
- * scan fails. Once the new memzone is added, it re-scans and should return
+ * Main function to allocate a block of memory from the heap.
+ * It locks the free list, scans it, and adds a new memseg if the
+ * scan fails. Once the new memseg is added, it re-scans and should return
  * the new element after releasing the lock.
  */
-void *
-malloc_heap_alloc(struct malloc_heap *heap,
-               const char *type __attribute__((unused)), size_t size, unsigned align)
+static void *
+heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
+               unsigned int flags, size_t align, size_t bound, bool contig)
 {
+       struct malloc_elem *elem;
+
        size = RTE_CACHE_LINE_ROUNDUP(size);
        align = RTE_CACHE_LINE_ROUNDUP(align);
-       rte_spinlock_lock(&heap->lock);
-       struct malloc_elem *elem = find_suitable_element(heap, size, align);
-       if (elem == NULL){
-               if ((malloc_heap_add_memzone(heap, size, align)) == 0)
-                       elem = find_suitable_element(heap, size, align);
-       }
 
-       if (elem != NULL){
-               elem = malloc_elem_alloc(elem, size, align);
+       elem = find_suitable_element(heap, size, flags, align, bound, contig);
+       if (elem != NULL) {
+               elem = malloc_elem_alloc(elem, size, align, bound, contig);
+
                /* increase heap's count of allocated elements */
                heap->alloc_count++;
        }
-       rte_spinlock_unlock(&heap->lock);
+
        return elem == NULL ? NULL : (void *)(&elem[1]);
+}
+
+static int
+try_expand_heap(struct malloc_heap *heap, size_t pg_sz, size_t elt_size,
+               int socket, unsigned int flags, size_t align, size_t bound,
+               bool contig)
+{
+       size_t map_len;
+       struct rte_memseg_list *msl;
+       struct rte_memseg **ms;
+       struct malloc_elem *elem;
+       int n_segs, allocd_pages;
+       void *ret, *map_addr;
+
+       align = RTE_MAX(align, MALLOC_ELEM_HEADER_LEN);
+       map_len = RTE_ALIGN_CEIL(align + elt_size + MALLOC_ELEM_TRAILER_LEN,
+                       pg_sz);
+
+       n_segs = map_len / pg_sz;
+
+       /* we can't know in advance how many pages we'll need, so malloc */
+       ms = malloc(sizeof(*ms) * n_segs);
+
+       allocd_pages = eal_memalloc_alloc_seg_bulk(ms, n_segs, pg_sz,
+                       socket, true);
+
+       /* make sure we've allocated our pages... */
+       if (allocd_pages < 0)
+               goto free_ms;
+
+       map_addr = ms[0]->addr;
+       msl = rte_mem_virt2memseg_list(map_addr);
+
+       /* check if we wanted contiguous memory but didn't get it */
+       if (contig && !eal_memalloc_is_contig(msl, map_addr, map_len)) {
+               RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
+                               __func__);
+               goto free_pages;
+       }
+
+       /* add newly minted memsegs to malloc heap */
+       elem = malloc_heap_add_memory(heap, msl, map_addr, map_len);
+
+       /* try once more, as now we have allocated new memory */
+       ret = find_suitable_element(heap, elt_size, flags, align, bound,
+                       contig);
+
+       if (ret == NULL)
+               goto free_elem;
+
+       RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
+               socket, map_len >> 20ULL);
+
+       free(ms);
+
+       return 0;
+
+free_elem:
+       malloc_elem_free_list_remove(elem);
+       malloc_elem_hide_region(elem, map_addr, map_len);
+       heap->total_size -= map_len;
+
+free_pages:
+       eal_memalloc_free_seg_bulk(ms, n_segs);
+free_ms:
+       free(ms);
+
+       return -1;
+}
+
+static int
+compare_pagesz(const void *a, const void *b)
+{
+       const struct rte_memseg_list * const*mpa = a;
+       const struct rte_memseg_list * const*mpb = b;
+       const struct rte_memseg_list *msla = *mpa;
+       const struct rte_memseg_list *mslb = *mpb;
+       uint64_t pg_sz_a = msla->page_sz;
+       uint64_t pg_sz_b = mslb->page_sz;
+
+       if (pg_sz_a < pg_sz_b)
+               return -1;
+       if (pg_sz_a > pg_sz_b)
+               return 1;
+       return 0;
+}
+
+static int
+alloc_mem_on_socket(size_t size, int socket, unsigned int flags, size_t align,
+               size_t bound, bool contig)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
+       struct rte_memseg_list *requested_msls[RTE_MAX_MEMSEG_LISTS];
+       struct rte_memseg_list *other_msls[RTE_MAX_MEMSEG_LISTS];
+       uint64_t requested_pg_sz[RTE_MAX_MEMSEG_LISTS];
+       uint64_t other_pg_sz[RTE_MAX_MEMSEG_LISTS];
+       uint64_t prev_pg_sz;
+       int i, n_other_msls, n_other_pg_sz, n_requested_msls, n_requested_pg_sz;
+       bool size_hint = (flags & RTE_MEMZONE_SIZE_HINT_ONLY) > 0;
+       unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
+       void *ret;
+
+       memset(requested_msls, 0, sizeof(requested_msls));
+       memset(other_msls, 0, sizeof(other_msls));
+       memset(requested_pg_sz, 0, sizeof(requested_pg_sz));
+       memset(other_pg_sz, 0, sizeof(other_pg_sz));
+
+       /*
+        * go through memseg list and take note of all the page sizes available,
+        * and if any of them were specifically requested by the user.
+        */
+       n_requested_msls = 0;
+       n_other_msls = 0;
+       for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
+               struct rte_memseg_list *msl = &mcfg->memsegs[i];
+
+               if (msl->socket_id != socket)
+                       continue;
+
+               if (msl->base_va == NULL)
+                       continue;
+
+               /* if pages of specific size were requested */
+               if (size_flags != 0 && check_hugepage_sz(size_flags,
+                               msl->page_sz))
+                       requested_msls[n_requested_msls++] = msl;
+               else if (size_flags == 0 || size_hint)
+                       other_msls[n_other_msls++] = msl;
+       }
+
+       /* sort the lists, smallest first */
+       qsort(requested_msls, n_requested_msls, sizeof(requested_msls[0]),
+                       compare_pagesz);
+       qsort(other_msls, n_other_msls, sizeof(other_msls[0]),
+                       compare_pagesz);
+
+       /* now, extract page sizes we are supposed to try */
+       prev_pg_sz = 0;
+       n_requested_pg_sz = 0;
+       for (i = 0; i < n_requested_msls; i++) {
+               uint64_t pg_sz = requested_msls[i]->page_sz;
+
+               if (prev_pg_sz != pg_sz) {
+                       requested_pg_sz[n_requested_pg_sz++] = pg_sz;
+                       prev_pg_sz = pg_sz;
+               }
+       }
+       prev_pg_sz = 0;
+       n_other_pg_sz = 0;
+       for (i = 0; i < n_other_msls; i++) {
+               uint64_t pg_sz = other_msls[i]->page_sz;
+
+               if (prev_pg_sz != pg_sz) {
+                       other_pg_sz[n_other_pg_sz++] = pg_sz;
+                       prev_pg_sz = pg_sz;
+               }
+       }
+
+       /* finally, try allocating memory of specified page sizes, starting from
+        * the smallest sizes
+        */
+       for (i = 0; i < n_requested_pg_sz; i++) {
+               uint64_t pg_sz = requested_pg_sz[i];
+
+               /*
+                * do not pass the size hint here, as user expects other page
+                * sizes first, before resorting to best effort allocation.
+                */
+               if (!try_expand_heap(heap, pg_sz, size, socket, size_flags,
+                               align, bound, contig))
+                       return 0;
+       }
+       if (n_other_pg_sz == 0)
+               return -1;
+
+       /* now, check if we can reserve anything with size hint */
+       ret = find_suitable_element(heap, size, flags, align, bound, contig);
+       if (ret != NULL)
+               return 0;
+
+       /*
+        * we still couldn't reserve memory, so try expanding heap with other
+        * page sizes, if there are any
+        */
+       for (i = 0; i < n_other_pg_sz; i++) {
+               uint64_t pg_sz = other_pg_sz[i];
+
+               if (!try_expand_heap(heap, pg_sz, size, socket, flags,
+                               align, bound, contig))
+                       return 0;
+       }
+       return -1;
+}
+
+/* this will try lower page sizes first */
+static void *
+heap_alloc_on_socket(const char *type, size_t size, int socket,
+               unsigned int flags, size_t align, size_t bound, bool contig)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
+       unsigned int size_flags = flags & ~RTE_MEMZONE_SIZE_HINT_ONLY;
+       void *ret;
+
+       rte_spinlock_lock(&(heap->lock));
+
+       align = align == 0 ? 1 : align;
+
+       /* for legacy mode, try once and with all flags */
+       if (internal_config.legacy_mem) {
+               ret = heap_alloc(heap, type, size, flags, align, bound, contig);
+               goto alloc_unlock;
+       }
+
+       /*
+        * we do not pass the size hint here, because even if allocation fails,
+        * we may still be able to allocate memory from appropriate page sizes,
+        * we just need to request more memory first.
+        */
+       ret = heap_alloc(heap, type, size, size_flags, align, bound, contig);
+       if (ret != NULL)
+               goto alloc_unlock;
+
+       if (!alloc_mem_on_socket(size, socket, flags, align, bound, contig)) {
+               ret = heap_alloc(heap, type, size, flags, align, bound, contig);
+
+               /* this should have succeeded */
+               if (ret == NULL)
+                       RTE_LOG(ERR, EAL, "Error allocating from heap\n");
+       }
+alloc_unlock:
+       rte_spinlock_unlock(&(heap->lock));
+       return ret;
+}
+
+void *
+malloc_heap_alloc(const char *type, size_t size, int socket_arg,
+               unsigned int flags, size_t align, size_t bound, bool contig)
+{
+       int socket, i, cur_socket;
+       void *ret;
+
+       /* return NULL if size is 0 or alignment is not power-of-2 */
+       if (size == 0 || (align && !rte_is_power_of_2(align)))
+               return NULL;
+
+       if (!rte_eal_has_hugepages())
+               socket_arg = SOCKET_ID_ANY;
 
+       if (socket_arg == SOCKET_ID_ANY)
+               socket = malloc_get_numa_socket();
+       else
+               socket = socket_arg;
+
+       /* Check socket parameter */
+       if (socket >= RTE_MAX_NUMA_NODES)
+               return NULL;
+
+       ret = heap_alloc_on_socket(type, size, socket, flags, align, bound,
+                       contig);
+       if (ret != NULL || socket_arg != SOCKET_ID_ANY)
+               return ret;
+
+       /* try other heaps */
+       for (i = 0; i < (int) rte_socket_count(); i++) {
+               cur_socket = rte_socket_id_by_idx(i);
+               if (cur_socket == socket)
+                       continue;
+               ret = heap_alloc_on_socket(type, size, cur_socket, flags,
+                               align, bound, contig);
+               if (ret != NULL)
+                       return ret;
+       }
+       return NULL;
+}
+
+int
+malloc_heap_free(struct malloc_elem *elem)
+{
+       struct malloc_heap *heap;
+       void *start, *aligned_start, *end, *aligned_end;
+       size_t len, aligned_len, page_sz;
+       struct rte_memseg_list *msl;
+       int n_segs, seg_idx, max_seg_idx, ret;
+
+       if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
+               return -1;
+
+       /* elem may be merged with previous element, so keep heap address */
+       heap = elem->heap;
+       msl = elem->msl;
+       page_sz = (size_t)msl->page_sz;
+
+       rte_spinlock_lock(&(heap->lock));
+
+       /* mark element as free */
+       elem->state = ELEM_FREE;
+
+       elem = malloc_elem_free(elem);
+
+       /* anything after this is a bonus */
+       ret = 0;
+
+       /* ...of which we can't avail if we are in legacy mode */
+       if (internal_config.legacy_mem)
+               goto free_unlock;
+
+       /* check if we can free any memory back to the system */
+       if (elem->size < page_sz)
+               goto free_unlock;
+
+       /* probably, but let's make sure, as we may not be using up full page */
+       start = elem;
+       len = elem->size;
+       aligned_start = RTE_PTR_ALIGN_CEIL(start, page_sz);
+       end = RTE_PTR_ADD(elem, len);
+       aligned_end = RTE_PTR_ALIGN_FLOOR(end, page_sz);
+
+       aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
+
+       /* can't free anything */
+       if (aligned_len < page_sz)
+               goto free_unlock;
+
+       malloc_elem_free_list_remove(elem);
+
+       malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
+
+       /* we don't really care if we fail to deallocate memory */
+       n_segs = aligned_len / page_sz;
+       seg_idx = RTE_PTR_DIFF(aligned_start, msl->base_va) / page_sz;
+       max_seg_idx = seg_idx + n_segs;
+
+       for (; seg_idx < max_seg_idx; seg_idx++) {
+               struct rte_memseg *ms;
+
+               ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
+               eal_memalloc_free_seg(ms);
+       }
+       heap->total_size -= aligned_len;
+
+       RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
+               msl->socket_id, aligned_len >> 20ULL);
+free_unlock:
+       rte_spinlock_unlock(&(heap->lock));
+       return ret;
+}
+
+int
+malloc_heap_resize(struct malloc_elem *elem, size_t size)
+{
+       int ret;
+
+       if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
+               return -1;
+
+       rte_spinlock_lock(&(elem->heap->lock));
+
+       ret = malloc_elem_resize(elem, size);
+
+       rte_spinlock_unlock(&(elem->heap->lock));
+
+       return ret;
 }
 
 /*
  * Function to retrieve data for heap on given socket
  */
 int
-malloc_heap_get_stats(const struct malloc_heap *heap,
+malloc_heap_get_stats(struct malloc_heap *heap,
                struct rte_malloc_socket_stats *socket_stats)
 {
        size_t idx;
        struct malloc_elem *elem;
 
+       rte_spinlock_lock(&heap->lock);
+
        /* Initialise variables for heap */
        socket_stats->free_count = 0;
        socket_stats->heap_freesz_bytes = 0;
@@ -204,5 +568,45 @@ malloc_heap_get_stats(const struct malloc_heap *heap,
        socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
                        socket_stats->heap_freesz_bytes);
        socket_stats->alloc_count = heap->alloc_count;
+
+       rte_spinlock_unlock(&heap->lock);
        return 0;
 }
+
+/*
+ * Function to retrieve data for heap on given socket
+ */
+void
+malloc_heap_dump(struct malloc_heap *heap, FILE *f)
+{
+       struct malloc_elem *elem;
+
+       rte_spinlock_lock(&heap->lock);
+
+       fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
+       fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
+
+       elem = heap->first;
+       while (elem) {
+               malloc_elem_dump(elem, f);
+               elem = elem->next;
+       }
+
+       rte_spinlock_unlock(&heap->lock);
+}
+
+int
+rte_eal_malloc_heap_init(void)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+
+       if (mcfg == NULL)
+               return -1;
+
+       /* secondary process does not need to initialize anything */
+       if (rte_eal_process_type() != RTE_PROC_PRIMARY)
+               return 0;
+
+       /* add all IOVA-contiguous areas to the heap */
+       return rte_memseg_contig_walk(malloc_add_seg, NULL);
+}