Skip to content

Commit f340208

Browse files
committed
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
Pull virtio fixes from Michael Tsirkin: "Several fixes, some of them for CVEs" * tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost: vhost: scsi: add weight support vhost: vsock: add weight support vhost_net: fix possible infinite loop vhost: introduce vhost_exceeds_weight() virtio: Fix indentation of VIRTIO_MMIO virtio: add unlikely() to WARN_ON_ONCE()
2 parents f2c7c76 + c1ea02f commit f340208

File tree

7 files changed

+77
-48
lines changed

7 files changed

+77
-48
lines changed

drivers/vhost/net.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -604,12 +604,6 @@ static size_t init_iov_iter(struct vhost_virtqueue *vq, struct iov_iter *iter,
604604
return iov_iter_count(iter);
605605
}
606606

607-
static bool vhost_exceeds_weight(int pkts, int total_len)
608-
{
609-
return total_len >= VHOST_NET_WEIGHT ||
610-
pkts >= VHOST_NET_PKT_WEIGHT;
611-
}
612-
613607
static int get_tx_bufs(struct vhost_net *net,
614608
struct vhost_net_virtqueue *nvq,
615609
struct msghdr *msg,
@@ -779,7 +773,7 @@ static void handle_tx_copy(struct vhost_net *net, struct socket *sock)
779773
int sent_pkts = 0;
780774
bool sock_can_batch = (sock->sk->sk_sndbuf == INT_MAX);
781775

782-
for (;;) {
776+
do {
783777
bool busyloop_intr = false;
784778

785779
if (nvq->done_idx == VHOST_NET_BATCH)
@@ -845,11 +839,7 @@ static void handle_tx_copy(struct vhost_net *net, struct socket *sock)
845839
vq->heads[nvq->done_idx].id = cpu_to_vhost32(vq, head);
846840
vq->heads[nvq->done_idx].len = 0;
847841
++nvq->done_idx;
848-
if (vhost_exceeds_weight(++sent_pkts, total_len)) {
849-
vhost_poll_queue(&vq->poll);
850-
break;
851-
}
852-
}
842+
} while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
853843

854844
vhost_tx_batch(net, nvq, sock, &msg);
855845
}
@@ -874,7 +864,7 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
874864
bool zcopy_used;
875865
int sent_pkts = 0;
876866

877-
for (;;) {
867+
do {
878868
bool busyloop_intr;
879869

880870
/* Release DMAs done buffers first */
@@ -951,11 +941,7 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
951941
else
952942
vhost_zerocopy_signal_used(net, vq);
953943
vhost_net_tx_packet(net);
954-
if (unlikely(vhost_exceeds_weight(++sent_pkts, total_len))) {
955-
vhost_poll_queue(&vq->poll);
956-
break;
957-
}
958-
}
944+
} while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
959945
}
960946

961947
/* Expects to be always run from workqueue - which acts as
@@ -1153,8 +1139,11 @@ static void handle_rx(struct vhost_net *net)
11531139
vq->log : NULL;
11541140
mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
11551141

1156-
while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
1157-
&busyloop_intr))) {
1142+
do {
1143+
sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
1144+
&busyloop_intr);
1145+
if (!sock_len)
1146+
break;
11581147
sock_len += sock_hlen;
11591148
vhost_len = sock_len + vhost_hlen;
11601149
headcount = get_rx_bufs(vq, vq->heads + nvq->done_idx,
@@ -1239,14 +1228,11 @@ static void handle_rx(struct vhost_net *net)
12391228
vhost_log_write(vq, vq_log, log, vhost_len,
12401229
vq->iov, in);
12411230
total_len += vhost_len;
1242-
if (unlikely(vhost_exceeds_weight(++recv_pkts, total_len))) {
1243-
vhost_poll_queue(&vq->poll);
1244-
goto out;
1245-
}
1246-
}
1231+
} while (likely(!vhost_exceeds_weight(vq, ++recv_pkts, total_len)));
1232+
12471233
if (unlikely(busyloop_intr))
12481234
vhost_poll_queue(&vq->poll);
1249-
else
1235+
else if (!sock_len)
12501236
vhost_net_enable_vq(net, vq);
12511237
out:
12521238
vhost_net_signal_used(nvq);
@@ -1338,7 +1324,8 @@ static int vhost_net_open(struct inode *inode, struct file *f)
13381324
vhost_net_buf_init(&n->vqs[i].rxq);
13391325
}
13401326
vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
1341-
UIO_MAXIOV + VHOST_NET_BATCH);
1327+
UIO_MAXIOV + VHOST_NET_BATCH,
1328+
VHOST_NET_PKT_WEIGHT, VHOST_NET_WEIGHT);
13421329

13431330
vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev);
13441331
vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);

drivers/vhost/scsi.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
#define VHOST_SCSI_PREALLOC_UPAGES 2048
5858
#define VHOST_SCSI_PREALLOC_PROT_SGLS 2048
5959

60+
/* Max number of requests before requeueing the job.
61+
* Using this limit prevents one virtqueue from starving others with
62+
* request.
63+
*/
64+
#define VHOST_SCSI_WEIGHT 256
65+
6066
struct vhost_scsi_inflight {
6167
/* Wait for the flush operation to finish */
6268
struct completion comp;
@@ -912,7 +918,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
912918
struct iov_iter in_iter, prot_iter, data_iter;
913919
u64 tag;
914920
u32 exp_data_len, data_direction;
915-
int ret, prot_bytes;
921+
int ret, prot_bytes, c = 0;
916922
u16 lun;
917923
u8 task_attr;
918924
bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
@@ -932,7 +938,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
932938

933939
vhost_disable_notify(&vs->dev, vq);
934940

935-
for (;;) {
941+
do {
936942
ret = vhost_scsi_get_desc(vs, vq, &vc);
937943
if (ret)
938944
goto err;
@@ -1112,7 +1118,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
11121118
break;
11131119
else if (ret == -EIO)
11141120
vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out);
1115-
}
1121+
} while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
11161122
out:
11171123
mutex_unlock(&vq->mutex);
11181124
}
@@ -1171,7 +1177,7 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
11711177
} v_req;
11721178
struct vhost_scsi_ctx vc;
11731179
size_t typ_size;
1174-
int ret;
1180+
int ret, c = 0;
11751181

11761182
mutex_lock(&vq->mutex);
11771183
/*
@@ -1185,7 +1191,7 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
11851191

11861192
vhost_disable_notify(&vs->dev, vq);
11871193

1188-
for (;;) {
1194+
do {
11891195
ret = vhost_scsi_get_desc(vs, vq, &vc);
11901196
if (ret)
11911197
goto err;
@@ -1264,7 +1270,7 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
12641270
break;
12651271
else if (ret == -EIO)
12661272
vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out);
1267-
}
1273+
} while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
12681274
out:
12691275
mutex_unlock(&vq->mutex);
12701276
}
@@ -1621,7 +1627,8 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
16211627
vqs[i] = &vs->vqs[i].vq;
16221628
vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
16231629
}
1624-
vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ, UIO_MAXIOV);
1630+
vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ, UIO_MAXIOV,
1631+
VHOST_SCSI_WEIGHT, 0);
16251632

16261633
vhost_scsi_init_inflight(vs, NULL);
16271634

drivers/vhost/vhost.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,24 @@ static void vhost_dev_free_iovecs(struct vhost_dev *dev)
413413
vhost_vq_free_iovecs(dev->vqs[i]);
414414
}
415415

416+
bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
417+
int pkts, int total_len)
418+
{
419+
struct vhost_dev *dev = vq->dev;
420+
421+
if ((dev->byte_weight && total_len >= dev->byte_weight) ||
422+
pkts >= dev->weight) {
423+
vhost_poll_queue(&vq->poll);
424+
return true;
425+
}
426+
427+
return false;
428+
}
429+
EXPORT_SYMBOL_GPL(vhost_exceeds_weight);
430+
416431
void vhost_dev_init(struct vhost_dev *dev,
417-
struct vhost_virtqueue **vqs, int nvqs, int iov_limit)
432+
struct vhost_virtqueue **vqs, int nvqs,
433+
int iov_limit, int weight, int byte_weight)
418434
{
419435
struct vhost_virtqueue *vq;
420436
int i;
@@ -428,6 +444,8 @@ void vhost_dev_init(struct vhost_dev *dev,
428444
dev->mm = NULL;
429445
dev->worker = NULL;
430446
dev->iov_limit = iov_limit;
447+
dev->weight = weight;
448+
dev->byte_weight = byte_weight;
431449
init_llist_head(&dev->work_list);
432450
init_waitqueue_head(&dev->wait);
433451
INIT_LIST_HEAD(&dev->read_list);

drivers/vhost/vhost.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,13 @@ struct vhost_dev {
171171
struct list_head pending_list;
172172
wait_queue_head_t wait;
173173
int iov_limit;
174+
int weight;
175+
int byte_weight;
174176
};
175177

178+
bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
176179
void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
177-
int nvqs, int iov_limit);
180+
int nvqs, int iov_limit, int weight, int byte_weight);
178181
long vhost_dev_set_owner(struct vhost_dev *dev);
179182
bool vhost_dev_has_owner(struct vhost_dev *dev);
180183
long vhost_dev_check_owner(struct vhost_dev *);

drivers/vhost/vsock.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
#include "vhost.h"
2222

2323
#define VHOST_VSOCK_DEFAULT_HOST_CID 2
24+
/* Max number of bytes transferred before requeueing the job.
25+
* Using this limit prevents one virtqueue from starving others. */
26+
#define VHOST_VSOCK_WEIGHT 0x80000
27+
/* Max number of packets transferred before requeueing the job.
28+
* Using this limit prevents one virtqueue from starving others with
29+
* small pkts.
30+
*/
31+
#define VHOST_VSOCK_PKT_WEIGHT 256
2432

2533
enum {
2634
VHOST_VSOCK_FEATURES = VHOST_FEATURES,
@@ -78,6 +86,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
7886
struct vhost_virtqueue *vq)
7987
{
8088
struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
89+
int pkts = 0, total_len = 0;
8190
bool added = false;
8291
bool restart_tx = false;
8392

@@ -89,7 +98,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
8998
/* Avoid further vmexits, we're already processing the virtqueue */
9099
vhost_disable_notify(&vsock->dev, vq);
91100

92-
for (;;) {
101+
do {
93102
struct virtio_vsock_pkt *pkt;
94103
struct iov_iter iov_iter;
95104
unsigned out, in;
@@ -174,8 +183,9 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
174183
*/
175184
virtio_transport_deliver_tap_pkt(pkt);
176185

186+
total_len += pkt->len;
177187
virtio_transport_free_pkt(pkt);
178-
}
188+
} while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
179189
if (added)
180190
vhost_signal(&vsock->dev, vq);
181191

@@ -350,7 +360,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
350360
struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
351361
dev);
352362
struct virtio_vsock_pkt *pkt;
353-
int head;
363+
int head, pkts = 0, total_len = 0;
354364
unsigned int out, in;
355365
bool added = false;
356366

@@ -360,7 +370,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
360370
goto out;
361371

362372
vhost_disable_notify(&vsock->dev, vq);
363-
for (;;) {
373+
do {
364374
u32 len;
365375

366376
if (!vhost_vsock_more_replies(vsock)) {
@@ -401,9 +411,11 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
401411
else
402412
virtio_transport_free_pkt(pkt);
403413

404-
vhost_add_used(vq, head, sizeof(pkt->hdr) + len);
414+
len += sizeof(pkt->hdr);
415+
vhost_add_used(vq, head, len);
416+
total_len += len;
405417
added = true;
406-
}
418+
} while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
407419

408420
no_more_replies:
409421
if (added)
@@ -531,7 +543,9 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
531543
vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
532544
vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
533545

534-
vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs), UIO_MAXIOV);
546+
vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs),
547+
UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT,
548+
VHOST_VSOCK_WEIGHT);
535549

536550
file->private_data = vsock;
537551
spin_lock_init(&vsock->send_pkt_list_lock);

drivers/virtio/Kconfig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ config VIRTIO_INPUT
6363

6464
If unsure, say M.
6565

66-
config VIRTIO_MMIO
66+
config VIRTIO_MMIO
6767
tristate "Platform bus driver for memory mapped virtio devices"
6868
depends on HAS_IOMEM && HAS_DMA
69-
select VIRTIO
70-
---help---
71-
This drivers provides support for memory mapped virtio
69+
select VIRTIO
70+
---help---
71+
This drivers provides support for memory mapped virtio
7272
platform device driver.
7373

7474
If unsure, say N.

tools/virtio/linux/kernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static inline void free_page(unsigned long addr)
127127
#define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
128128
#define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
129129

130-
#define WARN_ON_ONCE(cond) ((cond) ? fprintf (stderr, "WARNING\n") : 0)
130+
#define WARN_ON_ONCE(cond) (unlikely(cond) ? fprintf (stderr, "WARNING\n") : 0)
131131

132132
#define min(x, y) ({ \
133133
typeof(x) _min1 = (x); \

0 commit comments

Comments
 (0)