From patchwork Mon Dec 21 21:32:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Long Li X-Patchwork-Id: 85618 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id AEBC7A09EF; Mon, 21 Dec 2020 22:32:51 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 05D2CCAC4; Mon, 21 Dec 2020 22:32:50 +0100 (CET) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by dpdk.org (Postfix) with ESMTP id 56429CA8D for ; Mon, 21 Dec 2020 22:32:47 +0100 (CET) Received: by linux.microsoft.com (Postfix, from userid 1004) id 879E320B83DE; Mon, 21 Dec 2020 13:32:46 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 879E320B83DE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxonhyperv.com; s=default; t=1608586366; bh=Msdcy0VxXiHuM2Kg/InBHyy+bjH2y7rW/GmKd4Edi84=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Iq53pT3tY65nvmvaGtCjbASKLNUcbtMXxUsnyMKCCx/D4Ji4+MR3vTuTEzZjIOu1L uLfZ3m8L+8Z8KR5gFprHI9p81EbD35vag97HIr+eSFJf2W3c32B5yWs7+e4+nBYnzD XFputesCWALqNRZBPM4NyigFeno7EGXIagkIBJTg= From: Long Li To: Stephen Hemminger Cc: dev@dpdk.org, Long Li Date: Mon, 21 Dec 2020 13:32:36 -0800 Message-Id: <1608586356-20046-1-git-send-email-longli@linuxonhyperv.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1606809383-26660-1-git-send-email-longli@linuxonhyperv.com> References: <1606809383-26660-1-git-send-email-longli@linuxonhyperv.com> Subject: [dpdk-dev] [PATCH v2 1/2] eal/hotplug: allow monitor to be setup by multiple places X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" From: Long Li In some cases, a device or infrastructure may want to enable hotplug but application may also try and start hotplug as well. Therefore change the monitor_started from a boolean into a reference count. Signed-off-by: Long Li --- lib/librte_eal/linux/eal_dev.c | 56 ++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/librte_eal/linux/eal_dev.c b/lib/librte_eal/linux/eal_dev.c index 5c0e752b2d..3b905e18f5 100644 --- a/lib/librte_eal/linux/eal_dev.c +++ b/lib/librte_eal/linux/eal_dev.c @@ -23,8 +23,12 @@ #include "eal_private.h" -static struct rte_intr_handle intr_handle = {.fd = -1 }; -static bool monitor_started; +static struct rte_intr_handle intr_handle = { + .type = RTE_INTR_HANDLE_DEV_EVENT, + .fd = -1, +}; +static rte_rwlock_t monitor_lock = RTE_RWLOCK_INITIALIZER; +static uint32_t monitor_refcount; static bool hotplug_handle; #define EAL_UEV_MSG_LEN 4096 @@ -298,50 +302,70 @@ dev_uev_handler(__rte_unused void *param) int rte_dev_event_monitor_start(void) { - int ret; + int ret = 0; - if (monitor_started) - return 0; + rte_rwlock_write_lock(&monitor_lock); + + if (monitor_refcount) { + monitor_refcount++; + goto exit; + } ret = dev_uev_socket_fd_create(); if (ret) { RTE_LOG(ERR, EAL, "error create device event fd.\n"); - return -1; + goto exit; } - intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT; ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL); if (ret) { RTE_LOG(ERR, EAL, "fail to register uevent callback.\n"); - return -1; + close(intr_handle.fd); + intr_handle.fd = -1; + goto exit; } - monitor_started = true; + monitor_refcount++; - return 0; +exit: + rte_rwlock_write_unlock(&monitor_lock); + return ret; } int rte_dev_event_monitor_stop(void) { - int ret; + int ret = 0; - if (!monitor_started) - return 0; + rte_rwlock_write_lock(&monitor_lock); + + if (!monitor_refcount) { + RTE_LOG(ERR, EAL, "device event monitor already stopped\n"); + goto exit; + } + + if (monitor_refcount > 1) { + monitor_refcount--; + goto exit; + } ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler, (void *)-1); if (ret < 0) { RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n"); - return ret; + goto exit; } close(intr_handle.fd); intr_handle.fd = -1; - monitor_started = false; - return 0; + monitor_refcount--; + +exit: + rte_rwlock_write_unlock(&monitor_lock); + + return ret; } int