vsock/virtio: bound what a peer can make the host queue
Subsystems (7)
cover vsock/virtio: bound what a peer can make the host queue
A virtio-vsock peer that does not consume what it is sent can make the local side queue memory that no limit covers. On a host the peer is a guest, and the memory is GFP_KERNEL, charged to no memory cgroup. Two ways remain on net/main:
- the send credit trusts the fwd_cnt the peer reports, so a peer that never reads but keeps acknowledging gets unlimited credit (patch 2);
- credit updates are not bounded at all: a peer can solicit them with OP_CREDIT_REQUEST, or by sending small packets that a reader drains, while posting no rx buffers (patch 3).
Patch 1 fixes the accounting of packets that vhost drops on rx errors; patch 2 relies on bytes_unsent being right.
Nothing changes on the wire.
Tested in an x86_64 qemu/KVM guest running the kernel under test (net/main 03897066069f); the "guest" is a user-space program driving /dev/vhost-vsock directly, talking to plain AF_VSOCK host sockets:
case net/main this series lying fwd_cnt, no rx buffers, 2 s of 256 MiB 256 KiB non-blocking send() on one socket queued (one window) 20000 OP_CREDIT_REQUEST, no rx buffers +18.5 MiB +0 20000 1-byte packets, each read by host +18.0 MiB +24 KiB
vsock_test from tools/testing/vsock passes over loopback and between a nested guest and its host. A bidirectional stress test (up to 512 connections streaming both ways, readers draining in small reads) runs without stalls, and vsock_perf throughput stays within run-to-run noise. With KASAN and lockdep enabled, these tests show no reports.
The reproducer (a small user-space program that drives /dev/vhost-vsock as the guest) is available on request.
The patches, the test setup and these changelogs were prepared with an AI coding assistant (Claude); the tests above were run as described.
Oleg Drokin (3):
vhost/vsock: release the accounting of packets dropped on rx errors vsock/virtio: bound the send credit by bytes not yet consumed by the
transport
vsock/virtio: queue at most one credit update per socket
drivers/vhost/vsock.c | 25 +++++++-- include/linux/virtio_vsock.h | 5 ++ net/vmw_vsock/virtio_transport.c | 2 + net/vmw_vsock/virtio_transport_common.c | 69 +++++++++++++++++++++++-- net/vmw_vsock/vsock_loopback.c | 1 + 5 files changed, 95 insertions(+), 7 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/3 vhost/vsock: release the accounting of packets dropped on rx errors
When vhost_transport_do_send_pkt() fails to deliver a packet to the guest (unexpected output buffers, a buffer smaller than the header, a fault while copying the header or the payload) it frees the skb with kfree_skb() and gives up. The success path does more:virtio_transport_consume_skb_sent() subtracts the payload from the socket's bytes_unsent (SIOCOUTQ), and for a reply the queued_replies counter is decremented, restarting the guest's tx queue once it drops below the ring size.
The error paths skip both. Every such drop leaves SIOCOUTQ too high for the life of the socket, and every dropped reply permanently takes one slot of the queued_replies budget, so that after a ring's worth of them vhost stops processing the guest's tx queue for good.
Release both on the error paths, as the success path does.
--- drivers/vhost/vsock.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +19 -4
@@ -104,6 +104,21 @@ static bool vhost_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid)return found; } +/* Undo the send accounting of an undelivered skb; true if tx may restart. */ +static bool vhost_vsock_drop_skb (struct vhost_vsock *vsock,struct sk_buff *skb) +{ +bool restart_tx = false; + +if (virtio_vsock_skb_reply (skb)) { +int val =atomic_dec_return (&vsock->queued_replies); + + restart_tx = val +1 == vsock->vqs[VSOCK_VQ_TX ].num; + } +virtio_transport_consume_skb_sent (skb, false); +kfree_skb (skb); +return restart_tx; +} +static void vhost_transport_do_send_pkt (struct vhost_vsock *vsock,struct vhost_virtqueue *vq)
@@ -162,14 +177,14 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock, }if (out) { -kfree_skb (skb); + restart_tx |=vhost_vsock_drop_skb (vsock, skb);vq_err (vq,"Expected 0 output buffers, got %u\n" , out);break ; } iov_len =iov_length (&vq->iov[out], in);if (iov_len <sizeof (*hdr)) { -kfree_skb (skb); + restart_tx |=vhost_vsock_drop_skb (vsock, skb);vq_err (vq,"Buffer len [%zu] too small\n" , iov_len);break ; }
@@ -212,7 +227,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock, nbytes =copy_to_iter (hdr,sizeof (*hdr), &iov_iter);if (nbytes !=sizeof (*hdr)) { -kfree_skb (skb); + restart_tx |=vhost_vsock_drop_skb (vsock, skb);vq_err (vq,"Faulted on copying pkt hdr\n" );break ; }
@@ -221,7 +236,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock, offset, &iov_iter, payload_len)) { -kfree_skb (skb); + restart_tx |=vhost_vsock_drop_skb (vsock, skb);vq_err (vq,"Faulted on copying pkt buf\n" );break ; }
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH net 1/3] vhost/vsock: release the accounting of packets dropped on rx errors
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
When
vhost_transport_do_send_pkt()fails to deliver a packet to the guest (unexpected output buffers, a buffer smaller than the header, a fault while copying the header or the payload) it frees the skb withkfree_skb()and gives up. The success path does more:virtio_transport_consume_skb_sent()subtracts the payload from the socket'sbytes_unsent(SIOCOUTQ), and for a reply thequeued_repliescounter is decremented, restarting the guest's tx queue once it drops below the ring size.The error paths skip both. Every such drop leaves SIOCOUTQ too high for the life of the socket, and every dropped reply permanently takes one slot of the
queued_repliesbudget, so that after a ring's worth of them vhost stops processing the guest's tx queue for good.
Quoted 5 lines
Release both on the error paths, as the success path does.
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")Fixes e6ab45005772 ("vsock/virtio: add SIOCOUTQ support for all virtio based transports")Signed-off-by Oleg Drokin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/3 vsock/virtio: bound the send credit by bytes not yet consumed by the transport
In reply to Oleg DrokinThe send credit is
min(peer_buf_alloc, buf_alloc) - (tx_cnt - peer_fwd_cnt)
where peer_fwd_cnt is taken unchecked from every header the peer sends. A peer that never consumes what it is sent but keeps reporting fwd_cnt == tx_cnt gets unlimited credit: every send() is accepted and the skbs pile up in the transport's queue (on a host, vhost's send_pkt_queue, for a guest that posts no rx buffers). The skbs are GFP_KERNEL allocations charged to no memory cgroup. The peer learns tx_cnt easily when the host side relays a stream it also controls.
Commit 8ee784fdf006 ("vsock/virtio: cap TX credit to local buffer size") bounded peer_buf_alloc, but not peer_fwd_cnt.
bytes_unsent (maintained for SIOCOUTQ) counts what was handed to the transport and not yet consumed by the device. It is local knowledge, so bounding it by the same window limits a lying peer to one window of queued data per socket.
For an honest peer, bytes_unsent can exceed tx_cnt - peer_fwd_cnt only by data the peer already has but the transport has not yet released:
- on a host, the delivered part of an skb that vhost splits across rx buffers smaller than the skb: the peer acknowledges the pieces, but bytes_unsent drops only once the whole skb is delivered, so at most one skb (64 KiB);
- in a guest, tx buffers the host has consumed but virtio_transport_tx_work() has not reaped yet: vhost passes the packet to the host socket before it marks the buffer used, so the host's credit update can arrive first.
Either way the window shrinks only until the transport catches up; nothing stalls and nothing changes on the wire. The writer is woken when bytes_unsent drops (sock_wfree() calls sk_write_space()).
With a user-space vhost-vsock "guest" that posts no rx buffers and acknowledges everything, one host socket queued 256 MiB in under 2 s (SIOCOUTQ 268304384, where the test stops); with this patch it stays at one window (SIOCOUTQ 262144).
--- net/vmw_vsock/virtio_transport_common.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
net/vmw_vsock/virtio_transport_common.c +5 -2
@@ -1031,14 +1031,17 @@ EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_has_data);static s64 virtio_transport_has_space (struct virtio_vsock_sock *vvs) { +s64 tx_buf_size =virtio_transport_tx_buf_size (vvs);s64 bytes;/* Use s64 arithmetic so if the peer shrinks peer_buf_alloc while * we have bytes in flight (tx_cnt - peer_fwd_cnt), the subtraction * does not underflow. */ - bytes = (s64 )virtio_transport_tx_buf_size (vvs) - - (vvs->tx_cnt - vvs->peer_fwd_cnt); + bytes = tx_buf_size - (vvs->tx_cnt - vvs->peer_fwd_cnt); + +/* peer_fwd_cnt is peer-controlled, bytes_unsent is not. */ + bytes =min_t (s64, bytes, tx_buf_size - (s64 )vvs->bytes_unsent);if (bytes <0 ) bytes =0 ;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH net 2/3] vsock/virtio: bound the send credit by bytes not yet consumed by the transport
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines
The send credit is
min(peer_buf_alloc, buf_alloc)- (tx_cnt-peer_fwd_cnt)where
peer_fwd_cntis taken unchecked from every header the peer sends. A peer that never consumes what it is sent but keeps reportingfwd_cnt==tx_cntgets unlimited credit: everysend()is accepted and the skbs pile up in the transport's queue (on a host, vhost'ssend_pkt_queue, for a guest that posts no rx buffers). The skbs areGFP_KERNELallocations charged to no memory cgroup. The peer learnstx_cnteasily when the host side relays a stream it also controls.Commit
8ee784fdf006("vsock/virtio: cap TX credit to local buffer size") boundedpeer_buf_alloc, but notpeer_fwd_cnt.
Quoted 6 lines
[ ... ]
(SIOCOUTQ 268304384, where the test stops); with this patch it stays at one window (SIOCOUTQ 262144).Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")Signed-off-by Oleg Drokin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/3 vsock/virtio: queue at most one credit update per socket
In reply to Oleg DrokinA credit update carries no data, so the send credit never limits it, and the peer can solicit one at will: with OP_CREDIT_REQUEST, or by sending data that a local reader drains (a read that empties the receive queue sends one). A peer that posts no rx buffers can therefore make the other side queue credit updates without limit, about 1 KiB of unaccounted slab each for a 44-byte packet.
Do not queue a credit update while the socket's last queued packet is already one. Every packet carries fwd_cnt and buf_alloc, so a packet queued later makes the queued update redundant and lets a new one be queued after it. When a transport puts a queued update on the wire (vhost once it has an rx buffer for it, the guest driver when it adds it to the tx virtqueue, loopback when it delivers it) and nothing has been queued after it, it refreshes the update's fwd_cnt and buf_alloc and releases the socket. So a suppressed update always reaches the peer, either in the queued one or in a later packet, and the fwd_cnt the peer sees never goes back.
connect() can move a socket to another transport, freeing its virtio_vsock_sock, while packets are still queued in the old one. So a tracked update holds a reference on the virtio_vsock_sock, and its skb destructor clears the socket's pointer to it and drops the reference, however the skb is freed: sent, purged or dropped.
A peer can thus make a connection queue at most one credit update, plus one per packet the socket itself queued after it.
Counting credit updates in queued_replies instead would not work: most of them are sent by local reads, not in answer to the peer, and under bidirectional traffic vhost would stop reading the guest's tx queue while the guest stops reading its rx queue, each waiting for the other.
--- drivers/vhost/vsock.c | 2 + include/linux/virtio_vsock.h | 5 ++ net/vmw_vsock/virtio_transport.c | 2 + net/vmw_vsock/virtio_transport_common.c | 62 ++++++++++++++++++++++++- net/vmw_vsock/vsock_loopback.c | 1 + 5 files changed, 71 insertions(+), 1 deletion(-)
drivers/vhost/vsock.c +2 -0
@@ -176,6 +176,8 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,break ; } +virtio_transport_credit_update_xmit (skb); +if (out) { restart_tx |=vhost_vsock_drop_skb (vsock, skb);vq_err (vq,"Expected 0 output buffers, got %u\n" , out);
include/linux/virtio_vsock.h +5 -0
@@ -13,6 +13,7 @@ struct virtio_vsock_skb_cb {bool reply;bool tap_delivered;u32 offset; +struct virtio_vsock_sock *vvs; };#define VIRTIO_VSOCK_SKB_CB (skb) ((struct virtio_vsock_skb_cb *)((skb)->cb))
@@ -167,6 +168,9 @@ struct virtio_vsock_sock {u32 buf_used;struct sk_buff_head rx_queue;u32 msg_count; +struct sk_buff *credit_update; + +refcount_t refcnt; };struct virtio_vsock_pkt_info {
@@ -283,6 +287,7 @@ void virtio_transport_destruct(struct vsock_sock *vsk);void virtio_transport_recv_pkt (struct virtio_transport *t,struct sk_buff *skb,struct net *net);void virtio_transport_inc_tx_pkt (struct virtio_vsock_sock *vvs,struct sk_buff *skb); +void virtio_transport_credit_update_xmit (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);void virtio_transport_deliver_tap_pkt (struct sk_buff *skb);
net/vmw_vsock/virtio_transport.c +2 -0
@@ -104,6 +104,8 @@ static int virtio_transport_send_skb(struct sk_buff *skb, struct virtqueue *vq,int ret, in_sg =0 , out_sg =0 ;struct scatterlist **sgs; +virtio_transport_credit_update_xmit (skb); + sgs = vsock->out_sgs;sg_init_one (sgs[out_sg],virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb)));
net/vmw_vsock/virtio_transport_common.c +61 -1
104 lines not on this page · raw message
net/vmw_vsock/vsock_loopback.c +1 -0
@@ -134,6 +134,7 @@ static void vsock_loopback_work(struct work_struct *work)spin_unlock_bh (&vsock->pkt_queue.lock);while ((skb =__skb_dequeue (&pkts))) { +virtio_transport_credit_update_xmit (skb);/* Decrement the bytes_unsent counter without deallocating skb * It is freed by the receiver. */
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH net 3/3] vsock/virtio: queue at most one credit update per socket
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] vsock/virtio: premature credit_update clear in virtio_transport_send_skb() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] vsock/virtio: premature credit_update clear in virtio_transport_send_skb() --
commit 2cdb1a5b77890d0e5001ce1a907ca3477d945d5d
Author: Oleg Drokin <[email protected]>
vsock/virtio: queue at most one credit update per socket
This commit limits each vsock socket to at most one queued credit update packet when its last queued packet is already an OP_CREDIT_UPDATE. When the transport transmits the queued credit update and no newer packet has been queued after it, virtio_transport_credit_update_xmit() refreshes its fwd_cnt and buf_alloc and clears vvs->credit_update.
net/vmw_vsock/virtio_transport.c static int virtio_transport_send_skb(struct sk_buff *skb, struct virtqueue *vq, · line 111
Quoted 13 lines
net/vmw_vsock/virtio_transport.c +2
-0@@ -104,6 +104,8 @@ static int virtio_transport_send_skb(struct sk_buff *skb, struct virtqueue *vq,int ret, in_sg =0 , out_sg =0 ;struct scatterlist **sgs; +virtio_transport_credit_update_xmit (skb); + sgs = vsock->out_sgs;sg_init_one (sgs[out_sg],virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb)));
[Severity: High]
Does calling virtio_transport_credit_update_xmit(skb) before virtqueue_add_sgs() clear vvs->credit_update and advance vvs->last_fwd_cnt even when adding the packet to the tx virtqueue fails?
In virtio_transport_send_skb(), virtqueue_add_sgs() can fail (for example with -ENOSPC when VSOCK_VQ_TX is full, or -ENOMEM under GFP_ATOMIC on the fast path):
net/vmw_vsock/virtio_transport.c:virtio_transport_send_skb() {
virtio_transport_credit_update_xmit(skb);
...
ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, skb, gfp);
if (ret < 0)
return ret;
}When that happens, virtio_transport_credit_update_xmit(skb) has already set vvs->credit_update = NULL and vvs->last_fwd_cnt = vvs->fwd_cnt, and the caller then queues or requeues skb in vsock->send_pkt_queue:
- via virtio_transport_send_pkt() -> virtio_transport_send_skb_fast_path(), which falls back to virtio_vsock_skb_queue_tail(&vsock->send_pkt_queue, skb) when vsock->send_pkt_queue was initially empty, or
- via virtio_transport_send_pkt_work(), which puts skb back with virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb).
While skb sits in vsock->send_pkt_queue with vvs->credit_update cleared to NULL, subsequent calls to virtio_transport_send_credit_update() (from OP_CREDIT_REQUEST or local reads) will queue additional OP_CREDIT_UPDATE skbs behind it. In addition, when skb is later sent by virtio_transport_send_pkt_work(), virtio_transport_credit_update_xmit(skb) sees vvs->credit_update != skb and will not refresh hdr->fwd_cnt or hdr->buf_alloc with credits freed while it was queued.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Michael S. Tsirkin
maintainerRe: [PATCH net 3/3] vsock/virtio: queue at most one credit update per socket
In reply to Oleg DrokinQuoted 68 lines · Oleg Drokin
A credit update carries no data, so the send credit never limits it, and the peer can solicit one at will: with
OP_CREDIT_REQUEST, or by sending data that a local reader drains (a read that empties the receive queue sends one). A peer that posts no rx buffers can therefore make the other side queue credit updates without limit, about 1 KiB of unaccounted slab each for a 44-byte packet.Do not queue a credit update while the socket's last queued packet is already one. Every packet carries
fwd_cntandbuf_alloc, so a packet queued later makes the queued update redundant and lets a new one be queued after it. When a transport puts a queued update on the wire (vhost once it has an rx buffer for it, the guest driver when it adds it to the tx virtqueue, loopback when it delivers it) and nothing has been queued after it, it refreshes the update'sfwd_cntandbuf_allocand releases the socket. So a suppressed update always reaches the peer, either in the queued one or in a later packet, and thefwd_cntthe peer sees never goes back.
connect()can move a socket to another transport, freeing itsvirtio_vsock_sock, while packets are still queued in the old one. So a tracked update holds a reference on thevirtio_vsock_sock, and its skb destructor clears the socket's pointer to it and drops the reference, however the skb is freed: sent, purged or dropped.A peer can thus make a connection queue at most one credit update, plus one per packet the socket itself queued after it.
Counting credit updates in
queued_repliesinstead would not work: most of them are sent by local reads, not in answer to the peer, and under bidirectional traffic vhost would stop reading the guest's tx queue while the guest stops reading its rx queue, each waiting for the other.Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")Signed-off-by Oleg Drokin <[email protected]>--- drivers/vhost/vsock.c | 2 + include/linux/virtio_vsock.h | 5 ++ net/vmw_vsock/virtio_transport.c | 2 + net/vmw_vsock/virtio_transport_common.c | 62 ++++++++++++++++++++++++- net/vmw_vsock/vsock_loopback.c | 1 + 5 files changed, 71 insertions(+), 1 deletion(-)drivers/vhost/vsock.c +2
-0@@ -176,6 +176,8 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,break ; } +virtio_transport_credit_update_xmit (skb); +if (out) { restart_tx |=vhost_vsock_drop_skb (vsock, skb);vq_err (vq,"Expected 0 output buffers, got %u\n" , out);include/linux/virtio_vsock.h +1
-0@@ -13,6 +13,7 @@ struct virtio_vsock_skb_cb {bool reply;bool tap_delivered;u32 offset; +struct virtio_vsock_sock *vvs; };#define VIRTIO_VSOCK_SKB_CB (skb) ((struct virtio_vsock_skb_cb *)((skb)->cb))
include/linux/virtio_vsock.h struct virtio_vsock_sock { · line 173
Quoted 6 lines
include/linux/virtio_vsock.h +3
-0@@ -167,6 +168,9 @@ struct virtio_vsock_sock {u32 buf_used;struct sk_buff_head rx_queue;u32 msg_count; +struct sk_buff *credit_update; + +refcount_t refcnt;
why all this complexity? can you not just peek at the queue tail instead?
Quoted 151 lines
include/linux/virtio_vsock.h +1
-0};struct virtio_vsock_pkt_info {@@ -283,6 +287,7 @@ void virtio_transport_destruct(struct vsock_sock *vsk);void virtio_transport_recv_pkt (struct virtio_transport *t,struct sk_buff *skb,struct net *net);void virtio_transport_inc_tx_pkt (struct virtio_vsock_sock *vvs,struct sk_buff *skb); +void virtio_transport_credit_update_xmit (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);void virtio_transport_deliver_tap_pkt (struct sk_buff *skb);net/vmw_vsock/virtio_transport.c +2
-0@@ -104,6 +104,8 @@ static int virtio_transport_send_skb(struct sk_buff *skb, struct virtqueue *vq,int ret, in_sg =0 , out_sg =0 ;struct scatterlist **sgs; +virtio_transport_credit_update_xmit (skb); + sgs = vsock->out_sgs;sg_init_one (sgs[out_sg],virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb)));net/vmw_vsock/virtio_transport_common.c +61
-1@@ -604,18 +604,68 @@ static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs, vvs->fwd_cnt += bytes_dequeued; } +static void virtio_transport_put_vvs (struct virtio_vsock_sock *vvs) +{ +if (refcount_dec_and_test (&vvs->refcnt)) +kfree (vvs); +} + +static void virtio_transport_credit_update_destruct (struct sk_buff *skb) +{ +struct virtio_vsock_sock *vvs =VIRTIO_VSOCK_SKB_CB (skb)->vvs; + +spin_lock_bh (&vvs->rx_lock); +if (vvs->credit_update == skb) + vvs->credit_update =NULL ; +spin_unlock_bh (&vvs->rx_lock); + +virtio_transport_put_vvs (vvs); +sock_wfree (skb); +} +void virtio_transport_inc_tx_pkt (struct virtio_vsock_sock *vvs,struct sk_buff *skb) {struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb); +bool track =le16_to_cpu (hdr->op) ==VIRTIO_VSOCK_OP_CREDIT_UPDATE && + skb->destructor == sock_wfree; + +/* The skb pins vvs, which connect() may detach from the socket. */ +if (track) { +refcount_inc (&vvs->refcnt); +VIRTIO_VSOCK_SKB_CB (skb)->vvs = vvs; + skb->destructor = virtio_transport_credit_update_destruct; + }spin_lock_bh (&vvs->rx_lock); vvs->last_fwd_cnt = vvs->fwd_cnt; hdr->fwd_cnt =cpu_to_le32 (vvs->fwd_cnt); hdr->buf_alloc =cpu_to_le32 (vvs->buf_alloc); + vvs->credit_update = track ? skb :NULL ;spin_unlock_bh (&vvs->rx_lock); }EXPORT_SYMBOL_GPL (virtio_transport_inc_tx_pkt); +/* Called by the transports when @skb is put on the wire. */ +void virtio_transport_credit_update_xmit (struct sk_buff *skb) +{ +struct virtio_vsock_sock *vvs =VIRTIO_VSOCK_SKB_CB (skb)->vvs; +struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb); + +if (!vvs) +return ; + +spin_lock_bh (&vvs->rx_lock); +/* Still the socket's last packet: carry what was suppressed. */ +if (vvs->credit_update == skb) { + vvs->credit_update =NULL ; + vvs->last_fwd_cnt = vvs->fwd_cnt; + hdr->fwd_cnt =cpu_to_le32 (vvs->fwd_cnt); + hdr->buf_alloc =cpu_to_le32 (vvs->buf_alloc); + } +spin_unlock_bh (&vvs->rx_lock); +} +EXPORT_SYMBOL_GPL (virtio_transport_credit_update_xmit); +void virtio_transport_consume_skb_sent (struct sk_buff *skb,bool consume) {struct sock *s = skb->sk;@@ -667,11 +717,20 @@ EXPORT_SYMBOL_GPL(virtio_transport_put_credit);static int virtio_transport_send_credit_update (struct vsock_sock *vsk) { +struct virtio_vsock_sock *vvs = vsk->trans;struct virtio_vsock_pkt_info info = { .op =VIRTIO_VSOCK_OP_CREDIT_UPDATE , .vsk = vsk, .net =sock_net (sk_vsock (vsk)), }; +bool queued; + +spin_lock_bh (&vvs->rx_lock); + queued = vvs->credit_update; +spin_unlock_bh (&vvs->rx_lock); + +if (queued) +return 0 ;return virtio_transport_send_pkt_info (vsk, &info); }@@ -1086,6 +1145,7 @@ int virtio_transport_do_socket_init(struct vsock_sock *vsk,spin_lock_init (&vvs->rx_lock);spin_lock_init (&vvs->tx_lock);skb_queue_head_init (&vvs->rx_queue); +refcount_set (&vvs->refcnt,1 );return 0 ; }@@ -1276,8 +1336,8 @@ void virtio_transport_destruct(struct vsock_sock *vsk)virtio_transport_cancel_close_work (vsk, true); -kfree (vvs); vsk->trans =NULL ; +virtio_transport_put_vvs (vvs); }EXPORT_SYMBOL_GPL (virtio_transport_destruct);net/vmw_vsock/vsock_loopback.c +1
-0@@ -134,6 +134,7 @@ static void vsock_loopback_work(struct work_struct *work)spin_unlock_bh (&vsock->pkt_queue.lock);while ((skb =__skb_dequeue (&pkts))) { +virtio_transport_credit_update_xmit (skb);/* Decrement the bytes_unsent counter without deallocating skb * It is freed by the receiver. */ Signature · 1 line
2.56.0
Re: [PATCH net 3/3] vsock/virtio: queue at most one credit update per socket
Quoted 15 lines · Michael S. Tsirkin
include/linux/virtio_vsock.h struct virtio_vsock_sock { · line 173
Quoted 11 lines · Oleg Drokin
#defineVIRTIO_VSOCK_SKB_CB(skb)((structvirtio_vsock_skb_cb*)((skb)->cb))include/linux/virtio_vsock.h +3
-0@@ -167,6 +168,9 @@ struct virtio_vsock_sock { u32 buf_used; struct sk_buff_head rx_queue; u32 msg_count; +struct sk_buff *credit_update; + +refcount_t refcnt;why all this complexity? can you not just peek at the queue tail instead?
Thanks for the suggestion. The send queues are per device, not per
socket: vhost has one per guest, and the guest driver and loopback have one each. A peer with two connections can alternate its credit requests between them, so the tail is always the other connection's update and nothing would ever be merged.
v2 reworks this third patch:vsock_sock remembers its newest queued packet while that packet is a credit update, together with the queue it is in.
The refcount, the skb destructor and the transmit hooks are gone, and vhost and the guest driver are not touched.
[email protected]
AI reviewRe: [PATCH net 3/3] vsock/virtio: queue at most one credit update per socket
In reply to Oleg DrokinThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
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).
- [Medium] Both the guest and vhost transports call virtio_transport_credit_update_xmit() before the packet is actually…
--
vsock/virtio: queue at most one credit update per socket
This patch limits how many credit update packets each virtio vsock socket can have queued. It tracks the last queued credit update skb in virtio_vsock_sock and suppresses new ones while that skb is pending. Transports call a new virtio_transport_credit_update_xmit() hook to refresh fwd_cnt/buf_alloc on the tracked skb and release tracking when it is sent. A tracked skb holds a refcount on virtio_vsock_sock to keep it alive.
[ ... ]
Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +0
-0@@ -604,18 +604,68 @@ static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
[ ... ]
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10
-0+/* Called by the transports when @skb is put on the wire. */ +void virtio_transport_credit_update_xmit (struct sk_buff *skb) +{ +struct virtio_vsock_sock *vvs =VIRTIO_VSOCK_SKB_CB (skb)->vvs; +struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb); + +if (!vvs) +return ; + +spin_lock_bh (&vvs->rx_lock);
net/vmw_vsock/virtio_transport_common.c static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs, · line 667
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10
-0+/* Still the socket's last packet: carry what was suppressed. */ +if (vvs->credit_update == skb) { + vvs->credit_update =NULL ; + vvs->last_fwd_cnt = vvs->fwd_cnt; + hdr->fwd_cnt =cpu_to_le32 (vvs->fwd_cnt); + hdr->buf_alloc =cpu_to_le32 (vvs->buf_alloc); + } +spin_unlock_bh (&vvs->rx_lock); +} +EXPORT_SYMBOL_GPL (virtio_transport_credit_update_xmit);
[Severity: Medium]
This hook can't be undone once it has run. It clears vvs->credit_update, advances vvs->last_fwd_cnt and rewrites the header. Do the guest and vhost transports call it before the packet is actually committed?
In the guest driver, virtio_transport_send_skb() calls it before virtqueue_add_sgs(), and that call fails when the tx vq is full:
net/vmw_vsock/virtio_transport.c:virtio_transport_send_skb() {
virtio_transport_credit_update_xmit(skb);
...
ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, skb, gfp);
if (ret < 0)
return ret;
...
}virtio_transport_send_pkt_work() then puts the skb back at the head:
ret = virtio_transport_send_skb(skb, vq, vsock, GFP_KERNEL);
if (ret < 0) {
virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb);
break;
}When the fast path fails, virtio_transport_send_pkt() queues it at the tail instead.
The requeued skb is no longer tracked. So the next virtio_transport_send_credit_update() is not suppressed, and it queues a second, tracked credit update behind the first.
Doesn't that make the per-socket bound two, rather than the "at most one credit update" the commit message describes? Any later updates are still suppressed by the second tracked one, so the unbounded queuing stays fixed.
In vhost, vhost_transport_do_send_pkt() calls the hook right after it gets a descriptor, before the checks that drop the skb:
virtio_transport_credit_update_xmit(skb);
if (out) {
restart_tx |= vhost_vsock_drop_skb(vsock, skb);
...
}
iov_len = iov_length(&vq->iov[out], in);
if (iov_len < sizeof(*hdr)) {
restart_tx |= vhost_vsock_drop_skb(vsock, skb);
...The copy_to_iter() and skb_copy_datagram_iter() fault paths drop the skb in the same way.
Suppose updates were suppressed while this one was queued. Their fwd_cnt has been merged into this skb and recorded in last_fwd_cnt, but the guest never receives this skb.
Before this patch, the suppressed update would have been a separate skb, delivered on the next descriptor. Now nothing resends it until more local reads happen. Could the guest-side sender then stall waiting for credit?
Moving the hook after these checks would not be enough by itself, since the suppressed state is lost whenever a tracked update is dropped.
The commit message says:
So a suppressed update always reaches the
peer, either in the queued one or in a later packet, and the fwd_cnt the peer sees never goes back.
Is that still true on these drop paths?
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261105000141.3617241-1-green%40linuxhacker.ru
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Hello!
This was discovered while working on porting a project from xen to kvm hypervisor. One of the llm security reviews demonstrated it as valid, so it's not purely a static analysis, It was also discovered that a partial patch exists that already was included yesterday, by Daehyeon Ko independently posted the same
core fix - Commit: d5e912448cd9 "vhost/vsock: size receive SKBs from declared payload".
Quoted 1 line
Quoted 17 lines · On Thu, 2026-11-05 at 00:03 +0000, [email protected] wrote:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.