[dpdk-dev] [PATCH v2 03/14] lib: use new memory locking API

Anatoly Burakov anatoly.burakov at intel.com
Tue Jun 25 18:05:15 CEST 2019


Replace usages of direct access to shared memory config with
calls to the new API.

Signed-off-by: Anatoly Burakov <anatoly.burakov at intel.com>
---
 lib/librte_eal/common/eal_common_memory.c | 43 ++++++++++-------------
 lib/librte_eal/common/malloc_heap.c       | 14 ++++----
 lib/librte_eal/common/rte_malloc.c        | 32 ++++++++---------
 lib/librte_eal/linux/eal/eal_vfio.c       | 16 ++++-----
 4 files changed, 44 insertions(+), 61 deletions(-)

diff --git a/lib/librte_eal/common/eal_common_memory.c b/lib/librte_eal/common/eal_common_memory.c
index 5ae8d0124..e73a413cc 100644
--- a/lib/librte_eal/common/eal_common_memory.c
+++ b/lib/librte_eal/common/eal_common_memory.c
@@ -596,13 +596,12 @@ rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg)
 int __rte_experimental
 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	int ret = 0;
 
 	/* do not allow allocations/frees/init while we iterate */
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 	ret = rte_memseg_contig_walk_thread_unsafe(func, arg);
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	return ret;
 }
@@ -638,13 +637,12 @@ rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
 int __rte_experimental
 rte_memseg_walk(rte_memseg_walk_t func, void *arg)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	int ret = 0;
 
 	/* do not allow allocations/frees/init while we iterate */
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 	ret = rte_memseg_walk_thread_unsafe(func, arg);
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	return ret;
 }
@@ -671,13 +669,12 @@ rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg)
 int __rte_experimental
 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	int ret = 0;
 
 	/* do not allow allocations/frees/init while we iterate */
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 	ret = rte_memseg_list_walk_thread_unsafe(func, arg);
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	return ret;
 }
@@ -727,12 +724,11 @@ rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms)
 int __rte_experimental
 rte_memseg_get_fd(const struct rte_memseg *ms)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	int ret;
 
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 	ret = rte_memseg_get_fd_thread_unsafe(ms);
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	return ret;
 }
@@ -783,12 +779,11 @@ rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
 int __rte_experimental
 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	int ret;
 
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 	ret = rte_memseg_get_fd_offset_thread_unsafe(ms, offset);
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	return ret;
 }
@@ -809,7 +804,7 @@ rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	/* make sure the segment doesn't already exist */
 	if (malloc_heap_find_external_seg(va_addr, len) != NULL) {
@@ -838,14 +833,13 @@ rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
 	/* memseg list successfully created - increment next socket ID */
 	mcfg->next_socket_id++;
 unlock:
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 	return ret;
 }
 
 int __rte_experimental
 rte_extmem_unregister(void *va_addr, size_t len)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	struct rte_memseg_list *msl;
 	int ret = 0;
 
@@ -853,7 +847,7 @@ rte_extmem_unregister(void *va_addr, size_t len)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	/* find our segment */
 	msl = malloc_heap_find_external_seg(va_addr, len);
@@ -865,14 +859,13 @@ rte_extmem_unregister(void *va_addr, size_t len)
 
 	ret = malloc_heap_destroy_external_seg(msl);
 unlock:
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 	return ret;
 }
 
 static int
 sync_memory(void *va_addr, size_t len, bool attach)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	struct rte_memseg_list *msl;
 	int ret = 0;
 
@@ -880,7 +873,7 @@ sync_memory(void *va_addr, size_t len, bool attach)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	/* find our segment */
 	msl = malloc_heap_find_external_seg(va_addr, len);
@@ -895,7 +888,7 @@ sync_memory(void *va_addr, size_t len, bool attach)
 		ret = rte_fbarray_detach(&msl->memseg_arr);
 
 unlock:
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 	return ret;
 }
 
@@ -923,7 +916,7 @@ rte_eal_memory_init(void)
 		return -1;
 
 	/* lock mem hotplug here, to prevent races while we init */
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 
 	if (rte_eal_memseg_init() < 0)
 		goto fail;
@@ -942,6 +935,6 @@ rte_eal_memory_init(void)
 
 	return 0;
 fail:
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 	return -1;
 }
diff --git a/lib/librte_eal/common/malloc_heap.c b/lib/librte_eal/common/malloc_heap.c
index f9235932e..f1d31de0d 100644
--- a/lib/librte_eal/common/malloc_heap.c
+++ b/lib/librte_eal/common/malloc_heap.c
@@ -485,10 +485,9 @@ try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
 		int socket, unsigned int flags, size_t align, size_t bound,
 		bool contig)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	int ret;
 
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 		ret = try_expand_heap_primary(heap, pg_sz, elt_size, socket,
@@ -498,7 +497,7 @@ try_expand_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
 				flags, align, bound, contig);
 	}
 
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 	return ret;
 }
 
@@ -821,7 +820,6 @@ malloc_heap_free_pages(void *aligned_start, size_t aligned_len)
 int
 malloc_heap_free(struct malloc_elem *elem)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	struct malloc_heap *heap;
 	void *start, *aligned_start, *end, *aligned_end;
 	size_t len, aligned_len, page_sz;
@@ -935,7 +933,7 @@ malloc_heap_free(struct malloc_elem *elem)
 
 	/* now we can finally free us some pages */
 
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	/*
 	 * we allow secondary processes to clear the heap of this allocated
@@ -990,7 +988,7 @@ malloc_heap_free(struct malloc_elem *elem)
 	RTE_LOG(DEBUG, EAL, "Heap on socket %d was shrunk by %zdMB\n",
 		msl->socket_id, aligned_len >> 20ULL);
 
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 free_unlock:
 	rte_spinlock_unlock(&(heap->lock));
 	return ret;
@@ -1344,7 +1342,7 @@ rte_eal_malloc_heap_init(void)
 
 	if (register_mp_requests()) {
 		RTE_LOG(ERR, EAL, "Couldn't register malloc multiprocess actions\n");
-		rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+		rte_mcfg_mem_read_unlock();
 		return -1;
 	}
 
@@ -1352,7 +1350,7 @@ rte_eal_malloc_heap_init(void)
 	 * even come before primary itself is fully initialized, and secondaries
 	 * do not need to initialize the heap.
 	 */
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	/* secondary process does not need to initialize anything */
 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
diff --git a/lib/librte_eal/common/rte_malloc.c b/lib/librte_eal/common/rte_malloc.c
index 54792ea5b..98e1bdd07 100644
--- a/lib/librte_eal/common/rte_malloc.c
+++ b/lib/librte_eal/common/rte_malloc.c
@@ -223,7 +223,7 @@ rte_malloc_heap_get_socket(const char *name)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 	for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
 		struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
 
@@ -239,7 +239,7 @@ rte_malloc_heap_get_socket(const char *name)
 		rte_errno = ENOENT;
 		ret = -1;
 	}
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	return ret;
 }
@@ -254,7 +254,7 @@ rte_malloc_heap_socket_is_external(int socket_id)
 	if (socket_id == SOCKET_ID_ANY)
 		return 0;
 
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 	for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
 		struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
 
@@ -264,7 +264,7 @@ rte_malloc_heap_socket_is_external(int socket_id)
 			break;
 		}
 	}
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 
 	return ret;
 }
@@ -352,7 +352,6 @@ int
 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
 		rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	struct malloc_heap *heap = NULL;
 	struct rte_memseg_list *msl;
 	unsigned int n;
@@ -369,7 +368,7 @@ rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	/* find our heap */
 	heap = find_named_heap(heap_name);
@@ -398,7 +397,7 @@ rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
 	rte_spinlock_unlock(&heap->lock);
 
 unlock:
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 
 	return ret;
 }
@@ -406,7 +405,6 @@ rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
 int
 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	struct malloc_heap *heap = NULL;
 	struct rte_memseg_list *msl;
 	int ret;
@@ -418,7 +416,7 @@ rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 	/* find our heap */
 	heap = find_named_heap(heap_name);
 	if (heap == NULL) {
@@ -448,7 +446,7 @@ rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
 	ret = malloc_heap_destroy_external_seg(msl);
 
 unlock:
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 
 	return ret;
 }
@@ -456,7 +454,6 @@ rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
 static int
 sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	struct malloc_heap *heap = NULL;
 	struct rte_memseg_list *msl;
 	int ret;
@@ -468,7 +465,7 @@ sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_lock();
 
 	/* find our heap */
 	heap = find_named_heap(heap_name);
@@ -516,7 +513,7 @@ sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
 		}
 	}
 unlock:
-	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_read_unlock();
 	return ret;
 }
 
@@ -549,7 +546,7 @@ rte_malloc_heap_create(const char *heap_name)
 	/* check if there is space in the heap list, or if heap with this name
 	 * already exists.
 	 */
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	for (i = 0; i < RTE_MAX_HEAPS; i++) {
 		struct malloc_heap *tmp = &mcfg->malloc_heaps[i];
@@ -578,7 +575,7 @@ rte_malloc_heap_create(const char *heap_name)
 	/* we're sure that we can create a new heap, so do it */
 	ret = malloc_heap_create(heap, heap_name);
 unlock:
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 
 	return ret;
 }
@@ -586,7 +583,6 @@ rte_malloc_heap_create(const char *heap_name)
 int
 rte_malloc_heap_destroy(const char *heap_name)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	struct malloc_heap *heap = NULL;
 	int ret;
 
@@ -597,7 +593,7 @@ rte_malloc_heap_destroy(const char *heap_name)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_lock();
 
 	/* start from non-socket heaps */
 	heap = find_named_heap(heap_name);
@@ -621,7 +617,7 @@ rte_malloc_heap_destroy(const char *heap_name)
 	if (ret < 0)
 		rte_spinlock_unlock(&heap->lock);
 unlock:
-	rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+	rte_mcfg_mem_write_unlock();
 
 	return ret;
 }
diff --git a/lib/librte_eal/linux/eal/eal_vfio.c b/lib/librte_eal/linux/eal/eal_vfio.c
index 6892a2c14..924cba526 100644
--- a/lib/librte_eal/linux/eal/eal_vfio.c
+++ b/lib/librte_eal/linux/eal/eal_vfio.c
@@ -635,8 +635,6 @@ int
 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 		int *vfio_dev_fd, struct vfio_device_info *device_info)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
-	rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
 	struct vfio_group_status group_status = {
 			.argsz = sizeof(group_status)
 	};
@@ -739,7 +737,7 @@ rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 			/* lock memory hotplug before mapping and release it
 			 * after registering callback, to prevent races
 			 */
-			rte_rwlock_read_lock(mem_lock);
+			rte_mcfg_mem_read_lock();
 			if (vfio_cfg == default_vfio_cfg)
 				ret = t->dma_map_func(vfio_container_fd);
 			else
@@ -750,7 +748,7 @@ rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 					dev_addr, errno, strerror(errno));
 				close(vfio_group_fd);
 				rte_vfio_clear_group(vfio_group_fd);
-				rte_rwlock_read_unlock(mem_lock);
+				rte_mcfg_mem_read_unlock();
 				return -1;
 			}
 
@@ -781,7 +779,7 @@ rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 							map->len);
 					rte_spinlock_recursive_unlock(
 							&user_mem_maps->lock);
-					rte_rwlock_read_unlock(mem_lock);
+					rte_mcfg_mem_read_unlock();
 					return -1;
 				}
 			}
@@ -795,7 +793,7 @@ rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 			else
 				ret = 0;
 			/* unlock memory hotplug */
-			rte_rwlock_read_unlock(mem_lock);
+			rte_mcfg_mem_read_unlock();
 
 			if (ret && rte_errno != ENOTSUP) {
 				RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
@@ -862,8 +860,6 @@ int
 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
 		    int vfio_dev_fd)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
-	rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
 	struct vfio_group_status group_status = {
 			.argsz = sizeof(group_status)
 	};
@@ -876,7 +872,7 @@ rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
 	 * VFIO device, because this might be the last device and we might need
 	 * to unregister the callback.
 	 */
-	rte_rwlock_read_lock(mem_lock);
+	rte_mcfg_mem_read_lock();
 
 	/* get group number */
 	ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
@@ -947,7 +943,7 @@ rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
 	ret = 0;
 
 out:
-	rte_rwlock_read_unlock(mem_lock);
+	rte_mcfg_mem_read_unlock();
 	return ret;
 }
 
-- 
2.17.1


More information about the dev mailing list