aboutsummaryrefslogtreecommitdiffstats
path: root/net/core
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2018-02-20 13:23:57 -0500
committerDavid S. Miller <davem@davemloft.net>2018-02-20 13:23:57 -0500
commitb99fe0e28fe0ba7d6e17e234e1150f1d9e197632 (patch)
tree0ebe27833d80a4162d34b6b46c8bc20db85b9dac /net/core
parentibmvnic: Keep track of supplementary TX descriptors (diff)
parentnet: Queue net_cleanup_work only if there is first net added (diff)
downloadlinux-dev-b99fe0e28fe0ba7d6e17e234e1150f1d9e197632.tar.xz
linux-dev-b99fe0e28fe0ba7d6e17e234e1150f1d9e197632.zip
Merge branch 'net-Get-rid-of-net_mutex-and-simplify-cleanup_list-queueing'
Kirill Tkhai says: ==================== net: Get rid of net_mutex and simplify cleanup_list queueing [1/3] kills net_mutex and makes net_sem be taken for write instead. This is made to take less locks (1 instead of 2) for the time before all pernet_operations are converted. [2-3/3] simplifies dead net cleanup queueing, and makes llist api be used for that. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core')
-rw-r--r--net/core/net_namespace.c75
1 files changed, 39 insertions, 36 deletions
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index bcab9a938d6f..27a55236ad64 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -29,8 +29,6 @@
static LIST_HEAD(pernet_list);
static struct list_head *first_device = &pernet_list;
-/* Used only if there are !async pernet_operations registered */
-DEFINE_MUTEX(net_mutex);
LIST_HEAD(net_namespace_list);
EXPORT_SYMBOL_GPL(net_namespace_list);
@@ -407,6 +405,7 @@ struct net *copy_net_ns(unsigned long flags,
{
struct ucounts *ucounts;
struct net *net;
+ unsigned write;
int rv;
if (!(flags & CLONE_NEWNET))
@@ -424,20 +423,26 @@ struct net *copy_net_ns(unsigned long flags,
refcount_set(&net->passive, 1);
net->ucounts = ucounts;
get_user_ns(user_ns);
-
- rv = down_read_killable(&net_sem);
+again:
+ write = READ_ONCE(nr_sync_pernet_ops);
+ if (write)
+ rv = down_write_killable(&net_sem);
+ else
+ rv = down_read_killable(&net_sem);
if (rv < 0)
goto put_userns;
- if (nr_sync_pernet_ops) {
- rv = mutex_lock_killable(&net_mutex);
- if (rv < 0)
- goto up_read;
+
+ if (!write && unlikely(READ_ONCE(nr_sync_pernet_ops))) {
+ up_read(&net_sem);
+ goto again;
}
rv = setup_net(net, user_ns);
- if (nr_sync_pernet_ops)
- mutex_unlock(&net_mutex);
-up_read:
- up_read(&net_sem);
+
+ if (write)
+ up_write(&net_sem);
+ else
+ up_read(&net_sem);
+
if (rv < 0) {
put_userns:
put_user_ns(user_ns);
@@ -476,28 +481,33 @@ static void unhash_nsid(struct net *net, struct net *last)
spin_unlock_bh(&net->nsid_lock);
}
-static DEFINE_SPINLOCK(cleanup_list_lock);
-static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
+static LLIST_HEAD(cleanup_list);
static void cleanup_net(struct work_struct *work)
{
const struct pernet_operations *ops;
struct net *net, *tmp, *last;
- struct list_head net_kill_list;
+ struct llist_node *net_kill_list;
LIST_HEAD(net_exit_list);
+ unsigned write;
/* Atomically snapshot the list of namespaces to cleanup */
- spin_lock_irq(&cleanup_list_lock);
- list_replace_init(&cleanup_list, &net_kill_list);
- spin_unlock_irq(&cleanup_list_lock);
+ net_kill_list = llist_del_all(&cleanup_list);
+again:
+ write = READ_ONCE(nr_sync_pernet_ops);
+ if (write)
+ down_write(&net_sem);
+ else
+ down_read(&net_sem);
- down_read(&net_sem);
- if (nr_sync_pernet_ops)
- mutex_lock(&net_mutex);
+ if (!write && unlikely(READ_ONCE(nr_sync_pernet_ops))) {
+ up_read(&net_sem);
+ goto again;
+ }
/* Don't let anyone else find us. */
rtnl_lock();
- list_for_each_entry(net, &net_kill_list, cleanup_list)
+ llist_for_each_entry(net, net_kill_list, cleanup_list)
list_del_rcu(&net->list);
/* Cache last net. After we unlock rtnl, no one new net
* added to net_namespace_list can assign nsid pointer
@@ -512,7 +522,7 @@ static void cleanup_net(struct work_struct *work)
last = list_last_entry(&net_namespace_list, struct net, list);
rtnl_unlock();
- list_for_each_entry(net, &net_kill_list, cleanup_list) {
+ llist_for_each_entry(net, net_kill_list, cleanup_list) {
unhash_nsid(net, last);
list_add_tail(&net->exit_list, &net_exit_list);
}
@@ -528,14 +538,14 @@ static void cleanup_net(struct work_struct *work)
list_for_each_entry_reverse(ops, &pernet_list, list)
ops_exit_list(ops, &net_exit_list);
- if (nr_sync_pernet_ops)
- mutex_unlock(&net_mutex);
-
/* Free the net generic variables */
list_for_each_entry_reverse(ops, &pernet_list, list)
ops_free_list(ops, &net_exit_list);
- up_read(&net_sem);
+ if (write)
+ up_write(&net_sem);
+ else
+ up_read(&net_sem);
/* Ensure there are no outstanding rcu callbacks using this
* network namespace.
@@ -563,8 +573,6 @@ static void cleanup_net(struct work_struct *work)
void net_ns_barrier(void)
{
down_write(&net_sem);
- mutex_lock(&net_mutex);
- mutex_unlock(&net_mutex);
up_write(&net_sem);
}
EXPORT_SYMBOL(net_ns_barrier);
@@ -574,13 +582,8 @@ static DECLARE_WORK(net_cleanup_work, cleanup_net);
void __put_net(struct net *net)
{
/* Cleanup the network namespace in process context */
- unsigned long flags;
-
- spin_lock_irqsave(&cleanup_list_lock, flags);
- list_add(&net->cleanup_list, &cleanup_list);
- spin_unlock_irqrestore(&cleanup_list_lock, flags);
-
- queue_work(netns_wq, &net_cleanup_work);
+ if (llist_add(&net->cleanup_list, &cleanup_list))
+ queue_work(netns_wq, &net_cleanup_work);
}
EXPORT_SYMBOL_GPL(__put_net);