dm: virtio-input: apply new mevent API to avoid race issue

Teardown callback is provided when mevent_add is called and it is
used to free the virtio-input resources.

Tracked-On: #1877
Signed-off-by: Jian Jun Chen <jian.jun.chen@intel.com>
Acked-by: Yin Fengwei <fengwei.yin@intel.com>
This commit is contained in:
Jian Jun Chen 2018-12-12 14:14:53 +08:00 committed by wenlingz
parent c2df4a8557
commit baf8f8bdbf

View File

@ -543,12 +543,32 @@ virtio_input_get_config(struct virtio_input *vi, uint8_t select,
return found; return found;
} }
static void
virtio_input_teardown(void *param)
{
struct virtio_input *vi;
vi = (struct virtio_input *)param;
if (vi) {
pthread_mutex_destroy(&vi->mtx);
if (vi->event_queue)
free(vi->event_queue);
if (vi->fd > 0)
close(vi->fd);
if (vi->evdev)
free(vi->evdev);
if (vi->serial)
free(vi->serial);
free(vi);
vi = NULL;
}
}
static int static int
virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
{ {
struct virtio_input *vi; struct virtio_input *vi;
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
bool mutex_initialized = false;
char *opt; char *opt;
int flags, ver; int flags, ver;
int rc; int rc;
@ -570,27 +590,27 @@ virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
opt = strsep(&opts, ","); opt = strsep(&opts, ",");
if (!opt) { if (!opt) {
WPRINTF(("%s: evdev path is NULL\n", __func__)); WPRINTF(("%s: evdev path is NULL\n", __func__));
goto fail; goto opt_fail;
} }
vi->evdev = strdup(opt); vi->evdev = strdup(opt);
if (!vi->evdev) { if (!vi->evdev) {
WPRINTF(("%s: strdup failed\n", __func__)); WPRINTF(("%s: strdup failed\n", __func__));
goto fail; goto opt_fail;
} }
if (opts) { if (opts) {
vi->serial = strdup(opts); vi->serial = strdup(opts);
if (!vi->serial) { if (!vi->serial) {
WPRINTF(("%s: strdup serial failed\n", __func__)); WPRINTF(("%s: strdup serial failed\n", __func__));
goto fail; goto serial_fail;
} }
} }
vi->fd = open(vi->evdev, O_RDWR); vi->fd = open(vi->evdev, O_RDWR);
if (vi->fd < 0) { if (vi->fd < 0) {
WPRINTF(("open %s failed %d\n", vi->evdev, errno)); WPRINTF(("open %s failed %d\n", vi->evdev, errno));
goto fail; goto open_fail;
} }
flags = fcntl(vi->fd, F_GETFL); flags = fcntl(vi->fd, F_GETFL);
fcntl(vi->fd, F_SETFL, flags | O_NONBLOCK); fcntl(vi->fd, F_SETFL, flags | O_NONBLOCK);
@ -598,13 +618,13 @@ virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
rc = ioctl(vi->fd, EVIOCGVERSION, &ver); /* is it a evdev device? */ rc = ioctl(vi->fd, EVIOCGVERSION, &ver); /* is it a evdev device? */
if (rc < 0) { if (rc < 0) {
WPRINTF(("%s: get version failed\n", vi->evdev)); WPRINTF(("%s: get version failed\n", vi->evdev));
goto fail; goto evdev_fail;
} }
rc = ioctl(vi->fd, EVIOCGRAB, 1); /* exclusive access */ rc = ioctl(vi->fd, EVIOCGRAB, 1); /* exclusive access */
if (rc < 0) { if (rc < 0) {
WPRINTF(("%s: grab device failed %d\n", vi->evdev, errno)); WPRINTF(("%s: grab device failed %d\n", vi->evdev, errno));
goto fail; goto evdev_fail;
} }
/* init mutex attribute properly to avoid deadlock */ /* init mutex attribute properly to avoid deadlock */
@ -619,7 +639,6 @@ virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
if (rc) if (rc)
DPRINTF(("vtinput: pthread_mutex_init failed with " DPRINTF(("vtinput: pthread_mutex_init failed with "
"error %d!\n", rc)); "error %d!\n", rc));
mutex_initialized = (rc == 0) ? true : false;
vi->event_qsize = VIRTIO_INPUT_PACKET_SIZE; vi->event_qsize = VIRTIO_INPUT_PACKET_SIZE;
vi->event_qindex = 0; vi->event_qindex = 0;
@ -627,13 +646,14 @@ virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
sizeof(struct virtio_input_event_elem)); sizeof(struct virtio_input_event_elem));
if (!vi->event_queue) { if (!vi->event_queue) {
WPRINTF(("vtinput: could not alloc event queue buf\n")); WPRINTF(("vtinput: could not alloc event queue buf\n"));
goto fail; goto evqueue_fail;
} }
vi->mevp = mevent_add(vi->fd, EVF_READ, virtio_input_read_event, vi, NULL, NULL); vi->mevp = mevent_add(vi->fd, EVF_READ, virtio_input_read_event, vi,
virtio_input_teardown, vi);
if (vi->mevp == NULL) { if (vi->mevp == NULL) {
WPRINTF(("vtinput: could not register event\n")); WPRINTF(("vtinput: could not register event\n"));
goto fail; goto mevent_fail;
} }
virtio_linkup(&vi->base, &virtio_input_ops, vi, dev, vi->queues, BACKEND_VBSU); virtio_linkup(&vi->base, &virtio_input_ops, vi, dev, vi->queues, BACKEND_VBSU);
@ -665,32 +685,29 @@ virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
return rc; return rc;
fail: fail:
if (vi) { /* all resources will be freed in the teardown callback */
if (mutex_initialized) mevent_delete(vi->mevp);
pthread_mutex_destroy(&vi->mtx); return -1;
if (vi->event_queue) {
free(vi->event_queue); mevent_fail:
vi->event_queue = NULL; free(vi->event_queue);
} vi->event_queue = NULL;
if (vi->mevp) { evqueue_fail:
mevent_delete(vi->mevp); pthread_mutex_destroy(&vi->mtx);
vi->mevp = NULL; evdev_fail:
} close(vi->fd);
if (vi->fd > 0) { vi->fd = -1;
close(vi->fd); open_fail:
vi->fd = -1; if (vi->serial) {
} free(vi->serial);
if (vi->serial) { vi->serial = NULL;
free(vi->serial);
vi->serial = NULL;
}
if (vi->evdev) {
free(vi->evdev);
vi->evdev = NULL;
}
free(vi);
vi = NULL;
} }
serial_fail:
free(vi->evdev);
vi->evdev = NULL;
opt_fail:
free(vi);
vi = NULL;
return -1; return -1;
} }
@ -700,21 +717,8 @@ virtio_input_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
struct virtio_input *vi; struct virtio_input *vi;
vi = (struct virtio_input *)dev->arg; vi = (struct virtio_input *)dev->arg;
if (vi) { if (vi && vi->mevp)
pthread_mutex_destroy(&vi->mtx); mevent_delete(vi->mevp);
if (vi->event_queue)
free(vi->event_queue);
if (vi->mevp)
mevent_delete(vi->mevp);
if (vi->fd > 0)
close(vi->fd);
if (vi->evdev)
free(vi->evdev);
if (vi->serial)
free(vi->serial);
free(vi);
vi = NULL;
}
} }
struct pci_vdev_ops pci_ops_virtio_input = { struct pci_vdev_ops pci_ops_virtio_input = {