Skip to content

Commit e82b9b0

Browse files
jasowangmstsirkin
authored andcommitted
vhost: introduce vhost_exceeds_weight()
We used to have vhost_exceeds_weight() for vhost-net to: - prevent vhost kthread from hogging the cpu - balance the time spent between TX and RX This function could be useful for vsock and scsi as well. So move it to vhost.c. Device must specify a weight which counts the number of requests, or it can also specific a byte_weight which counts the number of bytes that has been processed. Signed-off-by: Jason Wang <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 6166e53 commit e82b9b0

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

drivers/vhost/net.c

Lines changed: 6 additions & 16 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,
@@ -845,10 +839,8 @@ 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);
842+
if (vhost_exceeds_weight(vq, ++sent_pkts, total_len))
850843
break;
851-
}
852844
}
853845

854846
vhost_tx_batch(net, nvq, sock, &msg);
@@ -951,10 +943,9 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
951943
else
952944
vhost_zerocopy_signal_used(net, vq);
953945
vhost_net_tx_packet(net);
954-
if (unlikely(vhost_exceeds_weight(++sent_pkts, total_len))) {
955-
vhost_poll_queue(&vq->poll);
946+
if (unlikely(vhost_exceeds_weight(vq, ++sent_pkts,
947+
total_len)))
956948
break;
957-
}
958949
}
959950
}
960951

@@ -1239,10 +1230,8 @@ static void handle_rx(struct vhost_net *net)
12391230
vhost_log_write(vq, vq_log, log, vhost_len,
12401231
vq->iov, in);
12411232
total_len += vhost_len;
1242-
if (unlikely(vhost_exceeds_weight(++recv_pkts, total_len))) {
1243-
vhost_poll_queue(&vq->poll);
1233+
if (unlikely(vhost_exceeds_weight(vq, ++recv_pkts, total_len)))
12441234
goto out;
1245-
}
12461235
}
12471236
if (unlikely(busyloop_intr))
12481237
vhost_poll_queue(&vq->poll);
@@ -1338,7 +1327,8 @@ static int vhost_net_open(struct inode *inode, struct file *f)
13381327
vhost_net_buf_init(&n->vqs[i].rxq);
13391328
}
13401329
vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
1341-
UIO_MAXIOV + VHOST_NET_BATCH);
1330+
UIO_MAXIOV + VHOST_NET_BATCH,
1331+
VHOST_NET_WEIGHT, VHOST_NET_PKT_WEIGHT);
13421332

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

drivers/vhost/scsi.c

Lines changed: 8 additions & 1 deletion
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;
@@ -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: 11 additions & 1 deletion
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,
@@ -531,7 +539,9 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
531539
vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
532540
vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
533541

534-
vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs), UIO_MAXIOV);
542+
vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs),
543+
UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT,
544+
VHOST_VSOCK_WEIGHT);
535545

536546
file->private_data = vsock;
537547
spin_lock_init(&vsock->send_pkt_list_lock);

0 commit comments

Comments
 (0)