[PATCH net-next 6/6] bonding: factor out bond_enslave_validate

From: Hangbin Liu

Date: Fri Nov 13 2026 - 12:11:04 EST


From: Hangbin Liu <liuhangbin@xxxxxxxxxx>

bond_enslave() is a long linear function whose flow is hard to see
behind the inline blocks. Factor out bond_enslave_validate to do
all read-only checks.

Failures return directly, no undo needed at that point.

Signed-off-by: Hangbin Liu <liuhangbin@xxxxxxxxxx>
---
drivers/net/bonding/bond_main.c | 36 +++++++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index 5e36612cfaec..8f97b0e2c0b3 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -1935,17 +1935,16 @@ static void bond_publish_slave(struct bonding *bond, struct slave *slave)
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)
+/* Fail-fast validation of the slave and the bond. Everything here
+ * reads state only, so a failure needs no undo. Must stay ahead of
+ * the first mutation in bond_enslave().
+ */
+static int bond_enslave_validate(struct net_device *bond_dev,
+ struct net_device *slave_dev,
+ struct netlink_ext_ack *extack)
{
struct bonding *bond = netdev_priv(bond_dev);
- const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
- int orig_fail_over_mac = bond->params.fail_over_mac;
- struct slave *new_slave = NULL, *prev_slave;
- bool alb_initialized = false;
- struct sockaddr_storage ss;
- int res = 0, i;
+ int res;

if (slave_dev->type == ARPHRD_CAN) {
BOND_NL_ERR(bond_dev, extack,
@@ -2010,6 +2009,25 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
return res;
}

+ return 0;
+}
+
+/* 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)
+{
+ struct bonding *bond = netdev_priv(bond_dev);
+ const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
+ int orig_fail_over_mac = bond->params.fail_over_mac;
+ struct slave *new_slave = NULL, *prev_slave;
+ bool alb_initialized = false;
+ struct sockaddr_storage ss;
+ int res = 0, i;
+
+ res = bond_enslave_validate(bond_dev, slave_dev, extack);
+ if (res)
+ return res;
+
/* set bonding device ether type by slave - bonding netdevices are
* created with ether_setup, so when the slave type is not ARPHRD_ETHER
* there is a need to override some of the type dependent attribs/funcs.

--
2.56.0