Skip to content

Commit

Permalink
alx: use fine-grained locking instead of RTNL
Browse files Browse the repository at this point in the history
In the alx driver, all locking depended on the RTNL, but
that causes issues with ipconfig ("ip=..." command line)
because that waits for the netdev to have a carrier while
holding the RTNL, but the alx workers etc. require RTNL,
so the carrier won't be set until the RTNL is dropped and
can be acquired by alx workers. This causes long delays
at boot, as reported by Nikolai Zhubr.

Really the only sensible thing to do here is to not use
the RTNL for everything, but instead have fine-grained
locking for just the driver. Do that, it's not that hard.

Reported-by: Nikolai Zhubr <zhubr.2@gmail.com>
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Johannes Berg authored and David S. Miller committed May 13, 2021
1 parent 1351170 commit 4a5fe57
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
2 changes: 2 additions & 0 deletions drivers/net/ethernet/atheros/alx/alx.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ struct alx_priv {

/* protects hw.stats */
spinlock_t stats_lock;

struct mutex mtx;
};

extern const struct ethtool_ops alx_ethtool_ops;
Expand Down
17 changes: 13 additions & 4 deletions drivers/net/ethernet/atheros/alx/ethtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ static int alx_get_link_ksettings(struct net_device *netdev,
}
}

mutex_lock(&alx->mtx);
cmd->base.speed = hw->link_speed;
cmd->base.duplex = hw->duplex;
mutex_unlock(&alx->mtx);

ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
supported);
Expand All @@ -181,8 +183,7 @@ static int alx_set_link_ksettings(struct net_device *netdev,
struct alx_hw *hw = &alx->hw;
u32 adv_cfg;
u32 advertising;

ASSERT_RTNL();
int ret;

ethtool_convert_link_mode_to_legacy_u32(&advertising,
cmd->link_modes.advertising);
Expand All @@ -200,7 +201,12 @@ static int alx_set_link_ksettings(struct net_device *netdev,
}

hw->adv_cfg = adv_cfg;
return alx_setup_speed_duplex(hw, adv_cfg, hw->flowctrl);

mutex_lock(&alx->mtx);
ret = alx_setup_speed_duplex(hw, adv_cfg, hw->flowctrl);
mutex_unlock(&alx->mtx);

return ret;
}

static void alx_get_pauseparam(struct net_device *netdev,
Expand All @@ -209,10 +215,12 @@ static void alx_get_pauseparam(struct net_device *netdev,
struct alx_priv *alx = netdev_priv(netdev);
struct alx_hw *hw = &alx->hw;

mutex_lock(&alx->mtx);
pause->autoneg = !!(hw->flowctrl & ALX_FC_ANEG &&
hw->adv_cfg & ADVERTISED_Autoneg);
pause->tx_pause = !!(hw->flowctrl & ALX_FC_TX);
pause->rx_pause = !!(hw->flowctrl & ALX_FC_RX);
mutex_unlock(&alx->mtx);
}


Expand All @@ -232,7 +240,7 @@ static int alx_set_pauseparam(struct net_device *netdev,
if (pause->autoneg)
fc |= ALX_FC_ANEG;

ASSERT_RTNL();
mutex_lock(&alx->mtx);

/* restart auto-neg for auto-mode */
if (hw->adv_cfg & ADVERTISED_Autoneg) {
Expand All @@ -254,6 +262,7 @@ static int alx_set_pauseparam(struct net_device *netdev,
alx_cfg_mac_flowcontrol(hw, fc);

hw->flowctrl = fc;
mutex_unlock(&alx->mtx);

return 0;
}
Expand Down
86 changes: 61 additions & 25 deletions drivers/net/ethernet/atheros/alx/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 Johannes Berg <johannes@sipsolutions.net>
* Copyright (c) 2013, 2021 Johannes Berg <johannes@sipsolutions.net>
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -1091,8 +1091,9 @@ static int alx_init_sw(struct alx_priv *alx)
ALX_MAC_CTRL_RXFC_EN |
ALX_MAC_CTRL_TXFC_EN |
7 << ALX_MAC_CTRL_PRMBLEN_SHIFT;
mutex_init(&alx->mtx);

return err;
return 0;
}


Expand Down Expand Up @@ -1122,6 +1123,8 @@ static void alx_halt(struct alx_priv *alx)
{
struct alx_hw *hw = &alx->hw;

lockdep_assert_held(&alx->mtx);

alx_netif_stop(alx);
hw->link_speed = SPEED_UNKNOWN;
hw->duplex = DUPLEX_UNKNOWN;
Expand All @@ -1147,6 +1150,8 @@ static void alx_configure(struct alx_priv *alx)

static void alx_activate(struct alx_priv *alx)
{
lockdep_assert_held(&alx->mtx);

/* hardware setting lost, restore it */
alx_reinit_rings(alx);
alx_configure(alx);
Expand All @@ -1161,7 +1166,7 @@ static void alx_activate(struct alx_priv *alx)

static void alx_reinit(struct alx_priv *alx)
{
ASSERT_RTNL();
lockdep_assert_held(&alx->mtx);

alx_halt(alx);
alx_activate(alx);
Expand Down Expand Up @@ -1249,6 +1254,8 @@ static int __alx_open(struct alx_priv *alx, bool resume)

static void __alx_stop(struct alx_priv *alx)
{
lockdep_assert_held(&alx->mtx);

alx_free_irq(alx);

cancel_work_sync(&alx->link_check_wk);
Expand Down Expand Up @@ -1284,6 +1291,8 @@ static void alx_check_link(struct alx_priv *alx)
int old_speed;
int err;

lockdep_assert_held(&alx->mtx);

/* clear PHY internal interrupt status, otherwise the main
* interrupt status will be asserted forever
*/
Expand Down Expand Up @@ -1338,12 +1347,24 @@ static void alx_check_link(struct alx_priv *alx)

static int alx_open(struct net_device *netdev)
{
return __alx_open(netdev_priv(netdev), false);
struct alx_priv *alx = netdev_priv(netdev);
int ret;

mutex_lock(&alx->mtx);
ret = __alx_open(alx, false);
mutex_unlock(&alx->mtx);

return ret;
}

static int alx_stop(struct net_device *netdev)
{
__alx_stop(netdev_priv(netdev));
struct alx_priv *alx = netdev_priv(netdev);

mutex_lock(&alx->mtx);
__alx_stop(alx);
mutex_unlock(&alx->mtx);

return 0;
}

Expand All @@ -1353,18 +1374,18 @@ static void alx_link_check(struct work_struct *work)

alx = container_of(work, struct alx_priv, link_check_wk);

rtnl_lock();
mutex_lock(&alx->mtx);
alx_check_link(alx);
rtnl_unlock();
mutex_unlock(&alx->mtx);
}

static void alx_reset(struct work_struct *work)
{
struct alx_priv *alx = container_of(work, struct alx_priv, reset_wk);

rtnl_lock();
mutex_lock(&alx->mtx);
alx_reinit(alx);
rtnl_unlock();
mutex_unlock(&alx->mtx);
}

static int alx_tpd_req(struct sk_buff *skb)
Expand Down Expand Up @@ -1771,6 +1792,8 @@ static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
goto out_unmap;
}

mutex_lock(&alx->mtx);

alx_reset_pcie(hw);

phy_configured = alx_phy_configured(hw);
Expand All @@ -1781,7 +1804,7 @@ static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
err = alx_reset_mac(hw);
if (err) {
dev_err(&pdev->dev, "MAC Reset failed, error = %d\n", err);
goto out_unmap;
goto out_unlock;
}

/* setup link to put it in a known good starting state */
Expand All @@ -1791,7 +1814,7 @@ static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
dev_err(&pdev->dev,
"failed to configure PHY speed/duplex (err=%d)\n",
err);
goto out_unmap;
goto out_unlock;
}
}

Expand Down Expand Up @@ -1824,17 +1847,19 @@ static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
if (!alx_get_phy_info(hw)) {
dev_err(&pdev->dev, "failed to identify PHY\n");
err = -EIO;
goto out_unmap;
goto out_unlock;
}

mutex_unlock(&alx->mtx);

INIT_WORK(&alx->link_check_wk, alx_link_check);
INIT_WORK(&alx->reset_wk, alx_reset);
netif_carrier_off(netdev);

err = register_netdev(netdev);
if (err) {
dev_err(&pdev->dev, "register netdevice failed\n");
goto out_unmap;
goto out_unlock;
}

netdev_info(netdev,
Expand All @@ -1843,6 +1868,8 @@ static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)

return 0;

out_unlock:
mutex_unlock(&alx->mtx);
out_unmap:
iounmap(hw->hw_addr);
out_free_netdev:
Expand All @@ -1869,6 +1896,8 @@ static void alx_remove(struct pci_dev *pdev)
pci_disable_pcie_error_reporting(pdev);
pci_disable_device(pdev);

mutex_destroy(&alx->mtx);

free_netdev(alx->dev);
}

Expand All @@ -1880,7 +1909,11 @@ static int alx_suspend(struct device *dev)
if (!netif_running(alx->dev))
return 0;
netif_device_detach(alx->dev);

mutex_lock(&alx->mtx);
__alx_stop(alx);
mutex_unlock(&alx->mtx);

return 0;
}

Expand All @@ -1890,20 +1923,23 @@ static int alx_resume(struct device *dev)
struct alx_hw *hw = &alx->hw;
int err;

mutex_lock(&alx->mtx);
alx_reset_phy(hw);

if (!netif_running(alx->dev))
return 0;
if (!netif_running(alx->dev)) {
err = 0;
goto unlock;
}

rtnl_lock();
err = __alx_open(alx, true);
rtnl_unlock();
if (err)
return err;
goto unlock;

netif_device_attach(alx->dev);

return 0;
unlock:
mutex_unlock(&alx->mtx);
return err;
}

static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
Expand All @@ -1922,7 +1958,7 @@ static pci_ers_result_t alx_pci_error_detected(struct pci_dev *pdev,

dev_info(&pdev->dev, "pci error detected\n");

rtnl_lock();
mutex_lock(&alx->mtx);

if (netif_running(netdev)) {
netif_device_detach(netdev);
Expand All @@ -1934,7 +1970,7 @@ static pci_ers_result_t alx_pci_error_detected(struct pci_dev *pdev,
else
pci_disable_device(pdev);

rtnl_unlock();
mutex_unlock(&alx->mtx);

return rc;
}
Expand All @@ -1947,7 +1983,7 @@ static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)

dev_info(&pdev->dev, "pci error slot reset\n");

rtnl_lock();
mutex_lock(&alx->mtx);

if (pci_enable_device(pdev)) {
dev_err(&pdev->dev, "Failed to re-enable PCI device after reset\n");
Expand All @@ -1960,7 +1996,7 @@ static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
if (!alx_reset_mac(hw))
rc = PCI_ERS_RESULT_RECOVERED;
out:
rtnl_unlock();
mutex_unlock(&alx->mtx);

return rc;
}
Expand All @@ -1972,14 +2008,14 @@ static void alx_pci_error_resume(struct pci_dev *pdev)

dev_info(&pdev->dev, "pci error resume\n");

rtnl_lock();
mutex_lock(&alx->mtx);

if (netif_running(netdev)) {
alx_activate(alx);
netif_device_attach(netdev);
}

rtnl_unlock();
mutex_unlock(&alx->mtx);
}

static const struct pci_error_handlers alx_err_handlers = {
Expand Down

0 comments on commit 4a5fe57

Please sign in to comment.