[v5,2/3] graph: add support for node free API

Message ID 20241126044402.1209998-2-kirankumark@marvell.com (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series [v5,1/3] graph: avoid global node ID counter |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Kiran Kumar Kokkilagadda Nov. 26, 2024, 4:44 a.m. UTC
From: Kiran Kumar K <kirankumark@marvell.com>

Add support for rte_node_free API to free the node and its
memory, if node is not part of any of the created graphs.

Signed-off-by: Kiran Kumar K <kirankumark@marvell.com>
---
v5:
- Fix review comments.

 lib/graph/graph.c         | 16 ++++++++++++++++
 lib/graph/graph_private.h | 13 +++++++++++++
 lib/graph/node.c          | 25 +++++++++++++++++++++++++
 lib/graph/rte_graph.h     | 15 +++++++++++++++
 lib/graph/version.map     |  3 +++
 5 files changed, 72 insertions(+)

--
2.43.0
  

Comments

Jerin Jacob Feb. 10, 2025, 4:49 p.m. UTC | #1
> -----Original Message-----
> From: kirankumark@marvell.com <kirankumark@marvell.com>
> Sent: Tuesday, November 26, 2024 10:14 AM
> To: Jerin Jacob <jerinj@marvell.com>; Kiran Kumar Kokkilagadda
> <kirankumark@marvell.com>; Nithin Kumar Dabilpuram
> <ndabilpuram@marvell.com>; Zhirun Yan <yanzhirun_163@163.com>
> Cc: dev@dpdk.org; rjarry@redhat.com; chcchc88@163.com
> Subject: [PATCH v5 2/3] graph: add support for node free API
> 
> From: Kiran Kumar K <kirankumark@marvell.com>
> 
> Add support for rte_node_free API to free the node and its memory, if node is
> not part of any of the created graphs.
> 
> Signed-off-by: Kiran Kumar K <kirankumark@marvell.com>

Acked-by: Jerin Jacob <jerinj@marvell.com>

> ---
> v5:
> - Fix review comments.
> 
>  lib/graph/graph.c         | 16 ++++++++++++++++
>  lib/graph/graph_private.h | 13 +++++++++++++
>  lib/graph/node.c          | 25 +++++++++++++++++++++++++
>  lib/graph/rte_graph.h     | 15 +++++++++++++++
>  lib/graph/version.map     |  3 +++
>  5 files changed, 72 insertions(+)
> 
> diff --git a/lib/graph/graph.c b/lib/graph/graph.c index dff8e690a8..bad00c8fc0
> 100644
> --- a/lib/graph/graph.c
> +++ b/lib/graph/graph.c
> @@ -20,6 +20,22 @@
>  static struct graph_head graph_list = STAILQ_HEAD_INITIALIZER(graph_list);
>  static rte_spinlock_t graph_lock = RTE_SPINLOCK_INITIALIZER;
> 
> +bool
> +graph_is_node_active_in_graph(struct node *node) {
> +	struct graph *graph;
> +
> +	STAILQ_FOREACH(graph, &graph_list, next) {
> +		struct graph_node *graph_node;
> +
> +		STAILQ_FOREACH(graph_node, &graph->node_list, next)
> +			if (graph_node->node == node)
> +				return 1;
> +	}
> +
> +	return 0;
> +}
> +
>  /* Private functions */
>  static struct graph *
>  graph_from_id(rte_graph_t id)
> diff --git a/lib/graph/graph_private.h b/lib/graph/graph_private.h index
> fdaf5649b8..6eecba4f78 100644
> --- a/lib/graph/graph_private.h
> +++ b/lib/graph/graph_private.h
> @@ -440,4 +440,17 @@ int graph_sched_wq_create(struct graph *_graph,
> struct graph *_parent_graph,
>   */
>  void graph_sched_wq_destroy(struct graph *_graph);
> 
> +/**
> + * @internal
> + *
> + * Check if given node present in any of the created graphs.
> + *
> + * @param node
> + *   The node object
> + *
> + * @return
> + *   0 if not present in any graph, else return 1.
> + */
> +bool graph_is_node_active_in_graph(struct node *_node);
> +
>  #endif /* _RTE_GRAPH_PRIVATE_H_ */
> diff --git a/lib/graph/node.c b/lib/graph/node.c index 5ff8dfcd55..5aaf787d5e
> 100644
> --- a/lib/graph/node.c
> +++ b/lib/graph/node.c
> @@ -481,3 +481,28 @@ rte_node_max_count(void)
>  	}
>  	return node_id;
>  }
> +
> +int
> +rte_node_free(rte_node_t id)
> +{
> +	struct node *node;
> +	int rc = -1;
> +
> +	if (node_from_id(id) == NULL)
> +		goto fail;
> +
> +	graph_spinlock_lock();
> +	STAILQ_FOREACH(node, &node_list, next) {
> +		if (id == node->id) {
> +			if (!graph_is_node_active_in_graph(node)) {
> +				STAILQ_REMOVE(&node_list, node, node,
> next);
> +				free(node);
> +				rc = 0;
> +			}
> +			break;
> +		}
> +	}
> +	graph_spinlock_unlock();
> +fail:
> +	return rc;
> +}
> diff --git a/lib/graph/rte_graph.h b/lib/graph/rte_graph.h index
> f5e575dbed..097d0dc9d5 100644
> --- a/lib/graph/rte_graph.h
> +++ b/lib/graph/rte_graph.h
> @@ -22,6 +22,7 @@
>  #include <stdio.h>
> 
>  #include <rte_common.h>
> +#include <rte_compat.h>
> 
>  #ifdef __cplusplus
>  extern "C" {
> @@ -661,6 +662,20 @@ rte_node_is_invalid(rte_node_t id)
>  	return (id == RTE_NODE_ID_INVALID);
>  }
> 
> +/**
> + * Release the memory allocated for a node created using
> +RTE_NODE_REGISTER or rte_node_clone,
> + * if it is not linked to any graphs.
> + *
> + * @param id
> + *   Node id to check.
> + *
> + * @return
> + *   - 0: Success.
> + *   -<0: Failure.
> + */
> +__rte_experimental
> +int rte_node_free(rte_node_t id);
> +
>  /**
>   * Test the validity of edge id.
>   *
> diff --git a/lib/graph/version.map b/lib/graph/version.map index
> 44fadc00fd..a793ea1d8e 100644
> --- a/lib/graph/version.map
> +++ b/lib/graph/version.map
> @@ -58,4 +58,7 @@ EXPERIMENTAL {
> 
>  	# added in 24.11
>  	rte_node_xstat_increment;
> +
> +	# added in 25.03
> +	rte_node_free;
>  };
> --
> 2.43.0
  

Patch

diff --git a/lib/graph/graph.c b/lib/graph/graph.c
index dff8e690a8..bad00c8fc0 100644
--- a/lib/graph/graph.c
+++ b/lib/graph/graph.c
@@ -20,6 +20,22 @@ 
 static struct graph_head graph_list = STAILQ_HEAD_INITIALIZER(graph_list);
 static rte_spinlock_t graph_lock = RTE_SPINLOCK_INITIALIZER;

+bool
+graph_is_node_active_in_graph(struct node *node)
+{
+	struct graph *graph;
+
+	STAILQ_FOREACH(graph, &graph_list, next) {
+		struct graph_node *graph_node;
+
+		STAILQ_FOREACH(graph_node, &graph->node_list, next)
+			if (graph_node->node == node)
+				return 1;
+	}
+
+	return 0;
+}
+
 /* Private functions */
 static struct graph *
 graph_from_id(rte_graph_t id)
diff --git a/lib/graph/graph_private.h b/lib/graph/graph_private.h
index fdaf5649b8..6eecba4f78 100644
--- a/lib/graph/graph_private.h
+++ b/lib/graph/graph_private.h
@@ -440,4 +440,17 @@  int graph_sched_wq_create(struct graph *_graph, struct graph *_parent_graph,
  */
 void graph_sched_wq_destroy(struct graph *_graph);

+/**
+ * @internal
+ *
+ * Check if given node present in any of the created graphs.
+ *
+ * @param node
+ *   The node object
+ *
+ * @return
+ *   0 if not present in any graph, else return 1.
+ */
+bool graph_is_node_active_in_graph(struct node *_node);
+
 #endif /* _RTE_GRAPH_PRIVATE_H_ */
diff --git a/lib/graph/node.c b/lib/graph/node.c
index 5ff8dfcd55..5aaf787d5e 100644
--- a/lib/graph/node.c
+++ b/lib/graph/node.c
@@ -481,3 +481,28 @@  rte_node_max_count(void)
 	}
 	return node_id;
 }
+
+int
+rte_node_free(rte_node_t id)
+{
+	struct node *node;
+	int rc = -1;
+
+	if (node_from_id(id) == NULL)
+		goto fail;
+
+	graph_spinlock_lock();
+	STAILQ_FOREACH(node, &node_list, next) {
+		if (id == node->id) {
+			if (!graph_is_node_active_in_graph(node)) {
+				STAILQ_REMOVE(&node_list, node, node, next);
+				free(node);
+				rc = 0;
+			}
+			break;
+		}
+	}
+	graph_spinlock_unlock();
+fail:
+	return rc;
+}
diff --git a/lib/graph/rte_graph.h b/lib/graph/rte_graph.h
index f5e575dbed..097d0dc9d5 100644
--- a/lib/graph/rte_graph.h
+++ b/lib/graph/rte_graph.h
@@ -22,6 +22,7 @@ 
 #include <stdio.h>

 #include <rte_common.h>
+#include <rte_compat.h>

 #ifdef __cplusplus
 extern "C" {
@@ -661,6 +662,20 @@  rte_node_is_invalid(rte_node_t id)
 	return (id == RTE_NODE_ID_INVALID);
 }

+/**
+ * Release the memory allocated for a node created using RTE_NODE_REGISTER or rte_node_clone,
+ * if it is not linked to any graphs.
+ *
+ * @param id
+ *   Node id to check.
+ *
+ * @return
+ *   - 0: Success.
+ *   -<0: Failure.
+ */
+__rte_experimental
+int rte_node_free(rte_node_t id);
+
 /**
  * Test the validity of edge id.
  *
diff --git a/lib/graph/version.map b/lib/graph/version.map
index 44fadc00fd..a793ea1d8e 100644
--- a/lib/graph/version.map
+++ b/lib/graph/version.map
@@ -58,4 +58,7 @@  EXPERIMENTAL {

 	# added in 24.11
 	rte_node_xstat_increment;
+
+	# added in 25.03
+	rte_node_free;
 };