Skip to contentSkip to conversation

vsock/virtio: SKB allocation improvements

PATCH Superseded Will Deacon 2 participants 20 messages Jun 25, 2025 – Jul 1, 2025

Subsystems (6)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover vsock/virtio: SKB allocation improvements

Hi folks,

We're using vsock extensively in Android as a channel over which we can route binder transactions to/from virtual machines managed by the Android Virtualisation Framework. However, we have been observing some issues in production builds when using vsock in a low-memory environment (on the host and the guest) such as:

* The host receive path hanging forever, despite the guest performing a successful write to the socket.

* Page allocation failures in the vhost receive path (this is a likely contributor to the above)

* -ENOMEM coming back from sendmsg()

This series aims to improve the vsock SKB allocation for both the host (vhost) and the guest when using the virtio transport to help mitigate these issues. Specifically:

- Avoid single allocations of order > PAGE_ALLOC_COSTLY_ORDER

- Use non-linear SKBs for the transmit and vhost receive paths

- Reduce the guest RX buffers to a single page

There are more details in the individual commit messages but overall this results in less wasted memory and puts less pressure on the allocator.

This is my first time looking at this stuff, so all feedback is welcome.

Patches based on v6.16-rc3.

Cheers,

Will

Cc Keir Fraser <[email protected]>
Cc Steven Moreland <[email protected]>
Cc Frederick Mayle <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc "Eugenio Pérez" <[email protected]>

--->8

Will Deacon (5):
vhost/vsock: Avoid allocating arbitrarily-sized SKBs vsock/virtio: Resize receive buffers so that each SKB fits in a page vhost/vsock: Allocate nonlinear SKBs for handling large receive
buffers
vsock/virtio: Rename virtio_vsock_skb_rx_put() to
virtio_vsock_skb_put()
vhost/vsock: Allocate nonlinear SKBs for handling large transmit
buffers

 drivers/vhost/vsock.c                   | 21 +++++++++------
 include/linux/virtio_vsock.h            | 36 +++++++++++++++++++------
 net/vmw_vsock/virtio_transport.c        |  2 +-
 net/vmw_vsock/virtio_transport_common.c |  9 +++++--
 4 files changed, 49 insertions(+), 19 deletions(-)
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#1 lore raw

PATCH 1/5 vhost/vsock: Avoid allocating arbitrarily-sized SKBs

vhost_vsock_alloc_skb() returns NULL for packets advertising a length larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE in the packet header. However, this is only checked once the SKB has been allocated and, if the length in the packet header is zero, the SKB may not be freed immediately.

Hoist the size check before the SKB allocation so that an iovec larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + the header size is rejected outright. The subsequent check on the length field in the header can then simply check that the allocated SKB is indeed large enough to hold the packet.

---
 drivers/vhost/vsock.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/vsock.c +4 -2
index 802153e23073..66a0f060770e 100644
@@ -344,6 +344,9 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
 	len = iov_length(vq->iov, out);
 
+	if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
+		return NULL;
+
 	/* len contains both payload and hdr */
 	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
 	if (!skb)
@@ -367,8 +370,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return skb;
 
 	/* The pkt is too big or the length in the header is invalid */
-	if (payload_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ||
-	    payload_len + sizeof(*hdr) > len) {
+	if (payload_len + sizeof(*hdr) > len) {
 		kfree_skb(skb);
 		return NULL;
 	}
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#5 lore raw

PATCH 2/5 vsock/virtio: Resize receive buffers so that each SKB fits in a page

In reply to Will Deacon

When allocating receive buffers for the vsock virtio RX virtqueue, an SKB is allocated with a 4140 data payload (the 44-byte packet header + VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE). Even when factoring in the SKB overhead, the resulting 8KiB allocation thanks to the rounding in kmalloc_reserve() is wasteful (~3700 unusable bytes) and results in a higher-order page allocation for the sake of a few hundred bytes of packet data.

Limit the vsock virtio RX buffers to a page per SKB, resulting in much better memory utilisation and removing the need to allocate higher-order pages entirely.

---
 include/linux/virtio_vsock.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/virtio_vsock.h +2 -1
index 36fb3edfa403..67ffb64325ef 100644
@@ -111,7 +111,8 @@ static inline size_t virtio_vsock_skb_len(struct sk_buff *skb)
 	return (size_t)(skb_end_pointer(skb) - skb->head);
 }
 
-#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	(1024 * 4)
+#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	(SKB_WITH_OVERHEAD(PAGE_SIZE) \
+						 - VIRTIO_VSOCK_SKB_HEADROOM)
 #define VIRTIO_VSOCK_MAX_BUF_SIZE		0xFFFFFFFFUL
 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
 
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#8 lore raw

PATCH 3/5 vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers

In reply to Will Deacon

When receiving a packet from a guest, vhost_vsock_handle_tx_kick() calls vhost_vsock_alloc_skb() to allocate and fill an SKB with the receive data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet.

Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the receive data in the fragments.

---
 drivers/vhost/vsock.c        | 15 +++++++++------
 include/linux/virtio_vsock.h | 31 +++++++++++++++++++++++++------
 2 files changed, 34 insertions(+), 12 deletions(-)
drivers/vhost/vsock.c +9 -6
index 66a0f060770e..cfa4e1bcf367 100644
@@ -344,11 +344,16 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
 	len = iov_length(vq->iov, out);
 
-	if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
+	if (len < VIRTIO_VSOCK_SKB_HEADROOM ||
+	    len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
 		return NULL;
 
 	/* len contains both payload and hdr */
-	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+	if (len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		skb = virtio_vsock_alloc_skb_with_frags(len, GFP_KERNEL);
+	else
+		skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+
 	if (!skb)
 		return NULL;
 
@@ -377,10 +382,8 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
 	virtio_vsock_skb_rx_put(skb);
 
-	nbytes = copy_from_iter(skb->data, payload_len, &iov_iter);
-	if (nbytes != payload_len) {
-		vq_err(vq, "Expected %zu byte payload, got %zu bytes\n",
-		       payload_len, nbytes);
+	if (skb_copy_datagram_from_iter(skb, 0, &iov_iter, payload_len)) {
+		vq_err(vq, "Failed to copy %zu byte payload\n", payload_len);
 		kfree_skb(skb);
 		return NULL;
 	}
include/linux/virtio_vsock.h +25 -6
index 67ffb64325ef..8f9fa1cab32a 100644
@@ -51,27 +51,46 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
 {
 	u32 len;
 
+	DEBUG_NET_WARN_ON_ONCE(skb->len);
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
 
-	if (len > 0)
+	if (skb_is_nonlinear(skb))
+		skb->len = len;
+	else
 		skb_put(skb, len);
 }
 
-static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
+static inline struct sk_buff *
+__virtio_vsock_alloc_skb_with_frags(unsigned int header_len,
+				    unsigned int data_len,
+				    gfp_t mask)
 {
 	struct sk_buff *skb;
+	int err;
 
-	if (size < VIRTIO_VSOCK_SKB_HEADROOM)
-		return NULL;
-
-	skb = alloc_skb(size, mask);
+	skb = alloc_skb_with_frags(header_len, data_len,
+				   PAGE_ALLOC_COSTLY_ORDER, &err, mask);
 	if (!skb)
 		return NULL;
 
 	skb_reserve(skb, VIRTIO_VSOCK_SKB_HEADROOM);
+	skb->data_len = data_len;
 	return skb;
 }
 
+static inline struct sk_buff *
+virtio_vsock_alloc_skb_with_frags(unsigned int size, gfp_t mask)
+{
+	size -= VIRTIO_VSOCK_SKB_HEADROOM;
+	return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
+						   size, mask);
+}
+
+static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
+{
+	return __virtio_vsock_alloc_skb_with_frags(size, 0, mask);
+}
+
 static inline void
 virtio_vsock_skb_queue_head(struct sk_buff_head *list, struct sk_buff *skb)
 {
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#13 lore raw

PATCH 4/5 vsock/virtio: Rename virtio_vsock_skb_rx_put() to virtio_vsock_skb_put()

In reply to Will Deacon

In preparation for using virtio_vsock_skb_rx_put() when populating SKBs on the vsock TX path, rename virtio_vsock_skb_rx_put() to virtio_vsock_skb_put().

No functional change.

---
 drivers/vhost/vsock.c            | 2 +-
 include/linux/virtio_vsock.h     | 2 +-
 net/vmw_vsock/virtio_transport.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/vhost/vsock.c +1 -1
index cfa4e1bcf367..3799c0aeeec5 100644
@@ -380,7 +380,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return NULL;
 	}
 
-	virtio_vsock_skb_rx_put(skb);
+	virtio_vsock_skb_put(skb);
 
 	if (skb_copy_datagram_from_iter(skb, 0, &iov_iter, payload_len)) {
 		vq_err(vq, "Failed to copy %zu byte payload\n", payload_len);
include/linux/virtio_vsock.h +1 -1
index 8f9fa1cab32a..d237ca0fc320 100644
@@ -47,7 +47,7 @@ static inline void virtio_vsock_skb_clear_tap_delivered(struct sk_buff *skb)
 	VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered = false;
 }
 
-static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
+static inline void virtio_vsock_skb_put(struct sk_buff *skb)
 {
 	u32 len;
 
net/vmw_vsock/virtio_transport.c +1 -1
index f0e48e6911fc..3319be2ee3aa 100644
@@ -648,7 +648,7 @@ static void virtio_transport_rx_work(struct work_struct *work)
 				continue;
 			}
 
-			virtio_vsock_skb_rx_put(skb);
+			virtio_vsock_skb_put(skb);
 			virtio_transport_deliver_tap_pkt(skb);
 			virtio_transport_recv_pkt(&virtio_transport, skb);
 		}
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#15 lore raw

PATCH 5/5 vhost/vsock: Allocate nonlinear SKBs for handling large transmit buffers

In reply to Will Deacon

When transmitting a vsock packet, virtio_transport_send_pkt_info() calls virtio_transport_alloc_skb() to allocate and fill SKBs with the transmit data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet.

Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the transmit data in the fragments.

---
 net/vmw_vsock/virtio_transport_common.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/vmw_vsock/virtio_transport_common.c +7 -2
index 1b5d9896edae..424eb69e84f9 100644
@@ -109,7 +109,8 @@ static int virtio_transport_fill_skb(struct sk_buff *skb,
 		return __zerocopy_sg_from_iter(info->msg, NULL, skb,
 					       &info->msg->msg_iter, len, NULL);
 
-	return memcpy_from_msg(skb_put(skb, len), info->msg, len);
+	virtio_vsock_skb_put(skb);
+	return skb_copy_datagram_from_iter(skb, 0, &info->msg->msg_iter, len);
 }
 
 static void virtio_transport_init_hdr(struct sk_buff *skb,
@@ -261,7 +262,11 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *
 	if (!zcopy)
 		skb_len += payload_len;
 
-	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
+	if (skb_len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		skb = virtio_vsock_alloc_skb_with_frags(skb_len, GFP_KERNEL);
+	else
+		skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
+
 	if (!skb)
 		return NULL;
 
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#2 lore raw

Re: [PATCH 1/5] vhost/vsock: Avoid allocating arbitrarily-sized SKBs

In reply to Will Deacon
Quoted 10 lines · Will Deacon

vhost_vsock_alloc_skb() returns NULL for packets advertising a length larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE in the packet header. However, this is only checked once the SKB has been allocated and, if the length in the packet header is zero, the SKB may not be freed immediately.

Hoist the size check before the SKB allocation so that an iovec larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + the header size is rejected outright. The subsequent check on the length field in the header can then simply check that the allocated SKB is indeed large enough to hold the packet.

LGTM, but should we consider this as stable material adding a Fixes tag?

Thanks,
Stefano

Quoted 34 lines

---
drivers/vhost/vsock.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/vsock.c +4 -2
index 802153e23073..66a0f060770e 100644
@@ -344,6 +344,9 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
 	len = iov_length(vq->iov, out);
 
+	if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
+		return NULL;
+
 	/* len contains both payload and hdr */
 	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
 	if (!skb)
@@ -367,8 +370,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return skb;
 
 	/* The pkt is too big or the length in the header is invalid */
-	if (payload_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ||
-	    payload_len + sizeof(*hdr) > len) {
+	if (payload_len + sizeof(*hdr) > len) {
 		kfree_skb(skb);
 		return NULL;
 	}
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#6 lore raw

Re: [PATCH 2/5] vsock/virtio: Resize receive buffers so that each SKB fits in a page

In reply to Will Deacon
Quoted 22 lines · Will Deacon

When allocating receive buffers for the vsock virtio RX virtqueue, an SKB is allocated with a 4140 data payload (the 44-byte packet header + VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE). Even when factoring in the SKB overhead, the resulting 8KiB allocation thanks to the rounding in kmalloc_reserve() is wasteful (~3700 unusable bytes) and results in a higher-order page allocation for the sake of a few hundred bytes of packet data.

Limit the vsock virtio RX buffers to a page per SKB, resulting in much better memory utilisation and removing the need to allocate higher-order pages entirely.

---
include/linux/virtio_vsock.h | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/virtio_vsock.h +0 -0
index 36fb3edfa403..67ffb64325ef 100644

include/linux/virtio_vsock.h static inline size_t virtio_vsock_skb_len(struct sk_buff *skb) · line 115

Quoted 6 lines
include/linux/virtio_vsock.h +2 -1
@@ -111,7 +111,8 @@ static inline size_t virtio_vsock_skb_len(struct sk_buff *skb)
 	return (size_t)(skb_end_pointer(skb) - skb->head);
 }
 
-#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	(1024 * 4)
+#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	(SKB_WITH_OVERHEAD(PAGE_SIZE) \
+						 - VIRTIO_VSOCK_SKB_HEADROOM)

This is only used in net/vmw_vsock/virtio_transport.c :

static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)

{
	int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;

What about just remove VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE and use SKB_WITH_OVERHEAD(PAGE_SIZE) there? (maybe with a comment summarizing the issue we found).

Thanks,
Stefano

Quoted 7 lines
include/linux/virtio_vsock.h +0 -0
 #define VIRTIO_VSOCK_MAX_BUF_SIZE		0xFFFFFFFFUL
 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
 
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#9 lore raw

Re: [PATCH 3/5] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers

In reply to Will Deacon
Quoted 24 lines · Will Deacon

When receiving a packet from a guest, vhost_vsock_handle_tx_kick() calls vhost_vsock_alloc_skb() to allocate and fill an SKB with the receive data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet.

Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the receive data in the fragments.

---
drivers/vhost/vsock.c | 15 +++++++++------
include/linux/virtio_vsock.h | 31 +++++++++++++++++++++++++------

2 files changed, 34 insertions(+), 12 deletions(-)
drivers/vhost/vsock.c +0 -0
index 66a0f060770e..cfa4e1bcf367 100644

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 347

Quoted 5 lines
drivers/vhost/vsock.c +1 -1
@@ -344,11 +344,16 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
 	len = iov_length(vq->iov, out);
 
-	if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
+	if (len < VIRTIO_VSOCK_SKB_HEADROOM ||

Why moving this check here?

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 355

Quoted 9 lines
drivers/vhost/vsock.c +5 -1
+	    len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
 		return NULL;
 
 	/* len contains both payload and hdr */
-	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+	if (len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		skb = virtio_vsock_alloc_skb_with_frags(len, GFP_KERNEL);
+	else
+		skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);

Can we do this directly in virtio_vsock_alloc_skb() so we don't need to duplicate code on virtio/vhost code?

Quoted 22 lines
drivers/vhost/vsock.c +3 -4
+
 	if (!skb)
 		return NULL;
 
@@ -377,10 +382,8 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
 	virtio_vsock_skb_rx_put(skb);
 
-	nbytes = copy_from_iter(skb->data, payload_len, &iov_iter);
-	if (nbytes != payload_len) {
-		vq_err(vq, "Expected %zu byte payload, got %zu bytes\n",
-		       payload_len, nbytes);
+	if (skb_copy_datagram_from_iter(skb, 0, &iov_iter, payload_len)) {
+		vq_err(vq, "Failed to copy %zu byte payload\n", payload_len);
 		kfree_skb(skb);
 		return NULL;
 	}
include/linux/virtio_vsock.h +0 -0
index 67ffb64325ef..8f9fa1cab32a 100644

include/linux/virtio_vsock.h static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb) · line 54

Quoted 4 lines
include/linux/virtio_vsock.h +1 -0
@@ -51,27 +51,46 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
 {
 	u32 len;
 
+	DEBUG_NET_WARN_ON_ONCE(skb->len);

Should we mention in the commit message?

include/linux/virtio_vsock.h static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb) · line 56

Quoted 3 lines
include/linux/virtio_vsock.h +0 -1
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
 
-	if (len > 0)

Why removing this check?

Thanks,
Stefano

Quoted 49 lines
include/linux/virtio_vsock.h +24 -5
+	if (skb_is_nonlinear(skb))
+		skb->len = len;
+	else
 		skb_put(skb, len);
 }
 
-static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
+static inline struct sk_buff *
+__virtio_vsock_alloc_skb_with_frags(unsigned int header_len,
+				    unsigned int data_len,
+				    gfp_t mask)
 {
 	struct sk_buff *skb;
+	int err;
 
-	if (size < VIRTIO_VSOCK_SKB_HEADROOM)
-		return NULL;
-
-	skb = alloc_skb(size, mask);
+	skb = alloc_skb_with_frags(header_len, data_len,
+				   PAGE_ALLOC_COSTLY_ORDER, &err, mask);
 	if (!skb)
 		return NULL;
 
 	skb_reserve(skb, VIRTIO_VSOCK_SKB_HEADROOM);
+	skb->data_len = data_len;
 	return skb;
 }
 
+static inline struct sk_buff *
+virtio_vsock_alloc_skb_with_frags(unsigned int size, gfp_t mask)
+{
+	size -= VIRTIO_VSOCK_SKB_HEADROOM;
+	return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
+						   size, mask);
+}
+
+static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
+{
+	return __virtio_vsock_alloc_skb_with_frags(size, 0, mask);
+}
+

static inline void
virtio_vsock_skb_queue_head(struct sk_buff_head *list, struct sk_buff *skb) {

Signature · 1 line
2.50.0.714.g196bf9f422-goog
maintainer Reviewed-by on v1 4/5
#14 lore raw

Re: [PATCH 4/5] vsock/virtio: Rename virtio_vsock_skb_rx_put() to virtio_vsock_skb_put()

In reply to Will Deacon
Quoted 4 lines · Will Deacon

In preparation for using virtio_vsock_skb_rx_put() when populating SKBs on the vsock TX path, rename virtio_vsock_skb_rx_put() to virtio_vsock_skb_put().

Quoted 8 lines

No functional change.

---

drivers/vhost/vsock.c            | 2 +-
include/linux/virtio_vsock.h     | 2 +-
net/vmw_vsock/virtio_transport.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)

LGMT!

Quoted 44 lines
drivers/vhost/vsock.c +1 -1
index cfa4e1bcf367..3799c0aeeec5 100644
@@ -380,7 +380,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return NULL;
 	}
 
-	virtio_vsock_skb_rx_put(skb);
+	virtio_vsock_skb_put(skb);
 
 	if (skb_copy_datagram_from_iter(skb, 0, &iov_iter, payload_len)) {
 		vq_err(vq, "Failed to copy %zu byte payload\n", payload_len);
include/linux/virtio_vsock.h +1 -1
index 8f9fa1cab32a..d237ca0fc320 100644
@@ -47,7 +47,7 @@ static inline void virtio_vsock_skb_clear_tap_delivered(struct sk_buff *skb)
 	VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered = false;
 }
 
-static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
+static inline void virtio_vsock_skb_put(struct sk_buff *skb)
 {
 	u32 len;
 
net/vmw_vsock/virtio_transport.c +1 -1
index f0e48e6911fc..3319be2ee3aa 100644
@@ -648,7 +648,7 @@ static void virtio_transport_rx_work(struct work_struct *work)
 				continue;
 			}
 
-			virtio_vsock_skb_rx_put(skb);
+			virtio_vsock_skb_put(skb);
 			virtio_transport_deliver_tap_pkt(skb);
 			virtio_transport_recv_pkt(&virtio_transport, skb);
 		}
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#16 lore raw

Re: [PATCH 5/5] vhost/vsock: Allocate nonlinear SKBs for handling large transmit buffers

In reply to Will Deacon

nit: I'd use vsock/virtio: prefix since we are touching the virtio transport common code. Maybe we can mention that this will affect both virtio and vhost transports.

Quoted 33 lines · Will Deacon

When transmitting a vsock packet, virtio_transport_send_pkt_info() calls virtio_transport_alloc_skb() to allocate and fill SKBs with the transmit data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet.

Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the transmit data in the fragments.

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

1 file changed, 7 insertions(+), 2 deletions(-)
net/vmw_vsock/virtio_transport_common.c +2 -1
index 1b5d9896edae..424eb69e84f9 100644
@@ -109,7 +109,8 @@ static int virtio_transport_fill_skb(struct sk_buff *skb,
 		return __zerocopy_sg_from_iter(info->msg, NULL, skb,
 					       &info->msg->msg_iter, len, NULL);
 
-	return memcpy_from_msg(skb_put(skb, len), info->msg, len);
+	virtio_vsock_skb_put(skb);
+	return skb_copy_datagram_from_iter(skb, 0, &info->msg->msg_iter, len);
 }
 
 static void virtio_transport_init_hdr(struct sk_buff *skb,

net/vmw_vsock/virtio_transport_common.c static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info * · line 269

Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +5 -1
@@ -261,7 +262,11 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *
 	if (!zcopy)
 		skb_len += payload_len;
 
-	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
+	if (skb_len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		skb = virtio_vsock_alloc_skb_with_frags(skb_len, GFP_KERNEL);
+	else
+		skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
+

As I mentioned in the other patch, we may avoid this code duplication hiding this in virtio_vsock_alloc_skb() or adding a new function that we can use when we want to allocate frags or not.

Thanks,
Stefano

Quoted 7 lines
net/vmw_vsock/virtio_transport_common.c +0 -0
 	if (!skb)
 		return NULL;
 
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#18 lore raw
In reply to Will Deacon
Quoted 24 lines · Will Deacon

Hi folks,

We're using vsock extensively in Android as a channel over which we can route binder transactions to/from virtual machines managed by the Android Virtualisation Framework. However, we have been observing some issues in production builds when using vsock in a low-memory environment (on the host and the guest) such as:

* The host receive path hanging forever, despite the guest performing a successful write to the socket.

* Page allocation failures in the vhost receive path (this is a likely contributor to the above)

* -ENOMEM coming back from sendmsg()

This series aims to improve the vsock SKB allocation for both the host (vhost) and the guest when using the virtio transport to help mitigate these issues. Specifically:

- Avoid single allocations of order > PAGE_ALLOC_COSTLY_ORDER

- Use non-linear SKBs for the transmit and vhost receive paths

Quoted 7 lines

- Reduce the guest RX buffers to a single page

There are more details in the individual commit messages but overall this results in less wasted memory and puts less pressure on the allocator.

This is my first time looking at this stuff, so all feedback is welcome.

Thank you very much for this series!

I left some minor comments, but overall LGTM!

Thanks,
Stefano

Quoted 40 lines

Patches based on v6.16-rc3.

Cheers,

Will

Cc Keir Fraser <[email protected]>
Cc Steven Moreland <[email protected]>
Cc Frederick Mayle <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc "Eugenio Pérez" <[email protected]>

--->8

Will Deacon (5):
vhost/vsock: Avoid allocating arbitrarily-sized SKBs vsock/virtio: Resize receive buffers so that each SKB fits in a page vhost/vsock: Allocate nonlinear SKBs for handling large receive
buffers
vsock/virtio: Rename virtio_vsock_skb_rx_put() to
virtio_vsock_skb_put()
vhost/vsock: Allocate nonlinear SKBs for handling large transmit
buffers

drivers/vhost/vsock.c                   | 21 +++++++++------
include/linux/virtio_vsock.h            | 36 +++++++++++++++++++------
net/vmw_vsock/virtio_transport.c        |  2 +-
net/vmw_vsock/virtio_transport_common.c |  9 +++++--
4 files changed, 49 insertions(+), 19 deletions(-)
Signature · 1 line
2.50.0.714.g196bf9f422-goog
#19 lore raw
Quoted 29 lines · Stefano Garzarella
Quoted 28 lines · Will Deacon

We're using vsock extensively in Android as a channel over which we can route binder transactions to/from virtual machines managed by the Android Virtualisation Framework. However, we have been observing some issues in production builds when using vsock in a low-memory environment (on the host and the guest) such as:

* The host receive path hanging forever, despite the guest performing a successful write to the socket.

* Page allocation failures in the vhost receive path (this is a likely contributor to the above)

* -ENOMEM coming back from sendmsg()

This series aims to improve the vsock SKB allocation for both the host (vhost) and the guest when using the virtio transport to help mitigate these issues. Specifically:

- Avoid single allocations of order > PAGE_ALLOC_COSTLY_ORDER

- Use non-linear SKBs for the transmit and vhost receive paths

- Reduce the guest RX buffers to a single page

There are more details in the individual commit messages but overall this results in less wasted memory and puts less pressure on the allocator.

Quoted 5 lines
Quoted 1 line

This is my first time looking at this stuff, so all feedback is welcome.

Thank you very much for this series!

I left some minor comments, but overall LGTM!

Cheers for going through it! I'll work through your comments now...

Will

#3 lore raw

Re: [PATCH 1/5] vhost/vsock: Avoid allocating arbitrarily-sized SKBs

In reply to Stefano Garzarella
Quoted 6 lines · Stefano Garzarella
Quoted 5 lines · Will Deacon

vhost_vsock_alloc_skb() returns NULL for packets advertising a length larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE in the packet header. However, this is only checked once the SKB has been allocated and, if the length in the packet header is zero, the SKB may not be freed immediately.

Quoted 7 lines
Quoted 5 lines

Hoist the size check before the SKB allocation so that an iovec larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + the header size is rejected outright. The subsequent check on the length field in the header can then simply check that the allocated SKB is indeed large enough to hold the packet.

LGTM, but should we consider this as stable material adding a Fixes tag?

Yup, absolutely. I put it first so that it can be backported easily but, for some reason, I thought networking didn't CC stable. I have no idea _why_ I thought that, so I'll add it (and a Fixes: line) for v2!

That seems to be:

Fixes 71dc9ec9ac7d ("virtio/vsock: replace virtio_vsock_pkt with sk_buff")

from what I can tell.

Cheers,

Will

#7 lore raw

Re: [PATCH 2/5] vsock/virtio: Resize receive buffers so that each SKB fits in a page

In reply to Stefano Garzarella
Quoted 32 lines · Stefano Garzarella
Quoted 22 lines · Will Deacon

When allocating receive buffers for the vsock virtio RX virtqueue, an SKB is allocated with a 4140 data payload (the 44-byte packet header + VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE). Even when factoring in the SKB overhead, the resulting 8KiB allocation thanks to the rounding in kmalloc_reserve() is wasteful (~3700 unusable bytes) and results in a higher-order page allocation for the sake of a few hundred bytes of packet data.

Limit the vsock virtio RX buffers to a page per SKB, resulting in much better memory utilisation and removing the need to allocate higher-order pages entirely.

---
include/linux/virtio_vsock.h | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/virtio_vsock.h +0 -0
index 36fb3edfa403..67ffb64325ef 100644

include/linux/virtio_vsock.h static inline size_t virtio_vsock_skb_len(struct sk_buff *skb) · line 115

Quoted 6 lines
include/linux/virtio_vsock.h +2 -1
@@ -111,7 +111,8 @@ static inline size_t virtio_vsock_skb_len(struct sk_buff *skb)
 	return (size_t)(skb_end_pointer(skb) - skb->head);
 }
 
-#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	(1024 * 4)
+#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	(SKB_WITH_OVERHEAD(PAGE_SIZE) \
+						 - VIRTIO_VSOCK_SKB_HEADROOM)

This is only used in net/vmw_vsock/virtio_transport.c :

Quoted 8 lines

static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)

{
	int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;

What about just remove VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE and use SKB_WITH_OVERHEAD(PAGE_SIZE) there? (maybe with a comment summarizing the issue we found).

Sure, works for me. That gets rid of the funny +- VIRTIO_VSOCK_SKB_HEADROOM too.

Will

#10 lore raw

Re: [PATCH 3/5] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers

In reply to Stefano Garzarella
Quoted 26 lines · Stefano Garzarella
Quoted 25 lines · Will Deacon

When receiving a packet from a guest, vhost_vsock_handle_tx_kick() calls vhost_vsock_alloc_skb() to allocate and fill an SKB with the receive data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet.

Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the receive data in the fragments.

---
drivers/vhost/vsock.c | 15 +++++++++------
include/linux/virtio_vsock.h | 31 +++++++++++++++++++++++++------

2 files changed, 34 insertions(+), 12 deletions(-)
drivers/vhost/vsock.c +0 -0
index 66a0f060770e..cfa4e1bcf367 100644
@@ -344,11 +344,16 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
Quoted 6 lines

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 347

Quoted 4 lines
drivers/vhost/vsock.c +1 -1
 	len = iov_length(vq->iov, out);
 
-	if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
+	if (len < VIRTIO_VSOCK_SKB_HEADROOM ||

Why moving this check here?

I moved it here because virtio_vsock_alloc_skb_with_frags() does:

+ size -= VIRTIO_VSOCK_SKB_HEADROOM;
+ return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
+ size, mask);

and so having the check in __virtio_vsock_alloc_skb_with_frags() looks strange as, by then, it really only applies to the linear case. It also feels weird to me to have the upper-bound of the length checked by the caller but the lower-bound checked in the callee. I certainly find it easier to reason about if they're in the same place.

Additionally, the lower-bound check is only needed by the vhost receive code, as the transmit path uses virtio_vsock_alloc_skb(), which never passes a size smaller than VIRTIO_VSOCK_SKB_HEADROOM.

Given all that, moving it to the one place that needs it seemed like the best option. What do you think?

Quoted 12 lines

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 355

Quoted 9 lines
drivers/vhost/vsock.c +5 -1
+	    len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
 		return NULL;
 
 	/* len contains both payload and hdr */
-	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+	if (len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		skb = virtio_vsock_alloc_skb_with_frags(len, GFP_KERNEL);
+	else
+		skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);

Can we do this directly in virtio_vsock_alloc_skb() so we don't need to duplicate code on virtio/vhost code?

We can, but then I think we should do something different for the rx_fill() path -- it feels fragile to rely on that using small-enough buffers to guarantee linear allocations. How about I:

1. Add virtio_vsock_alloc_linear_skb(), which always performs a linear allocation.

2. Change virtio_vsock_alloc_skb() to use nonlinear SKBs for sizes greater than SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)

3. Use virtio_vsock_alloc_linear_skb() to fill the guest RX buffers

4. Use virtio_vsock_alloc_skb() for everything else

If you like the idea, I'll rework the series along those lines. Diff below... (see end of mail)

Quoted 11 lines

include/linux/virtio_vsock.h static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb) · line 54

Quoted 9 lines
include/linux/virtio_vsock.h +1 -0
index 67ffb64325ef..8f9fa1cab32a 100644
@@ -51,27 +51,46 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
 {
 	u32 len;
 
+	DEBUG_NET_WARN_ON_ONCE(skb->len);

Should we mention in the commit message?

Sure, I'll add something. The non-linear handling doesn't accumulate len, so it's a debug check to ensure that len hasn't been messed with between allocation and here.

Quoted 5 lines

include/linux/virtio_vsock.h static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb) · line 56

Quoted 3 lines
include/linux/virtio_vsock.h +0 -1
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
 
-	if (len > 0)

Why removing this check?

I think it's redundant: len is a u32, so we're basically just checking to see if it's non-zero. All the callers have already checked for this but, even if they didn't, skb_put(skb, 0) is harmless afaict.

Will

--->8

drivers/vhost/vsock.c +1 -5
index 3799c0aeeec5..a6cd72a32f63 100644
@@ -349,11 +349,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return NULL;
 
 	/* len contains both payload and hdr */
-	if (len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
-		skb = virtio_vsock_alloc_skb_with_frags(len, GFP_KERNEL);
-	else
-		skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
-
+	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
include/linux/virtio_vsock.h +8 -5
index 0e265921be03..ed5eab46e3dc 100644
@@ -79,16 +79,19 @@ __virtio_vsock_alloc_skb_with_frags(unsigned int header_len,
 }
 
 static inline struct sk_buff *
-virtio_vsock_alloc_skb_with_frags(unsigned int size, gfp_t mask)
+virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask)
 {
-	size -= VIRTIO_VSOCK_SKB_HEADROOM;
-	return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
-						   size, mask);
+	return __virtio_vsock_alloc_skb_with_frags(size, 0, mask);
 }
 
 static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
 {
-	return __virtio_vsock_alloc_skb_with_frags(size, 0, mask);
+	if (size <= SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		return virtio_vsock_alloc_linear_skb(size, mask);
+
+	size -= VIRTIO_VSOCK_SKB_HEADROOM;
+	return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
+						   size, mask);
 }
 
 static inline void
net/vmw_vsock/virtio_transport.c +1 -1
index 4ae714397ca3..8c9ca0cb0d4e 100644
@@ -321,7 +321,7 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 	vq = vsock->vqs[VSOCK_VQ_RX];
 
 	do {
-		skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL);
+		skb = virtio_vsock_alloc_linear_skb(total_len, GFP_KERNEL);
 		if (!skb)
 			break;
 
net/vmw_vsock/virtio_transport_common.c +1 -5
index 424eb69e84f9..f74677c3511e 100644
@@ -262,11 +262,7 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *
 	if (!zcopy)
 		skb_len += payload_len;
 
-	if (skb_len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
-		skb = virtio_vsock_alloc_skb_with_frags(skb_len, GFP_KERNEL);
-	else
-		skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
-
+	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
#17 lore raw

Re: [PATCH 5/5] vhost/vsock: Allocate nonlinear SKBs for handling large transmit buffers

In reply to Stefano Garzarella
Quoted 3 lines · Stefano Garzarella

nit: I'd use vsock/virtio: prefix since we are touching the virtio transport common code. Maybe we can mention that this will affect both virtio and vhost transports.

Sure, I'll do that.

Quoted 37 lines · Will Deacon
Quoted 36 lines

When transmitting a vsock packet, virtio_transport_send_pkt_info() calls virtio_transport_alloc_skb() to allocate and fill SKBs with the transmit data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet.

Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the transmit data in the fragments.

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

1 file changed, 7 insertions(+), 2 deletions(-)
net/vmw_vsock/virtio_transport_common.c +2 -1
index 1b5d9896edae..424eb69e84f9 100644
@@ -109,7 +109,8 @@ static int virtio_transport_fill_skb(struct sk_buff *skb,
 		return __zerocopy_sg_from_iter(info->msg, NULL, skb,
 					       &info->msg->msg_iter, len, NULL);
 
-	return memcpy_from_msg(skb_put(skb, len), info->msg, len);
+	virtio_vsock_skb_put(skb);
+	return skb_copy_datagram_from_iter(skb, 0, &info->msg->msg_iter, len);
 }
 
 static void virtio_transport_init_hdr(struct sk_buff *skb,
@@ -261,7 +262,11 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *
 	if (!zcopy)
 		skb_len += payload_len;
 
Quoted 10 lines

net/vmw_vsock/virtio_transport_common.c static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info * · line 269

Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +5 -1
-	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
+	if (skb_len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		skb = virtio_vsock_alloc_skb_with_frags(skb_len, GFP_KERNEL);
+	else
+		skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
+

As I mentioned in the other patch, we may avoid this code duplication hiding this in virtio_vsock_alloc_skb() or adding a new function that we can use when we want to allocate frags or not.

That would be good. I had a crack at it in the diff I sent in reply to the earlier patch, so please take a look.

Will

#4 lore raw

Re: [PATCH 1/5] vhost/vsock: Avoid allocating arbitrarily-sized SKBs

In reply to Will Deacon
Quoted 13 lines · Will Deacon
Quoted 12 lines · Stefano Garzarella
Quoted 10 lines · Will Deacon

vhost_vsock_alloc_skb() returns NULL for packets advertising a length larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE in the packet header. However, this is only checked once the SKB has been allocated and, if the length in the packet header is zero, the SKB may not be freed immediately.

Hoist the size check before the SKB allocation so that an iovec larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + the header size is rejected outright. The subsequent check on the length field in the header can then simply check that the allocated SKB is indeed large enough to hold the packet.

Quoted 5 lines
Quoted 1 line

LGTM, but should we consider this as stable material adding a Fixes tag?

Yup, absolutely. I put it first so that it can be backported easily but, for some reason, I thought networking didn't CC stable. I have no idea _why_ I thought that, so I'll add it (and a Fixes: line) for v2!

yeah, this was the case till last year IIRC, but we always used Fixes tag, also if we didn't cc stable.

Quoted 6 lines

That seems to be:

Fixes 71dc9ec9ac7d ("virtio/vsock: replace virtio_vsock_pkt with sk_buff")

from what I can tell.

I think so!

Thanks,
Stefano

#11 lore raw

Re: [PATCH 3/5] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers

In reply to Will Deacon
Quoted 46 lines · Will Deacon
Quoted 26 lines · Stefano Garzarella
Quoted 25 lines · Will Deacon

When receiving a packet from a guest, vhost_vsock_handle_tx_kick() calls vhost_vsock_alloc_skb() to allocate and fill an SKB with the receive data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet.

Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the receive data in the fragments.

---
drivers/vhost/vsock.c | 15 +++++++++------
include/linux/virtio_vsock.h | 31 +++++++++++++++++++++++++------

2 files changed, 34 insertions(+), 12 deletions(-)
drivers/vhost/vsock.c +0 -0
index 66a0f060770e..cfa4e1bcf367 100644
@@ -344,11 +344,16 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
Quoted 6 lines

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 347

Quoted 4 lines
drivers/vhost/vsock.c +1 -1
 	len = iov_length(vq->iov, out);
 
-	if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
+	if (len < VIRTIO_VSOCK_SKB_HEADROOM ||

Why moving this check here?

I moved it here because virtio_vsock_alloc_skb_with_frags() does:

include/linux/virtio_vsock.h +3 -0
+       size -= VIRTIO_VSOCK_SKB_HEADROOM;
+       return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
+                                                  size, mask);

and so having the check in __virtio_vsock_alloc_skb_with_frags() looks strange as, by then, it really only applies to the linear case. It also feels weird to me to have the upper-bound of the length checked by the caller but the lower-bound checked in the callee. I certainly find it easier to reason about if they're in the same place.

Quoted 6 lines

Additionally, the lower-bound check is only needed by the vhost receive code, as the transmit path uses virtio_vsock_alloc_skb(), which never passes a size smaller than VIRTIO_VSOCK_SKB_HEADROOM.

Given all that, moving it to the one place that needs it seemed like the best option. What do you think?

Okay, I see now. Yep, it's fine, but please mention in the commit description.

Quoted 24 lines
Quoted 4 lines
Quoted 4 lines
drivers/vhost/vsock.c +1 -0
+	    len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
 		return NULL;
 
Quoted 9 lines

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 352

Quoted 6 lines
drivers/vhost/vsock.c +4 -1
 	/* len contains both payload and hdr */
-	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+	if (len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		skb = virtio_vsock_alloc_skb_with_frags(len, GFP_KERNEL);
+	else
+		skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);

Can we do this directly in virtio_vsock_alloc_skb() so we don't need to duplicate code on virtio/vhost code?

We can, but then I think we should do something different for the rx_fill() path -- it feels fragile to rely on that using small-enough buffers to guarantee linear allocations. How about I:

1. Add virtio_vsock_alloc_linear_skb(), which always performs a linear allocation.

2. Change virtio_vsock_alloc_skb() to use nonlinear SKBs for sizes greater than SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)

Quoted 6 lines

3. Use virtio_vsock_alloc_linear_skb() to fill the guest RX buffers

4. Use virtio_vsock_alloc_skb() for everything else

If you like the idea, I'll rework the series along those lines. Diff below... (see end of mail)

I really like it :-) let's go in that direction!

Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
include/linux/virtio_vsock.h +0 -0
index 67ffb64325ef..8f9fa1cab32a 100644
@@ -51,27 +51,46 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
 {
 	u32 len;
 
Quoted 3 lines

include/linux/virtio_vsock.h static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb) · line 54

Quoted 1 line
include/linux/virtio_vsock.h +1 -0
+	DEBUG_NET_WARN_ON_ONCE(skb->len);

Should we mention in the commit message?

Sure, I'll add something. The non-linear handling doesn't accumulate len, so it's a debug check to ensure that len hasn't been messed with between allocation and here.

Quoted 2 lines
Quoted 2 lines
include/linux/virtio_vsock.h +0 -0
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
 
Quoted 7 lines
Quoted 3 lines

include/linux/virtio_vsock.h static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb) · line 56

Quoted 1 line
include/linux/virtio_vsock.h +0 -1
-	if (len > 0)

Why removing this check?

I think it's redundant: len is a u32, so we're basically just checking to see if it's non-zero. All the callers have already checked for this but, even if they didn't, skb_put(skb, 0) is harmless afaict.

Yep, I see, but now I don't remember why we have it, could it be more expensive to call skb_put(skb, 0), instead of just having the if for control packets with no payload?

Thanks,
Stefano

Quoted 82 lines

Will

--->8

drivers/vhost/vsock.c +1 -5
index 3799c0aeeec5..a6cd72a32f63 100644
@@ -349,11 +349,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return NULL;
 
 	/* len contains both payload and hdr */
-	if (len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
-		skb = virtio_vsock_alloc_skb_with_frags(len, GFP_KERNEL);
-	else
-		skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
-
+	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
include/linux/virtio_vsock.h +0 -0
index 0e265921be03..ed5eab46e3dc 100644
@@ -79,16 +79,19 @@ __virtio_vsock_alloc_skb_with_frags(unsigned int header_len,
 }
 

static inline struct sk_buff *

include/linux/virtio_vsock.h +1 -1
-virtio_vsock_alloc_skb_with_frags(unsigned int size, gfp_t mask)
+virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask)

{

include/linux/virtio_vsock.h +1 -3
-	size -= VIRTIO_VSOCK_SKB_HEADROOM;
-	return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
-						   size, mask);
+	return __virtio_vsock_alloc_skb_with_frags(size, 0, mask);

}

static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask) {

include/linux/virtio_vsock.h +6 -1
-	return __virtio_vsock_alloc_skb_with_frags(size, 0, mask);
+	if (size <= SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		return virtio_vsock_alloc_linear_skb(size, mask);
+
+	size -= VIRTIO_VSOCK_SKB_HEADROOM;
+	return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
+						   size, mask);

}

static inline void

net/vmw_vsock/virtio_transport.c +1 -1
index 4ae714397ca3..8c9ca0cb0d4e 100644
@@ -321,7 +321,7 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 	vq = vsock->vqs[VSOCK_VQ_RX];
 
 	do {
-		skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL);
+		skb = virtio_vsock_alloc_linear_skb(total_len, GFP_KERNEL);
 		if (!skb)
 			break;
 
net/vmw_vsock/virtio_transport_common.c +1 -5
index 424eb69e84f9..f74677c3511e 100644
@@ -262,11 +262,7 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *
 	if (!zcopy)
 		skb_len += payload_len;
 
-	if (skb_len > SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
-		skb = virtio_vsock_alloc_skb_with_frags(skb_len, GFP_KERNEL);
-	else
-		skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
-
+	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
#12 lore raw

Re: [PATCH 3/5] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers

Quoted 32 lines · Stefano Garzarella
Quoted 31 lines · Will Deacon
Quoted 7 lines · Stefano Garzarella
Quoted 6 lines · Will Deacon
drivers/vhost/vsock.c +0 -0
index 66a0f060770e..cfa4e1bcf367 100644
@@ -344,11 +344,16 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 
Quoted 6 lines

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 347

Quoted 4 lines
drivers/vhost/vsock.c +1 -1
 	len = iov_length(vq->iov, out);
 
-	if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)
+	if (len < VIRTIO_VSOCK_SKB_HEADROOM ||

Why moving this check here?

I moved it here because virtio_vsock_alloc_skb_with_frags() does:

include/linux/virtio_vsock.h +3 -0
+       size -= VIRTIO_VSOCK_SKB_HEADROOM;
+       return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
+                                                  size, mask);

and so having the check in __virtio_vsock_alloc_skb_with_frags() looks strange as, by then, it really only applies to the linear case. It also feels weird to me to have the upper-bound of the length checked by the caller but the lower-bound checked in the callee. I certainly find it easier to reason about if they're in the same place.

Additionally, the lower-bound check is only needed by the vhost receive code, as the transmit path uses virtio_vsock_alloc_skb(), which never passes a size smaller than VIRTIO_VSOCK_SKB_HEADROOM.

Quoted 5 lines
Quoted 2 lines

Given all that, moving it to the one place that needs it seemed like the best option. What do you think?

Okay, I see now. Yep, it's fine, but please mention in the commit description.

Great, I'll do that.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

drivers/vhost/vsock.c vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, · line 350

Quoted 3 lines
drivers/vhost/vsock.c +0 -1
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
 
-	if (len > 0)

Why removing this check?

Quoted 7 lines
Quoted 3 lines

I think it's redundant: len is a u32, so we're basically just checking to see if it's non-zero. All the callers have already checked for this but, even if they didn't, skb_put(skb, 0) is harmless afaict.

Yep, I see, but now I don't remember why we have it, could it be more expensive to call skb_put(skb, 0), instead of just having the if for control packets with no payload?

That sounds like a questionable optimisation, but I can preserve it in the only caller that doesn't already check for a non-zero size (virtio_transport_rx_work()). I mistakenly thought that it was already checking it, but on closer inspection it only checks the size of the virtqueue buffer and doesn't look at the packet header at all.

In fact, that is itself a bug because nothing prevents an SKB overflow on the put path...

I'll add an extra fix for that in v2 so that it can be backported independently.

Will