Skip to contentSkip to conversation

vsock/virtio: cap TX credit to local buffer size

PATCH In review Melbin K Mathew 3 participants 13 messages Dec 11, 2025 – Dec 15, 2025

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 vsock/virtio: cap TX credit to local buffer size

The virtio vsock transport currently derives its TX credit directly from peer_buf_alloc, which is populated from the remote endpoint's SO_VM_SOCKETS_BUFFER_SIZE value.

On the host side, this means the amount of data we are willing to queue for a given connection is scaled purely by a peer-chosen value, rather than by the host's own vsock buffer configuration. A guest that advertises a very large buffer and reads slowly can cause the host to allocate a correspondingly large amount of sk_buff memory for that connection.

In practice, a malicious guest can:

- set a large AF_VSOCK buffer size (e.g. 2 GiB) with SO_VM_SOCKETS_BUFFER_MAX_SIZE / SO_VM_SOCKETS_BUFFER_SIZE, and

- open multiple connections to a host vsock service that sends data while the guest drains slowly.

On an unconstrained host this can drive Slab/SUnreclaim into the tens of GiB range, causing allocation failures and OOM kills in unrelated host processes while the offending VM remains running.

On non-virtio transports and compatibility:

- VMCI uses the AF_VSOCK buffer knobs to size its queue pairs per socket based on the local vsk->buffer_* values; the remote side can’t enlarge those queues beyond what the local endpoint configured.

- Hyper-V’s vsock transport uses fixed-size VMBus ring buffers and an MTU bound; there is no peer-controlled credit field comparable to peer_buf_alloc, and the remote endpoint can’t drive in-flight kernel memory above those ring sizes.

- The loopback path reuses virtio_transport_common.c, so it naturally follows the same semantics as the virtio transport.

Make virtio-vsock consistent with that model by intersecting the peer’s advertised receive window with the local vsock buffer size when computing TX credit. We introduce a small helper and use it in virtio_transport_get_credit(), virtio_transport_has_space() and virtio_transport_seqpacket_enqueue(), so that:

effective_tx_window = min(peer_buf_alloc, buf_alloc)

This prevents a remote endpoint from forcing us to queue more data than our own configuration allows, while preserving the existing credit semantics and keeping virtio-vsock compatible with the other transports.

On an unpatched Ubuntu 22.04 host (~64 GiB RAM), running a PoC with 32 guest vsock connections advertising 2 GiB each and reading slowly drove Slab/SUnreclaim from ~0.5 GiB to ~57 GiB and the system only recovered after killing the QEMU process.

With this patch applied, rerunning the same PoC yields:

Before:

    MemFree:        ~61.6 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~142 MiB
    SUnreclaim:     ~117 MiB

After 32 high-credit connections:

    MemFree:        ~61.5 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~178 MiB
    SUnreclaim:     ~152 MiB

i.e. only ~35 MiB increase in Slab/SUnreclaim, no host OOM, and the guest remains responsive.

Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
---
 net/vmw_vsock/virtio_transport_common.c | 27 ++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +18 -3
index dcc8a1d58..02eeb96dd 100644
@@ -491,6 +491,25 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we
+ * cap that to our local buf_alloc (derived from
+ * SO_VM_SOCKETS_BUFFER_SIZE and already clamped to buffer_max_size)
+ * so that a remote endpoint cannot force us to queue more data than
+ * our own configuration allows.
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+	return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
 	u32 ret;
@@ -499,7 +518,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 		return 0;
 
 	spin_lock_bh(&vvs->tx_lock);
-	ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	ret = virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (ret > credit)
 		ret = credit;
 	vvs->tx_cnt += ret;
@@ -831,7 +851,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 
 	spin_lock_bh(&vvs->tx_lock);
 
-	if (len > vvs->peer_buf_alloc) {
+	if (len > virtio_transport_tx_buf_alloc(vvs)) {
 		spin_unlock_bh(&vvs->tx_lock);
 		return -EMSGSIZE;
 	}
@@ -882,7 +902,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
 	struct virtio_vsock_sock *vvs = vsk->trans;
 	s64 bytes;
 
-	bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (bytes < 0)
 		bytes = 0;
 
Signature · 1 line
2.34.1
maintainer Acked-by on v3 1/1
#1 lore raw
Quoted 123 lines · Melbin K Mathew

The virtio vsock transport currently derives its TX credit directly from peer_buf_alloc, which is populated from the remote endpoint's SO_VM_SOCKETS_BUFFER_SIZE value.

On the host side, this means the amount of data we are willing to queue for a given connection is scaled purely by a peer-chosen value, rather than by the host's own vsock buffer configuration. A guest that advertises a very large buffer and reads slowly can cause the host to allocate a correspondingly large amount of sk_buff memory for that connection.

In practice, a malicious guest can:

- set a large AF_VSOCK buffer size (e.g. 2 GiB) with SO_VM_SOCKETS_BUFFER_MAX_SIZE / SO_VM_SOCKETS_BUFFER_SIZE, and

- open multiple connections to a host vsock service that sends data while the guest drains slowly.

On an unconstrained host this can drive Slab/SUnreclaim into the tens of GiB range, causing allocation failures and OOM kills in unrelated host processes while the offending VM remains running.

On non-virtio transports and compatibility:

- VMCI uses the AF_VSOCK buffer knobs to size its queue pairs per socket based on the local vsk->buffer_* values; the remote side can’t enlarge those queues beyond what the local endpoint configured.

- Hyper-V’s vsock transport uses fixed-size VMBus ring buffers and an MTU bound; there is no peer-controlled credit field comparable to peer_buf_alloc, and the remote endpoint can’t drive in-flight kernel memory above those ring sizes.

- The loopback path reuses virtio_transport_common.c, so it naturally follows the same semantics as the virtio transport.

Make virtio-vsock consistent with that model by intersecting the peer’s advertised receive window with the local vsock buffer size when computing TX credit. We introduce a small helper and use it in virtio_transport_get_credit(), virtio_transport_has_space() and virtio_transport_seqpacket_enqueue(), so that:

effective_tx_window = min(peer_buf_alloc, buf_alloc)

This prevents a remote endpoint from forcing us to queue more data than our own configuration allows, while preserving the existing credit semantics and keeping virtio-vsock compatible with the other transports.

On an unpatched Ubuntu 22.04 host (~64 GiB RAM), running a PoC with 32 guest vsock connections advertising 2 GiB each and reading slowly drove Slab/SUnreclaim from ~0.5 GiB to ~57 GiB and the system only recovered after killing the QEMU process.

With this patch applied, rerunning the same PoC yields:

Before:

    MemFree:        ~61.6 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~142 MiB
    SUnreclaim:     ~117 MiB

After 32 high-credit connections:

    MemFree:        ~61.5 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~178 MiB
    SUnreclaim:     ~152 MiB

i.e. only ~35 MiB increase in Slab/SUnreclaim, no host OOM, and the guest remains responsive.

Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
---
 net/vmw_vsock/virtio_transport_common.c | 27 ++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +16 -2
index dcc8a1d58..02eeb96dd 100644
@@ -491,6 +491,25 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we
+ * cap that to our local buf_alloc (derived from
+ * SO_VM_SOCKETS_BUFFER_SIZE and already clamped to buffer_max_size)
+ * so that a remote endpoint cannot force us to queue more data than
+ * our own configuration allows.
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+	return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
 	u32 ret;
@@ -499,7 +518,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 		return 0;
 
 	spin_lock_bh(&vvs->tx_lock);
-	ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	ret = virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (ret > credit)
 		ret = credit;
 	vvs->tx_cnt += ret;
@@ -831,7 +851,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 
 	spin_lock_bh(&vvs->tx_lock);
 
-	if (len > vvs->peer_buf_alloc) {
+	if (len > virtio_transport_tx_buf_alloc(vvs)) {
 		spin_unlock_bh(&vvs->tx_lock);
 		return -EMSGSIZE;
 	}

net/vmw_vsock/virtio_transport_common.c static s64 virtio_transport_has_space(struct vsock_sock *vsk) · line 909

Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
@@ -882,7 +902,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
 	struct virtio_vsock_sock *vvs = vsk->trans;
 	s64 bytes;
 
-	bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (bytes < 0)
 		bytes = 0;
 

Looking at this, why is one place casting to s64 the other is not?

Quoted 2 lines
Signature · 1 line
2.34.1
#2 lore raw
Quoted 137 lines · Michael S. Tsirkin
Quoted 123 lines · Melbin K Mathew

The virtio vsock transport currently derives its TX credit directly from peer_buf_alloc, which is populated from the remote endpoint's SO_VM_SOCKETS_BUFFER_SIZE value.

On the host side, this means the amount of data we are willing to queue for a given connection is scaled purely by a peer-chosen value, rather than by the host's own vsock buffer configuration. A guest that advertises a very large buffer and reads slowly can cause the host to allocate a correspondingly large amount of sk_buff memory for that connection.

In practice, a malicious guest can:

- set a large AF_VSOCK buffer size (e.g. 2 GiB) with SO_VM_SOCKETS_BUFFER_MAX_SIZE / SO_VM_SOCKETS_BUFFER_SIZE, and

- open multiple connections to a host vsock service that sends data while the guest drains slowly.

On an unconstrained host this can drive Slab/SUnreclaim into the tens of GiB range, causing allocation failures and OOM kills in unrelated host processes while the offending VM remains running.

On non-virtio transports and compatibility:

- VMCI uses the AF_VSOCK buffer knobs to size its queue pairs per socket based on the local vsk->buffer_* values; the remote side can’t enlarge those queues beyond what the local endpoint configured.

- Hyper-V’s vsock transport uses fixed-size VMBus ring buffers and an MTU bound; there is no peer-controlled credit field comparable to peer_buf_alloc, and the remote endpoint can’t drive in-flight kernel memory above those ring sizes.

- The loopback path reuses virtio_transport_common.c, so it naturally follows the same semantics as the virtio transport.

Make virtio-vsock consistent with that model by intersecting the peer’s advertised receive window with the local vsock buffer size when computing TX credit. We introduce a small helper and use it in virtio_transport_get_credit(), virtio_transport_has_space() and virtio_transport_seqpacket_enqueue(), so that:

effective_tx_window = min(peer_buf_alloc, buf_alloc)

This prevents a remote endpoint from forcing us to queue more data than our own configuration allows, while preserving the existing credit semantics and keeping virtio-vsock compatible with the other transports.

On an unpatched Ubuntu 22.04 host (~64 GiB RAM), running a PoC with 32 guest vsock connections advertising 2 GiB each and reading slowly drove Slab/SUnreclaim from ~0.5 GiB to ~57 GiB and the system only recovered after killing the QEMU process.

With this patch applied, rerunning the same PoC yields:

Before:

    MemFree:        ~61.6 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~142 MiB
    SUnreclaim:     ~117 MiB

After 32 high-credit connections:

    MemFree:        ~61.5 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~178 MiB
    SUnreclaim:     ~152 MiB

i.e. only ~35 MiB increase in Slab/SUnreclaim, no host OOM, and the guest remains responsive.

Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
---
 net/vmw_vsock/virtio_transport_common.c | 27 ++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +16 -2
index dcc8a1d58..02eeb96dd 100644
@@ -491,6 +491,25 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we
+ * cap that to our local buf_alloc (derived from
+ * SO_VM_SOCKETS_BUFFER_SIZE and already clamped to buffer_max_size)
+ * so that a remote endpoint cannot force us to queue more data than
+ * our own configuration allows.
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+	return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
 	u32 ret;
@@ -499,7 +518,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 		return 0;
 
 	spin_lock_bh(&vvs->tx_lock);
-	ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	ret = virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (ret > credit)
 		ret = credit;
 	vvs->tx_cnt += ret;
@@ -831,7 +851,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 
 	spin_lock_bh(&vvs->tx_lock);
 
-	if (len > vvs->peer_buf_alloc) {
+	if (len > virtio_transport_tx_buf_alloc(vvs)) {
 		spin_unlock_bh(&vvs->tx_lock);
 		return -EMSGSIZE;
 	}

net/vmw_vsock/virtio_transport_common.c static s64 virtio_transport_has_space(struct vsock_sock *vsk) · line 909

Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
@@ -882,7 +902,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
 	struct virtio_vsock_sock *vvs = vsk->trans;
 	s64 bytes;
 
-	bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (bytes < 0)
 		bytes = 0;
 
Quoted 1 line

Looking at this, why is one place casting to s64 the other is not?

Yeah, I pointed out that too in previous interactions. IMO we should fix virtio_transport_get_credit() since the peer can reduce peer_buf_alloc so it will overflow. Fortunately, we are limited by the credit requested by the caller, but we are still sending stuff when we shouldn't be.

@Melbin let me know if you will fix it, otherwise I can do that, but I'd like to do in a single series (multiple patches), since they depends on each other.

So if you prefer, I can pickup this patch and post a series with this + the other fix + the fix on the test I posted on the v2.

Stefano

#3 lore raw

Hi Stefano, Michael,

Thanks for the feedback and for pointing out the s64 issue in virtio_transport_get_credit() and the vsock_test regression.

I can take this up and send a small series:

1/2 – vsock/virtio: cap TX credit to local buffer size
- use a helper to bound peer_buf_alloc by buf_alloc
- compute available credit in s64 like has_space(), and clamp negative values to zero before applying the caller’s credit

2/2 – vsock/test: fix seqpacket message bounds test
- include your vsock_test.c change so the seqpacket bounds test keeps working with the corrected TX credit handling

I’ll roll these into a [PATCH net v4 0/2] series and send it out shortly.

Thanks again for all the guidance,
Melbin

Quoted 155 lines · Stefano Garzarella
Quoted 137 lines · Michael S. Tsirkin
Quoted 123 lines · Melbin K Mathew

The virtio vsock transport currently derives its TX credit directly from peer_buf_alloc, which is populated from the remote endpoint's SO_VM_SOCKETS_BUFFER_SIZE value.

On the host side, this means the amount of data we are willing to queue for a given connection is scaled purely by a peer-chosen value, rather than by the host's own vsock buffer configuration. A guest that advertises a very large buffer and reads slowly can cause the host to allocate a correspondingly large amount of sk_buff memory for that connection.

In practice, a malicious guest can:

- set a large AF_VSOCK buffer size (e.g. 2 GiB) with SO_VM_SOCKETS_BUFFER_MAX_SIZE / SO_VM_SOCKETS_BUFFER_SIZE, and

- open multiple connections to a host vsock service that sends data while the guest drains slowly.

On an unconstrained host this can drive Slab/SUnreclaim into the tens of GiB range, causing allocation failures and OOM kills in unrelated host processes while the offending VM remains running.

On non-virtio transports and compatibility:

- VMCI uses the AF_VSOCK buffer knobs to size its queue pairs per socket based on the local vsk->buffer_* values; the remote side can’t enlarge those queues beyond what the local endpoint configured.

- Hyper-V’s vsock transport uses fixed-size VMBus ring buffers and an MTU bound; there is no peer-controlled credit field comparable to peer_buf_alloc, and the remote endpoint can’t drive in-flight kernel memory above those ring sizes.

- The loopback path reuses virtio_transport_common.c, so it naturally follows the same semantics as the virtio transport.

Make virtio-vsock consistent with that model by intersecting the peer’s advertised receive window with the local vsock buffer size when computing TX credit. We introduce a small helper and use it in virtio_transport_get_credit(), virtio_transport_has_space() and virtio_transport_seqpacket_enqueue(), so that:

effective_tx_window = min(peer_buf_alloc, buf_alloc)

This prevents a remote endpoint from forcing us to queue more data than our own configuration allows, while preserving the existing credit semantics and keeping virtio-vsock compatible with the other transports.

On an unpatched Ubuntu 22.04 host (~64 GiB RAM), running a PoC with 32 guest vsock connections advertising 2 GiB each and reading slowly drove Slab/SUnreclaim from ~0.5 GiB to ~57 GiB and the system only recovered after killing the QEMU process.

With this patch applied, rerunning the same PoC yields:

Before:

    MemFree:        ~61.6 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~142 MiB
    SUnreclaim:     ~117 MiB

After 32 high-credit connections:

    MemFree:        ~61.5 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~178 MiB
    SUnreclaim:     ~152 MiB

i.e. only ~35 MiB increase in Slab/SUnreclaim, no host OOM, and the guest remains responsive.

Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
---
 net/vmw_vsock/virtio_transport_common.c | 27 ++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +16 -2
index dcc8a1d58..02eeb96dd 100644
@@ -491,6 +491,25 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we
+ * cap that to our local buf_alloc (derived from
+ * SO_VM_SOCKETS_BUFFER_SIZE and already clamped to buffer_max_size)
+ * so that a remote endpoint cannot force us to queue more data than
+ * our own configuration allows.
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+    return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
     u32 ret;
@@ -499,7 +518,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
             return 0;
 
     spin_lock_bh(&vvs->tx_lock);
-    ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+    ret = virtio_transport_tx_buf_alloc(vvs) -
+            (vvs->tx_cnt - vvs->peer_fwd_cnt);
     if (ret > credit)
             ret = credit;
     vvs->tx_cnt += ret;
@@ -831,7 +851,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 
     spin_lock_bh(&vvs->tx_lock);
 
-    if (len > vvs->peer_buf_alloc) {
+    if (len > virtio_transport_tx_buf_alloc(vvs)) {
             spin_unlock_bh(&vvs->tx_lock);
             return -EMSGSIZE;
     }

net/vmw_vsock/virtio_transport_common.c static s64 virtio_transport_has_space(struct vsock_sock *vsk) · line 909

Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
@@ -882,7 +902,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
     struct virtio_vsock_sock *vvs = vsk->trans;
     s64 bytes;
 
-    bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+    bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+            (vvs->tx_cnt - vvs->peer_fwd_cnt);
     if (bytes < 0)
             bytes = 0;
 
Quoted 1 line

Looking at this, why is one place casting to s64 the other is not?

Yeah, I pointed out that too in previous interactions. IMO we should fix virtio_transport_get_credit() since the peer can reduce peer_buf_alloc so it will overflow. Fortunately, we are limited by the credit requested by the caller, but we are still sending stuff when we shouldn't be.

@Melbin let me know if you will fix it, otherwise I can do that, but I'd like to do in a single series (multiple patches), since they depends on each other.

So if you prefer, I can pickup this patch and post a series with this + the other fix + the fix on the test I posted on the v2.

Stefano

#4 lore raw
Quoted 6 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the feedback and for pointing out the s64 issue in virtio_transport_get_credit() and the vsock_test regression.

Quoted 6 lines

I can take this up and send a small series:

1/2 – vsock/virtio: cap TX credit to local buffer size
- use a helper to bound peer_buf_alloc by buf_alloc
- compute available credit in s64 like has_space(), and clamp negative values to zero before applying the caller’s credit

IMO they should be fixed in 2 different patches.

I think we can easily reuse virtio_transport_has_space() in virtio_transport_get_credit().

Quoted 6 lines

2/2 – vsock/test: fix seqpacket message bounds test
- include your vsock_test.c change so the seqpacket bounds test keeps working with the corrected TX credit handling

I’ll roll these into a [PATCH net v4 0/2] series and send it out shortly.

Please, wait a bit also for other maintainers comments.
See https://www.kernel.org/doc/html/latest/process/submitting-patches.html#don-t-get-discouraged-or-impatient

So, to recap I'd do the following:
Patch 1: fix virtio_transport_get_credit() maybe using virtio_transport_has_space() to calculate the space
Patch 2: (this one) cap TX credit to local buffer size
Patch 3: vsock/test: fix seqpacket message bounds test
Patch 4 (if you have time): add a new test for TX credit on stream socket

Stefano

Quoted 162 lines

Thanks again for all the guidance,
Melbin

Quoted 155 lines · Stefano Garzarella
Quoted 137 lines · Michael S. Tsirkin
Quoted 123 lines · Melbin K Mathew

The virtio vsock transport currently derives its TX credit directly from peer_buf_alloc, which is populated from the remote endpoint's SO_VM_SOCKETS_BUFFER_SIZE value.

On the host side, this means the amount of data we are willing to queue for a given connection is scaled purely by a peer-chosen value, rather than by the host's own vsock buffer configuration. A guest that advertises a very large buffer and reads slowly can cause the host to allocate a correspondingly large amount of sk_buff memory for that connection.

In practice, a malicious guest can:

- set a large AF_VSOCK buffer size (e.g. 2 GiB) with SO_VM_SOCKETS_BUFFER_MAX_SIZE / SO_VM_SOCKETS_BUFFER_SIZE, and

- open multiple connections to a host vsock service that sends data while the guest drains slowly.

On an unconstrained host this can drive Slab/SUnreclaim into the tens of GiB range, causing allocation failures and OOM kills in unrelated host processes while the offending VM remains running.

On non-virtio transports and compatibility:

- VMCI uses the AF_VSOCK buffer knobs to size its queue pairs per socket based on the local vsk->buffer_* values; the remote side can’t enlarge those queues beyond what the local endpoint configured.

- Hyper-V’s vsock transport uses fixed-size VMBus ring buffers and an MTU bound; there is no peer-controlled credit field comparable to peer_buf_alloc, and the remote endpoint can’t drive in-flight kernel memory above those ring sizes.

- The loopback path reuses virtio_transport_common.c, so it naturally follows the same semantics as the virtio transport.

Make virtio-vsock consistent with that model by intersecting the peer’s advertised receive window with the local vsock buffer size when computing TX credit. We introduce a small helper and use it in virtio_transport_get_credit(), virtio_transport_has_space() and virtio_transport_seqpacket_enqueue(), so that:

effective_tx_window = min(peer_buf_alloc, buf_alloc)

This prevents a remote endpoint from forcing us to queue more data than our own configuration allows, while preserving the existing credit semantics and keeping virtio-vsock compatible with the other transports.

On an unpatched Ubuntu 22.04 host (~64 GiB RAM), running a PoC with 32 guest vsock connections advertising 2 GiB each and reading slowly drove Slab/SUnreclaim from ~0.5 GiB to ~57 GiB and the system only recovered after killing the QEMU process.

With this patch applied, rerunning the same PoC yields:

Before:

    MemFree:        ~61.6 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~142 MiB
    SUnreclaim:     ~117 MiB

After 32 high-credit connections:

    MemFree:        ~61.5 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~178 MiB
    SUnreclaim:     ~152 MiB

i.e. only ~35 MiB increase in Slab/SUnreclaim, no host OOM, and the guest remains responsive.

Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
---
 net/vmw_vsock/virtio_transport_common.c | 27 ++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +16 -2
index dcc8a1d58..02eeb96dd 100644
@@ -491,6 +491,25 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we
+ * cap that to our local buf_alloc (derived from
+ * SO_VM_SOCKETS_BUFFER_SIZE and already clamped to buffer_max_size)
+ * so that a remote endpoint cannot force us to queue more data than
+ * our own configuration allows.
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+    return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
     u32 ret;
@@ -499,7 +518,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
             return 0;
 
     spin_lock_bh(&vvs->tx_lock);
-    ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+    ret = virtio_transport_tx_buf_alloc(vvs) -
+            (vvs->tx_cnt - vvs->peer_fwd_cnt);
     if (ret > credit)
             ret = credit;
     vvs->tx_cnt += ret;
@@ -831,7 +851,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 
     spin_lock_bh(&vvs->tx_lock);
 
-    if (len > vvs->peer_buf_alloc) {
+    if (len > virtio_transport_tx_buf_alloc(vvs)) {
             spin_unlock_bh(&vvs->tx_lock);
             return -EMSGSIZE;
     }

net/vmw_vsock/virtio_transport_common.c static s64 virtio_transport_has_space(struct vsock_sock *vsk) · line 909

Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
@@ -882,7 +902,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
     struct virtio_vsock_sock *vvs = vsk->trans;
     s64 bytes;
 
-    bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+    bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+            (vvs->tx_cnt - vvs->peer_fwd_cnt);
     if (bytes < 0)
             bytes = 0;
 
Quoted 1 line

Looking at this, why is one place casting to s64 the other is not?

Yeah, I pointed out that too in previous interactions. IMO we should fix virtio_transport_get_credit() since the peer can reduce peer_buf_alloc so it will overflow. Fortunately, we are limited by the credit requested by the caller, but we are still sending stuff when we shouldn't be.

@Melbin let me know if you will fix it, otherwise I can do that, but I'd like to do in a single series (multiple patches), since they depends on each other.

So if you prefer, I can pickup this patch and post a series with this + the other fix + the fix on the test I posted on the v2.

Stefano

#5 lore raw

Hi Stefano, Michael,

Thanks for the suggestions and guidance.

I’ve drafted a 4-part series based on the recap. I’ve included the four diffs below for discussion. Can wait for comments, iterate, and then send the patch series in a few days.

---

Patch 1/4 — vsock/virtio: make get_credit() s64-safe and clamp negatives

virtio_transport_get_credit() was doing unsigned arithmetic; if the peer shrinks its window, the subtraction can underflow and look like “lots of credit”. This makes it compute “space” in s64 and clamp < 0 to 0.

(quoted diff) +0 -0

b/net/vmw_vsock/virtio_transport_common.c

net/vmw_vsock/virtio_transport_common.c +7 -3
@@ -494,16 +494,23 @@ EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
+ s64 bytes;
  u32 ret;
 
  if (!credit)
  return 0;
 
  spin_lock_bh(&vvs->tx_lock);
- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
- if (ret > credit)
- ret = credit;
+ bytes = (s64)vvs->peer_buf_alloc -
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
+ if (bytes < 0)
+ bytes = 0;
+
+ ret = min_t(u32, credit, (u32)bytes);
  vvs->tx_cnt += ret;
  vvs->bytes_unsent += ret;
  spin_unlock_bh(&vvs->tx_lock);
 
  return ret;

}

---

Patch 2/4 — vsock/virtio: cap TX window by local buffer (helper + use everywhere in TX path)

Cap the effective advertised window to min(peer_buf_alloc, buf_alloc) and use it consistently in TX paths (get_credit, has_space, seqpacket_enqueue).

(quoted diff) +0 -0

b/net/vmw_vsock/virtio_transport_common.c

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -491,6 +491,16 @@ void virtio_transport_consume_skb_sent(struct

sk_buff *skb, bool consume)
}
EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we cap it
+ * to our local buf_alloc (derived from SO_VM_SOCKETS_BUFFER_SIZE and
+ * already clamped to buffer_max_size).
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs) +{
+ return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) {
s64 bytes;
@@ -502,7 +512,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
return 0;

spin_lock_bh(&vvs->tx_lock);
- bytes = (s64)vvs->peer_buf_alloc -
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) - ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
if (bytes < 0)
bytes = 0;
@@ -834,7 +845,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
spin_lock_bh(&vvs->tx_lock);

- if (len > vvs->peer_buf_alloc) {
+ if (len > virtio_transport_tx_buf_alloc(vvs)) { spin_unlock_bh(&vvs->tx_lock);
return -EMSGSIZE;
}
@@ -884,7 +895,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
struct virtio_vsock_sock *vvs = vsk->trans;
s64 bytes;

- bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
if (bytes < 0)
bytes = 0;

return bytes;
}

---

Patch 3/4 — vsock/test: fix seqpacket msg bounds test (set client buf too)

After fixing TX credit bounds, the client can fill its TX window and block before it wakes the server. Setting the buffer on the client makes the test deterministic again.

tools/testing/vsock/vsock_test.c +0 -0
@@ -353,6 +353,7 @@ static void test_stream_msg_peek_server(const

struct test_opts *opts)

static void test_seqpacket_msg_bounds_client(const struct test_opts *opts) {
+ unsigned long long sock_buf_size;
unsigned long curr_hash;
size_t max_msg_size;
int page_size;
@@ -366,6 +367,18 @@ static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
exit(EXIT_FAILURE);
}

+ sock_buf_size = SOCK_BUF_SIZE;
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
+ sock_buf_size,
+ "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
+ sock_buf_size,
+ "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
+
/* Wait, until receiver sets buffer size. */ control_expectln("SRVREADY");

---

Patch 4/4 — vsock/test: add stream TX credit bounds regression test

This directly guards the original failure mode for stream sockets: if the peer advertises a large window but the sender’s local policy is small, the sender must stall quickly (hit EAGAIN in nonblocking mode) rather than queueing megabytes.

tools/testing/vsock/vsock_test.c +1 -0
@@ -349,6 +349,7 @@
 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
+#define SMALL_SOCK_BUF_SIZE (64 * 1024ULL)
 #define MAX_MSG_PAGES 4
 
 /* Insert new test functions after test_stream_msg_peek_server, before
  * test_seqpacket_msg_bounds_client (around line 352) */
 

+static void test_stream_tx_credit_bounds_client(const struct test_opts *opts) +{
+ ... /* full function as provided */
+}
+
+static void test_stream_tx_credit_bounds_server(const struct test_opts *opts) +{
+ ... /* full function as provided */
+}

@@ -2224,6 +2305,10 @@
.run_client = test_stream_msg_peek_client, .run_server = test_stream_msg_peek_server, },
+ {
+ .name = "SOCK_STREAM TX credit bounds",
+ .run_client = test_stream_tx_credit_bounds_client,
+ .run_server = test_stream_tx_credit_bounds_server,
+ },
{
.name = "SOCK_SEQPACKET msg bounds",

Quoted 202 lines · Stefano Garzarella
Quoted 6 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the feedback and for pointing out the s64 issue in virtio_transport_get_credit() and the vsock_test regression.

Quoted 6 lines

I can take this up and send a small series:

1/2 – vsock/virtio: cap TX credit to local buffer size
- use a helper to bound peer_buf_alloc by buf_alloc
- compute available credit in s64 like has_space(), and clamp negative values to zero before applying the caller’s credit

IMO they should be fixed in 2 different patches.

I think we can easily reuse virtio_transport_has_space() in virtio_transport_get_credit().

Quoted 6 lines

2/2 – vsock/test: fix seqpacket message bounds test
- include your vsock_test.c change so the seqpacket bounds test keeps working with the corrected TX credit handling

I’ll roll these into a [PATCH net v4 0/2] series and send it out shortly.

Please, wait a bit also for other maintainers comments.
See https://www.kernel.org/doc/html/latest/process/submitting-patches.html#don-t-get-discouraged-or-impatient

So, to recap I'd do the following:
Patch 1: fix virtio_transport_get_credit() maybe using virtio_transport_has_space() to calculate the space
Patch 2: (this one) cap TX credit to local buffer size
Patch 3: vsock/test: fix seqpacket message bounds test
Patch 4 (if you have time): add a new test for TX credit on stream socket

Stefano

Quoted 162 lines

Thanks again for all the guidance,
Melbin

Quoted 155 lines · Stefano Garzarella
Quoted 137 lines · Michael S. Tsirkin
Quoted 123 lines · Melbin K Mathew

The virtio vsock transport currently derives its TX credit directly from peer_buf_alloc, which is populated from the remote endpoint's SO_VM_SOCKETS_BUFFER_SIZE value.

On the host side, this means the amount of data we are willing to queue for a given connection is scaled purely by a peer-chosen value, rather than by the host's own vsock buffer configuration. A guest that advertises a very large buffer and reads slowly can cause the host to allocate a correspondingly large amount of sk_buff memory for that connection.

In practice, a malicious guest can:

- set a large AF_VSOCK buffer size (e.g. 2 GiB) with SO_VM_SOCKETS_BUFFER_MAX_SIZE / SO_VM_SOCKETS_BUFFER_SIZE, and

- open multiple connections to a host vsock service that sends data while the guest drains slowly.

On an unconstrained host this can drive Slab/SUnreclaim into the tens of GiB range, causing allocation failures and OOM kills in unrelated host processes while the offending VM remains running.

On non-virtio transports and compatibility:

- VMCI uses the AF_VSOCK buffer knobs to size its queue pairs per socket based on the local vsk->buffer_* values; the remote side can’t enlarge those queues beyond what the local endpoint configured.

- Hyper-V’s vsock transport uses fixed-size VMBus ring buffers and an MTU bound; there is no peer-controlled credit field comparable to peer_buf_alloc, and the remote endpoint can’t drive in-flight kernel memory above those ring sizes.

- The loopback path reuses virtio_transport_common.c, so it naturally follows the same semantics as the virtio transport.

Make virtio-vsock consistent with that model by intersecting the peer’s advertised receive window with the local vsock buffer size when computing TX credit. We introduce a small helper and use it in virtio_transport_get_credit(), virtio_transport_has_space() and virtio_transport_seqpacket_enqueue(), so that:

effective_tx_window = min(peer_buf_alloc, buf_alloc)

This prevents a remote endpoint from forcing us to queue more data than our own configuration allows, while preserving the existing credit semantics and keeping virtio-vsock compatible with the other transports.

On an unpatched Ubuntu 22.04 host (~64 GiB RAM), running a PoC with 32 guest vsock connections advertising 2 GiB each and reading slowly drove Slab/SUnreclaim from ~0.5 GiB to ~57 GiB and the system only recovered after killing the QEMU process.

With this patch applied, rerunning the same PoC yields:

Before:

    MemFree:        ~61.6 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~142 MiB
    SUnreclaim:     ~117 MiB

After 32 high-credit connections:

    MemFree:        ~61.5 GiB
    MemAvailable:   ~62.3 GiB
    Slab:           ~178 MiB
    SUnreclaim:     ~152 MiB

i.e. only ~35 MiB increase in Slab/SUnreclaim, no host OOM, and the guest remains responsive.

Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
---
 net/vmw_vsock/virtio_transport_common.c | 27 ++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +16 -2
index dcc8a1d58..02eeb96dd 100644
@@ -491,6 +491,25 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we
+ * cap that to our local buf_alloc (derived from
+ * SO_VM_SOCKETS_BUFFER_SIZE and already clamped to buffer_max_size)
+ * so that a remote endpoint cannot force us to queue more data than
+ * our own configuration allows.
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+    return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
     u32 ret;
@@ -499,7 +518,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
             return 0;
 
     spin_lock_bh(&vvs->tx_lock);
-    ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+    ret = virtio_transport_tx_buf_alloc(vvs) -
+            (vvs->tx_cnt - vvs->peer_fwd_cnt);
     if (ret > credit)
             ret = credit;
     vvs->tx_cnt += ret;
@@ -831,7 +851,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 
     spin_lock_bh(&vvs->tx_lock);
 
-    if (len > vvs->peer_buf_alloc) {
+    if (len > virtio_transport_tx_buf_alloc(vvs)) {
             spin_unlock_bh(&vvs->tx_lock);
             return -EMSGSIZE;
     }

net/vmw_vsock/virtio_transport_common.c static s64 virtio_transport_has_space(struct vsock_sock *vsk) · line 909

Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
@@ -882,7 +902,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
     struct virtio_vsock_sock *vvs = vsk->trans;
     s64 bytes;
 
-    bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+    bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+            (vvs->tx_cnt - vvs->peer_fwd_cnt);
     if (bytes < 0)
             bytes = 0;
 
Quoted 1 line

Looking at this, why is one place casting to s64 the other is not?

Yeah, I pointed out that too in previous interactions. IMO we should fix virtio_transport_get_credit() since the peer can reduce peer_buf_alloc so it will overflow. Fortunately, we are limited by the credit requested by the caller, but we are still sending stuff when we shouldn't be.

@Melbin let me know if you will fix it, otherwise I can do that, but I'd like to do in a single series (multiple patches), since they depends on each other.

So if you prefer, I can pickup this patch and post a series with this + the other fix + the fix on the test I posted on the v2.

Stefano

#6 lore raw
Quoted 3 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the suggestions and guidance.

You're welcome, but please avoid top-posting in the future:
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#use-trimmed-interleaved-replies-in-email-discussions

Quoted 22 lines

I’ve drafted a 4-part series based on the recap. I’ve included the four diffs below for discussion. Can wait for comments, iterate, and then send the patch series in a few days.

---

Patch 1/4 — vsock/virtio: make get_credit() s64-safe and clamp negatives

virtio_transport_get_credit() was doing unsigned arithmetic; if the peer shrinks its window, the subtraction can underflow and look like “lots of credit”. This makes it compute “space” in s64 and clamp < 0 to 0.

net/vmw_vsock/virtio_transport_common.c +1 -0
@@ -494,16 +494,23 @@ EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
+ s64 bytes;

net/vmw_vsock/virtio_transport_common.c EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent); · line 503

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +1 -3
 u32 ret;
 
 if (!credit)
 return 0;
 
 spin_lock_bh(&vvs->tx_lock);
- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
- if (ret > credit)
- ret = credit;
+ bytes = (s64)vvs->peer_buf_alloc -

Why not just calling virtio_transport_has_space()?

Quoted 73 lines
net/vmw_vsock/virtio_transport_common.c +5 -0
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
+ if (bytes < 0)
+ bytes = 0;
+
+ ret = min_t(u32, credit, (u32)bytes);
 vvs->tx_cnt += ret;
 vvs->bytes_unsent += ret;
 spin_unlock_bh(&vvs->tx_lock);
 
 return ret;

}

---

Patch 2/4 — vsock/virtio: cap TX window by local buffer (helper + use everywhere in TX path)

Cap the effective advertised window to min(peer_buf_alloc, buf_alloc) and use it consistently in TX paths (get_credit, has_space, seqpacket_enqueue).

net/vmw_vsock/virtio_transport_common.c +10 -0
@@ -491,6 +491,16 @@ void virtio_transport_consume_skb_sent(struct
 sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we cap it
+ * to our local buf_alloc (derived from SO_VM_SOCKETS_BUFFER_SIZE and
+ * already clamped to buffer_max_size).
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+ return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
 
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {

s64 bytes;

net/vmw_vsock/virtio_transport_common.c +4 -3
@@ -502,7 +512,8 @@ u32 virtio_transport_get_credit(struct
 virtio_vsock_sock *vvs, u32 credit)
 return 0;
 
 spin_lock_bh(&vvs->tx_lock);
- bytes = (s64)vvs->peer_buf_alloc -
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
 ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
 if (bytes < 0)
 bytes = 0;
@@ -834,7 +845,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 spin_lock_bh(&vvs->tx_lock);
 
- if (len > vvs->peer_buf_alloc) {
+ if (len > virtio_transport_tx_buf_alloc(vvs)) {
 spin_unlock_bh(&vvs->tx_lock);
 return -EMSGSIZE;
 }
@@ -884,7 +895,8 @@ static s64 virtio_transport_has_space(struct
 vsock_sock *vsk)
 struct virtio_vsock_sock *vvs = vsk->trans;
 s64 bytes;
 
- bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
 if (bytes < 0)
 bytes = 0;
Quoted 7 lines

return bytes;
}

---

Patch 3/4 — vsock/test: fix seqpacket msg bounds test (set client buf too)

Please just include in the series the patch I sent to you.

Quoted 38 lines

After fixing TX credit bounds, the client can fill its TX window and block before it wakes the server. Setting the buffer on the client makes the test deterministic again.

tools/testing/vsock/vsock_test.c +1 -0
@@ -353,6 +353,7 @@ static void test_stream_msg_peek_server(const
 struct test_opts *opts)
 
 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
 {
+ unsigned long long sock_buf_size;
 unsigned long curr_hash;
 size_t max_msg_size;

int page_size;

tools/testing/vsock/vsock_test.c +10 -1
@@ -366,6 +367,18 @@ static void
 test_seqpacket_msg_bounds_client(const struct test_opts *opts)
 exit(EXIT_FAILURE);
 }
 
+ sock_buf_size = SOCK_BUF_SIZE;
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
+
 /* Wait, until receiver sets buffer size. */
 control_expectln("SRVREADY");
 
 
---
Quoted 6 lines

Patch 4/4 — vsock/test: add stream TX credit bounds regression test

This directly guards the original failure mode for stream sockets: if the peer advertises a large window but the sender’s local policy is small, the sender must stall quickly (hit EAGAIN in nonblocking mode) rather than queueing megabytes.

Yeah, using nonblocking mode LGTM!

Quoted 23 lines
tools/testing/vsock/vsock_test.c +10 -0
@@ -349,6 +349,7 @@
 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
+#define SMALL_SOCK_BUF_SIZE (64 * 1024ULL)
 #define MAX_MSG_PAGES 4
 
 /* Insert new test functions after test_stream_msg_peek_server, before
 * test_seqpacket_msg_bounds_client (around line 352) */
 
+static void test_stream_tx_credit_bounds_client(const struct test_opts *opts)
+{
+ ... /* full function as provided */
+}
+
+static void test_stream_tx_credit_bounds_server(const struct test_opts *opts)
+{
+ ... /* full function as provided */
+}

tools/testing/vsock/vsock_test.c line 2312

Quoted 8 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2224,6 +2305,10 @@
 .run_client = test_stream_msg_peek_client,
 .run_server = test_stream_msg_peek_server,
 },
+ {
+ .name = "SOCK_STREAM TX credit bounds",
+ .run_client = test_stream_tx_credit_bounds_client,
+ .run_server = test_stream_tx_credit_bounds_server,
+ },

Please put it at the bottom. Tests are skipped by index, so we don't want to change index of old tests.

Please fix your editor, those diffs are hard to read without tabs/spaces.

Thanks,
Stefano

#7 lore raw
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the suggestions and guidance.

You're welcome, but please avoid top-posting in the future:
https://www.kernel.org/doc/html/latest/process/submitting- patches.html#use-trimmed-interleaved-replies-in-email-discussions

Sure. Thanks

Quoted 28 lines
Quoted 28 lines

I’ve drafted a 4-part series based on the recap. I’ve included the four diffs below for discussion. Can wait for comments, iterate, and then send the patch series in a few days.

---

Patch 1/4 — vsock/virtio: make get_credit() s64-safe and clamp negatives

virtio_transport_get_credit() was doing unsigned arithmetic; if the peer shrinks its window, the subtraction can underflow and look like “lots of credit”. This makes it compute “space” in s64 and clamp < 0 to 0.

net/vmw_vsock/virtio_transport_common.c +1 -0
@@ -494,16 +494,23 @@ 
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
+ s64 bytes;
  u32 ret;
 
  if (!credit)
  return 0;
 
Quoted 7 lines

net/vmw_vsock/virtio_transport_common.c EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent); · line 503

Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +1 -3
  spin_lock_bh(&vvs->tx_lock);
- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
- if (ret > credit)
- ret = credit;
+ bytes = (s64)vvs->peer_buf_alloc -

Why not just calling virtio_transport_has_space()?

virtio_transport_has_space() takes struct vsock_sock *, while virtio_transport_get_credit() takes struct virtio_vsock_sock *, so I cannot directly call has_space() from get_credit() without changing signatures.

Would you be OK if I factor the common “space” calculation into a small helper that operates on struct virtio_vsock_sock * and is used by both paths? Something like:

/* Must be called with vvs->tx_lock held. Returns >= 0. */ static s64 virtio_transport_tx_space(struct virtio_vsock_sock *vvs)

{
	s64 bytes;

	bytes = (s64)vvs->peer_buf_alloc -
		((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
	if (bytes < 0)
		bytes = 0;

	return bytes;
}

Then:

get_credit() would do bytes = virtio_transport_tx_space(vvs); ret = min_t(u32, credit, (u32)bytes);

has_space() would use the same helper after obtaining vvs = vsk->trans;

Does that match what you had in mind, or would you prefer a different factoring?

Quoted 80 lines
Quoted 80 lines
net/vmw_vsock/virtio_transport_common.c +5 -0
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
+ if (bytes < 0)
+ bytes = 0;
+
+ ret = min_t(u32, credit, (u32)bytes);
  vvs->tx_cnt += ret;
  vvs->bytes_unsent += ret;
  spin_unlock_bh(&vvs->tx_lock);
 

 return ret;
}

---

Patch 2/4 — vsock/virtio: cap TX window by local buffer (helper + use everywhere in TX path)

Cap the effective advertised window to min(peer_buf_alloc, buf_alloc) and use it consistently in TX paths (get_credit, has_space, seqpacket_enqueue).

net/vmw_vsock/virtio_transport_common.c +3 -0
@@ -491,6 +491,16 @@ void virtio_transport_consume_skb_sent(struct
 sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we 

cap it
+ * to our local buf_alloc (derived from SO_VM_SOCKETS_BUFFER_SIZE and
+ * already clamped to buffer_max_size).
+ */

net/vmw_vsock/virtio_transport_common.c +4 -0
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+ return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) {
 s64 bytes;

net/vmw_vsock/virtio_transport_common.c +1 -1
@@ -502,7 +512,8 @@ u32 virtio_transport_get_credit(struct
 virtio_vsock_sock *vvs, u32 credit)
  return 0;
 
  spin_lock_bh(&vvs->tx_lock);
- bytes = (s64)vvs->peer_buf_alloc -
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
  ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)

 bytes = 0;

net/vmw_vsock/virtio_transport_common.c +3 -2
@@ -834,7 +845,7 @@ virtio_transport_seqpacket_enqueue(struct 
 vsock_sock *vsk,
  spin_lock_bh(&vvs->tx_lock);
 
- if (len > vvs->peer_buf_alloc) {
+ if (len > virtio_transport_tx_buf_alloc(vvs)) {
  spin_unlock_bh(&vvs->tx_lock);
  return -EMSGSIZE;
  }
@@ -884,7 +895,8 @@ static s64 virtio_transport_has_space(struct
 vsock_sock *vsk)
  struct virtio_vsock_sock *vvs = vsk->trans;
  s64 bytes;
 
- bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)
  bytes = 0;

 return bytes;
}

Quoted 6 lines
Quoted 3 lines

---

Patch 3/4 — vsock/test: fix seqpacket msg bounds test (set client buf too)

Please just include in the series the patch I sent to you.

Thanks. I'll use your vsock_test.c patch as-is for 3/4

Quoted 83 lines
Quoted 38 lines

After fixing TX credit bounds, the client can fill its TX window and block before it wakes the server. Setting the buffer on the client makes the test deterministic again.

tools/testing/vsock/vsock_test.c +1 -0
@@ -353,6 +353,7 @@ static void test_stream_msg_peek_server(const
 struct test_opts *opts)
 
 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
 {
+ unsigned long long sock_buf_size;
  unsigned long curr_hash;
  size_t max_msg_size;

 int page_size;

tools/testing/vsock/vsock_test.c +10 -1
@@ -366,6 +367,18 @@ static void
 test_seqpacket_msg_bounds_client(const struct test_opts *opts)
  exit(EXIT_FAILURE);
  }
 
+ sock_buf_size = SOCK_BUF_SIZE;
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
+
  /* Wait, until receiver sets buffer size. */
  control_expectln("SRVREADY");
 
 
---
Quoted 6 lines

Patch 4/4 — vsock/test: add stream TX credit bounds regression test

This directly guards the original failure mode for stream sockets: if the peer advertises a large window but the sender’s local policy is small, the sender must stall quickly (hit EAGAIN in nonblocking mode) rather than queueing megabytes.

Yeah, using nonblocking mode LGTM!

Quoted 25 lines
tools/testing/vsock/vsock_test.c +2 -0
@@ -349,6 +349,7 @@
 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
+#define SMALL_SOCK_BUF_SIZE (64 * 1024ULL)
 #define MAX_MSG_PAGES 4
 
 /* Insert new test functions after test_stream_msg_peek_server, before
  * test_seqpacket_msg_bounds_client (around line 352) */
 
+static void test_stream_tx_credit_bounds_client(const struct 

test_opts *opts)

tools/testing/vsock/vsock_test.c +5 -0
+{
+ ... /* full function as provided */
+}
+
+static void test_stream_tx_credit_bounds_server(const struct 

test_opts *opts)

tools/testing/vsock/vsock_test.c +3 -0
+{
+ ... /* full function as provided */
+}

tools/testing/vsock/vsock_test.c line 2312

Quoted 8 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2224,6 +2305,10 @@
  .run_client = test_stream_msg_peek_client,
  .run_server = test_stream_msg_peek_server,
  },
+ {
+ .name = "SOCK_STREAM TX credit bounds",
+ .run_client = test_stream_tx_credit_bounds_client,
+ .run_server = test_stream_tx_credit_bounds_server,
+ },

Please put it at the bottom. Tests are skipped by index, so we don't want to change index of old tests.

Quoted 1 line

Please fix your editor, those diffs are hard to read without tabs/spaces.

seems like some issue with my email client. Hope it is okay now

Quoted 4 lines

Thanks,
Stefano

#8 lore raw
Quoted 59 lines · Melbin K Mathew
Quoted 9 lines · Stefano Garzarella
Quoted 3 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the suggestions and guidance.

You're welcome, but please avoid top-posting in the future:
https://www.kernel.org/doc/html/latest/process/submitting-
patches.html#use-trimmed-interleaved-replies-in-email-discussions

Sure. Thanks

Quoted 29 lines
Quoted 29 lines

I’ve drafted a 4-part series based on the recap. I’ve included the four diffs below for discussion. Can wait for comments, iterate, and then send the patch series in a few days.

---

Patch 1/4 — vsock/virtio: make get_credit() s64-safe and clamp negatives

virtio_transport_get_credit() was doing unsigned arithmetic; if the peer shrinks its window, the subtraction can underflow and look like “lots of credit”. This makes it compute “space” in s64 and clamp < 0 to 0.

net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -494,16 +494,23 @@
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
{
+ s64 bytes;
 u32 ret;

 if (!credit)
 return 0;

Quoted 7 lines
Quoted 5 lines

 spin_lock_bh(&vvs->tx_lock);
- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
- if (ret > credit)
- ret = credit;
+ bytes = (s64)vvs->peer_buf_alloc -

Why not just calling virtio_transport_has_space()?

virtio_transport_has_space() takes struct vsock_sock *, while virtio_transport_get_credit() takes struct virtio_vsock_sock *, so I cannot directly call has_space() from get_credit() without changing signatures.

Would you be OK if I factor the common “space” calculation into a small helper that operates on struct virtio_vsock_sock * and is used by both paths? Something like:

/* Must be called with vvs->tx_lock held. Returns >= 0. */ static s64 virtio_transport_tx_space(struct virtio_vsock_sock *vvs)

Quoted 10 lines
{
	s64 bytes;

	bytes = (s64)vvs->peer_buf_alloc -
		((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
	if (bytes < 0)
		bytes = 0;

	return bytes;
}

some explanation of what all these casts do, can't hurt.

Quoted 192 lines

Then:

get_credit() would do bytes = virtio_transport_tx_space(vvs); ret = min_t(u32, credit, (u32)bytes);

has_space() would use the same helper after obtaining vvs = vsk->trans;

Does that match what you had in mind, or would you prefer a different factoring?

Quoted 81 lines
Quoted 81 lines

+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
+ if (bytes < 0)
+ bytes = 0;
+
+ ret = min_t(u32, credit, (u32)bytes);
 vvs->tx_cnt += ret;
 vvs->bytes_unsent += ret;
 spin_unlock_bh(&vvs->tx_lock);

 return ret;
}

---

Patch 2/4 — vsock/virtio: cap TX window by local buffer (helper + use everywhere in TX path)

Cap the effective advertised window to min(peer_buf_alloc, buf_alloc) and use it consistently in TX paths (get_credit, has_space, seqpacket_enqueue).

net/vmw_vsock/virtio_transport_common.c +3 -0
@@ -491,6 +491,16 @@ void virtio_transport_consume_skb_sent(struct
 sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but

we cap it
+ * to our local buf_alloc (derived from SO_VM_SOCKETS_BUFFER_SIZE and
+ * already clamped to buffer_max_size).
+ */

net/vmw_vsock/virtio_transport_common.c +4 -0
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+ return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
{
 s64 bytes;

net/vmw_vsock/virtio_transport_common.c +1 -1
@@ -502,7 +512,8 @@ u32 virtio_transport_get_credit(struct
 virtio_vsock_sock *vvs, u32 credit)
  return 0;
 
  spin_lock_bh(&vvs->tx_lock);
- bytes = (s64)vvs->peer_buf_alloc -
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
  ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)

 bytes = 0;

net/vmw_vsock/virtio_transport_common.c +3 -2
@@ -834,7 +845,7 @@ virtio_transport_seqpacket_enqueue(struct
 vsock_sock *vsk,
  spin_lock_bh(&vvs->tx_lock);
 
- if (len > vvs->peer_buf_alloc) {
+ if (len > virtio_transport_tx_buf_alloc(vvs)) {
  spin_unlock_bh(&vvs->tx_lock);
  return -EMSGSIZE;
  }
@@ -884,7 +895,8 @@ static s64 virtio_transport_has_space(struct
 vsock_sock *vsk)
  struct virtio_vsock_sock *vvs = vsk->trans;
  s64 bytes;
 
- bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)
  bytes = 0;

 return bytes;
}

Quoted 7 lines
Quoted 4 lines

---

Patch 3/4 — vsock/test: fix seqpacket msg bounds test (set client buf too)

Please just include in the series the patch I sent to you.

Thanks. I'll use your vsock_test.c patch as-is for 3/4

Quoted 87 lines
Quoted 40 lines

After fixing TX credit bounds, the client can fill its TX window and block before it wakes the server. Setting the buffer on the client makes the test deterministic again.

tools/testing/vsock/ renamed from tools/testing/vsock/vsock_test.c +0 -0

vsock_test.c

tools/testing/vsock/vsock_test.c +0 -0
@@ -353,6 +353,7 @@ static void test_stream_msg_peek_server(const
 struct test_opts *opts)
 

static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
{
+ unsigned long long sock_buf_size;
 unsigned long curr_hash;
 size_t max_msg_size;
 int page_size;

tools/testing/vsock/vsock_test.c +10 -1
@@ -366,6 +367,18 @@ static void
 test_seqpacket_msg_bounds_client(const struct test_opts *opts)
  exit(EXIT_FAILURE);
  }
 
+ sock_buf_size = SOCK_BUF_SIZE;
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
+
  /* Wait, until receiver sets buffer size. */
  control_expectln("SRVREADY");
 
 
---
Quoted 6 lines

Patch 4/4 — vsock/test: add stream TX credit bounds regression test

This directly guards the original failure mode for stream sockets: if the peer advertises a large window but the sender’s local policy is small, the sender must stall quickly (hit EAGAIN in nonblocking mode) rather than queueing megabytes.

Yeah, using nonblocking mode LGTM!

Quoted 26 lines
tools/testing/vsock/ renamed from tools/testing/vsock/vsock_test.c +0 -0

vsock_test.c

tools/testing/vsock/vsock_test.c +2 -0
@@ -349,6 +349,7 @@
 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
+#define SMALL_SOCK_BUF_SIZE (64 * 1024ULL)
 #define MAX_MSG_PAGES 4
 
 /* Insert new test functions after test_stream_msg_peek_server, before
  * test_seqpacket_msg_bounds_client (around line 352) */
 
+static void test_stream_tx_credit_bounds_client(const struct

test_opts *opts)

tools/testing/vsock/vsock_test.c +5 -0
+{
+ ... /* full function as provided */
+}
+
+static void test_stream_tx_credit_bounds_server(const struct

test_opts *opts)

tools/testing/vsock/vsock_test.c +3 -0
+{
+ ... /* full function as provided */
+}

tools/testing/vsock/vsock_test.c line 2312

Quoted 8 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2224,6 +2305,10 @@
  .run_client = test_stream_msg_peek_client,
  .run_server = test_stream_msg_peek_server,
  },
+ {
+ .name = "SOCK_STREAM TX credit bounds",
+ .run_client = test_stream_tx_credit_bounds_client,
+ .run_server = test_stream_tx_credit_bounds_server,
+ },

Please put it at the bottom. Tests are skipped by index, so we don't want to change index of old tests.

Quoted 1 line

Please fix your editor, those diffs are hard to read without tabs/spaces.

seems like some issue with my email client. Hope it is okay now

Quoted 4 lines

Thanks,
Stefano

#9 lore raw
In reply to Melbin K Mathew
Quoted 47 lines · Melbin K Mathew
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the suggestions and guidance.

You're welcome, but please avoid top-posting in the future:
https://www.kernel.org/doc/html/latest/process/submitting- patches.html#use-trimmed-interleaved-replies-in-email-discussions

Sure. Thanks

Quoted 28 lines
Quoted 28 lines

I’ve drafted a 4-part series based on the recap. I’ve included the four diffs below for discussion. Can wait for comments, iterate, and then send the patch series in a few days.

---

Patch 1/4 — vsock/virtio: make get_credit() s64-safe and clamp negatives

virtio_transport_get_credit() was doing unsigned arithmetic; if the peer shrinks its window, the subtraction can underflow and look like “lots of credit”. This makes it compute “space” in s64 and clamp < 0 to 0.

net/vmw_vsock/virtio_transport_common.c +1 -0
@@ -494,16 +494,23 @@ 
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
+ s64 bytes;
  u32 ret;
 
  if (!credit)
  return 0;
 
Quoted 7 lines

net/vmw_vsock/virtio_transport_common.c EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent); · line 503

Quoted 5 lines
net/vmw_vsock/virtio_transport_common.c +1 -3
  spin_lock_bh(&vvs->tx_lock);
- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
- if (ret > credit)
- ret = credit;
+ bytes = (s64)vvs->peer_buf_alloc -

Why not just calling virtio_transport_has_space()?

Quoted 3 lines

virtio_transport_has_space() takes struct vsock_sock *, while virtio_transport_get_credit() takes struct virtio_vsock_sock *, so I cannot directly call has_space() from get_credit() without changing signatures.

Would you be OK if I factor the common “space” calculation into a small helper that operates on struct virtio_vsock_sock * and is used by both paths? Something like:

Why not just change the signature of virtio_transport_has_space()?

Thanks,
Stefano

Quoted 199 lines

/* Must be called with vvs->tx_lock held. Returns >= 0. */ static s64 virtio_transport_tx_space(struct virtio_vsock_sock *vvs)

{
	s64 bytes;

	bytes = (s64)vvs->peer_buf_alloc -
		((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
	if (bytes < 0)
		bytes = 0;

	return bytes;
}

Then:

get_credit() would do bytes = virtio_transport_tx_space(vvs); ret = min_t(u32, credit, (u32)bytes);

has_space() would use the same helper after obtaining vvs = vsk->trans;

Does that match what you had in mind, or would you prefer a different factoring?

Quoted 80 lines
Quoted 80 lines
net/vmw_vsock/virtio_transport_common.c +5 -0
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
+ if (bytes < 0)
+ bytes = 0;
+
+ ret = min_t(u32, credit, (u32)bytes);
  vvs->tx_cnt += ret;
  vvs->bytes_unsent += ret;
  spin_unlock_bh(&vvs->tx_lock);
 

 return ret;
}

---

Patch 2/4 — vsock/virtio: cap TX window by local buffer (helper + use everywhere in TX path)

Cap the effective advertised window to min(peer_buf_alloc, buf_alloc) and use it consistently in TX paths (get_credit, has_space, seqpacket_enqueue).

net/vmw_vsock/virtio_transport_common.c +3 -0
@@ -491,6 +491,16 @@ void virtio_transport_consume_skb_sent(struct
 sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but 

we cap it
+ * to our local buf_alloc (derived from SO_VM_SOCKETS_BUFFER_SIZE and
+ * already clamped to buffer_max_size).
+ */

net/vmw_vsock/virtio_transport_common.c +4 -0
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+ return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) {
 s64 bytes;

net/vmw_vsock/virtio_transport_common.c +1 -1
@@ -502,7 +512,8 @@ u32 virtio_transport_get_credit(struct
 virtio_vsock_sock *vvs, u32 credit)
  return 0;
 
  spin_lock_bh(&vvs->tx_lock);
- bytes = (s64)vvs->peer_buf_alloc -
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
  ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)

 bytes = 0;

net/vmw_vsock/virtio_transport_common.c +3 -2
@@ -834,7 +845,7 @@ virtio_transport_seqpacket_enqueue(struct 
 vsock_sock *vsk,
  spin_lock_bh(&vvs->tx_lock);
 
- if (len > vvs->peer_buf_alloc) {
+ if (len > virtio_transport_tx_buf_alloc(vvs)) {
  spin_unlock_bh(&vvs->tx_lock);
  return -EMSGSIZE;
  }
@@ -884,7 +895,8 @@ static s64 virtio_transport_has_space(struct
 vsock_sock *vsk)
  struct virtio_vsock_sock *vvs = vsk->trans;
  s64 bytes;
 
- bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)
  bytes = 0;

 return bytes;
}

Quoted 6 lines
Quoted 3 lines

---

Patch 3/4 — vsock/test: fix seqpacket msg bounds test (set client buf too)

Please just include in the series the patch I sent to you.

Thanks. I'll use your vsock_test.c patch as-is for 3/4

Quoted 83 lines
Quoted 38 lines

After fixing TX credit bounds, the client can fill its TX window and block before it wakes the server. Setting the buffer on the client makes the test deterministic again.

tools/testing/vsock/vsock_test.c +1 -0
@@ -353,6 +353,7 @@ static void test_stream_msg_peek_server(const
 struct test_opts *opts)
 
 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
 {
+ unsigned long long sock_buf_size;
  unsigned long curr_hash;
  size_t max_msg_size;

 int page_size;

tools/testing/vsock/vsock_test.c +10 -1
@@ -366,6 +367,18 @@ static void
 test_seqpacket_msg_bounds_client(const struct test_opts *opts)
  exit(EXIT_FAILURE);
  }
 
+ sock_buf_size = SOCK_BUF_SIZE;
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
+
  /* Wait, until receiver sets buffer size. */
  control_expectln("SRVREADY");
 
 
---
Quoted 6 lines

Patch 4/4 — vsock/test: add stream TX credit bounds regression test

This directly guards the original failure mode for stream sockets: if the peer advertises a large window but the sender’s local policy is small, the sender must stall quickly (hit EAGAIN in nonblocking mode) rather than queueing megabytes.

Yeah, using nonblocking mode LGTM!

Quoted 25 lines
tools/testing/vsock/vsock_test.c +2 -0
@@ -349,6 +349,7 @@
 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
+#define SMALL_SOCK_BUF_SIZE (64 * 1024ULL)
 #define MAX_MSG_PAGES 4
 
 /* Insert new test functions after test_stream_msg_peek_server, before
  * test_seqpacket_msg_bounds_client (around line 352) */
 
+static void test_stream_tx_credit_bounds_client(const struct 

test_opts *opts)

tools/testing/vsock/vsock_test.c +5 -0
+{
+ ... /* full function as provided */
+}
+
+static void test_stream_tx_credit_bounds_server(const struct 

test_opts *opts)

tools/testing/vsock/vsock_test.c +3 -0
+{
+ ... /* full function as provided */
+}

tools/testing/vsock/vsock_test.c line 2312

Quoted 8 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2224,6 +2305,10 @@
  .run_client = test_stream_msg_peek_client,
  .run_server = test_stream_msg_peek_server,
  },
+ {
+ .name = "SOCK_STREAM TX credit bounds",
+ .run_client = test_stream_tx_credit_bounds_client,
+ .run_server = test_stream_tx_credit_bounds_server,
+ },

Please put it at the bottom. Tests are skipped by index, so we don't want to change index of old tests.

Quoted 1 line

Please fix your editor, those diffs are hard to read without tabs/spaces.

seems like some issue with my email client. Hope it is okay now

Quoted 4 lines

Thanks,
Stefano

#10 lore raw
Quoted 48 lines · Stefano Garzarella
Quoted 47 lines · Melbin K Mathew
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the suggestions and guidance.

You're welcome, but please avoid top-posting in the future:
https://www.kernel.org/doc/html/latest/process/submitting- patches.html#use-trimmed-interleaved-replies-in-email-discussions

Sure. Thanks

Quoted 35 lines
Quoted 23 lines

I’ve drafted a 4-part series based on the recap. I’ve included the four diffs below for discussion. Can wait for comments, iterate, and then send the patch series in a few days.

---

Patch 1/4 — vsock/virtio: make get_credit() s64-safe and clamp negatives

virtio_transport_get_credit() was doing unsigned arithmetic; if the peer shrinks its window, the subtraction can underflow and look like “lots of credit”. This makes it compute “space” in s64 and clamp < 0 to 0.

net/vmw_vsock/virtio_transport_common.c +1 -0
@@ -494,16 +494,23 @@ 
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
+ s64 bytes;

net/vmw_vsock/virtio_transport_common.c line 504

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +1 -3
  u32 ret;
 
  if (!credit)
  return 0;
 
  spin_lock_bh(&vvs->tx_lock);
- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
- if (ret > credit)
- ret = credit;
+ bytes = (s64)vvs->peer_buf_alloc -

Why not just calling virtio_transport_has_space()?

Quoted 5 lines
Quoted 3 lines

virtio_transport_has_space() takes struct vsock_sock *, while virtio_transport_get_credit() takes struct virtio_vsock_sock *, so I cannot directly call has_space() from get_credit() without changing signatures.

Would you be OK if I factor the common “space” calculation into a small helper that operates on struct virtio_vsock_sock * and is used by both paths? Something like:

Why not just change the signature of virtio_transport_has_space()?

Thanks, that is cleaner.

For Patch 1 i'll change virtio_transport_has_space() to take struct virtio_vsock_sock * and call it from both virtio_transport_stream_has_space() and virtio_transport_get_credit().

/*
* Return available peer buffer space for TX (>= 0).
*
* Use s64 arithmetic so that if the peer shrinks peer_buf_alloc while
* we have bytes in flight (tx_cnt - peer_fwd_cnt), the subtraction does
* not underflow into a large positive value as it would with u32. *
* Must be called with vvs->tx_lock held.
*/
static s64 virtio_transport_has_space(struct virtio_vsock_sock *vvs)

{
	s64 bytes;

	bytes = (s64)vvs->peer_buf_alloc -
		((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
	if (bytes < 0)
		bytes = 0;

	return bytes;
}

s64 virtio_transport_stream_has_space(struct vsock_sock *vsk)

{
	struct virtio_vsock_sock *vvs = vsk->trans;
	s64 bytes;

	spin_lock_bh(&vvs->tx_lock);
	bytes = virtio_transport_has_space(vvs);
	spin_unlock_bh(&vvs->tx_lock);

	return bytes;
}

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)

{
	u32 ret;

	if (!credit)
		return 0;

	spin_lock_bh(&vvs->tx_lock);
	ret = min_t(u32, credit, (u32)virtio_transport_has_space(vvs));
	vvs->tx_cnt += ret;
	vvs->bytes_unsent += ret;
	spin_unlock_bh(&vvs->tx_lock);

	return ret;
}

Does this look right?

Quoted 204 lines

Thanks,
Stefano

Quoted 200 lines

/* Must be called with vvs->tx_lock held. Returns >= 0. */ static s64 virtio_transport_tx_space(struct virtio_vsock_sock *vvs) {
    s64 bytes;

    bytes = (s64)vvs->peer_buf_alloc - ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt); if (bytes < 0)
        bytes = 0;

    return bytes;
}

Then:

get_credit() would do bytes = virtio_transport_tx_space(vvs); ret = min_t(u32, credit, (u32)bytes);

has_space() would use the same helper after obtaining vvs = vsk->trans;

Does that match what you had in mind, or would you prefer a different factoring?

Quoted 81 lines
Quoted 81 lines
net/vmw_vsock/virtio_transport_common.c +5 -0
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
+ if (bytes < 0)
+ bytes = 0;
+
+ ret = min_t(u32, credit, (u32)bytes);
  vvs->tx_cnt += ret;
  vvs->bytes_unsent += ret;
  spin_unlock_bh(&vvs->tx_lock);
 

 return ret;
}

---

Patch 2/4 — vsock/virtio: cap TX window by local buffer (helper + use everywhere in TX path)

Cap the effective advertised window to min(peer_buf_alloc, buf_alloc) and use it consistently in TX paths (get_credit, has_space, seqpacket_enqueue).

net/vmw_vsock/virtio_transport_common.c +3 -0
@@ -491,6 +491,16 @@ void virtio_transport_consume_skb_sent(struct
 sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but 

we cap it
+ * to our local buf_alloc (derived from SO_VM_SOCKETS_BUFFER_SIZE and
+ * already clamped to buffer_max_size).
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)

net/vmw_vsock/virtio_transport_common.c +3 -0
+{
+ return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) {
 s64 bytes;

net/vmw_vsock/virtio_transport_common.c +1 -1
@@ -502,7 +512,8 @@ u32 virtio_transport_get_credit(struct
 virtio_vsock_sock *vvs, u32 credit)
  return 0;
 
  spin_lock_bh(&vvs->tx_lock);
- bytes = (s64)vvs->peer_buf_alloc -
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
  ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)

 bytes = 0;

net/vmw_vsock/virtio_transport_common.c +3 -2
@@ -834,7 +845,7 @@ virtio_transport_seqpacket_enqueue(struct 
 vsock_sock *vsk,
  spin_lock_bh(&vvs->tx_lock);
 
- if (len > vvs->peer_buf_alloc) {
+ if (len > virtio_transport_tx_buf_alloc(vvs)) {
  spin_unlock_bh(&vvs->tx_lock);
  return -EMSGSIZE;
  }
@@ -884,7 +895,8 @@ static s64 virtio_transport_has_space(struct
 vsock_sock *vsk)
  struct virtio_vsock_sock *vvs = vsk->trans;
  s64 bytes;
 
- bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+ bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+ ((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);
  if (bytes < 0)
  bytes = 0;

 return bytes;
}

Quoted 6 lines
Quoted 3 lines

---

Patch 3/4 — vsock/test: fix seqpacket msg bounds test (set client buf too)

Please just include in the series the patch I sent to you.

Thanks. I'll use your vsock_test.c patch as-is for 3/4

Quoted 83 lines
Quoted 38 lines

After fixing TX credit bounds, the client can fill its TX window and block before it wakes the server. Setting the buffer on the client makes the test deterministic again.

tools/testing/vsock/vsock_test.c +1 -0
@@ -353,6 +353,7 @@ static void test_stream_msg_peek_server(const
 struct test_opts *opts)
 
 static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
 {
+ unsigned long long sock_buf_size;
  unsigned long curr_hash;
  size_t max_msg_size;

 int page_size;

tools/testing/vsock/vsock_test.c +10 -1
@@ -366,6 +367,18 @@ static void
 test_seqpacket_msg_bounds_client(const struct test_opts *opts)
  exit(EXIT_FAILURE);
  }
 
+ sock_buf_size = SOCK_BUF_SIZE;
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
+
+ setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
+    sock_buf_size,
+    "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
+
  /* Wait, until receiver sets buffer size. */
  control_expectln("SRVREADY");
 
 
---
Quoted 6 lines

Patch 4/4 — vsock/test: add stream TX credit bounds regression test

This directly guards the original failure mode for stream sockets: if the peer advertises a large window but the sender’s local policy is small, the sender must stall quickly (hit EAGAIN in nonblocking mode) rather than queueing megabytes.

Yeah, using nonblocking mode LGTM!

Quoted 25 lines
tools/testing/vsock/vsock_test.c +2 -0
@@ -349,6 +349,7 @@
 #define SOCK_BUF_SIZE (2 * 1024 * 1024)
+#define SMALL_SOCK_BUF_SIZE (64 * 1024ULL)
 #define MAX_MSG_PAGES 4
 
 /* Insert new test functions after test_stream_msg_peek_server, before
  * test_seqpacket_msg_bounds_client (around line 352) */
 
+static void test_stream_tx_credit_bounds_client(const struct 

test_opts *opts)

tools/testing/vsock/vsock_test.c +5 -0
+{
+ ... /* full function as provided */
+}
+
+static void test_stream_tx_credit_bounds_server(const struct 

test_opts *opts)

tools/testing/vsock/vsock_test.c +3 -0
+{
+ ... /* full function as provided */
+}

tools/testing/vsock/vsock_test.c line 2312

Quoted 8 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2224,6 +2305,10 @@
  .run_client = test_stream_msg_peek_client,
  .run_server = test_stream_msg_peek_server,
  },
+ {
+ .name = "SOCK_STREAM TX credit bounds",
+ .run_client = test_stream_tx_credit_bounds_client,
+ .run_server = test_stream_tx_credit_bounds_server,
+ },

Please put it at the bottom. Tests are skipped by index, so we don't want to change index of old tests.

Quoted 1 line

Please fix your editor, those diffs are hard to read without tabs/ spaces.

seems like some issue with my email client. Hope it is okay now

Quoted 4 lines

Thanks,
Stefano

#11 lore raw
Quoted 72 lines · Melbin K Mathew
Quoted 48 lines · Stefano Garzarella
Quoted 47 lines · Melbin K Mathew
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Melbin Mathew Antony

Hi Stefano, Michael,

Thanks for the suggestions and guidance.

You're welcome, but please avoid top-posting in the future:
https://www.kernel.org/doc/html/latest/process/submitting- patches.html#use-trimmed-interleaved-replies-in-email-discussions

Sure. Thanks

Quoted 35 lines
Quoted 23 lines

I’ve drafted a 4-part series based on the recap. I’ve included the four diffs below for discussion. Can wait for comments, iterate, and then send the patch series in a few days.

---

Patch 1/4 — vsock/virtio: make get_credit() s64-safe and clamp negatives

virtio_transport_get_credit() was doing unsigned arithmetic; if the peer shrinks its window, the subtraction can underflow and look like “lots of credit”. This makes it compute “space” in s64 and clamp < 0 to 0.

net/vmw_vsock/virtio_transport_common.c +1 -0
@@ -494,16 +494,23 @@ 
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
+ s64 bytes;

net/vmw_vsock/virtio_transport_common.c line 504

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +1 -3
  u32 ret;
 
  if (!credit)
  return 0;
 
  spin_lock_bh(&vvs->tx_lock);
- ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
- if (ret > credit)
- ret = credit;
+ bytes = (s64)vvs->peer_buf_alloc -

Why not just calling virtio_transport_has_space()?

Quoted 5 lines
Quoted 3 lines

virtio_transport_has_space() takes struct vsock_sock *, while virtio_transport_get_credit() takes struct virtio_vsock_sock *, so I cannot directly call has_space() from get_credit() without changing signatures.

Would you be OK if I factor the common “space” calculation into a small helper that operates on struct virtio_vsock_sock * and is used by both paths? Something like:

Why not just change the signature of virtio_transport_has_space()?

Thanks, that is cleaner.

For Patch 1 i'll change virtio_transport_has_space() to take struct virtio_vsock_sock * and call it from both virtio_transport_stream_has_space() and virtio_transport_get_credit().

/*
* Return available peer buffer space for TX (>= 0).
*
* Use s64 arithmetic so that if the peer shrinks peer_buf_alloc while
* we have bytes in flight (tx_cnt - peer_fwd_cnt), the subtraction does
* not underflow into a large positive value as it would with u32. *
* Must be called with vvs->tx_lock held.
*/
static s64 virtio_transport_has_space(struct virtio_vsock_sock *vvs)

Quoted 5 lines
{
	s64 bytes;

	bytes = (s64)vvs->peer_buf_alloc -
		((s64)vvs->tx_cnt - (s64)vvs->peer_fwd_cnt);

wait, why casting also the counters?
they are supposed to wrap, so should be fine to avoid the cast there.

Please, avoid too many changes in a single patch.

Quoted 19 lines
	if (bytes < 0)
		bytes = 0;

	return bytes;
}

s64 virtio_transport_stream_has_space(struct vsock_sock *vsk)

{
	struct virtio_vsock_sock *vvs = vsk->trans;
	s64 bytes;

	spin_lock_bh(&vvs->tx_lock);
	bytes = virtio_transport_has_space(vvs);
	spin_unlock_bh(&vvs->tx_lock);

	return bytes;
}

u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)

Quoted 8 lines
{
	u32 ret;

	if (!credit)
		return 0;

	spin_lock_bh(&vvs->tx_lock);
	ret = min_t(u32, credit, (u32)virtio_transport_has_space(vvs));

min_t() is supposed to be use exactly to avoid to cast each member, so why adding the cast to the value returned by virtio_transport_has_space() ?

Quoted 8 lines
	vvs->tx_cnt += ret;
	vvs->bytes_unsent += ret;
	spin_unlock_bh(&vvs->tx_lock);

	return ret;
}

Does this look right?

Pretty much yes, a part some comments, but I'd like to see the final solution.

Thanks,
Stefano

#12 lore raw
In reply to Melbin K Mathew
Quoted 72 lines · Melbin K Mathew

The virtio vsock transport currently derives its TX credit directly from peer_buf_alloc, which is populated from the remote endpoint's SO_VM_SOCKETS_BUFFER_SIZE value.

On the host side, this means the amount of data we are willing to queue for a given connection is scaled purely by a peer-chosen value, rather than by the host's own vsock buffer configuration. A guest that advertises a very large buffer and reads slowly can cause the host to allocate a correspondingly large amount of sk_buff memory for that connection.

In practice, a malicious guest can:

- set a large AF_VSOCK buffer size (e.g. 2 GiB) with SO_VM_SOCKETS_BUFFER_MAX_SIZE / SO_VM_SOCKETS_BUFFER_SIZE, and

- open multiple connections to a host vsock service that sends data while the guest drains slowly.

On an unconstrained host this can drive Slab/SUnreclaim into the tens of GiB range, causing allocation failures and OOM kills in unrelated host processes while the offending VM remains running.

On non-virtio transports and compatibility:

- VMCI uses the AF_VSOCK buffer knobs to size its queue pairs per socket based on the local vsk->buffer_* values; the remote side can’t enlarge those queues beyond what the local endpoint configured.

- Hyper-V’s vsock transport uses fixed-size VMBus ring buffers and an MTU bound; there is no peer-controlled credit field comparable to peer_buf_alloc, and the remote endpoint can’t drive in-flight kernel memory above those ring sizes.

- The loopback path reuses virtio_transport_common.c, so it naturally follows the same semantics as the virtio transport.

Make virtio-vsock consistent with that model by intersecting the peer’s advertised receive window with the local vsock buffer size when computing TX credit. We introduce a small helper and use it in virtio_transport_get_credit(), virtio_transport_has_space() and virtio_transport_seqpacket_enqueue(), so that:

effective_tx_window = min(peer_buf_alloc, buf_alloc)

This prevents a remote endpoint from forcing us to queue more data than our own configuration allows, while preserving the existing credit semantics and keeping virtio-vsock compatible with the other transports.

On an unpatched Ubuntu 22.04 host (~64 GiB RAM), running a PoC with 32 guest vsock connections advertising 2 GiB each and reading slowly drove Slab/SUnreclaim from ~0.5 GiB to ~57 GiB and the system only recovered after killing the QEMU process.

With this patch applied, rerunning the same PoC yields:

Before:

   MemFree:        ~61.6 GiB
   MemAvailable:   ~62.3 GiB
   Slab:           ~142 MiB
   SUnreclaim:     ~117 MiB

After 32 high-credit connections:

   MemFree:        ~61.5 GiB
   MemAvailable:   ~62.3 GiB
   Slab:           ~178 MiB
   SUnreclaim:     ~152 MiB

i.e. only ~35 MiB increase in Slab/SUnreclaim, no host OOM, and the guest remains responsive.

Quoted 6 lines
Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")

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

1 file changed, 24 insertions(+), 3 deletions(-)

Changes LGTM, but the patch seems corrupted.

$ git am ./v3_20251211_mlbnkm1_vsock_virtio_cap_tx_credit_to_local_buffer_size.mbx
Applying: vsock/virtio: cap TX credit to local buffer size
error: corrupt patch at line 29
Patch failed at 0001 vsock/virtio: cap TX credit to local buffer size

See also https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Stefano

Quoted 57 lines
net/vmw_vsock/virtio_transport_common.c +18 -3
index dcc8a1d58..02eeb96dd 100644
@@ -491,6 +491,25 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
 
+/* Return the effective peer buffer size for TX credit computation.
+ *
+ * The peer advertises its receive buffer via peer_buf_alloc, but we
+ * cap that to our local buf_alloc (derived from
+ * SO_VM_SOCKETS_BUFFER_SIZE and already clamped to buffer_max_size)
+ * so that a remote endpoint cannot force us to queue more data than
+ * our own configuration allows.
+ */
+static u32 virtio_transport_tx_buf_alloc(struct virtio_vsock_sock *vvs)
+{
+	return min(vvs->peer_buf_alloc, vvs->buf_alloc);
+}
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
 	u32 ret;
@@ -499,7 +518,8 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 		return 0;
 
 	spin_lock_bh(&vvs->tx_lock);
-	ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	ret = virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (ret > credit)
 		ret = credit;
 	vvs->tx_cnt += ret;
@@ -831,7 +851,7 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
 
 	spin_lock_bh(&vvs->tx_lock);
 
-	if (len > vvs->peer_buf_alloc) {
+	if (len > virtio_transport_tx_buf_alloc(vvs)) {
 		spin_unlock_bh(&vvs->tx_lock);
 		return -EMSGSIZE;
 	}
@@ -882,7 +902,8 @@ static s64 virtio_transport_has_space(struct vsock_sock *vsk)
 	struct virtio_vsock_sock *vvs = vsk->trans;
 	s64 bytes;
 
-	bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
+	bytes = (s64)virtio_transport_tx_buf_alloc(vvs) -
+		(vvs->tx_cnt - vvs->peer_fwd_cnt);
 	if (bytes < 0)
 		bytes = 0;
 
Signature · 1 line
2.34.1