vsock/virtio: reduce RX per-packet socket overhead
Subsystems (7)
v6 cover vsock/virtio: reduce RX per-packet socket overhead
This series reduces per-packet socket overhead in the virtio-vsock RX worker.
Patch 1 separates socket lookup from processing under the socket lock.
Patch 2 retains one socket lookup reference and socket lock across a bounded run of established STREAM/RW packets for the same native socket.
Performance:
The measurements below cover both patches. Throughput uses vsock_perf as the host sender and a custom single-threaded blocking receiver in the Guest, with the receiver on CPU0 and the virtio-vsock IRQ/RX worker on CPU1. Each row has 16 accepted, balanced AB/BA pairs and a fresh Guest per state. The fixed-syscall campaign was collected separately; each table compares paired baseline and series runs from its own campaign. The host uses vhost-vsock.
Accepted runs also passed byte-integrity, CPU-affinity/migration, IRQ-activity, host/Guest steal, I/O-wait and swap, sentinel, kernel-diagnostic, and applicable perf-validity checks.
End-to-end RX throughput
(64K Guest recv() size, SO_RCVLOWAT=1; Gbit/s):
payload baseline RX series RX throughput change (95% CI)
(Gbit/s) (Gbit/s) 64B 0.1212 0.2555 +110.81% [+106.99%, +114.70%] 256B 0.5551 1.0212 +83.95% [+79.94%, +88.06%] 512B 1.0568 1.8761 +77.53% [+73.02%, +82.15%] 1K 1.9260 3.1453 +63.31% [+58.58%, +68.17%] 4K 3.5175 6.3239 +79.78% [+72.79%, +87.06%] 8K 4.1183 5.6867 +38.08% [+31.06%, +45.48%] 16K 5.0155 6.8346 +36.27% [+31.12%, +41.62%] 64K 5.0541 5.8403 +15.56% [+11.12%, +20.17%]
Fixed-syscall stress test
(receiver recv() size = sender write size; Gbit/s):
payload baseline RX series RX throughput change (95% CI)
(Gbit/s) (Gbit/s)
64B 0.1435 0.2750 +91.60% [+82.19%, +101.50%]
recv() calls (baseline/series): 1048577/1048577
256B 0.4218 0.9659 +129.00% [+110.65%, +148.95%]
recv() calls (baseline/series): 1048577/1048577
512B 0.8062 1.6979 +110.60% [+94.49%, +128.04%]
recv() calls (baseline/series): 1048577/1048577
1K 1.5116 2.9585 +95.72% [+85.22%, +106.82%]
recv() calls (baseline/series): 1048577/1048577
4K 2.9234 6.3365 +116.75% [+110.78%, +122.89%]
recv() calls (baseline/series): 538537/525565
8K 4.0430 7.1193 +76.09% [+69.04%, +83.44%]
recv() calls (baseline/series): 276901/264412
16K 4.9182 7.7688 +57.96% [+54.40%, +61.60%]
recv() calls (baseline/series): 147674/132088
64K 5.6683 7.1636 +26.38% [+20.49%, +32.56%]
recv() calls (baseline/series): 50615/33343Request-response latency
(ping-pong RTT, 10,000 requests per state):
payload baseline mean RTT series mean RTT change (95% CI)
(us) (us)
64B 121.067 120.977 -0.07% [-2.45%, +2.36%] 256B 120.733 120.809 +0.06% [-1.69%, +1.85%] 4K 123.161 121.525 -1.33% [-2.87%, +0.24%]
---
Compared with v5, the performance suite was rerun with stricter validity gates, including host/Guest scheduling, steal, I/O-wait and swap monitoring, plus QEMU affinity and migration checks. Runs affected by these sources of noise were rejected, so the current results better isolate the RX batching improvement.
Changes since v5:
- keep patch 1 unchanged apart from Stefano's blank-line nit, and add his Reviewed-by tag
- drop the metadata-budget helper and the rx_buf_full() batch boundary
- apply Bobby's receive tracepoint refactor in the batching patch
- remove the locked helper's batch-specific parameters; return packet validation status and keep the continuation check in the batch caller
- keep sockmap eligibility checks at packet boundaries without taking sk_callback_lock
- move notification coalescing and deferral to a separate follow-up series
- update the performance measurements and add fixed-syscall stress results
v5: https://lore.kernel.org/netdev/[email protected]/
Jia Jia (2):
vsock/virtio: split socket lookup from locked RX processing vsock/virtio: amortize RX socket locking for stream packets
include/linux/virtio_vsock.h | 23 ++ .../events/vsock_virtio_transport_common.h | 40 +- net/vmw_vsock/virtio_transport.c | 7 +- net/vmw_vsock/virtio_transport_common.c | 368 +++++++++++++++--- 4 files changed, 359 insertions(+), 79 deletions(-)
base-commit: 8df0638138d3e0344fd1fb36cf2d1ca1cf5028f0
Signature · 1 line
2.53.0
PATCH v6 1/2 vsock/virtio: split socket lookup from locked RX processing
Split virtio_transport_recv_pkt() so socket lookup is separate from processing that occurs while holding the socket lock. This prepares the receive path to retain a looked-up socket across multiple packets in a later change.
Keep virtio_transport_recv_pkt() on its existing per-packet locking path. Pass the caller-provided network namespace to the locked helper so reset handling retains its existing semantics. Preserve source validation and failure handling.
--- net/vmw_vsock/virtio_transport_common.c | 132 ++++++++++++++---------- 1 file changed, 78 insertions(+), 54 deletions(-)
net/vmw_vsock/virtio_transport_common.c +78 -54
@@ -1774,6 +1774,80 @@ static bool virtio_transport_valid_type(u16 type) (type ==VIRTIO_VSOCK_TYPE_SEQPACKET ); } +static struct sock * +virtio_transport_find_socket (struct sockaddr_vm *src, +struct sockaddr_vm *dst,struct net *net) +{ +struct sock *sk; + + sk =vsock_find_connected_socket_net (src, dst, net); +if (!sk) + sk =vsock_find_bound_socket_net (dst, net); + +return sk; +} + +/* +* The caller holds sk's socket lock. +*/ +static void +virtio_transport_recv_pkt_locked (struct virtio_transport *t, +struct sk_buff *skb,struct sock *sk, +struct net *net) +{ +struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb); +struct vsock_sock *vsk =vsock_sk (sk); +struct sockaddr_vm src; +bool space_available; + +vsock_addr_init (&src,le64_to_cpu (hdr->src_cid), +le32_to_cpu (hdr->src_port)); + +/* +* The socket may have been closed or reassigned after lookup but before +* its socket lock was acquired. Listener sockets have no assigned +* transport. +*/ +if (sock_flag (sk,SOCK_DONE ) || + (sk->sk_state !=TCP_LISTEN && + !vsock_check_source (vsk, &t->transport, &src))) { + (void )virtio_transport_reset_no_sock (t, skb, net); +kfree_skb (skb); +return ; + } + + space_available =virtio_transport_space_update (sk, skb); + +/* Update CID in case it has changed after a transport reset event */ +if (vsk->local_addr.svm_cid !=VMADDR_CID_ANY ) + vsk->local_addr.svm_cid =le64_to_cpu (hdr->dst_cid); + +if (space_available) + sk->sk_write_space (sk); + +switch (sk->sk_state) { +case TCP_LISTEN : +virtio_transport_recv_listen (sk, skb, t); +kfree_skb (skb); +break ; +case TCP_SYN_SENT : +virtio_transport_recv_connecting (sk, skb); +kfree_skb (skb); +break ; +case TCP_ESTABLISHED : +virtio_transport_recv_connected (sk, skb); +break ; +case TCP_CLOSING : +virtio_transport_recv_disconnecting (sk, skb); +kfree_skb (skb); +break ; +default : + (void )virtio_transport_reset_no_sock (t, skb, net); +kfree_skb (skb); +break ; + } +} +/* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex * lock. */
@@ -1782,9 +1856,7 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, {struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb);struct sockaddr_vm src, dst; -struct vsock_sock *vsk;struct sock *sk; -bool space_available;vsock_addr_init (&src,le64_to_cpu (hdr->src_cid),le32_to_cpu (hdr->src_port));
@@ -1808,13 +1880,10 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,/* The socket must be in connected or bound table * otherwise send reset back */ - sk =vsock_find_connected_socket_net (&src, &dst, net); + sk =virtio_transport_find_socket (&src, &dst, net);if (!sk) { - sk =vsock_find_bound_socket_net (&dst, net); -if (!sk) { - (void )virtio_transport_reset_no_sock (t, skb, net); - goto free_pkt; - } + (void )virtio_transport_reset_no_sock (t, skb, net); + goto free_pkt; }if (virtio_transport_get_type (sk) !=le16_to_cpu (hdr->type)) {
@@ -1828,53 +1897,8 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, goto free_pkt; } - vsk =vsock_sk (sk); -lock_sock (sk); - -/* Check if sk has been closed or assigned to another transport before -* lock_sock (note: listener sockets are not assigned to any transport) -*/ -if (sock_flag (sk,SOCK_DONE ) || - (sk->sk_state !=TCP_LISTEN && - !vsock_check_source (vsk, &t->transport, &src))) { - (void )virtio_transport_reset_no_sock (t, skb, net); -release_sock (sk); -sock_put (sk); - goto free_pkt; - } - - space_available =virtio_transport_space_update (sk, skb); - -/* Update CID in case it has changed after a transport reset event */ -if (vsk->local_addr.svm_cid !=VMADDR_CID_ANY ) - vsk->local_addr.svm_cid = dst.svm_cid; - -if (space_available) - sk->sk_write_space (sk); - -switch (sk->sk_state) { -case TCP_LISTEN : -virtio_transport_recv_listen (sk, skb, t); -kfree_skb (skb); -break ; -case TCP_SYN_SENT : -virtio_transport_recv_connecting (sk, skb); -kfree_skb (skb); -break ; -case TCP_ESTABLISHED : -virtio_transport_recv_connected (sk, skb); -break ; -case TCP_CLOSING : -virtio_transport_recv_disconnecting (sk, skb); -kfree_skb (skb); -break ; -default : - (void )virtio_transport_reset_no_sock (t, skb, net); -kfree_skb (skb); -break ; - } - +virtio_transport_recv_pkt_locked (t, skb, sk, net);release_sock (sk);/* Release refcnt obtained when we fetched this socket out of the
Signature · 1 line
2.53.0
PATCH v6 2/2 vsock/virtio: amortize RX socket locking for stream packets
In reply to Jia JiaThe virtio-vsock RX worker looks up a socket and takes its lock for every packet. Retain the connected lookup reference and socket lock across a bounded run of STREAM/RW packets for one established native socket.
Reuse the locked socket while packets match the same flow. Finish the batch on a malformed packet, flow change, non-RW traffic, or a change to the socket state, transport, protocol, sockmap state, or receive buffer limits. A connected lookup miss falls back to the existing receive path, which can still find a bound socket.
Limit an uninterrupted socket-lock ownership interval to 64 packets and 64 KiB of payload. The payload limit is one VIRTIO_VSOCK_MAX_PKT_BUF_SIZE. Small or zero-length packets still incur per-packet work, so an independent packet limit is needed. The 64-packet limit uses VHOST_NET_BATCH as a reference; it is a policy choice rather than a protocol limit.
Beyond these limits, a larger batch would only further amortize socket lookup and lock/unlock operations, while virtqueue work, validation, tracing, skb ownership, credit update, and queueing remain per-packet. I therefore expect diminishing returns while the uninterrupted socket-lock interval continues to grow, but I have not measured larger limits yet.
Only the guest virtio RX worker is changed. The benchmark exercises the host-sender-to-guest-receiver direction, where this worker is on the receive path. I have not measured the corresponding worker and socket-lock overhead in vhost-vsock or vsock_loopback, and they use different worker and locking models. Leave those paths unchanged pending separate policy and validation. I plan to evaluate vhost-vsock separately as follow-up work.
Performance:
These results cover both patches in this series.
Tested on a single-stream vsock connection (one Guest receiver thread on CPU0, virtio-vsock IRQ on Guest CPU1, fresh QEMU Guest per state, with 16 accepted, balanced AB/BA pairs per payload). The host uses vhost-vsock.
For throughput, vsock_perf runs on the host as the
sender, with a custom single-threaded Guest receiver pinned to Guest CPU0 using blocking recv(). The table uses a 64K buffer. The virtio-vsock IRQ/RX worker is pinned to Guest CPU1. To normalize run times across payloads into a bounded, deterministic window and avoid long runs being skewed by host background noise or virtualization scheduling jitter, transfer sizes scale proportionally with the request size (64B/64M, 256B/256M, etc.), capped at 2G for 4K-64K. The custom receiver keeps the recv() size and count explicit; vsock_perf's receiver uses poll()+read().
End-to-end RX throughput
(64K Guest recv() size, SO_RCVLOWAT=1; Gbit/s):
payload baseline RX series RX throughput change (95% CI)
(Gbit/s) (Gbit/s) 64B 0.1212 0.2555 +110.81% [+106.99%, +114.70%] 256B 0.5551 1.0212 +83.95% [+79.94%, +88.06%] 512B 1.0568 1.8761 +77.53% [+73.02%, +82.15%] 1K 1.9260 3.1453 +63.31% [+58.58%, +68.17%] 4K 3.5175 6.3239 +79.78% [+72.79%, +87.06%] 8K 4.1183 5.6867 +38.08% [+31.06%, +45.48%] 16K 5.0155 6.8346 +36.27% [+31.12%, +41.62%] 64K 5.0541 5.8403 +15.56% [+11.12%, +20.17%]
Fixed-syscall stress test
(receiver recv() size = sender write size; Gbit/s):
payload baseline RX series RX throughput change (95% CI)
(Gbit/s) (Gbit/s)
64B 0.1435 0.2750 +91.60% [+82.19%, +101.50%]
recv() calls (baseline/series): 1048577/1048577
256B 0.4218 0.9659 +129.00% [+110.65%, +148.95%]
recv() calls (baseline/series): 1048577/1048577
512B 0.8062 1.6979 +110.60% [+94.49%, +128.04%]
recv() calls (baseline/series): 1048577/1048577
1K 1.5116 2.9585 +95.72% [+85.22%, +106.82%]
recv() calls (baseline/series): 1048577/1048577
4K 2.9234 6.3365 +116.75% [+110.78%, +122.89%]
recv() calls (baseline/series): 538537/525565
8K 4.0430 7.1193 +76.09% [+69.04%, +83.44%]
recv() calls (baseline/series): 276901/264412
16K 4.9182 7.7688 +57.96% [+54.40%, +61.60%]
recv() calls (baseline/series): 147674/132088
64K 5.6683 7.1636 +26.38% [+20.49%, +32.56%]
recv() calls (baseline/series): 50615/33343--- include/linux/virtio_vsock.h | 23 ++ .../events/vsock_virtio_transport_common.h | 40 +-- net/vmw_vsock/virtio_transport.c | 7 +- net/vmw_vsock/virtio_transport_common.c | 268 +++++++++++++++++- 4 files changed, 297 insertions(+), 41 deletions(-)
include/linux/virtio_vsock.h +23 -0
@@ -282,6 +282,29 @@ void virtio_transport_destruct(struct vsock_sock *vsk);void virtio_transport_recv_pkt (struct virtio_transport *t,struct sk_buff *skb,struct net *net); + +/* +* Initialize a batch to zero before use. A non-empty batch owns @sk's socket +* lock and the reference returned by vsock_find_connected_socket_net(). The +* caller retains both until it calls virtio_transport_rx_batch_finish(). An +* empty batch owns neither. +*/ +struct virtio_transport_rx_batch { +struct sock *sk; +unsigned int pkts; +size_t bytes; +}; + +static inline bool +virtio_transport_rx_batch_empty (const struct virtio_transport_rx_batch *batch) +{ +return !batch->sk; +} + +void virtio_transport_recv_pkt_batch (struct virtio_transport *t, +struct sk_buff *skb,struct net *net, +struct virtio_transport_rx_batch *batch); +void virtio_transport_rx_batch_finish (struct virtio_transport_rx_batch *batch);void virtio_transport_inc_tx_pkt (struct virtio_vsock_sock *vvs,struct sk_buff *skb);u32 virtio_transport_get_credit (struct virtio_vsock_sock *vvs,u32 wanted);void virtio_transport_put_credit (struct virtio_vsock_sock *vvs,u32 credit);
include/trace/events/vsock_virtio_transport_common.h +14 -26
@@ -88,24 +88,12 @@ TRACE_EVENT(virtio_transport_alloc_pkt, TRACE_EVENT(virtio_transport_recv_pkt, TP_PROTO( - __u32 src_cid, __u32 src_port, - __u32 dst_cid, __u32 dst_port, - __u32 len, - __u16 type, - __u16 op, - __u32 flags, - __u32 buf_alloc, - __u32 fwd_cnt + const struct virtio_vsock_hdr *hdr, + const struct sockaddr_vm *src, + const struct sockaddr_vm *dst ), TP_ARGS( - src_cid, src_port, - dst_cid, dst_port, - len, - type, - op, - flags, - buf_alloc, - fwd_cnt + hdr, src, dst ), TP_STRUCT__entry( __field(__u32, src_cid)
@@ -120,16 +108,16 @@ TRACE_EVENT(virtio_transport_recv_pkt,__field (__u32, fwd_cnt) ),TP_fast_assign ( - __entry->src_cid = src_cid; - __entry->src_port = src_port; - __entry->dst_cid = dst_cid; - __entry->dst_port = dst_port; - __entry->len = len; - __entry->type = type; - __entry->op = op; - __entry->flags = flags; - __entry->buf_alloc = buf_alloc; - __entry->fwd_cnt = fwd_cnt; + __entry->src_cid = src->svm_cid; + __entry->src_port = src->svm_port; + __entry->dst_cid = dst->svm_cid; + __entry->dst_port = dst->svm_port; + __entry->len =le32_to_cpu (hdr->len); + __entry->type =le16_to_cpu (hdr->type); + __entry->op =le16_to_cpu (hdr->op); + __entry->flags =le32_to_cpu (hdr->flags); + __entry->buf_alloc =le32_to_cpu (hdr->buf_alloc); + __entry->fwd_cnt =le32_to_cpu (hdr->fwd_cnt); ), TP_printk("%u:%u -> %u:%u len=%u type=%s op=%s flags=%#x " "buf_alloc=%u fwd_cnt=%u",
net/vmw_vsock/virtio_transport.c +6 -1
@@ -633,6 +633,7 @@ static void virtio_transport_rx_work(struct work_struct *work) {struct virtio_vsock *vsock = container_of(work, struct virtio_vsock, rx_work); +struct virtio_transport_rx_batch batch = {};struct virtqueue *vq;mutex_lock (&vsock->rx_lock);
@@ -666,6 +667,7 @@ static void virtio_transport_rx_work(struct work_struct *work)/* Drop short/long packets */ if (unlikely (len <sizeof (*hdr) || len >virtio_vsock_skb_len (skb))) { +virtio_transport_rx_batch_finish (&batch);kfree_skb (skb);continue ; }
@@ -673,6 +675,7 @@ static void virtio_transport_rx_work(struct work_struct *work) hdr =virtio_vsock_hdr (skb); payload_len =le32_to_cpu (hdr->len);if (unlikely (payload_len > len -sizeof (*hdr))) { +virtio_transport_rx_batch_finish (&batch);kfree_skb (skb);continue ; }
@@ -685,11 +688,13 @@ static void virtio_transport_rx_work(struct work_struct *work)/* Force virtio-transport into global mode since it * does not yet support local-mode namespacing. */ -virtio_transport_recv_pkt (&virtio_transport, skb,NULL ); +virtio_transport_recv_pkt_batch (&virtio_transport, skb, +NULL , &batch); } }while (!virtqueue_enable_cb (vq)); out: +virtio_transport_rx_batch_finish (&batch);if (vsock->rx_buf_nr < vsock->rx_buf_max_nr /2 )virtio_vsock_rx_fill (vsock); out_nofill:
net/vmw_vsock/virtio_transport_common.c +254 -14
313 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]