Skip to contentSkip to conversation

vsock/virtio: reduce RX per-packet socket overhead

PATCH New Jia Jia 2 participants 4 messages Dec 4

Subsystems (6)
lore
Share & tools
lore

Jia Jia

author
#0 lore raw

v7 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 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.9497      7.5715  +91.70% [+86.36%, +97.19%]
  8K           4.8355      8.0738  +66.97% [+62.71%, +71.33%]
  16K          5.4708      8.0804  +47.70% [+44.61%, +50.86%]
  64K          5.8880      8.2147  +39.52% [+35.87%, +43.26%]

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           3.0190      6.3147  +109.16% [+99.17%, +119.66%]
       recv() calls (baseline/series): 537756/525298
  8K           4.0434      7.3899  +82.76% [+75.28%, +90.56%]
       recv() calls (baseline/series): 276505/263947
  16K          4.9556      8.2543  +66.56% [+61.89%, +71.38%]
       recv() calls (baseline/series): 147233/132504
  64K          5.8581      8.2548  +40.91% [+36.91%, +45.03%]
       recv() calls (baseline/series): 50990/33103

Request-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.

v7:
- remove the redundant buf_used + len batch eligibility check and the now-unused len arguments
- drop the 64K byte cap and virtio_transport_rx_batch_can_add(); the packet count advances by one per packet, so the post-processing check ends the batch at 64
- remove the sk_prot and psock eligibility checks left over from the notification-deferral patches, along with the associated single-packet fallback branch
- restore the original trace_virtio_transport_recv_pkt() signature now that the batching path adds only one call site
- rerun 4K, 8K, 16K and 64K throughput tests after dropping the byte cap; end-to-end RX exceeded the v6 byte-capped results at every size, while fixed-syscall RX exceeded them at 8K-64K and was 0.34% lower at 4K
v6: https://lore.kernel.org/netdev/[email protected]/
- 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            |  22 ++
 net/vmw_vsock/virtio_transport.c        |   7 +-
 net/vmw_vsock/virtio_transport_common.c | 310 ++++++++++++++++++++----
 3 files changed, 285 insertions(+), 54 deletions(-)

base-commit: 8df0638138d3e0344fd1fb36cf2d1ca1cf5028f0

Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v7 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
index f225f53ed4bab..bebf63115b263 100644
@@ -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
author
#2 lore raw

PATCH v7 2/2 vsock/virtio: amortize RX socket locking for stream packets

In reply to Jia Jia

The 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 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 or transport. 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. The limit uses VHOST_NET_BATCH as a reference; it is a policy choice rather than a protocol limit.

Beyond this limit, 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.9497      7.5715  +91.70% [+86.36%, +97.19%]
  8K           4.8355      8.0738  +66.97% [+62.71%, +71.33%]
  16K          5.4708      8.0804  +47.70% [+44.61%, +50.86%]
  64K          5.8880      8.2147  +39.52% [+35.87%, +43.26%]

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           3.0190      6.3147  +109.16% [+99.17%, +119.66%]
       recv() calls (baseline/series): 537756/525298
  8K           4.0434      7.3899  +82.76% [+75.28%, +90.56%]
       recv() calls (baseline/series): 276505/263947
  16K          4.9556      8.2543  +66.56% [+61.89%, +71.38%]
       recv() calls (baseline/series): 147233/132504
  64K          5.8581      8.2548  +40.91% [+36.91%, +45.03%]
       recv() calls (baseline/series): 50990/33103
---
 include/linux/virtio_vsock.h            |  22 +++
 net/vmw_vsock/virtio_transport.c        |   7 +-
 net/vmw_vsock/virtio_transport_common.c | 192 +++++++++++++++++++++++-
 3 files changed, 214 insertions(+), 7 deletions(-)
include/linux/virtio_vsock.h +22 -0
index f91704731057e..6eb3167c7ec8b 100644
@@ -282,6 +282,28 @@ 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;
+};
+
+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);
net/vmw_vsock/virtio_transport.c +6 -1
index 4f9aa9c4c3aa5..9af85712bd387 100644
@@ -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 +186 -6

231 lines not on this page · raw message

Signature · 1 line
2.53.0