[PATCH net-next 4/6] bonding: factor out slave publication
From: Hangbin Liu
Date: Fri Nov 13 2026 - 12:05:58 EST
From: Hangbin Liu <liuhangbin@xxxxxxxxxx>
Factor the publication step of bond_enslave() into bond_publish_slave()
so the infallible "commit point" of the function is explicit and cannot
be interleaved with new fallible code by accident.
No functional change.
Signed-off-by: Hangbin Liu <liuhangbin@xxxxxxxxxx>
---
drivers/net/bonding/bond_main.c | 49 ++++++++++++++++++++++++-----------------
1 file changed, 29 insertions(+), 20 deletions(-)
diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index f696a0d15034..bb3752683456 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -1884,6 +1884,34 @@ void bond_xdp_set_features(struct net_device *bond_dev)
xdp_set_features_flag(bond_dev, val);
}
+/* Make the fully prepared slave visible to the rest of the bonding
+ * code. All fallible steps of bond_enslave() must have completed
+ * before this runs: error paths never undo the publication.
+ */
+static void bond_publish_slave(struct bonding *bond, struct slave *slave)
+{
+ /* Must called before bond_update_slave_arr() or bond_select_active_slave() */
+ WRITE_ONCE(bond->slave_cnt, bond->slave_cnt + 1);
+ netdev_compute_master_upper_features(bond->dev, true);
+ bond_set_carrier(bond);
+
+ /* Needs to be called before bond_select_active_slave(), which will
+ * remove the maddrs if the slave is selected as active slave.
+ */
+ bond_slave_ns_maddrs_add(bond, slave);
+
+ if (bond_uses_primary(bond)) {
+ block_netpoll_tx();
+ bond_select_active_slave(bond);
+ unblock_netpoll_tx();
+ }
+
+ /* broadcast mode uses the all_slaves to loop through slaves. */
+ if (bond_mode_can_use_xmit_hash(bond) ||
+ BOND_MODE(bond) == BOND_MODE_BROADCAST)
+ bond_update_slave_arr(bond, NULL);
+}
+
/* enslave device <slave> to bond device <master> */
int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
struct netlink_ext_ack *extack)
@@ -2341,26 +2369,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
bond_xdp_set_features(bond_dev);
- /* Must called before bond_update_slave_arr() or bond_select_active_slave() */
- WRITE_ONCE(bond->slave_cnt, bond->slave_cnt + 1);
- netdev_compute_master_upper_features(bond->dev, true);
- bond_set_carrier(bond);
-
- /* Needs to be called before bond_select_active_slave(), which will
- * remove the maddrs if the slave is selected as active slave.
- */
- bond_slave_ns_maddrs_add(bond, new_slave);
-
- if (bond_uses_primary(bond)) {
- block_netpoll_tx();
- bond_select_active_slave(bond);
- unblock_netpoll_tx();
- }
-
- /* broadcast mode uses the all_slaves to loop through slaves. */
- if (bond_mode_can_use_xmit_hash(bond) ||
- BOND_MODE(bond) == BOND_MODE_BROADCAST)
- bond_update_slave_arr(bond, NULL);
+ bond_publish_slave(bond, new_slave);
slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
bond_is_active_slave(new_slave) ? "an active" : "a backup",
--
2.56.0