Skip to content

Commit 1a57feb

Browse files
Kirill Tkhaidavem330
authored andcommitted
net: Introduce net_sem for protection of pernet_list
Currently, the mutex is mostly used to protect pernet operations list. It orders setup_net() and cleanup_net() with parallel {un,}register_pernet_operations() calls, so ->exit{,batch} methods of the same pernet operations are executed for a dying net, as were used to call ->init methods, even after the net namespace is unlinked from net_namespace_list in cleanup_net(). But there are several problems with scalability. The first one is that more than one net can't be created or destroyed at the same moment on the node. For big machines with many cpus running many containers it's very sensitive. The second one is that it's need to synchronize_rcu() after net is removed from net_namespace_list(): Destroy net_ns: cleanup_net() mutex_lock(&net_mutex) list_del_rcu(&net->list) synchronize_rcu() <--- Sleep there for ages list_for_each_entry_reverse(ops, &pernet_list, list) ops_exit_list(ops, &net_exit_list) list_for_each_entry_reverse(ops, &pernet_list, list) ops_free_list(ops, &net_exit_list) mutex_unlock(&net_mutex) This primitive is not fast, especially on the systems with many processors and/or when preemptible RCU is enabled in config. So, all the time, while cleanup_net() is waiting for RCU grace period, creation of new net namespaces is not possible, the tasks, who makes it, are sleeping on the same mutex: Create net_ns: copy_net_ns() mutex_lock_killable(&net_mutex) <--- Sleep there for ages I observed 20-30 seconds hangs of "unshare -n" on ordinary 8-cpu laptop with preemptible RCU enabled after CRIU tests round is finished. The solution is to convert net_mutex to the rw_semaphore and add fine grain locks to really small number of pernet_operations, what really need them. Then, pernet_operations::init/::exit methods, modifying the net-related data, will require down_read() locking only, while down_write() will be used for changing pernet_list (i.e., when modules are being loaded and unloaded). This gives signify performance increase, after all patch set is applied, like you may see here: %for i in {1..10000}; do unshare -n bash -c exit; done *before* real 1m40,377s user 0m9,672s sys 0m19,928s *after* real 0m17,007s user 0m5,311s sys 0m11,779 (5.8 times faster) This patch starts replacing net_mutex to net_sem. It adds rw_semaphore, describes the variables it protects, and makes to use, where appropriate. net_mutex is still present, and next patches will kick it out step-by-step. Signed-off-by: Kirill Tkhai <[email protected]> Acked-by: Andrei Vagin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5ba049a commit 1a57feb

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

include/linux/rtnetlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern int rtnl_is_locked(void);
3636

3737
extern wait_queue_head_t netdev_unregistering_wq;
3838
extern struct mutex net_mutex;
39+
extern struct rw_semaphore net_sem;
3940

4041
#ifdef CONFIG_PROVE_LOCKING
4142
extern bool lockdep_rtnl_is_held(void);

net/core/net_namespace.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ struct net init_net = {
4141
EXPORT_SYMBOL(init_net);
4242

4343
static bool init_net_initialized;
44+
/*
45+
* net_sem: protects: pernet_list, net_generic_ids,
46+
* init_net_initialized and first_device pointer.
47+
*/
48+
DECLARE_RWSEM(net_sem);
4449

4550
#define MIN_PERNET_OPS_ID \
4651
((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
@@ -286,7 +291,7 @@ struct net *get_net_ns_by_id(struct net *net, int id)
286291
*/
287292
static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
288293
{
289-
/* Must be called with net_mutex held */
294+
/* Must be called with net_sem held */
290295
const struct pernet_operations *ops, *saved_ops;
291296
int error = 0;
292297
LIST_HEAD(net_exit_list);
@@ -418,12 +423,16 @@ struct net *copy_net_ns(unsigned long flags,
418423
net->ucounts = ucounts;
419424
get_user_ns(user_ns);
420425

421-
rv = mutex_lock_killable(&net_mutex);
426+
rv = down_read_killable(&net_sem);
422427
if (rv < 0)
423428
goto put_userns;
424-
429+
rv = mutex_lock_killable(&net_mutex);
430+
if (rv < 0)
431+
goto up_read;
425432
rv = setup_net(net, user_ns);
426433
mutex_unlock(&net_mutex);
434+
up_read:
435+
up_read(&net_sem);
427436
if (rv < 0) {
428437
put_userns:
429438
put_user_ns(user_ns);
@@ -477,6 +486,7 @@ static void cleanup_net(struct work_struct *work)
477486
list_replace_init(&cleanup_list, &net_kill_list);
478487
spin_unlock_irq(&cleanup_list_lock);
479488

489+
down_read(&net_sem);
480490
mutex_lock(&net_mutex);
481491

482492
/* Don't let anyone else find us. */
@@ -517,6 +527,7 @@ static void cleanup_net(struct work_struct *work)
517527
ops_free_list(ops, &net_exit_list);
518528

519529
mutex_unlock(&net_mutex);
530+
up_read(&net_sem);
520531

521532
/* Ensure there are no outstanding rcu callbacks using this
522533
* network namespace.
@@ -543,8 +554,10 @@ static void cleanup_net(struct work_struct *work)
543554
*/
544555
void net_ns_barrier(void)
545556
{
557+
down_write(&net_sem);
546558
mutex_lock(&net_mutex);
547559
mutex_unlock(&net_mutex);
560+
up_write(&net_sem);
548561
}
549562
EXPORT_SYMBOL(net_ns_barrier);
550563

@@ -871,12 +884,12 @@ static int __init net_ns_init(void)
871884

872885
rcu_assign_pointer(init_net.gen, ng);
873886

874-
mutex_lock(&net_mutex);
887+
down_write(&net_sem);
875888
if (setup_net(&init_net, &init_user_ns))
876889
panic("Could not setup the initial network namespace");
877890

878891
init_net_initialized = true;
879-
mutex_unlock(&net_mutex);
892+
up_write(&net_sem);
880893

881894
register_pernet_subsys(&net_ns_ops);
882895

@@ -1016,9 +1029,9 @@ static void unregister_pernet_operations(struct pernet_operations *ops)
10161029
int register_pernet_subsys(struct pernet_operations *ops)
10171030
{
10181031
int error;
1019-
mutex_lock(&net_mutex);
1032+
down_write(&net_sem);
10201033
error = register_pernet_operations(first_device, ops);
1021-
mutex_unlock(&net_mutex);
1034+
up_write(&net_sem);
10221035
return error;
10231036
}
10241037
EXPORT_SYMBOL_GPL(register_pernet_subsys);
@@ -1034,9 +1047,9 @@ EXPORT_SYMBOL_GPL(register_pernet_subsys);
10341047
*/
10351048
void unregister_pernet_subsys(struct pernet_operations *ops)
10361049
{
1037-
mutex_lock(&net_mutex);
1050+
down_write(&net_sem);
10381051
unregister_pernet_operations(ops);
1039-
mutex_unlock(&net_mutex);
1052+
up_write(&net_sem);
10401053
}
10411054
EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
10421055

@@ -1062,11 +1075,11 @@ EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
10621075
int register_pernet_device(struct pernet_operations *ops)
10631076
{
10641077
int error;
1065-
mutex_lock(&net_mutex);
1078+
down_write(&net_sem);
10661079
error = register_pernet_operations(&pernet_list, ops);
10671080
if (!error && (first_device == &pernet_list))
10681081
first_device = &ops->list;
1069-
mutex_unlock(&net_mutex);
1082+
up_write(&net_sem);
10701083
return error;
10711084
}
10721085
EXPORT_SYMBOL_GPL(register_pernet_device);
@@ -1082,11 +1095,11 @@ EXPORT_SYMBOL_GPL(register_pernet_device);
10821095
*/
10831096
void unregister_pernet_device(struct pernet_operations *ops)
10841097
{
1085-
mutex_lock(&net_mutex);
1098+
down_write(&net_sem);
10861099
if (&ops->list == first_device)
10871100
first_device = first_device->next;
10881101
unregister_pernet_operations(ops);
1089-
mutex_unlock(&net_mutex);
1102+
up_write(&net_sem);
10901103
}
10911104
EXPORT_SYMBOL_GPL(unregister_pernet_device);
10921105

net/core/rtnetlink.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,11 @@ static void rtnl_lock_unregistering_all(void)
454454
void rtnl_link_unregister(struct rtnl_link_ops *ops)
455455
{
456456
/* Close the race with cleanup_net() */
457-
mutex_lock(&net_mutex);
457+
down_write(&net_sem);
458458
rtnl_lock_unregistering_all();
459459
__rtnl_link_unregister(ops);
460460
rtnl_unlock();
461-
mutex_unlock(&net_mutex);
461+
up_write(&net_sem);
462462
}
463463
EXPORT_SYMBOL_GPL(rtnl_link_unregister);
464464

0 commit comments

Comments
 (0)