Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp360573pxb; Wed, 13 Jan 2021 05:40:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzn3TiYEQ2gWloN2RPn/kZEiVDaL/p8q3XHYVh2iIkcf1Zqk2CNZ0cj6N4B+cdJ2Wa/VXkO X-Received: by 2002:aa7:df91:: with SMTP id b17mr1866835edy.272.1610545234801; Wed, 13 Jan 2021 05:40:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610545234; cv=none; d=google.com; s=arc-20160816; b=QxgxpbOO2w5RWSCKeJ3K7dDB0EqsLsdIwSVwDbmYUyfCxkmYzhdkC5/X/fyiNNIPml fvSQZsYKMhVjQlm2aijwkWFYWA3XHCB680uLZwP+zNeVRc/9n8AwIwDVSPkFJjJm6NAx nNyBt5nf+1BZgkvAWYuBHVTKeeJaVLpzTv0pl++Wv0LcCzPctulBNGg7QC8JR1Bk/DGM hamyg1X+W0azJGChmFmFc8HkvFdkGPNjSQGX5ODLYj0F9Y4K7zZdzfsbCknIYfS4Obk0 VJmw7wszTAp6rvuxEy4rZraBAXKKvs90S/9KC6kqGiouI3VFVm+ZS6l/1g70U7mUUn7W bIMg== 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=UfgI6Rrb/SGM19pvZtc2Put5d76631YVvNFcRByKDyk=; b=0dIYg6xzruiiFLqK33v7/34kcG+cv3f+IXcp9JzIPVe2oogD+GrdVDsXr/PCrl2G8n +JX2igrTWOxSNgV2TV2KXC8PyWagu2y8YbOQvfVcuoaw8h/Lt39XKnz76GmHQB7LBBag AJrJOVVpuyJTVwyo5MX9CskJy4FyxOamjLP26CX//w4owb6zQORWmae/0LNreOFPxhm1 SvLVM6biYUAvTJCfgucOST0qBt/6lSfl0CjGXRI9nzbMQZDL9D+MgGXBc2fDSlKHmh4f AA7KoAS2BjPaAZhgUiHcksYE/m8Y7U7VaG0uEZWrwQYFPYg4VsT3C+6E4neEeoMZ/uE0 l3yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pm.me header.s=protonmail header.b=FiJR8qIk; 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 y13si979588edv.531.2021.01.13.05.40.10; Wed, 13 Jan 2021 05:40:34 -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=FiJR8qIk; 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 S1726777AbhAMNiB (ORCPT + 99 others); Wed, 13 Jan 2021 08:38:01 -0500 Received: from mail2.protonmail.ch ([185.70.40.22]:44070 "EHLO mail2.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726011AbhAMNiA (ORCPT ); Wed, 13 Jan 2021 08:38:00 -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 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 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); =20 #define NAPI_SKB_CACHE_SIZE=0964 +#define NAPI_SKB_CACHE_HALF=09(NAPI_SKB_CACHE_SIZE / 2) =20 struct napi_alloc_cache { =09struct page_frag_cache page; @@ -487,7 +488,15 @@ EXPORT_SYMBOL(__netdev_alloc_skb); =20 static struct sk_buff *napi_skb_cache_get(struct napi_alloc_cache *nc) { -=09return kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); +=09if (unlikely(!nc->skb_count)) +=09=09nc->skb_count =3D kmem_cache_alloc_bulk(skbuff_head_cache, +=09=09=09=09=09=09 GFP_ATOMIC, +=09=09=09=09=09=09 NAPI_SKB_CACHE_HALF, +=09=09=09=09=09=09 nc->skb_cache); +=09if (unlikely(!nc->skb_count)) +=09=09return NULL; + +=09return nc->skb_cache[--nc->skb_count]; } =20 /** @@ -867,40 +876,47 @@ void __consume_stateless_skb(struct sk_buff *skb) void __kfree_skb_flush(void) { =09struct napi_alloc_cache *nc =3D this_cpu_ptr(&napi_alloc_cache); +=09size_t count; +=09void **ptr; + +=09if (unlikely(nc->skb_count =3D=3D NAPI_SKB_CACHE_HALF)) +=09=09return; + +=09if (nc->skb_count > NAPI_SKB_CACHE_HALF) { +=09=09count =3D nc->skb_count - NAPI_SKB_CACHE_HALF; +=09=09ptr =3D nc->skb_cache + NAPI_SKB_CACHE_HALF; =20 -=09/* flush skb_cache if containing objects */ -=09if (nc->skb_count) { -=09=09kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, -=09=09=09=09 nc->skb_cache); -=09=09nc->skb_count =3D 0; +=09=09kmem_cache_free_bulk(skbuff_head_cache, count, ptr); +=09=09nc->skb_count =3D NAPI_SKB_CACHE_HALF; +=09} else { +=09=09count =3D NAPI_SKB_CACHE_HALF - nc->skb_count; +=09=09ptr =3D nc->skb_cache + nc->skb_count; + +=09=09nc->skb_count +=3D kmem_cache_alloc_bulk(skbuff_head_cache, +=09=09=09=09=09=09 GFP_ATOMIC, count, +=09=09=09=09=09=09 ptr); =09} } =20 -static inline void _kfree_skb_defer(struct sk_buff *skb) +static void napi_skb_cache_put(struct sk_buff *skb) { =09struct napi_alloc_cache *nc =3D this_cpu_ptr(&napi_alloc_cache); =20 =09/* drop skb->head and call any destructors for packet */ =09skb_release_all(skb); =20 -=09/* record skb to CPU local list */ =09nc->skb_cache[nc->skb_count++] =3D skb; =20 -#ifdef CONFIG_SLUB -=09/* SLUB writes into objects when freeing */ -=09prefetchw(skb); -#endif - -=09/* flush skb_cache if it is filled */ =09if (unlikely(nc->skb_count =3D=3D NAPI_SKB_CACHE_SIZE)) { -=09=09kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, -=09=09=09=09 nc->skb_cache); -=09=09nc->skb_count =3D 0; +=09=09kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, +=09=09=09=09 nc->skb_cache + NAPI_SKB_CACHE_HALF); +=09=09nc->skb_count =3D NAPI_SKB_CACHE_HALF; =09} } + void __kfree_skb_defer(struct sk_buff *skb) { -=09_kfree_skb_defer(skb); +=09napi_skb_cache_put(skb); } =20 void napi_consume_skb(struct sk_buff *skb, int budget) @@ -925,7 +941,7 @@ void napi_consume_skb(struct sk_buff *skb, int budget) =09=09return; =09} =20 -=09_kfree_skb_defer(skb); +=09napi_skb_cache_put(skb); } EXPORT_SYMBOL(napi_consume_skb); =20 --=20 2.30.0