[dpdk-dev,RFC] mem: zero out memory when freed

Message ID 1447860097-12615-1-git-send-email-sergio.gonzalez.monroy@intel.com (mailing list archive)
State RFC, archived
Headers

Commit Message

Sergio Gonzalez Monroy Nov. 18, 2015, 3:21 p.m. UTC
  Before the introduction of rte_memzone_free, all reserved memzones were
zeroed out by default.

Now that we can free and reserve again, we can potentially get memory
that is not zeroed out.
This also adds to the fact that the memzone reserve could have been a
malloc element.

We now zeroed all memory on free, assuming that all allocated memory
after EAL init is already zeroed. This means that all available memory
is zeroed at all times.

Signed-off-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
---
 lib/librte_eal/common/malloc_elem.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
  

Patch

diff --git a/lib/librte_eal/common/malloc_elem.c b/lib/librte_eal/common/malloc_elem.c
index b54ee33..b112e2b 100644
--- a/lib/librte_eal/common/malloc_elem.c
+++ b/lib/librte_eal/common/malloc_elem.c
@@ -261,6 +261,7 @@  join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
 	struct malloc_elem *next = RTE_PTR_ADD(elem2, elem2->size);
 	elem1->size += elem2->size;
 	next->prev = elem1;
+	memset(elem2, 0, sizeof(struct malloc_elem));
 }
 
 /*
@@ -275,6 +276,10 @@  malloc_elem_free(struct malloc_elem *elem)
 		return -1;
 
 	rte_spinlock_lock(&(elem->heap->lock));
+
+	memset(elem + 1, 0, elem->size - sizeof(struct malloc_elem));
+
+	/* Check if next element is free, and if so join with it */
 	struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
 	if (next->state == ELEM_FREE){
 		/* remove from free list, join to this one */
@@ -282,21 +287,18 @@  malloc_elem_free(struct malloc_elem *elem)
 		join_elem(elem, next);
 	}
 
-	/* check if previous element is free, if so join with it and return,
-	 * need to re-insert in free list, as that element's size is changing
-	 */
-	if (elem->prev != NULL && elem->prev->state == ELEM_FREE) {
-		elem_free_list_remove(elem->prev);
-		join_elem(elem->prev, elem);
-		malloc_elem_free_list_insert(elem->prev);
-	}
-	/* otherwise add ourselves to the free list */
-	else {
-		malloc_elem_free_list_insert(elem);
-		elem->pad = 0;
+	/* Check if previous element is free, and if so join with it */
+	struct malloc_elem *prev = elem->prev;
+	if (prev != NULL && prev->state == ELEM_FREE) {
+		elem_free_list_remove(prev);
+		join_elem(prev, elem);
+		elem = prev;
 	}
+
+	malloc_elem_free_list_insert(elem);
 	/* decrease heap's count of allocated elements */
 	elem->heap->alloc_count--;
+
 	rte_spinlock_unlock(&(elem->heap->lock));
 
 	return 0;