Skip to contentSkip to conversation

netlink: switch net_ns only if net is not init_net

PATCH In review Gu Zheng 2 participants 5 messages Mar 7, 2014 – Mar 10, 2014

Subsystems (3)
lore
Share & tools
lore

Gu Zheng

author
#0 lore raw

PATCH 1/1 netlink: switch net_ns only if net is not init_net

Many netlink users create netlink sock in the init_net, and the switching nes_ns(init_net-->net) is needless in this case. So here we add a pre-check to avoid this.

---
 net/netlink/af_netlink.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
net/netlink/af_netlink.c +2 -1
index fdf5135..e9e4be9 100644
@@ -2429,7 +2429,8 @@ __netlink_kernel_create(struct net *net, int unit, struct module *module,
 		goto out_sock_release_nosk;
 
 	sk = sock->sk;
-	sk_change_net(sk, net);
+	if (!net_eq(net, &init_net))
+		sk_change_net(sk, net);
 
 	if (!cfg || cfg->groups < 32)
 		groups = 32;
Signature · 1 line
1.7.7
maintainer
#1 lore raw

From: Gu Zheng <[email protected]>
Date: Fri, 07 Mar 2014 18:47:30 +0800

Quoted 5 lines

Many netlink users create netlink sock in the init_net, and the switching nes_ns(init_net-->net) is needless in this case. So here we add a pre-check to avoid this.

This check is more appropriately placed into sk_change_net() itself.

author
#2 lore raw
Quoted 10 lines · David Miller

From: Gu Zheng <[email protected]>
Date: Fri, 07 Mar 2014 18:47:30 +0800

Quoted 5 lines

Many netlink users create netlink sock in the init_net, and the switching nes_ns(init_net-->net) is needless in this case. So here we add a pre-check to avoid this.

This check is more appropriately placed into sk_change_net() itself.

Agree. I'll update it soon.

Regards,
Gu

Quoted 1 line
author
#3 lore raw

PATCH 1/1 net: add a pre-check of net_ns in sk_change_net()

In reply to David Miller

We do not need to switch the net_ns if the target net_ns the same as the current one, so here we add a pre-check of net_ns to avoid this as David suggested.

---
 include/net/sock.h |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
include/net/sock.h +6 -2
index 5c3f7c3..9678569 100644
@@ -2252,8 +2252,12 @@ void sock_net_set(struct sock *sk, struct net *net)
  */
 static inline void sk_change_net(struct sock *sk, struct net *net)
 {
-	put_net(sock_net(sk));
-	sock_net_set(sk, hold_net(net));
+	struct net *current_net = sock_net(sk);
+
+	if (!net_eq(current_net, net)) {
+		put_net(current_net);
+		sock_net_set(sk, hold_net(net));
+	}
 }
 
 static inline struct sock *skb_steal_sock(struct sk_buff *skb)
Signature · 1 line
1.7.7
maintainer Applied
#4 lore raw

Re: [PATCH] net: add a pre-check of net_ns in sk_change_net()

From: Gu Zheng <[email protected]>
Date: Mon, 10 Mar 2014 09:57:34 +0800

Quoted 5 lines

We do not need to switch the net_ns if the target net_ns the same as the current one, so here we add a pre-check of net_ns to avoid this as David suggested.

Applied to net-next, thank you.