From nobody Mon Sep 28 19:23:43 2026 Received: from mta1.migadu.com (out-130.mta1.migadu.com [95.215.58.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 112AC43E06B for ; Tue, 18 Aug 2026 08:48:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787042896; cv=none; b=WItKPtAzN8+SlvFcV/8YNKyvIgqlFI3Wibv92b40+pBqCRnPUszzVq01PRn1RNXAA8t7Y7d0zrHJyhhkYWdJmWSYXTc6MSYtnMYQQZCJvCkVb9AlY/YDOt70g67FL8NVWPTCZBjA8kpe7e//YxQNjGV0ImzTNyHaPpSZHZTtcPU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787042896; c=relaxed/simple; bh=8QH4Vs3EPyHFDe9FmLx6FO9ukIEa87Zjaonq8CWRufs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Gd5UebUqg1N0mh0GSQkXfEInxgZmrHYc6V/A/Mp74TE39S6KlfyUGHodeQdvn2qWtpgAvRjnLFHBzVrBC/APYtwr0dzI3df9TBeZj+xdQ6U6sSlJRxwjCN4X+69CQzzctdqqMHid20Ql+EiBr0zH+Qm9aJwQ8LbpLAGxlvP5/ag= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=xsm5isH7; arc=none smtp.client-ip=95.215.58.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="xsm5isH7" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=8QH4Vs3EPyHFDe9FmLx6FO9ukIEa87Zjaonq8CWRufs=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787042888; v=1; x=1787647688; b=xsm5isH7dYmAduQJZwM2LPG9t2W1VOFvUNbW73wku/8E0K1Gref7GCCap+FbJDbwcv5tx16Y qgAkvSOqSggiYKxO9KCIBQ1rYGwWwNY98XutkyAsljlc6l0K/7ofFNJNhR/VT71vXKt89hDr5D6 +LBssTmGaVBDO9Le3AFJ8hK4= X-Envelope-To: linux-kernel@vger.kernel.org Received: from [192.168.110.119] (203.175.12.240) by smtp.migadu.com with ESMTPS id e5463ac3d8899d14; Tue, 18 Aug 2026 08:48:08 +0000 X-Migadu-Flow: FLOW_OUT From: Hangbin Liu Date: Tue, 18 Aug 2026 16:47:44 +0800 Subject: [PATCH net v3 1/2] bonding: convert unbalanced_load to per-cpu state Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260818-bond_overflow-v3-1-e05d4dbc2fd8@kylinos.cn> References: <20260818-bond_overflow-v3-0-e05d4dbc2fd8@kylinos.cn> In-Reply-To: <20260818-bond_overflow-v3-0-e05d4dbc2fd8@kylinos.cn> To: Jay Vosburgh , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: Hangbin Liu , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Hangbin Liu X-Mailer: b4 0.14.3 From: Hangbin Liu A later patch widens the bonding TLB tx counters from u32 to u64. The unbalanced_load counter sits in the transmit hot path, and cross-CPU synchronization of a u64 would introduce measurable overhead. Convert unbalanced_load to a per-cpu counter first so that the subsequent widening only touches per-cpu data local to each CPU. Introduce struct unbalanced_load_stats to hold the per-cpu counter, and move the aggregation into a helper, reset_unbalanced_load(), which sums and clears all per-cpu instances. Signed-off-by: Hangbin Liu --- drivers/net/bonding/bond_alb.c | 25 ++++++++++++++++++------- drivers/net/bonding/bond_main.c | 9 +++++++++ include/net/bond_alb.h | 6 +++++- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/drivers/net/bonding/bond_alb.c b/drivers/net/bonding/bond_alb.c index 839f7482dc18..d54d834cf72b 100644 --- a/drivers/net/bonding/bond_alb.c +++ b/drivers/net/bonding/bond_alb.c @@ -1345,7 +1345,7 @@ static netdev_tx_t bond_do_alb_xmit(struct sk_buff *s= kb, struct bonding *bond, /* unbalanced or unassigned, send through primary */ tx_slave =3D rcu_dereference(bond->curr_active_slave); if (bond->params.tlb_dynamic_lb) - bond_info->unbalanced_load +=3D skb->len; + this_cpu_add(bond_info->unbalanced_load->tx_bytes, skb->len); } =20 if (tx_slave && bond_slave_can_tx(tx_slave)) { @@ -1529,6 +1529,21 @@ netdev_tx_t bond_alb_xmit(struct sk_buff *skb, struc= t net_device *bond_dev) return bond_do_alb_xmit(skb, bond, tx_slave); } =20 +static u32 reset_unbalanced_load(struct alb_bond_info *bond_info) +{ + struct unbalanced_load_stats *p; + u32 total_bytes =3D 0; + int i; + + for_each_possible_cpu(i) { + p =3D per_cpu_ptr(bond_info->unbalanced_load, i); + total_bytes +=3D READ_ONCE(p->tx_bytes); + WRITE_ONCE(p->tx_bytes, 0); + } + + return total_bytes / BOND_TLB_REBALANCE_INTERVAL; +} + void bond_alb_monitor(struct work_struct *work) { struct bonding *bond =3D container_of(work, struct bonding, @@ -1570,12 +1585,8 @@ void bond_alb_monitor(struct work_struct *work) if (atomic_read(&bond_info->tx_rebalance_counter) >=3D BOND_TLB_REBALANCE= _TICKS) { bond_for_each_slave_rcu(bond, slave, iter) { tlb_clear_slave(bond, slave, 1); - if (slave =3D=3D rcu_access_pointer(bond->curr_active_slave)) { - SLAVE_TLB_INFO(slave).load =3D - bond_info->unbalanced_load / - BOND_TLB_REBALANCE_INTERVAL; - bond_info->unbalanced_load =3D 0; - } + if (slave =3D=3D rcu_access_pointer(bond->curr_active_slave)) + SLAVE_TLB_INFO(slave).load =3D reset_unbalanced_load(bond_info); } atomic_set(&bond_info->tx_rebalance_counter, 0); } diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_mai= n.c index 522eab060f9e..9fb44e0031c8 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -5995,6 +5995,7 @@ static void bond_destructor(struct net_device *bond_d= ev) destroy_workqueue(bond->wq); =20 free_percpu(bond->rr_tx_counter); + free_percpu(bond->alb_info.unbalanced_load); } =20 void bond_setup(struct net_device *bond_dev) @@ -6494,6 +6495,10 @@ static int bond_init(struct net_device *bond_dev) if (!bond->wq) return -ENOMEM; =20 + bond->alb_info.unbalanced_load =3D alloc_percpu(struct unbalanced_load_st= ats); + if (!bond->alb_info.unbalanced_load) + goto wq_out; + bond->notifier_ctx =3D false; =20 spin_lock_init(&bond->stats_lock); @@ -6511,6 +6516,10 @@ static int bond_init(struct net_device *bond_dev) eth_hw_addr_random(bond_dev); =20 return 0; + +wq_out: + destroy_workqueue(bond->wq); + return -ENOMEM; } =20 unsigned int bond_get_num_tx_queues(void) diff --git a/include/net/bond_alb.h b/include/net/bond_alb.h index e5945427f38d..3fabf4714dec 100644 --- a/include/net/bond_alb.h +++ b/include/net/bond_alb.h @@ -123,9 +123,13 @@ struct tlb_slave_info { */ }; =20 +struct unbalanced_load_stats { + u32 tx_bytes; +}; + struct alb_bond_info { struct tlb_client_info *tx_hashtbl; /* Dynamically allocated */ - u32 unbalanced_load; + struct unbalanced_load_stats __percpu *unbalanced_load; atomic_t tx_rebalance_counter; int lp_counter; /* -------- rlb parameters -------- */ --=20 2.55.0 From nobody Mon Sep 28 19:23:43 2026 Received: from mta0.migadu.com (out-135.mta0.migadu.com [91.218.175.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0088443A9D for ; Tue, 18 Aug 2026 08:48:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787042901; cv=none; b=IZMRKT9XpJs7Uq0fcKqPXoJBWYzqF8XTFoX3FBYn/iBjF85xavTdZaehAxmiwwpf16b0YgzoQaAFewvYDurlUufkr1utECPrUsGXIEbsS0mnPhqATdUyKCVBXT9PQMDnjZdPQ7eW1YxkrHaEIeWkCcpq/l/TAY0vLni++Oq0BZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787042901; c=relaxed/simple; bh=FhibvI8n+f6tpPj6C6nyP1Mbhp8+pxN7yzMbG0Dwipg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XiEM8uLPgRoi5iAAOYYUqy/kR5EAvI+NgGH0zmN0+yz1Njtn3F8M9sJUPTfk2Iavt85EPpM1p7YK2ge9w7zDV4BN6my+CXnvPOe+lbi3UH7QvAkxbltvhNLJa0j8+pC7f/xBEVHXku151IEAkAsOTGI4KVvR8FaCTsQE7EEaKJc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=feunZEaE; arc=none smtp.client-ip=91.218.175.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="feunZEaE" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=FhibvI8n+f6tpPj6C6nyP1Mbhp8+pxN7yzMbG0Dwipg=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787042896; v=1; x=1787647696; b=feunZEaESShFJhAyVnB4jmGDfIuQCUZFPaqlykPsdSREvM/7/ik9nFL0KmQlg7JJqQJnUDW+ IzuDVqr29pYOgp2GirxcpXYiSldyQrkdFhhZKx7IUpYK0vWwR/rZik6ae9r7DH8P5nz1mzBN4He 7JVk1ODWnPxz3hTu6YnkEoTM= X-Envelope-To: linux-kernel@vger.kernel.org Received: from [192.168.110.119] (203.175.12.240) by smtp.migadu.com with ESMTPS id 1d20c1b81865f820; Tue, 18 Aug 2026 08:48:16 +0000 X-Migadu-Flow: FLOW_OUT From: Hangbin Liu Date: Tue, 18 Aug 2026 16:47:45 +0800 Subject: [PATCH net v3 2/2] bonding: fix u32 overflow in compute_gap() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260818-bond_overflow-v3-2-e05d4dbc2fd8@kylinos.cn> References: <20260818-bond_overflow-v3-0-e05d4dbc2fd8@kylinos.cn> In-Reply-To: <20260818-bond_overflow-v3-0-e05d4dbc2fd8@kylinos.cn> To: Jay Vosburgh , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: Hangbin Liu , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Hangbin Liu X-Mailer: b4 0.14.3 From: Hangbin Liu The TLB load-tracking fields tx_bytes, load_history, load, and unbalanced_load are all u32. At sustained throughput above ~3.2 Gbit/s over the 10-second rebalance interval the byte counters wrap, causing compute_gap() to produce incorrect gap values and mis-select slaves. Such speeds are common on modern NICs under heavy traffic. Widen these fields to u64. Use u64_stats_sync to protect the per-cpu unbalanced_load_stats against tearing on 32-bit architectures, and div_u64() for the 64-bit divisions. The tx_bytes, load, and load_history are protected in spin_lock. Rework compute_gap() to use u64 arithmetic throughout. Return 0 when the speed is unknown or the slave is already overloaded. Detected by AI code review. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Hangbin Liu --- drivers/net/bonding/bond_alb.c | 56 ++++++++++++++++++++++++++++++-------= ---- drivers/net/bonding/bond_main.c | 2 +- include/net/bond_alb.h | 9 ++++--- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/drivers/net/bonding/bond_alb.c b/drivers/net/bonding/bond_alb.c index d54d834cf72b..659a77323444 100644 --- a/drivers/net/bonding/bond_alb.c +++ b/drivers/net/bonding/bond_alb.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -74,8 +75,8 @@ static inline u8 _simple_hash(const u8 *hash_start, int h= ash_size) static inline void tlb_init_table_entry(struct tlb_client_info *entry, int= save_load) { if (save_load) { - entry->load_history =3D 1 + entry->tx_bytes / - BOND_TLB_REBALANCE_INTERVAL; + entry->load_history =3D 1 + div_u64(entry->tx_bytes, + BOND_TLB_REBALANCE_INTERVAL); entry->tx_bytes =3D 0; } =20 @@ -158,25 +159,35 @@ static void tlb_deinitialize(struct bonding *bond) spin_unlock_bh(&bond->mode_lock); } =20 -static long long compute_gap(struct slave *slave) +static u64 compute_gap(struct slave *slave) { - return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */ - (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */ + u32 raw_speed =3D READ_ONCE(slave->speed); + u64 speed =3D (u64)raw_speed; + + /* It's meaningless to compare gap on unknown speed NIC */ + if (raw_speed =3D=3D (u32)SPEED_UNKNOWN) + return 0; + + /* skip slave which is over loaded */ + if ((speed << 20) <=3D (SLAVE_TLB_INFO(slave).load << 3)) + return 0; + + return (speed << 20) - /* Convert to Megabit per sec */ + (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */ } =20 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond) { struct slave *slave, *least_loaded; struct list_head *iter; - long long max_gap; + u64 max_gap =3D 0; =20 least_loaded =3D NULL; - max_gap =3D LLONG_MIN; =20 /* Find the slave with the largest gap */ bond_for_each_slave_rcu(bond, slave, iter) { if (bond_slave_can_tx(slave)) { - long long gap =3D compute_gap(slave); + u64 gap =3D compute_gap(slave); =20 if (max_gap < gap) { least_loaded =3D slave; @@ -1344,8 +1355,14 @@ static netdev_tx_t bond_do_alb_xmit(struct sk_buff *= skb, struct bonding *bond, if (!tx_slave) { /* unbalanced or unassigned, send through primary */ tx_slave =3D rcu_dereference(bond->curr_active_slave); - if (bond->params.tlb_dynamic_lb) - this_cpu_add(bond_info->unbalanced_load->tx_bytes, skb->len); + if (bond->params.tlb_dynamic_lb) { + struct unbalanced_load_stats *pcpu_load; + + pcpu_load =3D this_cpu_ptr(bond_info->unbalanced_load); + u64_stats_update_begin(&pcpu_load->syncp); + u64_stats_add(&pcpu_load->tx_bytes, skb->len); + u64_stats_update_end(&pcpu_load->syncp); + } } =20 if (tx_slave && bond_slave_can_tx(tx_slave)) { @@ -1529,19 +1546,28 @@ netdev_tx_t bond_alb_xmit(struct sk_buff *skb, stru= ct net_device *bond_dev) return bond_do_alb_xmit(skb, bond, tx_slave); } =20 -static u32 reset_unbalanced_load(struct alb_bond_info *bond_info) +static u64 reset_unbalanced_load(struct alb_bond_info *bond_info) { struct unbalanced_load_stats *p; - u32 total_bytes =3D 0; + u64 tx_bytes, total_bytes =3D 0; + unsigned int start; int i; =20 for_each_possible_cpu(i) { p =3D per_cpu_ptr(bond_info->unbalanced_load, i); - total_bytes +=3D READ_ONCE(p->tx_bytes); - WRITE_ONCE(p->tx_bytes, 0); + do { + start =3D u64_stats_fetch_begin(&p->syncp); + tx_bytes =3D u64_stats_read(&p->tx_bytes); + } while (u64_stats_fetch_retry(&p->syncp, start)); + + u64_stats_update_begin(&p->syncp); + u64_stats_set(&p->tx_bytes, 0); + u64_stats_update_end(&p->syncp); + + total_bytes +=3D tx_bytes; } =20 - return total_bytes / BOND_TLB_REBALANCE_INTERVAL; + return div_u64(total_bytes, BOND_TLB_REBALANCE_INTERVAL); } =20 void bond_alb_monitor(struct work_struct *work) diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_mai= n.c index 9fb44e0031c8..4c4d9bf71e0c 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -6495,7 +6495,7 @@ static int bond_init(struct net_device *bond_dev) if (!bond->wq) return -ENOMEM; =20 - bond->alb_info.unbalanced_load =3D alloc_percpu(struct unbalanced_load_st= ats); + bond->alb_info.unbalanced_load =3D netdev_alloc_pcpu_stats(struct unbalan= ced_load_stats); if (!bond->alb_info.unbalanced_load) goto wq_out; =20 diff --git a/include/net/bond_alb.h b/include/net/bond_alb.h index 3fabf4714dec..51c083c76115 100644 --- a/include/net/bond_alb.h +++ b/include/net/bond_alb.h @@ -57,12 +57,12 @@ struct tlb_client_info { * packets to a Client that the Hash function * gave this entry index. */ - u32 tx_bytes; /* Each Client accumulates the BytesTx that + u64 tx_bytes; /* Each Client accumulates the BytesTx that * were transmitted to it, and after each * CallBack the LoadHistory is divided * by the balance interval */ - u32 load_history; /* This field contains the amount of Bytes + u64 load_history; /* This field contains the amount of Bytes * that were transmitted to this client by * the server on the previous balance * interval in Bps. @@ -118,13 +118,14 @@ struct tlb_slave_info { * are the entries that were assigned to use this * slave for transmit. */ - u32 load; /* Each slave sums the loadHistory of all clients + u64 load; /* Each slave sums the loadHistory of all clients * assigned to it */ }; =20 struct unbalanced_load_stats { - u32 tx_bytes; + u64_stats_t tx_bytes; + struct u64_stats_sync syncp; }; =20 struct alb_bond_info { --=20 2.55.0