Skip to content

Commit 0f6925b

Browse files
edumazetdavem330
authored andcommitted
virtio_net: Do not pull payload in skb->head
Xuan Zhuo reported that commit 3226b15 ("net: avoid 32 x truesize under-estimation for tiny skbs") brought a ~10% performance drop. The reason for the performance drop was that GRO was forced to chain sk_buff (using skb_shinfo(skb)->frag_list), which uses more memory but also cause packet consumers to go over a lot of overhead handling all the tiny skbs. It turns out that virtio_net page_to_skb() has a wrong strategy : It allocates skbs with GOOD_COPY_LEN (128) bytes in skb->head, then copies 128 bytes from the page, before feeding the packet to GRO stack. This was suboptimal before commit 3226b15 ("net: avoid 32 x truesize under-estimation for tiny skbs") because GRO was using 2 frags per MSS, meaning we were not packing MSS with 100% efficiency. Fix is to pull only the ethernet header in page_to_skb() Then, we change virtio_net_hdr_to_skb() to pull the missing headers, instead of assuming they were already pulled by callers. This fixes the performance regression, but could also allow virtio_net to accept packets with more than 128bytes of headers. Many thanks to Xuan Zhuo for his report, and his tests/help. Fixes: 3226b15 ("net: avoid 32 x truesize under-estimation for tiny skbs") Reported-by: Xuan Zhuo <[email protected]> Link: https://www.spinics.net/lists/netdev/msg731397.html Co-Developed-by: Xuan Zhuo <[email protected]> Signed-off-by: Xuan Zhuo <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Cc: "Michael S. Tsirkin" <[email protected]> Cc: Jason Wang <[email protected]> Cc: [email protected] Acked-by: Jason Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b25b343 commit 0f6925b

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

drivers/net/virtio_net.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,13 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
406406
offset += hdr_padded_len;
407407
p += hdr_padded_len;
408408

409-
copy = len;
410-
if (copy > skb_tailroom(skb))
411-
copy = skb_tailroom(skb);
409+
/* Copy all frame if it fits skb->head, otherwise
410+
* we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
411+
*/
412+
if (len <= skb_tailroom(skb))
413+
copy = len;
414+
else
415+
copy = ETH_HLEN + metasize;
412416
skb_put_data(skb, p, copy);
413417

414418
if (metasize) {

include/linux/virtio_net.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,18 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
6565
skb_reset_mac_header(skb);
6666

6767
if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
68-
u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
69-
u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
68+
u32 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
69+
u32 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
70+
u32 needed = start + max_t(u32, thlen, off + sizeof(__sum16));
71+
72+
if (!pskb_may_pull(skb, needed))
73+
return -EINVAL;
7074

7175
if (!skb_partial_csum_set(skb, start, off))
7276
return -EINVAL;
7377

7478
p_off = skb_transport_offset(skb) + thlen;
75-
if (p_off > skb_headlen(skb))
79+
if (!pskb_may_pull(skb, p_off))
7680
return -EINVAL;
7781
} else {
7882
/* gso packets without NEEDS_CSUM do not set transport_offset.
@@ -102,14 +106,14 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
102106
}
103107

104108
p_off = keys.control.thoff + thlen;
105-
if (p_off > skb_headlen(skb) ||
109+
if (!pskb_may_pull(skb, p_off) ||
106110
keys.basic.ip_proto != ip_proto)
107111
return -EINVAL;
108112

109113
skb_set_transport_header(skb, keys.control.thoff);
110114
} else if (gso_type) {
111115
p_off = thlen;
112-
if (p_off > skb_headlen(skb))
116+
if (!pskb_may_pull(skb, p_off))
113117
return -EINVAL;
114118
}
115119
}

0 commit comments

Comments
 (0)