All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] tg3: Fix vunmap() BUG_ON() triggered from tg3_free_consistent().
@ 2018-05-04  0:04 Michael Chan
  2018-05-04 16:57 ` David Miller
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Chan @ 2018-05-04  0:04 UTC (permalink / raw
  To: davem; +Cc: netdev, siva.kallam, zumeng.chen

tg3_free_consistent() calls dma_free_coherent() to free tp->hw_stats
under spinlock and can trigger BUG_ON() in vunmap() because vunmap()
may sleep.  Fix it by removing the spinlock and relying on the
TG3_FLAG_INIT_COMPLETE flag to prevent race conditions between
tg3_get_stats64() and tg3_free_consistent().  TG3_FLAG_INIT_COMPLETE
is always cleared under tp->lock before tg3_free_consistent()
and therefore tg3_get_stats64() can safely access tp->hw_stats
under tp->lock if TG3_FLAG_INIT_COMPLETE is set.

Fixes: f5992b72ebe0 ("tg3: Fix race condition in tg3_get_stats64().")
Reported-by: Zumeng Chen <zumeng.chen@gmail.com>
Signed-off-by: Michael Chan <michael.chan@broadcom.com>
---
 drivers/net/ethernet/broadcom/tg3.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/tg3.c b/drivers/net/ethernet/broadcom/tg3.c
index 08bbb63..9f59b12 100644
--- a/drivers/net/ethernet/broadcom/tg3.c
+++ b/drivers/net/ethernet/broadcom/tg3.c
@@ -8733,14 +8733,15 @@ static void tg3_free_consistent(struct tg3 *tp)
 	tg3_mem_rx_release(tp);
 	tg3_mem_tx_release(tp);
 
-	/* Protect tg3_get_stats64() from reading freed tp->hw_stats. */
-	tg3_full_lock(tp, 0);
+	/* tp->hw_stats can be referenced safely:
+	 *     1. under rtnl_lock
+	 *     2. or under tp->lock if TG3_FLAG_INIT_COMPLETE is set.
+	 */
 	if (tp->hw_stats) {
 		dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
 				  tp->hw_stats, tp->stats_mapping);
 		tp->hw_stats = NULL;
 	}
-	tg3_full_unlock(tp);
 }
 
 /*
@@ -14178,7 +14179,7 @@ static void tg3_get_stats64(struct net_device *dev,
 	struct tg3 *tp = netdev_priv(dev);
 
 	spin_lock_bh(&tp->lock);
-	if (!tp->hw_stats) {
+	if (!tp->hw_stats || !tg3_flag(tp, INIT_COMPLETE)) {
 		*stats = tp->net_stats_prev;
 		spin_unlock_bh(&tp->lock);
 		return;
-- 
1.8.3.1

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

* Re: [PATCH net] tg3: Fix vunmap() BUG_ON() triggered from tg3_free_consistent().
  2018-05-04  0:04 [PATCH net] tg3: Fix vunmap() BUG_ON() triggered from tg3_free_consistent() Michael Chan
@ 2018-05-04 16:57 ` David Miller
  0 siblings, 0 replies; 2+ messages in thread
From: David Miller @ 2018-05-04 16:57 UTC (permalink / raw
  To: michael.chan; +Cc: netdev, siva.kallam, zumeng.chen

From: Michael Chan <michael.chan@broadcom.com>
Date: Thu,  3 May 2018 20:04:27 -0400

> tg3_free_consistent() calls dma_free_coherent() to free tp->hw_stats
> under spinlock and can trigger BUG_ON() in vunmap() because vunmap()
> may sleep.  Fix it by removing the spinlock and relying on the
> TG3_FLAG_INIT_COMPLETE flag to prevent race conditions between
> tg3_get_stats64() and tg3_free_consistent().  TG3_FLAG_INIT_COMPLETE
> is always cleared under tp->lock before tg3_free_consistent()
> and therefore tg3_get_stats64() can safely access tp->hw_stats
> under tp->lock if TG3_FLAG_INIT_COMPLETE is set.
> 
> Fixes: f5992b72ebe0 ("tg3: Fix race condition in tg3_get_stats64().")
> Reported-by: Zumeng Chen <zumeng.chen@gmail.com>
> Signed-off-by: Michael Chan <michael.chan@broadcom.com>

Applied and queued up for -stable, thanks.

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

end of thread, other threads:[~2018-05-04 16:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-05-04  0:04 [PATCH net] tg3: Fix vunmap() BUG_ON() triggered from tg3_free_consistent() Michael Chan
2018-05-04 16:57 ` David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.