mem: do not use lockfiles for single file segments mode
[dpdk.git] / lib / librte_eal / common / malloc_heap.c
index c2c112a..c5d254d 100644 (file)
@@ -94,7 +94,7 @@ malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
 {
        struct malloc_elem *elem = start;
 
-       malloc_elem_init(elem, heap, msl, len);
+       malloc_elem_init(elem, heap, msl, len, elem, len);
 
        malloc_elem_insert(elem);
 
@@ -334,7 +334,7 @@ alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
         * executed. For 2) implies the new memory can not be added.
         */
        if (mcfg->dma_maskbits &&
-           rte_mem_check_dma_mask(mcfg->dma_maskbits)) {
+           rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
                /*
                 * Currently this can only happen if IOMMU is enabled
                 * and the address width supported by the IOMMU hw is
@@ -857,6 +857,13 @@ malloc_heap_free(struct malloc_elem *elem)
        if (elem->size < page_sz)
                goto free_unlock;
 
+       /* if user requested to match allocations, the sizes must match - if not,
+        * we will defer freeing these hugepages until the entire original allocation
+        * can be freed
+        */
+       if (internal_config.match_allocations && elem->size != elem->orig_size)
+               goto free_unlock;
+
        /* probably, but let's make sure, as we may not be using up full page */
        start = elem;
        len = elem->size;
@@ -1067,12 +1074,9 @@ malloc_heap_dump(struct malloc_heap *heap, FILE *f)
 }
 
 static int
-destroy_seg(struct malloc_elem *elem, size_t len)
+destroy_elem(struct malloc_elem *elem, size_t len)
 {
        struct malloc_heap *heap = elem->heap;
-       struct rte_memseg_list *msl;
-
-       msl = elem->msl;
 
        /* notify all subscribers that a memory area is going to be removed */
        eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE, elem, len);
@@ -1085,19 +1089,13 @@ destroy_seg(struct malloc_elem *elem, size_t len)
 
        memset(elem, 0, sizeof(*elem));
 
-       /* destroy the fbarray backing this memory */
-       if (rte_fbarray_destroy(&msl->memseg_arr) < 0)
-               return -1;
-
-       /* reset the memseg list */
-       memset(msl, 0, sizeof(*msl));
-
        return 0;
 }
 
-int
-malloc_heap_add_external_memory(struct malloc_heap *heap, void *va_addr,
-               rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz)
+struct rte_memseg_list *
+malloc_heap_create_external_seg(void *va_addr, rte_iova_t iova_addrs[],
+               unsigned int n_pages, size_t page_sz, const char *seg_name,
+               unsigned int socket_id)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        char fbarray_name[RTE_FBARRAY_NAME_LEN];
@@ -1117,17 +1115,17 @@ malloc_heap_add_external_memory(struct malloc_heap *heap, void *va_addr,
        if (msl == NULL) {
                RTE_LOG(ERR, EAL, "Couldn't find empty memseg list\n");
                rte_errno = ENOSPC;
-               return -1;
+               return NULL;
        }
 
        snprintf(fbarray_name, sizeof(fbarray_name) - 1, "%s_%p",
-                       heap->name, va_addr);
+                       seg_name, va_addr);
 
        /* create the backing fbarray */
        if (rte_fbarray_init(&msl->memseg_arr, fbarray_name, n_pages,
                        sizeof(struct rte_memseg)) < 0) {
                RTE_LOG(ERR, EAL, "Couldn't create fbarray backing the memseg list\n");
-               return -1;
+               return NULL;
        }
        arr = &msl->memseg_arr;
 
@@ -1143,32 +1141,95 @@ malloc_heap_add_external_memory(struct malloc_heap *heap, void *va_addr,
                ms->len = page_sz;
                ms->nchannel = rte_memory_get_nchannel();
                ms->nrank = rte_memory_get_nrank();
-               ms->socket_id = heap->socket_id;
+               ms->socket_id = socket_id;
        }
 
        /* set up the memseg list */
        msl->base_va = va_addr;
        msl->page_sz = page_sz;
-       msl->socket_id = heap->socket_id;
+       msl->socket_id = socket_id;
        msl->len = seg_len;
        msl->version = 0;
        msl->external = 1;
 
+       return msl;
+}
+
+struct extseg_walk_arg {
+       void *va_addr;
+       size_t len;
+       struct rte_memseg_list *msl;
+};
+
+static int
+extseg_walk(const struct rte_memseg_list *msl, void *arg)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct extseg_walk_arg *wa = arg;
+
+       if (msl->base_va == wa->va_addr && msl->len == wa->len) {
+               unsigned int found_idx;
+
+               /* msl is const */
+               found_idx = msl - mcfg->memsegs;
+               wa->msl = &mcfg->memsegs[found_idx];
+               return 1;
+       }
+       return 0;
+}
+
+struct rte_memseg_list *
+malloc_heap_find_external_seg(void *va_addr, size_t len)
+{
+       struct extseg_walk_arg wa;
+       int res;
+
+       wa.va_addr = va_addr;
+       wa.len = len;
+
+       res = rte_memseg_list_walk_thread_unsafe(extseg_walk, &wa);
+
+       if (res != 1) {
+               /* 0 means nothing was found, -1 shouldn't happen */
+               if (res == 0)
+                       rte_errno = ENOENT;
+               return NULL;
+       }
+       return wa.msl;
+}
+
+int
+malloc_heap_destroy_external_seg(struct rte_memseg_list *msl)
+{
+       /* destroy the fbarray backing this memory */
+       if (rte_fbarray_destroy(&msl->memseg_arr) < 0)
+               return -1;
+
+       /* reset the memseg list */
+       memset(msl, 0, sizeof(*msl));
+
+       return 0;
+}
+
+int
+malloc_heap_add_external_memory(struct malloc_heap *heap,
+               struct rte_memseg_list *msl)
+{
        /* erase contents of new memory */
-       memset(va_addr, 0, seg_len);
+       memset(msl->base_va, 0, msl->len);
 
        /* now, add newly minted memory to the malloc heap */
-       malloc_heap_add_memory(heap, msl, va_addr, seg_len);
+       malloc_heap_add_memory(heap, msl, msl->base_va, msl->len);
 
-       heap->total_size += seg_len;
+       heap->total_size += msl->len;
 
        /* all done! */
        RTE_LOG(DEBUG, EAL, "Added segment for heap %s starting at %p\n",
-                       heap->name, va_addr);
+                       heap->name, msl->base_va);
 
        /* notify all subscribers that a new memory area has been added */
        eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
-                       va_addr, seg_len);
+                       msl->base_va, msl->len);
 
        return 0;
 }
@@ -1198,7 +1259,7 @@ malloc_heap_remove_external_memory(struct malloc_heap *heap, void *va_addr,
                rte_errno = EBUSY;
                return -1;
        }
-       return destroy_seg(elem, len);
+       return destroy_elem(elem, len);
 }
 
 int
@@ -1259,6 +1320,10 @@ rte_eal_malloc_heap_init(void)
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        unsigned int i;
 
+       if (internal_config.match_allocations) {
+               RTE_LOG(DEBUG, EAL, "Hugepages will be freed exactly as allocated.\n");
+       }
+
        if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
                /* assign min socket ID to external heaps */
                mcfg->next_socket_id = EXTERNAL_HEAP_MIN_SOCKET_ID;