Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: initialize net->net_cookie at netns setup

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>
Tested-by: Lorenz Bauer <lmb@cloudflare.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Eric Dumazet and committed by
David S. Miller
3d368ab8 06e56697

+7 -24
+1 -3
include/net/net_namespace.h
··· 165 165 struct netns_xfrm xfrm; 166 166 #endif 167 167 168 - atomic64_t net_cookie; /* written once */ 168 + u64 net_cookie; /* written once */ 169 169 170 170 #if IS_ENABLED(CONFIG_IP_VS) 171 171 struct netns_ipvs *ipvs; ··· 223 223 224 224 struct net *get_net_ns_by_pid(pid_t pid); 225 225 struct net *get_net_ns_by_fd(int fd); 226 - 227 - u64 __net_gen_cookie(struct net *net); 228 226 229 227 #ifdef CONFIG_SYSCTL 230 228 void ipx_register_sysctl(void);
+3 -5
net/core/filter.c
··· 4645 4645 4646 4646 static u64 __bpf_get_netns_cookie(struct sock *sk) 4647 4647 { 4648 - #ifdef CONFIG_NET_NS 4649 - return __net_gen_cookie(sk ? sk->sk_net.net : &init_net); 4650 - #else 4651 - return 0; 4652 - #endif 4648 + const struct net *net = sk ? sock_net(sk) : &init_net; 4649 + 4650 + return net->net_cookie; 4653 4651 } 4654 4652 4655 4653 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
+3 -16
net/core/net_namespace.c
··· 72 72 73 73 DEFINE_COOKIE(net_cookie); 74 74 75 - u64 __net_gen_cookie(struct net *net) 76 - { 77 - while (1) { 78 - u64 res = atomic64_read(&net->net_cookie); 79 - 80 - if (res) 81 - return res; 82 - res = gen_cookie_next(&net_cookie); 83 - atomic64_cmpxchg(&net->net_cookie, 0, res); 84 - } 85 - } 86 - 87 75 static struct net_generic *net_alloc_generic(void) 88 76 { 89 77 struct net_generic *ng; ··· 320 332 refcount_set(&net->ns.count, 1); 321 333 refcount_set(&net->passive, 1); 322 334 get_random_bytes(&net->hash_mix, sizeof(u32)); 335 + preempt_disable(); 336 + net->net_cookie = gen_cookie_next(&net_cookie); 337 + preempt_enable(); 323 338 net->dev_base_seq = 1; 324 339 net->user_ns = user_ns; 325 340 idr_init(&net->netns_ids); ··· 1093 1102 panic("Could not allocate generic netns"); 1094 1103 1095 1104 rcu_assign_pointer(init_net.gen, ng); 1096 - 1097 - preempt_disable(); 1098 - __net_gen_cookie(&init_net); 1099 - preempt_enable(); 1100 1105 1101 1106 down_write(&pernet_ops_rwsem); 1102 1107 if (setup_net(&init_net, &init_user_ns))