Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next-2.6] net: make sure rtnl is held in rtnl_fill_ifinfo()
@ 2011-05-18  9:35 Eric Dumazet
  2011-05-18 16:02 ` Eric Dumazet
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Dumazet @ 2011-05-18  9:35 UTC (permalink / raw)
  To: David Miller; +Cc: netdev

Commit e67f88dd12f610 (net: dont hold rtnl mutex during netlink dump
callbacks) added a problem in rtnl_fill_ifinfo() not being always called
with RTNL held, which is racy.

1) This patch extends rtnl_mutex helper functions so that :

rtnl_lock() is able to BUG_ON() if recursively called.
[This was only provided if LOCKDEP was on]

rtnl_is_locked() is able to check if current thread owns rtnl_mutex
[ASSERT_RTNL() gets this added feature too]

2) Add one ASSERT_RTNL() in rtnl_fill_ifinfo()

3) Make sure rtnl is held in rtnl_dump_ifinfo()

Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
---
 net/core/rtnetlink.c |   19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index d2ba259..4e09f70 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -59,15 +59,19 @@ struct rtnl_link {
 };
 
 static DEFINE_MUTEX(rtnl_mutex);
+static struct thread_info  *rtnl_owner;
 
 void rtnl_lock(void)
 {
+	BUG_ON(rtnl_owner == current_thread_info());
 	mutex_lock(&rtnl_mutex);
+	rtnl_owner = current_thread_info();
 }
 EXPORT_SYMBOL(rtnl_lock);
 
 void __rtnl_unlock(void)
 {
+	rtnl_owner = NULL;
 	mutex_unlock(&rtnl_mutex);
 }
 
@@ -80,13 +84,17 @@ EXPORT_SYMBOL(rtnl_unlock);
 
 int rtnl_trylock(void)
 {
-	return mutex_trylock(&rtnl_mutex);
+	int ret = mutex_trylock(&rtnl_mutex);
+
+	if (ret)
+		rtnl_owner = current_thread_info();
+	return ret;
 }
 EXPORT_SYMBOL(rtnl_trylock);
 
 int rtnl_is_locked(void)
 {
-	return mutex_is_locked(&rtnl_mutex);
+	return mutex_is_locked(&rtnl_mutex) && rtnl_owner == current_thread_info();
 }
 EXPORT_SYMBOL(rtnl_is_locked);
 
@@ -850,6 +858,7 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
 	struct nlattr *attr, *af_spec;
 	struct rtnl_af_ops *af_ops;
 
+	ASSERT_RTNL();
 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
 	if (nlh == NULL)
 		return -EMSGSIZE;
@@ -1003,10 +1012,12 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 	struct net_device *dev;
 	struct hlist_head *head;
 	struct hlist_node *node;
+	int rtnl_was_locked = rtnl_is_locked();
 
 	s_h = cb->args[0];
 	s_idx = cb->args[1];
-
+	if (!rtnl_was_locked)
+		rtnl_lock();
 	rcu_read_lock();
 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
 		idx = 0;
@@ -1025,6 +1036,8 @@ cont:
 	}
 out:
 	rcu_read_unlock();
+	if (!rtnl_was_locked)
+		rtnl_unlock();
 	cb->args[1] = idx;
 	cb->args[0] = h;
 



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-05-18 16:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-18  9:35 [PATCH net-next-2.6] net: make sure rtnl is held in rtnl_fill_ifinfo() Eric Dumazet
2011-05-18 16:02 ` Eric Dumazet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox