From 6b6c526147bb00b5788a2f48463481dd30c29b71 Mon Sep 17 00:00:00 2001 From: dingtianhong Date: Thu, 24 Oct 2013 11:09:03 +0800 Subject: [PATCH 1/5] bonding: remove bond read lock for bond_mii_monitor() The bond slave list may change when the monitor is running, the slave list is no longer protected by bond->lock, only protected by rtnl lock(), so we have 3 ways to modify it: 1.add bond_master_upper_dev_link() and bond_upper_dev_unlink() in bond->lock, but it is unsafe to call call_netdevice_notifiers() in write lock. 2.remove unused bond->lock for monitor function, only use the existing rtnl lock(). 3.use rcu_read_lock() to protect it, of course, it will transform bond_for_each_slave to bond_for_each_slave_rcu() and performance is better, but in slow path, it is ignored. so I remove the bond->lock and move the rtnl lock to protect the whole monitor function. Signed-off-by: Ding Tianhong Signed-off-by: David S. Miller --- drivers/net/bonding/bond_main.c | 46 ++++++++++----------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c index a141f406cb98..0a7e32578540 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -2118,49 +2118,29 @@ void bond_mii_monitor(struct work_struct *work) struct bonding *bond = container_of(work, struct bonding, mii_work.work); bool should_notify_peers = false; - unsigned long delay; - read_lock(&bond->lock); - - delay = msecs_to_jiffies(bond->params.miimon); - - if (!bond_has_slaves(bond)) + if (!rtnl_trylock()) goto re_arm; + if (!bond_has_slaves(bond)) { + rtnl_unlock(); + goto re_arm; + } + should_notify_peers = bond_should_notify_peers(bond); - if (bond_miimon_inspect(bond)) { - read_unlock(&bond->lock); - - /* Race avoidance with bond_close cancel of workqueue */ - if (!rtnl_trylock()) { - read_lock(&bond->lock); - delay = 1; - should_notify_peers = false; - goto re_arm; - } - - read_lock(&bond->lock); - + if (bond_miimon_inspect(bond)) bond_miimon_commit(bond); - read_unlock(&bond->lock); - rtnl_unlock(); /* might sleep, hold no other locks */ - read_lock(&bond->lock); - } + if (should_notify_peers) + call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev); + + rtnl_unlock(); re_arm: if (bond->params.miimon) - queue_delayed_work(bond->wq, &bond->mii_work, delay); - - read_unlock(&bond->lock); - - if (should_notify_peers) { - if (!rtnl_trylock()) - return; - call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev); - rtnl_unlock(); - } + queue_delayed_work(bond->wq, &bond->mii_work, + msecs_to_jiffies(bond->params.miimon)); } static bool bond_has_this_ip(struct bonding *bond, __be32 ip) From 2d0dafb0152a6ac61cd31d38c3ef3d49463b6a57 Mon Sep 17 00:00:00 2001 From: dingtianhong Date: Thu, 24 Oct 2013 11:09:12 +0800 Subject: [PATCH 2/5] bonding: remove bond read lock for bond_alb_monitor() The bond slave list may change when the monitor is running, the slave list is no longer protected by bond->lock, only protected by rtnl lock(), so we have 3 ways to modify it: 1.add bond_master_upper_dev_link() and bond_upper_dev_unlink() in bond->lock, but it is unsafe to call call_netdevice_notifiers() in write lock. 2.remove unused bond->lock for monitor function, only use the existing rtnl lock(). 3.use rcu_read_lock() to protect it, of course, it will transform bond_for_each_slave to bond_for_each_slave_rcu() and performance is better, but in slow path, it is ignored. so I remove the bond->lock and move the rtnl lock to protect the whole monitor function. Signed-off-by: Ding Tianhong Signed-off-by: David S. Miller --- drivers/net/bonding/bond_alb.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/drivers/net/bonding/bond_alb.c b/drivers/net/bonding/bond_alb.c index 02872405d35d..5d79f5e529e0 100644 --- a/drivers/net/bonding/bond_alb.c +++ b/drivers/net/bonding/bond_alb.c @@ -1495,11 +1495,13 @@ void bond_alb_monitor(struct work_struct *work) struct list_head *iter; struct slave *slave; - read_lock(&bond->lock); + if (!rtnl_trylock()) + goto re_arm; if (!bond_has_slaves(bond)) { bond_info->tx_rebalance_counter = 0; bond_info->lp_counter = 0; + rtnl_unlock(); goto re_arm; } @@ -1548,16 +1550,6 @@ void bond_alb_monitor(struct work_struct *work) if (bond_info->primary_is_promisc && (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) { - /* - * dev_set_promiscuity requires rtnl and - * nothing else. Avoid race with bond_close. - */ - read_unlock(&bond->lock); - if (!rtnl_trylock()) { - read_lock(&bond->lock); - goto re_arm; - } - bond_info->rlb_promisc_timeout_counter = 0; /* If the primary was set to promiscuous mode @@ -1566,9 +1558,6 @@ void bond_alb_monitor(struct work_struct *work) */ dev_set_promiscuity(bond->curr_active_slave->dev, -1); bond_info->primary_is_promisc = 0; - - rtnl_unlock(); - read_lock(&bond->lock); } if (bond_info->rlb_rebalance) { @@ -1591,10 +1580,9 @@ void bond_alb_monitor(struct work_struct *work) } } + rtnl_unlock(); re_arm: queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks); - - read_unlock(&bond->lock); } /* assumption: called before the slave is attached to the bond From 7f1bb571b753ac75b6548f0b7c932dfc0bb1f970 Mon Sep 17 00:00:00 2001 From: dingtianhong Date: Thu, 24 Oct 2013 11:09:17 +0800 Subject: [PATCH 3/5] bonding: remove bond read lock for bond_loadbalance_arp_mon() The bond slave list may change when the monitor is running, the slave list is no longer protected by bond->lock, only protected by rtnl lock(), so we have 3 ways to modify it: 1.add bond_master_upper_dev_link() and bond_upper_dev_unlink() in bond->lock, but it is unsafe to call call_netdevice_notifiers() in write lock. 2.remove unused bond->lock for monitor function, only use the existing rtnl lock(). 3.use rcu_read_lock() to protect it, of course, it will transform bond_for_each_slave to bond_for_each_slave_rcu() and performance is better, but in slow path, it is ignored. so I remove the bond->lock and add the rtnl lock to protect the whole monitor function. Signed-off-by: Ding Tianhong Signed-off-by: David S. Miller --- drivers/net/bonding/bond_main.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c index 0a7e32578540..a620dfae1c82 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -2396,11 +2396,14 @@ void bond_loadbalance_arp_mon(struct work_struct *work) struct list_head *iter; int do_failover = 0; - read_lock(&bond->lock); - - if (!bond_has_slaves(bond)) + if (!rtnl_trylock()) goto re_arm; + if (!bond_has_slaves(bond)) { + rtnl_unlock(); + goto re_arm; + } + oldcurrent = bond->curr_active_slave; /* see if any of the previous devices are up now (i.e. they have * xmt and rcv traffic). the curr_active_slave does not come into @@ -2481,13 +2484,12 @@ void bond_loadbalance_arp_mon(struct work_struct *work) write_unlock_bh(&bond->curr_slave_lock); unblock_netpoll_tx(); } + rtnl_unlock(); re_arm: if (bond->params.arp_interval) queue_delayed_work(bond->wq, &bond->arp_work, msecs_to_jiffies(bond->params.arp_interval)); - - read_unlock(&bond->lock); } /* From 80b9d236ec56ecc18da4a43bd79e8ec9ac5036ff Mon Sep 17 00:00:00 2001 From: dingtianhong Date: Thu, 24 Oct 2013 11:09:25 +0800 Subject: [PATCH 4/5] bonding: remove bond read lock for bond_activebackup_arp_mon() The bond slave list may change when the monitor is running, the slave list is no longer protected by bond->lock, only protected by rtnl lock(), so we have 3 ways to modify it: 1.add bond_master_upper_dev_link() and bond_upper_dev_unlink() in bond->lock, but it is unsafe to call call_netdevice_notifiers() in write lock. 2.remove unused bond->lock for monitor function, only use the existing rtnl lock(). 3.use rcu_read_lock() to protect it, of course, it will transform bond_for_each_slave to bond_for_each_slave_rcu() and performance is better, but in slow path, it is ignored. so I remove the bond->lock and move the rtnl lock to protect the whole monitor function. Signed-off-by: Ding Tianhong Signed-off-by: David S. Miller --- drivers/net/bonding/bond_main.c | 48 ++++++++++----------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c index a620dfae1c82..535570ea8bbc 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -2726,51 +2726,31 @@ void bond_activebackup_arp_mon(struct work_struct *work) struct bonding *bond = container_of(work, struct bonding, arp_work.work); bool should_notify_peers = false; - int delta_in_ticks; - read_lock(&bond->lock); - - delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); - - if (!bond_has_slaves(bond)) + if (!rtnl_trylock()) goto re_arm; + if (!bond_has_slaves(bond)) { + rtnl_unlock(); + goto re_arm; + } + should_notify_peers = bond_should_notify_peers(bond); - if (bond_ab_arp_inspect(bond)) { - read_unlock(&bond->lock); - - /* Race avoidance with bond_close flush of workqueue */ - if (!rtnl_trylock()) { - read_lock(&bond->lock); - delta_in_ticks = 1; - should_notify_peers = false; - goto re_arm; - } - - read_lock(&bond->lock); - + if (bond_ab_arp_inspect(bond)) bond_ab_arp_commit(bond); - read_unlock(&bond->lock); - rtnl_unlock(); - read_lock(&bond->lock); - } - bond_ab_arp_probe(bond); + if (should_notify_peers) + call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev); + + rtnl_unlock(); + re_arm: if (bond->params.arp_interval) - queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks); - - read_unlock(&bond->lock); - - if (should_notify_peers) { - if (!rtnl_trylock()) - return; - call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev); - rtnl_unlock(); - } + queue_delayed_work(bond->wq, &bond->arp_work, + msecs_to_jiffies(bond->params.arp_interval)); } /*-------------------------- netdev event handling --------------------------*/ From 5cc172c6de80cd9bf2a1228cb928b1fb42e30deb Mon Sep 17 00:00:00 2001 From: dingtianhong Date: Thu, 24 Oct 2013 11:09:31 +0800 Subject: [PATCH 5/5] bonding: remove bond read lock for bond_3ad_state_machine_handler() The bond slave list may change when the monitor is running, the slave list is no longer protected by bond->lock, only protected by rtnl lock(), so we have 3 ways to modify it: 1.add bond_master_upper_dev_link() and bond_upper_dev_unlink() in bond->lock, but it is unsafe to call call_netdevice_notifiers() in write lock. 2.remove unused bond->lock for monitor function, only use the existing rtnl lock(). 3.use rcu_read_lock() to protect it, of course, it will transform bond_for_each_slave to bond_for_each_slave_rcu() and performance is better, but in slow path, it is ignored. so I remove the bond->lock and move the rtnl lock to protect the whole monitor function. Signed-off-by: Ding Tianhong Signed-off-by: David S. Miller --- drivers/net/bonding/bond_3ad.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/net/bonding/bond_3ad.c b/drivers/net/bonding/bond_3ad.c index 187b1b7772ef..d6fe00bf4858 100644 --- a/drivers/net/bonding/bond_3ad.c +++ b/drivers/net/bonding/bond_3ad.c @@ -2068,8 +2068,10 @@ void bond_3ad_state_machine_handler(struct work_struct *work) struct slave *slave; struct port *port; - read_lock(&bond->lock); - + if (!rtnl_trylock()) { + queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks); + return; + } //check if there are any slaves if (!bond_has_slaves(bond)) goto re_arm; @@ -2122,9 +2124,8 @@ void bond_3ad_state_machine_handler(struct work_struct *work) } re_arm: + rtnl_unlock(); queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks); - - read_unlock(&bond->lock); } /**