Skip to content

Commit 447cd7a

Browse files
Kirill Tkhaidavem330
authored andcommitted
net: Allow pernet_operations to be executed in parallel
This adds new pernet_operations::async flag to indicate operations, which ->init(), ->exit() and ->exit_batch() methods are allowed to be executed in parallel with the methods of any other pernet_operations. When there are only asynchronous pernet_operations in the system, net_mutex won't be taken for a net construction and destruction. Also, remove BUG_ON(mutex_is_locked()) from net_assign_generic() without replacing with the equivalent net_sem check, as there is one more lockdep assert below. v3: Add comment near net_mutex. Suggested-by: Eric W. Biederman <[email protected]> Signed-off-by: Kirill Tkhai <[email protected]> Acked-by: Andrei Vagin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent bcab1dd commit 447cd7a

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

include/net/net_namespace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ struct pernet_operations {
313313
void (*exit_batch)(struct list_head *net_exit_list);
314314
unsigned int *id;
315315
size_t size;
316+
/*
317+
* Indicates above methods are allowed to be executed in parallel
318+
* with methods of any other pernet_operations, i.e. they are not
319+
* need synchronization via net_mutex.
320+
*/
321+
bool async;
316322
};
317323

318324
/*

net/core/net_namespace.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
static LIST_HEAD(pernet_list);
3131
static struct list_head *first_device = &pernet_list;
32+
/* Used only if there are !async pernet_operations registered */
3233
DEFINE_MUTEX(net_mutex);
3334

3435
LIST_HEAD(net_namespace_list);
@@ -41,8 +42,9 @@ struct net init_net = {
4142
EXPORT_SYMBOL(init_net);
4243

4344
static bool init_net_initialized;
45+
static unsigned nr_sync_pernet_ops;
4446
/*
45-
* net_sem: protects: pernet_list, net_generic_ids,
47+
* net_sem: protects: pernet_list, net_generic_ids, nr_sync_pernet_ops,
4648
* init_net_initialized and first_device pointer.
4749
*/
4850
DECLARE_RWSEM(net_sem);
@@ -70,11 +72,10 @@ static int net_assign_generic(struct net *net, unsigned int id, void *data)
7072
{
7173
struct net_generic *ng, *old_ng;
7274

73-
BUG_ON(!mutex_is_locked(&net_mutex));
7475
BUG_ON(id < MIN_PERNET_OPS_ID);
7576

7677
old_ng = rcu_dereference_protected(net->gen,
77-
lockdep_is_held(&net_mutex));
78+
lockdep_is_held(&net_sem));
7879
if (old_ng->s.len > id) {
7980
old_ng->ptr[id] = data;
8081
return 0;
@@ -426,11 +427,14 @@ struct net *copy_net_ns(unsigned long flags,
426427
rv = down_read_killable(&net_sem);
427428
if (rv < 0)
428429
goto put_userns;
429-
rv = mutex_lock_killable(&net_mutex);
430-
if (rv < 0)
431-
goto up_read;
430+
if (nr_sync_pernet_ops) {
431+
rv = mutex_lock_killable(&net_mutex);
432+
if (rv < 0)
433+
goto up_read;
434+
}
432435
rv = setup_net(net, user_ns);
433-
mutex_unlock(&net_mutex);
436+
if (nr_sync_pernet_ops)
437+
mutex_unlock(&net_mutex);
434438
up_read:
435439
up_read(&net_sem);
436440
if (rv < 0) {
@@ -487,7 +491,8 @@ static void cleanup_net(struct work_struct *work)
487491
spin_unlock_irq(&cleanup_list_lock);
488492

489493
down_read(&net_sem);
490-
mutex_lock(&net_mutex);
494+
if (nr_sync_pernet_ops)
495+
mutex_lock(&net_mutex);
491496

492497
/* Don't let anyone else find us. */
493498
rtnl_lock();
@@ -522,7 +527,8 @@ static void cleanup_net(struct work_struct *work)
522527
list_for_each_entry_reverse(ops, &pernet_list, list)
523528
ops_exit_list(ops, &net_exit_list);
524529

525-
mutex_unlock(&net_mutex);
530+
if (nr_sync_pernet_ops)
531+
mutex_unlock(&net_mutex);
526532

527533
/* Free the net generic variables */
528534
list_for_each_entry_reverse(ops, &pernet_list, list)
@@ -994,14 +1000,18 @@ static int register_pernet_operations(struct list_head *list,
9941000
rcu_barrier();
9951001
if (ops->id)
9961002
ida_remove(&net_generic_ids, *ops->id);
1003+
} else if (!ops->async) {
1004+
pr_info_once("Pernet operations %ps are sync.\n", ops);
1005+
nr_sync_pernet_ops++;
9971006
}
9981007

9991008
return error;
10001009
}
10011010

10021011
static void unregister_pernet_operations(struct pernet_operations *ops)
10031012
{
1004-
1013+
if (!ops->async)
1014+
BUG_ON(nr_sync_pernet_ops-- == 0);
10051015
__unregister_pernet_operations(ops);
10061016
rcu_barrier();
10071017
if (ops->id)

0 commit comments

Comments
 (0)