From patchwork Wed Jan 13 13:36:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12016817 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F33A6C433E0 for ; Wed, 13 Jan 2021 13:38:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A5541233FB for ; Wed, 13 Jan 2021 13:38:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726674AbhAMNhp (ORCPT ); Wed, 13 Jan 2021 08:37:45 -0500 Received: from mail-40134.protonmail.ch ([185.70.40.134]:34210 "EHLO mail-40134.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725770AbhAMNho (ORCPT ); Wed, 13 Jan 2021 08:37:44 -0500 Date: Wed, 13 Jan 2021 13:36:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail; t=1610545022; bh=Wxlzuo0a4dl7vh+BrgTHx6uBHCVOGXVNozBxr9mFN2Q=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=Wx4+MmnLUv7niL6jpu4x8NgcUVxpCedUpf2G7Die60hRiBHBYW7TJZc6f49knbxc7 j+T8zI+kcDE+xNf163xH0m8LL7M8jLY1PQR6Q/WPuBa4rNUS4U4Ir+LfMU0RxiSiix IvHCcTXLujUw+fgn7WH+hCVLrVPfdl3kxRT1fU1J39JKUUZ1GHozTITOJEzDdPfyQ2 xRwNhQB74LcBdu//REEbDKZ0MQvy7RJ6RHTzDNHdkha8UbkMN9u2YyBo/2xZkXmEFN 3TYZLFyVPZCGDTccc7djvGnngSiuMgUGadbFBaAWVfLUAX5hMcfkZHEspa+oO83v5n Dvur77vAI5Ykw== To: "David S. Miller" , Jakub Kicinski From: Alexander Lobakin Cc: Eric Dumazet , Edward Cree , Jonathan Lemon , Willem de Bruijn , Miaohe Lin , Alexander Lobakin , Steffen Klassert , Guillaume Nault , Yadu Kishore , Al Viro , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Reply-To: Alexander Lobakin Subject: [PATCH v2 net-next 1/3] skbuff: open-code __build_skb() inside __napi_alloc_skb() Message-ID: <20210113133635.39402-1-alobakin@pm.me> In-Reply-To: <20210113133523.39205-1-alobakin@pm.me> References: <20210113133523.39205-1-alobakin@pm.me> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org In preparation for skbuff_heads caching and reusing, open-code __build_skb() inside __napi_alloc_skb() with factoring out the skbbuff_head allocation itself. Note that the return value of __build_skb_around() is not checked since it never returns anything except the given skb. Signed-off-by: Alexander Lobakin --- net/core/skbuff.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 7626a33cce59..dc3300dc2ac4 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -485,6 +485,11 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, } EXPORT_SYMBOL(__netdev_alloc_skb); +static struct sk_buff *napi_skb_cache_get(struct napi_alloc_cache *nc) +{ + return kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); +} + /** * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance * @napi: napi instance this buffer was allocated for @@ -525,12 +530,15 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, if (unlikely(!data)) return NULL; - skb = __build_skb(data, len); + skb = napi_skb_cache_get(nc); if (unlikely(!skb)) { skb_free_frag(data); return NULL; } + memset(skb, 0, offsetof(struct sk_buff, tail)); + __build_skb_around(skb, data, len); + if (nc->page.pfmemalloc) skb->pfmemalloc = 1; skb->head_frag = 1; From patchwork Wed Jan 13 13:37:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12016819 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E69DC433E6 for ; Wed, 13 Jan 2021 13:38:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C98EA23432 for ; Wed, 13 Jan 2021 13:38:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726733AbhAMNh7 (ORCPT ); Wed, 13 Jan 2021 08:37:59 -0500 Received: from mail1.protonmail.ch ([185.70.40.18]:53707 "EHLO mail1.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725770AbhAMNh7 (ORCPT ); Wed, 13 Jan 2021 08:37:59 -0500 Date: Wed, 13 Jan 2021 13:37:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail; t=1610545035; bh=UfgI6Rrb/SGM19pvZtc2Put5d76631YVvNFcRByKDyk=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=FiJR8qIkTMbWnEjOFEEyCr3hSq5mmGIMsWdSKxVPbhuY6DMnAzbJ0WLY2iGqAmyw9 VpaKgK0nqCdeHjAChrRwnwyT5v6d3k7R3BSqW7C/e4cnFG014lmD/ojlXkZ81LrtuA U+TNew4ZPlrG5nfmjGxkkQkX7zWdIAnBpGpm5w2kDNXxhYCMdMqV2o2SE+nIie7dQ8 o7mZNjFT21k7MtSl2d3MK9MpgfWvrgIe2b0oiaFlyHfpq1U94WU0+vVW5g4arpCBrj QwlImqVhqBh1XO2i+qxgb7cIbOfOIwfFCThRXQ2C7v54AKqa0VfU5MehPSJERJDCka HSo4B0ExigOqQ== To: "David S. Miller" , Jakub Kicinski From: Alexander Lobakin Cc: Eric Dumazet , Edward Cree , Jonathan Lemon , Willem de Bruijn , Miaohe Lin , Alexander Lobakin , Steffen Klassert , Guillaume Nault , Yadu Kishore , Al Viro , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Reply-To: Alexander Lobakin Subject: [PATCH v2 net-next 2/3] skbuff: (re)use NAPI skb cache on allocation path Message-ID: <20210113133635.39402-2-alobakin@pm.me> In-Reply-To: <20210113133635.39402-1-alobakin@pm.me> References: <20210113133523.39205-1-alobakin@pm.me> <20210113133635.39402-1-alobakin@pm.me> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Instead of calling kmem_cache_alloc() every time when building a NAPI skb, (re)use skbuff_heads from napi_alloc_cache.skb_cache. Previously this cache was only used for bulk-freeing skbuff_heads consumed via napi_consume_skb() or __kfree_skb_defer(). Typical path is: - skb is queued for freeing from driver or stack, its skbuff_head goes into the cache instead of immediate freeing; - driver or stack requests NAPI skb allocation, an skbuff_head is taken from the cache instead of allocation. Corner cases: - if it's empty on skb allocation, bulk-allocate the first half; - if it's full on skb consuming, bulk-wipe the second half. Also try to balance its size after completing network softirqs (__kfree_skb_flush()). prefetchw() on CONFIG_SLUB is dropped since it makes no sense anymore. Suggested-by: Edward Cree Signed-off-by: Alexander Lobakin --- net/core/skbuff.c | 54 ++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index dc3300dc2ac4..f42a3a04b918 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -364,6 +364,7 @@ struct sk_buff *build_skb_around(struct sk_buff *skb, EXPORT_SYMBOL(build_skb_around); #define NAPI_SKB_CACHE_SIZE 64 +#define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) struct napi_alloc_cache { struct page_frag_cache page; @@ -487,7 +488,15 @@ EXPORT_SYMBOL(__netdev_alloc_skb); static struct sk_buff *napi_skb_cache_get(struct napi_alloc_cache *nc) { - return kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); + if (unlikely(!nc->skb_count)) + nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, + GFP_ATOMIC, + NAPI_SKB_CACHE_HALF, + nc->skb_cache); + if (unlikely(!nc->skb_count)) + return NULL; + + return nc->skb_cache[--nc->skb_count]; } /** @@ -867,40 +876,47 @@ void __consume_stateless_skb(struct sk_buff *skb) void __kfree_skb_flush(void) { struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); + size_t count; + void **ptr; + + if (unlikely(nc->skb_count == NAPI_SKB_CACHE_HALF)) + return; + + if (nc->skb_count > NAPI_SKB_CACHE_HALF) { + count = nc->skb_count - NAPI_SKB_CACHE_HALF; + ptr = nc->skb_cache + NAPI_SKB_CACHE_HALF; - /* flush skb_cache if containing objects */ - if (nc->skb_count) { - kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, - nc->skb_cache); - nc->skb_count = 0; + kmem_cache_free_bulk(skbuff_head_cache, count, ptr); + nc->skb_count = NAPI_SKB_CACHE_HALF; + } else { + count = NAPI_SKB_CACHE_HALF - nc->skb_count; + ptr = nc->skb_cache + nc->skb_count; + + nc->skb_count += kmem_cache_alloc_bulk(skbuff_head_cache, + GFP_ATOMIC, count, + ptr); } } -static inline void _kfree_skb_defer(struct sk_buff *skb) +static void napi_skb_cache_put(struct sk_buff *skb) { struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); /* drop skb->head and call any destructors for packet */ skb_release_all(skb); - /* record skb to CPU local list */ nc->skb_cache[nc->skb_count++] = skb; -#ifdef CONFIG_SLUB - /* SLUB writes into objects when freeing */ - prefetchw(skb); -#endif - - /* flush skb_cache if it is filled */ if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { - kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, - nc->skb_cache); - nc->skb_count = 0; + kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, + nc->skb_cache + NAPI_SKB_CACHE_HALF); + nc->skb_count = NAPI_SKB_CACHE_HALF; } } + void __kfree_skb_defer(struct sk_buff *skb) { - _kfree_skb_defer(skb); + napi_skb_cache_put(skb); } void napi_consume_skb(struct sk_buff *skb, int budget) @@ -925,7 +941,7 @@ void napi_consume_skb(struct sk_buff *skb, int budget) return; } - _kfree_skb_defer(skb); + napi_skb_cache_put(skb); } EXPORT_SYMBOL(napi_consume_skb); From patchwork Wed Jan 13 13:37:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12016821 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8AECFC433E9 for ; Wed, 13 Jan 2021 13:38:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3FE09233FB for ; Wed, 13 Jan 2021 13:38:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726852AbhAMNiP (ORCPT ); Wed, 13 Jan 2021 08:38:15 -0500 Received: from mail-40136.protonmail.ch ([185.70.40.136]:17434 "EHLO mail-40136.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726222AbhAMNiP (ORCPT ); Wed, 13 Jan 2021 08:38:15 -0500 Date: Wed, 13 Jan 2021 13:37:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail; t=1610545052; bh=q+0EE369dr2h9X/gAhh7q0i4CdC3yGGoUz33tMS3zRg=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=JjOso4hKWYW+3zksOpVBtrjglv3v/DlT8/WAyovGt1tf1EJjVeijNX1S/CfhBuD/e OJDuZqLkG/A7KLlO6ow4Y5t7c0Mwi9F6xTiXNIb+NdQ5+VnF5OnUfLOMNmB59HASx3 lPoBCyuexpmXAgFCBSiYWHGv7KgZfXML0y1V0+GYw0GsLJYW4fGc8zuRFwCualGcjE f57n8B9UApeLSxcPtRPTukeI4hk8w4D9Ij3zvAywLUALGvt4Z5jXYfDNwohYX6IjxT 2Rn+3JomIlO+vT//HHaI7z6ilgZCLSh6vKmViO6AKP19vHjclnyJmLIPit+nqi73gN O8oJBJccyVaqQ== To: "David S. Miller" , Jakub Kicinski From: Alexander Lobakin Cc: Eric Dumazet , Edward Cree , Jonathan Lemon , Willem de Bruijn , Miaohe Lin , Alexander Lobakin , Steffen Klassert , Guillaume Nault , Yadu Kishore , Al Viro , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Reply-To: Alexander Lobakin Subject: [PATCH v2 net-next 3/3] skbuff: recycle GRO_MERGED_FREE skbs into NAPI skb cache Message-ID: <20210113133635.39402-3-alobakin@pm.me> In-Reply-To: <20210113133635.39402-1-alobakin@pm.me> References: <20210113133523.39205-1-alobakin@pm.me> <20210113133635.39402-1-alobakin@pm.me> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Instead of immediate freeing, recycle GRO_MERGED_FREE skbs into NAPI skb cache. This is safe, because napi_gro_receive() and napi_gro_frags() are called only inside NAPI softirq context. As many drivers call napi_alloc_skb()/napi_get_frags() on their receive path, this becomes especially useful. Signed-off-by: Alexander Lobakin --- include/linux/skbuff.h | 1 + net/core/dev.c | 9 +-------- net/core/skbuff.c | 12 +++++++++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 7a057b1f1eb8..507f1598e446 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -2888,6 +2888,7 @@ void napi_consume_skb(struct sk_buff *skb, int budget); void __kfree_skb_flush(void); void __kfree_skb_defer(struct sk_buff *skb); +void napi_skb_free_stolen_head(struct sk_buff *skb); /** * __dev_alloc_pages - allocate page for network Rx diff --git a/net/core/dev.c b/net/core/dev.c index e4d77c8abe76..c28f0d601378 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -6054,13 +6054,6 @@ struct packet_offload *gro_find_complete_by_type(__be16 type) } EXPORT_SYMBOL(gro_find_complete_by_type); -static void napi_skb_free_stolen_head(struct sk_buff *skb) -{ - skb_dst_drop(skb); - skb_ext_put(skb); - kmem_cache_free(skbuff_head_cache, skb); -} - static gro_result_t napi_skb_finish(struct napi_struct *napi, struct sk_buff *skb, gro_result_t ret) @@ -6074,7 +6067,7 @@ static gro_result_t napi_skb_finish(struct napi_struct *napi, if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) napi_skb_free_stolen_head(skb); else - __kfree_skb(skb); + __kfree_skb_defer(skb); break; case GRO_HELD: diff --git a/net/core/skbuff.c b/net/core/skbuff.c index f42a3a04b918..bf6f92f1f4c7 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -902,9 +902,6 @@ static void napi_skb_cache_put(struct sk_buff *skb) { struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); - /* drop skb->head and call any destructors for packet */ - skb_release_all(skb); - nc->skb_cache[nc->skb_count++] = skb; if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { @@ -916,6 +913,14 @@ static void napi_skb_cache_put(struct sk_buff *skb) void __kfree_skb_defer(struct sk_buff *skb) { + skb_release_all(skb); + napi_skb_cache_put(skb); +} + +void napi_skb_free_stolen_head(struct sk_buff *skb) +{ + skb_dst_drop(skb); + skb_ext_put(skb); napi_skb_cache_put(skb); } @@ -941,6 +946,7 @@ void napi_consume_skb(struct sk_buff *skb, int budget) return; } + skb_release_all(skb); napi_skb_cache_put(skb); } EXPORT_SYMBOL(napi_consume_skb);