netfilter: call synchronize_net only once from nf_register_net_hooks
Subsystems (2)
RFC PATCH 1/1 netfilter: call synchronize_net only once from nf_register_net_hooks
SELinux, if enabled, registers for each new network namespace 6 netfilter hooks. Avoid to use synchronize_net for each new hook, but do it once after all the hooks are added. The net benefit on an SMP machine with two cores is that creating a new network namespace takes -40% of the original time.
--- net/netfilter/core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
net/netfilter/core.c +12 -3
@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, constreturn NULL ; } -int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +static int __nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) {struct nf_hook_entries *p, *new_hooks;struct nf_hook_entries __rcu **pp;
@@ -291,11 +291,19 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)#ifdef HAVE_JUMP_LABEL static_key_slow_inc (&nf_hooks_needed[reg->pf][reg->hooknum]);#endif -synchronize_net ();BUG_ON (p == new_hooks);kvfree (p);return 0 ; } + +int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +{ +int ret =__nf_register_net_hook (net, reg); +if (ret <0 ) +return ret; +synchronize_net (); +return 0 ; +}EXPORT_SYMBOL (nf_register_net_hook);/*
@@ -379,10 +387,11 @@ int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,int err =0 ;for (i =0 ; i < n; i++) { - err =nf_register_net_hook (net, ®[i]); + err =__nf_register_net_hook (net, ®[i]);if (err) goto err; } +synchronize_net ();return err; err:
Signature · 1 line
2.14.3
Florian Westphal
maintainerQuoted 25 lines · Giuseppe Scrivano
SELinux, if enabled, registers for each new network namespace 6 netfilter hooks. Avoid to use
synchronize_netfor each new hook, but do it once after all the hooks are added. The net benefit on an SMP machine with two cores is that creating a new network namespace takes -40% of the original time.Signed-off-by Giuseppe Scrivano <[email protected]>--- net/netfilter/core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)net/netfilter/core.c +1
-1@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, constreturn NULL ; } -int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +static int __nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) {struct nf_hook_entries *p, *new_hooks;struct nf_hook_entries __rcu **pp;
net/netfilter/core.c int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) · line 295
Quoted 6 lines
net/netfilter/core.c +0
-1@@ -291,11 +291,19 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)#ifdef HAVE_JUMP_LABEL static_key_slow_inc (&nf_hooks_needed[reg->pf][reg->hooknum]);#endif -synchronize_net ();BUG_ON (p == new_hooks);kvfree (p);
This adds use after free?
Florian Westphal
maintainerQuoted 5 lines · Giuseppe Scrivano
SELinux, if enabled, registers for each new network namespace 6 netfilter hooks. Avoid to use
synchronize_netfor each new hook, but do it once after all the hooks are added. The net benefit on an SMP machine with two cores is that creating a new network namespace takes -40% of the original time.
but this needs more work.
Quoted 10 lines
Signed-off-by Giuseppe Scrivano <[email protected]>--- net/netfilter/core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)net/netfilter/core.c +0
-0
net/netfilter/core.c static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const · line 255
Quoted 5 lines
net/netfilter/core.c +1
-1@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, constreturn NULL ; } -int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +static int __nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg)
Change this to return struct nf_hook_entries *
net/netfilter/core.c int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) · line 295
Quoted 10 lines
net/netfilter/core.c +0
-1{struct nf_hook_entries *p, *new_hooks;struct nf_hook_entries __rcu **pp;@@ -291,11 +291,19 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)#ifdef HAVE_JUMP_LABEL static_key_slow_inc (&nf_hooks_needed[reg->pf][reg->hooknum]);#endif -synchronize_net ();BUG_ON (p == new_hooks);kvfree (p);
remove kvfree()
net/netfilter/core.c int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) · line 296
Quoted 1 line
net/netfilter/core.c +0
-0return 0 ;
return p;
net/netfilter/core.c int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) · line 304
Quoted 6 lines
net/netfilter/core.c +6
-0+int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +{ +int ret =__nf_register_net_hook (net, reg); +if (ret <0 ) +return ret; +synchronize_net ();
then free p here.
net/netfilter/core.c int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, · line 390
Quoted 11 lines
net/netfilter/core.c +3
-1+return 0 ; +}EXPORT_SYMBOL (nf_register_net_hook);/* @@ -379,10 +387,11 @@ int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,int err =0 ;for (i =0 ; i < n; i++) { - err =nf_register_net_hook (net, ®[i]); + err =__nf_register_net_hook (net, ®[i]);
here, store the old 'p' values and then
free them after synchronize_net().
Florian Westphal
maintainerQuoted 44 lines · Florian Westphal
Quoted 5 lines · Giuseppe Scrivano
SELinux, if enabled, registers for each new network namespace 6 netfilter hooks. Avoid to use
synchronize_netfor each new hook, but do it once after all the hooks are added. The net benefit on an SMP machine with two cores is that creating a new network namespace takes -40% of the original time.but this needs more work.
Quoted 10 lines
Signed-off-by Giuseppe Scrivano <[email protected]>--- net/netfilter/core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)net/netfilter/core.c +0
-0net/netfilter/core.c static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const · line 255
Quoted 5 lines
net/netfilter/core.c +1
-1@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, constreturn NULL ; } -int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +static int __nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg)Change this to return struct
nf_hook_entries*net/netfilter/core.c int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) · line 295
Quoted 10 lines
net/netfilter/core.c +0
-1{struct nf_hook_entries *p, *new_hooks;struct nf_hook_entries __rcu **pp;@@ -291,11 +291,19 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)#ifdef HAVE_JUMP_LABEL static_key_slow_inc (&nf_hooks_needed[reg->pf][reg->hooknum]);#endif -synchronize_net ();BUG_ON (p == new_hooks);kvfree (p);remove
kvfree()net/netfilter/core.c int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) · line 296
Quoted 1 line
net/netfilter/core.c +0
-0return 0 ;return p;
Quoted 8 lines
net/netfilter/core.c int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) · line 304
Quoted 6 lines
net/netfilter/core.c +6
-0+int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +{ +int ret =__nf_register_net_hook (net, reg); +if (ret <0 ) +return ret; +synchronize_net ();then free p here.
Yet another alternative is to place an rcu_head at the end of struct nf_hook_entries.
This is a bit tricky because the location is not fixed due to dynamically sized array members.
But if you do this you can use call_rcu to do the freeing which removes need for all but the nfqueue related synchronize_net() calls.
Quoted 22 lines · Florian Westphal
Quoted 5 lines · Giuseppe Scrivano
SELinux, if enabled, registers for each new network namespace 6 netfilter hooks. Avoid to use
synchronize_netfor each new hook, but do it once after all the hooks are added. The net benefit on an SMP machine with two cores is that creating a new network namespace takes -40% of the original time.but this needs more work.
Quoted 13 lines
Signed-off-by Giuseppe Scrivano <[email protected]>--- net/netfilter/core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)net/netfilter/core.c +0
-0@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, constreturn NULL ; }
Quoted 4 lines
net/netfilter/core.c static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const · line 255
Quoted 2 lines
net/netfilter/core.c +1
-1-int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +static int __nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg)Change this to return struct
nf_hook_entries*
thanks for the quick review. Are you fine if I change it to:
static int __nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg, struct nf_hook_entries **old)
so that it is still possible to return other errors as the current code does?
Giuseppe
Florian Westphal
maintainerQuoted 24 lines · Giuseppe Scrivano
Quoted 22 lines · Florian Westphal
Quoted 5 lines · Giuseppe Scrivano
SELinux, if enabled, registers for each new network namespace 6 netfilter hooks. Avoid to use
synchronize_netfor each new hook, but do it once after all the hooks are added. The net benefit on an SMP machine with two cores is that creating a new network namespace takes -40% of the original time.but this needs more work.
Quoted 13 lines
Signed-off-by Giuseppe Scrivano <[email protected]>--- net/netfilter/core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)net/netfilter/core.c +0
-0@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, constreturn NULL ; }
Quoted 8 lines
Quoted 4 lines
net/netfilter/core.c static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const · line 255
Quoted 2 lines
net/netfilter/core.c +1
-1-int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +static int __nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg)Change this to return struct
nf_hook_entries*thanks for the quick review. Are you fine if I change it to:
static int
__nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg, struct nf_hook_entries **old)
You can use ERR_PTR/PTR_ERR instead.
However, I suggest you try to go with call_rcu to get rid of all of the synchronize_net() calls. I don't even see why its still needed for nfqueue case provided we invoke the nfqueue drop handler first.
PoC example, untested:
include/linux/netfilter.h +9 -1
@@ -77,6 +77,11 @@ struct nf_hook_entry {void *priv; }; +struct nf_hook_entries_rcu_head { +struct rcu_head head; +void *allocation; +}; +struct nf_hook_entries {u16 num_hook_entries;/* padding */
@@ -87,10 +92,13 @@ struct nf_hook_entries {* This is not part of struct nf_hook_entry since its only * needed in slow path (hook register/unregister). * -* const struct nf_hook_ops *orig_ops[] +* const struct nf_hook_ops *orig_ops[]; +* struct nf_hook_entries_rcu_head; */ }; +void nf_hook_entries_free_rcu (struct nf_hook_entries *e); +static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops (const struct nf_hook_entries *e) {unsigned int n = e->num_hook_entries;
net/netfilter/core.c +48 -68
162 lines not on this page · raw message
Quoted 5 lines · Florian Westphal
However, I suggest you try to go with
call_rcuto get rid of all of thesynchronize_net()calls. I don't even see why its still needed for nfqueue case provided we invoke the nfqueue drop handler first.PoC example, untested:
thanks, the patch seems to work for me, at least the problem I was seeing before is now fixed.
Is there any reason for defining "struct nf_hook_entries_rcu_head"?
I've used this fixup patch and it works as well. As someone new to the code, I find it a bit easier to follow:
include/linux/netfilter.h +1 -5
@@ -77,12 +77,8 @@ struct nf_hook_entry {void *priv; }; -struct nf_hook_entries_rcu_head { -struct rcu_head head; -void *allocation; -}; -struct nf_hook_entries { +struct rcu_head head;u16 num_hook_entries;/* padding */ struct nf_hook_entry hooks[];
net/netfilter/core.c +5 -30
@@ -69,59 +69,34 @@ static DEFINE_MUTEX(nf_hook_mutex);#define nf_entry_dereference (e) \ rcu_dereference_protected(e, lockdep_is_held(&nf_hook_mutex)) -static struct nf_hook_entries_rcu_head * -nf_hook_entries_rcu_head_get (struct nf_hook_entries *e) -{ -unsigned int num = e->num_hook_entries; -struct nf_hook_entries_rcu_head *head; -static struct nf_hook_ops **ops; - - ops =nf_hook_entries_get_hook_ops (e); - head = (void *)&ops[num]; - -return head; -} -static struct nf_hook_entries *allocate_hook_entries_size (u16 num) {struct nf_hook_entries *e;size_t alloc =sizeof (*e) +sizeof (struct nf_hook_entry ) * num + -sizeof (struct nf_hook_ops *) * num + -sizeof (struct nf_hook_entries_rcu_head ); +sizeof (struct nf_hook_ops *) * num;if (num ==0 )return NULL ; e =kvzalloc (alloc,GFP_KERNEL );if (e) { -struct nf_hook_entries_rcu_head *head; - e->num_hook_entries = num; - - head =nf_hook_entries_rcu_head_get (e); - head->allocation = e; }return e; }void __nf_hook_entries_free (struct rcu_head *h) { -struct nf_hook_entries_rcu_head *head; +struct nf_hook_entries *entries; - head = container_of(h, struct nf_hook_entries_rcu_head, head); -kvfree (head->allocation); + entries = container_of(h, struct nf_hook_entries, head); +kvfree (entries); }void nf_hook_entries_free_rcu (struct nf_hook_entries *e) { -struct nf_hook_entries_rcu_head *head; - - head =nf_hook_entries_rcu_head_get (e); -if (WARN_ON (e != head->allocation)) -return ; - -call_rcu (&head->head, __nf_hook_entries_free); +call_rcu (&e->head, __nf_hook_entries_free); }
Florian Westphal
maintainerQuoted 10 lines · Giuseppe Scrivano
Quoted 5 lines
However, I suggest you try to go with
call_rcuto get rid of all of thesynchronize_net()calls. I don't even see why its still needed for nfqueue case provided we invoke the nfqueue drop handler first.PoC example, untested:
thanks, the patch seems to work for me, at least the problem I was seeing before is now fixed.
Is there any reason for defining "struct
nf_hook_entries_rcu_head"?
Yes, idea is to only place elements we need in packet path at the beginning of structure.
Quoted 9 lines
I've used this fixup patch and it works as well. As someone new to the code, I find it a bit easier to follow:
include/linux/netfilter.h +0
-0
include/linux/netfilter.h struct nf_hook_entry { · line 81
Quoted 10 lines
include/linux/netfilter.h +1
-5@@ -77,12 +77,8 @@ struct nf_hook_entry {void *priv; }; -struct nf_hook_entries_rcu_head { -struct rcu_head head; -void *allocation; -}; -struct nf_hook_entries { +struct rcu_head head;
Right, this works too.
I wanted to keep the rcu head out of the fastpath part as we only need it during destruction.