Skip to contentSkip to conversation

net: Random cleanup for netns initialisation.

PATCH Superseded Kuniyuki Iwashima 2 participants 13 messages Jul 29, 2024 – Jul 31, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: Random cleanup for netns initialisation.

patch 1 & 2 suppress unwanted memory allocation for net->gen->ptr[].

patch 3 ~ 6 move part of netns initialisation to prenet_init() that do not reqruire pernet_ops_rwsem.

Kuniyuki Iwashima (6):
l2tp: Don't assign net->gen->ptr[] for pppol2tp_net_ops.
net: Don't register pernet_operations if only one of id or size is
specified.
net: Initialise net->passive once in preinit_net().
net: Call preinit_net() without pernet_ops_rwsem.
net: Slim down setup_net().
net: Initialise net.core sysctl defaults in preinit_net().

 include/net/net_namespace.h |  4 +-
 net/core/net_namespace.c    | 84 ++++++++++++++++---------------------
 net/l2tp/l2tp_ppp.c         |  3 --
 3 files changed, 39 insertions(+), 52 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/6 l2tp: Don't assign net->gen->ptr[] for pppol2tp_net_ops.

Commit fd558d186df2 ("l2tp: Split pppol2tp patch into separate l2tp and ppp parts") converted net->gen->ptr[pppol2tp_net_id] in l2tp_ppp.c to net->gen->ptr[l2tp_net_id] in l2tp_core.c.

Now the leftover wastes one entry of net->gen->ptr[] in each netns.

Let's avoid the unwanted allocation.

Fixes fd558d186df2 ("l2tp: Split pppol2tp patch into separate l2tp and ppp parts")

---
CC: James Chapman <[email protected]>

---
 net/l2tp/l2tp_ppp.c | 3 ---
 1 file changed, 3 deletions(-)
net/l2tp/l2tp_ppp.c +0 -3
index 3596290047b2..246089b17910 100644
@@ -1406,8 +1406,6 @@ static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
  *****************************************************************************/
 
-static unsigned int pppol2tp_net_id;
-
 #ifdef CONFIG_PROC_FS
 
 struct pppol2tp_seq_data {
@@ -1641,7 +1639,6 @@ static __net_exit void pppol2tp_exit_net(struct net *net)
 static struct pernet_operations pppol2tp_net_ops = {
 	.init = pppol2tp_init_net,
 	.exit = pppol2tp_exit_net,
-	.id   = &pppol2tp_net_id,
 };
 
 /*****************************************************************************
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [PATCH v1 net-next 1/6] l2tp: Don't assign net->gen->ptr[] for pppol2tp_net_ops.

Quoted 1 line · On Mon, 29 Jul 2024 14:07:56 -0700 Kuniyuki Iwashima wrote:
Fixes fd558d186df2 ("l2tp: Split pppol2tp patch into separate l2tp and ppp parts")

pourquoi the Fixes tag?

#3 lore raw

Re: [PATCH v1 net-next 1/6] l2tp: Don't assign net->gen->ptr[] for pppol2tp_net_ops.

From: Jakub Kicinski <[email protected]>
Date: Tue, 30 Jul 2024 18:52:19 -0700

Quoted 4 lines · On Mon, 29 Jul 2024 14:07:56 -0700 Kuniyuki Iwashima wrote:
Quoted 1 line
Fixes fd558d186df2 ("l2tp: Split pppol2tp patch into separate l2tp and ppp parts")

pourquoi the Fixes tag?

Ah, forgot to remove it.
Will remove it in v2.

Merci!

P.S. TIL pourquoi :)

#4 lore raw

PATCH 2/6 net: Don't register pernet_operations if only one of id or size is specified.

In reply to Kuniyuki Iwashima

We can allocate per-netns memory for struct pernet_operations by specifying id and size.

register_pernet_operations() assigns an id to pernet_operations and later ops_init() allocates the specified size of memory as net->gen->ptr[id].

If id is missing, no memory is allocated. If size is not specified, pernet_operations just wastes an entry of net->gen->ptr[] for every netns.

net_generic is useful only when both id and size are specified, so let's ensure that.

While we are at it, we add const to both fields.

---
 include/net/net_namespace.h |  4 ++--
 net/core/net_namespace.c    | 12 ++++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)
include/net/net_namespace.h +2 -2
index 20c34bd7a077..e67b483cc8bb 100644
@@ -451,8 +451,8 @@ struct pernet_operations {
 	/* Following method is called with RTNL held. */
 	void (*exit_batch_rtnl)(struct list_head *net_exit_list,
 				struct list_head *dev_kill_list);
-	unsigned int *id;
-	size_t size;
+	unsigned int * const id;
+	const size_t size;
 };
 
 /*
net/core/net_namespace.c +8 -4
index 6a823ba906c6..1d164ae097a5 100644
@@ -125,7 +125,7 @@ static int ops_init(const struct pernet_operations *ops, struct net *net)
 	int err = -ENOMEM;
 	void *data = NULL;
 
-	if (ops->id && ops->size) {
+	if (ops->id) {
 		data = kzalloc(ops->size, GFP_KERNEL);
 		if (!data)
 			goto out;
@@ -140,7 +140,7 @@ static int ops_init(const struct pernet_operations *ops, struct net *net)
 	if (!err)
 		return 0;
 
-	if (ops->id && ops->size) {
+	if (ops->id) {
 		ng = rcu_dereference_protected(net->gen,
 					       lockdep_is_held(&pernet_ops_rwsem));
 		ng->ptr[*ops->id] = NULL;
@@ -182,7 +182,8 @@ static void ops_free_list(const struct pernet_operations *ops,
 			  struct list_head *net_exit_list)
 {
 	struct net *net;
-	if (ops->size && ops->id) {
+
+	if (ops->id) {
 		list_for_each_entry(net, net_exit_list, exit_list)
 			kfree(net_generic(net, *ops->id));
 	}
@@ -1244,7 +1245,7 @@ static int __register_pernet_operations(struct list_head *list,
 	LIST_HEAD(net_exit_list);
 
 	list_add_tail(&ops->list, list);
-	if (ops->init || (ops->id && ops->size)) {
+	if (ops->init || ops->id) {
 		/* We held write locked pernet_ops_rwsem, and parallel
 		 * setup_net() and cleanup_net() are not possible.
 		 */
@@ -1310,6 +1311,9 @@ static int register_pernet_operations(struct list_head *list,
 {
 	int error;
 
+	if (WARN_ON((ops->id && !ops->size) || (!ops->id && ops->size)))
+		return -EINVAL;
+
 	if (ops->id) {
 		error = ida_alloc_min(&net_generic_ids, MIN_PERNET_OPS_ID,
 				GFP_KERNEL);
Signature · 1 line
2.30.2
maintainer
#5 lore raw

Re: [PATCH v1 net-next 2/6] net: Don't register pernet_operations if only one of id or size is specified.

net/core/net_namespace.c static int register_pernet_operations(struct list_head *list, · line 1314

Quoted 1 line · On Mon, 29 Jul 2024 14:07:57 -0700 Kuniyuki Iwashima wrote:
net/core/net_namespace.c +1 -0
+	if (WARN_ON((ops->id && !ops->size) || (!ops->id && ops->size)))

I'd write as:

	if (WARN_ON(!!ops->id != !!ops->size))

or

	if (WARN_ON(!!ops->id ^ !!ops->size))

but not 100% sure if it's idiomatic or just my preference..

#6 lore raw

Re: [PATCH v1 net-next 2/6] net: Don't register pernet_operations if only one of id or size is specified.

From: Jakub Kicinski <[email protected]>
Date: Tue, 30 Jul 2024 18:54:12 -0700

Quoted 5 lines · On Mon, 29 Jul 2024 14:07:57 -0700 Kuniyuki Iwashima wrote:

net/core/net_namespace.c static int register_pernet_operations(struct list_head *list, · line 1314

Quoted 1 line
net/core/net_namespace.c +1 -0
+	if (WARN_ON((ops->id && !ops->size) || (!ops->id && ops->size)))

I'd write as:

Quoted 7 lines
	if (WARN_ON(!!ops->id != !!ops->size))

or

	if (WARN_ON(!!ops->id ^ !!ops->size))

but not 100% sure if it's idiomatic or just my preference..

Actually I wrote the latter first so will use it in v2. Just not confident about which was easier to read.

Thanks!

#7 lore raw

PATCH 3/6 net: Initialise net->passive once in preinit_net().

In reply to Kuniyuki Iwashima

When initialising the root netns, we set net->passive in setup_net().

However, we do it twice for non-root netns in copy_net_ns() and setup_net().

This is because we could bypass setup_net() in copy_net_ns() if down_read_killable() fails.

preinit_net() is a better place to put such an operation.

Let's initialise net->passive in preinit_net().

---
 net/core/net_namespace.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/core/net_namespace.c +1 -2
index 1d164ae097a5..e4e99e7ba9f8 100644
@@ -312,6 +312,7 @@ EXPORT_SYMBOL_GPL(get_net_ns_by_id);
 /* init code that must occur even if setup_net() is not called. */
 static __net_init void preinit_net(struct net *net)
 {
+	refcount_set(&net->passive, 1);
 	ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net notrefcnt");
 }
 
@@ -329,7 +330,6 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
 	refcount_set(&net->ns.count, 1);
 	ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt");
 
-	refcount_set(&net->passive, 1);
 	get_random_bytes(&net->hash_mix, sizeof(u32));
 	preempt_disable();
 	net->net_cookie = gen_cookie_next(&net_cookie);
@@ -498,7 +498,6 @@ struct net *copy_net_ns(unsigned long flags,
 	}
 
 	preinit_net(net);
-	refcount_set(&net->passive, 1);
 	net->ucounts = ucounts;
 	get_user_ns(user_ns);
 
Signature · 1 line
2.30.2
#8 lore raw

PATCH 4/6 net: Call preinit_net() without pernet_ops_rwsem.

In reply to Kuniyuki Iwashima

When initialising the root netns, we call preinit_net() under pernet_ops_rwsem.

However, the operations in preinit_net() do not require pernet_ops_rwsem.

Also, we don't hold it for preinit_net() when initialising non-root netns.

To be consistent, let's call preinit_net() without pernet_ops_rwsem in net_ns_init().

---
 net/core/net_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/net_namespace.c +2 -1
index e4e99e7ba9f8..f1b6cea7a9b6 100644
@@ -1199,8 +1199,9 @@ void __init net_ns_init(void)
 #ifdef CONFIG_KEYS
 	init_net.key_domain = &init_net_key_domain;
 #endif
-	down_write(&pernet_ops_rwsem);
 	preinit_net(&init_net);
+
+	down_write(&pernet_ops_rwsem);
 	if (setup_net(&init_net, &init_user_ns))
 		panic("Could not setup the initial network namespace");
 
Signature · 1 line
2.30.2
#9 lore raw

PATCH 5/6 net: Slim down setup_net().

In reply to Kuniyuki Iwashima

Most initialisations in setup_net() do not require pernet_ops_rwsem and can be moved to preinit_net().

---
 net/core/net_namespace.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
net/core/net_namespace.c +16 -15
index f1b6cea7a9b6..7498f2cebbfe 100644
@@ -310,16 +310,26 @@ struct net *get_net_ns_by_id(const struct net *net, int id)
 EXPORT_SYMBOL_GPL(get_net_ns_by_id);
 
 /* init code that must occur even if setup_net() is not called. */
-static __net_init void preinit_net(struct net *net)
+static __net_init void preinit_net(struct net *net, struct user_namespace *user_ns)
 {
 	refcount_set(&net->passive, 1);
+	refcount_set(&net->ns.count, 1);
+	ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt");
 	ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net notrefcnt");
+
+	get_random_bytes(&net->hash_mix, sizeof(u32));
+	net->dev_base_seq = 1;
+	net->user_ns = user_ns;
+
+	idr_init(&net->netns_ids);
+	spin_lock_init(&net->nsid_lock);
+	mutex_init(&net->ipv4.ra_mutex);
 }
 
 /*
  * setup_net runs the initializers for the network namespace object.
  */
-static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
+static __net_init int setup_net(struct net *net)
 {
 	/* Must be called with pernet_ops_rwsem held */
 	const struct pernet_operations *ops, *saved_ops;
@@ -327,18 +337,9 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
 	LIST_HEAD(dev_kill_list);
 	int error = 0;
 
-	refcount_set(&net->ns.count, 1);
-	ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt");
-
-	get_random_bytes(&net->hash_mix, sizeof(u32));
 	preempt_disable();
 	net->net_cookie = gen_cookie_next(&net_cookie);
 	preempt_enable();
-	net->dev_base_seq = 1;
-	net->user_ns = user_ns;
-	idr_init(&net->netns_ids);
-	spin_lock_init(&net->nsid_lock);
-	mutex_init(&net->ipv4.ra_mutex);
 
 	list_for_each_entry(ops, &pernet_list, list) {
 		error = ops_init(ops, net);
@@ -497,7 +498,7 @@ struct net *copy_net_ns(unsigned long flags,
 		goto dec_ucounts;
 	}
 
-	preinit_net(net);
+	preinit_net(net, user_ns);
 	net->ucounts = ucounts;
 	get_user_ns(user_ns);
 
@@ -505,7 +506,7 @@ struct net *copy_net_ns(unsigned long flags,
 	if (rv < 0)
 		goto put_userns;
 
-	rv = setup_net(net, user_ns);
+	rv = setup_net(net);
 
 	up_read(&pernet_ops_rwsem);
 
@@ -1199,10 +1200,10 @@ void __init net_ns_init(void)
 #ifdef CONFIG_KEYS
 	init_net.key_domain = &init_net_key_domain;
 #endif
-	preinit_net(&init_net);
+	preinit_net(&init_net, &init_user_ns);
 
 	down_write(&pernet_ops_rwsem);
-	if (setup_net(&init_net, &init_user_ns))
+	if (setup_net(&init_net))
 		panic("Could not setup the initial network namespace");
 
 	init_net_initialized = true;
Signature · 1 line
2.30.2
#10 lore raw

PATCH 6/6 net: Initialise net.core sysctl defaults in preinit_net().

In reply to Kuniyuki Iwashima

Commit 7c3f1875c66f ("net: move somaxconn init from sysctl code") introduced net_defaults_ops to make sure that net.core sysctl knobs are always initialised even if CONFIG_SYSCTL is disabled.

Such operations better fit preinit_net() added for a similar purpose by commit 6e77a5a4af05 ("net: initialize net->notrefcnt_tracker earlier").

Let's initialise the sysctl defaults in preinit_net().

---
 net/core/net_namespace.c | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)
net/core/net_namespace.c +11 -26
index 7498f2cebbfe..a96a3be77f12 100644
@@ -309,6 +309,16 @@ struct net *get_net_ns_by_id(const struct net *net, int id)
 }
 EXPORT_SYMBOL_GPL(get_net_ns_by_id);
 
+static __net_init void preinit_net_sysctl(struct net *net)
+{
+	net->core.sysctl_somaxconn = SOMAXCONN;
+	/* Limits per socket sk_omem_alloc usage.
+	 * TCP zerocopy regular usage needs 128 KB.
+	 */
+	net->core.sysctl_optmem_max = 128 * 1024;
+	net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
+}
+
 /* init code that must occur even if setup_net() is not called. */
 static __net_init void preinit_net(struct net *net, struct user_namespace *user_ns)
 {
@@ -324,6 +334,7 @@ static __net_init void preinit_net(struct net *net, struct user_namespace *user_
 	idr_init(&net->netns_ids);
 	spin_lock_init(&net->nsid_lock);
 	mutex_init(&net->ipv4.ra_mutex);
+	preinit_net_sysctl(net);
 }
 
 /*
@@ -384,32 +395,6 @@ static __net_init int setup_net(struct net *net)
 	goto out;
 }
 
-static int __net_init net_defaults_init_net(struct net *net)
-{
-	net->core.sysctl_somaxconn = SOMAXCONN;
-	/* Limits per socket sk_omem_alloc usage.
-	 * TCP zerocopy regular usage needs 128 KB.
-	 */
-	net->core.sysctl_optmem_max = 128 * 1024;
-	net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
-
-	return 0;
-}
-
-static struct pernet_operations net_defaults_ops = {
-	.init = net_defaults_init_net,
-};
-
-static __init int net_defaults_init(void)
-{
-	if (register_pernet_subsys(&net_defaults_ops))
-		panic("Cannot initialize net default settings");
-
-	return 0;
-}
-
-core_initcall(net_defaults_init);
-
 #ifdef CONFIG_NET_NS
 static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
 {
Signature · 1 line
2.30.2
#12 lore raw

From: Jakub Kicinski <[email protected]>
Date: Tue, 30 Jul 2024 18:52:34 -0700

Quoted 4 lines · On Mon, 29 Jul 2024 14:07:55 -0700 Kuniyuki Iwashima wrote:
Quoted 1 line

do not reqruire pernet_ops_rwsem.

require

Aha, I should've used Grammarly even for short cover letter :)

will fix it.

Thanks!