Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1975318pxf; Sat, 13 Mar 2021 03:52:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJx5x9nssEs4aIb5Vcf/nki6fDhiXAI313mzmr4sCqGVUL9WCcpmfXkjU1AqYKFk2IOX/zrY X-Received: by 2002:a17:906:c414:: with SMTP id u20mr13452304ejz.215.1615636365685; Sat, 13 Mar 2021 03:52:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615636365; cv=none; d=google.com; s=arc-20160816; b=GIDfddYf1JWLdc9wkR1tH6amW6AH74S/G2Xkuh2HGK8WLd7CfWETf7d3pwJ79OnJvv t/QmaeO6f7cIjD370rIQ4CBy6i+30JCfejMBu4vfzZFF3beHUprxPR+jXyyQcha8ku9B DLnILnBTvBlk25vEiauZEOK2oymYMXNpMynVljQ+Kt1RYCD6GHx53O+wq3gWL4QBYtJ+ mDANKg+8zjPh+PArYc9Z/Rzdgyj63KnPHNrrAj1MIk3cVXXhFt7n71MFUkv+DcfP0ZJG Rmh0lc59CJA1u4aFmF3PaXtSDW47voMG+hvhngETa1j75zKirQypPnxIetZelo2aNznK k7oA== 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=5MpWKAjO4E1TpGup0jNHoJh4fKp9mBQ4WRLVO089bHE=; b=K4dduL0SJ+c+IHKJZTsf9SXGKB4geH61Ca/azGT/TZDOhXkF/y0PAgoDldIpedwqQ7 m32CvyvckhUXf70SFnt3Eq8ieunUQgqZKpOubEb7H9POtK+FDntUbXhHTCwaKHuA3Nph +mUQwrC4TnRnTtYL0ucFXLnteDr9EAY0dhbqVLQE5Dt6noX/2xcW+0ISg/O89fb25dWF DcvEUjlr1KBr+CJm4Jw9nuNRRAkvqZ6NXAFlMogjmuwp0KEBSeNNNcwAetstY4UCCd2W WpGqIKfX7ExKkjlaGZYXUY8D7D3D8vk1fYeWvF4ZnPCVKOQNzLV5DcwgBIozuAgtl2XM S9DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pm.me header.s=protonmail header.b=lJosGFsI; 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 j19si6585765ejs.516.2021.03.13.03.52.23; Sat, 13 Mar 2021 03:52:45 -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=lJosGFsI; 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 S233881AbhCMLii (ORCPT + 99 others); Sat, 13 Mar 2021 06:38:38 -0500 Received: from mail2.protonmail.ch ([185.70.40.22]:60278 "EHLO mail2.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231497AbhCMLiU (ORCPT ); Sat, 13 Mar 2021 06:38:20 -0500 Date: Sat, 13 Mar 2021 11:38:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail; t=1615635498; bh=5MpWKAjO4E1TpGup0jNHoJh4fKp9mBQ4WRLVO089bHE=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=lJosGFsIHUoNJ7eKfmhWt2Fru4UoM5jFXEAsiNn5NDRGwCJGYg4NvdQ38WsnfNHj2 a1r70/D7C1YtcSVIuzNOJqzHw2CcebtcZ/mVXMqrDTcRiyh4XDpd3xtIbqXr/lc2EE oiaC5h0TeGNlcFzcr7UPVZJcjpRNhA3O9n/sFsTuMBwtjVuHam9CmkQ+aWmpYaYhWs /DXzzRntt/lyDy0E2koNv5SroShb81BlO6uctaqRp+OQhmegZ8ky/xFIkVWotQyHby lYnBuBni7bVaa8J6VQopc7g3htNmt1C3RBCMOaay4d7veR6qVPr2Pb+1jixjUFFQrS R9gITd9sNnnbQ== To: "David S. Miller" , Jakub Kicinski From: Alexander Lobakin Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Jonathan Lemon , Alexander Lobakin , Eric Dumazet , Willem de Bruijn , Kevin Hao , Pablo Neira Ayuso , Jakub Sitnicki , Marco Elver , Dexuan Cui , Vladimir Oltean , Ariel Levkovich , Wang Qing , Davide Caratti , Guillaume Nault , Eran Ben Elisha , Mauro Carvalho Chehab , Kirill Tkhai , Bartosz Golaszewski , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Reply-To: Alexander Lobakin Subject: [PATCH v2 net-next 6/6] skbuff: micro-optimize {,__}skb_header_pointer() Message-ID: <20210313113645.5949-7-alobakin@pm.me> In-Reply-To: <20210313113645.5949-1-alobakin@pm.me> References: <20210313113645.5949-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 {,__}skb_header_pointer() helpers exist mainly for preventing accesses-beyond-end of the linear data. In the vast majorify of cases, they bail out on the first condition. All code going after is mostly a fallback. Mark the most common branch as 'likely' one to move it in-line. Also, skb_copy_bits() can return negative values only when the input arguments are invalid, e.g. offset is greater than skb->len. It can be safely marked as 'unlikely' branch, assuming that hotpath code provides sane input to not fail here. These two bump the throughput with a single Flow Dissector pass on every packet (e.g. with RPS or driver that uses eth_get_headlen()) on 20 Mbps per flow/core. Signed-off-by: Alexander Lobakin --- include/linux/skbuff.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 46c61e127e9f..ecc029674ae4 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3680,11 +3680,10 @@ static inline void * __must_check __skb_header_pointer(const struct sk_buff *skb, int offset, int len, =09=09 const void *data, int hlen, void *buffer) { -=09if (hlen - offset >=3D len) +=09if (likely(hlen - offset >=3D len)) =09=09return (void *)data + offset; -=09if (!skb || -=09 skb_copy_bits(skb, offset, buffer, len) < 0) +=09if (!skb || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0)) =09=09return NULL; =09return buffer; -- 2.30.2