Memory Hotplug for DPDK
Subsystems (4)
cover Memory Hotplug for DPDK
This patchset introduces dynamic memory allocation for DPDK (aka memory hotplug). Based upon RFC submitted in December [1].
Dependencies (to be applied in specified order):
- IPC bugfixes patchset [2]
- IPC improvements patchset [3]
- IPC asynchronous request API patch [4]
- Function to return number of sockets [5]
Deprecation notices relevant to this patchset:
- General outline of memory hotplug changes [6]
- EAL NUMA node count changes [7]
The vast majority of changes are in the EAL and malloc, the external API disruption is minimal: a new set of API's are added for contiguous memory allocation for rte_memzone, and a few API additions in rte_memory due to switch to memseg_lists as opposed to memsegs. Every other API change is internal to EAL, and all of the memory allocation/freeing is handled through rte_malloc, with no externally visible API changes.
Quick outline of all changes done as part of this patchset:
* Malloc heap adjusted to handle holes in address space
* Single memseg list replaced by multiple memseg lists
* VA space for hugepages is preallocated in advance
* Added alloc/free for pages happening as needed on rte_malloc/rte_free
* Added contiguous memory allocation API's for rte_memzone
* Integrated Pawel Wodkowski's patch for registering/unregistering memory with VFIO [8]
* Callbacks for registering memory allocations
* Multiprocess support done via DPDK IPC introduced in 18.02
The biggest difference is a "memseg" now represents a single page (as opposed to being a big contiguous block of pages). As a consequence, both memzones and malloc elements are no longer guaranteed to be physically contiguous, unless the user asks for it at reserve time. To preserve whatever functionality that was dependent on previous behavior, a legacy memory option is also provided, however it is expected (or perhaps vainly hoped) to be temporary solution.
Why multiple memseg lists instead of one? Since memseg is a single page now, the list of memsegs will get quite big, and we need to locate pages somehow when we allocate and free them. We could of course just walk the list and allocate one contiguous chunk of VA space for memsegs, but this implementation uses separate lists instead in order to speed up many operations with memseg lists.
For v1, the following limitations are present:
- FreeBSD does not even compile, let alone run
- No 32-bit support
- There are some minor quality-of-life improvements planned that aren't ready yet and will be part of v2
- VFIO support is only smoke-tested (but is expected to work), VFIO support with secondary processes is not tested; work is ongoing to validate VFIO for all use cases
- Dynamic mapping/unmapping memory with VFIO is not supported in sPAPR IOMMU mode - help from sPAPR maintainers requested
Nevertheless, this patchset should be testable under 64-bit Linux, and should work for all use cases bar those mentioned above.
[1] http://dpdk.org/dev/patchwork/bundle/aburakov/Memory_RFC/
[2] http://dpdk.org/dev/patchwork/bundle/aburakov/IPC_Fixes/
[3] http://dpdk.org/dev/patchwork/bundle/aburakov/IPC_Improvements/
[4] http://dpdk.org/dev/patchwork/bundle/aburakov/IPC_Async_Request/
[5] http://dpdk.org/dev/patchwork/bundle/aburakov/Num_Sockets/
[6] http://dpdk.org/dev/patchwork/patch/34002/
[7] http://dpdk.org/dev/patchwork/patch/33853/
[8] http://dpdk.org/dev/patchwork/patch/24484/
Anatoly Burakov (41):
eal: move get_virtual_area out of linuxapp eal_memory.c
eal: move all locking to heap
eal: make malloc heap a doubly-linked list
eal: add function to dump malloc heap contents
test: add command to dump malloc heap contents
eal: make malloc_elem_join_adjacent_free public
eal: make malloc free list remove public
eal: make malloc free return resulting malloc element
eal: add rte_fbarray
eal: add "single file segments" command-line option
eal: add "legacy memory" option
eal: read hugepage counts from node-specific sysfs path
eal: replace memseg with memseg lists
eal: add support for mapping hugepages at runtime
eal: add support for unmapping pages at runtime
eal: make use of memory hotplug for init
eal: enable memory hotplug support in rte_malloc
test: fix malloc autotest to support memory hotplug
eal: add API to check if memory is contiguous
eal: add backend support for contiguous allocation
eal: enable reserving physically contiguous memzones
eal: replace memzone array with fbarray
mempool: add support for the new allocation methods
vfio: allow to map other memory regions
eal: map/unmap memory with VFIO when alloc/free pages
eal: prepare memseg lists for multiprocess sync
eal: add multiprocess init with memory hotplug
eal: add support for multiprocess memory hotplug
eal: add support for callbacks on memory hotplug
eal: enable callbacks on malloc/free and mp sync
ethdev: use contiguous allocation for DMA memory crypto/qat: use contiguous allocation for DMA memory net/avf: use contiguous allocation for DMA memory net/bnx2x: use contiguous allocation for DMA memory net/cxgbe: use contiguous allocation for DMA memory net/ena: use contiguous allocation for DMA memory net/enic: use contiguous allocation for DMA memory net/i40e: use contiguous allocation for DMA memory net/qede: use contiguous allocation for DMA memory net/virtio: use contiguous allocation for DMA memory net/vmxnet3: use contiguous allocation for DMA memory
config/common_base | 15 +- drivers/bus/pci/linux/pci.c | 29 +- drivers/crypto/qat/qat_qp.c | 4 +- drivers/net/avf/avf_ethdev.c | 2 +- drivers/net/bnx2x/bnx2x.c | 2 +- drivers/net/bnx2x/bnx2x_rxtx.c | 3 +- drivers/net/cxgbe/sge.c | 3 +- drivers/net/ena/base/ena_plat_dpdk.h | 7 +- drivers/net/ena/ena_ethdev.c | 10 +- drivers/net/enic/enic_main.c | 4 +- drivers/net/i40e/i40e_ethdev.c | 2 +- drivers/net/i40e/i40e_rxtx.c | 2 +- drivers/net/qede/base/bcm_osal.c | 5 +- drivers/net/virtio/virtio_ethdev.c | 8 +- drivers/net/virtio/virtio_user/vhost_kernel.c | 108 ++- drivers/net/vmxnet3/vmxnet3_ethdev.c | 7 +- lib/librte_eal/bsdapp/eal/Makefile | 4 + lib/librte_eal/bsdapp/eal/eal.c | 25 + lib/librte_eal/bsdapp/eal/eal_hugepage_info.c | 7 + lib/librte_eal/bsdapp/eal/eal_memalloc.c | 33 + lib/librte_eal/bsdapp/eal/meson.build | 1 + lib/librte_eal/common/Makefile | 2 +- lib/librte_eal/common/eal_common_fbarray.c | 859 +++++++++++++++++ lib/librte_eal/common/eal_common_memalloc.c | 181 ++++ lib/librte_eal/common/eal_common_memory.c | 512 +++++++++- lib/librte_eal/common/eal_common_memzone.c | 275 ++++-- lib/librte_eal/common/eal_common_options.c | 8 + lib/librte_eal/common/eal_filesystem.h | 13 + lib/librte_eal/common/eal_hugepages.h | 7 + lib/librte_eal/common/eal_internal_cfg.h | 10 +- lib/librte_eal/common/eal_memalloc.h | 41 + lib/librte_eal/common/eal_options.h | 4 + lib/librte_eal/common/eal_private.h | 33 + lib/librte_eal/common/include/rte_eal_memconfig.h | 29 +- lib/librte_eal/common/include/rte_fbarray.h | 352 +++++++ lib/librte_eal/common/include/rte_malloc.h | 9 + lib/librte_eal/common/include/rte_malloc_heap.h | 6 + lib/librte_eal/common/include/rte_memory.h | 79 +- lib/librte_eal/common/include/rte_memzone.h | 155 ++- lib/librte_eal/common/include/rte_vfio.h | 39 + lib/librte_eal/common/malloc_elem.c | 436 +++++++-- lib/librte_eal/common/malloc_elem.h | 41 +- lib/librte_eal/common/malloc_heap.c | 694 +++++++++++++- lib/librte_eal/common/malloc_heap.h | 15 +- lib/librte_eal/common/malloc_mp.c | 723 ++++++++++++++ lib/librte_eal/common/malloc_mp.h | 86 ++ lib/librte_eal/common/meson.build | 4 + lib/librte_eal/common/rte_malloc.c | 75 +- lib/librte_eal/linuxapp/eal/Makefile | 5 + lib/librte_eal/linuxapp/eal/eal.c | 102 +- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 155 ++- lib/librte_eal/linuxapp/eal/eal_memalloc.c | 1049 +++++++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_memory.c | 516 ++++++---- lib/librte_eal/linuxapp/eal/eal_vfio.c | 318 +++++-- lib/librte_eal/linuxapp/eal/eal_vfio.h | 11 + lib/librte_eal/linuxapp/eal/meson.build | 1 + lib/librte_eal/rte_eal_version.map | 23 +- lib/librte_ether/rte_ethdev.c | 3 +- lib/librte_mempool/rte_mempool.c | 87 +- test/test/commands.c | 3 + test/test/test_malloc.c | 71 +- test/test/test_memory.c | 43 +- test/test/test_memzone.c | 26 +- 63 files changed, 6631 insertions(+), 751 deletions(-) create mode 100644 lib/librte_eal/bsdapp/eal/eal_memalloc.c create mode 100644 lib/librte_eal/common/eal_common_fbarray.c create mode 100644 lib/librte_eal/common/eal_common_memalloc.c create mode 100644 lib/librte_eal/common/eal_memalloc.h create mode 100644 lib/librte_eal/common/include/rte_fbarray.h create mode 100644 lib/librte_eal/common/malloc_mp.c create mode 100644 lib/librte_eal/common/malloc_mp.h create mode 100644 lib/librte_eal/linuxapp/eal/eal_memalloc.c
Signature · 1 line
2.7.4
PATCH 1/41 eal: move get_virtual_area out of linuxapp eal_memory.c
Move get_virtual_area out of linuxapp EAL memory and make it common to EAL, so that other code could reserve virtual areas as well.
--- lib/librte_eal/common/eal_common_memory.c | 101 ++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 33 +++++++ lib/librte_eal/linuxapp/eal/eal_memory.c | 137 ++++++------------------------ 3 files changed, 161 insertions(+), 110 deletions(-)
lib/librte_eal/common/eal_common_memory.c +101 -0
@@ -2,10 +2,12 @@* Copyright(c) 2010-2014 Intel Corporation */ +#include <errno.h> #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <stdarg.h> +#include <string.h> #include <unistd.h> #include <inttypes.h> #include <sys/mman.h>
@@ -14,12 +16,111 @@#include <rte_memory.h> #include <rte_eal.h> #include <rte_eal_memconfig.h> +#include <rte_errno.h> #include <rte_log.h> #include "eal_private.h" #include "eal_internal_cfg.h" /* +* Try to mmap *size bytes in /dev/zero. If it is successful, return the +* pointer to the mmap'd area and keep *size unmodified. Else, retry +* with a smaller zone: decrease *size by hugepage_sz until it reaches +* 0. In this case, return NULL. Note: this function returns an address +* which is a multiple of hugepage size. +*/ + +static uint64_t baseaddr_offset; +static uint64_t system_page_sz; + +void * +eal_get_virtual_area (void *requested_addr,uint64_t *size, +uint64_t page_sz,int flags,int mmap_flags) +{ +bool addr_is_hint, allow_shrink, unmap, no_align; +uint64_t map_sz; +void *mapped_addr, *aligned_addr; + +if (system_page_sz ==0 ) + system_page_sz =sysconf (_SC_PAGESIZE); + + mmap_flags |=MAP_PRIVATE |MAP_ANONYMOUS ; + +RTE_LOG (DEBUG ,EAL ,"Ask a virtual area of 0x%zx bytes\n" , *size); + + addr_is_hint = (flags &EAL_VIRTUAL_AREA_ADDR_IS_HINT ) >0 ; + allow_shrink = (flags &EAL_VIRTUAL_AREA_ALLOW_SHRINK ) >0 ; + unmap = (flags &EAL_VIRTUAL_AREA_UNMAP ) >0 ; + +if (requested_addr ==NULL && internal_config.base_virtaddr !=0 ) { + requested_addr = (void *) (internal_config.base_virtaddr + + baseaddr_offset); + requested_addr =RTE_PTR_ALIGN (requested_addr, page_sz); + addr_is_hint = true; + } + +/* if requested address is not aligned by page size, or if requested +* address is NULL, add page size to requested length as we may get an +* address that's aligned by system page size, which can be smaller than +* our requested page size. additionally, we shouldn't try to align if +* system page size is the same as requested page size. +*/ + no_align = (requested_addr !=NULL && + ((uintptr_t )requested_addr & (page_sz -1 )) ==0 ) || + page_sz == system_page_sz; + +do { + map_sz = no_align ? *size : *size + page_sz; + + mapped_addr =mmap (requested_addr, map_sz,PROT_READ , + mmap_flags,-1 ,0 ); +if (mapped_addr ==MAP_FAILED && allow_shrink) + *size -= page_sz; + }while (allow_shrink && mapped_addr ==MAP_FAILED && *size >0 ); + +/* align resulting address - if map failed, we will ignore the value +* anyway, so no need to add additional checks. +*/ + aligned_addr = no_align ? mapped_addr : +RTE_PTR_ALIGN (mapped_addr, page_sz); + +if (*size ==0 ) { +RTE_LOG (ERR ,EAL ,"Cannot get a virtual area of any size: %s\n" , +strerror (errno)); + rte_errno = errno; +return NULL ; + }else if (mapped_addr ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL ,"Cannot get a virtual area: %s\n" , +strerror (errno)); +/* pass errno up the call chain */ + rte_errno = errno; +return NULL ; + }else if (requested_addr !=NULL && !addr_is_hint && + aligned_addr != requested_addr) { +RTE_LOG (ERR ,EAL ,"Cannot get a virtual area at requested address: %p (got %p)\n" , + requested_addr, aligned_addr); +munmap (mapped_addr, map_sz); + rte_errno =EADDRNOTAVAIL ; +return NULL ; + }else if (requested_addr !=NULL && addr_is_hint && + aligned_addr != requested_addr) { +RTE_LOG (WARNING ,EAL ,"WARNING! Base virtual address hint (%p != %p) not respected!\n" , + requested_addr, aligned_addr); +RTE_LOG (WARNING ,EAL ," This may cause issues with mapping memory into secondary processes\n" ); + } + +if (unmap) +munmap (mapped_addr, map_sz); + +RTE_LOG (DEBUG ,EAL ,"Virtual area found at %p (size = 0x%zx)\n" , + aligned_addr, *size); + + baseaddr_offset += *size; + +return aligned_addr; +} + +/* * Return a pointer to a read-only table of struct rte_physmem_desc * elements, containing the layout of all addressable physical * memory. The last element of the table contains a NULL address.
lib/librte_eal/common/eal_private.h +33 -0
@@ -127,6 +127,39 @@ int rte_eal_alarm_init(void);int rte_eal_check_module (const char *module_name);/** +* Get virtual area of specified size from the OS. +* +* This function is private to the EAL. +* +* @param requested_addr +* Address where to request address space. +* @param size +* Size of requested area. +* @param page_sz +* Page size on which to align requested virtual area. +* @param flags +* EAL_VIRTUAL_AREA_* flags. +* @param mmap_flags +* Extra flags passed directly to mmap(). +* +* @return +* Virtual area address if successful. +* NULL if unsuccessful. +*/ + +#define EAL_VIRTUAL_AREA_ADDR_IS_HINT (1 << 0) +/**< don't fail if cannot get exact requested address. */ +#define EAL_VIRTUAL_AREA_ALLOW_SHRINK (1 << 1) +/**< try getting smaller sized (decrement by page size) virtual areas if cannot +* get area of requested size. +*/ +#define EAL_VIRTUAL_AREA_UNMAP (1 << 2) +/**< immediately unmap reserved virtual area. */ +void * +eal_get_virtual_area (void *requested_addr,uint64_t *size, +uint64_t page_sz,int flags,int mmap_flags); + +/** * Get cpu core_id. * * This function is private to the EAL.
lib/librte_eal/linuxapp/eal/eal_memory.c +27 -110
204 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 2/41 eal: move all locking to heap
In reply to Anatoly BurakovDown the line, we will need to do everything from the heap as any alloc or free may trigger alloc/free OS memory, which would involve growing/shrinking heap.
--- lib/librte_eal/common/malloc_elem.c | 16 ++-------------- lib/librte_eal/common/malloc_heap.c | 38 +++++++++++++++++++++++++++++++++++++ lib/librte_eal/common/malloc_heap.h | 6 ++++++ lib/librte_eal/common/rte_malloc.c | 4 ++-- 4 files changed, 48 insertions(+), 16 deletions(-)
lib/librte_eal/common/malloc_elem.c +2 -14
@@ -243,10 +243,6 @@ join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)int malloc_elem_free (struct malloc_elem *elem) { -if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY ) -return -1 ; - -rte_spinlock_lock (&(elem->heap->lock));size_t sz = elem->size -sizeof (*elem) -MALLOC_ELEM_TRAILER_LEN ;uint8_t *ptr = (uint8_t *)&elem[1 ];struct malloc_elem *next =RTE_PTR_ADD (elem, elem->size);
@@ -274,8 +270,6 @@ malloc_elem_free(struct malloc_elem *elem)memset (ptr,0 , sz); -rte_spinlock_unlock (&(elem->heap->lock)); -return 0 ; }
@@ -292,11 +286,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)return 0 ;struct malloc_elem *next =RTE_PTR_ADD (elem, elem->size); -rte_spinlock_lock (&elem->heap->lock);if (next ->state !=ELEM_FREE ) - goto err_return; +return -1 ;if (elem->size + next->size < new_size) - goto err_return; +return -1 ;/* we now know the element fits, so remove from free list, * join the two
@@ -311,10 +304,5 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } -rte_spinlock_unlock (&elem->heap->lock);return 0 ; - -err_return: -rte_spinlock_unlock (&elem->heap->lock); -return -1 ; }
lib/librte_eal/common/malloc_heap.c +38 -0
@@ -145,6 +145,44 @@ malloc_heap_alloc(struct malloc_heap *heap,return elem ==NULL ?NULL : (void *)(&elem[1 ]); } +int +malloc_heap_free (struct malloc_elem *elem) +{ +struct malloc_heap *heap; +int 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; + +rte_spinlock_lock (&(heap->lock)); + + ret =malloc_elem_free (elem); + +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 */
lib/librte_eal/common/malloc_heap.h +6 -0
@@ -28,6 +28,12 @@ malloc_heap_alloc(struct malloc_heap *heap, const char *type, size_t size, unsigned flags, size_t align, size_t bound);int +malloc_heap_free (struct malloc_elem *elem); + +int +malloc_heap_resize (struct malloc_elem *elem,size_t size); + +int malloc_heap_get_stats (struct malloc_heap *heap,struct rte_malloc_socket_stats *socket_stats);
lib/librte_eal/common/rte_malloc.c +2 -2
@@ -29,7 +29,7 @@void rte_free (void *addr) {if (addr ==NULL )return ; -if (malloc_elem_free (malloc_elem_from_data (addr)) <0 ) +if (malloc_heap_free (malloc_elem_from_data (addr)) <0 )rte_panic ("Fatal error: Invalid memory\n" ); }
@@ -140,7 +140,7 @@ rte_realloc(void *ptr, size_t size, unsigned align) size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check alignment matches first, and if ok, see if we can resize block */ if (RTE_PTR_ALIGN (ptr,align) == ptr && -malloc_elem_resize (elem, size) ==0 ) +malloc_heap_resize (elem, size) ==0 )return ptr;/* either alignment is off, or we have no room to expand,
Signature · 1 line
2.7.4
PATCH 3/41 eal: make malloc heap a doubly-linked list
In reply to Anatoly BurakovAs we are preparing for dynamic memory allocation, we need to be able to handle holes in our malloc heap, hence we're switching to doubly linked list, and prepare infrastructure to support it.
Since our heap is now aware where are our first and last elements, there is no longer any need to have a dummy element at the end of each heap, so get rid of that as well. Instead, let insert/remove/ join/split operations handle end-of-list conditions automatically.
--- lib/librte_eal/common/include/rte_malloc_heap.h | 6 + lib/librte_eal/common/malloc_elem.c | 200 +++++++++++++++++++----- lib/librte_eal/common/malloc_elem.h | 14 +- lib/librte_eal/common/malloc_heap.c | 8 +- 4 files changed, 179 insertions(+), 49 deletions(-)
lib/librte_eal/common/include/rte_malloc_heap.h +6 -0
@@ -13,12 +13,18 @@/* Number of free lists per heap, grouped by size. */ #define RTE_HEAP_NUM_FREELISTS 13 +/* dummy definition, for pointers */ +struct malloc_elem ; +/** * Structure to hold malloc heap */ struct malloc_heap {rte_spinlock_t lock; LIST_HEAD(, malloc_elem) free_head[RTE_HEAP_NUM_FREELISTS]; +struct malloc_elem *first; +struct malloc_elem *last; +unsigned alloc_count;size_t total_size; } __rte_cache_aligned;
lib/librte_eal/common/malloc_elem.c +164 -36
271 lines not on this page · raw message
lib/librte_eal/common/malloc_elem.h +7 -7
@@ -18,8 +18,12 @@ enum elem_state {struct malloc_elem {struct malloc_heap *heap; -struct malloc_elem *volatile prev;/* points to prev elem in memseg */ -LIST_ENTRY (malloc_elem) free_list;/* list of free elements in heap */ +struct malloc_elem *volatile prev; +/**< points to prev elem in memseg */ +struct malloc_elem *volatile next; +/**< points to next elem in memseg */ +LIST_ENTRY (malloc_elem) free_list; +/**< list of free elements in heap */ const struct rte_memseg *ms;volatile enum elem_state state;uint32_t pad;
@@ -110,12 +114,8 @@ malloc_elem_init(struct malloc_elem *elem,const struct rte_memseg *ms, size_t size); -/* -* initialise a dummy malloc_elem header for the end-of-memseg marker -*/ void -malloc_elem_mkend (struct malloc_elem *elem, -struct malloc_elem *prev_free); +malloc_elem_insert (struct malloc_elem *elem);/* * return true if the current malloc_elem can hold a block of data
lib/librte_eal/common/malloc_heap.c +2 -6
@@ -70,15 +70,11 @@ check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)static void malloc_heap_add_memseg (struct malloc_heap *heap,struct rte_memseg *ms) { -/* allocate the memory block headers, one at end, one at start */ struct malloc_elem *start_elem = (struct malloc_elem *)ms->addr; -struct malloc_elem *end_elem =RTE_PTR_ADD (ms->addr, - ms->len -MALLOC_ELEM_OVERHEAD ); - end_elem =RTE_PTR_ALIGN_FLOOR (end_elem,RTE_CACHE_LINE_SIZE ); -const size_t elem_size = (uintptr_t )end_elem - (uintptr_t )start_elem; +const size_t elem_size = ms->len -MALLOC_ELEM_OVERHEAD ;malloc_elem_init (start_elem, heap, ms, elem_size); -malloc_elem_mkend (end_elem, start_elem); +malloc_elem_insert (start_elem);malloc_elem_free_list_insert (start_elem); heap->total_size += elem_size;
Signature · 1 line
2.7.4
Re: [PATCH 03/41] eal: make malloc heap a doubly-linked list
Quoted 104 lines · Anatoly Burakov
As we are preparing for dynamic memory allocation, we need to be able to handle holes in our malloc heap, hence we're switching to doubly linked list, and prepare infrastructure to support it.
Since our heap is now aware where are our first and last elements, there is no longer any need to have a dummy element at the end of each heap, so get rid of that as well. Instead, let insert/remove/ join/split operations handle end-of-list conditions automatically.
Signed-off-by Anatoly Burakov <[email protected]>--- lib/librte_eal/common/include/rte_malloc_heap.h | 6 + lib/librte_eal/common/malloc_elem.c | 200 +++++++++++++++++++----- lib/librte_eal/common/malloc_elem.h | 14 +- lib/librte_eal/common/malloc_heap.c | 8 +- 4 files changed, 179 insertions(+), 49 deletions(-)lib/librte_eal/common/include/rte_malloc_heap.h +6
-0@@ -13,12 +13,18 @@/* Number of free lists per heap, grouped by size. */ #define RTE_HEAP_NUM_FREELISTS 13 +/* dummy definition, for pointers */ +struct malloc_elem ; +/** * Structure to hold malloc heap */ struct malloc_heap {rte_spinlock_t lock; LIST_HEAD(, malloc_elem) free_head[RTE_HEAP_NUM_FREELISTS]; +struct malloc_elem *first; +struct malloc_elem *last; +unsigned alloc_count;size_t total_size; } __rte_cache_aligned;lib/librte_eal/common/malloc_elem.c +40
-7@@ -31,6 +31,7 @@ malloc_elem_init(struct malloc_elem *elem, elem->heap = heap; elem->ms = ms; elem->prev =NULL ; + elem->next =NULL ;memset (&elem->free_list,0 ,sizeof (elem->free_list)); elem->state =ELEM_FREE ; elem->size = size;@@ -39,15 +40,56 @@ malloc_elem_init(struct malloc_elem *elem,set_trailer (elem); } -/* -* Initialize a dummy malloc_elem header for the end-of-memseg marker -*/ void -malloc_elem_mkend (struct malloc_elem *elem,struct malloc_elem *prev) +malloc_elem_insert (struct malloc_elem *elem) { -malloc_elem_init (elem, prev->heap, prev->ms,0 ); - elem->prev = prev; - elem->state =ELEM_BUSY ;/* mark busy so its never merged */ +struct malloc_elem *prev_elem, *next_elem; +struct malloc_heap *heap = elem->heap; + +if (heap->first ==NULL && heap->last ==NULL ) { +/* if empty heap */ + heap->first = elem; + heap->last = elem; + prev_elem =NULL ; + next_elem =NULL ; + }else if (elem < heap->first) { +/* if lower than start */ + prev_elem =NULL ; + next_elem = heap->first; + heap->first = elem; + }else if (elem > heap->last) { +/* if higher than end */ + prev_elem = heap->last; + next_elem =NULL ; + heap->last = elem; + }else { +/* the new memory is somewhere inbetween start and end */ +uint64_t dist_from_start, dist_from_end; + + dist_from_end =RTE_PTR_DIFF (heap->last, elem); + dist_from_start =RTE_PTR_DIFF (elem, heap->first); + +/* check which is closer, and find closest list entries */ +if (dist_from_start < dist_from_end) { + prev_elem = heap->first; +while (prev_elem->next < elem) + prev_elem = prev_elem->next; + next_elem = prev_elem->next; + }else { + next_elem = heap->last; +while (next_elem->prev > elem) + next_elem = next_elem->prev; + prev_elem = next_elem->prev; + }
lib/librte_eal/common/malloc_elem.c malloc_elem_init(struct malloc_elem *elem, · line 93
Quoted 10 lines
lib/librte_eal/common/malloc_elem.c +9
-0+ } + +/* insert new element */ + elem->prev = prev_elem; + elem->next = next_elem; +if (prev_elem) + prev_elem->next = elem; +if (next_elem) + next_elem->prev = elem; }
Would it be possible here to use a TAILQ? If yes, it could be easier to read.
Re: [PATCH 03/41] eal: make malloc heap a doubly-linked list
Quoted 110 lines · Olivier Matz
Quoted 109 lines · Anatoly Burakov
As we are preparing for dynamic memory allocation, we need to be able to handle holes in our malloc heap, hence we're switching to doubly linked list, and prepare infrastructure to support it.
Since our heap is now aware where are our first and last elements, there is no longer any need to have a dummy element at the end of each heap, so get rid of that as well. Instead, let insert/remove/ join/split operations handle end-of-list conditions automatically.
Signed-off-by Anatoly Burakov <[email protected]>--- lib/librte_eal/common/include/rte_malloc_heap.h | 6 + lib/librte_eal/common/malloc_elem.c | 200 +++++++++++++++++++----- lib/librte_eal/common/malloc_elem.h | 14 +- lib/librte_eal/common/malloc_heap.c | 8 +- 4 files changed, 179 insertions(+), 49 deletions(-)lib/librte_eal/common/include/rte_malloc_heap.h +6
-0@@ -13,12 +13,18 @@/* Number of free lists per heap, grouped by size. */ #define RTE_HEAP_NUM_FREELISTS 13 +/* dummy definition, for pointers */ +struct malloc_elem ; +/** * Structure to hold malloc heap */ struct malloc_heap {rte_spinlock_t lock; LIST_HEAD(, malloc_elem) free_head[RTE_HEAP_NUM_FREELISTS]; +struct malloc_elem *first; +struct malloc_elem *last; +unsigned alloc_count;size_t total_size; } __rte_cache_aligned;lib/librte_eal/common/malloc_elem.c +45
-7@@ -31,6 +31,7 @@ malloc_elem_init(struct malloc_elem *elem, elem->heap = heap; elem->ms = ms; elem->prev =NULL ; + elem->next =NULL ;memset (&elem->free_list,0 ,sizeof (elem->free_list)); elem->state =ELEM_FREE ; elem->size = size;@@ -39,15 +40,56 @@ malloc_elem_init(struct malloc_elem *elem,set_trailer (elem); } -/* -* Initialize a dummy malloc_elem header for the end-of-memseg marker -*/ void -malloc_elem_mkend (struct malloc_elem *elem,struct malloc_elem *prev) +malloc_elem_insert (struct malloc_elem *elem) { -malloc_elem_init (elem, prev->heap, prev->ms,0 ); - elem->prev = prev; - elem->state =ELEM_BUSY ;/* mark busy so its never merged */ +struct malloc_elem *prev_elem, *next_elem; +struct malloc_heap *heap = elem->heap; + +if (heap->first ==NULL && heap->last ==NULL ) { +/* if empty heap */ + heap->first = elem; + heap->last = elem; + prev_elem =NULL ; + next_elem =NULL ; + }else if (elem < heap->first) { +/* if lower than start */ + prev_elem =NULL ; + next_elem = heap->first; + heap->first = elem; + }else if (elem > heap->last) { +/* if higher than end */ + prev_elem = heap->last; + next_elem =NULL ; + heap->last = elem; + }else { +/* the new memory is somewhere inbetween start and end */ +uint64_t dist_from_start, dist_from_end; + + dist_from_end =RTE_PTR_DIFF (heap->last, elem); + dist_from_start =RTE_PTR_DIFF (elem, heap->first); + +/* check which is closer, and find closest list entries */ +if (dist_from_start < dist_from_end) { + prev_elem = heap->first; +while (prev_elem->next < elem) + prev_elem = prev_elem->next; + next_elem = prev_elem->next; + }else { + next_elem = heap->last; +while (next_elem->prev > elem) + next_elem = next_elem->prev; + prev_elem = next_elem->prev; + } + } + +/* insert new element */ + elem->prev = prev_elem; + elem->next = next_elem;
Quoted 9 lines
lib/librte_eal/common/malloc_elem.c malloc_elem_init(struct malloc_elem *elem, · line 93
Quoted 5 lines
lib/librte_eal/common/malloc_elem.c +4
-0+if (prev_elem) + prev_elem->next = elem; +if (next_elem) + next_elem->prev = elem; }Would it be possible here to use a TAILQ? If yes, it could be easier to read.
Hi Olivier,
I think it would be a bit hard to make TAILQ's work with pad elements without making the code unreadable :) I am inclined to leave it as is.
Signature · 2 lines
Thanks, Anatoly
PATCH 4/41 eal: add function to dump malloc heap contents
In reply to Anatoly BurakovMalloc heap is now a doubly linked list, so it's now possible to iterate over each malloc element regardless of its state.
--- lib/librte_eal/common/include/rte_malloc.h | 9 +++++++++ lib/librte_eal/common/malloc_elem.c | 24 ++++++++++++++++++++++++ lib/librte_eal/common/malloc_elem.h | 6 ++++++ lib/librte_eal/common/malloc_heap.c | 22 ++++++++++++++++++++++ lib/librte_eal/common/malloc_heap.h | 3 +++ lib/librte_eal/common/rte_malloc.c | 16 ++++++++++++++++ lib/librte_eal/rte_eal_version.map | 1 + 7 files changed, 81 insertions(+)
lib/librte_eal/common/include/rte_malloc.h +9 -0
@@ -278,6 +278,15 @@ void rte_malloc_dump_stats(FILE *f, const char *type);/** +* Dump contents of all malloc heaps to a file. +* +* @param f +* A pointer to a file for output +*/ +void +rte_malloc_dump_heaps (FILE *f); + +/** * Set the maximum amount of allocated memory for this type. * * This is not yet implemented
lib/librte_eal/common/malloc_elem.c +24 -0
@@ -1,6 +1,7 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2010-2014 Intel Corporation */ +#include <inttypes.h> #include <stdint.h> #include <stddef.h> #include <stdio.h>
@@ -434,3 +435,26 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size) }return 0 ; } + +static inline const char * +elem_state_to_str (enum elem_state state) +{ +switch (state) { +case ELEM_PAD : +return "PAD" ; +case ELEM_BUSY : +return "BUSY" ; +case ELEM_FREE : +return "FREE" ; + } +return "ERROR" ; +} + +void +malloc_elem_dump (const struct malloc_elem *elem,FILE *f) +{ +fprintf (f,"Malloc element at %p (%s)\n" , elem, +elem_state_to_str (elem->state)); +fprintf (f," len: 0x%zx pad: 0x%" PRIx32"\n" , elem->size, elem->pad); +fprintf (f," prev: %p next: %p\n" , elem->prev, elem->next); +}
lib/librte_eal/common/malloc_elem.h +6 -0
@@ -149,6 +149,12 @@ int malloc_elem_resize(struct malloc_elem *elem, size_t size);/* +* dump contents of malloc elem to a file. +*/ +void +malloc_elem_dump (const struct malloc_elem *elem,FILE *f); + +/* * Given an element size, compute its freelist index. */ size_t
lib/librte_eal/common/malloc_heap.c +22 -0
@@ -217,6 +217,28 @@ malloc_heap_get_stats(struct malloc_heap *heap,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 ) {
lib/librte_eal/common/malloc_heap.h +3 -0
@@ -37,6 +37,9 @@ int malloc_heap_get_stats(struct malloc_heap *heap, struct rte_malloc_socket_stats *socket_stats); +void +malloc_heap_dump (struct malloc_heap *heap,FILE *f); +int rte_eal_malloc_heap_init (void );
lib/librte_eal/common/rte_malloc.c +16 -0
@@ -182,6 +182,22 @@ rte_malloc_get_socket_stats(int socket, }/* +* Function to dump contents of all heaps +*/ +void +rte_malloc_dump_heaps (FILE *f) +{ +struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config; +unsigned int socket; + +for (socket =0 ; socket <rte_num_sockets (); socket++) { +fprintf (f,"Heap on socket %i:\n" , socket); +malloc_heap_dump (&mcfg->malloc_heaps[socket], f); + } + +} + +/* * Print stats on memory type. If type is NULL, info on all types is printed */ void
lib/librte_eal/rte_eal_version.map +1 -0
@@ -215,6 +215,7 @@ DPDK_18.05 { global: rte_num_sockets; + rte_malloc_dump_heaps; } DPDK_18.02;
Signature · 1 line
2.7.4
PATCH 5/41 test: add command to dump malloc heap contents
In reply to Anatoly Burakov--- test/test/commands.c | 3 +++ 1 file changed, 3 insertions(+)
test/test/commands.c +3 -0
@@ -137,6 +137,8 @@ static void cmd_dump_parsed(void *parsed_result,rte_log_dump (stdout); else if (!strcmp(res->dump, "dump_malloc_stats"))rte_malloc_dump_stats (stdout,NULL ); + else if (!strcmp(res->dump, "dump_malloc_heaps")) +rte_malloc_dump_heaps (stdout); } cmdline_parse_token_string_t cmd_dump_dump =
@@ -147,6 +149,7 @@ cmdline_parse_token_string_t cmd_dump_dump ="dump_ring#" "dump_mempool#" "dump_malloc_stats#" +"dump_malloc_heaps#" "dump_devargs#" "dump_log_types");
Signature · 1 line
2.7.4
PATCH 6/41 eal: make malloc_elem_join_adjacent_free public
In reply to Anatoly BurakovWe need this function to join newly allocated segments with the heap.
--- lib/librte_eal/common/malloc_elem.c | 6 +++--- lib/librte_eal/common/malloc_elem.h | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-)
lib/librte_eal/common/malloc_elem.c +3 -3
@@ -325,8 +325,8 @@ join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2) elem1->next = next; } -static struct malloc_elem * -elem_join_adjacent_free (struct malloc_elem *elem) +struct malloc_elem * +malloc_elem_join_adjacent_free (struct malloc_elem *elem) {/* * check if next element exists, is adjacent and is free, if so join
@@ -388,7 +388,7 @@ malloc_elem_free(struct malloc_elem *elem) ptr =RTE_PTR_ADD (elem,sizeof (*elem)); data_len = elem->size -MALLOC_ELEM_OVERHEAD ; - elem =elem_join_adjacent_free (elem); + elem =malloc_elem_join_adjacent_free (elem);malloc_elem_free_list_insert (elem);
lib/librte_eal/common/malloc_elem.h +3 -0
@@ -141,6 +141,9 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size,int malloc_elem_free (struct malloc_elem *elem); +struct malloc_elem * +malloc_elem_join_adjacent_free (struct malloc_elem *elem); +/* * attempt to resize a malloc_elem by expanding into any free space * immediately after it in memory.
Signature · 1 line
2.7.4
PATCH 7/41 eal: make malloc free list remove public
In reply to Anatoly Burakov--- lib/librte_eal/common/malloc_elem.c | 12 ++++++------ lib/librte_eal/common/malloc_elem.h | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-)
lib/librte_eal/common/malloc_elem.c +6 -6
@@ -245,8 +245,8 @@ malloc_elem_free_list_insert(struct malloc_elem *elem)/* * Remove the specified element from its heap's free list. */ -static void -elem_free_list_remove (struct malloc_elem *elem) +void +malloc_elem_free_list_remove (struct malloc_elem *elem) {LIST_REMOVE (elem, free_list); }
@@ -266,7 +266,7 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,const size_t trailer_size = elem->size - old_elem_size - size -MALLOC_ELEM_OVERHEAD ; -elem_free_list_remove (elem); +malloc_elem_free_list_remove (elem);if (trailer_size >MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) {/* split it, too much free space after elem */
@@ -340,7 +340,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) erase =RTE_PTR_SUB (elem->next,MALLOC_ELEM_TRAILER_LEN );/* remove from free list, join to this one */ -elem_free_list_remove (elem->next); +malloc_elem_free_list_remove (elem->next);join_elem (elem, elem->next);/* erase header and trailer */
@@ -360,7 +360,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) erase =RTE_PTR_SUB (elem,MALLOC_ELEM_TRAILER_LEN );/* remove from free list, join to this one */ -elem_free_list_remove (elem->prev); +malloc_elem_free_list_remove (elem->prev); new_elem = elem->prev;join_elem (new_elem, elem);
@@ -423,7 +423,7 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* we now know the element fits, so remove from free list, * join the two */ -elem_free_list_remove (elem->next); +malloc_elem_free_list_remove (elem->next);join_elem (elem, elem->next);if (elem->size - new_size >=MIN_DATA_SIZE +MALLOC_ELEM_OVERHEAD ) {
lib/librte_eal/common/malloc_elem.h +3 -0
@@ -151,6 +151,9 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem);int malloc_elem_resize (struct malloc_elem *elem,size_t size); +void +malloc_elem_free_list_remove (struct malloc_elem *elem); +/* * dump contents of malloc elem to a file. */
Signature · 1 line
2.7.4
PATCH 8/41 eal: make malloc free return resulting malloc element
In reply to Anatoly Burakov--- lib/librte_eal/common/malloc_elem.c | 4 ++-- lib/librte_eal/common/malloc_elem.h | 2 +- lib/librte_eal/common/malloc_heap.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
lib/librte_eal/common/malloc_elem.c +2 -2
@@ -379,7 +379,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem)* blocks either immediately before or immediately after newly freed block * are also free, the blocks are merged together. */ -int +struct malloc_elem *malloc_elem_free (struct malloc_elem *elem) {void *ptr;
@@ -397,7 +397,7 @@ malloc_elem_free(struct malloc_elem *elem)memset (ptr,0 , data_len); -return 0 ; +return elem; }/*
lib/librte_eal/common/malloc_elem.h +1 -1
@@ -138,7 +138,7 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size,* blocks either immediately before or immediately after newly freed block * are also free, the blocks are merged together. */ -int +struct malloc_elem *malloc_elem_free (struct malloc_elem *elem); struct malloc_elem *
lib/librte_eal/common/malloc_heap.c +2 -2
@@ -145,7 +145,7 @@ int malloc_heap_free(struct malloc_elem *elem) {struct malloc_heap *heap; -int ret; +struct malloc_elem *ret;if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ;
@@ -159,7 +159,7 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_unlock (&(heap->lock)); -return ret; +return ret !=NULL ?0 :-1 ; }int
Signature · 1 line
2.7.4
Re: [PATCH 08/41] eal: make malloc free return resulting malloc element
Quoted 21 lines · Anatoly Burakov
Signed-off-by Anatoly Burakov <[email protected]>--- lib/librte_eal/common/malloc_elem.c | 4 ++-- lib/librte_eal/common/malloc_elem.h | 2 +- lib/librte_eal/common/malloc_heap.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)lib/librte_eal/common/malloc_elem.c +1
-1@@ -379,7 +379,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem)* blocks either immediately before or immediately after newly freed block * are also free, the blocks are merged together. */ -int +struct malloc_elem *malloc_elem_free (struct malloc_elem *elem) {void *ptr;
lib/librte_eal/common/malloc_elem.c malloc_elem_free(struct malloc_elem *elem) · line 403
Quoted 8 lines
lib/librte_eal/common/malloc_elem.c +1
-1@@ -397,7 +397,7 @@ malloc_elem_free(struct malloc_elem *elem)memset (ptr,0 , data_len); -return 0 ; +return elem; }/*
An explanation about why this change is needed would make sense I think.
Thanks,
Olivier
Re: [PATCH 08/41] eal: make malloc free return resulting malloc element
Quoted 29 lines · Olivier Matz
Quoted 28 lines · Anatoly Burakov
Signed-off-by Anatoly Burakov <[email protected]>--- lib/librte_eal/common/malloc_elem.c | 4 ++-- lib/librte_eal/common/malloc_elem.h | 2 +- lib/librte_eal/common/malloc_heap.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)lib/librte_eal/common/malloc_elem.c +2
-2@@ -379,7 +379,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem)* blocks either immediately before or immediately after newly freed block * are also free, the blocks are merged together. */ -int +struct malloc_elem *malloc_elem_free (struct malloc_elem *elem) {void *ptr;@@ -397,7 +397,7 @@ malloc_elem_free(struct malloc_elem *elem)memset (ptr,0 , data_len); -return 0 ; +return elem; }
Quoted 7 lines
lib/librte_eal/common/malloc_elem.c malloc_elem_free(struct malloc_elem *elem) · line 403
Quoted 1 line
lib/librte_eal/common/malloc_elem.c +0
-0/* An explanation about why this change is needed would make sense I think.
Thanks,
Olivier
Sure, i'll add this in future commits.
However, to provide some context - we need this because down the line we will need to know which element did we create/free to be able to roll back the changes, should the sync fail.
Signature · 2 lines
Thanks, Anatoly
PATCH 9/41 eal: add rte_fbarray
In reply to Anatoly Burakovrte_fbarray is a simple indexed array stored in shared memory via mapping files into memory. Rationale for its existence is the
following: since we are going to map memory page-by-page, there could be quite a lot of memory segments to keep track of (for smaller page sizes, page count can easily reach thousands). We can't really make page lists truly dynamic and infinitely expandable, because that involves reallocating memory (which is a big no-no in multiprocess). What we can do instead is have a maximum capacity as something really, really large, and decide at allocation time how big the array is going to be. We map the entire file into memory, which makes it possible to use fbarray as shared memory, provided the structure itself is allocated in shared memory. Per-fbarray locking is also used to avoid index data races (but not contents data races - that is up to user application to synchronize).
In addition, in understanding that we will frequently need to scan this array for free space and iterating over array linearly can become slow, rte_fbarray provides facilities to index array's usage. The following use cases are covered:
- find next free/used slot (useful either for adding new elements to fbarray, or walking the list)
- find starting index for next N free/used slots (useful for when we want to allocate chunk of VA-contiguous memory composed of several pages)
- find how many contiguous free/used slots there are, starting from specified index (useful for when we want to figure out how many pages we have until next hole in allocated memory, to speed up some bulk operations where we would otherwise have to walk the array and add pages one by one)
This is accomplished by storing a usage mask in-memory, right after the data section of the array, and using some bit-level magic to figure out the info we need.
---
Notes:
Initial version of this had resizing capability, however it was removed due to the fact that in multiprocess scenario, each fbarray would have its own view of mapped memory, which might not correspond with others due to some other process performing a resize that current process didn't know about.
It was therefore decided that to avoid cost of synchronization on each and every operation (to make sure the array wasn't resized), resizing feature should be dropped.
lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/common/Makefile | 2 +- lib/librte_eal/common/eal_common_fbarray.c | 859 ++++++++++++++++++++++++++++ lib/librte_eal/common/eal_filesystem.h | 13 + lib/librte_eal/common/include/rte_fbarray.h | 352 ++++++++++++ lib/librte_eal/common/meson.build | 2 + lib/librte_eal/linuxapp/eal/Makefile | 1 + lib/librte_eal/rte_eal_version.map | 17 + 8 files changed, 1246 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/eal_common_fbarray.c create mode 100644 lib/librte_eal/common/include/rte_fbarray.h
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -53,6 +53,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_dev.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_options.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_proc.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_heap.c
lib/librte_eal/common/Makefile +1 -1
@@ -16,7 +16,7 @@ INC += rte_pci_dev_feature_defs.h rte_pci_dev_features.h INC += rte_malloc.h rte_keepalive.h rte_time.h INC += rte_service.h rte_service_component.h INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h -INC += rte_reciprocal.h +INC += rte_reciprocal.h rte_fbarray.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h
lib/librte_eal/common/eal_common_fbarray.c new file +859 -0
859 lines not on this page · raw message
lib/librte_eal/common/eal_filesystem.h +13 -0
@@ -13,6 +13,7 @@/** Path of rte config file. */ #define RUNTIME_CONFIG_FMT "%s/.%s_config" +#define FBARRAY_FMT "%s/%s_%s"#include <stdint.h> #include <limits.h>
@@ -55,6 +56,18 @@ eal_mp_socket_path(void)return buffer; } +static inline const char * +eal_get_fbarray_path (char *buffer,size_t buflen,const char *name) { +const char *directory ="/tmp" ; +const char *home_dir =getenv ("HOME" ); + +if (getuid () !=0 && home_dir !=NULL ) + directory = home_dir; +snprintf (buffer, buflen -1 ,FBARRAY_FMT , directory, + internal_config.hugefile_prefix, name); +return buffer; +} +/** Path of hugepage info file. */ #define HUGEPAGE_INFO_FMT "%s/.%s_hugepage_info"
lib/librte_eal/common/include/rte_fbarray.h new file +352 -0
352 lines not on this page · raw message
lib/librte_eal/common/meson.build +2 -0
@@ -11,6 +11,7 @@ common_sources = files( 'eal_common_devargs.c', 'eal_common_dev.c', 'eal_common_errno.c', + 'eal_common_fbarray.c', 'eal_common_hexdump.c', 'eal_common_launch.c', 'eal_common_lcore.c',
@@ -51,6 +52,7 @@ common_headers = files( 'include/rte_eal_memconfig.h', 'include/rte_eal_interrupts.h', 'include/rte_errno.h', + 'include/rte_fbarray.h', 'include/rte_hexdump.h', 'include/rte_interrupts.h', 'include/rte_keepalive.h',
lib/librte_eal/linuxapp/eal/Makefile +1 -0
@@ -61,6 +61,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_dev.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_options.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_proc.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_heap.c
lib/librte_eal/rte_eal_version.map +17 -0
@@ -216,6 +216,23 @@ DPDK_18.05 { rte_num_sockets; rte_malloc_dump_heaps; + rte_fbarray_init; + rte_fbarray_destroy; + rte_fbarray_attach; + rte_fbarray_detach; + rte_fbarray_resize; + rte_fbarray_get; + rte_fbarray_find_idx; + rte_fbarray_set_free; + rte_fbarray_set_used; + rte_fbarray_is_used; + rte_fbarray_find_next_free; + rte_fbarray_find_next_used; + rte_fbarray_find_next_n_free; + rte_fbarray_find_next_n_used; + rte_fbarray_find_contig_free; + rte_fbarray_find_contig_used; + rte_fbarray_dump_metadata; } DPDK_18.02;
Signature · 1 line
2.7.4
PATCH 10/41 eal: add "single file segments" command-line option
In reply to Anatoly BurakovFor now, this option does nothing, but it will be useful in dynamic memory allocation down the line. Currently, DPDK stores all pages as separate files in hugetlbfs. This option will allow storing all pages in one file (one file per socket, per page size).
--- lib/librte_eal/common/eal_common_options.c | 4 ++++ lib/librte_eal/common/eal_internal_cfg.h | 4 ++++ lib/librte_eal/common/eal_options.h | 2 ++ lib/librte_eal/linuxapp/eal/eal.c | 1 + 4 files changed, 11 insertions(+)
lib/librte_eal/common/eal_common_options.c +4 -0
@@ -73,6 +73,7 @@ eal_long_options[] = { {OPT_VDEV, 1, NULL, OPT_VDEV_NUM }, {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM }, {OPT_VMWARE_TSC_MAP, 0, NULL, OPT_VMWARE_TSC_MAP_NUM }, + {OPT_SINGLE_FILE_SEGMENTS, 0, NULL, OPT_SINGLE_FILE_SEGMENTS_NUM}, {0, 0, NULL, 0 } };
@@ -1161,6 +1162,9 @@ eal_parse_common_option(int opt, const char *optarg, core_parsed =LCORE_OPT_MAP ;break ; +case OPT_SINGLE_FILE_SEGMENTS_NUM : + conf->single_file_segments =1 ; +break ;/* don't know what to do, leave this to caller */ default :
lib/librte_eal/common/eal_internal_cfg.h +4 -0
@@ -47,6 +47,10 @@ struct internal_config {volatile unsigned force_sockets;volatile uint64_t socket_mem[RTE_MAX_NUMA_NODES ];/**< amount of memory per socket */ uintptr_t base_virtaddr;/**< base address to try and reserve memory from */ +volatile unsigned single_file_segments; +/**< true if storing all pages within single files (per-page-size, +* per-node). +*/ volatile int syslog_facility;/**< facility passed to openlog() */ /** default interrupt mode for VFIO */ volatile enum rte_intr_mode vfio_intr_mode;
lib/librte_eal/common/eal_options.h +2 -0
@@ -55,6 +55,8 @@ enum {OPT_VFIO_INTR_NUM , #define OPT_VMWARE_TSC_MAP "vmware-tsc-map"OPT_VMWARE_TSC_MAP_NUM , +#define OPT_SINGLE_FILE_SEGMENTS "single-file-segments" +OPT_SINGLE_FILE_SEGMENTS_NUM , OPT_LONG_MAX_NUM };
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -348,6 +348,7 @@ eal_usage(const char *prgname)" --" OPT_BASE_VIRTADDR " Base virtual address\n" " --" OPT_CREATE_UIO_DEV " Create /dev/uioX (usually done by hotplug)\n" " --" OPT_VFIO_INTR " Interrupt mode for VFIO (legacy|msi|msix)\n" +" --" OPT_SINGLE_FILE_SEGMENTS " Put all hugepage memory in single files\n" "\n");/* Allow the application to print its usage message too if hook is set */ if ( rte_application_usage_hook ) {
Signature · 1 line
2.7.4
PATCH 11/41 eal: add "legacy memory" option
In reply to Anatoly BurakovThis adds a "--legacy-mem" command-line switch. It will be used to go back to the old memory behavior, one where we can't dynamically allocate/free memory (the downside), but one where the user can get physically contiguous memory, like before (the upside).
For now, nothing but the legacy behavior exists, non-legacy memory init sequence will be added later.
--- lib/librte_eal/bsdapp/eal/eal.c | 3 +++ lib/librte_eal/common/eal_common_options.c | 4 ++++ lib/librte_eal/common/eal_internal_cfg.h | 4 ++++ lib/librte_eal/common/eal_options.h | 2 ++ lib/librte_eal/linuxapp/eal/eal.c | 1 + lib/librte_eal/linuxapp/eal/eal_memory.c | 24 ++++++++++++++++++++---- 6 files changed, 34 insertions(+), 4 deletions(-)
lib/librte_eal/bsdapp/eal/eal.c +3 -0
@@ -531,6 +531,9 @@ rte_eal_init(int argc, char **argv)return -1 ; } +/* FreeBSD always uses legacy memory model */ + internal_config.legacy_mem = true; +if (eal_plugins_init () <0 ) {rte_eal_init_alert ("Cannot init plugins\n" ); rte_errno =EINVAL ;
lib/librte_eal/common/eal_common_options.c +4 -0
@@ -74,6 +74,7 @@ eal_long_options[] = { {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM }, {OPT_VMWARE_TSC_MAP, 0, NULL, OPT_VMWARE_TSC_MAP_NUM }, {OPT_SINGLE_FILE_SEGMENTS, 0, NULL, OPT_SINGLE_FILE_SEGMENTS_NUM}, + {OPT_LEGACY_MEM, 0, NULL, OPT_LEGACY_MEM_NUM }, {0, 0, NULL, 0 } };
@@ -1165,6 +1166,9 @@ eal_parse_common_option(int opt, const char *optarg,case OPT_SINGLE_FILE_SEGMENTS_NUM : conf->single_file_segments =1 ;break ; +case OPT_LEGACY_MEM_NUM : + conf->legacy_mem =1 ; +break ;/* don't know what to do, leave this to caller */ default :
lib/librte_eal/common/eal_internal_cfg.h +4 -0
@@ -51,6 +51,10 @@ struct internal_config {/**< true if storing all pages within single files (per-page-size, * per-node). */ +volatile unsigned legacy_mem; +/**< true to enable legacy memory behavior (no dynamic allocation, +* contiguous segments). +*/ volatile int syslog_facility;/**< facility passed to openlog() */ /** default interrupt mode for VFIO */ volatile enum rte_intr_mode vfio_intr_mode;
lib/librte_eal/common/eal_options.h +2 -0
@@ -57,6 +57,8 @@ enum {OPT_VMWARE_TSC_MAP_NUM , #define OPT_SINGLE_FILE_SEGMENTS "single-file-segments"OPT_SINGLE_FILE_SEGMENTS_NUM , +#define OPT_LEGACY_MEM "legacy-mem" +OPT_LEGACY_MEM_NUM , OPT_LONG_MAX_NUM };
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -349,6 +349,7 @@ eal_usage(const char *prgname)" --" OPT_CREATE_UIO_DEV " Create /dev/uioX (usually done by hotplug)\n" " --" OPT_VFIO_INTR " Interrupt mode for VFIO (legacy|msi|msix)\n" " --" OPT_SINGLE_FILE_SEGMENTS " Put all hugepage memory in single files\n" +" --" OPT_LEGACY_MEM " Legacy memory mode (no dynamic allocation, contiguous segments)\n" "\n");/* Allow the application to print its usage message too if hook is set */ if ( rte_application_usage_hook ) {
lib/librte_eal/linuxapp/eal/eal_memory.c +20 -4
@@ -919,8 +919,8 @@ huge_recover_sigbus(void)* 6. unmap the first mapping * 7. fill memsegs in configuration with contiguous zones */ -int -rte_eal_hugepage_init (void ) +static int +eal_legacy_hugepage_init (void ) {struct rte_mem_config *mcfg;struct hugepage_file *hugepage =NULL , *tmp_hp =NULL ;
@@ -1262,8 +1262,8 @@ getFileSize(int fd)* configuration and finds the hugepages which form that segment, mapping them * in order to form a contiguous block in the virtual memory space */ -int -rte_eal_hugepage_attach (void ) +static int +eal_legacy_hugepage_attach (void ) {const struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config;struct hugepage_file *hp =NULL ;
@@ -1399,6 +1399,22 @@ rte_eal_hugepage_attach(void) }int +rte_eal_hugepage_init (void ) +{ +if (internal_config.legacy_mem) +return eal_legacy_hugepage_init (); +return -1 ; +} + +int +rte_eal_hugepage_attach (void ) +{ +if (internal_config.legacy_mem) +return eal_legacy_hugepage_attach (); +return -1 ; +} + +int rte_eal_using_phys_addrs (void ) {return phys_addrs_available;
Signature · 1 line
2.7.4
PATCH 12/41 eal: read hugepage counts from node-specific sysfs path
In reply to Anatoly BurakovFor non-legacy memory init mode, instead of looking at generic sysfs path, look at sysfs paths pertaining to each NUMA node for hugepage counts. Note that per-NUMA node path does not provide information regarding reserved pages, so we might not get the best info from these paths, but this saves us from the whole mapping/remapping business before we're actually able to tell which page is on which socket, because we no longer require our memory to be physically contiguous.
Legacy memory init will not use this.
--- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 79 +++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 6 deletions(-)
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +73 -6
@@ -30,6 +30,7 @@#include "eal_filesystem.h" static const char sys_dir_path[] ="/sys/kernel/mm/hugepages" ; +static const char sys_pages_numa_dir_path[] ="/sys/devices/system/node" ;/* this function is only called from eal_hugepage_info_init which itself * is only called from a primary process */
@@ -70,6 +71,45 @@ get_num_hugepages(const char *subdir)return num_pages; } +static uint32_t +get_num_hugepages_on_node (const char *subdir,unsigned int socket) +{ +char path[PATH_MAX ], socketpath[PATH_MAX ]; +DIR *socketdir; +unsigned long num_pages =0 ; +const char *nr_hp_file ="free_hugepages" ; + +snprintf (socketpath,sizeof (socketpath),"%s/node%u/hugepages" , + sys_pages_numa_dir_path, socket); + + socketdir =opendir (socketpath); +if (socketdir) { +/* Keep calm and carry on */ +closedir (socketdir); + }else { +/* Can't find socket dir, so ignore it */ +return 0 ; + } + +snprintf (path,sizeof (path),"%s/%s/%s" , + socketpath, subdir, nr_hp_file); +if (eal_parse_sysfs_value (path, &num_pages) <0 ) +return 0 ; + +if (num_pages ==0 ) +RTE_LOG (WARNING ,EAL ,"No free hugepages reported in %s\n" , + subdir); + +/* +* we want to return a uint32_t and more than this looks suspicious +* anyway ... +*/ +if (num_pages >UINT32_MAX ) + num_pages =UINT32_MAX ; + +return num_pages; +} +static uint64_t get_default_hp_size (void ) {
@@ -248,7 +288,7 @@ eal_hugepage_info_init(void) {const char dirent_start_text[] ="hugepages-" ;const size_t dirent_start_len =sizeof (dirent_start_text) -1 ; -unsigned i, num_sizes =0 ; +unsigned int i, total_pages, num_sizes =0 ;DIR *dir;struct dirent *dirent;
@@ -302,9 +342,27 @@ eal_hugepage_info_init(void)if (clear_hugedir (hpi->hugedir) ==-1 )break ; -/* for now, put all pages into socket 0, -* later they will be sorted */ - hpi->num_pages[0 ] =get_num_hugepages (dirent->d_name); +/* +* first, try to put all hugepages into relevant sockets, but +* if first attempts fails, fall back to collecting all pages +* in one socket and sorting them later +*/ + total_pages =0 ; +/* we also don't want to do this for legacy init */ +if (!internal_config.legacy_mem) +for (i =0 ; i <rte_num_sockets (); i++) { +unsigned int num_pages = +get_num_hugepages_on_node ( + dirent->d_name, i); + hpi->num_pages[i] = num_pages; + total_pages += num_pages; + } +/* +* we failed to sort memory from the get go, so fall +* back to old way +*/ +if (total_pages ==0 ) + hpi->num_pages[0 ] =get_num_hugepages (dirent->d_name);#ifndef RTE_ARCH_64 /* for 32-bit systems, limit number of hugepages to
@@ -328,10 +386,19 @@ eal_hugepage_info_init(void) sizeof(internal_config.hugepage_info[0]), compare_hpi);/* now we have all info, check we have at least one valid size */ -for (i =0 ; i < num_sizes; i++) +for (i =0 ; i < num_sizes; i++) { +/* pages may no longer all be on socket 0, so check all */ +unsigned int j, num_pages =0 ; + +for (j =0 ; j <RTE_MAX_NUMA_NODES ; j++) { +struct hugepage_info *hpi = + &internal_config.hugepage_info[i]; + num_pages += hpi->num_pages[j]; + }if (internal_config.hugepage_info[i].hugedir !=NULL && - internal_config.hugepage_info[i].num_pages[0 ] >0 ) + num_pages >0 )return 0 ; + }/* no valid hugepage mounts available, return error */ return -1 ;
Signature · 1 line
2.7.4
PATCH 13/41 eal: replace memseg with memseg lists
In reply to Anatoly BurakovBefore, we were aggregating multiple pages into one memseg, so the number of memsegs was small. Now, each page gets its own memseg, so the list of memsegs is huge. To accommodate the new memseg list size and to keep the under-the-hood workings sane, the memseg list is now not just a single list, but multiple lists. To be precise, each hugepage size available on the system gets one or more memseg lists, per socket.
In order to support dynamic memory allocation, we reserve all memory in advance. As in, we do an anonymous mmap() of the entire maximum size of memory per hugepage size, per socket (which is limited to either RTE_MAX_MEMSEG_PER_TYPE pages or RTE_MAX_MEM_PER_TYPE gigabytes worth of memory, whichever is the smaller one), split over multiple lists (which are limited to either RTE_MAX_MEMSEG_PER_LIST memsegs or RTE_MAX_MEM_PER_LIST gigabytes per list, whichever is the smaller one).
So, for each hugepage size, we get (by default) up to 128G worth of memory, per socket, split into chunks of up to 32G in size. The address space is claimed at the start, in eal_common_memory.c. The actual page allocation code is in eal_memalloc.c (Linux-only for now), and largely consists of copied EAL memory init code.
Pages in the list are also indexed by address. That is, for non-legacy mode, in order to figure out where the page belongs, one can simply look at base address for a memseg list. Similarly, figuring out IOVA address of a memzone is a matter of finding the right memseg list, getting offset and dividing by page size to get the appropriate memseg. For legacy mode, old behavior of walking the memseg list remains.
Due to switch to fbarray and to avoid any intrusive changes, secondary processes are not supported in this commit. Also, one particular API call (dump physmem layout) no longer makes sense and was removed, according to deprecation notice [1].
In legacy mode, nothing is preallocated, and all memsegs are in a list like before, but each segment still resides in an appropriate memseg list.
The rest of the changes are really ripple effects from the memseg change - heap changes, compile fixes, and rewrites to support fbarray-backed memseg lists.
[1] http://dpdk.org/dev/patchwork/patch/34002/
--- config/common_base | 15 +- drivers/bus/pci/linux/pci.c | 29 +- drivers/net/virtio/virtio_user/vhost_kernel.c | 108 +++++--- lib/librte_eal/common/eal_common_memory.c | 322 +++++++++++++++++++--- lib/librte_eal/common/eal_common_memzone.c | 12 +- lib/librte_eal/common/eal_hugepages.h | 2 + lib/librte_eal/common/eal_internal_cfg.h | 2 +- lib/librte_eal/common/include/rte_eal_memconfig.h | 22 +- lib/librte_eal/common/include/rte_memory.h | 33 ++- lib/librte_eal/common/include/rte_memzone.h | 1 - lib/librte_eal/common/malloc_elem.c | 8 +- lib/librte_eal/common/malloc_elem.h | 6 +- lib/librte_eal/common/malloc_heap.c | 92 +++++-- lib/librte_eal/common/rte_malloc.c | 22 +- lib/librte_eal/linuxapp/eal/eal.c | 21 +- lib/librte_eal/linuxapp/eal/eal_memory.c | 297 +++++++++++++------- lib/librte_eal/linuxapp/eal/eal_vfio.c | 164 +++++++---- lib/librte_eal/rte_eal_version.map | 3 +- test/test/test_malloc.c | 29 +- test/test/test_memory.c | 43 ++- test/test/test_memzone.c | 17 +- 21 files changed, 917 insertions(+), 331 deletions(-)
config/common_base +14 -1
@@ -61,7 +61,20 @@ CONFIG_RTE_CACHE_LINE_SIZE=64 CONFIG_RTE_LIBRTE_EAL=y CONFIG_RTE_MAX_LCORE=128 CONFIG_RTE_MAX_NUMA_NODES=8 -CONFIG_RTE_MAX_MEMSEG=256 +CONFIG_RTE_MAX_MEMSEG_LISTS=32 +# each memseg list will be limited to either RTE_MAX_MEMSEG_PER_LIST pages +# or RTE_MAX_MEM_PER_LIST gigabytes worth of memory, whichever is the smallest +CONFIG_RTE_MAX_MEMSEG_PER_LIST=8192 +CONFIG_RTE_MAX_MEM_PER_LIST=32 +# a "type" is a combination of page size and NUMA node. total number of memseg +# lists per type will be limited to either RTE_MAX_MEMSEG_PER_TYPE pages (split +# over multiple lists of RTE_MAX_MEMSEG_PER_LIST pages), or RTE_MAX_MEM_PER_TYPE +# gigabytes of memory (split over multiple lists of RTE_MAX_MEM_PER_LIST), +# whichever is the smallest +CONFIG_RTE_MAX_MEMSEG_PER_TYPE=32768 +CONFIG_RTE_MAX_MEM_PER_TYPE=128 +# legacy mem mode only +CONFIG_RTE_MAX_LEGACY_MEMSEG=256 CONFIG_RTE_MAX_MEMZONE=2560 CONFIG_RTE_MAX_TAILQ=32 CONFIG_RTE_ENABLE_ASSERT=n
drivers/bus/pci/linux/pci.c +20 -9
@@ -119,19 +119,30 @@ rte_pci_unmap_device(struct rte_pci_device *dev)void *pci_find_max_end_va (void ) { -const struct rte_memseg *seg =rte_eal_get_physmem_layout (); -const struct rte_memseg *last = seg; -unsigned i =0 ; +void *cur_end, *max_end =NULL ; +int i =0 ; -for (i =0 ; i <RTE_MAX_MEMSEG ; i++, seg++) { -if (seg->addr ==NULL ) -break ; +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config; +struct rte_memseg_list *msl = &mcfg->memsegs[i]; +struct rte_fbarray *arr = &msl->memseg_arr; -if (seg->addr > last->addr) - last = seg; +if (arr->count ==0 ) +continue ; +/* +* we need to handle legacy mem case, so don't rely on page size +* to calculate max VA end +*/ +while ((i =rte_fbarray_find_next_used (arr, i)) >=0 ) { +struct rte_memseg *ms =rte_fbarray_get (arr, i); + cur_end =RTE_PTR_ADD (ms->addr, ms->len); +if (cur_end > max_end) + max_end = cur_end; + } } -return RTE_PTR_ADD (last->addr, last->len); +return max_end; }/* parse one line of the "resource" sysfs file (note that the 'line'
drivers/net/virtio/virtio_user/vhost_kernel.c +66 -42
132 lines not on this page · raw message
lib/librte_eal/common/eal_common_memory.c +291 -31
372 lines not on this page · raw message
lib/librte_eal/common/eal_common_memzone.c +2 -10
@@ -226,10 +226,9 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, mz->iova =rte_malloc_virt2iova (mz_addr); mz->addr = mz_addr; mz->len = (requested_len ==0 ? elem->size : requested_len); - mz->hugepage_sz = elem->ms->hugepage_sz; - mz->socket_id = elem->ms->socket_id; + mz->hugepage_sz = elem->msl->hugepage_sz; + mz->socket_id = elem->msl->socket_id; mz->flags =0 ; - mz->memseg_id = elem->ms -rte_eal_get_configuration ()->mem_config->memseg;return mz; }
@@ -382,7 +381,6 @@ intrte_eal_memzone_init (void) {struct rte_mem_config *mcfg; -const struct rte_memseg *memseg;/* get pointer to global configuration */ mcfg =rte_eal_get_configuration ()->mem_config;
@@ -391,12 +389,6 @@ rte_eal_memzone_init(void)if (rte_eal_process_type () ==RTE_PROC_SECONDARY )return 0 ; - memseg =rte_eal_get_physmem_layout (); -if (memseg ==NULL ) { -RTE_LOG (ERR ,EAL ,"%s(): Cannot get physical layout\n" , __func__); -return -1 ; - } -rte_rwlock_write_lock (&mcfg->mlock);/* delete all zones */
lib/librte_eal/common/eal_hugepages.h +2 -0
@@ -23,6 +23,8 @@ struct hugepage_file {int socket_id;/**< NUMA socket ID */ int file_id;/**< the '%d' in HUGEFILE_FMT */ int memseg_id;/**< the memory segment to which page belongs */ +int memseg_list_id; +/**< the memory segment list to which page belongs */ char filepath[MAX_HUGEPAGE_PATH ];/**< path to backing file on filesystem */ };
lib/librte_eal/common/eal_internal_cfg.h +1 -1
@@ -23,7 +23,7 @@ struct hugepage_info {uint64_t hugepage_sz;/**< size of a huge page */ const char *hugedir;/**< dir where hugetlbfs is mounted */ uint32_t num_pages[RTE_MAX_NUMA_NODES ]; -/**< number of hugepages of that size on each socket */ +/**< number of hugepages of that size on each socket */ int lock_descriptor;/**< file descriptor for hugepage dir */ };
lib/librte_eal/common/include/rte_eal_memconfig.h +21 -1
@@ -12,12 +12,30 @@#include <rte_malloc_heap.h> #include <rte_rwlock.h> #include <rte_pause.h> +#include <rte_fbarray.h> #ifdef __cplusplus extern "C" { #endif /** + * memseg list is a special case as we need to store a bunch of other data + * together with the array itself. + */ +struct rte_memseg_list { + RTE_STD_C11 + union { + void *base_va; + /**< Base virtual address for this memseg list. */ + uint64_t addr_64; + /**< Makes sure addr is always 64-bits */ + }; + int socket_id; /**< Socket ID for all memsegs in this list. */ + uint64_t hugepage_sz; /**< page size for all memsegs in this list. */ + struct rte_fbarray memseg_arr; +}; + +/** * the structure for the memory configuration for the RTE. * Used by the rte_config structure. It is separated out, as for multi-process * support, the memory details should be shared across instances
@@ -43,9 +61,11 @@ struct rte_mem_config {uint32_t memzone_cnt;/**< Number of allocated memzones */ /* memory segments and zones */ -struct rte_memseg memseg[RTE_MAX_MEMSEG ];/**< Physmem descriptors. */ struct rte_memzone memzone[RTE_MAX_MEMZONE ];/**< Memzone descriptors. */ +struct rte_memseg_list memsegs[RTE_MAX_MEMSEG_LISTS ]; +/**< list of dynamic arrays holding memsegs */ +struct rte_tailq_head tailq_head[RTE_MAX_TAILQ ];/**< Tailqs for objects */ /* Heaps of Malloc per socket */
lib/librte_eal/common/include/rte_memory.h +21 -12
@@ -22,6 +22,9 @@ extern "C" {#include <rte_common.h> #include <rte_config.h> +/* forward declaration for pointers */ +struct rte_memseg_list ; + __extension__ enum rte_page_sizes { RTE_PGSIZE_4K = 1ULL << 12,
@@ -130,21 +133,27 @@ phys_addr_t rte_mem_virt2phy(const void *virt);rte_iova_t rte_mem_virt2iova (const void *virt);/** -* Get the layout of the available physical memory. +* Get memseg corresponding to virtual memory address. * -* It can be useful for an application to have the full physical -* memory layout to decide the size of a memory zone to reserve. This -* table is stored in rte_config (see rte_eal_get_configuration()). +* @param virt +* The virtual address. +* @param msl +* Memseg list in which to look for memsegs (can be NULL). +* @return +* Memseg to which this virtual address belongs to. +*/ +struct rte_memseg *rte_mem_virt2memseg (const void *virt, +const struct rte_memseg_list *msl); + +/** +* Get memseg list corresponding to virtual memory address. * +* @param virt +* The virtual address. * @return -* - On success, return a pointer to a read-only table of struct -* rte_physmem_desc elements, containing the layout of all -* addressable physical memory. The last element of the table -* contains a NULL address. -* - On error, return NULL. This should not happen since it is a fatal -* error that will probably cause the entire system to panic. -*/ -const struct rte_memseg *rte_eal_get_physmem_layout (void ); +* Memseg list to which this virtual address belongs to. +*/ +struct rte_memseg_list *rte_mem_virt2memseg_list (const void *virt);/** * Dump the physical memory layout to a file.
lib/librte_eal/common/include/rte_memzone.h +0 -1
@@ -66,7 +66,6 @@ struct rte_memzone {int32_t socket_id;/**< NUMA socket ID. */ uint32_t flags;/**< Characteristics of this memzone. */ -uint32_t memseg_id;/**< Memseg it belongs. */ } __attribute__((__packed__));/**
lib/librte_eal/common/malloc_elem.c +4 -4
@@ -26,11 +26,11 @@* Initialize a general malloc_elem header structure */ void -malloc_elem_init (struct malloc_elem *elem, -struct malloc_heap *heap,const struct rte_memseg *ms,size_t size) +malloc_elem_init (struct malloc_elem *elem,struct malloc_heap *heap, +struct rte_memseg_list *msl,size_t size) { elem->heap = heap; - elem->ms = ms; + elem->msl = msl; elem->prev =NULL ; elem->next =NULL ;memset (&elem->free_list,0 ,sizeof (elem->free_list));
@@ -145,7 +145,7 @@ split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)const size_t old_elem_size = (uintptr_t )split_pt - (uintptr_t )elem;const size_t new_elem_size = elem->size - old_elem_size; -malloc_elem_init (split_pt, elem->heap, elem->ms, new_elem_size); +malloc_elem_init (split_pt, elem->heap, elem->msl, new_elem_size); split_pt->prev = elem; split_pt->next = next_elem;if (next_elem)
lib/librte_eal/common/malloc_elem.h +3 -3
@@ -5,7 +5,7 @@#ifndef MALLOC_ELEM_H_ #define MALLOC_ELEM_H_ -#include <rte_memory.h> +#include <rte_eal_memconfig.h> /* dummy definition of struct so we can use pointers to it in malloc_elem struct */ struct malloc_heap ;
@@ -24,7 +24,7 @@ struct malloc_elem {/**< points to next elem in memseg */ LIST_ENTRY (malloc_elem) free_list;/**< list of free elements in heap */ -const struct rte_memseg *ms; +struct rte_memseg_list *msl;volatile enum elem_state state;uint32_t pad;size_t size;
@@ -111,7 +111,7 @@ malloc_elem_from_data(const void *data)void malloc_elem_init (struct malloc_elem *elem,struct malloc_heap *heap, -const struct rte_memseg *ms, +struct rte_memseg_list *msl,size_t size);void
lib/librte_eal/common/malloc_heap.c +73 -19
124 lines not on this page · raw message
lib/librte_eal/common/rte_malloc.c +13 -9
@@ -238,17 +238,21 @@ rte_malloc_set_limit(__rte_unused const char *type,rte_iova_t rte_malloc_virt2iova (const void *addr) { -rte_iova_t iova; -const struct malloc_elem *elem =malloc_elem_from_data (addr); +const struct rte_memseg *ms; +struct malloc_elem *elem =malloc_elem_from_data (addr); +if (elem ==NULL )return RTE_BAD_IOVA ; -if (elem->ms->iova ==RTE_BAD_IOVA ) -return RTE_BAD_IOVA ;if (rte_eal_iova_mode () ==RTE_IOVA_VA ) - iova = (uintptr_t )addr; -else - iova = elem->ms->iova + -RTE_PTR_DIFF (addr, elem->ms->addr); -return iova; +return (uintptr_t ) addr; + + ms =rte_mem_virt2memseg (addr, elem->msl); +if (ms ==NULL ) +return RTE_BAD_IOVA ; + +if (ms->iova ==RTE_BAD_IOVA ) +return RTE_BAD_IOVA ; + +return ms->iova +RTE_PTR_DIFF (addr, ms->addr); }
lib/librte_eal/linuxapp/eal/eal.c +12 -9
@@ -74,8 +74,8 @@ static int mem_cfg_fd = -1;static struct flock wr_lock = { .l_type =F_WRLCK , .l_whence =SEEK_SET , - .l_start = offsetof(struct rte_mem_config , memseg), - .l_len =sizeof (early_mem_config.memseg), + .l_start = offsetof(struct rte_mem_config , memsegs), + .l_len =sizeof (early_mem_config.memsegs), };/* Address of global and public configuration */
@@ -643,17 +643,20 @@ eal_parse_args(int argc, char **argv)static void eal_check_mem_on_local_socket (void ) { -const struct rte_memseg *ms; +const struct rte_memseg_list *msl;int i, socket_id; socket_id =rte_lcore_to_socket_id (rte_config.master_lcore); - ms =rte_eal_get_physmem_layout (); - -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) -if (ms[i].socket_id == socket_id && - ms[i].len >0 ) -return ; +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { + msl = &rte_eal_get_configuration ()->mem_config->memsegs[i]; +if (msl->socket_id != socket_id) +continue ; +/* for legacy memory, check if there's anything allocated */ +if (internal_config.legacy_mem && msl->memseg_arr.count ==0 ) +continue ; +return ; + }RTE_LOG (WARNING ,EAL ,"WARNING: Master core has no " "memory on local socket!\n" );
lib/librte_eal/linuxapp/eal/eal_memory.c +203 -94
385 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/eal_vfio.c +109 -55
208 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -1
@@ -25,7 +25,6 @@ DPDK_2.0 { rte_eal_devargs_type_count; rte_eal_get_configuration; rte_eal_get_lcore_state; - rte_eal_get_physmem_layout; rte_eal_get_physmem_size; rte_eal_has_hugepages; rte_eal_hpet_init;
@@ -215,6 +214,8 @@ DPDK_18.05 { global: rte_num_sockets; + rte_mem_virt2memseg; + rte_mem_virt2memseg_list; rte_malloc_dump_heaps; rte_fbarray_init; rte_fbarray_destroy;
test/test/test_malloc.c +15 -14
@@ -12,6 +12,7 @@#include <rte_common.h> #include <rte_memory.h> +#include <rte_eal_memconfig.h> #include <rte_per_lcore.h> #include <rte_launch.h> #include <rte_eal.h>
@@ -705,15 +706,23 @@ test_malloc_bad_params(void)return -1 ; } -/* Check if memory is available on a specific socket */ +/* Check if memory is avilable on a specific socket */ static int is_mem_on_socket (int32_t socket) { -const struct rte_memseg *ms =rte_eal_get_physmem_layout (); +const struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config;unsigned i; -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) { -if (socket == ms[i].socket_id) +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +const struct rte_memseg_list *msl = + &mcfg->memsegs[i]; +const struct rte_fbarray *arr = &msl->memseg_arr; + +if (msl->socket_id != socket) +continue ; + +if (arr->count)return 1 ; }return 0 ;
@@ -726,16 +735,8 @@ is_mem_on_socket(int32_t socket)static int32_t addr_to_socket (void * addr) { -const struct rte_memseg *ms =rte_eal_get_physmem_layout (); -unsigned i; - -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) { -if ((ms[i].addr <= addr) && - ((uintptr_t )addr < - ((uintptr_t )ms[i].addr + (uintptr_t )ms[i].len))) -return ms[i].socket_id; - } -return -1 ; +const struct rte_memseg *ms =rte_mem_virt2memseg (addr,NULL ); +return ms ==NULL ?-1 : ms->socket_id; }/* Test using rte_[c|m|zm]alloc_socket() on a specific socket */
test/test/test_memory.c +35 -8
73 lines not on this page · raw message
test/test/test_memzone.c +10 -7
@@ -108,22 +108,25 @@ static inttest_memzone_reserve_flags (void) {const struct rte_memzone *mz; -const struct rte_memseg *ms;int hugepage_2MB_avail =0 ;int hugepage_1GB_avail =0 ;int hugepage_16MB_avail =0 ;int hugepage_16GB_avail =0 ;const size_t size =100 ;int i =0 ; - ms =rte_eal_get_physmem_layout (); -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) { -if (ms[i].hugepage_sz ==RTE_PGSIZE_2M ) + +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config; +struct rte_memseg_list *msl = &mcfg->memsegs[i]; + +if (msl->hugepage_sz ==RTE_PGSIZE_2M ) hugepage_2MB_avail =1 ; -if (ms[i].hugepage_sz ==RTE_PGSIZE_1G ) +if (msl->hugepage_sz ==RTE_PGSIZE_1G ) hugepage_1GB_avail =1 ; -if (ms[i].hugepage_sz ==RTE_PGSIZE_16M ) +if (msl->hugepage_sz ==RTE_PGSIZE_16M ) hugepage_16MB_avail =1 ; -if (ms[i].hugepage_sz ==RTE_PGSIZE_16G ) +if (msl->hugepage_sz ==RTE_PGSIZE_16G ) hugepage_16GB_avail =1 ; }/* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
Signature · 1 line
2.7.4
Re: [PATCH 13/41] eal: replace memseg with memseg lists
On Sat, Mar 03, 2018 at 01:46:01PM +0000, Anatoly Burakov wrote:
[...]
Quoted 11 lines
config/common_base +4
-1@@ -61,7 +61,20 @@ CONFIG_RTE_CACHE_LINE_SIZE=64 CONFIG_RTE_LIBRTE_EAL=y CONFIG_RTE_MAX_LCORE=128 CONFIG_RTE_MAX_NUMA_NODES=8 -CONFIG_RTE_MAX_MEMSEG=256 +CONFIG_RTE_MAX_MEMSEG_LISTS=32 +# each memseg list will be limited to either RTE_MAX_MEMSEG_PER_LIST pages +# or RTE_MAX_MEM_PER_LIST gigabytes worth of memory, whichever is the smallest +CONFIG_RTE_MAX_MEMSEG_PER_LIST=8192
config/common_base CONFIG_RTE_CACHE_LINE_SIZE=64 · line 77
Quoted 10 lines
config/common_base +10
-0+CONFIG_RTE_MAX_MEM_PER_LIST=32 +# a "type" is a combination of page size and NUMA node. total number of memseg +# lists per type will be limited to either RTE_MAX_MEMSEG_PER_TYPE pages (split +# over multiple lists of RTE_MAX_MEMSEG_PER_LIST pages), or RTE_MAX_MEM_PER_TYPE +# gigabytes of memory (split over multiple lists of RTE_MAX_MEM_PER_LIST), +# whichever is the smallest +CONFIG_RTE_MAX_MEMSEG_PER_TYPE=32768 +CONFIG_RTE_MAX_MEM_PER_TYPE=128 +# legacy mem mode only +CONFIG_RTE_MAX_LEGACY_MEMSEG=256
Would it be possible to suffix CONFIG_RTE_MAX_MEM_PER_LIST and CONFIG_RTE_MAX_MEM_PER_TYPE with _GB? It's not that obvious that is it gigabytes.
What is the impact of changing one of these values on the ABI? And what would be the impact on performance? The underlying question is: shall we increase these values to avoid changing them later?
Re: [PATCH 13/41] eal: replace memseg with memseg lists
Quoted 21 lines · Olivier Matz
On Sat, Mar 03, 2018 at 01:46:01PM +0000, Anatoly Burakov wrote:
[...]
Quoted 17 lines
config/common_base +10
-1@@ -61,7 +61,20 @@ CONFIG_RTE_CACHE_LINE_SIZE=64 CONFIG_RTE_LIBRTE_EAL=y CONFIG_RTE_MAX_LCORE=128 CONFIG_RTE_MAX_NUMA_NODES=8 -CONFIG_RTE_MAX_MEMSEG=256 +CONFIG_RTE_MAX_MEMSEG_LISTS=32 +# each memseg list will be limited to either RTE_MAX_MEMSEG_PER_LIST pages +# or RTE_MAX_MEM_PER_LIST gigabytes worth of memory, whichever is the smallest +CONFIG_RTE_MAX_MEMSEG_PER_LIST=8192 +CONFIG_RTE_MAX_MEM_PER_LIST=32 +# a "type" is a combination of page size and NUMA node. total number of memseg +# lists per type will be limited to either RTE_MAX_MEMSEG_PER_TYPE pages (split +# over multiple lists of RTE_MAX_MEMSEG_PER_LIST pages), or RTE_MAX_MEM_PER_TYPE +# gigabytes of memory (split over multiple lists of RTE_MAX_MEM_PER_LIST), +# whichever is the smallest
Quoted 8 lines
config/common_base CONFIG_RTE_CACHE_LINE_SIZE=64 · line 77
Quoted 4 lines
config/common_base +4
-0+CONFIG_RTE_MAX_MEMSEG_PER_TYPE=32768 +CONFIG_RTE_MAX_MEM_PER_TYPE=128 +# legacy mem mode only +CONFIG_RTE_MAX_LEGACY_MEMSEG=256Would it be possible to suffix
CONFIG_RTE_MAX_MEM_PER_LISTandCONFIG_RTE_MAX_MEM_PER_TYPEwith_GB? It's not that obvious that is it gigabytes.
Sure, will add this.
Quoted 2 lines
What is the impact of changing one of these values on the ABI?
Some of them will change the ABI, some won't. MAX_MEMSEG_LISTS will change the ABI because it's in the rte_eal_memconfig, but other values are not and are only used during init (and LEGACY_MEMSEG is already removed in GitHub code).
Quoted 1 line
And what would be the impact on performance?
Depending on what you mean by performance. Generally, no impact on performance will be noticeable because we're not really doing anything differently - a page is a page, no matter how or when it is mapped. These changes might also speed up some lookup operations on memseg lists themselves.
Quoted 2 lines
The underlying question is: shall we increase these values to avoid changing them later?
I do plan to increase the MAX_MEMSEG_LISTS value to at least 64.
Signature · 2 lines
Thanks, Anatoly
PATCH 14/41 eal: add support for mapping hugepages at runtime
In reply to Anatoly BurakovNothing uses this code yet. The bulk of it is copied from old memory allocation code (linuxapp eal_memory.c). We provide an EAL-internal API to allocate either one page or multiple pages, guaranteeing that we'll get contiguous VA for all of the pages that we requested.
For single-file segments, we will use fallocate() to grow and shrink memory segments, however fallocate() is not supported on all kernel versions, so we will fall back to using ftruncate() to grow the file, and disable shrinking as there's little we can do there. This will enable vhost use cases where having single file segments is of great value even without support for hot-unplugging memory.
Not supported on FreeBSD.
Locking is done via fcntl() because that way, when it comes to taking out write locks or unlocking on deallocation, we don't have to keep original fd's around. Plus, using fcntl() gives us ability to lock parts of a file, which is useful for single-file segments.
--- lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/bsdapp/eal/eal_memalloc.c | 26 ++ lib/librte_eal/bsdapp/eal/meson.build | 1 + lib/librte_eal/common/eal_memalloc.h | 19 + lib/librte_eal/linuxapp/eal/Makefile | 2 + lib/librte_eal/linuxapp/eal/eal_memalloc.c | 609 +++++++++++++++++++++++++++++ lib/librte_eal/linuxapp/eal/meson.build | 1 + 7 files changed, 659 insertions(+) create mode 100644 lib/librte_eal/bsdapp/eal/eal_memalloc.c create mode 100644 lib/librte_eal/common/eal_memalloc.h create mode 100644 lib/librte_eal/linuxapp/eal/eal_memalloc.c
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -29,6 +29,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_memory.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_hugepage_info.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_debug.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_lcore.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_interrupts.c
lib/librte_eal/bsdapp/eal/eal_memalloc.c new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2017-2018 Intel Corporation +*/ + +#include <inttypes.h> + +#include <rte_log.h> +#include <rte_memory.h> + +#include "eal_memalloc.h" + +int +eal_memalloc_alloc_page_bulk(struct rte_memseg **ms __rte_unused, + int __rte_unused n, uint64_t __rte_unused size, + int __rte_unused socket, bool __rte_unused exact) +{ +RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" ); +return -1 ; +} + +struct rte_memseg * +eal_memalloc_alloc_page(uint64_t __rte_unused size, int __rte_unused socket) +{ +RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" ); +return NULL ; +}
lib/librte_eal/bsdapp/eal/meson.build +1 -0
@@ -8,6 +8,7 @@ env_sources = files('eal_alarm.c', 'eal_hugepage_info.c', 'eal_interrupts.c', 'eal_lcore.c', + 'eal_memalloc.c', 'eal_thread.c', 'eal_timer.c', 'eal.c',
lib/librte_eal/common/eal_memalloc.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2017-2018 Intel Corporation +*/ + +#ifndef EAL_MEMALLOC_H +#define EAL_MEMALLOC_H + +#include <stdbool.h> + +#include <rte_memory.h> + +struct rte_memseg * +eal_memalloc_alloc_page (uint64_t size,int socket); + +int +eal_memalloc_alloc_page_bulk (struct rte_memseg **ms,int n,uint64_t size, +int socket,bool exact); + +#endif // EAL_MEMALLOC_H
lib/librte_eal/linuxapp/eal/Makefile +2 -0
@@ -36,6 +36,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_log.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_vfio.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_vfio_mp_sync.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_debug.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_lcore.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c
@@ -82,6 +83,7 @@ CFLAGS_eal_interrupts.o := -D_GNU_SOURCE CFLAGS_eal_vfio_mp_sync.o := -D_GNU_SOURCE CFLAGS_eal_timer.o := -D_GNU_SOURCE CFLAGS_eal_lcore.o := -D_GNU_SOURCE +CFLAGS_eal_memalloc.o := -D_GNU_SOURCE CFLAGS_eal_thread.o := -D_GNU_SOURCE CFLAGS_eal_log.o := -D_GNU_SOURCE CFLAGS_eal_common_log.o := -D_GNU_SOURCE
lib/librte_eal/linuxapp/eal/eal_memalloc.c new file +609 -0
609 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/meson.build +1 -0
@@ -10,6 +10,7 @@ env_sources = files('eal_alarm.c', 'eal_debug.c', 'eal_hugepage_info.c', 'eal_interrupts.c', + 'eal_memalloc.c', 'eal_lcore.c', 'eal_log.c', 'eal_thread.c',
Signature · 1 line
2.7.4
Re: [PATCH 14/41] eal: add support for mapping hugepages at runtime
Quoted 16 lines · Anatoly Burakov
Nothing uses this code yet. The bulk of it is copied from old memory allocation code (linuxapp
eal_memory.c). We provide an EAL-internal API to allocate either one page or multiple pages, guaranteeing that we'll get contiguous VA for all of the pages that we requested.For single-file segments, we will use
fallocate()to grow and shrink memory segments, howeverfallocate()is not supported on all kernel versions, so we will fall back to usingftruncate()to grow the file, and disable shrinking as there's little we can do there. This will enable vhost use cases where having single file segments is of great value even without support for hot-unplugging memory.Not supported on FreeBSD.
Quoted 7 lines
Locking is done via
fcntl()because that way, when it comes to taking out write locks or unlocking on deallocation, we don't have to keep original fd's around. Plus, usingfcntl()gives us ability to lock parts of a file, which is useful for single-file segments.Signed-off-by Anatoly Burakov <[email protected]>
Few minor typos:
[...]
lib/librte_eal/linuxapp/eal/eal_memalloc.c line 140
Quoted 2 lines
lib/librte_eal/linuxapp/eal/eal_memalloc.c new file +2
-0+static void +resotre_numa (int *oldpolicy,struct bitmask *oldmask)
restore
[...]
lib/librte_eal/linuxapp/eal/eal_memalloc.c line 189
Quoted 2 lines
lib/librte_eal/linuxapp/eal/eal_memalloc.c new file +2
-0+static off_t +getFileSize (int fd)
should it be get_file_size()?
[...]
Quoted 27 lines
lib/librte_eal/linuxapp/eal/eal_memalloc.c new file +27
-0+static int +alloc_page (struct rte_memseg *ms,void *addr,uint64_t size,int socket_id, +struct hugepage_info *hi,unsigned int list_idx, +unsigned int seg_idx) +{ +int cur_socket_id =0 ; +uint64_t map_offset; +char path[PATH_MAX ]; +int ret =0 ; +int fd; + + fd =get_page_fd (path,sizeof (path), hi, list_idx, seg_idx); +if (fd <0 ) +return -1 ; + + +if (internal_config.single_file_segments) { + map_offset = seg_idx * size; + ret =resize_hugefile (fd, map_offset, size, true); +if (ret <1 ) + goto resized; + }else { + map_offset =0 ; +if (ftruncate (fd, size) <0 ) { +RTE_LOG (DEBUG ,EAL ,"%s(): ftruncate() failed: %s\n" , + __func__,strerror (errno)); + goto resized;
lib/librte_eal/linuxapp/eal/eal_memalloc.c line 403
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_memalloc.c new file +10
-0+ } +/* we've allocated a page - take out a read lock. we're using +* fcntl() locks rather than flock() here because doing that +* gives us one huge advantage - fcntl() locks are per-process, +* not per-file descriptor, which means that we don't have to +* keep the original fd's around to keep a lock on the file. +* +* this is useful, because when it comes to unmapping pages, we +* will have to take out a write lock (to figure out if another +* process still has this page mapped), and to do itwith flock()
typo: itwith
PATCH 15/41 eal: add support for unmapping pages at runtime
In reply to Anatoly BurakovThis isn't used anywhere yet, but the support is now there. Also, adding cleanup to allocation procedures, so that if we fail to allocate everything we asked for, we can free all of it back.
--- lib/librte_eal/common/eal_memalloc.h | 3 + lib/librte_eal/linuxapp/eal/eal_memalloc.c | 148 ++++++++++++++++++++++++++++- 2 files changed, 146 insertions(+), 5 deletions(-)
lib/librte_eal/common/eal_memalloc.h +3 -0
@@ -16,4 +16,7 @@ int eal_memalloc_alloc_page_bulk(struct rte_memseg **ms, int n, uint64_t size, int socket, bool exact); +int +eal_memalloc_free_page (struct rte_memseg *ms); +#endif // EAL_MEMALLOC_H
lib/librte_eal/linuxapp/eal/eal_memalloc.c +143 -5
190 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 16/41 eal: make use of memory hotplug for init
In reply to Anatoly BurakovAdd a new (non-legacy) memory init path for EAL. It uses the new memory hotplug facilities, although it's only being run at startup.
If no -m or --socket-mem switches were specified, the new init will not allocate anything, whereas if those switches were passed, appropriate amounts of pages would be requested, just like for legacy init.
Since rte_malloc support for dynamic allocation comes in later patches, running DPDK without --socket-mem or -m switches will fail in this patch.
Also, allocated pages will be physically discontiguous (or rather, they're not guaranteed to be physically contiguous - they may still be, by accident) unless IOVA_AS_VA mode is used.
Since memory hotplug subsystem relies on partial file locking, replace flock() locks with fcntl() locks.
---
Notes:
This commit shows "the wolrd as it could have been". All of this other monstrous amount of code in eal_memory.c is there because of legacy init option. Do we *really* want to keep it around, and make DPDK init and memory system suffer from split personality?
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 25 ++++++++- lib/librte_eal/linuxapp/eal/eal_memory.c | 74 +++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 7 deletions(-)
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +23 -2
@@ -15,6 +15,7 @@#include <unistd.h> #include <errno.h> #include <sys/queue.h> +#include <sys/stat.h> #include <rte_memory.h> #include <rte_eal.h>
@@ -200,6 +201,18 @@ get_hugepage_dir(uint64_t hugepage_sz) }/* +* uses fstat to report the size of a file on disk +*/ +static off_t +getFileSize (int fd) +{ +struct stat st; +if (fstat (fd, &st) <0 ) +return 0 ; +return st.st_size; +} + +/* * Clear the hugepage directory of whatever hugepage files * there are. Checks if the file is locked (i.e. * if it's in use by another DPDK process).
@@ -229,6 +242,8 @@ clear_hugedir(const char * hugedir) }while (dirent !=NULL ){ +struct flock lck = {0 }; +/* skip files that don't match the hugepage pattern */ if (fnmatch (filter, dirent->d_name,0 ) >0 ) { dirent =readdir (dir);
@@ -245,11 +260,17 @@ clear_hugedir(const char * hugedir) }/* non-blocking lock */ - lck_result =flock (fd,LOCK_EX |LOCK_NB ); + lck.l_type =F_RDLCK ; + lck.l_whence =SEEK_SET ; + lck.l_start =0 ; + lck.l_len =getFileSize (fd); + + lck_result =fcntl (fd,F_SETLK , &lck);/* if lock succeeds, unlock and remove the file */ if (lck_result !=-1 ) { -flock (fd,LOCK_UN ); + lck.l_type =F_UNLCK ; +fcntl (fd,F_SETLK , &lck);unlinkat (dir_fd, dirent->d_name,0 ); }close (fd);
lib/librte_eal/linuxapp/eal/eal_memory.c +69 -5
110 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 17/41 eal: enable memory hotplug support in rte_malloc
In reply to Anatoly BurakovThis set of changes enables rte_malloc to allocate and free memory as needed. The way it works is, first malloc checks if there is enough memory already allocated to satisfy user's request. If there isn't, we try and allocate more memory. The reverse happens with free - we free an element, check its size (including free element merging due to adjacency) and see if it's bigger than hugepage size and that its start and end span a hugepage or more. Then we remove the area from malloc heap (adjusting element lengths where appropriate), and deallocate the page.
For legacy mode, runtime alloc/free of pages is disabled.
It is worth noting that memseg lists are being sorted by page size, and that we try our best to satisfy user's request. That is, if the user requests an element from a 2MB page memory, we will check if we can satisfy that request from existing memory, if not we try and allocate more 2MB pages. If that fails and user also specified a "size is hint" flag, we then check other page sizes and try to allocate from there. If that fails too, then, depending on flags, we may try allocating from other sockets. In other words, we try our best to give the user what they asked for, but going to other sockets is last resort - first we try to allocate more memory on the same socket.
--- lib/librte_eal/common/eal_common_memzone.c | 23 +- lib/librte_eal/common/malloc_elem.c | 85 ++++++++ lib/librte_eal/common/malloc_elem.h | 3 + lib/librte_eal/common/malloc_heap.c | 332 ++++++++++++++++++++++++++++- lib/librte_eal/common/malloc_heap.h | 4 +- lib/librte_eal/common/rte_malloc.c | 31 +-- 6 files changed, 416 insertions(+), 62 deletions(-)
lib/librte_eal/common/eal_common_memzone.c +2 -21
@@ -103,7 +103,6 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,struct rte_memzone *mz;struct rte_mem_config *mcfg;size_t requested_len; -int socket, i;/* get pointer to global configuration */ mcfg =rte_eal_get_configuration ()->mem_config;
@@ -181,27 +180,9 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, } } -if (socket_id ==SOCKET_ID_ANY ) - socket =malloc_get_numa_socket (); -else - socket = socket_id; -/* allocate memory on heap */ -void *mz_addr =malloc_heap_alloc (&mcfg->malloc_heaps[socket],NULL , - requested_len, flags, align, bound); - -if ((mz_addr ==NULL ) && (socket_id ==SOCKET_ID_ANY )) { -/* try other heaps */ -for (i =0 ; i <RTE_MAX_NUMA_NODES ; i++) { -if (socket == i) -continue ; - - mz_addr =malloc_heap_alloc (&mcfg->malloc_heaps[i], -NULL , requested_len, flags, align, bound); -if (mz_addr !=NULL ) -break ; - } - } +void *mz_addr =malloc_heap_alloc (NULL , requested_len, socket_id, flags, + align, bound);if (mz_addr ==NULL ) { rte_errno =ENOMEM ;
lib/librte_eal/common/malloc_elem.c +85 -0
91 lines not on this page · raw message
lib/librte_eal/common/malloc_elem.h +3 -0
@@ -152,6 +152,9 @@ int malloc_elem_resize(struct malloc_elem *elem, size_t size);void +malloc_elem_hide_region (struct malloc_elem *elem,void *start,size_t len); + +void malloc_elem_free_list_remove (struct malloc_elem *elem);/*
lib/librte_eal/common/malloc_heap.c +321 -11
377 lines not on this page · raw message
lib/librte_eal/common/malloc_heap.h +2 -2
@@ -24,8 +24,8 @@ malloc_get_numa_socket(void) }void * -malloc_heap_alloc (struct malloc_heap *heap,const char *type,size_t size, -unsigned flags,size_t align,size_t bound); +malloc_heap_alloc (const char *type,size_t size,int socket,unsigned int flags, +size_t align,size_t bound);int malloc_heap_free (struct malloc_elem *elem);
lib/librte_eal/common/rte_malloc.c +3 -28
@@ -39,10 +39,6 @@ void rte_free(void *addr)void *rte_malloc_socket (const char *type,size_t size,unsigned align,int socket_arg) { -struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config; -int socket, i; -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 ;
@@ -50,33 +46,12 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket_arg)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 ) +if (socket_arg >=RTE_MAX_NUMA_NODES )return NULL ; - ret =malloc_heap_alloc (&mcfg->malloc_heaps[socket], type, - size,0 , align ==0 ?1 : align,0 ); -if (ret !=NULL || socket_arg !=SOCKET_ID_ANY ) -return ret; - -/* try other heaps */ -for (i =0 ; i <RTE_MAX_NUMA_NODES ; i++) { -/* we already tried this one */ -if (i == socket) -continue ; - - ret =malloc_heap_alloc (&mcfg->malloc_heaps[i], type, - size,0 , align ==0 ?1 : align,0 ); -if (ret !=NULL ) -return ret; - } - -return NULL ; +return malloc_heap_alloc (type, size, socket_arg,0 , + align ==0 ?1 : align,0 ); }/*
Signature · 1 line
2.7.4
Re: [PATCH 17/41] eal: enable memory hotplug support in rte_malloc
Quoted 25 lines · Anatoly Burakov
This set of changes enables
rte_mallocto allocate and free memory as needed. The way it works is, first malloc checks if there is enough memory already allocated to satisfy user's request. If there isn't, we try and allocate more memory. The reverse happens with free - we free an element, check its size (including free element merging due to adjacency) and see if it's bigger than hugepage size and that its start and end span a hugepage or more. Then we remove the area from malloc heap (adjusting element lengths where appropriate), and deallocate the page.For legacy mode, runtime alloc/free of pages is disabled.
It is worth noting that memseg lists are being sorted by page size, and that we try our best to satisfy user's request. That is, if the user requests an element from a 2MB page memory, we will check if we can satisfy that request from existing memory, if not we try and allocate more 2MB pages. If that fails and user also specified a "size is hint" flag, we then check other page sizes and try to allocate from there. If that fails too, then, depending on flags, we may try allocating from other sockets. In other words, we try our best to give the user what they asked for, but going to other sockets is last resort - first we try to allocate more memory on the same socket.
Signed-off-by Anatoly Burakov <[email protected]>
[...]
Quoted 20 lines
lib/librte_eal/common/malloc_heap.c +3
-6@@ -123,48 +125,356 @@ find_suitable_element(struct malloc_heap *heap, size_t size,* 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 flags, -size_t align,size_t bound) +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) {struct malloc_elem *elem; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align); -rte_spinlock_lock (&heap->lock); - elem =find_suitable_element (heap, size, flags, align, bound);
lib/librte_eal/common/malloc_heap.c find_suitable_element(struct malloc_heap *heap, size_t size, · line 146
Quoted 10 lines
lib/librte_eal/common/malloc_heap.c +1
-1if (elem !=NULL ) { elem =malloc_elem_alloc (elem, size, align, bound); +/* increase heap's count of allocated elements */ heap->alloc_count++; } -rte_spinlock_unlock (&heap->lock);return elem ==NULL ?NULL : (void *)(&elem[1 ]); }
The comment on top of the function says "after releasing the lock" but it seems it's not relevant anymore because the lock is removed.
[...]
Quoted 16 lines
lib/librte_eal/common/malloc_heap.c +5
-1int malloc_heap_free (struct malloc_elem *elem) {struct malloc_heap *heap; -struct malloc_elem *ret; +void *start, *aligned_start, *end, *aligned_end; +size_t len, aligned_len; +struct rte_memseg_list *msl; +int n_pages, page_idx, max_page_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;
lib/librte_eal/common/malloc_heap.c find_suitable_element(struct malloc_heap *heap, size_t size, · line 434
Quoted 10 lines
lib/librte_eal/common/malloc_heap.c +4
-2rte_spinlock_lock (&(heap->lock)); - ret =malloc_elem_free (elem); + elem =malloc_elem_free (elem); -rte_spinlock_unlock (&(heap->lock)); +/* anything after this is a bonus */ + ret =0 ; +
The fact that there was previously 2 rte_spinlock_unlock() calls looks strange to me. Is there something wrong in a previous patch?
PATCH 18/41 test: fix malloc autotest to support memory hotplug
In reply to Anatoly BurakovThe test was expecting memory already being allocated on all sockets, and thus was failing because calling rte_malloc could trigger memory hotplug event and allocate memory where there was none before.
Fix it to instead report availability of memory on specific sockets by attempting to allocate a page and see if that succeeds. Technically, this can still cause failure as memory might not be available at the time of check, but become available by the time the test is run, but this is a corner case not worth considering.
--- test/test/test_malloc.c | 52 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-)
test/test/test_malloc.c +44 -8
@@ -22,6 +22,8 @@#include <rte_random.h> #include <rte_string_fns.h> +#include "../../lib/librte_eal/common/eal_memalloc.h" +#include "test.h" #define N 10000
@@ -708,22 +710,56 @@ test_malloc_bad_params(void)/* Check if memory is avilable on a specific socket */ static int -is_mem_on_socket (int32_t socket) +is_mem_on_socket (unsigned int socket) { +struct rte_malloc_socket_stats stats;const struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config; -unsigned i; +uint64_t prev_pgsz; +unsigned int i; + +/* we cannot know if there's memory on a specific socket, since it might +* be available, but not yet allocated. so, in addition to checking +* already mapped memory, we will attempt to allocate a page from that +* socket and see if it works. +*/ +if (socket >=rte_num_sockets ()) +return 0 ; +rte_malloc_get_socket_stats (socket, &stats); + +/* if heap has memory allocated, stop */ +if (stats.heap_totalsz_bytes >0 ) +return 1 ; + +/* to allocate a page, we will have to know its size, so go through all +* supported page sizes and try with each one. +*/ + prev_pgsz =0 ;for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { -const struct rte_memseg_list *msl = - &mcfg->memsegs[i]; -const struct rte_fbarray *arr = &msl->memseg_arr; +const struct rte_memseg_list *msl = &mcfg->memsegs[i]; +uint64_t page_sz; -if (msl->socket_id != socket) +/* skip unused memseg lists */ +if (msl->memseg_arr.len ==0 )continue ; + page_sz = msl->hugepage_sz; -if (arr->count) -return 1 ; +/* skip page sizes we've tried already */ +if (prev_pgsz == page_sz) +continue ; + + prev_pgsz = page_sz; + +struct rte_memseg *ms =eal_memalloc_alloc_page (page_sz, + socket); + +if (ms ==NULL ) +continue ; + +eal_memalloc_free_page (ms); + +return 1 ; }return 0 ; }
Signature · 1 line
2.7.4
Re: [PATCH 18/41] test: fix malloc autotest to support memory hotplug
Quoted 20 lines · Anatoly Burakov
The test was expecting memory already being allocated on all sockets, and thus was failing because calling
rte_malloccould trigger memory hotplug event and allocate memory where there was none before.Fix it to instead report availability of memory on specific sockets by attempting to allocate a page and see if that succeeds. Technically, this can still cause failure as memory might not be available at the time of check, but become available by the time the test is run, but this is a corner case not worth considering.
Signed-off-by Anatoly Burakov <[email protected]>--- test/test/test_malloc.c | 52 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-)test/test/test_malloc.c +0
-0
test/test/test_malloc.c line 26
Quoted 5 lines
test/test/test_malloc.c +2
-0@@ -22,6 +22,8 @@#include <rte_random.h> #include <rte_string_fns.h> +#include "../../lib/librte_eal/common/eal_memalloc.h" +
I guess there is no way to test without importing a private EAL function, correct? If yes, maybe it deserves a quick explanation.
PATCH 19/41 eal: add API to check if memory is contiguous
In reply to Anatoly BurakovThis will be helpful down the line when we implement support for allocating physically contiguous memory. We can no longer guarantee physically contiguous memory unless we're in IOVA_AS_VA mode, but we can certainly try and see if we succeed. In addition, this would be useful for e.g. PMD's who may allocate chunks that are smaller than the pagesize, but they must not cross the page boundary, in which case we will be able to accommodate that request.
--- lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/common/eal_common_memalloc.c | 49 +++++++++++++++++++++++++++++ lib/librte_eal/common/eal_memalloc.h | 5 +++ lib/librte_eal/common/meson.build | 1 + lib/librte_eal/linuxapp/eal/Makefile | 1 + 5 files changed, 57 insertions(+) create mode 100644 lib/librte_eal/common/eal_common_memalloc.c
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -41,6 +41,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_memzone.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_log.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_launch.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_memory.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_tailqs.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_errno.c
lib/librte_eal/common/eal_common_memalloc.c new file +49 -0
49 lines not on this page · raw message
lib/librte_eal/common/eal_memalloc.h +5 -0
@@ -8,6 +8,7 @@#include <stdbool.h> #include <rte_memory.h> +#include <rte_eal_memconfig.h> struct rte_memseg *eal_memalloc_alloc_page (uint64_t size,int socket);
@@ -19,4 +20,8 @@ eal_memalloc_alloc_page_bulk(struct rte_memseg **ms, int n, uint64_t size,int eal_memalloc_free_page (struct rte_memseg *ms); +bool +eal_memalloc_is_contig (struct rte_memseg_list *msl,void *start, +size_t len); +#endif // EAL_MEMALLOC_H
lib/librte_eal/common/meson.build +1 -0
@@ -16,6 +16,7 @@ common_sources = files( 'eal_common_launch.c', 'eal_common_lcore.c', 'eal_common_log.c', + 'eal_common_memalloc.c', 'eal_common_memory.c', 'eal_common_memzone.c', 'eal_common_options.c',
lib/librte_eal/linuxapp/eal/Makefile +1 -0
@@ -49,6 +49,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_memzone.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_log.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_launch.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_memory.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_tailqs.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_errno.c
Signature · 1 line
2.7.4
PATCH 20/41 eal: add backend support for contiguous allocation
In reply to Anatoly BurakovNo major changes, just add some checks in a few key places, and a new parameter to pass around.
--- lib/librte_eal/common/eal_common_memzone.c | 20 +++--- lib/librte_eal/common/malloc_elem.c | 101 ++++++++++++++++++++++------- lib/librte_eal/common/malloc_elem.h | 4 +- lib/librte_eal/common/malloc_heap.c | 57 ++++++++++------ lib/librte_eal/common/malloc_heap.h | 4 +- lib/librte_eal/common/rte_malloc.c | 6 +- 6 files changed, 134 insertions(+), 58 deletions(-)
lib/librte_eal/common/eal_common_memzone.c +11 -9
@@ -98,7 +98,8 @@ find_heap_max_free_elem(int *s, unsigned align)static const struct rte_memzone *memzone_reserve_aligned_thread_unsafe (const char *name,size_t len, -int socket_id,unsigned flags,unsigned align,unsigned bound) +int socket_id,unsigned int flags,unsigned int align, +unsigned int bound,bool contig) {struct rte_memzone *mz;struct rte_mem_config *mcfg;
@@ -182,7 +183,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,/* allocate memory on heap */ void *mz_addr =malloc_heap_alloc (NULL , requested_len, socket_id, flags, - align, bound); + align, bound, contig);if (mz_addr ==NULL ) { rte_errno =ENOMEM ;
@@ -215,9 +216,9 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, }static const struct rte_memzone * -rte_memzone_reserve_thread_safe (const char *name,size_t len, -int socket_id,unsigned flags,unsigned align, -unsigned bound) +rte_memzone_reserve_thread_safe (const char *name,size_t len,int socket_id, +unsigned int flags,unsigned int align,unsigned int bound, +bool contig) {struct rte_mem_config *mcfg;const struct rte_memzone *mz =NULL ;
@@ -228,7 +229,7 @@ rte_memzone_reserve_thread_safe(const char *name, size_t len,rte_rwlock_write_lock (&mcfg->mlock); mz =memzone_reserve_aligned_thread_unsafe ( - name, len, socket_id, flags, align, bound); + name, len, socket_id, flags, align, bound, contig);rte_rwlock_write_unlock (&mcfg->mlock);
@@ -245,7 +246,7 @@ rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id, unsigned flags, unsigned align, unsigned bound) { return rte_memzone_reserve_thread_safe(name, len, socket_id, flags, -align ,bound ); +align ,bound ,false ); }/*
@@ -257,7 +258,7 @@ rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id, unsigned flags, unsigned align) {return rte_memzone_reserve_thread_safe (name, len, socket_id, flags, - align,0 ); + align,0 , false); }/*
@@ -269,7 +270,8 @@ rte_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags) {return rte_memzone_reserve_thread_safe (name, len, socket_id, - flags,RTE_CACHE_LINE_SIZE ,0 ); + flags,RTE_CACHE_LINE_SIZE ,0 , + false); }int
lib/librte_eal/common/malloc_elem.c +79 -22
136 lines not on this page · raw message
lib/librte_eal/common/malloc_elem.h +2 -2
@@ -123,7 +123,7 @@ malloc_elem_insert(struct malloc_elem *elem);*/ int malloc_elem_can_hold (struct malloc_elem *elem,size_t size, -unsigned align,size_t bound); +unsigned int align,size_t bound,bool contig);/* * reserve a block of data in an existing malloc_elem. If the malloc_elem
@@ -131,7 +131,7 @@ malloc_elem_can_hold(struct malloc_elem *elem, size_t size,*/ struct malloc_elem *malloc_elem_alloc (struct malloc_elem *elem,size_t size, -unsigned align,size_t bound); +unsigned int align,size_t bound,bool contig);/* * free a malloc_elem block by adding it to the free list. If the
lib/librte_eal/common/malloc_heap.c +36 -21
172 lines not on this page · raw message
lib/librte_eal/common/malloc_heap.h +3 -1
@@ -5,6 +5,8 @@#ifndef MALLOC_HEAP_H_ #define MALLOC_HEAP_H_ +#include <stdbool.h> +#include <rte_malloc.h> #include <rte_malloc_heap.h>
@@ -25,7 +27,7 @@ malloc_get_numa_socket(void)void *malloc_heap_alloc (const char *type,size_t size,int socket,unsigned int flags, -size_t align,size_t bound); +size_t align,size_t bound,bool contig);int malloc_heap_free (struct malloc_elem *elem);
lib/librte_eal/common/rte_malloc.c +3 -3
@@ -37,7 +37,8 @@ void rte_free(void *addr)* Allocate memory on specified heap. */ void * -rte_malloc_socket (const char *type,size_t size,unsigned align,int socket_arg) +rte_malloc_socket (const char *type,size_t size,unsigned int align, +int socket_arg) {/* return NULL if size is 0 or alignment is not power-of-2 */ if (size ==0 || (align && !rte_is_power_of_2 (align)))
@@ -50,8 +51,7 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket_arg)if (socket_arg >=RTE_MAX_NUMA_NODES )return NULL ; -return malloc_heap_alloc (type, size, socket_arg,0 , - align ==0 ?1 : align,0 ); +return malloc_heap_alloc (type, size, socket_arg,0 , align,0 , false); }/*
Signature · 1 line
2.7.4
PATCH 21/41 eal: enable reserving physically contiguous memzones
In reply to Anatoly BurakovThis adds a new set of _contig API's to rte_memzone.
--- lib/librte_eal/common/eal_common_memzone.c | 44 ++++++++ lib/librte_eal/common/include/rte_memzone.h | 154 ++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+)
lib/librte_eal/common/eal_common_memzone.c +44 -0
@@ -170,6 +170,12 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, socket_id =SOCKET_ID_ANY ;if (len ==0 ) { +/* len == 0 is only allowed for non-contiguous zones */ +if (contig) { +RTE_LOG (DEBUG ,EAL ,"Reserving zero-length contiguous memzones is not supported\n" ); + rte_errno =EINVAL ; +return NULL ; + }if (bound !=0 ) requested_len = bound;else {
@@ -251,6 +257,19 @@ rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,/* * Return a pointer to a correctly filled memzone descriptor (with a +* specified alignment and boundary). If the allocation cannot be done, +* return NULL. +*/ +const struct rte_memzone * +rte_memzone_reserve_bounded_contig (const char *name,size_t len,int socket_id, +unsigned int flags,unsigned int align,unsigned int bound) +{ +return rte_memzone_reserve_thread_safe (name, len, socket_id, flags, + align, bound, true); +} + +/* +* Return a pointer to a correctly filled memzone descriptor (with a * specified alignment). If the allocation cannot be done, return NULL. */ const struct rte_memzone *
@@ -262,6 +281,18 @@ rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id, }/* +* Return a pointer to a correctly filled memzone descriptor (with a +* specified alignment). If the allocation cannot be done, return NULL. +*/ +const struct rte_memzone * +rte_memzone_reserve_aligned_contig (const char *name,size_t len,int socket_id, +unsigned int flags,unsigned int align) +{ +return rte_memzone_reserve_thread_safe (name, len, socket_id, flags, + align,0 , true); +} + +/* * Return a pointer to a correctly filled memzone descriptor. If the * allocation cannot be done, return NULL. */
@@ -274,6 +305,19 @@ rte_memzone_reserve(const char *name, size_t len, int socket_id, false); } +/* +* Return a pointer to a correctly filled memzone descriptor. If the +* allocation cannot be done, return NULL. +*/ +const struct rte_memzone * +rte_memzone_reserve_contig (const char *name,size_t len,int socket_id, +unsigned int flags) +{ +return rte_memzone_reserve_thread_safe (name, len, socket_id, + flags,RTE_CACHE_LINE_SIZE ,0 , + true); +} +int rte_memzone_free (const struct rte_memzone *mz) {
lib/librte_eal/common/include/rte_memzone.h +154 -0
160 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 22/41 eal: replace memzone array with fbarray
In reply to Anatoly BurakovIt's there, so we might as well use it. Some operations will be sped up by that.
Since we have to allocate an fbarray for memzones, we have to do it before we initialize memory subsystem, because that, in secondary processes, will (later) allocate more fbarrays than the primary process, which will result in inability to attach to memzone fbarray if we do it after the fact.
---
Notes:
Code for ENA driver makes little sense to me, but i've attempted to keep the same semantics as the old code.
drivers/net/ena/ena_ethdev.c | 10 +- lib/librte_eal/bsdapp/eal/eal.c | 6 + lib/librte_eal/common/eal_common_memzone.c | 180 +++++++++++++++------- lib/librte_eal/common/include/rte_eal_memconfig.h | 4 +- lib/librte_eal/common/malloc_heap.c | 4 + lib/librte_eal/linuxapp/eal/eal.c | 13 +- test/test/test_memzone.c | 9 +- 7 files changed, 157 insertions(+), 69 deletions(-)
drivers/net/ena/ena_ethdev.c +7 -3
@@ -264,11 +264,15 @@ static const struct eth_dev_ops ena_dev_ops = {static inline int ena_cpu_to_node (int cpu) {struct rte_config *config =rte_eal_get_configuration (); +struct rte_fbarray *arr = &config->mem_config->memzones; +const struct rte_memzone *mz; -if (likely (cpu <RTE_MAX_MEMZONE )) -return config->mem_config->memzone[cpu].socket_id; +if (unlikely (cpu >=RTE_MAX_MEMZONE )) +return NUMA_NO_NODE ; -return NUMA_NO_NODE ; + mz =rte_fbarray_get (arr, cpu); + +return mz->socket_id; } static inline void ena_rx_mbuf_prepare(struct rte_mbuf *mbuf,
lib/librte_eal/bsdapp/eal/eal.c +6 -0
@@ -608,6 +608,12 @@ rte_eal_init(int argc, char **argv)return -1 ; } +if (rte_eal_malloc_heap_init () <0 ) { +rte_eal_init_alert ("Cannot init malloc heap\n" ); + rte_errno =ENODEV ; +return -1 ; + } +if (rte_eal_tailqs_init () <0 ) {rte_eal_init_alert ("Cannot init tail queues for objects\n" ); rte_errno =EFAULT ;
lib/librte_eal/common/eal_common_memzone.c +123 -57
277 lines not on this page · raw message
lib/librte_eal/common/include/rte_eal_memconfig.h +1 -3
@@ -58,10 +58,8 @@ struct rte_mem_config {rte_rwlock_t qlock;/**< used for tailq operation for thread safe. */ rte_rwlock_t mplock;/**< only used by mempool LIB for thread-safe. */ -uint32_t memzone_cnt;/**< Number of allocated memzones */ -/* memory segments and zones */ -struct rte_memzone memzone[RTE_MAX_MEMZONE ];/**< Memzone descriptors. */ +struct rte_fbarray memzones;/**< Memzone descriptors. */ struct rte_memseg_list memsegs[RTE_MAX_MEMSEG_LISTS ];/**< list of dynamic arrays holding memsegs */
lib/librte_eal/common/malloc_heap.c +4 -0
@@ -579,6 +579,10 @@ rte_eal_malloc_heap_init(void)if (mcfg ==NULL )return -1 ; +/* secondary processes don't need to initialize heap */ +if (rte_eal_process_type () ==RTE_PROC_SECONDARY ) +return 0 ; +for (msl_idx =0 ; msl_idx <RTE_MAX_MEMSEG_LISTS ; msl_idx++) {int start;struct rte_fbarray *arr;
lib/librte_eal/linuxapp/eal/eal.c +11 -2
@@ -857,6 +857,15 @@ rte_eal_init(int argc, char **argv) return -1; }#endif +/* memzone_init maps rte_fbarrays, which has to be done before hugepage +* init/attach, because attach creates extra fbarrays in secondary +* process, resulting in inability to map memzone fbarray. +*/ +if (rte_eal_memzone_init () <0 ) { +rte_eal_init_alert ("Cannot init memzone\n" ); + rte_errno =ENODEV ; +return -1 ; + }if (rte_eal_memory_init () <0 ) {rte_eal_init_alert ("Cannot init memory\n" );
@@ -867,8 +876,8 @@ rte_eal_init(int argc, char **argv)/* the directories are locked during eal_hugepage_info_init */ eal_hugedirs_unlock (); -if (rte_eal_memzone_init () <0 ) { -rte_eal_init_alert ("Cannot init memzone\n" ); +if (rte_eal_malloc_heap_init () <0 ) { +rte_eal_init_alert ("Cannot init malloc heap\n" ); rte_errno =ENODEV ;return -1 ; }
test/test/test_memzone.c +5 -4
@@ -893,7 +893,7 @@ test_memzone_basic(void)const struct rte_memzone *mz;int memzone_cnt_after, memzone_cnt_expected;int memzone_cnt_before = -rte_eal_get_configuration ()->mem_config->memzone_cnt; +rte_eal_get_configuration ()->mem_config->memzones.count; memzone1 =rte_memzone_reserve (TEST_MEMZONE_NAME ("testzone1" ),100 ,SOCKET_ID_ANY ,0 );
@@ -917,7 +917,7 @@ test_memzone_basic(void) (memzone3 !=NULL ) + (memzone4 !=NULL ); memzone_cnt_after = -rte_eal_get_configuration ()->mem_config->memzone_cnt; +rte_eal_get_configuration ()->mem_config->memzones.count;if (memzone_cnt_after != memzone_cnt_expected)return -1 ;
@@ -996,7 +996,7 @@ test_memzone_basic(void) } memzone_cnt_after = -rte_eal_get_configuration ()->mem_config->memzone_cnt; +rte_eal_get_configuration ()->mem_config->memzones.count;if (memzone_cnt_after != memzone_cnt_before)return -1 ;
@@ -1017,7 +1017,8 @@ static inttest_memzone (void) {/* take note of how many memzones were allocated before running */ -int memzone_cnt =rte_eal_get_configuration ()->mem_config->memzone_cnt; +int memzone_cnt = +rte_eal_get_configuration ()->mem_config->memzones.count;printf ("test basic memzone API\n" );if (test_memzone_basic () <0 )
Signature · 1 line
2.7.4
PATCH 23/41 mempool: add support for the new allocation methods
In reply to Anatoly BurakovIf a user has specified that the zone should have contiguous memory, use the new _contig allocation API's instead of normal ones. Otherwise, account for the fact that unless we're in IOVA_AS_VA mode, we cannot guarantee that the pages would be physically contiguous, so we calculate the memzone size and alignments as if we were getting the smallest page size available.
--- lib/librte_mempool/rte_mempool.c | 87 +++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 9 deletions(-)
lib/librte_mempool/rte_mempool.c +78 -9
@@ -98,6 +98,27 @@ static unsigned optimize_object_size(unsigned obj_size)return new_obj_size *RTE_MEMPOOL_ALIGN ; } +static size_t +get_min_page_size (void ) +{ +const struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config; +int i; +size_t min_pagesz =SIZE_MAX ; + +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +const struct rte_memseg_list *msl = &mcfg->memsegs[i]; + +if (msl->base_va ==NULL ) +continue ; + +if (msl->hugepage_sz < min_pagesz) + min_pagesz = msl->hugepage_sz; + } + +return min_pagesz ==SIZE_MAX ? (size_t )getpagesize () : min_pagesz; +} +static void mempool_add_elem (struct rte_mempool *mp,void *obj,rte_iova_t iova) {
@@ -549,6 +570,7 @@ rte_mempool_populate_default(struct rte_mempool *mp)unsigned mz_id, n;unsigned int mp_flags;int ret; +bool force_contig, no_contig;/* mempool must not be populated */ if (mp->nb_mem_chunks !=0 )
@@ -563,10 +585,46 @@ rte_mempool_populate_default(struct rte_mempool *mp)/* update mempool capabilities */ mp->flags |= mp_flags; -if (rte_eal_has_hugepages ()) { - pg_shift =0 ;/* not needed, zone is physically contiguous */ + no_contig = mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ; + force_contig = mp->flags &MEMPOOL_F_CAPA_PHYS_CONTIG ; + +/* +* there are several considerations for page size and page shift here. +* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything. +* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory. +* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so +* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/ + +if (no_contig || force_contig ||rte_eal_iova_mode () ==RTE_IOVA_VA ) { pg_sz =0 ; + pg_shift =0 ; align =RTE_CACHE_LINE_SIZE ; + }else if (rte_eal_has_hugepages ()) { + pg_sz =get_min_page_size (); + pg_shift =rte_bsf32 (pg_sz); + align = pg_sz; }else { pg_sz =getpagesize (); pg_shift =rte_bsf32 (pg_sz);
@@ -585,23 +643,34 @@ rte_mempool_populate_default(struct rte_mempool *mp) goto fail; } - mz =rte_memzone_reserve_aligned (mz_name, size, - mp->socket_id, mz_flags, align); -/* not enough memory, retry with the biggest zone we have */ -if (mz ==NULL ) - mz =rte_memzone_reserve_aligned (mz_name,0 , +if (force_contig) { +/* +* if contiguous memory for entire mempool memory was +* requested, don't try reserving again if we fail. +*/ + mz =rte_memzone_reserve_aligned_contig (mz_name, size, + mp->socket_id, mz_flags, align); + }else { + mz =rte_memzone_reserve_aligned (mz_name, size, mp->socket_id, mz_flags, align); +/* not enough memory, retry with the biggest zone we +* have +*/ +if (mz ==NULL ) + mz =rte_memzone_reserve_aligned (mz_name,0 , + mp->socket_id, mz_flags, align); + }if (mz ==NULL ) { ret = -rte_errno; goto fail; } -if (mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ) +if (no_contig) iova =RTE_BAD_IOVA ;else iova = mz->iova; -if (rte_eal_has_hugepages ()) +if (rte_eal_has_hugepages () && force_contig) ret = rte_mempool_populate_iova(mp, mz->addr, iova, mz->len, rte_mempool_memchunk_mz_free,
Signature · 1 line
2.7.4
PATCH 24/41 vfio: allow to map other memory regions
In reply to Anatoly BurakovCurrently it is not possible to use memory that is not owned by DPDK to perform DMA. This scenarion might be used in vhost applications (like SPDK) where guest send its own memory table. To fill this gap provide API to allow registering arbitrary address in VFIO container.
--- lib/librte_eal/bsdapp/eal/eal.c | 16 ++++ lib/librte_eal/common/include/rte_vfio.h | 39 ++++++++ lib/librte_eal/linuxapp/eal/eal_vfio.c | 153 ++++++++++++++++++++++++++----- lib/librte_eal/linuxapp/eal/eal_vfio.h | 11 +++ 4 files changed, 196 insertions(+), 23 deletions(-)
lib/librte_eal/bsdapp/eal/eal.c +16 -0
@@ -755,6 +755,8 @@ int rte_vfio_enable(const char *modname);int rte_vfio_is_enabled (const char *modname);int rte_vfio_noiommu_is_enabled (void );int rte_vfio_clear_group (int vfio_group_fd); +int rte_vfio_dma_map (uint64_t vaddr,uint64_t iova,uint64_t len); +int rte_vfio_dma_unmap (uint64_t vaddr,uint64_t iova,uint64_t len); int rte_vfio_setup_device(__rte_unused const char *sysfs_base, __rte_unused const char *dev_addr,
@@ -790,3 +792,17 @@ int rte_vfio_clear_group(__rte_unused int vfio_group_fd) {return 0 ; } + +int +rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova, + __rte_unused uint64_t len) +{ +return -1 ; +} + +int +rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova, + __rte_unused uint64_t len) +{ +return -1 ; +}
lib/librte_eal/common/include/rte_vfio.h +39 -0
@@ -123,6 +123,45 @@ int rte_vfio_noiommu_is_enabled(void);int rte_vfio_clear_group (int vfio_group_fd); +/** +* Map memory region for use with VFIO. +* +* @param vaddr +* Starting virtual address of memory to be mapped. +* +* @param iova +* Starting IOVA address of memory to be mapped. +* +* @param len +* Length of memory segment being mapped. +* +* @return +* 0 if success. +* -1 on error. +*/ +int +rte_vfio_dma_map (uint64_t vaddr,uint64_t iova,uint64_t len); + + +/** +* Unmap memory region from VFIO. +* +* @param vaddr +* Starting virtual address of memory to be unmapped. +* +* @param iova +* Starting IOVA address of memory to be unmapped. +* +* @param len +* Length of memory segment being unmapped. +* +* @return +* 0 if success. +* -1 on error. +*/ +int +rte_vfio_dma_unmap (uint64_t vaddr,uint64_t iova,uint64_t len); +#endif /* VFIO_PRESENT */ #endif /* _RTE_VFIO_H_ */
lib/librte_eal/linuxapp/eal/eal_vfio.c +130 -23
208 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/eal_vfio.h +11 -0
@@ -19,6 +19,7 @@#ifdef VFIO_PRESENT +#include <stdint.h> #include <linux/vfio.h> #define RTE_VFIO_TYPE1 VFIO_TYPE1_IOMMU
@@ -110,6 +111,7 @@ struct vfio_config {int vfio_enabled;int vfio_container_fd;int vfio_active_groups; +const struct vfio_iommu_type *vfio_iommu_type;struct vfio_group vfio_groups[VFIO_MAX_GROUPS ]; };
@@ -119,9 +121,18 @@ struct vfio_config {* */ typedef int (*vfio_dma_func_t )(int ); +/* Custom memory region DMA mapping function prototype. +* Takes VFIO container fd, virtual address, phisical address, length and +* operation type (0 to unmap 1 for map) as a parameters. +* Returns 0 on success, -1 on error. +**/ +typedef int (*vfio_dma_user_func_t )(int fd,uint64_t vaddr,uint64_t iova, +uint64_t len,int do_map); +struct vfio_iommu_type {int type_id;const char *name; +vfio_dma_user_func_t dma_user_map_func;vfio_dma_func_t dma_map_func; };
Signature · 1 line
2.7.4
PATCH 25/41 eal: map/unmap memory with VFIO when alloc/free pages
In reply to Anatoly Burakov--- lib/librte_eal/linuxapp/eal/eal_memalloc.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
lib/librte_eal/linuxapp/eal/eal_memalloc.c +11 -0
@@ -34,6 +34,7 @@#include <rte_eal.h> #include <rte_memory.h> #include <rte_spinlock.h> +#include <rte_vfio.h> #include "eal_filesystem.h" #include "eal_internal_cfg.h"
@@ -476,6 +477,10 @@ alloc_page(struct rte_memseg *ms, void *addr, uint64_t size, int socket_id, ms->iova = iova; ms->socket_id = socket_id; +/* map the segment so that VFIO has access to it */ +if (rte_eal_iova_mode () ==RTE_IOVA_VA && +rte_vfio_dma_map (ms->addr_64, iova, size)) +RTE_LOG (DEBUG ,EAL ,"Cannot register segment with VFIO\n" );return 0 ; mapped:
@@ -507,6 +512,12 @@ free_page(struct rte_memseg *ms, struct hugepage_info *hi,char path[PATH_MAX ];int fd, ret; +/* unmap the segment from VFIO */ +if (rte_eal_iova_mode () ==RTE_IOVA_VA && +rte_vfio_dma_unmap (ms->addr_64, ms->iova, ms->len)) { +RTE_LOG (DEBUG ,EAL ,"Cannot unregister segment with VFIO\n" ); + } +if (mmap (ms->addr, ms->hugepage_sz,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ) ==MAP_FAILED ) {
Signature · 1 line
2.7.4
PATCH 26/41 eal: prepare memseg lists for multiprocess sync
In reply to Anatoly BurakovIn preparation for implementing multiprocess support, we are adding a version number and write locks to memseg lists.
There are two ways of implementing multiprocess support for memory
hotplug: either all information about mapped memory is shared between processes, and secondary processes simply attempt to map/unmap memory based on requests from the primary, or secondary processes store their own maps and only check if they are in sync with the primary process' maps.
This implementation will opt for the latter option: primary process shared mappings will be authoritative, and each secondary process will use its own interal view of mapped memory, and will attempt to synchronize on these mappings using versioning.
Under this model, only primary process will decide which pages get mapped, and secondary processes will only copy primary's page maps and get notified of the changes via IPC mechanism (coming in later commits).
To avoid race conditions, memseg lists will also have write locks - that is, it will be possible for several secondary processes to initialize concurrently, but it will not be possible for several processes to request memory allocation unless all other allocations were complete (on a single socket - it is OK to allocate/free memory on different sockets concurrently).
In principle, it is possible for multiple processes to request allocation/deallcation on multiple sockets, but we will only allow one such request to be active at any one time.
--- lib/librte_eal/bsdapp/eal/eal_memalloc.c | 7 + lib/librte_eal/common/eal_memalloc.h | 4 + lib/librte_eal/common/include/rte_eal_memconfig.h | 2 + lib/librte_eal/linuxapp/eal/eal_memalloc.c | 288 +++++++++++++++++++++- 4 files changed, 295 insertions(+), 6 deletions(-)
lib/librte_eal/bsdapp/eal/eal_memalloc.c +7 -0
@@ -24,3 +24,10 @@ eal_memalloc_alloc_page(uint64_t __rte_unused size, int __rte_unused socket)RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" );return NULL ; } + +int +eal_memalloc_sync_with_primary (void ) +{ +RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" ); +return -1 ; +}
lib/librte_eal/common/eal_memalloc.h +4 -0
@@ -24,4 +24,8 @@ bool eal_memalloc_is_contig(struct rte_memseg_list *msl, void *start, size_t len); +/* synchronize local memory map to primary process */ +int +eal_memalloc_sync_with_primary (void ); +#endif // EAL_MEMALLOC_H
lib/librte_eal/common/include/rte_eal_memconfig.h +2 -0
@@ -32,6 +32,8 @@ struct rte_memseg_list { };int socket_id;/**< Socket ID for all memsegs in this list. */ uint64_t hugepage_sz;/**< page size for all memsegs in this list. */ +rte_rwlock_t mplock;/**< read-write lock for multiprocess sync. */ +uint32_t version;/**< version number for multiprocess sync. */ struct rte_fbarray memseg_arr; };
lib/librte_eal/linuxapp/eal/eal_memalloc.c +282 -6
336 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 27/41 eal: add multiprocess init with memory hotplug
In reply to Anatoly Burakovfor legacy memory mode, attach to primary's memseg list, and map hugepages as before.
for non-legacy mode, preallocate all VA space and then do a sync of local memory map.
--- lib/librte_eal/bsdapp/eal/eal_hugepage_info.c | 7 ++ lib/librte_eal/common/eal_common_memory.c | 99 +++++++++++++++++++++---- lib/librte_eal/common/eal_hugepages.h | 5 ++ lib/librte_eal/linuxapp/eal/eal.c | 18 +++-- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 53 ++++++++----- lib/librte_eal/linuxapp/eal/eal_memory.c | 24 ++++-- 6 files changed, 159 insertions(+), 47 deletions(-)
lib/librte_eal/bsdapp/eal/eal_hugepage_info.c +7 -0
@@ -103,3 +103,10 @@ eal_hugepage_info_init(void)return 0 ; } + +/* memory hotplug is not supported in FreeBSD, so no need to implement this */ +int +eal_hugepage_info_read (void ) +{ +return 0 ; +}
lib/librte_eal/common/eal_common_memory.c +84 -15
151 lines not on this page · raw message
lib/librte_eal/common/eal_hugepages.h +5 -0
@@ -34,4 +34,9 @@ struct hugepage_file {*/ int eal_hugepage_info_init (void ); +/** +* Read information about hugepages on Linux, but don't clear them out. +*/ +int eal_hugepage_info_read (void ); +#endif /* EAL_HUGEPAGES_H */
lib/librte_eal/linuxapp/eal/eal.c +11 -7
@@ -805,13 +805,17 @@ rte_eal_init(int argc, char **argv) "KNI module inserted\n"); } -if (internal_config.no_hugetlbfs ==0 && - internal_config.process_type !=RTE_PROC_SECONDARY && -eal_hugepage_info_init () <0 ) { -rte_eal_init_alert ("Cannot get hugepage information." ); - rte_errno =EACCES ; -rte_atomic32_clear (&run_once); -return -1 ; +if (internal_config.no_hugetlbfs ==0 ) { +/* rte_config isn't initialized yet */ + ret = internal_config.process_type ==RTE_PROC_PRIMARY ? +eal_hugepage_info_init () : +eal_hugepage_info_read (); +if (ret <0 ) { +rte_eal_init_alert ("Cannot get hugepage information." ); + rte_errno =EACCES ; +rte_atomic32_clear (&run_once); +return -1 ; + } }if (internal_config.memory ==0 && internal_config.force_sockets ==0 ) {
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +33 -20
82 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/eal_memory.c +19 -5
@@ -1569,6 +1569,22 @@ eal_legacy_hugepage_attach(void)return -1 ; } +static int +eal_hugepage_attach (void ) +{ +if (eal_memalloc_sync_with_primary ()) { +RTE_LOG (ERR ,EAL ,"Could not map memory from primary process\n" ); +if (aslr_enabled () >0 ) { +RTE_LOG (ERR ,EAL ,"It is recommended to " +"disable ASLR in the kernel " +"and retry running both primary " +"and secondary processes\n" ); + } +return -1 ; + } +return 0 ; +} +int rte_eal_hugepage_init (void ) {
@@ -1580,11 +1596,9 @@ rte_eal_hugepage_init(void)int rte_eal_hugepage_attach (void ) { -if (internal_config.legacy_mem) -return eal_legacy_hugepage_attach (); -else -RTE_LOG (ERR ,EAL ,"Secondary processes aren't supported yet\n" ); -return -1 ; +return internal_config.legacy_mem ? +eal_legacy_hugepage_attach () : +eal_hugepage_attach (); }int
Signature · 1 line
2.7.4
PATCH 28/41 eal: add support for multiprocess memory hotplug
In reply to Anatoly BurakovThis enables multiprocess synchronization for memory hotplug requests at runtime (as opposed to initialization).
Basic workflow is the following. Primary process always does initial mapping and unmapping, and secondary processes always follow primary page map. Only one allocation request can be active at any one time.
When primary allocates memory, it ensures that all other processes have allocated the same set of hugepages successfully, otherwise any allocations made are being rolled back, and heap is freed back. Heap is locked throughout the process, so no race conditions can happen.
When primary frees memory, it frees the heap, deallocates affected pages, and notifies other processes of deallocations. Since heap is freed from that memory chunk, the area basically becomes invisible to other processes even if they happen to fail to unmap that specific set of pages, so it's completely safe to ignore results of sync requests.
When secondary allocates memory, it does not do so by itself. Instead, it sends a request to primary process to try and allocate pages of specified size and on specified socket, such that a specified heap allocation request could complete. Primary process then sends all secondaries (including the requestor) a separate notification of allocated pages, and expects all secondary processes to report success before considering pages as "allocated".
Only after primary process ensures that all memory has been successfully allocated in all secondary process, it will respond positively to the initial request, and let secondary proceed with the allocation. Since the heap now has memory that can satisfy allocation request, and it was locked all this time (so no other allocations could take place), secondary process will be able to allocate memory from the heap.
When secondary frees memory, it hides pages to be deallocated from the heap. Then, it sends a deallocation request to primary process, so that it deallocates pages itself, and then sends a separate sync request to all other processes (including the requestor) to unmap the same pages. This way, even if secondary fails to notify other processes of this deallocation, that memory will become invisible to other processes, and will not be allocated from again.
So, to summarize: address space will only become part of the heap if primary process can ensure that all other processes have allocated this memory successfully. If anything goes wrong, the worst thing that could happen is that a page will "leak" and will not be available to neither DPDK nor the system, as some process will still hold onto it. It's not an actual leak, as we can account for the page - it's just that none of the processes will be able to use this page for anything useful, until it gets allocated from by the primary.
Due to underlying DPDK IPC implementation being single-threaded, some asynchronous magic had to be done, as we need to complete several requests before we can definitively allow secondary process to use allocated memory (namely, it has to be present in all other secondary processes before it can be used). Additionally, only one allocation request is allowed to be submitted at once.
Memory allocation requests are only allowed when there are no secondary processes currently initializing. To enforce that, a shared rwlock is used, that is set to read lock on init (so that several secondaries could initialize concurrently), and write lock on making allocation requests (so that either secondary init will have to wait, or allocation request will have to wait until all processes have initialized).
To reduce possibility of not releasing lock on fail to init, replace all rte_panic's with init alert followed by a return -1.
---
Notes:
This problem is evidently complex to solve without multithreaded IPC implementation. An alternative approach would be to process each individual message in its own thread (or at least spawn a thread per incoming request) - that way, we can send requests while responding to another request, and this problem becomes trivial to solve (and in fact it was solved that way initially, before my aversion to certain other programming languages kicked in).
Is the added complexity worth saving a couple of thread spin-ups here and there?
lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/common/include/rte_eal_memconfig.h | 3 + lib/librte_eal/common/malloc_heap.c | 250 ++++++-- lib/librte_eal/common/malloc_mp.c | 723 ++++++++++++++++++++++ lib/librte_eal/common/malloc_mp.h | 86 +++ lib/librte_eal/common/meson.build | 1 + lib/librte_eal/linuxapp/eal/Makefile | 1 + lib/librte_eal/linuxapp/eal/eal.c | 50 +- 8 files changed, 1054 insertions(+), 61 deletions(-) create mode 100644 lib/librte_eal/common/malloc_mp.c create mode 100644 lib/librte_eal/common/malloc_mp.h
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -59,6 +59,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_heap.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_mp.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_keepalive.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_service.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_reciprocal.c
lib/librte_eal/common/include/rte_eal_memconfig.h +3 -0
@@ -60,6 +60,9 @@ struct rte_mem_config {rte_rwlock_t qlock;/**< used for tailq operation for thread safe. */ rte_rwlock_t mplock;/**< only used by mempool LIB for thread-safe. */ +rte_rwlock_t memory_hotplug_lock; +/**< indicates whether memory hotplug request is in progress. */ +/* memory segments and zones */ struct rte_fbarray memzones;/**< Memzone descriptors. */
lib/librte_eal/common/malloc_heap.c +208 -42
381 lines not on this page · raw message
lib/librte_eal/common/malloc_mp.c new file +723 -0
723 lines not on this page · raw message
lib/librte_eal/common/malloc_mp.h new file +86 -0
86 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -27,6 +27,7 @@ common_sources = files( 'eal_common_timer.c', 'malloc_elem.c', 'malloc_heap.c', + 'malloc_mp.c', 'rte_keepalive.c', 'rte_malloc.c', 'rte_reciprocal.c',
lib/librte_eal/linuxapp/eal/Makefile +1 -0
@@ -67,6 +67,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_heap.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_mp.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_keepalive.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_service.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_reciprocal.c
lib/librte_eal/linuxapp/eal/eal.c +31 -19
153 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 29/41 eal: add support for callbacks on memory hotplug
In reply to Anatoly BurakovEach process will have its own callbacks. Callbacks will indicate whether it's allocation and deallocation that's happened, and will also provide start VA address and length of allocated block.
Since memory hotplug isn't supported on FreeBSD and in legacy mem mode, it will not be possible to register them in either.
--- lib/librte_eal/common/eal_common_memalloc.c | 132 ++++++++++++++++++++++++++++ lib/librte_eal/common/eal_common_memory.c | 28 ++++++ lib/librte_eal/common/eal_memalloc.h | 10 +++ lib/librte_eal/common/include/rte_memory.h | 48 ++++++++++ lib/librte_eal/rte_eal_version.map | 2 + 5 files changed, 220 insertions(+)
lib/librte_eal/common/eal_common_memalloc.c +132 -0
@@ -2,16 +2,46 @@* Copyright(c) 2017-2018 Intel Corporation */ +#include <string.h> + +#include <rte_errno.h> #include <rte_lcore.h> #include <rte_fbarray.h> #include <rte_memzone.h> #include <rte_memory.h> #include <rte_eal_memconfig.h> +#include <rte_rwlock.h> #include "eal_private.h" #include "eal_internal_cfg.h" #include "eal_memalloc.h" +struct mem_event_callback_entry { +TAILQ_ENTRY (mem_event_callback_entry) next; +char name[RTE_MEM_EVENT_CALLBACK_NAME_LEN ]; +rte_mem_event_callback_t clb; +}; + +/** Double linked list of actions. */ +TAILQ_HEAD (mem_event_callback_entry_list, mem_event_callback_entry); + +static struct mem_event_callback_entry_list callback_list = +TAILQ_HEAD_INITIALIZER (callback_list); + +static rte_rwlock_t rwlock =RTE_RWLOCK_INITIALIZER ; + +static struct mem_event_callback_entry * +find_callback (const char *name) +{ +struct mem_event_callback_entry *r; + + TAILQ_FOREACH(r, &callback_list, next) { +if (!strcmp (r->name, name)) +break ; + } +return r; +} +bool eal_memalloc_is_contig (struct rte_memseg_list *msl,void *start,size_t len)
@@ -47,3 +77,105 @@ eal_memalloc_is_contig(struct rte_memseg_list *msl, void *start,return true; } + +int +eal_memalloc_callback_register (const char *name, +rte_mem_event_callback_t clb) +{ +struct mem_event_callback_entry *entry; +int ret, len; +if (name ==NULL || clb ==NULL ) { + rte_errno =EINVAL ; +return -1 ; + } + len =strnlen (name,RTE_MEM_EVENT_CALLBACK_NAME_LEN ); +if (len ==0 ) { + rte_errno =EINVAL ; +return -1 ; + }else if (len ==RTE_MEM_EVENT_CALLBACK_NAME_LEN ) { + rte_errno =ENAMETOOLONG ; +return -1 ; + } +rte_rwlock_write_lock (&rwlock); + + entry =find_callback (name); +if (entry !=NULL ) { + rte_errno =EEXIST ; + ret =-1 ; + goto unlock; + } + + entry =malloc (sizeof (*entry)); +if (entry ==NULL ) { + rte_errno =ENOMEM ; + ret =-1 ; + goto unlock; + } + +/* callback successfully created and is valid, add it to the list */ + entry->clb = clb; +snprintf (entry->name,RTE_MEM_EVENT_CALLBACK_NAME_LEN ,"%s" , name); +TAILQ_INSERT_TAIL (&callback_list, entry, next); + + ret =0 ; + +RTE_LOG (DEBUG ,EAL ,"Mem event callback '%s' registered\n" , name); + +unlock: +rte_rwlock_write_unlock (&rwlock); +return ret; +} + +int +eal_memalloc_callback_unregister (const char *name) +{ +struct mem_event_callback_entry *entry; +int ret, len; + +if (name ==NULL ) { + rte_errno =EINVAL ; +return -1 ; + } + len =strnlen (name,RTE_MEM_EVENT_CALLBACK_NAME_LEN ); +if (len ==0 ) { + rte_errno =EINVAL ; +return -1 ; + }else if (len ==RTE_MEM_EVENT_CALLBACK_NAME_LEN ) { + rte_errno =ENAMETOOLONG ; +return -1 ; + } +rte_rwlock_write_lock (&rwlock); + + entry =find_callback (name); +if (entry ==NULL ) { + rte_errno =ENOENT ; + ret =-1 ; + goto unlock; + } +TAILQ_REMOVE (&callback_list, entry, next); +free (entry); + + ret =0 ; + +RTE_LOG (DEBUG ,EAL ,"Mem event callback '%s' unregistered\n" , name); + +unlock: +rte_rwlock_write_unlock (&rwlock); +return ret; +} + +void +eal_memalloc_notify (enum rte_mem_event event,const void *start,size_t len) +{ +struct mem_event_callback_entry *entry; + +rte_rwlock_read_lock (&rwlock); + + TAILQ_FOREACH(entry, &callback_list, next) { +RTE_LOG (DEBUG ,EAL ,"Calling mem event callback %s" , + entry->name); + entry->clb (event, start, len); + } + +rte_rwlock_read_unlock (&rwlock); +}
lib/librte_eal/common/eal_common_memory.c +28 -0
@@ -466,6 +466,34 @@ rte_eal_get_physmem_size(void)return total_len; } +/* +* Defining here because declared in rte_memory.h, but the actual implementation +* is in eal_common_memalloc.c, like all other memalloc internals. +*/ +int +rte_mem_event_register_callback (const char *name,rte_mem_event_callback_t clb) +{ +/* FreeBSD boots with legacy mem enabled by default */ +if (internal_config.legacy_mem) { +RTE_LOG (DEBUG ,EAL ,"Registering mem event callbacks not supported\n" ); + rte_errno =ENOTSUP ; +return -1 ; + } +return eal_memalloc_callback_register (name, clb); +} + +int +rte_mem_event_unregister_callback (const char *name) +{ +/* FreeBSD boots with legacy mem enabled by default */ +if (internal_config.legacy_mem) { +RTE_LOG (DEBUG ,EAL ,"Registering mem event callbacks not supported\n" ); + rte_errno =ENOTSUP ; +return -1 ; + } +return eal_memalloc_callback_unregister (name); +} +/* Dump the physical memory layout on console */ void rte_dump_physmem_layout (FILE *f)
lib/librte_eal/common/eal_memalloc.h +10 -0
@@ -28,4 +28,14 @@ eal_memalloc_is_contig(struct rte_memseg_list *msl, void *start,int eal_memalloc_sync_with_primary (void ); +int +eal_memalloc_callback_register (const char *name, +rte_mem_event_callback_t clb); + +int +eal_memalloc_callback_unregister (const char *name); + +void +eal_memalloc_notify (enum rte_mem_event event,const void *start,size_t len); +#endif // EAL_MEMALLOC_H
lib/librte_eal/common/include/rte_memory.h +48 -0
54 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -0
@@ -214,6 +214,8 @@ DPDK_18.05 { global: rte_num_sockets; + rte_mem_event_callback_register; + rte_mem_event_callback_unregister; rte_mem_virt2memseg; rte_mem_virt2memseg_list; rte_malloc_dump_heaps;
Signature · 1 line
2.7.4
PATCH 30/41 eal: enable callbacks on malloc/free and mp sync
In reply to Anatoly BurakovAlso, rewrite VFIO to rely on memory callbacks instead of manually registering memory with VFIO. Callbacks will only be registered if VFIO is enabled.
--- lib/librte_eal/common/malloc_heap.c | 21 +++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_memalloc.c | 37 +++++++++++++++++++++--------- lib/librte_eal/linuxapp/eal/eal_vfio.c | 35 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 11 deletions(-)
lib/librte_eal/common/malloc_heap.c +21 -0
@@ -223,6 +223,7 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,void *map_addr;size_t map_len;int n_pages; +bool callback_triggered = false; map_len =RTE_ALIGN_CEIL (align + elt_size +MALLOC_ELEM_TRAILER_LEN , pg_sz);
@@ -242,14 +243,25 @@ 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_notify (RTE_MEM_EVENT_ALLOC , map_addr, map_len); +/* 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 += map_len; RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
@@ -260,6 +272,9 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,return 0 ; free_elem: +if (callback_triggered) +eal_memalloc_notify (RTE_MEM_EVENT_FREE , map_addr, map_len); +rollback_expand_heap (ms, n_pages, elem, map_addr, map_len);request_sync ();
@@ -615,6 +630,10 @@ malloc_heap_free(struct malloc_elem *elem) heap->total_size -= n_pages * msl->hugepage_sz;if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) { +/* notify user about changes in memory map */ +eal_memalloc_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);
@@ -637,6 +656,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); }
lib/librte_eal/linuxapp/eal/eal_memalloc.c +26 -11
@@ -34,7 +34,6 @@#include <rte_eal.h> #include <rte_memory.h> #include <rte_spinlock.h> -#include <rte_vfio.h> #include "eal_filesystem.h" #include "eal_internal_cfg.h"
@@ -480,10 +479,6 @@ alloc_page(struct rte_memseg *ms, void *addr, uint64_t size, int socket_id, ms->iova = iova; ms->socket_id = socket_id; -/* map the segment so that VFIO has access to it */ -if (rte_eal_iova_mode () ==RTE_IOVA_VA && -rte_vfio_dma_map (ms->addr_64, iova, size)) -RTE_LOG (DEBUG ,EAL ,"Cannot register segment with VFIO\n" );return 0 ; mapped:
@@ -515,12 +510,6 @@ free_page(struct rte_memseg *ms, struct hugepage_info *hi,char path[PATH_MAX ];int fd, ret; -/* unmap the segment from VFIO */ -if (rte_eal_iova_mode () ==RTE_IOVA_VA && -rte_vfio_dma_unmap (ms->addr_64, ms->iova, ms->len)) { -RTE_LOG (DEBUG ,EAL ,"Cannot unregister segment with VFIO\n" ); - } -if (mmap (ms->addr, ms->hugepage_sz,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ) ==MAP_FAILED ) {
@@ -808,6 +797,19 @@ sync_chunk(struct rte_memseg_list *primary_msl, diff_len =RTE_MIN (chunk_len, diff_len); +/* if we are freeing memory, notif the application */ +if (!used) { +struct rte_memseg *ms; +void *start_va; +size_t len; + + ms =rte_fbarray_get (l_arr, start); + start_va = ms->addr; + len = ms->len * diff_len; + +eal_memalloc_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;
@@ -834,6 +836,19 @@ 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; + + ms =rte_fbarray_get (l_arr, start); + start_va = ms->addr; + len = ms->len * diff_len; + +eal_memalloc_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) :
lib/librte_eal/linuxapp/eal/eal_vfio.c +35 -0
@@ -214,6 +214,37 @@ vfio_group_device_count(int vfio_group_fd)return vfio_cfg.vfio_groups[i].devices; } +static void +vfio_mem_event_callback (enum rte_mem_event type,const void *addr,size_t len) +{ +struct rte_memseg_list *msl; +struct rte_memseg *ms; +size_t cur_len =0 ; +uint64_t pgsz; + + msl =rte_mem_virt2memseg_list (addr); + pgsz = msl->hugepage_sz; + +while (cur_len < len) { +const void *va =RTE_PTR_ADD (addr, cur_len); +uint64_t vfio_va, iova; + + ms =rte_mem_virt2memseg (va, msl); + vfio_va = (uint64_t ) (uintptr_t ) va; + iova = ms->iova; + +/* this never gets called in legacy mode, so we can be sure that +* each segment is a single page. +*/ +if (type ==RTE_MEM_EVENT_ALLOC ) +rte_vfio_dma_map (vfio_va, iova, pgsz); +else +rte_vfio_dma_unmap (vfio_va, iova, pgsz); + + cur_len += pgsz; + } +} +int rte_vfio_clear_group (int vfio_group_fd) {
@@ -507,6 +538,10 @@ rte_vfio_enable(const char *modname)if (vfio_cfg.vfio_container_fd !=-1 ) {RTE_LOG (NOTICE ,EAL ,"VFIO support initialized\n" ); vfio_cfg.vfio_enabled =1 ; + +/* register callback for mem events */ +rte_mem_event_register_callback ("vfio_mem_event_clb" , + vfio_mem_event_callback); }else {RTE_LOG (NOTICE ,EAL ,"VFIO support could not be initialized\n" ); }
Signature · 1 line
2.7.4
PATCH 31/41 ethdev: use contiguous allocation for DMA memory
In reply to Anatoly BurakovThis fixes the following drivers in one go:
grep -Rl rte_eth_dma_zone_reserve drivers/
drivers/net/avf/avf_rxtx.cdrivers/net/thunderx/nicvf_ethdev.cdrivers/net/e1000/igb_rxtx.cdrivers/net/e1000/em_rxtx.cdrivers/net/fm10k/fm10k_ethdev.cdrivers/net/vmxnet3/vmxnet3_rxtx.cdrivers/net/liquidio/lio_rxtx.cdrivers/net/i40e/i40e_rxtx.cdrivers/net/sfc/sfc.cdrivers/net/ixgbe/ixgbe_rxtx.cdrivers/net/nfp/nfp_net.c
--- lib/librte_ether/rte_ethdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_ether/rte_ethdev.c +2 -1
@@ -3401,7 +3401,8 @@ rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,if (mz)return mz; -return rte_memzone_reserve_aligned (z_name, size, socket_id,0 , align); +return rte_memzone_reserve_aligned_contig (z_name, size, socket_id,0 , + align); }int
Signature · 1 line
2.7.4
Re: [PATCH 31/41] ethdev: use contiguous allocation for DMA memory
Quoted 1 line · Anatoly Burakov
This fixes the following drivers in one go:
Does it mean that these drivers are broken in the middle of patch set and fixed now? If so, it would be good to avoid it. It breaks bisect.
Quoted 33 lines
grep -Rl
rte_eth_dma_zone_reservedrivers/
drivers/net/avf/avf_rxtx.cdrivers/net/thunderx/nicvf_ethdev.cdrivers/net/e1000/igb_rxtx.cdrivers/net/e1000/em_rxtx.cdrivers/net/fm10k/fm10k_ethdev.cdrivers/net/vmxnet3/vmxnet3_rxtx.cdrivers/net/liquidio/lio_rxtx.cdrivers/net/i40e/i40e_rxtx.cdrivers/net/sfc/sfc.cdrivers/net/ixgbe/ixgbe_rxtx.cdrivers/net/nfp/nfp_net.cSigned-off-by Anatoly Burakov <[email protected]>--- lib/librte_ether/rte_ethdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/librte_ether/rte_ethdev.c +2
-1@@ -3401,7 +3401,8 @@ rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,if (mz)return mz; -return rte_memzone_reserve_aligned (z_name, size, socket_id,0 , align); +return rte_memzone_reserve_aligned_contig (z_name, size, socket_id,0 , + align); }int
Re: [PATCH 31/41] ethdev: use contiguous allocation for DMA memory
Quoted 6 lines · Andrew Rybchenko
Quoted 1 line · Anatoly Burakov
This fixes the following drivers in one go:
Does it mean that these drivers are broken in the middle of patch set and fixed now? If so, it would be good to avoid it. It breaks bisect.
Depends on the definition of "broken". Legacy memory mode will still work for all drivers throughout the patchset. As for new memory mode, yes, it will be "broken in the middle of the patchset", but due to the fact that there's enormous amount of code to review between fbarray changes, malloc changes, contiguous allocation changes and adding new rte_memzone API's, i favored ease of code review over bisect.
I can of course reorder and roll up several different patchset and all driver updates into one giant patch, but do you really want to be the one reviewing such a patch?
Signature · 2 lines
Thanks, Anatoly
Re: [PATCH 31/41] ethdev: use contiguous allocation for DMA memory
Quoted 11 lines · Anatoly
Quoted 6 lines · Andrew Rybchenko
Quoted 1 line · Anatoly Burakov
This fixes the following drivers in one go:
Does it mean that these drivers are broken in the middle of patch set and fixed now? If so, it would be good to avoid it. It breaks bisect.
Depends on the definition of "broken". Legacy memory mode will still work for all drivers throughout the patchset. As for new memory mode, yes, it will be "broken in the middle of the patchset", but due to the fact that there's enormous amount of code to review between fbarray changes, malloc changes, contiguous allocation changes and adding new
rte_memzoneAPI's, i favored ease of code review over bisect.I can of course reorder and roll up several different patchset and all driver updates into one giant patch, but do you really want to be the one reviewing such a patch?
Is it possible to:
1. Introduce _contig function
2. Switch users of the contiguous allocation to it as you do now
3. Make the old function to allocate possibly non-contiguous memory
Re: [PATCH 31/41] ethdev: use contiguous allocation for DMA memory
Quoted 9 lines · Andrew Rybchenko
Quoted 8 lines · Anatoly
Quoted 6 lines · Andrew Rybchenko
Quoted 1 line · Anatoly Burakov
This fixes the following drivers in one go:
Does it mean that these drivers are broken in the middle of patch set and fixed now? If so, it would be good to avoid it. It breaks bisect.
Quoted 9 lines
Quoted 3 lines
Depends on the definition of "broken". Legacy memory mode will still work for all drivers throughout the patchset. As for new memory mode, yes, it will be "broken in the middle of the patchset", but due to the fact that there's enormous amount of code to review between fbarray changes, malloc changes, contiguous allocation changes and adding new
rte_memzoneAPI's, i favored ease of code review over bisect.I can of course reorder and roll up several different patchset and all driver updates into one giant patch, but do you really want to be the one reviewing such a patch?
Is it possible to:
1. Introduce_contigfunction
2. Switch users of the contiguous allocation to it as you do now
3. Make the old function to allocate possibly non-contiguous memory
Good point. I'll see if i can shuffle patches around for v2. Thanks!
Signature · 2 lines
Thanks, Anatoly
PATCH 32/41 crypto/qat: use contiguous allocation for DMA memory
In reply to Anatoly Burakov--- drivers/crypto/qat/qat_qp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/qat/qat_qp.c +2 -2
@@ -95,8 +95,8 @@ queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size,default : memzone_flags =RTE_MEMZONE_SIZE_HINT_ONLY ; } -return rte_memzone_reserve_aligned (queue_name, queue_size, socket_id, - memzone_flags, queue_size); +return rte_memzone_reserve_aligned_contig (queue_name, queue_size, + socket_id, memzone_flags, queue_size); } int qat_crypto_sym_qp_setup(struct rte_cryptodev *dev, uint16_t queue_pair_id,
Signature · 1 line
2.7.4
Re: [PATCH 32/41] crypto/qat: use contiguous allocation for DMA memory
Quoted 16 lines
-----Original Message-----
From: Burakov, Anatoly
Sent: Saturday, March 3, 2018 1:46 PM
To: [email protected]
Cc: Griffin, John <[email protected]>; Trahe, Fiona <[email protected]>; Jain, Deepak K <[email protected]>; Wiles, Keith <[email protected]>; Tan, Jianfeng <[email protected]>; [email protected]; [email protected]; Walker, Benjamin <[email protected]>; Richardson, Bruce <[email protected]>; [email protected]; Ananyev, Konstantin <[email protected]>; Ramakrishnan, Kuralamudhan <[email protected]>; Daly, Louise M <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH 32/41]crypto/qat: use contiguous allocation for DMA memory
Quoted 1 line
Signed-off-by Anatoly Burakov <[email protected]>
PATCH 33/41 net/avf: use contiguous allocation for DMA memory
In reply to Anatoly Burakov--- drivers/net/avf/avf_ethdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/avf/avf_ethdev.c +1 -1
@@ -1365,7 +1365,7 @@ avf_allocate_dma_mem_d(__rte_unused struct avf_hw *hw,return AVF_ERR_PARAM ;snprintf (z_name,sizeof (z_name),"avf_dma_%" PRIu64,rte_rand ()); - mz =rte_memzone_reserve_bounded (z_name, size,SOCKET_ID_ANY ,0 , + mz =rte_memzone_reserve_bounded_contig (z_name, size,SOCKET_ID_ANY ,0 , alignment,RTE_PGSIZE_2M );if (!mz)return AVF_ERR_NO_MEMORY ;
Signature · 1 line
2.7.4
PATCH 34/41 net/bnx2x: use contiguous allocation for DMA memory
In reply to Anatoly Burakov--- drivers/net/bnx2x/bnx2x.c | 2 +- drivers/net/bnx2x/bnx2x_rxtx.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/net/bnx2x/bnx2x.c +1 -1
@@ -177,7 +177,7 @@ bnx2x_dma_alloc(struct bnx2x_softc *sc, size_t size, struct bnx2x_dma *dma, rte_get_timer_cycles());/* Caller must take care that strlen(mz_name) < RTE_MEMZONE_NAMESIZE */ - z =rte_memzone_reserve_aligned (mz_name, (uint64_t ) (size), + z =rte_memzone_reserve_aligned_contig (mz_name, (uint64_t )size,SOCKET_ID_ANY ,0 , align);if (z ==NULL ) {
drivers/net/bnx2x/bnx2x_rxtx.c +2 -1
@@ -26,7 +26,8 @@ ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,if (mz)return mz; -return rte_memzone_reserve_aligned (z_name, ring_size, socket_id,0 ,BNX2X_PAGE_SIZE ); +return rte_memzone_reserve_aligned_contig (z_name, ring_size, socket_id, +0 ,BNX2X_PAGE_SIZE ); } static void
Signature · 1 line
2.7.4
PATCH 35/41 net/cxgbe: use contiguous allocation for DMA memory
In reply to Anatoly Burakov---
Notes:
It is not 100% clear if this memzone is used for DMA, corrections welcome.
drivers/net/cxgbe/sge.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/cxgbe/sge.c +2 -1
@@ -1299,7 +1299,8 @@ static void *alloc_ring(size_t nelem, size_t elem_size,* handle the maximum ring size is allocated in order to allow for * resizing in later calls to the queue setup function. */ - tz =rte_memzone_reserve_aligned (z_name, len, socket_id,0 ,4096 ); + tz =rte_memzone_reserve_aligned_contig (z_name, len, socket_id,0 , +4096 );if (!tz)return NULL ;
Signature · 1 line
2.7.4
PATCH 36/41 net/ena: use contiguous allocation for DMA memory
In reply to Anatoly Burakov--- drivers/net/ena/base/ena_plat_dpdk.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/ena/base/ena_plat_dpdk.h +4 -3
@@ -188,7 +188,8 @@ typedef uint64_t dma_addr_t;ENA_TOUCH (dmadev);ENA_TOUCH (handle); \snprintf (z_name,sizeof (z_name), \"ena_alloc_%d" , ena_alloc_cnt++); \ - mz =rte_memzone_reserve (z_name, size,SOCKET_ID_ANY ,0 ); \ + mz =rte_memzone_reserve_contig (z_name, \ + size,SOCKET_ID_ANY ,0 ); \memset (mz->addr,0 , size); \ virt = mz->addr; \ phys = mz->iova; \
@@ -206,7 +207,7 @@ typedef uint64_t dma_addr_t;ENA_TOUCH (dmadev);ENA_TOUCH (dev_node); \snprintf (z_name,sizeof (z_name), \"ena_alloc_%d" , ena_alloc_cnt++); \ - mz =rte_memzone_reserve (z_name, size, node,0 ); \ + mz =rte_memzone_reserve_contig (z_name, size, node,0 ); \memset (mz->addr,0 , size); \ virt = mz->addr; \ phys = mz->iova; \
@@ -219,7 +220,7 @@ typedef uint64_t dma_addr_t;ENA_TOUCH (dmadev);ENA_TOUCH (dev_node); \snprintf (z_name,sizeof (z_name), \"ena_alloc_%d" , ena_alloc_cnt++); \ - mz =rte_memzone_reserve (z_name, size, node,0 ); \ + mz =rte_memzone_reserve_contig (z_name, size, node,0 ); \memset (mz->addr,0 , size); \ virt = mz->addr; \ }while (0 )
Signature · 1 line
2.7.4
PATCH 37/41 net/enic: use contiguous allocation for DMA memory
In reply to Anatoly Burakov---
Notes:
It is not 100% clear that second call to memzone_reserve is allocating DMA memory. Corrections welcome.
drivers/net/enic/enic_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/enic/enic_main.c +2 -2
@@ -319,7 +319,7 @@ enic_alloc_consistent(void *priv, size_t size,struct enic *enic = (struct enic *)priv;struct enic_memzone_entry *mze; - rz =rte_memzone_reserve_aligned ((const char *)name, + rz =rte_memzone_reserve_aligned_contig ((const char *)name, size,SOCKET_ID_ANY ,0 ,ENIC_ALIGN );if (!rz) { pr_err("%s : Failed to allocate memory requested for %s\n",
@@ -787,7 +787,7 @@ int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,"vnic_cqmsg-%s-%d-%d" , enic->bdf_name, queue_idx, instance++); - wq->cqmsg_rz =rte_memzone_reserve_aligned ((const char *)name, + wq->cqmsg_rz =rte_memzone_reserve_aligned_contig ((const char *)name,sizeof (uint32_t ),SOCKET_ID_ANY ,0 ,ENIC_ALIGN );
Signature · 1 line
2.7.4
Re: [PATCH 37/41] net/enic: use contiguous allocation for DMA memory
Hi Anatoly,
Looks good, see inline for details.
Thanks,
John
Quoted 15 lines
-----Original Message-----
From: Anatoly Burakov [mailto:[email protected]]
Sent: Saturday, March 03, 2018 5:46 AM
To: [email protected]
Cc: John Daley (johndale) <[email protected]>; Hyong Youb Kim (hyonkim) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH 37/41] net/enic: use contiguous allocation for DMA memory
Quoted 6 lines
Signed-off-by Anatoly Burakov <[email protected]>---
Notes:
It is not 100% clear that second call tomemzone_reserveis allocating DMA memory. Corrections welcome.
The 2nd call is allocating DMA memory so I believe your patch is correct.
Quoted 27 lines
drivers/net/enic/enic_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/enic/enic_main.c index renamed from drivers/net/enic/enic_main.c +0
-0ec9d343..cb2a7ba 100644
drivers/net/enic/enic_main.c +1
-1@@ -319,7 +319,7 @@ enic_alloc_consistent(void *priv, size_t size,struct enic *enic = (struct enic *)priv;struct enic_memzone_entry *mze; - rz =rte_memzone_reserve_aligned ((const char *)name, + rz =rte_memzone_reserve_aligned_contig ((const char *)name, size,SOCKET_ID_ANY ,0 ,ENIC_ALIGN );if (!rz) {drivers/net/enic/enic_main.c +1
-1pr_err("%s : Failed to allocate memory requested for %s\n",@@ -787,7 +787,7 @@ int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,"vnic_cqmsg-%s-%d-%d" , enic->bdf_name, queue_idx, instance++); - wq->cqmsg_rz = rte_memzone_reserve_aligned((const char *)name, + wq->cqmsg_rz = rte_memzone_reserve_aligned_contig((const char *)name, sizeof(uint32_t), SOCKET_ID_ANY, 0,
Quoted 1 line
ENIC_ALIGN);
This is a send completion landing spot which is DMA'd to by the NIC so it does have to be contiguous. However the size is only 4 bytes so it might not matter.
Quoted 2 lines
--
2.7.4
PATCH 38/41 net/i40e: use contiguous allocation for DMA memory
In reply to Anatoly Burakov---
Notes:
It is not 100% clear that all users of this function need to allocate DMA memory. Corrections welcome.
drivers/net/i40e/i40e_ethdev.c | 2 +- drivers/net/i40e/i40e_rxtx.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/i40e/i40e_ethdev.c +1 -1
@@ -4010,7 +4010,7 @@ i40e_allocate_dma_mem_d(__attribute__((unused)) struct i40e_hw *hw,return I40E_ERR_PARAM ;snprintf (z_name,sizeof (z_name),"i40e_dma_%" PRIu64,rte_rand ()); - mz =rte_memzone_reserve_bounded (z_name, size,SOCKET_ID_ANY ,0 , + mz =rte_memzone_reserve_bounded_contig (z_name, size,SOCKET_ID_ANY ,0 , alignment,RTE_PGSIZE_2M );if (!mz)return I40E_ERR_NO_MEMORY ;
drivers/net/i40e/i40e_rxtx.c +1 -1
@@ -2189,7 +2189,7 @@ i40e_memzone_reserve(const char *name, uint32_t len, int socket_id)if (mz)return mz; - mz =rte_memzone_reserve_aligned (name, len, + mz =rte_memzone_reserve_aligned_contig (name, len, socket_id,0 ,I40E_RING_BASE_ALIGN );return mz; }
Signature · 1 line
2.7.4
PATCH 39/41 net/qede: use contiguous allocation for DMA memory
In reply to Anatoly Burakov---
Notes:
Doing "grep -R rte_memzone_reserve drivers/net/qede" returns the following:
drivers/net/qede/qede_fdir.c: mz = rte_memzone_reserve_aligned(mz_name, QEDE_MAX_FDIR_PKT_LEN,
drivers/net/qede/base/bcm_osal.c: mz = rte_memzone_reserve_aligned_contig(mz_name, size, drivers/net/qede/base/bcm_osal.c: mz = rte_memzone_reserve_aligned_contig(mz_name, size, socket_id, 0,
I took a brief look at memzone in qede_fdir and it didn't look like memzone was used for DMA, so i left it alone. Corrections welcome.
drivers/net/qede/base/bcm_osal.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/qede/base/bcm_osal.c +3 -2
@@ -135,7 +135,7 @@ void *osal_dma_alloc_coherent(struct ecore_dev *p_dev,if (core_id == (unsigned int )LCORE_ID_ANY ) core_id =0 ; socket_id =rte_lcore_to_socket_id (core_id); - mz =rte_memzone_reserve_aligned (mz_name, size, + mz =rte_memzone_reserve_aligned_contig (mz_name, size, socket_id,0 ,RTE_CACHE_LINE_SIZE );if (!mz) { DP_ERR(p_dev, "Unable to allocate DMA memory "
@@ -174,7 +174,8 @@ void *osal_dma_alloc_coherent_aligned(struct ecore_dev *p_dev,if (core_id == (unsigned int )LCORE_ID_ANY ) core_id =0 ; socket_id =rte_lcore_to_socket_id (core_id); - mz =rte_memzone_reserve_aligned (mz_name, size, socket_id,0 , align); + mz =rte_memzone_reserve_aligned_contig (mz_name, size, socket_id,0 , + align);if (!mz) { DP_ERR(p_dev, "Unable to allocate DMA memory " "of size %zu bytes - %s\n",
Signature · 1 line
2.7.4
PATCH 40/41 net/virtio: use contiguous allocation for DMA memory
In reply to Anatoly Burakov---
Notes:
Not sure if virtio needs to allocate DMA-capable memory, being a software driver and all. Corrections welcome.
drivers/net/virtio/virtio_ethdev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/virtio/virtio_ethdev.c +4 -4
@@ -391,7 +391,7 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)PMD_INIT_LOG (DEBUG ,"vring_size: %d, rounded_vring_size: %d" , size, vq->vq_ring_size); - mz =rte_memzone_reserve_aligned (vq_name, vq->vq_ring_size, + mz =rte_memzone_reserve_aligned_contig (vq_name, vq->vq_ring_size,SOCKET_ID_ANY ,0 ,VIRTIO_PCI_VRING_ALIGN );if (mz ==NULL ) {
@@ -417,9 +417,9 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)if (sz_hdr_mz) {snprintf (vq_hdr_name,sizeof (vq_hdr_name),"port%d_vq%d_hdr" , dev->data->port_id, vtpci_queue_idx); - hdr_mz =rte_memzone_reserve_aligned (vq_hdr_name, sz_hdr_mz, -SOCKET_ID_ANY ,0 , -RTE_CACHE_LINE_SIZE ); + hdr_mz =rte_memzone_reserve_aligned_contig (vq_hdr_name, + sz_hdr_mz,SOCKET_ID_ANY ,0 , +RTE_CACHE_LINE_SIZE );if (hdr_mz ==NULL ) {if (rte_errno ==EEXIST ) hdr_mz =rte_memzone_lookup (vq_hdr_name);
Signature · 1 line
2.7.4
Re: [PATCH 40/41] net/virtio: use contiguous allocation for DMA memory
Quoted 6 lines · Anatoly Burakov
Signed-off-by Anatoly Burakov <[email protected]>---
Notes:
Not sure if virtio needs to allocate DMA-capable memory, being a software driver and all. Corrections welcome.
1. The first change is correct - virtio rings need to be contiguous in guest physical address
space.
2. The second change - virtio_tx_region contains both a virtio_net_hdr and indirect table.
virtio devices require virtio_net_hdr to be contiguous (in pre-1.0
devices w/o F_ANY_LAYOUT)
but do not require the indirect table to be contiguous w/virtio_net_hdr. You may want this to
avoid splitting up the structure though.
HTH,
-- vs;
PATCH 41/41 net/vmxnet3: use contiguous allocation for DMA memory
In reply to Anatoly Burakov---
Notes:
Not sure if DMA-capable memzones are needed for vmxnet3. Corrections welcome.
drivers/net/vmxnet3/vmxnet3_ethdev.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/vmxnet3/vmxnet3_ethdev.c +4 -3
@@ -150,14 +150,15 @@ gpa_zone_reserve(struct rte_eth_dev *dev, uint32_t size,if (!reuse) {if (mz)rte_memzone_free (mz); -return rte_memzone_reserve_aligned (z_name, size, socket_id, -0 , align); +return rte_memzone_reserve_aligned_contig (z_name, size, + socket_id,0 , align); }if (mz)return mz; -return rte_memzone_reserve_aligned (z_name, size, socket_id,0 , align); +return rte_memzone_reserve_aligned_contig (z_name, size, socket_id,0 , + align); }/**
Signature · 1 line
2.7.4
Quoted 2 lines · Anatoly Burakov
This patchset introduces dynamic memory allocation for DPDK (aka memory hotplug). Based upon RFC submitted in December [1].
For those testing this patch, there's a deadlock-at-startup issue when DPDK is started with no memory. This will be fixed in v2 (as well as dependent IPC patches), but for now the workaround is to start DPDK with -m/--socket-mem switches.
Signature · 2 lines
Thanks, Anatoly
Hi Anatoly,
I am trying to run some test with this series, but it seems to be based on some other commits of yours. I have already identified the following one [1] it seems I am missing some others.
It is possible to have a list of commits to apply on the current master branch [2] before this series?
Thanks,
[1] https://dpdk.org/patch/35043
[2] https://dpdk.org/browse/dpdk/commit/?id=c06ddf9698e0c2a9653cfa971f9ddc205065662c
Signature · 2 lines
Nélio Laranjeiro 6WIND
Quoted 6 lines · Nélio Laranjeiro
Hi Anatoly,
I am trying to run some test with this series, but it seems to be based on some other commits of yours. I have already identified the following one [1] it seems I am missing some others.
Quoted 8 lines
It is possible to have a list of commits to apply on the current master branch [2] before this series?
Thanks,
[1] https://dpdk.org/patch/35043
[2] https://dpdk.org/browse/dpdk/commit/?id=c06ddf9698e0c2a9653cfa971f9ddc205065662c
Hi Nelio,
Yes, my apologies. I'm aware of the apply issues. The issue is due to me missing a rebase on one of the dependent patchsets. I'm preparing a v2 that will fix the issue (pending some internal processes).
Signature · 2 lines
Thanks, Anatoly
Quoted 12 lines · Anatoly
Quoted 11 lines · Nélio Laranjeiro
Hi Anatoly,
I am trying to run some test with this series, but it seems to be based on some other commits of yours. I have already identified the following one [1] it seems I am missing some others.
It is possible to have a list of commits to apply on the current master branch [2] before this series?
Thanks,
Quoted 9 lines
Quoted 4 lines
[1] https://dpdk.org/patch/35043
[2] https://dpdk.org/browse/dpdk/commit/?id=c06ddf9698e0c2a9653cfa971f9ddc205065662cHi Nelio,
Yes, my apologies. I'm aware of the apply issues. The issue is due to me missing a rebase on one of the dependent patchsets. I'm preparing a v2 that will fix the issue (pending some internal processes).
Hi Nelio,
The v2 is now up, with corrected rebase. You can see the list of dependent patches in the cover letter [1]. Once again apologies for incorrect rebase in v1. Looking forward to your feedback!
[1] http://dpdk.org/ml/archives/dev/2018-March/092070.html
Signature · 2 lines
Thanks, Anatoly
Quoted 25 lines · Anatoly
Quoted 17 lines · Anatoly
Quoted 6 lines · Nélio Laranjeiro
Hi Anatoly,
I am trying to run some test with this series, but it seems to be based on some other commits of yours. I have already identified the following one [1] it seems I am missing some others.
Quoted 9 lines
It is possible to have a list of commits to apply on the current master branch [2] before this series?
Thanks,
[1] https://dpdk.org/patch/35043
[2] https://dpdk.org/browse/dpdk/commit/?id=c06ddf9698e0c2a9653cfa971f9ddc205065662cQuoted 6 lines
Hi Nelio,
Yes, my apologies. I'm aware of the apply issues. The issue is due to me missing a rebase on one of the dependent patchsets. I'm preparing a v2 that will fix the issue (pending some internal processes).
Quoted 7 lines
Hi Nelio,
The v2 is now up, with corrected rebase. You can see the list of dependent patches in the cover letter [1]. Once again apologies for incorrect rebase in v1. Looking forward to your feedback!
Hi Anatoly,
First feedbacks, I have some issue when compiling it on desktop/server machine with clang and GCC, maybe due some different configuration items depending on the machine compile it.
Clang error
-----------
dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:590:6: error: variable 'have_numa' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
if (!hi) {
^~~
CC eal_lcore.o
dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:6: note: uninitialized use occurs here
if (have_numa)
^~~~~~~~~
dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:590:2: note: remove the 'if' if its condition is always false
if (!hi) {
^~~~~~~~~~
dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:580:6: error: variable 'have_numa' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
if (internal_config.legacy_mem)
^~~~~~~~~~~~~~~~~~~~~~~~~~
dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:6: note: uninitialized use occurs here
if (have_numa)
^~~~~~~~~
dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:580:2: note: remove the 'if' if its condition is always false
if (internal_config.legacy_mem)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:573:16: note: initialize the variable 'have_numa' to silence this warning
bool have_numa;
^
= falseGCC errors
----------
/root/dpdk/lib/librte_eal/common/eal_common_memzone.c: In function ‘rte_memzone_free’:
/root/dpdk/lib/librte_eal/common/eal_common_memzone.c:355:2: error: ‘addr’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
rte_free(addr);
^~~~~~~~~~~~~~
/root/dpdk/lib/librte_eal/linuxapp/eal/eal_memalloc.c: In function ‘eal_memalloc_alloc_page_bulk’:
/root/dpdk/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:5: error: ‘have_numa’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (have_numa)
^
cc1: all warnings being treated as errors
/root/dpdk/mk/internal/rte.compile-pre.mk:114: recipe for target 'eal_common_memzone.o' failed
make[5]: *** [eal_common_memzone.o] Error 1
make[5]: *** Waiting for unfinished jobs....
cc1: all warnings being treated as errors
/root/dpdk/mk/internal/rte.compile-pre.mk:114: recipe for target 'eal_memalloc.o' failed
make[5]: *** [eal_memalloc.o] Error 1
/root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c: In function ‘rte_eal_hugepage_attach’:
/root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1556:7: error: ‘max_seg’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (cur_seg >= max_seg)
^
/root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1391:24: note: ‘max_seg’ was declared here
unsigned int cur_seg, max_seg;
^~~~~~~
/root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1389:15: error: ‘i’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
unsigned int i;
^It worse to ask Thomas to have a dedicated repository/branch on DPDK, otherwise it will be a nightmare for anyone who want to test if we need each time to apply 54 patches.
Can you see it with him?
Thanks,
Signature · 2 lines
Nélio Laranjeiro 6WIND
Quoted 96 lines · Nélio Laranjeiro
Quoted 25 lines · Anatoly
Quoted 17 lines · Anatoly
Quoted 6 lines · Nélio Laranjeiro
Hi Anatoly,
I am trying to run some test with this series, but it seems to be based on some other commits of yours. I have already identified the following one [1] it seems I am missing some others.
Quoted 9 lines
It is possible to have a list of commits to apply on the current master branch [2] before this series?
Thanks,
[1] https://dpdk.org/patch/35043
[2] https://dpdk.org/browse/dpdk/commit/?id=c06ddf9698e0c2a9653cfa971f9ddc205065662cQuoted 6 lines
Hi Nelio,
Yes, my apologies. I'm aware of the apply issues. The issue is due to me missing a rebase on one of the dependent patchsets. I'm preparing a v2 that will fix the issue (pending some internal processes).
Quoted 7 lines
Hi Nelio,
The v2 is now up, with corrected rebase. You can see the list of dependent patches in the cover letter [1]. Once again apologies for incorrect rebase in v1. Looking forward to your feedback!
Hi Anatoly,
First feedbacks, I have some issue when compiling it on desktop/server machine with clang and GCC, maybe due some different configuration items depending on the machine compile it.
Clang error
-----------dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:590:6: error: variable 'have_numa' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized] if (!hi) { ^~~ CC eal_lcore.o dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:6: note: uninitialized use occurs here if (have_numa) ^~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:590:2: note: remove the 'if' if its condition is always false if (!hi) { ^~~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:580:6: error: variable 'have_numa' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized] if (internal_config.legacy_mem) ^~~~~~~~~~~~~~~~~~~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:6: note: uninitialized use occurs here if (have_numa) ^~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:580:2: note: remove the 'if' if its condition is always false if (internal_config.legacy_mem) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:573:16: note: initialize the variable 'have_numa' to silence this warning bool have_numa; ^ = falseGCC errors
----------
/root/dpdk/lib/librte_eal/common/eal_common_memzone.c: In function ‘rte_memzone_free’:
/root/dpdk/lib/librte_eal/common/eal_common_memzone.c:355:2: error: ‘addr’ may be used uninitialized in this function [-Werror=maybe-uninitialized]rte_free(addr); ^~~~~~~~~~~~~~ /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memalloc.c: In function ‘eal_memalloc_alloc_page_bulk’: /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:5: error: ‘have_numa’ may be used uninitialized in this function [-Werror=maybe-uninitialized] if (have_numa) ^ cc1: all warnings being treated as errors /root/dpdk/mk/internal/rte.compile-pre.mk:114: recipe for target 'eal_common_memzone.o' failed make[5]: *** [eal_common_memzone.o] Error 1 make[5]: *** Waiting for unfinished jobs.... cc1: all warnings being treated as errors /root/dpdk/mk/internal/rte.compile-pre.mk:114: recipe for target 'eal_memalloc.o' failed make[5]: *** [eal_memalloc.o] Error 1 /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c: In function ‘rte_eal_hugepage_attach’: /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1556:7: error: ‘max_seg’ may be used uninitialized in this function [-Werror=maybe-uninitialized] if (cur_seg >= max_seg) ^ /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1391:24: note: ‘max_seg’ was declared here unsigned int cur_seg, max_seg; ^~~~~~~ /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1389:15: error: ‘i’ may be used uninitialized in this function [-Werror=maybe-uninitialized] unsigned int i; ^
Quoted 8 lines
It worse to ask Thomas to have a dedicated repository/branch on DPDK, otherwise it will be a nightmare for anyone who want to test if we need each time to apply 54 patches.
Can you see it with him?
Thanks,
Hi Nelio,
Thanks for your feedback.
We're working on merging dependencies into the main tree. I've spoken with Thomas about this, and he suggested to create a GitHub repo for this patchset, so i'll be looking into this as well.
Signature · 2 lines
Thanks, Anatoly
Quoted 105 lines · Anatoly
Quoted 95 lines · Nélio Laranjeiro
Quoted 25 lines · Anatoly
Quoted 17 lines · Anatoly
Quoted 6 lines · Nélio Laranjeiro
Hi Anatoly,
I am trying to run some test with this series, but it seems to be based on some other commits of yours. I have already identified the following one [1] it seems I am missing some others.
Quoted 9 lines
It is possible to have a list of commits to apply on the current master branch [2] before this series?
Thanks,
[1] https://dpdk.org/patch/35043
[2] https://dpdk.org/browse/dpdk/commit/?id=c06ddf9698e0c2a9653cfa971f9ddc205065662cQuoted 6 lines
Hi Nelio,
Yes, my apologies. I'm aware of the apply issues. The issue is due to me missing a rebase on one of the dependent patchsets. I'm preparing a v2 that will fix the issue (pending some internal processes).
Quoted 7 lines
Hi Nelio,
The v2 is now up, with corrected rebase. You can see the list of dependent patches in the cover letter [1]. Once again apologies for incorrect rebase in v1. Looking forward to your feedback!
Hi Anatoly,
First feedbacks, I have some issue when compiling it on desktop/server machine with clang and GCC, maybe due some different configuration items depending on the machine compile it.
Clang error
-----------dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:590:6: error: variable 'have_numa' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized] if (!hi) { ^~~ CC eal_lcore.o dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:6: note: uninitialized use occurs here if (have_numa) ^~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:590:2: note: remove the 'if' if its condition is always false if (!hi) { ^~~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:580:6: error: variable 'have_numa' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized] if (internal_config.legacy_mem) ^~~~~~~~~~~~~~~~~~~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:6: note: uninitialized use occurs here if (have_numa) ^~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:580:2: note: remove the 'if' if its condition is always false if (internal_config.legacy_mem) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ dpdk.org/lib/librte_eal/linuxapp/eal/eal_memalloc.c:573:16: note: initialize the variable 'have_numa' to silence this warning bool have_numa; ^ = falseGCC errors
----------
/root/dpdk/lib/librte_eal/common/eal_common_memzone.c: In function ‘rte_memzone_free’:
/root/dpdk/lib/librte_eal/common/eal_common_memzone.c:355:2: error: ‘addr’ may be used uninitialized in this function [-Werror=maybe-uninitialized]rte_free(addr); ^~~~~~~~~~~~~~ /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memalloc.c: In function ‘eal_memalloc_alloc_page_bulk’: /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memalloc.c:693:5: error: ‘have_numa’ may be used uninitialized in this function [-Werror=maybe-uninitialized] if (have_numa) ^ cc1: all warnings being treated as errors /root/dpdk/mk/internal/rte.compile-pre.mk:114: recipe for target 'eal_common_memzone.o' failed make[5]: *** [eal_common_memzone.o] Error 1 make[5]: *** Waiting for unfinished jobs.... cc1: all warnings being treated as errors /root/dpdk/mk/internal/rte.compile-pre.mk:114: recipe for target 'eal_memalloc.o' failed make[5]: *** [eal_memalloc.o] Error 1 /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c: In function ‘rte_eal_hugepage_attach’: /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1556:7: error: ‘max_seg’ may be used uninitialized in this function [-Werror=maybe-uninitialized] if (cur_seg >= max_seg) ^ /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1391:24: note: ‘max_seg’ was declared here unsigned int cur_seg, max_seg; ^~~~~~~ /root/dpdk/lib/librte_eal/linuxapp/eal/eal_memory.c:1389:15: error: ‘i’ may be used uninitialized in this function [-Werror=maybe-uninitialized] unsigned int i; ^Quoted 8 lines
It worse to ask Thomas to have a dedicated repository/branch on DPDK, otherwise it will be a nightmare for anyone who want to test if we need each time to apply 54 patches.
Can you see it with him?
Thanks,
Quoted 8 lines
Hi Nelio,
Thanks for your feedback.
We're working on merging dependencies into the main tree. I've spoken with Thomas about this, and he suggested to create a GitHub repo for this patchset, so i'll be looking into this as well.
I think some of the dependent patches are already acked, so perhaps they could be applied to the main tree soon? That would a) help test them and
b) make the life easier of everyone testing this big memory rework set. I still think we have a big issue with all patches being applied in a "big bang" near the end of the release cycle.
/Bruce
Quoted 6 lines · Nélio Laranjeiro
Hi Anatoly,
I am trying to run some test with this series, but it seems to be based on some other commits of yours. I have already identified the following one [1] it seems I am missing some others.
Quoted 8 lines
It is possible to have a list of commits to apply on the current master branch [2] before this series?
Thanks,
[1] https://dpdk.org/patch/35043
[2] https://dpdk.org/browse/dpdk/commit/?id=c06ddf9698e0c2a9653cfa971f9ddc205065662c
Also, the cover letter you're responding to, lists dependent patches as well :) it's just that current patchset does not apply cleanly atop of them due to rebase errors from my side.
Signature · 2 lines
Thanks, Anatoly
v2 cover Memory Hotplug for DPDK
In reply to Anatoly BurakovThis patchset introduces dynamic memory allocation for DPDK (aka memory hotplug). Based upon RFC submitted in December [1].
Dependencies (to be applied in specified order):
- IPC bugfixes patchset [2]
- IPC improvements patchset [3]
- IPC asynchronous request API patch [4]
- Function to return number of sockets [5]
Deprecation notices relevant to this patchset:
- General outline of memory hotplug changes [6]
- EAL NUMA node count changes [7]
The vast majority of changes are in the EAL and malloc, the external API disruption is minimal: a new set of API's are added for contiguous memory allocation for rte_memzone, and a few API additions in rte_memory due to switch to memseg_lists as opposed to memsegs. Every other API change is internal to EAL, and all of the memory allocation/freeing is handled through rte_malloc, with no externally visible API changes.
Quick outline of all changes done as part of this patchset:
* Malloc heap adjusted to handle holes in address space
* Single memseg list replaced by multiple memseg lists
* VA space for hugepages is preallocated in advance
* Added alloc/free for pages happening as needed on rte_malloc/rte_free
* Added contiguous memory allocation API's for rte_memzone
* Integrated Pawel Wodkowski's patch for registering/unregistering memory with VFIO [8]
* Callbacks for registering memory allocations
* Multiprocess support done via DPDK IPC introduced in 18.02
The biggest difference is a "memseg" now represents a single page (as opposed to being a big contiguous block of pages). As a consequence, both memzones and malloc elements are no longer guaranteed to be physically contiguous, unless the user asks for it at reserve time. To preserve whatever functionality that was dependent on previous behavior, a legacy memory option is also provided, however it is expected (or perhaps vainly hoped) to be temporary solution.
Why multiple memseg lists instead of one? Since memseg is a single page now, the list of memsegs will get quite big, and we need to locate pages somehow when we allocate and free them. We could of course just walk the list and allocate one contiguous chunk of VA space for memsegs, but this implementation uses separate lists instead in order to speed up many operations with memseg lists.
For v1 and v2, the following limitations are present:
- FreeBSD does not even compile, let alone run
- No 32-bit support
- There are some minor quality-of-life improvements planned that aren't ready yet and will be part of v3
- VFIO support is only smoke-tested (but is expected to work), VFIO support with secondary processes is not tested; work is ongoing to validate VFIO for all use cases
- Dynamic mapping/unmapping memory with VFIO is not supported in sPAPR IOMMU mode - help from sPAPR maintainers requested
Nevertheless, this patchset should be testable under 64-bit Linux, and should work for all use cases bar those mentioned above.
v2: - fixed deadlock at init
- reverted rte_panic changes at init, this is now handled inside IPC
[1] http://dpdk.org/dev/patchwork/bundle/aburakov/Memory_RFC/
[2] http://dpdk.org/dev/patchwork/bundle/aburakov/IPC_Fixes/
[3] http://dpdk.org/dev/patchwork/bundle/aburakov/IPC_Improvements/
[4] http://dpdk.org/dev/patchwork/bundle/aburakov/IPC_Async_Request/
[5] http://dpdk.org/dev/patchwork/bundle/aburakov/Num_Sockets/
[6] http://dpdk.org/dev/patchwork/patch/34002/
[7] http://dpdk.org/dev/patchwork/patch/33853/
[8] http://dpdk.org/dev/patchwork/patch/24484/
Anatoly Burakov (41):
eal: move get_virtual_area out of linuxapp eal_memory.c
eal: move all locking to heap
eal: make malloc heap a doubly-linked list
eal: add function to dump malloc heap contents
test: add command to dump malloc heap contents
eal: make malloc_elem_join_adjacent_free public
eal: make malloc free list remove public
eal: make malloc free return resulting malloc element
eal: add rte_fbarray
eal: add "single file segments" command-line option
eal: add "legacy memory" option
eal: read hugepage counts from node-specific sysfs path
eal: replace memseg with memseg lists
eal: add support for mapping hugepages at runtime
eal: add support for unmapping pages at runtime
eal: make use of memory hotplug for init
eal: enable memory hotplug support in rte_malloc
test: fix malloc autotest to support memory hotplug
eal: add API to check if memory is contiguous
eal: add backend support for contiguous allocation
eal: enable reserving physically contiguous memzones
eal: replace memzone array with fbarray
mempool: add support for the new allocation methods
vfio: allow to map other memory regions
eal: map/unmap memory with VFIO when alloc/free pages
eal: prepare memseg lists for multiprocess sync
eal: add multiprocess init with memory hotplug
eal: add support for multiprocess memory hotplug
eal: add support for callbacks on memory hotplug
eal: enable callbacks on malloc/free and mp sync
ethdev: use contiguous allocation for DMA memory crypto/qat: use contiguous allocation for DMA memory net/avf: use contiguous allocation for DMA memory net/bnx2x: use contiguous allocation for DMA memory net/cxgbe: use contiguous allocation for DMA memory net/ena: use contiguous allocation for DMA memory net/enic: use contiguous allocation for DMA memory net/i40e: use contiguous allocation for DMA memory net/qede: use contiguous allocation for DMA memory net/virtio: use contiguous allocation for DMA memory net/vmxnet3: use contiguous allocation for DMA memory
config/common_base | 15 +- drivers/bus/pci/linux/pci.c | 29 +- drivers/crypto/qat/qat_qp.c | 4 +- drivers/net/avf/avf_ethdev.c | 2 +- drivers/net/bnx2x/bnx2x.c | 2 +- drivers/net/bnx2x/bnx2x_rxtx.c | 3 +- drivers/net/cxgbe/sge.c | 3 +- drivers/net/ena/base/ena_plat_dpdk.h | 7 +- drivers/net/ena/ena_ethdev.c | 10 +- drivers/net/enic/enic_main.c | 4 +- drivers/net/i40e/i40e_ethdev.c | 2 +- drivers/net/i40e/i40e_rxtx.c | 2 +- drivers/net/qede/base/bcm_osal.c | 5 +- drivers/net/virtio/virtio_ethdev.c | 8 +- drivers/net/virtio/virtio_user/vhost_kernel.c | 108 ++- drivers/net/vmxnet3/vmxnet3_ethdev.c | 7 +- lib/librte_eal/bsdapp/eal/Makefile | 4 + lib/librte_eal/bsdapp/eal/eal.c | 25 + lib/librte_eal/bsdapp/eal/eal_hugepage_info.c | 7 + lib/librte_eal/bsdapp/eal/eal_memalloc.c | 33 + lib/librte_eal/bsdapp/eal/meson.build | 1 + lib/librte_eal/common/Makefile | 2 +- lib/librte_eal/common/eal_common_fbarray.c | 859 +++++++++++++++++ lib/librte_eal/common/eal_common_memalloc.c | 181 ++++ lib/librte_eal/common/eal_common_memory.c | 526 ++++++++++- lib/librte_eal/common/eal_common_memzone.c | 275 ++++-- lib/librte_eal/common/eal_common_options.c | 8 + lib/librte_eal/common/eal_filesystem.h | 13 + lib/librte_eal/common/eal_hugepages.h | 7 + lib/librte_eal/common/eal_internal_cfg.h | 10 +- lib/librte_eal/common/eal_memalloc.h | 41 + lib/librte_eal/common/eal_options.h | 4 + lib/librte_eal/common/eal_private.h | 33 + lib/librte_eal/common/include/rte_eal_memconfig.h | 29 +- lib/librte_eal/common/include/rte_fbarray.h | 352 +++++++ lib/librte_eal/common/include/rte_malloc.h | 9 + lib/librte_eal/common/include/rte_malloc_heap.h | 6 + lib/librte_eal/common/include/rte_memory.h | 79 +- lib/librte_eal/common/include/rte_memzone.h | 155 ++- lib/librte_eal/common/include/rte_vfio.h | 39 + lib/librte_eal/common/malloc_elem.c | 436 +++++++-- lib/librte_eal/common/malloc_elem.h | 41 +- lib/librte_eal/common/malloc_heap.c | 699 +++++++++++++- lib/librte_eal/common/malloc_heap.h | 15 +- lib/librte_eal/common/malloc_mp.c | 723 ++++++++++++++ lib/librte_eal/common/malloc_mp.h | 86 ++ lib/librte_eal/common/meson.build | 4 + lib/librte_eal/common/rte_malloc.c | 75 +- lib/librte_eal/linuxapp/eal/Makefile | 5 + lib/librte_eal/linuxapp/eal/eal.c | 54 +- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 155 ++- lib/librte_eal/linuxapp/eal/eal_memalloc.c | 1049 +++++++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_memory.c | 516 ++++++---- lib/librte_eal/linuxapp/eal/eal_vfio.c | 318 +++++-- lib/librte_eal/linuxapp/eal/eal_vfio.h | 11 + lib/librte_eal/linuxapp/eal/meson.build | 1 + lib/librte_eal/rte_eal_version.map | 23 +- lib/librte_ether/rte_ethdev.c | 3 +- lib/librte_mempool/rte_mempool.c | 87 +- test/test/commands.c | 3 + test/test/test_malloc.c | 71 +- test/test/test_memory.c | 43 +- test/test/test_memzone.c | 26 +- 63 files changed, 6617 insertions(+), 736 deletions(-) create mode 100644 lib/librte_eal/bsdapp/eal/eal_memalloc.c create mode 100644 lib/librte_eal/common/eal_common_fbarray.c create mode 100644 lib/librte_eal/common/eal_common_memalloc.c create mode 100644 lib/librte_eal/common/eal_memalloc.h create mode 100644 lib/librte_eal/common/include/rte_fbarray.h create mode 100644 lib/librte_eal/common/malloc_mp.c create mode 100644 lib/librte_eal/common/malloc_mp.h create mode 100644 lib/librte_eal/linuxapp/eal/eal_memalloc.c
Signature · 1 line
2.7.4
PATCH v2 1/41 eal: move get_virtual_area out of linuxapp eal_memory.c
Move get_virtual_area out of linuxapp EAL memory and make it common to EAL, so that other code could reserve virtual areas as well.
--- lib/librte_eal/common/eal_common_memory.c | 101 ++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 33 +++++++ lib/librte_eal/linuxapp/eal/eal_memory.c | 137 ++++++------------------------ 3 files changed, 161 insertions(+), 110 deletions(-)
lib/librte_eal/common/eal_common_memory.c +101 -0
@@ -2,10 +2,12 @@* Copyright(c) 2010-2014 Intel Corporation */ +#include <errno.h> #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <stdarg.h> +#include <string.h> #include <unistd.h> #include <inttypes.h> #include <sys/mman.h>
@@ -14,12 +16,111 @@#include <rte_memory.h> #include <rte_eal.h> #include <rte_eal_memconfig.h> +#include <rte_errno.h> #include <rte_log.h> #include "eal_private.h" #include "eal_internal_cfg.h" /* +* Try to mmap *size bytes in /dev/zero. If it is successful, return the +* pointer to the mmap'd area and keep *size unmodified. Else, retry +* with a smaller zone: decrease *size by hugepage_sz until it reaches +* 0. In this case, return NULL. Note: this function returns an address +* which is a multiple of hugepage size. +*/ + +static uint64_t baseaddr_offset; +static uint64_t system_page_sz; + +void * +eal_get_virtual_area (void *requested_addr,uint64_t *size, +uint64_t page_sz,int flags,int mmap_flags) +{ +bool addr_is_hint, allow_shrink, unmap, no_align; +uint64_t map_sz; +void *mapped_addr, *aligned_addr; + +if (system_page_sz ==0 ) + system_page_sz =sysconf (_SC_PAGESIZE); + + mmap_flags |=MAP_PRIVATE |MAP_ANONYMOUS ; + +RTE_LOG (DEBUG ,EAL ,"Ask a virtual area of 0x%zx bytes\n" , *size); + + addr_is_hint = (flags &EAL_VIRTUAL_AREA_ADDR_IS_HINT ) >0 ; + allow_shrink = (flags &EAL_VIRTUAL_AREA_ALLOW_SHRINK ) >0 ; + unmap = (flags &EAL_VIRTUAL_AREA_UNMAP ) >0 ; + +if (requested_addr ==NULL && internal_config.base_virtaddr !=0 ) { + requested_addr = (void *) (internal_config.base_virtaddr + + baseaddr_offset); + requested_addr =RTE_PTR_ALIGN (requested_addr, page_sz); + addr_is_hint = true; + } + +/* if requested address is not aligned by page size, or if requested +* address is NULL, add page size to requested length as we may get an +* address that's aligned by system page size, which can be smaller than +* our requested page size. additionally, we shouldn't try to align if +* system page size is the same as requested page size. +*/ + no_align = (requested_addr !=NULL && + ((uintptr_t )requested_addr & (page_sz -1 )) ==0 ) || + page_sz == system_page_sz; + +do { + map_sz = no_align ? *size : *size + page_sz; + + mapped_addr =mmap (requested_addr, map_sz,PROT_READ , + mmap_flags,-1 ,0 ); +if (mapped_addr ==MAP_FAILED && allow_shrink) + *size -= page_sz; + }while (allow_shrink && mapped_addr ==MAP_FAILED && *size >0 ); + +/* align resulting address - if map failed, we will ignore the value +* anyway, so no need to add additional checks. +*/ + aligned_addr = no_align ? mapped_addr : +RTE_PTR_ALIGN (mapped_addr, page_sz); + +if (*size ==0 ) { +RTE_LOG (ERR ,EAL ,"Cannot get a virtual area of any size: %s\n" , +strerror (errno)); + rte_errno = errno; +return NULL ; + }else if (mapped_addr ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL ,"Cannot get a virtual area: %s\n" , +strerror (errno)); +/* pass errno up the call chain */ + rte_errno = errno; +return NULL ; + }else if (requested_addr !=NULL && !addr_is_hint && + aligned_addr != requested_addr) { +RTE_LOG (ERR ,EAL ,"Cannot get a virtual area at requested address: %p (got %p)\n" , + requested_addr, aligned_addr); +munmap (mapped_addr, map_sz); + rte_errno =EADDRNOTAVAIL ; +return NULL ; + }else if (requested_addr !=NULL && addr_is_hint && + aligned_addr != requested_addr) { +RTE_LOG (WARNING ,EAL ,"WARNING! Base virtual address hint (%p != %p) not respected!\n" , + requested_addr, aligned_addr); +RTE_LOG (WARNING ,EAL ," This may cause issues with mapping memory into secondary processes\n" ); + } + +if (unmap) +munmap (mapped_addr, map_sz); + +RTE_LOG (DEBUG ,EAL ,"Virtual area found at %p (size = 0x%zx)\n" , + aligned_addr, *size); + + baseaddr_offset += *size; + +return aligned_addr; +} + +/* * Return a pointer to a read-only table of struct rte_physmem_desc * elements, containing the layout of all addressable physical * memory. The last element of the table contains a NULL address.
lib/librte_eal/common/eal_private.h +33 -0
@@ -127,6 +127,39 @@ int rte_eal_alarm_init(void);int rte_eal_check_module (const char *module_name);/** +* Get virtual area of specified size from the OS. +* +* This function is private to the EAL. +* +* @param requested_addr +* Address where to request address space. +* @param size +* Size of requested area. +* @param page_sz +* Page size on which to align requested virtual area. +* @param flags +* EAL_VIRTUAL_AREA_* flags. +* @param mmap_flags +* Extra flags passed directly to mmap(). +* +* @return +* Virtual area address if successful. +* NULL if unsuccessful. +*/ + +#define EAL_VIRTUAL_AREA_ADDR_IS_HINT (1 << 0) +/**< don't fail if cannot get exact requested address. */ +#define EAL_VIRTUAL_AREA_ALLOW_SHRINK (1 << 1) +/**< try getting smaller sized (decrement by page size) virtual areas if cannot +* get area of requested size. +*/ +#define EAL_VIRTUAL_AREA_UNMAP (1 << 2) +/**< immediately unmap reserved virtual area. */ +void * +eal_get_virtual_area (void *requested_addr,uint64_t *size, +uint64_t page_sz,int flags,int mmap_flags); + +/** * Get cpu core_id. * * This function is private to the EAL.
lib/librte_eal/linuxapp/eal/eal_memory.c +27 -110
204 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 2/41 eal: move all locking to heap
In reply to Anatoly BurakovDown the line, we will need to do everything from the heap as any alloc or free may trigger alloc/free OS memory, which would involve growing/shrinking heap.
--- lib/librte_eal/common/malloc_elem.c | 16 ++-------------- lib/librte_eal/common/malloc_heap.c | 38 +++++++++++++++++++++++++++++++++++++ lib/librte_eal/common/malloc_heap.h | 6 ++++++ lib/librte_eal/common/rte_malloc.c | 4 ++-- 4 files changed, 48 insertions(+), 16 deletions(-)
lib/librte_eal/common/malloc_elem.c +2 -14
@@ -243,10 +243,6 @@ join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)int malloc_elem_free (struct malloc_elem *elem) { -if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY ) -return -1 ; - -rte_spinlock_lock (&(elem->heap->lock));size_t sz = elem->size -sizeof (*elem) -MALLOC_ELEM_TRAILER_LEN ;uint8_t *ptr = (uint8_t *)&elem[1 ];struct malloc_elem *next =RTE_PTR_ADD (elem, elem->size);
@@ -274,8 +270,6 @@ malloc_elem_free(struct malloc_elem *elem)memset (ptr,0 , sz); -rte_spinlock_unlock (&(elem->heap->lock)); -return 0 ; }
@@ -292,11 +286,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)return 0 ;struct malloc_elem *next =RTE_PTR_ADD (elem, elem->size); -rte_spinlock_lock (&elem->heap->lock);if (next ->state !=ELEM_FREE ) - goto err_return; +return -1 ;if (elem->size + next->size < new_size) - goto err_return; +return -1 ;/* we now know the element fits, so remove from free list, * join the two
@@ -311,10 +304,5 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } -rte_spinlock_unlock (&elem->heap->lock);return 0 ; - -err_return: -rte_spinlock_unlock (&elem->heap->lock); -return -1 ; }
lib/librte_eal/common/malloc_heap.c +38 -0
@@ -145,6 +145,44 @@ malloc_heap_alloc(struct malloc_heap *heap,return elem ==NULL ?NULL : (void *)(&elem[1 ]); } +int +malloc_heap_free (struct malloc_elem *elem) +{ +struct malloc_heap *heap; +int 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; + +rte_spinlock_lock (&(heap->lock)); + + ret =malloc_elem_free (elem); + +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 */
lib/librte_eal/common/malloc_heap.h +6 -0
@@ -28,6 +28,12 @@ malloc_heap_alloc(struct malloc_heap *heap, const char *type, size_t size, unsigned flags, size_t align, size_t bound);int +malloc_heap_free (struct malloc_elem *elem); + +int +malloc_heap_resize (struct malloc_elem *elem,size_t size); + +int malloc_heap_get_stats (struct malloc_heap *heap,struct rte_malloc_socket_stats *socket_stats);
lib/librte_eal/common/rte_malloc.c +2 -2
@@ -29,7 +29,7 @@void rte_free (void *addr) {if (addr ==NULL )return ; -if (malloc_elem_free (malloc_elem_from_data (addr)) <0 ) +if (malloc_heap_free (malloc_elem_from_data (addr)) <0 )rte_panic ("Fatal error: Invalid memory\n" ); }
@@ -140,7 +140,7 @@ rte_realloc(void *ptr, size_t size, unsigned align) size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check alignment matches first, and if ok, see if we can resize block */ if (RTE_PTR_ALIGN (ptr,align) == ptr && -malloc_elem_resize (elem, size) ==0 ) +malloc_heap_resize (elem, size) ==0 )return ptr;/* either alignment is off, or we have no room to expand,
Signature · 1 line
2.7.4
PATCH v2 3/41 eal: make malloc heap a doubly-linked list
In reply to Anatoly BurakovAs we are preparing for dynamic memory allocation, we need to be able to handle holes in our malloc heap, hence we're switching to doubly linked list, and prepare infrastructure to support it.
Since our heap is now aware where are our first and last elements, there is no longer any need to have a dummy element at the end of each heap, so get rid of that as well. Instead, let insert/remove/ join/split operations handle end-of-list conditions automatically.
--- lib/librte_eal/common/include/rte_malloc_heap.h | 6 + lib/librte_eal/common/malloc_elem.c | 200 +++++++++++++++++++----- lib/librte_eal/common/malloc_elem.h | 14 +- lib/librte_eal/common/malloc_heap.c | 8 +- 4 files changed, 179 insertions(+), 49 deletions(-)
lib/librte_eal/common/include/rte_malloc_heap.h +6 -0
@@ -13,12 +13,18 @@/* Number of free lists per heap, grouped by size. */ #define RTE_HEAP_NUM_FREELISTS 13 +/* dummy definition, for pointers */ +struct malloc_elem ; +/** * Structure to hold malloc heap */ struct malloc_heap {rte_spinlock_t lock; LIST_HEAD(, malloc_elem) free_head[RTE_HEAP_NUM_FREELISTS]; +struct malloc_elem *first; +struct malloc_elem *last; +unsigned alloc_count;size_t total_size; } __rte_cache_aligned;
lib/librte_eal/common/malloc_elem.c +164 -36
271 lines not on this page · raw message
lib/librte_eal/common/malloc_elem.h +7 -7
@@ -18,8 +18,12 @@ enum elem_state {struct malloc_elem {struct malloc_heap *heap; -struct malloc_elem *volatile prev;/* points to prev elem in memseg */ -LIST_ENTRY (malloc_elem) free_list;/* list of free elements in heap */ +struct malloc_elem *volatile prev; +/**< points to prev elem in memseg */ +struct malloc_elem *volatile next; +/**< points to next elem in memseg */ +LIST_ENTRY (malloc_elem) free_list; +/**< list of free elements in heap */ const struct rte_memseg *ms;volatile enum elem_state state;uint32_t pad;
@@ -110,12 +114,8 @@ malloc_elem_init(struct malloc_elem *elem,const struct rte_memseg *ms, size_t size); -/* -* initialise a dummy malloc_elem header for the end-of-memseg marker -*/ void -malloc_elem_mkend (struct malloc_elem *elem, -struct malloc_elem *prev_free); +malloc_elem_insert (struct malloc_elem *elem);/* * return true if the current malloc_elem can hold a block of data
lib/librte_eal/common/malloc_heap.c +2 -6
@@ -70,15 +70,11 @@ check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)static void malloc_heap_add_memseg (struct malloc_heap *heap,struct rte_memseg *ms) { -/* allocate the memory block headers, one at end, one at start */ struct malloc_elem *start_elem = (struct malloc_elem *)ms->addr; -struct malloc_elem *end_elem =RTE_PTR_ADD (ms->addr, - ms->len -MALLOC_ELEM_OVERHEAD ); - end_elem =RTE_PTR_ALIGN_FLOOR (end_elem,RTE_CACHE_LINE_SIZE ); -const size_t elem_size = (uintptr_t )end_elem - (uintptr_t )start_elem; +const size_t elem_size = ms->len -MALLOC_ELEM_OVERHEAD ;malloc_elem_init (start_elem, heap, ms, elem_size); -malloc_elem_mkend (end_elem, start_elem); +malloc_elem_insert (start_elem);malloc_elem_free_list_insert (start_elem); heap->total_size += elem_size;
Signature · 1 line
2.7.4
PATCH v2 4/41 eal: add function to dump malloc heap contents
In reply to Anatoly BurakovMalloc heap is now a doubly linked list, so it's now possible to iterate over each malloc element regardless of its state.
--- lib/librte_eal/common/include/rte_malloc.h | 9 +++++++++ lib/librte_eal/common/malloc_elem.c | 24 ++++++++++++++++++++++++ lib/librte_eal/common/malloc_elem.h | 6 ++++++ lib/librte_eal/common/malloc_heap.c | 22 ++++++++++++++++++++++ lib/librte_eal/common/malloc_heap.h | 3 +++ lib/librte_eal/common/rte_malloc.c | 16 ++++++++++++++++ lib/librte_eal/rte_eal_version.map | 1 + 7 files changed, 81 insertions(+)
lib/librte_eal/common/include/rte_malloc.h +9 -0
@@ -278,6 +278,15 @@ void rte_malloc_dump_stats(FILE *f, const char *type);/** +* Dump contents of all malloc heaps to a file. +* +* @param f +* A pointer to a file for output +*/ +void +rte_malloc_dump_heaps (FILE *f); + +/** * Set the maximum amount of allocated memory for this type. * * This is not yet implemented
lib/librte_eal/common/malloc_elem.c +24 -0
@@ -1,6 +1,7 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2010-2014 Intel Corporation */ +#include <inttypes.h> #include <stdint.h> #include <stddef.h> #include <stdio.h>
@@ -434,3 +435,26 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size) }return 0 ; } + +static inline const char * +elem_state_to_str (enum elem_state state) +{ +switch (state) { +case ELEM_PAD : +return "PAD" ; +case ELEM_BUSY : +return "BUSY" ; +case ELEM_FREE : +return "FREE" ; + } +return "ERROR" ; +} + +void +malloc_elem_dump (const struct malloc_elem *elem,FILE *f) +{ +fprintf (f,"Malloc element at %p (%s)\n" , elem, +elem_state_to_str (elem->state)); +fprintf (f," len: 0x%zx pad: 0x%" PRIx32"\n" , elem->size, elem->pad); +fprintf (f," prev: %p next: %p\n" , elem->prev, elem->next); +}
lib/librte_eal/common/malloc_elem.h +6 -0
@@ -149,6 +149,12 @@ int malloc_elem_resize(struct malloc_elem *elem, size_t size);/* +* dump contents of malloc elem to a file. +*/ +void +malloc_elem_dump (const struct malloc_elem *elem,FILE *f); + +/* * Given an element size, compute its freelist index. */ size_t
lib/librte_eal/common/malloc_heap.c +22 -0
@@ -217,6 +217,28 @@ malloc_heap_get_stats(struct malloc_heap *heap,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 ) {
lib/librte_eal/common/malloc_heap.h +3 -0
@@ -37,6 +37,9 @@ int malloc_heap_get_stats(struct malloc_heap *heap, struct rte_malloc_socket_stats *socket_stats); +void +malloc_heap_dump (struct malloc_heap *heap,FILE *f); +int rte_eal_malloc_heap_init (void );
lib/librte_eal/common/rte_malloc.c +16 -0
@@ -182,6 +182,22 @@ rte_malloc_get_socket_stats(int socket, }/* +* Function to dump contents of all heaps +*/ +void +rte_malloc_dump_heaps (FILE *f) +{ +struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config; +unsigned int socket; + +for (socket =0 ; socket <rte_num_sockets (); socket++) { +fprintf (f,"Heap on socket %i:\n" , socket); +malloc_heap_dump (&mcfg->malloc_heaps[socket], f); + } + +} + +/* * Print stats on memory type. If type is NULL, info on all types is printed */ void
lib/librte_eal/rte_eal_version.map +1 -0
@@ -215,6 +215,7 @@ DPDK_18.05 { global: rte_num_sockets; + rte_malloc_dump_heaps; } DPDK_18.02;
Signature · 1 line
2.7.4
PATCH v2 5/41 test: add command to dump malloc heap contents
In reply to Anatoly Burakov--- test/test/commands.c | 3 +++ 1 file changed, 3 insertions(+)
test/test/commands.c +3 -0
@@ -137,6 +137,8 @@ static void cmd_dump_parsed(void *parsed_result,rte_log_dump (stdout); else if (!strcmp(res->dump, "dump_malloc_stats"))rte_malloc_dump_stats (stdout,NULL ); + else if (!strcmp(res->dump, "dump_malloc_heaps")) +rte_malloc_dump_heaps (stdout); } cmdline_parse_token_string_t cmd_dump_dump =
@@ -147,6 +149,7 @@ cmdline_parse_token_string_t cmd_dump_dump ="dump_ring#" "dump_mempool#" "dump_malloc_stats#" +"dump_malloc_heaps#" "dump_devargs#" "dump_log_types");
Signature · 1 line
2.7.4
PATCH v2 6/41 eal: make malloc_elem_join_adjacent_free public
In reply to Anatoly BurakovWe need this function to join newly allocated segments with the heap.
--- lib/librte_eal/common/malloc_elem.c | 6 +++--- lib/librte_eal/common/malloc_elem.h | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-)
lib/librte_eal/common/malloc_elem.c +3 -3
@@ -325,8 +325,8 @@ join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2) elem1->next = next; } -static struct malloc_elem * -elem_join_adjacent_free (struct malloc_elem *elem) +struct malloc_elem * +malloc_elem_join_adjacent_free (struct malloc_elem *elem) {/* * check if next element exists, is adjacent and is free, if so join
@@ -388,7 +388,7 @@ malloc_elem_free(struct malloc_elem *elem) ptr =RTE_PTR_ADD (elem,sizeof (*elem)); data_len = elem->size -MALLOC_ELEM_OVERHEAD ; - elem =elem_join_adjacent_free (elem); + elem =malloc_elem_join_adjacent_free (elem);malloc_elem_free_list_insert (elem);
lib/librte_eal/common/malloc_elem.h +3 -0
@@ -141,6 +141,9 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size,int malloc_elem_free (struct malloc_elem *elem); +struct malloc_elem * +malloc_elem_join_adjacent_free (struct malloc_elem *elem); +/* * attempt to resize a malloc_elem by expanding into any free space * immediately after it in memory.
Signature · 1 line
2.7.4
PATCH v2 7/41 eal: make malloc free list remove public
In reply to Anatoly Burakov--- lib/librte_eal/common/malloc_elem.c | 12 ++++++------ lib/librte_eal/common/malloc_elem.h | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-)
lib/librte_eal/common/malloc_elem.c +6 -6
@@ -245,8 +245,8 @@ malloc_elem_free_list_insert(struct malloc_elem *elem)/* * Remove the specified element from its heap's free list. */ -static void -elem_free_list_remove (struct malloc_elem *elem) +void +malloc_elem_free_list_remove (struct malloc_elem *elem) {LIST_REMOVE (elem, free_list); }
@@ -266,7 +266,7 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,const size_t trailer_size = elem->size - old_elem_size - size -MALLOC_ELEM_OVERHEAD ; -elem_free_list_remove (elem); +malloc_elem_free_list_remove (elem);if (trailer_size >MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) {/* split it, too much free space after elem */
@@ -340,7 +340,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) erase =RTE_PTR_SUB (elem->next,MALLOC_ELEM_TRAILER_LEN );/* remove from free list, join to this one */ -elem_free_list_remove (elem->next); +malloc_elem_free_list_remove (elem->next);join_elem (elem, elem->next);/* erase header and trailer */
@@ -360,7 +360,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) erase =RTE_PTR_SUB (elem,MALLOC_ELEM_TRAILER_LEN );/* remove from free list, join to this one */ -elem_free_list_remove (elem->prev); +malloc_elem_free_list_remove (elem->prev); new_elem = elem->prev;join_elem (new_elem, elem);
@@ -423,7 +423,7 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* we now know the element fits, so remove from free list, * join the two */ -elem_free_list_remove (elem->next); +malloc_elem_free_list_remove (elem->next);join_elem (elem, elem->next);if (elem->size - new_size >=MIN_DATA_SIZE +MALLOC_ELEM_OVERHEAD ) {
lib/librte_eal/common/malloc_elem.h +3 -0
@@ -151,6 +151,9 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem);int malloc_elem_resize (struct malloc_elem *elem,size_t size); +void +malloc_elem_free_list_remove (struct malloc_elem *elem); +/* * dump contents of malloc elem to a file. */
Signature · 1 line
2.7.4
PATCH v2 8/41 eal: make malloc free return resulting malloc element
In reply to Anatoly Burakov--- lib/librte_eal/common/malloc_elem.c | 4 ++-- lib/librte_eal/common/malloc_elem.h | 2 +- lib/librte_eal/common/malloc_heap.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
lib/librte_eal/common/malloc_elem.c +2 -2
@@ -379,7 +379,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem)* blocks either immediately before or immediately after newly freed block * are also free, the blocks are merged together. */ -int +struct malloc_elem *malloc_elem_free (struct malloc_elem *elem) {void *ptr;
@@ -397,7 +397,7 @@ malloc_elem_free(struct malloc_elem *elem)memset (ptr,0 , data_len); -return 0 ; +return elem; }/*
lib/librte_eal/common/malloc_elem.h +1 -1
@@ -138,7 +138,7 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size,* blocks either immediately before or immediately after newly freed block * are also free, the blocks are merged together. */ -int +struct malloc_elem *malloc_elem_free (struct malloc_elem *elem); struct malloc_elem *
lib/librte_eal/common/malloc_heap.c +2 -2
@@ -145,7 +145,7 @@ int malloc_heap_free(struct malloc_elem *elem) {struct malloc_heap *heap; -int ret; +struct malloc_elem *ret;if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ;
@@ -159,7 +159,7 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_unlock (&(heap->lock)); -return ret; +return ret !=NULL ?0 :-1 ; }int
Signature · 1 line
2.7.4
PATCH v2 9/41 eal: add rte_fbarray
In reply to Anatoly Burakovrte_fbarray is a simple indexed array stored in shared memory via mapping files into memory. Rationale for its existence is the
following: since we are going to map memory page-by-page, there could be quite a lot of memory segments to keep track of (for smaller page sizes, page count can easily reach thousands). We can't really make page lists truly dynamic and infinitely expandable, because that involves reallocating memory (which is a big no-no in multiprocess). What we can do instead is have a maximum capacity as something really, really large, and decide at allocation time how big the array is going to be. We map the entire file into memory, which makes it possible to use fbarray as shared memory, provided the structure itself is allocated in shared memory. Per-fbarray locking is also used to avoid index data races (but not contents data races - that is up to user application to synchronize).
In addition, in understanding that we will frequently need to scan this array for free space and iterating over array linearly can become slow, rte_fbarray provides facilities to index array's usage. The following use cases are covered:
- find next free/used slot (useful either for adding new elements to fbarray, or walking the list)
- find starting index for next N free/used slots (useful for when we want to allocate chunk of VA-contiguous memory composed of several pages)
- find how many contiguous free/used slots there are, starting from specified index (useful for when we want to figure out how many pages we have until next hole in allocated memory, to speed up some bulk operations where we would otherwise have to walk the array and add pages one by one)
This is accomplished by storing a usage mask in-memory, right after the data section of the array, and using some bit-level magic to figure out the info we need.
---
Notes:
Initial version of this had resizing capability, however it was removed due to the fact that in multiprocess scenario, each fbarray would have its own view of mapped memory, which might not correspond with others due to some other process performing a resize that current process didn't know about.
It was therefore decided that to avoid cost of synchronization on each and every operation (to make sure the array wasn't resized), resizing feature should be dropped.
lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/common/Makefile | 2 +- lib/librte_eal/common/eal_common_fbarray.c | 859 ++++++++++++++++++++++++++++ lib/librte_eal/common/eal_filesystem.h | 13 + lib/librte_eal/common/include/rte_fbarray.h | 352 ++++++++++++ lib/librte_eal/common/meson.build | 2 + lib/librte_eal/linuxapp/eal/Makefile | 1 + lib/librte_eal/rte_eal_version.map | 17 + 8 files changed, 1246 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/eal_common_fbarray.c create mode 100644 lib/librte_eal/common/include/rte_fbarray.h
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -53,6 +53,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_dev.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_options.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_proc.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_heap.c
lib/librte_eal/common/Makefile +1 -1
@@ -16,7 +16,7 @@ INC += rte_pci_dev_feature_defs.h rte_pci_dev_features.h INC += rte_malloc.h rte_keepalive.h rte_time.h INC += rte_service.h rte_service_component.h INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h -INC += rte_reciprocal.h +INC += rte_reciprocal.h rte_fbarray.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h
lib/librte_eal/common/eal_common_fbarray.c new file +859 -0
859 lines not on this page · raw message
lib/librte_eal/common/eal_filesystem.h +13 -0
@@ -13,6 +13,7 @@/** Path of rte config file. */ #define RUNTIME_CONFIG_FMT "%s/.%s_config" +#define FBARRAY_FMT "%s/%s_%s"#include <stdint.h> #include <limits.h>
@@ -55,6 +56,18 @@ eal_mp_socket_path(void)return buffer; } +static inline const char * +eal_get_fbarray_path (char *buffer,size_t buflen,const char *name) { +const char *directory ="/tmp" ; +const char *home_dir =getenv ("HOME" ); + +if (getuid () !=0 && home_dir !=NULL ) + directory = home_dir; +snprintf (buffer, buflen -1 ,FBARRAY_FMT , directory, + internal_config.hugefile_prefix, name); +return buffer; +} +/** Path of hugepage info file. */ #define HUGEPAGE_INFO_FMT "%s/.%s_hugepage_info"
lib/librte_eal/common/include/rte_fbarray.h new file +352 -0
352 lines not on this page · raw message
lib/librte_eal/common/meson.build +2 -0
@@ -11,6 +11,7 @@ common_sources = files( 'eal_common_devargs.c', 'eal_common_dev.c', 'eal_common_errno.c', + 'eal_common_fbarray.c', 'eal_common_hexdump.c', 'eal_common_launch.c', 'eal_common_lcore.c',
@@ -51,6 +52,7 @@ common_headers = files( 'include/rte_eal_memconfig.h', 'include/rte_eal_interrupts.h', 'include/rte_errno.h', + 'include/rte_fbarray.h', 'include/rte_hexdump.h', 'include/rte_interrupts.h', 'include/rte_keepalive.h',
lib/librte_eal/linuxapp/eal/Makefile +1 -0
@@ -61,6 +61,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_dev.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_options.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_proc.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_heap.c
lib/librte_eal/rte_eal_version.map +17 -0
@@ -216,6 +216,23 @@ DPDK_18.05 { rte_num_sockets; rte_malloc_dump_heaps; + rte_fbarray_init; + rte_fbarray_destroy; + rte_fbarray_attach; + rte_fbarray_detach; + rte_fbarray_resize; + rte_fbarray_get; + rte_fbarray_find_idx; + rte_fbarray_set_free; + rte_fbarray_set_used; + rte_fbarray_is_used; + rte_fbarray_find_next_free; + rte_fbarray_find_next_used; + rte_fbarray_find_next_n_free; + rte_fbarray_find_next_n_used; + rte_fbarray_find_contig_free; + rte_fbarray_find_contig_used; + rte_fbarray_dump_metadata; } DPDK_18.02;
Signature · 1 line
2.7.4
PATCH v2 10/41 eal: add "single file segments" command-line option
In reply to Anatoly BurakovFor now, this option does nothing, but it will be useful in dynamic memory allocation down the line. Currently, DPDK stores all pages as separate files in hugetlbfs. This option will allow storing all pages in one file (one file per socket, per page size).
--- lib/librte_eal/common/eal_common_options.c | 4 ++++ lib/librte_eal/common/eal_internal_cfg.h | 4 ++++ lib/librte_eal/common/eal_options.h | 2 ++ lib/librte_eal/linuxapp/eal/eal.c | 1 + 4 files changed, 11 insertions(+)
lib/librte_eal/common/eal_common_options.c +4 -0
@@ -73,6 +73,7 @@ eal_long_options[] = { {OPT_VDEV, 1, NULL, OPT_VDEV_NUM }, {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM }, {OPT_VMWARE_TSC_MAP, 0, NULL, OPT_VMWARE_TSC_MAP_NUM }, + {OPT_SINGLE_FILE_SEGMENTS, 0, NULL, OPT_SINGLE_FILE_SEGMENTS_NUM}, {0, 0, NULL, 0 } };
@@ -1161,6 +1162,9 @@ eal_parse_common_option(int opt, const char *optarg, core_parsed =LCORE_OPT_MAP ;break ; +case OPT_SINGLE_FILE_SEGMENTS_NUM : + conf->single_file_segments =1 ; +break ;/* don't know what to do, leave this to caller */ default :
lib/librte_eal/common/eal_internal_cfg.h +4 -0
@@ -47,6 +47,10 @@ struct internal_config {volatile unsigned force_sockets;volatile uint64_t socket_mem[RTE_MAX_NUMA_NODES ];/**< amount of memory per socket */ uintptr_t base_virtaddr;/**< base address to try and reserve memory from */ +volatile unsigned single_file_segments; +/**< true if storing all pages within single files (per-page-size, +* per-node). +*/ volatile int syslog_facility;/**< facility passed to openlog() */ /** default interrupt mode for VFIO */ volatile enum rte_intr_mode vfio_intr_mode;
lib/librte_eal/common/eal_options.h +2 -0
@@ -55,6 +55,8 @@ enum {OPT_VFIO_INTR_NUM , #define OPT_VMWARE_TSC_MAP "vmware-tsc-map"OPT_VMWARE_TSC_MAP_NUM , +#define OPT_SINGLE_FILE_SEGMENTS "single-file-segments" +OPT_SINGLE_FILE_SEGMENTS_NUM , OPT_LONG_MAX_NUM };
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -348,6 +348,7 @@ eal_usage(const char *prgname)" --" OPT_BASE_VIRTADDR " Base virtual address\n" " --" OPT_CREATE_UIO_DEV " Create /dev/uioX (usually done by hotplug)\n" " --" OPT_VFIO_INTR " Interrupt mode for VFIO (legacy|msi|msix)\n" +" --" OPT_SINGLE_FILE_SEGMENTS " Put all hugepage memory in single files\n" "\n");/* Allow the application to print its usage message too if hook is set */ if ( rte_application_usage_hook ) {
Signature · 1 line
2.7.4
PATCH v2 11/41 eal: add "legacy memory" option
In reply to Anatoly BurakovThis adds a "--legacy-mem" command-line switch. It will be used to go back to the old memory behavior, one where we can't dynamically allocate/free memory (the downside), but one where the user can get physically contiguous memory, like before (the upside).
For now, nothing but the legacy behavior exists, non-legacy memory init sequence will be added later.
--- lib/librte_eal/bsdapp/eal/eal.c | 3 +++ lib/librte_eal/common/eal_common_options.c | 4 ++++ lib/librte_eal/common/eal_internal_cfg.h | 4 ++++ lib/librte_eal/common/eal_options.h | 2 ++ lib/librte_eal/linuxapp/eal/eal.c | 1 + lib/librte_eal/linuxapp/eal/eal_memory.c | 24 ++++++++++++++++++++---- 6 files changed, 34 insertions(+), 4 deletions(-)
lib/librte_eal/bsdapp/eal/eal.c +3 -0
@@ -531,6 +531,9 @@ rte_eal_init(int argc, char **argv)return -1 ; } +/* FreeBSD always uses legacy memory model */ + internal_config.legacy_mem = true; +if (eal_plugins_init () <0 ) {rte_eal_init_alert ("Cannot init plugins\n" ); rte_errno =EINVAL ;
lib/librte_eal/common/eal_common_options.c +4 -0
@@ -74,6 +74,7 @@ eal_long_options[] = { {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM }, {OPT_VMWARE_TSC_MAP, 0, NULL, OPT_VMWARE_TSC_MAP_NUM }, {OPT_SINGLE_FILE_SEGMENTS, 0, NULL, OPT_SINGLE_FILE_SEGMENTS_NUM}, + {OPT_LEGACY_MEM, 0, NULL, OPT_LEGACY_MEM_NUM }, {0, 0, NULL, 0 } };
@@ -1165,6 +1166,9 @@ eal_parse_common_option(int opt, const char *optarg,case OPT_SINGLE_FILE_SEGMENTS_NUM : conf->single_file_segments =1 ;break ; +case OPT_LEGACY_MEM_NUM : + conf->legacy_mem =1 ; +break ;/* don't know what to do, leave this to caller */ default :
lib/librte_eal/common/eal_internal_cfg.h +4 -0
@@ -51,6 +51,10 @@ struct internal_config {/**< true if storing all pages within single files (per-page-size, * per-node). */ +volatile unsigned legacy_mem; +/**< true to enable legacy memory behavior (no dynamic allocation, +* contiguous segments). +*/ volatile int syslog_facility;/**< facility passed to openlog() */ /** default interrupt mode for VFIO */ volatile enum rte_intr_mode vfio_intr_mode;
lib/librte_eal/common/eal_options.h +2 -0
@@ -57,6 +57,8 @@ enum {OPT_VMWARE_TSC_MAP_NUM , #define OPT_SINGLE_FILE_SEGMENTS "single-file-segments"OPT_SINGLE_FILE_SEGMENTS_NUM , +#define OPT_LEGACY_MEM "legacy-mem" +OPT_LEGACY_MEM_NUM , OPT_LONG_MAX_NUM };
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -349,6 +349,7 @@ eal_usage(const char *prgname)" --" OPT_CREATE_UIO_DEV " Create /dev/uioX (usually done by hotplug)\n" " --" OPT_VFIO_INTR " Interrupt mode for VFIO (legacy|msi|msix)\n" " --" OPT_SINGLE_FILE_SEGMENTS " Put all hugepage memory in single files\n" +" --" OPT_LEGACY_MEM " Legacy memory mode (no dynamic allocation, contiguous segments)\n" "\n");/* Allow the application to print its usage message too if hook is set */ if ( rte_application_usage_hook ) {
lib/librte_eal/linuxapp/eal/eal_memory.c +20 -4
@@ -919,8 +919,8 @@ huge_recover_sigbus(void)* 6. unmap the first mapping * 7. fill memsegs in configuration with contiguous zones */ -int -rte_eal_hugepage_init (void ) +static int +eal_legacy_hugepage_init (void ) {struct rte_mem_config *mcfg;struct hugepage_file *hugepage =NULL , *tmp_hp =NULL ;
@@ -1262,8 +1262,8 @@ getFileSize(int fd)* configuration and finds the hugepages which form that segment, mapping them * in order to form a contiguous block in the virtual memory space */ -int -rte_eal_hugepage_attach (void ) +static int +eal_legacy_hugepage_attach (void ) {const struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config;struct hugepage_file *hp =NULL ;
@@ -1399,6 +1399,22 @@ rte_eal_hugepage_attach(void) }int +rte_eal_hugepage_init (void ) +{ +if (internal_config.legacy_mem) +return eal_legacy_hugepage_init (); +return -1 ; +} + +int +rte_eal_hugepage_attach (void ) +{ +if (internal_config.legacy_mem) +return eal_legacy_hugepage_attach (); +return -1 ; +} + +int rte_eal_using_phys_addrs (void ) {return phys_addrs_available;
Signature · 1 line
2.7.4
PATCH v2 12/41 eal: read hugepage counts from node-specific sysfs path
In reply to Anatoly BurakovFor non-legacy memory init mode, instead of looking at generic sysfs path, look at sysfs paths pertaining to each NUMA node for hugepage counts. Note that per-NUMA node path does not provide information regarding reserved pages, so we might not get the best info from these paths, but this saves us from the whole mapping/remapping business before we're actually able to tell which page is on which socket, because we no longer require our memory to be physically contiguous.
Legacy memory init will not use this.
--- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 79 +++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 6 deletions(-)
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +73 -6
@@ -30,6 +30,7 @@#include "eal_filesystem.h" static const char sys_dir_path[] ="/sys/kernel/mm/hugepages" ; +static const char sys_pages_numa_dir_path[] ="/sys/devices/system/node" ;/* this function is only called from eal_hugepage_info_init which itself * is only called from a primary process */
@@ -70,6 +71,45 @@ get_num_hugepages(const char *subdir)return num_pages; } +static uint32_t +get_num_hugepages_on_node (const char *subdir,unsigned int socket) +{ +char path[PATH_MAX ], socketpath[PATH_MAX ]; +DIR *socketdir; +unsigned long num_pages =0 ; +const char *nr_hp_file ="free_hugepages" ; + +snprintf (socketpath,sizeof (socketpath),"%s/node%u/hugepages" , + sys_pages_numa_dir_path, socket); + + socketdir =opendir (socketpath); +if (socketdir) { +/* Keep calm and carry on */ +closedir (socketdir); + }else { +/* Can't find socket dir, so ignore it */ +return 0 ; + } + +snprintf (path,sizeof (path),"%s/%s/%s" , + socketpath, subdir, nr_hp_file); +if (eal_parse_sysfs_value (path, &num_pages) <0 ) +return 0 ; + +if (num_pages ==0 ) +RTE_LOG (WARNING ,EAL ,"No free hugepages reported in %s\n" , + subdir); + +/* +* we want to return a uint32_t and more than this looks suspicious +* anyway ... +*/ +if (num_pages >UINT32_MAX ) + num_pages =UINT32_MAX ; + +return num_pages; +} +static uint64_t get_default_hp_size (void ) {
@@ -248,7 +288,7 @@ eal_hugepage_info_init(void) {const char dirent_start_text[] ="hugepages-" ;const size_t dirent_start_len =sizeof (dirent_start_text) -1 ; -unsigned i, num_sizes =0 ; +unsigned int i, total_pages, num_sizes =0 ;DIR *dir;struct dirent *dirent;
@@ -302,9 +342,27 @@ eal_hugepage_info_init(void)if (clear_hugedir (hpi->hugedir) ==-1 )break ; -/* for now, put all pages into socket 0, -* later they will be sorted */ - hpi->num_pages[0 ] =get_num_hugepages (dirent->d_name); +/* +* first, try to put all hugepages into relevant sockets, but +* if first attempts fails, fall back to collecting all pages +* in one socket and sorting them later +*/ + total_pages =0 ; +/* we also don't want to do this for legacy init */ +if (!internal_config.legacy_mem) +for (i =0 ; i <rte_num_sockets (); i++) { +unsigned int num_pages = +get_num_hugepages_on_node ( + dirent->d_name, i); + hpi->num_pages[i] = num_pages; + total_pages += num_pages; + } +/* +* we failed to sort memory from the get go, so fall +* back to old way +*/ +if (total_pages ==0 ) + hpi->num_pages[0 ] =get_num_hugepages (dirent->d_name);#ifndef RTE_ARCH_64 /* for 32-bit systems, limit number of hugepages to
@@ -328,10 +386,19 @@ eal_hugepage_info_init(void) sizeof(internal_config.hugepage_info[0]), compare_hpi);/* now we have all info, check we have at least one valid size */ -for (i =0 ; i < num_sizes; i++) +for (i =0 ; i < num_sizes; i++) { +/* pages may no longer all be on socket 0, so check all */ +unsigned int j, num_pages =0 ; + +for (j =0 ; j <RTE_MAX_NUMA_NODES ; j++) { +struct hugepage_info *hpi = + &internal_config.hugepage_info[i]; + num_pages += hpi->num_pages[j]; + }if (internal_config.hugepage_info[i].hugedir !=NULL && - internal_config.hugepage_info[i].num_pages[0 ] >0 ) + num_pages >0 )return 0 ; + }/* no valid hugepage mounts available, return error */ return -1 ;
Signature · 1 line
2.7.4
PATCH v2 13/41 eal: replace memseg with memseg lists
In reply to Anatoly BurakovBefore, we were aggregating multiple pages into one memseg, so the number of memsegs was small. Now, each page gets its own memseg, so the list of memsegs is huge. To accommodate the new memseg list size and to keep the under-the-hood workings sane, the memseg list is now not just a single list, but multiple lists. To be precise, each hugepage size available on the system gets one or more memseg lists, per socket.
In order to support dynamic memory allocation, we reserve all memory in advance. As in, we do an anonymous mmap() of the entire maximum size of memory per hugepage size, per socket (which is limited to either RTE_MAX_MEMSEG_PER_TYPE pages or RTE_MAX_MEM_PER_TYPE gigabytes worth of memory, whichever is the smaller one), split over multiple lists (which are limited to either RTE_MAX_MEMSEG_PER_LIST memsegs or RTE_MAX_MEM_PER_LIST gigabytes per list, whichever is the smaller one).
So, for each hugepage size, we get (by default) up to 128G worth of memory, per socket, split into chunks of up to 32G in size. The address space is claimed at the start, in eal_common_memory.c. The actual page allocation code is in eal_memalloc.c (Linux-only for now), and largely consists of copied EAL memory init code.
Pages in the list are also indexed by address. That is, for non-legacy mode, in order to figure out where the page belongs, one can simply look at base address for a memseg list. Similarly, figuring out IOVA address of a memzone is a matter of finding the right memseg list, getting offset and dividing by page size to get the appropriate memseg. For legacy mode, old behavior of walking the memseg list remains.
Due to switch to fbarray and to avoid any intrusive changes, secondary processes are not supported in this commit. Also, one particular API call (dump physmem layout) no longer makes sense and was removed, according to deprecation notice [1].
In legacy mode, nothing is preallocated, and all memsegs are in a list like before, but each segment still resides in an appropriate memseg list.
The rest of the changes are really ripple effects from the memseg change - heap changes, compile fixes, and rewrites to support fbarray-backed memseg lists.
[1] http://dpdk.org/dev/patchwork/patch/34002/
--- config/common_base | 15 +- drivers/bus/pci/linux/pci.c | 29 +- drivers/net/virtio/virtio_user/vhost_kernel.c | 108 +++++--- lib/librte_eal/common/eal_common_memory.c | 322 +++++++++++++++++++--- lib/librte_eal/common/eal_common_memzone.c | 12 +- lib/librte_eal/common/eal_hugepages.h | 2 + lib/librte_eal/common/eal_internal_cfg.h | 2 +- lib/librte_eal/common/include/rte_eal_memconfig.h | 22 +- lib/librte_eal/common/include/rte_memory.h | 33 ++- lib/librte_eal/common/include/rte_memzone.h | 1 - lib/librte_eal/common/malloc_elem.c | 8 +- lib/librte_eal/common/malloc_elem.h | 6 +- lib/librte_eal/common/malloc_heap.c | 92 +++++-- lib/librte_eal/common/rte_malloc.c | 22 +- lib/librte_eal/linuxapp/eal/eal.c | 21 +- lib/librte_eal/linuxapp/eal/eal_memory.c | 297 +++++++++++++------- lib/librte_eal/linuxapp/eal/eal_vfio.c | 164 +++++++---- lib/librte_eal/rte_eal_version.map | 3 +- test/test/test_malloc.c | 29 +- test/test/test_memory.c | 43 ++- test/test/test_memzone.c | 17 +- 21 files changed, 917 insertions(+), 331 deletions(-)
config/common_base +14 -1
@@ -61,7 +61,20 @@ CONFIG_RTE_CACHE_LINE_SIZE=64 CONFIG_RTE_LIBRTE_EAL=y CONFIG_RTE_MAX_LCORE=128 CONFIG_RTE_MAX_NUMA_NODES=8 -CONFIG_RTE_MAX_MEMSEG=256 +CONFIG_RTE_MAX_MEMSEG_LISTS=32 +# each memseg list will be limited to either RTE_MAX_MEMSEG_PER_LIST pages +# or RTE_MAX_MEM_PER_LIST gigabytes worth of memory, whichever is the smallest +CONFIG_RTE_MAX_MEMSEG_PER_LIST=8192 +CONFIG_RTE_MAX_MEM_PER_LIST=32 +# a "type" is a combination of page size and NUMA node. total number of memseg +# lists per type will be limited to either RTE_MAX_MEMSEG_PER_TYPE pages (split +# over multiple lists of RTE_MAX_MEMSEG_PER_LIST pages), or RTE_MAX_MEM_PER_TYPE +# gigabytes of memory (split over multiple lists of RTE_MAX_MEM_PER_LIST), +# whichever is the smallest +CONFIG_RTE_MAX_MEMSEG_PER_TYPE=32768 +CONFIG_RTE_MAX_MEM_PER_TYPE=128 +# legacy mem mode only +CONFIG_RTE_MAX_LEGACY_MEMSEG=256 CONFIG_RTE_MAX_MEMZONE=2560 CONFIG_RTE_MAX_TAILQ=32 CONFIG_RTE_ENABLE_ASSERT=n
drivers/bus/pci/linux/pci.c +20 -9
@@ -119,19 +119,30 @@ rte_pci_unmap_device(struct rte_pci_device *dev)void *pci_find_max_end_va (void ) { -const struct rte_memseg *seg =rte_eal_get_physmem_layout (); -const struct rte_memseg *last = seg; -unsigned i =0 ; +void *cur_end, *max_end =NULL ; +int i =0 ; -for (i =0 ; i <RTE_MAX_MEMSEG ; i++, seg++) { -if (seg->addr ==NULL ) -break ; +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config; +struct rte_memseg_list *msl = &mcfg->memsegs[i]; +struct rte_fbarray *arr = &msl->memseg_arr; -if (seg->addr > last->addr) - last = seg; +if (arr->count ==0 ) +continue ; +/* +* we need to handle legacy mem case, so don't rely on page size +* to calculate max VA end +*/ +while ((i =rte_fbarray_find_next_used (arr, i)) >=0 ) { +struct rte_memseg *ms =rte_fbarray_get (arr, i); + cur_end =RTE_PTR_ADD (ms->addr, ms->len); +if (cur_end > max_end) + max_end = cur_end; + } } -return RTE_PTR_ADD (last->addr, last->len); +return max_end; }/* parse one line of the "resource" sysfs file (note that the 'line'
drivers/net/virtio/virtio_user/vhost_kernel.c +66 -42
132 lines not on this page · raw message
lib/librte_eal/common/eal_common_memory.c +291 -31
372 lines not on this page · raw message
lib/librte_eal/common/eal_common_memzone.c +2 -10
@@ -226,10 +226,9 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, mz->iova =rte_malloc_virt2iova (mz_addr); mz->addr = mz_addr; mz->len = (requested_len ==0 ? elem->size : requested_len); - mz->hugepage_sz = elem->ms->hugepage_sz; - mz->socket_id = elem->ms->socket_id; + mz->hugepage_sz = elem->msl->hugepage_sz; + mz->socket_id = elem->msl->socket_id; mz->flags =0 ; - mz->memseg_id = elem->ms -rte_eal_get_configuration ()->mem_config->memseg;return mz; }
@@ -382,7 +381,6 @@ intrte_eal_memzone_init (void) {struct rte_mem_config *mcfg; -const struct rte_memseg *memseg;/* get pointer to global configuration */ mcfg =rte_eal_get_configuration ()->mem_config;
@@ -391,12 +389,6 @@ rte_eal_memzone_init(void)if (rte_eal_process_type () ==RTE_PROC_SECONDARY )return 0 ; - memseg =rte_eal_get_physmem_layout (); -if (memseg ==NULL ) { -RTE_LOG (ERR ,EAL ,"%s(): Cannot get physical layout\n" , __func__); -return -1 ; - } -rte_rwlock_write_lock (&mcfg->mlock);/* delete all zones */
lib/librte_eal/common/eal_hugepages.h +2 -0
@@ -23,6 +23,8 @@ struct hugepage_file {int socket_id;/**< NUMA socket ID */ int file_id;/**< the '%d' in HUGEFILE_FMT */ int memseg_id;/**< the memory segment to which page belongs */ +int memseg_list_id; +/**< the memory segment list to which page belongs */ char filepath[MAX_HUGEPAGE_PATH ];/**< path to backing file on filesystem */ };
lib/librte_eal/common/eal_internal_cfg.h +1 -1
@@ -23,7 +23,7 @@ struct hugepage_info {uint64_t hugepage_sz;/**< size of a huge page */ const char *hugedir;/**< dir where hugetlbfs is mounted */ uint32_t num_pages[RTE_MAX_NUMA_NODES ]; -/**< number of hugepages of that size on each socket */ +/**< number of hugepages of that size on each socket */ int lock_descriptor;/**< file descriptor for hugepage dir */ };
lib/librte_eal/common/include/rte_eal_memconfig.h +21 -1
@@ -12,12 +12,30 @@#include <rte_malloc_heap.h> #include <rte_rwlock.h> #include <rte_pause.h> +#include <rte_fbarray.h> #ifdef __cplusplus extern "C" { #endif /** + * memseg list is a special case as we need to store a bunch of other data + * together with the array itself. + */ +struct rte_memseg_list { + RTE_STD_C11 + union { + void *base_va; + /**< Base virtual address for this memseg list. */ + uint64_t addr_64; + /**< Makes sure addr is always 64-bits */ + }; + int socket_id; /**< Socket ID for all memsegs in this list. */ + uint64_t hugepage_sz; /**< page size for all memsegs in this list. */ + struct rte_fbarray memseg_arr; +}; + +/** * the structure for the memory configuration for the RTE. * Used by the rte_config structure. It is separated out, as for multi-process * support, the memory details should be shared across instances
@@ -43,9 +61,11 @@ struct rte_mem_config {uint32_t memzone_cnt;/**< Number of allocated memzones */ /* memory segments and zones */ -struct rte_memseg memseg[RTE_MAX_MEMSEG ];/**< Physmem descriptors. */ struct rte_memzone memzone[RTE_MAX_MEMZONE ];/**< Memzone descriptors. */ +struct rte_memseg_list memsegs[RTE_MAX_MEMSEG_LISTS ]; +/**< list of dynamic arrays holding memsegs */ +struct rte_tailq_head tailq_head[RTE_MAX_TAILQ ];/**< Tailqs for objects */ /* Heaps of Malloc per socket */
lib/librte_eal/common/include/rte_memory.h +21 -12
@@ -22,6 +22,9 @@ extern "C" {#include <rte_common.h> #include <rte_config.h> +/* forward declaration for pointers */ +struct rte_memseg_list ; + __extension__ enum rte_page_sizes { RTE_PGSIZE_4K = 1ULL << 12,
@@ -130,21 +133,27 @@ phys_addr_t rte_mem_virt2phy(const void *virt);rte_iova_t rte_mem_virt2iova (const void *virt);/** -* Get the layout of the available physical memory. +* Get memseg corresponding to virtual memory address. * -* It can be useful for an application to have the full physical -* memory layout to decide the size of a memory zone to reserve. This -* table is stored in rte_config (see rte_eal_get_configuration()). +* @param virt +* The virtual address. +* @param msl +* Memseg list in which to look for memsegs (can be NULL). +* @return +* Memseg to which this virtual address belongs to. +*/ +struct rte_memseg *rte_mem_virt2memseg (const void *virt, +const struct rte_memseg_list *msl); + +/** +* Get memseg list corresponding to virtual memory address. * +* @param virt +* The virtual address. * @return -* - On success, return a pointer to a read-only table of struct -* rte_physmem_desc elements, containing the layout of all -* addressable physical memory. The last element of the table -* contains a NULL address. -* - On error, return NULL. This should not happen since it is a fatal -* error that will probably cause the entire system to panic. -*/ -const struct rte_memseg *rte_eal_get_physmem_layout (void ); +* Memseg list to which this virtual address belongs to. +*/ +struct rte_memseg_list *rte_mem_virt2memseg_list (const void *virt);/** * Dump the physical memory layout to a file.
lib/librte_eal/common/include/rte_memzone.h +0 -1
@@ -66,7 +66,6 @@ struct rte_memzone {int32_t socket_id;/**< NUMA socket ID. */ uint32_t flags;/**< Characteristics of this memzone. */ -uint32_t memseg_id;/**< Memseg it belongs. */ } __attribute__((__packed__));/**
lib/librte_eal/common/malloc_elem.c +4 -4
@@ -26,11 +26,11 @@* Initialize a general malloc_elem header structure */ void -malloc_elem_init (struct malloc_elem *elem, -struct malloc_heap *heap,const struct rte_memseg *ms,size_t size) +malloc_elem_init (struct malloc_elem *elem,struct malloc_heap *heap, +struct rte_memseg_list *msl,size_t size) { elem->heap = heap; - elem->ms = ms; + elem->msl = msl; elem->prev =NULL ; elem->next =NULL ;memset (&elem->free_list,0 ,sizeof (elem->free_list));
@@ -145,7 +145,7 @@ split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)const size_t old_elem_size = (uintptr_t )split_pt - (uintptr_t )elem;const size_t new_elem_size = elem->size - old_elem_size; -malloc_elem_init (split_pt, elem->heap, elem->ms, new_elem_size); +malloc_elem_init (split_pt, elem->heap, elem->msl, new_elem_size); split_pt->prev = elem; split_pt->next = next_elem;if (next_elem)
lib/librte_eal/common/malloc_elem.h +3 -3
@@ -5,7 +5,7 @@#ifndef MALLOC_ELEM_H_ #define MALLOC_ELEM_H_ -#include <rte_memory.h> +#include <rte_eal_memconfig.h> /* dummy definition of struct so we can use pointers to it in malloc_elem struct */ struct malloc_heap ;
@@ -24,7 +24,7 @@ struct malloc_elem {/**< points to next elem in memseg */ LIST_ENTRY (malloc_elem) free_list;/**< list of free elements in heap */ -const struct rte_memseg *ms; +struct rte_memseg_list *msl;volatile enum elem_state state;uint32_t pad;size_t size;
@@ -111,7 +111,7 @@ malloc_elem_from_data(const void *data)void malloc_elem_init (struct malloc_elem *elem,struct malloc_heap *heap, -const struct rte_memseg *ms, +struct rte_memseg_list *msl,size_t size);void
lib/librte_eal/common/malloc_heap.c +73 -19
124 lines not on this page · raw message
lib/librte_eal/common/rte_malloc.c +13 -9
@@ -238,17 +238,21 @@ rte_malloc_set_limit(__rte_unused const char *type,rte_iova_t rte_malloc_virt2iova (const void *addr) { -rte_iova_t iova; -const struct malloc_elem *elem =malloc_elem_from_data (addr); +const struct rte_memseg *ms; +struct malloc_elem *elem =malloc_elem_from_data (addr); +if (elem ==NULL )return RTE_BAD_IOVA ; -if (elem->ms->iova ==RTE_BAD_IOVA ) -return RTE_BAD_IOVA ;if (rte_eal_iova_mode () ==RTE_IOVA_VA ) - iova = (uintptr_t )addr; -else - iova = elem->ms->iova + -RTE_PTR_DIFF (addr, elem->ms->addr); -return iova; +return (uintptr_t ) addr; + + ms =rte_mem_virt2memseg (addr, elem->msl); +if (ms ==NULL ) +return RTE_BAD_IOVA ; + +if (ms->iova ==RTE_BAD_IOVA ) +return RTE_BAD_IOVA ; + +return ms->iova +RTE_PTR_DIFF (addr, ms->addr); }
lib/librte_eal/linuxapp/eal/eal.c +12 -9
@@ -74,8 +74,8 @@ static int mem_cfg_fd = -1;static struct flock wr_lock = { .l_type =F_WRLCK , .l_whence =SEEK_SET , - .l_start = offsetof(struct rte_mem_config , memseg), - .l_len =sizeof (early_mem_config.memseg), + .l_start = offsetof(struct rte_mem_config , memsegs), + .l_len =sizeof (early_mem_config.memsegs), };/* Address of global and public configuration */
@@ -643,17 +643,20 @@ eal_parse_args(int argc, char **argv)static void eal_check_mem_on_local_socket (void ) { -const struct rte_memseg *ms; +const struct rte_memseg_list *msl;int i, socket_id; socket_id =rte_lcore_to_socket_id (rte_config.master_lcore); - ms =rte_eal_get_physmem_layout (); - -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) -if (ms[i].socket_id == socket_id && - ms[i].len >0 ) -return ; +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { + msl = &rte_eal_get_configuration ()->mem_config->memsegs[i]; +if (msl->socket_id != socket_id) +continue ; +/* for legacy memory, check if there's anything allocated */ +if (internal_config.legacy_mem && msl->memseg_arr.count ==0 ) +continue ; +return ; + }RTE_LOG (WARNING ,EAL ,"WARNING: Master core has no " "memory on local socket!\n" );
lib/librte_eal/linuxapp/eal/eal_memory.c +203 -94
385 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/eal_vfio.c +109 -55
208 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -1
@@ -25,7 +25,6 @@ DPDK_2.0 { rte_eal_devargs_type_count; rte_eal_get_configuration; rte_eal_get_lcore_state; - rte_eal_get_physmem_layout; rte_eal_get_physmem_size; rte_eal_has_hugepages; rte_eal_hpet_init;
@@ -215,6 +214,8 @@ DPDK_18.05 { global: rte_num_sockets; + rte_mem_virt2memseg; + rte_mem_virt2memseg_list; rte_malloc_dump_heaps; rte_fbarray_init; rte_fbarray_destroy;
test/test/test_malloc.c +15 -14
@@ -12,6 +12,7 @@#include <rte_common.h> #include <rte_memory.h> +#include <rte_eal_memconfig.h> #include <rte_per_lcore.h> #include <rte_launch.h> #include <rte_eal.h>
@@ -705,15 +706,23 @@ test_malloc_bad_params(void)return -1 ; } -/* Check if memory is available on a specific socket */ +/* Check if memory is avilable on a specific socket */ static int is_mem_on_socket (int32_t socket) { -const struct rte_memseg *ms =rte_eal_get_physmem_layout (); +const struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config;unsigned i; -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) { -if (socket == ms[i].socket_id) +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +const struct rte_memseg_list *msl = + &mcfg->memsegs[i]; +const struct rte_fbarray *arr = &msl->memseg_arr; + +if (msl->socket_id != socket) +continue ; + +if (arr->count)return 1 ; }return 0 ;
@@ -726,16 +735,8 @@ is_mem_on_socket(int32_t socket)static int32_t addr_to_socket (void * addr) { -const struct rte_memseg *ms =rte_eal_get_physmem_layout (); -unsigned i; - -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) { -if ((ms[i].addr <= addr) && - ((uintptr_t )addr < - ((uintptr_t )ms[i].addr + (uintptr_t )ms[i].len))) -return ms[i].socket_id; - } -return -1 ; +const struct rte_memseg *ms =rte_mem_virt2memseg (addr,NULL ); +return ms ==NULL ?-1 : ms->socket_id; }/* Test using rte_[c|m|zm]alloc_socket() on a specific socket */
test/test/test_memory.c +35 -8
73 lines not on this page · raw message
test/test/test_memzone.c +10 -7
@@ -108,22 +108,25 @@ static inttest_memzone_reserve_flags (void) {const struct rte_memzone *mz; -const struct rte_memseg *ms;int hugepage_2MB_avail =0 ;int hugepage_1GB_avail =0 ;int hugepage_16MB_avail =0 ;int hugepage_16GB_avail =0 ;const size_t size =100 ;int i =0 ; - ms =rte_eal_get_physmem_layout (); -for (i =0 ; i <RTE_MAX_MEMSEG ; i++) { -if (ms[i].hugepage_sz ==RTE_PGSIZE_2M ) + +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config; +struct rte_memseg_list *msl = &mcfg->memsegs[i]; + +if (msl->hugepage_sz ==RTE_PGSIZE_2M ) hugepage_2MB_avail =1 ; -if (ms[i].hugepage_sz ==RTE_PGSIZE_1G ) +if (msl->hugepage_sz ==RTE_PGSIZE_1G ) hugepage_1GB_avail =1 ; -if (ms[i].hugepage_sz ==RTE_PGSIZE_16M ) +if (msl->hugepage_sz ==RTE_PGSIZE_16M ) hugepage_16MB_avail =1 ; -if (ms[i].hugepage_sz ==RTE_PGSIZE_16G ) +if (msl->hugepage_sz ==RTE_PGSIZE_16G ) hugepage_16GB_avail =1 ; }/* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
Signature · 1 line
2.7.4
Re: [PATCH v2 13/41] eal: replace memseg with memseg lists
Hi Anatoly,
Quoted 40 lines · Anatoly Burakov
Before, we were aggregating multiple pages into one memseg, so the number of memsegs was small. Now, each page gets its own memseg, so the list of memsegs is huge. To accommodate the new memseg list size and to keep the under-the-hood workings sane, the memseg list is now not just a single list, but multiple lists. To be precise, each hugepage size available on the system gets one or more memseg lists, per socket.
In order to support dynamic memory allocation, we reserve all memory in advance. As in, we do an anonymous
mmap()of the entire maximum size of memory per hugepage size, per socket (which is limited to eitherRTE_MAX_MEMSEG_PER_TYPEpages orRTE_MAX_MEM_PER_TYPEgigabytes worth of memory, whichever is the smaller one), split over multiple lists (which are limited to eitherRTE_MAX_MEMSEG_PER_LISTmemsegs orRTE_MAX_MEM_PER_LISTgigabytes per list, whichever is the smaller one).So, for each hugepage size, we get (by default) up to 128G worth of memory, per socket, split into chunks of up to 32G in size. The address space is claimed at the start, in
eal_common_memory.c. The actual page allocation code is ineal_memalloc.c(Linux-only for now), and largely consists of copied EAL memory init code.Pages in the list are also indexed by address. That is, for non-legacy mode, in order to figure out where the page belongs, one can simply look at base address for a memseg list. Similarly, figuring out IOVA address of a memzone is a matter of finding the right memseg list, getting offset and dividing by page size to get the appropriate memseg. For legacy mode, old behavior of walking the memseg list remains.
Due to switch to fbarray and to avoid any intrusive changes, secondary processes are not supported in this commit. Also, one particular API call (dump physmem layout) no longer makes sense and was removed, according to deprecation notice [1].
In legacy mode, nothing is preallocated, and all memsegs are in a list like before, but each segment still resides in an appropriate memseg list.
Quoted 8 lines
The rest of the changes are really ripple effects from the memseg change - heap changes, compile fixes, and rewrites to support fbarray-backed memseg lists.
[1] http://dpdk.org/dev/patchwork/patch/34002/
Signed-off-by Anatoly Burakov <[email protected]>---
Thanks for good work!.
Few observations:
# Noticed performance regression for thunderx platform for l3fwd application, drops by 3%. git bisect shows this changeset is offending commit. I'm still investigating reason for perf-dip..
Would like to know - have you noticed any regression on x86 platform?
# In next version, pl. make sure that individual patch builds successfully. Right now, Some patches are dependent on other, leads to build break, observed while git-bisecting.
Few examples are:
Quoted 4 lines
Quoted 4 lines
fa71cdef6963ed795fdd7e7f35085170bb300e391037fcd989176c5cc83db6223534205cac469765befdec10759d30275a17a829919ee45228d91d3c495e60f4e02af8a344c0f817a60d1ee9b9322df4
[above commits are from your github repo..]
# Nits:
Perhaps you may club all below comits into one single patch,
as changes are identical... that way you'd reduce patch count by few less. 9a1e2a7bd9f6248c680ad3e444b6f173eb92d457 net/vmxnet3: use contiguous allocation for DMA memory 46388b194cd559b5cf7079e01b04bf67a99b64d7 net/virtio: use contiguous allocation for DMA memory a3d2eb10bd998ba3ae3a3d39adeaff38d2e53a9d net/qede: use contiguous allocation for DMA memory 6f16b23ef1f472db475edf05159dea5ae741dbf8 net/i40e: use contiguous allocation for DMA memory f9f7576eed35cb6aa50793810cdda43bcc0f4642 net/enic: use contiguous allocation for DMA memory 2af6c33009b8008da7028a351efed2932b1a13d0 net/ena: use contiguous allocation for DMA memory 18003e22bd7087e5e2e03543cb662d554f7bec52 net/cxgbe: use contiguous allocation for DMA memory 59f79182502dcb3634dfa3e7b918195829777460 net/bnx2x: use contiguous allocation for DMA memory f481a321e41da82ddfa00f5ddbcb42fc29e6ae76 net/avf: use contiguous allocation for DMA memory 5253e9b757c1855a296656d939f5c28e651fea69 crypto/qat: use contiguous allocation for DMA memory 297ab037b4c0d9d725aa6cfdd2c33f7cd9396899 ethdev: use contiguous allocation for DMA memory
Thanks.
Re: [PATCH v2 13/41] eal: replace memseg with memseg lists
Quoted 8 lines · santosh
Hi Anatoly,
Thanks for good work!.
Few observations:
# Noticed performance regression for thunderx platform for l3fwd application, drops by 3%. git bisect shows this changeset is offending commit. I'm still investigating reason for perf-dip..
Would like to know - have you noticed any regression on x86 platform?
I haven't noticed any regressions on x86. Would it by any chance be due to the fact that memory segments are now non-contiguous or are allocated from smaller page sizes first?
I am in the process of preparing a v3, which moves some things around and is better at git bisect (and fixes all compile issues i am or was made aware of). Does performance regression also happen in legacy mode?
Thanks for testing!
Quoted 5 lines
Perhaps you may club all below comits into one single patch,
as changes are identical... that way you'd reduce patch count by few less.9a1e2a7bd9f6248c680ad3e444b6f173eb92d457net/vmxnet3: use contiguous allocation for DMA memory46388b194cd559b5cf7079e01b04bf67a99b64d7net/virtio: use contiguous allocation for DMA memorya3d2eb10bd998ba3ae3a3d39adeaff38d2e53a9dnet/qede: use contiguous allocation for DMA memory
Quoted 8 lines
6f16b23ef1f472db475edf05159dea5ae741dbf8net/i40e: use contiguous allocation for DMA memoryf9f7576eed35cb6aa50793810cdda43bcc0f4642net/enic: use contiguous allocation for DMA memory2af6c33009b8008da7028a351efed2932b1a13d0net/ena: use contiguous allocation for DMA memory18003e22bd7087e5e2e03543cb662d554f7bec52net/cxgbe: use contiguous allocation for DMA memory59f79182502dcb3634dfa3e7b918195829777460net/bnx2x: use contiguous allocation for DMA memoryf481a321e41da82ddfa00f5ddbcb42fc29e6ae76net/avf: use contiguous allocation for DMA memory5253e9b757c1855a296656d939f5c28e651fea69crypto/qat: use contiguous allocation for DMA memory297ab037b4c0d9d725aa6cfdd2c33f7cd9396899ethdev: use contiguous allocation for DMA memory
I would like to keep these as separate patches. It makes it easier to track which changes were accepted by maintainers of respective drivers, and which weren't.
Signature · 2 lines
Thanks, Anatoly
Re: [PATCH v2 13/41] eal: replace memseg with memseg lists
Quoted 10 lines · Anatoly
Quoted 8 lines · santosh
Hi Anatoly,
Thanks for good work!.
Few observations:
# Noticed performance regression for thunderx platform for l3fwd application, drops by 3%. git bisect shows this changeset is offending commit. I'm still investigating reason for perf-dip..
Would like to know - have you noticed any regression on x86 platform?
Quoted 4 lines
I haven't noticed any regressions on x86. Would it by any chance be due to the fact that memory segments are now non-contiguous or are allocated from smaller page sizes first?
I am in the process of preparing a v3, which moves some things around and is better at git bisect (and fixes all compile issues i am or was made aware of). Does performance regression also happen in legacy mode?
Test ran for legacy mode only and noticed performance regression.
Thanks.
[..]
Re: [PATCH v2 13/41] eal: replace memseg with memseg lists
Quoted 13 lines · santosh
Quoted 10 lines · Anatoly
Quoted 8 lines · santosh
Hi Anatoly,
Thanks for good work!.
Few observations:
# Noticed performance regression for thunderx platform for l3fwd application, drops by 3%. git bisect shows this changeset is offending commit. I'm still investigating reason for perf-dip..
Would like to know - have you noticed any regression on x86 platform?
Quoted 10 lines
Quoted 4 lines
I haven't noticed any regressions on x86. Would it by any chance be due to the fact that memory segments are now non-contiguous or are allocated from smaller page sizes first?
I am in the process of preparing a v3, which moves some things around and is better at git bisect (and fixes all compile issues i am or was made aware of). Does performance regression also happen in legacy mode?
Test ran for legacy mode only and noticed performance regression.
Thanks.
[..]
Legacy mode does not do IPC memory allocation, so that is out of the question. Does thunderx do any address translation or other memory lookups on fast path? That is the only thing that comes to mind that could affect performance once all allocations are complete.
Signature · 2 lines
Thanks, Anatoly
PATCH v2 14/41 eal: add support for mapping hugepages at runtime
In reply to Anatoly BurakovNothing uses this code yet. The bulk of it is copied from old memory allocation code (linuxapp eal_memory.c). We provide an EAL-internal API to allocate either one page or multiple pages, guaranteeing that we'll get contiguous VA for all of the pages that we requested.
For single-file segments, we will use fallocate() to grow and shrink memory segments, however fallocate() is not supported on all kernel versions, so we will fall back to using ftruncate() to grow the file, and disable shrinking as there's little we can do there. This will enable vhost use cases where having single file segments is of great value even without support for hot-unplugging memory.
Not supported on FreeBSD.
Locking is done via fcntl() because that way, when it comes to taking out write locks or unlocking on deallocation, we don't have to keep original fd's around. Plus, using fcntl() gives us ability to lock parts of a file, which is useful for single-file segments.
--- lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/bsdapp/eal/eal_memalloc.c | 26 ++ lib/librte_eal/bsdapp/eal/meson.build | 1 + lib/librte_eal/common/eal_memalloc.h | 19 + lib/librte_eal/linuxapp/eal/Makefile | 2 + lib/librte_eal/linuxapp/eal/eal_memalloc.c | 609 +++++++++++++++++++++++++++++ lib/librte_eal/linuxapp/eal/meson.build | 1 + 7 files changed, 659 insertions(+) create mode 100644 lib/librte_eal/bsdapp/eal/eal_memalloc.c create mode 100644 lib/librte_eal/common/eal_memalloc.h create mode 100644 lib/librte_eal/linuxapp/eal/eal_memalloc.c
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -29,6 +29,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_memory.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_hugepage_info.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_debug.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_lcore.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_interrupts.c
lib/librte_eal/bsdapp/eal/eal_memalloc.c new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2017-2018 Intel Corporation +*/ + +#include <inttypes.h> + +#include <rte_log.h> +#include <rte_memory.h> + +#include "eal_memalloc.h" + +int +eal_memalloc_alloc_page_bulk(struct rte_memseg **ms __rte_unused, + int __rte_unused n, uint64_t __rte_unused size, + int __rte_unused socket, bool __rte_unused exact) +{ +RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" ); +return -1 ; +} + +struct rte_memseg * +eal_memalloc_alloc_page(uint64_t __rte_unused size, int __rte_unused socket) +{ +RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" ); +return NULL ; +}
lib/librte_eal/bsdapp/eal/meson.build +1 -0
@@ -8,6 +8,7 @@ env_sources = files('eal_alarm.c', 'eal_hugepage_info.c', 'eal_interrupts.c', 'eal_lcore.c', + 'eal_memalloc.c', 'eal_thread.c', 'eal_timer.c', 'eal.c',
lib/librte_eal/common/eal_memalloc.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2017-2018 Intel Corporation +*/ + +#ifndef EAL_MEMALLOC_H +#define EAL_MEMALLOC_H + +#include <stdbool.h> + +#include <rte_memory.h> + +struct rte_memseg * +eal_memalloc_alloc_page (uint64_t size,int socket); + +int +eal_memalloc_alloc_page_bulk (struct rte_memseg **ms,int n,uint64_t size, +int socket,bool exact); + +#endif // EAL_MEMALLOC_H
lib/librte_eal/linuxapp/eal/Makefile +2 -0
@@ -36,6 +36,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_thread.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_log.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_vfio.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_vfio_mp_sync.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_debug.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_lcore.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_timer.c
@@ -82,6 +83,7 @@ CFLAGS_eal_interrupts.o := -D_GNU_SOURCE CFLAGS_eal_vfio_mp_sync.o := -D_GNU_SOURCE CFLAGS_eal_timer.o := -D_GNU_SOURCE CFLAGS_eal_lcore.o := -D_GNU_SOURCE +CFLAGS_eal_memalloc.o := -D_GNU_SOURCE CFLAGS_eal_thread.o := -D_GNU_SOURCE CFLAGS_eal_log.o := -D_GNU_SOURCE CFLAGS_eal_common_log.o := -D_GNU_SOURCE
lib/librte_eal/linuxapp/eal/eal_memalloc.c new file +609 -0
609 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/meson.build +1 -0
@@ -10,6 +10,7 @@ env_sources = files('eal_alarm.c', 'eal_debug.c', 'eal_hugepage_info.c', 'eal_interrupts.c', + 'eal_memalloc.c', 'eal_lcore.c', 'eal_log.c', 'eal_thread.c',
Signature · 1 line
2.7.4
PATCH v2 15/41 eal: add support for unmapping pages at runtime
In reply to Anatoly BurakovThis isn't used anywhere yet, but the support is now there. Also, adding cleanup to allocation procedures, so that if we fail to allocate everything we asked for, we can free all of it back.
--- lib/librte_eal/common/eal_memalloc.h | 3 + lib/librte_eal/linuxapp/eal/eal_memalloc.c | 148 ++++++++++++++++++++++++++++- 2 files changed, 146 insertions(+), 5 deletions(-)
lib/librte_eal/common/eal_memalloc.h +3 -0
@@ -16,4 +16,7 @@ int eal_memalloc_alloc_page_bulk(struct rte_memseg **ms, int n, uint64_t size, int socket, bool exact); +int +eal_memalloc_free_page (struct rte_memseg *ms); +#endif // EAL_MEMALLOC_H
lib/librte_eal/linuxapp/eal/eal_memalloc.c +143 -5
190 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 16/41 eal: make use of memory hotplug for init
In reply to Anatoly BurakovAdd a new (non-legacy) memory init path for EAL. It uses the new memory hotplug facilities, although it's only being run at startup.
If no -m or --socket-mem switches were specified, the new init will not allocate anything, whereas if those switches were passed, appropriate amounts of pages would be requested, just like for legacy init.
Since rte_malloc support for dynamic allocation comes in later patches, running DPDK without --socket-mem or -m switches will fail in this patch.
Also, allocated pages will be physically discontiguous (or rather, they're not guaranteed to be physically contiguous - they may still be, by accident) unless IOVA_AS_VA mode is used.
Since memory hotplug subsystem relies on partial file locking, replace flock() locks with fcntl() locks.
---
Notes:
This commit shows "the wolrd as it could have been". All of this other monstrous amount of code in eal_memory.c is there because of legacy init option. Do we *really* want to keep it around, and make DPDK init and memory system suffer from split personality?
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 25 ++++++++- lib/librte_eal/linuxapp/eal/eal_memory.c | 74 +++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 7 deletions(-)
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +23 -2
@@ -15,6 +15,7 @@#include <unistd.h> #include <errno.h> #include <sys/queue.h> +#include <sys/stat.h> #include <rte_memory.h> #include <rte_eal.h>
@@ -200,6 +201,18 @@ get_hugepage_dir(uint64_t hugepage_sz) }/* +* uses fstat to report the size of a file on disk +*/ +static off_t +getFileSize (int fd) +{ +struct stat st; +if (fstat (fd, &st) <0 ) +return 0 ; +return st.st_size; +} + +/* * Clear the hugepage directory of whatever hugepage files * there are. Checks if the file is locked (i.e. * if it's in use by another DPDK process).
@@ -229,6 +242,8 @@ clear_hugedir(const char * hugedir) }while (dirent !=NULL ){ +struct flock lck = {0 }; +/* skip files that don't match the hugepage pattern */ if (fnmatch (filter, dirent->d_name,0 ) >0 ) { dirent =readdir (dir);
@@ -245,11 +260,17 @@ clear_hugedir(const char * hugedir) }/* non-blocking lock */ - lck_result =flock (fd,LOCK_EX |LOCK_NB ); + lck.l_type =F_RDLCK ; + lck.l_whence =SEEK_SET ; + lck.l_start =0 ; + lck.l_len =getFileSize (fd); + + lck_result =fcntl (fd,F_SETLK , &lck);/* if lock succeeds, unlock and remove the file */ if (lck_result !=-1 ) { -flock (fd,LOCK_UN ); + lck.l_type =F_UNLCK ; +fcntl (fd,F_SETLK , &lck);unlinkat (dir_fd, dirent->d_name,0 ); }close (fd);
lib/librte_eal/linuxapp/eal/eal_memory.c +69 -5
110 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 17/41 eal: enable memory hotplug support in rte_malloc
In reply to Anatoly BurakovThis set of changes enables rte_malloc to allocate and free memory as needed. The way it works is, first malloc checks if there is enough memory already allocated to satisfy user's request. If there isn't, we try and allocate more memory. The reverse happens with free - we free an element, check its size (including free element merging due to adjacency) and see if it's bigger than hugepage size and that its start and end span a hugepage or more. Then we remove the area from malloc heap (adjusting element lengths where appropriate), and deallocate the page.
For legacy mode, runtime alloc/free of pages is disabled.
It is worth noting that memseg lists are being sorted by page size, and that we try our best to satisfy user's request. That is, if the user requests an element from a 2MB page memory, we will check if we can satisfy that request from existing memory, if not we try and allocate more 2MB pages. If that fails and user also specified a "size is hint" flag, we then check other page sizes and try to allocate from there. If that fails too, then, depending on flags, we may try allocating from other sockets. In other words, we try our best to give the user what they asked for, but going to other sockets is last resort - first we try to allocate more memory on the same socket.
--- lib/librte_eal/common/eal_common_memzone.c | 23 +- lib/librte_eal/common/malloc_elem.c | 85 ++++++++ lib/librte_eal/common/malloc_elem.h | 3 + lib/librte_eal/common/malloc_heap.c | 332 ++++++++++++++++++++++++++++- lib/librte_eal/common/malloc_heap.h | 4 +- lib/librte_eal/common/rte_malloc.c | 31 +-- 6 files changed, 416 insertions(+), 62 deletions(-)
lib/librte_eal/common/eal_common_memzone.c +2 -21
@@ -103,7 +103,6 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,struct rte_memzone *mz;struct rte_mem_config *mcfg;size_t requested_len; -int socket, i;/* get pointer to global configuration */ mcfg =rte_eal_get_configuration ()->mem_config;
@@ -181,27 +180,9 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, } } -if (socket_id ==SOCKET_ID_ANY ) - socket =malloc_get_numa_socket (); -else - socket = socket_id; -/* allocate memory on heap */ -void *mz_addr =malloc_heap_alloc (&mcfg->malloc_heaps[socket],NULL , - requested_len, flags, align, bound); - -if ((mz_addr ==NULL ) && (socket_id ==SOCKET_ID_ANY )) { -/* try other heaps */ -for (i =0 ; i <RTE_MAX_NUMA_NODES ; i++) { -if (socket == i) -continue ; - - mz_addr =malloc_heap_alloc (&mcfg->malloc_heaps[i], -NULL , requested_len, flags, align, bound); -if (mz_addr !=NULL ) -break ; - } - } +void *mz_addr =malloc_heap_alloc (NULL , requested_len, socket_id, flags, + align, bound);if (mz_addr ==NULL ) { rte_errno =ENOMEM ;
lib/librte_eal/common/malloc_elem.c +85 -0
91 lines not on this page · raw message
lib/librte_eal/common/malloc_elem.h +3 -0
@@ -152,6 +152,9 @@ int malloc_elem_resize(struct malloc_elem *elem, size_t size);void +malloc_elem_hide_region (struct malloc_elem *elem,void *start,size_t len); + +void malloc_elem_free_list_remove (struct malloc_elem *elem);/*
lib/librte_eal/common/malloc_heap.c +321 -11
377 lines not on this page · raw message
lib/librte_eal/common/malloc_heap.h +2 -2
@@ -24,8 +24,8 @@ malloc_get_numa_socket(void) }void * -malloc_heap_alloc (struct malloc_heap *heap,const char *type,size_t size, -unsigned flags,size_t align,size_t bound); +malloc_heap_alloc (const char *type,size_t size,int socket,unsigned int flags, +size_t align,size_t bound);int malloc_heap_free (struct malloc_elem *elem);
lib/librte_eal/common/rte_malloc.c +3 -28
@@ -39,10 +39,6 @@ void rte_free(void *addr)void *rte_malloc_socket (const char *type,size_t size,unsigned align,int socket_arg) { -struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config; -int socket, i; -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 ;
@@ -50,33 +46,12 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket_arg)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 ) +if (socket_arg >=RTE_MAX_NUMA_NODES )return NULL ; - ret =malloc_heap_alloc (&mcfg->malloc_heaps[socket], type, - size,0 , align ==0 ?1 : align,0 ); -if (ret !=NULL || socket_arg !=SOCKET_ID_ANY ) -return ret; - -/* try other heaps */ -for (i =0 ; i <RTE_MAX_NUMA_NODES ; i++) { -/* we already tried this one */ -if (i == socket) -continue ; - - ret =malloc_heap_alloc (&mcfg->malloc_heaps[i], type, - size,0 , align ==0 ?1 : align,0 ); -if (ret !=NULL ) -return ret; - } - -return NULL ; +return malloc_heap_alloc (type, size, socket_arg,0 , + align ==0 ?1 : align,0 ); }/*
Signature · 1 line
2.7.4
PATCH v2 18/41 test: fix malloc autotest to support memory hotplug
In reply to Anatoly BurakovThe test was expecting memory already being allocated on all sockets, and thus was failing because calling rte_malloc could trigger memory hotplug event and allocate memory where there was none before.
Fix it to instead report availability of memory on specific sockets by attempting to allocate a page and see if that succeeds. Technically, this can still cause failure as memory might not be available at the time of check, but become available by the time the test is run, but this is a corner case not worth considering.
--- test/test/test_malloc.c | 52 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-)
test/test/test_malloc.c +44 -8
@@ -22,6 +22,8 @@#include <rte_random.h> #include <rte_string_fns.h> +#include "../../lib/librte_eal/common/eal_memalloc.h" +#include "test.h" #define N 10000
@@ -708,22 +710,56 @@ test_malloc_bad_params(void)/* Check if memory is avilable on a specific socket */ static int -is_mem_on_socket (int32_t socket) +is_mem_on_socket (unsigned int socket) { +struct rte_malloc_socket_stats stats;const struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config; -unsigned i; +uint64_t prev_pgsz; +unsigned int i; + +/* we cannot know if there's memory on a specific socket, since it might +* be available, but not yet allocated. so, in addition to checking +* already mapped memory, we will attempt to allocate a page from that +* socket and see if it works. +*/ +if (socket >=rte_num_sockets ()) +return 0 ; +rte_malloc_get_socket_stats (socket, &stats); + +/* if heap has memory allocated, stop */ +if (stats.heap_totalsz_bytes >0 ) +return 1 ; + +/* to allocate a page, we will have to know its size, so go through all +* supported page sizes and try with each one. +*/ + prev_pgsz =0 ;for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { -const struct rte_memseg_list *msl = - &mcfg->memsegs[i]; -const struct rte_fbarray *arr = &msl->memseg_arr; +const struct rte_memseg_list *msl = &mcfg->memsegs[i]; +uint64_t page_sz; -if (msl->socket_id != socket) +/* skip unused memseg lists */ +if (msl->memseg_arr.len ==0 )continue ; + page_sz = msl->hugepage_sz; -if (arr->count) -return 1 ; +/* skip page sizes we've tried already */ +if (prev_pgsz == page_sz) +continue ; + + prev_pgsz = page_sz; + +struct rte_memseg *ms =eal_memalloc_alloc_page (page_sz, + socket); + +if (ms ==NULL ) +continue ; + +eal_memalloc_free_page (ms); + +return 1 ; }return 0 ; }
Signature · 1 line
2.7.4
PATCH v2 19/41 eal: add API to check if memory is contiguous
In reply to Anatoly BurakovThis will be helpful down the line when we implement support for allocating physically contiguous memory. We can no longer guarantee physically contiguous memory unless we're in IOVA_AS_VA mode, but we can certainly try and see if we succeed. In addition, this would be useful for e.g. PMD's who may allocate chunks that are smaller than the pagesize, but they must not cross the page boundary, in which case we will be able to accommodate that request.
--- lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/common/eal_common_memalloc.c | 49 +++++++++++++++++++++++++++++ lib/librte_eal/common/eal_memalloc.h | 5 +++ lib/librte_eal/common/meson.build | 1 + lib/librte_eal/linuxapp/eal/Makefile | 1 + 5 files changed, 57 insertions(+) create mode 100644 lib/librte_eal/common/eal_common_memalloc.c
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -41,6 +41,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_memzone.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_log.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_launch.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_memory.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_tailqs.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_errno.c
lib/librte_eal/common/eal_common_memalloc.c new file +49 -0
49 lines not on this page · raw message
lib/librte_eal/common/eal_memalloc.h +5 -0
@@ -8,6 +8,7 @@#include <stdbool.h> #include <rte_memory.h> +#include <rte_eal_memconfig.h> struct rte_memseg *eal_memalloc_alloc_page (uint64_t size,int socket);
@@ -19,4 +20,8 @@ eal_memalloc_alloc_page_bulk(struct rte_memseg **ms, int n, uint64_t size,int eal_memalloc_free_page (struct rte_memseg *ms); +bool +eal_memalloc_is_contig (struct rte_memseg_list *msl,void *start, +size_t len); +#endif // EAL_MEMALLOC_H
lib/librte_eal/common/meson.build +1 -0
@@ -16,6 +16,7 @@ common_sources = files( 'eal_common_launch.c', 'eal_common_lcore.c', 'eal_common_log.c', + 'eal_common_memalloc.c', 'eal_common_memory.c', 'eal_common_memzone.c', 'eal_common_options.c',
lib/librte_eal/linuxapp/eal/Makefile +1 -0
@@ -49,6 +49,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_timer.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_memzone.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_log.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_launch.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_memalloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_memory.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_tailqs.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_errno.c
Signature · 1 line
2.7.4
PATCH v2 20/41 eal: add backend support for contiguous allocation
In reply to Anatoly BurakovNo major changes, just add some checks in a few key places, and a new parameter to pass around.
--- lib/librte_eal/common/eal_common_memzone.c | 20 +++--- lib/librte_eal/common/malloc_elem.c | 101 ++++++++++++++++++++++------- lib/librte_eal/common/malloc_elem.h | 4 +- lib/librte_eal/common/malloc_heap.c | 57 ++++++++++------ lib/librte_eal/common/malloc_heap.h | 4 +- lib/librte_eal/common/rte_malloc.c | 6 +- 6 files changed, 134 insertions(+), 58 deletions(-)
lib/librte_eal/common/eal_common_memzone.c +11 -9
@@ -98,7 +98,8 @@ find_heap_max_free_elem(int *s, unsigned align)static const struct rte_memzone *memzone_reserve_aligned_thread_unsafe (const char *name,size_t len, -int socket_id,unsigned flags,unsigned align,unsigned bound) +int socket_id,unsigned int flags,unsigned int align, +unsigned int bound,bool contig) {struct rte_memzone *mz;struct rte_mem_config *mcfg;
@@ -182,7 +183,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,/* allocate memory on heap */ void *mz_addr =malloc_heap_alloc (NULL , requested_len, socket_id, flags, - align, bound); + align, bound, contig);if (mz_addr ==NULL ) { rte_errno =ENOMEM ;
@@ -215,9 +216,9 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, }static const struct rte_memzone * -rte_memzone_reserve_thread_safe (const char *name,size_t len, -int socket_id,unsigned flags,unsigned align, -unsigned bound) +rte_memzone_reserve_thread_safe (const char *name,size_t len,int socket_id, +unsigned int flags,unsigned int align,unsigned int bound, +bool contig) {struct rte_mem_config *mcfg;const struct rte_memzone *mz =NULL ;
@@ -228,7 +229,7 @@ rte_memzone_reserve_thread_safe(const char *name, size_t len,rte_rwlock_write_lock (&mcfg->mlock); mz =memzone_reserve_aligned_thread_unsafe ( - name, len, socket_id, flags, align, bound); + name, len, socket_id, flags, align, bound, contig);rte_rwlock_write_unlock (&mcfg->mlock);
@@ -245,7 +246,7 @@ rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id, unsigned flags, unsigned align, unsigned bound) { return rte_memzone_reserve_thread_safe(name, len, socket_id, flags, -align ,bound ); +align ,bound ,false ); }/*
@@ -257,7 +258,7 @@ rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id, unsigned flags, unsigned align) {return rte_memzone_reserve_thread_safe (name, len, socket_id, flags, - align,0 ); + align,0 , false); }/*
@@ -269,7 +270,8 @@ rte_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags) {return rte_memzone_reserve_thread_safe (name, len, socket_id, - flags,RTE_CACHE_LINE_SIZE ,0 ); + flags,RTE_CACHE_LINE_SIZE ,0 , + false); }int
lib/librte_eal/common/malloc_elem.c +79 -22
136 lines not on this page · raw message
lib/librte_eal/common/malloc_elem.h +2 -2
@@ -123,7 +123,7 @@ malloc_elem_insert(struct malloc_elem *elem);*/ int malloc_elem_can_hold (struct malloc_elem *elem,size_t size, -unsigned align,size_t bound); +unsigned int align,size_t bound,bool contig);/* * reserve a block of data in an existing malloc_elem. If the malloc_elem
@@ -131,7 +131,7 @@ malloc_elem_can_hold(struct malloc_elem *elem, size_t size,*/ struct malloc_elem *malloc_elem_alloc (struct malloc_elem *elem,size_t size, -unsigned align,size_t bound); +unsigned int align,size_t bound,bool contig);/* * free a malloc_elem block by adding it to the free list. If the
lib/librte_eal/common/malloc_heap.c +36 -21
172 lines not on this page · raw message
lib/librte_eal/common/malloc_heap.h +3 -1
@@ -5,6 +5,8 @@#ifndef MALLOC_HEAP_H_ #define MALLOC_HEAP_H_ +#include <stdbool.h> +#include <rte_malloc.h> #include <rte_malloc_heap.h>
@@ -25,7 +27,7 @@ malloc_get_numa_socket(void)void *malloc_heap_alloc (const char *type,size_t size,int socket,unsigned int flags, -size_t align,size_t bound); +size_t align,size_t bound,bool contig);int malloc_heap_free (struct malloc_elem *elem);
lib/librte_eal/common/rte_malloc.c +3 -3
@@ -37,7 +37,8 @@ void rte_free(void *addr)* Allocate memory on specified heap. */ void * -rte_malloc_socket (const char *type,size_t size,unsigned align,int socket_arg) +rte_malloc_socket (const char *type,size_t size,unsigned int align, +int socket_arg) {/* return NULL if size is 0 or alignment is not power-of-2 */ if (size ==0 || (align && !rte_is_power_of_2 (align)))
@@ -50,8 +51,7 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket_arg)if (socket_arg >=RTE_MAX_NUMA_NODES )return NULL ; -return malloc_heap_alloc (type, size, socket_arg,0 , - align ==0 ?1 : align,0 ); +return malloc_heap_alloc (type, size, socket_arg,0 , align,0 , false); }/*
Signature · 1 line
2.7.4
PATCH v2 21/41 eal: enable reserving physically contiguous memzones
In reply to Anatoly BurakovThis adds a new set of _contig API's to rte_memzone.
--- lib/librte_eal/common/eal_common_memzone.c | 44 ++++++++ lib/librte_eal/common/include/rte_memzone.h | 154 ++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+)
lib/librte_eal/common/eal_common_memzone.c +44 -0
@@ -170,6 +170,12 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, socket_id =SOCKET_ID_ANY ;if (len ==0 ) { +/* len == 0 is only allowed for non-contiguous zones */ +if (contig) { +RTE_LOG (DEBUG ,EAL ,"Reserving zero-length contiguous memzones is not supported\n" ); + rte_errno =EINVAL ; +return NULL ; + }if (bound !=0 ) requested_len = bound;else {
@@ -251,6 +257,19 @@ rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,/* * Return a pointer to a correctly filled memzone descriptor (with a +* specified alignment and boundary). If the allocation cannot be done, +* return NULL. +*/ +const struct rte_memzone * +rte_memzone_reserve_bounded_contig (const char *name,size_t len,int socket_id, +unsigned int flags,unsigned int align,unsigned int bound) +{ +return rte_memzone_reserve_thread_safe (name, len, socket_id, flags, + align, bound, true); +} + +/* +* Return a pointer to a correctly filled memzone descriptor (with a * specified alignment). If the allocation cannot be done, return NULL. */ const struct rte_memzone *
@@ -262,6 +281,18 @@ rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id, }/* +* Return a pointer to a correctly filled memzone descriptor (with a +* specified alignment). If the allocation cannot be done, return NULL. +*/ +const struct rte_memzone * +rte_memzone_reserve_aligned_contig (const char *name,size_t len,int socket_id, +unsigned int flags,unsigned int align) +{ +return rte_memzone_reserve_thread_safe (name, len, socket_id, flags, + align,0 , true); +} + +/* * Return a pointer to a correctly filled memzone descriptor. If the * allocation cannot be done, return NULL. */
@@ -274,6 +305,19 @@ rte_memzone_reserve(const char *name, size_t len, int socket_id, false); } +/* +* Return a pointer to a correctly filled memzone descriptor. If the +* allocation cannot be done, return NULL. +*/ +const struct rte_memzone * +rte_memzone_reserve_contig (const char *name,size_t len,int socket_id, +unsigned int flags) +{ +return rte_memzone_reserve_thread_safe (name, len, socket_id, + flags,RTE_CACHE_LINE_SIZE ,0 , + true); +} +int rte_memzone_free (const struct rte_memzone *mz) {
lib/librte_eal/common/include/rte_memzone.h +154 -0
160 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 22/41 eal: replace memzone array with fbarray
In reply to Anatoly BurakovIt's there, so we might as well use it. Some operations will be sped up by that.
Since we have to allocate an fbarray for memzones, we have to do it before we initialize memory subsystem, because that, in secondary processes, will (later) allocate more fbarrays than the primary process, which will result in inability to attach to memzone fbarray if we do it after the fact.
---
Notes:
Code for ENA driver makes little sense to me, but i've attempted to keep the same semantics as the old code.
drivers/net/ena/ena_ethdev.c | 10 +- lib/librte_eal/bsdapp/eal/eal.c | 6 + lib/librte_eal/common/eal_common_memzone.c | 180 +++++++++++++++------- lib/librte_eal/common/include/rte_eal_memconfig.h | 4 +- lib/librte_eal/common/malloc_heap.c | 4 + lib/librte_eal/linuxapp/eal/eal.c | 13 +- test/test/test_memzone.c | 9 +- 7 files changed, 157 insertions(+), 69 deletions(-)
drivers/net/ena/ena_ethdev.c +7 -3
@@ -264,11 +264,15 @@ static const struct eth_dev_ops ena_dev_ops = {static inline int ena_cpu_to_node (int cpu) {struct rte_config *config =rte_eal_get_configuration (); +struct rte_fbarray *arr = &config->mem_config->memzones; +const struct rte_memzone *mz; -if (likely (cpu <RTE_MAX_MEMZONE )) -return config->mem_config->memzone[cpu].socket_id; +if (unlikely (cpu >=RTE_MAX_MEMZONE )) +return NUMA_NO_NODE ; -return NUMA_NO_NODE ; + mz =rte_fbarray_get (arr, cpu); + +return mz->socket_id; } static inline void ena_rx_mbuf_prepare(struct rte_mbuf *mbuf,
lib/librte_eal/bsdapp/eal/eal.c +6 -0
@@ -608,6 +608,12 @@ rte_eal_init(int argc, char **argv)return -1 ; } +if (rte_eal_malloc_heap_init () <0 ) { +rte_eal_init_alert ("Cannot init malloc heap\n" ); + rte_errno =ENODEV ; +return -1 ; + } +if (rte_eal_tailqs_init () <0 ) {rte_eal_init_alert ("Cannot init tail queues for objects\n" ); rte_errno =EFAULT ;
lib/librte_eal/common/eal_common_memzone.c +123 -57
277 lines not on this page · raw message
lib/librte_eal/common/include/rte_eal_memconfig.h +1 -3
@@ -58,10 +58,8 @@ struct rte_mem_config {rte_rwlock_t qlock;/**< used for tailq operation for thread safe. */ rte_rwlock_t mplock;/**< only used by mempool LIB for thread-safe. */ -uint32_t memzone_cnt;/**< Number of allocated memzones */ -/* memory segments and zones */ -struct rte_memzone memzone[RTE_MAX_MEMZONE ];/**< Memzone descriptors. */ +struct rte_fbarray memzones;/**< Memzone descriptors. */ struct rte_memseg_list memsegs[RTE_MAX_MEMSEG_LISTS ];/**< list of dynamic arrays holding memsegs */
lib/librte_eal/common/malloc_heap.c +4 -0
@@ -579,6 +579,10 @@ rte_eal_malloc_heap_init(void)if (mcfg ==NULL )return -1 ; +/* secondary processes don't need to initialize heap */ +if (rte_eal_process_type () ==RTE_PROC_SECONDARY ) +return 0 ; +for (msl_idx =0 ; msl_idx <RTE_MAX_MEMSEG_LISTS ; msl_idx++) {int start;struct rte_fbarray *arr;
lib/librte_eal/linuxapp/eal/eal.c +11 -2
@@ -857,6 +857,15 @@ rte_eal_init(int argc, char **argv) return -1; }#endif +/* memzone_init maps rte_fbarrays, which has to be done before hugepage +* init/attach, because attach creates extra fbarrays in secondary +* process, resulting in inability to map memzone fbarray. +*/ +if (rte_eal_memzone_init () <0 ) { +rte_eal_init_alert ("Cannot init memzone\n" ); + rte_errno =ENODEV ; +return -1 ; + }if (rte_eal_memory_init () <0 ) {rte_eal_init_alert ("Cannot init memory\n" );
@@ -867,8 +876,8 @@ rte_eal_init(int argc, char **argv)/* the directories are locked during eal_hugepage_info_init */ eal_hugedirs_unlock (); -if (rte_eal_memzone_init () <0 ) { -rte_eal_init_alert ("Cannot init memzone\n" ); +if (rte_eal_malloc_heap_init () <0 ) { +rte_eal_init_alert ("Cannot init malloc heap\n" ); rte_errno =ENODEV ;return -1 ; }
test/test/test_memzone.c +5 -4
@@ -893,7 +893,7 @@ test_memzone_basic(void)const struct rte_memzone *mz;int memzone_cnt_after, memzone_cnt_expected;int memzone_cnt_before = -rte_eal_get_configuration ()->mem_config->memzone_cnt; +rte_eal_get_configuration ()->mem_config->memzones.count; memzone1 =rte_memzone_reserve (TEST_MEMZONE_NAME ("testzone1" ),100 ,SOCKET_ID_ANY ,0 );
@@ -917,7 +917,7 @@ test_memzone_basic(void) (memzone3 !=NULL ) + (memzone4 !=NULL ); memzone_cnt_after = -rte_eal_get_configuration ()->mem_config->memzone_cnt; +rte_eal_get_configuration ()->mem_config->memzones.count;if (memzone_cnt_after != memzone_cnt_expected)return -1 ;
@@ -996,7 +996,7 @@ test_memzone_basic(void) } memzone_cnt_after = -rte_eal_get_configuration ()->mem_config->memzone_cnt; +rte_eal_get_configuration ()->mem_config->memzones.count;if (memzone_cnt_after != memzone_cnt_before)return -1 ;
@@ -1017,7 +1017,8 @@ static inttest_memzone (void) {/* take note of how many memzones were allocated before running */ -int memzone_cnt =rte_eal_get_configuration ()->mem_config->memzone_cnt; +int memzone_cnt = +rte_eal_get_configuration ()->mem_config->memzones.count;printf ("test basic memzone API\n" );if (test_memzone_basic () <0 )
Signature · 1 line
2.7.4
PATCH v2 23/41 mempool: add support for the new allocation methods
In reply to Anatoly BurakovIf a user has specified that the zone should have contiguous memory, use the new _contig allocation API's instead of normal ones. Otherwise, account for the fact that unless we're in IOVA_AS_VA mode, we cannot guarantee that the pages would be physically contiguous, so we calculate the memzone size and alignments as if we were getting the smallest page size available.
--- lib/librte_mempool/rte_mempool.c | 87 +++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 9 deletions(-)
lib/librte_mempool/rte_mempool.c +78 -9
@@ -98,6 +98,27 @@ static unsigned optimize_object_size(unsigned obj_size)return new_obj_size *RTE_MEMPOOL_ALIGN ; } +static size_t +get_min_page_size (void ) +{ +const struct rte_mem_config *mcfg = +rte_eal_get_configuration ()->mem_config; +int i; +size_t min_pagesz =SIZE_MAX ; + +for (i =0 ; i <RTE_MAX_MEMSEG_LISTS ; i++) { +const struct rte_memseg_list *msl = &mcfg->memsegs[i]; + +if (msl->base_va ==NULL ) +continue ; + +if (msl->hugepage_sz < min_pagesz) + min_pagesz = msl->hugepage_sz; + } + +return min_pagesz ==SIZE_MAX ? (size_t )getpagesize () : min_pagesz; +} +static void mempool_add_elem (struct rte_mempool *mp,void *obj,rte_iova_t iova) {
@@ -549,6 +570,7 @@ rte_mempool_populate_default(struct rte_mempool *mp)unsigned mz_id, n;unsigned int mp_flags;int ret; +bool force_contig, no_contig;/* mempool must not be populated */ if (mp->nb_mem_chunks !=0 )
@@ -563,10 +585,46 @@ rte_mempool_populate_default(struct rte_mempool *mp)/* update mempool capabilities */ mp->flags |= mp_flags; -if (rte_eal_has_hugepages ()) { - pg_shift =0 ;/* not needed, zone is physically contiguous */ + no_contig = mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ; + force_contig = mp->flags &MEMPOOL_F_CAPA_PHYS_CONTIG ; + +/* +* there are several considerations for page size and page shift here. +* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything. +* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory. +* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so +* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/ + +if (no_contig || force_contig ||rte_eal_iova_mode () ==RTE_IOVA_VA ) { pg_sz =0 ; + pg_shift =0 ; align =RTE_CACHE_LINE_SIZE ; + }else if (rte_eal_has_hugepages ()) { + pg_sz =get_min_page_size (); + pg_shift =rte_bsf32 (pg_sz); + align = pg_sz; }else { pg_sz =getpagesize (); pg_shift =rte_bsf32 (pg_sz);
@@ -585,23 +643,34 @@ rte_mempool_populate_default(struct rte_mempool *mp) goto fail; } - mz =rte_memzone_reserve_aligned (mz_name, size, - mp->socket_id, mz_flags, align); -/* not enough memory, retry with the biggest zone we have */ -if (mz ==NULL ) - mz =rte_memzone_reserve_aligned (mz_name,0 , +if (force_contig) { +/* +* if contiguous memory for entire mempool memory was +* requested, don't try reserving again if we fail. +*/ + mz =rte_memzone_reserve_aligned_contig (mz_name, size, + mp->socket_id, mz_flags, align); + }else { + mz =rte_memzone_reserve_aligned (mz_name, size, mp->socket_id, mz_flags, align); +/* not enough memory, retry with the biggest zone we +* have +*/ +if (mz ==NULL ) + mz =rte_memzone_reserve_aligned (mz_name,0 , + mp->socket_id, mz_flags, align); + }if (mz ==NULL ) { ret = -rte_errno; goto fail; } -if (mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ) +if (no_contig) iova =RTE_BAD_IOVA ;else iova = mz->iova; -if (rte_eal_has_hugepages ()) +if (rte_eal_has_hugepages () && force_contig) ret = rte_mempool_populate_iova(mp, mz->addr, iova, mz->len, rte_mempool_memchunk_mz_free,
Signature · 1 line
2.7.4
Re: [PATCH v2 23/41] mempool: add support for the new allocation methods
Hi Anatoly,
Please find some comments below.
Quoted 8 lines · Anatoly Burakov
If a user has specified that the zone should have contiguous memory, use the new
_contigallocation API's instead of normal ones. Otherwise, account for the fact that unless we're inIOVA_AS_VAmode, we cannot guarantee that the pages would be physically contiguous, so we calculate the memzone size and alignments as if we were getting the smallest page size available.Signed-off-by Anatoly Burakov <[email protected]>
[...]
lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 592
Quoted 11 lines
lib/librte_mempool/rte_mempool.c +5
-2@@ -563,10 +585,46 @@ rte_mempool_populate_default(struct rte_mempool *mp)/* update mempool capabilities */ mp->flags |= mp_flags; -if (rte_eal_has_hugepages ()) { - pg_shift =0 ;/* not needed, zone is physically contiguous */ + no_contig = mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ; + force_contig = mp->flags &MEMPOOL_F_CAPA_PHYS_CONTIG ; + +/* +* there are several considerations for page size and page shift here.
I would add a little word here to describe what page size and page shift are used for:
These values impact the result of rte_mempool_xmem_size() (*), which returns the amount of memory that should be allocated to store the desired number of objects. When not zero, it allocates more memory for the padding between objects, to ensure that an object does not cross a page boundary.
(*) it is renamed in Andrew's patchset about mempool_ops API, but it seems the memory rework may be pushed before.
Quoted 16 lines
lib/librte_mempool/rte_mempool.c +16
-0+* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything. +* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory.
lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 618
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10
-0+* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so +* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/
This comment is helpful to understand, thanks.
(by the way, reading it makes me think we should rename MEMPOOL_F_*_PHYS_CONTIG as MEMPOOL_F_*_IOVA_CONTIG)
Quoted 28 lines
lib/librte_mempool/rte_mempool.c +14
-5+ +if (no_contig || force_contig ||rte_eal_iova_mode () ==RTE_IOVA_VA ) { pg_sz =0 ; + pg_shift =0 ; align =RTE_CACHE_LINE_SIZE ; + }else if (rte_eal_has_hugepages ()) { + pg_sz =get_min_page_size (); + pg_shift =rte_bsf32 (pg_sz); + align = pg_sz; }else { pg_sz =getpagesize (); pg_shift =rte_bsf32 (pg_sz);@@ -585,23 +643,34 @@ rte_mempool_populate_default(struct rte_mempool *mp) goto fail; } - mz =rte_memzone_reserve_aligned (mz_name, size, - mp->socket_id, mz_flags, align); -/* not enough memory, retry with the biggest zone we have */ -if (mz ==NULL ) - mz = rte_memzone_reserve_aligned(mz_name, 0, +if (force_contig) { +/* +* if contiguous memory for entire mempool memory was +* requested, don't try reserving again if we fail. +*/ + mz =rte_memzone_reserve_aligned_contig (mz_name, size, + mp->socket_id, mz_flags, align);
lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 662
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +9
-0+ }else { + mz =rte_memzone_reserve_aligned (mz_name, size, mp->socket_id, mz_flags, align); +/* not enough memory, retry with the biggest zone we +* have +*/ +if (mz ==NULL ) + mz =rte_memzone_reserve_aligned (mz_name,0 , + mp->socket_id, mz_flags, align); + }
This is not wrong, but at first glance I think it is not required, because we have this in populate_iova():
/* Detect pool area has sufficient space for elements */
if (mp_capa_flags & MEMPOOL_F_CAPA_PHYS_CONTIG) {
if (len < total_elt_sz * mp->size) {
RTE_LOG(ERR, MEMPOOL,
"pool area %" PRIx64 " not enough\n",
(uint64_t)len);
return -ENOSPC;
}
}Thanks,
Olivier
Re: [PATCH v2 23/41] mempool: add support for the new allocation methods
Quoted 28 lines · Olivier Matz
Quoted 16 lines
lib/librte_mempool/rte_mempool.c +16
-0+* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything. +* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory. lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 618
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10
-0+* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so +* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/ This comment is helpful to understand, thanks.
Quoted 2 lines
(by the way, reading it makes me think we should rename MEMPOOL_F_*_PHYS_CONTIG as MEMPOOL_F_*_IOVA_CONTIG)
I'll care about renaming in my patchset about mempool_ops API.
Re: [PATCH v2 23/41] mempool: add support for the new allocation methods
Quoted 29 lines · Andrew Rybchenko
Quoted 28 lines · Olivier Matz
Quoted 16 lines
lib/librte_mempool/rte_mempool.c +16
-0+* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything. +* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory. lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 618
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10
-0+* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so +* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/ This comment is helpful to understand, thanks.
Quoted 4 lines
Quoted 2 lines
(by the way, reading it makes me think we should rename MEMPOOL_F_*_PHYS_CONTIG as MEMPOOL_F_*_IOVA_CONTIG)
I'll care about renaming in my patchset about
mempool_opsAPI.
Great, thanks!
Please also keep the old ones for now, we will remove them later.
Re: [PATCH v2 23/41] mempool: add support for the new allocation methods
In reply to Anatoly BurakovHello Anatoly,
Quoted 9 lines · Anatoly Burakov
If a user has specified that the zone should have contiguous memory, use the new
_contigallocation API's instead of normal ones. Otherwise, account for the fact that unless we're inIOVA_AS_VAmode, we cannot guarantee that the pages would be physically contiguous, so we calculate the memzone size and alignments as if we were getting the smallest page size available.Signed-off-by Anatoly Burakov <[email protected]>---
[...]
Quoted 16 lines
static void
mempool_add_elem(struct rte_mempool *mp, void *obj, rte_iova_t iova){lib/librte_mempool/rte_mempool.c +1
-1@@ -549,6 +570,7 @@ rte_mempool_populate_default(struct rte_mempool *mp)unsigned mz_id, n;unsigned int mp_flags;int ret; +bool force_contig, no_contig;/* mempool must not be populated */ if (mp->nb_mem_chunks !=0 )@@ -563,10 +585,46 @@ rte_mempool_populate_default(struct rte_mempool *mp)/* update mempool capabilities */ mp->flags |= mp_flags; -if (rte_eal_has_hugepages ()) {
lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 596
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +9
-1- pg_shift =0 ;/* not needed, zone is physically contiguous */ + no_contig = mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ; + force_contig = mp->flags &MEMPOOL_F_CAPA_PHYS_CONTIG ; + +/* +* there are several considerations for page size and page shift here. +* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything.
I think the above case is not handled properly here. reason below...
Quoted 17 lines
lib/librte_mempool/rte_mempool.c +17
-0+* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory. +* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so
lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 623
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +8
-0+* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/ + +if (no_contig || force_contig ||rte_eal_iova_mode () ==RTE_IOVA_VA ) { pg_sz =0 ; + pg_shift =0 ; align =RTE_CACHE_LINE_SIZE ;
So, assuming dpaa2 as example, I ran testpmd. IOVA=VA is the mode. pg_sz = 0 is set.
same as before applying the hotplug patchset except that earlier this decision was purely based on availability of hugepages (rte_eal_has_hugepages()).
Moving on...
Quoted 36 lines
lib/librte_mempool/rte_mempool.c +20
-5+ }else if (rte_eal_has_hugepages ()) { + pg_sz =get_min_page_size (); + pg_shift =rte_bsf32 (pg_sz); + align = pg_sz; }else { pg_sz =getpagesize (); pg_shift =rte_bsf32 (pg_sz);@@ -585,23 +643,34 @@ rte_mempool_populate_default(struct rte_mempool *mp) goto fail; } - mz =rte_memzone_reserve_aligned (mz_name, size, - mp->socket_id, mz_flags, align); -/* not enough memory, retry with the biggest zone we have */ -if (mz ==NULL ) - mz =rte_memzone_reserve_aligned (mz_name,0 , +if (force_contig) { +/* +* if contiguous memory for entire mempool memory was +* requested, don't try reserving again if we fail. +*/ + mz =rte_memzone_reserve_aligned_contig (mz_name, size, + mp->socket_id, mz_flags, align); + }else { + mz =rte_memzone_reserve_aligned (mz_name, size, mp->socket_id, mz_flags, align); +/* not enough memory, retry with the biggest zone we +* have +*/ +if (mz ==NULL ) + mz =rte_memzone_reserve_aligned (mz_name,0 , + mp->socket_id, mz_flags, align); + }if (mz ==NULL ) { ret = -rte_errno; goto fail;
lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 673
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +2
-2} -if (mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ) +if (no_contig) iova =RTE_BAD_IOVA ;else iova = mz->iova; -if (rte_eal_has_hugepages ()) +if (rte_eal_has_hugepages () && force_contig)
So, pre-hotplugging patch, call used to enter mempool_populate_iova. But, with the 'force_contig' not set (in app/test-pmd/testpmd.c:521) while calling rte_pktmbuf_pool_create, rte_mempool_populate_va is called instead.
Quoted 5 lines
lib/librte_mempool/rte_mempool.c +0
-1ret = rte_mempool_populate_iova(mp, mz->addr, iova, mz->len, rte_mempool_memchunk_mz_free, --2.7.4
This is called with pg_sz = 0:
678 else
Quoted 1 line
Quoted 1 line
# 679 ret = rte_mempool_populate_virt(mp,
mz->addr,
680 mz->len, pg_sz, 681 rte_mempool_memchunk_mz_free, 682 (void *)(uintptr_t)mz);
In this function,
512 /* address and len must be page-aligned */
513 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
514 return -EINVAL;
This is where error is returned.
I don't think RTE_PTR_ALIGN_CEIL is designed to handle pg_sz = 0.
It is roughly equivalent to:RTE_PTR_ALIGN_FLOOR(((uintptr_t)addr - 1), pg_sz) which returns NULL (0 ~ pg_sz).
Basically, this ends up failing rte_mempool_populate_default.
I think the reason is the assumption that when rte_mempool_populate_virt is called, it can handle 0 page sizes (there would issues besides the above RTE_PTR_ALIGN_CEIL as well, like a for-loop looping over off+pg_sz), is wrong. It needs a valid page-size value to work with (!0).
So, basically, DPAA2 is stuck with this patch because of above issue, if I am correctly comprehending it as above.
Regards,
Shreyansh
Re: [PATCH v2 23/41] mempool: add support for the new allocation methods
Quoted 168 lines · Shreyansh Jain
Hello Anatoly,
Quoted 9 lines · Anatoly Burakov
If a user has specified that the zone should have contiguous memory, use the new
_contigallocation API's instead of normal ones. Otherwise, account for the fact that unless we're inIOVA_AS_VAmode, we cannot guarantee that the pages would be physically contiguous, so we calculate the memzone size and alignments as if we were getting the smallest page size available.Signed-off-by Anatoly Burakov <[email protected]>---
[...]
Quoted 16 lines
static void mempool_add_elem(struct rte_mempool *mp, void *obj, rte_iova_t iova) {lib/librte_mempool/rte_mempool.c +1
-1@@ -549,6 +570,7 @@ rte_mempool_populate_default(struct rte_mempool *mp)unsigned mz_id, n;unsigned int mp_flags;int ret; +bool force_contig, no_contig;/* mempool must not be populated */ if (mp->nb_mem_chunks !=0 )@@ -563,10 +585,46 @@ rte_mempool_populate_default(struct rte_mempool *mp)/* update mempool capabilities */ mp->flags |= mp_flags; -if (rte_eal_has_hugepages ()) {lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 596
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +9
-1- pg_shift =0 ;/* not needed, zone is physically contiguous */ + no_contig = mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ; + force_contig = mp->flags &MEMPOOL_F_CAPA_PHYS_CONTIG ; + +/* +* there are several considerations for page size and page shift here. +* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything. I think the above case is not handled properly here. reason below...
Quoted 17 lines
lib/librte_mempool/rte_mempool.c +17
-0+* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory. +* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 623
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +8
-0+* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/ + +if (no_contig || force_contig ||rte_eal_iova_mode () ==RTE_IOVA_VA ) { pg_sz =0 ; + pg_shift =0 ; align =RTE_CACHE_LINE_SIZE ;So, assuming dpaa2 as example, I ran testpmd. IOVA=VA is the mode.
pg_sz= 0 is set.
same as before applying the hotplug patchset except that earlier this decision was purely based on availability of hugepages (rte_eal_has_hugepages()).
Moving on...Quoted 36 lines
lib/librte_mempool/rte_mempool.c +20
-5+ }else if (rte_eal_has_hugepages ()) { + pg_sz =get_min_page_size (); + pg_shift =rte_bsf32 (pg_sz); + align = pg_sz; }else { pg_sz =getpagesize (); pg_shift =rte_bsf32 (pg_sz);@@ -585,23 +643,34 @@ rte_mempool_populate_default(struct rte_mempool *mp) goto fail; } - mz =rte_memzone_reserve_aligned (mz_name, size, - mp->socket_id, mz_flags, align); -/* not enough memory, retry with the biggest zone we have */ -if (mz ==NULL ) - mz =rte_memzone_reserve_aligned (mz_name,0 , +if (force_contig) { +/* +* if contiguous memory for entire mempool memory was +* requested, don't try reserving again if we fail. +*/ + mz =rte_memzone_reserve_aligned_contig (mz_name, size, + mp->socket_id, mz_flags, align); + }else { + mz =rte_memzone_reserve_aligned (mz_name, size, mp->socket_id, mz_flags, align); +/* not enough memory, retry with the biggest zone we +* have +*/ +if (mz ==NULL ) + mz =rte_memzone_reserve_aligned (mz_name,0 , + mp->socket_id, mz_flags, align); + }if (mz ==NULL ) { ret = -rte_errno; goto fail;lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 673
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +2
-2} -if (mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ) +if (no_contig) iova =RTE_BAD_IOVA ;else iova = mz->iova; -if (rte_eal_has_hugepages ()) +if (rte_eal_has_hugepages () && force_contig)So, pre-hotplugging patch, call used to enter
mempool_populate_iova. But, with the 'force_contig' not set (inapp/test-pmd/testpmd.c:521) while callingrte_pktmbuf_pool_create,rte_mempool_populate_vais called instead.Quoted 5 lines
lib/librte_mempool/rte_mempool.c +0
-1ret = rte_mempool_populate_iova(mp, mz->addr, iova, mz->len, rte_mempool_memchunk_mz_free, --2.7.4
This is called with
pg_sz= 0:
678 elseQuoted 1 line
Quoted 1 line
# 679 ret = rte_mempool_populate_virt(mp,
mz->addr,680 mz->len, pg_sz, 681 rte_mempool_memchunk_mz_free, 682 (void *)(uintptr_t)mz);In this function,
512 /* address and len must be page-aligned */
513 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz)!= addr)
514 return-EINVAL;This is where error is returned.
I don't think
RTE_PTR_ALIGN_CEILis designed to handlepg_sz= 0.It is roughly equivalent to:
RTE_PTR_ALIGN_FLOOR(((uintptr_t)addr - 1), pg_sz)which returns NULL (0 ~pg_sz).Basically, this ends up failing
rte_mempool_populate_default.I think the reason is the assumption that when
rte_mempool_populate_virtis called, it can handle 0 page sizes (there would issues besides the aboveRTE_PTR_ALIGN_CEILas well, like a for-loop looping over off+pg_sz), is wrong. It needs a valid page-size value to work with (!0).
Quoted 6 lines
So, basically, DPAA2 is stuck with this patch because of above issue, if I am correctly comprehending it as above.
Regards,
Shreyansh
Thanks for testing this. I'll look into fixing it.
Signature · 2 lines
Thanks, Anatoly
Re: [PATCH v2 23/41] mempool: add support for the new allocation methods
In reply to Shreyansh JainQuoted 168 lines · Shreyansh Jain
Hello Anatoly,
Quoted 9 lines · Anatoly Burakov
If a user has specified that the zone should have contiguous memory, use the new
_contigallocation API's instead of normal ones. Otherwise, account for the fact that unless we're inIOVA_AS_VAmode, we cannot guarantee that the pages would be physically contiguous, so we calculate the memzone size and alignments as if we were getting the smallest page size available.Signed-off-by Anatoly Burakov <[email protected]>---
[...]
Quoted 16 lines
static void mempool_add_elem(struct rte_mempool *mp, void *obj, rte_iova_t iova) {lib/librte_mempool/rte_mempool.c +1
-1@@ -549,6 +570,7 @@ rte_mempool_populate_default(struct rte_mempool *mp)unsigned mz_id, n;unsigned int mp_flags;int ret; +bool force_contig, no_contig;/* mempool must not be populated */ if (mp->nb_mem_chunks !=0 )@@ -563,10 +585,46 @@ rte_mempool_populate_default(struct rte_mempool *mp)/* update mempool capabilities */ mp->flags |= mp_flags; -if (rte_eal_has_hugepages ()) {lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 596
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +9
-1- pg_shift =0 ;/* not needed, zone is physically contiguous */ + no_contig = mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ; + force_contig = mp->flags &MEMPOOL_F_CAPA_PHYS_CONTIG ; + +/* +* there are several considerations for page size and page shift here. +* +* if we don't need our mempools to have physically contiguous objects, +* then just set page shift and page size to 0, because the user has +* indicated that there's no need to care about anything. I think the above case is not handled properly here. reason below...
Quoted 17 lines
lib/librte_mempool/rte_mempool.c +17
-0+* +* if we do need contiguous objects, there is also an option to reserve +* the entire mempool memory as one contiguous block of memory, in +* which case the page shift and alignment wouldn't matter as well. +* +* if we require contiguous objects, but not necessarily the entire +* mempool reserved space to be contiguous, then there are two options. +* +* if our IO addresses are virtual, not actual physical (IOVA as VA +* case), then no page shift needed - our memory allocation will give us +* contiguous physical memory as far as the hardware is concerned, so +* act as if we're getting contiguous memory. +* +* if our IO addresses are physical, we may get memory from bigger +* pages, or we might get memory from smaller pages, and how much of it +* we require depends on whether we want bigger or smaller pages. +* However, requesting each and every memory size is too much work, so lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 623
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +8
-0+* what we'll do instead is walk through the page sizes available, pick +* the smallest one and set up page shift to match that one. We will be +* wasting some space this way, but it's much nicer than looping around +* trying to reserve each and every page size. +*/ + +if (no_contig || force_contig ||rte_eal_iova_mode () ==RTE_IOVA_VA ) { pg_sz =0 ; + pg_shift =0 ; align =RTE_CACHE_LINE_SIZE ;So, assuming dpaa2 as example, I ran testpmd. IOVA=VA is the mode.
pg_sz= 0 is set.
same as before applying the hotplug patchset except that earlier this decision was purely based on availability of hugepages (rte_eal_has_hugepages()).
Moving on...Quoted 36 lines
lib/librte_mempool/rte_mempool.c +20
-5+ }else if (rte_eal_has_hugepages ()) { + pg_sz =get_min_page_size (); + pg_shift =rte_bsf32 (pg_sz); + align = pg_sz; }else { pg_sz =getpagesize (); pg_shift =rte_bsf32 (pg_sz);@@ -585,23 +643,34 @@ rte_mempool_populate_default(struct rte_mempool *mp) goto fail; } - mz =rte_memzone_reserve_aligned (mz_name, size, - mp->socket_id, mz_flags, align); -/* not enough memory, retry with the biggest zone we have */ -if (mz ==NULL ) - mz =rte_memzone_reserve_aligned (mz_name,0 , +if (force_contig) { +/* +* if contiguous memory for entire mempool memory was +* requested, don't try reserving again if we fail. +*/ + mz =rte_memzone_reserve_aligned_contig (mz_name, size, + mp->socket_id, mz_flags, align); + }else { + mz =rte_memzone_reserve_aligned (mz_name, size, mp->socket_id, mz_flags, align); +/* not enough memory, retry with the biggest zone we +* have +*/ +if (mz ==NULL ) + mz =rte_memzone_reserve_aligned (mz_name,0 , + mp->socket_id, mz_flags, align); + }if (mz ==NULL ) { ret = -rte_errno; goto fail;lib/librte_mempool/rte_mempool.c rte_mempool_populate_default(struct rte_mempool *mp) · line 673
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +2
-2} -if (mp->flags &MEMPOOL_F_NO_PHYS_CONTIG ) +if (no_contig) iova =RTE_BAD_IOVA ;else iova = mz->iova; -if (rte_eal_has_hugepages ()) +if (rte_eal_has_hugepages () && force_contig)So, pre-hotplugging patch, call used to enter
mempool_populate_iova. But, with the 'force_contig' not set (inapp/test-pmd/testpmd.c:521) while callingrte_pktmbuf_pool_create,rte_mempool_populate_vais called instead.Quoted 5 lines
lib/librte_mempool/rte_mempool.c +0
-1ret = rte_mempool_populate_iova(mp, mz->addr, iova, mz->len, rte_mempool_memchunk_mz_free, --2.7.4
This is called with
pg_sz= 0:
678 elseQuoted 1 line
Quoted 1 line
# 679 ret = rte_mempool_populate_virt(mp,
mz->addr,680 mz->len, pg_sz, 681 rte_mempool_memchunk_mz_free, 682 (void *)(uintptr_t)mz);In this function,
512 /* address and len must be page-aligned */
513 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz)!= addr)
514 return-EINVAL;This is where error is returned.
I don't think
RTE_PTR_ALIGN_CEILis designed to handlepg_sz= 0.It is roughly equivalent to:
RTE_PTR_ALIGN_FLOOR(((uintptr_t)addr - 1), pg_sz)which returns NULL (0 ~pg_sz).Basically, this ends up failing
rte_mempool_populate_default.I think the reason is the assumption that when
rte_mempool_populate_virtis called, it can handle 0 page sizes (there would issues besides the aboveRTE_PTR_ALIGN_CEILas well, like a for-loop looping over off+pg_sz), is wrong. It needs a valid page-size value to work with (!0).
Quoted 6 lines
So, basically, DPAA2 is stuck with this patch because of above issue, if I am correctly comprehending it as above.
Regards,
Shreyansh
Thanks for finding this issue. Fix is now pushed to github for testing.
Signature · 2 lines
Thanks, Anatoly
PATCH v2 24/41 vfio: allow to map other memory regions
In reply to Anatoly BurakovCurrently it is not possible to use memory that is not owned by DPDK to perform DMA. This scenarion might be used in vhost applications (like SPDK) where guest send its own memory table. To fill this gap provide API to allow registering arbitrary address in VFIO container.
--- lib/librte_eal/bsdapp/eal/eal.c | 16 ++++ lib/librte_eal/common/include/rte_vfio.h | 39 ++++++++ lib/librte_eal/linuxapp/eal/eal_vfio.c | 153 ++++++++++++++++++++++++++----- lib/librte_eal/linuxapp/eal/eal_vfio.h | 11 +++ 4 files changed, 196 insertions(+), 23 deletions(-)
lib/librte_eal/bsdapp/eal/eal.c +16 -0
@@ -755,6 +755,8 @@ int rte_vfio_enable(const char *modname);int rte_vfio_is_enabled (const char *modname);int rte_vfio_noiommu_is_enabled (void );int rte_vfio_clear_group (int vfio_group_fd); +int rte_vfio_dma_map (uint64_t vaddr,uint64_t iova,uint64_t len); +int rte_vfio_dma_unmap (uint64_t vaddr,uint64_t iova,uint64_t len); int rte_vfio_setup_device(__rte_unused const char *sysfs_base, __rte_unused const char *dev_addr,
@@ -790,3 +792,17 @@ int rte_vfio_clear_group(__rte_unused int vfio_group_fd) {return 0 ; } + +int +rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova, + __rte_unused uint64_t len) +{ +return -1 ; +} + +int +rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova, + __rte_unused uint64_t len) +{ +return -1 ; +}
lib/librte_eal/common/include/rte_vfio.h +39 -0
@@ -123,6 +123,45 @@ int rte_vfio_noiommu_is_enabled(void);int rte_vfio_clear_group (int vfio_group_fd); +/** +* Map memory region for use with VFIO. +* +* @param vaddr +* Starting virtual address of memory to be mapped. +* +* @param iova +* Starting IOVA address of memory to be mapped. +* +* @param len +* Length of memory segment being mapped. +* +* @return +* 0 if success. +* -1 on error. +*/ +int +rte_vfio_dma_map (uint64_t vaddr,uint64_t iova,uint64_t len); + + +/** +* Unmap memory region from VFIO. +* +* @param vaddr +* Starting virtual address of memory to be unmapped. +* +* @param iova +* Starting IOVA address of memory to be unmapped. +* +* @param len +* Length of memory segment being unmapped. +* +* @return +* 0 if success. +* -1 on error. +*/ +int +rte_vfio_dma_unmap (uint64_t vaddr,uint64_t iova,uint64_t len); +#endif /* VFIO_PRESENT */ #endif /* _RTE_VFIO_H_ */
lib/librte_eal/linuxapp/eal/eal_vfio.c +130 -23
208 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/eal_vfio.h +11 -0
@@ -19,6 +19,7 @@#ifdef VFIO_PRESENT +#include <stdint.h> #include <linux/vfio.h> #define RTE_VFIO_TYPE1 VFIO_TYPE1_IOMMU
@@ -110,6 +111,7 @@ struct vfio_config {int vfio_enabled;int vfio_container_fd;int vfio_active_groups; +const struct vfio_iommu_type *vfio_iommu_type;struct vfio_group vfio_groups[VFIO_MAX_GROUPS ]; };
@@ -119,9 +121,18 @@ struct vfio_config {* */ typedef int (*vfio_dma_func_t )(int ); +/* Custom memory region DMA mapping function prototype. +* Takes VFIO container fd, virtual address, phisical address, length and +* operation type (0 to unmap 1 for map) as a parameters. +* Returns 0 on success, -1 on error. +**/ +typedef int (*vfio_dma_user_func_t )(int fd,uint64_t vaddr,uint64_t iova, +uint64_t len,int do_map); +struct vfio_iommu_type {int type_id;const char *name; +vfio_dma_user_func_t dma_user_map_func;vfio_dma_func_t dma_map_func; };
Signature · 1 line
2.7.4
PATCH v2 24/41 vfio: allow to map other memory regions
From: Gowrishankar Muthukrishnan <[email protected]>
Below patch adds powerpc arch specific changes.
--- lib/librte_eal/linuxapp/eal/eal_vfio.c | 63 +++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 16 deletions(-)
lib/librte_eal/linuxapp/eal/eal_vfio.c +47 -16
@@ -24,6 +24,7 @@static int vfio_type1_dma_map (int );static int vfio_type1_dma_mem_map (int ,uint64_t ,uint64_t ,uint64_t ,int );static int vfio_spapr_dma_map (int ); +static int vfio_spapr_dma_mem_map (int ,uint64_t ,uint64_t ,uint64_t ,int );static int vfio_noiommu_dma_map (int );static int vfio_noiommu_dma_mem_map (int ,uint64_t ,uint64_t ,uint64_t ,int );
@@ -41,8 +42,7 @@ .type_id = RTE_VFIO_SPAPR, .name = "sPAPR", .dma_map_func = &vfio_spapr_dma_map, - .dma_user_map_func =NULL -// TODO: work with PPC64 people on enabling this, window size! + .dma_user_map_func = &vfio_spapr_dma_mem_map },/* IOMMU-less mode */ {
@@ -801,10 +801,51 @@ struct spapr_create_window_walk_param { }static int +vfio_spapr_dma_mem_map (int vfio_container_fd,uint64_t vaddr,uint64_t iova, +uint64_t len,int do_map) +{ +struct vfio_iommu_type1_dma_map dma_map; +struct vfio_iommu_type1_dma_unmap dma_unmap; +int ret; + +if (do_map !=0 ) { +memset (&dma_map,0 ,sizeof (dma_map)); + dma_map.argsz =sizeof (struct vfio_iommu_type1_dma_map ); + dma_map.vaddr = vaddr; + dma_map.size = len; + dma_map.iova = iova; + dma_map.flags =VFIO_DMA_MAP_FLAG_READ | +VFIO_DMA_MAP_FLAG_WRITE ; + + ret =ioctl (vfio_container_fd,VFIO_IOMMU_MAP_DMA , &dma_map); +if (ret) { +RTE_LOG (ERR ,EAL ," cannot set up DMA remapping, error %i (%s)\n" , + errno,strerror (errno)); +return -1 ; + } + + }else { +memset (&dma_unmap,0 ,sizeof (dma_unmap)); + dma_unmap.argsz =sizeof (struct vfio_iommu_type1_dma_unmap ); + dma_unmap.size = len; + dma_unmap.iova = iova; + + ret =ioctl (vfio_container_fd,VFIO_IOMMU_UNMAP_DMA , + &dma_unmap); +if (ret) { +RTE_LOG (ERR ,EAL ," cannot clear DMA remapping, error %i (%s)\n" , + errno,strerror (errno)); +return -1 ; + } + } + +return 0 ; +} + +static int vfio_spapr_dma_map_walk(const struct rte_memseg_list *msl __rte_unused,const struct rte_memseg *ms,void *arg) { -struct vfio_iommu_type1_dma_map dma_map;struct vfio_iommu_spapr_register_memory reg = { .argsz =sizeof (reg), .flags =0
@@ -828,17 +869,8 @@ struct spapr_create_window_walk_param {return -1 ; } -memset (&dma_map,0 ,sizeof (dma_map)); - dma_map.argsz =sizeof (struct vfio_iommu_type1_dma_map ); - dma_map.vaddr = addr; - dma_map.size = len; - dma_map.iova = hw_addr; - dma_map.flags =VFIO_DMA_MAP_FLAG_READ | -VFIO_DMA_MAP_FLAG_WRITE ; - - ret =ioctl (*vfio_container_fd,VFIO_IOMMU_MAP_DMA , - &dma_map); - + ret =vfio_spapr_dma_mem_map (*vfio_container_fd, addr, + hw_addr, len,1 );if (ret) {RTE_LOG (ERR ,EAL ," cannot set up DMA remapping, error %i (%s)\n" , errno,strerror (errno));
@@ -852,7 +884,6 @@ struct spapr_create_window_walk_param { vfio_spapr_dma_map(int vfio_container_fd) {int ret; -uint64_t hugepage_sz =0 ;struct spapr_create_window_walk_param wa;struct vfio_iommu_spapr_tce_info info = {
@@ -890,7 +921,7 @@ struct spapr_create_window_walk_param {/* sPAPR requires window size to be a power of 2 */ create.window_size =rte_align64pow2 (create.window_size); - create.page_shift =__builtin_ctzll (hugepage_sz); + create.page_shift =__builtin_ctzll (wa.hugepage_sz); create.levels =1 ; ret =ioctl (vfio_container_fd,VFIO_IOMMU_SPAPR_TCE_CREATE , &create);
Signature · 1 line
1.9.1
PATCH v2 24/41 vfio: allow to map other memory regions
In reply to Anatoly BurakovFrom: Gowrishankar Muthukrishnan <[email protected]>
Below patch adds powerpc arch specific changes.
--- lib/librte_eal/linuxapp/eal/eal_vfio.c | 110 +++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 5 deletions(-)
lib/librte_eal/linuxapp/eal/eal_vfio.c +105 -5
@@ -24,6 +24,7 @@static int vfio_type1_dma_map (int );static int vfio_type1_dma_mem_map (int ,uint64_t ,uint64_t ,uint64_t ,int );static int vfio_spapr_dma_map (int ); +static int vfio_spapr_dma_mem_map (int ,uint64_t ,uint64_t ,uint64_t ,int );static int vfio_noiommu_dma_map (int );static int vfio_noiommu_dma_mem_map (int ,uint64_t ,uint64_t ,uint64_t ,int );
@@ -41,8 +42,7 @@ .type_id = RTE_VFIO_SPAPR, .name = "sPAPR", .dma_map_func = &vfio_spapr_dma_map, - .dma_user_map_func =NULL -// TODO: work with PPC64 people on enabling this, window size! + .dma_user_map_func = &vfio_spapr_dma_mem_map },/* IOMMU-less mode */ {
@@ -838,7 +838,6 @@ struct spapr_create_window_walk_param { ret =ioctl (*vfio_container_fd,VFIO_IOMMU_MAP_DMA , &dma_map); -if (ret) {RTE_LOG (ERR ,EAL ," cannot set up DMA remapping, error %i (%s)\n" , errno,strerror (errno));
@@ -852,7 +851,6 @@ struct spapr_create_window_walk_param { vfio_spapr_dma_map(int vfio_container_fd) {int ret; -uint64_t hugepage_sz =0 ;struct spapr_create_window_walk_param wa;struct vfio_iommu_spapr_tce_info info = {
@@ -890,7 +888,7 @@ struct spapr_create_window_walk_param {/* sPAPR requires window size to be a power of 2 */ create.window_size =rte_align64pow2 (create.window_size); - create.page_shift =__builtin_ctzll (hugepage_sz); + create.page_shift =__builtin_ctzll (wa.hugepage_sz); create.levels =1 ; ret =ioctl (vfio_container_fd,VFIO_IOMMU_SPAPR_TCE_CREATE , &create);
@@ -912,6 +910,108 @@ struct spapr_create_window_walk_param { }static int +vfio_spapr_dma_mem_map (int vfio_container_fd,uint64_t vaddr,uint64_t iova, +uint64_t len,int do_map) +{ +int ret; +struct spapr_create_window_walk_param wa = { + .hugepage_sz =0 , + }; +struct vfio_iommu_spapr_tce_create create = { + .argsz =sizeof (create), + }; + +/* check if DMA window is from 0 to max(phys_addr + len) */ + wa.create = &create; +rte_memseg_walk (vfio_spapr_create_window_walk, &wa); + create.window_size =rte_align64pow2 (create.window_size); +if (iova > create.window_size) { +struct vfio_iommu_spapr_tce_info info = { + .argsz =sizeof (info), + }; +struct vfio_iommu_spapr_tce_remove remove = { + .argsz =sizeof (remove), + }; + +/* query spapr iommu info */ + ret =ioctl (vfio_container_fd, +VFIO_IOMMU_SPAPR_TCE_GET_INFO , &info); +if (ret) { +RTE_LOG (ERR ,EAL ," cannot get iommu info, " +"error %i (%s)\n" , errno,strerror (errno)); +return -1 ; + } + +/* remove old DMA window */ + remove.start_addr = info.dma32_window_start; + ret =ioctl (vfio_container_fd, +VFIO_IOMMU_SPAPR_TCE_REMOVE , &remove); +if (ret) { +RTE_LOG (ERR ,EAL ," cannot remove default DMA window, " +"error %i (%s)\n" , errno,strerror (errno)); +return -1 ; + } + create.page_shift =__builtin_ctzll (wa.hugepage_sz); + create.levels =1 ; + + ret =ioctl (vfio_container_fd, +VFIO_IOMMU_SPAPR_TCE_CREATE , &create); +if (ret) { +RTE_LOG (ERR ,EAL ," cannot create new DMA window, " +"error %i (%s)\n" , errno,strerror (errno)); +return -1 ; + } + +if (create.start_addr !=0 ) { +RTE_LOG (ERR ,EAL ," DMA window start address != 0\n" ); +return -1 ; + } + + } + +if (do_map !=0 ) { +if (rte_memseg_walk (vfio_spapr_dma_map_walk, &vfio_container_fd)) +return -1 ; + }else { +struct vfio_iommu_type1_dma_unmap dma_unmap; +struct vfio_iommu_spapr_register_memory reg = { + .argsz =sizeof (reg), + .flags =0 + }; + +/* for unmap, check if iova within DMA window */ +if (iova > create.window_size) { +RTE_LOG (ERR ,EAL ,"iova not beyond DMA window for unmap" ); +return -1 ; + } + + reg.vaddr = (uintptr_t ) vaddr; + reg.size = len; + ret =ioctl (vfio_container_fd, +VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY , ®); +if (ret) { +RTE_LOG (ERR ,EAL ," cannot unregister vaddr for IOMMU, error %i (%s)\n" , + errno,strerror (errno)); +return -1 ; + } + +memset (&dma_unmap,0 ,sizeof (dma_unmap)); + dma_unmap.argsz =sizeof (struct vfio_iommu_type1_dma_unmap ); + dma_unmap.size = len; + dma_unmap.iova = iova; + + ret =ioctl (vfio_container_fd,VFIO_IOMMU_UNMAP_DMA , + &dma_unmap); +if (ret) { +RTE_LOG (ERR ,EAL ," cannot clear DMA remapping, error %i (%s)\n" , + errno,strerror (errno)); +return -1 ; + } + } +return 0 ; +} + +static int vfio_noiommu_dma_map(int __rte_unused vfio_container_fd) {/* No-IOMMU mode does not need DMA mapping */
Signature · 1 line
1.9.1
PATCH v2 25/41 eal: map/unmap memory with VFIO when alloc/free pages
In reply to Anatoly Burakov--- lib/librte_eal/linuxapp/eal/eal_memalloc.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
lib/librte_eal/linuxapp/eal/eal_memalloc.c +11 -0
@@ -34,6 +34,7 @@#include <rte_eal.h> #include <rte_memory.h> #include <rte_spinlock.h> +#include <rte_vfio.h> #include "eal_filesystem.h" #include "eal_internal_cfg.h"
@@ -476,6 +477,10 @@ alloc_page(struct rte_memseg *ms, void *addr, uint64_t size, int socket_id, ms->iova = iova; ms->socket_id = socket_id; +/* map the segment so that VFIO has access to it */ +if (rte_eal_iova_mode () ==RTE_IOVA_VA && +rte_vfio_dma_map (ms->addr_64, iova, size)) +RTE_LOG (DEBUG ,EAL ,"Cannot register segment with VFIO\n" );return 0 ; mapped:
@@ -507,6 +512,12 @@ free_page(struct rte_memseg *ms, struct hugepage_info *hi,char path[PATH_MAX ];int fd, ret; +/* unmap the segment from VFIO */ +if (rte_eal_iova_mode () ==RTE_IOVA_VA && +rte_vfio_dma_unmap (ms->addr_64, ms->iova, ms->len)) { +RTE_LOG (DEBUG ,EAL ,"Cannot unregister segment with VFIO\n" ); + } +if (mmap (ms->addr, ms->hugepage_sz,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ) ==MAP_FAILED ) {
Signature · 1 line
2.7.4
PATCH v2 26/41 eal: prepare memseg lists for multiprocess sync
In reply to Anatoly BurakovIn preparation for implementing multiprocess support, we are adding a version number and write locks to memseg lists.
There are two ways of implementing multiprocess support for memory
hotplug: either all information about mapped memory is shared between processes, and secondary processes simply attempt to map/unmap memory based on requests from the primary, or secondary processes store their own maps and only check if they are in sync with the primary process' maps.
This implementation will opt for the latter option: primary process shared mappings will be authoritative, and each secondary process will use its own interal view of mapped memory, and will attempt to synchronize on these mappings using versioning.
Under this model, only primary process will decide which pages get mapped, and secondary processes will only copy primary's page maps and get notified of the changes via IPC mechanism (coming in later commits).
To avoid race conditions, memseg lists will also have write locks - that is, it will be possible for several secondary processes to initialize concurrently, but it will not be possible for several processes to request memory allocation unless all other allocations were complete (on a single socket - it is OK to allocate/free memory on different sockets concurrently).
In principle, it is possible for multiple processes to request allocation/deallcation on multiple sockets, but we will only allow one such request to be active at any one time.
--- lib/librte_eal/bsdapp/eal/eal_memalloc.c | 7 + lib/librte_eal/common/eal_memalloc.h | 4 + lib/librte_eal/common/include/rte_eal_memconfig.h | 2 + lib/librte_eal/linuxapp/eal/eal_memalloc.c | 288 +++++++++++++++++++++- 4 files changed, 295 insertions(+), 6 deletions(-)
lib/librte_eal/bsdapp/eal/eal_memalloc.c +7 -0
@@ -24,3 +24,10 @@ eal_memalloc_alloc_page(uint64_t __rte_unused size, int __rte_unused socket)RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" );return NULL ; } + +int +eal_memalloc_sync_with_primary (void ) +{ +RTE_LOG (ERR ,EAL ,"Memory hotplug not supported on FreeBSD\n" ); +return -1 ; +}
lib/librte_eal/common/eal_memalloc.h +4 -0
@@ -24,4 +24,8 @@ bool eal_memalloc_is_contig(struct rte_memseg_list *msl, void *start, size_t len); +/* synchronize local memory map to primary process */ +int +eal_memalloc_sync_with_primary (void ); +#endif // EAL_MEMALLOC_H
lib/librte_eal/common/include/rte_eal_memconfig.h +2 -0
@@ -32,6 +32,8 @@ struct rte_memseg_list { };int socket_id;/**< Socket ID for all memsegs in this list. */ uint64_t hugepage_sz;/**< page size for all memsegs in this list. */ +rte_rwlock_t mplock;/**< read-write lock for multiprocess sync. */ +uint32_t version;/**< version number for multiprocess sync. */ struct rte_fbarray memseg_arr; };
lib/librte_eal/linuxapp/eal/eal_memalloc.c +282 -6
336 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 27/41 eal: add multiprocess init with memory hotplug
In reply to Anatoly Burakovfor legacy memory mode, attach to primary's memseg list, and map hugepages as before.
for non-legacy mode, preallocate all VA space and then do a sync of local memory map.
--- lib/librte_eal/bsdapp/eal/eal_hugepage_info.c | 7 ++ lib/librte_eal/common/eal_common_memory.c | 99 +++++++++++++++++++++---- lib/librte_eal/common/eal_hugepages.h | 5 ++ lib/librte_eal/linuxapp/eal/eal.c | 18 +++-- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 53 ++++++++----- lib/librte_eal/linuxapp/eal/eal_memory.c | 24 ++++-- 6 files changed, 159 insertions(+), 47 deletions(-)
lib/librte_eal/bsdapp/eal/eal_hugepage_info.c +7 -0
@@ -103,3 +103,10 @@ eal_hugepage_info_init(void)return 0 ; } + +/* memory hotplug is not supported in FreeBSD, so no need to implement this */ +int +eal_hugepage_info_read (void ) +{ +return 0 ; +}
lib/librte_eal/common/eal_common_memory.c +84 -15
151 lines not on this page · raw message
lib/librte_eal/common/eal_hugepages.h +5 -0
@@ -34,4 +34,9 @@ struct hugepage_file {*/ int eal_hugepage_info_init (void ); +/** +* Read information about hugepages on Linux, but don't clear them out. +*/ +int eal_hugepage_info_read (void ); +#endif /* EAL_HUGEPAGES_H */
lib/librte_eal/linuxapp/eal/eal.c +11 -7
@@ -805,13 +805,17 @@ rte_eal_init(int argc, char **argv) "KNI module inserted\n"); } -if (internal_config.no_hugetlbfs ==0 && - internal_config.process_type !=RTE_PROC_SECONDARY && -eal_hugepage_info_init () <0 ) { -rte_eal_init_alert ("Cannot get hugepage information." ); - rte_errno =EACCES ; -rte_atomic32_clear (&run_once); -return -1 ; +if (internal_config.no_hugetlbfs ==0 ) { +/* rte_config isn't initialized yet */ + ret = internal_config.process_type ==RTE_PROC_PRIMARY ? +eal_hugepage_info_init () : +eal_hugepage_info_read (); +if (ret <0 ) { +rte_eal_init_alert ("Cannot get hugepage information." ); + rte_errno =EACCES ; +rte_atomic32_clear (&run_once); +return -1 ; + } }if (internal_config.memory ==0 && internal_config.force_sockets ==0 ) {
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +33 -20
82 lines not on this page · raw message
lib/librte_eal/linuxapp/eal/eal_memory.c +19 -5
@@ -1569,6 +1569,22 @@ eal_legacy_hugepage_attach(void)return -1 ; } +static int +eal_hugepage_attach (void ) +{ +if (eal_memalloc_sync_with_primary ()) { +RTE_LOG (ERR ,EAL ,"Could not map memory from primary process\n" ); +if (aslr_enabled () >0 ) { +RTE_LOG (ERR ,EAL ,"It is recommended to " +"disable ASLR in the kernel " +"and retry running both primary " +"and secondary processes\n" ); + } +return -1 ; + } +return 0 ; +} +int rte_eal_hugepage_init (void ) {
@@ -1580,11 +1596,9 @@ rte_eal_hugepage_init(void)int rte_eal_hugepage_attach (void ) { -if (internal_config.legacy_mem) -return eal_legacy_hugepage_attach (); -else -RTE_LOG (ERR ,EAL ,"Secondary processes aren't supported yet\n" ); -return -1 ; +return internal_config.legacy_mem ? +eal_legacy_hugepage_attach () : +eal_hugepage_attach (); }int
Signature · 1 line
2.7.4
PATCH v2 28/41 eal: add support for multiprocess memory hotplug
In reply to Anatoly BurakovThis enables multiprocess synchronization for memory hotplug requests at runtime (as opposed to initialization).
Basic workflow is the following. Primary process always does initial mapping and unmapping, and secondary processes always follow primary page map. Only one allocation request can be active at any one time.
When primary allocates memory, it ensures that all other processes have allocated the same set of hugepages successfully, otherwise any allocations made are being rolled back, and heap is freed back. Heap is locked throughout the process, so no race conditions can happen.
When primary frees memory, it frees the heap, deallocates affected pages, and notifies other processes of deallocations. Since heap is freed from that memory chunk, the area basically becomes invisible to other processes even if they happen to fail to unmap that specific set of pages, so it's completely safe to ignore results of sync requests.
When secondary allocates memory, it does not do so by itself. Instead, it sends a request to primary process to try and allocate pages of specified size and on specified socket, such that a specified heap allocation request could complete. Primary process then sends all secondaries (including the requestor) a separate notification of allocated pages, and expects all secondary processes to report success before considering pages as "allocated".
Only after primary process ensures that all memory has been successfully allocated in all secondary process, it will respond positively to the initial request, and let secondary proceed with the allocation. Since the heap now has memory that can satisfy allocation request, and it was locked all this time (so no other allocations could take place), secondary process will be able to allocate memory from the heap.
When secondary frees memory, it hides pages to be deallocated from the heap. Then, it sends a deallocation request to primary process, so that it deallocates pages itself, and then sends a separate sync request to all other processes (including the requestor) to unmap the same pages. This way, even if secondary fails to notify other processes of this deallocation, that memory will become invisible to other processes, and will not be allocated from again.
So, to summarize: address space will only become part of the heap if primary process can ensure that all other processes have allocated this memory successfully. If anything goes wrong, the worst thing that could happen is that a page will "leak" and will not be available to neither DPDK nor the system, as some process will still hold onto it. It's not an actual leak, as we can account for the page - it's just that none of the processes will be able to use this page for anything useful, until it gets allocated from by the primary.
Due to underlying DPDK IPC implementation being single-threaded, some asynchronous magic had to be done, as we need to complete several requests before we can definitively allow secondary process to use allocated memory (namely, it has to be present in all other secondary processes before it can be used). Additionally, only one allocation request is allowed to be submitted at once.
Memory allocation requests are only allowed when there are no secondary processes currently initializing. To enforce that, a shared rwlock is used, that is set to read lock on init (so that several secondaries could initialize concurrently), and write lock on making allocation requests (so that either secondary init will have to wait, or allocation request will have to wait until all processes have initialized).
---
Notes:
v2: - fixed deadlocking on init problem
- reverted rte_panic changes (fixed by changes in IPC instead)
This problem is evidently complex to solve without multithreaded IPC implementation. An alternative approach would be to process each individual message in its own thread (or at least spawn a thread per incoming request) - that way, we can send requests while responding to another request, and this problem becomes trivial to solve (and in fact it was solved that way initially, before my aversion to certain other programming languages kicked in).
Is the added complexity worth saving a couple of thread spin-ups here and there?
lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/common/eal_common_memory.c | 16 +- lib/librte_eal/common/include/rte_eal_memconfig.h | 3 + lib/librte_eal/common/malloc_heap.c | 255 ++++++-- lib/librte_eal/common/malloc_mp.c | 723 ++++++++++++++++++++++ lib/librte_eal/common/malloc_mp.h | 86 +++ lib/librte_eal/common/meson.build | 1 + lib/librte_eal/linuxapp/eal/Makefile | 1 + 8 files changed, 1040 insertions(+), 46 deletions(-) create mode 100644 lib/librte_eal/common/malloc_mp.c create mode 100644 lib/librte_eal/common/malloc_mp.h
lib/librte_eal/bsdapp/eal/Makefile +1 -0
@@ -59,6 +59,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_heap.c +SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += malloc_mp.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_keepalive.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_service.c SRCS-$(CONFIG_RTE_EXEC_ENV_BSDAPP) += rte_reciprocal.c
lib/librte_eal/common/eal_common_memory.c +13 -3
@@ -543,24 +543,34 @@ rte_mem_lock_page(const void *virt)int rte_eal_memory_init (void ) { +struct rte_mem_config *mcfg =rte_eal_get_configuration ()->mem_config;int retval;RTE_LOG (DEBUG ,EAL ,"Setting up physically contiguous memory...\n" ); +if (!mcfg) +return -1 ; + +/* lock mem hotplug here, to prevent races while we init */ +rte_rwlock_read_lock (&mcfg->memory_hotplug_lock); + retval =rte_eal_process_type () ==RTE_PROC_PRIMARY ?memseg_primary_init () :memseg_secondary_init ();if (retval <0 ) -return -1 ; + goto fail; retval =rte_eal_process_type () ==RTE_PROC_PRIMARY ?rte_eal_hugepage_init () :rte_eal_hugepage_attach ();if (retval <0 ) -return -1 ; + goto fail;if (internal_config.no_shconf ==0 &&rte_eal_memdevice_init () <0 ) -return -1 ; + goto fail;return 0 ; +fail: +rte_rwlock_read_unlock (&mcfg->memory_hotplug_lock); +return -1 ; }
lib/librte_eal/common/include/rte_eal_memconfig.h +3 -0
@@ -60,6 +60,9 @@ struct rte_mem_config {rte_rwlock_t qlock;/**< used for tailq operation for thread safe. */ rte_rwlock_t mplock;/**< only used by mempool LIB for thread-safe. */ +rte_rwlock_t memory_hotplug_lock; +/**< indicates whether memory hotplug request is in progress. */ +/* memory segments and zones */ struct rte_fbarray memzones;/**< Memzone descriptors. */
lib/librte_eal/common/malloc_heap.c +212 -43
387 lines not on this page · raw message
lib/librte_eal/common/malloc_mp.c new file +723 -0
723 lines not on this page · raw message
lib/librte_eal/common/malloc_mp.h new file +86 -0
86 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -27,6 +27,7 @@ common_sources = files( 'eal_common_timer.c', 'malloc_elem.c', 'malloc_heap.c', + 'malloc_mp.c', 'rte_keepalive.c', 'rte_malloc.c', 'rte_reciprocal.c',
lib/librte_eal/linuxapp/eal/Makefile +1 -0
@@ -67,6 +67,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += eal_common_fbarray.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_malloc.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_elem.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_heap.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += malloc_mp.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_keepalive.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_service.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUXAPP) += rte_reciprocal.c
Signature · 1 line
2.7.4
Re: [PATCH v2 28/41] eal: add support for multiprocess memory hotplug
Quoted 99 lines · Anatoly Burakov
This enables multiprocess synchronization for memory hotplug requests at runtime (as opposed to initialization).
Basic workflow is the following. Primary process always does initial mapping and unmapping, and secondary processes always follow primary page map. Only one allocation request can be active at any one time.
When primary allocates memory, it ensures that all other processes have allocated the same set of hugepages successfully, otherwise any allocations made are being rolled back, and heap is freed back. Heap is locked throughout the process, so no race conditions can happen.
When primary frees memory, it frees the heap, deallocates affected pages, and notifies other processes of deallocations. Since heap is freed from that memory chunk, the area basically becomes invisible to other processes even if they happen to fail to unmap that specific set of pages, so it's completely safe to ignore results of sync requests.
When secondary allocates memory, it does not do so by itself. Instead, it sends a request to primary process to try and allocate pages of specified size and on specified socket, such that a specified heap allocation request could complete. Primary process then sends all secondaries (including the requestor) a separate notification of allocated pages, and expects all secondary processes to report success before considering pages as "allocated".
Only after primary process ensures that all memory has been successfully allocated in all secondary process, it will respond positively to the initial request, and let secondary proceed with the allocation. Since the heap now has memory that can satisfy allocation request, and it was locked all this time (so no other allocations could take place), secondary process will be able to allocate memory from the heap.
When secondary frees memory, it hides pages to be deallocated from the heap. Then, it sends a deallocation request to primary process, so that it deallocates pages itself, and then sends a separate sync request to all other processes (including the requestor) to unmap the same pages. This way, even if secondary fails to notify other processes of this deallocation, that memory will become invisible to other processes, and will not be allocated from again.
So, to summarize: address space will only become part of the heap if primary process can ensure that all other processes have allocated this memory successfully. If anything goes wrong, the worst thing that could happen is that a page will "leak" and will not be available to neither DPDK nor the system, as some process will still hold onto it. It's not an actual leak, as we can account for the page - it's just that none of the processes will be able to use this page for anything useful, until it gets allocated from by the primary.
Due to underlying DPDK IPC implementation being single-threaded, some asynchronous magic had to be done, as we need to complete several requests before we can definitively allow secondary process to use allocated memory (namely, it has to be present in all other secondary processes before it can be used). Additionally, only one allocation request is allowed to be submitted at once.
Memory allocation requests are only allowed when there are no secondary processes currently initializing. To enforce that, a shared rwlock is used, that is set to read lock on init (so that several secondaries could initialize concurrently), and write lock on making allocation requests (so that either secondary init will have to wait, or allocation request will have to wait until all processes have initialized).
Signed-off-by Anatoly Burakov <[email protected]>---
Notes:
v2: - fixed deadlocking on init problem
- revertedrte_panicchanges (fixed by changes in IPC instead)This problem is evidently complex to solve without multithreaded IPC implementation. An alternative approach would be to process each individual message in its own thread (or at least spawn a thread per incoming request) - that way, we can send requests while responding to another request, and this problem becomes trivial to solve (and in fact it was solved that way initially, before my aversion to certain other programming languages kicked in).
Is the added complexity worth saving a couple of thread spin-ups here and there?
lib/librte_eal/bsdapp/eal/Makefile | 1 + lib/librte_eal/common/eal_common_memory.c | 16 +- lib/librte_eal/common/include/rte_eal_memconfig.h | 3 + lib/librte_eal/common/malloc_heap.c | 255 ++++++-- lib/librte_eal/common/malloc_mp.c | 723 ++++++++++++++++++++++ lib/librte_eal/common/malloc_mp.h | 86 +++ lib/librte_eal/common/meson.build | 1 + lib/librte_eal/linuxapp/eal/Makefile | 1 + 8 files changed, 1040 insertions(+), 46 deletions(-)create mode 100644
lib/librte_eal/common/malloc_mp.ccreate mode 100644lib/librte_eal/common/malloc_mp.h
...
lib/librte_eal/common/malloc_mp.c line 323
Quoted 5 lines
lib/librte_eal/common/malloc_mp.c new file +5
-0+/* callback for asynchronous sync requests for primary. this will either do a +* sendmsg with results, or trigger rollback request. +*/ +static int +handle_sync_response(const struct rte_mp_msg *request,
Rename to handle_async_response()?
PATCH v2 29/41 eal: add support for callbacks on memory hotplug
In reply to Anatoly BurakovEach process will have its own callbacks. Callbacks will indicate whether it's allocation and deallocation that's happened, and will also provide start VA address and length of allocated block.
Since memory hotplug isn't supported on FreeBSD and in legacy mem mode, it will not be possible to register them in either.
--- lib/librte_eal/common/eal_common_memalloc.c | 132 ++++++++++++++++++++++++++++ lib/librte_eal/common/eal_common_memory.c | 28 ++++++ lib/librte_eal/common/eal_memalloc.h | 10 +++ lib/librte_eal/common/include/rte_memory.h | 48 ++++++++++ lib/librte_eal/rte_eal_version.map | 2 + 5 files changed, 220 insertions(+)
lib/librte_eal/common/eal_common_memalloc.c +132 -0
@@ -2,16 +2,46 @@* Copyright(c) 2017-2018 Intel Corporation */ +#include <string.h> + +#include <rte_errno.h> #include <rte_lcore.h> #include <rte_fbarray.h> #include <rte_memzone.h> #include <rte_memory.h> #include <rte_eal_memconfig.h> +#include <rte_rwlock.h> #include "eal_private.h" #include "eal_internal_cfg.h" #include "eal_memalloc.h" +struct mem_event_callback_entry { +TAILQ_ENTRY (mem_event_callback_entry) next; +char name[RTE_MEM_EVENT_CALLBACK_NAME_LEN ]; +rte_mem_event_callback_t clb; +}; + +/** Double linked list of actions. */ +TAILQ_HEAD (mem_event_callback_entry_list, mem_event_callback_entry); + +static struct mem_event_callback_entry_list callback_list = +TAILQ_HEAD_INITIALIZER (callback_list); + +static rte_rwlock_t rwlock =RTE_RWLOCK_INITIALIZER ; + +static struct mem_event_callback_entry * +find_callback (const char *name) +{ +struct mem_event_callback_entry *r; + + TAILQ_FOREACH(r, &callback_list, next) { +if (!strcmp (r->name, name)) +break ; + } +return r; +} +bool eal_memalloc_is_contig (struct rte_memseg_list *msl,void *start,size_t len)
@@ -47,3 +77,105 @@ eal_memalloc_is_contig(struct rte_memseg_list *msl, void *start,return true; } + +int +eal_memalloc_callback_register (const char *name, +rte_mem_event_callback_t clb) +{ +struct mem_event_callback_entry *entry; +int ret, len; +if (name ==NULL || clb ==NULL ) { + rte_errno =EINVAL ; +return -1 ; + } + len =strnlen (name,RTE_MEM_EVENT_CALLBACK_NAME_LEN ); +if (len ==0 ) { + rte_errno =EINVAL ; +return -1 ; + }else if (len ==RTE_MEM_EVENT_CALLBACK_NAME_LEN ) { + rte_errno =ENAMETOOLONG ; +return -1 ; + } +rte_rwlock_write_lock (&rwlock); + + entry =find_callback (name); +if (entry !=NULL ) { + rte_errno =EEXIST ; + ret =-1 ; + goto unlock; + } + + entry =malloc (sizeof (*entry)); +if (entry ==NULL ) { + rte_errno =ENOMEM ; + ret =-1 ; + goto unlock; + } + +/* callback successfully created and is valid, add it to the list */ + entry->clb = clb; +snprintf (entry->name,RTE_MEM_EVENT_CALLBACK_NAME_LEN ,"%s" , name); +TAILQ_INSERT_TAIL (&callback_list, entry, next); + + ret =0 ; + +RTE_LOG (DEBUG ,EAL ,"Mem event callback '%s' registered\n" , name); + +unlock: +rte_rwlock_write_unlock (&rwlock); +return ret; +} + +int +eal_memalloc_callback_unregister (const char *name) +{ +struct mem_event_callback_entry *entry; +int ret, len; + +if (name ==NULL ) { + rte_errno =EINVAL ; +return -1 ; + } + len =strnlen (name,RTE_MEM_EVENT_CALLBACK_NAME_LEN ); +if (len ==0 ) { + rte_errno =EINVAL ; +return -1 ; + }else if (len ==RTE_MEM_EVENT_CALLBACK_NAME_LEN ) { + rte_errno =ENAMETOOLONG ; +return -1 ; + } +rte_rwlock_write_lock (&rwlock); + + entry =find_callback (name); +if (entry ==NULL ) { + rte_errno =ENOENT ; + ret =-1 ; + goto unlock; + } +TAILQ_REMOVE (&callback_list, entry, next); +free (entry); + + ret =0 ; + +RTE_LOG (DEBUG ,EAL ,"Mem event callback '%s' unregistered\n" , name); + +unlock: +rte_rwlock_write_unlock (&rwlock); +return ret; +} + +void +eal_memalloc_notify (enum rte_mem_event event,const void *start,size_t len) +{ +struct mem_event_callback_entry *entry; + +rte_rwlock_read_lock (&rwlock); + + TAILQ_FOREACH(entry, &callback_list, next) { +RTE_LOG (DEBUG ,EAL ,"Calling mem event callback %s" , + entry->name); + entry->clb (event, start, len); + } + +rte_rwlock_read_unlock (&rwlock); +}
lib/librte_eal/common/eal_common_memory.c +28 -0
@@ -466,6 +466,34 @@ rte_eal_get_physmem_size(void)return total_len; } +/* +* Defining here because declared in rte_memory.h, but the actual implementation +* is in eal_common_memalloc.c, like all other memalloc internals. +*/ +int +rte_mem_event_register_callback (const char *name,rte_mem_event_callback_t clb) +{ +/* FreeBSD boots with legacy mem enabled by default */ +if (internal_config.legacy_mem) { +RTE_LOG (DEBUG ,EAL ,"Registering mem event callbacks not supported\n" ); + rte_errno =ENOTSUP ; +return -1 ; + } +return eal_memalloc_callback_register (name, clb); +} + +int +rte_mem_event_unregister_callback (const char *name) +{ +/* FreeBSD boots with legacy mem enabled by default */ +if (internal_config.legacy_mem) { +RTE_LOG (DEBUG ,EAL ,"Registering mem event callbacks not supported\n" ); + rte_errno =ENOTSUP ; +return -1 ; + } +return eal_memalloc_callback_unregister (name); +} +/* Dump the physical memory layout on console */ void rte_dump_physmem_layout (FILE *f)
lib/librte_eal/common/eal_memalloc.h +10 -0
@@ -28,4 +28,14 @@ eal_memalloc_is_contig(struct rte_memseg_list *msl, void *start,int eal_memalloc_sync_with_primary (void ); +int +eal_memalloc_callback_register (const char *name, +rte_mem_event_callback_t clb); + +int +eal_memalloc_callback_unregister (const char *name); + +void +eal_memalloc_notify (enum rte_mem_event event,const void *start,size_t len); +#endif // EAL_MEMALLOC_H
lib/librte_eal/common/include/rte_memory.h +48 -0
54 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -0
@@ -214,6 +214,8 @@ DPDK_18.05 { global: rte_num_sockets; + rte_mem_event_callback_register; + rte_mem_event_callback_unregister; rte_mem_virt2memseg; rte_mem_virt2memseg_list; rte_malloc_dump_heaps;
Signature · 1 line
2.7.4
PATCH v2 30/41 eal: enable callbacks on malloc/free and mp sync
In reply to Anatoly BurakovAlso, rewrite VFIO to rely on memory callbacks instead of manually registering memory with VFIO. Callbacks will only be registered if VFIO is enabled.
--- lib/librte_eal/common/malloc_heap.c | 21 +++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_memalloc.c | 37 +++++++++++++++++++++--------- lib/librte_eal/linuxapp/eal/eal_vfio.c | 35 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 11 deletions(-)
lib/librte_eal/common/malloc_heap.c +21 -0
@@ -223,6 +223,7 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,void *map_addr;size_t map_len;int n_pages; +bool callback_triggered = false; map_len =RTE_ALIGN_CEIL (align + elt_size +MALLOC_ELEM_TRAILER_LEN , pg_sz);
@@ -242,14 +243,25 @@ 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_notify (RTE_MEM_EVENT_ALLOC , map_addr, map_len); +/* 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 += map_len; RTE_LOG(DEBUG, EAL, "Heap on socket %d was expanded by %zdMB\n",
@@ -260,6 +272,9 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,return 0 ; free_elem: +if (callback_triggered) +eal_memalloc_notify (RTE_MEM_EVENT_FREE , map_addr, map_len); +rollback_expand_heap (ms, n_pages, elem, map_addr, map_len);request_sync ();
@@ -615,6 +630,10 @@ malloc_heap_free(struct malloc_elem *elem) heap->total_size -= n_pages * msl->hugepage_sz;if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) { +/* notify user about changes in memory map */ +eal_memalloc_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);
@@ -637,6 +656,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); }
lib/librte_eal/linuxapp/eal/eal_memalloc.c +26 -11
@@ -34,7 +34,6 @@#include <rte_eal.h> #include <rte_memory.h> #include <rte_spinlock.h> -#include <rte_vfio.h> #include "eal_filesystem.h" #include "eal_internal_cfg.h"
@@ -480,10 +479,6 @@ alloc_page(struct rte_memseg *ms, void *addr, uint64_t size, int socket_id, ms->iova = iova; ms->socket_id = socket_id; -/* map the segment so that VFIO has access to it */ -if (rte_eal_iova_mode () ==RTE_IOVA_VA && -rte_vfio_dma_map (ms->addr_64, iova, size)) -RTE_LOG (DEBUG ,EAL ,"Cannot register segment with VFIO\n" );return 0 ; mapped:
@@ -515,12 +510,6 @@ free_page(struct rte_memseg *ms, struct hugepage_info *hi,char path[PATH_MAX ];int fd, ret; -/* unmap the segment from VFIO */ -if (rte_eal_iova_mode () ==RTE_IOVA_VA && -rte_vfio_dma_unmap (ms->addr_64, ms->iova, ms->len)) { -RTE_LOG (DEBUG ,EAL ,"Cannot unregister segment with VFIO\n" ); - } -if (mmap (ms->addr, ms->hugepage_sz,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ) ==MAP_FAILED ) {
@@ -808,6 +797,19 @@ sync_chunk(struct rte_memseg_list *primary_msl, diff_len =RTE_MIN (chunk_len, diff_len); +/* if we are freeing memory, notif the application */ +if (!used) { +struct rte_memseg *ms; +void *start_va; +size_t len; + + ms =rte_fbarray_get (l_arr, start); + start_va = ms->addr; + len = ms->len * diff_len; + +eal_memalloc_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;
@@ -834,6 +836,19 @@ 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; + + ms =rte_fbarray_get (l_arr, start); + start_va = ms->addr; + len = ms->len * diff_len; + +eal_memalloc_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) :
lib/librte_eal/linuxapp/eal/eal_vfio.c +35 -0
@@ -214,6 +214,37 @@ vfio_group_device_count(int vfio_group_fd)return vfio_cfg.vfio_groups[i].devices; } +static void +vfio_mem_event_callback (enum rte_mem_event type,const void *addr,size_t len) +{ +struct rte_memseg_list *msl; +struct rte_memseg *ms; +size_t cur_len =0 ; +uint64_t pgsz; + + msl =rte_mem_virt2memseg_list (addr); + pgsz = msl->hugepage_sz; + +while (cur_len < len) { +const void *va =RTE_PTR_ADD (addr, cur_len); +uint64_t vfio_va, iova; + + ms =rte_mem_virt2memseg (va, msl); + vfio_va = (uint64_t ) (uintptr_t ) va; + iova = ms->iova; + +/* this never gets called in legacy mode, so we can be sure that +* each segment is a single page. +*/ +if (type ==RTE_MEM_EVENT_ALLOC ) +rte_vfio_dma_map (vfio_va, iova, pgsz); +else +rte_vfio_dma_unmap (vfio_va, iova, pgsz); + + cur_len += pgsz; + } +} +int rte_vfio_clear_group (int vfio_group_fd) {
@@ -507,6 +538,10 @@ rte_vfio_enable(const char *modname)if (vfio_cfg.vfio_container_fd !=-1 ) {RTE_LOG (NOTICE ,EAL ,"VFIO support initialized\n" ); vfio_cfg.vfio_enabled =1 ; + +/* register callback for mem events */ +rte_mem_event_register_callback ("vfio_mem_event_clb" , + vfio_mem_event_callback); }else {RTE_LOG (NOTICE ,EAL ,"VFIO support could not be initialized\n" ); }
Signature · 1 line
2.7.4
PATCH v2 31/41 ethdev: use contiguous allocation for DMA memory
In reply to Anatoly BurakovThis fixes the following drivers in one go:
grep -Rl rte_eth_dma_zone_reserve drivers/
drivers/net/avf/avf_rxtx.cdrivers/net/thunderx/nicvf_ethdev.cdrivers/net/e1000/igb_rxtx.cdrivers/net/e1000/em_rxtx.cdrivers/net/fm10k/fm10k_ethdev.cdrivers/net/vmxnet3/vmxnet3_rxtx.cdrivers/net/liquidio/lio_rxtx.cdrivers/net/i40e/i40e_rxtx.cdrivers/net/sfc/sfc.cdrivers/net/ixgbe/ixgbe_rxtx.cdrivers/net/nfp/nfp_net.c
--- lib/librte_ether/rte_ethdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_ether/rte_ethdev.c +2 -1
@@ -3401,7 +3401,8 @@ rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,if (mz)return mz; -return rte_memzone_reserve_aligned (z_name, size, socket_id,0 , align); +return rte_memzone_reserve_aligned_contig (z_name, size, socket_id,0 , + align); }int
Signature · 1 line
2.7.4
PATCH v2 32/41 crypto/qat: use contiguous allocation for DMA memory
In reply to Anatoly Burakov--- drivers/crypto/qat/qat_qp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/qat/qat_qp.c +2 -2
@@ -95,8 +95,8 @@ queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size,default : memzone_flags =RTE_MEMZONE_SIZE_HINT_ONLY ; } -return rte_memzone_reserve_aligned (queue_name, queue_size, socket_id, - memzone_flags, queue_size); +return rte_memzone_reserve_aligned_contig (queue_name, queue_size, + socket_id, memzone_flags, queue_size); } int qat_crypto_sym_qp_setup(struct rte_cryptodev *dev, uint16_t queue_pair_id,
Signature · 1 line
2.7.4
PATCH v2 33/41 net/avf: use contiguous allocation for DMA memory
In reply to Anatoly Burakov--- drivers/net/avf/avf_ethdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/avf/avf_ethdev.c +1 -1
@@ -1365,7 +1365,7 @@ avf_allocate_dma_mem_d(__rte_unused struct avf_hw *hw,return AVF_ERR_PARAM ;snprintf (z_name,sizeof (z_name),"avf_dma_%" PRIu64,rte_rand ()); - mz =rte_memzone_reserve_bounded (z_name, size,SOCKET_ID_ANY ,0 , + mz =rte_memzone_reserve_bounded_contig (z_name, size,SOCKET_ID_ANY ,0 , alignment,RTE_PGSIZE_2M );if (!mz)return AVF_ERR_NO_MEMORY ;
Signature · 1 line
2.7.4
PATCH v2 34/41 net/bnx2x: use contiguous allocation for DMA memory
In reply to Anatoly Burakov--- drivers/net/bnx2x/bnx2x.c | 2 +- drivers/net/bnx2x/bnx2x_rxtx.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/net/bnx2x/bnx2x.c +1 -1
@@ -177,7 +177,7 @@ bnx2x_dma_alloc(struct bnx2x_softc *sc, size_t size, struct bnx2x_dma *dma, rte_get_timer_cycles());/* Caller must take care that strlen(mz_name) < RTE_MEMZONE_NAMESIZE */ - z =rte_memzone_reserve_aligned (mz_name, (uint64_t ) (size), + z =rte_memzone_reserve_aligned_contig (mz_name, (uint64_t )size,SOCKET_ID_ANY ,0 , align);if (z ==NULL ) {
drivers/net/bnx2x/bnx2x_rxtx.c +2 -1
@@ -26,7 +26,8 @@ ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,if (mz)return mz; -return rte_memzone_reserve_aligned (z_name, ring_size, socket_id,0 ,BNX2X_PAGE_SIZE ); +return rte_memzone_reserve_aligned_contig (z_name, ring_size, socket_id, +0 ,BNX2X_PAGE_SIZE ); } static void
Signature · 1 line
2.7.4
PATCH v2 35/41 net/cxgbe: use contiguous allocation for DMA memory
In reply to Anatoly Burakov---
Notes:
It is not 100% clear if this memzone is used for DMA, corrections welcome.
drivers/net/cxgbe/sge.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/cxgbe/sge.c +2 -1
@@ -1299,7 +1299,8 @@ static void *alloc_ring(size_t nelem, size_t elem_size,* handle the maximum ring size is allocated in order to allow for * resizing in later calls to the queue setup function. */ - tz =rte_memzone_reserve_aligned (z_name, len, socket_id,0 ,4096 ); + tz =rte_memzone_reserve_aligned_contig (z_name, len, socket_id,0 , +4096 );if (!tz)return NULL ;
Signature · 1 line
2.7.4