[v2] cryptodev: free memzone when releasing cryptodev

Message ID 201906281916.x5SJGeFj004670@lectura.cs.arizona.edu (mailing list archive)
State Accepted, archived
Delegated to: akhil goyal
Headers
Series [v2] cryptodev: free memzone when releasing cryptodev |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/mellanox-Performance-Testing success Performance Testing PASS
ci/intel-Performance-Testing success Performance Testing PASS

Commit Message

Junxiao Shi June 28, 2019, 7:11 p.m. UTC
  When a cryptodev is created in a primary process,
rte_cryptodev_data_alloc reserves a memzone.
However, this memzone was not released when the cryptodev
is uninitialized. After that, new cryptodev cannot be
created due to memzone name conflict.

This commit frees the memzone when a cryptodev is
uninitialized, fixing this bug. This approach is chosen
instead of keeping and reusing the old memzone, because
the new cryptodev could belong to a different NUMA socket.

Also, rte_cryptodev_data pointer is now properly recorded
in cryptodev_globals.data array.

Bugzilla ID: 105

Signed-off-by: Junxiao Shi <git@mail1.yoursunny.com>
---
 lib/librte_cryptodev/rte_cryptodev.c | 46 ++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 7 deletions(-)
  

Comments

Akhil Goyal July 1, 2019, 11:48 a.m. UTC | #1
> 
> When a cryptodev is created in a primary process,
> rte_cryptodev_data_alloc reserves a memzone.
> However, this memzone was not released when the cryptodev
> is uninitialized. After that, new cryptodev cannot be
> created due to memzone name conflict.
> 
> This commit frees the memzone when a cryptodev is
> uninitialized, fixing this bug. This approach is chosen
> instead of keeping and reusing the old memzone, because
> the new cryptodev could belong to a different NUMA socket.
> 
> Also, rte_cryptodev_data pointer is now properly recorded
> in cryptodev_globals.data array.
> 
> Bugzilla ID: 105
> 
> Signed-off-by: Junxiao Shi <git@mail1.yoursunny.com>
> ---
Acked-by: Akhil Goyal <akhil.goyal@nxp.com>

Applied to dpdk-next-crypto

Thanks.
  

Patch

diff --git a/lib/librte_cryptodev/rte_cryptodev.c b/lib/librte_cryptodev/rte_cryptodev.c
index 00c2cf4..a7a0d4b 100644
--- a/lib/librte_cryptodev/rte_cryptodev.c
+++ b/lib/librte_cryptodev/rte_cryptodev.c
@@ -627,7 +627,7 @@  static inline int
 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
 		int socket_id)
 {
-	char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
+	char mz_name[RTE_MEMZONE_NAMESIZE];
 	const struct rte_memzone *mz;
 	int n;
 
@@ -653,6 +653,31 @@  rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
 	return 0;
 }
 
+static inline int
+rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
+{
+	char mz_name[RTE_MEMZONE_NAMESIZE];
+	const struct rte_memzone *mz;
+	int n;
+
+	/* generate memzone name */
+	n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
+	if (n >= (int)sizeof(mz_name))
+		return -EINVAL;
+
+	mz = rte_memzone_lookup(mz_name);
+	if (mz == NULL)
+		return -ENOMEM;
+
+	RTE_ASSERT(*data == mz->addr);
+	*data = NULL;
+
+	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
+		return rte_memzone_free(mz);
+
+	return 0;
+}
+
 static uint8_t
 rte_cryptodev_find_free_device_index(void)
 {
@@ -687,16 +712,16 @@  rte_cryptodev_pmd_allocate(const char *name, int socket_id)
 	cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
 
 	if (cryptodev->data == NULL) {
-		struct rte_cryptodev_data *cryptodev_data =
-				cryptodev_globals.data[dev_id];
+		struct rte_cryptodev_data **cryptodev_data =
+				&cryptodev_globals.data[dev_id];
 
-		int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
+		int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
 				socket_id);
 
-		if (retval < 0 || cryptodev_data == NULL)
+		if (retval < 0 || *cryptodev_data == NULL)
 			return NULL;
 
-		cryptodev->data = cryptodev_data;
+		cryptodev->data = *cryptodev_data;
 
 		strlcpy(cryptodev->data->name, name,
 			RTE_CRYPTODEV_NAME_MAX_LEN);
@@ -720,17 +745,24 @@  int
 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
 {
 	int ret;
+	uint8_t dev_id;
 
 	if (cryptodev == NULL)
 		return -EINVAL;
 
+	dev_id = cryptodev->data->dev_id;
+
 	/* Close device only if device operations have been set */
 	if (cryptodev->dev_ops) {
-		ret = rte_cryptodev_close(cryptodev->data->dev_id);
+		ret = rte_cryptodev_close(dev_id);
 		if (ret < 0)
 			return ret;
 	}
 
+	ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
+	if (ret < 0)
+		return ret;
+
 	cryptodev->attached = RTE_CRYPTODEV_DETACHED;
 	cryptodev_globals.nb_devs--;
 	return 0;