rx busy polling support for virtio-net
Subsystems (4)
v2 cover rx busy polling support for virtio-net
Hi all:
This series introduces the support for rx busy polling support. This was useful for reduing the latency for a kvm guest. Patch 1-2 introduces helpers which is used for rx busy polling. Patch 3 implement the main function.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both busy_poll and busy_read are set to 50 in guest, 1 byte netperf tcp_rr shows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.
Changes from V1:
- split the patch info smaller ones
- add more details about test setup/configuration
Please review.
Jason Wang (3):
virtio-net: introduce helpers to enable and disable all NAPIs
virtio-net: introduce virtnet_receive()
virtio-net: rx busy polling support
drivers/net/virtio_net.c | 234 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 13 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/3 virtio-net: introduce helpers to enable and disable all NAPIs
This patch introduces helpers to disable and enable NAPI for all rx queues. This will be used by rx busy polling support.
--- drivers/net/virtio_net.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
drivers/net/virtio_net.c +24 -10
@@ -760,6 +760,22 @@ again:return received; } +static void virtnet_napi_enable_all (struct virtnet_info *vi) +{ +int i; + +for (i =0 ; i < vi->max_queue_pairs; i++) +virtnet_napi_enable (&vi->rq[i]); +} + +static void virtnet_napi_disable_all (struct virtnet_info *vi) +{ +int i; + +for (i =0 ; i < vi->max_queue_pairs; i++) +napi_disable (&vi->rq[i].napi); +} +static int virtnet_open (struct net_device *dev) {struct virtnet_info *vi =netdev_priv (dev);
@@ -770,9 +786,10 @@ static int virtnet_open(struct net_device *dev)/* Make sure we have some buffers: if oom use wq. */ if (!try_fill_recv (&vi->rq[i],GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 ); -virtnet_napi_enable (&vi->rq[i]); } +virtnet_napi_enable_all (vi); +return 0 ; }
@@ -1076,13 +1093,11 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)static int virtnet_close (struct net_device *dev) {struct virtnet_info *vi =netdev_priv (dev); -int i;/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill); -for (i =0 ; i < vi->max_queue_pairs; i++) -napi_disable (&vi->rq[i].napi); +virtnet_napi_disable_all (vi);return 0 ; }
@@ -1853,11 +1868,11 @@ static int virtnet_freeze(struct virtio_device *vdev)netif_device_detach (vi->dev);cancel_delayed_work_sync (&vi->refill); -if (netif_running (vi->dev)) -for (i =0 ; i < vi->max_queue_pairs; i++) { -napi_disable (&vi->rq[i].napi); +if (netif_running (vi->dev)) { +virtnet_napi_disable_all (vi); +for (i =0 ; i < vi->max_queue_pairs; i++)netif_napi_del (&vi->rq[i].napi); - } + }remove_vq_common (vi);
@@ -1880,8 +1895,7 @@ static int virtnet_restore(struct virtio_device *vdev)if (!try_fill_recv (&vi->rq[i],GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 ); -for (i =0 ; i < vi->max_queue_pairs; i++) -virtnet_napi_enable (&vi->rq[i]); +virtnet_napi_enable_all (vi); }netif_device_attach (vi->dev);
Signature · 1 line
1.8.3.1
PATCH v2 2/3 virtio-net: introduce virtnet_receive()
In reply to Jason WangMove common receive logic to a new helper virtnet_receive(). It will also be used by rx busy polling method.
--- drivers/net/virtio_net.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/net/virtio_net.c +15 -5
@@ -725,15 +725,12 @@ static void refill_work(struct work_struct *work) } } -static int virtnet_poll (struct napi_struct *napi,int budget) +static int virtnet_receive (struct receive_queue *rq,int budget) { -struct receive_queue *rq = - container_of(napi, struct receive_queue, napi);struct virtnet_info *vi = rq->vq->vdev->priv; +unsigned int len, received =0 ;void *buf; -unsigned int r, len, received =0 ; -again:while (received < budget && (buf =virtqueue_get_buf (rq->vq, &len)) !=NULL ) {receive_buf (rq, buf, len);
@@ -745,6 +742,18 @@ again:schedule_delayed_work (&vi->refill,0 ); } +return received; +} + +static int virtnet_poll (struct napi_struct *napi,int budget) +{ +struct receive_queue *rq = + container_of(napi, struct receive_queue, napi); +unsigned int r, received =0 ; + +again: + received +=virtnet_receive (rq, budget); +/* Out of packets? */ if (received < budget) { r =virtqueue_enable_cb_prepare (rq->vq);
@@ -753,6 +762,7 @@ again: napi_schedule_prep(napi)) {virtqueue_disable_cb (rq->vq);__napi_schedule (napi); + budget -= received; goto again; } }
Signature · 1 line
1.8.3.1
PATCH v2 3/3 virtio-net: rx busy polling support
In reply to Jason WangAdd basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both busy_poll and busy_read are set to 50 in guest, 1 byte netperf tcp_rr shows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.
--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +187 -3
@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );
@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state; +#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */ +#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI | VIRTNET_RQ_STATE_POLL) +#define VIRTNET_RQ_LOCKED (VIRTNET_RQ_OWNED | VIRTNET_RQ_STATE_DISABLED) +#define VIRTNET_RQ_STATE_NAPI_YIELD 8/* NAPI or refill yielded this RQ */ +#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */ }; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock); + rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a +* receive queue. +*/ +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +int rc = true; + +spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI ); + rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock); +return rc; +} + +/* returns true is someone tried to get the rq while napi or refill had it */ +static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +int rc = false; + +spin_lock (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_POLL | +VIRTNET_RQ_STATE_NAPI_YIELD )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock (&rq->lock); +return rc; +} + +/* called from virtnet_low_latency_recv() */ +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if ((rq->state &VIRTNET_RQ_LOCKED )) { + rq->state |=VIRTNET_RQ_STATE_POLL_YIELD ; + rc = false; + }else +/* preserve yield marks */ + rq->state |=VIRTNET_RQ_STATE_POLL ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* returns true if someone tried to get the receive queue while it was locked */ +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +int rc = false; + +spin_lock_bh (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_NAPI )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* return false if RQ is currently owned */ +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if (rq->state &VIRTNET_RQ_OWNED ) + rc = false; + rq->state |=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); + +return rc; +} + +#else /* CONFIG_NET_RX_BUSY_POLL */ +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ +} + +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +return true; +} + +static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +return true; +} + +#endif /* CONFIG_NET_RX_BUSY_POLL */ +struct virtnet_info {struct virtio_device *vdev;struct virtqueue *cvq;
@@ -521,6 +657,8 @@ static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)skb_shinfo (skb)->gso_segs =0 ; } +skb_mark_napi_id (skb, &rq->napi); +netif_receive_skb (skb);return ;
@@ -714,7 +852,12 @@ static void refill_work(struct work_struct *work)struct receive_queue *rq = &vi->rq[i];napi_disable (&rq->napi); +if (!virtnet_rq_lock_napi_refill (rq)) { +virtnet_napi_enable (rq); +continue ; + } still_empty = !try_fill_recv (rq,GFP_KERNEL ); +virtnet_rq_unlock_napi_refill (rq);virtnet_napi_enable (rq);/* In theory, this can happen: if we don't get any buffers in
@@ -752,8 +895,13 @@ static int virtnet_poll(struct napi_struct *napi, int budget)unsigned int r, received =0 ; again: +if (!virtnet_rq_lock_napi_refill (rq)) +return budget; + received +=virtnet_receive (rq, budget); +virtnet_rq_unlock_napi_refill (rq); +/* Out of packets? */ if (received < budget) { r =virtqueue_enable_cb_prepare (rq->vq);
@@ -770,20 +918,50 @@ again:return received; } +#ifdef CONFIG_NET_RX_BUSY_POLL +/* must be called with local_bh_disable()d */ +static int virtnet_low_latency_recv (struct napi_struct *napi) +{ +struct receive_queue *rq = + container_of(napi, struct receive_queue, napi); +struct virtnet_info *vi = rq->vq->vdev->priv; +int received; + +if (!(vi->status &VIRTIO_NET_S_LINK_UP )) +return LL_FLUSH_FAILED ; + +if (!virtnet_rq_lock_poll (rq)) +return LL_FLUSH_BUSY ; + + received =virtnet_receive (rq,4 ); + +virtnet_rq_unlock_poll (rq); + +return received; +} +#endif /* CONFIG_NET_RX_BUSY_POLL */ +static void virtnet_napi_enable_all (struct virtnet_info *vi) {int i; -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +virtnet_rq_init_lock (&vi->rq[i]);virtnet_napi_enable (&vi->rq[i]); + } }static void virtnet_napi_disable_all (struct virtnet_info *vi) {int i; -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) {napi_disable (&vi->rq[i].napi); +while (!virtnet_rq_disable (&vi->rq[i])) { +pr_info ("RQ %d locked\n" , i); +usleep_range (1000 ,20000 ); + } + } }static int virtnet_open (struct net_device *dev)
@@ -1372,6 +1550,9 @@ static const struct net_device_ops virtnet_netdev = {#ifdef CONFIG_NET_POLL_CONTROLLER .ndo_poll_controller = virtnet_netpoll,#endif +#ifdef CONFIG_NET_RX_BUSY_POLL + .ndo_busy_poll = virtnet_low_latency_recv, +#endif };static void virtnet_config_changed_work (struct work_struct *work)
@@ -1577,6 +1758,7 @@ static int virtnet_alloc_queues(struct virtnet_info *vi) vi->rq[i].pages =NULL ;netif_napi_add (vi->dev, &vi->rq[i].napi, virtnet_poll, napi_weight); +napi_hash_add (&vi->rq[i].napi);sg_init_table (vi->rq[i].sg,ARRAY_SIZE (vi->rq[i].sg));ewma_init (&vi->rq[i].mrg_avg_pkt_len,1 ,RECEIVE_AVG_WEIGHT );
@@ -1880,8 +2062,10 @@ static int virtnet_freeze(struct virtio_device *vdev)if (netif_running (vi->dev)) {virtnet_napi_disable_all (vi); -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +napi_hash_del (&vi->rq[i].napi);netif_napi_del (&vi->rq[i].napi); + } }remove_vq_common (vi);
Signature · 1 line
1.8.3.1
Re: [PATCH net-next V2 3/3] virtio-net: rx busy polling support
Quoted 52 lines · Jason Wang
Add basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.Cc Rusty Russell <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Vlad Yasevich <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)drivers/net/virtio_net.c +19
-0@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state; +#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */ +#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI | VIRTNET_RQ_STATE_POLL) +#define VIRTNET_RQ_LOCKED (VIRTNET_RQ_OWNED | VIRTNET_RQ_STATE_DISABLED) +#define VIRTNET_RQ_STATE_NAPI_YIELD 8/* NAPI or refill yielded this RQ */ +#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */ }; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock);
drivers/net/virtio_net.c struct receive_queue { · line 127
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+ rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a +* receive queue. +*/ +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +int rc = true; +
bool instead of int...?
drivers/net/virtio_net.c struct receive_queue { · line 136
Quoted 9 lines
drivers/net/virtio_net.c +9
-0+spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI ); + rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock);
Lock for rq->state ...?
If yes:spin_lock(&rq->lock);
if (rq->state & VIRTNET_RQ_LOCKED) {
rq->state |= VIRTNET_RQ_STATE_NAPI_YIELD;
spin_unlock(&rq->lock);
WARN_ON(rq->state & VIRTNET_RQ_STATE_NAPI);
rc = false;
} else {
/* we don't care if someone yielded */
rq->state = VIRTNET_RQ_STATE_NAPI;
spin_unlock(&rq->lock);
}Quoted 220 lines
drivers/net/virtio_net.c +149
-3+ return rc; +} + +/* returns true is someone tried to get the rq while napi or refill had it */ +static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +int rc = false; + +spin_lock (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_POLL | +VIRTNET_RQ_STATE_NAPI_YIELD )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock (&rq->lock); +return rc; +} + +/* called from virtnet_low_latency_recv() */ +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if ((rq->state &VIRTNET_RQ_LOCKED )) { + rq->state |=VIRTNET_RQ_STATE_POLL_YIELD ; + rc = false; + }else +/* preserve yield marks */ + rq->state |=VIRTNET_RQ_STATE_POLL ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* returns true if someone tried to get the receive queue while it was locked */ +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +int rc = false; + +spin_lock_bh (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_NAPI )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* return false if RQ is currently owned */ +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if (rq->state &VIRTNET_RQ_OWNED ) + rc = false; + rq->state |=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); + +return rc; +} + +#else /* CONFIG_NET_RX_BUSY_POLL */ +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ +} + +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +return true; +} + +static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +return true; +} + +#endif /* CONFIG_NET_RX_BUSY_POLL */ +struct virtnet_info {struct virtio_device *vdev;struct virtqueue *cvq;@@ -521,6 +657,8 @@ static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)skb_shinfo (skb)->gso_segs =0 ; } +skb_mark_napi_id (skb, &rq->napi); +netif_receive_skb (skb);return ;@@ -714,7 +852,12 @@ static void refill_work(struct work_struct *work)struct receive_queue *rq = &vi->rq[i];napi_disable (&rq->napi); +if (!virtnet_rq_lock_napi_refill (rq)) { +virtnet_napi_enable (rq); +continue ; + } still_empty = !try_fill_recv (rq,GFP_KERNEL ); +virtnet_rq_unlock_napi_refill (rq);virtnet_napi_enable (rq);/* In theory, this can happen: if we don't get any buffers in @@ -752,8 +895,13 @@ static int virtnet_poll(struct napi_struct *napi, int budget)unsigned int r, received =0 ; again: +if (!virtnet_rq_lock_napi_refill (rq)) +return budget; + received +=virtnet_receive (rq, budget); +virtnet_rq_unlock_napi_refill (rq); +/* Out of packets? */ if (received < budget) { r =virtqueue_enable_cb_prepare (rq->vq);@@ -770,20 +918,50 @@ again:return received; } +#ifdef CONFIG_NET_RX_BUSY_POLL +/* must be called with local_bh_disable()d */ +static int virtnet_low_latency_recv (struct napi_struct *napi) +{ +struct receive_queue *rq = + container_of(napi, struct receive_queue, napi); +struct virtnet_info *vi = rq->vq->vdev->priv; +int received; + +if (!(vi->status &VIRTIO_NET_S_LINK_UP )) +return LL_FLUSH_FAILED ; + +if (!virtnet_rq_lock_poll (rq)) +return LL_FLUSH_BUSY ; + + received =virtnet_receive (rq,4 ); + +virtnet_rq_unlock_poll (rq); + +return received; +} +#endif /* CONFIG_NET_RX_BUSY_POLL */ +static void virtnet_napi_enable_all (struct virtnet_info *vi) {int i; -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +virtnet_rq_init_lock (&vi->rq[i]);virtnet_napi_enable (&vi->rq[i]); + } }static void virtnet_napi_disable_all (struct virtnet_info *vi) {int i; -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) {napi_disable (&vi->rq[i].napi); +while (!virtnet_rq_disable (&vi->rq[i])) { +pr_info ("RQ %d locked\n" , i); +usleep_range (1000 ,20000 ); + } + } }static int virtnet_open (struct net_device *dev)@@ -1372,6 +1550,9 @@ static const struct net_device_ops virtnet_netdev = {#ifdef CONFIG_NET_POLL_CONTROLLER .ndo_poll_controller = virtnet_netpoll,#endif +#ifdef CONFIG_NET_RX_BUSY_POLL + .ndo_busy_poll = virtnet_low_latency_recv, +#endif };static void virtnet_config_changed_work (struct work_struct *work)@@ -1577,6 +1758,7 @@ static int virtnet_alloc_queues(struct virtnet_info *vi) vi->rq[i].pages =NULL ;netif_napi_add (vi->dev, &vi->rq[i].napi, virtnet_poll, napi_weight); +napi_hash_add (&vi->rq[i].napi);sg_init_table (vi->rq[i].sg,ARRAY_SIZE (vi->rq[i].sg));ewma_init (&vi->rq[i].mrg_avg_pkt_len,1 ,RECEIVE_AVG_WEIGHT );@@ -1880,8 +2062,10 @@ static int virtnet_freeze(struct virtio_device *vdev)if (netif_running (vi->dev)) {virtnet_napi_disable_all (vi); -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +napi_hash_del (&vi->rq[i].napi);netif_napi_del (&vi->rq[i].napi); + } }remove_vq_common (vi);
Signature · 2 lines
Regards, Varka Bhadram.
Re: [PATCH net-next V2 3/3] virtio-net: rx busy polling support
Quoted 61 lines · Varka Bhadram
Quoted 60 lines · Jason Wang
Add basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.Cc Rusty Russell <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Vlad Yasevich <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)drivers/net/virtio_net.c +6
-0@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state; +#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */
drivers/net/virtio_net.c +3
-0+#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI |
VIRTNET_RQ_STATE_POLL)
+#defineVIRTNET_RQ_LOCKED(VIRTNET_RQ_OWNED|VIRTNET_RQ_STATE_DISABLED)
+#defineVIRTNET_RQ_STATE_NAPI_YIELD8 /* NAPI or refill yielded
this RQ */drivers/net/virtio_net.c +11
-0+#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */ }; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock); + rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a
+ * receive queue.
Quoted 8 lines
drivers/net/virtio_net.c struct receive_queue { · line 127
Quoted 6 lines
+ */
+static inline boolvirtnet_rq_lock_napi_refill(struct receive_queue *rq)drivers/net/virtio_net.c +3
-0+{ +int rc = true; +bool instead of int...?
Yes, it was better.
Quoted 15 lines
drivers/net/virtio_net.c struct receive_queue { · line 136
Quoted 10 lines
drivers/net/virtio_net.c +9
-0+spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI ); + rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock);Lock for
rq->state...?If yes:
spin_lock(&rq->lock);
Quoted 10 lines
if (rq->state & VIRTNET_RQ_LOCKED) {
rq->state |= VIRTNET_RQ_STATE_NAPI_YIELD;
spin_unlock(&rq->lock);
WARN_ON(rq->state & VIRTNET_RQ_STATE_NAPI);
rc = false;
} else {
/* we don't care if someone yielded */
rq->state = VIRTNET_RQ_STATE_NAPI;
spin_unlock(&rq->lock);
} I didn't see any differences. Is this used to catch the bug of driver earlier? btw, several other rx busy polling capable driver does the same thing.
Re: [PATCH net-next V2 3/3] virtio-net: rx busy polling support
Quoted 93 lines · Jason Wang
Quoted 60 lines · Varka Bhadram
Quoted 59 lines · Jason Wang
Add basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.Cc Rusty Russell <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Vlad Yasevich <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)drivers/net/virtio_net.c +6
-0@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state; +#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */
drivers/net/virtio_net.c +3
-0+#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI |
VIRTNET_RQ_STATE_POLL)
+#defineVIRTNET_RQ_LOCKED(VIRTNET_RQ_OWNED|VIRTNET_RQ_STATE_DISABLED)
+#defineVIRTNET_RQ_STATE_NAPI_YIELD8 /* NAPI or refill yielded
this RQ */drivers/net/virtio_net.c +11
-0+#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */ }; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock); + rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a
Quoted 8 lines
drivers/net/virtio_net.c struct receive_queue { · line 127
Quoted 7 lines
+ * receive queue.
+ */
+static inline boolvirtnet_rq_lock_napi_refill(struct receive_queue *rq)drivers/net/virtio_net.c +3
-0+{ +int rc = true; +bool instead of int...?
Yes, it was better.
Quoted 13 lines
drivers/net/virtio_net.c struct receive_queue { · line 136
Quoted 9 lines
drivers/net/virtio_net.c +9
-0+spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI ); + rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock);Lock for
rq->state...?If yes:
spin_lock(&rq->lock);Quoted 10 lines
if (rq->state & VIRTNET_RQ_LOCKED) { rq->state |= VIRTNET_RQ_STATE_NAPI_YIELD; spin_unlock(&rq->lock); WARN_ON(rq->state & VIRTNET_RQ_STATE_NAPI); rc = false; } else { /* we don't care if someone yielded */ rq->state = VIRTNET_RQ_STATE_NAPI; spin_unlock(&rq->lock); }
Quoted 3 lines
I didn't see any differences. Is this used to catch the bug of driver earlier? btw, several other rx busy polling capable driver does the same thing.
We need not to include WARN_ON() & rc=false under critical section.
Signature · 2 lines
Regards, Varka Bhadram
Re: [PATCH net-next V2 3/3] virtio-net: rx busy polling support
Quoted 95 lines · Varka Bhadram
Quoted 93 lines · Jason Wang
Quoted 60 lines · Varka Bhadram
Quoted 59 lines · Jason Wang
Add basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.Cc Rusty Russell <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Vlad Yasevich <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)drivers/net/virtio_net.c +6
-0@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state; +#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */
drivers/net/virtio_net.c +3
-0+#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI |
VIRTNET_RQ_STATE_POLL)
+#defineVIRTNET_RQ_LOCKED(VIRTNET_RQ_OWNED|VIRTNET_RQ_STATE_DISABLED)
+#defineVIRTNET_RQ_STATE_NAPI_YIELD8 /* NAPI or refill yielded
this RQ */drivers/net/virtio_net.c +11
-0+#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */ }; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock); + rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a
Quoted 8 lines
drivers/net/virtio_net.c struct receive_queue { · line 127
Quoted 7 lines
+ * receive queue.
+ */
+static inline boolvirtnet_rq_lock_napi_refill(struct receive_queue *rq)drivers/net/virtio_net.c +3
-0+{ +int rc = true; +bool instead of int...?
Yes, it was better.
Quoted 23 lines
drivers/net/virtio_net.c struct receive_queue { · line 136
Quoted 9 lines
drivers/net/virtio_net.c +9
-0+spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI ); + rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock);Lock for
rq->state...?If yes:
spin_lock(&rq->lock);if (rq->state & VIRTNET_RQ_LOCKED) { rq->state |= VIRTNET_RQ_STATE_NAPI_YIELD; spin_unlock(&rq->lock); WARN_ON(rq->state & VIRTNET_RQ_STATE_NAPI); rc = false; } else { /* we don't care if someone yielded */ rq->state = VIRTNET_RQ_STATE_NAPI; spin_unlock(&rq->lock); }
Quoted 6 lines
Quoted 3 lines
I didn't see any differences. Is this used to catch the bug of driver earlier? btw, several other rx busy polling capable driver does the same thing.
We need not to include
WARN_ON()& rc=false under critical section.
Ok. but unless there's a bug in the driver itself, WARN_ON() should be just a condition check for a branch, so there should not be noticeable differences.
Also we should not check rq->state outside the protection of lock.
Re: [PATCH net-next V2 3/3] virtio-net: rx busy polling support
Quoted 98 lines · Jason Wang
Quoted 97 lines · Varka Bhadram
Quoted 96 lines · Jason Wang
Quoted 60 lines · Varka Bhadram
Quoted 59 lines · Jason Wang
Add basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.Cc Rusty Russell <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Vlad Yasevich <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)drivers/net/virtio_net.c +6
-0@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state; +#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */
drivers/net/virtio_net.c +3
-0+#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI |
VIRTNET_RQ_STATE_POLL)
+#defineVIRTNET_RQ_LOCKED(VIRTNET_RQ_OWNED|VIRTNET_RQ_STATE_DISABLED)
+#defineVIRTNET_RQ_STATE_NAPI_YIELD8 /* NAPI or refill yielded
this RQ */drivers/net/virtio_net.c +11
-0+#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */ }; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock); + rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a
Quoted 8 lines
drivers/net/virtio_net.c struct receive_queue { · line 127
Quoted 7 lines
+ * receive queue.
+ */
+static inline boolvirtnet_rq_lock_napi_refill(struct receive_queue *rq)drivers/net/virtio_net.c +3
-0+{ +int rc = true; +bool instead of int...?
Yes, it was better.
Quoted 13 lines
drivers/net/virtio_net.c struct receive_queue { · line 136
Quoted 9 lines
drivers/net/virtio_net.c +9
-0+spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI ); + rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock);Lock for
rq->state...?If yes:
spin_lock(&rq->lock);Quoted 10 lines
if (rq->state & VIRTNET_RQ_LOCKED) { rq->state |= VIRTNET_RQ_STATE_NAPI_YIELD; spin_unlock(&rq->lock); WARN_ON(rq->state & VIRTNET_RQ_STATE_NAPI); rc = false; } else { /* we don't care if someone yielded */ rq->state = VIRTNET_RQ_STATE_NAPI; spin_unlock(&rq->lock); }I didn't see any differences. Is this used to catch the bug of driver earlier? btw, several other rx busy polling capable driver does the same thing.
Quoted 7 lines
Quoted 2 lines
We need not to include
WARN_ON()& rc=false under critical section.Ok. but unless there's a bug in the driver itself,
WARN_ON()should be just a condition check for a branch, so there should not be noticeable differences.Also we should not check
rq->stateoutside the protection of lock.
Ok. I will agree with you. But 'rc' can be outside the protection of lock
Signature · 2 lines
Regards, Varka Bhadram
David Miller
maintainerFrom: Jason Wang <[email protected]>
Date: Wed, 16 Jul 2014 14:21:44 +0800
Quoted 12 lines
Hi all:
This series introduces the support for rx busy polling support. This was useful for reduing the latency for a kvm guest. Patch 1-2 introduces helpers which is used for rx busy polling. Patch 3 implement the main function.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.
Quoted 5 lines
Changes from V1:
- split the patch info smaller ones
- add more details about test setup/configurationPlease review.
Looks like you can make some minor adjustments based upon review, and anyways I'd like to wait for Michael to get back and review a change of this nature.
Thanks.
Quoted 19 lines · David Miller
From: Jason Wang <[email protected]>
Date: Wed, 16 Jul 2014 14:21:44 +0800Quoted 16 lines
Hi all:
This series introduces the support for rx busy polling support. This was useful for reduing the latency for a kvm guest. Patch 1-2 introduces helpers which is used for rx busy polling. Patch 3 implement the main function.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.Changes from V1:
- split the patch info smaller ones
- add more details about test setup/configuration
Quoted 6 lines
Quoted 1 line
Please review.
Looks like you can make some minor adjustments based upon review, and anyways I'd like to wait for Michael to get back and review a change of this nature.
Thanks.
Ok, I will wait for Michael's comments and then post V3.
Thanks
Michael S. Tsirkin
maintainerRe: [PATCH net-next V2 3/3] virtio-net: rx busy polling support
In reply to Jason WangQuoted 6 lines · Jason Wang
Add basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.
Pls include data about non polling tests: any effect on cpu utilization there?
There could be as we are adding locking.
Quoted 29 lines
Cc Rusty Russell <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Vlad Yasevich <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)drivers/net/virtio_net.c +4
-0@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state;
drivers/net/virtio_net.c struct receive_queue { · line 110
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */ +#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI | VIRTNET_RQ_STATE_POLL) +#define VIRTNET_RQ_LOCKED (VIRTNET_RQ_OWNED | VIRTNET_RQ_STATE_DISABLED) +#define VIRTNET_RQ_STATE_NAPI_YIELD 8/* NAPI or refill yielded this RQ */ +#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */
do we have to have a new state? no way to reuse the napi state for this? two lock/unlock operations for a poll seems excessive.
Quoted 20 lines
drivers/net/virtio_net.c +18
-0}; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock); + rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a +* receive queue. +*/ +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +int rc = true; + +spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI );
drivers/net/virtio_net.c struct receive_queue { · line 140
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+ rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock); +return rc; +} + +/* returns true is someone tried to get the rq while napi or refill had it */
s/is/if/
Quoted 134 lines
drivers/net/virtio_net.c +106
-0+static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +int rc = false; + +spin_lock (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_POLL | +VIRTNET_RQ_STATE_NAPI_YIELD )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock (&rq->lock); +return rc; +} + +/* called from virtnet_low_latency_recv() */ +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if ((rq->state &VIRTNET_RQ_LOCKED )) { + rq->state |=VIRTNET_RQ_STATE_POLL_YIELD ; + rc = false; + }else +/* preserve yield marks */ + rq->state |=VIRTNET_RQ_STATE_POLL ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* returns true if someone tried to get the receive queue while it was locked */ +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +int rc = false; + +spin_lock_bh (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_NAPI )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* return false if RQ is currently owned */ +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if (rq->state &VIRTNET_RQ_OWNED ) + rc = false; + rq->state |=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); + +return rc; +} + +#else /* CONFIG_NET_RX_BUSY_POLL */ +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ +} + +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +return true; +} + +static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +return true; +} + +#endif /* CONFIG_NET_RX_BUSY_POLL */ +struct virtnet_info {struct virtio_device *vdev;struct virtqueue *cvq;@@ -521,6 +657,8 @@ static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)skb_shinfo (skb)->gso_segs =0 ; } +skb_mark_napi_id (skb, &rq->napi); +netif_receive_skb (skb);return ;@@ -714,7 +852,12 @@ static void refill_work(struct work_struct *work)struct receive_queue *rq = &vi->rq[i];napi_disable (&rq->napi); +if (!virtnet_rq_lock_napi_refill (rq)) { +virtnet_napi_enable (rq); +continue ; + } still_empty = !try_fill_recv (rq,GFP_KERNEL ); +virtnet_rq_unlock_napi_refill (rq);virtnet_napi_enable (rq);/* In theory, this can happen: if we don't get any buffers in @@ -752,8 +895,13 @@ static int virtnet_poll(struct napi_struct *napi, int budget)unsigned int r, received =0 ; again: +if (!virtnet_rq_lock_napi_refill (rq)) +return budget; + received +=virtnet_receive (rq, budget); +virtnet_rq_unlock_napi_refill (rq); +/* Out of packets? */ if (received < budget) { r =virtqueue_enable_cb_prepare (rq->vq);
drivers/net/virtio_net.c again: · line 923
Quoted 6 lines
drivers/net/virtio_net.c +3
-0@@ -770,20 +918,50 @@ again:return received; } +#ifdef CONFIG_NET_RX_BUSY_POLL +/* must be called with local_bh_disable()d */ +static int virtnet_low_latency_recv (struct napi_struct *napi)
let's call it busy poll :)
drivers/net/virtio_net.c again: · line 936
Quoted 13 lines
drivers/net/virtio_net.c +13
-0+{ +struct receive_queue *rq = + container_of(napi, struct receive_queue, napi); +struct virtnet_info *vi = rq->vq->vdev->priv; +int received; + +if (!(vi->status &VIRTIO_NET_S_LINK_UP )) +return LL_FLUSH_FAILED ; + +if (!virtnet_rq_lock_poll (rq)) +return LL_FLUSH_BUSY ; + + received =virtnet_receive (rq,4 );
Hmm why 4 exactly?
Quoted 18 lines
drivers/net/virtio_net.c +10
-1+ +virtnet_rq_unlock_poll (rq); + + return received; +} +#endif /* CONFIG_NET_RX_BUSY_POLL */ +static void virtnet_napi_enable_all (struct virtnet_info *vi) {int i; -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +virtnet_rq_init_lock (&vi->rq[i]);virtnet_napi_enable (&vi->rq[i]); + } }
drivers/net/virtio_net.c again: · line 962
Quoted 10 lines
drivers/net/virtio_net.c +4
-1static void virtnet_napi_disable_all (struct virtnet_info *vi) {int i; -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) {napi_disable (&vi->rq[i].napi); +while (!virtnet_rq_disable (&vi->rq[i])) { +pr_info ("RQ %d locked\n" , i); +usleep_range (1000 ,20000 );
What's going on here, exactly?
Quoted 37 lines
drivers/net/virtio_net.c +9
-1+ } + } }static int virtnet_open (struct net_device *dev)@@ -1372,6 +1550,9 @@ static const struct net_device_ops virtnet_netdev = {#ifdef CONFIG_NET_POLL_CONTROLLER .ndo_poll_controller = virtnet_netpoll,#endif +#ifdef CONFIG_NET_RX_BUSY_POLL + .ndo_busy_poll = virtnet_low_latency_recv, +#endif };static void virtnet_config_changed_work (struct work_struct *work)@@ -1577,6 +1758,7 @@ static int virtnet_alloc_queues(struct virtnet_info *vi) vi->rq[i].pages =NULL ;netif_napi_add (vi->dev, &vi->rq[i].napi, virtnet_poll, napi_weight); +napi_hash_add (&vi->rq[i].napi);sg_init_table (vi->rq[i].sg,ARRAY_SIZE (vi->rq[i].sg));ewma_init (&vi->rq[i].mrg_avg_pkt_len,1 ,RECEIVE_AVG_WEIGHT );@@ -1880,8 +2062,10 @@ static int virtnet_freeze(struct virtio_device *vdev)if (netif_running (vi->dev)) {virtnet_napi_disable_all (vi); -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +napi_hash_del (&vi->rq[i].napi);netif_napi_del (&vi->rq[i].napi); + } }remove_vq_common (vi);Signature · 1 line
1.8.3.1
Michael S. Tsirkin
maintainerQuoted 12 lines · Jason Wang
Hi all:
This series introduces the support for rx busy polling support. This was useful for reduing the latency for a kvm guest. Patch 1-2 introduces helpers which is used for rx busy polling. Patch 3 implement the main function.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.
Quoted 5 lines
Changes from V1:
- split the patch info smaller ones
- add more details about test setup/configurationPlease review.
Generally I think we should let host know we are polling. For example, kick the rq or something. Or maybe add another io address.
Something like this would need a new feature flag though, so I'm fine with just polling in guest until that is available.
Quoted 10 lines
Jason Wang (3):
virtio-net: introduce helpers to enable and disable all NAPIs
virtio-net: introducevirtnet_receive()
virtio-net: rx busy polling supportdrivers/net/virtio_net.c | 234 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 13 deletions(-)Signature · 1 line
1.8.3.1
Re: [PATCH net-next V2 3/3] virtio-net: rx busy polling support
In reply to Michael S. TsirkinQuoted 10 lines · Michael S. Tsirkin
Quoted 6 lines · Jason Wang
Add basic support for rx busy polling.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.Pls include data about non polling tests: any effect on cpu utilization there?
There could be as we are adding locking.
I will do some test on this.
Quoted 34 lines
Quoted 34 lines
Cc Rusty Russell <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Vlad Yasevich <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 3 deletions(-)drivers/net/virtio_net.c +9
-0@@ -27,6 +27,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/average.h> +#include <net/busy_poll.h> static int napi_weight =NAPI_POLL_WEIGHT ;module_param (napi_weight, int,0444 );@@ -94,8 +95,143 @@ struct receive_queue {/* Name of this receive queue: input.$index */ char name[40 ]; + +#ifdef CONFIG_NET_RX_BUSY_POLL +unsigned int state; +#define VIRTNET_RQ_STATE_IDLE 0 +#define VIRTNET_RQ_STATE_NAPI 1/* NAPI or refill owns this RQ */ +#define VIRTNET_RQ_STATE_POLL 2/* poll owns this RQ */ +#define VIRTNET_RQ_STATE_DISABLED 4/* RQ is disabled */ +#define VIRTNET_RQ_OWNED (VIRTNET_RQ_STATE_NAPI | VIRTNET_RQ_STATE_POLL)
Quoted 8 lines
drivers/net/virtio_net.c struct receive_queue { · line 110
Quoted 5 lines
drivers/net/virtio_net.c +5
-0+#define VIRTNET_RQ_LOCKED (VIRTNET_RQ_OWNED | VIRTNET_RQ_STATE_DISABLED) +#define VIRTNET_RQ_STATE_NAPI_YIELD 8/* NAPI or refill yielded this RQ */ +#define VIRTNET_RQ_STATE_POLL_YIELD 16/* poll yielded this RQ */ +spinlock_t lock; +#endif /* CONFIG_NET_RX_BUSY_POLL */ do we have to have a new state? no way to reuse the napi state for this? two lock/unlock operations for a poll seems excessive.
I try this way and it works. The only usage I can think of introducing those states is to detect the yield and do some optimizations after. But only few drivers (bnx2x) use the yield flag.
I think I can switch to use NAPI state since we don't do such optimization in virtio-net.
Quoted 170 lines
Quoted 21 lines
drivers/net/virtio_net.c +18
-0}; +#ifdef CONFIG_NET_RX_BUSY_POLL +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ + +spin_lock_init (&rq->lock); + rq->state =VIRTNET_RQ_STATE_IDLE ; +} + +/* called from the device poll routine or refill routine to get ownership of a +* receive queue. +*/ +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +int rc = true; + +spin_lock (&rq->lock); +if (rq->state &VIRTNET_RQ_LOCKED ) { +WARN_ON (rq->state &VIRTNET_RQ_STATE_NAPI );drivers/net/virtio_net.c struct receive_queue { · line 140
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+ rq->state |=VIRTNET_RQ_STATE_NAPI_YIELD ; + rc = false; + }else +/* we don't care if someone yielded */ + rq->state =VIRTNET_RQ_STATE_NAPI ; +spin_unlock (&rq->lock); +return rc; +} + +/* returns true is someone tried to get the rq while napi or refill had it */ s/is/if/
Quoted 137 lines
drivers/net/virtio_net.c +106
-0+static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +int rc = false; + +spin_lock (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_POLL | +VIRTNET_RQ_STATE_NAPI_YIELD )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock (&rq->lock); +return rc; +} + +/* called from virtnet_low_latency_recv() */ +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if ((rq->state &VIRTNET_RQ_LOCKED )) { + rq->state |=VIRTNET_RQ_STATE_POLL_YIELD ; + rc = false; + }else +/* preserve yield marks */ + rq->state |=VIRTNET_RQ_STATE_POLL ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* returns true if someone tried to get the receive queue while it was locked */ +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +int rc = false; + +spin_lock_bh (&rq->lock); +WARN_ON (rq->state & (VIRTNET_RQ_STATE_NAPI )); + +if (rq->state &VIRTNET_RQ_STATE_POLL_YIELD ) + rc = true; +/* will reset state to idle, unless RQ is disabled */ + rq->state &=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); +return rc; +} + +/* return false if RQ is currently owned */ +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +int rc = true; + +spin_lock_bh (&rq->lock); +if (rq->state &VIRTNET_RQ_OWNED ) + rc = false; + rq->state |=VIRTNET_RQ_STATE_DISABLED ; +spin_unlock_bh (&rq->lock); + +return rc; +} + +#else /* CONFIG_NET_RX_BUSY_POLL */ +static inline void virtnet_rq_init_lock (struct receive_queue *rq) +{ +} + +static inline bool virtnet_rq_lock_napi_refill (struct receive_queue *rq) +{ +return true; +} + +static inline bool virtnet_rq_unlock_napi_refill (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_lock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_unlock_poll (struct receive_queue *rq) +{ +return false; +} + +static inline bool virtnet_rq_disable (struct receive_queue *rq) +{ +return true; +} + +#endif /* CONFIG_NET_RX_BUSY_POLL */ +struct virtnet_info {struct virtio_device *vdev;struct virtqueue *cvq;@@ -521,6 +657,8 @@ static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)skb_shinfo (skb)->gso_segs =0 ; } +skb_mark_napi_id (skb, &rq->napi); +netif_receive_skb (skb);return ;@@ -714,7 +852,12 @@ static void refill_work(struct work_struct *work)struct receive_queue *rq = &vi->rq[i];napi_disable (&rq->napi); +if (!virtnet_rq_lock_napi_refill (rq)) { +virtnet_napi_enable (rq); +continue ; + } still_empty = !try_fill_recv (rq,GFP_KERNEL ); +virtnet_rq_unlock_napi_refill (rq);virtnet_napi_enable (rq);/* In theory, this can happen: if we don't get any buffers in @@ -752,8 +895,13 @@ static int virtnet_poll(struct napi_struct *napi, int budget)unsigned int r, received =0 ; again: +if (!virtnet_rq_lock_napi_refill (rq)) +return budget; + received +=virtnet_receive (rq, budget); +virtnet_rq_unlock_napi_refill (rq); +/* Out of packets? */ if (received < budget) { r =virtqueue_enable_cb_prepare (rq->vq);@@ -770,20 +918,50 @@ again:return received; }
Quoted 4 lines
drivers/net/virtio_net.c again: · line 923
Quoted 3 lines
drivers/net/virtio_net.c +3
-0+#ifdef CONFIG_NET_RX_BUSY_POLL +/* must be called with local_bh_disable()d */ +static int virtnet_low_latency_recv (struct napi_struct *napi)let's call it busy poll :)
Ok.
Quoted 6 lines
Quoted 6 lines
drivers/net/virtio_net.c +6
-0+{ +struct receive_queue *rq = + container_of(napi, struct receive_queue, napi); +struct virtnet_info *vi = rq->vq->vdev->priv; +int received; +
Quoted 8 lines
drivers/net/virtio_net.c again: · line 936
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+if (!(vi->status &VIRTIO_NET_S_LINK_UP )) +return LL_FLUSH_FAILED ; + +if (!virtnet_rq_lock_poll (rq)) +return LL_FLUSH_BUSY ; + + received =virtnet_receive (rq,4 );Hmm why 4 exactly?
I think the reason is we need a quota here to prevent the busy polling method from starving other threads. 4 is just copied form the existed implementation (ixgbe).
Quoted 22 lines
Quoted 22 lines
drivers/net/virtio_net.c +10
-1+ +virtnet_rq_unlock_poll (rq); + + return received; +} +#endif /* CONFIG_NET_RX_BUSY_POLL */ +static void virtnet_napi_enable_all (struct virtnet_info *vi) {int i; -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +virtnet_rq_init_lock (&vi->rq[i]);virtnet_napi_enable (&vi->rq[i]); + } }static void virtnet_napi_disable_all (struct virtnet_info *vi) {int i;
Quoted 7 lines
drivers/net/virtio_net.c again: · line 962
Quoted 6 lines
drivers/net/virtio_net.c +4
-1-for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) {napi_disable (&vi->rq[i].napi); +while (!virtnet_rq_disable (&vi->rq[i])) { +pr_info ("RQ %d locked\n" , i); +usleep_range (1000 ,20000 );What's going on here, exactly?
It was used to wait for the completion of busy polling to finish.
Quoted 37 lines
Quoted 37 lines
drivers/net/virtio_net.c +9
-1+ } + } }static int virtnet_open (struct net_device *dev)@@ -1372,6 +1550,9 @@ static const struct net_device_ops virtnet_netdev = {#ifdef CONFIG_NET_POLL_CONTROLLER .ndo_poll_controller = virtnet_netpoll,#endif +#ifdef CONFIG_NET_RX_BUSY_POLL + .ndo_busy_poll = virtnet_low_latency_recv, +#endif };static void virtnet_config_changed_work (struct work_struct *work)@@ -1577,6 +1758,7 @@ static int virtnet_alloc_queues(struct virtnet_info *vi) vi->rq[i].pages =NULL ;netif_napi_add (vi->dev, &vi->rq[i].napi, virtnet_poll, napi_weight); +napi_hash_add (&vi->rq[i].napi);sg_init_table (vi->rq[i].sg,ARRAY_SIZE (vi->rq[i].sg));ewma_init (&vi->rq[i].mrg_avg_pkt_len,1 ,RECEIVE_AVG_WEIGHT );@@ -1880,8 +2062,10 @@ static int virtnet_freeze(struct virtio_device *vdev)if (netif_running (vi->dev)) {virtnet_napi_disable_all (vi); -for (i =0 ; i < vi->max_queue_pairs; i++) +for (i =0 ; i < vi->max_queue_pairs; i++) { +napi_hash_del (&vi->rq[i].napi);netif_napi_del (&vi->rq[i].napi); + } }remove_vq_common (vi);Signature · 1 line
1.8.3.1
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Jason Wang
Hi all:
This series introduces the support for rx busy polling support. This was useful for reduing the latency for a kvm guest. Patch 1-2 introduces helpers which is used for rx busy polling. Patch 3 implement the main function.
Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both
busy_pollandbusy_readare set to 50 in guest, 1 byte netperftcp_rrshows 116% improvement:
transaction rate was increased from 9151.94 to 19787.37.
Quoted 8 lines
Quoted 5 lines
Changes from V1:
- split the patch info smaller ones
- add more details about test setup/configurationPlease review.
Generally I think we should let host know we are polling. For example, kick the rq or something. Or maybe add another io address.
Yes, I'm also working on busy polling for tun and vhost which may also help here.
Quoted 2 lines
Something like this would need a new feature flag though, so I'm fine with just polling in guest until that is available.
Yes.
Quoted 16 lines
Quoted 11 lines
Jason Wang (3):
virtio-net: introduce helpers to enable and disable all NAPIs
virtio-net: introducevirtnet_receive()
virtio-net: rx busy polling supportdrivers/net/virtio_net.c | 234 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 13 deletions(-)Signature · 1 line
1.8.3.1--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/