DPDK patches and discussions
 help / color / mirror / Atom feed
From: David Marchand <david.marchand@redhat.com>
To: dev@dpdk.org
Cc: probb@iol.unh.edu, "Morten Brørup" <mb@smartsharesystems.com>,
	"Thomas Monjalon" <thomas@monjalon.net>,
	"Ferruh Yigit" <ferruh.yigit@amd.com>,
	"Andrew Rybchenko" <andrew.rybchenko@oktetlabs.ru>,
	"Anatoly Burakov" <anatoly.burakov@intel.com>
Subject: [PATCH v4 3/3] ethdev: cleanup shared data with the last port
Date: Wed, 27 Sep 2023 13:45:15 +0200	[thread overview]
Message-ID: <20230927114515.1245213-4-david.marchand@redhat.com> (raw)
In-Reply-To: <20230927114515.1245213-1-david.marchand@redhat.com>

If no port is allocated and no port owner is still registered,
ethdev from a primary process may release the memzone used to store
port data.
This makes it possible for the DPDK memory allocator to release
associated resources back to the OS.

Signed-off-by: David Marchand <david.marchand@redhat.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
Changes since v2:
- tracked owners count and and avoided releasing shared mem if some
  owner is still registered,

---
 lib/ethdev/ethdev_driver.c  |  6 ++++++
 lib/ethdev/ethdev_private.c | 21 ++++++++++++++++++++-
 lib/ethdev/ethdev_private.h |  4 ++++
 lib/ethdev/rte_ethdev.c     |  3 +++
 4 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/lib/ethdev/ethdev_driver.c b/lib/ethdev/ethdev_driver.c
index b339e325a0..fff4b7b4cd 100644
--- a/lib/ethdev/ethdev_driver.c
+++ b/lib/ethdev/ethdev_driver.c
@@ -115,6 +115,8 @@ rte_eth_dev_allocate(const char *name)
 	eth_dev->data->backer_port_id = RTE_MAX_ETHPORTS;
 	eth_dev->data->mtu = RTE_ETHER_MTU;
 	pthread_mutex_init(&eth_dev->data->flow_ops_mutex, NULL);
+	RTE_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
+	eth_dev_shared_data->allocated_ports++;
 
 unlock:
 	rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
@@ -265,6 +267,10 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
 		rte_free(eth_dev->data->dev_private);
 		pthread_mutex_destroy(&eth_dev->data->flow_ops_mutex);
 		memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data));
+		eth_dev->data = NULL;
+
+		eth_dev_shared_data->allocated_ports--;
+		eth_dev_shared_data_release();
 	}
 
 	rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
diff --git a/lib/ethdev/ethdev_private.c b/lib/ethdev/ethdev_private.c
index 911de1e595..aea9112cc2 100644
--- a/lib/ethdev/ethdev_private.c
+++ b/lib/ethdev/ethdev_private.c
@@ -11,6 +11,7 @@
 
 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
 
+static const struct rte_memzone *eth_dev_shared_mz;
 struct eth_dev_shared *eth_dev_shared_data;
 
 /* spinlock for eth device callbacks */
@@ -324,7 +325,7 @@ eth_dev_shared_data_prepare(void)
 	const unsigned int flags = 0;
 	const struct rte_memzone *mz;
 
-	if (eth_dev_shared_data == NULL) {
+	if (eth_dev_shared_mz == NULL) {
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			/* Allocate port data and ownership shared memory. */
 			mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
@@ -337,10 +338,13 @@ eth_dev_shared_data_prepare(void)
 			goto out;
 		}
 
+		eth_dev_shared_mz = mz;
 		eth_dev_shared_data = mz->addr;
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+			eth_dev_shared_data->allocated_owners = 0;
 			eth_dev_shared_data->next_owner_id =
 					RTE_ETH_DEV_NO_OWNER + 1;
+			eth_dev_shared_data->allocated_ports = 0;
 			memset(eth_dev_shared_data->data, 0,
 			       sizeof(eth_dev_shared_data->data));
 		}
@@ -349,6 +353,21 @@ eth_dev_shared_data_prepare(void)
 	return eth_dev_shared_data;
 }
 
+void
+eth_dev_shared_data_release(void)
+{
+	RTE_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
+
+	if (eth_dev_shared_data->allocated_ports != 0)
+		return;
+	if (eth_dev_shared_data->allocated_owners != 0)
+		return;
+
+	rte_memzone_free(eth_dev_shared_mz);
+	eth_dev_shared_mz = NULL;
+	eth_dev_shared_data = NULL;
+}
+
 void
 eth_dev_rxq_release(struct rte_eth_dev *dev, uint16_t qid)
 {
diff --git a/lib/ethdev/ethdev_private.h b/lib/ethdev/ethdev_private.h
index 1572da7b48..0d36b9c30f 100644
--- a/lib/ethdev/ethdev_private.h
+++ b/lib/ethdev/ethdev_private.h
@@ -14,7 +14,9 @@
 #include "rte_ethdev.h"
 
 struct eth_dev_shared {
+	uint64_t allocated_owners;
 	uint64_t next_owner_id;
+	uint64_t allocated_ports;
 	struct rte_eth_dev_data data[RTE_MAX_ETHPORTS];
 };
 
@@ -69,6 +71,8 @@ void eth_dev_fp_ops_setup(struct rte_eth_fp_ops *fpo,
 
 void *eth_dev_shared_data_prepare(void)
 	__rte_exclusive_locks_required(rte_mcfg_ethdev_get_lock());
+void eth_dev_shared_data_release(void)
+	__rte_exclusive_locks_required(rte_mcfg_ethdev_get_lock());
 
 void eth_dev_rxq_release(struct rte_eth_dev *dev, uint16_t qid);
 void eth_dev_txq_release(struct rte_eth_dev *dev, uint16_t qid);
diff --git a/lib/ethdev/rte_ethdev.c b/lib/ethdev/rte_ethdev.c
index 5c9495ecfe..61572d0cd1 100644
--- a/lib/ethdev/rte_ethdev.c
+++ b/lib/ethdev/rte_ethdev.c
@@ -444,6 +444,7 @@ rte_eth_dev_owner_new(uint64_t *owner_id)
 
 	if (eth_dev_shared_data_prepare() != NULL) {
 		*owner_id = eth_dev_shared_data->next_owner_id++;
+		eth_dev_shared_data->allocated_owners++;
 		ret = 0;
 	} else {
 		ret = -ENOMEM;
@@ -566,6 +567,8 @@ rte_eth_dev_owner_delete(const uint64_t owner_id)
 		RTE_ETHDEV_LOG(NOTICE,
 			"All port owners owned by %016"PRIx64" identifier have removed\n",
 			owner_id);
+		eth_dev_shared_data->allocated_owners--;
+		eth_dev_shared_data_release();
 	} else {
 		RTE_ETHDEV_LOG(ERR,
 			       "Invalid owner ID=%016"PRIx64"\n",
-- 
2.41.0


  parent reply	other threads:[~2023-09-27 11:45 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-18  9:13 [PATCH 0/2] Release ethdev shared memory on port cleanup David Marchand
2023-08-18  9:13 ` [PATCH 1/2] ethdev: protect shared memory accesses under one lock David Marchand
2023-08-18  9:13 ` [PATCH 2/2] ethdev: cleanup shared data with the last port David Marchand
2023-08-18 11:36   ` David Marchand
2023-08-18 10:18 ` [PATCH 0/2] Release ethdev shared memory on port cleanup Morten Brørup
2023-08-31 15:34   ` Thomas Monjalon
2023-08-18 13:41 ` [PATCH v2 " David Marchand
2023-08-18 13:41   ` [PATCH v2 1/2] ethdev: protect shared memory accesses under one lock David Marchand
2023-08-18 13:41   ` [PATCH v2 2/2] ethdev: cleanup shared data with the last port David Marchand
2023-08-21  8:58 ` [PATCH v3 0/3] Release ethdev shared memory on port cleanup David Marchand
2023-08-21  8:58   ` [PATCH v3 1/3] ethdev: protect shared memory accesses under one lock David Marchand
2023-08-21  8:58   ` [PATCH v3 2/3] ethdev: avoid panicking in absence of ethdev shared data David Marchand
2023-08-21  8:58   ` [PATCH v3 3/3] ethdev: cleanup shared data with the last port David Marchand
2023-08-31 16:05   ` [PATCH v3 0/3] Release ethdev shared memory on port cleanup Thomas Monjalon
2023-09-01  7:32     ` David Marchand
2023-09-27 11:45 ` [PATCH v4 " David Marchand
2023-09-27 11:45   ` [PATCH v4 1/3] ethdev: protect shared memory accesses under one lock David Marchand
2023-09-27 11:45   ` [PATCH v4 2/3] ethdev: avoid panicking in absence of ethdev shared data David Marchand
2023-09-27 11:45   ` David Marchand [this message]
2023-10-11 12:53   ` [PATCH v4 0/3] Release ethdev shared memory on port cleanup Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230927114515.1245213-4-david.marchand@redhat.com \
    --to=david.marchand@redhat.com \
    --cc=anatoly.burakov@intel.com \
    --cc=andrew.rybchenko@oktetlabs.ru \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=mb@smartsharesystems.com \
    --cc=probb@iol.unh.edu \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).