Currently, the hibmcge netdev acquires the rtnl_lock in
pci_error_handlers.reset_prepare() and releases it in
pci_error_handlers.reset_done().
However, in the PCI framework:
pci_reset_bus - __pci_reset_slot - pci_slot_save_and_disable_locked -
pci_dev_save_and_disable - err_handler->reset_prepare(dev);
In pci_slot_save_and_disable_locked():
list_for_each_entry(dev, &slot->bus->devices, bus_list) {
if (!dev->slot || dev->slot!= slot)
continue;
pci_dev_save_and_disable(dev);
if (dev->subordinate)
pci_bus_save_and_disable_locked(dev->subordinate);
}
This will iterate through all devices under the current bus and execute
err_handler->reset_prepare(), causing two devices of the hibmcge driver
to sequentially request the rtnl_lock, leading to a deadlock.
Since the driver now executes netif_device_detach()
before the reset process, it will not concurrently with
other netdev APIs, so there is no need to hold the rtnl_lock now.
Therefore, this patch removes the rtnl_lock during the reset process and
adjusts the position of HBG_NIC_STATE_RESETTING to ensure
that multiple resets are not executed concurrently.
Fixes: 3f5a61f6d504f ("net: hibmcge: Add reset supported in this module")
Signed-off-by: Jijie Shao <shaojijie@huawei.com>
---
drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c
index 503cfbfb4a8a..94bc6f0da912 100644
--- a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c
+++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c
@@ -53,9 +53,11 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type)
{
int ret;
- ASSERT_RTNL();
+ if (test_and_set_bit(HBG_NIC_STATE_RESETTING, &priv->state))
+ return -EBUSY;
if (netif_running(priv->netdev)) {
+ clear_bit(HBG_NIC_STATE_RESETTING, &priv->state);
dev_warn(&priv->pdev->dev,
"failed to reset because port is up\n");
return -EBUSY;
@@ -64,7 +66,6 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type)
netif_device_detach(priv->netdev);
priv->reset_type = type;
- set_bit(HBG_NIC_STATE_RESETTING, &priv->state);
clear_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET);
if (ret) {
@@ -84,10 +85,8 @@ static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type)
type != priv->reset_type)
return 0;
- ASSERT_RTNL();
-
- clear_bit(HBG_NIC_STATE_RESETTING, &priv->state);
ret = hbg_rebuild(priv);
+ clear_bit(HBG_NIC_STATE_RESETTING, &priv->state);
if (ret) {
priv->stats.reset_fail_cnt++;
set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
@@ -101,12 +100,10 @@ static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type)
return ret;
}
-/* must be protected by rtnl lock */
int hbg_reset(struct hbg_priv *priv)
{
int ret;
- ASSERT_RTNL();
ret = hbg_reset_prepare(priv, HBG_RESET_TYPE_FUNCTION);
if (ret)
return ret;
@@ -171,7 +168,6 @@ static void hbg_pci_err_reset_prepare(struct pci_dev *pdev)
struct net_device *netdev = pci_get_drvdata(pdev);
struct hbg_priv *priv = netdev_priv(netdev);
- rtnl_lock();
hbg_reset_prepare(priv, HBG_RESET_TYPE_FLR);
}
@@ -181,7 +177,6 @@ static void hbg_pci_err_reset_done(struct pci_dev *pdev)
struct hbg_priv *priv = netdev_priv(netdev);
hbg_reset_done(priv, HBG_RESET_TYPE_FLR);
- rtnl_unlock();
}
static const struct pci_error_handlers hbg_pci_err_handler = {
--
2.33.0
On Thu, Jul 31, 2025 at 09:47:47PM +0800, Jijie Shao wrote: > Currently, the hibmcge netdev acquires the rtnl_lock in > pci_error_handlers.reset_prepare() and releases it in > pci_error_handlers.reset_done(). > > However, in the PCI framework: > pci_reset_bus - __pci_reset_slot - pci_slot_save_and_disable_locked - > pci_dev_save_and_disable - err_handler->reset_prepare(dev); > > In pci_slot_save_and_disable_locked(): > list_for_each_entry(dev, &slot->bus->devices, bus_list) { > if (!dev->slot || dev->slot!= slot) > continue; > pci_dev_save_and_disable(dev); > if (dev->subordinate) > pci_bus_save_and_disable_locked(dev->subordinate); > } > > This will iterate through all devices under the current bus and execute > err_handler->reset_prepare(), causing two devices of the hibmcge driver > to sequentially request the rtnl_lock, leading to a deadlock. > > Since the driver now executes netif_device_detach() > before the reset process, it will not concurrently with > other netdev APIs, so there is no need to hold the rtnl_lock now. > > Therefore, this patch removes the rtnl_lock during the reset process and > adjusts the position of HBG_NIC_STATE_RESETTING to ensure > that multiple resets are not executed concurrently. > > Fixes: 3f5a61f6d504f ("net: hibmcge: Add reset supported in this module") > Signed-off-by: Jijie Shao <shaojijie@huawei.com> > --- > drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c | 13 ++++--------- > 1 file changed, 4 insertions(+), 9 deletions(-) > > diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c > index 503cfbfb4a8a..94bc6f0da912 100644 > --- a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c > +++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c > @@ -53,9 +53,11 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type) > { > int ret; > > - ASSERT_RTNL(); > + if (test_and_set_bit(HBG_NIC_STATE_RESETTING, &priv->state)) > + return -EBUSY; > > if (netif_running(priv->netdev)) { > + clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); > dev_warn(&priv->pdev->dev, > "failed to reset because port is up\n"); > return -EBUSY; > @@ -64,7 +66,6 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type) > netif_device_detach(priv->netdev); > > priv->reset_type = type; > - set_bit(HBG_NIC_STATE_RESETTING, &priv->state); > clear_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); > ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET); > if (ret) { > @@ -84,10 +85,8 @@ static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type) > type != priv->reset_type) > return 0; > > - ASSERT_RTNL(); > - > - clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); > ret = hbg_rebuild(priv); > + clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); Hi Jijie, If I understand things correctly, then with this patch the HBG_NIC_STATE_RESETTING bit is used to prevent concurrent execution. Noting that a reset may be triggered via eththool, where hbg_reset() is used as a callback, I am concerned about concurrency implications for lines below this one. > if (ret) { > priv->stats.reset_fail_cnt++; > set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); > @@ -101,12 +100,10 @@ static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type) > return ret; > } > > -/* must be protected by rtnl lock */ > int hbg_reset(struct hbg_priv *priv) > { > int ret; > > - ASSERT_RTNL(); > ret = hbg_reset_prepare(priv, HBG_RESET_TYPE_FUNCTION); > if (ret) > return ret; > @@ -171,7 +168,6 @@ static void hbg_pci_err_reset_prepare(struct pci_dev *pdev) > struct net_device *netdev = pci_get_drvdata(pdev); > struct hbg_priv *priv = netdev_priv(netdev); > > - rtnl_lock(); > hbg_reset_prepare(priv, HBG_RESET_TYPE_FLR); > } > > @@ -181,7 +177,6 @@ static void hbg_pci_err_reset_done(struct pci_dev *pdev) > struct hbg_priv *priv = netdev_priv(netdev); > > hbg_reset_done(priv, HBG_RESET_TYPE_FLR); > - rtnl_unlock(); > } > > static const struct pci_error_handlers hbg_pci_err_handler = { > -- > 2.33.0 >
on 2025/8/1 18:05, Simon Horman wrote: > On Thu, Jul 31, 2025 at 09:47:47PM +0800, Jijie Shao wrote: >> Currently, the hibmcge netdev acquires the rtnl_lock in >> pci_error_handlers.reset_prepare() and releases it in >> pci_error_handlers.reset_done(). >> >> However, in the PCI framework: >> pci_reset_bus - __pci_reset_slot - pci_slot_save_and_disable_locked - >> pci_dev_save_and_disable - err_handler->reset_prepare(dev); >> >> In pci_slot_save_and_disable_locked(): >> list_for_each_entry(dev, &slot->bus->devices, bus_list) { >> if (!dev->slot || dev->slot!= slot) >> continue; >> pci_dev_save_and_disable(dev); >> if (dev->subordinate) >> pci_bus_save_and_disable_locked(dev->subordinate); >> } >> >> This will iterate through all devices under the current bus and execute >> err_handler->reset_prepare(), causing two devices of the hibmcge driver >> to sequentially request the rtnl_lock, leading to a deadlock. >> >> Since the driver now executes netif_device_detach() >> before the reset process, it will not concurrently with >> other netdev APIs, so there is no need to hold the rtnl_lock now. >> >> Therefore, this patch removes the rtnl_lock during the reset process and >> adjusts the position of HBG_NIC_STATE_RESETTING to ensure >> that multiple resets are not executed concurrently. >> >> Fixes: 3f5a61f6d504f ("net: hibmcge: Add reset supported in this module") >> Signed-off-by: Jijie Shao <shaojijie@huawei.com> >> --- >> drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c | 13 ++++--------- >> 1 file changed, 4 insertions(+), 9 deletions(-) >> >> diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c >> index 503cfbfb4a8a..94bc6f0da912 100644 >> --- a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c >> +++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c >> @@ -53,9 +53,11 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type) >> { >> int ret; >> >> - ASSERT_RTNL(); >> + if (test_and_set_bit(HBG_NIC_STATE_RESETTING, &priv->state)) >> + return -EBUSY; >> >> if (netif_running(priv->netdev)) { >> + clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); >> dev_warn(&priv->pdev->dev, >> "failed to reset because port is up\n"); >> return -EBUSY; >> @@ -64,7 +66,6 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type) >> netif_device_detach(priv->netdev); >> >> priv->reset_type = type; >> - set_bit(HBG_NIC_STATE_RESETTING, &priv->state); >> clear_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); >> ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET); >> if (ret) { >> @@ -84,10 +85,8 @@ static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type) >> type != priv->reset_type) >> return 0; >> >> - ASSERT_RTNL(); >> - >> - clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); >> ret = hbg_rebuild(priv); >> + clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); > Hi Jijie, > > If I understand things correctly, then with this patch the > HBG_NIC_STATE_RESETTING bit is used to prevent concurrent execution. > > Noting that a reset may be triggered via eththool, where hbg_reset() is > used as a callback, I am concerned about concurrency implications for lines > below this one. Yes, just like the following, it can lead to reset and net open concurrency. =========== reset1 reset2 open set_bit HBG_NIC_STATE_RESETTING netif_device_detach() resetting... clear_bit HBG_NIC_STATE_RESETTING set_bit HBG_NIC_STATE_RESETTING netif_device_detach() netif_device_attach() resetting... hbg_net_open() hbg_txrx_init() clear_bit HBG_NIC_STATE_RESETTING netif_device_attach() ============ Thank you for your reminder. I will fix it in V2 Jijie Shao >> if (ret) { >> priv->stats.reset_fail_cnt++; >> set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); >> @@ -101,12 +100,10 @@ static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type) >> return ret; >> } >> >> -/* must be protected by rtnl lock */ >> int hbg_reset(struct hbg_priv *priv) >> { >> int ret; >> >> - ASSERT_RTNL(); >> ret = hbg_reset_prepare(priv, HBG_RESET_TYPE_FUNCTION); >> if (ret) >> return ret; >> @@ -171,7 +168,6 @@ static void hbg_pci_err_reset_prepare(struct pci_dev *pdev) >> struct net_device *netdev = pci_get_drvdata(pdev); >> struct hbg_priv *priv = netdev_priv(netdev); >> >> - rtnl_lock(); >> hbg_reset_prepare(priv, HBG_RESET_TYPE_FLR); >> } >> >> @@ -181,7 +177,6 @@ static void hbg_pci_err_reset_done(struct pci_dev *pdev) >> struct hbg_priv *priv = netdev_priv(netdev); >> >> hbg_reset_done(priv, HBG_RESET_TYPE_FLR); >> - rtnl_unlock(); >> } >> >> static const struct pci_error_handlers hbg_pci_err_handler = { >> -- >> 2.33.0 >>
On Fri, Aug 01, 2025 at 06:44:36PM +0800, Jijie Shao wrote: > > on 2025/8/1 18:05, Simon Horman wrote: > > On Thu, Jul 31, 2025 at 09:47:47PM +0800, Jijie Shao wrote: > > > Currently, the hibmcge netdev acquires the rtnl_lock in > > > pci_error_handlers.reset_prepare() and releases it in > > > pci_error_handlers.reset_done(). > > > > > > However, in the PCI framework: > > > pci_reset_bus - __pci_reset_slot - pci_slot_save_and_disable_locked - > > > pci_dev_save_and_disable - err_handler->reset_prepare(dev); > > > > > > In pci_slot_save_and_disable_locked(): > > > list_for_each_entry(dev, &slot->bus->devices, bus_list) { > > > if (!dev->slot || dev->slot!= slot) > > > continue; > > > pci_dev_save_and_disable(dev); > > > if (dev->subordinate) > > > pci_bus_save_and_disable_locked(dev->subordinate); > > > } > > > > > > This will iterate through all devices under the current bus and execute > > > err_handler->reset_prepare(), causing two devices of the hibmcge driver > > > to sequentially request the rtnl_lock, leading to a deadlock. > > > > > > Since the driver now executes netif_device_detach() > > > before the reset process, it will not concurrently with > > > other netdev APIs, so there is no need to hold the rtnl_lock now. > > > > > > Therefore, this patch removes the rtnl_lock during the reset process and > > > adjusts the position of HBG_NIC_STATE_RESETTING to ensure > > > that multiple resets are not executed concurrently. > > > > > > Fixes: 3f5a61f6d504f ("net: hibmcge: Add reset supported in this module") > > > Signed-off-by: Jijie Shao <shaojijie@huawei.com> > > > --- > > > drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c | 13 ++++--------- > > > 1 file changed, 4 insertions(+), 9 deletions(-) > > > > > > diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c > > > index 503cfbfb4a8a..94bc6f0da912 100644 > > > --- a/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c > > > +++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c > > > @@ -53,9 +53,11 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type) > > > { > > > int ret; > > > - ASSERT_RTNL(); > > > + if (test_and_set_bit(HBG_NIC_STATE_RESETTING, &priv->state)) > > > + return -EBUSY; > > > if (netif_running(priv->netdev)) { > > > + clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); > > > dev_warn(&priv->pdev->dev, > > > "failed to reset because port is up\n"); > > > return -EBUSY; > > > @@ -64,7 +66,6 @@ static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type) > > > netif_device_detach(priv->netdev); > > > priv->reset_type = type; > > > - set_bit(HBG_NIC_STATE_RESETTING, &priv->state); > > > clear_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); > > > ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET); > > > if (ret) { > > > @@ -84,10 +85,8 @@ static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type) > > > type != priv->reset_type) > > > return 0; > > > - ASSERT_RTNL(); > > > - > > > - clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); > > > ret = hbg_rebuild(priv); > > > + clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); > > Hi Jijie, > > > > If I understand things correctly, then with this patch the > > HBG_NIC_STATE_RESETTING bit is used to prevent concurrent execution. > > > > Noting that a reset may be triggered via eththool, where hbg_reset() is > > used as a callback, I am concerned about concurrency implications for lines > > below this one. > > Yes, just like the following, it can lead to reset and net open concurrency. > =========== > > reset1 reset2 open > > set_bit HBG_NIC_STATE_RESETTING > > netif_device_detach() > resetting... > > clear_bit HBG_NIC_STATE_RESETTING > set_bit HBG_NIC_STATE_RESETTING > netif_device_detach() > > netif_device_attach() > resetting... hbg_net_open() > hbg_txrx_init() > > clear_bit HBG_NIC_STATE_RESETTING > netif_device_attach() > > ============ > Thank you for your reminder. > I will fix it in V2 Likewise, thanks. -- pw-bot: cr
© 2016 - 2025 Red Hat, Inc.