Skip to contentSkip to conversation

vsock/virtio: SKB allocation improvements

PATCH Superseded Will Deacon 4 participants 21 messages Jul 1, 2025 – Jul 13, 2025

Subsystems (7)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover vsock/virtio: SKB allocation improvements

Hello again,

Here is version two of the patches I previously posted here:

https://lore.kernel.org/r/[email protected]

Changes since v1 include:

* Remove virtio_vsock_alloc_skb_with_frags() and instead push decision to allocate nonlinear SKBs into virtio_vsock_alloc_skb()

* Remove VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE and inline its definition along with a comment

* Validate the length advertised by the packet header on the guest receive path

* Minor tweaks to the commit messages and addition of stable tags

Thanks to Stefano for all the review feedback so far.

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 (8):
vhost/vsock: Avoid allocating arbitrarily-sized SKBs vsock/virtio: Validate length in packet header before skb_put() vsock/virtio: Move length check to callers of
virtio_vsock_skb_rx_put()
vsock/virtio: Resize receive buffers so that each SKB fits in a page vsock/virtio: Add vsock helper for linear SKB allocation vhost/vsock: Allocate nonlinear SKBs for handling large receive
buffers
vsock/virtio: Rename virtio_vsock_skb_rx_put() to
virtio_vsock_skb_put()
vsock/virtio: Allocate nonlinear SKBs for handling large transmit
buffers

 drivers/vhost/vsock.c                   | 15 +++++-----
 include/linux/virtio_vsock.h            | 37 +++++++++++++++++++------
 net/vmw_vsock/virtio_transport.c        | 25 +++++++++++++----
 net/vmw_vsock/virtio_transport_common.c |  3 +-
 4 files changed, 59 insertions(+), 21 deletions(-)
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#1 lore raw

PATCH v2 1/8 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.

Fixes 71dc9ec9ac7d ("virtio/vsock: replace virtio_vsock_pkt with sk_buff")
---
 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.727.gbf7dc18ff4-goog
#2 lore raw

PATCH v2 2/8 vsock/virtio: Validate length in packet header before skb_put()

In reply to Will Deacon

When receiving a vsock packet in the guest, only the virtqueue buffer size is validated prior to virtio_vsock_skb_rx_put(). Unfortunately, virtio_vsock_skb_rx_put() uses the length from the packet header as the length argument to skb_put(), potentially resulting in SKB overflow if the host has gone wonky.

Validate the length as advertised by the packet header before calling virtio_vsock_skb_rx_put().

Fixes 71dc9ec9ac7d ("virtio/vsock: replace virtio_vsock_pkt with sk_buff")
---
 net/vmw_vsock/virtio_transport.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
net/vmw_vsock/virtio_transport.c +10 -2
index f0e48e6911fc..bd2c6aaa1a93 100644
@@ -624,8 +624,9 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	do {
 		virtqueue_disable_cb(vq);
 		for (;;) {
+			unsigned int len, payload_len;
+			struct virtio_vsock_hdr *hdr;
 			struct sk_buff *skb;
-			unsigned int len;
 
 			if (!virtio_transport_more_replies(vsock)) {
 				/* Stop rx until the device processes already
@@ -642,12 +643,19 @@ static void virtio_transport_rx_work(struct work_struct *work)
 			vsock->rx_buf_nr--;
 
 			/* Drop short/long packets */
-			if (unlikely(len < sizeof(struct virtio_vsock_hdr) ||
+			if (unlikely(len < sizeof(*hdr) ||
 				     len > virtio_vsock_skb_len(skb))) {
 				kfree_skb(skb);
 				continue;
 			}
 
+			hdr = virtio_vsock_hdr(skb);
+			payload_len = le32_to_cpu(hdr->len);
+			if (payload_len > len - sizeof(*hdr)) {
+				kfree_skb(skb);
+				continue;
+			}
+
 			virtio_vsock_skb_rx_put(skb);
 			virtio_transport_deliver_tap_pkt(skb);
 			virtio_transport_recv_pkt(&virtio_transport, skb);
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#3 lore raw

PATCH v2 3/8 vsock/virtio: Move length check to callers of virtio_vsock_skb_rx_put()

In reply to Will Deacon

virtio_vsock_skb_rx_put() only calls skb_put() if the length in the packet header is not zero even though skb_put() handles this case gracefully.

Remove the functionally redundant check from virtio_vsock_skb_rx_put() and, on the assumption that this is a worthwhile optimisation for handling credit messages, augment the existing length checks in virtio_transport_rx_work() to elide the call for zero-length payloads. Note that the vhost code already has similar logic in vhost_vsock_alloc_skb().

---
 include/linux/virtio_vsock.h     | 4 +---
 net/vmw_vsock/virtio_transport.c | 4 +++-
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/virtio_vsock.h +1 -3
index 36fb3edfa403..eb6980aa19fd 100644
@@ -52,9 +52,7 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
 	u32 len;
 
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
-
-	if (len > 0)
-		skb_put(skb, len);
+	skb_put(skb, len);
 }
 
 static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
net/vmw_vsock/virtio_transport.c +3 -1
index bd2c6aaa1a93..488e6ddc6ffa 100644
@@ -656,7 +656,9 @@ static void virtio_transport_rx_work(struct work_struct *work)
 				continue;
 			}
 
-			virtio_vsock_skb_rx_put(skb);
+			if (payload_len)
+				virtio_vsock_skb_rx_put(skb);
+
 			virtio_transport_deliver_tap_pkt(skb);
 			virtio_transport_recv_pkt(&virtio_transport, skb);
 		}
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#6 lore raw

PATCH v2 4/8 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     | 1 -
 net/vmw_vsock/virtio_transport.c | 7 ++++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/linux/virtio_vsock.h +0 -1
index eb6980aa19fd..1b5731186095 100644
@@ -109,7 +109,6 @@ 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_MAX_BUF_SIZE		0xFFFFFFFFUL
 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
 
net/vmw_vsock/virtio_transport.c +6 -1
index 488e6ddc6ffa..3daba06ed499 100644
@@ -307,7 +307,12 @@ virtio_transport_cancel_pkt(struct vsock_sock *vsk)
 
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
-	int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;
+	/* Dimension the SKB so that the entire thing fits exactly into
+	 * a single page. This avoids wasting memory due to alloc_skb()
+	 * rounding up to the next page order and also means that we
+	 * don't leave higher-order pages sitting around in the RX queue.
+	 */
+	int total_len = SKB_WITH_OVERHEAD(PAGE_SIZE);
 	struct scatterlist pkt, *p;
 	struct virtqueue *vq;
 	struct sk_buff *skb;
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#10 lore raw

PATCH v2 5/8 vsock/virtio: Add vsock helper for linear SKB allocation

In reply to Will Deacon

In preparation for nonlinear allocations for large SKBs, introduce a new virtio_vsock_alloc_linear_skb() helper to return linear SKBs unconditionally and switch all callers over to this new interface for now.

No functional change.

---
 drivers/vhost/vsock.c                   | 2 +-
 include/linux/virtio_vsock.h            | 6 ++++++
 net/vmw_vsock/virtio_transport.c        | 2 +-
 net/vmw_vsock/virtio_transport_common.c | 2 +-
 4 files changed, 9 insertions(+), 3 deletions(-)
drivers/vhost/vsock.c +1 -1
index 66a0f060770e..b13f6be452ba 100644
@@ -348,7 +348,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return NULL;
 
 	/* len contains both payload and hdr */
-	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+	skb = virtio_vsock_alloc_linear_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
include/linux/virtio_vsock.h +6 -0
index 1b5731186095..6d4a933c895a 100644
@@ -70,6 +70,12 @@ static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t ma
 	return skb;
 }
 
+static inline struct sk_buff *
+virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask)
+{
+	return virtio_vsock_alloc_skb(size, mask);
+}
+
 static inline void
 virtio_vsock_skb_queue_head(struct sk_buff_head *list, struct sk_buff *skb)
 {
net/vmw_vsock/virtio_transport.c +1 -1
index 3daba06ed499..2959db0404ed 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 -1
index 1b5d9896edae..c9eb7f7ac00d 100644
@@ -261,7 +261,7 @@ 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);
+	skb = virtio_vsock_alloc_linear_skb(skb_len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#13 lore raw

PATCH v2 6/8 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_linear_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. Move the VIRTIO_VSOCK_SKB_HEADROOM check out of the allocation function and into the single caller that needs it and add a debug warning if virtio_vsock_skb_rx_put() is ever called on an SKB with a non-zero length, as this would be destructive for the nonlinear case.

---
 drivers/vhost/vsock.c        | 11 +++++------
 include/linux/virtio_vsock.h | 32 +++++++++++++++++++++++++-------
 2 files changed, 30 insertions(+), 13 deletions(-)
drivers/vhost/vsock.c +5 -6
index b13f6be452ba..f3c2ea1d0ae7 100644
@@ -344,11 +344,12 @@ 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_linear_skb(len, GFP_KERNEL);
+	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
@@ -377,10 +378,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 -7
index 6d4a933c895a..ad69668f6b91 100644
@@ -51,29 +51,47 @@ 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);
-	skb_put(skb, len);
+
+	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_linear_skb(unsigned int size, gfp_t mask)
 {
-	return virtio_vsock_alloc_skb(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)
+{
+	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
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#16 lore raw

PATCH v2 7/8 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 f3c2ea1d0ae7..a6cd72a32f63 100644
@@ -376,7 +376,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 ad69668f6b91..ed5eab46e3dc 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 2959db0404ed..44751cf8dfca 100644
@@ -662,7 +662,7 @@ static void virtio_transport_rx_work(struct work_struct *work)
 			}
 
 			if (payload_len)
-				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.727.gbf7dc18ff4-goog
#17 lore raw

PATCH v2 8/8 vsock/virtio: 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_linear_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. No that this affects both the vhost and virtio transports.

---
 net/vmw_vsock/virtio_transport_common.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/vmw_vsock/virtio_transport_common.c +3 -2
index c9eb7f7ac00d..f74677c3511e 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,7 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *
 	if (!zcopy)
 		skb_len += payload_len;
 
-	skb = virtio_vsock_alloc_linear_skb(skb_len, GFP_KERNEL);
+	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#7 lore raw

Re: [PATCH v2 4/8] vsock/virtio: Resize receive buffers so that each SKB fits in a page

In reply to Will Deacon
Quoted 35 lines · On Tue, 1 Jul 2025 17:45:03 +0100 Will Deacon <[email protected]> wrote:

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     | 1 -
 net/vmw_vsock/virtio_transport.c | 7 ++++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/linux/virtio_vsock.h +0 -1
index eb6980aa19fd..1b5731186095 100644
@@ -109,7 +109,6 @@ 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_MAX_BUF_SIZE		0xFFFFFFFFUL
 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
 
net/vmw_vsock/virtio_transport.c +0 -0
index 488e6ddc6ffa..3daba06ed499 100644

net/vmw_vsock/virtio_transport.c virtio_transport_cancel_pkt(struct vsock_sock *vsk) · line 315

Quoted 10 lines
net/vmw_vsock/virtio_transport.c +6 -1
@@ -307,7 +307,12 @@ virtio_transport_cancel_pkt(struct vsock_sock *vsk)
 
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
-	int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;
+	/* Dimension the SKB so that the entire thing fits exactly into
+	 * a single page. This avoids wasting memory due to alloc_skb()
+	 * rounding up to the next page order and also means that we
+	 * don't leave higher-order pages sitting around in the RX queue.
+	 */
+	int total_len = SKB_WITH_OVERHEAD(PAGE_SIZE);

Should that be an explicit 4096?
Otherwise it is very wasteful of memory on systems with large pages.

David

Quoted 3 lines
net/vmw_vsock/virtio_transport.c +0 -0
 	struct scatterlist pkt, *p;
 	struct virtqueue *vq;
 	struct sk_buff *skb;
#8 lore raw

Re: [PATCH v2 4/8] vsock/virtio: Resize receive buffers so that each SKB fits in a page

Quoted 39 lines · David Laight
Quoted 36 lines · On Tue, 1 Jul 2025 17:45:03 +0100 Will Deacon <[email protected]> wrote:

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     | 1 -
 net/vmw_vsock/virtio_transport.c | 7 ++++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/linux/virtio_vsock.h +0 -1
index eb6980aa19fd..1b5731186095 100644
@@ -109,7 +109,6 @@ 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_MAX_BUF_SIZE		0xFFFFFFFFUL
 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
 
net/vmw_vsock/virtio_transport.c +0 -0
index 488e6ddc6ffa..3daba06ed499 100644
@@ -307,7 +307,12 @@ virtio_transport_cancel_pkt(struct vsock_sock *vsk)
 
Quoted 12 lines

net/vmw_vsock/virtio_transport.c virtio_transport_cancel_pkt(struct vsock_sock *vsk) · line 315

Quoted 9 lines
net/vmw_vsock/virtio_transport.c +6 -1
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
-	int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;
+	/* Dimension the SKB so that the entire thing fits exactly into
+	 * a single page. This avoids wasting memory due to alloc_skb()
+	 * rounding up to the next page order and also means that we
+	 * don't leave higher-order pages sitting around in the RX queue.
+	 */
+	int total_len = SKB_WITH_OVERHEAD(PAGE_SIZE);

Should that be an explicit 4096?
Otherwise it is very wasteful of memory on systems with large pages.

This is a good point!

What about SKB_WITH_OVERHEAD(VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE) ?

Thanks,
Stefano

Quoted 8 lines

David

Quoted 3 lines
net/vmw_vsock/virtio_transport.c +0 -0
 	struct scatterlist pkt, *p;
 	struct virtqueue *vq;
 	struct sk_buff *skb;
#4 lore raw

Re: [PATCH v2 3/8] vsock/virtio: Move length check to callers of virtio_vsock_skb_rx_put()

In reply to Will Deacon
Quoted 22 lines · Will Deacon

virtio_vsock_skb_rx_put() only calls skb_put() if the length in the packet header is not zero even though skb_put() handles this case gracefully.

Remove the functionally redundant check from virtio_vsock_skb_rx_put() and, on the assumption that this is a worthwhile optimisation for handling credit messages, augment the existing length checks in virtio_transport_rx_work() to elide the call for zero-length payloads. Note that the vhost code already has similar logic in vhost_vsock_alloc_skb().

---
include/linux/virtio_vsock.h | 4 +---
net/vmw_vsock/virtio_transport.c | 4 +++-

2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/virtio_vsock.h +0 -0
index 36fb3edfa403..eb6980aa19fd 100644

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

Quoted 7 lines
include/linux/virtio_vsock.h +1 -3
@@ -52,9 +52,7 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
 	u32 len;
 
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
-
-	if (len > 0)
-		skb_put(skb, len);
+	skb_put(skb, len);

Since the caller is supposed to check the len, can we just pass it as parameter?

So we can avoid the le32_to_cpu(virtio_vsock_hdr(skb)->len) here.

Thanks,
Stefano

Quoted 21 lines
include/linux/virtio_vsock.h +0 -0
 }
 
 static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
net/vmw_vsock/virtio_transport.c +3 -1
index bd2c6aaa1a93..488e6ddc6ffa 100644
@@ -656,7 +656,9 @@ static void virtio_transport_rx_work(struct work_struct *work)
 				continue;
 			}
 
-			virtio_vsock_skb_rx_put(skb);
+			if (payload_len)
+				virtio_vsock_skb_rx_put(skb);
+
 			virtio_transport_deliver_tap_pkt(skb);
 			virtio_transport_recv_pkt(&virtio_transport, skb);
 		}
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#11 lore raw

Re: [PATCH v2 5/8] vsock/virtio: Add vsock helper for linear SKB allocation

In reply to Will Deacon
Quoted 33 lines · Will Deacon

In preparation for nonlinear allocations for large SKBs, introduce a new virtio_vsock_alloc_linear_skb() helper to return linear SKBs unconditionally and switch all callers over to this new interface for now.

No functional change.

---

drivers/vhost/vsock.c                   | 2 +-
include/linux/virtio_vsock.h            | 6 ++++++
net/vmw_vsock/virtio_transport.c        | 2 +-
net/vmw_vsock/virtio_transport_common.c | 2 +-
4 files changed, 9 insertions(+), 3 deletions(-)
drivers/vhost/vsock.c +1 -1
index 66a0f060770e..b13f6be452ba 100644
@@ -348,7 +348,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return NULL;
 
 	/* len contains both payload and hdr */
-	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+	skb = virtio_vsock_alloc_linear_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
include/linux/virtio_vsock.h +0 -0
index 1b5731186095..6d4a933c895a 100644

include/linux/virtio_vsock.h static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t ma · line 76

Quoted 7 lines
include/linux/virtio_vsock.h +4 -0
@@ -70,6 +70,12 @@ static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t ma
 	return skb;
 }
 
+static inline struct sk_buff *
+virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask)
+{
+	return virtio_vsock_alloc_skb(size, mask);

Why not just renaming virtio_vsock_alloc_skb in this patch?

In that way we are sure when building this patch we don't leave any "old" virtio_vsock_alloc_skb() around.

Thanks,
Stefano

Quoted 34 lines
include/linux/virtio_vsock.h +2 -0
+}
+

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

net/vmw_vsock/virtio_transport.c +1 -1
index 3daba06ed499..2959db0404ed 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 -1
index 1b5d9896edae..c9eb7f7ac00d 100644
@@ -261,7 +261,7 @@ 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);
+	skb = virtio_vsock_alloc_linear_skb(skb_len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#14 lore raw

Re: [PATCH v2 6/8] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers

In reply to Will Deacon
Quoted 74 lines · Will Deacon

When receiving a packet from a guest, vhost_vsock_handle_tx_kick() calls vhost_vsock_alloc_linear_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. Move the VIRTIO_VSOCK_SKB_HEADROOM check out of the allocation function and into the single caller that needs it and add a debug warning if virtio_vsock_skb_rx_put() is ever called on an SKB with a non-zero length, as this would be destructive for the nonlinear case.

---
drivers/vhost/vsock.c | 11 +++++------
include/linux/virtio_vsock.h | 32 +++++++++++++++++++++++++-------

2 files changed, 30 insertions(+), 13 deletions(-)
drivers/vhost/vsock.c +5 -6
index b13f6be452ba..f3c2ea1d0ae7 100644
@@ -344,11 +344,12 @@ 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_linear_skb(len, GFP_KERNEL);
+	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
@@ -377,10 +378,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 +6 -2
index 6d4a933c895a..ad69668f6b91 100644
@@ -51,29 +51,47 @@ 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);
-	skb_put(skb, len);
+
+	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)

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

Quoted 10 lines
include/linux/virtio_vsock.h +5 -2
+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;

I would have made this change in a separate patch, but IIUC the only other caller is virtio_transport_alloc_skb() where this condition is implied, right?

I don't know, maybe we could have one patch where you touch this and virtio_vsock_skb_rx_put(), and another where you introduce nonlinear allocation for vhost/vsock. What do you think? (not a strong opinion, just worried about doing 2 things in a single patch)

Thanks,
Stefano

Quoted 33 lines
include/linux/virtio_vsock.h +3 -2
-
-	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_linear_skb(unsigned int size, gfp_t mask) {

include/linux/virtio_vsock.h +11 -1
-	return virtio_vsock_alloc_skb(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)
+{
+	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

Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
maintainer Reviewed-by on v2 8/8
#18 lore raw

Re: [PATCH v2 8/8] vsock/virtio: Allocate nonlinear SKBs for handling large transmit buffers

In reply to Will Deacon
Quoted 13 lines · Will Deacon

When transmitting a vsock packet, virtio_transport_send_pkt_info() calls virtio_transport_alloc_linear_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. No that this affects both the vhost and virtio transports.

Quoted 4 lines

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

1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 27 lines
net/vmw_vsock/virtio_transport_common.c +3 -2
index c9eb7f7ac00d..f74677c3511e 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,7 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *
 	if (!zcopy)
 		skb_len += payload_len;
 
-	skb = virtio_vsock_alloc_linear_skb(skb_len, GFP_KERNEL);
+	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Tested-by on the v2 series
#19 lore raw
In reply to Will Deacon

I tested this series of patches with virtio-net regression tests, everything works fine.

Quoted 64 lines · Will Deacon

Hello again,

Here is version two of the patches I previously posted here:

https://lore.kernel.org/r/[email protected]

Changes since v1 include:

* Remove virtio_vsock_alloc_skb_with_frags() and instead push decision to allocate nonlinear SKBs into virtio_vsock_alloc_skb()

* Remove VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE and inline its definition along with a comment

* Validate the length advertised by the packet header on the guest receive path

* Minor tweaks to the commit messages and addition of stable tags

Thanks to Stefano for all the review feedback so far.

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 (8):
vhost/vsock: Avoid allocating arbitrarily-sized SKBs vsock/virtio: Validate length in packet header before skb_put() vsock/virtio: Move length check to callers of
virtio_vsock_skb_rx_put()
vsock/virtio: Resize receive buffers so that each SKB fits in a page vsock/virtio: Add vsock helper for linear SKB allocation vhost/vsock: Allocate nonlinear SKBs for handling large receive
buffers
vsock/virtio: Rename virtio_vsock_skb_rx_put() to
virtio_vsock_skb_put()
vsock/virtio: Allocate nonlinear SKBs for handling large transmit
buffers

 drivers/vhost/vsock.c                   | 15 +++++-----
 include/linux/virtio_vsock.h            | 37 +++++++++++++++++++------
 net/vmw_vsock/virtio_transport.c        | 25 +++++++++++++----
 net/vmw_vsock/virtio_transport_common.c |  3 +-
 4 files changed, 59 insertions(+), 21 deletions(-)

--
2.50.0.727.gbf7dc18ff4-goog

#20 lore raw
Quoted 4 lines · Lei Yang

I tested this series of patches with virtio-net regression tests, everything works fine.

Thanks, but this series doesn't touch virtio-net: it's purely about the virtio transport for vsock. Do the virtio-net regression tests exercise that?

Cheers,

Will

#5 lore raw

Re: [PATCH v2 3/8] vsock/virtio: Move length check to callers of virtio_vsock_skb_rx_put()

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

virtio_vsock_skb_rx_put() only calls skb_put() if the length in the packet header is not zero even though skb_put() handles this case gracefully.

Remove the functionally redundant check from virtio_vsock_skb_rx_put() and, on the assumption that this is a worthwhile optimisation for handling credit messages, augment the existing length checks in virtio_transport_rx_work() to elide the call for zero-length payloads. Note that the vhost code already has similar logic in vhost_vsock_alloc_skb().

---
include/linux/virtio_vsock.h | 4 +---
net/vmw_vsock/virtio_transport.c | 4 +++-

2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/virtio_vsock.h +0 -0
index 36fb3edfa403..eb6980aa19fd 100644

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

Quoted 7 lines
include/linux/virtio_vsock.h +1 -3
@@ -52,9 +52,7 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
 	u32 len;
 
 	len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
-
-	if (len > 0)
-		skb_put(skb, len);
+	skb_put(skb, len);

Since the caller is supposed to check the len, can we just pass it as parameter?

Quoted 1 line

So we can avoid the le32_to_cpu(virtio_vsock_hdr(skb)->len) here.

Sure, I'll do that. It means that virtio_vsock_skb_rx_put() will briefly be a simple wrapper around skb_put() but once the non-linear handling comes in then it becomes useful again.

Will

#9 lore raw

Re: [PATCH v2 4/8] vsock/virtio: Resize receive buffers so that each SKB fits in a page

In reply to Stefano Garzarella
Quoted 53 lines · Stefano Garzarella
Quoted 39 lines · David Laight
Quoted 36 lines · On Tue, 1 Jul 2025 17:45:03 +0100 Will Deacon <[email protected]> wrote:

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     | 1 -
 net/vmw_vsock/virtio_transport.c | 7 ++++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/linux/virtio_vsock.h +0 -1
index eb6980aa19fd..1b5731186095 100644
@@ -109,7 +109,6 @@ 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_MAX_BUF_SIZE		0xFFFFFFFFUL
 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
 
net/vmw_vsock/virtio_transport.c +0 -0
index 488e6ddc6ffa..3daba06ed499 100644
@@ -307,7 +307,12 @@ virtio_transport_cancel_pkt(struct vsock_sock *vsk)
 
Quoted 12 lines

net/vmw_vsock/virtio_transport.c virtio_transport_cancel_pkt(struct vsock_sock *vsk) · line 315

Quoted 9 lines
net/vmw_vsock/virtio_transport.c +6 -1
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
-	int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;
+	/* Dimension the SKB so that the entire thing fits exactly into
+	 * a single page. This avoids wasting memory due to alloc_skb()
+	 * rounding up to the next page order and also means that we
+	 * don't leave higher-order pages sitting around in the RX queue.
+	 */
+	int total_len = SKB_WITH_OVERHEAD(PAGE_SIZE);

Should that be an explicit 4096?
Otherwise it is very wasteful of memory on systems with large pages.

Quoted 3 lines

This is a good point!

What about SKB_WITH_OVERHEAD(VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE) ?

Personally, I'd prefer to use PAGE_SIZE here as I think it's reasonable that using a larger page size ends up with a proportionately higher memory utilisation but potentially better performance. At least, configuring the kernel with a larger page size and expecting no impact on memory consumption is misguided. The 4KiB constant seems fairly arbitrary (and has been there since day 1), so I can only assume that it is acting as a proxy for PAGE_SIZE on architectures where that is fixed to 4KiB.

However, I concede that the intention of this patch is to avoid the allocation inefficiency described in the commit message, so I'll revert to 4KiB as you suggest above but moving the SKB overhead calculation into the definition:

#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE SKB_WITH_OVERHEAD(1024 * 4)

That way, I can move the comment there as well and it should be clear why we've ended up with what we have.

Will

#12 lore raw

Re: [PATCH v2 5/8] vsock/virtio: Add vsock helper for linear SKB allocation

In reply to Stefano Garzarella
Quoted 44 lines · Stefano Garzarella
Quoted 33 lines · Will Deacon

In preparation for nonlinear allocations for large SKBs, introduce a new virtio_vsock_alloc_linear_skb() helper to return linear SKBs unconditionally and switch all callers over to this new interface for now.

No functional change.

---

drivers/vhost/vsock.c                   | 2 +-
include/linux/virtio_vsock.h            | 6 ++++++
net/vmw_vsock/virtio_transport.c        | 2 +-
net/vmw_vsock/virtio_transport_common.c | 2 +-
4 files changed, 9 insertions(+), 3 deletions(-)
drivers/vhost/vsock.c +1 -1
index 66a0f060770e..b13f6be452ba 100644
@@ -348,7 +348,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq,
 		return NULL;
 
 	/* len contains both payload and hdr */
-	skb = virtio_vsock_alloc_skb(len, GFP_KERNEL);
+	skb = virtio_vsock_alloc_linear_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
 
include/linux/virtio_vsock.h +0 -0
index 1b5731186095..6d4a933c895a 100644

include/linux/virtio_vsock.h static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t ma · line 76

Quoted 7 lines
include/linux/virtio_vsock.h +4 -0
@@ -70,6 +70,12 @@ static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t ma
 	return skb;
 }
 
+static inline struct sk_buff *
+virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask)
+{
+	return virtio_vsock_alloc_skb(size, mask);

Why not just renaming virtio_vsock_alloc_skb in this patch?

Quoted 2 lines

In that way we are sure when building this patch we don't leave any "old" virtio_vsock_alloc_skb() around.

We'll be bringing virtio_vsock_alloc_skb() back almost immediately, but I can do that if you like.

Will

#15 lore raw

Re: [PATCH v2 6/8] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers

In reply to Stefano Garzarella
Quoted 10 lines · Stefano Garzarella
Quoted 9 lines · Will Deacon
include/linux/virtio_vsock.h +4 -1
-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;
include/linux/virtio_vsock.h +1 -0
+	int err;
Quoted 6 lines

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

Quoted 2 lines
include/linux/virtio_vsock.h +0 -2
-	if (size < VIRTIO_VSOCK_SKB_HEADROOM)
-		return NULL;

I would have made this change in a separate patch, but IIUC the only other caller is virtio_transport_alloc_skb() where this condition is implied, right?

At this point in the series, virtio_vsock_alloc_skb() only has a single caller (vhost_vsock_alloc_skb()) which already has a partial bounds check and so this patch extends it to cover the lower bound.

Later (in "vsock/virtio: Allocate nonlinear SKBs for handling large transmit buffers"), virtio_transport_alloc_skb() gets converted over and that's fine because it never allocates less than VIRTIO_VSOCK_SKB_HEADROOM.

Quoted 4 lines

I don't know, maybe we could have one patch where you touch this and virtio_vsock_skb_rx_put(), and another where you introduce nonlinear allocation for vhost/vsock. What do you think? (not a strong opinion, just worried about doing 2 things in a single patch)

I can spin a separate patch for the bounds check.

Will