diff options
author | Jason Wang <jasowang@redhat.com> | 2013-01-27 20:05:18 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2013-01-29 15:43:03 -0500 |
commit | 2b8b328b61c799957a456a5a8dab8cc7dea68575 (patch) | |
tree | 7f782227156eb9aca1a0f3c9cd097e778bc8b7af | |
parent | 692a998b908ae4c612d95d1f5f5adae03eca2b79 (diff) |
vhost_net: handle polling errors when setting backend
Currently, the polling errors were ignored, which can lead following issues:
- vhost remove itself unconditionally from waitqueue when stopping the poll,
this may crash the kernel since the previous attempt of starting may fail to
add itself to the waitqueue
- userspace may think the backend were successfully set even when the polling
failed.
Solve this by:
- check poll->wqh before trying to remove from waitqueue
- report polling errors in vhost_poll_start(), tx_poll_start(), the return value
will be checked and returned when userspace want to set the backend
After this fix, there still could be a polling failure after backend is set, it
will addressed by the next patch.
Signed-off-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | drivers/vhost/net.c | 27 | ||||
-rw-r--r-- | drivers/vhost/vhost.c | 18 | ||||
-rw-r--r-- | drivers/vhost/vhost.h | 2 |
3 files changed, 34 insertions, 13 deletions
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index d10ad6f8df7e..959b1cd89e6a 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c | |||
@@ -165,12 +165,16 @@ static void tx_poll_stop(struct vhost_net *net) | |||
165 | } | 165 | } |
166 | 166 | ||
167 | /* Caller must have TX VQ lock */ | 167 | /* Caller must have TX VQ lock */ |
168 | static void tx_poll_start(struct vhost_net *net, struct socket *sock) | 168 | static int tx_poll_start(struct vhost_net *net, struct socket *sock) |
169 | { | 169 | { |
170 | int ret; | ||
171 | |||
170 | if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED)) | 172 | if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED)) |
171 | return; | 173 | return 0; |
172 | vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file); | 174 | ret = vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file); |
173 | net->tx_poll_state = VHOST_NET_POLL_STARTED; | 175 | if (!ret) |
176 | net->tx_poll_state = VHOST_NET_POLL_STARTED; | ||
177 | return ret; | ||
174 | } | 178 | } |
175 | 179 | ||
176 | /* In case of DMA done not in order in lower device driver for some reason. | 180 | /* In case of DMA done not in order in lower device driver for some reason. |
@@ -642,20 +646,23 @@ static void vhost_net_disable_vq(struct vhost_net *n, | |||
642 | vhost_poll_stop(n->poll + VHOST_NET_VQ_RX); | 646 | vhost_poll_stop(n->poll + VHOST_NET_VQ_RX); |
643 | } | 647 | } |
644 | 648 | ||
645 | static void vhost_net_enable_vq(struct vhost_net *n, | 649 | static int vhost_net_enable_vq(struct vhost_net *n, |
646 | struct vhost_virtqueue *vq) | 650 | struct vhost_virtqueue *vq) |
647 | { | 651 | { |
648 | struct socket *sock; | 652 | struct socket *sock; |
653 | int ret; | ||
649 | 654 | ||
650 | sock = rcu_dereference_protected(vq->private_data, | 655 | sock = rcu_dereference_protected(vq->private_data, |
651 | lockdep_is_held(&vq->mutex)); | 656 | lockdep_is_held(&vq->mutex)); |
652 | if (!sock) | 657 | if (!sock) |
653 | return; | 658 | return 0; |
654 | if (vq == n->vqs + VHOST_NET_VQ_TX) { | 659 | if (vq == n->vqs + VHOST_NET_VQ_TX) { |
655 | n->tx_poll_state = VHOST_NET_POLL_STOPPED; | 660 | n->tx_poll_state = VHOST_NET_POLL_STOPPED; |
656 | tx_poll_start(n, sock); | 661 | ret = tx_poll_start(n, sock); |
657 | } else | 662 | } else |
658 | vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file); | 663 | ret = vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file); |
664 | |||
665 | return ret; | ||
659 | } | 666 | } |
660 | 667 | ||
661 | static struct socket *vhost_net_stop_vq(struct vhost_net *n, | 668 | static struct socket *vhost_net_stop_vq(struct vhost_net *n, |
@@ -833,7 +840,9 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) | |||
833 | r = vhost_init_used(vq); | 840 | r = vhost_init_used(vq); |
834 | if (r) | 841 | if (r) |
835 | goto err_used; | 842 | goto err_used; |
836 | vhost_net_enable_vq(n, vq); | 843 | r = vhost_net_enable_vq(n, vq); |
844 | if (r) | ||
845 | goto err_used; | ||
837 | 846 | ||
838 | oldubufs = vq->ubufs; | 847 | oldubufs = vq->ubufs; |
839 | vq->ubufs = ubufs; | 848 | vq->ubufs = ubufs; |
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 34389f75fe65..9759249e6d90 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c | |||
@@ -77,26 +77,38 @@ void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, | |||
77 | init_poll_funcptr(&poll->table, vhost_poll_func); | 77 | init_poll_funcptr(&poll->table, vhost_poll_func); |
78 | poll->mask = mask; | 78 | poll->mask = mask; |
79 | poll->dev = dev; | 79 | poll->dev = dev; |
80 | poll->wqh = NULL; | ||
80 | 81 | ||
81 | vhost_work_init(&poll->work, fn); | 82 | vhost_work_init(&poll->work, fn); |
82 | } | 83 | } |
83 | 84 | ||
84 | /* Start polling a file. We add ourselves to file's wait queue. The caller must | 85 | /* Start polling a file. We add ourselves to file's wait queue. The caller must |
85 | * keep a reference to a file until after vhost_poll_stop is called. */ | 86 | * keep a reference to a file until after vhost_poll_stop is called. */ |
86 | void vhost_poll_start(struct vhost_poll *poll, struct file *file) | 87 | int vhost_poll_start(struct vhost_poll *poll, struct file *file) |
87 | { | 88 | { |
88 | unsigned long mask; | 89 | unsigned long mask; |
90 | int ret = 0; | ||
89 | 91 | ||
90 | mask = file->f_op->poll(file, &poll->table); | 92 | mask = file->f_op->poll(file, &poll->table); |
91 | if (mask) | 93 | if (mask) |
92 | vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask); | 94 | vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask); |
95 | if (mask & POLLERR) { | ||
96 | if (poll->wqh) | ||
97 | remove_wait_queue(poll->wqh, &poll->wait); | ||
98 | ret = -EINVAL; | ||
99 | } | ||
100 | |||
101 | return ret; | ||
93 | } | 102 | } |
94 | 103 | ||
95 | /* Stop polling a file. After this function returns, it becomes safe to drop the | 104 | /* Stop polling a file. After this function returns, it becomes safe to drop the |
96 | * file reference. You must also flush afterwards. */ | 105 | * file reference. You must also flush afterwards. */ |
97 | void vhost_poll_stop(struct vhost_poll *poll) | 106 | void vhost_poll_stop(struct vhost_poll *poll) |
98 | { | 107 | { |
99 | remove_wait_queue(poll->wqh, &poll->wait); | 108 | if (poll->wqh) { |
109 | remove_wait_queue(poll->wqh, &poll->wait); | ||
110 | poll->wqh = NULL; | ||
111 | } | ||
100 | } | 112 | } |
101 | 113 | ||
102 | static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work, | 114 | static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work, |
@@ -792,7 +804,7 @@ long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp) | |||
792 | fput(filep); | 804 | fput(filep); |
793 | 805 | ||
794 | if (pollstart && vq->handle_kick) | 806 | if (pollstart && vq->handle_kick) |
795 | vhost_poll_start(&vq->poll, vq->kick); | 807 | r = vhost_poll_start(&vq->poll, vq->kick); |
796 | 808 | ||
797 | mutex_unlock(&vq->mutex); | 809 | mutex_unlock(&vq->mutex); |
798 | 810 | ||
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 2639c58b23ab..17261e277c02 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h | |||
@@ -42,7 +42,7 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work); | |||
42 | 42 | ||
43 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, | 43 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
44 | unsigned long mask, struct vhost_dev *dev); | 44 | unsigned long mask, struct vhost_dev *dev); |
45 | void vhost_poll_start(struct vhost_poll *poll, struct file *file); | 45 | int vhost_poll_start(struct vhost_poll *poll, struct file *file); |
46 | void vhost_poll_stop(struct vhost_poll *poll); | 46 | void vhost_poll_stop(struct vhost_poll *poll); |
47 | void vhost_poll_flush(struct vhost_poll *poll); | 47 | void vhost_poll_flush(struct vhost_poll *poll); |
48 | void vhost_poll_queue(struct vhost_poll *poll); | 48 | void vhost_poll_queue(struct vhost_poll *poll); |