Skip to contentSkip to conversation

vsock/virtio: fix skb overhead accounting to preserve full buf_alloc

PATCH Superseded Stefano Garzarella 2 participants 12 messages May 13 – May 15

Subsystems (6)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Stefano Garzarella

author maintainer
#0 lore raw

v3 cover vsock/virtio: fix skb overhead accounting to preserve full buf_alloc

The code is exactly the same as in v2, but split into two patches as Michael suggested in v2:
- Patch 1 resets the connection when we can no longer queue packets, this prevents silent data loss, and both peers are notified.
- Patch 2 increases the total budget to buf_alloc * 2 for payload plus skb overhead similar to how SO_RCVBUF is doubled to reserve space for sk_buff metadata. This preserves the full buf_alloc for payload under normal operation, while still bounding the skb queue growth.

v3:
- Split in 2 patches [MST]

v2: https://lore.kernel.org/netdev/[email protected]/
- Close the connection when we can no longer queue new packets instead of losing data.
- No longer announce the reduced buf_alloc to avoid violating the spec. [MST]

v1: https://lore.kernel.org/netdev/[email protected]/

Stefano Garzarella (2):
vsock/virtio: reset connection on receiving queue overflow vsock/virtio: fix skb overhead accounting to preserve full buf_alloc

 net/vmw_vsock/virtio_transport_common.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v3 1/2 vsock/virtio: reset connection on receiving queue overflow

From: Stefano Garzarella <[email protected]>

When there is no more space to queue an incoming packet, the packet is silently dropped. This causes data loss without any notification to either peer, since there is no retransmission.

Under normal circumstances, this should never happen. However, it could happen if the other peer doesn't respect the credit, or if the skb overhead, which we recently began to take into account with commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), is too high.

Fix this by resetting the connection and setting the local socket error to ENOBUFS when virtio_transport_recv_enqueue() can no longer queue a packet, so both peers are explicitly notified of the failure rather than silently losing data.

Fixes ae6fcfbf5f03 ("vsock/virtio: discard packets if credit is not respected")
---
 net/vmw_vsock/virtio_transport_common.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
net/vmw_vsock/virtio_transport_common.c +14 -5
index 989cc252d3d3..4a4ac69d1ad1 100644
@@ -1350,7 +1350,7 @@ virtio_transport_recv_connecting(struct sock *sk,
 	return err;
 }
 
-static void
+static bool
 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 			      struct sk_buff *skb)
 {
@@ -1365,10 +1365,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 	spin_lock_bh(&vvs->rx_lock);
 
 	can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
-	if (!can_enqueue) {
-		free_pkt = true;
+	if (!can_enqueue)
 		goto out;
-	}
 
 	if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
 		vvs->msg_count++;
@@ -1408,6 +1406,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 	spin_unlock_bh(&vvs->rx_lock);
 	if (free_pkt)
 		kfree_skb(skb);
+
+	return can_enqueue;
 }
 
 static int
@@ -1420,7 +1420,16 @@ virtio_transport_recv_connected(struct sock *sk,
 
 	switch (le16_to_cpu(hdr->op)) {
 	case VIRTIO_VSOCK_OP_RW:
-		virtio_transport_recv_enqueue(vsk, skb);
+		if (!virtio_transport_recv_enqueue(vsk, skb)) {
+			/* There is no more space to queue the packet, so let's
+			 * close the connection; otherwise, we'll lose data.
+			 */
+			(void)virtio_transport_reset(vsk, skb);
+			sk->sk_state = TCP_CLOSE;
+			sk->sk_err = ENOBUFS;
+			sk_error_report(sk);
+			break;
+		}
 		vsock_data_ready(sk);
 		return err;
 	case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 60 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

When there is no more space to queue an incoming packet, the packet is silently dropped. This causes data loss without any notification to either peer, since there is no retransmission.

Under normal circumstances, this should never happen. However, it could happen if the other peer doesn't respect the credit, or if the skb overhead, which we recently began to take into account with commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), is too high.

Fix this by resetting the connection and setting the local socket error to ENOBUFS when virtio_transport_recv_enqueue() can no longer queue a packet, so both peers are explicitly notified of the failure rather than silently losing data.

Fixes ae6fcfbf5f03 ("vsock/virtio: discard packets if credit is not respected")

---
net/vmw_vsock/virtio_transport_common.c | 19 ++++++++++++++-----

1 file changed, 14 insertions(+), 5 deletions(-)
net/vmw_vsock/virtio_transport_common.c +4 -4
index 989cc252d3d3..4a4ac69d1ad1 100644
@@ -1350,7 +1350,7 @@ virtio_transport_recv_connecting(struct sock *sk,
 	return err;
 }
 
-static void
+static bool
 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 			      struct sk_buff *skb)
 {
@@ -1365,10 +1365,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 	spin_lock_bh(&vvs->rx_lock);
 
 	can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
-	if (!can_enqueue) {
-		free_pkt = true;
+	if (!can_enqueue)
 		goto out;
-	}
 
 	if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
 		vvs->msg_count++;
@@ -1408,6 +1406,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 	spin_unlock_bh(&vvs->rx_lock);
 	if (free_pkt)
 		kfree_skb(skb);
+
+	return can_enqueue;
 }
 

static int

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -1420,7 +1420,16 @@ virtio_transport_recv_connected(struct sock *sk,
 
 	switch (le16_to_cpu(hdr->op)) {

net/vmw_vsock/virtio_transport_common.c virtio_transport_recv_connected(struct sock *sk, · line 1430

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8 -1
 	case VIRTIO_VSOCK_OP_RW:
-		virtio_transport_recv_enqueue(vsk, skb);
+		if (!virtio_transport_recv_enqueue(vsk, skb)) {
+			/* There is no more space to queue the packet, so let's
+			 * close the connection; otherwise, we'll lose data.
+			 */
+			(void)virtio_transport_reset(vsk, skb);
+			sk->sk_state = TCP_CLOSE;
+			sk->sk_err = ENOBUFS;
+			sk_error_report(sk);

sashiko reported some issues related to setting TCP_CLOSE state and not removing the socket from the connect table:
https://sashiko.dev/#/patchset/20260513105417.56761-1-sgarzare%40redhat.com

I'll change this by calling virtio_transport_do_close() and vsock_remove_sock() in the next version.

Stefano

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +2 -0
+			break;
+		}
 		vsock_data_ready(sk);
 		return err;
 	case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 81 lines · Stefano Garzarella
Quoted 60 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

When there is no more space to queue an incoming packet, the packet is silently dropped. This causes data loss without any notification to either peer, since there is no retransmission.

Under normal circumstances, this should never happen. However, it could happen if the other peer doesn't respect the credit, or if the skb overhead, which we recently began to take into account with commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), is too high.

Fix this by resetting the connection and setting the local socket error to ENOBUFS when virtio_transport_recv_enqueue() can no longer queue a packet, so both peers are explicitly notified of the failure rather than silently losing data.

Fixes ae6fcfbf5f03 ("vsock/virtio: discard packets if credit is not respected")

---
net/vmw_vsock/virtio_transport_common.c | 19 ++++++++++++++-----

1 file changed, 14 insertions(+), 5 deletions(-)
net/vmw_vsock/virtio_transport_common.c +4 -4
index 989cc252d3d3..4a4ac69d1ad1 100644
@@ -1350,7 +1350,7 @@ virtio_transport_recv_connecting(struct sock *sk,
 	return err;
 }
 
-static void
+static bool
 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 			      struct sk_buff *skb)
 {
@@ -1365,10 +1365,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 	spin_lock_bh(&vvs->rx_lock);
 
 	can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
-	if (!can_enqueue) {
-		free_pkt = true;
+	if (!can_enqueue)
 		goto out;
-	}
 
 	if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
 		vvs->msg_count++;
@@ -1408,6 +1406,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 	spin_unlock_bh(&vvs->rx_lock);
 	if (free_pkt)
 		kfree_skb(skb);
+
+	return can_enqueue;
 }
 

static int

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -1420,7 +1420,16 @@ virtio_transport_recv_connected(struct sock *sk,
 
 	switch (le16_to_cpu(hdr->op)) {

net/vmw_vsock/virtio_transport_common.c virtio_transport_recv_connected(struct sock *sk, · line 1430

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8 -1
 	case VIRTIO_VSOCK_OP_RW:
-		virtio_transport_recv_enqueue(vsk, skb);
+		if (!virtio_transport_recv_enqueue(vsk, skb)) {
+			/* There is no more space to queue the packet, so let's
+			 * close the connection; otherwise, we'll lose data.
+			 */
+			(void)virtio_transport_reset(vsk, skb);
+			sk->sk_state = TCP_CLOSE;
+			sk->sk_err = ENOBUFS;
+			sk_error_report(sk);

sashiko reported some issues related to setting TCP_CLOSE state and not removing the socket from the connect table:
https://sashiko.dev/#/patchset/20260513105417.56761-1-sgarzare%40redhat.com

I'll change this by calling virtio_transport_do_close() and vsock_remove_sock() in the next version.

Stefano

Quoted 8 lines
Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +2 -0
+			break;
+		}
 		vsock_data_ready(sk);
 		return err;
 	case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
Signature · 1 line
2.54.0

And so the bag of hacks grows. I feel this is energy not well spent. Please, let us fix this properly *first*. And then worry about how to backport. Maybe it will not be so terrible to backport after all.

author maintainer
#4 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 93 lines · Michael S. Tsirkin
Quoted 76 lines · Stefano Garzarella
Quoted 60 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

When there is no more space to queue an incoming packet, the packet is silently dropped. This causes data loss without any notification to either peer, since there is no retransmission.

Under normal circumstances, this should never happen. However, it could happen if the other peer doesn't respect the credit, or if the skb overhead, which we recently began to take into account with commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), is too high.

Fix this by resetting the connection and setting the local socket error to ENOBUFS when virtio_transport_recv_enqueue() can no longer queue a packet, so both peers are explicitly notified of the failure rather than silently losing data.

Fixes ae6fcfbf5f03 ("vsock/virtio: discard packets if credit is not respected")

---
net/vmw_vsock/virtio_transport_common.c | 19 ++++++++++++++-----

1 file changed, 14 insertions(+), 5 deletions(-)
net/vmw_vsock/virtio_transport_common.c +4 -4
index 989cc252d3d3..4a4ac69d1ad1 100644
@@ -1350,7 +1350,7 @@ virtio_transport_recv_connecting(struct sock *sk,
    return err;
 }
 
-static void
+static bool
 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
                          struct sk_buff *skb)
 {
@@ -1365,10 +1365,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_lock_bh(&vvs->rx_lock);
 
    can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
-   if (!can_enqueue) {
-           free_pkt = true;
+   if (!can_enqueue)
            goto out;
-   }
 
    if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
            vvs->msg_count++;
@@ -1408,6 +1406,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_unlock_bh(&vvs->rx_lock);
    if (free_pkt)
            kfree_skb(skb);
+
+   return can_enqueue;
 }
 

static int

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -1420,7 +1420,16 @@ virtio_transport_recv_connected(struct sock *sk,
 
    switch (le16_to_cpu(hdr->op)) {

net/vmw_vsock/virtio_transport_common.c virtio_transport_recv_connected(struct sock *sk, · line 1430

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8 -1
    case VIRTIO_VSOCK_OP_RW:
-           virtio_transport_recv_enqueue(vsk, skb);
+           if (!virtio_transport_recv_enqueue(vsk, skb)) {
+                   /* There is no more space to queue the packet, so let's
+                    * close the connection; otherwise, we'll lose data.
+                    */
+                   (void)virtio_transport_reset(vsk, skb);
+                   sk->sk_state = TCP_CLOSE;
+                   sk->sk_err = ENOBUFS;
+                   sk_error_report(sk);

sashiko reported some issues related to setting TCP_CLOSE state and not removing the socket from the connect table:
https://sashiko.dev/#/patchset/20260513105417.56761-1-sgarzare%40redhat.com

Quoted 13 lines

I'll change this by calling virtio_transport_do_close() and vsock_remove_sock() in the next version.

Stefano

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
+                   break;
+           }
            vsock_data_ready(sk);
            return err;
    case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
--

2.54.0

Quoted 4 lines

And so the bag of hacks grows. I feel this is energy not well spent. Please, let us fix this properly *first*. And then worry about how to backport. Maybe it will not be so terrible to backport after all.

TBH I don't think this is an hack, but an issue we should fix in any case. Regarding the second patch, I see your point, but it's a big change that worries me. I'd like some more time to fix it properly without rushing. Staying calm without realizing that userspace is broken like we are now without this series :-(

That said, evaluating further, I think we have a similar issue also with STREAM on the host side where the skb usually doesn't free space, so we need a merge strategy also there.

So, I'd like to have time to fix both definitely. If you have time and want to go ahead, please do.

Thanks,
Stefano

#5 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 109 lines · Stefano Garzarella
Quoted 93 lines · Michael S. Tsirkin
Quoted 76 lines · Stefano Garzarella
Quoted 60 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

When there is no more space to queue an incoming packet, the packet is silently dropped. This causes data loss without any notification to either peer, since there is no retransmission.

Under normal circumstances, this should never happen. However, it could happen if the other peer doesn't respect the credit, or if the skb overhead, which we recently began to take into account with commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), is too high.

Fix this by resetting the connection and setting the local socket error to ENOBUFS when virtio_transport_recv_enqueue() can no longer queue a packet, so both peers are explicitly notified of the failure rather than silently losing data.

Fixes ae6fcfbf5f03 ("vsock/virtio: discard packets if credit is not respected")

---
net/vmw_vsock/virtio_transport_common.c | 19 ++++++++++++++-----

1 file changed, 14 insertions(+), 5 deletions(-)
net/vmw_vsock/virtio_transport_common.c +4 -4
index 989cc252d3d3..4a4ac69d1ad1 100644
@@ -1350,7 +1350,7 @@ virtio_transport_recv_connecting(struct sock *sk,
    return err;
 }
 
-static void
+static bool
 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
                          struct sk_buff *skb)
 {
@@ -1365,10 +1365,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_lock_bh(&vvs->rx_lock);
 
    can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
-   if (!can_enqueue) {
-           free_pkt = true;
+   if (!can_enqueue)
            goto out;
-   }
 
    if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
            vvs->msg_count++;
@@ -1408,6 +1406,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_unlock_bh(&vvs->rx_lock);
    if (free_pkt)
            kfree_skb(skb);
+
+   return can_enqueue;
 }
 

static int

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -1420,7 +1420,16 @@ virtio_transport_recv_connected(struct sock *sk,
 
    switch (le16_to_cpu(hdr->op)) {

net/vmw_vsock/virtio_transport_common.c virtio_transport_recv_connected(struct sock *sk, · line 1430

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8 -1
    case VIRTIO_VSOCK_OP_RW:
-           virtio_transport_recv_enqueue(vsk, skb);
+           if (!virtio_transport_recv_enqueue(vsk, skb)) {
+                   /* There is no more space to queue the packet, so let's
+                    * close the connection; otherwise, we'll lose data.
+                    */
+                   (void)virtio_transport_reset(vsk, skb);
+                   sk->sk_state = TCP_CLOSE;
+                   sk->sk_err = ENOBUFS;
+                   sk_error_report(sk);

sashiko reported some issues related to setting TCP_CLOSE state and not removing the socket from the connect table:
https://sashiko.dev/#/patchset/20260513105417.56761-1-sgarzare%40redhat.com

Quoted 13 lines

I'll change this by calling virtio_transport_do_close() and vsock_remove_sock() in the next version.

Stefano

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
+                   break;
+           }
            vsock_data_ready(sk);
            return err;
    case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
--

2.54.0

Quoted 4 lines

And so the bag of hacks grows. I feel this is energy not well spent. Please, let us fix this properly *first*. And then worry about how to backport. Maybe it will not be so terrible to backport after all.

TBH I don't think this is an hack, but an issue we should fix in any case. Regarding the second patch, I see your point, but it's a big change that worries me. I'd like some more time to fix it properly without rushing. Staying calm without realizing that userspace is broken like we are now without this series :-(

That said, evaluating further, I think we have a similar issue also with STREAM on the host side where the skb usually doesn't free space, so we need a merge strategy also there.

Quoted 5 lines

So, I'd like to have time to fix both definitely. If you have time and want to go ahead, please do.

Thanks,
Stefano

Well my patch was a start, we just need a strategy how to avoid copying everything, right?

Signature · 1 line
MST
author maintainer
#6 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 110 lines · Michael S. Tsirkin
Quoted 109 lines · Stefano Garzarella
Quoted 93 lines · Michael S. Tsirkin
Quoted 76 lines · Stefano Garzarella
Quoted 60 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

When there is no more space to queue an incoming packet, the packet is silently dropped. This causes data loss without any notification to either peer, since there is no retransmission.

Under normal circumstances, this should never happen. However, it could happen if the other peer doesn't respect the credit, or if the skb overhead, which we recently began to take into account with commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), is too high.

Fix this by resetting the connection and setting the local socket error to ENOBUFS when virtio_transport_recv_enqueue() can no longer queue a packet, so both peers are explicitly notified of the failure rather than silently losing data.

Fixes ae6fcfbf5f03 ("vsock/virtio: discard packets if credit is not respected")

---
net/vmw_vsock/virtio_transport_common.c | 19 ++++++++++++++-----

1 file changed, 14 insertions(+), 5 deletions(-)
net/vmw_vsock/virtio_transport_common.c +4 -4
index 989cc252d3d3..4a4ac69d1ad1 100644
@@ -1350,7 +1350,7 @@ virtio_transport_recv_connecting(struct sock *sk,
    return err;
 }
 
-static void
+static bool
 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
                          struct sk_buff *skb)
 {
@@ -1365,10 +1365,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_lock_bh(&vvs->rx_lock);
 
    can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
-   if (!can_enqueue) {
-           free_pkt = true;
+   if (!can_enqueue)
            goto out;
-   }
 
    if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
            vvs->msg_count++;
@@ -1408,6 +1406,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_unlock_bh(&vvs->rx_lock);
    if (free_pkt)
            kfree_skb(skb);
+
+   return can_enqueue;
 }
 

static int

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -1420,7 +1420,16 @@ virtio_transport_recv_connected(struct sock *sk,
 
    switch (le16_to_cpu(hdr->op)) {

net/vmw_vsock/virtio_transport_common.c virtio_transport_recv_connected(struct sock *sk, · line 1430

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8 -1
    case VIRTIO_VSOCK_OP_RW:
-           virtio_transport_recv_enqueue(vsk, skb);
+           if (!virtio_transport_recv_enqueue(vsk, skb)) {
+                   /* There is no more space to queue the packet, so let's
+                    * close the connection; otherwise, we'll lose data.
+                    */
+                   (void)virtio_transport_reset(vsk, skb);
+                   sk->sk_state = TCP_CLOSE;
+                   sk->sk_err = ENOBUFS;
+                   sk_error_report(sk);

sashiko reported some issues related to setting TCP_CLOSE state and not removing the socket from the connect table:
https://sashiko.dev/#/patchset/20260513105417.56761-1-sgarzare%40redhat.com

Quoted 13 lines

I'll change this by calling virtio_transport_do_close() and vsock_remove_sock() in the next version.

Stefano

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
+                   break;
+           }
            vsock_data_ready(sk);
            return err;
    case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
--

2.54.0

Quoted 4 lines

And so the bag of hacks grows. I feel this is energy not well spent. Please, let us fix this properly *first*. And then worry about how to backport. Maybe it will not be so terrible to backport after all.

TBH I don't think this is an hack, but an issue we should fix in any case. Regarding the second patch, I see your point, but it's a big change that worries me. I'd like some more time to fix it properly without rushing. Staying calm without realizing that userspace is broken like we are now without this series :-(

That said, evaluating further, I think we have a similar issue also with STREAM on the host side where the skb usually doesn't free space, so we need a merge strategy also there.

Quoted 8 lines
Quoted 5 lines

So, I'd like to have time to fix both definitely. If you have time and want to go ahead, please do.

Thanks,
Stefano

Well my patch was a start, we just need a strategy how to avoid copying everything, right?

Yep, and then there's the question of how to handle EOM without a payload, but I think that's a special case. In theory, we don't support sending it, but I'm not sure if POSIX allows it or not.

That said, is it okay if I send a v4 of this series?

(I'm not sure if I'll be able to work on the merging next week)

Stefano

#7 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 117 lines · Stefano Garzarella
Quoted 116 lines · Michael S. Tsirkin
Quoted 114 lines · Stefano Garzarella
Quoted 93 lines · Michael S. Tsirkin
Quoted 76 lines · Stefano Garzarella
Quoted 60 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

When there is no more space to queue an incoming packet, the packet is silently dropped. This causes data loss without any notification to either peer, since there is no retransmission.

Under normal circumstances, this should never happen. However, it could happen if the other peer doesn't respect the credit, or if the skb overhead, which we recently began to take into account with commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), is too high.

Fix this by resetting the connection and setting the local socket error to ENOBUFS when virtio_transport_recv_enqueue() can no longer queue a packet, so both peers are explicitly notified of the failure rather than silently losing data.

Fixes ae6fcfbf5f03 ("vsock/virtio: discard packets if credit is not respected")

---
net/vmw_vsock/virtio_transport_common.c | 19 ++++++++++++++-----

1 file changed, 14 insertions(+), 5 deletions(-)
net/vmw_vsock/virtio_transport_common.c +4 -4
index 989cc252d3d3..4a4ac69d1ad1 100644
@@ -1350,7 +1350,7 @@ virtio_transport_recv_connecting(struct sock *sk,
    return err;
 }
 
-static void
+static bool
 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
                          struct sk_buff *skb)
 {
@@ -1365,10 +1365,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_lock_bh(&vvs->rx_lock);
 
    can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
-   if (!can_enqueue) {
-           free_pkt = true;
+   if (!can_enqueue)
            goto out;
-   }
 
    if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
            vvs->msg_count++;
@@ -1408,6 +1406,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
    spin_unlock_bh(&vvs->rx_lock);
    if (free_pkt)
            kfree_skb(skb);
+
+   return can_enqueue;
 }
 

static int

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -1420,7 +1420,16 @@ virtio_transport_recv_connected(struct sock *sk,
 
    switch (le16_to_cpu(hdr->op)) {

net/vmw_vsock/virtio_transport_common.c virtio_transport_recv_connected(struct sock *sk, · line 1430

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +8 -1
    case VIRTIO_VSOCK_OP_RW:
-           virtio_transport_recv_enqueue(vsk, skb);
+           if (!virtio_transport_recv_enqueue(vsk, skb)) {
+                   /* There is no more space to queue the packet, so let's
+                    * close the connection; otherwise, we'll lose data.
+                    */
+                   (void)virtio_transport_reset(vsk, skb);
+                   sk->sk_state = TCP_CLOSE;
+                   sk->sk_err = ENOBUFS;
+                   sk_error_report(sk);

sashiko reported some issues related to setting TCP_CLOSE state and not removing the socket from the connect table:
https://sashiko.dev/#/patchset/20260513105417.56761-1-sgarzare%40redhat.com

Quoted 13 lines

I'll change this by calling virtio_transport_do_close() and vsock_remove_sock() in the next version.

Stefano

Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
+                   break;
+           }
            vsock_data_ready(sk);
            return err;
    case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
--

2.54.0

Quoted 4 lines

And so the bag of hacks grows. I feel this is energy not well spent. Please, let us fix this properly *first*. And then worry about how to backport. Maybe it will not be so terrible to backport after all.

TBH I don't think this is an hack, but an issue we should fix in any case. Regarding the second patch, I see your point, but it's a big change that worries me. I'd like some more time to fix it properly without rushing. Staying calm without realizing that userspace is broken like we are now without this series :-(

That said, evaluating further, I think we have a similar issue also with STREAM on the host side where the skb usually doesn't free space, so we need a merge strategy also there.

So, I'd like to have time to fix both definitely. If you have time and want to go ahead, please do.

Thanks,
Stefano

Quoted 6 lines
Quoted 2 lines

Well my patch was a start, we just need a strategy how to avoid copying everything, right?

Yep, and then there's the question of how to handle EOM without a payload, but I think that's a special case. In theory, we don't support sending it, but I'm not sure if POSIX allows it or not.

It seems to, but given we didn't allow it in the past, we probably should not start now without a good solution.
Really we should add a feature bit for EOM to steal a byte from buf_alloc. Or several bytes)

Quoted 5 lines

That said, is it okay if I send a v4 of this series?

(I'm not sure if I'll be able to work on the merging next week)

Stefano

I do worry we are piling up hacks and we'll end up with races for all our troubles. That said, up to you.

author maintainer
#8 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Stefano Garzarella
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Stefano Garzarella

On Thu, 14 May 2026 at 17:16, Michael S. Tsirkin <[email protected]> wrote:

[...]

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 17 lines
Quoted 6 lines

And so the bag of hacks grows. I feel this is energy not well spent. Please, let us fix this properly *first*. And then worry about how to backport. Maybe it will not be so terrible to backport after all.

TBH I don't think this is an hack, but an issue we should fix in any case. Regarding the second patch, I see your point, but it's a big change that worries me. I'd like some more time to fix it properly without rushing. Staying calm without realizing that userspace is broken like we are now without this series :-(

That said, evaluating further, I think we have a similar issue also with STREAM on the host side where the skb usually doesn't free space, so we need a merge strategy also there.

Quoted 5 lines

So, I'd like to have time to fix both definitely. If you have time and want to go ahead, please do.

Thanks,
Stefano

Well my patch was a start, we just need a strategy how to avoid copying everything, right?

Quoted 6 lines
Quoted 3 lines

Yep, and then there's the question of how to handle EOM without a payload, but I think that's a special case. In theory, we don't support sending it, but I'm not sure if POSIX allows it or not.

It seems to, but given we didn't allow it in the past, we probably should not start now without a good solution.

Agree.

Quoted 2 lines

Really we should add a feature bit for EOM to steal a byte from buf_alloc. Or several bytes)

Yes, I agree. At this point, though, could we define a new protocol that also takes overhead into account, or would it be too complicated to synchronize both?

Quoted 11 lines
Quoted 6 lines

That said, is it okay if I send a v4 of this series?

(I'm not sure if I'll be able to work on the merging next week)

Stefano

I do worry we are piling up hacks and we'll end up with races for all our troubles. That said, up to you.

I see it differently; Patch 1 should have been there from the start.
Patch 2, unless we completely remove the overhead, we should keep it, or use it to trigger merging (e.g., when the overhead reaches a threshold that depends on buf_alloc).

I prefer to send a v4.

Thanks,
Stefano

#9 lore raw

Re: [PATCH net v3 1/2] vsock/virtio: reset connection on receiving queue overflow

Quoted 41 lines · Stefano Garzarella
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Stefano Garzarella
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Stefano Garzarella

On Thu, 14 May 2026 at 17:16, Michael S. Tsirkin <[email protected]> wrote:

[...]

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 17 lines
Quoted 6 lines

And so the bag of hacks grows. I feel this is energy not well spent. Please, let us fix this properly *first*. And then worry about how to backport. Maybe it will not be so terrible to backport after all.

TBH I don't think this is an hack, but an issue we should fix in any case. Regarding the second patch, I see your point, but it's a big change that worries me. I'd like some more time to fix it properly without rushing. Staying calm without realizing that userspace is broken like we are now without this series :-(

That said, evaluating further, I think we have a similar issue also with STREAM on the host side where the skb usually doesn't free space, so we need a merge strategy also there.

Quoted 5 lines

So, I'd like to have time to fix both definitely. If you have time and want to go ahead, please do.

Thanks,
Stefano

Well my patch was a start, we just need a strategy how to avoid copying everything, right?

Quoted 6 lines
Quoted 3 lines

Yep, and then there's the question of how to handle EOM without a payload, but I think that's a special case. In theory, we don't support sending it, but I'm not sure if POSIX allows it or not.

It seems to, but given we didn't allow it in the past, we probably should not start now without a good solution.

Quoted 8 lines

Agree.

Quoted 2 lines

Really we should add a feature bit for EOM to steal a byte from buf_alloc. Or several bytes)

Yes, I agree. At this point, though, could we define a new protocol that also takes overhead into account, or would it be too complicated to synchronize both?

Well isn't it just X bytes per EOM? The rest can be fixed with merging?

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

That said, is it okay if I send a v4 of this series?

(I'm not sure if I'll be able to work on the merging next week)

Stefano

Quoted 8 lines
Quoted 3 lines

I do worry we are piling up hacks and we'll end up with races for all our troubles. That said, up to you.

I see it differently; Patch 1 should have been there from the start. Patch 2, unless we completely remove the overhead, we should keep it, or use it to trigger merging (e.g., when the overhead reaches a threshold that depends on buf_alloc).

I'm not sure we need to merge retroactively though. Maybe just start merging new stuff when over a threshold. Should be simpler.

Quoted 4 lines

I prefer to send a v4.

Thanks,
Stefano

author maintainer
#10 lore raw

PATCH v3 2/2 vsock/virtio: fix skb overhead accounting to preserve full buf_alloc

In reply to Stefano Garzarella

From: Stefano Garzarella <[email protected]>

After commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), virtio_transport_inc_rx_pkt() subtracts per-skb overhead from buf_alloc when checking whether a new packet fits. This reduces the effective receive buffer below what the user configured via SO_VM_SOCKETS_BUFFER_SIZE, causing legitimate data packets to be silently dropped and applications that rely on the full buffer size to deadlock.

Also, the reduced space is not communicated to the remote peer, so its credit calculation accounts more credit than the receiver will actually accept, causing data loss (there is no retransmission).

With this approach we currently have failures in tools/testing/vsock/vsock_test.c. Test 18 sometimes fails, while test 22 always fails in this way:
18 - SOCK_STREAM MSG_ZEROCOPY...hash mismatch

22 - SOCK_STREAM virtio credit update + SO_RCVLOWAT...send failed:
Resource temporarily unavailable

Fix this by using buf_alloc * 2 as the total budget for payload plus skb overhead in virtio_transport_inc_rx_pkt(), similar to how SO_RCVBUF is doubled to reserve space for sk_buff metadata. This preserves the full buf_alloc for payload under normal operation, while still bounding the skb queue growth.

With this patch, all tests in tools/testing/vsock/vsock_test.c are now passing again.

Fixes 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue")
---
 net/vmw_vsock/virtio_transport_common.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/vmw_vsock/virtio_transport_common.c +4 -1
index 4a4ac69d1ad1..e22117bf5dcd 100644
@@ -434,7 +434,10 @@ static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
 {
 	u64 skb_overhead = (skb_queue_len(&vvs->rx_queue) + 1) * SKB_TRUESIZE(0);
 
-	if (skb_overhead + vvs->buf_used + len > vvs->buf_alloc)
+	/* Use buf_alloc * 2 as total budget (payload + overhead), similar to
+	 * how SO_RCVBUF is doubled to reserve space for sk_buff metadata.
+	 */
+	if (skb_overhead + vvs->buf_used + len > (u64)vvs->buf_alloc * 2)
 		return false;
 
 	vvs->rx_bytes += len;
Signature · 1 line
2.54.0
author maintainer
#11 lore raw
Quoted 42 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

After commit 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue"), virtio_transport_inc_rx_pkt() subtracts per-skb overhead from buf_alloc when checking whether a new packet fits. This reduces the effective receive buffer below what the user configured via SO_VM_SOCKETS_BUFFER_SIZE, causing legitimate data packets to be silently dropped and applications that rely on the full buffer size to deadlock.

Also, the reduced space is not communicated to the remote peer, so its credit calculation accounts more credit than the receiver will actually accept, causing data loss (there is no retransmission).

With this approach we currently have failures in tools/testing/vsock/vsock_test.c. Test 18 sometimes fails, while test 22 always fails in this way:
18 - SOCK_STREAM MSG_ZEROCOPY...hash mismatch

22 - SOCK_STREAM virtio credit update + SO_RCVLOWAT...send failed:
Resource temporarily unavailable

Fix this by using buf_alloc * 2 as the total budget for payload plus skb overhead in virtio_transport_inc_rx_pkt(), similar to how SO_RCVBUF is doubled to reserve space for sk_buff metadata. This preserves the full buf_alloc for payload under normal operation, while still bounding the skb queue growth.

With this patch, all tests in tools/testing/vsock/vsock_test.c are now passing again.

Fixes 059b7dbd20a6 ("vsock/virtio: fix potential unbounded skb queue")

---
net/vmw_vsock/virtio_transport_common.c | 5 ++++-

1 file changed, 4 insertions(+), 1 deletion(-)
net/vmw_vsock/virtio_transport_common.c +0 -0
index 4a4ac69d1ad1..e22117bf5dcd 100644

net/vmw_vsock/virtio_transport_common.c static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs, · line 441

Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +4 -1
@@ -434,7 +434,10 @@ static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
 {
 	u64 skb_overhead = (skb_queue_len(&vvs->rx_queue) + 1) * SKB_TRUESIZE(0);
 
-	if (skb_overhead + vvs->buf_used + len > vvs->buf_alloc)
+	/* Use buf_alloc * 2 as total budget (payload + overhead), similar to
+	 * how SO_RCVBUF is doubled to reserve space for sk_buff metadata.
+	 */
+	if (skb_overhead + vvs->buf_used + len > (u64)vvs->buf_alloc * 2)
 		return false;

sashiko reported a potential overflow here:
https://sashiko.dev/#/patchset/20260513105417.56761-1-sgarzare%40redhat.com

I'll check the credit and overflow separately to ensure both are correct.

The portion relating to the user setting a buffer that is too small is a pre-existing issue and IMO an edge case that we can ignore.

Thanks,
Stefano