public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Kirill Tkhai <ktkhai@virtuozzo.com>
To: davem@davemloft.net, vyasevic@redhat.com,
	kstewart@linuxfoundation.org, pombredanne@nexb.com,
	vyasevich@gmail.com, mark.rutland@arm.com,
	gregkh@linuxfoundation.org, adobriyan@gmail.com, fw@strlen.de,
	nicolas.dichtel@6wind.com, xiyou.wangcong@gmail.com,
	roman.kapl@sysgo.com, paul@paul-moore.com, dsahern@gmail.com,
	daniel@iogearbox.net, lucien.xin@gmail.com,
	mschiffer@universe-factory.net, rshearma@brocade.com,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	ktkhai@virtuozzo.com, ebiederm@xmission.com,
	avagin@virtuozzo.com, gorcunov@virtuozzo.com,
	eric.dumazet@gmail.com, stephen@networkplumber.org,
	ktkhai@virtuozzo.com
Subject: [PATCH RFC 07/25] net: Make sys sublist pernet_operations executed out of net_mutex
Date: Fri, 17 Nov 2017 21:28:21 +0300	[thread overview]
Message-ID: <151094330120.20009.9804877551209771323.stgit@localhost.localdomain> (raw)
In-Reply-To: <151094119999.20009.6955267140148739392.stgit@localhost.localdomain>

Move net_mutex to setup_net() and cleanup_net(), and
do not hold it, while sys sublist methods are executed.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 net/core/net_namespace.c |   44 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)

diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index f4f4aaa5ce1f..7aec8c1afe50 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -84,11 +84,11 @@ static int net_assign_generic(struct net *net, unsigned int id, void *data)
 {
 	struct net_generic *ng, *old_ng;
 
-	BUG_ON(!mutex_is_locked(&net_mutex));
+	BUG_ON(!rwsem_is_locked(&net_sem));
 	BUG_ON(id < MIN_PERNET_OPS_ID);
 
 	old_ng = rcu_dereference_protected(net->gen,
-					   lockdep_is_held(&net_mutex));
+					   lockdep_is_held(&net_sem));
 	if (old_ng->s.len > id) {
 		old_ng->ptr[id] = data;
 		return 0;
@@ -300,6 +300,7 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
 {
 	/* Must be called with net_mutex held */
 	const struct pernet_operations *ops, *saved_ops;
+	bool locked = false;
 	int error = 0;
 	LIST_HEAD(net_exit_list);
 
@@ -311,14 +312,34 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
 	spin_lock_init(&net->nsid_lock);
 
 	list_for_each_entry(ops, &pernet_list, list) {
+		if (&ops->list == first_subsys) {
+			BUG_ON(locked);
+			error = mutex_lock_killable(&net_mutex);
+			if (error)
+				goto out_undo;
+			locked = true;
+		}
+
 		error = ops_init(ops, net);
 		if (error < 0)
 			goto out_undo;
 	}
+
+	if (!locked) {
+		/*
+		 * This may happen only on early boot, so we don't
+		 * care about possibility to interrupt the locking.
+		 */
+		mutex_lock(&net_mutex);
+		locked = true;
+	}
+
 	rtnl_lock();
 	list_add_tail_rcu(&net->list, &net_namespace_list);
 	rtnl_unlock();
 out:
+	if (locked)
+		mutex_unlock(&net_mutex);
 	return error;
 
 out_undo:
@@ -433,12 +454,7 @@ struct net *copy_net_ns(unsigned long flags,
 	rv = down_read_killable(&net_sem);
 	if (rv < 0)
 		goto put_userns;
-	rv = mutex_lock_killable(&net_mutex);
-	if (rv < 0)
-		goto up_read;
 	rv = setup_net(net, user_ns);
-	mutex_unlock(&net_mutex);
-up_read:
 	up_read(&net_sem);
 	if (rv < 0) {
 put_userns:
@@ -460,6 +476,7 @@ static void cleanup_net(struct work_struct *work)
 	struct net *net, *tmp;
 	struct list_head net_kill_list;
 	LIST_HEAD(net_exit_list);
+	bool locked;
 
 	/* Atomically snapshot the list of namespaces to cleanup */
 	spin_lock_irq(&cleanup_list_lock);
@@ -468,6 +485,7 @@ static void cleanup_net(struct work_struct *work)
 
 	down_read(&net_sem);
 	mutex_lock(&net_mutex);
+	locked = true;
 
 	/* Don't let anyone else find us. */
 	rtnl_lock();
@@ -500,10 +518,18 @@ static void cleanup_net(struct work_struct *work)
 	synchronize_rcu();
 
 	/* Run all of the network namespace exit methods */
-	list_for_each_entry_reverse(ops, &pernet_list, list)
+	list_for_each_entry_reverse(ops, &pernet_list, list) {
 		ops_exit_list(ops, &net_exit_list);
 
-	mutex_unlock(&net_mutex);
+		if (&ops->list == first_subsys) {
+			BUG_ON(!locked);
+			mutex_unlock(&net_mutex);
+			locked = false;
+		}
+	}
+
+	if (locked)
+		mutex_unlock(&net_mutex);
 
 	/* Free the net generic variables */
 	list_for_each_entry_reverse(ops, &pernet_list, list)

  parent reply	other threads:[~2017-11-17 18:28 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-17 18:27 [PATCH RFC 00/25] Replacing net_mutex with rw_semaphore Kirill Tkhai
2017-11-17 18:27 ` [PATCH RFC 01/25] net: Assign net to net_namespace_list in setup_net() Kirill Tkhai
2017-11-17 18:27 ` [PATCH RFC 02/25] net: Cleanup copy_net_ns() Kirill Tkhai
2017-11-17 18:27 ` [PATCH RFC 03/25] net: Introduce net_sem for protection of pernet_list Kirill Tkhai
2017-11-17 18:27 ` [PATCH RFC 04/25] net: Move mutex_unlock() in cleanup_net() up Kirill Tkhai
2017-11-17 18:28 ` [PATCH RFC 05/25] net: Add primitives to update heads of pernet_list sublists Kirill Tkhai
2017-11-17 18:28 ` [PATCH RFC 06/25] net: Add pernet sys and registration functions Kirill Tkhai
2017-11-17 18:28 ` Kirill Tkhai [this message]
2017-11-17 18:28 ` [PATCH RFC 08/25] net: Move proc_net_ns_ops to pernet_sys list Kirill Tkhai
2017-11-17 18:28 ` [PATCH RFC 09/25] net: Move net_ns_ops " Kirill Tkhai
2017-11-17 18:28 ` [PATCH RFC 10/25] net: Move sysctl_pernet_ops " Kirill Tkhai
2017-11-17 18:29 ` [PATCH RFC 11/25] net: Move netfilter_net_ops " Kirill Tkhai
2017-11-17 18:29 ` [PATCH RFC 12/25] net: Move nf_log_net_ops " Kirill Tkhai
2017-11-17 18:29 ` [PATCH RFC 13/25] net: Move net_inuse_ops " Kirill Tkhai
2017-11-17 18:29 ` [PATCH RFC 14/25] net: Move net_defaults_ops " Kirill Tkhai
2017-11-17 18:29 ` [PATCH RFC 15/25] net: Move netlink_net_ops " Kirill Tkhai
2017-11-17 18:29 ` [PATCH RFC 16/25] net: Move rtnetlink_net_ops " Kirill Tkhai
2017-11-17 18:29 ` [PATCH RFC 17/25] net: Move audit_net_ops " Kirill Tkhai
2017-11-17 18:30 ` [PATCH RFC 18/25] net: Move uevent_net_ops " Kirill Tkhai
2017-11-17 18:30 ` [PATCH RFC 19/25] net: Move proto_net_ops " Kirill Tkhai
2017-11-17 18:30 ` [PATCH RFC 20/25] net: Move pernet_subsys, registered via net_dev_init(), " Kirill Tkhai
2017-11-17 18:30 ` [PATCH RFC 21/25] net: Move fib_* pernet_operations, registered via subsys_initcall(), " Kirill Tkhai
2017-11-17 18:30 ` [PATCH RFC 22/25] net: Move subsys_initcall() registered pernet_operations from net/sched " Kirill Tkhai
2017-11-17 18:30 ` [PATCH RFC 23/25] net: Move genl_pernet_ops " Kirill Tkhai
2017-11-17 18:31 ` [PATCH RFC 24/25] net: Move wext_pernet_ops " Kirill Tkhai
2017-11-17 18:31 ` [PATCH RFC 25/25] net: Move sysctl_core_ops " Kirill Tkhai
2017-11-19  1:52 ` [PATCH RFC 00/25] Replacing net_mutex with rw_semaphore Eric W. Biederman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=151094330120.20009.9804877551209771323.stgit@localhost.localdomain \
    --to=ktkhai@virtuozzo.com \
    --cc=adobriyan@gmail.com \
    --cc=avagin@virtuozzo.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@gmail.com \
    --cc=ebiederm@xmission.com \
    --cc=eric.dumazet@gmail.com \
    --cc=fw@strlen.de \
    --cc=gorcunov@virtuozzo.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=kstewart@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=mark.rutland@arm.com \
    --cc=mschiffer@universe-factory.net \
    --cc=netdev@vger.kernel.org \
    --cc=nicolas.dichtel@6wind.com \
    --cc=paul@paul-moore.com \
    --cc=pombredanne@nexb.com \
    --cc=roman.kapl@sysgo.com \
    --cc=rshearma@brocade.com \
    --cc=stephen@networkplumber.org \
    --cc=vyasevic@redhat.com \
    --cc=vyasevich@gmail.com \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox