[PATCH] net: amd: xgbe: fix possible sleep-in-atomic-context bugs in xgbe_powerdown()

From: Jia-Ju Bai
Date: Wed Dec 18 2019 - 09:01:14 EST


The driver may sleep while holding a spinlock.
The function call path (from bottom to top) in Linux 4.19 is:

drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1261:
flush_workqueue in xgbe_powerdown
drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1253:
_raw_spin_lock_irqsave in xgbe_powerdown

drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1055:
napi_disable in xgbe_napi_disable
drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1266:
xgbe_napi_disable in xgbe_powerdown
drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1253:
_raw_spin_lock_irqsave in xgbe_powerdown

drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1049:
napi_disable in xgbe_napi_disable
drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1266:
xgbe_napi_disable in xgbe_powerdown
drivers/net/ethernet/amd/xgbe/xgbe-drv.c, 1253:
_raw_spin_lock_irqsave in xgbe_powerdown

flush_workqueue() and napi_disable() can sleep at runtime.

To fix these bugs, flush_workqueue() and xgbe_napi_disable() are called
without holding the spinlock.

These bugs are found by a static analysis tool STCheck written by
myself.

Signed-off-by: Jia-Ju Bai <baijiaju1990@xxxxxxxxx>
---
drivers/net/ethernet/amd/xgbe/xgbe-drv.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/amd/xgbe/xgbe-drv.c b/drivers/net/ethernet/amd/xgbe/xgbe-drv.c
index 98f8f2033154..328361d0e190 100644
--- a/drivers/net/ethernet/amd/xgbe/xgbe-drv.c
+++ b/drivers/net/ethernet/amd/xgbe/xgbe-drv.c
@@ -1257,17 +1257,18 @@ int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
netif_tx_stop_all_queues(netdev);

xgbe_stop_timers(pdata);
- flush_workqueue(pdata->dev_workqueue);

hw_if->powerdown_tx(pdata);
hw_if->powerdown_rx(pdata);

- xgbe_napi_disable(pdata, 0);
-
pdata->power_down = 1;

spin_unlock_irqrestore(&pdata->lock, flags);

+ flush_workqueue(pdata->dev_workqueue);
+
+ xgbe_napi_disable(pdata, 0);
+
DBGPR("<--xgbe_powerdown\n");

return 0;
--
2.17.1