Skip to content

Commit 83044bf

Browse files
committed
Merge branch 'netns-init-cleanups' into main
Kuniyuki Iwashima says: ==================== net: Random cleanup for netns initialisation. patch 1 & 2 suppress unwanted memory allocation for net->gen->ptr[]. patch 3 ~ 6 move part of netns initialisation to prenet_init() that do not require pernet_ops_rwsem. v2: patch 1 : Removed Fixes: tag patch 2 : Use XOR for WARN_ON() v1: https://lore.kernel.org/netdev/[email protected]/ ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents c4b28e5 + 8eaf71f commit 83044bf

File tree

3 files changed

+39
-52
lines changed

3 files changed

+39
-52
lines changed

include/net/net_namespace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ struct pernet_operations {
451451
/* Following method is called with RTNL held. */
452452
void (*exit_batch_rtnl)(struct list_head *net_exit_list,
453453
struct list_head *dev_kill_list);
454-
unsigned int *id;
455-
size_t size;
454+
unsigned int * const id;
455+
const size_t size;
456456
};
457457

458458
/*

net/core/net_namespace.c

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static int ops_init(const struct pernet_operations *ops, struct net *net)
125125
int err = -ENOMEM;
126126
void *data = NULL;
127127

128-
if (ops->id && ops->size) {
128+
if (ops->id) {
129129
data = kzalloc(ops->size, GFP_KERNEL);
130130
if (!data)
131131
goto out;
@@ -140,7 +140,7 @@ static int ops_init(const struct pernet_operations *ops, struct net *net)
140140
if (!err)
141141
return 0;
142142

143-
if (ops->id && ops->size) {
143+
if (ops->id) {
144144
ng = rcu_dereference_protected(net->gen,
145145
lockdep_is_held(&pernet_ops_rwsem));
146146
ng->ptr[*ops->id] = NULL;
@@ -182,7 +182,8 @@ static void ops_free_list(const struct pernet_operations *ops,
182182
struct list_head *net_exit_list)
183183
{
184184
struct net *net;
185-
if (ops->size && ops->id) {
185+
186+
if (ops->id) {
186187
list_for_each_entry(net, net_exit_list, exit_list)
187188
kfree(net_generic(net, *ops->id));
188189
}
@@ -308,36 +309,48 @@ struct net *get_net_ns_by_id(const struct net *net, int id)
308309
}
309310
EXPORT_SYMBOL_GPL(get_net_ns_by_id);
310311

312+
static __net_init void preinit_net_sysctl(struct net *net)
313+
{
314+
net->core.sysctl_somaxconn = SOMAXCONN;
315+
/* Limits per socket sk_omem_alloc usage.
316+
* TCP zerocopy regular usage needs 128 KB.
317+
*/
318+
net->core.sysctl_optmem_max = 128 * 1024;
319+
net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
320+
}
321+
311322
/* init code that must occur even if setup_net() is not called. */
312-
static __net_init void preinit_net(struct net *net)
323+
static __net_init void preinit_net(struct net *net, struct user_namespace *user_ns)
313324
{
325+
refcount_set(&net->passive, 1);
326+
refcount_set(&net->ns.count, 1);
327+
ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt");
314328
ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net notrefcnt");
329+
330+
get_random_bytes(&net->hash_mix, sizeof(u32));
331+
net->dev_base_seq = 1;
332+
net->user_ns = user_ns;
333+
334+
idr_init(&net->netns_ids);
335+
spin_lock_init(&net->nsid_lock);
336+
mutex_init(&net->ipv4.ra_mutex);
337+
preinit_net_sysctl(net);
315338
}
316339

317340
/*
318341
* setup_net runs the initializers for the network namespace object.
319342
*/
320-
static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
343+
static __net_init int setup_net(struct net *net)
321344
{
322345
/* Must be called with pernet_ops_rwsem held */
323346
const struct pernet_operations *ops, *saved_ops;
324347
LIST_HEAD(net_exit_list);
325348
LIST_HEAD(dev_kill_list);
326349
int error = 0;
327350

328-
refcount_set(&net->ns.count, 1);
329-
ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt");
330-
331-
refcount_set(&net->passive, 1);
332-
get_random_bytes(&net->hash_mix, sizeof(u32));
333351
preempt_disable();
334352
net->net_cookie = gen_cookie_next(&net_cookie);
335353
preempt_enable();
336-
net->dev_base_seq = 1;
337-
net->user_ns = user_ns;
338-
idr_init(&net->netns_ids);
339-
spin_lock_init(&net->nsid_lock);
340-
mutex_init(&net->ipv4.ra_mutex);
341354

342355
list_for_each_entry(ops, &pernet_list, list) {
343356
error = ops_init(ops, net);
@@ -382,32 +395,6 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
382395
goto out;
383396
}
384397

385-
static int __net_init net_defaults_init_net(struct net *net)
386-
{
387-
net->core.sysctl_somaxconn = SOMAXCONN;
388-
/* Limits per socket sk_omem_alloc usage.
389-
* TCP zerocopy regular usage needs 128 KB.
390-
*/
391-
net->core.sysctl_optmem_max = 128 * 1024;
392-
net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
393-
394-
return 0;
395-
}
396-
397-
static struct pernet_operations net_defaults_ops = {
398-
.init = net_defaults_init_net,
399-
};
400-
401-
static __init int net_defaults_init(void)
402-
{
403-
if (register_pernet_subsys(&net_defaults_ops))
404-
panic("Cannot initialize net default settings");
405-
406-
return 0;
407-
}
408-
409-
core_initcall(net_defaults_init);
410-
411398
#ifdef CONFIG_NET_NS
412399
static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
413400
{
@@ -496,16 +483,15 @@ struct net *copy_net_ns(unsigned long flags,
496483
goto dec_ucounts;
497484
}
498485

499-
preinit_net(net);
500-
refcount_set(&net->passive, 1);
486+
preinit_net(net, user_ns);
501487
net->ucounts = ucounts;
502488
get_user_ns(user_ns);
503489

504490
rv = down_read_killable(&pernet_ops_rwsem);
505491
if (rv < 0)
506492
goto put_userns;
507493

508-
rv = setup_net(net, user_ns);
494+
rv = setup_net(net);
509495

510496
up_read(&pernet_ops_rwsem);
511497

@@ -1199,9 +1185,10 @@ void __init net_ns_init(void)
11991185
#ifdef CONFIG_KEYS
12001186
init_net.key_domain = &init_net_key_domain;
12011187
#endif
1188+
preinit_net(&init_net, &init_user_ns);
1189+
12021190
down_write(&pernet_ops_rwsem);
1203-
preinit_net(&init_net);
1204-
if (setup_net(&init_net, &init_user_ns))
1191+
if (setup_net(&init_net))
12051192
panic("Could not setup the initial network namespace");
12061193

12071194
init_net_initialized = true;
@@ -1244,7 +1231,7 @@ static int __register_pernet_operations(struct list_head *list,
12441231
LIST_HEAD(net_exit_list);
12451232

12461233
list_add_tail(&ops->list, list);
1247-
if (ops->init || (ops->id && ops->size)) {
1234+
if (ops->init || ops->id) {
12481235
/* We held write locked pernet_ops_rwsem, and parallel
12491236
* setup_net() and cleanup_net() are not possible.
12501237
*/
@@ -1310,6 +1297,9 @@ static int register_pernet_operations(struct list_head *list,
13101297
{
13111298
int error;
13121299

1300+
if (WARN_ON(!!ops->id ^ !!ops->size))
1301+
return -EINVAL;
1302+
13131303
if (ops->id) {
13141304
error = ida_alloc_min(&net_generic_ids, MIN_PERNET_OPS_ID,
13151305
GFP_KERNEL);

net/l2tp/l2tp_ppp.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,8 +1393,6 @@ static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
13931393
* L2TPv2, we dump only L2TPv2 tunnels and sessions here.
13941394
*****************************************************************************/
13951395

1396-
static unsigned int pppol2tp_net_id;
1397-
13981396
#ifdef CONFIG_PROC_FS
13991397

14001398
struct pppol2tp_seq_data {
@@ -1628,7 +1626,6 @@ static __net_exit void pppol2tp_exit_net(struct net *net)
16281626
static struct pernet_operations pppol2tp_net_ops = {
16291627
.init = pppol2tp_init_net,
16301628
.exit = pppol2tp_exit_net,
1631-
.id = &pppol2tp_net_id,
16321629
};
16331630

16341631
/*****************************************************************************

0 commit comments

Comments
 (0)