netfilter: Cover more per-CPU storage with local nested BH locking.
Subsystems (5)
Sebastian Andrzej Siewior
maintainercover netfilter: Cover more per-CPU storage with local nested BH locking.
I was looking at the build-time defined per-CPU variables in netfilter and added the needed local-BH-locks in order to be able to remove the current per-CPU lock in local_bh_disable() on PREMPT_RT.
NF wise nft_set_pipapo is missing but this requires some core changes so I need to postspone it for now.
This has been split out of the networking series which was sent earlier. Therefore the last patch (nf_dup_netdev) will likely clash with net-next due to changes in include/linux/netdevice_xmit.h (both add an entry).
Sebastian Andrzej Siewior (3):
netfilter: nf_dup{4, 6}: Move duplication check to task_struct
netfilter: nft_inner: Use nested-BH locking for nft_pcpu_tun_ctx
netfilter: nf_dup_netdev: Move the recursion counter struct
netdev_xmit
include/linux/netdevice_xmit.h | 3 +++ include/linux/netfilter.h | 11 ----------- include/linux/sched.h | 1 + net/ipv4/netfilter/ip_tables.c | 2 +- net/ipv4/netfilter/nf_dup_ipv4.c | 6 +++--- net/ipv6/netfilter/ip6_tables.c | 2 +- net/ipv6/netfilter/nf_dup_ipv6.c | 6 +++--- net/netfilter/core.c | 3 --- net/netfilter/nf_dup_netdev.c | 22 ++++++++++++++++++---- net/netfilter/nft_inner.c | 18 +++++++++++++++--- 10 files changed, 45 insertions(+), 29 deletions(-)
Signature · 1 line
2.49.0
Sebastian Andrzej Siewior
maintainerPATCH 1/3 netfilter: nf_dup{4, 6}: Move duplication check to task_struct
nf_skb_duplicated is a per-CPU variable and relies on disabled BH for its locking. Without per-CPU locking in local_bh_disable() on PREEMPT_RT this data structure requires explicit locking.
Due to the recursion involved, the simplest change is to make it a per-task variable.
Move the per-CPU variable nf_skb_duplicated to task_struct and name it in_nf_duplicate. Add it to the existing bitfield so it doesn't use additional memory.
--- include/linux/netfilter.h | 11 ----------- include/linux/sched.h | 1 + net/ipv4/netfilter/ip_tables.c | 2 +- net/ipv4/netfilter/nf_dup_ipv4.c | 6 +++--- net/ipv6/netfilter/ip6_tables.c | 2 +- net/ipv6/netfilter/nf_dup_ipv6.c | 6 +++--- net/netfilter/core.c | 3 --- 7 files changed, 9 insertions(+), 22 deletions(-)
include/linux/netfilter.h +0 -11
@@ -497,17 +497,6 @@ struct nf_defrag_hook {extern const struct nf_defrag_hook __rcu *nf_defrag_v4_hook;extern const struct nf_defrag_hook __rcu *nf_defrag_v6_hook; -/* -* nf_skb_duplicated - TEE target has sent a packet -* -* When a xtables target sends a packet, the OUTPUT and POSTROUTING -* hooks are traversed again, i.e. nft and xtables are invoked recursively. -* -* This is used by xtables TEE target to prevent the duplicated skb from -* being duplicated again. -*/ -DECLARE_PER_CPU (bool, nf_skb_duplicated); -/* * Contains bitmask of ctnetlink event subscribers, if any. * Can't be pernet due to NETLINK_LISTEN_ALL_NSID setsockopt flag.
include/linux/sched.h +1 -0
@@ -1044,6 +1044,7 @@ struct task_struct {/* delay due to memory thrashing */ unsigned in_thrashing:1;#endif + unsigned in_nf_duplicate:1;#ifdef CONFIG_PREEMPT_RT struct netdev_xmit net_xmit;#endif
net/ipv4/netfilter/ip_tables.c +1 -1
@@ -270,7 +270,7 @@ ipt_do_table(void *priv,* but it is no problem since absolute verdict is issued by these. */ if (static_key_false (&xt_tee_enabled)) - jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); + jumpstack += private->stacksize * current->in_nf_duplicate; e =get_entry (table_base, private->hook_entry[hook]);
net/ipv4/netfilter/nf_dup_ipv4.c +3 -3
@@ -54,7 +54,7 @@ void nf_dup_ipv4(struct net *net, struct sk_buff *skb, unsigned int hooknum,struct iphdr *iph;local_bh_disable (); -if (this_cpu_read (nf_skb_duplicated)) +if (current->in_nf_duplicate) goto out;/* * Copy the skb, and route the copy. Will later return %XT_CONTINUE for
@@ -86,9 +86,9 @@ void nf_dup_ipv4(struct net *net, struct sk_buff *skb, unsigned int hooknum, --iph->ttl;if (nf_dup_ipv4_route (net, skb, gw, oif)) { -__this_cpu_write (nf_skb_duplicated, true); + current->in_nf_duplicate = true;ip_local_out (net, skb->sk, skb); -__this_cpu_write (nf_skb_duplicated, false); + current->in_nf_duplicate = false; }else {kfree_skb (skb); }
net/ipv6/netfilter/ip6_tables.c +1 -1
@@ -292,7 +292,7 @@ ip6t_do_table(void *priv, struct sk_buff *skb,* but it is no problem since absolute verdict is issued by these. */ if (static_key_false (&xt_tee_enabled)) - jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); + jumpstack += private->stacksize * current->in_nf_duplicate; e =get_entry (table_base, private->hook_entry[hook]);
net/ipv6/netfilter/nf_dup_ipv6.c +3 -3
@@ -48,7 +48,7 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, const struct in6_addr *gw, int oif) {local_bh_disable (); -if (this_cpu_read (nf_skb_duplicated)) +if (current->in_nf_duplicate) goto out; skb =pskb_copy (skb,GFP_ATOMIC );if (skb ==NULL )
@@ -64,9 +64,9 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, --iph->hop_limit; }if (nf_dup_ipv6_route (net, skb, gw, oif)) { -__this_cpu_write (nf_skb_duplicated, true); + current->in_nf_duplicate = true;ip6_local_out (net, skb->sk, skb); -__this_cpu_write (nf_skb_duplicated, false); + current->in_nf_duplicate = false; }else {kfree_skb (skb); }
net/netfilter/core.c +0 -3
@@ -31,9 +31,6 @@const struct nf_ipv6_ops __rcu *nf_ipv6_ops__read_mostly ;EXPORT_SYMBOL_GPL (nf_ipv6_ops); -DEFINE_PER_CPU (bool, nf_skb_duplicated); -EXPORT_SYMBOL_GPL (nf_skb_duplicated); -#ifdef CONFIG_JUMP_LABEL struct static_key nf_hooks_needed[NFPROTO_NUMPROTO ][NF_MAX_HOOKS ];EXPORT_SYMBOL (nf_hooks_needed);
Signature · 1 line
2.49.0
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next v1 1/3] netfilter: nf_dup{4, 6}: Move duplication check to task_struct
Hi Sebastian,
On Mon, May 12, 2025 at 12:28:44PM +0200, Sebastian Andrzej Siewior wrote:
[...]
net/ipv6/netfilter/nf_dup_ipv6.c void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, · line 51
Quoted 10 lines
net/ipv6/netfilter/nf_dup_ipv6.c +1
-1@@ -48,7 +48,7 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, const struct in6_addr *gw, int oif) {local_bh_disable (); -if (this_cpu_read (nf_skb_duplicated)) +if (current->in_nf_duplicate)
Netfilter runs from the forwarding path too, where no current process is available.
Quoted 15 lines
net/ipv6/netfilter/nf_dup_ipv6.c +2
-2goto out; skb =pskb_copy (skb,GFP_ATOMIC );if (skb ==NULL )@@ -64,9 +64,9 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, --iph->hop_limit; }if (nf_dup_ipv6_route (net, skb, gw, oif)) { -__this_cpu_write (nf_skb_duplicated, true); + current->in_nf_duplicate = true;ip6_local_out (net, skb->sk, skb); -__this_cpu_write (nf_skb_duplicated, false); + current->in_nf_duplicate = false; }else {kfree_skb (skb); }
Sebastian Andrzej Siewior
maintainerRe: [PATCH nf-next v1 1/3] netfilter: nf_dup{4, 6}: Move duplication check to task_struct
Quoted 1 line · Pablo Neira Ayuso
Hi Sebastian,
Hi Pablo,
Quoted 15 lines · Sebastian Andrzej Siewior
[...]
net/ipv6/netfilter/nf_dup_ipv6.c void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, · line 51
Quoted 10 lines
net/ipv6/netfilter/nf_dup_ipv6.c +1
-1@@ -48,7 +48,7 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, const struct in6_addr *gw, int oif) {local_bh_disable (); -if (this_cpu_read (nf_skb_duplicated)) +if (current->in_nf_duplicate)Netfilter runs from the forwarding path too, where no current process is available.
If you refer to in-softirq with no task running then there is the idle task/ swapper which is pointed to by current in this case. There is one idle task for each CPU, they don't migrate.
Quoted 7 lines
Quoted 7 lines
net/ipv6/netfilter/nf_dup_ipv6.c +0
-0goto out; skb =pskb_copy (skb,GFP_ATOMIC );if (skb ==NULL )@@ -64,9 +64,9 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, --iph->hop_limit; }if (nf_dup_ipv6_route (net, skb, gw, oif)) {
Quoted 8 lines
net/ipv6/netfilter/nf_dup_ipv6.c void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, · line 72
Quoted 8 lines
net/ipv6/netfilter/nf_dup_ipv6.c +2
-2-__this_cpu_write (nf_skb_duplicated, true); + current->in_nf_duplicate = true;ip6_local_out (net, skb->sk, skb); -__this_cpu_write (nf_skb_duplicated, false); + current->in_nf_duplicate = false; }else {kfree_skb (skb); }
Sebastian
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next v1 1/3] netfilter: nf_dup{4, 6}: Move duplication check to task_struct
Quoted 20 lines · Sebastian Andrzej Siewior
Quoted 1 line · Pablo Neira Ayuso
Hi Sebastian,
Hi Pablo,
Quoted 15 lines · Sebastian Andrzej Siewior
[...]
net/ipv6/netfilter/nf_dup_ipv6.c void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, · line 51
Quoted 10 lines
net/ipv6/netfilter/nf_dup_ipv6.c +1
-1@@ -48,7 +48,7 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum, const struct in6_addr *gw, int oif) {local_bh_disable (); -if (this_cpu_read (nf_skb_duplicated)) +if (current->in_nf_duplicate)Netfilter runs from the forwarding path too, where no current process is available.
Quoted 3 lines
If you refer to in-softirq with no task running then there is the idle task/ swapper which is pointed to by current in this case. There is one idle task for each CPU, they don't migrate.
Thanks for explaining.
I am going to place this series in nf-next.
Sebastian Andrzej Siewior
maintainerPATCH 2/3 netfilter: nft_inner: Use nested-BH locking for nft_pcpu_tun_ctx
In reply to Sebastian Andrzej Siewiornft_pcpu_tun_ctx is a per-CPU variable and relies on disabled BH for its locking. Without per-CPU locking in local_bh_disable() on PREEMPT_RT this data structure requires explicit locking.
Make a struct with a nft_inner_tun_ctx member (original nft_pcpu_tun_ctx) and a local_lock_t and use local_lock_nested_bh() for locking. This change adds only lockdep coverage and does not alter the functional behaviour for !PREEMPT_RT.
--- net/netfilter/nft_inner.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
net/netfilter/nft_inner.c +15 -3
@@ -23,7 +23,14 @@#include <linux/ip.h> #include <linux/ipv6.h> -static DEFINE_PER_CPU(struct nft_inner_tun_ctx, nft_pcpu_tun_ctx); +struct nft_inner_tun_ctx_locked { +struct nft_inner_tun_ctx ctx; +local_lock_t bh_lock; +}; + +static DEFINE_PER_CPU(struct nft_inner_tun_ctx_locked, nft_pcpu_tun_ctx) = { + .bh_lock =INIT_LOCAL_LOCK (bh_lock), +};/* Same layout as nft_expr but it embeds the private expression data area. */ struct __nft_expr {
@@ -237,12 +244,15 @@ static bool nft_inner_restore_tun_ctx(const struct nft_pktinfo *pkt,struct nft_inner_tun_ctx *this_cpu_tun_ctx;local_bh_disable (); - this_cpu_tun_ctx =this_cpu_ptr (&nft_pcpu_tun_ctx); +local_lock_nested_bh (&nft_pcpu_tun_ctx.bh_lock); + this_cpu_tun_ctx =this_cpu_ptr (&nft_pcpu_tun_ctx.ctx);if (this_cpu_tun_ctx->cookie != (unsigned long )pkt->skb) {local_bh_enable (); +local_unlock_nested_bh (&nft_pcpu_tun_ctx.bh_lock);return false; } *tun_ctx = *this_cpu_tun_ctx; +local_unlock_nested_bh (&nft_pcpu_tun_ctx.bh_lock);local_bh_enable ();return true;
@@ -254,9 +264,11 @@ static void nft_inner_save_tun_ctx(const struct nft_pktinfo *pkt,struct nft_inner_tun_ctx *this_cpu_tun_ctx;local_bh_disable (); - this_cpu_tun_ctx =this_cpu_ptr (&nft_pcpu_tun_ctx); +local_lock_nested_bh (&nft_pcpu_tun_ctx.bh_lock); + this_cpu_tun_ctx =this_cpu_ptr (&nft_pcpu_tun_ctx.ctx);if (this_cpu_tun_ctx->cookie != tun_ctx->cookie) *this_cpu_tun_ctx = *tun_ctx; +local_unlock_nested_bh (&nft_pcpu_tun_ctx.bh_lock);local_bh_enable (); }
Signature · 1 line
2.49.0
Sebastian Andrzej Siewior
maintainerPATCH 3/3 netfilter: nf_dup_netdev: Move the recursion counter struct netdev_xmit
In reply to Sebastian Andrzej Siewiornf_dup_skb_recursion is a per-CPU variable and relies on disabled BH for its locking. Without per-CPU locking in local_bh_disable() on PREEMPT_RT this data structure requires explicit locking.
Move nf_dup_skb_recursion to struct netdev_xmit, provide wrappers.
--- include/linux/netdevice_xmit.h | 3 +++ net/netfilter/nf_dup_netdev.c | 22 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-)
include/linux/netdevice_xmit.h +3 -0
@@ -8,6 +8,9 @@ struct netdev_xmit {#ifdef CONFIG_NET_EGRESS u8 skip_txqueue;#endif +#if IS_ENABLED (CONFIG_NF_DUP_NETDEV ) +u8 nf_dup_skb_recursion; +#endif };#endif
net/netfilter/nf_dup_netdev.c +18 -4
@@ -15,12 +15,26 @@#define NF_RECURSION_LIMIT 2 -static DEFINE_PER_CPU(u8, nf_dup_skb_recursion); +#ifndef CONFIG_PREEMPT_RT +static u8 *nf_get_nf_dup_skb_recursion (void ) +{ +return this_cpu_ptr (&softnet_data.xmit.nf_dup_skb_recursion); +} +#else + +static u8 *nf_get_nf_dup_skb_recursion (void ) +{ +return ¤t->net_xmit.nf_dup_skb_recursion; +} + +#endif static void nf_do_netdev_egress (struct sk_buff *skb,struct net_device *dev,enum nf_dev_hooks hook) { -if (__this_cpu_read (nf_dup_skb_recursion) >NF_RECURSION_LIMIT ) +u8 *nf_dup_skb_recursion =nf_get_nf_dup_skb_recursion (); + +if (*nf_dup_skb_recursion >NF_RECURSION_LIMIT ) goto err;if (hook ==NF_NETDEV_INGRESS &&skb_mac_header_was_set (skb)) {
@@ -32,9 +46,9 @@ static void nf_do_netdev_egress(struct sk_buff *skb, struct net_device *dev, skb->dev = dev;skb_clear_tstamp (skb); -__this_cpu_inc (nf_dup_skb_recursion); + (*nf_dup_skb_recursion)++;dev_queue_xmit (skb); -__this_cpu_dec (nf_dup_skb_recursion); + (*nf_dup_skb_recursion)--;return ; err:kfree_skb (skb);
Signature · 1 line
2.49.0
Quoted 9 lines · Sebastian Andrzej Siewior
I was looking at the build-time defined per-CPU variables in netfilter and added the needed local-BH-locks in order to be able to remove the current per-CPU lock in
local_bh_disable()onPREMPT_RT.
NF wisenft_set_pipapois missing but this requires some core changes so I need to postspone it for now.This has been split out of the networking series which was sent earlier. Therefore the last patch (
nf_dup_netdev) will likely clash with net-next due to changes ininclude/linux/netdevice_xmit.h(both add an entry).
Applied to nf-next, thanks.