From patchwork Thu Feb 29 12:25:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 137485 X-Patchwork-Delegate: maxime.coquelin@redhat.com 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 92E5643C36; Thu, 29 Feb 2024 13:25:48 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4E35442DEF; Thu, 29 Feb 2024 13:25:23 +0100 (CET) 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 A545742D72 for ; Thu, 29 Feb 2024 13:25:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709209517; 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=LmF7qPGs1eZr0guitFSXpVk4EJJ8BM7JQyUuHQzD+ws=; b=hVp6jSsb9PukqW0pGjWa4xQrZUZAbgHYLAh/20k5WichlnH/NNGNXByCKziEkQFyOu3RMJ h7SGVd16v1QiBmVftB5RYzrjKdmyAFJBaQMS9mmiQ6RDBDgvJzoy8pwBZxtRKx3oRjxbDD NEW4JZnzN18o02zqypoibmvrms32poE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-371-VoG6xYfWPvSoFoA5ab2Fog-1; Thu, 29 Feb 2024 07:25:15 -0500 X-MC-Unique: VoG6xYfWPvSoFoA5ab2Fog-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6BCE110651E6; Thu, 29 Feb 2024 12:25:15 +0000 (UTC) Received: from max-p1.redhat.com (unknown [10.39.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 291AE111D3DA; Thu, 29 Feb 2024 12:25:14 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, david.marchand@redhat.com, chenbox@nvidia.com Cc: Maxime Coquelin Subject: [PATCH 6/7] vhost: convert fdset sync to eventfd Date: Thu, 29 Feb 2024 13:25:01 +0100 Message-ID: <20240229122502.2572343-7-maxime.coquelin@redhat.com> In-Reply-To: <20240229122502.2572343-1-maxime.coquelin@redhat.com> References: <20240229122502.2572343-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 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 This patch converts fdset's sync mechanism from pipe to eventfd, as we only use it to send notification events. Signed-off-by: Maxime Coquelin --- lib/vhost/fd_man.c | 65 +++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c index 8b47c97d45..6a5bd74656 100644 --- a/lib/vhost/fd_man.c +++ b/lib/vhost/fd_man.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -40,19 +41,11 @@ struct fdset { pthread_mutex_t fd_polling_mutex; int num; /* current fd number of this fdset */ - union pipefds { - struct { - int pipefd[2]; - }; - struct { - int readfd; - int writefd; - }; - } u; - + int sync_fd; pthread_mutex_t sync_mutex; pthread_cond_t sync_cond; bool sync; + bool destroy; }; @@ -97,12 +90,11 @@ fdset_insert(struct fdset *fdset) } static void -fdset_pipe_read_cb(int readfd, void *dat, - int *remove __rte_unused) +fdset_sync_read_cb(int sync_fd, void *dat, int *remove __rte_unused) { - char charbuf[16]; + eventfd_t val; struct fdset *fdset = dat; - int r = read(readfd, charbuf, sizeof(charbuf)); + int r = eventfd_read(sync_fd, &val); /* * Just an optimization, we don't care if read() failed * so ignore explicitly its return value to make the @@ -117,37 +109,33 @@ fdset_pipe_read_cb(int readfd, void *dat, } static void -fdset_pipe_uninit(struct fdset *fdset) +fdset_sync_uninit(struct fdset *fdset) { - fdset_del(fdset, fdset->u.readfd); - close(fdset->u.readfd); - fdset->u.readfd = -1; - close(fdset->u.writefd); - fdset->u.writefd = -1; + fdset_del(fdset, fdset->sync_fd); + close(fdset->sync_fd); + fdset->sync_fd = -1; } static int -fdset_pipe_init(struct fdset *fdset) +fdset_sync_init(struct fdset *fdset) { int ret; pthread_mutex_init(&fdset->sync_mutex, NULL); pthread_cond_init(&fdset->sync_cond, NULL); - if (pipe(fdset->u.pipefd) < 0) { - VHOST_FDMAN_LOG(ERR, - "failed to create pipe for vhost fdset"); + fdset->sync_fd = eventfd(0, 0); + if (fdset->sync_fd < 0) { + VHOST_FDMAN_LOG(ERR, "failed to create eventfd for %s fdset", fdset->name); return -1; } - ret = fdset_add_no_sync(fdset, fdset->u.readfd, - fdset_pipe_read_cb, NULL, fdset); + ret = fdset_add_no_sync(fdset, fdset->sync_fd, fdset_sync_read_cb, NULL, fdset); if (ret < 0) { - VHOST_FDMAN_LOG(ERR, - "failed to add pipe readfd %d into vhost server fdset", - fdset->u.readfd); + VHOST_FDMAN_LOG(ERR, "failed to add eventfd %d to %s fdset", + fdset->sync_fd, fdset->name); - fdset_pipe_uninit(fdset); + fdset_sync_uninit(fdset); return -1; } @@ -162,11 +150,10 @@ fdset_sync(struct fdset *fdset) pthread_mutex_lock(&fdset->sync_mutex); fdset->sync = false; - ret = write(fdset->u.writefd, "1", 1); + ret = eventfd_write(fdset->sync_fd, (eventfd_t)1); if (ret < 0) { - VHOST_FDMAN_LOG(ERR, - "Failed to write to notification pipe: %s", - strerror(errno)); + VHOST_FDMAN_LOG(ERR, "Failed to write sync eventfd for %s fdset: %s", + fdset->name, strerror(errno)); goto out_unlock; } @@ -292,8 +279,8 @@ fdset_init(const char *name) } fdset->num = 0; - if (fdset_pipe_init(fdset)) { - VHOST_FDMAN_LOG(ERR, "Failed to init pipe for %s", name); + if (fdset_sync_init(fdset)) { + VHOST_FDMAN_LOG(ERR, "Failed to init sync for %s", name); goto err_free; } @@ -301,7 +288,7 @@ fdset_init(const char *name) fdset_event_dispatch, fdset)) { VHOST_FDMAN_LOG(ERR, "Failed to create %s event dispatch thread", fdset->name); - goto err_pipe; + goto err_sync; } if (fdset_insert(fdset)) { @@ -317,8 +304,8 @@ fdset_init(const char *name) fdset->destroy = true; fdset_sync(fdset); rte_thread_join(fdset->tid, &val); -err_pipe: - fdset_pipe_uninit(fdset); +err_sync: + fdset_sync_uninit(fdset); err_free: rte_free(fdset); err_unlock: