Skip to contentSkip to conversation

A series of minor optimizations of the bonding module

PATCH Applied Tonghao Zhang 3 participants 7 messages Jan 18 – Jan 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover A series of minor optimizations of the bonding module

These patches mainly target the peer notify mechanism of the bonding module. Including updates of peer notify, lock races, etc. For more information, please refer to the patch.

Cc Jay Vosburgh <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
Cc Hangbin Liu <[email protected]>
Cc Jason Xing <[email protected]>

v5: introduce the bond_peer_notify_may_events used in patch 1 and 2.
v4: patch1 keeps the netdevice notifier order, and all patch rebase the codes.
v3: drop the 5/5 patch, net: bonding: combine rtnl lock block for arp monitor in activebackup mode

Tonghao Zhang (4):
net: bonding: use workqueue to make sure peer notify updated in lacp
mode
net: bonding: move bond_should_notify_peers, e.g. into rtnl lock block
net: bonding: skip the 2nd trylock when first one fail
net: bonding: add the READ_ONCE/WRITE_ONCE for outside lock accessing

 drivers/net/bonding/bond_3ad.c  |   7 +-
 drivers/net/bonding/bond_main.c | 113 ++++++++++++++++++++------------
 include/net/bonding.h           |   2 +
 3 files changed, 75 insertions(+), 47 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/4 net: bonding: use workqueue to make sure peer notify updated in lacp mode

The rtnl lock might be locked, preventing ad_cond_set_peer_notif() from acquiring the lock and updating send_peer_notif. This patch addresses the issue by using a workqueue. Since updating send_peer_notif does not require high real-time performance, such delayed updates are entirely acceptable.

In fact, checking this value and using it in multiple places, all operations are protected at the same time by rtnl lock, such as
- read send_peer_notif
- send_peer_notif--
- bond_should_notify_peers

By the way, rtnl lock is still required, when accessing bond.params.* for updating send_peer_notif. In lacp mode, resetting send_peer_notif in workqueue is safe, simple and effective way.

Additionally, this patch introduces bond_peer_notify_may_events(), which is used to check whether an event should be sent. This function will be used in both patch 1 and 2.

Cc Jay Vosburgh <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
Cc Hangbin Liu <[email protected]>
Cc Jason Xing <[email protected]>

---
v5:
- introduce the common bond_peer_notify_may_events used in patch 1 and 2.
v4:
- keep the netdevice notifier order.
v2/3:
- no change
v1:
- This patch is actually version v3, https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
- add a comment why we use the trylock.
- add this patch to series

---
 drivers/net/bonding/bond_3ad.c  |  7 +---
 drivers/net/bonding/bond_main.c | 66 ++++++++++++++++++++++++++-------
 include/net/bonding.h           |  2 +
 3 files changed, 56 insertions(+), 19 deletions(-)
drivers/net/bonding/bond_3ad.c +2 -5
index 1a8de2bf8655..01ae0269a138 100644
@@ -1008,11 +1008,8 @@ static void ad_cond_set_peer_notif(struct port *port)
 {
 	struct bonding *bond = port->slave->bond;
 
-	if (bond->params.broadcast_neighbor && rtnl_trylock()) {
-		bond->send_peer_notif = bond->params.num_peer_notif *
-			max(1, bond->params.peer_notif_delay);
-		rtnl_unlock();
-	}
+	if (bond->params.broadcast_neighbor)
+		bond_peer_notify_work_rearm(bond, 0);
 }
 
 /**
drivers/net/bonding/bond_main.c +52 -14

103 lines not on this page · raw message

include/net/bonding.h +2 -0
index 49edc7da0586..63d08056a4a4 100644
@@ -254,6 +254,7 @@ struct bonding {
 	struct   delayed_work ad_work;
 	struct   delayed_work mcast_work;
 	struct   delayed_work slave_arr_work;
+	struct   delayed_work peer_notify_work;
 #ifdef CONFIG_DEBUG_FS
 	/* debugging support via debugfs */
 	struct	 dentry *debug_dir;
@@ -709,6 +710,7 @@ struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
 					      int level);
 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave);
 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay);
+void bond_peer_notify_work_rearm(struct bonding *bond, unsigned long delay);
 void bond_work_init_all(struct bonding *bond);
 void bond_work_cancel_all(struct bonding *bond);
 
Signature · 1 line
2.34.1
Reviewed-by on v5 1/4
#2 lore raw

Re: [PATCH net-next v5 1/4] net: bonding: use workqueue to make sure peer notify updated in lacp mode

Quoted 49 lines · Tonghao Zhang

The rtnl lock might be locked, preventing ad_cond_set_peer_notif() from acquiring the lock and updating send_peer_notif. This patch addresses the issue by using a workqueue. Since updating send_peer_notif does not require high real-time performance, such delayed updates are entirely acceptable.

In fact, checking this value and using it in multiple places, all operations are protected at the same time by rtnl lock, such as
- read send_peer_notif
- send_peer_notif--
- bond_should_notify_peers

By the way, rtnl lock is still required, when accessing bond.params.* for updating send_peer_notif. In lacp mode, resetting send_peer_notif in workqueue is safe, simple and effective way.

Additionally, this patch introduces bond_peer_notify_may_events(), which is used to check whether an event should be sent. This function will be used in both patch 1 and 2.

Cc Jay Vosburgh <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
Cc Hangbin Liu <[email protected]>
Cc Jason Xing <[email protected]>

---
v5:
- introduce the common bond_peer_notify_may_events used in patch 1 and 2.
v4:
- keep the netdevice notifier order.
v2/3:
- no change
v1:
- This patch is actually version v3, https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
- add a comment why we use the trylock.
- add this patch to series

---
 drivers/net/bonding/bond_3ad.c  |  7 +---
 drivers/net/bonding/bond_main.c | 66 ++++++++++++++++++++++++++-------
 include/net/bonding.h           |  2 +
 3 files changed, 56 insertions(+), 19 deletions(-)

[...]

Quoted 4 lines
drivers/net/bonding/bond_main.c +3 -0
+/* Peer notify events post. Holds only RTNL */
+static void bond_peer_notify_may_events(struct bonding *bond, bool force)
+{

drivers/net/bonding/bond_main.c static bool bond_should_notify_peers(struct bonding *bond) · line 1238

Quoted 10 lines
drivers/net/bonding/bond_main.c +10 -0
+	bool notified = false;
+
+	if (bond_should_notify_peers(bond)) {
+		notified = true;
+		call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
+	}
+
+	if (notified || force)
+		bond->send_peer_notif--;
+}

If there is a new version, maybe change the logic to

static void bond_peer_notify_may_events(struct bonding *bond, bool force)

{
        bool do_notify = bond_should_notify_peers(bond);

        if (do_notify)
                call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);

        if (do_notify || force)
                bond->send_peer_notif--;
}

Others looks good to me.

#3 lore raw

PATCH v5 2/4 net: bonding: move bond_should_notify_peers, e.g. into rtnl lock block

In reply to Tonghao Zhang

This patch tries to avoid the possible peer notify event loss.

In bond_mii_monitor()/bond_activebackup_arp_mon(), when we hold the rtnl lock:
- check send_peer_notif again to avoid unconditionally reducing this value.
- send_peer_notif may have been reset. Therefore, it is necessary to check whether to send peer notify via bond_should_notify_peers() to avoid the loss of notification events.

Cc Jay Vosburgh <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
Cc Hangbin Liu <[email protected]>
Cc Jason Xing <[email protected]>

---
v2-5:
- no change
v1:
- splitted from: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
- this patch only move the bond_should_notify_peers to rtnl lock.
- add this patch to series

---
 drivers/net/bonding/bond_main.c | 30 +++++++++---------------------
 1 file changed, 9 insertions(+), 21 deletions(-)
drivers/net/bonding/bond_main.c +9 -21
index 353b0c8a0ca7..16418e1dc10a 100644
@@ -2816,11 +2816,10 @@ static void bond_mii_monitor(struct work_struct *work)
 {
 	struct bonding *bond = container_of(work, struct bonding,
 					    mii_work.work);
-	bool should_notify_peers;
-	bool commit;
-	unsigned long delay;
-	struct slave *slave;
 	struct list_head *iter;
+	struct slave *slave;
+	unsigned long delay;
+	bool commit;
 
 	delay = msecs_to_jiffies(bond->params.miimon);
 
@@ -2829,7 +2828,6 @@ static void bond_mii_monitor(struct work_struct *work)
 
 	rcu_read_lock();
 
-	should_notify_peers = bond_should_notify_peers(bond);
 	commit = !!bond_miimon_inspect(bond);
 
 	rcu_read_unlock();
@@ -2849,12 +2847,8 @@ static void bond_mii_monitor(struct work_struct *work)
 			bond_miimon_commit(bond);
 		}
 
-		if (bond->send_peer_notif) {
-			bond->send_peer_notif--;
-			if (should_notify_peers)
-				call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
-							 bond->dev);
-		}
+		if (bond->send_peer_notif)
+			bond_peer_notify_may_events(bond, true);
 
 		rtnl_unlock();	/* might sleep, hold no other locks */
 	}
@@ -3765,7 +3759,6 @@ static bool bond_ab_arp_probe(struct bonding *bond)
 
 static void bond_activebackup_arp_mon(struct bonding *bond)
 {
-	bool should_notify_peers = false;
 	bool should_notify_rtnl = false;
 	int delta_in_ticks;
 
@@ -3776,15 +3769,12 @@ static void bond_activebackup_arp_mon(struct bonding *bond)
 
 	rcu_read_lock();
 
-	should_notify_peers = bond_should_notify_peers(bond);
-
 	if (bond_ab_arp_inspect(bond)) {
 		rcu_read_unlock();
 
 		/* Race avoidance with bond_close flush of workqueue */
 		if (!rtnl_trylock()) {
 			delta_in_ticks = 1;
-			should_notify_peers = false;
 			goto re_arm;
 		}
 
@@ -3801,15 +3791,13 @@ static void bond_activebackup_arp_mon(struct bonding *bond)
 	if (bond->params.arp_interval)
 		queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
 
-	if (should_notify_peers || should_notify_rtnl) {
+	if (bond->send_peer_notif || should_notify_rtnl) {
 		if (!rtnl_trylock())
 			return;
 
-		if (should_notify_peers) {
-			bond->send_peer_notif--;
-			call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
-						 bond->dev);
-		}
+		if (bond->send_peer_notif)
+			bond_peer_notify_may_events(bond, true);
+
 		if (should_notify_rtnl) {
 			bond_slave_state_notify(bond);
 			bond_slave_link_notify(bond);
Signature · 1 line
2.34.1
#4 lore raw

PATCH v5 3/4 net: bonding: skip the 2nd trylock when first one fail

In reply to Tonghao Zhang

After the first trylock fail, retrying immediately is not advised as there is a high probability of failing to acquire the lock again. This optimization makes sense.

Cc Jay Vosburgh <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
Cc Hangbin Liu <[email protected]>
Cc Jason Xing <[email protected]>

---
v2-5:
- no change
v1:
- splitted from: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
- this patch only skip the 2nd rtnl lock.
- add this patch to series

---
 drivers/net/bonding/bond_main.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/net/bonding/bond_main.c +9 -7
index 16418e1dc10a..98f391ac761f 100644
@@ -3759,7 +3759,7 @@ static bool bond_ab_arp_probe(struct bonding *bond)
 
 static void bond_activebackup_arp_mon(struct bonding *bond)
 {
-	bool should_notify_rtnl = false;
+	bool should_notify_rtnl;
 	int delta_in_ticks;
 
 	delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
@@ -3787,13 +3787,11 @@ static void bond_activebackup_arp_mon(struct bonding *bond)
 	should_notify_rtnl = bond_ab_arp_probe(bond);
 	rcu_read_unlock();
 
-re_arm:
-	if (bond->params.arp_interval)
-		queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
-
 	if (bond->send_peer_notif || should_notify_rtnl) {
-		if (!rtnl_trylock())
-			return;
+		if (!rtnl_trylock()) {
+			delta_in_ticks = 1;
+			goto re_arm;
+		}
 
 		if (bond->send_peer_notif)
 			bond_peer_notify_may_events(bond, true);
@@ -3805,6 +3803,10 @@ static void bond_activebackup_arp_mon(struct bonding *bond)
 
 		rtnl_unlock();
 	}
+
+re_arm:
+	if (bond->params.arp_interval)
+		queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
 }
 
 static void bond_arp_monitor(struct work_struct *work)
Signature · 1 line
2.34.1
#5 lore raw

PATCH v5 4/4 net: bonding: add the READ_ONCE/WRITE_ONCE for outside lock accessing

In reply to Tonghao Zhang

Although operations on the variable send_peer_notif are already within a lock-protected critical section, there are cases where it is accessed outside the lock. Therefore, READ_ONCE() and WRITE_ONCE() should be added to it.

Cc Jay Vosburgh <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
Cc Hangbin Liu <[email protected]>
Cc Jason Xing <[email protected]>

---
v3-5:
- no change
v2: fix compilation errors

---
 drivers/net/bonding/bond_main.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/bonding/bond_main.c +5 -4
index 98f391ac761f..b7923d052a8d 100644
@@ -1204,8 +1204,9 @@ void bond_peer_notify_work_rearm(struct bonding *bond, unsigned long delay)
 /* Peer notify update handler. Holds only RTNL */
 static void bond_peer_notify_reset(struct bonding *bond)
 {
-	bond->send_peer_notif = bond->params.num_peer_notif *
-		max(1, bond->params.peer_notif_delay);
+	WRITE_ONCE(bond->send_peer_notif,
+		   bond->params.num_peer_notif *
+		   max(1, bond->params.peer_notif_delay));
 }
 
 static void bond_peer_notify_handler(struct work_struct *work)
@@ -2832,7 +2833,7 @@ static void bond_mii_monitor(struct work_struct *work)
 
 	rcu_read_unlock();
 
-	if (commit || bond->send_peer_notif) {
+	if (commit || READ_ONCE(bond->send_peer_notif)) {
 		/* Race avoidance with bond_close cancel of workqueue */
 		if (!rtnl_trylock()) {
 			delay = 1;
@@ -3787,7 +3788,7 @@ static void bond_activebackup_arp_mon(struct bonding *bond)
 	should_notify_rtnl = bond_ab_arp_probe(bond);
 	rcu_read_unlock();
 
-	if (bond->send_peer_notif || should_notify_rtnl) {
+	if (READ_ONCE(bond->send_peer_notif) || should_notify_rtnl) {
 		if (!rtnl_trylock()) {
 			delta_in_ticks = 1;
 			goto re_arm;
Signature · 1 line
2.34.1
#6 lore raw
In reply to Tonghao Zhang
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 16 lines · On Sun, 18 Jan 2026 12:21:10 +0800 you wrote:

These patches mainly target the peer notify mechanism of the bonding module. Including updates of peer notify, lock races, etc. For more information, please refer to the patch.

Cc Jay Vosburgh <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
Cc Hangbin Liu <[email protected]>
Cc Jason Xing <[email protected]>
Quoted 1 line

[...]

Here is the summary with links:
- [net-next,v5,1/4] net: bonding: use workqueue to make sure peer notify updated in lacp mode

    https://git.kernel.org/netdev/net-next/c/11ea9b8a88dd
  - [net-next,v5,2/4] net: bonding: move bond_should_notify_peers, e.g. into rtnl lock block
    https://git.kernel.org/netdev/net-next/c/70fc6649b36c
  - [net-next,v5,3/4] net: bonding: skip the 2nd trylock when first one fail
    https://git.kernel.org/netdev/net-next/c/f1986b3a9f2e
  - [net-next,v5,4/4] net: bonding: add the READ_ONCE/WRITE_ONCE for outside lock accessing
    https://git.kernel.org/netdev/net-next/c/429208aab9db

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html