From patchwork Fri Aug 18 09:13:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Marchand X-Patchwork-Id: 130510 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6D7D243095; Fri, 18 Aug 2023 11:13:43 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2B4A64324E; Fri, 18 Aug 2023 11:13:42 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id C40BA43257 for ; Fri, 18 Aug 2023 11:13:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692350020; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g3h9SJN3dNdSVeWlOg4mc4F/9ieAp+Wdp3tXFou+qOk=; b=XUNYwheOJxbJyQcMqh/gG8SkNotplUhYQlYygL9ePYq/lNE5Z6mQGBrYTj6Itzk+GXEEEI y+i0YI7WeAvGyw01vM76g3qlQdko+1lp8wVuQErVKun78/hmR/S4QM6Z+1+PAXRTaXPJjo 2z5LiXR8xJo4MB7XdIrP+FfqRKpJFWk= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-605-rRHN8scWOnmYZI_iEpSQQw-1; Fri, 18 Aug 2023 05:13:35 -0400 X-MC-Unique: rRHN8scWOnmYZI_iEpSQQw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E4A941C07264; Fri, 18 Aug 2023 09:13:34 +0000 (UTC) Received: from dmarchan.redhat.com (unknown [10.45.226.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id BDF3540C207B; Fri, 18 Aug 2023 09:13:33 +0000 (UTC) From: David Marchand To: dev@dpdk.org Cc: probb@iol.unh.edu, Thomas Monjalon , Ferruh Yigit , Andrew Rybchenko , Anatoly Burakov Subject: [PATCH 2/2] ethdev: cleanup shared data with the last port Date: Fri, 18 Aug 2023 11:13:21 +0200 Message-ID: <20230818091321.2404089-3-david.marchand@redhat.com> In-Reply-To: <20230818091321.2404089-1-david.marchand@redhat.com> References: <20230818091321.2404089-1-david.marchand@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org If no port is allocated, ethdev (from a primary process) can 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 --- lib/ethdev/ethdev_driver.c | 6 ++++++ lib/ethdev/ethdev_private.c | 16 +++++++++++++++- lib/ethdev/ethdev_private.h | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/ethdev/ethdev_driver.c b/lib/ethdev/ethdev_driver.c index 5bb9c3f97c..c650421c3a 100644 --- a/lib/ethdev/ethdev_driver.c +++ b/lib/ethdev/ethdev_driver.c @@ -113,6 +113,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(ð_dev->data->flow_ops_mutex, NULL); + RTE_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY); + eth_dev_shared_data->allocated_count++; unlock: rte_spinlock_unlock(rte_mcfg_ethdev_get_lock()); @@ -253,6 +255,10 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev) rte_free(eth_dev->data->dev_private); pthread_mutex_destroy(ð_dev->data->flow_ops_mutex); memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data)); + + eth_dev_shared_data->allocated_count--; + if (eth_dev_shared_data->allocated_count == 0) + 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 6756625729..6fe2e37c56 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, @@ -335,16 +336,29 @@ eth_dev_shared_data_prepare(void) if (mz == NULL) rte_panic("Cannot allocate ethdev shared data\n"); + eth_dev_shared_mz = mz; eth_dev_shared_data = mz->addr; if (rte_eal_process_type() == RTE_PROC_PRIMARY) { eth_dev_shared_data->next_owner_id = RTE_ETH_DEV_NO_OWNER + 1; + eth_dev_shared_data->allocated_count = 0; memset(eth_dev_shared_data->data, 0, sizeof(eth_dev_shared_data->data)); } } } +void +eth_dev_shared_data_release(void) +{ + RTE_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY); + if (eth_dev_shared_mz != NULL) { + 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 f7706e6a95..c456ba9a50 100644 --- a/lib/ethdev/ethdev_private.h +++ b/lib/ethdev/ethdev_private.h @@ -15,6 +15,7 @@ struct eth_dev_shared { uint64_t next_owner_id; + uint64_t allocated_count; struct rte_eth_dev_data data[RTE_MAX_ETHPORTS]; }; @@ -69,6 +70,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);