[PATCH 3/7] netpoll: Fix RCU usage

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Herbert Xu
Date: Thursday, June 10, 2010 - 5:42 am

netpoll: Fix RCU usage

The use of RCU in netpoll is incorrect in a number of places:

1) The initial setting is lacking a write barrier.
2) The synchronize_rcu is in the wrong place.
3) Read barriers are missing.
4) Some places are even missing rcu_read_lock.
5) npinfo is zeroed after freeing.

This patch fixes those issues.  As most users are in BH context,
this also converts the RCU usage to the BH variant.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 include/linux/netpoll.h |   13 ++++++++-----
 net/core/netpoll.c      |   20 ++++++++++++--------
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/include/linux/netpoll.h b/include/linux/netpoll.h
index e9e2312..95c9f7e 100644
--- a/include/linux/netpoll.h
+++ b/include/linux/netpoll.h
@@ -57,12 +57,15 @@ void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb);
 #ifdef CONFIG_NETPOLL
 static inline bool netpoll_rx(struct sk_buff *skb)
 {
-	struct netpoll_info *npinfo = skb->dev->npinfo;
+	struct netpoll_info *npinfo;
 	unsigned long flags;
 	bool ret = false;
 
+	rcu_read_lock_bh();
+	npinfo = rcu_dereference(skb->dev->npinfo);
+
 	if (!npinfo || (list_empty(&npinfo->rx_np) && !npinfo->rx_flags))
-		return false;
+		goto out;
 
 	spin_lock_irqsave(&npinfo->rx_lock, flags);
 	/* check rx_flags again with the lock held */
@@ -70,12 +73,14 @@ static inline bool netpoll_rx(struct sk_buff *skb)
 		ret = true;
 	spin_unlock_irqrestore(&npinfo->rx_lock, flags);
 
+out:
+	rcu_read_unlock_bh();
 	return ret;
 }
 
 static inline int netpoll_rx_on(struct sk_buff *skb)
 {
-	struct netpoll_info *npinfo = skb->dev->npinfo;
+	struct netpoll_info *npinfo = rcu_dereference(skb->dev->npinfo);
 
 	return npinfo && (!list_empty(&npinfo->rx_np) || npinfo->rx_flags);
 }
@@ -91,7 +96,6 @@ static inline void *netpoll_poll_lock(struct napi_struct *napi)
 {
 	struct net_device *dev = napi->dev;
 
-	rcu_read_lock(); /* deal with race on ->npinfo */
 	if (dev && dev->npinfo) {
 		spin_lock(&napi->poll_lock);
 		napi->poll_owner = smp_processor_id();
@@ -108,7 +112,6 @@ static inline void netpoll_poll_unlock(void *have)
 		napi->poll_owner = -1;
 		spin_unlock(&napi->poll_lock);
 	}
-	rcu_read_unlock();
 }
 
 #else
diff --git a/net/core/netpoll.c b/net/core/netpoll.c
index 748c930..4b7623d 100644
--- a/net/core/netpoll.c
+++ b/net/core/netpoll.c
@@ -292,6 +292,7 @@ void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
 	unsigned long tries;
 	struct net_device *dev = np->dev;
 	const struct net_device_ops *ops = dev->netdev_ops;
+	/* It is up to the caller to keep npinfo alive. */
 	struct netpoll_info *npinfo = np->dev->npinfo;
 
 	if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
@@ -841,10 +842,7 @@ int netpoll_setup(struct netpoll *np)
 	refill_skbs();
 
 	/* last thing to do is link it to the net device structure */
-	ndev->npinfo = npinfo;
-
-	/* avoid racing with NAPI reading npinfo */
-	synchronize_rcu();
+	rcu_assign_pointer(ndev->npinfo, npinfo);
 
 	return 0;
 
@@ -888,6 +886,16 @@ void netpoll_cleanup(struct netpoll *np)
 
 			if (atomic_dec_and_test(&npinfo->refcnt)) {
 				const struct net_device_ops *ops;
+
+				ops = np->dev->netdev_ops;
+				if (ops->ndo_netpoll_cleanup)
+					ops->ndo_netpoll_cleanup(np->dev);
+
+				rcu_assign_pointer(np->dev->npinfo, NULL);
+
+				/* avoid racing with NAPI reading npinfo */
+				synchronize_rcu_bh();
+
 				skb_queue_purge(&npinfo->arp_tx);
 				skb_queue_purge(&npinfo->txq);
 				cancel_rearming_delayed_work(&npinfo->tx_work);
@@ -895,10 +903,6 @@ void netpoll_cleanup(struct netpoll *np)
 				/* clean after last, unfinished work */
 				__skb_queue_purge(&npinfo->txq);
 				kfree(npinfo);
-				ops = np->dev->netdev_ops;
-				if (ops->ndo_netpoll_cleanup)
-					ops->ndo_netpoll_cleanup(np->dev);
-				np->dev->npinfo = NULL;
 			}
 		}
 
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[0/8] netpoll/bridge fixes, Herbert Xu, (Thu Jun 10, 5:40 am)
[PATCH 3/7] netpoll: Fix RCU usage, Herbert Xu, (Thu Jun 10, 5:42 am)
[PATCH 5/7] netpoll: Add ndo_netpoll_setup, Herbert Xu, (Thu Jun 10, 5:42 am)
[PATCH 7/7] bridge: Fix netpoll support, Herbert Xu, (Thu Jun 10, 5:42 am)
Re: [0/8] netpoll/bridge fixes, Stephen Hemminger, (Thu Jun 10, 7:49 am)
Re: [0/8] netpoll/bridge fixes, Herbert Xu, (Thu Jun 10, 2:56 pm)
Re: [0/8] netpoll/bridge fixes, Stephen Hemminger, (Thu Jun 10, 2:59 pm)
Re: [0/8] netpoll/bridge fixes, Herbert Xu, (Thu Jun 10, 3:48 pm)
Re: [0/8] netpoll/bridge fixes, Herbert Xu, (Thu Jun 10, 7:11 pm)
[PATCH 3/8] netpoll: Fix RCU usage, Herbert Xu, (Thu Jun 10, 7:12 pm)
[PATCH 5/8] netpoll: Add ndo_netpoll_setup, Herbert Xu, (Thu Jun 10, 7:12 pm)
[PATCH 7/8] netpoll: Add netpoll_tx_running, Herbert Xu, (Thu Jun 10, 7:12 pm)
[PATCH 8/8] bridge: Fix netpoll support, Herbert Xu, (Thu Jun 10, 7:12 pm)
fired a bug report on bugzilla.redhat.com, Qianfeng Zhang, (Thu Jun 10, 8:08 pm)
Re: [0/8] netpoll/bridge fixes, Matt Mackall, (Fri Jun 11, 1:03 pm)
Re: [PATCH 3/8] netpoll: Fix RCU usage, Paul E. McKenney, (Fri Jun 11, 4:10 pm)
Re: [0/8] netpoll/bridge fixes, Cong Wang, (Tue Jun 15, 3:17 am)
Re: [PATCH 8/8] bridge: Fix netpoll support, Cong Wang, (Tue Jun 15, 3:28 am)
Re: [0/8] netpoll/bridge fixes, David Miller, (Tue Jun 15, 11:39 am)
Re: [0/8] netpoll/bridge fixes, Eric Dumazet, (Tue Jun 15, 7:58 pm)
Re: [0/8] netpoll/bridge fixes, Eric Dumazet, (Tue Jun 15, 8:03 pm)
Re: [0/8] netpoll/bridge fixes, Herbert Xu, (Tue Jun 15, 8:33 pm)
Re: [0/8] netpoll/bridge fixes, David Miller, (Tue Jun 15, 9:47 pm)
Re: [0/8] netpoll/bridge fixes, Paul E. McKenney, (Tue Jun 15, 10:08 pm)
Re: [0/8] netpoll/bridge fixes, Eric Dumazet, (Tue Jun 15, 11:16 pm)
Re: [0/8] netpoll/bridge fixes, Eric Dumazet, (Tue Jun 15, 11:21 pm)
Re: [0/8] netpoll/bridge fixes, Paul E. McKenney, (Wed Jun 16, 9:01 am)
Re: [0/8] netpoll/bridge fixes, Paul E. McKenney, (Wed Jun 16, 4:02 pm)
Re: [0/8] netpoll/bridge fixes, Michael S. Tsirkin, (Thu Jun 17, 3:18 am)
Re: [PATCH 8/8] bridge: Fix netpoll support, Herbert Xu, (Thu Jun 17, 3:38 am)
Re: [PATCH 8/8] bridge: Fix netpoll support, Herbert Xu, (Thu Jun 17, 3:55 am)
Re: [PATCH 8/8] bridge: Fix netpoll support, Cong Wang, (Thu Jun 17, 3:57 am)
Re: [0/8] netpoll/bridge fixes, Paul E. McKenney, (Thu Jun 17, 2:26 pm)
Re: [PATCH 8/8] bridge: Fix netpoll support, Cong Wang, (Thu Jun 17, 8:06 pm)
Re: [0/8] netpoll/bridge fixes, Yanko Kaneti, (Tue Jun 29, 5:53 am)
Re: [0/8] netpoll/bridge fixes, Michael S. Tsirkin, (Mon Jul 19, 3:19 am)
Re: [0/8] netpoll/bridge fixes, Herbert Xu, (Mon Jul 19, 3:53 am)
Re: [0/8] netpoll/bridge fixes, Herbert Xu, (Mon Jul 19, 4:54 am)
Re: [0/8] netpoll/bridge fixes, David Miller, (Mon Jul 19, 9:05 am)
Re: [0/8] netpoll/bridge fixes, Eric Dumazet, (Mon Jul 19, 9:52 am)
Re: [0/8] netpoll/bridge fixes, David Miller, (Mon Jul 19, 1:35 pm)
Re: [0/8] netpoll/bridge fixes, Herbert Xu, (Mon Jul 19, 10:26 pm)
Re: [0/8] netpoll/bridge fixes, David Miller, (Mon Jul 19, 11:28 pm)