bonding: RCUify bond_set_rx_mode()
Subsystems (3)
PATCH 1/1 bonding: RCUify bond_set_rx_mode()
Currently, we might easily deadlock with bond_set_rx_mode() and bond_hw_addr_swap(). bond_set_rx_mode() is called via dev_set_rx_mode(), which already holds the netif_addr_lock_bh(bond), and inside it takes the bond->curr_active_slave lock, while bond_hw_addr_swap() is called with bond->curr_active_slave lock held and then takes netif_addr_lock_bh(bond), which results in deadlock.
CPU0 CPU1
---- ----
lock(&bonding_netdev_addr_lock_key); lock(&bond->curr_slave_lock); lock(&bonding_netdev_addr_lock_key);
lock(&bond->curr_slave_lock);
Fix this by using the RCU primites in bond_set_rx_mode(). We're safe wrt racing of dev_?c_(un)sync() because we hold lock(&bonding_netdev_addr_lock_key), and thus nobody will be able to modify these lists before we finish.
--- drivers/net/bonding/bond_main.c | 10 ++++------ 1 files changed, 4 insertions(+), 6 deletions(-)
drivers/net/bonding/bond_main.c +4 -6
@@ -3571,24 +3571,22 @@ static void bond_set_rx_mode(struct net_device *bond_dev)struct bonding *bond =netdev_priv (bond_dev);struct slave *slave; -read_lock (&bond->lock); +rcu_read_lock ();if (USES_PRIMARY (bond->params.mode)) { -read_lock (&bond->curr_slave_lock); - slave = bond->curr_active_slave; + slave =rcu_dereference (bond->curr_active_slave);if (slave) {dev_uc_sync (slave->dev, bond_dev);dev_mc_sync (slave->dev, bond_dev); } -read_unlock (&bond->curr_slave_lock); }else { -bond_for_each_slave (bond, slave) { +bond_for_each_slave_rcu (bond, slave) {dev_uc_sync_multiple (slave->dev, bond_dev);dev_mc_sync_multiple (slave->dev, bond_dev); } } -read_unlock (&bond->lock); +rcu_read_unlock (); }static int bond_neigh_init (struct neighbour *n)
Signature · 1 line
1.7.1
Quoted 12 lines · Veaceslav Falico
Currently, we might easily deadlock with
bond_set_rx_mode()andbond_hw_addr_swap().bond_set_rx_mode()is called viadev_set_rx_mode(), which already holds thenetif_addr_lock_bh(bond), and inside it takes thebond->curr_active_slavelock, whilebond_hw_addr_swap()is called withbond->curr_active_slavelock held and then takesnetif_addr_lock_bh(bond), which results in deadlock.CPU0 CPU1
---- ----lock(&bonding_netdev_addr_lock_key); lock(&bond->curr_slave_lock); lock(&bonding_netdev_addr_lock_key);
Quoted 7 lines
lock(&bond->curr_slave_lock);Fix this by using the RCU primites in
bond_set_rx_mode(). We're safe wrt racing of dev_?c_(un)sync() because we holdlock(&bonding_netdev_addr_lock_key), and thus nobody will be able to modify these lists before we finish.
Hi,
I don't think this deadlock can actually happen because bond_hw_addr_swap() is called from bond_change_active_slave() only in USES_PRIMARY mode, and in such mode it's always called with rtnl acquired before that, and since dev_set_rx_mode is called with rtnl, IMO such deadlock can't happen. Also I think bond_set_rx_mode() can work without RCU because of the held rtnl and converted to ASSERT_RTNL (this is optional) + rtnl_dereference for the curr_active_slave.
Cheers,
Nik
Quoted 5 lines
CC Jay Vosburgh <[email protected]>CC Andy Gospodarek <[email protected]>CC Nikolay Aleksandrov <[email protected]>Signed-off-by Veaceslav Falico <[email protected]>---
Quoted 20 lines · Nikolay Aleksandrov
Quoted 12 lines · Veaceslav Falico
Currently, we might easily deadlock with
bond_set_rx_mode()andbond_hw_addr_swap().bond_set_rx_mode()is called viadev_set_rx_mode(), which already holds thenetif_addr_lock_bh(bond), and inside it takes thebond->curr_active_slavelock, whilebond_hw_addr_swap()is called withbond->curr_active_slavelock held and then takesnetif_addr_lock_bh(bond), which results in deadlock.CPU0 CPU1
---- ----lock(&bonding_netdev_addr_lock_key); lock(&bond->curr_slave_lock); lock(&bonding_netdev_addr_lock_key);Quoted 7 lines
lock(&bond->curr_slave_lock);Fix this by using the RCU primites in
bond_set_rx_mode(). We're safe wrt racing of dev_?c_(un)sync() because we holdlock(&bonding_netdev_addr_lock_key), and thus nobody will be able to modify these lists before we finish.
Quoted 5 lines
Hi,
I don't think this deadlock can actually happen becausebond_hw_addr_swap()is called frombond_change_active_slave()only inUSES_PRIMARYmode, and in such mode it's always called with rtnl acquired before that, and sincedev_set_rx_modeis called with rtnl, IMO such deadlock can't happen.
Yep, indeed, missed the part with USES_PRIMARY(). So the lockdep had a false alarm.
Quoted 3 lines
Also I think
bond_set_rx_mode()can work without RCU because of the held rtnl and converted toASSERT_RTNL(this is optional) +rtnl_dereferencefor thecurr_active_slave.
Yes, we don't need the real rcu cause we're under rtnl and everybody else who touches it also is under rtnl. Awesome catch.
Thanks, will resubmit another patch (hard to call it v2...).
Quoted 10 lines
Cheers,
NikQuoted 5 lines
CC Jay Vosburgh <[email protected]>CC Andy Gospodarek <[email protected]>CC Nikolay Aleksandrov <[email protected]>Signed-off-by Veaceslav Falico <[email protected]>---
Quoted 19 lines · Veaceslav Falico
Currently, we might easily deadlock with
bond_set_rx_mode()andbond_hw_addr_swap().bond_set_rx_mode()is called viadev_set_rx_mode(), which already holds thenetif_addr_lock_bh(bond), and inside it takes thebond->curr_active_slavelock, whilebond_hw_addr_swap()is called withbond->curr_active_slavelock held and then takesnetif_addr_lock_bh(bond), which results in deadlock.CPU0 CPU1
---- ----lock(&bonding_netdev_addr_lock_key); lock(&bond->curr_slave_lock); lock(&bonding_netdev_addr_lock_key);
lock(&bond->curr_slave_lock);Fix this by using the RCU primites in
bond_set_rx_mode(). We're safe wrt racing of dev_?c_(un)sync() because we holdlock(&bonding_netdev_addr_lock_key), and thus nobody will be able to modify these lists before we finish.
Quoted 4 lines
CC Jay Vosburgh <[email protected]>CC Andy Gospodarek <[email protected]>CC Nikolay Aleksandrov <[email protected]>Signed-off-by Veaceslav Falico <[email protected]>
Self-NAK, for clarity. Posted a reworked patch - "[net-next] bonding: remove locking from bond_set_rx_mode()" for the same issue.
Quoted 40 lines
---
drivers/net/bonding/bond_main.c| 10 ++++------1 files changed, 4 insertions(+), 6 deletions(-)drivers/net/bonding/bond_main.c +4
-6@@ -3571,24 +3571,22 @@ static void bond_set_rx_mode(struct net_device *bond_dev)struct bonding *bond =netdev_priv (bond_dev);struct slave *slave; -read_lock (&bond->lock); +rcu_read_lock ();if (USES_PRIMARY (bond->params.mode)) { -read_lock (&bond->curr_slave_lock); - slave = bond->curr_active_slave; + slave =rcu_dereference (bond->curr_active_slave);if (slave) {dev_uc_sync (slave->dev, bond_dev);dev_mc_sync (slave->dev, bond_dev); } -read_unlock (&bond->curr_slave_lock); }else { -bond_for_each_slave (bond, slave) { +bond_for_each_slave_rcu (bond, slave) {dev_uc_sync_multiple (slave->dev, bond_dev);dev_mc_sync_multiple (slave->dev, bond_dev); } } -read_unlock (&bond->lock); +rcu_read_unlock (); }static int bond_neigh_init (struct neighbour *n)Signature · 1 line
1.7.1