diff mbox series

[net-next] net: initialize net->net_cookie at netns setup

Message ID 20210210144144.24284-1-eric.dumazet@gmail.com (mailing list archive)
State Accepted
Commit 3d368ab87cf6681f928de1ddf804d69600671bb2
Delegated to: Netdev Maintainers
Headers show
Series [net-next] net: initialize net->net_cookie at netns setup | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for net-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 11 maintainers not CCed: yhs@fb.com christian.brauner@ubuntu.com ap420073@gmail.com kafai@fb.com ast@kernel.org songliubraving@fb.com john.fastabend@gmail.com kpsingh@kernel.org serge@hallyn.com andrii@kernel.org bpf@vger.kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 7462 this patch: 7462
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 67 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 7860 this patch: 7860
netdev/header_inline success Link
netdev/stable success Stable not CCed

Commit Message

Eric Dumazet Feb. 10, 2021, 2:41 p.m. UTC
From: Eric Dumazet <edumazet@google.com>

It is simpler to make net->net_cookie a plain u64
written once in setup_net() instead of looping
and using atomic64 helpers.

Lorenz Bauer wants to add SO_NETNS_COOKIE socket option
and this patch would makes his patch series simpler.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Lorenz Bauer <lmb@cloudflare.com>
---
 include/net/net_namespace.h |  4 +---
 net/core/filter.c           |  8 +++-----
 net/core/net_namespace.c    | 19 +++----------------
 3 files changed, 7 insertions(+), 24 deletions(-)

Comments

Daniel Borkmann Feb. 10, 2021, 2:50 p.m. UTC | #1
On 2/10/21 3:41 PM, Eric Dumazet wrote:
> From: Eric Dumazet <edumazet@google.com>
> 
> It is simpler to make net->net_cookie a plain u64
> written once in setup_net() instead of looping
> and using atomic64 helpers.
> 
> Lorenz Bauer wants to add SO_NETNS_COOKIE socket option
> and this patch would makes his patch series simpler.
> 
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Lorenz Bauer <lmb@cloudflare.com>

Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Lorenz Bauer Feb. 11, 2021, 11:31 a.m. UTC | #2
On Wed, 10 Feb 2021 at 14:41, Eric Dumazet <eric.dumazet@gmail.com> wrote:
>
> From: Eric Dumazet <edumazet@google.com>
>
> It is simpler to make net->net_cookie a plain u64
> written once in setup_net() instead of looping
> and using atomic64 helpers.
>
> Lorenz Bauer wants to add SO_NETNS_COOKIE socket option
> and this patch would makes his patch series simpler.

This is nice, thank you!

Tested-by: Lorenz Bauer <lmb@cloudflare.com>

--
Lorenz Bauer  |  Systems Engineer
6th Floor, County Hall/The Riverside Building, SE1 7PB, UK

www.cloudflare.com
patchwork-bot+netdevbpf@kernel.org Feb. 11, 2021, 10:20 p.m. UTC | #3
Hello:

This patch was applied to netdev/net-next.git (refs/heads/master):

On Wed, 10 Feb 2021 06:41:44 -0800 you wrote:
> From: Eric Dumazet <edumazet@google.com>
> 
> It is simpler to make net->net_cookie a plain u64
> written once in setup_net() instead of looping
> and using atomic64 helpers.
> 
> Lorenz Bauer wants to add SO_NETNS_COOKIE socket option
> and this patch would makes his patch series simpler.
> 
> [...]

Here is the summary with links:
  - [net-next] net: initialize net->net_cookie at netns setup
    https://git.kernel.org/netdev/net-next/c/3d368ab87cf6

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
diff mbox series

Patch

diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index 29567875f4280116200f0676d6d619f3d9b46279..dcaee24a4d8773ff3bb2adbe5fe9136dfc186184 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -165,7 +165,7 @@  struct net {
 	struct netns_xfrm	xfrm;
 #endif
 
-	atomic64_t		net_cookie; /* written once */
+	u64			net_cookie; /* written once */
 
 #if IS_ENABLED(CONFIG_IP_VS)
 	struct netns_ipvs	*ipvs;
@@ -224,8 +224,6 @@  extern struct list_head net_namespace_list;
 struct net *get_net_ns_by_pid(pid_t pid);
 struct net *get_net_ns_by_fd(int fd);
 
-u64 __net_gen_cookie(struct net *net);
-
 #ifdef CONFIG_SYSCTL
 void ipx_register_sysctl(void);
 void ipx_unregister_sysctl(void);
diff --git a/net/core/filter.c b/net/core/filter.c
index 9ab94e90d66059702a7417ab844d4e8e5f4732c7..74bd401bf483d212884a026599167c1098e78d28 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4645,11 +4645,9 @@  static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
 
 static u64 __bpf_get_netns_cookie(struct sock *sk)
 {
-#ifdef CONFIG_NET_NS
-	return __net_gen_cookie(sk ? sk->sk_net.net : &init_net);
-#else
-	return 0;
-#endif
+	const struct net *net = sk ? sock_net(sk) : &init_net;
+
+	return net->net_cookie;
 }
 
 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 2ef3b4557f40d72d27a1642d98f1e8f06caf9835..43b6ac4c44395b55c966f3fb9141c25bb91848cf 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -72,18 +72,6 @@  static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
 
 DEFINE_COOKIE(net_cookie);
 
-u64 __net_gen_cookie(struct net *net)
-{
-	while (1) {
-		u64 res = atomic64_read(&net->net_cookie);
-
-		if (res)
-			return res;
-		res = gen_cookie_next(&net_cookie);
-		atomic64_cmpxchg(&net->net_cookie, 0, res);
-	}
-}
-
 static struct net_generic *net_alloc_generic(void)
 {
 	struct net_generic *ng;
@@ -332,6 +320,9 @@  static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
 	refcount_set(&net->ns.count, 1);
 	refcount_set(&net->passive, 1);
 	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);
@@ -1103,10 +1094,6 @@  static int __init net_ns_init(void)
 
 	rcu_assign_pointer(init_net.gen, ng);
 
-	preempt_disable();
-	__net_gen_cookie(&init_net);
-	preempt_enable();
-
 	down_write(&pernet_ops_rwsem);
 	if (setup_net(&init_net, &init_user_ns))
 		panic("Could not setup the initial network namespace");