net: lockless skb_attempt_defer_free()
Subsystems (2)
v2 cover net: lockless skb_attempt_defer_free()
Platforms with many cpus and relatively slow inter connect show a significant spinlock contention in skb_attempt_defer_free().
This series refactors this infrastructure to be NUMA aware, and lockless.
Tested on various platforms, including AMD Zen 2/3/4 and Intel Granite Rapids, showing significant cost reductions under network stress (more than 20 Mpps).
Eric Dumazet (3):
net: make softnet_data.defer_count an atomic
net: use llist for sd->defer_list
net: add NUMA awareness to skb_attempt_defer_free()
include/linux/netdevice.h | 6 +----- include/net/hotdata.h | 7 +++++++ net/core/dev.c | 41 ++++++++++++++++++++++----------------- net/core/dev.h | 2 +- net/core/skbuff.c | 24 +++++++++++------------ 5 files changed, 43 insertions(+), 37 deletions(-)
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
PATCH v2 1/3 net: make softnet_data.defer_count an atomic
This is preparation work to remove the softnet_data.defer_lock, as it is contended on hosts with large number of cores.
--- include/linux/netdevice.h | 2 +- net/core/dev.c | 2 +- net/core/skbuff.c | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-)
include/linux/netdevice.h +1 -1
@@ -3538,7 +3538,7 @@ struct softnet_data {/* Another possibly contended cache line */ spinlock_t defer_lock____cacheline_aligned_in_smp ; -int defer_count; +atomic_t defer_count;int defer_ipi_scheduled;struct sk_buff *defer_list;call_single_data_t defer_csd;
net/core/dev.c +1 -1
@@ -6726,7 +6726,7 @@ static void skb_defer_free_flush(struct softnet_data *sd)spin_lock (&sd->defer_lock); skb = sd->defer_list; sd->defer_list =NULL ; - sd->defer_count =0 ; +atomic_set (&sd->defer_count,0 );spin_unlock (&sd->defer_lock);while (skb !=NULL ) {
net/core/skbuff.c +2 -4
@@ -7201,14 +7201,12 @@ nodefer: kfree_skb_napi_cache(skb); sd = &per_cpu (softnet_data, cpu); defer_max =READ_ONCE (net_hotdata.sysctl_skb_defer_max); -if (READ_ONCE (sd->defer_count) >= defer_max) +if (atomic_read (&sd->defer_count) >= defer_max) goto nodefer;spin_lock_bh (&sd->defer_lock);/* Send an IPI every time queue reaches half capacity. */ - kick = sd->defer_count == (defer_max >>1 ); -/* Paired with the READ_ONCE() few lines above */ -WRITE_ONCE (sd->defer_count, sd->defer_count +1 ); + kick = (atomic_inc_return (&sd->defer_count) -1 ) == (defer_max >>1 ); skb->next = sd->defer_list;/* Paired with READ_ONCE() in skb_defer_free_flush() */
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
PATCH v2 2/3 net: use llist for sd->defer_list
In reply to Eric DumazetGet rid of sd->defer_lock and adopt llist operations.
We optimize skb_attempt_defer_free() for the common case, where the packet is queued. Otherwise sd->defer_count is increasing, until skb_defer_free_flush() clears it.
--- include/linux/netdevice.h | 8 ++++---- net/core/dev.c | 18 ++++++------------ net/core/skbuff.c | 15 +++++++-------- 3 files changed, 17 insertions(+), 24 deletions(-)
include/linux/netdevice.h +4 -4
@@ -3537,10 +3537,10 @@ struct softnet_data {struct numa_drop_counters drop_counters;/* Another possibly contended cache line */ -spinlock_t defer_lock____cacheline_aligned_in_smp ; -atomic_t defer_count; -int defer_ipi_scheduled; -struct sk_buff *defer_list; +struct llist_head defer_list____cacheline_aligned_in_smp ; +atomic_long_t defer_count; + +int defer_ipi_scheduled____cacheline_aligned_in_smp ;call_single_data_t defer_csd; };
net/core/dev.c +6 -12
@@ -6717,22 +6717,16 @@ EXPORT_SYMBOL(napi_complete_done);static void skb_defer_free_flush (struct softnet_data *sd) { +struct llist_node *free_list;struct sk_buff *skb, *next; -/* Paired with WRITE_ONCE() in skb_attempt_defer_free() */ -if (!READ_ONCE (sd->defer_list)) +if (llist_empty (&sd->defer_list))return ; +atomic_long_set (&sd->defer_count,0 ); + free_list =llist_del_all (&sd->defer_list); -spin_lock (&sd->defer_lock); - skb = sd->defer_list; - sd->defer_list =NULL ; -atomic_set (&sd->defer_count,0 ); -spin_unlock (&sd->defer_lock); - -while (skb !=NULL ) { - next = skb->next; +llist_for_each_entry_safe (skb, next, free_list, ll_node) {napi_consume_skb (skb,1 ); - skb = next; } }
@@ -12995,7 +12989,7 @@ static int __init net_dev_init(void) sd->cpu = i;#endif INIT_CSD (&sd->defer_csd, trigger_rx_softirq, sd); -spin_lock_init (&sd->defer_lock); +init_llist_head (&sd->defer_list);gro_init (&sd->backlog.gro); sd->backlog.poll = process_backlog;
net/core/skbuff.c +7 -8
@@ -7184,6 +7184,7 @@ static void kfree_skb_napi_cache(struct sk_buff *skb)*/ void skb_attempt_defer_free (struct sk_buff *skb) { +unsigned long defer_count;int cpu = skb->alloc_cpu;struct softnet_data *sd;unsigned int defer_max;
@@ -7201,17 +7202,15 @@ nodefer: kfree_skb_napi_cache(skb); sd = &per_cpu (softnet_data, cpu); defer_max =READ_ONCE (net_hotdata.sysctl_skb_defer_max); -if (atomic_read (&sd->defer_count) >= defer_max) + defer_count =atomic_long_inc_return (&sd->defer_count); + +if (defer_count >= defer_max) goto nodefer; -spin_lock_bh (&sd->defer_lock); -/* Send an IPI every time queue reaches half capacity. */ - kick = (atomic_inc_return (&sd->defer_count) -1 ) == (defer_max >>1 ); +llist_add (&skb->ll_node, &sd->defer_list); - skb->next = sd->defer_list; -/* Paired with READ_ONCE() in skb_defer_free_flush() */ -WRITE_ONCE (sd->defer_list, skb); -spin_unlock_bh (&sd->defer_lock); +/* Send an IPI every time queue reaches half capacity. */ + kick = (defer_count -1 ) == (defer_max >>1 );/* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU * if we are unlucky enough (this seems very unlikely).
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
PATCH v2 3/3 net: add NUMA awareness to skb_attempt_defer_free()
In reply to Eric DumazetInstead of sharing sd->defer_list & sd->defer_count with many cpus, add one pair for each NUMA node.
---
v2: remove skb_defer_nodes leftover in net/core/dev.c,
its final location is in net_hotdata (Kuniyuki feedback)
include/linux/netdevice.h | 4 ---- include/net/hotdata.h | 7 +++++++ net/core/dev.c | 35 +++++++++++++++++++++++------------ net/core/dev.h | 2 +- net/core/skbuff.c | 11 ++++++----- 5 files changed, 37 insertions(+), 22 deletions(-)
include/linux/netdevice.h +0 -4
@@ -3536,10 +3536,6 @@ struct softnet_data {struct numa_drop_counters drop_counters; -/* Another possibly contended cache line */ -struct llist_head defer_list____cacheline_aligned_in_smp ; -atomic_long_t defer_count; -int defer_ipi_scheduled____cacheline_aligned_in_smp ;call_single_data_t defer_csd; };
include/net/hotdata.h +7 -0
@@ -2,10 +2,16 @@ #ifndef _NET_HOTDATA_H #define _NET_HOTDATA_H +#include <linux/llist.h> #include <linux/types.h> #include <linux/netdevice.h> #include <net/protocol.h> +struct skb_defer_node { + struct llist_head defer_list; + atomic_long_t defer_count; +} ____cacheline_aligned_in_smp; + /* Read mostly data used in network fast paths. */ struct net_hotdata { #if IS_ENABLED(CONFIG_INET)
@@ -30,6 +36,7 @@ struct net_hotdata {struct rps_sock_flow_table __rcu *rps_sock_flow_table;u32 rps_cpu_mask;#endif +struct skb_defer_node __percpu *skb_defer_nodes;int gro_normal_batch;int netdev_budget;int netdev_budget_usecs;
net/core/dev.c +23 -12
@@ -5180,8 +5180,9 @@ static void napi_schedule_rps(struct softnet_data *sd)__napi_schedule_irqoff (&mysd->backlog); } -void kick_defer_list_purge (struct softnet_data *sd,unsigned int cpu) +void kick_defer_list_purge (unsigned int cpu) { +struct softnet_data *sd = &per_cpu (softnet_data, cpu);unsigned long flags;if (use_backlog_threads ()) {
@@ -6715,18 +6716,24 @@ bool napi_complete_done(struct napi_struct *n, int work_done) }EXPORT_SYMBOL (napi_complete_done); -static void skb_defer_free_flush (struct softnet_data *sd) +static void skb_defer_free_flush (void ) {struct llist_node *free_list;struct sk_buff *skb, *next; +struct skb_defer_node *sdn; +int node; -if (llist_empty (&sd->defer_list)) -return ; -atomic_long_set (&sd->defer_count,0 ); - free_list =llist_del_all (&sd->defer_list); +for_each_node (node) { + sdn =this_cpu_ptr (net_hotdata.skb_defer_nodes) + node; + +if (llist_empty (&sdn->defer_list)) +continue ; +atomic_long_set (&sdn->defer_count,0 ); + free_list =llist_del_all (&sdn->defer_list); -llist_for_each_entry_safe (skb, next, free_list, ll_node) { -napi_consume_skb (skb,1 ); +llist_for_each_entry_safe (skb, next, free_list, ll_node) { +napi_consume_skb (skb,1 ); + } } }
@@ -6854,7 +6861,7 @@ static void __napi_busy_loop(unsigned int napi_id,if (work >0 )__NET_ADD_STATS (dev_net (napi->dev),LINUX_MIB_BUSYPOLLRXPACKETS , work); -skb_defer_free_flush (this_cpu_ptr (&softnet_data)); +skb_defer_free_flush ();bpf_net_ctx_clear (bpf_net_ctx);local_bh_enable ();
@@ -7713,7 +7720,7 @@ static void napi_threaded_poll_loop(struct napi_struct *napi)local_irq_disable ();net_rps_action_and_irq_enable (sd); } -skb_defer_free_flush (sd); +skb_defer_free_flush ();bpf_net_ctx_clear (bpf_net_ctx);local_bh_enable ();
@@ -7755,7 +7762,7 @@ static __latent_entropy void net_rx_action(void)for (;;) {struct napi_struct *n; -skb_defer_free_flush (sd); +skb_defer_free_flush ();if (list_empty (&list)) {if (list_empty (&repoll)) {
@@ -12989,7 +12996,6 @@ static int __init net_dev_init(void) sd->cpu = i;#endif INIT_CSD (&sd->defer_csd, trigger_rx_softirq, sd); -init_llist_head (&sd->defer_list);gro_init (&sd->backlog.gro); sd->backlog.poll = process_backlog;
@@ -12999,6 +13005,11 @@ static int __init net_dev_init(void)if (net_page_pool_create (i)) goto out; } + net_hotdata.skb_defer_nodes = +__alloc_percpu (sizeof (struct skb_defer_node ) * nr_node_ids, + __alignof__(struct skb_defer_node )); +if (!net_hotdata.skb_defer_nodes) + goto out;if (use_backlog_threads ())smpboot_register_percpu_thread (&backlog_threads);
net/core/dev.h +1 -1
@@ -357,7 +357,7 @@ static inline void napi_assert_will_not_race(const struct napi_struct *napi)WARN_ON (READ_ONCE (napi->list_owner) !=-1 ); } -void kick_defer_list_purge (struct softnet_data *sd,unsigned int cpu); +void kick_defer_list_purge (unsigned int cpu);#define XMIT_RECURSION_LIMIT 8
net/core/skbuff.c +6 -5
@@ -7184,9 +7184,9 @@ static void kfree_skb_napi_cache(struct sk_buff *skb)*/ void skb_attempt_defer_free (struct sk_buff *skb) { +struct skb_defer_node *sdn;unsigned long defer_count;int cpu = skb->alloc_cpu; -struct softnet_data *sd;unsigned int defer_max;bool kick;
@@ -7200,14 +7200,15 @@ nodefer: kfree_skb_napi_cache(skb);DEBUG_NET_WARN_ON_ONCE (skb_dst (skb));DEBUG_NET_WARN_ON_ONCE (skb->destructor); - sd = &per_cpu (softnet_data, cpu); + sdn =per_cpu_ptr (net_hotdata.skb_defer_nodes, cpu) +numa_node_id (); + defer_max =READ_ONCE (net_hotdata.sysctl_skb_defer_max); - defer_count =atomic_long_inc_return (&sd->defer_count); + defer_count =atomic_long_inc_return (&sdn->defer_count);if (defer_count >= defer_max) goto nodefer; -llist_add (&skb->ll_node, &sd->defer_list); +llist_add (&skb->ll_node, &sdn->defer_list);/* Send an IPI every time queue reaches half capacity. */ kick = (defer_count -1 ) == (defer_max >>1 );
@@ -7216,7 +7217,7 @@ nodefer: kfree_skb_napi_cache(skb);* if we are unlucky enough (this seems very unlikely). */ if (unlikely (kick)) -kick_defer_list_purge (sd, cpu); +kick_defer_list_purge (cpu); } static void skb_splice_csum_page(struct sk_buff *skb, struct page *page,
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 11 lines · On Sun, 28 Sep 2025 08:49:31 +0000 you wrote:
Platforms with many cpus and relatively slow inter connect show a significant spinlock contention in
skb_attempt_defer_free().This series refactors this infrastructure to be NUMA aware, and lockless.
Tested on various platforms, including AMD Zen 2/3/4 and Intel Granite Rapids, showing significant cost reductions under network stress (more than 20 Mpps).
[...]
Here is the summary with links:
- [v2,net-next,1/3] net: make softnet_data.defer_count an atomic
https://git.kernel.org/netdev/net-next/c/9c94ae6bb0b2 - [v2,net-next,2/3] net: use llist for sd->defer_list https://git.kernel.org/netdev/net-next/c/844c9db7f7f5 - [v2,net-next,3/3] net: add NUMA awareness to skb_attempt_defer_free() https://git.kernel.org/netdev/net-next/c/5628f3fe3b16
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html