[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 065/109] virtiofsd: Kill threads when queues are stopped
From: |
Dr. David Alan Gilbert (git) |
Subject: |
[PATCH v2 065/109] virtiofsd: Kill threads when queues are stopped |
Date: |
Tue, 21 Jan 2020 12:23:49 +0000 |
From: "Dr. David Alan Gilbert" <address@hidden>
Kill the threads we've started when the queues get stopped.
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
With improvements by:
Signed-off-by: Eryu Guan <address@hidden>
Reviewed-by: Daniel P. Berrangé <address@hidden>
---
tools/virtiofsd/fuse_virtio.c | 51 ++++++++++++++++++++++++++++++-----
1 file changed, 44 insertions(+), 7 deletions(-)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index 872968f2c8..7a8774a3ee 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -41,6 +41,7 @@ struct fv_QueueInfo {
/* Our queue index, corresponds to array position */
int qidx;
int kick_fd;
+ int kill_fd; /* For killing the thread */
/* The element for the command currently being processed */
VuVirtqElement *qe;
@@ -412,14 +413,17 @@ static void *fv_queue_thread(void *opaque)
fuse_log(FUSE_LOG_INFO, "%s: Start for queue %d kick_fd %d\n", __func__,
qi->qidx, qi->kick_fd);
while (1) {
- struct pollfd pf[1];
+ struct pollfd pf[2];
pf[0].fd = qi->kick_fd;
pf[0].events = POLLIN;
pf[0].revents = 0;
+ pf[1].fd = qi->kill_fd;
+ pf[1].events = POLLIN;
+ pf[1].revents = 0;
fuse_log(FUSE_LOG_DEBUG, "%s: Waiting for Queue %d event\n", __func__,
qi->qidx);
- int poll_res = ppoll(pf, 1, NULL, NULL);
+ int poll_res = ppoll(pf, 2, NULL, NULL);
if (poll_res == -1) {
if (errno == EINTR) {
@@ -430,12 +434,23 @@ static void *fv_queue_thread(void *opaque)
fuse_log(FUSE_LOG_ERR, "fv_queue_thread ppoll: %m\n");
break;
}
- assert(poll_res == 1);
+ assert(poll_res >= 1);
if (pf[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
fuse_log(FUSE_LOG_ERR, "%s: Unexpected poll revents %x Queue %d\n",
__func__, pf[0].revents, qi->qidx);
break;
}
+ if (pf[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
+ fuse_log(FUSE_LOG_ERR,
+ "%s: Unexpected poll revents %x Queue %d killfd\n",
+ __func__, pf[1].revents, qi->qidx);
+ break;
+ }
+ if (pf[1].revents) {
+ fuse_log(FUSE_LOG_INFO, "%s: kill event on queue %d - quitting\n",
+ __func__, qi->qidx);
+ break;
+ }
assert(pf[0].revents & POLLIN);
fuse_log(FUSE_LOG_DEBUG, "%s: Got queue event on Queue %d\n", __func__,
qi->qidx);
@@ -589,6 +604,28 @@ out:
return NULL;
}
+static void fv_queue_cleanup_thread(struct fv_VuDev *vud, int qidx)
+{
+ int ret;
+ struct fv_QueueInfo *ourqi;
+
+ assert(qidx < vud->nqueues);
+ ourqi = vud->qi[qidx];
+
+ /* Kill the thread */
+ if (eventfd_write(ourqi->kill_fd, 1)) {
+ fuse_log(FUSE_LOG_ERR, "Eventfd_write for queue %d: %s\n",
+ qidx, strerror(errno));
+ }
+ ret = pthread_join(ourqi->thread, NULL);
+ if (ret) {
+ fuse_log(FUSE_LOG_ERR, "%s: Failed to join thread idx %d err %d\n",
+ __func__, qidx, ret);
+ }
+ close(ourqi->kill_fd);
+ ourqi->kick_fd = -1;
+}
+
/* Callback from libvhost-user on start or stop of a queue */
static void fv_queue_set_started(VuDev *dev, int qidx, bool started)
{
@@ -633,16 +670,16 @@ static void fv_queue_set_started(VuDev *dev, int qidx,
bool started)
}
ourqi = vud->qi[qidx];
ourqi->kick_fd = dev->vq[qidx].kick_fd;
+
+ ourqi->kill_fd = eventfd(0, EFD_CLOEXEC | EFD_SEMAPHORE);
+ assert(ourqi->kill_fd != -1);
if (pthread_create(&ourqi->thread, NULL, fv_queue_thread, ourqi)) {
fuse_log(FUSE_LOG_ERR, "%s: Failed to create thread for queue
%d\n",
__func__, qidx);
assert(0);
}
} else {
- /* TODO: Kill the thread */
- assert(qidx < vud->nqueues);
- ourqi = vud->qi[qidx];
- ourqi->kick_fd = -1;
+ fv_queue_cleanup_thread(vud, qidx);
}
}
--
2.24.1
- Re: [PATCH v2 057/109] docs: Add docs/tools, (continued)
- [PATCH v2 058/109] virtiofsd: add security guide document, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 059/109] virtiofsd: add --syslog command-line option, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 060/109] virtiofsd: print log only when priority is high enough, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 062/109] virtiofsd: Add timestamp to the log with FUSE_LOG_DEBUG level, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 063/109] virtiofsd: Handle reinit, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 061/109] virtiofsd: Add ID to the log with FUSE_LOG_DEBUG level, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 065/109] virtiofsd: Kill threads when queues are stopped,
Dr. David Alan Gilbert (git) <=
- [PATCH v2 064/109] virtiofsd: Handle hard reboot, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 066/109] vhost-user: Print unexpected slave message types, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 067/109] contrib/libvhost-user: Protect slave fd with mutex, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 068/109] virtiofsd: passthrough_ll: add renameat2 support, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 069/109] virtiofsd: passthrough_ll: disable readdirplus on cache=never, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 071/109] virtiofsd: rename unref_inode() to unref_inode_lolocked(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 072/109] virtiofsd: fail when parent inode isn't known in lo_do_lookup(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 070/109] virtiofsd: passthrough_ll: control readdirplus, Dr. David Alan Gilbert (git), 2020/01/21