Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'virtio-net-tx-napi'

Willem de Bruijn says:

====================
virtio-net tx napi

Add napi for virtio-net transmit completion processing.

Changes:
v2 -> v3:
- convert __netif_tx_trylock to __netif_tx_lock on tx napi poll
ensure that the handler always cleans, to avoid deadlock
- unconditionally clean in start_xmit
avoid adding an unnecessary "if (use_napi)" branch
- remove virtqueue_disable_cb in patch 5/5
a noop in the common event_idx based loop
- document affinity_hint_set constraint

v1 -> v2:
- disable by default
- disable unless affinity_hint_set
because cache misses add up to a third higher cycle cost,
e.g., in TCP_RR tests. This is not limited to the patch
that enables tx completion cleaning in rx napi.
- use trylock to avoid contention between tx and rx napi
- keep interrupts masked during xmit_more (new patch 5/5)
this improves cycles especially for multi UDP_STREAM, which
does not benefit from cleaning tx completions on rx napi.
- move free_old_xmit_skbs (new patch 3/5)
to avoid forward declaration

not changed:
- deduplicate virnet_poll_tx and virtnet_poll_txclean
they look similar, but have differ too much to make it
worthwhile.
- delay netif_wake_subqueue for more than 2 + MAX_SKB_FRAGS
evaluated, but made no difference
- patch 1/5

RFC -> v1:
- dropped vhost interrupt moderation patch:
not needed and likely expensive at light load
- remove tx napi weight
- always clean all tx completions
- use boolean to toggle tx-napi, instead
- only clean tx in rx if tx-napi is enabled
- then clean tx before rx
- fix: add missing braces in virtnet_freeze_down
- testing: add 4KB TCP_RR + UDP test results

Based on previous patchsets by Jason Wang:

[RFC V7 PATCH 0/7] enable tx interrupts for virtio-net
http://lkml.iu.edu/hypermail/linux/kernel/1505.3/00245.html

Before commit b0c39dbdc204 ("virtio_net: don't free buffers in xmit
ring") the virtio-net driver would free transmitted packets on
transmission of new packets in ndo_start_xmit and, to catch the edge
case when no new packet is sent, also in a timer at 10HZ.

A timer can cause long stalls. VIRTIO_F_NOTIFY_ON_EMPTY avoids stalls
due to low free descriptor count. It does not address a stalls due to
low socket SO_SNDBUF. Increasing timer frequency decreases that stall
time, but increases interrupt rate and, thus, cycle count.

Currently, with no timer, packets are freed only at ndo_start_xmit.
Latency of consume_skb is now unbounded. To avoid a deadlock if a sock
reaches SO_SNDBUF, packets are orphaned on tx. This breaks TCP small
queues.

Reenable TCP small queues by removing the orphan. Instead of using a
timer, convert the driver to regular tx napi. This does not have the
unresolved stall issue and does not have any frequency to tune.

By keeping interrupts enabled by default, napi increases tx
interrupt rate. VIRTIO_F_EVENT_IDX avoids sending an interrupt if
one is already unacknowledged, so makes this more feasible today.
Combine that with an optimization that brings interrupt rate
back in line with the existing version for most workloads:

Tx completion cleaning on rx interrupts elides most explicit tx
interrupts by relying on the fact that many rx interrupts fire.

Tested by running {1, 10, 100} {TCP, UDP} STREAM, RR, 4K_RR benchmarks
from a guest to a server on the host, on an x86_64 Haswell. The guest
runs 4 vCPUs pinned to 4 cores. vhost and the test server are
pinned to a core each.

All results are the median of 5 runs, with variance well < 10%.
Used neper (github.com/google/neper) as test process.

Napi increases single stream throughput, but increases cycle cost.
The optimizations bring this down. The previous patchset saw a
regression with UDP_STREAM, which does not benefit from cleaning tx
interrupts in rx napi. This regression is now gone for 10x, 100x.
Remaining difference is higher 1x TCP_STREAM, lower 1x UDP_STREAM.

The latest results are with process, rx napi and tx napi affine to
the same core. All numbers are lower than the previous patchset.

upstream napi
TCP_STREAM:
1x:
Mbps 27816 39805
Gcycles 274 285

10x:
Mbps 42947 42531
Gcycles 300 296

100x:
Mbps 31830 28042
Gcycles 279 269

TCP_RR Latency (us):
1x:
p50 21 21
p99 27 27
Gcycles 180 167

10x:
p50 40 39
p99 52 52
Gcycles 214 211

100x:
p50 281 241
p99 411 337
Gcycles 218 226

TCP_RR 4K:
1x:
p50 28 29
p99 34 36
Gcycles 177 167

10x:
p50 70 71
p99 85 134
Gcycles 213 214

100x:
p50 442 611
p99 802 785
Gcycles 237 216

UDP_STREAM:
1x:
Mbps 29468 26800
Gcycles 284 293

10x:
Mbps 29891 29978
Gcycles 285 312

100x:
Mbps 30269 30304
Gcycles 318 316

UDP_RR:
1x:
p50 19 19
p99 23 23
Gcycles 180 173

10x:
p50 35 40
p99 54 64
Gcycles 245 237

100x:
p50 234 286
p99 484 473
Gcycles 224 214

Note that GSO is enabled, so 4K RR still translates to one packet
per request.

Lower throughput at 100x vs 10x can be (at least in part)
explained by looking at bytes per packet sent (nstat). It likely
also explains the lower throughput of 1x for some variants.

upstream:

N=1 bytes/pkt=16581
N=10 bytes/pkt=61513
N=100 bytes/pkt=51558

at_rx:

N=1 bytes/pkt=65204
N=10 bytes/pkt=65148
N=100 bytes/pkt=56840
====================

Acked-by: Michael S. Tsirkin <mst@redhat.com>

+152 -65
+152 -65
drivers/net/virtio_net.c
··· 33 33 static int napi_weight = NAPI_POLL_WEIGHT; 34 34 module_param(napi_weight, int, 0444); 35 35 36 - static bool csum = true, gso = true; 36 + static bool csum = true, gso = true, napi_tx; 37 37 module_param(csum, bool, 0444); 38 38 module_param(gso, bool, 0444); 39 + module_param(napi_tx, bool, 0644); 39 40 40 41 /* FIXME: MTU in config. */ 41 42 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) ··· 87 86 88 87 /* Name of the send queue: output.$index */ 89 88 char name[40]; 89 + 90 + struct napi_struct napi; 90 91 }; 91 92 92 93 /* Internal representation of a receive virtqueue */ ··· 242 239 return p; 243 240 } 244 241 242 + static void virtqueue_napi_schedule(struct napi_struct *napi, 243 + struct virtqueue *vq) 244 + { 245 + if (napi_schedule_prep(napi)) { 246 + virtqueue_disable_cb(vq); 247 + __napi_schedule(napi); 248 + } 249 + } 250 + 251 + static void virtqueue_napi_complete(struct napi_struct *napi, 252 + struct virtqueue *vq, int processed) 253 + { 254 + int opaque; 255 + 256 + opaque = virtqueue_enable_cb_prepare(vq); 257 + if (napi_complete_done(napi, processed) && 258 + unlikely(virtqueue_poll(vq, opaque))) 259 + virtqueue_napi_schedule(napi, vq); 260 + } 261 + 245 262 static void skb_xmit_done(struct virtqueue *vq) 246 263 { 247 264 struct virtnet_info *vi = vq->vdev->priv; 265 + struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 248 266 249 267 /* Suppress further interrupts. */ 250 268 virtqueue_disable_cb(vq); 251 269 252 - /* We were probably waiting for more output buffers. */ 253 - netif_wake_subqueue(vi->dev, vq2txq(vq)); 270 + if (napi->weight) 271 + virtqueue_napi_schedule(napi, vq); 272 + else 273 + /* We were probably waiting for more output buffers. */ 274 + netif_wake_subqueue(vi->dev, vq2txq(vq)); 254 275 } 255 276 256 277 static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx) ··· 963 936 struct virtnet_info *vi = rvq->vdev->priv; 964 937 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 965 938 966 - /* Schedule NAPI, Suppress further interrupts if successful. */ 967 - if (napi_schedule_prep(&rq->napi)) { 968 - virtqueue_disable_cb(rvq); 969 - __napi_schedule(&rq->napi); 970 - } 939 + virtqueue_napi_schedule(&rq->napi, rvq); 971 940 } 972 941 973 - static void virtnet_napi_enable(struct receive_queue *rq) 942 + static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 974 943 { 975 - napi_enable(&rq->napi); 944 + napi_enable(napi); 976 945 977 946 /* If all buffers were filled by other side before we napi_enabled, we 978 - * won't get another interrupt, so process any outstanding packets 979 - * now. virtnet_poll wants re-enable the queue, so we disable here. 980 - * We synchronize against interrupts via NAPI_STATE_SCHED */ 981 - if (napi_schedule_prep(&rq->napi)) { 982 - virtqueue_disable_cb(rq->vq); 983 - local_bh_disable(); 984 - __napi_schedule(&rq->napi); 985 - local_bh_enable(); 947 + * won't get another interrupt, so process any outstanding packets now. 948 + * Call local_bh_enable after to trigger softIRQ processing. 949 + */ 950 + local_bh_disable(); 951 + virtqueue_napi_schedule(napi, vq); 952 + local_bh_enable(); 953 + } 954 + 955 + static void virtnet_napi_tx_enable(struct virtnet_info *vi, 956 + struct virtqueue *vq, 957 + struct napi_struct *napi) 958 + { 959 + if (!napi->weight) 960 + return; 961 + 962 + /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 963 + * enable the feature if this is likely affine with the transmit path. 964 + */ 965 + if (!vi->affinity_hint_set) { 966 + napi->weight = 0; 967 + return; 986 968 } 969 + 970 + return virtnet_napi_enable(vq, napi); 987 971 } 988 972 989 973 static void refill_work(struct work_struct *work) ··· 1009 971 1010 972 napi_disable(&rq->napi); 1011 973 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1012 - virtnet_napi_enable(rq); 974 + virtnet_napi_enable(rq->vq, &rq->napi); 1013 975 1014 976 /* In theory, this can happen: if we don't get any buffers in 1015 977 * we will *never* try to fill again. ··· 1045 1007 return received; 1046 1008 } 1047 1009 1048 - static int virtnet_poll(struct napi_struct *napi, int budget) 1049 - { 1050 - struct receive_queue *rq = 1051 - container_of(napi, struct receive_queue, napi); 1052 - unsigned int r, received; 1053 - 1054 - received = virtnet_receive(rq, budget); 1055 - 1056 - /* Out of packets? */ 1057 - if (received < budget) { 1058 - r = virtqueue_enable_cb_prepare(rq->vq); 1059 - if (napi_complete_done(napi, received)) { 1060 - if (unlikely(virtqueue_poll(rq->vq, r)) && 1061 - napi_schedule_prep(napi)) { 1062 - virtqueue_disable_cb(rq->vq); 1063 - __napi_schedule(napi); 1064 - } 1065 - } 1066 - } 1067 - 1068 - return received; 1069 - } 1070 - 1071 - static int virtnet_open(struct net_device *dev) 1072 - { 1073 - struct virtnet_info *vi = netdev_priv(dev); 1074 - int i; 1075 - 1076 - for (i = 0; i < vi->max_queue_pairs; i++) { 1077 - if (i < vi->curr_queue_pairs) 1078 - /* Make sure we have some buffers: if oom use wq. */ 1079 - if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1080 - schedule_delayed_work(&vi->refill, 0); 1081 - virtnet_napi_enable(&vi->rq[i]); 1082 - } 1083 - 1084 - return 0; 1085 - } 1086 - 1087 1010 static void free_old_xmit_skbs(struct send_queue *sq) 1088 1011 { 1089 1012 struct sk_buff *skb; ··· 1073 1074 stats->tx_bytes += bytes; 1074 1075 stats->tx_packets += packets; 1075 1076 u64_stats_update_end(&stats->tx_syncp); 1077 + } 1078 + 1079 + static void virtnet_poll_cleantx(struct receive_queue *rq) 1080 + { 1081 + struct virtnet_info *vi = rq->vq->vdev->priv; 1082 + unsigned int index = vq2rxq(rq->vq); 1083 + struct send_queue *sq = &vi->sq[index]; 1084 + struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 1085 + 1086 + if (!sq->napi.weight) 1087 + return; 1088 + 1089 + if (__netif_tx_trylock(txq)) { 1090 + free_old_xmit_skbs(sq); 1091 + __netif_tx_unlock(txq); 1092 + } 1093 + 1094 + if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1095 + netif_tx_wake_queue(txq); 1096 + } 1097 + 1098 + static int virtnet_poll(struct napi_struct *napi, int budget) 1099 + { 1100 + struct receive_queue *rq = 1101 + container_of(napi, struct receive_queue, napi); 1102 + unsigned int received; 1103 + 1104 + virtnet_poll_cleantx(rq); 1105 + 1106 + received = virtnet_receive(rq, budget); 1107 + 1108 + /* Out of packets? */ 1109 + if (received < budget) 1110 + virtqueue_napi_complete(napi, rq->vq, received); 1111 + 1112 + return received; 1113 + } 1114 + 1115 + static int virtnet_open(struct net_device *dev) 1116 + { 1117 + struct virtnet_info *vi = netdev_priv(dev); 1118 + int i; 1119 + 1120 + for (i = 0; i < vi->max_queue_pairs; i++) { 1121 + if (i < vi->curr_queue_pairs) 1122 + /* Make sure we have some buffers: if oom use wq. */ 1123 + if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1124 + schedule_delayed_work(&vi->refill, 0); 1125 + virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1126 + virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1127 + } 1128 + 1129 + return 0; 1130 + } 1131 + 1132 + static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1133 + { 1134 + struct send_queue *sq = container_of(napi, struct send_queue, napi); 1135 + struct virtnet_info *vi = sq->vq->vdev->priv; 1136 + struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq)); 1137 + 1138 + __netif_tx_lock(txq, raw_smp_processor_id()); 1139 + free_old_xmit_skbs(sq); 1140 + __netif_tx_unlock(txq); 1141 + 1142 + virtqueue_napi_complete(napi, sq->vq, 0); 1143 + 1144 + if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1145 + netif_tx_wake_queue(txq); 1146 + 1147 + return 0; 1076 1148 } 1077 1149 1078 1150 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) ··· 1195 1125 int err; 1196 1126 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 1197 1127 bool kick = !skb->xmit_more; 1128 + bool use_napi = sq->napi.weight; 1198 1129 1199 1130 /* Free up any pending old buffers before queueing new ones. */ 1200 1131 free_old_xmit_skbs(sq); 1132 + 1133 + if (use_napi && kick) 1134 + virtqueue_enable_cb_delayed(sq->vq); 1201 1135 1202 1136 /* timestamp packet in software */ 1203 1137 skb_tx_timestamp(skb); ··· 1221 1147 } 1222 1148 1223 1149 /* Don't wait up for transmitted skbs to be freed. */ 1224 - skb_orphan(skb); 1225 - nf_reset(skb); 1150 + if (!use_napi) { 1151 + skb_orphan(skb); 1152 + nf_reset(skb); 1153 + } 1226 1154 1227 1155 /* If running out of space, stop queue to avoid getting packets that we 1228 1156 * are then unable to transmit. ··· 1238 1162 */ 1239 1163 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1240 1164 netif_stop_subqueue(dev, qnum); 1241 - if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 1165 + if (!use_napi && 1166 + unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 1242 1167 /* More just got used, free them then recheck. */ 1243 1168 free_old_xmit_skbs(sq); 1244 1169 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { ··· 1443 1366 /* Make sure refill_work doesn't re-enable napi! */ 1444 1367 cancel_delayed_work_sync(&vi->refill); 1445 1368 1446 - for (i = 0; i < vi->max_queue_pairs; i++) 1369 + for (i = 0; i < vi->max_queue_pairs; i++) { 1447 1370 napi_disable(&vi->rq[i].napi); 1371 + napi_disable(&vi->sq[i].napi); 1372 + } 1448 1373 1449 1374 return 0; 1450 1375 } ··· 1801 1722 cancel_delayed_work_sync(&vi->refill); 1802 1723 1803 1724 if (netif_running(vi->dev)) { 1804 - for (i = 0; i < vi->max_queue_pairs; i++) 1725 + for (i = 0; i < vi->max_queue_pairs; i++) { 1805 1726 napi_disable(&vi->rq[i].napi); 1727 + napi_disable(&vi->sq[i].napi); 1728 + } 1806 1729 } 1807 1730 } 1808 1731 ··· 1827 1746 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1828 1747 schedule_delayed_work(&vi->refill, 0); 1829 1748 1830 - for (i = 0; i < vi->max_queue_pairs; i++) 1831 - virtnet_napi_enable(&vi->rq[i]); 1749 + for (i = 0; i < vi->max_queue_pairs; i++) { 1750 + virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1751 + virtnet_napi_tx_enable(vi, vi->sq[i].vq, 1752 + &vi->sq[i].napi); 1753 + } 1832 1754 } 1833 1755 1834 1756 netif_device_attach(vi->dev); ··· 2036 1952 for (i = 0; i < vi->max_queue_pairs; i++) { 2037 1953 napi_hash_del(&vi->rq[i].napi); 2038 1954 netif_napi_del(&vi->rq[i].napi); 1955 + netif_napi_del(&vi->sq[i].napi); 2039 1956 } 2040 1957 2041 1958 /* We called napi_hash_del() before netif_napi_del(), ··· 2222 2137 vi->rq[i].pages = NULL; 2223 2138 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2224 2139 napi_weight); 2140 + netif_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2141 + napi_tx ? napi_weight : 0); 2225 2142 2226 2143 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 2227 2144 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);