Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1532001pxf; Fri, 12 Mar 2021 11:50:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJydopjpPds4c91AnKAxEgK0XKrVQAyreiJ80Nijw8A08Zh2ubw0hDYIZYKy/0bAwHzWsM3G X-Received: by 2002:a17:906:be9:: with SMTP id z9mr10260249ejg.35.1615578613172; Fri, 12 Mar 2021 11:50:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615578613; cv=none; d=google.com; s=arc-20160816; b=K60CEJbn3/3ogj1YZIZ/EcJm3YJTVz2LE0otgGQmQiWoHgARhzKqS/LqyJPX+5zKFY iMqISfw0yFnQVZMHS2xQBn70NgDM4gIqsgAnXp0HPemUcijwbAmGDD6Nf7w7ynFidzFL 6Zdz/jaOwjubsSXyZsymtSeN/ZgE+AgwvQID8mcHOyMOC9cYW0s7MRFXlnEoajPgpUp+ AHCik3MGat1u7wTevWwqw05yq857LvJ04pC2+arfLJGm84cs6OoZvuWmfrlJ2ME143Dk A+SxEvQ7sX0xfBQARZ3aPEaKFahKTuZwQ/YjxEN97yehiaxaW5EnvS94hFz5zpY9BNT6 11kA== 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=0WXUDRS6l3uzo7FR/B86Gske9XTm/nKNi8/gZiVufio=; b=zBrOIc0OuceF5+Pkl51pobvicGp4BttoZEcntyMA/5NZLYMFKYSf6gDHtUmWQsvPvh a9G+4OLskjfg4mNV3IPalmULcV9PWGPHjBqGYYz8h/q//Tzhlth9Q2duaNBk87uv/ntA YCs1UoxxbbNNApO7Ko/doya77XJuGSkVvbYVen/EsqozEX1BplK2N7UoOOFRYBMZG0kS YerlWYLi0mmDuUKW2jxc51Ybzp3PBPv3Z4h/4fsSyvwLc0gcK/WEJ/HmOUsGWhLIkQXF Gul+9jKbURSQXJn8borDo+DctHokJb+KR3niNKmOgfTyxEEP7Dgi47qPuEzwnuTs9BA4 sNHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pm.me header.s=protonmail header.b=W0fw0vAz; 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 y11si5172387edj.305.2021.03.12.11.49.50; Fri, 12 Mar 2021 11:50:13 -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=W0fw0vAz; 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 S234493AbhCLTrm (ORCPT + 99 others); Fri, 12 Mar 2021 14:47:42 -0500 Received: from mail-40133.protonmail.ch ([185.70.40.133]:18155 "EHLO mail-40133.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234485AbhCLTrN (ORCPT ); Fri, 12 Mar 2021 14:47:13 -0500 Date: Fri, 12 Mar 2021 19:47:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail; t=1615578431; bh=0WXUDRS6l3uzo7FR/B86Gske9XTm/nKNi8/gZiVufio=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=W0fw0vAzGEdIe1NRTwFI0Zc+jgpfNGER5XVeToKKqjERriU/eabdNJjFcYaPzIYvR sgsHppSchZl7I1AchfFUvTTIHfHiQn6wcnBeP+CKBgsdVAPF/raBbAKhLA0Rd/x3uU TcM7ZMb+BjPva1FjJYCBHlK9NXuEl44vhYJ1GfasZIITzCKPNlW18uvDcvV5BsWGz2 R6FHupHuS4DYidzdmyScY9++8sttJFJWdffJ0skHDWhN1dgUfY7saizL7brVRZF0sP oUQCymviXI5/PNSi+Crt42y1nYhSAccdY+hbPhzVKl3UzxAydlQ8qF2tk0XFRAq85H 8/P633Zz1TQtg== 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 net-next 6/6] skbuff: micro-optimize {,__}skb_header_pointer() Message-ID: <20210312194538.337504-7-alobakin@pm.me> In-Reply-To: <20210312194538.337504-1-alobakin@pm.me> References: <20210312194538.337504-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 7873f24c0ae5..71f4d609819e 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