Skip to content

Commit e79b431

Browse files
jasowangmstsirkin
authored andcommitted
vhost: vsock: add weight support
This patch will check the weight and exit the loop if we exceeds the weight. This is useful for preventing vsock kthread from hogging cpu which is guest triggerable. The weight can help to avoid starving the request from on direction while another direction is being processed. The value of weight is picked from vhost-net. This addresses CVE-2019-3900. Cc: Stefan Hajnoczi <[email protected]> Fixes: 433fc58 ("VSOCK: Introduce vhost_vsock.ko") Signed-off-by: Jason Wang <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent e2412c0 commit e79b431

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

drivers/vhost/vsock.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
8686
struct vhost_virtqueue *vq)
8787
{
8888
struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
89+
int pkts = 0, total_len = 0;
8990
bool added = false;
9091
bool restart_tx = false;
9192

@@ -97,7 +98,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
9798
/* Avoid further vmexits, we're already processing the virtqueue */
9899
vhost_disable_notify(&vsock->dev, vq);
99100

100-
for (;;) {
101+
do {
101102
struct virtio_vsock_pkt *pkt;
102103
struct iov_iter iov_iter;
103104
unsigned out, in;
@@ -182,8 +183,9 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
182183
*/
183184
virtio_transport_deliver_tap_pkt(pkt);
184185

186+
total_len += pkt->len;
185187
virtio_transport_free_pkt(pkt);
186-
}
188+
} while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
187189
if (added)
188190
vhost_signal(&vsock->dev, vq);
189191

@@ -358,7 +360,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
358360
struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
359361
dev);
360362
struct virtio_vsock_pkt *pkt;
361-
int head;
363+
int head, pkts = 0, total_len = 0;
362364
unsigned int out, in;
363365
bool added = false;
364366

@@ -368,7 +370,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
368370
goto out;
369371

370372
vhost_disable_notify(&vsock->dev, vq);
371-
for (;;) {
373+
do {
372374
u32 len;
373375

374376
if (!vhost_vsock_more_replies(vsock)) {
@@ -409,9 +411,11 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
409411
else
410412
virtio_transport_free_pkt(pkt);
411413

412-
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;
413417
added = true;
414-
}
418+
} while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
415419

416420
no_more_replies:
417421
if (added)

0 commit comments

Comments
 (0)