vsock/virtio: reduce RX per-packet socket overhead
Subsystems (7)
v2 cover vsock/virtio: reduce RX per-packet socket overhead
This series reduces per-packet socket overhead in the virtio-vsock RX worker.
Patch 1: separate socket lookup from locked packet processing.
Patch 2: keep one socket lock across a bounded run of established STREAM/RW packets for the same native socket.
Patch 3: reuse the locked socket for later packets with the same address tuple.
Patch 4: coalesce default write-space notifications while that lock is held.
Patch 5: defer the default readable callback until after the batch unlocks.
Performance:
Tested on a single-stream vsock connection (one Guest receiver thread on CPU0, virtio-vsock IRQ on Guest CPU1, fresh QEMU Guest per state, 16 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 fixed-64K table uses a 64K buffer; the matched table uses the sender's request size. 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().
Latency uses a custom user-space vsock ping-pong tool.
The measurements below were collected for the RX batching series.
This benchmark does not run vsock_perf on both ends. Instead, it uses vsock_perf as the sender on the host (vhost-vsock) side and a custom blocking recv() program without poll() on the Guest side, with the receiver thread pinned to Guest CPU0 (and the virtio-vsock RX worker pinned to Guest CPU1). Plain blocking recv() combined with strict core pinning gives audit-level determinism for tracing a single-threaded TID.
I think it is necessary to do further testing on the large I/O throughput peak observed at 4K to find the root cause (and rule out anything introduced by the measurement method). I used kprobe and temporary trace_printk in the source for mechanism diagnostics. The diagnostic data shows that lock wait time and contention at 4K both decrease strictly monotonically as payload size increases;
wire-level tracing also confirms that 4K is split into two packets, and that all sizes can reach the 64K batching limit.
The nature of the prominent 4K throughput gain most likely lies in it sitting exactly at the end-to-end pipeline sweet spot between host and guest:
Compared with 1K and smaller payloads: 4K halves the packet intensity, so upstream host-side syscall and interrupt injection overhead is no longer the bottleneck across the whole path, allowing the host to supply data adequately.
Compared with larger 8K to 64K payloads: 4K keeps the payload size moderate, and combined with the observed syscall reduction and cycle improvement, the batch-draining benefit from notification coalescing is fully realized.
As payload size increases further from 8K to 64K, the overall gain shrinks smoothly, consistent with Amdahl's law, because system execution time shifts toward page copying and VQ processing.
With all patches applied:
End-to-end RX throughput (64K Guest recv() size, SO_RCVLOWAT=1; Gbit/s):
payload baseline RX PATCH RX throughput change (95% CI)
(Gbit/s) (Gbit/s) 64B 0.1065 0.2503 +135.00% [+130.35%, +139.74%] 256B 0.5722 1.0286 +79.76% [+75.40%, +84.23%] 512B 1.1464 1.9779 +72.53% [+69.50%, +75.63%] 1K 2.2714 3.6368 +60.12% [+56.92%, +63.37%] 4K 3.9095 7.7160 +97.37% [+92.09%, +102.79%] 8K 3.3436 5.3111 +58.84% [+53.49%, +64.38%] 16K 3.8876 5.7456 +47.80% [+45.45%, +50.18%] 64K 4.0056 5.3590 +33.79% [+29.37%, +38.36%]
Fixed-syscall stress test (receiver recv() size = sender write size; Gbit/s):
payload baseline PATCH throughput change (95% CI) recv()
RX RX (base/patch)
(Gbit/s) (Gbit/s)
64B 0.1259 0.2651 +110.52% [+99.08%, +122.62%] 1048577/1048577 256B 0.3790 0.9444 +149.20% [+134.70%, +164.60%] 1048577/1048577 512B 0.7768 1.7009 +118.97% [+103.94%, +135.11%] 1048577/1048577 1K 1.4886 2.9477 +98.02% [+84.82%, +112.17%] 1048577/1048577 4K 2.1129 4.7331 +124.01% [+115.40%, +132.96%] 537436/525524 8K 2.8698 5.3683 +87.06% [+80.96%, +93.37%] 276448/264109 16K 3.5156 5.8463 +66.30% [+63.10%, +69.55%] 147849/132267 64K 4.2459 5.8576 +37.96% [+35.26%, +40.71%] 48447/33144
Request-response latency (Ping-Pong RTT, 10,000 requests):
payload baseline mean RTT (us) PATCH mean RTT (us) change (95% CI)
64B 221.968 218.144 -1.72%
[-4.23%, +0.85%]
256B 126.968 124.541 -1.91%
[-3.35%, -0.45%]
4K 130.362 126.664 -2.84%
[-5.12%, -0.50%]Diagnostic Guest CPU1 system-wide cycles (fixed-64K receiver, with perf):
Buffer size baseline cycles/B PATCH cycles/B cycles change (95% CI)
64B 62.032 52.370 -15.58% [-17.16%, -13.96%] 256B 15.287 13.630 -10.84% [-12.51%, -9.14%] 512B 7.306 6.611 -9.51% [-10.92%, -8.08%] 1K 3.811 3.458 -9.25% [-10.96%, -7.50%] 4K 1.850 1.669 -9.75% [-11.78%, -7.67%] 8K 1.504 1.363 -9.36% [-12.97%, -5.60%] 16K 1.321 1.210 -8.37% [-10.61%, -6.07%] 64K 1.342 1.227 -8.57% [-10.87%, -6.21%]
Patch-scope measurements and attribution:
The series was tested cumulatively in two ranges, using the same single-stream setup and paired AB/BA runs. The table below shows results from patches 1–3 only, before the notification changes in patches 4–5.
payload baseline RX PATCH RX throughput change (95% CI)
(Gbit/s) (Gbit/s) 64B 0.1211 0.2534 +109.26% [+106.34%, +112.23%] 256B 0.5671 0.9989 +76.15% [+70.63%, +81.85%] 512B 1.1334 1.9000 +67.64% [+64.64%, +70.69%] 1K 2.2650 3.5610 +57.22% [+50.91%, +63.79%] 4K 3.8331 7.2888 +90.15% [+83.46%, +97.09%] 8K 4.8244 7.6636 +58.85% [+53.71%, +64.16%] 16K 5.4979 8.2469 +50.00% [+45.57%, +54.57%] 64K 3.8875 4.7588 +22.41% [+17.77%, +27.24%]
Fixed-syscall stress test (receiver recv() size = sender write size; no perf; Gbit/s):
payload baseline PATCH throughput change (95% CI) recv()
RX RX (base/patch)
(Gbit/s) (Gbit/s)
64B 0.1397 0.2646 +89.40% [+80.92%, +98.27%] 1048577/1048577 256B 0.3960 0.9516 +140.34% [+117.48%, +165.60%] 1048577/1048577 512B 0.7541 1.6496 +118.74% [+101.42%, +137.55%] 1048577/1048577 1K 1.4903 2.8793 +93.21% [+77.06%, +110.82%] 1048577/1048577 4K 2.9466 6.2284 +111.37% [+97.52%, +126.20%] 538192/525220 8K 4.0043 6.9249 +72.94% [+60.18%, +86.71%] 276313/263852 16K 4.8981 7.5599 +54.34% [+49.77%, +59.06%] 147576/131600 64K 5.9294 7.7897 +31.37% [+27.18%, +35.70%] 50207/33014
Ping-Pong RTT (10,000 requests; no perf):
payload baseline mean RTT (us) PATCH mean RTT (us) change (95% CI)
64B 119.688 119.739 +0.04%
[-2.61%, +2.77%]
256B 120.603 124.485 +3.22%
[-0.44%, +7.01%]
4K 121.921 122.491 +0.47%
[-3.51%, +4.61%]Guest CPU1 system-wide aggregate cycles (fixed-64K receiver, with perf; cycles/byte):
Buffer size baseline cycles/B PATCH cycles/B cycles change (95% CI)
64B 66.838 60.012 -10.21% [-13.03%, -7.30%] 256B 13.446 12.305 -8.49% [-11.23%, -5.66%] 512B 6.833 6.317 -7.54% [ -9.52%, -5.52%] 1K 3.558 3.434 -3.48% [ -6.02%, -0.87%] 4K 1.806 1.686 -6.64% [ -8.69%, -4.54%] 8K 1.457 1.403 -3.73% [ -5.64%, -1.78%] 16K 1.295 1.279 -1.24% [ -5.21%, +2.89%] 64K 1.214 1.198 -1.30% [ -4.06%, +1.53%]
The split-patch tests show that PATCH 2 and PATCH 3 mainly contribute to the improvement in I/O Throughput, while PATCH 4 and PATCH 5 mainly contribute to the improvements in cycles and RTT latency (although PATCH 2-3 show no obvious regression here).
---
v2:
- stop a batch after it consumes the last skb metadata slot, sharing the admission predicate with virtio_transport_inc_rx_pkt()
- move replacement-callback handoff into vsock, process queued skbs one at a time, and trigger the handoff from vsock_bpf_update_proto() on attach and detach
- reduce the packet-context diff in the locked receive helper
- keep RX batch counters in the batch state and reset them in the finish path
- use the vsock/virtio subject prefix and add the requested measurements
v1: https://lore.kernel.org/virtualization/[email protected]/
Jia Jia (5):
vsock/virtio: split socket lookup from locked RX processing vsock/virtio: amortize RX socket locking for stream packets vsock/virtio: reuse same-flow socket lookup in RX batches vsock/virtio: coalesce RX write-space notifications in lock batches vsock/virtio: defer RX readable notifications until batch unlock
include/linux/virtio_vsock.h | 16 ++ include/net/af_vsock.h | 8 + net/vmw_vsock/af_vsock.c | 104 +++++++++ net/vmw_vsock/virtio_transport.c | 29 ++- net/vmw_vsock/virtio_transport_common.c | 389 +++++++++++++++++++++++++++----- net/vmw_vsock/vsock_bpf.c | 2 + 6 files changed, 496 insertions(+), 52 deletions(-)
base-commit: 8df0638138d3e0344fd1fb36cf2d1ca1cf5028f0
Signature · 1 line
2.53.0
PATCH v2 1/5 vsock/virtio: split socket lookup from locked RX processing
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 through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
--- net/vmw_vsock/virtio_transport_common.c | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-)
net/vmw_vsock/virtio_transport_common.c +96 -43
@@ -1774,81 +1774,97 @@ 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) +{ +const struct sockaddr_vm *src = ctx->src; +const struct sockaddr_vm *dst = ctx->dst; +struct vsock_sock *vsk =vsock_sk (sk); +struct net *net = ctx->net; +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))) { + !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; +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 = dst->svm_cid;if (space_available) sk->sk_write_space (sk);
@@ -1875,12 +1891,49 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,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_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +struct sock *sk; +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.34.1
PATCH v2 2/5 vsock/virtio: amortize RX socket locking for stream packets
In reply to Jia JiaThe virtio-vsock RX worker currently acquires and releases the socket lock for every received packet. Keep the lock held while processing a bounded run of packets that belongs to the same socket.
Only ordinary RW packets for an established STREAM socket whose transport and protocol remain unchanged are eligible. Control packets, SEQPACKET traffic, state or transport changes, malformed packets, and sockets whose protocol has been replaced end the active batch and retain their existing handling.
Serialize the initial eligibility check with sk_callback_lock because sockmap removal restores the protocol without taking the socket lock.
Limit each batch to 64 packets or 64KB of payload, bounding the work under one socket lock. Keep the lookup reference until the batch finishes.
Keep a batch only while another skb metadata charge fits in the receive buffer. Share this predicate with virtio_transport_inc_rx_pkt(). When an enqueued skb consumes the final metadata slot, finish the batch and release the socket lock before processing another packet, restoring the reader's scheduling opportunity from the per-packet path.
The virtqueue callback remains a queue_work() callback. Batching runs only in the process-context RX worker.
Introduce virtio_transport_rx_batch_finish() to tear down the batch and release the socket lock and lookup reference.
--- include/linux/virtio_vsock.h | 11 ++ net/vmw_vsock/virtio_transport.c | 29 ++++- net/vmw_vsock/virtio_transport_common.c | 147 +++++++++++++++++++++++- 3 files changed, 183 insertions(+), 4 deletions(-)
include/linux/virtio_vsock.h +11 -0
@@ -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);
net/vmw_vsock/virtio_transport.c +28 -1
@@ -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:
net/vmw_vsock/virtio_transport_common.c +144 -3
189 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 3/5 vsock/virtio: reuse same-flow socket lookup in RX batches
In reply to Jia JiaAn 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.
--- include/linux/virtio_vsock.h | 3 ++ net/vmw_vsock/virtio_transport_common.c | 58 +++++++++++++++---------- 2 files changed, 38 insertions(+), 23 deletions(-)
include/linux/virtio_vsock.h +3 -0
@@ -287,6 +287,9 @@ struct virtio_transport_rx_batch {struct sock *sk;unsigned int pkts;size_t bytes; +struct net *net; +struct sockaddr_vm src; +struct sockaddr_vm dst; }; void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
net/vmw_vsock/virtio_transport_common.c +35 -23
86 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 4/5 vsock/virtio: coalesce RX write-space notifications in lock batches
In reply to Jia JiaEach received packet updates peer credit and calls sk_write_space() when send space is available. A writer cannot use the newly advertised credit until the socket lock is released, so repeated callbacks within one lock batch cannot let it make progress sooner.
For the callback installed by sock_init_data(), record one pending write-space notification and deliver it through the saved default callback before release_sock(). If the callback has been replaced by batch finish, invoke the replacement as well. This keeps the native writer wakeup from being lost across a sockmap callback change while still notifying sockmap.
Save the initial sock_def_write_space() callback when the AF_VSOCK socket is created because it is not visible to virtio_transport_common when built as a module. The fast path uses READ_ONCE() and adds no callback lock.
Set the batch socket before processing its first packet so a batch ending on that packet cannot lose the notification.
Packets outside the eligible STREAM/RW batch path retain per-packet notification behavior. The 64-packet and 64K limits cap the packets whose notifications can be coalesced.
--- include/linux/virtio_vsock.h | 1 + include/net/af_vsock.h | 2 ++ net/vmw_vsock/af_vsock.c | 1 + net/vmw_vsock/virtio_transport_common.c | 42 ++++++++++++++++++++++--- 4 files changed, 41 insertions(+), 5 deletions(-)
include/linux/virtio_vsock.h +1 -0
@@ -290,6 +290,7 @@ struct virtio_transport_rx_batch {struct net *net;struct sockaddr_vm src;struct sockaddr_vm dst; +bool write_space_pending; }; void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
include/net/af_vsock.h +2 -0
@@ -63,6 +63,8 @@ struct vsock_sock {u32 peer_shutdown;bool sent_request;bool ignore_connecting_rst; +/* Initial callback, used to identify replacements. */ +void (*default_write_space)(struct sock *sk);/* Protected by lock_sock(sk) */ u64 buffer_size;
net/vmw_vsock/af_vsock.c +1 -0
@@ -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 );
net/vmw_vsock/virtio_transport_common.c +37 -5
102 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 5/5 vsock/virtio: defer RX readable notifications until batch unlock
In reply to Jia JiaAn RX batch can make a socket readable while the worker still owns its socket lock. Waking a reader at that point can make it immediately wait for the same lock.
For the callback installed by sock_init_data(), record when the existing low-watermark condition is met and issue one notification after the batch releases the lock. Save the initial sock_def_readable() callback when the AF_VSOCK socket is created because virtio_transport_common can be built as a module and cannot refer to that unexported symbol directly.
When sk_data_ready or sk_write_space no longer matches that initial callback, pass queued skbs to the current callback one skb at a time. The handoff stops if the callback is the default again or the queue does not shrink. After 64 successful callbacks, queue another bounded handoff if data remains. vsock_bpf_update_proto() schedules the same handoff so an attach or detach outside the worker still drains skbs already queued.
Always deliver a coalesced write-space event to the saved default callback before sampling the callback state. A current replacement is invoked by the handoff as well, so a callback change cannot hide the native writer wakeup or pending sockmap work.
Sampling at batch finish takes sk_callback_lock once. The per-packet fast path still uses READ_ONCE() and does not take that lock. Control packets, state transitions, transport changes and non-batched sockets keep their existing behavior.
--- include/linux/virtio_vsock.h | 1 + include/net/af_vsock.h | 8 +- net/vmw_vsock/af_vsock.c | 103 ++++++++++++++++++++++++ net/vmw_vsock/virtio_transport_common.c | 73 ++++++++++++++--- net/vmw_vsock/vsock_bpf.c | 2 + 5 files changed, 174 insertions(+), 13 deletions(-)
include/linux/virtio_vsock.h +1 -0
@@ -291,6 +291,7 @@ struct virtio_transport_rx_batch {struct sockaddr_vm src;struct sockaddr_vm dst;bool write_space_pending; +bool data_ready_pending; }; void virtio_transport_recv_pkt_batch(struct virtio_transport *t,
include/net/af_vsock.h +7 -1
@@ -63,8 +63,11 @@ struct vsock_sock {u32 peer_shutdown;bool sent_request;bool ignore_connecting_rst; -/* Initial callback, used to identify replacements. */ +/* Initial callbacks, used to identify replacements. */ +void (*default_data_ready)(struct sock *sk);void (*default_write_space)(struct sock *sk); +struct delayed_work rx_cb_work; +bool rx_cb_retried;/* Protected by lock_sock(sk) */ u64 buffer_size;
@@ -80,6 +83,9 @@ s64 vsock_stream_has_data(struct vsock_sock *vsk);s64 vsock_stream_has_space (struct vsock_sock *vsk);struct sock *vsock_create_connected (struct sock *parent);void vsock_data_ready (struct sock *sk); +bool vsock_rx_cb_is_native (struct sock *sk); +void vsock_rx_callback_kick (struct sock *sk); +void vsock_rx_callback_handoff (struct sock *sk);/**** TRANSPORT ****/
net/vmw_vsock/af_vsock.c +103 -0
121 lines not on this page · raw message
net/vmw_vsock/virtio_transport_common.c +61 -12
163 lines not on this page · raw message
net/vmw_vsock/vsock_bpf.c +2 -0
@@ -154,6 +154,7 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restoreif (restore) { sk->sk_write_space = psock->saved_write_space;sock_replace_proto (sk, psock->sk_proto); +vsock_rx_callback_kick (sk);return 0 ; }
@@ -166,6 +167,7 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restorevsock_bpf_check_needs_rebuild (psock->sk_proto);sock_replace_proto (sk, &vsock_bpf_prot); +vsock_rx_callback_kick (sk);return 0 ; }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Jia JiaSashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
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.
Quoted 8 lines
Pass the network namespace and decoded address tuple through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
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
[email protected]
AI reviewRe: [PATCH net-next v2 3/5] vsock/virtio: reuse same-flow socket lookup in RX batches
In reply to Jia JiaSashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
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.
Quoted 6 lines
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 <[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=3
[email protected]
AI reviewRe: [PATCH net-next v2 5/5] vsock/virtio: defer RX readable notifications until batch unlock
In reply to Jia JiaSashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines
An RX batch can make a socket readable while the worker still owns its socket lock. Waking a reader at that point can make it immediately wait for the same lock.
For the callback installed by
sock_init_data(), record when the existing low-watermark condition is met and issue one notification after the batch releases the lock. Save the initialsock_def_readable()callback when theAF_VSOCKsocket is created becausevirtio_transport_commoncan be built as a module and cannot refer to that unexported symbol directly.When
sk_data_readyorsk_write_spaceno longer matches that initial callback, pass queued skbs to the current callback one skb at a time. The handoff stops if the callback is the default again or the queue does not shrink. After 64 successful callbacks, queue another bounded handoff if data remains.vsock_bpf_update_proto()schedules the same handoff so an attach or detach outside the worker still drains skbs already queued.Always deliver a coalesced write-space event to the saved default callback before sampling the callback state. A current replacement is invoked by the handoff as well, so a callback change cannot hide the native writer wakeup or pending sockmap work.
Quoted 6 lines
Sampling at batch finish takes
sk_callback_lockonce. The per-packet fast path still usesREAD_ONCE()and does not take that lock. Control packets, state transitions, transport changes and non-batched sockets keep their existing behavior.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=5
[email protected]
AI reviewRe: [PATCH net-next v2 2/5] vsock/virtio: amortize RX socket locking for stream packets
In reply to Jia JiaSashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines
The virtio-vsock RX worker currently acquires and releases the socket lock for every received packet. Keep the lock held while processing a bounded run of packets that belongs to the same socket.
Only ordinary RW packets for an established STREAM socket whose transport and protocol remain unchanged are eligible. Control packets, SEQPACKET traffic, state or transport changes, malformed packets, and sockets whose protocol has been replaced end the active batch and retain their existing handling.
Serialize the initial eligibility check with
sk_callback_lockbecause sockmap removal restores the protocol without taking the socket lock.Limit each batch to 64 packets or 64KB of payload, bounding the work under one socket lock. Keep the lookup reference until the batch finishes.
Keep a batch only while another skb metadata charge fits in the receive buffer. Share this predicate with
virtio_transport_inc_rx_pkt(). When an enqueued skb consumes the final metadata slot, finish the batch and release the socket lock before processing another packet, restoring the reader's scheduling opportunity from the per-packet path.
Quoted 7 lines
The virtqueue callback remains a
queue_work()callback. Batching runs only in the process-context RX worker.Introduce
virtio_transport_rx_batch_finish()to tear down the batch and release the socket lock and lookup reference.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=2
[email protected]
AI reviewRe: [PATCH net-next v2 4/5] vsock/virtio: coalesce RX write-space notifications in lock batches
In reply to Jia JiaSashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
Each received packet updates peer credit and calls
sk_write_space()when send space is available. A writer cannot use the newly advertised credit until the socket lock is released, so repeated callbacks within one lock batch cannot let it make progress sooner.For the callback installed by
sock_init_data(), record one pending write-space notification and deliver it through the saved default callback beforerelease_sock(). If the callback has been replaced by batch finish, invoke the replacement as well. This keeps the native writer wakeup from being lost across a sockmap callback change while still notifying sockmap.Save the initial
sock_def_write_space()callback when theAF_VSOCKsocket is created because it is not visible tovirtio_transport_commonwhen built as a module. The fast path usesREAD_ONCE()and adds no callback lock.
Quoted 8 lines
Set the batch socket before processing its first packet so a batch ending on that packet cannot lose the notification.
Packets outside the eligible STREAM/RW batch path retain per-packet notification behavior. The 64-packet and 64K limits cap the packets whose notifications can be coalesced.
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=4
[email protected]
AI reviewRe: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Jia JiaThank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] Commit message concern: the message for "vsock/virtio: split socket lookup from locked RX processing" lists what the refactor does…
--
vsock/virtio: split socket lookup from locked RX processing
This commit splits virtio_transport_recv_pkt() into two helpers. One does the socket lookup. The other runs the receive state machine under the socket lock. The decoded source and destination addresses and the network namespace reach the locked helper through a new struct virtio_transport_rx_pkt_ctx.
Quoted 3 lines
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.
[Severity: Low]
This isn't a bug, but does "decoded only once" describe this patch on its own? The old virtio_transport_recv_pkt() also decoded src and dst once with vsock_addr_init(). It then reused them for the lookup and for vsock_check_source().
Quoted 3 lines
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
In this patch, virtio_transport_recv_pkt_locked() doesn't touch either reference. The caller still calls skb_set_owner_sk_safe(), release_sock(), sock_put() and kfree_skb() in the same order as before:
free_pkt = virtio_transport_recv_pkt_locked(t, skb, sk, &ctx);
release_sock(sk);
...
sock_put(sk);
if (free_pkt)
kfree_skb(skb);Also, struct virtio_transport_rx_pkt_ctx has only one user here.
These statements seem to describe the next patch in the series, "vsock/virtio: amortize RX socket locking for stream packets". That patch adds virtio_transport_recv_pkt_batch() as a second caller of virtio_transport_recv_pkt_find_socket() and virtio_transport_recv_pkt_locked().
It also adds batchable, batch and defer_data_ready to the ctx struct. It keeps the lookup reference for the life of the batch and takes the skb owner reference per packet.
The diff reorders one thing: on a type mismatch, sock_put() now runs before virtio_transport_reset_no_sock(). This has no effect, because virtio_transport_reset_no_sock() uses skb->sk and not the looked-up sk.
Could the commit message say that this is preparation for the batched RX work, and add a "No functional change intended" line?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261010142247.99223-1-physicalmtea%40gmail.com
[email protected]
AI reviewRe: [PATCH net-next v2 2/5] vsock/virtio: amortize RX socket locking for stream packets
In reply to Jia JiaThank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The patch is presented purely as a performance optimisation ("amortize RX socket locking") but gives no throughput, latency or CPU…
- [Low] Removing the local skb_overhead declaration from virtio_transport_inc_rx_pkt()…
--
vsock/virtio: amortize RX socket locking for stream packets
This commit keeps the socket lock held in the virtio-vsock RX worker while it processes a bounded run of RW packets for the same established STREAM socket. A batch is limited to 64 packets or 64KB of payload. The commit adds the exported helpers virtio_transport_recv_pkt_batch() and virtio_transport_rx_batch_finish(), and virtio_transport_inc_rx_pkt() now shares the skb metadata headroom check.
Quoted 5 lines
net/vmw_vsock/virtio_transport.c +0
-0
net/vmw_vsock/virtio_transport.c virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) · line 637
Quoted 9 lines
net/vmw_vsock/virtio_transport.c +6
-0@@ -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)
[Severity: Low]
This isn't a bug, but are there throughput, latency or CPU numbers for this change? Which workload does it help?
The commit is presented only as a performance optimization. To get that benefit it adds a new struct in include/linux/virtio_vsock.h, two exported symbols, and new rules for when a packet can join a batch.
It also keeps lock_sock() held across iterations of the loop in virtio_transport_rx_work(). As a result, recvmsg, sendmsg, setsockopt or close on the same socket can now wait for up to 64 packets / 64KB instead of one packet.
In this patch, the batched path in virtio_transport_recv_pkt_batch() still does the lookup and refcount work for every packet:
sk = virtio_transport_recv_pkt_find_socket(skb, &src, &dst, net);
...
if (!skb_set_owner_sk_safe(skb, sk)) {
...
if (batch->sk == sk) {
/* Keep the batch reference; drop this packet's lookup reference. */
sock_put(sk);virtio_transport_recv_pkt_find_socket() takes the global vsock_table_lock and does a sock_hold(). That means only the lock_sock()/release_sock() pair is saved.
sk_data_ready() also still runs while the lock is held. A reader it wakes can therefore go back to sleep on sk_lock.wq until the batch ends.
Two later commits in the series remove those costs: "vsock/virtio:
reuse same-flow socket lookup in RX batches" and "vsock/virtio: defer RX readable notifications until batch unlock". Even so, no commit message in the series has numbers showing that the change is a net win.
Could benchmark data be added to the commit message?
[ ... ]
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +0
-0@@ -576,10 +576,16 @@ virtio_transport_collapse_rx_queue(struct virtio_vsock_sock *vvs,
[ ... ]
net/vmw_vsock/virtio_transport_common.c virtio_transport_collapse_rx_queue(struct virtio_vsock_sock *vvs, · line 590
Quoted 6 lines
static bool virtio_transport_inc_rx_pkt(struct
virtio_vsock_sock*vvs,net/vmw_vsock/virtio_transport_common.c +0
-1u32 len) { -u64 skb_overhead = (skb_queue_len (&vvs->rx_queue) +1 ) *SKB_TRUESIZE (0 );/* Allow at most buf_alloc * 2 total budget (payload + overhead),
[Severity: Low]
This isn't a bug, but removing skb_overhead leaves a blank line right after the opening brace of virtio_transport_inc_rx_pkt().
checkpatch --strict warns about this ("Blank lines aren't necessary after an open brace"). The blank line is still there at the end of the series.
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261010142247.99223-1-physicalmtea%40gmail.com
[email protected]
AI reviewRe: [PATCH net-next v2 5/5] vsock/virtio: defer RX readable notifications until batch unlock
In reply to Jia JiaThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 (1) · Low: 2
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The new attach-time handoff passes partially read skbs to read_skb, which ignores the read offset.
- [Medium] Sockmap attach/detach now drains queued data on every vsock socket, and the commit message wrongly says non-batched sockets keep…
- [Low] The commit message says "Sampling at batch finish takes sk_callback_lock once." That holds only on the native path.
- [Low] vsock_rx_callback_handoff() calls sk_data_ready without the sk_rcvlowat/SOCK_DONE check that vsock_data_ready() and the per-packet…
Pre-existing issues:
- [Medium] vsock_rx_callback_handoff() reads sk_data_ready under read_lock_bh(&sk->sk_callback_lock), releases the lock, and only then…
--
vsock/virtio: defer RX readable notifications until batch unlock
During virtio vsock RX batches, this commit holds back the native sock_def_readable() notification until the batch has dropped the socket lock. If sk_data_ready or sk_write_space has been replaced, for example by sockmap, a new vsock_rx_callback_handoff() passes queued skbs to the current callback one at a time. vsock_bpf_update_proto() now schedules that handoff on sockmap attach and detach.
Quoted 5 lines
net/vmw_vsock/af_vsock.c +0
-0@@ -931,6 +931,107 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
[ ... ]
Quoted 29 lines
net/vmw_vsock/af_vsock.c +29
-0+/* Caller holds lock_sock(). */ +void vsock_rx_callback_handoff (struct sock *sk) +{ +struct vsock_sock *vsk =vsock_sk (sk); +void (*write_space)(struct sock *sk); +void (*data_ready)(struct sock *sk); +bool wrote = false; +s64 before, after; +int i; + +if (sock_flag (sk,SOCK_DEAD ) || !vsk->transport) +return ; + +read_lock_bh (&sk->sk_callback_lock); + data_ready =READ_ONCE (sk->sk_data_ready); +if (READ_ONCE (sk->sk_prot) != sk->sk_prot_creator && + data_ready == vsk->default_data_ready) { +read_unlock_bh (&sk->sk_callback_lock); +/* +* start_verdict publishes sk_data_ready after the proto swap. +* Retry once before treating this as a stable non-RX setup. +*/ +if (!vsk->rx_cb_retried) { + vsk->rx_cb_retried = true; +vsock_rx_callback_queue (sk,1 ); +return ; + } + vsk->rx_cb_retried = false; +data_ready (sk);
net/vmw_vsock/af_vsock.c static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) · line 999
Quoted 10 lines
net/vmw_vsock/af_vsock.c +10
-0+return ; + } +read_unlock_bh (&sk->sk_callback_lock); + vsk->rx_cb_retried = false; + +for (i =0 ; i <VSOCK_RX_CB_HANDOFF_MAX ; i++) { +read_lock_bh (&sk->sk_callback_lock); + data_ready =READ_ONCE (sk->sk_data_ready); + write_space =READ_ONCE (sk->sk_write_space); +read_unlock_bh (&sk->sk_callback_lock);
[Severity: Medium]
This is a pre-existing issue, but sk_callback_lock here only covers reading the pointers, not calling them. Can a concurrent sockmap delete restore sk_data_ready and clear the psock between this read_unlock_bh() and the data_ready(sk) call below?
The delete path takes only write_lock_bh(&sk->sk_callback_lock), not lock_sock():
__sock_map_delete()
sock_map_unref()
sock_map_del_link() / sk_psock_drop()
In that case the stale sk_psock_verdict_data_ready() still dequeues an skb through virtio_transport_read_skb(). That lowers rx_bytes and sends a credit update. sk_psock_verdict_recv() then finds no psock:
net/core/skmsg.c:sk_psock_verdict_recv() {
...
if (unlikely(!psock)) {
len = 0;
tcp_eat_skb(sk, skb);
sock_drop(sk, skb);
goto out;
}
...
}So the skb is dropped after it has already been credited to the peer.
The baseline vsock_data_ready() has the same window. It calls sk->sk_data_ready(sk) under lock_sock() without sk_callback_lock, and tcp_data_ready() has the same issue. This patch adds more call sites with the same pattern: the attach-kicked work item and the batch-finish handoff. Taking the read lock here can also make it look as if the call is protected when it is not.
net/vmw_vsock/af_vsock.c static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) · line 1012
Quoted 13 lines
net/vmw_vsock/af_vsock.c +13
-0+ +if (!wrote && write_space != vsk->default_write_space) { +write_space (sk); + wrote = true; + } +if (data_ready == vsk->default_data_ready) { +data_ready (sk); +break ; + } + before =vsock_stream_has_data (vsk); +if (before <=0 ) +break ; +data_ready (sk);
[Severity: Low]
Neither of these data_ready(sk) calls checks sk->sk_rcvlowat or SOCK_DONE, unlike vsock_data_ready() and the per-packet path in virtio_transport_recv_connected(). Is that intended?
For a replaced callback such as the sockmap verdict, data_ready(sk) runs whenever vsock_stream_has_data() > 0. The verdict program can then see data below SO_RCVLOWAT that the per-packet path would hold back.
For the default callback the call is unconditional, both here and in the retry branch above:
vsk->rx_cb_retried = false; data_ready(sk); return;
Doesn't this wake readers when no readable event was pending? That happens on every attach and restore kick from vsock_bpf_update_proto(). It also happens when virtio_transport_rx_batch_finish() enters the handoff with only write_space_pending set.
net/vmw_vsock/af_vsock.c static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) · line 1015
Quoted 3 lines
net/vmw_vsock/af_vsock.c +3
-0+ after =vsock_stream_has_data (vsk); +if (after >= before) +break ;
[Severity: High]
Can this hand a partially read skb to read_skb?
A native stream read can consume only part of the head skb. In that case virtio_transport_stream_do_dequeue() advances the private offset, leaves the skb queued, and lowers rx_bytes only by the bytes copied:
VIRTIO_VSOCK_SKB_CB(skb)->offset += bytes; ... virtio_transport_dec_rx_pkt(vvs, bytes, dequeued);
If the socket is then added to a sockmap with a verdict program, the new kick in vsock_bpf_update_proto() runs this loop:
vsock_rx_callback_work()
vsock_rx_callback_handoff()
sk_psock_verdict_data_ready()
vsock_read_skb()
virtio_transport_read_skb()
virtio_transport_read_skb() does not look at the offset:
pkt_len = le32_to_cpu(hdr->len); virtio_transport_dec_rx_pkt(vvs, pkt_len, pkt_len); ... return recv_actor(sk, skb);
Take a 1000 byte skb with 400 bytes already read, so rx_bytes is 600. The verdict program sees all 1000 bytes, 400 of them a second time. The u32 rx_bytes then wraps to 4294966896.
The after >= before check above sees no progress and stops. But vsock_stream_has_data() now reports about 4GB, so poll stays readable. The next native read hits WARN_ONCE("rx_queue is empty, but rx_bytes is non-zero") and returns -EFAULT.
The offset handling in virtio_transport_read_skb() predates this patch. In the baseline the same corruption needs a partial read, then an attach, then another packet from the peer. With this patch the extra packet is no longer needed, and the new attach-time drain depends on read_skb handling already-queued skbs correctly.
Quoted 4 lines
net/vmw_vsock/af_vsock.c +4
-0+if (i ==VSOCK_RX_CB_HANDOFF_MAX -1 && after >0 ) +vsock_rx_callback_queue (sk,0 ); + } +}
[ ... ]
Quoted 4 lines
net/vmw_vsock/virtio_transport_common.c +0
-0
[ ... ]
Quoted 1 line
net/vmw_vsock/virtio_transport_common.c +0
-0@@ -1986,33 +2007,45 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
[ ... ]
Quoted 13 lines
net/vmw_vsock/virtio_transport_common.c +7
-5-if (write_space_pending) { - vsk =vsock_sk (sk); + vsk =vsock_sk (sk); +if (write_space_pending) vsk->default_write_space (sk); - write_space =READ_ONCE (sk->sk_write_space); -if (write_space != vsk->default_write_space) -write_space (sk); + +read_lock_bh (&sk->sk_callback_lock); + native =vsock_rx_cb_is_native (sk); +read_unlock_bh (&sk->sk_callback_lock); +
net/vmw_vsock/virtio_transport_common.c EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); · line 2039
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8
-0+if (native) { +release_sock (sk); +if (data_ready_pending) + vsk->default_data_ready (sk); +sock_put (sk); +return ; } +if (write_space_pending || data_ready_pending) +vsock_rx_callback_handoff (sk);
[Severity: Low]
The commit message says:
Sampling at batch finish takes sk_callback_lock once.
Does that hold on the non-native path? sk_callback_lock is taken once here for vsock_rx_cb_is_native(). vsock_rx_callback_handoff() then takes it again at the top. It takes it once more on each loop iteration, up to VSOCK_RX_CB_HANDOFF_MAX (64) times, and the requeued work can do all of this again.
[ ... ]
Quoted 13 lines
net/vmw_vsock/vsock_bpf.c +1
-0@@ -154,6 +154,7 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restoreif (restore) { sk->sk_write_space = psock->saved_write_space;sock_replace_proto (sk, psock->sk_proto); +vsock_rx_callback_kick (sk);return 0 ; }
net/vmw_vsock/vsock_bpf.c int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore · line 172
Quoted 6 lines
net/vmw_vsock/vsock_bpf.c +1
-0@@ -166,6 +167,7 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restorevsock_bpf_check_needs_rebuild (psock->sk_proto);sock_replace_proto (sk, &vsock_bpf_prot); +vsock_rx_callback_kick (sk);return 0 ; }
[Severity: Medium]
These kicks run for every AF_VSOCK socket added to or removed from a sockmap or sockhash, not only for sockets in a virtio RX batch. Is this change in behaviour intended?
RX batching exists only in the guest virtio transport (virtio_transport_rx_work() -> virtio_transport_recv_pkt_batch()). vhost_vsock and vsock_loopback always use virtio_transport_recv_pkt(). Both still set .read_skb = virtio_transport_read_skb, so their sockets reach this code too. The same is true of SOCK_SEQPACKET sockets, which virtio_transport_recv_pkt_batchable() rejects.
Before this patch, sk_data_ready was not called by either path:
sock_map_link() -> sock_map_init_proto() -> vsock_bpf_update_proto() sk_psock_start_verdict()
Data queued before attach only reached the verdict program when a later packet arrived.
Now the kick pushes that data through sk_psock_verdict_data_ready(), up to 64 skbs per pass, and requeues itself if data remains. The verdict program can redirect or drop data that arrived before attach, where it used to stay for the native reader.
If only the prot was swapped, for example with a msg_parser-only map, the handoff calls the default callback unconditionally after one retry. That adds an extra wakeup on every attach and detach.
The commit message says "non-batched sockets keep their existing behavior". It also says the handoff is scheduled "so an attach or detach outside the worker still drains skbs already queued".
Neither statement seems to fit what the code does: this is a new sockmap behaviour for every vsock transport that has read_skb, and the subject line does not mention it. Could this be split out, or at least described in the commit message?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261010142247.99223-1-physicalmtea%40gmail.com
Re: [PATCH net-next v2 5/5] vsock/virtio: defer RX readable notifications until batch unlock
Quoted 306 lines
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 (1) · Low: 2
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The new attach-time handoff passes partially read skbs to
read_skb, which ignores the read offset.
- [Medium] Sockmap attach/detach now drains queued data on every vsock socket, and the commit message wrongly says non-batched sockets keep…
- [Low] The commit message says "Sampling at batch finish takessk_callback_lockonce." That holds only on the native path.
- [Low]vsock_rx_callback_handoff()callssk_data_readywithout the sk_rcvlowat/SOCK_DONE check thatvsock_data_ready()and the per-packet…Pre-existing issues:
- [Medium]vsock_rx_callback_handoff()readssk_data_readyunderread_lock_bh(&sk->sk_callback_lock), releases the lock, and only then…--
vsock/virtio: defer RX readable notifications until batch unlock
During virtio vsock RX batches, this commit holds back the native
sock_def_readable()notification until the batch has dropped the socket lock. Ifsk_data_readyorsk_write_spacehas been replaced, for example by sockmap, a newvsock_rx_callback_handoff()passes queued skbs to the current callback one at a time.vsock_bpf_update_proto()now schedules that handoff on sockmap attach and detach.Quoted 5 lines
net/vmw_vsock/af_vsock.c +0
-0@@ -931,6 +931,107 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)[ ... ]
Quoted 29 lines
net/vmw_vsock/af_vsock.c +29
-0+/* Caller holds lock_sock(). */ +void vsock_rx_callback_handoff (struct sock *sk) +{ +struct vsock_sock *vsk =vsock_sk (sk); +void (*write_space)(struct sock *sk); +void (*data_ready)(struct sock *sk); +bool wrote = false; +s64 before, after; +int i; + +if (sock_flag (sk,SOCK_DEAD ) || !vsk->transport) +return ; + +read_lock_bh (&sk->sk_callback_lock); + data_ready =READ_ONCE (sk->sk_data_ready); +if (READ_ONCE (sk->sk_prot) != sk->sk_prot_creator && + data_ready == vsk->default_data_ready) { +read_unlock_bh (&sk->sk_callback_lock); +/* +* start_verdict publishes sk_data_ready after the proto swap. +* Retry once before treating this as a stable non-RX setup. +*/ +if (!vsk->rx_cb_retried) { + vsk->rx_cb_retried = true; +vsock_rx_callback_queue (sk,1 ); +return ; + } + vsk->rx_cb_retried = false; +data_ready (sk);net/vmw_vsock/af_vsock.c static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) · line 999
Quoted 10 lines
net/vmw_vsock/af_vsock.c +10
-0+return ; + } +read_unlock_bh (&sk->sk_callback_lock); + vsk->rx_cb_retried = false; + +for (i =0 ; i <VSOCK_RX_CB_HANDOFF_MAX ; i++) { +read_lock_bh (&sk->sk_callback_lock); + data_ready =READ_ONCE (sk->sk_data_ready); + write_space =READ_ONCE (sk->sk_write_space); +read_unlock_bh (&sk->sk_callback_lock);[Severity: Medium]
This is a pre-existing issue, butsk_callback_lockhere only covers reading the pointers, not calling them. Can a concurrent sockmap delete restoresk_data_readyand clear the psock between thisread_unlock_bh()and thedata_ready(sk)call below?The delete path takes only
write_lock_bh(&sk->sk_callback_lock), notlock_sock():
__sock_map_delete()
sock_map_unref()sock_map_del_link() / sk_psock_drop()In that case the stale
sk_psock_verdict_data_ready()still dequeues an skb throughvirtio_transport_read_skb(). That lowersrx_bytesand sends a credit update.sk_psock_verdict_recv()then finds no psock:net/core/skmsg.c:sk_psock_verdict_recv() { ... if (unlikely(!psock)) { len = 0; tcp_eat_skb(sk, skb); sock_drop(sk, skb); goto out; } ... }So the skb is dropped after it has already been credited to the peer.
The baseline
vsock_data_ready()has the same window. It callssk->sk_data_ready(sk)underlock_sock()withoutsk_callback_lock, andtcp_data_ready()has the same issue. This patch adds more call sites with the same pattern: the attach-kicked work item and the batch-finish handoff. Taking the read lock here can also make it look as if the call is protected when it is not.net/vmw_vsock/af_vsock.c static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) · line 1012
Quoted 13 lines
net/vmw_vsock/af_vsock.c +13
-0+ +if (!wrote && write_space != vsk->default_write_space) { +write_space (sk); + wrote = true; + } +if (data_ready == vsk->default_data_ready) { +data_ready (sk); +break ; + } + before =vsock_stream_has_data (vsk); +if (before <=0 ) +break ; +data_ready (sk);[Severity: Low]
Neither of thesedata_ready(sk)calls checkssk->sk_rcvlowatorSOCK_DONE, unlikevsock_data_ready()and the per-packet path invirtio_transport_recv_connected(). Is that intended?For a replaced callback such as the sockmap verdict,
data_ready(sk)runs whenevervsock_stream_has_data()> 0. The verdict program can then see data belowSO_RCVLOWATthat the per-packet path would hold back.For the default callback the call is unconditional, both here and in the retry branch above:
vsk->rx_cb_retried = false; data_ready(sk); return;Doesn't this wake readers when no readable event was pending? That happens on every attach and restore kick from
vsock_bpf_update_proto(). It also happens whenvirtio_transport_rx_batch_finish()enters the handoff with onlywrite_space_pendingset.net/vmw_vsock/af_vsock.c static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) · line 1015
Quoted 3 lines
net/vmw_vsock/af_vsock.c +3
-0+ after =vsock_stream_has_data (vsk); +if (after >= before) +break ;[Severity: High]
Can this hand a partially read skb toread_skb?A native stream read can consume only part of the head skb. In that case
virtio_transport_stream_do_dequeue()advances the private offset, leaves the skb queued, and lowersrx_bytesonly by the bytes copied:VIRTIO_VSOCK_SKB_CB(skb)->offset += bytes; ... virtio_transport_dec_rx_pkt(vvs, bytes, dequeued);If the socket is then added to a sockmap with a verdict program, the new kick in
vsock_bpf_update_proto()runs this loop:
vsock_rx_callback_work()
vsock_rx_callback_handoff()
sk_psock_verdict_data_ready()
vsock_read_skb()
virtio_transport_read_skb()
virtio_transport_read_skb()does not look at the offset:pkt_len = le32_to_cpu(hdr->len); virtio_transport_dec_rx_pkt(vvs, pkt_len, pkt_len); ... return recv_actor(sk, skb);Take a 1000 byte skb with 400 bytes already read, so
rx_bytesis 600. The verdict program sees all 1000 bytes, 400 of them a second time. The u32rx_bytesthen wraps to 4294966896.The after >= before check above sees no progress and stops. But
vsock_stream_has_data()now reports about 4GB, so poll stays readable. The next native read hitsWARN_ONCE("rx_queue is empty, but rx_bytes is non-zero")and returns-EFAULT.The offset handling in
virtio_transport_read_skb()predates this patch. In the baseline the same corruption needs a partial read, then an attach, then another packet from the peer. With this patch the extra packet is no longer needed, and the new attach-time drain depends onread_skbhandling already-queued skbs correctly.Quoted 4 lines
net/vmw_vsock/af_vsock.c +4
-0+if (i ==VSOCK_RX_CB_HANDOFF_MAX -1 && after >0 ) +vsock_rx_callback_queue (sk,0 ); + } +}[ ... ]
Quoted 4 lines
net/vmw_vsock/virtio_transport_common.c +0
-0[ ... ]
Quoted 1 line
net/vmw_vsock/virtio_transport_common.c +0
-0@@ -1986,33 +2007,45 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);[ ... ]
Quoted 13 lines
net/vmw_vsock/virtio_transport_common.c +7
-5-if (write_space_pending) { - vsk =vsock_sk (sk); + vsk =vsock_sk (sk); +if (write_space_pending) vsk->default_write_space (sk); - write_space =READ_ONCE (sk->sk_write_space); -if (write_space != vsk->default_write_space) -write_space (sk); + +read_lock_bh (&sk->sk_callback_lock); + native =vsock_rx_cb_is_native (sk); +read_unlock_bh (&sk->sk_callback_lock); +net/vmw_vsock/virtio_transport_common.c EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); · line 2039
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8
-0+if (native) { +release_sock (sk); +if (data_ready_pending) + vsk->default_data_ready (sk); +sock_put (sk); +return ; } +if (write_space_pending || data_ready_pending) +vsock_rx_callback_handoff (sk);[Severity: Low]
The commit message says:Sampling at batch finish takes
sk_callback_lockonce.Does that hold on the non-native path?
sk_callback_lockis taken once here forvsock_rx_cb_is_native().vsock_rx_callback_handoff()then takes it again at the top. It takes it once more on each loop iteration, up toVSOCK_RX_CB_HANDOFF_MAX(64) times, and the requeued work can do all of this again.[ ... ]
Quoted 13 lines
net/vmw_vsock/vsock_bpf.c +1
-0@@ -154,6 +154,7 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restoreif (restore) { sk->sk_write_space = psock->saved_write_space;sock_replace_proto (sk, psock->sk_proto); +vsock_rx_callback_kick (sk);return 0 ; }net/vmw_vsock/vsock_bpf.c int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore · line 172
Quoted 6 lines
net/vmw_vsock/vsock_bpf.c +1
-0@@ -166,6 +167,7 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restorevsock_bpf_check_needs_rebuild (psock->sk_proto);sock_replace_proto (sk, &vsock_bpf_prot); +vsock_rx_callback_kick (sk);return 0 ; }[Severity: Medium]
These kicks run for everyAF_VSOCKsocket added to or removed from a sockmap or sockhash, not only for sockets in a virtio RX batch. Is this change in behaviour intended?RX batching exists only in the guest virtio transport (
virtio_transport_rx_work()->virtio_transport_recv_pkt_batch()).vhost_vsockandvsock_loopbackalways usevirtio_transport_recv_pkt(). Both still set .read_skb =virtio_transport_read_skb, so their sockets reach this code too. The same is true ofSOCK_SEQPACKETsockets, whichvirtio_transport_recv_pkt_batchable()rejects.Before this patch,
sk_data_readywas not called by either path:
sock_map_link()->sock_map_init_proto()->vsock_bpf_update_proto()sk_psock_start_verdict()Data queued before attach only reached the verdict program when a later packet arrived.
Now the kick pushes that data through
sk_psock_verdict_data_ready(), up to 64 skbs per pass, and requeues itself if data remains. The verdict program can redirect or drop data that arrived before attach, where it used to stay for the native reader.If only the prot was swapped, for example with a msg_parser-only map, the handoff calls the default callback unconditionally after one retry. That adds an extra wakeup on every attach and detach.
Quoted 9 lines
The commit message says "non-batched sockets keep their existing behavior". It also says the handoff is scheduled "so an attach or detach outside the worker still drains skbs already queued".
Neither statement seems to fit what the code does: this is a new sockmap behaviour for every vsock transport that has
read_skb, and the subject line does not mention it. Could this be split out, or at least described in the commit message?
I will drop the attempt to preserve RX batching across sockmap cb replacements, at least for this optimization series.
Quoted 2 lines
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261010142247.99223-1-physicalmtea%40gmail.com
Re: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Jia JiaQuoted 26 lines · Jia Jia
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 through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
Signed-off-by Jia Jia <[email protected]>--- net/vmw_vsock/virtio_transport_common.c | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-)net/vmw_vsock/virtio_transport_common.c +0
-0@@ -1774,81 +1774,97 @@ static bool virtio_transport_valid_type(u16 type) (type ==VIRTIO_VSOCK_TYPE_SEQPACKET ); }
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1780
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +4
-5-/* 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)
I'd prefer these calls stay inline, there is nothing complex enough here to justify abstracting IMHO.
Could we move things around so that we don't have the arbitrary move of virtio_transport_recv_pkt()? It tends to create noise and makes it harder to focus on the more important parts of the patch.
Quoted 23 lines
net/vmw_vsock/virtio_transport_common.c +10
-7{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,
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1805
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +3
-1- 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)); +}
Any chance we can get rid of this wrapper and just call trace_virtio_transport_recv_pkt() directly? Even from multiple call sites I don't think this is a big simplification win.
Quoted 37 lines
net/vmw_vsock/virtio_transport_common.c +16
-16-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);
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1831
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +3
-5- 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; +}
Similarly here, the unrelated functions blending makes for a harder read. Maybe worth moving things around or trying a different diff algorithm?
nit: maybe we could just call this virtio_transport_find_socket()? AFAIK the tx side doesn't need a socket lookup since we already have the socket there, so we can omit the disambiguation if it lends a nicer name.
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1837
Quoted 7 lines
net/vmw_vsock/virtio_transport_common.c +5
-1- vsk =vsock_sk (sk); +struct virtio_transport_rx_pkt_ctx { +struct net *net; +const struct sockaddr_vm *src; +const struct sockaddr_vm *dst; +};
Let's just pass these directly too...
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8
-1-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) +{
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1854
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +7
-2+const struct sockaddr_vm *src = ctx->src; +const struct sockaddr_vm *dst = ctx->dst; +struct vsock_sock *vsk =vsock_sk (sk); +struct net *net = ctx->net; +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.
We can probably remove the first sentence since the function-level comment already declares we are holding the socket lock.
Best,
Bobby
Quoted 74 lines
net/vmw_vsock/virtio_transport_common.c +40
-5*/ if (sock_flag (sk,SOCK_DONE ) || (sk->sk_state !=TCP_LISTEN && - !vsock_check_source (vsk, &t->transport, &src))) { + !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; +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 = dst->svm_cid;if (space_available) sk->sk_write_space (sk);@@ -1875,12 +1891,49 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,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_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +struct sock *sk; +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.34.1
Re: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Jia JiaQuoted 19 lines · Jia Jia
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 through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
Signed-off-by Jia Jia <[email protected]>--- net/vmw_vsock/virtio_transport_common.c | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-)
[...]
Quoted 68 lines
net/vmw_vsock/virtio_transport_common.c +43
-7+/* +* 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) +{ +const struct sockaddr_vm *src = ctx->src; +const struct sockaddr_vm *dst = ctx->dst; +struct vsock_sock *vsk =vsock_sk (sk); +struct net *net = ctx->net; +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))) { + !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; +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 = dst->svm_cid;if (space_available) sk->sk_write_space (sk);@@ -1875,12 +1891,49 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,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_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +struct sock *sk; +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" );
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1928
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10
-0+ 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);
At this point the socket lookup succeeded with src/dst/net, why not just pass sk which already has all of them?
Best,
Bobby
Re: [PATCH net-next v2 2/5] vsock/virtio: amortize RX socket locking for stream packets
In reply to Jia JiaQuoted 62 lines · Jia Jia
The virtio-vsock RX worker currently acquires and releases the socket lock for every received packet. Keep the lock held while processing a bounded run of packets that belongs to the same socket.
Only ordinary RW packets for an established STREAM socket whose transport and protocol remain unchanged are eligible. Control packets, SEQPACKET traffic, state or transport changes, malformed packets, and sockets whose protocol has been replaced end the active batch and retain their existing handling.
Serialize the initial eligibility check with
sk_callback_lockbecause sockmap removal restores the protocol without taking the socket lock.Limit each batch to 64 packets or 64KB of payload, bounding the work under one socket lock. Keep the lookup reference until the batch finishes.
Keep a batch only while another skb metadata charge fits in the receive buffer. Share this predicate with
virtio_transport_inc_rx_pkt(). When an enqueued skb consumes the final metadata slot, finish the batch and release the socket lock before processing another packet, restoring the reader's scheduling opportunity from the per-packet path.The virtqueue callback remains a
queue_work()callback. Batching runs only in the process-context RX worker.Introduce
virtio_transport_rx_batch_finish()to tear down the batch and release the socket lock and lookup reference.Signed-off-by Jia Jia <[email protected]>--- include/linux/virtio_vsock.h | 11 ++ net/vmw_vsock/virtio_transport.c | 29 ++++- net/vmw_vsock/virtio_transport_common.c | 147 +++++++++++++++++++++++- 3 files changed, 183 insertions(+), 4 deletions(-)include/linux/virtio_vsock.h +11
-0@@ -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);net/vmw_vsock/virtio_transport.c +0
-0
net/vmw_vsock/virtio_transport.c virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) · line 637
Quoted 9 lines
net/vmw_vsock/virtio_transport.c +6
-0@@ -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)
Why both? Seems like MAX_PKTS would sufficiently bound the lock hold time.
Quoted 38 lines
net/vmw_vsock/virtio_transport.c +10
-0+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. */
net/vmw_vsock/virtio_transport.c static void virtio_transport_rx_work(struct work_struct *work) · line 712
Quoted 10 lines
net/vmw_vsock/virtio_transport.c +9
-1-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);
I'm not fundamentally opposed to the batch struct, but I wonder if its needed? If we can get rid of BATCH_MAX_BYTES, then we only need to do pkts++ in a local var, and can pass an &sk ptr-ptr to virtio_transport_recv_pkt_batch()?
Best,
Bobby
Quoted 211 lines
net/vmw_vsock/virtio_transport.c +3
-0+ } + } } }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 +144
-3@@ -576,10 +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 @@ -588,7 +594,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;@@ -1834,10 +1840,28 @@ struct virtio_transport_rx_pkt_ctx {struct net *net;const struct sockaddr_vm *src;const struct sockaddr_vm *dst; +bool *batchable; }; +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); +} +/* -* 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,@@ -1850,6 +1874,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,struct net *net = ctx->net;bool space_available; +if (ctx->batchable) + *ctx->batchable = false; +/* Check after acquiring the socket lock. Listener sockets accept packets * from any source and are not assigned to a transport. */ @@ -1891,6 +1918,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,break ; } +if (ctx->batchable) + *ctx->batchable =virtio_transport_recv_pkt_batchable (t, sk); +return false; }@@ -1941,6 +1971,117 @@ 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 virtio_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +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 ; + } + +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) { +virtio_transport_rx_batch_finish (batch); + (void )virtio_transport_reset_no_sock (t, skb, net); +kfree_skb (skb); +return ; + } + +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 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); + + ctx = (struct virtio_transport_rx_pkt_ctx ) { + .net = net, + .src = &src, + .dst = &dst, + .batchable = start_batch ? &batchable :NULL , + }; + 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->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. Signature · 1 line
2.34.1
Re: [PATCH net-next v2 3/5] vsock/virtio: reuse same-flow socket lookup in RX batches
In reply to Jia JiaQuoted 104 lines · Jia Jia
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 <[email protected]>--- include/linux/virtio_vsock.h | 3 ++ net/vmw_vsock/virtio_transport_common.c | 58 +++++++++++++++---------- 2 files changed, 38 insertions(+), 23 deletions(-)include/linux/virtio_vsock.h +3
-0@@ -287,6 +287,9 @@ struct virtio_transport_rx_batch {struct sock *sk;unsigned int pkts;size_t bytes; +struct net *net; +struct sockaddr_vm src; +struct sockaddr_vm dst; }; void virtio_transport_recv_pkt_batch(struct virtio_transport *t,net/vmw_vsock/virtio_transport_common.c +32
-13@@ -1977,6 +1977,7 @@ void virtio_transport_rx_batch_finish(struct virtio_transport_rx_batch *batch) batch->sk =NULL ; batch->pkts =0 ; batch->bytes =0 ; + batch->net =NULL ;if (!sk)return ;@@ -2008,6 +2009,37 @@ 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);@@ -2018,33 +2050,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,
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt_batch(struct virtio_transport *t, · line 2047
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +0
-10- .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 ; - } -
Not sure I love adding code and then removing it in the same series. Seems like these are asking to be squashed?
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt_batch(struct virtio_transport *t, · line 2077
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +3
-0lock_sock (sk);/* * Sockmap removal restores the native protocol under sk_callback_lock. @@ -2063,6 +2072,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;
Similar to my comment in the other patch, seems these are all in sk already?
Best,
Bobby
Re: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Bobby EshlemanQuoted 42 lines · Jia Jia
Quoted 26 lines
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 through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
Signed-off-by Jia Jia <[email protected]>--- net/vmw_vsock/virtio_transport_common.c | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-)net/vmw_vsock/virtio_transport_common.c +0
-0@@ -1774,81 +1774,97 @@ static bool virtio_transport_valid_type(u16 type) (type ==VIRTIO_VSOCK_TYPE_SEQPACKET ); }net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1780
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +4
-5-/* 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)I'd prefer these calls stay inline, there is nothing complex enough here to justify abstracting IMHO.
Quoted 4 lines
Could we move things around so that we don't have the arbitrary move of
virtio_transport_recv_pkt()? It tends to create noise and makes it harder to focus on the more important parts of the patch.
Sure, I'll update this in the next version.
Quoted 22 lines
Quoted 22 lines
net/vmw_vsock/virtio_transport_common.c +10
-6{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);
Quoted 16 lines
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1805
Quoted 11 lines
net/vmw_vsock/virtio_transport_common.c +3
-2-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)); +}Any chance we can get rid of this wrapper and just call
trace_virtio_transport_recv_pkt()directly? Even from multiple call sites I don't think this is a big simplification win.
Sure, I'll update this in the next version.
Quoted 41 lines
Quoted 41 lines
net/vmw_vsock/virtio_transport_common.c +17
-17-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 ; }
Quoted 11 lines
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1831
Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +2
-4-if (!skb_set_owner_sk_safe (skb, sk)) { -WARN_ONCE (1 ,"receiving vsock socket has sk_refcnt == 0\n" ); - goto free_pkt; - } +return sk; +}Similarly here, the unrelated functions blending makes for a harder read. Maybe worth moving things around or trying a different diff algorithm?
Sure, I'll update this in the next version
Quoted 5 lines
nit: maybe we could just call this
virtio_transport_find_socket()? AFAIK the tx side doesn't need a socket lookup since we already have the socket there, so we can omit the disambiguation if it lends a nicer name.
Sure, I'll update this in the next version
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1837
Quoted 7 lines
net/vmw_vsock/virtio_transport_common.c +5
-1- vsk =vsock_sk (sk); +struct virtio_transport_rx_pkt_ctx { +struct net *net; +const struct sockaddr_vm *src; +const struct sockaddr_vm *dst; +};Let's just pass these directly too...
Sure, I'll update this in the next version.
Quoted 16 lines
Quoted 16 lines
net/vmw_vsock/virtio_transport_common.c +13
-1-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) +{ +const struct sockaddr_vm *src = ctx->src; +const struct sockaddr_vm *dst = ctx->dst; +struct vsock_sock *vsk =vsock_sk (sk); +struct net *net = ctx->net; +bool space_available;
Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_valid_type(u16 type) · line 1854
Quoted 4 lines
net/vmw_vsock/virtio_transport_common.c +2
-2-/* 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. We can probably remove the first sentence since the function-level comment already declares we are holding the socket lock.
Sure, I'll update this in the next version.
Quoted 77 lines
Best,
BobbyQuoted 74 lines
net/vmw_vsock/virtio_transport_common.c +40
-6*/ if (sock_flag (sk,SOCK_DONE ) || (sk->sk_state !=TCP_LISTEN && - !vsock_check_source (vsk, &t->transport, &src))) { + !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; +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 = dst->svm_cid;if (space_available) sk->sk_write_space (sk);@@ -1875,12 +1891,49 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,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_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +struct sock *sk; +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: --2.34.1
Re: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Bobby EshlemanQuoted 97 lines · Jia Jia
Quoted 19 lines
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 through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
Signed-off-by Jia Jia <[email protected]>--- net/vmw_vsock/virtio_transport_common.c | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-)[...]
Quoted 73 lines
net/vmw_vsock/virtio_transport_common.c +48
-7+/* +* 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) +{ +const struct sockaddr_vm *src = ctx->src; +const struct sockaddr_vm *dst = ctx->dst; +struct vsock_sock *vsk =vsock_sk (sk); +struct net *net = ctx->net; +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))) { + !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; +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 = dst->svm_cid;if (space_available) sk->sk_write_space (sk);@@ -1875,12 +1891,49 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,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_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +struct sock *sk; +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 ) {
Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1928
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +5
-0+ .net = net, + .src = &src, + .dst = &dst, + }; + free_pkt =virtio_transport_recv_pkt_locked (t, skb, sk, &ctx);At this point the socket lookup succeeded with src/dst/net, why not just pass sk which already has all of them?
If I understand this correctly, a successful lookup is not enough to recover all of this from sk. The lookup may fall back from the connected table to the bound
table, so the packet source must still be validated under the socket lock; otherwise that check would become ineffective. The packet destination is also needed after
a transport reset, and the RX namespace used for a reset reply is not necessarily sock_net(sk) in global mode. I’ll remove the context struct and pass src, dst, and
net directly.
Quoted 2 lines
Best,
Bobby
Re: [PATCH net-next v2 2/5] vsock/virtio: amortize RX socket locking for stream packets
In reply to Bobby EshlemanQuoted 112 lines · Jia Jia
Quoted 62 lines
The virtio-vsock RX worker currently acquires and releases the socket lock for every received packet. Keep the lock held while processing a bounded run of packets that belongs to the same socket.
Only ordinary RW packets for an established STREAM socket whose transport and protocol remain unchanged are eligible. Control packets, SEQPACKET traffic, state or transport changes, malformed packets, and sockets whose protocol has been replaced end the active batch and retain their existing handling.
Serialize the initial eligibility check with
sk_callback_lockbecause sockmap removal restores the protocol without taking the socket lock.Limit each batch to 64 packets or 64KB of payload, bounding the work under one socket lock. Keep the lookup reference until the batch finishes.
Keep a batch only while another skb metadata charge fits in the receive buffer. Share this predicate with
virtio_transport_inc_rx_pkt(). When an enqueued skb consumes the final metadata slot, finish the batch and release the socket lock before processing another packet, restoring the reader's scheduling opportunity from the per-packet path.The virtqueue callback remains a
queue_work()callback. Batching runs only in the process-context RX worker.Introduce
virtio_transport_rx_batch_finish()to tear down the batch and release the socket lock and lookup reference.Signed-off-by Jia Jia <[email protected]>--- include/linux/virtio_vsock.h | 11 ++ net/vmw_vsock/virtio_transport.c | 29 ++++- net/vmw_vsock/virtio_transport_common.c | 147 +++++++++++++++++++++++- 3 files changed, 183 insertions(+), 4 deletions(-)include/linux/virtio_vsock.h +11
-0@@ -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);net/vmw_vsock/virtio_transport.c +0
-0net/vmw_vsock/virtio_transport.c virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) · line 637
Quoted 9 lines
net/vmw_vsock/virtio_transport.c +6
-0@@ -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)Why both? Seems like
MAX_PKTSwould sufficiently bound the lock hold time.Quoted 35 lines
net/vmw_vsock/virtio_transport.c +10
-0+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);
Quoted 16 lines
net/vmw_vsock/virtio_transport.c static void virtio_transport_rx_work(struct work_struct *work) · line 712
Quoted 13 lines
net/vmw_vsock/virtio_transport.c +9
-1/* 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);I'm not fundamentally opposed to the batch struct, but I wonder if its needed?
With the byte limit retained, the batch still needs to carry the socket and both counters across calls. A batch may also be finished inside the receive helper, so
keeping that state together avoids having the worker maintain a separate counter that can get out of sync. The later notification patches add their pending state to the
same object as well, so I would prefer to keep the batch struct.
Quoted 4 lines
If we can get rid of
BATCH_MAX_BYTES, then we only need to do pkts++ in a local var, and can pass an &sk ptr-ptr tovirtio_transport_recv_pkt_batch()?
My thinking was that the two limits bound different things. The 64-packet limit bounds per-packet work for small packets, while the 64K limit bounds how much data can
be queued while the reader cannot acquire the socket lock. A single packet can carry up to 64K, so a packet-only limit could let one batch cover as much as 4M. Since
the receive buffer can be enlarged, I do not think the default 256K buffer is a substitute for the byte limit. I would prefer to keep both.
Quoted 214 lines
Best,
BobbyQuoted 211 lines
net/vmw_vsock/virtio_transport.c +3
-0+ } + } } }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 +144
-4@@ -576,10 +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 @@ -588,7 +594,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;@@ -1834,10 +1840,28 @@ struct virtio_transport_rx_pkt_ctx {struct net *net;const struct sockaddr_vm *src;const struct sockaddr_vm *dst; +bool *batchable; }; +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); +} +/* -* 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,@@ -1850,6 +1874,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,struct net *net = ctx->net;bool space_available; +if (ctx->batchable) + *ctx->batchable = false; +/* Check after acquiring the socket lock. Listener sockets accept packets * from any source and are not assigned to a transport. */ @@ -1891,6 +1918,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,break ; } +if (ctx->batchable) + *ctx->batchable =virtio_transport_recv_pkt_batchable (t, sk); +return false; }@@ -1941,6 +1971,117 @@ 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 virtio_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +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 ; + } + +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) { +virtio_transport_rx_batch_finish (batch); + (void )virtio_transport_reset_no_sock (t, skb, net); +kfree_skb (skb); +return ; + } + +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 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); + + ctx = (struct virtio_transport_rx_pkt_ctx ) { + .net = net, + .src = &src, + .dst = &dst, + .batchable = start_batch ? &batchable :NULL , + }; + 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->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.34.1
Re: [PATCH net-next v2 3/5] vsock/virtio: reuse same-flow socket lookup in RX batches
In reply to Bobby EshlemanQuoted 111 lines · Jia Jia
Quoted 109 lines
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 <[email protected]>--- include/linux/virtio_vsock.h | 3 ++ net/vmw_vsock/virtio_transport_common.c | 58 +++++++++++++++---------- 2 files changed, 38 insertions(+), 23 deletions(-)include/linux/virtio_vsock.h +3
-0@@ -287,6 +287,9 @@ struct virtio_transport_rx_batch {struct sock *sk;unsigned int pkts;size_t bytes; +struct net *net; +struct sockaddr_vm src; +struct sockaddr_vm dst; }; void virtio_transport_recv_pkt_batch(struct virtio_transport *t,net/vmw_vsock/virtio_transport_common.c +32
-18@@ -1977,6 +1977,7 @@ void virtio_transport_rx_batch_finish(struct virtio_transport_rx_batch *batch) batch->sk =NULL ; batch->pkts =0 ; batch->bytes =0 ; + batch->net =NULL ;if (!sk)return ;@@ -2008,6 +2009,37 @@ 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);@@ -2018,33 +2050,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);
Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt_batch(struct virtio_transport *t, · line 2047
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +0
-5-if (free_pkt) -kfree_skb (skb); -return ; - } -Not sure I love adding code and then removing it in the same series. Seems like these are asking to be squashed?
I'll fold patch 3 into patch 2 so batching is introduced with same-flow lookup reuse from the start.
Quoted 5 lines
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +0
-0lock_sock (sk);/* * Sockmap removal restores the native protocol under sk_callback_lock. @@ -2063,6 +2072,9 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t, free_pkt =virtio_transport_recv_pkt_locked (t, skb, sk, &ctx);
Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt_batch(struct virtio_transport *t, · line 2077
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +3
-0if (start_batch && batchable) {/* Keep the lookup reference until the batch is released. */ + batch->net = net; + batch->src = src; + batch->dst = dst;Similar to my comment in the other patch, seems these are all in sk already?
For the batch cache, yes. I'll use the locked socket state for the same-flow check. The packet values still need to be passed to the locked receive path for source validation and reset handling.
Quoted 2 lines
Best,
Bobby
Re: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Jia JiaQuoted 107 lines · Jia Jia
Quoted 97 lines · Jia Jia
Quoted 19 lines
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 through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
Signed-off-by Jia Jia <[email protected]>--- net/vmw_vsock/virtio_transport_common.c | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-)[...]
Quoted 73 lines
net/vmw_vsock/virtio_transport_common.c +48
-7+/* +* 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) +{ +const struct sockaddr_vm *src = ctx->src; +const struct sockaddr_vm *dst = ctx->dst; +struct vsock_sock *vsk =vsock_sk (sk); +struct net *net = ctx->net; +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))) { + !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; +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 = dst->svm_cid;if (space_available) sk->sk_write_space (sk);@@ -1875,12 +1891,49 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,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_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +struct sock *sk; +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 ) {Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1928
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +5
-0+ .net = net, + .src = &src, + .dst = &dst, + }; + free_pkt =virtio_transport_recv_pkt_locked (t, skb, sk, &ctx);At this point the socket lookup succeeded with src/dst/net, why not just pass sk which already has all of them?
Quoted 10 lines
If I understand this correctly, a successful lookup is not enough to recover all of this from sk. The lookup may fall back from the connected table to the bound
table, so the packet source must still be validated under the socket lock; otherwise that check would become ineffective. The packet destination is also needed after
a transport reset, and the RX namespace used for a reset reply is not necessarilysock_net(sk)in global mode. I’ll remove the context struct and pass src, dst, and
net directly.
True that the lookup may fallback to the bound table, but this begs the question, why bother with the bound table here? If a socket hasn't landed in the connected table yet, then OP_RW is -EINVAL and we respond with RST anyway... so I'd guess we shouldn't even be trying to batch for those sockets.
For connected sockets, the sk has the destination (vsk->remote_addr) needed by the reset. And yes, sock_net(sk) does not necessarily equal the net ptr used for a reset reply in global mode, but we don't really expect that for global mode in vsock, we only expect that vsock_net_check_mode() rules are followed. In this case where !net_eq(n0, n1), the RST reply will be routed correctly across namespaces following global mode rules.
I could certainly be missing something and happy to be pointed to an instance of sk being insufficient here. In general, I think its nice to avoid new data structures if possible.
Best,
Bobby
Re: [PATCH net-next v2 2/5] vsock/virtio: amortize RX socket locking for stream packets
In reply to Jia JiaQuoted 142 lines · Jia Jia
Quoted 112 lines · Jia Jia
Quoted 62 lines
The virtio-vsock RX worker currently acquires and releases the socket lock for every received packet. Keep the lock held while processing a bounded run of packets that belongs to the same socket.
Only ordinary RW packets for an established STREAM socket whose transport and protocol remain unchanged are eligible. Control packets, SEQPACKET traffic, state or transport changes, malformed packets, and sockets whose protocol has been replaced end the active batch and retain their existing handling.
Serialize the initial eligibility check with
sk_callback_lockbecause sockmap removal restores the protocol without taking the socket lock.Limit each batch to 64 packets or 64KB of payload, bounding the work under one socket lock. Keep the lookup reference until the batch finishes.
Keep a batch only while another skb metadata charge fits in the receive buffer. Share this predicate with
virtio_transport_inc_rx_pkt(). When an enqueued skb consumes the final metadata slot, finish the batch and release the socket lock before processing another packet, restoring the reader's scheduling opportunity from the per-packet path.The virtqueue callback remains a
queue_work()callback. Batching runs only in the process-context RX worker.Introduce
virtio_transport_rx_batch_finish()to tear down the batch and release the socket lock and lookup reference.Signed-off-by Jia Jia <[email protected]>--- include/linux/virtio_vsock.h | 11 ++ net/vmw_vsock/virtio_transport.c | 29 ++++- net/vmw_vsock/virtio_transport_common.c | 147 +++++++++++++++++++++++- 3 files changed, 183 insertions(+), 4 deletions(-)include/linux/virtio_vsock.h +11
-0@@ -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);net/vmw_vsock/virtio_transport.c +0
-0net/vmw_vsock/virtio_transport.c virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) · line 637
Quoted 9 lines
net/vmw_vsock/virtio_transport.c +6
-0@@ -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)Why both? Seems like
MAX_PKTSwould sufficiently bound the lock hold time.Quoted 35 lines
net/vmw_vsock/virtio_transport.c +10
-0+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);Quoted 16 lines
net/vmw_vsock/virtio_transport.c static void virtio_transport_rx_work(struct work_struct *work) · line 712
Quoted 13 lines
net/vmw_vsock/virtio_transport.c +9
-1/* 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);I'm not fundamentally opposed to the batch struct, but I wonder if its needed?
With the byte limit retained, the batch still needs to carry the socket and both counters across calls. A batch may also be finished inside the receive helper, so
keeping that state together avoids having the worker maintain a separate counter that can get out of sync. The later notification patches add their pending state to the
same object as well, so I would prefer to keep the batch struct.Quoted 4 lines
If we can get rid of
BATCH_MAX_BYTES, then we only need to do pkts++ in a local var, and can pass an &sk ptr-ptr tovirtio_transport_recv_pkt_batch()?
Quoted 10 lines
My thinking was that the two limits bound different things. The 64-packet limit bounds per-packet work for small packets, while the 64K limit bounds how much data can
be queued while the reader cannot acquire the socket lock. A single packet can carry up to 64K, so a packet-only limit could let one batch cover as much as 4M. Since
the receive buffer can be enlarged, I do not think the default 256K buffer is a substitute for the byte limit. I would prefer to keep both.
Fair enough, makes sense. Probably good to ensure a timely release of credits this way too, which won't happen until the batcher releases.
Best,
Bobby
Quoted 212 lines
Quoted 212 lines
Quoted 212 lines
net/vmw_vsock/virtio_transport.c +3
-0+ } + } } }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 +144
-4@@ -576,10 +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 @@ -588,7 +594,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;@@ -1834,10 +1840,28 @@ struct virtio_transport_rx_pkt_ctx {struct net *net;const struct sockaddr_vm *src;const struct sockaddr_vm *dst; +bool *batchable; }; +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); +} +/* -* 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,@@ -1850,6 +1874,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,struct net *net = ctx->net;bool space_available; +if (ctx->batchable) + *ctx->batchable = false; +/* Check after acquiring the socket lock. Listener sockets accept packets * from any source and are not assigned to a transport. */ @@ -1891,6 +1918,9 @@ virtio_transport_recv_pkt_locked(struct virtio_transport *t,break ; } +if (ctx->batchable) + *ctx->batchable =virtio_transport_recv_pkt_batchable (t, sk); +return false; }@@ -1941,6 +1971,117 @@ 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 virtio_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +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 ; + } + +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) { +virtio_transport_rx_batch_finish (batch); + (void )virtio_transport_reset_no_sock (t, skb, net); +kfree_skb (skb); +return ; + } + +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 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); + + ctx = (struct virtio_transport_rx_pkt_ctx ) { + .net = net, + .src = &src, + .dst = &dst, + .batchable = start_batch ? &batchable :NULL , + }; + 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->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.34.1
Re: [PATCH net-next v2 1/5] vsock/virtio: split socket lookup from locked RX processing
In reply to Bobby EshlemanQuoted 134 lines · Jia Jia
Quoted 107 lines
Quoted 97 lines · Jia Jia
Quoted 19 lines
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 through a packet context to the locked receive path.
Preserve source validation and failure handling. Keep the lookup reference separate from the skb owner reference so the locked receive path can manage the two lifetimes independently.
Signed-off-by Jia Jia <[email protected]>--- net/vmw_vsock/virtio_transport_common.c | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-)[...]
Quoted 73 lines
net/vmw_vsock/virtio_transport_common.c +48
-7+/* +* 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) +{ +const struct sockaddr_vm *src = ctx->src; +const struct sockaddr_vm *dst = ctx->dst; +struct vsock_sock *vsk =vsock_sk (sk); +struct net *net = ctx->net; +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))) { + !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; +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 = dst->svm_cid;if (space_available) sk->sk_write_space (sk);@@ -1875,12 +1891,49 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,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_transport_rx_pkt_ctx ctx; +struct sockaddr_vm src, dst; +struct sock *sk; +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 ) {Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1928
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +5
-0+ .net = net, + .src = &src, + .dst = &dst, + }; + free_pkt =virtio_transport_recv_pkt_locked (t, skb, sk, &ctx);At this point the socket lookup succeeded with src/dst/net, why not just pass sk which already has all of them?
Quoted 10 lines
If I understand this correctly, a successful lookup is not enough to recover all of this from sk. The lookup may fall back from the connected table to the bound
table, so the packet source must still be validated under the socket lock; otherwise that check would become ineffective. The packet destination is also needed after
a transport reset, and the RX namespace used for a reset reply is not necessarilysock_net(sk)in global mode. I’ll remove the context struct and pass src, dst, and
net directly.True that the lookup may fallback to the bound table, but this begs the question, why bother with the bound table here? If a socket hasn't landed in the connected table yet, then
OP_RWis-EINVALand we respond with RST anyway... so I'd guess we shouldn't even be trying to batch for those sockets.For connected sockets, the sk has the destination (
vsk->remote_addr) needed by the reset. And yes,sock_net(sk)does not necessarily equal the net ptr used for a reset reply in global mode, but we don't really expect that for global mode in vsock, we only expect thatvsock_net_check_mode()rules are followed. In this case where !net_eq(n0, n1), the RST reply will be routed correctly across namespaces following global mode rules.
Quoted 4 lines
I could certainly be missing something and happy to be pointed to an instance of sk being insufficient here. In general, I think its nice to avoid new data structures if possible.
You're right. Although virtio_transport_recv_pkt_batchable() requires TCP_ESTABLISHED and excludes other states from batching, the bound-table fallback is
unnecessary there; batch admission can use only the connected lookup and leave a miss to the normal path.
I also overlooked what the existing vsock_net_check_mode(sock_net(sk), net) check guarantees. If the namespaces differ and the lookup succeeds, both are
in global mode, so sock_net(sk) is sufficient for routing the RST. The extra context is therefore unnecessary. Thanks for pointing this out.
I'll update this in the next version.
Quoted 2 lines
Best,
Bobby