Skip to contentSkip to conversation

vhost: FD manager improvements

PATCH Superseded Maxime Coquelin 3 participants 15 messages Feb 29, 2024 – Mar 5, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover vhost: FD manager improvements

This series aims at improving the Vhost FD manager.

First patch is a fix necessary to have VDUSE devices destroy to work. I expect it to be taken into v24.03 release.

The rest of the series are various improvements to the FD manager that can wait v24.07 release.

Maxime Coquelin (7):
vhost: fix VDUSE device destruction failure
vhost: rename polling mutex
vhost: make use of FD manager init function
vhost: hide synchronization within FD manager
vhost: improve fdset initialization
vhost: convert fdset sync to eventfd
vhost: improve FD manager logging

 lib/vhost/fd_man.c      | 313 +++++++++++++++++------
 lib/vhost/fd_man.c.orig | 538 ++++++++++++++++++++++++++++++++++++++++
 lib/vhost/fd_man.h      |  41 +--
 lib/vhost/socket.c      |  37 +--
 lib/vhost/vduse.c       |  51 +---
 5 files changed, 800 insertions(+), 180 deletions(-)
 create mode 100644 lib/vhost/fd_man.c.orig
Signature · 1 line
2.43.2
#1 lore raw

PATCH 1/7 vhost: fix VDUSE device destruction failure

VDUSE_DESTROY_DEVICE ioctl can fail because the device's chardev is not released despite close syscall having been called. It happens because the events handler thread is still polling the file descriptor.

fdset_pipe_notify() is not enough because it does not ensure the notification has been handled by the event thread, it just returns once the notification is sent.

To fix this, this patch introduces a synchronization mechanism based on pthread's condition, so that fdset_pipe_notify() only returns once the pipe's read callback has been executed.

Fixes 51d018fdac4e ("vhost: add VDUSE events handler")
---
 lib/vhost/fd_man.c | 21 ++++++++++++++++++---
 lib/vhost/fd_man.h |  5 +++++
 2 files changed, 23 insertions(+), 3 deletions(-)
lib/vhost/fd_man.c +18 -3
index 79a8d2c006..42ce059039 100644
@@ -309,10 +309,11 @@ fdset_event_dispatch(void *arg)
 }
 
 static void
-fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
+fdset_pipe_read_cb(int readfd, void *dat,
 		   int *remove __rte_unused)
 {
 	char charbuf[16];
+	struct fdset *fdset = dat;
 	int r = read(readfd, charbuf, sizeof(charbuf));
 	/*
 	 * Just an optimization, we don't care if read() failed
@@ -320,6 +321,11 @@ fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
 	 * compiler happy
 	 */
 	RTE_SET_USED(r);
+
+	pthread_mutex_lock(&fdset->sync_mutex);
+	fdset->sync = true;
+	pthread_cond_broadcast(&fdset->sync_cond);
+	pthread_mutex_unlock(&fdset->sync_mutex);
 }
 
 void
@@ -342,7 +348,7 @@ fdset_pipe_init(struct fdset *fdset)
 	}
 
 	ret = fdset_add(fdset, fdset->u.readfd,
-			fdset_pipe_read_cb, NULL, NULL);
+			fdset_pipe_read_cb, NULL, fdset);
 
 	if (ret < 0) {
 		VHOST_FDMAN_LOG(ERR,
@@ -359,7 +365,12 @@ fdset_pipe_init(struct fdset *fdset)
 void
 fdset_pipe_notify(struct fdset *fdset)
 {
-	int r = write(fdset->u.writefd, "1", 1);
+	int r;
+
+	pthread_mutex_lock(&fdset->sync_mutex);
+
+	fdset->sync = false;
+	r = write(fdset->u.writefd, "1", 1);
 	/*
 	 * Just an optimization, we don't care if write() failed
 	 * so ignore explicitly its return value to make the
@@ -367,4 +378,8 @@ fdset_pipe_notify(struct fdset *fdset)
 	 */
 	RTE_SET_USED(r);
 
+	while (!fdset->sync)
+		pthread_cond_wait(&fdset->sync_cond, &fdset->sync_mutex);
+
+	pthread_mutex_unlock(&fdset->sync_mutex);
 }
lib/vhost/fd_man.h +5 -0
index 6315904c8e..cc19937612 100644
@@ -6,6 +6,7 @@
 #define _FD_MAN_H_
 #include <pthread.h>
 #include <poll.h>
+#include <stdbool.h>
 
 #define MAX_FDS 1024
 
@@ -35,6 +36,10 @@ struct fdset {
 			int writefd;
 		};
 	} u;
+
+	pthread_mutex_t sync_mutex;
+	pthread_cond_t sync_cond;
+	bool sync;
 };
 
 
Signature · 1 line
2.43.2
#2 lore raw

Re: [PATCH 1/7] vhost: fix VDUSE device destruction failure

Hey Maxime,

Quoted 10 lines · Maxime Coquelin

VDUSE_DESTROY_DEVICE ioctl can fail because the device's chardev is not released despite close syscall having been called. It happens because the events handler thread is still polling the file descriptor.

fdset_pipe_notify() is not enough because it does not ensure the notification has been handled by the event thread, it just returns once the notification is sent.

Quoted 6 lines

To fix this, this patch introduces a synchronization mechanism based on pthread's condition, so that fdset_pipe_notify() only returns once the pipe's read callback has been executed.

Fixes 51d018fdac4e ("vhost: add VDUSE events handler")

This looks to be a generic issue in the fd_man code.
In practice, VDUSE only seems to be affected, so I am ok with this Fixes: tag.

Quoted 82 lines
---
 lib/vhost/fd_man.c | 21 ++++++++++++++++++---
 lib/vhost/fd_man.h |  5 +++++
 2 files changed, 23 insertions(+), 3 deletions(-)
lib/vhost/fd_man.c +18 -3
index 79a8d2c006..42ce059039 100644
@@ -309,10 +309,11 @@ fdset_event_dispatch(void *arg)
 }
 
 static void
-fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
+fdset_pipe_read_cb(int readfd, void *dat,
                   int *remove __rte_unused)
 {
        char charbuf[16];
+       struct fdset *fdset = dat;
        int r = read(readfd, charbuf, sizeof(charbuf));
        /*
         * Just an optimization, we don't care if read() failed
@@ -320,6 +321,11 @@ fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
         * compiler happy
         */
        RTE_SET_USED(r);
+
+       pthread_mutex_lock(&fdset->sync_mutex);
+       fdset->sync = true;
+       pthread_cond_broadcast(&fdset->sync_cond);
+       pthread_mutex_unlock(&fdset->sync_mutex);
 }
 
 void
@@ -342,7 +348,7 @@ fdset_pipe_init(struct fdset *fdset)
        }
 
        ret = fdset_add(fdset, fdset->u.readfd,
-                       fdset_pipe_read_cb, NULL, NULL);
+                       fdset_pipe_read_cb, NULL, fdset);
 
        if (ret < 0) {
                VHOST_FDMAN_LOG(ERR,
@@ -359,7 +365,12 @@ fdset_pipe_init(struct fdset *fdset)
 void
 fdset_pipe_notify(struct fdset *fdset)
 {
-       int r = write(fdset->u.writefd, "1", 1);
+       int r;
+
+       pthread_mutex_lock(&fdset->sync_mutex);
+
+       fdset->sync = false;
+       r = write(fdset->u.writefd, "1", 1);
        /*
         * Just an optimization, we don't care if write() failed
         * so ignore explicitly its return value to make the
@@ -367,4 +378,8 @@ fdset_pipe_notify(struct fdset *fdset)
         */
        RTE_SET_USED(r);
 
+       while (!fdset->sync)
+               pthread_cond_wait(&fdset->sync_cond, &fdset->sync_mutex);
+
+       pthread_mutex_unlock(&fdset->sync_mutex);
 }
lib/vhost/fd_man.h +1 -0
index 6315904c8e..cc19937612 100644
@@ -6,6 +6,7 @@
 #define _FD_MAN_H_
 #include <pthread.h>
 #include <poll.h>
+#include <stdbool.h>
 
 #define MAX_FDS 1024
 

lib/vhost/fd_man.h struct fdset { · line 42

Quoted 7 lines
lib/vhost/fd_man.h +4 -0
@@ -35,6 +36,10 @@ struct fdset {
                        int writefd;
                };
        } u;
+
+       pthread_mutex_t sync_mutex;
+       pthread_cond_t sync_cond;
+       bool sync;

We should explicitly initialise those in
https://git.dpdk.org/dpdk/tree/lib/vhost/socket.c#n91 and
https://git.dpdk.org/dpdk/tree/lib/vhost/vduse.c#n34.
The rest looks acceptable to me.

Signature · 1 line
David Marchand
#3 lore raw

PATCH v2 1/1 vhost: fix VDUSE device destruction failure

In reply to Maxime Coquelin

From: Maxime Coquelin <[email protected]>

VDUSE_DESTROY_DEVICE ioctl can fail because the device's chardev is not released despite close syscall having been called. It happens because the events handler thread is still polling the file descriptor.

fdset_pipe_notify() is not enough because it does not ensure the notification has been handled by the event thread, it just returns once the notification is sent.

To fix this, this patch introduces a synchronization mechanism based on pthread's condition, so that fdset_pipe_notify_sync() only returns once the pipe's read callback has been executed.

Fixes 51d018fdac4e ("vhost: add VDUSE events handler")

---
Changes since v1:
- sync'd only when in VDUSE destruction path,
- added explicit init of sync_mutex,

---
 lib/vhost/fd_man.c | 23 +++++++++++++++++++++--
 lib/vhost/fd_man.h |  6 ++++++
 lib/vhost/socket.c |  1 +
 lib/vhost/vduse.c  |  3 ++-
 4 files changed, 30 insertions(+), 3 deletions(-)
lib/vhost/fd_man.c +21 -2
index 79a8d2c006..481e6b900a 100644
@@ -309,10 +309,11 @@ fdset_event_dispatch(void *arg)
 }
 
 static void
-fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
+fdset_pipe_read_cb(int readfd, void *dat,
 		   int *remove __rte_unused)
 {
 	char charbuf[16];
+	struct fdset *fdset = dat;
 	int r = read(readfd, charbuf, sizeof(charbuf));
 	/*
 	 * Just an optimization, we don't care if read() failed
@@ -320,6 +321,11 @@ fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
 	 * compiler happy
 	 */
 	RTE_SET_USED(r);
+
+	pthread_mutex_lock(&fdset->sync_mutex);
+	fdset->sync = true;
+	pthread_cond_broadcast(&fdset->sync_cond);
+	pthread_mutex_unlock(&fdset->sync_mutex);
 }
 
 void
@@ -342,7 +348,7 @@ fdset_pipe_init(struct fdset *fdset)
 	}
 
 	ret = fdset_add(fdset, fdset->u.readfd,
-			fdset_pipe_read_cb, NULL, NULL);
+			fdset_pipe_read_cb, NULL, fdset);
 
 	if (ret < 0) {
 		VHOST_FDMAN_LOG(ERR,
@@ -366,5 +372,18 @@ fdset_pipe_notify(struct fdset *fdset)
 	 * compiler happy
 	 */
 	RTE_SET_USED(r);
+}
+
+void
+fdset_pipe_notify_sync(struct fdset *fdset)
+{
+	pthread_mutex_lock(&fdset->sync_mutex);
+
+	fdset->sync = false;
+	fdset_pipe_notify(fdset);
+
+	while (!fdset->sync)
+		pthread_cond_wait(&fdset->sync_cond, &fdset->sync_mutex);
 
+	pthread_mutex_unlock(&fdset->sync_mutex);
 }
lib/vhost/fd_man.h +6 -0
index 6315904c8e..7816fb11ac 100644
@@ -6,6 +6,7 @@
 #define _FD_MAN_H_
 #include <pthread.h>
 #include <poll.h>
+#include <stdbool.h>
 
 #define MAX_FDS 1024
 
@@ -35,6 +36,10 @@ struct fdset {
 			int writefd;
 		};
 	} u;
+
+	pthread_mutex_t sync_mutex;
+	pthread_cond_t sync_cond;
+	bool sync;
 };
 
 
@@ -53,5 +58,6 @@ int fdset_pipe_init(struct fdset *fdset);
 void fdset_pipe_uninit(struct fdset *fdset);
 
 void fdset_pipe_notify(struct fdset *fdset);
+void fdset_pipe_notify_sync(struct fdset *fdset);
 
 #endif
lib/vhost/socket.c +1 -0
index a2fdac30a4..96b3ab5595 100644
@@ -93,6 +93,7 @@ static struct vhost_user vhost_user = {
 		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
 		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
 		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
+		.sync_mutex = PTHREAD_MUTEX_INITIALIZER,
 		.num = 0
 	},
 	.vsocket_cnt = 0,
lib/vhost/vduse.c +2 -1
index d462428d2c..e0c6991b69 100644
@@ -36,6 +36,7 @@ static struct vduse vduse = {
 		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
 		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
 		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
+		.sync_mutex = PTHREAD_MUTEX_INITIALIZER,
 		.num = 0
 	},
 };
@@ -618,7 +619,7 @@ vduse_device_destroy(const char *path)
 	vduse_device_stop(dev);
 
 	fdset_del(&vduse.fdset, dev->vduse_dev_fd);
-	fdset_pipe_notify(&vduse.fdset);
+	fdset_pipe_notify_sync(&vduse.fdset);
 
 	if (dev->vduse_dev_fd >= 0) {
 		close(dev->vduse_dev_fd);
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH v2] vhost: fix VDUSE device destruction failure

Le lun. 4 mars 2024, 11:36, David Marchand <[email protected]> a écrit :

Quoted 26 lines

From: Maxime Coquelin <[email protected]>

VDUSE_DESTROY_DEVICE ioctl can fail because the device's chardev is not released despite close syscall having been called. It happens because the events handler thread is still polling the file descriptor.

fdset_pipe_notify() is not enough because it does not ensure the notification has been handled by the event thread, it just returns once the notification is sent.

To fix this, this patch introduces a synchronization mechanism based on pthread's condition, so that fdset_pipe_notify_sync() only returns once the pipe's read callback has been executed.

Fixes 51d018fdac4e ("vhost: add VDUSE events handler")

---
Changes since v1:
- sync'd only when in VDUSE destruction path,
- added explicit init of sync_mutex,

Quoted 7 lines
---
 lib/vhost/fd_man.c | 23 +++++++++++++++++++++--
 lib/vhost/fd_man.h |  6 ++++++
 lib/vhost/socket.c |  1 +
 lib/vhost/vduse.c  |  3 ++-
 4 files changed, 30 insertions(+), 3 deletions(-)

Thanks for improving the patch,
Maxime

Quoted 124 lines
lib/vhost/fd_man.c +21 -2
index 79a8d2c006..481e6b900a 100644
@@ -309,10 +309,11 @@ fdset_event_dispatch(void *arg)
 }
 
 static void
-fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
+fdset_pipe_read_cb(int readfd, void *dat,
                   int *remove __rte_unused)
 {
        char charbuf[16];
+       struct fdset *fdset = dat;
        int r = read(readfd, charbuf, sizeof(charbuf));
        /*
         * Just an optimization, we don't care if read() failed
@@ -320,6 +321,11 @@ fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
         * compiler happy
         */
        RTE_SET_USED(r);
+
+       pthread_mutex_lock(&fdset->sync_mutex);
+       fdset->sync = true;
+       pthread_cond_broadcast(&fdset->sync_cond);
+       pthread_mutex_unlock(&fdset->sync_mutex);
 }
 
 void
@@ -342,7 +348,7 @@ fdset_pipe_init(struct fdset *fdset)
        }
 
        ret = fdset_add(fdset, fdset->u.readfd,
-                       fdset_pipe_read_cb, NULL, NULL);
+                       fdset_pipe_read_cb, NULL, fdset);
 
        if (ret < 0) {
                VHOST_FDMAN_LOG(ERR,
@@ -366,5 +372,18 @@ fdset_pipe_notify(struct fdset *fdset)
         * compiler happy
         */
        RTE_SET_USED(r);
+}
+
+void
+fdset_pipe_notify_sync(struct fdset *fdset)
+{
+       pthread_mutex_lock(&fdset->sync_mutex);
+
+       fdset->sync = false;
+       fdset_pipe_notify(fdset);
+
+       while (!fdset->sync)
+               pthread_cond_wait(&fdset->sync_cond, &fdset->sync_mutex);
 
+       pthread_mutex_unlock(&fdset->sync_mutex);
 }
lib/vhost/fd_man.h +6 -0
index 6315904c8e..7816fb11ac 100644
@@ -6,6 +6,7 @@
 #define _FD_MAN_H_
 #include <pthread.h>
 #include <poll.h>
+#include <stdbool.h>
 
 #define MAX_FDS 1024
 
@@ -35,6 +36,10 @@ struct fdset {
                        int writefd;
                };
        } u;
+
+       pthread_mutex_t sync_mutex;
+       pthread_cond_t sync_cond;
+       bool sync;
 };
 
 
@@ -53,5 +58,6 @@ int fdset_pipe_init(struct fdset *fdset);
 void fdset_pipe_uninit(struct fdset *fdset);
 
 void fdset_pipe_notify(struct fdset *fdset);
+void fdset_pipe_notify_sync(struct fdset *fdset);
 
 #endif
lib/vhost/socket.c +1 -0
index a2fdac30a4..96b3ab5595 100644
@@ -93,6 +93,7 @@ static struct vhost_user vhost_user = {
                .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
                .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
                .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
+               .sync_mutex = PTHREAD_MUTEX_INITIALIZER,
                .num = 0
        },
        .vsocket_cnt = 0,
lib/vhost/vduse.c +2 -2
index d462428d2c..e0c6991b69 100644
@@ -36,6 +36,7 @@ static struct vduse vduse = {
                .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
                .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
                .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
+               .sync_mutex = PTHREAD_MUTEX_INITIALIZER,
                .num = 0
        },
 };
@@ -618,7 +619,7 @@ vduse_device_destroy(const char *path)
        vduse_device_stop(dev);
 
        fdset_del(&vduse.fdset, dev->vduse_dev_fd);
-       fdset_pipe_notify(&vduse.fdset);
+       fdset_pipe_notify_sync(&vduse.fdset);
 
        if (dev->vduse_dev_fd >= 0) {
                close(dev->vduse_dev_fd);
--

2.43.0

#5 lore raw

Re: [PATCH v2] vhost: fix VDUSE device destruction failure

In reply to David Marchand
Quoted 20 lines · David Marchand

From: Maxime Coquelin <[email protected]>

VDUSE_DESTROY_DEVICE ioctl can fail because the device's chardev is not released despite close syscall having been called. It happens because the events handler thread is still polling the file descriptor.

fdset_pipe_notify() is not enough because it does not ensure the notification has been handled by the event thread, it just returns once the notification is sent.

To fix this, this patch introduces a synchronization mechanism based on pthread's condition, so that fdset_pipe_notify_sync() only returns once the pipe's read callback has been executed.

Fixes 51d018fdac4e ("vhost: add VDUSE events handler")
Quoted 6 lines

---
Changes since v1:
- sync'd only when in VDUSE destruction path,
- added explicit init of sync_mutex,

Applied, thanks.

Signature · 1 line
David Marchand
#6 lore raw

PATCH 2/7 vhost: rename polling mutex

In reply to Maxime Coquelin

This trivial patch fixes a typo in fd's manager polling mutex name.

---
 lib/vhost/fd_man.c | 8 ++++----
 lib/vhost/fd_man.h | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
lib/vhost/fd_man.c +4 -4
index 42ce059039..5dde40e51a 100644
@@ -125,9 +125,9 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
 	pthread_mutex_lock(&pfdset->fd_mutex);
 	i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
 	if (i == -1) {
-		pthread_mutex_lock(&pfdset->fd_pooling_mutex);
+		pthread_mutex_lock(&pfdset->fd_polling_mutex);
 		fdset_shrink_nolock(pfdset);
-		pthread_mutex_unlock(&pfdset->fd_pooling_mutex);
+		pthread_mutex_unlock(&pfdset->fd_polling_mutex);
 		i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
 		if (i == -1) {
 			pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -244,9 +244,9 @@ fdset_event_dispatch(void *arg)
 		numfds = pfdset->num;
 		pthread_mutex_unlock(&pfdset->fd_mutex);
 
-		pthread_mutex_lock(&pfdset->fd_pooling_mutex);
+		pthread_mutex_lock(&pfdset->fd_polling_mutex);
 		val = poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
-		pthread_mutex_unlock(&pfdset->fd_pooling_mutex);
+		pthread_mutex_unlock(&pfdset->fd_polling_mutex);
 		if (val < 0)
 			continue;
 
lib/vhost/fd_man.h +1 -1
index cc19937612..2517ae5a9b 100644
@@ -24,7 +24,7 @@ struct fdset {
 	struct pollfd rwfds[MAX_FDS];
 	struct fdentry fd[MAX_FDS];
 	pthread_mutex_t fd_mutex;
-	pthread_mutex_t fd_pooling_mutex;
+	pthread_mutex_t fd_polling_mutex;
 	int num;	/* current fd number of this fdset */
 
 	union pipefds {
Signature · 1 line
2.43.2
#8 lore raw

PATCH 3/7 vhost: make use of FD manager init function

In reply to Maxime Coquelin

Instead of statically initialize the fdset, this patch converts VDUSE and Vhost-user to use fdset_init() function, which now also initialize the mutexes.

This is preliminary rework to hide FDs manager pipe from its users.

---
 lib/vhost/fd_man.c |  9 +++++++--
 lib/vhost/fd_man.h |  2 +-
 lib/vhost/socket.c | 11 +++++------
 lib/vhost/vduse.c  | 14 ++++++--------
 4 files changed, 19 insertions(+), 17 deletions(-)
lib/vhost/fd_man.c +7 -2
index 5dde40e51a..d33036a171 100644
@@ -96,19 +96,24 @@ fdset_add_fd(struct fdset *pfdset, int idx, int fd,
 	pfd->revents = 0;
 }
 
-void
+int
 fdset_init(struct fdset *pfdset)
 {
 	int i;
 
 	if (pfdset == NULL)
-		return;
+		return -1;
+
+	pthread_mutex_init(&pfdset->fd_mutex, NULL);
+	pthread_mutex_init(&pfdset->fd_polling_mutex, NULL);
 
 	for (i = 0; i < MAX_FDS; i++) {
 		pfdset->fd[i].fd = -1;
 		pfdset->fd[i].dat = NULL;
 	}
 	pfdset->num = 0;
+
+	return 0;
 }
 
 /**
lib/vhost/fd_man.h +1 -1
index 2517ae5a9b..92d24d8591 100644
@@ -43,7 +43,7 @@ struct fdset {
 };
 
 
-void fdset_init(struct fdset *pfdset);
+int fdset_init(struct fdset *pfdset);
 
 int fdset_add(struct fdset *pfdset, int fd,
 	fd_cb rcb, fd_cb wcb, void *dat);
lib/vhost/socket.c +5 -6
index a2fdac30a4..b544e39be7 100644
@@ -89,12 +89,6 @@ static int create_unix_socket(struct vhost_user_socket *vsocket);
 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
 
 static struct vhost_user vhost_user = {
-	.fdset = {
-		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
-		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
-		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
-		.num = 0
-	},
 	.vsocket_cnt = 0,
 	.mutex = PTHREAD_MUTEX_INITIALIZER,
 };
@@ -1187,6 +1181,11 @@ rte_vhost_driver_start(const char *path)
 		return vduse_device_create(path, vsocket->net_compliant_ol_flags);
 
 	if (fdset_tid.opaque_id == 0) {
+		if (fdset_init(&vhost_user.fdset) < 0) {
+			VHOST_CONFIG_LOG(path, ERR, "Failed to init Vhost-user fdset");
+			return -1;
+		}
+
 		/**
 		 * create a pipe which will be waited by poll and notified to
 		 * rebuild the wait list of poll.
lib/vhost/vduse.c +6 -8
index d462428d2c..d83d7b0d7c 100644
@@ -31,14 +31,7 @@ struct vduse {
 	struct fdset fdset;
 };
 
-static struct vduse vduse = {
-	.fdset = {
-		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
-		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
-		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
-		.num = 0
-	},
-};
+static struct vduse vduse;
 
 static bool vduse_events_thread;
 
@@ -434,6 +427,11 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
 
 	/* If first device, create events dispatcher thread */
 	if (vduse_events_thread == false) {
+		if (fdset_init(&vduse.fdset) < 0) {
+			VHOST_CONFIG_LOG(path, ERR, "Failed to init VDUSE fdset");
+			return -1;
+		}
+
 		/**
 		 * create a pipe which will be waited by poll and notified to
 		 * rebuild the wait list of poll.
Signature · 1 line
2.43.2
#9 lore raw

Re: [PATCH 3/7] vhost: make use of FD manager init function

Quoted 26 lines · Maxime Coquelin

Instead of statically initialize the fdset, this patch converts VDUSE and Vhost-user to use fdset_init() function, which now also initialize the mutexes.

This is preliminary rework to hide FDs manager pipe from its users.

---
 lib/vhost/fd_man.c |  9 +++++++--
 lib/vhost/fd_man.h |  2 +-
 lib/vhost/socket.c | 11 +++++------
 lib/vhost/vduse.c  | 14 ++++++--------
 4 files changed, 19 insertions(+), 17 deletions(-)
lib/vhost/fd_man.c +1 -1
index 5dde40e51a..d33036a171 100644
@@ -96,19 +96,24 @@ fdset_add_fd(struct fdset *pfdset, int idx, int fd,
        pfd->revents = 0;
 }
 
-void
+int

lib/vhost/fd_man.c fdset_add_fd(struct fdset *pfdset, int idx, int fd, · line 108

Quoted 10 lines
lib/vhost/fd_man.c +4 -1
 fdset_init(struct fdset *pfdset)
 {
        int i;
 
        if (pfdset == NULL)
-               return;
+               return -1;
+
+       pthread_mutex_init(&pfdset->fd_mutex, NULL);
+       pthread_mutex_init(&pfdset->fd_polling_mutex, NULL);

Following patch 1, we are missing init of sync_* variables.

Quoted 42 lines
lib/vhost/fd_man.c +2 -0
        for (i = 0; i < MAX_FDS; i++) {
                pfdset->fd[i].fd = -1;
                pfdset->fd[i].dat = NULL;
        }
        pfdset->num = 0;
+
+       return 0;
 }
 
 /**
lib/vhost/fd_man.h +1 -1
index 2517ae5a9b..92d24d8591 100644
@@ -43,7 +43,7 @@ struct fdset {
 };
 
 
-void fdset_init(struct fdset *pfdset);
+int fdset_init(struct fdset *pfdset);
 
 int fdset_add(struct fdset *pfdset, int fd,
        fd_cb rcb, fd_cb wcb, void *dat);
lib/vhost/socket.c +0 -6
index a2fdac30a4..b544e39be7 100644
@@ -89,12 +89,6 @@ static int create_unix_socket(struct vhost_user_socket *vsocket);
 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
 
 static struct vhost_user vhost_user = {
-       .fdset = {
-               .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
-               .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
-               .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
-               .num = 0
-       },
        .vsocket_cnt = 0,
        .mutex = PTHREAD_MUTEX_INITIALIZER,
 };

lib/vhost/socket.c rte_vhost_driver_start(const char *path) · line 1185

Quoted 5 lines
lib/vhost/socket.c +2 -0
@@ -1187,6 +1181,11 @@ rte_vhost_driver_start(const char *path)
                return vduse_device_create(path, vsocket->net_compliant_ol_flags);
 
        if (fdset_tid.opaque_id == 0) {
+               if (fdset_init(&vhost_user.fdset) < 0) {
+                       VHOST_CONFIG_LOG(path, ERR, "Failed to init Vhost-user fdset");

Nit: other log messages in this function are not consistent wrt starting with a capital letter.

Quoted 27 lines
lib/vhost/socket.c +3 -0
+                       return -1;
+               }
+
                /**
                 * create a pipe which will be waited by poll and notified to
                 * rebuild the wait list of poll.
lib/vhost/vduse.c +1 -8
index d462428d2c..d83d7b0d7c 100644
@@ -31,14 +31,7 @@ struct vduse {
        struct fdset fdset;
 };
 
-static struct vduse vduse = {
-       .fdset = {
-               .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
-               .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
-               .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
-               .num = 0
-       },
-};
+static struct vduse vduse;
 
 static bool vduse_events_thread;
 

lib/vhost/vduse.c vduse_device_create(const char *path, bool compliant_ol_flags) · line 434

Quoted 8 lines
lib/vhost/vduse.c +5 -0
@@ -434,6 +427,11 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
 
        /* If first device, create events dispatcher thread */
        if (vduse_events_thread == false) {
+               if (fdset_init(&vduse.fdset) < 0) {
+                       VHOST_CONFIG_LOG(path, ERR, "Failed to init VDUSE fdset");
+                       return -1;
+               }
+

Nit: idem
+ other log messages use "vduse fdset".

Signature · 1 line
David Marchand
#10 lore raw

PATCH 4/7 vhost: hide synchronization within FD manager

In reply to Maxime Coquelin

This patch forces synchronization for all FDs additions or deletions in the FD set. With that, it is no more necessary for the user to know about the FD set pipe, so hide its initialization in the FD manager.

---
 lib/vhost/fd_man.c | 174 +++++++++++++++++++++++++--------------------
 lib/vhost/fd_man.h |   7 +-
 lib/vhost/socket.c |  12 +---
 lib/vhost/vduse.c  |  18 +----
 4 files changed, 101 insertions(+), 110 deletions(-)
lib/vhost/fd_man.c +97 -77
index d33036a171..0ae481b785 100644
@@ -2,7 +2,9 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
+#include <errno.h>
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
 
 #include <rte_common.h>
@@ -17,6 +19,87 @@ RTE_LOG_REGISTER_SUFFIX(vhost_fdset_logtype, fdset, INFO);
 
 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
 
+static void
+fdset_pipe_read_cb(int readfd, void *dat,
+		   int *remove __rte_unused)
+{
+	char charbuf[16];
+	struct fdset *fdset = dat;
+	int r = read(readfd, charbuf, sizeof(charbuf));
+	/*
+	 * Just an optimization, we don't care if read() failed
+	 * so ignore explicitly its return value to make the
+	 * compiler happy
+	 */
+	RTE_SET_USED(r);
+
+	pthread_mutex_lock(&fdset->sync_mutex);
+	fdset->sync = true;
+	pthread_cond_broadcast(&fdset->sync_cond);
+	pthread_mutex_unlock(&fdset->sync_mutex);
+}
+
+static void
+fdset_pipe_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;
+}
+
+static int
+fdset_pipe_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");
+		return -1;
+	}
+
+	ret = fdset_add(fdset, fdset->u.readfd,
+			fdset_pipe_read_cb, NULL, fdset);
+	if (ret < 0) {
+		VHOST_FDMAN_LOG(ERR,
+			"failed to add pipe readfd %d into vhost server fdset",
+			fdset->u.readfd);
+
+		fdset_pipe_uninit(fdset);
+		return -1;
+	}
+
+	return 0;
+}
+
+static void
+fdset_sync(struct fdset *fdset)
+{
+	int ret;
+
+	pthread_mutex_lock(&fdset->sync_mutex);
+
+	fdset->sync = false;
+	ret = write(fdset->u.writefd, "1", 1);
+	if (ret < 0) {
+		VHOST_FDMAN_LOG(ERR,
+			"Failed to write to notification pipe: %s",
+			strerror(errno));
+		goto out_unlock;
+	}
+
+	while (!fdset->sync)
+		pthread_cond_wait(&fdset->sync_cond, &fdset->sync_mutex);
+
+out_unlock:
+	pthread_mutex_unlock(&fdset->sync_mutex);
+}
+
 static int
 get_last_valid_idx(struct fdset *pfdset, int last_valid_idx)
 {
@@ -96,6 +179,12 @@ fdset_add_fd(struct fdset *pfdset, int idx, int fd,
 	pfd->revents = 0;
 }
 
+void
+fdset_uninit(struct fdset *pfdset)
+{
+	fdset_pipe_uninit(pfdset);
+}
+
 int
 fdset_init(struct fdset *pfdset)
 {
@@ -113,7 +202,7 @@ fdset_init(struct fdset *pfdset)
 	}
 	pfdset->num = 0;
 
-	return 0;
+	return fdset_pipe_init(pfdset);
 }
 
 /**
@@ -143,6 +232,8 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
 	fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
 	pthread_mutex_unlock(&pfdset->fd_mutex);
 
+	fdset_sync(pfdset);
+
 	return 0;
 }
 
@@ -174,6 +265,8 @@ fdset_del(struct fdset *pfdset, int fd)
 		pthread_mutex_unlock(&pfdset->fd_mutex);
 	} while (i != -1);
 
+	fdset_sync(pfdset);
+
 	return dat;
 }
 
@@ -207,6 +300,9 @@ fdset_try_del(struct fdset *pfdset, int fd)
 	}
 
 	pthread_mutex_unlock(&pfdset->fd_mutex);
+
+	fdset_sync(pfdset);
+
 	return 0;
 }
 
@@ -312,79 +408,3 @@ fdset_event_dispatch(void *arg)
 
 	return 0;
 }
-
-static void
-fdset_pipe_read_cb(int readfd, void *dat,
-		   int *remove __rte_unused)
-{
-	char charbuf[16];
-	struct fdset *fdset = dat;
-	int r = read(readfd, charbuf, sizeof(charbuf));
-	/*
-	 * Just an optimization, we don't care if read() failed
-	 * so ignore explicitly its return value to make the
-	 * compiler happy
-	 */
-	RTE_SET_USED(r);
-
-	pthread_mutex_lock(&fdset->sync_mutex);
-	fdset->sync = true;
-	pthread_cond_broadcast(&fdset->sync_cond);
-	pthread_mutex_unlock(&fdset->sync_mutex);
-}
-
-void
-fdset_pipe_uninit(struct fdset *fdset)
-{
-	fdset_del(fdset, fdset->u.readfd);
-	close(fdset->u.readfd);
-	close(fdset->u.writefd);
-}
-
-int
-fdset_pipe_init(struct fdset *fdset)
-{
-	int ret;
-
-	if (pipe(fdset->u.pipefd) < 0) {
-		VHOST_FDMAN_LOG(ERR,
-			"failed to create pipe for vhost fdset");
-		return -1;
-	}
-
-	ret = fdset_add(fdset, fdset->u.readfd,
-			fdset_pipe_read_cb, NULL, fdset);
-
-	if (ret < 0) {
-		VHOST_FDMAN_LOG(ERR,
-			"failed to add pipe readfd %d into vhost server fdset",
-			fdset->u.readfd);
-
-		fdset_pipe_uninit(fdset);
-		return -1;
-	}
-
-	return 0;
-}
-
-void
-fdset_pipe_notify(struct fdset *fdset)
-{
-	int r;
-
-	pthread_mutex_lock(&fdset->sync_mutex);
-
-	fdset->sync = false;
-	r = write(fdset->u.writefd, "1", 1);
-	/*
-	 * Just an optimization, we don't care if write() failed
-	 * so ignore explicitly its return value to make the
-	 * compiler happy
-	 */
-	RTE_SET_USED(r);
-
-	while (!fdset->sync)
-		pthread_cond_wait(&fdset->sync_cond, &fdset->sync_mutex);
-
-	pthread_mutex_unlock(&fdset->sync_mutex);
-}
lib/vhost/fd_man.h +1 -6
index 92d24d8591..c18e3a435c 100644
@@ -42,6 +42,7 @@ struct fdset {
 	bool sync;
 };
 
+void fdset_uninit(struct fdset *pfdset);
 
 int fdset_init(struct fdset *pfdset);
 
@@ -53,10 +54,4 @@ int fdset_try_del(struct fdset *pfdset, int fd);
 
 uint32_t fdset_event_dispatch(void *arg);
 
-int fdset_pipe_init(struct fdset *fdset);
-
-void fdset_pipe_uninit(struct fdset *fdset);
-
-void fdset_pipe_notify(struct fdset *fdset);
-
 #endif
lib/vhost/socket.c +1 -11
index b544e39be7..2f93d48c31 100644
@@ -278,7 +278,6 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
 	TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
 	pthread_mutex_unlock(&vsocket->conn_mutex);
 
-	fdset_pipe_notify(&vhost_user.fdset);
 	return;
 
 err_cleanup:
@@ -1186,20 +1185,11 @@ rte_vhost_driver_start(const char *path)
 			return -1;
 		}
 
-		/**
-		 * create a pipe which will be waited by poll and notified to
-		 * rebuild the wait list of poll.
-		 */
-		if (fdset_pipe_init(&vhost_user.fdset) < 0) {
-			VHOST_CONFIG_LOG(path, ERR, "failed to create pipe for vhost fdset");
-			return -1;
-		}
-
 		int ret = rte_thread_create_internal_control(&fdset_tid,
 				"vhost-evt", fdset_event_dispatch, &vhost_user.fdset);
 		if (ret != 0) {
 			VHOST_CONFIG_LOG(path, ERR, "failed to create fdset handling thread");
-			fdset_pipe_uninit(&vhost_user.fdset);
+			fdset_uninit(&vhost_user.fdset);
 			return -1;
 		}
 	}
lib/vhost/vduse.c +2 -16
index d83d7b0d7c..257285a89f 100644
@@ -225,7 +225,6 @@ vduse_vring_setup(struct virtio_net *dev, unsigned int index)
 			close(vq->kickfd);
 			vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 		}
-		fdset_pipe_notify(&vduse.fdset);
 		vhost_enable_guest_notification(dev, vq, 1);
 		VHOST_CONFIG_LOG(dev->ifname, INFO, "Ctrl queue event handler installed");
 	}
@@ -238,10 +237,8 @@ vduse_vring_cleanup(struct virtio_net *dev, unsigned int index)
 	struct vduse_vq_eventfd vq_efd;
 	int ret;
 
-	if (vq == dev->cvq && vq->kickfd >= 0) {
+	if (vq == dev->cvq && vq->kickfd >= 0)
 		fdset_del(&vduse.fdset, vq->kickfd);
-		fdset_pipe_notify(&vduse.fdset);
-	}
 
 	vq_efd.index = index;
 	vq_efd.fd = VDUSE_EVENTFD_DEASSIGN;
@@ -432,20 +429,11 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
 			return -1;
 		}
 
-		/**
-		 * create a pipe which will be waited by poll and notified to
-		 * rebuild the wait list of poll.
-		 */
-		if (fdset_pipe_init(&vduse.fdset) < 0) {
-			VHOST_CONFIG_LOG(path, ERR, "failed to create pipe for vduse fdset");
-			return -1;
-		}
-
 		ret = rte_thread_create_internal_control(&fdset_tid, "vduse-evt",
 				fdset_event_dispatch, &vduse.fdset);
 		if (ret != 0) {
 			VHOST_CONFIG_LOG(path, ERR, "failed to create vduse fdset handling thread");
-			fdset_pipe_uninit(&vduse.fdset);
+			fdset_uninit(&vduse.fdset);
 			return -1;
 		}
 
@@ -573,7 +561,6 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
 				dev->vduse_dev_fd);
 		goto out_dev_destroy;
 	}
-	fdset_pipe_notify(&vduse.fdset);
 
 	free(dev_config);
 
@@ -616,7 +603,6 @@ vduse_device_destroy(const char *path)
 	vduse_device_stop(dev);
 
 	fdset_del(&vduse.fdset, dev->vduse_dev_fd);
-	fdset_pipe_notify(&vduse.fdset);
 
 	if (dev->vduse_dev_fd >= 0) {
 		close(dev->vduse_dev_fd);
Signature · 1 line
2.43.2
#11 lore raw

PATCH 5/7 vhost: improve fdset initialization

In reply to Maxime Coquelin

This patch heavily reworks fdset initialization:
- fdsets are now dynamically allocated by the FD manager
- the event dispatcher is now created by the FD manager
- struct fdset is now opaque to VDUSE and Vhost

---
 lib/vhost/fd_man.c      | 177 +++++++++++--
 lib/vhost/fd_man.c.orig | 538 ++++++++++++++++++++++++++++++++++++++++
 lib/vhost/fd_man.h      |  39 +--
 lib/vhost/socket.c      |  24 +-
 lib/vhost/vduse.c       |  29 +--
 5 files changed, 715 insertions(+), 92 deletions(-)
 create mode 100644 lib/vhost/fd_man.c.orig
lib/vhost/fd_man.c +157 -20
index 0ae481b785..8b47c97d45 100644
@@ -3,12 +3,16 @@
  */
 
 #include <errno.h>
+#include <pthread.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
 #include <rte_common.h>
 #include <rte_log.h>
+#include <rte_malloc.h>
+#include <rte_string_fns.h>
+#include <rte_thread.h>
 
 #include "fd_man.h"
 
@@ -19,6 +23,79 @@ RTE_LOG_REGISTER_SUFFIX(vhost_fdset_logtype, fdset, INFO);
 
 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
 
+struct fdentry {
+	int fd;		/* -1 indicates this entry is empty */
+	fd_cb rcb;	/* callback when this fd is readable. */
+	fd_cb wcb;	/* callback when this fd is writeable.*/
+	void *dat;	/* fd context */
+	int busy;	/* whether this entry is being used in cb. */
+};
+
+struct fdset {
+	char name[RTE_THREAD_NAME_SIZE];
+	struct pollfd rwfds[MAX_FDS];
+	struct fdentry fd[MAX_FDS];
+	rte_thread_t tid;
+	pthread_mutex_t fd_mutex;
+	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;
+
+	pthread_mutex_t sync_mutex;
+	pthread_cond_t sync_cond;
+	bool sync;
+	bool destroy;
+};
+
+static int fdset_add_no_sync(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat);
+static uint32_t fdset_event_dispatch(void *arg);
+
+#define MAX_FDSETS 8
+
+static struct fdset *fdsets[MAX_FDSETS];
+pthread_mutex_t fdsets_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static struct fdset *
+fdset_lookup(const char *name)
+{
+	int i;
+
+	for (i = 0; i < MAX_FDSETS; i++) {
+		struct fdset *fdset = fdsets[i];
+		if (fdset == NULL)
+			continue;
+
+		if (!strncmp(fdset->name, name, RTE_THREAD_NAME_SIZE))
+			return fdset;
+	}
+
+	return NULL;
+}
+
+static int
+fdset_insert(struct fdset *fdset)
+{
+	int i;
+
+	for (i = 0; i < MAX_FDSETS; i++) {
+		if (fdsets[i] == NULL) {
+			fdsets[i] = fdset;
+			return 0;
+		}
+	}
+
+	return -1;
+}
+
 static void
 fdset_pipe_read_cb(int readfd, void *dat,
 		   int *remove __rte_unused)
@@ -63,7 +140,7 @@ fdset_pipe_init(struct fdset *fdset)
 		return -1;
 	}
 
-	ret = fdset_add(fdset, fdset->u.readfd,
+	ret = fdset_add_no_sync(fdset, fdset->u.readfd,
 			fdset_pipe_read_cb, NULL, fdset);
 	if (ret < 0) {
 		VHOST_FDMAN_LOG(ERR,
@@ -179,37 +256,82 @@ fdset_add_fd(struct fdset *pfdset, int idx, int fd,
 	pfd->revents = 0;
 }
 
-void
-fdset_uninit(struct fdset *pfdset)
-{
-	fdset_pipe_uninit(pfdset);
-}
-
-int
-fdset_init(struct fdset *pfdset)
+struct fdset *
+fdset_init(const char *name)
 {
+	struct fdset *fdset;
+	uint32_t val;
 	int i;
 
-	if (pfdset == NULL)
-		return -1;
+	if (name == NULL) {
+		VHOST_FDMAN_LOG(ERR, "Invalid name");
+		goto err;
+	}
 
-	pthread_mutex_init(&pfdset->fd_mutex, NULL);
-	pthread_mutex_init(&pfdset->fd_polling_mutex, NULL);
+	pthread_mutex_lock(&fdsets_mutex);
+	fdset = fdset_lookup(name);
+	if (fdset) {
+		pthread_mutex_unlock(&fdsets_mutex);
+		return fdset;
+	}
+
+	fdset = rte_zmalloc(NULL, sizeof(*fdset), 0);
+	if (!fdset) {
+		VHOST_FDMAN_LOG(ERR, "Failed to alloc fdset %s", name);
+		goto err_unlock;
+	}
+
+	rte_strscpy(fdset->name, name, RTE_THREAD_NAME_SIZE);
+
+	pthread_mutex_init(&fdset->fd_mutex, NULL);
+	pthread_mutex_init(&fdset->fd_polling_mutex, NULL);
 
 	for (i = 0; i < MAX_FDS; i++) {
-		pfdset->fd[i].fd = -1;
-		pfdset->fd[i].dat = NULL;
+		fdset->fd[i].fd = -1;
+		fdset->fd[i].dat = NULL;
 	}
-	pfdset->num = 0;
+	fdset->num = 0;
 
-	return fdset_pipe_init(pfdset);
+	if (fdset_pipe_init(fdset)) {
+		VHOST_FDMAN_LOG(ERR, "Failed to init pipe for %s", name);
+		goto err_free;
+	}
+
+	if (rte_thread_create_internal_control(&fdset->tid, fdset->name,
+					fdset_event_dispatch, fdset)) {
+		VHOST_FDMAN_LOG(ERR, "Failed to create %s event dispatch thread",
+				fdset->name);
+		goto err_pipe;
+	}
+
+	if (fdset_insert(fdset)) {
+		VHOST_FDMAN_LOG(ERR, "Failed to insert fdset %s", name);
+		goto err_thread;
+	}
+
+	pthread_mutex_unlock(&fdsets_mutex);
+
+	return fdset;
+
+err_thread:
+	fdset->destroy = true;
+	fdset_sync(fdset);
+	rte_thread_join(fdset->tid, &val);
+err_pipe:
+	fdset_pipe_uninit(fdset);
+err_free:
+	rte_free(fdset);
+err_unlock:
+	pthread_mutex_unlock(&fdsets_mutex);
+err:
+	return NULL;
 }
 
 /**
  * Register the fd in the fdset with read/write handler and context.
  */
-int
-fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
+static int
+fdset_add_no_sync(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
 {
 	int i;
 
@@ -232,6 +354,18 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
 	fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
 	pthread_mutex_unlock(&pfdset->fd_mutex);
 
+	return 0;
+}
+
+int
+fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
+{
+	int ret;
+
+	ret = fdset_add_no_sync(pfdset, fd, rcb, wcb, dat);
+	if (ret < 0)
+		return ret;
+
 	fdset_sync(pfdset);
 
 	return 0;
@@ -315,7 +449,7 @@ fdset_try_del(struct fdset *pfdset, int fd)
  * will wait until the flag is reset to zero(which indicates the callback is
  * finished), then it could free the context after fdset_del.
  */
-uint32_t
+static uint32_t
 fdset_event_dispatch(void *arg)
 {
 	int i;
@@ -404,6 +538,9 @@ fdset_event_dispatch(void *arg)
 
 		if (need_shrink)
 			fdset_shrink(pfdset);
+
+		if (pfdset->destroy)
+			break;
 	}
 
 	return 0;
lib/vhost/fd_man.c.orig new file +538 -0

538 lines not on this page · raw message

lib/vhost/fd_man.h +4 -35
index c18e3a435c..079fa0155f 100644
@@ -8,50 +8,19 @@
 #include <poll.h>
 #include <stdbool.h>
 
+struct fdset;
+
 #define MAX_FDS 1024
 
 typedef void (*fd_cb)(int fd, void *dat, int *remove);
 
-struct fdentry {
-	int fd;		/* -1 indicates this entry is empty */
-	fd_cb rcb;	/* callback when this fd is readable. */
-	fd_cb wcb;	/* callback when this fd is writeable.*/
-	void *dat;	/* fd context */
-	int busy;	/* whether this entry is being used in cb. */
-};
-
-struct fdset {
-	struct pollfd rwfds[MAX_FDS];
-	struct fdentry fd[MAX_FDS];
-	pthread_mutex_t fd_mutex;
-	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;
-
-	pthread_mutex_t sync_mutex;
-	pthread_cond_t sync_cond;
-	bool sync;
-};
-
-void fdset_uninit(struct fdset *pfdset);
-
-int fdset_init(struct fdset *pfdset);
+struct fdset *fdset_init(const char *name);
 
 int fdset_add(struct fdset *pfdset, int fd,
 	fd_cb rcb, fd_cb wcb, void *dat);
 
 void *fdset_del(struct fdset *pfdset, int fd);
-int fdset_try_del(struct fdset *pfdset, int fd);
 
-uint32_t fdset_event_dispatch(void *arg);
+int fdset_try_del(struct fdset *pfdset, int fd);
 
 #endif
lib/vhost/socket.c +8 -16
index 2f93d48c31..9eebc63479 100644
@@ -76,7 +76,7 @@ struct vhost_user_connection {
 #define MAX_VHOST_SOCKET 1024
 struct vhost_user {
 	struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
-	struct fdset fdset;
+	struct fdset *fdset;
 	int vsocket_cnt;
 	pthread_mutex_t mutex;
 };
@@ -261,7 +261,7 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
 	conn->connfd = fd;
 	conn->vsocket = vsocket;
 	conn->vid = vid;
-	ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
+	ret = fdset_add(vhost_user.fdset, fd, vhost_user_read_cb,
 			NULL, conn);
 	if (ret < 0) {
 		VHOST_CONFIG_LOG(vsocket->path, ERR,
@@ -394,7 +394,7 @@ vhost_user_start_server(struct vhost_user_socket *vsocket)
 	if (ret < 0)
 		goto err;
 
-	ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
+	ret = fdset_add(vhost_user.fdset, fd, vhost_user_server_new_connection,
 		  NULL, vsocket);
 	if (ret < 0) {
 		VHOST_CONFIG_LOG(path, ERR, "failed to add listen fd %d to vhost server fdset",
@@ -1079,7 +1079,7 @@ rte_vhost_driver_unregister(const char *path)
 			 * mutex lock, and try again since the r/wcb
 			 * may use the mutex lock.
 			 */
-			if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) == -1) {
+			if (fdset_try_del(vhost_user.fdset, vsocket->socket_fd) == -1) {
 				pthread_mutex_unlock(&vhost_user.mutex);
 				goto again;
 			}
@@ -1099,7 +1099,7 @@ rte_vhost_driver_unregister(const char *path)
 			 * try again since the r/wcb may use the
 			 * conn_mutex and mutex locks.
 			 */
-			if (fdset_try_del(&vhost_user.fdset,
+			if (fdset_try_del(vhost_user.fdset,
 					  conn->connfd) == -1) {
 				pthread_mutex_unlock(&vsocket->conn_mutex);
 				pthread_mutex_unlock(&vhost_user.mutex);
@@ -1167,7 +1167,6 @@ int
 rte_vhost_driver_start(const char *path)
 {
 	struct vhost_user_socket *vsocket;
-	static rte_thread_t fdset_tid;
 
 	pthread_mutex_lock(&vhost_user.mutex);
 	vsocket = find_vhost_user_socket(path);
@@ -1179,19 +1178,12 @@ rte_vhost_driver_start(const char *path)
 	if (vsocket->is_vduse)
 		return vduse_device_create(path, vsocket->net_compliant_ol_flags);
 
-	if (fdset_tid.opaque_id == 0) {
-		if (fdset_init(&vhost_user.fdset) < 0) {
+	if (vhost_user.fdset == NULL) {
+		vhost_user.fdset = fdset_init("vhost-evt");
+		if (vhost_user.fdset == NULL) {
 			VHOST_CONFIG_LOG(path, ERR, "Failed to init Vhost-user fdset");
 			return -1;
 		}
-
-		int ret = rte_thread_create_internal_control(&fdset_tid,
-				"vhost-evt", fdset_event_dispatch, &vhost_user.fdset);
-		if (ret != 0) {
-			VHOST_CONFIG_LOG(path, ERR, "failed to create fdset handling thread");
-			fdset_uninit(&vhost_user.fdset);
-			return -1;
-		}
 	}
 
 	if (vsocket->is_server)
lib/vhost/vduse.c +8 -21
index 257285a89f..ef2573bdf0 100644
@@ -28,13 +28,11 @@
 #define VDUSE_CTRL_PATH "/dev/vduse/control"
 
 struct vduse {
-	struct fdset fdset;
+	struct fdset *fdset;
 };
 
 static struct vduse vduse;
 
-static bool vduse_events_thread;
-
 static const char * const vduse_reqs_str[] = {
 	"VDUSE_GET_VQ_STATE",
 	"VDUSE_SET_STATUS",
@@ -215,7 +213,7 @@ vduse_vring_setup(struct virtio_net *dev, unsigned int index)
 	}
 
 	if (vq == dev->cvq) {
-		ret = fdset_add(&vduse.fdset, vq->kickfd, vduse_control_queue_event, NULL, dev);
+		ret = fdset_add(vduse.fdset, vq->kickfd, vduse_control_queue_event, NULL, dev);
 		if (ret) {
 			VHOST_CONFIG_LOG(dev->ifname, ERR,
 					"Failed to setup kickfd handler for VQ %u: %s",
@@ -238,7 +236,7 @@ vduse_vring_cleanup(struct virtio_net *dev, unsigned int index)
 	int ret;
 
 	if (vq == dev->cvq && vq->kickfd >= 0)
-		fdset_del(&vduse.fdset, vq->kickfd);
+		fdset_del(vduse.fdset, vq->kickfd);
 
 	vq_efd.index = index;
 	vq_efd.fd = VDUSE_EVENTFD_DEASSIGN;
@@ -413,7 +411,6 @@ int
 vduse_device_create(const char *path, bool compliant_ol_flags)
 {
 	int control_fd, dev_fd, vid, ret;
-	rte_thread_t fdset_tid;
 	uint32_t i, max_queue_pairs, total_queues;
 	struct virtio_net *dev;
 	struct virtio_net_config vnet_config = {{ 0 }};
@@ -422,22 +419,12 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
 	struct vduse_dev_config *dev_config = NULL;
 	const char *name = path + strlen("/dev/vduse/");
 
-	/* If first device, create events dispatcher thread */
-	if (vduse_events_thread == false) {
-		if (fdset_init(&vduse.fdset) < 0) {
+	if (vduse.fdset == NULL) {
+		vduse.fdset = fdset_init("vduse-evt");
+		if (vduse.fdset == NULL) {
 			VHOST_CONFIG_LOG(path, ERR, "Failed to init VDUSE fdset");
 			return -1;
 		}
-
-		ret = rte_thread_create_internal_control(&fdset_tid, "vduse-evt",
-				fdset_event_dispatch, &vduse.fdset);
-		if (ret != 0) {
-			VHOST_CONFIG_LOG(path, ERR, "failed to create vduse fdset handling thread");
-			fdset_uninit(&vduse.fdset);
-			return -1;
-		}
-
-		vduse_events_thread = true;
 	}
 
 	control_fd = open(VDUSE_CTRL_PATH, O_RDWR);
@@ -555,7 +542,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
 
 	dev->cvq = dev->virtqueue[max_queue_pairs * 2];
 
-	ret = fdset_add(&vduse.fdset, dev->vduse_dev_fd, vduse_events_handler, NULL, dev);
+	ret = fdset_add(vduse.fdset, dev->vduse_dev_fd, vduse_events_handler, NULL, dev);
 	if (ret) {
 		VHOST_CONFIG_LOG(name, ERR, "Failed to add fd %d to vduse fdset",
 				dev->vduse_dev_fd);
@@ -602,7 +589,7 @@ vduse_device_destroy(const char *path)
 
 	vduse_device_stop(dev);
 
-	fdset_del(&vduse.fdset, dev->vduse_dev_fd);
+	fdset_del(vduse.fdset, dev->vduse_dev_fd);
 
 	if (dev->vduse_dev_fd >= 0) {
 		close(dev->vduse_dev_fd);
Signature · 1 line
2.43.2
#12 lore raw

PATCH 6/7 vhost: convert fdset sync to eventfd

In reply to Maxime Coquelin

This patch converts fdset's sync mechanism from
pipe to eventfd, as we only use it to send
notification events.

---
 lib/vhost/fd_man.c | 65 +++++++++++++++++++---------------------------
 1 file changed, 26 insertions(+), 39 deletions(-)
lib/vhost/fd_man.c +26 -39
index 8b47c97d45..6a5bd74656 100644
@@ -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:
Signature · 1 line
2.43.2
#13 lore raw

PATCH 7/7 vhost: improve FD manager logging

In reply to Maxime Coquelin

Convert the logging macro to pass the fdset name
as argument.

---
 lib/vhost/fd_man.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
lib/vhost/fd_man.c +11 -14
index 6a5bd74656..18426095b4 100644
@@ -19,8 +19,8 @@
 
 RTE_LOG_REGISTER_SUFFIX(vhost_fdset_logtype, fdset, INFO);
 #define RTE_LOGTYPE_VHOST_FDMAN vhost_fdset_logtype
-#define VHOST_FDMAN_LOG(level, ...) \
-	RTE_LOG_LINE(level, VHOST_FDMAN, "" __VA_ARGS__)
+#define VHOST_FDMAN_LOG(prefix, level, fmt, args...) \
+	RTE_LOG_LINE(level, VHOST_FDMAN, "(%s) " fmt, prefix, ##args)
 
 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
 
@@ -126,15 +126,13 @@ fdset_sync_init(struct fdset *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);
+		VHOST_FDMAN_LOG(fdset->name, ERR, "failed to create eventfd");
 		return -1;
 	}
 
 	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 eventfd %d to %s fdset",
-			fdset->sync_fd, fdset->name);
-
+		VHOST_FDMAN_LOG(fdset->name, ERR, "failed to add eventfd %d", fdset->sync_fd);
 		fdset_sync_uninit(fdset);
 		return -1;
 	}
@@ -152,8 +150,8 @@ fdset_sync(struct fdset *fdset)
 	fdset->sync = false;
 	ret = eventfd_write(fdset->sync_fd, (eventfd_t)1);
 	if (ret < 0) {
-		VHOST_FDMAN_LOG(ERR, "Failed to write sync eventfd for %s fdset: %s",
-			fdset->name, strerror(errno));
+		VHOST_FDMAN_LOG(fdset->name, ERR, "Failed to write sync eventfd: %s",
+				strerror(errno));
 		goto out_unlock;
 	}
 
@@ -251,7 +249,7 @@ fdset_init(const char *name)
 	int i;
 
 	if (name == NULL) {
-		VHOST_FDMAN_LOG(ERR, "Invalid name");
+		VHOST_FDMAN_LOG("fdset", ERR, "Invalid name");
 		goto err;
 	}
 
@@ -264,7 +262,7 @@ fdset_init(const char *name)
 
 	fdset = rte_zmalloc(NULL, sizeof(*fdset), 0);
 	if (!fdset) {
-		VHOST_FDMAN_LOG(ERR, "Failed to alloc fdset %s", name);
+		VHOST_FDMAN_LOG(name, ERR, "Failed to alloc fdset");
 		goto err_unlock;
 	}
 
@@ -280,19 +278,18 @@ fdset_init(const char *name)
 	fdset->num = 0;
 
 	if (fdset_sync_init(fdset)) {
-		VHOST_FDMAN_LOG(ERR, "Failed to init sync for %s", name);
+		VHOST_FDMAN_LOG(fdset->name, ERR, "Failed to init sync");
 		goto err_free;
 	}
 
 	if (rte_thread_create_internal_control(&fdset->tid, fdset->name,
 					fdset_event_dispatch, fdset)) {
-		VHOST_FDMAN_LOG(ERR, "Failed to create %s event dispatch thread",
-				fdset->name);
+		VHOST_FDMAN_LOG(fdset->name, ERR, "Failed to create event dispatch thread");
 		goto err_sync;
 	}
 
 	if (fdset_insert(fdset)) {
-		VHOST_FDMAN_LOG(ERR, "Failed to insert fdset %s", name);
+		VHOST_FDMAN_LOG(fdset->name, ERR, "Failed to insert fdset");
 		goto err_thread;
 	}
 
Signature · 1 line
2.43.2
#14 lore raw
In reply to Maxime Coquelin
Quoted 19 lines · Maxime Coquelin

This series aims at improving the Vhost FD manager.

First patch is a fix necessary to have VDUSE devices destroy to work. I expect it to be taken into v24.03 release.

The rest of the series are various improvements to the FD manager that can wait v24.07 release.

Maxime Coquelin (7):
vhost: fix VDUSE device destruction failure
vhost: rename polling mutex
vhost: make use of FD manager init function
vhost: hide synchronization within FD manager
vhost: improve fdset initialization
vhost: convert fdset sync to eventfd
vhost: improve FD manager logging

Quoted 7 lines
 lib/vhost/fd_man.c      | 313 +++++++++++++++++------
 lib/vhost/fd_man.c.orig | 538 ++++++++++++++++++++++++++++++++++++++++
 lib/vhost/fd_man.h      |  41 +--
 lib/vhost/socket.c      |  37 +--
 lib/vhost/vduse.c       |  51 +---
 5 files changed, 800 insertions(+), 180 deletions(-)
 create mode 100644 lib/vhost/fd_man.c.orig

I marked all but the first patch as Deferred.
I'll send a new revision of the fix to address the deadlock revealed by CI.

Signature · 1 line
David Marchand