Skip to contentSkip to conversation

netfilter: call synchronize_net only once from nf_register_net_hooks

RFC PATCH In review Giuseppe Scrivano 2 participants 8 messages Nov 22, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 52cd2901a097..beeb0b36f429 100644
@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const
 	return 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, &reg[i]);
+		err = __nf_register_net_hook(net, &reg[i]);
 		if (err)
 			goto err;
 	}
+	synchronize_net();
 	return err;
 
 err:
Signature · 1 line
2.14.3
maintainer
#1 lore raw
Quoted 25 lines · Giuseppe Scrivano

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 +1 -1
index 52cd2901a097..beeb0b36f429 100644
@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const
 	return 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?

maintainer
#2 lore raw
In reply to Giuseppe Scrivano
Quoted 5 lines · Giuseppe Scrivano

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.

but this needs more work.

Quoted 10 lines
---
 net/netfilter/core.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
net/netfilter/core.c +0 -0
index 52cd2901a097..beeb0b36f429 100644

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, const
 	return 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 -0
 	return 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, &reg[i]);
+		err = __nf_register_net_hook(net, &reg[i]);

here, store the old 'p' values and then
free them after synchronize_net().

maintainer
#3 lore raw
Quoted 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_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.

but this needs more work.

Quoted 10 lines
---
 net/netfilter/core.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
net/netfilter/core.c +0 -0
index 52cd2901a097..beeb0b36f429 100644

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, const
 	return 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 -0
 	return 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.

#4 lore raw
In reply to Florian Westphal
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_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.

but this needs more work.

Quoted 13 lines
---
 net/netfilter/core.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
net/netfilter/core.c +0 -0
index 52cd2901a097..beeb0b36f429 100644
@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const
 	return 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

maintainer
#5 lore raw
Quoted 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_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.

but this needs more work.

Quoted 13 lines
---
 net/netfilter/core.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
net/netfilter/core.c +0 -0
index 52cd2901a097..beeb0b36f429 100644
@@ -252,7 +252,7 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const
 	return 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

#6 lore raw
Quoted 5 lines · Florian Westphal

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:

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
index c08fd706d9c7..663046776def 100644
@@ -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
index 1f25c914f6e9..6bcf90948810 100644
@@ -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);
 }
 
maintainer
#7 lore raw
Quoted 10 lines · Giuseppe Scrivano
Quoted 5 lines

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:

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
index c08fd706d9c7..663046776def 100644

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.