[dpdk-dev] [PATCH 1/2] eal/malloc: merge malloc_elems in heap if they are contiguous

Gowrishankar gowrishankar.m at linux.vnet.ibm.com
Thu May 3 12:11:35 CEST 2018


From: Gowrishankar Muthukrishnan <gowrishankar.m at linux.vnet.ibm.com>

During malloc heap init, if there are malloc_elems contiguous in
virt addresses, they could be merged so that, merged malloc_elem
would guarantee larger free memory size than its actual hugepage
size, it was created for.

Fixes: fafcc11985 ("mem: rework memzone to be allocated by malloc")
Cc: stable at dpdk.org

Signed-off-by: Gowrishankar Muthukrishnan <gowrishankar.m at linux.vnet.ibm.com>
---
 lib/librte_eal/common/malloc_heap.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/lib/librte_eal/common/malloc_heap.c b/lib/librte_eal/common/malloc_heap.c
index 267a4c6..1cacf7f 100644
--- a/lib/librte_eal/common/malloc_heap.c
+++ b/lib/librte_eal/common/malloc_heap.c
@@ -213,7 +213,9 @@
 {
 	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
 	unsigned ms_cnt;
-	struct rte_memseg *ms;
+	struct rte_memseg *ms, *prev_ms = NULL;
+	struct malloc_elem *elem, *prev_elem;
+	int ret;
 
 	if (mcfg == NULL)
 		return -1;
@@ -222,6 +224,32 @@
 			(ms_cnt < RTE_MAX_MEMSEG) && (ms->len > 0);
 			ms_cnt++, ms++) {
 		malloc_heap_add_memseg(&mcfg->malloc_heaps[ms->socket_id], ms);
+		elem = (struct malloc_elem *)ms->addr;
+		if (prev_ms != NULL && \
+			(ms->socket_id == prev_ms->socket_id)) {
+			prev_elem = (struct malloc_elem *)prev_ms->addr;
+
+			/* prev_elem and elem to be contiguous for the resize.
+			   Other wise look for prev_elem in iterations */
+			if (elem != RTE_PTR_ADD(prev_elem,
+				prev_elem->size + MALLOC_ELEM_OVERHEAD)) {
+				prev_ms = ms;
+				continue;
+			}
+			/* end BUSY elem pointed by prev_elem can be merged
+			   with prev_elem itself, as it expands it size now.
+			 */
+			prev_elem->size += MALLOC_ELEM_OVERHEAD;
+
+			/* preserve end BUSY elem that points to current elem,
+			   or else free_list will be broken */
+			ret = malloc_elem_resize(prev_elem,
+				prev_elem->size + elem->size - MALLOC_ELEM_OVERHEAD);
+			if (ret < 0)
+				prev_elem = elem;
+		} else {
+			prev_ms = ms;
+		}
 	}
 
 	return 0;
-- 
1.9.1



More information about the dev mailing list