From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 806115A92 for ; Wed, 18 Nov 2015 16:21:40 +0100 (CET) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP; 18 Nov 2015 07:21:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,313,1444719600"; d="scan'208";a="853733416" Received: from sie-lab-212-209.ir.intel.com (HELO silpixa00377983.ir.intel.com) ([10.237.212.209]) by orsmga002.jf.intel.com with ESMTP; 18 Nov 2015 07:21:38 -0800 From: Sergio Gonzalez Monroy To: dev@dpdk.org Date: Wed, 18 Nov 2015 15:21:37 +0000 Message-Id: <1447860097-12615-1-git-send-email-sergio.gonzalez.monroy@intel.com> X-Mailer: git-send-email 2.4.3 Subject: [dpdk-dev] [PATCH RFC] mem: zero out memory when freed X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Nov 2015 15:21:40 -0000 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 --- 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