atlantic: fix deadlock at aq_nic_stop
Subsystems (3)
PATCH 1/1 atlantic: fix deadlock at aq_nic_stop
NIC is stopped with rtnl_lock held, and during the stop it cancels the 'service_task' work and free irqs.
However, if CONFIG_MACSEC is set, rtnl_lock is acquired both from aq_nic_service_task and aq_linkstate_threaded_isr. Then a deadlock happens if aq_nic_stop tries to cancel/disable them when they've already started their execution.
As the deadlock is caused by rtnl_lock, it causes many other processes to stall, not only atlantic related stuff.
Fix trying to acquire rtnl_lock at the beginning of those functions, and returning if NIC closing is ongoing. Also do the "linkstate" stuff in a workqueue instead than in a threaded irq, where sleeping or waiting a mutex for a long time is discouraged.
The issue appeared repeteadly attaching and deattaching the NIC to a bond interface. Doing that after this patch I cannot reproduce the bug.
--- .../ethernet/aquantia/atlantic/aq_macsec.c | 7 +-- .../net/ethernet/aquantia/atlantic/aq_nic.c | 59 ++++++++++++++++--- .../net/ethernet/aquantia/atlantic/aq_nic.h | 1 + 3 files changed, 55 insertions(+), 12 deletions(-)
drivers/net/ethernet/aquantia/atlantic/aq_macsec.c +3 -4
@@ -1458,7 +1458,7 @@ int aq_macsec_enable(struct aq_nic_s *nic)if (!nic->macsec_cfg)return 0 ; -rtnl_lock (); +ASSERT_RTNL ();if (nic->aq_fw_ops->send_macsec_req) {struct macsec_cfg_request cfg = {0 };
@@ -1507,7 +1507,6 @@ int aq_macsec_enable(struct aq_nic_s *nic) ret =aq_apply_macsec_cfg (nic); unlock: -rtnl_unlock ();return ret; }
@@ -1519,9 +1518,9 @@ void aq_macsec_work(struct aq_nic_s *nic)if (!netif_carrier_ok (nic->ndev))return ; -rtnl_lock (); +ASSERT_RTNL (); +aq_check_txsa_expiration (nic); -rtnl_unlock (); }int aq_macsec_rx_sa_cnt (struct aq_nic_s *nic)
drivers/net/ethernet/aquantia/atlantic/aq_nic.c +51 -8
122 lines not on this page · raw message
drivers/net/ethernet/aquantia/atlantic/aq_nic.h +1 -0
@@ -140,6 +140,7 @@ struct aq_nic_s {const struct aq_fw_ops *aq_fw_ops;struct aq_nic_cfg_s aq_nic_cfg;struct timer_list service_timer; +struct work_struct linkstate_task;struct work_struct service_task;struct timer_list polling_timer;struct aq_hw_link_status_s link_status;
Signature · 1 line
2.34.1
Quoted 4 lines
Fix trying to acquire
rtnl_lockat the beginning of those functions, and returning if NIC closing is ongoing. Also do the "linkstate" stuff in a workqueue instead than in a threaded irq, where sleeping or waiting a mutex for a long time is discouraged.
What happens when the same interrupt fires again, while the work queue is still active? The advantage of the threaded interrupt handler is that the interrupt will be kept disabled, and should not fire again until the threaded interrupt handler exits.
Quoted 10 lines
drivers/net/ethernet/aquantia/atlantic/aq_nic.c +10
-0+static void aq_nic_linkstate_task (struct work_struct *work) +{ + struct aq_nic_s *self = container_of(work, struct aq_nic_s, + linkstate_task); + +#if IS_ENABLED (CONFIG_MACSEC ) +/* avoid deadlock at aq_nic_stop -> cancel_work_sync */ +while (!rtnl_trylock ()) { +if (aq_utils_obj_test (&self->flags,AQ_NIC_FLAG_CLOSING )) +return ;
drivers/net/ethernet/aquantia/atlantic/aq_nic.c static int aq_nic_update_link_status(struct aq_nic_s *self) · line 247
Quoted 10 lines
drivers/net/ethernet/aquantia/atlantic/aq_nic.c +8
-0+ msleep(AQ_TASK_RETRY_MS); + } +#endif +aq_nic_update_link_status (self); +#if IS_ENABLED (CONFIG_MACSEC ) +rtnl_unlock (); +#endif +
If MACSEC is enabled, aq_nic_update_link_status() is called with RTNL held. If it is not enabled, RTNL is not held. This sort of inconsistency could lead to further locking bugs, since it is not obvious. Please try to make this consistent.
Andrew
Quoted 10 lines · Andrew Lunn
Quoted 5 lines
Fix trying to acquire
rtnl_lockat the beginning of those functions, and returning if NIC closing is ongoing. Also do the "linkstate" stuff in a workqueue instead than in a threaded irq, where sleeping or waiting a mutex for a long time is discouraged.What happens when the same interrupt fires again, while the work queue is still active? The advantage of the threaded interrupt handler is that the interrupt will be kept disabled, and should not fire again until the threaded interrupt handler exits.
Nothing happens, if it's already queued, it won't be queued again, and when it runs it will evaluate the last link state. And in the worst case, it will be enqueued to run again, and if linkstate has changed it will be evaluated again. This will rarely happen and it's harmless.
Also, I haven't checked it but these lines suggest that the IRQ is auto-disabled in the hw until you enable it again. I didn't rely on this, anyway.
self->aq_hw_ops->hw_irq_enable(self->aq_hw,
BIT(self->aq_nic_cfg.link_irq_vec));Honestly I was a bit in doubt on doing this, with the threaded irq it would also work. I'd like to hear more opinions about this and I can change it back.
Quoted 17 lines
Quoted 17 lines
drivers/net/ethernet/aquantia/atlantic/aq_nic.c +14
-0+static void aq_nic_linkstate_task (struct work_struct *work) +{ + struct aq_nic_s *self = container_of(work, struct aq_nic_s, + linkstate_task); + +#if IS_ENABLED (CONFIG_MACSEC ) +/* avoid deadlock at aq_nic_stop -> cancel_work_sync */ +while (!rtnl_trylock ()) { +if (aq_utils_obj_test (&self->flags,AQ_NIC_FLAG_CLOSING )) +return ; +msleep (AQ_TASK_RETRY_MS ); + } +#endif +aq_nic_update_link_status (self);
Quoted 9 lines
drivers/net/ethernet/aquantia/atlantic/aq_nic.c static int aq_nic_update_link_status(struct aq_nic_s *self) · line 247
Quoted 4 lines
drivers/net/ethernet/aquantia/atlantic/aq_nic.c +4
-0+#if IS_ENABLED (CONFIG_MACSEC ) +rtnl_unlock (); +#endif +If MACSEC is enabled,
aq_nic_update_link_status()is called with RTNL held. If it is not enabled, RTNL is not held. This sort of inconsistency could lead to further locking bugs, since it is not obvious. Please try to make this consistent.
This is not new in these patches, that's what was already happening, I just moved it to get the lock a bit earlier. In my opinion, this is as it should be: why acquire a mutex if you don't have anything to protect with it? And it's worse with rtnl_lock which is held by many processes, and can be held for quite long times...
Quoted 3 lines
Andrew
Signature · 1 line
Íñigo Huguet
Quoted 20 lines · Íñigo Huguet
Quoted 10 lines · Andrew Lunn
Quoted 5 lines
Fix trying to acquire
rtnl_lockat the beginning of those functions, and returning if NIC closing is ongoing. Also do the "linkstate" stuff in a workqueue instead than in a threaded irq, where sleeping or waiting a mutex for a long time is discouraged.What happens when the same interrupt fires again, while the work queue is still active? The advantage of the threaded interrupt handler is that the interrupt will be kept disabled, and should not fire again until the threaded interrupt handler exits.
Nothing happens, if it's already queued, it won't be queued again, and when it runs it will evaluate the last link state. And in the worst case, it will be enqueued to run again, and if linkstate has changed it will be evaluated again. This will rarely happen and it's harmless.
Also, I haven't checked it but these lines suggest that the IRQ is auto-disabled in the hw until you enable it again. I didn't rely on this, anyway.
Quoted 6 lines
self->aq_hw_ops->hw_irq_enable(self->aq_hw, BIT(self->aq_nic_cfg.link_irq_vec));Honestly I was a bit in doubt on doing this, with the threaded irq it would also work. I'd like to hear more opinions about this and I can change it back.
Ethernet PHYs do all there interrupt handling in threaded IRQs. That can require a number of MDIO transactions. So we can be talking about 64 bits at 2.5MHz, so 25uS or more. We have not seen issues with that.
Quoted 10 lines
Quoted 4 lines
If MACSEC is enabled,
aq_nic_update_link_status()is called with RTNL held. If it is not enabled, RTNL is not held. This sort of inconsistency could lead to further locking bugs, since it is not obvious. Please try to make this consistent.This is not new in these patches, that's what was already happening, I just moved it to get the lock a bit earlier. In my opinion, this is as it should be: why acquire a mutex if you don't have anything to protect with it? And it's worse with
rtnl_lockwhich is held by many processes, and can be held for quite long times...
Maybe the lock needs to be moved closer to what actually needs to be protect? What is it protecting?
Andrew
Quoted 38 lines · Andrew Lunn
Quoted 20 lines · Íñigo Huguet
Quoted 10 lines · Andrew Lunn
Quoted 5 lines
Fix trying to acquire
rtnl_lockat the beginning of those functions, and returning if NIC closing is ongoing. Also do the "linkstate" stuff in a workqueue instead than in a threaded irq, where sleeping or waiting a mutex for a long time is discouraged.What happens when the same interrupt fires again, while the work queue is still active? The advantage of the threaded interrupt handler is that the interrupt will be kept disabled, and should not fire again until the threaded interrupt handler exits.
Nothing happens, if it's already queued, it won't be queued again, and when it runs it will evaluate the last link state. And in the worst case, it will be enqueued to run again, and if linkstate has changed it will be evaluated again. This will rarely happen and it's harmless.
Also, I haven't checked it but these lines suggest that the IRQ is auto-disabled in the hw until you enable it again. I didn't rely on this, anyway.
Quoted 6 lines
self->aq_hw_ops->hw_irq_enable(self->aq_hw, BIT(self->aq_nic_cfg.link_irq_vec));Honestly I was a bit in doubt on doing this, with the threaded irq it would also work. I'd like to hear more opinions about this and I can change it back.
Ethernet PHYs do all there interrupt handling in threaded IRQs. That can require a number of MDIO transactions. So we can be talking about 64 bits at 2.5MHz, so 25uS or more. We have not seen issues with that.
Quoted 5 lines
Quoted 4 lines
If MACSEC is enabled,
aq_nic_update_link_status()is called with RTNL held. If it is not enabled, RTNL is not held. This sort of inconsistency could lead to further locking bugs, since it is not obvious. Please try to make this consistent.
Quoted 8 lines
Quoted 5 lines
This is not new in these patches, that's what was already happening, I just moved it to get the lock a bit earlier. In my opinion, this is as it should be: why acquire a mutex if you don't have anything to protect with it? And it's worse with
rtnl_lockwhich is held by many processes, and can be held for quite long times...Maybe the lock needs to be moved closer to what actually needs to be protect? What is it protecting?
It's protecting the operations of aq_macsec_enable and aq_macsec_work. The locking was closer to them, but the idea of this patch is to move the locking to an earlier moment so, in the case we need to abort, do it before changing anything.
Quoted 3 lines
Andrew
Signature · 1 line
Íñigo Huguet
Quoted 7 lines
Quoted 2 lines
Maybe the lock needs to be moved closer to what actually needs to be protect? What is it protecting?
It's protecting the operations of
aq_macsec_enableandaq_macsec_work. The locking was closer to them, but the idea of this patch is to move the locking to an earlier moment so, in the case we need to abort, do it before changing anything.
aq_check_txsa_expiration() seems to be one of the issues? At least, the lock is taken before and released afterwards. So what in aq_check_txsa_expiration() requires the lock?
I don't like the use of rtnl_trylock(). It suggests the basic design is wrong, or overly complex, and so probably not working correctly.
https://blog.ffwll.ch/2022/07/locking-engineering.html
Please try to identify what is being protected. If it is driver internal state, could it be replaced with a driver mutex, rather than RTNL? Or is it network stack as a whole state, which really does require RTNL? If so, how do other drivers deal with this problem? Is it specific to MACSEC? Does MACSEC have a design problem?
Andrew
Quoted 4 lines · Andrew Lunn
Quoted 4 lines
Quoted 3 lines
Maybe the lock needs to be moved closer to what actually needs to be protect? What is it protecting?
Quoted 8 lines
Quoted 4 lines
It's protecting the operations of
aq_macsec_enableandaq_macsec_work. The locking was closer to them, but the idea of this patch is to move the locking to an earlier moment so, in the case we need to abort, do it before changing anything.
aq_check_txsa_expiration()seems to be one of the issues? At least, the lock is taken before and released afterwards. So what inaq_check_txsa_expiration()requires the lock?
Basically everything in the file aq_macsec.c seems to be implicitly protected by rtnl_lock. One group of functions are all callbacks of the struct macsec_ops aq_macsec_ops, which are responsible for configuring macsec offload, all called under rtnl_lock. The rest of the functions in the file are called from ethtool, also protected by rtnl_lock.
And part of the problem is that many of these operations are firmware and/or phy configurations which I don't have documentation about how they work. Despite this, it seems reasonable to think that they need to be lock protected.
Quoted 10 lines
I don't like the use of
rtnl_trylock(). It suggests the basic design is wrong, or overly complex, and so probably not working correctly.https://blog.ffwll.ch/2022/07/locking-engineering.html
Please try to identify what is being protected. If it is driver internal state, could it be replaced with a driver mutex, rather than RTNL? Or is it network stack as a whole state, which really does require RTNL? If so, how do other drivers deal with this problem? Is it specific to MACSEC? Does MACSEC have a design problem?
I already considered this possibility but discarded it because, as I say above, everything else is already legitimately protected by rtnl_lock.
The only alternative I can think of is to add a driver only mutex (let's call it aq_macsec_mutex), as you say, and everytime that macsec offload is to be changed both rtnl_lock and aq_macsec_mutex would be taken. It's true that aq_macsec_mutex wouldn't be much contended because almost always rtnl_lock needs to be acquired first. From the workqueue and the threaded irq there wouldn't be any deadlock because they only hold aq_macsec_mutex and ndo_stop only holds rtnl_lock. I would also allow to put the locking close to what they protect.
I thought that this solution would be a bit overkill, but maybe it's less overkill than the one I chose. If you're OK with this, I can prepare an v2.
--
Íñigo Huguet
Quoted 9 lines
Quoted 5 lines
Please try to identify what is being protected. If it is driver internal state, could it be replaced with a driver mutex, rather than RTNL? Or is it network stack as a whole state, which really does require RTNL? If so, how do other drivers deal with this problem? Is it specific to MACSEC? Does MACSEC have a design problem?
I already considered this possibility but discarded it because, as I say above, everything else is already legitimately protected by
rtnl_lock.
Did you look at other drivers using MACSEC offload? Is this driver unique in having stuff run in a work queue which you need to cancel? In fact, it is not limited to MACSEC, it could be any work queue which holds RTNL and needs to be cancelled.
Andrew
Jakub Kicinski
maintainerQuoted 6 lines · On Tue, 18 Oct 2022 02:27:40 +0200 Andrew Lunn wrote:
Quoted 6 lines
Quoted 5 lines
Please try to identify what is being protected. If it is driver internal state, could it be replaced with a driver mutex, rather than RTNL? Or is it network stack as a whole state, which really does require RTNL? If so, how do other drivers deal with this problem? Is it specific to MACSEC? Does MACSEC have a design problem?
Quoted 8 lines
Quoted 3 lines
I already considered this possibility but discarded it because, as I say above, everything else is already legitimately protected by
rtnl_lock.Did you look at other drivers using MACSEC offload? Is this driver unique in having stuff run in a work queue which you need to cancel? In fact, it is not limited to MACSEC, it could be any work queue which holds RTNL and needs to be cancelled.
FWIW the work APIs return a boolean to tell you if the work was actually scheduled / canceled, and you can pair that with a reference count of the netdev to avoid the typical _sync issues.
trigger()
ASSERT_RTNL(); if (schedule_work(netdev_priv->bla)) netdev_hold();
work()
rtnl_lock(); if (netif_running()) do_ya_thing(); netdev_put(); rtnl_unlock();
stop()
ASSERT_RTNL(); if (cancel_work(bla)) netdev_put();
I think.
Quoted 16 lines · Jakub Kicinski
FWIW the work APIs return a boolean to tell you if the work was actually scheduled / canceled, and you can pair that with a reference count of the netdev to avoid the typical
_syncissues.
trigger()ASSERT_RTNL(); if (schedule_work(netdev_priv->bla)) netdev_hold();
work()rtnl_lock(); if (netif_running()) do_ya_thing(); netdev_put(); rtnl_unlock();
Quoted 7 lines
stop()ASSERT_RTNL(); if (cancel_work(bla)) netdev_put();I think.
Interesting solution, I didn't even think of something like this. However, despite not being 100% sure, I think that it's not valid in this case because the work's task communicates with fw and uses resources that are deinitialized at ndo_stop. That's why I think that just holding a reference to the device is not enough.
Signature · 1 line
Íñigo Huguet
Jakub Kicinski
maintainerQuoted 5 lines · On Tue, 18 Oct 2022 08:15:38 +0200 Íñigo Huguet wrote:
Interesting solution, I didn't even think of something like this. However, despite not being 100% sure, I think that it's not valid in this case because the work's task communicates with fw and uses resources that are deinitialized at
ndo_stop. That's why I think that just holding a reference to the device is not enough.
You hold a reference to the netdev just to be able to take rtnl_lock() and check if it's still running. If it is UP you're protected from it going down due to rtnl_lock you took. If it's DOWN, as you say, it's not safe to access all the bits so just unlock and return.
But because you're holding the reference it's safe to cancel_work() without _sync on down, because the work itself will check if it should have been canceled.
Dunno if that's a good explanation :S
Quoted 13 lines · Jakub Kicinski
Quoted 5 lines · On Tue, 18 Oct 2022 08:15:38 +0200 Íñigo Huguet wrote:
Interesting solution, I didn't even think of something like this. However, despite not being 100% sure, I think that it's not valid in this case because the work's task communicates with fw and uses resources that are deinitialized at
ndo_stop. That's why I think that just holding a reference to the device is not enough.You hold a reference to the netdev just to be able to take
rtnl_lock()and check if it's still running. If it is UP you're protected from it going down due tortnl_lockyou took. If it's DOWN, as you say, it's not safe to access all the bits so just unlock and return.
Quoted 5 lines
But because you're holding the reference it's safe to
cancel_work()without_syncon down, because the work itself will check if it should have been canceled.Dunno if that's a good explanation :S
Yes, now I get it.
However, I think I won't use this strategy this time: rtnl_lock is only needed in the work task if IS_ENABLED(CONFIG_MACSEC). Acquiring rtnl_lock every time if macsec is not enabled wouldn't be protecting anything, so it would be a waste. I think that the strategy suggested by Andrew of adding a dedicated mutex to protect atlantic's macsec operations makes more sense in this case. Do you agree?
Signature · 1 line
Íñigo Huguet
Jakub Kicinski
maintainerQuoted 8 lines · On Wed, 19 Oct 2022 08:18:29 +0200 Íñigo Huguet wrote:
Yes, now I get it.
However, I think I won't use this strategy this time:
rtnl_lockis only needed in the work task ifIS_ENABLED(CONFIG_MACSEC). Acquiringrtnl_lockevery time if macsec is not enabled wouldn't be protecting anything, so it would be a waste. I think that the strategy suggested by Andrew of adding a dedicated mutex to protect atlantic's macsec operations makes more sense in this case. Do you agree?
Dunno, locks don't protect operations, they protect state (as the link Andrew sent probably explains?), so it's hard to say how easily you can inject a new lock into this driver covering relevant bits. My gut feeling is that refcounting would be less error prone. But I don't feel strongly enough to force one choice over the other.
Quoted 2 lines · Jakub Kicinski
Dunno, locks don't protect operations, they protect state (as the link Andrew sent probably explains?),
Yes, you're completely right. I understood that well, and Andrew's link (which is very good, thanks Andrew) explains it too. I wrongly said "operations" because in this case the lock/unlock must be done mainly inside the macsec_ops (operations), and thus my confusion.
I'm about to send my patch. If you still feel that it's not the best solution, feel free to insist on the refcount or any other approach you think is better.
Thanks and sorry for the noise
Signature · 1 line
Íñigo Huguet
Quoted 4 lines · Andrew Lunn
Did you look at other drivers using MACSEC offload? Is this driver unique in having stuff run in a work queue which you need to cancel? In fact, it is not limited to MACSEC, it could be any work queue which holds RTNL and needs to be cancelled.
Yes, I did.
About other drivers using MACSEC offload (which are only 2): they don't have work or anything similar related to macsec where they need to take rtnl_lock or any other lock. But in this driver the need of a lock seems justified, at least as far as I can understand.
About other drivers having works that need to take rtnl_lock: they cancel it in PCI shutdown/remove functions, then call unregister_netdev, acquiring rtnl_lock. I considered doing the same, but I didn't for 2 reasons:
1. There is no need to have a periodic task running for a stopped NIC
2. The task uses some resources that are deinitialized at NIC stop and try to communicate with NIC's firmware. If it's stopped in a way different to PCI shutdown/remove (i.e. ip link set down) the task would continue to be executed and try to use these deinitialized resources and communicate with a stopped hw.
Of course that point 2 possibly can be fixed to avoid doing it if NIC has been stopped, but it still remains point 1. I didn't research if other drivers really need to have the task running periodically even with the NIC stopped, but I certainly know that this one doesn't need it, looking at what the task does.
I do appreciate feedback, suggestions and changes requests (actually I happily accepted to send v2 according to them, right?). But I'd rather if they contained more specific proposals and examples of what I can do to improve my patches, instead of just suggesting that I should do some research before sending them, because I already did.
Best regards
Signature · 1 line
Íñigo Huguet
PATCH v2 1/1 atlantic: fix deadlock at aq_nic_stop
In reply to Íñigo HuguetNIC is stopped with rtnl_lock held, and during the stop it cancels the 'service_task' work and free irqs.
However, if CONFIG_MACSEC is set, rtnl_lock is acquired both from aq_nic_service_task and aq_linkstate_threaded_isr. Then a deadlock happens if aq_nic_stop tries to cancel/disable them when they've already started their execution.
As the deadlock is caused by rtnl_lock, it causes many other processes to stall, not only atlantic related stuff.
Fix it by introducing a mutex that protects each NIC's macsec related data, and locking it instead of the rtnl_lock from the service task and the threaded IRQ.
Before this patch, all macsec data was protected with rtnl_lock, but maybe not all of it needs to be protected. With this new mutex, further efforts can be made to limit the protected data only to that which requires it. However, probably it doesn't worth it because all macsec's data accesses are infrequent, and almost all are done from macsec_ops or ethtool callbacks, called holding rtnl_lock, so macsec_mutex won't never be much contended.
The issue appeared repeteadly attaching and deattaching the NIC to a bond interface. Doing that after this patch I cannot reproduce the bug.
---
v2: per Andrew Lunn's suggestion, avoid rtnl_trylock approach and use a dedicated mutex instead.
--- .../ethernet/aquantia/atlantic/aq_macsec.c | 96 ++++++++++++++----- .../net/ethernet/aquantia/atlantic/aq_nic.h | 2 + 2 files changed, 74 insertions(+), 24 deletions(-)
drivers/net/ethernet/aquantia/atlantic/aq_macsec.c +72 -24
@@ -1394,26 +1394,57 @@ static void aq_check_txsa_expiration(struct aq_nic_s *nic) egress_sa_threshold_expired); } +#define AQ_LOCKED_MDO_DEF (mdo) \ +static int aq_locked_mdo_##mdo(struct macsec_context *ctx) \ +{ \ + struct aq_nic_s *nic = netdev_priv(ctx->netdev); \ + int ret; \ + mutex_lock(&nic->macsec_mutex); \ + ret = aq_mdo_##mdo(ctx); \ + mutex_unlock(&nic->macsec_mutex); \ + return ret; \ +} + +AQ_LOCKED_MDO_DEF (dev_open) +AQ_LOCKED_MDO_DEF (dev_stop ) +AQ_LOCKED_MDO_DEF (add_secy) +AQ_LOCKED_MDO_DEF (upd_secy) +AQ_LOCKED_MDO_DEF (del_secy) +AQ_LOCKED_MDO_DEF (add_rxsc) +AQ_LOCKED_MDO_DEF (upd_rxsc) +AQ_LOCKED_MDO_DEF (del_rxsc) +AQ_LOCKED_MDO_DEF (add_rxsa) +AQ_LOCKED_MDO_DEF (upd_rxsa) +AQ_LOCKED_MDO_DEF (del_rxsa) +AQ_LOCKED_MDO_DEF (add_txsa) +AQ_LOCKED_MDO_DEF (upd_txsa) +AQ_LOCKED_MDO_DEF (del_txsa) +AQ_LOCKED_MDO_DEF (get_dev_stats) +AQ_LOCKED_MDO_DEF (get_tx_sc_stats) +AQ_LOCKED_MDO_DEF (get_tx_sa_stats) +AQ_LOCKED_MDO_DEF (get_rx_sc_stats) +AQ_LOCKED_MDO_DEF (get_rx_sa_stats) + const struct macsec_ops aq_macsec_ops = { - .mdo_dev_open = aq_mdo_dev_open, - .mdo_dev_stop = aq_mdo_dev_stop, - .mdo_add_secy = aq_mdo_add_secy, - .mdo_upd_secy = aq_mdo_upd_secy, - .mdo_del_secy = aq_mdo_del_secy, - .mdo_add_rxsc = aq_mdo_add_rxsc, - .mdo_upd_rxsc = aq_mdo_upd_rxsc, - .mdo_del_rxsc = aq_mdo_del_rxsc, - .mdo_add_rxsa = aq_mdo_add_rxsa, - .mdo_upd_rxsa = aq_mdo_upd_rxsa, - .mdo_del_rxsa = aq_mdo_del_rxsa, - .mdo_add_txsa = aq_mdo_add_txsa, - .mdo_upd_txsa = aq_mdo_upd_txsa, - .mdo_del_txsa = aq_mdo_del_txsa, - .mdo_get_dev_stats = aq_mdo_get_dev_stats, - .mdo_get_tx_sc_stats = aq_mdo_get_tx_sc_stats, - .mdo_get_tx_sa_stats = aq_mdo_get_tx_sa_stats, - .mdo_get_rx_sc_stats = aq_mdo_get_rx_sc_stats, - .mdo_get_rx_sa_stats = aq_mdo_get_rx_sa_stats, + .mdo_dev_open = aq_locked_mdo_dev_open, + .mdo_dev_stop = aq_locked_mdo_dev_stop, + .mdo_add_secy = aq_locked_mdo_add_secy, + .mdo_upd_secy = aq_locked_mdo_upd_secy, + .mdo_del_secy = aq_locked_mdo_del_secy, + .mdo_add_rxsc = aq_locked_mdo_add_rxsc, + .mdo_upd_rxsc = aq_locked_mdo_upd_rxsc, + .mdo_del_rxsc = aq_locked_mdo_del_rxsc, + .mdo_add_rxsa = aq_locked_mdo_add_rxsa, + .mdo_upd_rxsa = aq_locked_mdo_upd_rxsa, + .mdo_del_rxsa = aq_locked_mdo_del_rxsa, + .mdo_add_txsa = aq_locked_mdo_add_txsa, + .mdo_upd_txsa = aq_locked_mdo_upd_txsa, + .mdo_del_txsa = aq_locked_mdo_del_txsa, + .mdo_get_dev_stats = aq_locked_mdo_get_dev_stats, + .mdo_get_tx_sc_stats = aq_locked_mdo_get_tx_sc_stats, + .mdo_get_tx_sa_stats = aq_locked_mdo_get_tx_sa_stats, + .mdo_get_rx_sc_stats = aq_locked_mdo_get_rx_sc_stats, + .mdo_get_rx_sa_stats = aq_locked_mdo_get_rx_sa_stats, };int aq_macsec_init (struct aq_nic_s *nic)
@@ -1435,6 +1466,7 @@ int aq_macsec_init(struct aq_nic_s *nic) nic->ndev->features |=NETIF_F_HW_MACSEC ; nic->ndev->macsec_ops = &aq_macsec_ops; +mutex_init (&nic->macsec_mutex);return 0 ; }
@@ -1458,7 +1490,7 @@ int aq_macsec_enable(struct aq_nic_s *nic)if (!nic->macsec_cfg)return 0 ; -rtnl_lock (); +mutex_lock (&nic->macsec_mutex);if (nic->aq_fw_ops->send_macsec_req) {struct macsec_cfg_request cfg = {0 };
@@ -1507,7 +1539,7 @@ int aq_macsec_enable(struct aq_nic_s *nic) ret =aq_apply_macsec_cfg (nic); unlock: -rtnl_unlock (); +mutex_unlock (&nic->macsec_mutex);return ret; }
@@ -1519,9 +1551,9 @@ void aq_macsec_work(struct aq_nic_s *nic)if (!netif_carrier_ok (nic->ndev))return ; -rtnl_lock (); +mutex_lock (&nic->macsec_mutex);aq_check_txsa_expiration (nic); -rtnl_unlock (); +mutex_unlock (&nic->macsec_mutex); }int aq_macsec_rx_sa_cnt (struct aq_nic_s *nic)
@@ -1532,21 +1564,30 @@ int aq_macsec_rx_sa_cnt(struct aq_nic_s *nic)if (!cfg)return 0 ; +mutex_lock (&nic->macsec_mutex); +for (i =0 ; i <AQ_MACSEC_MAX_SC ; i++) {if (!test_bit (i, &cfg->rxsc_idx_busy))continue ; cnt +=hweight_long (cfg->aq_rxsc[i].rx_sa_idx_busy); } +mutex_unlock (&nic->macsec_mutex);return cnt; }int aq_macsec_tx_sc_cnt (struct aq_nic_s *nic) { +int cnt; +if (!nic->macsec_cfg)return 0 ; -return hweight_long (nic->macsec_cfg->txsc_idx_busy); +mutex_lock (&nic->macsec_mutex); + cnt =hweight_long (nic->macsec_cfg->txsc_idx_busy); +mutex_unlock (&nic->macsec_mutex); + +return cnt; }int aq_macsec_tx_sa_cnt (struct aq_nic_s *nic)
@@ -1557,12 +1598,15 @@ int aq_macsec_tx_sa_cnt(struct aq_nic_s *nic)if (!cfg)return 0 ; +mutex_lock (&nic->macsec_mutex); +for (i =0 ; i <AQ_MACSEC_MAX_SC ; i++) {if (!test_bit (i, &cfg->txsc_idx_busy))continue ; cnt +=hweight_long (cfg->aq_txsc[i].tx_sa_idx_busy); } +mutex_unlock (&nic->macsec_mutex);return cnt; }
@@ -1634,6 +1678,8 @@ u64 *aq_macsec_get_stats(struct aq_nic_s *nic, u64 *data)if (!cfg)return data; +mutex_lock (&nic->macsec_mutex); +aq_macsec_update_stats (nic); common_stats = &cfg->stats;
@@ -1716,5 +1762,7 @@ u64 *aq_macsec_get_stats(struct aq_nic_s *nic, u64 *data) data += i; +mutex_unlock (&nic->macsec_mutex); +return data; }
drivers/net/ethernet/aquantia/atlantic/aq_nic.h +2 -0
@@ -157,6 +157,8 @@ struct aq_nic_s {struct mutex fwreq_mutex;#if IS_ENABLED (CONFIG_MACSEC )struct aq_macsec_cfg *macsec_cfg; +/* mutex to protect data in macsec_cfg */ +struct mutex macsec_mutex;#endif /* PTP support */ struct aq_ptp_s *aq_ptp;
Signature · 1 line
2.34.1
Quoted 2 lines · Íñigo Huguet
NIC is stopped with
rtnl_lockheld, and during the stop it cancels the 'service_task' work and free irqs.
Hi Íñigo, thanks for taking care of this.
Just reviewed, overall looks reasonable for me. Unfortunately I don't recall now why RTNL lock was used originally, most probably we've tried to secure parallel "ip macsec configure something" commands execution.
But the model with internal mutex looks safe for me.
Unfortunately I now have no ability to verify your patch, edge usecase here would be to try stress running in parallel:
"ethtool -S <iface>"
"ip macsec show"
"ip macsec <change something>"
Plus ideal would be link flipping.
Have you tried something like that?
Regards,
Igor
Quoted 15 lines · Igor Russkikh
Quoted 2 lines · Íñigo Huguet
NIC is stopped with
rtnl_lockheld, and during the stop it cancels the 'service_task' work and free irqs.Hi Íñigo, thanks for taking care of this.
Just reviewed, overall looks reasonable for me. Unfortunately I don't recall now why RTNL lock was used originally, most probably we've tried to secure parallel "ip macsec configure something" commands execution.
But the model with internal mutex looks safe for me.
Quoted 8 lines
Unfortunately I now have no ability to verify your patch, edge usecase here would be to try stress running in parallel:
"ethtool -S <iface>"
"ip macsec show"
"ip macsec <change something>"
Plus ideal would be link flipping.Have you tried something like that?
I will try to run that tomorrow, thanks!
Quoted 6 lines
Reviewed-by Igor Russkikh <[email protected]>Regards,
Igor
Signature · 1 line
Íñigo Huguet
Quoted 13 lines · Igor Russkikh
Quoted 2 lines · Íñigo Huguet
NIC is stopped with
rtnl_lockheld, and during the stop it cancels the 'service_task' work and free irqs.Hi Íñigo, thanks for taking care of this.
Just reviewed, overall looks reasonable for me. Unfortunately I don't recall now why RTNL lock was used originally, most probably we've tried to secure parallel "ip macsec configure something" commands execution.
Quoted 8 lines
But the model with internal mutex looks safe for me.
Unfortunately I now have no ability to verify your patch, edge usecase here would be to try stress running in parallel:
"ethtool -S <iface>"
"ip macsec show"
"ip macsec <change something>"
Plus ideal would be link flipping.
I've been running all this in parallel for some hours without issue.
Quoted 8 lines
Have you tried something like that?
Reviewed-by Igor Russkikh <[email protected]>Regards,
Igor
--
Íñigo Huguet
This patch was applied to netdev/net.git (master) by David S. Miller <[email protected]>:
Hello:
This patch was applied to netdev/net.git (master)
by David S. Miller <[email protected]>:
Quoted 9 lines · On Thu, 20 Oct 2022 09:53:10 +0200 you wrote:
NIC is stopped with
rtnl_lockheld, and during the stop it cancels the 'service_task' work and free irqs.However, if
CONFIG_MACSECis set,rtnl_lockis acquired both fromaq_nic_service_taskandaq_linkstate_threaded_isr. Then a deadlock happens ifaq_nic_stoptries to cancel/disable them when they've already started their execution.[...]
Here is the summary with links:
- [v2,net] atlantic: fix deadlock at aq_nic_stop
https://git.kernel.org/netdev/net/c/6960d133f66e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html