malloc: enable callbacks on alloc/free and mp sync
authorAnatoly Burakov <anatoly.burakov@intel.com>
Wed, 11 Apr 2018 12:30:38 +0000 (13:30 +0100)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 11 Apr 2018 19:45:55 +0000 (21:45 +0200)
Callbacks will be triggered just after allocation and just
before deallocation, to ensure that memory address space
referenced in the callback is always valid by the time
callback is called.

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
Tested-by: Santosh Shukla <santosh.shukla@caviumnetworks.com>
Tested-by: Hemant Agrawal <hemant.agrawal@nxp.com>
Tested-by: Gowrishankar Muthukrishnan <gowrishankar.m@linux.vnet.ibm.com>
lib/librte_eal/common/malloc_heap.c
lib/librte_eal/linuxapp/eal/eal_memalloc.c
lib/librte_eal/linuxapp/eal/eal_vfio.c

index be39250..18c7b69 100644 (file)
@@ -241,6 +241,7 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
        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);
@@ -262,12 +263,22 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
 
        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;
@@ -280,6 +291,10 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
        return 0;
 
 free_elem:
+       if (callback_triggered)
+               eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
+                               map_addr, alloc_sz);
+
        rollback_expand_heap(ms, n_segs, elem, map_addr, alloc_sz);
 
        request_sync();
@@ -642,6 +657,10 @@ malloc_heap_free(struct malloc_elem *elem)
        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);
 
@@ -666,6 +685,8 @@ malloc_heap_free(struct malloc_elem *elem)
                 * 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);
        }
index 3c608b3..8063fe3 100644 (file)
@@ -877,6 +877,21 @@ sync_chunk(struct rte_memseg_list *primary_msl,
 
        diff_len = RTE_MIN(chunk_len, diff_len);
 
+       /* if we are freeing memory, notify the application */
+       if (!used) {
+               struct rte_memseg *ms;
+               void *start_va;
+               size_t len, page_sz;
+
+               ms = rte_fbarray_get(l_arr, start);
+               start_va = ms->addr;
+               page_sz = (size_t)primary_msl->page_sz;
+               len = page_sz * diff_len;
+
+               eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
+                               start_va, len);
+       }
+
        for (i = 0; i < diff_len; i++) {
                struct rte_memseg *p_ms, *l_ms;
                int seg_idx = start + i;
@@ -902,6 +917,21 @@ sync_chunk(struct rte_memseg_list *primary_msl,
                }
        }
 
+       /* if we just allocated memory, notify the application */
+       if (used) {
+               struct rte_memseg *ms;
+               void *start_va;
+               size_t len, page_sz;
+
+               ms = rte_fbarray_get(l_arr, start);
+               start_va = ms->addr;
+               page_sz = (size_t)primary_msl->page_sz;
+               len = page_sz * diff_len;
+
+               eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC,
+                               start_va, len);
+       }
+
        /* calculate how much we can advance until next chunk */
        diff_len = used ?
                        rte_fbarray_find_contig_used(l_arr, start) :
index 5101c04..2eea3b8 100644 (file)
@@ -1128,6 +1128,7 @@ vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
        create.levels = 1;
 
        if (do_map) {
+               void *addr;
                /* re-create window and remap the entire memory */
                if (iova > create.window_size) {
                        if (vfio_spapr_create_new_dma_window(vfio_container_fd,
@@ -1158,9 +1159,19 @@ vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
 
                /* now that we've remapped all of the memory that was present
                 * before, map the segment that we were requested to map.
+                *
+                * however, if we were called by the callback, the memory we
+                * were called with was already in the memseg list, so previous
+                * mapping should've mapped that segment already.
+                *
+                * virt2memseg_list is a relatively cheap check, so use that. if
+                * memory is within any memseg list, it's a memseg, so it's
+                * already mapped.
                 */
-               if (vfio_spapr_dma_do_map(vfio_container_fd,
-                               vaddr, iova, len, 1) < 0) {
+               addr = (void *)(uintptr_t)vaddr;
+               if (rte_mem_virt2memseg_list(addr) == NULL &&
+                               vfio_spapr_dma_do_map(vfio_container_fd,
+                                       vaddr, iova, len, 1) < 0) {
                        RTE_LOG(ERR, EAL, "Could not map segment\n");
                        ret = -1;
                        goto out;