DPDK patches and discussions
 help / color / mirror / Atom feed
From: Maxime Coquelin <maxime.coquelin@redhat.com>
To: dev@dpdk.org, david.marchand@redhat.com, chenbox@nvidia.com
Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
Subject: [PATCH 6/7] vhost: convert fdset sync to eventfd
Date: Thu, 29 Feb 2024 13:25:01 +0100	[thread overview]
Message-ID: <20240229122502.2572343-7-maxime.coquelin@redhat.com> (raw)
In-Reply-To: <20240229122502.2572343-1-maxime.coquelin@redhat.com>

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 <maxime.coquelin@redhat.com>
---
 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 <pthread.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/eventfd.h>
 #include <unistd.h>
 
 #include <rte_common.h>
@@ -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:
-- 
2.43.2


  parent reply	other threads:[~2024-02-29 12:25 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-29 12:24 [PATCH 0/7] vhost: FD manager improvements Maxime Coquelin
2024-02-29 12:24 ` [PATCH 1/7] vhost: fix VDUSE device destruction failure Maxime Coquelin
2024-02-29 13:31   ` David Marchand
2024-03-04 10:35   ` [PATCH v2] " David Marchand
2024-03-04 15:12     ` Maxime Coquelin
2024-03-05  9:05     ` David Marchand
2024-02-29 12:24 ` [PATCH 2/7] vhost: rename polling mutex Maxime Coquelin
2024-02-29 13:41   ` David Marchand
2024-02-29 12:24 ` [PATCH 3/7] vhost: make use of FD manager init function Maxime Coquelin
2024-02-29 13:42   ` David Marchand
2024-02-29 12:24 ` [PATCH 4/7] vhost: hide synchronization within FD manager Maxime Coquelin
2024-02-29 12:25 ` [PATCH 5/7] vhost: improve fdset initialization Maxime Coquelin
2024-02-29 12:25 ` Maxime Coquelin [this message]
2024-02-29 12:25 ` [PATCH 7/7] vhost: improve FD manager logging Maxime Coquelin
2024-03-04 10:35 ` [PATCH 0/7] vhost: FD manager improvements David Marchand

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=20240229122502.2572343-7-maxime.coquelin@redhat.com \
    --to=maxime.coquelin@redhat.com \
    --cc=chenbox@nvidia.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    /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).