🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #15770

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

#15770
From: Hangbin Liu
Date: Fri, 13 Nov 2026 14:04
73 lines
10837 bytes
From: Hangbin Liu <liuhangbin@kylinos.cn>

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@kylinos.cn>
---
 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
Technical Details
Message-ID: <20261113-bond_enslave_restruct-v1-6-364c549e78db@kylinos.cn>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-362694-linux-kernel-netdev=dewy.ru
References: <20261113-bond_enslave_restruct-v1-0-364c549e78db@kylinos.cn>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-362694-linux-kernel-netdev=dewy.ru
From: hangbin.liu@linux.dev (Hangbin Liu)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next 6/6] bonding: factor out bond_enslave_validate
Date: Fri, 13 Nov 2026 22:04:21 +0800
Organization: Usenet Network
Message-ID: <20261113-bond_enslave_restruct-v1-6-364c549e78db@kylinos.cn>
References: <20261113-bond_enslave_restruct-v1-0-364c549e78db@kylinos.cn>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
	logging-data="1359930"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:lHVmHpNj19g2SAZH4dfgDbJb8Dk= sha256:qZ2rXQnQ9DBbR6HNEin2uLCAZdJDcSny+Ibny4YG5H4=
	sha1:uIC1a1TwRBSRdzzTcn1vRWKzAko= sha256:8dUUWvQv8qJDRq0m7mJDCw3dX+W+TqS69AwjsL9E5nY=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 3B9F011455F; Fri, 13 Nov 2026 17:21:25 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=linux.dev
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id MU5pspCiGLMs;
	Fri, 13 Nov 2026 17:21:21 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-362694-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id 44CF211455C; Fri, 13 Nov 2026 17:21:13 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id 244AF108A04; Fri, 13 Nov 2026 14:07:04 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1794578803;
	b=O7tHvm5JfAIJPhzmCxX5QfMqO/d8f+v2HEG1DaCMS4Iv2Uwlzf/UicaldSDVZUfE5kOq
	 kxbZ42uotXi46lFhbpN220u5zPZlngSCAnBUmrV3nENuCOM4B4T+Ej1le7/g7tr5Jy7yQ
	 en+H27urn6hIZ/UA4r+J/LWmdxrSwWgW7PMtstmN6J/iVAw8Q7IduyR6aqh8+sGIv1EXG
	 UGU18lfXQhCBK/PEYf/XP0JVX+jqfUFWTz2ZtbT+MSOVZa/dNbstUFaX/YzlMgxoD8cZT
	 XaGmUsKvcPHGXR/LHqsT5D6lKmvKofsV/VhlNcGmjopELi6eA60i6c/u4bJMSpgqHmw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794578803;
	h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature:
	 X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:Date:
	 Subject:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type:
	 Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc:
	 X-Mailer;
	bh=7f9bU/O3D+ruldPY+B0WgXy7NAz35uXsO9r/E1V0JMU=;
	b=O4WFMfAyRDdglFcbZd7nsdtr5AUaWIA+YYOLU/31O0+gntdllWWxKhzPJ+r86eWsS7vT
	 OA+OMSb2fPpQtaPA7e7Xx08vNSbO52VaaG3xt3pQX59+lzAHyPai5nErOp/4cgu4LVjqx
	 aQaLOPJzHB3fghDUr8qOcqZzgyNGyAkHVFjj8R0bqpuEeK9I2DwjXZXSp2CLG3iq0TUpY
	 kMQlIm4jaw7yZN/vlpwxuPU/Tw9BryBpf7BfI1JdFn2VX2chSp83CKPD0s3MG6cDw9+U/
	 NpMw1gwNxgywfHwqSEiIu5sMcDpoMkMkAqB3WJOGzSR5Rv1ZqEV+Uv0U8FBTBGTkU2g==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ZE7sH7DZ;
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 2758B606A33D;
	Fri, 13 Nov 2026 14:06:43 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ZE7sH7DZ
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.9
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794578802;
	b=wIsMF2Pr6prv0o1r8SOzrO+yziYOSCXplg1Hu3ckw+dXhmZwYusgq+7I5AJqTxadyYQb
	 dpy4FPWK/3IfuJEdBSSOCjnX3J/NyFOFjirotgEJqmCHnaH4VYqCqVLVvFnF3gmFAwda9
	 RSofU9f8WSC+IajQZoAW3XQvwrJZq7t73tKXordcsla6ZKmYxlM/EZ2uSLmcrQG2KsS7E
	 SL46TQUEnbYt3RZO5y5X6zQqRr/mpYESP29JUdkQk8nQ9MMNsqD2GmVeKMnHhLnU1fFPk
	 D9GyFhI8/HtiiqLNvPobWmBWBfGLzZIgEgXwifks2n76rum8zVJmn9O9b8+QQkLs2Qw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794578802;
	h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received:
	 X-Mizu-Trace-ID:X-Migadu-Flow:From:Date:Subject:MIME-Version:
	 Content-Type:Content-Transfer-Encoding:Message-Id:References:
	 In-Reply-To:To:Cc:X-Mailer;
	bh=7f9bU/O3D+ruldPY+B0WgXy7NAz35uXsO9r/E1V0JMU=;
	b=e1Ano1w4/nuSc5lyXglnV7YqjwrnFWpDb0A0zLbxZytNuTM91kwofNWzBA86Z6xIuNYV
	 kevK5efZIHwHI86M6SZ7Wpq3XIpMN3YiH7bfvr9SKKuaa3iv4RgIlFB1zzIkkUrJ5zfng
	 xPt2b/92uCxmA/vznbEmUofkRnuRxtopltmc3Ytrsr+u147vi4ium+t0Ij3V3vBomPDGy
	 kT0/0/2YpUhZgpmI6GD28sN9ekwAm0/jJiviZRKujfRdMjGafgf/C9ZNQRvYp72NC2PsB
	 jvm3fFrYjcs3HWwY9CAo4B97LxqxVsSUAAd/qy9Oo0xyOzOpL1GBXBdTHJwinaaSRjg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ZE7sH7DZ;
	arc=none smtp.remote-ip=91.218.175.9
X-Received: from mta0.migadu.com (out-9.mta0.migadu.com [91.218.175.9])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6925606A32E
	for <netdev@vger.kernel.org>; Fri, 13 Nov 2026 14:06:41 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A6925606A32E
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev
X-Envelope-To: netdev@vger.kernel.org
X-DKIM-Signature: a=rsa-sha256; bh=b5LPlkeB/6xMio1zmdhb8ss8/a+9v/3+hmivnjp4xNU=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1794578799; v=1; x=1795183599;
 b=ZE7sH7DZtyH8E0lIKKyNtzX89EgawHU0egfmGScivh0HC3MHMyNfFBJYtbga2RdTczT5Q+DU
 6615FT0vCi27fIfkB4BEeMKbsIvTmZAxoszEQDC9AXCtdHR92tWUtMeU4qmG7r1qNR0I1iEF6wS
 36bCUyNXYLHenzFdf1oGAKHc=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by mta10.migadu.com with ESMTPS id d5599e53cf4d0115;
	Fri, 13 Nov 2026 14:06:39 +0000
X-Mizu-Trace-ID: d5599e53cf4d0115
X-Migadu-Flow: FLOW_OUT
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261113-bond_enslave_restruct-v1-0-364c549e78db@kylinos.cn>
X-Cc: Hangbin Liu <hangbin.liu@linux.dev>, netdev@vger.kernel.org, 
 linux-kernel@vger.kernel.org, bpf@vger.kernel.org, 
 Hangbin Liu <hangbin.liu@linux.dev>
X-Mailer: b4 0.16.0