From: Yehor Malikov <malikovyehor@gmail.com>
To: dev@dpdk.org
Cc: maxime.coquelin@redhat.com, chenbox@nvidia.com,
stephen@networkplumber.org,
Yehor Malikov <Yehor.Malikov@solidigm.com>
Subject: [PATCH v7] vhost: fix use-after-free in fdset during shutdown
Date: Thu, 5 Feb 2026 12:16:30 +0100 [thread overview]
Message-ID: <20260205111630.48009-1-malikovyehor@gmail.com> (raw)
In-Reply-To: <20260204230502.24983-1-malikovyehor@gmail.com>
From: Yehor Malikov <Yehor.Malikov@solidigm.com>
The fdset_event_dispatch thread runs in a loop checking the destroy
flag after each epoll_wait iteration. During process exit,
rte_eal_cleanup() frees hugepages memory while the fdset thread is
still running, causing use-after-free when accessing the fdset
structure.
Add fdset_deinit() function to stop the dispatch thread by setting
the destroy flag and waiting for thread completion. Resource cleanup
(epoll fd, fdsets array, memory) is intentionally skipped since the
function is only called during process exit when the OS will reclaim
all resources anyway, avoiding potential deadlocks from mutex
operations in destructor context.
Use symmetric RTE_FINI destructors for both vhost-user and VDUSE fdsets
to ensure proper cleanup before EAL teardown.
Fixes: e68a6feaa3b3 ("vhost: improve fdset initialization")
Signed-off-by: Yehor Malikov <Yehor.Malikov@solidigm.com>
---
.mailmap | 1 +
lib/vhost/fd_man.c | 16 ++++++++++++++++
lib/vhost/fd_man.h | 1 +
lib/vhost/rte_vhost.h | 15 +++++++++++++++
lib/vhost/socket.c | 29 ++++++++++++++++++++++++++++-
lib/vhost/vduse.c | 17 ++++++++++++++++-
lib/vhost/vduse.h | 1 +
7 files changed, 78 insertions(+), 2 deletions(-)
diff --git a/.mailmap b/.mailmap
index fc53ed2a55..711a6ceff5 100644
--- a/.mailmap
+++ b/.mailmap
@@ -1840,6 +1840,7 @@ Yaroslav Brustinov <ybrustin@cisco.com>
Yash Sharma <ysharma@marvell.com>
Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp> <yasufum.o@gmail.com>
Yelena Krivosheev <yelena@marvell.com>
+Yehor Malikov <Yehor.Malikov@solidigm.com>
Yerden Zhumabekov <e_zhumabekov@sts.kz> <yerden.zhumabekov@sts.kz>
Yevgeny Kliteynik <kliteyn@nvidia.com>
Yi Chen <chenyi221@huawei.com>
diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c
index f9147edee7..9790c8a586 100644
--- a/lib/vhost/fd_man.c
+++ b/lib/vhost/fd_man.c
@@ -149,6 +149,22 @@ fdset_init(const char *name)
return NULL;
}
+void
+fdset_deinit(struct fdset *pfdset)
+{
+ unsigned int val;
+
+ if (pfdset == NULL)
+ return;
+
+ /* Signal the dispatch thread to stop */
+ pfdset->destroy = true;
+
+ /* Wait for the dispatch thread to exit */
+ if (rte_thread_join(pfdset->tid, &val) != 0)
+ VHOST_FDMAN_LOG(ERR, "Failed to join %s event dispatch thread", pfdset->name);
+}
+
static int
fdset_insert_entry(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
{
diff --git a/lib/vhost/fd_man.h b/lib/vhost/fd_man.h
index eadcc6fb42..c9e51badaa 100644
--- a/lib/vhost/fd_man.h
+++ b/lib/vhost/fd_man.h
@@ -15,6 +15,7 @@ struct fdset;
typedef void (*fd_cb)(int fd, void *dat, int *close);
struct fdset *fdset_init(const char *name);
+void fdset_deinit(struct fdset *pfdset);
int fdset_add(struct fdset *pfdset, int fd,
fd_cb rcb, fd_cb wcb, void *dat);
diff --git a/lib/vhost/rte_vhost.h b/lib/vhost/rte_vhost.h
index 2f7c4c0080..31cbbe6039 100644
--- a/lib/vhost/rte_vhost.h
+++ b/lib/vhost/rte_vhost.h
@@ -464,6 +464,21 @@ int rte_vhost_driver_register(const char *path, uint64_t flags);
/* Unregister vhost driver. This is only meaningful to vhost user. */
int rte_vhost_driver_unregister(const char *path);
+/**
+ * Cleanup vhost library resources.
+ *
+ * This function stops the fdset event dispatch threads for both vhost-user
+ * and VDUSE backends. It should be called before rte_eal_cleanup() to ensure
+ * proper shutdown without use-after-free issues.
+ *
+ * The function is safe to call multiple times or even if no vhost driver
+ * was ever started.
+ *
+ * @return
+ * 0 on success
+ */
+int rte_vhost_cleanup(void);
+
/**
* Set the vdpa device id, enforce single connection per socket
*
diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index ae95e7e6b0..e378028fe1 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -76,6 +76,8 @@ struct vhost_user_connection {
};
#define MAX_VHOST_SOCKET 1024
+#define VHOST_USER_FDSET_NAME "vhost-evt"
+
struct vhost_user {
struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
struct fdset *fdset;
@@ -1198,7 +1200,7 @@ rte_vhost_driver_start(const char *path)
vsocket->extbuf, vsocket->linearbuf);
if (vhost_user.fdset == NULL) {
- vhost_user.fdset = fdset_init("vhost-evt");
+ vhost_user.fdset = fdset_init(VHOST_USER_FDSET_NAME);
if (vhost_user.fdset == NULL) {
VHOST_CONFIG_LOG(path, ERR, "failed to init Vhost-user fdset");
return -1;
@@ -1210,3 +1212,28 @@ rte_vhost_driver_start(const char *path)
else
return vhost_user_start_client(vsocket);
}
+
+static void
+vhost_user_fdset_cleanup(void)
+{
+ if (vhost_user.fdset != NULL) {
+ fdset_deinit(vhost_user.fdset);
+ vhost_user.fdset = NULL;
+ }
+}
+
+RTE_EXPORT_SYMBOL(rte_vhost_cleanup)
+int
+rte_vhost_cleanup(void)
+{
+ vhost_user_fdset_cleanup();
+ vduse_fdset_cleanup();
+
+ return 0;
+}
+
+RTE_FINI(vhost_fini)
+{
+ vhost_user_fdset_cleanup();
+ vduse_fdset_cleanup();
+}
diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
index 0b5d158fee..2633871472 100644
--- a/lib/vhost/vduse.c
+++ b/lib/vhost/vduse.c
@@ -27,6 +27,7 @@
#define VHOST_VDUSE_API_VERSION 0
#define VDUSE_CTRL_PATH "/dev/vduse/control"
+#define VDUSE_FDSET_NAME "vduse-evt"
struct vduse {
struct fdset *fdset;
@@ -685,7 +686,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags, bool extbuf, bool
bool reconnect = false;
if (vduse.fdset == NULL) {
- vduse.fdset = fdset_init("vduse-evt");
+ vduse.fdset = fdset_init(VDUSE_FDSET_NAME);
if (vduse.fdset == NULL) {
VHOST_CONFIG_LOG(path, ERR, "failed to init VDUSE fdset");
return -1;
@@ -942,3 +943,17 @@ vduse_device_destroy(const char *path)
return 0;
}
+
+void
+vduse_fdset_cleanup(void)
+{
+ if (vduse.fdset != NULL) {
+ fdset_deinit(vduse.fdset);
+ vduse.fdset = NULL;
+ }
+}
+
+RTE_FINI(vduse_fdset_fini)
+{
+ vduse_fdset_cleanup();
+}
diff --git a/lib/vhost/vduse.h b/lib/vhost/vduse.h
index b2515bb9df..53f16b0238 100644
--- a/lib/vhost/vduse.h
+++ b/lib/vhost/vduse.h
@@ -11,5 +11,6 @@
int vduse_device_create(const char *path, bool compliant_ol_flags, bool extbuf, bool linearbuf);
int vduse_device_destroy(const char *path);
+void vduse_fdset_cleanup(void);
#endif /* _VDUSE_H */
--
2.52.0
next prev parent reply other threads:[~2026-02-05 13:49 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-04 18:03 [PATCH] vhost: fix use-after-free in fdset during shutdown Yehor Malikov
2026-02-04 18:48 ` [PATCH v2] " malikovyehor
2026-02-04 18:58 ` [PATCH v3] " Yehor Malikov
2026-02-04 20:34 ` Stephen Hemminger
2026-02-04 21:32 ` Yehor Malikov
2026-02-04 21:35 ` [PATCH v4] " Yehor Malikov
2026-02-04 22:00 ` [PATCH v5] " Yehor Malikov
2026-02-04 23:05 ` [PATCH v6] " Yehor Malikov
2026-02-05 1:17 ` Stephen Hemminger
2026-02-05 11:16 ` Yehor Malikov [this message]
2026-02-05 11:20 ` [PATCH v7] " Yehor Malikov
2026-02-05 18:30 ` [PATCH v8] " Yehor Malikov
2026-02-05 18:35 ` [PATCH v9] " Yehor Malikov
2026-02-16 10:17 ` Yehor Malikov
2026-02-17 14:31 ` David Marchand
2026-02-18 7:50 ` [PATCH v10] " Yehor Malikov
2026-02-18 8:01 ` [PATCH v11] " Yehor Malikov
2026-02-18 8:52 ` David Marchand
2026-02-18 9:05 ` [PATCH v12] " Yehor Malikov
2026-02-18 10:27 ` David Marchand
2026-02-27 9:00 ` fengchengwen
2026-03-05 10:50 ` Maxime Coquelin
2026-03-05 13:52 ` Maxime Coquelin
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=20260205111630.48009-1-malikovyehor@gmail.com \
--to=malikovyehor@gmail.com \
--cc=Yehor.Malikov@solidigm.com \
--cc=chenbox@nvidia.com \
--cc=dev@dpdk.org \
--cc=maxime.coquelin@redhat.com \
--cc=stephen@networkplumber.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