Skip to contentSkip to conversation

net: net_namespace: Optimize the code

PATCH Superseded Yajun Deng 2 participants 2 messages Aug 17, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yajun Deng

author
#0 lore raw

PATCH 1/1 net: net_namespace: Optimize the code

Inline ops_free(), becase there is only one caller. Separate net_drop_ns() and net_free(), so the net_free() can be called directly.
Add free_exit_list() helper function for free net_exit_list.

---
 net/core/net_namespace.c | 49 ++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 27 deletions(-)
net/core/net_namespace.c +8 -13
index 9b5a767eddd5..27acbf2df78f 100644
@@ -148,13 +148,6 @@ static int ops_init(const struct pernet_operations *ops, struct net *net)
 	return err;
 }
 
-static void ops_free(const struct pernet_operations *ops, struct net *net)
-{
-	if (ops->id && ops->size) {
-		kfree(net_generic(net, *ops->id));
-	}
-}
-
 static void ops_pre_exit_list(const struct pernet_operations *ops,
 			      struct list_head *net_exit_list)
 {
@@ -184,7 +177,7 @@ static void ops_free_list(const struct pernet_operations *ops,
 	struct net *net;
 	if (ops->size && ops->id) {
 		list_for_each_entry(net, net_exit_list, exit_list)
-			ops_free(ops, net);
+			kfree(net_generic(net, *ops->id));
 	}
 }
 
@@ -433,15 +426,17 @@ static struct net *net_alloc(void)
 
 static void net_free(struct net *net)
 {
-	kfree(rcu_access_pointer(net->gen));
-	kmem_cache_free(net_cachep, net);
+	if (refcount_dec_and_test(&net->passive)) {
+		kfree(rcu_access_pointer(net->gen));
+		kmem_cache_free(net_cachep, net);
+	}
 }
 
 void net_drop_ns(void *p)
 {
-	struct net *ns = p;
-	if (ns && refcount_dec_and_test(&ns->passive))
-		net_free(ns);
+	struct net *net = (struct net *)p;
 
+	if (net)
+		net_free(net);
 }
 

struct net *copy_net_ns(unsigned long flags,
@@ -479,7 +474,7 @@ struct net *copy_net_ns(unsigned long flags,
put_userns:
key_remove_domain(net->key_domain); put_user_ns(user_ns);
- net_drop_ns(net);
+ net_free(net);
dec_ucounts:
dec_net_namespaces(ucounts);
return ERR_PTR(rv);
@@ -611,7 +606,7 @@ static void cleanup_net(struct work_struct *work)
dec_net_namespaces(net->ucounts); key_remove_domain(net->key_domain); put_user_ns(net->user_ns);
- net_drop_ns(net);
+ net_free(net);
}
}

@@ -1120,6 +1115,14 @@ static int __init net_ns_init(void)

pure_initcall(net_ns_init);

+static void free_exit_list(struct pernet_operations *ops, struct list_head *net_exit_list) +{
+ ops_pre_exit_list(ops, net_exit_list);
+ synchronize_rcu();
+ ops_exit_list(ops, net_exit_list);
+ ops_free_list(ops, net_exit_list);
+}
+
#ifdef CONFIG_NET_NS
static int __register_pernet_operations(struct list_head *list,
struct pernet_operations *ops)
@@ -1145,10 +1148,7 @@ static int __register_pernet_operations(struct list_head *list,
out_undo:
/* If I have an error cleanup all namespaces I initialized */ list_del(&ops->list);
- ops_pre_exit_list(ops, &net_exit_list);
- synchronize_rcu();
- ops_exit_list(ops, &net_exit_list);
- ops_free_list(ops, &net_exit_list);
+ free_exit_list(ops, &net_exit_list);
return error;
}

@@ -1161,10 +1161,8 @@ static void __unregister_pernet_operations(struct pernet_operations *ops)
/* See comment in __register_pernet_operations() */
for_each_net(net)
list_add_tail(&net->exit_list, &net_exit_list);
- ops_pre_exit_list(ops, &net_exit_list);
- synchronize_rcu();
- ops_exit_list(ops, &net_exit_list);
- ops_free_list(ops, &net_exit_list);
+
+ free_exit_list(ops, &net_exit_list);
}

#else
@@ -1187,10 +1185,7 @@ static void __unregister_pernet_operations(struct pernet_operations *ops)
} else {
LIST_HEAD(net_exit_list);
list_add(&init_net.exit_list, &net_exit_list);
- ops_pre_exit_list(ops, &net_exit_list);
- synchronize_rcu();
- ops_exit_list(ops, &net_exit_list);
- ops_free_list(ops, &net_exit_list);
+ free_exit_list(ops, &net_exit_list);
}
}

Signature · 1 line
2.32.0
maintainer
#1 lore raw
Quoted 6 lines · On Tue, 17 Aug 2021 18:20:01 +0800 Yajun Deng wrote:

Inline ops_free(), becase there is only one caller. Separate net_drop_ns() and net_free(), so the net_free() can be called directly.
Add free_exit_list() helper function for free net_exit_list.

This patch does not apply, please rebase.