vsock: defer RX readable notifications until batch unlock
Subsystems (6)
PATCH 5/5 vsock: defer RX readable notifications until batch unlock
From: Jia Jia <[email protected]>
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 initial sock_def_readable() callback when the AF_VSOCK socket is created. virtio_transport_common can be built as a module and cannot refer to that unexported symbol directly.
Custom data-ready callbacks retain per-packet notification behavior. If the callback changes while a default notification is pending, finish the batch before returning to the per-packet path.
Keep the socket reference until the deferred callback completes. A sockmap attachment takes the socket lock, so data queued before the attachment is reported through the saved default callback.
Control packets, state transitions, transport changes and non-batched sockets retain their existing behavior.
--- include/linux/virtio_vsock.h | 1 + include/net/af_vsock.h | 3 +- net/vmw_vsock/af_vsock.c | 1 + net/vmw_vsock/virtio_transport_common.c | 53 ++++++++++++++++++++++++++----- 4 files changed, 49 insertions(+), 9 deletions(-)
include/linux/virtio_vsock.h +1 -0
@@ -289,6 +289,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 +2 -1
@@ -63,7 +63,8 @@ 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);/* Protected by lock_sock(sk) */
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_data_ready = sk->sk_data_ready; vsk->default_write_space = sk->sk_write_space;vsock_addr_init (&vsk->local_addr,VMADDR_CID_ANY ,VMADDR_PORT_ANY );vsock_addr_init (&vsk->remote_addr,VMADDR_CID_ANY ,VMADDR_PORT_ANY );
net/vmw_vsock/virtio_transport_common.c +45 -8
138 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] vsock: unreachable callback replacement abort logic in batching --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] vsock: unreachable callback replacement abort logic in batching
--
commit 811453fb266663fdded1db37d3b4cf3bde547c9f
Author: Jia Jia <[email protected]>
vsock: defer RX readable notifications until batch unlock
This commit defers vsock RX data-ready notifications until the batch finishes and releases the socket lock. It records the initial callback to handle mid-batch replacements.
Quoted 3 lines
net/vmw_vsock/virtio_transport_common.c +0
-0
[ ... ]
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt_batch(struct virtio_transport *t, · line 2049
Quoted 11 lines
net/vmw_vsock/virtio_transport_common.c +7
-0@@ -2015,6 +2040,13 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t, vsock_addr_equals_addr(&batch->dst, &dst) && virtio_transport_recv_pkt_batchable(t, batch->sk)) { sk = batch->sk; + defer_data_ready =READ_ONCE (sk->sk_data_ready) == +vsock_sk (sk)->default_data_ready; +if (batch->data_ready_pending && !defer_data_ready) { +virtio_transport_rx_batch_finish (batch); + goto lookup; + } +
[Severity: Low]
Can this branch ever execute?
The condition checks if a custom callback was installed (!defer_data_ready) while a default notification is already pending.
Since custom callback attachments (such as BPF sockmap insertion) require acquiring the exact same socket lock, and the lock is held continuously while the batch is active, is it possible for the callback to change from default to custom mid-batch?
Could this bailout branch be unreachable dead code due to the structural mutual exclusion provided by the socket lock?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Quoted 20 lines
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] vsock: unreachable callback replacement abort logic in batching
--commit
811453fb266663fdded1db37d3b4cf3bde547c9f
Author: Jia Jia <[email protected]>vsock: defer RX readable notifications until batch unlock
This commit defers vsock RX data-ready notifications until the batch finishes and releases the socket lock. It records the initial callback to handle mid-batch replacements.
Quoted 3 lines
net/vmw_vsock/virtio_transport_common.c +0
-0[ ... ]
Quoted 15 lines
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt_batch(struct virtio_transport *t, · line 2049
Quoted 11 lines
net/vmw_vsock/virtio_transport_common.c +7
-0@@ -2015,6 +2040,13 @@ void virtio_transport_recv_pkt_batch(struct virtio_transport *t, vsock_addr_equals_addr(&batch->dst, &dst) && virtio_transport_recv_pkt_batchable(t, batch->sk)) { sk = batch->sk; + defer_data_ready =READ_ONCE (sk->sk_data_ready) == +vsock_sk (sk)->default_data_ready; +if (batch->data_ready_pending && !defer_data_ready) { +virtio_transport_rx_batch_finish (batch); + goto lookup; + } +[Severity: Low]
Can this branch ever execute?
Unless I'm mistaken, this branch can actually be reached.
Quoted 11 lines
The condition checks if a custom callback was installed (!defer_data_ready) while a default notification is already pending.
Since custom callback attachments (such as BPF sockmap insertion) require acquiring the exact same socket lock, and the lock is held continuously while the batch is active, is it possible for the callback to change from default to custom mid-batch?
Could this bailout branch be unreachable dead code due to the structural mutual exclusion provided by the socket lock?
The premise that sockmap updates always take lock_sock(sk) applies only to the syscall path. When a BPF program calls bpf_map_update_elem(), for example from SK_LOOKUP or BPF_TRACE_ITER, sock_map_update_elem() takes bh_lock_sock(sk),
The batch reuse path checks sk->sk_prot in virtio_transport_recv_pkt_batchable() before reading sk->sk_data_ready. A concurrent BPF update can replace both callbacks between those reads while the RX worker owns the socket.
Quoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5