Skip to content

Commit 3d368ab

Browse files
edumazetdavem330
authored andcommitted
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>
1 parent 06e5669 commit 3d368ab

3 files changed

Lines changed: 7 additions & 24 deletions

File tree

‎include/net/net_namespace.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct net {
165165
struct netns_xfrm xfrm;
166166
#endif
167167

168-
atomic64_t net_cookie; /* written once */
168+
u64 net_cookie; /* written once */
169169

170170
#if IS_ENABLED(CONFIG_IP_VS)
171171
struct netns_ipvs *ipvs;
@@ -224,8 +224,6 @@ extern struct list_head net_namespace_list;
224224
struct net *get_net_ns_by_pid(pid_t pid);
225225
struct net *get_net_ns_by_fd(int fd);
226226

227-
u64 __net_gen_cookie(struct net *net);
228-
229227
#ifdef CONFIG_SYSCTL
230228
void ipx_register_sysctl(void);
231229
void ipx_unregister_sysctl(void);

‎net/core/filter.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4645,11 +4645,9 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
46454645

46464646
static u64 __bpf_get_netns_cookie(struct sock *sk)
46474647
{
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;
46534651
}
46544652

46554653
BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)

‎net/core/net_namespace.c‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
7272

7373
DEFINE_COOKIE(net_cookie);
7474

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-
8775
static struct net_generic *net_alloc_generic(void)
8876
{
8977
struct net_generic *ng;
@@ -332,6 +320,9 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
332320
refcount_set(&net->ns.count, 1);
333321
refcount_set(&net->passive, 1);
334322
get_random_bytes(&net->hash_mix, sizeof(u32));
323+
preempt_disable();
324+
net->net_cookie = gen_cookie_next(&net_cookie);
325+
preempt_enable();
335326
net->dev_base_seq = 1;
336327
net->user_ns = user_ns;
337328
idr_init(&net->netns_ids);
@@ -1103,10 +1094,6 @@ static int __init net_ns_init(void)
11031094

11041095
rcu_assign_pointer(init_net.gen, ng);
11051096

1106-
preempt_disable();
1107-
__net_gen_cookie(&init_net);
1108-
preempt_enable();
1109-
11101097
down_write(&pernet_ops_rwsem);
11111098
if (setup_net(&init_net, &init_user_ns))
11121099
panic("Could not setup the initial network namespace");

0 commit comments

Comments
 (0)