net: shrink napi_skb_cache_{put,get}() and napi_skb_cache_get_bulk()
Subsystems (2)
PATCH v2 1/1 net: shrink napi_skb_cache_{put,get}() and napi_skb_cache_get_bulk()
Following loop in napi_skb_cache_put() is unrolled by the compiler even if CONFIG_KASAN is not enabled:
for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
kasan_mempool_unpoison_object(nc->skb_cache[i], kmem_cache_size(net_hotdata.skbuff_cache));
We have 32 times this sequence, for a total of 384 bytes.
48 8b 3d 00 00 00 00 net_hotdata.skbuff_cache,%rdi e8 00 00 00 00 call kmem_cache_size
This is because kmem_cache_size() is not an inline and not const, and kasan_unpoison_object_data() is an inline function.
Cache kmem_cache_size() result in a variable, so that the compiler can remove dead code (and variable) when/if CONFIG_KASAN is unset.
After this patch, napi_skb_cache_put() is inlined in its callers, and we avoid one kmem_cache_size() call in napi_skb_cache_get() and napi_skb_cache_get_bulk().
--- net/core/skbuff.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
net/core/skbuff.c +10 -5
@@ -274,6 +274,11 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) }EXPORT_SYMBOL (__netdev_alloc_frag_align); +/* Cache kmem_cache_size(net_hotdata.skbuff_cache) to help the compiler +* remove dead code (and skbuff_cache_size) when CONFIG_KASAN is unset. +*/ +static u32 skbuff_cache_size__read_mostly ; +static struct sk_buff *napi_skb_cache_get (void ) {struct napi_alloc_cache *nc =this_cpu_ptr (&napi_alloc_cache);
@@ -293,7 +298,7 @@ static struct sk_buff *napi_skb_cache_get(void) skb = nc->skb_cache[--nc->skb_count];local_unlock_nested_bh (&napi_alloc_cache.bh_lock); -kasan_mempool_unpoison_object (skb,kmem_cache_size (net_hotdata.skbuff_cache)); +kasan_mempool_unpoison_object (skb, skbuff_cache_size);return skb; }
@@ -345,11 +350,9 @@ u32 napi_skb_cache_get_bulk(void **skbs, u32 n) get:for (u32 base = nc->skb_count - n, i =0 ; i < n; i++) { -u32 cache_size =kmem_cache_size (net_hotdata.skbuff_cache); - skbs[i] = nc->skb_cache[base + i]; -kasan_mempool_unpoison_object (skbs[i], cache_size); +kasan_mempool_unpoison_object (skbs[i], skbuff_cache_size);memset (skbs[i],0 , offsetof(struct sk_buff , tail)); }
@@ -1428,7 +1431,7 @@ static void napi_skb_cache_put(struct sk_buff *skb)if (unlikely (nc->skb_count ==NAPI_SKB_CACHE_SIZE )) {for (i =NAPI_SKB_CACHE_HALF ; i <NAPI_SKB_CACHE_SIZE ; i++)kasan_mempool_unpoison_object (nc->skb_cache[i], -kmem_cache_size (net_hotdata.skbuff_cache)); + skbuff_cache_size);kmem_cache_free_bulk (net_hotdata.skbuff_cache,NAPI_SKB_CACHE_HALF , nc->skb_cache +NAPI_SKB_CACHE_HALF );
@@ -5116,6 +5119,8 @@ void __init skb_init(void) offsetof(struct sk_buff , cb), sizeof_field(struct sk_buff, cb), NULL); + skbuff_cache_size =kmem_cache_size (net_hotdata.skbuff_cache); + net_hotdata.skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", sizeof(struct sk_buff_fclones), 0,
Signature · 1 line
2.51.0.858.gf9c4a03a3a-goog
Quoted 15 lines · Eric Dumazet
Following loop in
napi_skb_cache_put()is unrolled by the compiler even ifCONFIG_KASANis not enabled:for (i =
NAPI_SKB_CACHE_HALF; i <NAPI_SKB_CACHE_SIZE; i++)kasan_mempool_unpoison_object(nc->skb_cache[i], kmem_cache_size(net_hotdata.skbuff_cache));We have 32 times this sequence, for a total of 384 bytes.
48 8b 3d 00 00 00 00 net_hotdata.skbuff_cache,%rdi e8 00 00 00 00 call kmem_cache_sizeThis is because
kmem_cache_size()is not an inline and not const, andkasan_unpoison_object_data()is an inline function.
Quoted 7 lines
Cache
kmem_cache_size()result in a variable, so that the compiler can remove dead code (and variable) when/ifCONFIG_KASANis unset.After this patch,
napi_skb_cache_put()is inlined in its callers, and we avoid onekmem_cache_size()call innapi_skb_cache_get()andnapi_skb_cache_get_bulk().
Looks like a reasonable way to fix this to me.
Quoted 33 lines
Signed-off-by Eric Dumazet <[email protected]>Cc Alexander Lobakin <[email protected]>--- net/core/skbuff.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)net/core/skbuff.c +6
-1@@ -274,6 +274,11 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) }EXPORT_SYMBOL (__netdev_alloc_frag_align); +/* Cache kmem_cache_size(net_hotdata.skbuff_cache) to help the compiler +* remove dead code (and skbuff_cache_size) when CONFIG_KASAN is unset. +*/ +static u32 skbuff_cache_size__read_mostly ; +static struct sk_buff *napi_skb_cache_get (void ) {struct napi_alloc_cache *nc =this_cpu_ptr (&napi_alloc_cache);@@ -293,7 +298,7 @@ static struct sk_buff *napi_skb_cache_get(void) skb = nc->skb_cache[--nc->skb_count];local_unlock_nested_bh (&napi_alloc_cache.bh_lock); -kasan_mempool_unpoison_object (skb,kmem_cache_size (net_hotdata.skbuff_cache)); +kasan_mempool_unpoison_object (skb, skbuff_cache_size);return skb; }
net/core/skbuff.c u32 napi_skb_cache_get_bulk(void **skbs, u32 n) · line 355
Quoted 9 lines
net/core/skbuff.c +1
-3@@ -345,11 +350,9 @@ u32 napi_skb_cache_get_bulk(void **skbs, u32 n) get:for (u32 base = nc->skb_count - n, i =0 ; i < n; i++) { -u32 cache_size =kmem_cache_size (net_hotdata.skbuff_cache); - skbs[i] = nc->skb_cache[base + i]; -kasan_mempool_unpoison_object (skbs[i], cache_size); +kasan_mempool_unpoison_object (skbs[i], skbuff_cache_size);
This look already looked up cache_size separately and then call this. I guess that would be another way to avoid this. However, using the global __read_mostly makes sense. It is initialized once instead of every call, so its cheaper.
net/core/skbuff.c static void napi_skb_cache_put(struct sk_buff *skb) · line 1434
Quoted 9 lines
net/core/skbuff.c +1
-1memset (skbs[i],0 , offsetof(struct sk_buff , tail)); }@@ -1428,7 +1431,7 @@ static void napi_skb_cache_put(struct sk_buff *skb)if (unlikely (nc->skb_count ==NAPI_SKB_CACHE_SIZE )) {for (i =NAPI_SKB_CACHE_HALF ; i <NAPI_SKB_CACHE_SIZE ; i++)kasan_mempool_unpoison_object (nc->skb_cache[i], -kmem_cache_size (net_hotdata.skbuff_cache)); + skbuff_cache_size);
Previously, this inlines to a bunch of calls that check kasan_enabled().. but the compiler can't reason about it because kmem_cache_size could have side effects. Now, it sees skbuff_cache_size. Even though that variable isn't constant, it can still realize that kasan_enabled() is false, so it properly elides the entire block.
Makes sense.
Quoted 12 lines
net/core/skbuff.c +2
-0kmem_cache_free_bulk (net_hotdata.skbuff_cache,NAPI_SKB_CACHE_HALF , nc->skb_cache +NAPI_SKB_CACHE_HALF );@@ -5116,6 +5119,8 @@ void __init skb_init(void) offsetof(struct sk_buff , cb), sizeof_field(struct sk_buff, cb), NULL); + skbuff_cache_size =kmem_cache_size (net_hotdata.skbuff_cache); + net_hotdata.skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", sizeof(struct sk_buff_fclones), 0,
Quoted 20 lines · Eric Dumazet
Following loop in
napi_skb_cache_put()is unrolled by the compiler even ifCONFIG_KASANis not enabled:for (i =
NAPI_SKB_CACHE_HALF; i <NAPI_SKB_CACHE_SIZE; i++)kasan_mempool_unpoison_object(nc->skb_cache[i], kmem_cache_size(net_hotdata.skbuff_cache));We have 32 times this sequence, for a total of 384 bytes.
48 8b 3d 00 00 00 00 net_hotdata.skbuff_cache,%rdi
e8 00 00 00 00 callkmem_cache_sizeThis is because
kmem_cache_size()is not an inline and not const, andkasan_unpoison_object_data()is an inline function.Cache
kmem_cache_size()result in a variable, so that the compiler can remove dead code (and variable) when/ifCONFIG_KASANis unset.
Quoted 5 lines
After this patch,
napi_skb_cache_put()is inlined in its callers, and we avoid onekmem_cache_size()call innapi_skb_cache_get()andnapi_skb_cache_get_bulk().Signed-off-by Eric Dumazet <[email protected]>
From: Eric Dumazet <[email protected]>
Date: Thu, 16 Oct 2025 18:29:11 +0000
Quoted 19 lines
Following loop in
napi_skb_cache_put()is unrolled by the compiler even ifCONFIG_KASANis not enabled:for (i =
NAPI_SKB_CACHE_HALF; i <NAPI_SKB_CACHE_SIZE; i++)kasan_mempool_unpoison_object(nc->skb_cache[i], kmem_cache_size(net_hotdata.skbuff_cache));We have 32 times this sequence, for a total of 384 bytes.
48 8b 3d 00 00 00 00 net_hotdata.skbuff_cache,%rdi e8 00 00 00 00 call kmem_cache_sizeThis is because
kmem_cache_size()is not an inline and not const, andkasan_unpoison_object_data()is an inline function.Cache
kmem_cache_size()result in a variable, so that the compiler can remove dead code (and variable) when/ifCONFIG_KASANis unset.
Quoted 6 lines
After this patch,
napi_skb_cache_put()is inlined in its callers, and we avoid onekmem_cache_size()call innapi_skb_cache_get()andnapi_skb_cache_get_bulk().Signed-off-by Eric Dumazet <[email protected]>Cc Alexander Lobakin <[email protected]>
Thanks,
Olek
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Thu, 16 Oct 2025 18:29:11 +0000 you wrote:
Following loop in
napi_skb_cache_put()is unrolled by the compiler even ifCONFIG_KASANis not enabled:for (i =
NAPI_SKB_CACHE_HALF; i <NAPI_SKB_CACHE_SIZE; i++)kasan_mempool_unpoison_object(nc->skb_cache[i], kmem_cache_size(net_hotdata.skbuff_cache));[...]
Here is the summary with links:
- [v2,net-next] net: shrink napi_skb_cache_{put,get}() and napi_skb_cache_get_bulk()
https://git.kernel.org/netdev/net-next/c/a5cd3a60aa1d
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html