* [PATCH net-next v4 1/4] vsock/virtio: split socket lookup from locked RX processing
2026-10-14 11:56 [PATCH net-next v4 0/4] vsock/virtio: reduce RX per-packet socket overhead Jia Jia
@ 2026-10-14 11:56 ` Jia Jia
2026-10-14 11:56 ` [PATCH net-next v4 2/4] vsock/virtio: amortize RX socket locking for stream packets Jia Jia
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Jia Jia @ 2026-10-14 11:56 UTC (permalink / raw)
To: stefanha, sgarzare
Cc: mst, jasowangio, eperezma, xuanzhuo, davem, edumazet, kuba,
pabeni, horms, virtualization, kvm, netdev, linux-kernel,
bobbyeshleman
Split virtio_transport_recv_pkt() so socket lookup is separate from
receive state machine processing under the socket lock. This prepares the
receive path to keep a looked-up socket across multiple packets in a later
change.
Keep virtio_transport_recv_pkt() on its existing per-packet locking path.
The locked helper obtains packet-specific addresses from the skb and uses
the matched socket namespace for reset replies. Preserve source validation
and failure handling.
No functional change intended.
Signed-off-by: Jia Jia <physicalmtea@gmail.com>
---
net/vmw_vsock/virtio_transport_common.c | 128 ++++++++++++++----------
1 file changed, 75 insertions(+), 53 deletions(-)
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index f225f53ed4bab..61ee3bc205d5d 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1774,81 +1774,60 @@ static bool virtio_transport_valid_type(u16 type)
(type == VIRTIO_VSOCK_TYPE_SEQPACKET);
}
-/* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
- * lock.
- */
-void virtio_transport_recv_pkt(struct virtio_transport *t,
- struct sk_buff *skb, struct net *net)
+static struct sock *
+virtio_transport_find_socket(struct sk_buff *skb, struct sockaddr_vm *src,
+ struct sockaddr_vm *dst, struct net *net)
{
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));
- vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid),
- le32_to_cpu(hdr->dst_port));
-
- trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
- dst.svm_cid, dst.svm_port,
- le32_to_cpu(hdr->len),
- le16_to_cpu(hdr->type),
- le16_to_cpu(hdr->op),
- le32_to_cpu(hdr->flags),
- le32_to_cpu(hdr->buf_alloc),
- le32_to_cpu(hdr->fwd_cnt));
- if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) {
- (void)virtio_transport_reset_no_sock(t, skb, net);
- goto free_pkt;
- }
+ if (!virtio_transport_valid_type(le16_to_cpu(hdr->type)))
+ return NULL;
- /* The socket must be in connected or bound table
- * otherwise send reset back
- */
- sk = vsock_find_connected_socket_net(&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;
- }
- }
+ sk = vsock_find_connected_socket_net(src, dst, net);
+ if (!sk)
+ sk = vsock_find_bound_socket_net(dst, net);
+ if (!sk)
+ return NULL;
if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) {
- (void)virtio_transport_reset_no_sock(t, skb, net);
sock_put(sk);
- goto free_pkt;
+ return NULL;
}
- if (!skb_set_owner_sk_safe(skb, sk)) {
- WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n");
- goto free_pkt;
- }
+ return sk;
+}
- vsk = vsock_sk(sk);
+/*
+ * The caller holds sk's socket lock and must free skb if this returns true.
+ */
+static bool
+virtio_transport_recv_pkt_locked(struct virtio_transport *t,
+ struct sk_buff *skb, struct sock *sk)
+{
+ struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
+ struct sockaddr_vm src;
+ struct vsock_sock *vsk = vsock_sk(sk);
+ bool space_available;
- lock_sock(sk);
+ vsock_addr_init(&src, le64_to_cpu(hdr->src_cid),
+ le32_to_cpu(hdr->src_port));
- /* Check if sk has been closed or assigned to another transport before
- * lock_sock (note: listener sockets are not assigned to any transport)
+ /* Listener sockets accept packets from any source and are not assigned to
+ * a 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;
+ (void)virtio_transport_reset_no_sock(t, skb, sock_net(sk));
+ return true;
}
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;
+ vsk->local_addr.svm_cid = le64_to_cpu(hdr->dst_cid);
if (space_available)
sk->sk_write_space(sk);
@@ -1870,17 +1849,60 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
kfree_skb(skb);
break;
default:
- (void)virtio_transport_reset_no_sock(t, skb, net);
+ (void)virtio_transport_reset_no_sock(t, skb, sock_net(sk));
kfree_skb(skb);
break;
}
+ return false;
+}
+
+/* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
+ * lock.
+ */
+void virtio_transport_recv_pkt(struct virtio_transport *t,
+ struct sk_buff *skb, struct net *net)
+{
+ struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
+ struct sockaddr_vm src, dst;
+ struct sock *sk;
+ bool free_pkt;
+
+ vsock_addr_init(&src, le64_to_cpu(hdr->src_cid),
+ le32_to_cpu(hdr->src_port));
+ vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid),
+ le32_to_cpu(hdr->dst_port));
+
+ trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
+ dst.svm_cid, dst.svm_port,
+ le32_to_cpu(hdr->len),
+ le16_to_cpu(hdr->type),
+ le16_to_cpu(hdr->op),
+ le32_to_cpu(hdr->flags),
+ le32_to_cpu(hdr->buf_alloc),
+ le32_to_cpu(hdr->fwd_cnt));
+
+ sk = virtio_transport_find_socket(skb, &src, &dst, net);
+ if (!sk) {
+ (void)virtio_transport_reset_no_sock(t, skb, net);
+ goto free_pkt;
+ }
+
+ if (!skb_set_owner_sk_safe(skb, sk)) {
+ WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n");
+ goto free_pkt;
+ }
+
+ lock_sock(sk);
+ free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk);
release_sock(sk);
/* Release refcnt obtained when we fetched this socket out of the
* bound or connected list.
*/
sock_put(sk);
+ if (free_pkt)
+ kfree_skb(skb);
return;
free_pkt:
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net-next v4 2/4] vsock/virtio: amortize RX socket locking for stream packets
2026-10-14 11:56 [PATCH net-next v4 0/4] vsock/virtio: reduce RX per-packet socket overhead Jia Jia
2026-10-14 11:56 ` [PATCH net-next v4 1/4] vsock/virtio: split socket lookup from locked RX processing Jia Jia
@ 2026-10-14 11:56 ` Jia Jia
2026-10-14 11:56 ` [PATCH net-next v4 3/4] vsock/virtio: coalesce RX write-space notifications in lock batches Jia Jia
2026-10-14 11:56 ` [PATCH net-next v4 4/4] vsock/virtio: defer RX notifications until batch unlock Jia Jia
3 siblings, 0 replies; 5+ messages in thread
From: Jia Jia @ 2026-10-14 11:56 UTC (permalink / raw)
To: stefanha, sgarzare
Cc: mst, jasowangio, eperezma, xuanzhuo, davem, edumazet, kuba,
pabeni, horms, virtualization, kvm, netdev, linux-kernel,
bobbyeshleman
The virtio-vsock RX worker currently looks up the socket and takes its
lock for every packet. Keep the socket lock and lookup reference across a
bounded run of STREAM/RW packets for the same established socket.
Reuse the locked socket while packets continue to match the same flow.
End the batch for a different flow, non-RW traffic, malformed packets, or
a change in socket state, transport, protocol, or receive-buffer headroom.
A connected-table miss uses the existing receive path.
The reused path still traces every packet, takes an skb owner reference,
validates the source and transport, updates credit, and runs the existing
receive state machine.
Sockmap can replace the protocol without taking the socket lock. Check the
psock marker at each packet boundary and serialize initial admission with
sk_callback_lock, so a concurrent attach ends batching at the next packet.
Cap a batch at 64 packets and 64K of payload. The packet limit bounds
per-packet work, while the byte limit bounds how much data can queue before
a reader acquires the socket lock. Batching remains in the process-context
RX worker.
Signed-off-by: Jia Jia <physicalmtea@gmail.com>
---
include/linux/virtio_vsock.h | 11 ++
net/vmw_vsock/virtio_transport.c | 29 +++-
net/vmw_vsock/virtio_transport_common.c | 183 +++++++++++++++++++++++-
3 files changed, 216 insertions(+), 7 deletions(-)
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index f91704731057e..26dde6909c4ed 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -282,6 +282,17 @@ void virtio_transport_destruct(struct vsock_sock *vsk);
void virtio_transport_recv_pkt(struct virtio_transport *t,
struct sk_buff *skb, struct net *net);
+
+struct virtio_transport_rx_batch {
+ struct sock *sk;
+ unsigned int pkts;
+ size_t bytes;
+};
+
+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);
diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 4f9aa9c4c3aa5..ef076732158d4 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -629,8 +629,16 @@ virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid)
return seqpacket_allow;
}
+/*
+ * Keep a bounded run of packets for one socket under a single socket lock.
+ * Limit packet count and payload size to bound the work done while locked.
+ */
+#define VIRTIO_TRANSPORT_RX_BATCH_MAX_PKTS 64
+#define VIRTIO_TRANSPORT_RX_BATCH_MAX_BYTES (64 * 1024)
+
static void virtio_transport_rx_work(struct work_struct *work)
{
+ struct virtio_transport_rx_batch batch = {};
struct virtio_vsock *vsock =
container_of(work, struct virtio_vsock, rx_work);
struct virtqueue *vq;
@@ -666,6 +674,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 +682,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;
}
@@ -680,16 +690,33 @@ static void virtio_transport_rx_work(struct work_struct *work)
if (payload_len)
virtio_vsock_skb_put(skb, payload_len);
+ if (batch.sk &&
+ payload_len > VIRTIO_TRANSPORT_RX_BATCH_MAX_BYTES -
+ batch.bytes) {
+ virtio_transport_rx_batch_finish(&batch);
+ }
+
virtio_transport_deliver_tap_pkt(skb);
/* 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);
+
+ if (batch.sk) {
+ batch.pkts++;
+ batch.bytes += payload_len;
+ if (batch.pkts >= VIRTIO_TRANSPORT_RX_BATCH_MAX_PKTS ||
+ batch.bytes >= VIRTIO_TRANSPORT_RX_BATCH_MAX_BYTES) {
+ virtio_transport_rx_batch_finish(&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:
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 61ee3bc205d5d..ff74e805527a5 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -576,11 +576,16 @@ virtio_transport_collapse_rx_queue(struct virtio_vsock_sock *vvs,
skb_queue_splice(&new_queue, &vvs->rx_queue);
}
+static bool
+virtio_transport_rx_skb_has_headroom(struct virtio_vsock_sock *vvs)
+{
+ return (u64)(skb_queue_len(&vvs->rx_queue) + 1) * SKB_TRUESIZE(0) <=
+ vvs->buf_alloc;
+}
+
static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
u32 len)
{
- u64 skb_overhead = (skb_queue_len(&vvs->rx_queue) + 1) * SKB_TRUESIZE(0);
-
/* Allow at most buf_alloc * 2 total budget (payload + overhead),
* similar to how SO_RCVBUF is doubled to reserve space for sk_buff
* metadata. Check payload against buf_alloc to be sure the other
@@ -588,7 +593,7 @@ static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
* queue growth.
*/
if ((u64)vvs->buf_used + len > vvs->buf_alloc ||
- skb_overhead > vvs->buf_alloc)
+ !virtio_transport_rx_skb_has_headroom(vvs))
return false;
vvs->rx_bytes += len;
@@ -1798,12 +1803,38 @@ virtio_transport_find_socket(struct sk_buff *skb, struct sockaddr_vm *src,
return sk;
}
+static bool
+virtio_transport_sock_has_psock(const struct sock *sk)
+{
+ return (uintptr_t)rcu_access_pointer(__sk_user_data(sk)) &
+ SK_USER_DATA_PSOCK;
+}
+
+static bool
+virtio_transport_recv_pkt_batchable(struct virtio_transport *t,
+ struct sock *sk)
+{
+ struct vsock_sock *vsk = vsock_sk(sk);
+ struct virtio_vsock_sock *vvs = vsk->trans;
+
+ return sk->sk_state == TCP_ESTABLISHED &&
+ sk->sk_type == SOCK_STREAM &&
+ READ_ONCE(sk->sk_prot) == sk->sk_prot_creator &&
+ !sock_flag(sk, SOCK_DONE) &&
+ vsk->transport == &t->transport &&
+ vvs && virtio_transport_rx_skb_has_headroom(vvs) &&
+ !virtio_transport_sock_has_psock(sk);
+}
+
/*
- * The caller holds sk's socket lock and must free skb if this returns true.
+ * The caller holds sk's socket lock. Set @batchable if the socket can remain
+ * locked for another ordinary STREAM/RW packet. Return true if the caller
+ * must free @skb.
*/
static bool
virtio_transport_recv_pkt_locked(struct virtio_transport *t,
- struct sk_buff *skb, struct sock *sk)
+ struct sk_buff *skb, struct sock *sk,
+ bool *batchable)
{
struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
struct sockaddr_vm src;
@@ -1813,6 +1844,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,
vsock_addr_init(&src, le64_to_cpu(hdr->src_cid),
le32_to_cpu(hdr->src_port));
+ if (batchable)
+ *batchable = false;
+
/* Listener sockets accept packets from any source and are not assigned to
* a transport.
*/
@@ -1854,6 +1888,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,
break;
}
+ if (batchable)
+ *batchable = virtio_transport_recv_pkt_batchable(t, sk);
+
return false;
}
@@ -1894,7 +1931,7 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
}
lock_sock(sk);
- free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk);
+ free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk, NULL);
release_sock(sk);
/* Release refcnt obtained when we fetched this socket out of the
@@ -1910,6 +1947,140 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
}
EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
+/*
+ * Finish the RX batch.
+ * For a non-empty batch, the caller must hold the socket lock acquired with
+ * lock_sock(). This function releases the lock and the batch's lookup
+ * reference.
+ * An empty batch is a no-op.
+ */
+void virtio_transport_rx_batch_finish(struct virtio_transport_rx_batch *batch)
+{
+ struct sock *sk = batch->sk;
+
+ batch->sk = NULL;
+ batch->pkts = 0;
+ batch->bytes = 0;
+
+ if (!sk)
+ return;
+
+ release_sock(sk);
+ sock_put(sk);
+}
+EXPORT_SYMBOL_GPL(virtio_transport_rx_batch_finish);
+
+void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
+ struct sk_buff *skb, struct net *net,
+ struct virtio_transport_rx_batch *batch)
+{
+ struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
+ struct sockaddr_vm src, dst;
+ struct vsock_sock *vsk;
+ bool batchable, start_batch;
+ struct sock *sk;
+ bool free_pkt;
+
+ /* Only STREAM/RW packets can share a socket lock. */
+ if (le16_to_cpu(hdr->type) != VIRTIO_VSOCK_TYPE_STREAM ||
+ le16_to_cpu(hdr->op) != VIRTIO_VSOCK_OP_RW) {
+ virtio_transport_rx_batch_finish(batch);
+ virtio_transport_recv_pkt(t, skb, net);
+ return;
+ }
+
+ vsock_addr_init(&src, le64_to_cpu(hdr->src_cid),
+ le32_to_cpu(hdr->src_port));
+ vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid),
+ le32_to_cpu(hdr->dst_port));
+
+ if (batch->sk) {
+ vsk = vsock_sk(batch->sk);
+ if (vsock_addr_equals_addr(&src, &vsk->remote_addr) &&
+ dst.svm_port == vsk->local_addr.svm_port &&
+ vsock_net_check_mode(sock_net(batch->sk), net) &&
+ virtio_transport_recv_pkt_batchable(t, batch->sk)) {
+ sk = batch->sk;
+ trace_virtio_transport_recv_pkt(src.svm_cid,
+ src.svm_port,
+ dst.svm_cid,
+ dst.svm_port,
+ le32_to_cpu(hdr->len),
+ le16_to_cpu(hdr->type),
+ le16_to_cpu(hdr->op),
+ le32_to_cpu(hdr->flags),
+ le32_to_cpu(hdr->buf_alloc),
+ le32_to_cpu(hdr->fwd_cnt));
+ 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;
+ }
+
+ free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk,
+ &batchable);
+ if (!batchable)
+ virtio_transport_rx_batch_finish(batch);
+ if (free_pkt)
+ kfree_skb(skb);
+ return;
+ }
+
+ virtio_transport_rx_batch_finish(batch);
+ }
+
+ sk = vsock_find_connected_socket_net(&src, &dst, net);
+ if (!sk) {
+ virtio_transport_recv_pkt(t, skb, net);
+ return;
+ }
+
+ if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) {
+ sock_put(sk);
+ virtio_transport_recv_pkt(t, skb, net);
+ return;
+ }
+
+ trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
+ dst.svm_cid, dst.svm_port,
+ le32_to_cpu(hdr->len),
+ le16_to_cpu(hdr->type),
+ le16_to_cpu(hdr->op),
+ le32_to_cpu(hdr->flags),
+ le32_to_cpu(hdr->buf_alloc),
+ le32_to_cpu(hdr->fwd_cnt));
+
+ if (!skb_set_owner_sk_safe(skb, sk)) {
+ WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n");
+ kfree_skb(skb);
+ return;
+ }
+
+ lock_sock(sk);
+ /*
+ * Sockmap removal restores the native protocol under sk_callback_lock.
+ * Serialize this initial eligibility check with that update.
+ */
+ read_lock_bh(&sk->sk_callback_lock);
+ start_batch = virtio_transport_recv_pkt_batchable(t, sk);
+ read_unlock_bh(&sk->sk_callback_lock);
+
+ free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk,
+ start_batch ? &batchable : NULL);
+ if (start_batch && batchable) {
+ /* Keep the lookup reference until the batch is released. */
+ batch->sk = sk;
+ return;
+ }
+
+ release_sock(sk);
+ sock_put(sk);
+ if (free_pkt)
+ kfree_skb(skb);
+}
+EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt_batch);
+
/* Remove skbs found in a queue that have a vsk that matches.
*
* Each skb is freed.
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net-next v4 3/4] vsock/virtio: coalesce RX write-space notifications in lock batches
2026-10-14 11:56 [PATCH net-next v4 0/4] vsock/virtio: reduce RX per-packet socket overhead Jia Jia
2026-10-14 11:56 ` [PATCH net-next v4 1/4] vsock/virtio: split socket lookup from locked RX processing Jia Jia
2026-10-14 11:56 ` [PATCH net-next v4 2/4] vsock/virtio: amortize RX socket locking for stream packets Jia Jia
@ 2026-10-14 11:56 ` Jia Jia
2026-10-14 11:56 ` [PATCH net-next v4 4/4] vsock/virtio: defer RX notifications until batch unlock Jia Jia
3 siblings, 0 replies; 5+ messages in thread
From: Jia Jia @ 2026-10-14 11:56 UTC (permalink / raw)
To: stefanha, sgarzare
Cc: mst, jasowangio, eperezma, xuanzhuo, davem, edumazet, kuba,
pabeni, horms, virtualization, kvm, netdev, linux-kernel,
bobbyeshleman
Each received packet updates peer credit and may call sk_write_space().
A writer cannot use that credit until the batch releases the socket lock,
so one notification at batch finish is sufficient.
Save the initial sk_write_space callback when the AF_VSOCK socket is
created and require it to remain installed for batching. This lets the
transport module deliver the deferred notification without referring to
the unexported default callback. Sockets with replaced callbacks retain
their per-packet behavior.
Record the batch socket before processing its first packet so a batch that
ends on that packet cannot lose the pending notification.
Signed-off-by: Jia Jia <physicalmtea@gmail.com>
---
include/linux/virtio_vsock.h | 1 +
include/net/af_vsock.h | 5 +++
net/vmw_vsock/af_vsock.c | 1 +
net/vmw_vsock/virtio_transport_common.c | 53 +++++++++++++++++--------
4 files changed, 44 insertions(+), 16 deletions(-)
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 26dde6909c4ed..bf077ee0ded88 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -287,6 +287,7 @@ struct virtio_transport_rx_batch {
struct sock *sk;
unsigned int pkts;
size_t bytes;
+ bool write_space_pending;
};
void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 5549298c1ec6b..9a40960d10cbe 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -63,6 +63,11 @@ struct vsock_sock {
u32 peer_shutdown;
bool sent_request;
bool ignore_connecting_rst;
+ /*
+ * Initial callback, used to identify replacements and deliver deferred
+ * write-space notifications.
+ */
+ void (*default_write_space)(struct sock *sk);
/* Protected by lock_sock(sk) */
u64 buffer_size;
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 44cee74519555..fe7f49da6c4bc 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -958,6 +958,7 @@ static struct sock *__vsock_create(struct net *net,
sk->sk_type = type;
vsk = vsock_sk(sk);
+ vsk->default_write_space = sk->sk_write_space;
vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index ff74e805527a5..f4b5f0230493e 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1820,6 +1820,7 @@ virtio_transport_recv_pkt_batchable(struct virtio_transport *t,
return sk->sk_state == TCP_ESTABLISHED &&
sk->sk_type == SOCK_STREAM &&
READ_ONCE(sk->sk_prot) == sk->sk_prot_creator &&
+ READ_ONCE(sk->sk_write_space) == vsk->default_write_space &&
!sock_flag(sk, SOCK_DONE) &&
vsk->transport == &t->transport &&
vvs && virtio_transport_rx_skb_has_headroom(vvs) &&
@@ -1828,13 +1829,15 @@ virtio_transport_recv_pkt_batchable(struct virtio_transport *t,
/*
* The caller holds sk's socket lock. Set @batchable if the socket can remain
- * locked for another ordinary STREAM/RW packet. Return true if the caller
- * must free @skb.
+ * locked for another ordinary STREAM/RW packet. When @batch is non-NULL,
+ * record a default write-space notification for batch finish instead of
+ * delivering it immediately. Return true if the caller must free @skb.
*/
static bool
virtio_transport_recv_pkt_locked(struct virtio_transport *t,
struct sk_buff *skb, struct sock *sk,
- bool *batchable)
+ bool *batchable,
+ struct virtio_transport_rx_batch *batch)
{
struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
struct sockaddr_vm src;
@@ -1863,8 +1866,12 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,
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);
+ if (space_available) {
+ if (batch)
+ batch->write_space_pending = true;
+ else
+ sk->sk_write_space(sk);
+ }
switch (sk->sk_state) {
case TCP_LISTEN:
@@ -1931,7 +1938,7 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
}
lock_sock(sk);
- free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk, NULL);
+ free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk, NULL, NULL);
release_sock(sk);
/* Release refcnt obtained when we fetched this socket out of the
@@ -1949,22 +1956,27 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
/*
* Finish the RX batch.
- * For a non-empty batch, the caller must hold the socket lock acquired with
- * lock_sock(). This function releases the lock and the batch's lookup
- * reference.
+ * For a non-empty batch, the caller holds the socket lock acquired with
+ * lock_sock(). Deliver any deferred write-space notification while the lock
+ * is held, then release the lock and the batch's lookup reference.
* An empty batch is a no-op.
*/
void virtio_transport_rx_batch_finish(struct virtio_transport_rx_batch *batch)
{
+ bool write_space_pending = batch->write_space_pending;
struct sock *sk = batch->sk;
batch->sk = NULL;
batch->pkts = 0;
batch->bytes = 0;
+ batch->write_space_pending = false;
if (!sk)
return;
+ if (write_space_pending)
+ vsock_sk(sk)->default_write_space(sk);
+
release_sock(sk);
sock_put(sk);
}
@@ -2019,7 +2031,8 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
}
free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk,
- &batchable);
+ &batchable,
+ batch);
if (!batchable)
virtio_transport_rx_batch_finish(batch);
if (free_pkt)
@@ -2059,23 +2072,31 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
lock_sock(sk);
/*
- * Sockmap removal restores the native protocol under sk_callback_lock.
- * Serialize this initial eligibility check with that update.
+ * Sockmap teardown restores callbacks under sk_callback_lock. Keep the
+ * initial eligibility check consistent with that update.
*/
read_lock_bh(&sk->sk_callback_lock);
start_batch = virtio_transport_recv_pkt_batchable(t, sk);
read_unlock_bh(&sk->sk_callback_lock);
+ if (start_batch)
+ batch->sk = sk;
+
free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk,
- start_batch ? &batchable : NULL);
+ start_batch ? &batchable : NULL,
+ start_batch ? batch : NULL);
if (start_batch && batchable) {
/* Keep the lookup reference until the batch is released. */
- batch->sk = sk;
return;
}
- release_sock(sk);
- sock_put(sk);
+ if (start_batch) {
+ virtio_transport_rx_batch_finish(batch);
+ } else {
+ release_sock(sk);
+ sock_put(sk);
+ }
+
if (free_pkt)
kfree_skb(skb);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net-next v4 4/4] vsock/virtio: defer RX notifications until batch unlock
2026-10-14 11:56 [PATCH net-next v4 0/4] vsock/virtio: reduce RX per-packet socket overhead Jia Jia
` (2 preceding siblings ...)
2026-10-14 11:56 ` [PATCH net-next v4 3/4] vsock/virtio: coalesce RX write-space notifications in lock batches Jia Jia
@ 2026-10-14 11:56 ` Jia Jia
3 siblings, 0 replies; 5+ messages in thread
From: Jia Jia @ 2026-10-14 11:56 UTC (permalink / raw)
To: stefanha, sgarzare
Cc: mst, jasowangio, eperezma, xuanzhuo, davem, edumazet, kuba,
pabeni, horms, virtualization, kvm, netdev, linux-kernel,
bobbyeshleman
An RX batch can make a socket readable while the worker still holds its
socket lock. Waking a blocking reader then only moves it to waiting for
the same lock.
Record the existing low-watermark condition and notify once after
release_sock(). Move the coalesced write-space notification after the
unlock as well, since both callbacks wake sk_sleep(sk). Deliver
write-space before data-ready and keep the lookup reference until both
callbacks complete.
As with write-space, save the initial sk_data_ready callback and batch only
while both saved callbacks remain installed. Sockets with replaced
callbacks retain their per-packet behavior.
The existing psock-marker check also covers a concurrent sockmap attach:
at most the already admitted packet completes on the native path, and the
next packet ends the batch.
Signed-off-by: Jia Jia <physicalmtea@gmail.com>
---
include/linux/virtio_vsock.h | 1 +
include/net/af_vsock.h | 5 ++--
net/vmw_vsock/af_vsock.c | 1 +
net/vmw_vsock/virtio_transport_common.c | 35 ++++++++++++++++++-------
4 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index bf077ee0ded88..f1396ca3ad9b5 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -288,6 +288,7 @@ struct virtio_transport_rx_batch {
unsigned int pkts;
size_t bytes;
bool write_space_pending;
+ bool data_ready_pending;
};
void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 9a40960d10cbe..78bab5820f528 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -64,9 +64,10 @@ struct vsock_sock {
bool sent_request;
bool ignore_connecting_rst;
/*
- * Initial callback, used to identify replacements and deliver deferred
- * write-space notifications.
+ * Initial callbacks, used to identify replacements and deliver deferred
+ * notifications.
*/
+ void (*default_data_ready)(struct sock *sk);
void (*default_write_space)(struct sock *sk);
/* Protected by lock_sock(sk) */
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index fe7f49da6c4bc..f40659ec29df0 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -958,6 +958,7 @@ static struct sock *__vsock_create(struct net *net,
sk->sk_type = type;
vsk = vsock_sk(sk);
+ vsk->default_data_ready = sk->sk_data_ready;
vsk->default_write_space = sk->sk_write_space;
vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index f4b5f0230493e..8b62b789b71c5 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1588,7 +1588,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
static int
virtio_transport_recv_connected(struct sock *sk,
- struct sk_buff *skb)
+ struct sk_buff *skb,
+ bool *data_ready_pending)
{
struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
struct vsock_sock *vsk = vsock_sk(sk);
@@ -1607,7 +1608,13 @@ virtio_transport_recv_connected(struct sock *sk,
vsock_remove_sock(vsk);
break;
}
- vsock_data_ready(sk);
+ if (!data_ready_pending) {
+ vsock_data_ready(sk);
+ } else if (!*data_ready_pending &&
+ (vsock_stream_has_data(vsk) >= sk->sk_rcvlowat ||
+ sock_flag(sk, SOCK_DONE))) {
+ *data_ready_pending = true;
+ }
return err;
case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
virtio_transport_send_credit_update(vsk);
@@ -1820,6 +1827,7 @@ virtio_transport_recv_pkt_batchable(struct virtio_transport *t,
return sk->sk_state == TCP_ESTABLISHED &&
sk->sk_type == SOCK_STREAM &&
READ_ONCE(sk->sk_prot) == sk->sk_prot_creator &&
+ READ_ONCE(sk->sk_data_ready) == vsk->default_data_ready &&
READ_ONCE(sk->sk_write_space) == vsk->default_write_space &&
!sock_flag(sk, SOCK_DONE) &&
vsk->transport == &t->transport &&
@@ -1830,8 +1838,9 @@ virtio_transport_recv_pkt_batchable(struct virtio_transport *t,
/*
* The caller holds sk's socket lock. Set @batchable if the socket can remain
* locked for another ordinary STREAM/RW packet. When @batch is non-NULL,
- * record a default write-space notification for batch finish instead of
- * delivering it immediately. Return true if the caller must free @skb.
+ * record default write-space and data-ready notifications for batch finish
+ * instead of delivering them immediately. Return true if the caller must
+ * free @skb.
*/
static bool
virtio_transport_recv_pkt_locked(struct virtio_transport *t,
@@ -1883,7 +1892,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,
kfree_skb(skb);
break;
case TCP_ESTABLISHED:
- virtio_transport_recv_connected(sk, skb);
+ virtio_transport_recv_connected(sk, skb,
+ batch ?
+ &batch->data_ready_pending : NULL);
break;
case TCP_CLOSING:
virtio_transport_recv_disconnecting(sk, skb);
@@ -1957,27 +1968,33 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
/*
* Finish the RX batch.
* For a non-empty batch, the caller holds the socket lock acquired with
- * lock_sock(). Deliver any deferred write-space notification while the lock
- * is held, then release the lock and the batch's lookup reference.
- * An empty batch is a no-op.
+ * lock_sock(). Release the lock, deliver any deferred write-space and
+ * data-ready notifications in that order, then drop the batch's lookup
+ * reference. An empty batch is a no-op.
*/
void virtio_transport_rx_batch_finish(struct virtio_transport_rx_batch *batch)
{
bool write_space_pending = batch->write_space_pending;
+ bool data_ready_pending = batch->data_ready_pending;
struct sock *sk = batch->sk;
batch->sk = NULL;
batch->pkts = 0;
batch->bytes = 0;
batch->write_space_pending = false;
+ batch->data_ready_pending = false;
if (!sk)
return;
+ release_sock(sk);
+
if (write_space_pending)
vsock_sk(sk)->default_write_space(sk);
- release_sock(sk);
+ if (data_ready_pending)
+ vsock_sk(sk)->default_data_ready(sk);
+
sock_put(sk);
}
EXPORT_SYMBOL_GPL(virtio_transport_rx_batch_finish);
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread