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); } }