vsock: split socket lookup from locked RX processing
Subsystems (6)
PATCH 1/5 vsock: split socket lookup from locked RX processing
From: Jia Jia <[email protected]>
Split virtio_transport_recv_pkt() so socket lookup is separate from the receive state machine that runs under the socket lock.
Keep virtio_transport_recv_pkt() on its existing per-packet locking path. Pass the source and destination addresses used for lookup to the locked receive path so they are decoded only once.
Pass the network namespace and decoded address tuple to the locked receive path through a packet context so later extensions remain cleanly encapsulated.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so a later patch can retain it while processing a bounded run of packets under one socket lock.
--- net/vmw_vsock/virtio_transport_common.c | 140 ++++++++++++++++-------- 1 file changed, 95 insertions(+), 45 deletions(-)
net/vmw_vsock/virtio_transport_common.c +95 -45
@@ -1774,81 +1774,94 @@ 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 void +virtio_transport_recv_pkt_init_addrs (struct sk_buff *skb, +struct sockaddr_vm *src, +struct sockaddr_vm *dst) {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), +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), +vsock_addr_init (dst,le64_to_cpu (hdr->dst_cid),le32_to_cpu (hdr->dst_port)); +} + +static void +virtio_transport_trace_recv_pkt (struct sk_buff *skb, +const struct sockaddr_vm *src, +const struct sockaddr_vm *dst) +{ +struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb); -trace_virtio_transport_recv_pkt (src.svm_cid, src.svm_port, - dst.svm_cid, dst.svm_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; - } +static struct sock * +virtio_transport_recv_pkt_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 sock *sk; -/* 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; - } - } +if (!virtio_transport_valid_type (le16_to_cpu (hdr->type))) +return NULL ; + + 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); +struct virtio_transport_rx_pkt_ctx { +struct net *net; +const struct sockaddr_vm *src; +const struct sockaddr_vm *dst; +}; -lock_sock (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, +const struct virtio_transport_rx_pkt_ctx *ctx) +{ +struct vsock_sock *vsk =vsock_sk (sk); +bool space_available; -/* Check if sk has been closed or assigned to another transport before -* lock_sock (note: listener sockets are not assigned to any transport) +/* Check after acquiring the socket lock. 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; + !vsock_check_source (vsk, &t->transport, ctx->src))) { + (void )virtio_transport_reset_no_sock (t, skb, ctx->net); +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 = ctx->dst->svm_cid;if (space_available) sk->sk_write_space (sk);
@@ -1870,17 +1883,54 @@ 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, ctx->net);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 sockaddr_vm src, dst; +struct sock *sk; +struct virtio_transport_rx_pkt_ctx ctx; +bool free_pkt; + +virtio_transport_recv_pkt_init_addrs (skb, &src, &dst); +virtio_transport_trace_recv_pkt (skb, &src, &dst); + + sk =virtio_transport_recv_pkt_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); + ctx = (struct virtio_transport_rx_pkt_ctx ) { + .net = net, + .src = &src, + .dst = &dst, + }; + free_pkt =virtio_transport_recv_pkt_locked (t, skb, sk, &ctx);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:
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
From: Jia Jia <[email protected]>
Split
virtio_transport_recv_pkt()so socket lookup is separate from the receive state machine that runs under the socket lock.Keep
virtio_transport_recv_pkt()on its existing per-packet locking path. Pass the source and destination addresses used for lookup to the locked receive path so they are decoded only once.Pass the network namespace and decoded address tuple to the locked receive path through a packet context so later extensions remain cleanly encapsulated.
Quoted 5 lines
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so a later patch can retain it while processing a bounded run of packets under one socket lock.
Signed-off-by Jia Jia <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jakub Kicinski
maintainerQuoted 2 lines · On Fri, 2 Oct 2026 07:45:47 +0000 [email protected] wrote:
Split
virtio_transport_recv_pkt()so socket lookup is separate from the receive state machine that runs under the socket lock.
Please add a cover letter to the series and repost correctly threaded.
Jakub Kicinski
maintainerQuoted 5 lines · On Thu, 8 Oct 2026 10:03:52 -0700 Jakub Kicinski wrote:
Quoted 2 lines · On Fri, 2 Oct 2026 07:45:47 +0000 [email protected] wrote:
Split
virtio_transport_recv_pkt()so socket lookup is separate from the receive state machine that runs under the socket lock.Please add a cover letter to the series and repost correctly threaded.
Ugh, I see the cover letter now, just thread things correctly when you repost, please.
Quoted 10 lines · On Thu, 8 Oct 2026 10:03:52 -0700 Jakub Kicinski wrote:
Quoted 5 lines · On Fri, 2 Oct 2026 07:45:47 +0000 [email protected] wrote:
Quoted 2 lines
Split
virtio_transport_recv_pkt()so socket lookup is separate from the receive state machine that runs under the socket lock.Please add a cover letter to the series and repost correctly threaded.
Ugh, I see the cover letter now, just thread things correctly when you repost, please.
Thanks for the note. I will thread v2 correctly.
I am still debugging a conflict with eBPF sockmap and rerunning the performance tests. I will send v2 after that.