From: physicalmtea@gmail.com
To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com
Cc: jasowangio@gmail.com, eperezma@redhat.com,
xuanzhuo@linux.alibaba.com, davem@davemloft.net,
edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
horms@kernel.org, virtualization@lists.linux.dev,
kvm@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 3/5] vsock: reuse same-flow socket lookup in RX batches
Date: Fri, 2 Oct 2026 07:45:49 +0000 [thread overview]
Message-ID: <20261002074551.318789-4-physicalmtea@gmail.com> (raw)
From: Jia Jia <physicalmtea@gmail.com>
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@gmail.com>
---
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
reply other threads:[~2026-10-02 7:46 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261002074551.318789-4-physicalmtea@gmail.com \
--to=physicalmtea@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=eperezma@redhat.com \
--cc=horms@kernel.org \
--cc=jasowangio@gmail.com \
--cc=kuba@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=xuanzhuo@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®