[PATCH 3/5] vsock: reuse same-flow socket lookup in RX batches

From: physicalmtea

Date: Fri Oct 02 2026 - 03:48:52 EST


From: Jia Jia <physicalmtea@xxxxxxxxx>

An RX lock batch still looks up the socket for every packet and takes a
temporary lookup reference, even though the batch already holds a
reference to the locked socket.

Record the network namespace and packet address tuple when a batch
starts. Reuse the batch socket for later STREAM/RW packets with the same
tuple, and release the batch before looking up a different flow.

The cached path still traces every packet, takes an skb owner reference,
validates socket state, source and transport, updates credit, and runs
the receive state machine. Only the socket table lookup and its temporary
reference are skipped.

Signed-off-by: Jia Jia <physicalmtea@xxxxxxxxx>
---
include/linux/virtio_vsock.h | 3 ++
net/vmw_vsock/virtio_transport_common.c | 58 +++++++++++++++----------
2 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 95ab1a75f..4369adc07 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -285,6 +285,9 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,

struct virtio_transport_rx_batch {
struct sock *sk;
+ struct net *net;
+ struct sockaddr_vm src;
+ struct sockaddr_vm dst;
};

void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index ad1e99689..d8c9c90c0 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1965,6 +1965,7 @@ void virtio_transport_rx_batch_finish(struct virtio_transport_rx_batch *batch)
struct sock *sk = batch->sk;

batch->sk = NULL;
+ batch->net = NULL;

if (!sk)
return;
@@ -1996,9 +1997,38 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
virtio_transport_recv_pkt_init_addrs(skb, &src, &dst);
virtio_transport_trace_recv_pkt(skb, &src, &dst);

+ if (batch->sk) {
+ if (batch->net == net &&
+ vsock_addr_equals_addr(&batch->src, &src) &&
+ vsock_addr_equals_addr(&batch->dst, &dst) &&
+ virtio_transport_recv_pkt_batchable(t, batch->sk)) {
+ sk = batch->sk;
+ if (!skb_set_owner_sk_safe(skb, sk)) {
+ WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n");
+ virtio_transport_rx_batch_finish(batch);
+ kfree_skb(skb);
+ return;
+ }
+
+ ctx = (struct virtio_transport_rx_pkt_ctx) {
+ .net = net,
+ .src = &src,
+ .dst = &dst,
+ .batchable = &batchable,
+ };
+ free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk, &ctx);
+ if (!batchable)
+ virtio_transport_rx_batch_finish(batch);
+ if (free_pkt)
+ kfree_skb(skb);
+ return;
+ }
+
+ virtio_transport_rx_batch_finish(batch);
+ }
+
sk = virtio_transport_recv_pkt_find_socket(skb, &src, &dst, net);
if (!sk) {
- virtio_transport_rx_batch_finish(batch);
(void)virtio_transport_reset_no_sock(t, skb, net);
kfree_skb(skb);
return;
@@ -2006,33 +2036,10 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t,

if (!skb_set_owner_sk_safe(skb, sk)) {
WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n");
- virtio_transport_rx_batch_finish(batch);
kfree_skb(skb);
return;
}

- if (batch->sk && batch->sk != sk) {
- /* Never acquire a second socket lock. */
- virtio_transport_rx_batch_finish(batch);
- }
-
- if (batch->sk == sk) {
- /* Keep the batch reference; drop this packet's lookup reference. */
- sock_put(sk);
- ctx = (struct virtio_transport_rx_pkt_ctx) {
- .net = net,
- .src = &src,
- .dst = &dst,
- .batchable = &batchable,
- };
- free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk, &ctx);
- if (!batchable)
- virtio_transport_rx_batch_finish(batch);
- if (free_pkt)
- kfree_skb(skb);
- return;
- }
-
lock_sock(sk);
/*
* Sockmap insertion takes the socket lock, but removal only holds
@@ -2051,6 +2058,9 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk, &ctx);
if (start_batch && batchable) {
/* Keep the lookup reference until the batch is released. */
+ batch->net = net;
+ batch->src = src;
+ batch->dst = dst;
batch->sk = sk;
return;
}
--
2.53.0