DPDK patches and discussions
 help / color / mirror / Atom feed
From: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH RFC] mem: zero out memory when freed
Date: Wed, 18 Nov 2015 15:21:37 +0000	[thread overview]
Message-ID: <1447860097-12615-1-git-send-email-sergio.gonzalez.monroy@intel.com> (raw)

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(-)

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;
-- 
2.4.3

                 reply	other threads:[~2015-11-18 15:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447860097-12615-1-git-send-email-sergio.gonzalez.monroy@intel.com \
    --to=sergio.gonzalez.monroy@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).