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>, stable@dpdk.org
Subject: [PATCH 1/7] vhost: fix VDUSE device destruction failure
Date: Thu, 29 Feb 2024 13:24:56 +0100	[thread overview]
Message-ID: <20240229122502.2572343-2-maxime.coquelin@redhat.com> (raw)
In-Reply-To: <20240229122502.2572343-1-maxime.coquelin@redhat.com>

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")
Cc: stable@dpdk.org

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/vhost/fd_man.c | 21 ++++++++++++++++++---
 lib/vhost/fd_man.h |  5 +++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c
index 79a8d2c006..42ce059039 100644
--- a/lib/vhost/fd_man.c
+++ b/lib/vhost/fd_man.c
@@ -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);
 }
diff --git a/lib/vhost/fd_man.h b/lib/vhost/fd_man.h
index 6315904c8e..cc19937612 100644
--- a/lib/vhost/fd_man.h
+++ b/lib/vhost/fd_man.h
@@ -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;
 };
 
 
-- 
2.43.2


  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 ` Maxime Coquelin [this message]
2024-02-29 13:31   ` [PATCH 1/7] vhost: fix VDUSE device destruction failure 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 ` [PATCH 6/7] vhost: convert fdset sync to eventfd Maxime Coquelin
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-2-maxime.coquelin@redhat.com \
    --to=maxime.coquelin@redhat.com \
    --cc=chenbox@nvidia.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=stable@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).