malloc: allow reserving biggest element
[dpdk.git] / lib / librte_eal / common / malloc_heap.c
index 5f8c643..12aaf2d 100644 (file)
@@ -10,6 +10,7 @@
 #include <sys/queue.h>
 
 #include <rte_memory.h>
+#include <rte_errno.h>
 #include <rte_eal.h>
 #include <rte_eal_memconfig.h>
 #include <rte_launch.h>
@@ -26,6 +27,7 @@
 #include "eal_memalloc.h"
 #include "malloc_elem.h"
 #include "malloc_heap.h"
+#include "malloc_mp.h"
 
 static unsigned
 check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
@@ -81,8 +83,6 @@ malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
 
        malloc_elem_free_list_insert(elem);
 
-       heap->total_size += len;
-
        return elem;
 }
 
@@ -99,13 +99,16 @@ malloc_add_seg(const struct rte_memseg_list *msl,
 
        /* msl is const, so find it */
        msl_idx = msl - mcfg->memsegs;
-       found_msl = &mcfg->memsegs[msl_idx];
 
        if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
                return -1;
 
+       found_msl = &mcfg->memsegs[msl_idx];
+
        malloc_heap_add_memory(heap, found_msl, ms->addr, len);
 
+       heap->total_size += len;
+
        RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
                        msl->socket_id);
        return 0;
@@ -145,6 +148,52 @@ find_suitable_element(struct malloc_heap *heap, size_t size,
        return NULL;
 }
 
+/*
+ * Iterates through the freelist for a heap to find a free element with the
+ * biggest size and requested alignment. Will also set size to whatever element
+ * size that was found.
+ * Returns null on failure, or pointer to element on success.
+ */
+static struct malloc_elem *
+find_biggest_element(struct malloc_heap *heap, size_t *size,
+               unsigned int flags, size_t align, bool contig)
+{
+       struct malloc_elem *elem, *max_elem = NULL;
+       size_t idx, max_size = 0;
+
+       for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
+               for (elem = LIST_FIRST(&heap->free_head[idx]);
+                               !!elem; elem = LIST_NEXT(elem, free_list)) {
+                       size_t cur_size;
+                       if (!check_hugepage_sz(flags, elem->msl->page_sz))
+                               continue;
+                       if (contig) {
+                               cur_size =
+                                       malloc_elem_find_max_iova_contig(elem,
+                                                       align);
+                       } else {
+                               void *data_start = RTE_PTR_ADD(elem,
+                                               MALLOC_ELEM_HEADER_LEN);
+                               void *data_end = RTE_PTR_ADD(elem, elem->size -
+                                               MALLOC_ELEM_TRAILER_LEN);
+                               void *aligned = RTE_PTR_ALIGN_CEIL(data_start,
+                                               align);
+                               /* check if aligned data start is beyond end */
+                               if (aligned >= data_end)
+                                       continue;
+                               cur_size = RTE_PTR_DIFF(data_end, aligned);
+                       }
+                       if (cur_size > max_size) {
+                               max_size = cur_size;
+                               max_elem = elem;
+                       }
+               }
+       }
+
+       *size = max_size;
+       return max_elem;
+}
+
 /*
  * 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
@@ -171,74 +220,220 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
        return elem == NULL ? NULL : (void *)(&elem[1]);
 }
 
-static int
-try_expand_heap(struct malloc_heap *heap, size_t pg_sz, size_t elt_size,
+static void *
+heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,
+               unsigned int flags, size_t align, bool contig)
+{
+       struct malloc_elem *elem;
+       size_t size;
+
+       align = RTE_CACHE_LINE_ROUNDUP(align);
+
+       elem = find_biggest_element(heap, &size, flags, align, contig);
+       if (elem != NULL) {
+               elem = malloc_elem_alloc(elem, size, align, 0, contig);
+
+               /* increase heap's count of allocated elements */
+               heap->alloc_count++;
+       }
+
+       return elem == NULL ? NULL : (void *)(&elem[1]);
+}
+
+/* this function is exposed in malloc_mp.h */
+void
+rollback_expand_heap(struct rte_memseg **ms, int n_segs,
+               struct malloc_elem *elem, void *map_addr, size_t map_len)
+{
+       if (elem != NULL) {
+               malloc_elem_free_list_remove(elem);
+               malloc_elem_hide_region(elem, map_addr, map_len);
+       }
+
+       eal_memalloc_free_seg_bulk(ms, n_segs);
+}
+
+/* this function is exposed in malloc_mp.h */
+struct malloc_elem *
+alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
                int socket, unsigned int flags, size_t align, size_t bound,
-               bool contig)
+               bool contig, struct rte_memseg **ms, int n_segs)
 {
-       size_t map_len;
        struct rte_memseg_list *msl;
-       struct rte_memseg **ms;
-       struct malloc_elem *elem;
-       int n_segs, allocd_pages;
+       struct malloc_elem *elem = NULL;
+       size_t alloc_sz;
+       int 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;
+       alloc_sz = (size_t)pg_sz * n_segs;
 
-       /* we can't know in advance how many pages we'll need, so malloc */
-       ms = malloc(sizeof(*ms) * n_segs);
+       /* first, check if we're allowed to allocate this memory */
+       if (eal_memalloc_mem_alloc_validate(socket,
+                       heap->total_size + alloc_sz) < 0) {
+               RTE_LOG(DEBUG, EAL, "User has disallowed allocation\n");
+               return NULL;
+       }
 
        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;
+               return NULL;
 
        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)) {
+       if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {
                RTE_LOG(DEBUG, EAL, "%s(): couldn't allocate physically contiguous space\n",
                                __func__);
-               goto free_pages;
+               goto fail;
        }
 
        /* add newly minted memsegs to malloc heap */
-       elem = malloc_heap_add_memory(heap, msl, map_addr, map_len);
+       elem = malloc_heap_add_memory(heap, msl, map_addr, alloc_sz);
 
        /* 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 fail;
+
+       return elem;
+
+fail:
+       rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
+       return NULL;
+}
+
+static int
+try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
+               size_t elt_size, int socket, unsigned int flags, size_t align,
+               size_t bound, bool contig)
+{
+       struct malloc_elem *elem;
+       struct rte_memseg **ms;
+       void *map_addr;
+       size_t alloc_sz;
+       int n_segs;
+       bool callback_triggered = false;
+
+       alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
+                       MALLOC_ELEM_TRAILER_LEN, pg_sz);
+       n_segs = alloc_sz / pg_sz;
+
+       /* we can't know in advance how many pages we'll need, so we malloc */
+       ms = malloc(sizeof(*ms) * n_segs);
+
+       memset(ms, 0, sizeof(*ms) * n_segs);
+
+       if (ms == NULL)
+               return -1;
+
+       elem = alloc_pages_on_heap(heap, pg_sz, elt_size, socket, flags, align,
+                       bound, contig, ms, n_segs);
+
+       if (elem == NULL)
+               goto free_ms;
+
+       map_addr = ms[0]->addr;
+
+       /* notify user about changes in memory map */
+       eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
+
+       /* notify other processes that this has happened */
+       if (request_sync()) {
+               /* we couldn't ensure all processes have mapped memory,
+                * so free it back and notify everyone that it's been
+                * freed back.
+                *
+                * technically, we could've avoided adding memory addresses to
+                * the map, but that would've led to inconsistent behavior
+                * between primary and secondary processes, as those get
+                * callbacks during sync. therefore, force primary process to
+                * do alloc-and-rollback syncs as well.
+                */
+               callback_triggered = true;
                goto free_elem;
+       }
+       heap->total_size += alloc_sz;
 
        RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
-               socket, map_len >> 20ULL);
+               socket, alloc_sz >> 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;
+       if (callback_triggered)
+               eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
+                               map_addr, alloc_sz);
 
-free_pages:
-       eal_memalloc_free_seg_bulk(ms, n_segs);
+       rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
+
+       request_sync();
 free_ms:
        free(ms);
 
        return -1;
 }
 
+static int
+try_expand_heap_secondary(struct malloc_heap *heap, uint64_t pg_sz,
+               size_t elt_size, int socket, unsigned int flags, size_t align,
+               size_t bound, bool contig)
+{
+       struct malloc_mp_req req;
+       int req_result;
+
+       memset(&req, 0, sizeof(req));
+
+       req.t = REQ_TYPE_ALLOC;
+       req.alloc_req.align = align;
+       req.alloc_req.bound = bound;
+       req.alloc_req.contig = contig;
+       req.alloc_req.flags = flags;
+       req.alloc_req.elt_size = elt_size;
+       req.alloc_req.page_sz = pg_sz;
+       req.alloc_req.socket = socket;
+       req.alloc_req.heap = heap; /* it's in shared memory */
+
+       req_result = request_to_primary(&req);
+
+       if (req_result != 0)
+               return -1;
+
+       if (req.result != REQ_RESULT_SUCCESS)
+               return -1;
+
+       return 0;
+}
+
+static int
+try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_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;
+       int ret;
+
+       rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+               ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
+                               flags, align, bound, contig);
+       } else {
+               ret = try_expand_heap_secondary(heap, pg_sz, elt_size, socket,
+                               flags, align, bound, contig);
+       }
+
+       rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+       return ret;
+}
+
 static int
 compare_pagesz(const void *a, const void *b)
 {
@@ -257,11 +452,10 @@ compare_pagesz(const void *a, const void *b)
 }
 
 static int
-alloc_mem_on_socket(size_t size, int socket, unsigned int flags, size_t align,
-               size_t bound, bool contig)
+alloc_more_mem_on_socket(struct malloc_heap *heap, 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];
@@ -393,7 +587,8 @@ heap_alloc_on_socket(const char *type, size_t size, int socket,
        if (ret != NULL)
                goto alloc_unlock;
 
-       if (!alloc_mem_on_socket(size, socket, flags, align, bound, contig)) {
+       if (!alloc_more_mem_on_socket(heap, size, socket, flags, align, bound,
+                       contig)) {
                ret = heap_alloc(heap, type, size, flags, align, bound, contig);
 
                /* this should have succeeded */
@@ -446,14 +641,102 @@ malloc_heap_alloc(const char *type, size_t size, int socket_arg,
        return NULL;
 }
 
+static void *
+heap_alloc_biggest_on_socket(const char *type, int socket, unsigned int flags,
+               size_t align, bool contig)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct malloc_heap *heap = &mcfg->malloc_heaps[socket];
+       void *ret;
+
+       rte_spinlock_lock(&(heap->lock));
+
+       align = align == 0 ? 1 : align;
+
+       ret = heap_alloc_biggest(heap, type, flags, align, contig);
+
+       rte_spinlock_unlock(&(heap->lock));
+
+       return ret;
+}
+
+void *
+malloc_heap_alloc_biggest(const char *type, int socket_arg, unsigned int flags,
+               size_t align, bool contig)
+{
+       int socket, i, cur_socket;
+       void *ret;
+
+       /* return NULL if align is not power-of-2 */
+       if ((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_biggest_on_socket(type, socket, flags, align,
+                       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_biggest_on_socket(type, cur_socket, flags,
+                               align, contig);
+               if (ret != NULL)
+                       return ret;
+       }
+       return NULL;
+}
+
+/* this function is exposed in malloc_mp.h */
+int
+malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
+{
+       int n_segs, seg_idx, max_seg_idx;
+       struct rte_memseg_list *msl;
+       size_t page_sz;
+
+       msl = rte_mem_virt2memseg_list(aligned_start);
+       if (msl == NULL)
+               return -1;
+
+       page_sz = (size_t)msl->page_sz;
+       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);
+       }
+       return 0;
+}
+
 int
 malloc_heap_free(struct malloc_elem *elem)
 {
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        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;
+       unsigned int i, n_segs, before_space, after_space;
+       int ret;
 
        if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
                return -1;
@@ -494,25 +777,120 @@ malloc_heap_free(struct malloc_elem *elem)
        if (aligned_len < page_sz)
                goto free_unlock;
 
-       malloc_elem_free_list_remove(elem);
+       /* we can free something. however, some of these pages may be marked as
+        * unfreeable, so also check that as well
+        */
+       n_segs = aligned_len / page_sz;
+       for (i = 0; i < n_segs; i++) {
+               const struct rte_memseg *tmp =
+                               rte_mem_virt2memseg(aligned_start, msl);
 
-       malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
+               if (tmp->flags & RTE_MEMSEG_FLAG_DO_NOT_FREE) {
+                       /* this is an unfreeable segment, so move start */
+                       aligned_start = RTE_PTR_ADD(tmp->addr, tmp->len);
+               }
+       }
 
-       /* we don't really care if we fail to deallocate memory */
+       /* recalculate length and number of segments */
+       aligned_len = RTE_PTR_DIFF(aligned_end, aligned_start);
        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;
+       /* check if we can still free some pages */
+       if (n_segs == 0)
+               goto free_unlock;
 
-               ms = rte_fbarray_get(&msl->memseg_arr, seg_idx);
-               eal_memalloc_free_seg(ms);
+       /* We're not done yet. We also have to check if by freeing space we will
+        * be leaving free elements that are too small to store new elements.
+        * Check if we have enough space in the beginning and at the end, or if
+        * start/end are exactly page aligned.
+        */
+       before_space = RTE_PTR_DIFF(aligned_start, elem);
+       after_space = RTE_PTR_DIFF(end, aligned_end);
+       if (before_space != 0 &&
+                       before_space < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
+               /* There is not enough space before start, but we may be able to
+                * move the start forward by one page.
+                */
+               if (n_segs == 1)
+                       goto free_unlock;
+
+               /* move start */
+               aligned_start = RTE_PTR_ADD(aligned_start, page_sz);
+               aligned_len -= page_sz;
+               n_segs--;
        }
+       if (after_space != 0 && after_space <
+                       MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
+               /* There is not enough space after end, but we may be able to
+                * move the end backwards by one page.
+                */
+               if (n_segs == 1)
+                       goto free_unlock;
+
+               /* move end */
+               aligned_end = RTE_PTR_SUB(aligned_end, page_sz);
+               aligned_len -= page_sz;
+               n_segs--;
+       }
+
+       /* now we can finally free us some pages */
+
+       rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+
+       /*
+        * we allow secondary processes to clear the heap of this allocated
+        * memory because it is safe to do so, as even if notifications about
+        * unmapped pages don't make it to other processes, heap is shared
+        * across all processes, and will become empty of this memory anyway,
+        * and nothing can allocate it back unless primary process will be able
+        * to deliver allocation message to every single running process.
+        */
+
+       malloc_elem_free_list_remove(elem);
+
+       malloc_elem_hide_region(elem, (void *) aligned_start, aligned_len);
+
        heap->total_size -= aligned_len;
 
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+               /* notify user about changes in memory map */
+               eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
+                               aligned_start, aligned_len);
+
+               /* don't care if any of this fails */
+               malloc_heap_free_pages(aligned_start, aligned_len);
+
+               request_sync();
+       } else {
+               struct malloc_mp_req req;
+
+               memset(&req, 0, sizeof(req));
+
+               req.t = REQ_TYPE_FREE;
+               req.free_req.addr = aligned_start;
+               req.free_req.len = aligned_len;
+
+               /*
+                * we request primary to deallocate pages, but we don't do it
+                * in this thread. instead, we notify primary that we would like
+                * to deallocate pages, and this process will receive another
+                * request (in parallel) that will do it for us on another
+                * thread.
+                *
+                * we also don't really care if this succeeds - the data is
+                * already removed from the heap, so it is, for all intents and
+                * purposes, hidden from the rest of DPDK even if some other
+                * process (including this one) may have these pages mapped.
+                *
+                * notifications about deallocated memory happen during sync.
+                */
+               request_to_primary(&req);
+       }
+
        RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
                msl->socket_id, aligned_len >> 20ULL);
+
+       rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
 free_unlock:
        rte_spinlock_unlock(&(heap->lock));
        return ret;
@@ -600,8 +978,17 @@ rte_eal_malloc_heap_init(void)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 
-       if (mcfg == NULL)
+       if (register_mp_requests()) {
+               RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
+               rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
                return -1;
+       }
+
+       /* unlock mem hotplug here. it's safe for primary as no requests can
+        * even come before primary itself is fully initialized, and secondaries
+        * do not need to initialize the heap.
+        */
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
 
        /* secondary process does not need to initialize anything */
        if (rte_eal_process_type() != RTE_PROC_PRIMARY)