Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1596852pxb; Mon, 12 Apr 2021 01:52:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuoEHqrl5ubO2gtMfbhSm23GibHr4/SYO3zI2ZmP44AHds1BDUE0c86i54MzhR9fWqCglY X-Received: by 2002:a17:902:bd4b:b029:e6:a4a1:9d8b with SMTP id b11-20020a170902bd4bb02900e6a4a19d8bmr26119641plx.56.1618217535041; Mon, 12 Apr 2021 01:52:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618217535; cv=none; d=google.com; s=arc-20160816; b=R+R8CY+IRCyk2zN6OVSuJ9sMmmcn5XaU9WkUuoIAEApzs77C8mklIvv+EwDvAPe5VW 5GPgSuthjd0Q5cvjVjYv5m/k5D2BqKy4pVTbTvz7Xb+UreCcAYGa1+u1j/j5bfoMBVF1 WPP4befDQJro6PaAwZOCJfGfgn4V7sRrVqmbeqsbLYGuOigXeR+TKhfauwI2zLYs67ER PPRKtoBvlgG76UiEjEvAWVNQy5Yi1K9V6uix5BZGwUADWoJyQ0axVExLWEKdpJpElGuA MUF1pilTY91EaCuqIIrJ/2JMfhmTTr8asZfqDcRZCACbcMGYuFv7fjR5yNO8AuezluvX P1bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TNGnI/6WkaqOGNQelyLm8Dgte5Id3U38hLKvR9yJvDI=; b=btQIaquJU/92R0AJuBOFNCGFMLuYErDF6C2ZDFbFnxsBZGYYti8kCU8a3oS4P7+P2y 9fR+AzUuV7sb06pvCenQQJy7tMTxEPQNjwKBS4wTqBDfmdA4v1XldwSls/NpGu3+/FnZ bccX+nhXC3VFqaPLoGTFHI90U38MxaoXFtMpuLF+ojqQSwk3hTVJYqavsXf89soEo/fU U4xUpFPfyC679mb0ZhCffud6x/fupGm40IQiCNphld7nkV4ZGEGBwhX9DE1pXUEUYEr/ D/4AzqPr3W1qlsLQni3ADBeOomhyu66reXSZ5C3l4kMboYP1YB0g0zagkCjb9ZKLrMIa eWLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LCS9HHt9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 136si15779282pfz.261.2021.04.12.01.52.01; Mon, 12 Apr 2021 01:52:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LCS9HHt9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238569AbhDLIuG (ORCPT + 99 others); Mon, 12 Apr 2021 04:50:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:40046 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238005AbhDLIrH (ORCPT ); Mon, 12 Apr 2021 04:47:07 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1716C61247; Mon, 12 Apr 2021 08:46:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618217209; bh=trgiaBG4kEh8rt00jC3X9zIWjcQ2Ue7UylSCEk2Qjkk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LCS9HHt9hGdaA5G3mvAWUYwLKjCXSGKuZra4FCJFT3++uPN0sTHU1nOzwn83izMIs CL3cBGc3ow9oY7Pt2Q/D3LRQXnnHpuP4y+oMaR1pxL8Hhv536GdMnPfYgs7E0T0dgE 0sbJX6VrKkfjMy3GyUkgh5sMiFKPRYTg/loMcoy0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xuan Zhuo , Eric Dumazet , "Michael S. Tsirkin" , Jason Wang , virtualization@lists.linux-foundation.org, "David S. Miller" , Sasha Levin Subject: [PATCH 5.4 042/111] virtio_net: Do not pull payload in skb->head Date: Mon, 12 Apr 2021 10:40:20 +0200 Message-Id: <20210412084005.653952525@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210412084004.200986670@linuxfoundation.org> References: <20210412084004.200986670@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet [ Upstream commit 0f6925b3e8da0dbbb52447ca8a8b42b371aac7db ] Xuan Zhuo reported that commit 3226b158e67c ("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 3226b158e67c ("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: 3226b158e67c ("net: avoid 32 x truesize under-estimation for tiny skbs") Reported-by: Xuan Zhuo Link: https://www.spinics.net/lists/netdev/msg731397.html Co-Developed-by: Xuan Zhuo Signed-off-by: Xuan Zhuo Signed-off-by: Eric Dumazet Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: virtualization@lists.linux-foundation.org Acked-by: Jason Wang Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/net/virtio_net.c | 10 +++++++--- include/linux/virtio_net.h | 14 +++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index b67460864b3c..d8ee001d8e8e 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -406,9 +406,13 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi, offset += hdr_padded_len; p += hdr_padded_len; - copy = len; - if (copy > skb_tailroom(skb)) - copy = skb_tailroom(skb); + /* Copy all frame if it fits skb->head, otherwise + * we let virtio_net_hdr_to_skb() and GRO pull headers as needed. + */ + if (len <= skb_tailroom(skb)) + copy = len; + else + copy = ETH_HLEN + metasize; skb_put_data(skb, p, copy); if (metasize) { diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h index 98775d7fa696..b465f8f3e554 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -65,14 +65,18 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb, skb_reset_mac_header(skb); if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { - u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start); - u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset); + u32 start = __virtio16_to_cpu(little_endian, hdr->csum_start); + u32 off = __virtio16_to_cpu(little_endian, hdr->csum_offset); + u32 needed = start + max_t(u32, thlen, off + sizeof(__sum16)); + + if (!pskb_may_pull(skb, needed)) + return -EINVAL; if (!skb_partial_csum_set(skb, start, off)) return -EINVAL; p_off = skb_transport_offset(skb) + thlen; - if (p_off > skb_headlen(skb)) + if (!pskb_may_pull(skb, p_off)) return -EINVAL; } else { /* 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, } p_off = keys.control.thoff + thlen; - if (p_off > skb_headlen(skb) || + if (!pskb_may_pull(skb, p_off) || keys.basic.ip_proto != ip_proto) return -EINVAL; skb_set_transport_header(skb, keys.control.thoff); } else if (gso_type) { p_off = thlen; - if (p_off > skb_headlen(skb)) + if (!pskb_may_pull(skb, p_off)) return -EINVAL; } } -- 2.30.2