]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/3.4.56/virtio-race-2of2.diff
Linux 5.1.4
[thirdparty/kernel/stable-queue.git] / releases / 3.4.56 / virtio-race-2of2.diff
1 From: Michael S. Tsirkin <mst@redhat.com>
2 Subject: virtio_net: fix race in RX VQ processing
3
4 From: Michael S. Tsirkin <mst@redhat.com>
5
6 commit cbdadbbf0c790f79350a8f36029208944c5487d0 upstream
7
8 virtio net called virtqueue_enable_cq on RX path after napi_complete, so
9 with NAPI_STATE_SCHED clear - outside the implicit napi lock.
10 This violates the requirement to synchronize virtqueue_enable_cq wrt
11 virtqueue_add_buf. In particular, used event can move backwards,
12 causing us to lose interrupts.
13 In a debug build, this can trigger panic within START_USE.
14
15 Jason Wang reports that he can trigger the races artificially,
16 by adding udelay() in virtqueue_enable_cb() after virtio_mb().
17
18 However, we must call napi_complete to clear NAPI_STATE_SCHED before
19 polling the virtqueue for used buffers, otherwise napi_schedule_prep in
20 a callback will fail, causing us to lose RX events.
21
22 To fix, call virtqueue_enable_cb_prepare with NAPI_STATE_SCHED
23 set (under napi lock), later call virtqueue_poll with
24 NAPI_STATE_SCHED clear (outside the lock).
25
26 Reported-by: Jason Wang <jasowang@redhat.com>
27 Tested-by: Jason Wang <jasowang@redhat.com>
28 Acked-by: Jason Wang <jasowang@redhat.com>
29 Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
30 Signed-off-by: David S. Miller <davem@davemloft.net>
31 [wg: Backported to 3.2]
32 Signed-off-by: Wolfram Gloger <wmglo@dent.med.uni-muenchen.de>
33 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
34 ---
35 ---
36 drivers/net/virtio_net.c | 5 +++--
37 1 file changed, 3 insertions(+), 2 deletions(-)
38
39 --- a/drivers/net/virtio_net.c
40 +++ b/drivers/net/virtio_net.c
41 @@ -518,7 +518,7 @@ static int virtnet_poll(struct napi_stru
42 {
43 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
44 void *buf;
45 - unsigned int len, received = 0;
46 + unsigned int r, len, received = 0;
47
48 again:
49 while (received < budget &&
50 @@ -535,8 +535,9 @@ again:
51
52 /* Out of packets? */
53 if (received < budget) {
54 + r = virtqueue_enable_cb_prepare(vi->rvq);
55 napi_complete(napi);
56 - if (unlikely(!virtqueue_enable_cb(vi->rvq)) &&
57 + if (unlikely(virtqueue_poll(vi->rvq, r)) &&
58 napi_schedule_prep(napi)) {
59 virtqueue_disable_cb(vi->rvq);
60 __napi_schedule(napi);