From: Lin Jiapeng <ljp1205831794@gmail.com>
To: Nikolay Aleksandrov <razor@blackwall.org>,
jv@jvosburgh.net, andrew+netdev@lunn.ch, davem@davemloft.net,
edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
sfeldma@cumulusnetworks.com, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: Lin Jiapeng <jiapenglin@tencent.com>,
TencentOS Corvus AI <corvus@tencent.com>
Subject: Re: [PATCH net v2] bonding: unbind 802.3ad slaves on enslave failure
Date: Thu, 26 Nov 2026 16:20:55 +0800 [thread overview]
Message-ID: <5b2703a3-ae4f-431e-bf4e-d44348654913@gmail.com> (raw)
In-Reply-To: <654889ea-3b83-4fd4-b283-184f87b3d208@blackwall.org>
On 2026-11-25 3:14 p.m., Nikolay Aleksandrov wrote:
> On 24/11/2026 04:41, Lin Jiapeng wrote:
>> From: Lin Jiapeng <jiapenglin@tencent.com>
>>
>> The 802.3ad enslave path binds a slave before publishing it in the bond's
>> slave list. The periodic state machine can select the port and attach
>> it to
>> a LAG while enslavement is in progress, including while
>> NETDEV_CHANGEUPPER
>> notifiers are running.
>>
>> If enslavement fails, rollback can free ad_slave_info without
>> unbinding its
>> port and leave later LAG processing with a dangling pointer. This affects
>> both a failed upper-link notification and later failures such as
>> bond_sysfs_slave_add(). A test-only kretprobe forces the latter to return
>> -ENOMEM, after which KASAN reports:
>> BUG: KASAN: slab-use-after-free in
>> ad_agg_selection_logic+0x1a0d/0x1fa0
>> Read of size 1 by task kworker/u16:0/13
>> Freed by task 110:
>> kfree
>> slave_kobj_release
>> kobject_put
>> bond_enslave
>>
>> Drain the RX handler before unbinding so receive handlers cannot race
>> with
>> port->slave becoming NULL. Unbind before upper unlink when the link
>> exists,
>> and also unbind after an upper-link failure without repeating the unlink.
>> Rebuild the slave array after aggregator changes and leave earlier
>> unpublished failures unchanged.
>>
>> Fixes: 07699f9a7c8d ("bonding: add sysfs /slave dir for bond slave
>> devices.")
>> Reported-by: TencentOS Corvus AI <corvus@tencent.com>
>> Link: https://lore.kernel.org/netdev/20261113130454.63032-1-
>> ljp1205831794@gmail.com/
>> ---
>> v1 -> v2:
>> - Drain the RX handler before unbinding to avoid NULL dereferences
>> in the
>> 802.3ad receive path.
>> - Cover NETDEV_CHANGEUPPER failure after transient upper-link
>> publication,
>> without repeating upper-dev unlink.
>> - Rebuild the 802.3ad slave array after rollback.
>>
>> drivers/net/bonding/bond_main.c | 11 +++++++++++
>> 1 file changed, 11 insertions(+)
>>
>> diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/
>> bond_main.c
>> index e9f5e753f677..7ca948bb27d0 100644
>> --- a/drivers/net/bonding/bond_main.c
>> +++ b/drivers/net/bonding/bond_main.c
>> @@ -2355,10 +2355,21 @@ int bond_enslave(struct net_device *bond_dev,
>> struct net_device *slave_dev,
>> bond_sysfs_slave_del(new_slave);
>> err_upper_unlink:
>> + netdev_rx_handler_unregister(slave_dev);
>> + if (BOND_MODE(bond) == BOND_MODE_8023AD)
>> + bond_3ad_unbind_slave(new_slave);
>> bond_upper_dev_unlink(bond, new_slave);
>> + goto err_update_slave_arr;
>> err_unregister:
>> netdev_rx_handler_unregister(slave_dev);
>> + if (BOND_MODE(bond) == BOND_MODE_8023AD)
>> + bond_3ad_unbind_slave(new_slave);
>> +
>> +err_update_slave_arr:
>> + if (BOND_MODE(bond) == BOND_MODE_8023AD &&
>> + bond_update_slave_arr(bond, new_slave))
>> + bond_slave_arr_work_rearm(bond, 0);
>> err_detach:
>> vlan_vids_del_by_dev(slave_dev, bond_dev);
>>
>> base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea
>
> This looks awful jumping around error labels and doing the same thing
> at different places. Surely you could rearrange them better, maybe
> something like this (untested, needs proper review):
> --- a/drivers/net/bonding/bond_main.c
> +++ b/drivers/net/bonding/bond_main.c
> @@ -1888,10 +1888,11 @@
> int bond_enslave(struct net_device *bond_dev, struct net_device
> *slave_dev,
> struct netlink_ext_ack *extack)
> {
> + const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
> struct bonding *bond = netdev_priv(bond_dev);
> - const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
> struct slave *new_slave = NULL, *prev_slave;
> struct sockaddr_storage ss;
> + bool upper_linked = false;
> int res = 0, i;
>
> if (slave_dev->type == ARPHRD_CAN ||
> @@ -2246,13 +2247,14 @@
> slave_dbg(bond_dev, slave_dev, "Error %d calling
> bond_master_upper_dev_link\n", res);
> goto err_unregister;
> }
> + upper_linked = true;
>
> bond_lower_state_changed(new_slave);
>
> res = bond_sysfs_slave_add(new_slave);
> if (res) {
> slave_dbg(bond_dev, slave_dev, "Error %d calling
> bond_sysfs_slave_add\n", res);
> - goto err_upper_unlink;
> + goto err_unregister;
> }
>
> /* If the mode uses primary, then the following is handled by
> @@ -2357,11 +2359,15 @@
> err_sysfs_del:
> bond_sysfs_slave_del(new_slave);
>
> -err_upper_unlink:
> - bond_upper_dev_unlink(bond, new_slave);
> -
> err_unregister:
> netdev_rx_handler_unregister(slave_dev);
> + if (BOND_MODE(bond) == BOND_MODE_8023AD) {
> + bond_3ad_unbind_slave(new_slave);
> + if (bond_update_slave_arr(bond, new_slave))
> + bond_slave_arr_work_rearm(bond, 0);
> + }
> + if (upper_linked)
> + bond_upper_dev_unlink(bond, new_slave);
>
> err_detach:
> vlan_vids_del_by_dev(slave_dev, bond_dev);
Thanks a lot for the suggestions. I have adopted your suggested
structure in v3: the err_upper_unlink label is
gone and the rollback is a single err_unregister block gated by
upper_linked. On top of that, v3 includes a few changes based on the
pw-bot review of v2, mainly binding the 802.3ad port only after the
upper link has been established and re-evaluating the carrier in the
rollback path.
prev parent reply other threads:[~2026-11-26 8:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 2:41 [PATCH net v2] bonding: unbind 802.3ad slaves on enslave failure Lin Jiapeng
2026-11-25 3:43 ` netdev-bot+sashiko
2026-11-26 8:11 ` Lin Jiapeng
2026-11-25 7:14 ` Nikolay Aleksandrov
2026-11-26 8:20 ` Lin Jiapeng [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5b2703a3-ae4f-431e-bf4e-d44348654913@gmail.com \
--to=ljp1205831794@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=corvus@tencent.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=jiapenglin@tencent.com \
--cc=jv@jvosburgh.net \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=razor@blackwall.org \
--cc=sfeldma@cumulusnetworks.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.