summaryrefslogtreecommitdiffstats
path: root/drivers/vhost
diff options
context:
space:
mode:
authorJason Wang <jasowang@redhat.com>2018-11-15 04:43:09 -0500
committerDavid S. Miller <davem@davemloft.net>2018-11-17 15:00:42 -0500
commite4dab1e6ea64376ebd3b59281ecaaeb788116be3 (patch)
tree2f1e7d1b6ff105cec5ebcf0f1808ff7da72f9543 /drivers/vhost
parentb8b9618a4f6a6660cb1ff44dd401a4b21f2caf6c (diff)
vhost_net: mitigate page reference counting during page frag refill
We do a get_page() which involves a atomic operation. This patch tries to mitigate a per packet atomic operation by maintaining a reference bias which is initially USHRT_MAX. Each time a page is got, instead of calling get_page() we decrease the bias and when we find it's time to use a new page we will decrease the bias at one time through __page_cache_drain_cache(). Testpmd(virtio_user + vhost_net) + XDP_DROP on TAP shows about 1.6% improvement. Before: 4.63Mpps After: 4.71Mpps Signed-off-by: Jason Wang <jasowang@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers/vhost')
-rw-r--r--drivers/vhost/net.c54
1 files changed, 51 insertions, 3 deletions
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index ab11b2bee273..d919284f103b 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -141,6 +141,10 @@ struct vhost_net {
141 unsigned tx_zcopy_err; 141 unsigned tx_zcopy_err;
142 /* Flush in progress. Protected by tx vq lock. */ 142 /* Flush in progress. Protected by tx vq lock. */
143 bool tx_flush; 143 bool tx_flush;
144 /* Private page frag */
145 struct page_frag page_frag;
146 /* Refcount bias of page frag */
147 int refcnt_bias;
144}; 148};
145 149
146static unsigned vhost_net_zcopy_mask __read_mostly; 150static unsigned vhost_net_zcopy_mask __read_mostly;
@@ -637,14 +641,53 @@ static bool tx_can_batch(struct vhost_virtqueue *vq, size_t total_len)
637 !vhost_vq_avail_empty(vq->dev, vq); 641 !vhost_vq_avail_empty(vq->dev, vq);
638} 642}
639 643
644#define SKB_FRAG_PAGE_ORDER get_order(32768)
645
646static bool vhost_net_page_frag_refill(struct vhost_net *net, unsigned int sz,
647 struct page_frag *pfrag, gfp_t gfp)
648{
649 if (pfrag->page) {
650 if (pfrag->offset + sz <= pfrag->size)
651 return true;
652 __page_frag_cache_drain(pfrag->page, net->refcnt_bias);
653 }
654
655 pfrag->offset = 0;
656 net->refcnt_bias = 0;
657 if (SKB_FRAG_PAGE_ORDER) {
658 /* Avoid direct reclaim but allow kswapd to wake */
659 pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
660 __GFP_COMP | __GFP_NOWARN |
661 __GFP_NORETRY,
662 SKB_FRAG_PAGE_ORDER);
663 if (likely(pfrag->page)) {
664 pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
665 goto done;
666 }
667 }
668 pfrag->page = alloc_page(gfp);
669 if (likely(pfrag->page)) {
670 pfrag->size = PAGE_SIZE;
671 goto done;
672 }
673 return false;
674
675done:
676 net->refcnt_bias = USHRT_MAX;
677 page_ref_add(pfrag->page, USHRT_MAX - 1);
678 return true;
679}
680
640#define VHOST_NET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 681#define VHOST_NET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
641 682
642static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, 683static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
643 struct iov_iter *from) 684 struct iov_iter *from)
644{ 685{
645 struct vhost_virtqueue *vq = &nvq->vq; 686 struct vhost_virtqueue *vq = &nvq->vq;
687 struct vhost_net *net = container_of(vq->dev, struct vhost_net,
688 dev);
646 struct socket *sock = vq->private_data; 689 struct socket *sock = vq->private_data;
647 struct page_frag *alloc_frag = &current->task_frag; 690 struct page_frag *alloc_frag = &net->page_frag;
648 struct virtio_net_hdr *gso; 691 struct virtio_net_hdr *gso;
649 struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp]; 692 struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp];
650 struct tun_xdp_hdr *hdr; 693 struct tun_xdp_hdr *hdr;
@@ -665,7 +708,8 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
665 708
666 buflen += SKB_DATA_ALIGN(len + pad); 709 buflen += SKB_DATA_ALIGN(len + pad);
667 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES); 710 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
668 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) 711 if (unlikely(!vhost_net_page_frag_refill(net, buflen,
712 alloc_frag, GFP_KERNEL)))
669 return -ENOMEM; 713 return -ENOMEM;
670 714
671 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 715 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
@@ -703,7 +747,7 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
703 xdp->data_end = xdp->data + len; 747 xdp->data_end = xdp->data + len;
704 hdr->buflen = buflen; 748 hdr->buflen = buflen;
705 749
706 get_page(alloc_frag->page); 750 --net->refcnt_bias;
707 alloc_frag->offset += buflen; 751 alloc_frag->offset += buflen;
708 752
709 ++nvq->batched_xdp; 753 ++nvq->batched_xdp;
@@ -1292,6 +1336,8 @@ static int vhost_net_open(struct inode *inode, struct file *f)
1292 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev); 1336 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);
1293 1337
1294 f->private_data = n; 1338 f->private_data = n;
1339 n->page_frag.page = NULL;
1340 n->refcnt_bias = 0;
1295 1341
1296 return 0; 1342 return 0;
1297} 1343}
@@ -1366,6 +1412,8 @@ static int vhost_net_release(struct inode *inode, struct file *f)
1366 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue); 1412 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
1367 kfree(n->vqs[VHOST_NET_VQ_TX].xdp); 1413 kfree(n->vqs[VHOST_NET_VQ_TX].xdp);
1368 kfree(n->dev.vqs); 1414 kfree(n->dev.vqs);
1415 if (n->page_frag.page)
1416 __page_frag_cache_drain(n->page_frag.page, n->refcnt_bias);
1369 kvfree(n); 1417 kvfree(n);
1370 return 0; 1418 return 0;
1371} 1419}