Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp5632885pxb; Tue, 16 Feb 2021 03:47:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJwMJeweFRPqy85lZTQDz33aUjzSCQ7qGD2sjUfteGmCLD7kvsil4HA45sxWhqUDILlV+zJx X-Received: by 2002:a17:906:719:: with SMTP id y25mr19735047ejb.180.1613476042496; Tue, 16 Feb 2021 03:47:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613476042; cv=none; d=google.com; s=arc-20160816; b=So8esNgT83whv94JaWu1DXWK5T7i5lcJcmT0HfGsoDkjB6vsu+RH9IExSCKa9OQyAq fwRiA+faiWlAdlYui2zRyQgXMxsDQ7puoLz5KykG8PfMYVntvlYNfr2pjjmqCl/RbNo9 VRfZzidfiwhhavnUnaszL91uByARuX4wl+4uSeH6/i/D1UPchXfSMSoS8e848BKIF9Dg Vz6GDNQebQyhHP0N0dPN+n+f4rFN44MvsVt22WKE76clTvZtd2V5v2F5HIxig3qRe71b TC0l9GRshPTzRuF8ymzx5NUaLUU2prgHmFF7SxF7cBET8m4PSt4BzFAf+PW1SE/wQMyX gckA== 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 :references:in-reply-to:message-id:subject:reply-to:cc:from:to :dkim-signature:date; bh=Ru9ZuTDMSkYaHJQlZyLU2P2nHfTlHc6MhspZ6RaCTbE=; b=jHvpyk3Fc8bUzHe7cbdZ6jGW06wvvqLttuFTzNgNhiCqQneQ/61iXrKT2o81lQG1X3 NQRiGrRejALTB2GKB297y/k1jr8lBNbvacDFfHB5APWVSNBXK5TjdHRIlGub1KRpfGfn C4mCC4tFDyJ/YA6HG62aZ1oOZabDcJuIIDljNQPJQvrin96xadj25n9Fc/quDf6RDRew zQPSjzpscK91zWWioc3U1QneC3iGf8l2L4PARQdp6SPt1WjDt743rfDOxgKyJWgRHnbV xL4U6apwwTH2f409bPo0BczdmOA71cZ2OCmyrENTT0q7eHeT3p8obt6OyiERc/qk9lgp Jl1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pm.me header.s=protonmail header.b=ZuBxx12w; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pm.me Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rp11si14268372ejb.724.2021.02.16.03.46.59; Tue, 16 Feb 2021 03:47:22 -0800 (PST) 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=@pm.me header.s=protonmail header.b=ZuBxx12w; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pm.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229767AbhBPLqG (ORCPT + 99 others); Tue, 16 Feb 2021 06:46:06 -0500 Received: from mail-40134.protonmail.ch ([185.70.40.134]:20002 "EHLO mail-40134.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230399AbhBPLjy (ORCPT ); Tue, 16 Feb 2021 06:39:54 -0500 Date: Tue, 16 Feb 2021 11:39:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail; t=1613475552; bh=Ru9ZuTDMSkYaHJQlZyLU2P2nHfTlHc6MhspZ6RaCTbE=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=ZuBxx12wpCbrpRmiZ7BovRt9vjJP/J4c3uRvxy7IydJdCgzCU2LZmlCTZ62QROZ4V AZAgz38M07zP0GbPYEZWaiE284XL4zxwwaqsrUbVFa922j01mynYNQwEebaJvoACqs gVlkiXwuDce53+5riHHwZ6i6nLkEoQzf5FRpdTem8Ger2lo57SglQYxUPCiVnExFCx ZEVI/+xO0d9edWlAdxPWqhCzGq2rt7QwlMbe5Cn//vS2u8L0XGIm5mYOMBokOS+lzz AyDN1hZCNJpZbNVVhRJxpe0UoTeebhLhNhIthQqkdzcRhUxA4Q4uMCOwYo3BLyPVvR 5cxOi1aQ7BAOg== To: Magnus Karlsson , =?utf-8?Q?Bj=C3=B6rn_T=C3=B6pel?= From: Alexander Lobakin Cc: "Michael S. Tsirkin" , Jason Wang , "David S. Miller" , Jakub Kicinski , Jonathan Lemon , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Paolo Abeni , Eric Dumazet , Xuan Zhuo , Dust Li , Alexander Lobakin , virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Reply-To: Alexander Lobakin Subject: [PATCH v4 bpf-next 5/6] xsk: respect device's headroom and tailroom on generic xmit path Message-ID: <20210216113740.62041-6-alobakin@pm.me> In-Reply-To: <20210216113740.62041-1-alobakin@pm.me> References: <20210216113740.62041-1-alobakin@pm.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=10.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mailout.protonmail.ch Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org xsk_generic_xmit() allocates a new skb and then queues it for xmitting. The size of new skb's headroom is desc->len, so it comes to the driver/device with no reserved headroom and/or tailroom. Lots of drivers need some headroom (and sometimes tailroom) to prepend (and/or append) some headers or data, e.g. CPU tags, device-specific headers/descriptors (LSO, TLS etc.), and if case of no available space skb_cow_head() will reallocate the skb. Reallocations are unwanted on fast-path, especially when it comes to XDP, so generic XSK xmit should reserve the spaces declared in dev->needed_headroom and dev->needed tailroom to avoid them. Note on max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom)): Usually, output functions reserve LL_RESERVED_SPACE(dev), which consists of dev->hard_header_len + dev->needed_headroom, aligned by 16. However, on XSK xmit hard header is already here in the chunk, so hard_header_len is not needed. But it'd still be better to align data up to cacheline, while reserving no less than driver requests for headroom. NET_SKB_PAD here is to double-insure there will be no reallocations even when the driver advertises no needed_headroom, but in fact need it (not so rare case). Fixes: 35fcde7f8deb ("xsk: support for Tx") Signed-off-by: Alexander Lobakin --- net/xdp/xsk.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index 4faabd1ecfd1..143979ea4165 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -454,12 +454,16 @@ static int xsk_generic_xmit(struct sock *sk) =09struct sk_buff *skb; =09unsigned long flags; =09int err =3D 0; +=09u32 hr, tr; =20 =09mutex_lock(&xs->mutex); =20 =09if (xs->queue_id >=3D xs->dev->real_num_tx_queues) =09=09goto out; =20 +=09hr =3D max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom)); +=09tr =3D xs->dev->needed_tailroom; + =09while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) { =09=09char *buffer; =09=09u64 addr; @@ -471,11 +475,13 @@ static int xsk_generic_xmit(struct sock *sk) =09=09} =20 =09=09len =3D desc.len; -=09=09skb =3D sock_alloc_send_skb(sk, len, 1, &err); +=09=09skb =3D sock_alloc_send_skb(sk, hr + len + tr, 1, &err); =09=09if (unlikely(!skb)) =09=09=09goto out; =20 +=09=09skb_reserve(skb, hr); =09=09skb_put(skb, len); + =09=09addr =3D desc.addr; =09=09buffer =3D xsk_buff_raw_get_data(xs->pool, addr); =09=09err =3D skb_store_bits(skb, 0, buffer, len); --=20 2.30.1