From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <sergio.gonzalez.monroy@intel.com>
Received: from mga02.intel.com (mga02.intel.com [134.134.136.20])
 by dpdk.org (Postfix) with ESMTP id 4413C558D
 for <dev@dpdk.org>; Fri, 22 Jul 2016 18:01:26 +0200 (CEST)
Received: from fmsmga004.fm.intel.com ([10.253.24.48])
 by orsmga101.jf.intel.com with ESMTP; 22 Jul 2016 09:01:21 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.28,405,1464678000"; d="scan'208";a="143872594"
Received: from sie-lab-212-209.ir.intel.com (HELO
 silpixa00377983.ir.intel.com) ([10.237.212.209])
 by fmsmga004.fm.intel.com with ESMTP; 22 Jul 2016 09:01:21 -0700
From: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
To: dev@dpdk.org
Cc: thomas.monjalon@6wind.com
Date: Fri, 22 Jul 2016 17:01:18 +0100
Message-Id: <1469203278-91363-2-git-send-email-sergio.gonzalez.monroy@intel.com>
X-Mailer: git-send-email 2.4.11
In-Reply-To: <1469203278-91363-1-git-send-email-sergio.gonzalez.monroy@intel.com>
References: <1469203278-91363-1-git-send-email-sergio.gonzalez.monroy@intel.com>
Subject: [dpdk-dev] [PATCH 2/2] mempool: fix unsafe tailq element removal
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches and discussions about DPDK <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Fri, 22 Jul 2016 16:01:26 -0000

Potentially user provided function could remove/free tailq elements.
Doing so within a TAILQ_FOREACH loop is not safe.

Use _SAFE versions of _FOREACH macros.

Signed-off-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
---
 lib/librte_mempool/rte_mempool.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/librte_mempool/rte_mempool.c b/lib/librte_mempool/rte_mempool.c
index 8806633..394154a 100644
--- a/lib/librte_mempool/rte_mempool.c
+++ b/lib/librte_mempool/rte_mempool.c
@@ -157,10 +157,10 @@ rte_mempool_obj_iter(struct rte_mempool *mp,
 	rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
 {
 	struct rte_mempool_objhdr *hdr;
-	void *obj;
+	void *obj, *temp;
 	unsigned n = 0;
 
-	STAILQ_FOREACH(hdr, &mp->elt_list, next) {
+	STAILQ_FOREACH_SAFE(hdr, &mp->elt_list, next, temp) {
 		obj = (char *)hdr + sizeof(*hdr);
 		obj_cb(mp, obj_cb_arg, obj, n);
 		n++;
@@ -176,8 +176,9 @@ rte_mempool_mem_iter(struct rte_mempool *mp,
 {
 	struct rte_mempool_memhdr *hdr;
 	unsigned n = 0;
+	void *temp;
 
-	STAILQ_FOREACH(hdr, &mp->mem_list, next) {
+	STAILQ_FOREACH_SAFE(hdr, &mp->mem_list, next, temp) {
 		mem_cb(mp, mem_cb_arg, hdr, n);
 		n++;
 	}
@@ -1283,12 +1284,13 @@ void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
 {
 	struct rte_tailq_entry *te = NULL;
 	struct rte_mempool_list *mempool_list;
+	void *temp;
 
 	mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
 
 	rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
-	TAILQ_FOREACH(te, mempool_list, next) {
+	TAILQ_FOREACH_SAFE(te, mempool_list, next, temp) {
 		(*func)((struct rte_mempool *) te->data, arg);
 	}
 
-- 
2.4.11