Skip to contentSkip to conversation

network namespace locking rules

PATCH In review Denis V. Lunev 4 participants 5 messages Sep 28, 2007 3 messages not in the archive Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 network namespace locking rules

Current locking for network namespace list/initialization is broken. for_each_net is called under single rtnl_lock in register_netdevice_notifier.

Locking:

    net_mutex -> rtnl_lock() -> dev_base_lock

Reasoning:
- net_mutex holds serialization of the addition/removal of subsystems/modules and the creation/destruction of network namespaces as a whole
- loopback device is one of such subsystems and it takes rtnl_lock inside
- per/namespace RTNL netlink socket requires an iteration over namespace list inside rtnl_unlock, which is called inside net_mutex
Resume:
net_namespace_list is guarded by both rtnl_lock & net_mutex and can be safely iterated under any of them

--------

include/net/net_namespace.h +15 -2
index b8186ea..2845992 100644
@@ -174,8 +174,21 @@ static inline void release_net(struct net *net)
 	atomic_dec(&net->use_count);
 }
 
-extern void net_lock(void);
-extern void net_unlock(void);
+/*
+ * Locking:
+ *     net_mutex -> rtnl_lock() -> dev_base_lock
+ * Reasoning:
+ *   - net_mutex holds serialization of the addition/removal of
+ *     subsystems/modules and the creation/destruction of network
+ *     namespaces as a whole
+ *   - loopback device is one of such subsystems and it takes
+ *     rtnl_lock inside
+ *   - per/namespace RTNL netlink socket requires an iteration over
+ *     namespace list inside rtnl_unlock, which is called inside net_mutex
+ * Resume:
+ *     net_namespace_list is guarded by both rtnl_lock & net_mutex and
+ *     can be safely iterated under any of them
+ */
 
 #define for_each_net(VAR)				\
 	list_for_each_entry(VAR, &net_namespace_list, list)
net/core/net_namespace.c +7 -17
index 026e39a..07682a2 100644
@@ -10,6 +10,7 @@
 
 /*
  *	Our network namespace constructor/destructor lists
+ *	Locking rules are described in details in include/net/net_namespace.h
  */
 
 static LIST_HEAD(pernet_list);
@@ -24,16 +25,6 @@ static struct kmem_cache *net_cachep;
 struct net init_net;
 EXPORT_SYMBOL_GPL(init_net);
 
-void net_lock(void)
-{
-	mutex_lock(&net_list_mutex);
-}
-
-void net_unlock(void)
-{
-	mutex_unlock(&net_list_mutex);
-}
-
 static struct net *net_alloc(void)
 {
 	return kmem_cache_alloc(net_cachep, GFP_KERNEL);
@@ -71,9 +62,9 @@ static void cleanup_net(struct work_struct *work)
 	mutex_lock(&net_mutex);
 
 	/* Don't let anyone else find us. */
-	net_lock();
+	rtnl_lock();
 	list_del(&net->list);
-	net_unlock();
+	rtnl_unlock();
 
 	/* Run all of the network namespace exit methods */
 	pernet_count = 0;
@@ -193,9 +184,9 @@ struct net *copy_net_ns(unsigned long flags, struct net *old_net)
 	if (err)
 		goto out_unlock;
 
-	net_lock();
+	rtnl_lock();
 	list_add_tail(&new_net->list, &net_namespace_list);
-	net_unlock();
+	rtnl_unlock();
 
 
 out_unlock:
@@ -220,14 +211,13 @@ static int __init net_ns_init(void)
 	mutex_lock(&net_mutex);
 	err = setup_net(&init_net);
 
-	net_lock();
+	rtnl_lock();
 	list_add_tail(&init_net.list, &net_namespace_list);
-	net_unlock();
+	rtnl_unlock();
 
 	mutex_unlock(&net_mutex);
 	if (err)
 		panic("Could not setup the initial network namespace");
-
 	return 0;
 }
 
net/core/rtnetlink.c +13 -5
index 82ebc23..e610313 100644
@@ -73,16 +73,24 @@ void __rtnl_unlock(void)
 void rtnl_unlock(void)
 {
 	struct net *net;
-	mutex_unlock(&rtnl_mutex);
-	
-	net_lock();
+
+retry:
 	for_each_net(net) {
 		struct sock *rtnl = net->rtnl;
+
+		if (rtnl == NULL || rtnl->sk_receive_queue.qlen == 0)
+			continue;
+
+		get_net(net);
+		mutex_unlock(&rtnl_mutex);
 		if (rtnl && rtnl->sk_receive_queue.qlen)
 			rtnl->sk_data_ready(rtnl, 0);
-	}
-	net_unlock();
+		mutex_lock(&rtnl_mutex);
+		put_net(net);
 
+		goto retry;
+	}
+	mutex_unlock(&rtnl_mutex);
 	netdev_run_todo();
 }

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 21 lines · Denis V. Lunev

Current locking for network namespace list/initialization is broken. for_each_net is called under single rtnl_lock in register_netdevice_notifier.

Locking:

    net_mutex -> rtnl_lock() -> dev_base_lock

Reasoning:
- net_mutex holds serialization of the addition/removal of subsystems/modules and the creation/destruction of network namespaces as a whole
- loopback device is one of such subsystems and it takes rtnl_lock inside
- per/namespace RTNL netlink socket requires an iteration over namespace list inside rtnl_unlock, which is called inside net_mutex
Resume:
net_namespace_list is guarded by both rtnl_lock & net_mutex and can be safely iterated under any of them

--------

[ cut ]

net/core/net_namespace.c static struct kmem_cache *net_cachep; · line 36

Quoted 11 lines
net/core/net_namespace.c +0 -10
-void net_lock(void)
-{
-	mutex_lock(&net_list_mutex);
-}
-
-void net_unlock(void)
-{
-	mutex_unlock(&net_list_mutex);
-}
-

net_list_mutex is no more needed, right ? The removing of its declaration is missing (already done in #netns49)

Quoted 34 lines

static struct net *net_alloc(void)
{

net/core/net_namespace.c +5 -5
 	return kmem_cache_alloc(net_cachep, GFP_KERNEL);
@@ -71,9 +62,9 @@ static void cleanup_net(struct work_struct *work)
 	mutex_lock(&net_mutex);
 
 	/* Don't let anyone else find us. */
-	net_lock();
+	rtnl_lock();
 	list_del(&net->list);
-	net_unlock();
+	rtnl_unlock();
 
 	/* Run all of the network namespace exit methods */
 	pernet_count = 0;
@@ -193,9 +184,9 @@ struct net *copy_net_ns(unsigned long flags, struct net *old_net)
 	if (err)
 		goto out_unlock;
 
-	net_lock();
+	rtnl_lock();
 	list_add_tail(&new_net->list, &net_namespace_list);
-	net_unlock();
+	rtnl_unlock();
 
 
 out_unlock:
@@ -220,14 +211,13 @@ static int __init net_ns_init(void)
 	mutex_lock(&net_mutex);
 	err = setup_net(&init_net);
 
-	net_lock();
+	rtnl_lock();
 	list_add_tail(&init_net.list, &net_namespace_list);

net/core/net_namespace.c static int __init net_ns_init(void) · line 223

Quoted 10 lines
net/core/net_namespace.c +1 -2
-	net_unlock();
+	rtnl_unlock();
 
 	mutex_unlock(&net_mutex);
 	if (err)
 		panic("Could not setup the initial network namespace");
-
 	return 0;
 }
 

Eric did already these changes in #netns49.

Quoted 12 lines
net/core/rtnetlink.c +1 -3
index 82ebc23..e610313 100644
@@ -73,16 +73,24 @@ void __rtnl_unlock(void)
 void rtnl_unlock(void)
 {
 	struct net *net;
-	mutex_unlock(&rtnl_mutex);
-	
-	net_lock();
+

net/core/rtnetlink.c void __rtnl_unlock(void) · line 86

Quoted 10 lines
net/core/rtnetlink.c +7 -0
+retry:
 	for_each_net(net) {
 		struct sock *rtnl = net->rtnl;
+
+		if (rtnl == NULL || rtnl->sk_receive_queue.qlen == 0)
+			continue;
+
+		get_net(net);
+		mutex_unlock(&rtnl_mutex);
 		if (rtnl && rtnl->sk_receive_queue.qlen)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

this statement is always true with the previous one.

net/core/rtnetlink.c void __rtnl_unlock(void) · line 91

Quoted 7 lines
net/core/rtnetlink.c +3 -2
 			rtnl->sk_data_ready(rtnl, 0);
-	}
-	net_unlock();
+		mutex_lock(&rtnl_mutex);
+		put_net(net);
 
+		goto retry;

Why do you need to return to the beginning of the list ?

net/core/rtnetlink.c void __rtnl_unlock(void) · line 95

Quoted 4 lines
net/core/rtnetlink.c +2 -0
+	}
+	mutex_unlock(&rtnl_mutex);
 	netdev_run_todo();
 }

Denis, can you explain why this part must be modified ?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Daniel, I've sent you last letter from Eric regarding change below. By the way, you've been in CC: for that thread :)

I'll rebase this to #49 on monday.

Regards,
Den

Quoted 132 lines · Daniel Lezcano
Quoted 21 lines · Denis V. Lunev

Current locking for network namespace list/initialization is broken. for_each_net is called under single rtnl_lock in register_netdevice_notifier.

Locking:

    net_mutex -> rtnl_lock() -> dev_base_lock

Reasoning:
- net_mutex holds serialization of the addition/removal of subsystems/modules and the creation/destruction of network namespaces as a whole
- loopback device is one of such subsystems and it takes rtnl_lock inside
- per/namespace RTNL netlink socket requires an iteration over namespace list inside rtnl_unlock, which is called inside net_mutex
Resume:
net_namespace_list is guarded by both rtnl_lock & net_mutex and can be safely iterated under any of them

--------

[ cut ]

net/core/net_namespace.c static struct kmem_cache *net_cachep; · line 36

Quoted 11 lines
net/core/net_namespace.c +0 -10
-void net_lock(void)
-{
-    mutex_lock(&net_list_mutex);
-}
-
-void net_unlock(void)
-{
-    mutex_unlock(&net_list_mutex);
-}
-

net_list_mutex is no more needed, right ? The removing of its declaration is missing (already done in #netns49)

Quoted 35 lines

static struct net *net_alloc(void)

 {
     return kmem_cache_alloc(net_cachep, GFP_KERNEL);
net/core/net_namespace.c +4 -4
@@ -71,9 +62,9 @@ static void cleanup_net(struct work_struct *work)
     mutex_lock(&net_mutex);
 
     /* Don't let anyone else find us. */
-    net_lock();
+    rtnl_lock();
     list_del(&net->list);
-    net_unlock();
+    rtnl_unlock();
 
     /* Run all of the network namespace exit methods */
     pernet_count = 0;
@@ -193,9 +184,9 @@ struct net *copy_net_ns(unsigned long flags,
 struct net *old_net)
     if (err)
         goto out_unlock;
 
-    net_lock();
+    rtnl_lock();
     list_add_tail(&new_net->list, &net_namespace_list);
-    net_unlock();
+    rtnl_unlock();
 
 

out_unlock:

net/core/net_namespace.c +1 -1
@@ -220,14 +211,13 @@ static int __init net_ns_init(void)
     mutex_lock(&net_mutex);
     err = setup_net(&init_net);
 
-    net_lock();
+    rtnl_lock();
     list_add_tail(&init_net.list, &net_namespace_list);

net/core/net_namespace.c static int __init net_ns_init(void) · line 223

Quoted 10 lines
net/core/net_namespace.c +1 -2
-    net_unlock();
+    rtnl_unlock();
 
     mutex_unlock(&net_mutex);
     if (err)
         panic("Could not setup the initial network namespace");
-
     return 0;
 }
 

Eric did already these changes in #netns49.

Quoted 12 lines
net/core/rtnetlink.c +1 -3
index 82ebc23..e610313 100644
@@ -73,16 +73,24 @@ void __rtnl_unlock(void)
 void rtnl_unlock(void)
 {
     struct net *net;
-    mutex_unlock(&rtnl_mutex);
-   
-    net_lock();
+

net/core/rtnetlink.c void __rtnl_unlock(void) · line 86

Quoted 10 lines
net/core/rtnetlink.c +7 -0
+retry:
     for_each_net(net) {
         struct sock *rtnl = net->rtnl;
+
+        if (rtnl == NULL || rtnl->sk_receive_queue.qlen == 0)
+            continue;
+
+        get_net(net);
+        mutex_unlock(&rtnl_mutex);
         if (rtnl && rtnl->sk_receive_queue.qlen)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

this statement is always true with the previous one.

net/core/rtnetlink.c void __rtnl_unlock(void) · line 91

Quoted 7 lines
net/core/rtnetlink.c +3 -2
             rtnl->sk_data_ready(rtnl, 0);
-    }
-    net_unlock();
+        mutex_lock(&rtnl_mutex);
+        put_net(net);
 
+        goto retry;

Why do you need to return to the beginning of the list ?

net/core/rtnetlink.c void __rtnl_unlock(void) · line 95

Quoted 4 lines
net/core/rtnetlink.c +2 -0
+    }
+    mutex_unlock(&rtnl_mutex);
     netdev_run_todo();
 }

Denis, can you explain why this part must be modified ?

#5 lore raw
In reply to a message not in the archive
Quoted 3 lines · Denis V. Lunev

Current locking for network namespace list/initialization is broken. for_each_net is called under single rtnl_lock in register_netdevice_notifier.

As of 984e617a3e1974022b8f671427a76ffbe886f75b this issue has been addressed in net-2.6.24

The only remaining part to address is rtnl_unlock().

My current hypothesis is that rtnl_unlock() only needs to process the packets that are queued while we had the rtnl_lock held, to maintain the current semantics.

So the retry may not be necessary as it may be possible to prove that the only extra packets that could come in come from another thread taking the rtnl_lock and they will those packets in their rtnl_unlock() if we don't.

I will review this later today, and add the retry if necessary.

One way or another I think we agree on how to get the locking correct. The other details are a bit tricky but look usable.

Eric

A message that is not in the archive (sent off-list or not yet received): m1d4w2d92m.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#7 lore raw
Quoted 20 lines · Eric W. Biederman
Quoted 3 lines · Denis V. Lunev

Current locking for network namespace list/initialization is broken. for_each_net is called under single rtnl_lock in register_netdevice_notifier.

As of 984e617a3e1974022b8f671427a76ffbe886f75b this issue has been addressed in net-2.6.24

The only remaining part to address is rtnl_unlock().

My current hypothesis is that rtnl_unlock() only needs to process the packets that are queued while we had the rtnl_lock held, to maintain the current semantics.

So the retry may not be necessary as it may be possible to prove that the only extra packets that could come in come from another thread taking the rtnl_lock and they will those packets in their rtnl_unlock() if we don't.

Quoted 7 lines

I will review this later today, and add the retry if necessary.

One way or another I think we agree on how to get the locking correct. The other details are a bit tricky but look usable.

Eric

Unfortunately, the answer is 'no'. data_ready callback takes rtnl inside, so we can have new packets from other namespaces during the processing.

No way, but restart :(

Regards,
Den