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>
This commit is contained in:
Eric Dumazet 2021-02-10 06:41:44 -08:00 committed by David S. Miller
parent 06e56697bd
commit 3d368ab87c
3 changed files with 7 additions and 24 deletions

View File

@ -165,7 +165,7 @@ struct net {
struct netns_xfrm xfrm; struct netns_xfrm xfrm;
#endif #endif
atomic64_t net_cookie; /* written once */ u64 net_cookie; /* written once */
#if IS_ENABLED(CONFIG_IP_VS) #if IS_ENABLED(CONFIG_IP_VS)
struct netns_ipvs *ipvs; 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_pid(pid_t pid);
struct net *get_net_ns_by_fd(int fd); struct net *get_net_ns_by_fd(int fd);
u64 __net_gen_cookie(struct net *net);
#ifdef CONFIG_SYSCTL #ifdef CONFIG_SYSCTL
void ipx_register_sysctl(void); void ipx_register_sysctl(void);
void ipx_unregister_sysctl(void); void ipx_unregister_sysctl(void);

View File

@ -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) static u64 __bpf_get_netns_cookie(struct sock *sk)
{ {
#ifdef CONFIG_NET_NS const struct net *net = sk ? sock_net(sk) : &init_net;
return __net_gen_cookie(sk ? sk->sk_net.net : &init_net);
#else return net->net_cookie;
return 0;
#endif
} }
BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx) BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)

View File

@ -72,18 +72,6 @@ static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
DEFINE_COOKIE(net_cookie); 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) static struct net_generic *net_alloc_generic(void)
{ {
struct net_generic *ng; 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->ns.count, 1);
refcount_set(&net->passive, 1); refcount_set(&net->passive, 1);
get_random_bytes(&net->hash_mix, sizeof(u32)); 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->dev_base_seq = 1;
net->user_ns = user_ns; net->user_ns = user_ns;
idr_init(&net->netns_ids); idr_init(&net->netns_ids);
@ -1103,10 +1094,6 @@ static int __init net_ns_init(void)
rcu_assign_pointer(init_net.gen, ng); rcu_assign_pointer(init_net.gen, ng);
preempt_disable();
__net_gen_cookie(&init_net);
preempt_enable();
down_write(&pernet_ops_rwsem); down_write(&pernet_ops_rwsem);
if (setup_net(&init_net, &init_user_ns)) if (setup_net(&init_net, &init_user_ns))
panic("Could not setup the initial network namespace"); panic("Could not setup the initial network namespace");