From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <olivier.matz@6wind.com>
Received: from mail.droids-corp.org (zoll.droids-corp.org [94.23.50.67])
 by dpdk.org (Postfix) with ESMTP id F345A5693
 for <dev@dpdk.org>; Mon,  7 Sep 2015 10:45:22 +0200 (CEST)
Received: from was59-1-82-226-113-214.fbx.proxad.net ([82.226.113.214]
 helo=[192.168.0.10])
 by mail.droids-corp.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84) (envelope-from <olivier.matz@6wind.com>)
 id 1ZYs3N-0001Hq-2s; Mon, 07 Sep 2015 10:45:29 +0200
Message-ID: <55ED4E97.3070505@6wind.com>
Date: Mon, 07 Sep 2015 10:45:11 +0200
From: Olivier MATZ <olivier.matz@6wind.com>
User-Agent: Mozilla/5.0 (X11; Linux x86_64;
 rv:31.0) Gecko/20100101 Icedove/31.7.0
MIME-Version: 1.0
To: Pablo de Lara <pablo.de.lara.guarch@intel.com>, dev@dpdk.org
References: <1439906454-12860-1-git-send-email-pablo.de.lara.guarch@intel.com>
In-Reply-To: <1439906454-12860-1-git-send-email-pablo.de.lara.guarch@intel.com>
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Subject: Re: [dpdk-dev] [PATCH] ring: add function to free a ring
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: Mon, 07 Sep 2015 08:45:23 -0000

Hi Pablo,

Please find some comments below.

On 08/18/2015 04:00 PM, Pablo de Lara wrote:
> When creating a ring, a memzone is created to allocate it in memory,
> but the ring could not be freed, as memzones could not be.
> 
> Since memzones can be freed now, then rings can be as well,
> taking into account if they were initialized using pre-allocated memory
> (in which case, memory should be freed externally) or using rte_memzone_reserve
> (with rte_ring_create), freeing the memory with rte_memzone_free.
> 
> Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
> ---
>  lib/librte_ring/rte_ring.c           | 43 ++++++++++++++++++++++++++++++++++++
>  lib/librte_ring/rte_ring.h           |  7 ++++++
>  lib/librte_ring/rte_ring_version.map |  7 ++++++
>  3 files changed, 57 insertions(+)
> 
> diff --git a/lib/librte_ring/rte_ring.c b/lib/librte_ring/rte_ring.c
> index c9e59d4..83ce6d3 100644
> --- a/lib/librte_ring/rte_ring.c
> +++ b/lib/librte_ring/rte_ring.c
> @@ -208,6 +208,49 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
>  	return r;
>  }
>  
> +/* free the ring */
> +void
> +rte_ring_free(struct rte_ring *r)
> +{
> +	struct rte_ring_list *ring_list = NULL;
> +	char mz_name[RTE_MEMZONE_NAMESIZE];
> +	struct rte_tailq_entry *te;
> +	const struct rte_memzone *mz;
> +
> +	if (r == NULL)
> +		return;
> +
> +	snprintf(mz_name, sizeof(mz_name), "%s%s", RTE_RING_MZ_PREFIX, r->name);
> +	mz = rte_memzone_lookup(mz_name);
> +
> +	/*
> +	 * Free ring memory if it was allocated with rte_memzone_reserve,
> +	 * otherwise it should be freed externally
> +	 */
> +	if (rte_memzone_free(mz) != 0)
> +		return;

Should we have a log here? I think it may hide important
bugs if we just return silently here.

> +
> +	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
> +	rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
> +
> +	/* find out tailq entry */
> +	TAILQ_FOREACH(te, ring_list, next) {
> +		if (te->data == (void *) r)
> +			break;
> +	}
> +
> +	if (te == NULL) {
> +		rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
> +		return;
> +	}

If I understand well, a ring is in the tailq only if it was
created with rte_ring_create(). A ring that is statically created
in memory is not in the tailq. But we already returned in that
case. So (te == NULL) should not happen here, right? We could
also add an error log then.

I'm not sure we should handle the case where the ring is not allocated
with rte_ring_create() in this function. If the ring is allocated with
another mean (either in a global variable, or with another dynamic
memory allocator), this function should not be called.

What do you think?


Regards,
Olivier


> +
> +	TAILQ_REMOVE(ring_list, te, next);
> +
> +	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
> +
> +	rte_free(te);
> +}
> +
>  /*
>   * change the high water mark. If *count* is 0, water marking is
>   * disabled
> diff --git a/lib/librte_ring/rte_ring.h b/lib/librte_ring/rte_ring.h
> index af68888..e75566f 100644
> --- a/lib/librte_ring/rte_ring.h
> +++ b/lib/librte_ring/rte_ring.h
> @@ -300,6 +300,13 @@ int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
>   */
>  struct rte_ring *rte_ring_create(const char *name, unsigned count,
>  				 int socket_id, unsigned flags);
> +/**
> + * De-allocate all memory used by the ring.
> + *
> + * @param r
> + *   Ring to free
> + */
> +void rte_ring_free(struct rte_ring *r);
>  
>  /**
>   * Change the high water mark.
> diff --git a/lib/librte_ring/rte_ring_version.map b/lib/librte_ring/rte_ring_version.map
> index 982fdd1..5474b98 100644
> --- a/lib/librte_ring/rte_ring_version.map
> +++ b/lib/librte_ring/rte_ring_version.map
> @@ -11,3 +11,10 @@ DPDK_2.0 {
>  
>  	local: *;
>  };
> +
> +DPDK_2.2 {
> +	global:
> +
> +	rte_ring_free;
> +
> +} DPDK_2.0;
>