[dpdk-dev] [PATCH 1/4] malloc: separate creating memseg list and malloc heap

Yongseok Koh yskoh at mellanox.com
Fri Dec 14 10:33:38 CET 2018


On Thu, Nov 29, 2018 at 01:48:32PM +0000, Anatoly Burakov wrote:
> Currently, creating external malloc heap involves also creating
> a memseg list backing that malloc heap. We need to have them as
> separate functions, to allow creating memseg lists without
> creating a malloc heap.
> 
> Signed-off-by: Anatoly Burakov <anatoly.burakov at intel.com>
> ---

Acked-by: Yongseok Koh <yskoh at mellanox.com>

Thanks

>  lib/librte_eal/common/malloc_heap.c | 34 ++++++++++++++++++-----------
>  lib/librte_eal/common/malloc_heap.h |  9 ++++++--
>  lib/librte_eal/common/rte_malloc.c  | 11 ++++++++--
>  3 files changed, 37 insertions(+), 17 deletions(-)
> 
> diff --git a/lib/librte_eal/common/malloc_heap.c b/lib/librte_eal/common/malloc_heap.c
> index c6a6d4f6b..25693481f 100644
> --- a/lib/librte_eal/common/malloc_heap.c
> +++ b/lib/librte_eal/common/malloc_heap.c
> @@ -1095,9 +1095,10 @@ destroy_seg(struct malloc_elem *elem, size_t len)
>  	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 +1118,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 +1144,39 @@ 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;
> +}
> +
> +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;
>  }
> diff --git a/lib/librte_eal/common/malloc_heap.h b/lib/librte_eal/common/malloc_heap.h
> index e48996d52..255a315b8 100644
> --- a/lib/librte_eal/common/malloc_heap.h
> +++ b/lib/librte_eal/common/malloc_heap.h
> @@ -39,9 +39,14 @@ malloc_heap_create(struct malloc_heap *heap, const char *heap_name);
>  int
>  malloc_heap_destroy(struct malloc_heap *heap);
>  
> +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);
> +
>  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);
> +malloc_heap_add_external_memory(struct malloc_heap *heap,
> +		struct rte_memseg_list *msl);
>  
>  int
>  malloc_heap_remove_external_memory(struct malloc_heap *heap, void *va_addr,
> diff --git a/lib/librte_eal/common/rte_malloc.c b/lib/librte_eal/common/rte_malloc.c
> index 0da5ad5e8..66bfe63c3 100644
> --- a/lib/librte_eal/common/rte_malloc.c
> +++ b/lib/librte_eal/common/rte_malloc.c
> @@ -340,6 +340,7 @@ rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
>  {
>  	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
>  	struct malloc_heap *heap = NULL;
> +	struct rte_memseg_list *msl;
>  	unsigned int n;
>  	int ret;
>  
> @@ -373,9 +374,15 @@ rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
>  		goto unlock;
>  	}
>  
> +	msl = malloc_heap_create_external_seg(va_addr, iova_addrs, n, page_sz,
> +			heap_name, heap->socket_id);
> +	if (msl == NULL) {
> +		ret = -1;
> +		goto unlock;
> +	}
> +
>  	rte_spinlock_lock(&heap->lock);
> -	ret = malloc_heap_add_external_memory(heap, va_addr, iova_addrs, n,
> -			page_sz);
> +	ret = malloc_heap_add_external_memory(heap, msl);
>  	rte_spinlock_unlock(&heap->lock);
>  
>  unlock:
> -- 
> 2.17.1


More information about the dev mailing list