Skip to contentSkip to conversation

netns: correct gfp flags for alloc_netid()

PATCH In review Cong Wang 3 participants 5 messages Feb 27, 2015

Subsystems (2)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 netns: correct gfp flags for alloc_netid()

This fixes the following kernel warning:

===============================
[ INFO: suspicious RCU usage. ]
3.19.0+ #805 Tainted: G W
-------------------------------
include/linux/rcupdate.h:538 Illegal context switch in RCU read-side critical section!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 0
2 locks held by ip/771:
#0: (rtnl_mutex){+.+.+.}, at: [<ffffffff8182b8f4>] netlink_dump+0x21/0x26c #1: (rcu_read_lock){......}, at: [<ffffffff817d785b>] rcu_read_lock+0x0/0x6e

 stack backtrace:
 CPU: 3 PID: 771 Comm: ip Tainted: G        W       3.19.0+ #805
 Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
  0000000000000001 ffff8800d51e7718 ffffffff81a27457 0000000029e729e6
  ffff8800d6108000 ffff8800d51e7748 ffffffff810b539b ffffffff820013dd
  00000000000001c8 0000000000000000 ffff8800d7448088 ffff8800d51e7758
 Call Trace:
  [<ffffffff81a27457>] dump_stack+0x4c/0x65
  [<ffffffff810b539b>] lockdep_rcu_suspicious+0x107/0x110
  [<ffffffff8109796f>] rcu_preempt_sleep_check+0x45/0x47
  [<ffffffff8109e457>] ___might_sleep+0x1d/0x1cb
  [<ffffffff8109e67d>] __might_sleep+0x78/0x80
  [<ffffffff814b9b1f>] idr_alloc+0x45/0xd1
  [<ffffffff810cb7ab>] ? rcu_read_lock_held+0x3b/0x3d
  [<ffffffff814b9f9d>] ? idr_for_each+0x53/0x101
  [<ffffffff817c1383>] alloc_netid+0x61/0x69
  [<ffffffff817c14c3>] __peernet2id+0x79/0x8d
  [<ffffffff817c1ab7>] peernet2id+0x13/0x1f
  [<ffffffff817d8673>] rtnl_fill_ifinfo+0xa8d/0xc20
  [<ffffffff810b17d9>] ? __lock_is_held+0x39/0x52
  [<ffffffff817d894f>] rtnl_dump_ifinfo+0x149/0x213
  [<ffffffff8182b9c2>] netlink_dump+0xef/0x26c
  [<ffffffff8182bcba>] netlink_recvmsg+0x17b/0x2c5
  [<ffffffff817b0adc>] __sock_recvmsg+0x4e/0x59
  [<ffffffff817b1b40>] sock_recvmsg+0x3f/0x51
  [<ffffffff817b1f9a>] ___sys_recvmsg+0xf6/0x1d9
  [<ffffffff8115dc67>] ? handle_pte_fault+0x6e1/0xd3d
  [<ffffffff8100a3a0>] ? native_sched_clock+0x35/0x37
  [<ffffffff8109f45b>] ? sched_clock_local+0x12/0x72
  [<ffffffff8109f6ac>] ? sched_clock_cpu+0x9e/0xb7
  [<ffffffff810cb7ab>] ? rcu_read_lock_held+0x3b/0x3d
  [<ffffffff811abde8>] ? __fcheck_files+0x4c/0x58
  [<ffffffff811ac556>] ? __fget_light+0x2d/0x52
  [<ffffffff817b376f>] __sys_recvmsg+0x42/0x60
  [<ffffffff817b379f>] SyS_recvmsg+0x12/0x1c
Fixes commit 0c7aecd4bde4b7302 ("netns: add rtnl cmd to add and get peer netns ids")
Cc Nicolas Dichtel <[email protected]>

---

net/core/net_namespace.c +5 -4
index cb5290b..ecf8e70 100644
@@ -148,7 +148,8 @@ static void ops_free_list(const struct pernet_operations *ops,
 	}
 }
 
-static int alloc_netid(struct net *net, struct net *peer, int reqid)
+static int alloc_netid(struct net *net, struct net *peer, int reqid,
+		       gfp_t flags)
 {
 	int min = 0, max = 0;
 
@@ -159,7 +160,7 @@ static int alloc_netid(struct net *net, struct net *peer, int reqid)
 		max = reqid + 1;
 	}
 
-	return idr_alloc(&net->netns_ids, peer, min, max, GFP_KERNEL);
+	return idr_alloc(&net->netns_ids, peer, min, max, flags);
 }
 
 /* This function is used by idr_for_each(). If net is equal to peer, the
@@ -188,7 +189,7 @@ static int __peernet2id(struct net *net, struct net *peer, bool alloc)
 		return id;
 
 	if (alloc)
-		return alloc_netid(net, peer, -1);
+		return alloc_netid(net, peer, -1, GFP_ATOMIC);
 
 	return -ENOENT;
 }
@@ -524,7 +525,7 @@ static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh)
 		goto out;
 	}
 
-	err = alloc_netid(net, peer, nsid);
+	err = alloc_netid(net, peer, nsid, GFP_KERNEL);
 	if (err > 0)
 		err = 0;
 out:
#1 lore raw
Quoted 10 lines · Cong Wang

This fixes the following kernel warning:

===============================
[ INFO: suspicious RCU usage. ]
3.19.0+ #805 Tainted: G W
-------------------------------
include/linux/rcupdate.h:538 Illegal context switch in RCU read-side critical section!

other info that might help us debug this:

I do not think this is the right way to fix this.

Bug is that rtnl_dump_ifinfo() should not use rcu_read_lock(), as RTNL is held : GFP_KERNEL allocations in control path should be the golden rule.

I guess I should have reverted e67f88dd12f6 completely instead of the partial revert (2907c35ff647080)

author
#2 lore raw
Quoted 12 lines · Eric Dumazet
Quoted 10 lines · Cong Wang

This fixes the following kernel warning:

===============================
[ INFO: suspicious RCU usage. ]
3.19.0+ #805 Tainted: G W
-------------------------------
include/linux/rcupdate.h:538 Illegal context switch in RCU read-side critical section!

other info that might help us debug this:

Quoted 8 lines

I do not think this is the right way to fix this.

Bug is that rtnl_dump_ifinfo() should not use rcu_read_lock(), as RTNL is held : GFP_KERNEL allocations in control path should be the golden rule.

I guess I should have reverted e67f88dd12f6 completely instead of the partial revert (2907c35ff647080)

LOL, it has been changed back and forth.

I thought rcu read lock was introduced to optimize dumping performance, if it were really important, we should solve the deadlock you mentioned in commit 2907c35ff647080?

Otherwise, as you said, completely revert to using rtnl lock.

Or for now just use my patch since idr doesn't allocate that much memory in atomic?

#3 lore raw
Quoted 8 lines · Cong Wang

LOL, it has been changed back and forth.

I thought rcu read lock was introduced to optimize dumping performance, if it were really important, we should solve the deadlock you mentioned in commit 2907c35ff647080?

Otherwise, as you said, completely revert to using rtnl lock.

Dumping is so fat nowadays, it makes sense to remove rcu_read_lock() to allow cond_resched() eventually.

rcu_read_lock() made sense _if_ RTNL mutex could be avoided, but unfortunately this could not be done easily.

Please try this patch instead :

net/core/rtnetlink.c +1 -3
index 1385de0fa080..74ce25d2cded 100644
@@ -1300,7 +1300,6 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 	s_h = cb->args[0];
 	s_idx = cb->args[1];
 
-	rcu_read_lock();
 	cb->seq = net->dev_base_seq;
 
 	/* A hack to preserve kernel<->userspace interface.
@@ -1322,7 +1321,7 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
 		idx = 0;
 		head = &net->dev_index_head[h];
-		hlist_for_each_entry_rcu(dev, head, index_hlist) {
+		hlist_for_each_entry(dev, head, index_hlist) {
 			if (idx < s_idx)
 				goto cont;
 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
@@ -1344,7 +1343,6 @@ cont:
 		}
 	}
 out:
-	rcu_read_unlock();
 	cb->args[1] = idx;
 	cb->args[0] = h;
 
#4 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 7 lines · Cong Wang

LOL, it has been changed back and forth.

I thought rcu read lock was introduced to optimize dumping performance, if it were really important, we should solve the deadlock you mentioned in commit 2907c35ff647080?

Quoted 7 lines
Quoted 1 line

Otherwise, as you said, completely revert to using rtnl lock.

Dumping is so fat nowadays, it makes sense to remove rcu_read_lock() to allow cond_resched() eventually.

rcu_read_lock() made sense _if_ RTNL mutex could be avoided, but unfortunately this could not be done easily.

Thinking a bit more, in this specific case, we don't have to allocate memory in dumping. I am testing the attached patch.

Quoted 3 lines

Please try this patch instead :

It should fix the warning, I can test it tomorrow.

Thanks.

include/net/net_namespace.h +13 -1
index 36faf49..25ac2e0 100644
@@ -27,6 +27,7 @@
 #include <net/netns/nftables.h>
 #include <net/netns/xfrm.h>
 #include <linux/ns_common.h>
+#include <uapi/linux/net_namespace.h>
 
 struct user_namespace;
 struct proc_dir_entry;
@@ -291,7 +292,18 @@ static inline struct net *read_pnet(struct net * const *pnet)
 #define __net_initconst	__initconst
 #endif
 
-int peernet2id(struct net *net, struct net *peer);
+int __peernet2id(struct net *net, struct net *peer, bool alloc);
+
+/* This function returns the id of a peer netns. If no id is assigned, one will
+ * be allocated and returned.
+ */
+static inline int peernet2id(struct net *net, struct net *peer)
+{
+	int id = __peernet2id(net, peer, true);
+
+	return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
+}
+
 struct net *get_net_ns_by_id(struct net *net, int id);
 
 struct pernet_operations {
net/core/net_namespace.c +2 -12
index cb5290b..9ff2164 100644
@@ -175,7 +175,7 @@ static int net_eq_idr(int id, void *net, void *peer)
 	return 0;
 }
 
-static int __peernet2id(struct net *net, struct net *peer, bool alloc)
+int __peernet2id(struct net *net, struct net *peer, bool alloc)
 {
 	int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
 
@@ -192,17 +192,7 @@ static int __peernet2id(struct net *net, struct net *peer, bool alloc)
 
 	return -ENOENT;
 }
-
-/* This function returns the id of a peer netns. If no id is assigned, one will
- * be allocated and returned.
- */
-int peernet2id(struct net *net, struct net *peer)
-{
-	int id = __peernet2id(net, peer, true);
-
-	return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
-}
-EXPORT_SYMBOL(peernet2id);
+EXPORT_SYMBOL(__peernet2id);
 
 struct net *get_net_ns_by_id(struct net *net, int id)
 {
net/core/rtnetlink.c +10 -2
index 1385de0..bd41427 100644
@@ -1175,8 +1175,10 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
 
 		if (!net_eq(dev_net(dev), link_net)) {
-			int id = peernet2id(dev_net(dev), link_net);
+			int id = __peernet2id(dev_net(dev), link_net, false);
 
+			if (id < 0)
+				id = NETNSA_NSID_NOT_ASSIGNED;
 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
 				goto nla_put_failure;
 		}
@@ -2142,7 +2144,13 @@ replay:
 		dev->ifindex = ifm->ifi_index;
 
 		if (ops->newlink) {
-			err = ops->newlink(link_net ? : net, dev, tb, data);
+			struct net *src_net = link_net ?: net;
+
+			if (ops->get_link_net && !net_eq(src_net, dev_net(dev)))
+				/* We don't care the return value */
+				(void) peernet2id(dev_net(dev), src_net);
+
+			err = ops->newlink(src_net, dev, tb, data);
 			/* Drivers should call free_netdev() in ->destructor
 			 * and unregister it on failure after registration
 			 * so that device could be finally freed in rtnl_unlock.