From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A7C543A7E9 for ; Fri, 24 Jul 2026 14:19:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784902754; cv=none; b=JziVAt2EN2k1VsqA5JVykacOWBpfNX/2ErkSNM3fb1IGGtsiV4N5TElkNtPciThj4O0UQFN1rKTSXen/d8FP5Oa/CVLt7vmNZcT2zMN6Y8o9dWoCJGC/eW7NEiIZpAeY/jWkVMjM8RjzdPTogexzvj29VJPLPhCfsCI9+vsQfXQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784902754; c=relaxed/simple; bh=ob4J+U080i8ImmOVbb+QW0QA1MHoV58gu39+VNwXi8c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LH/FEK2g27PcsUa57KwTd2h7M7k+8HIcdJhOe0aVLV10B/r18vSZu5u/5XPYaADkRCD53DTIgNQtHBPW4PB45YmZQCTYe1gCpFE7VmrlCrm+OND/7aZT3nE1L+0UD16gQFQb+G2S4ovjyrYHWrhAmzzL18HbP7YBNB4RVcVHO5s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KMoMFut+; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KMoMFut+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 877E81F000E9; Fri, 24 Jul 2026 14:19:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784902753; bh=2d95gBKRLIRPk46z3u1WxiEdY2SddpLr9a2RJusilXQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KMoMFut+T+LCM0cz8EhklSgmEcWwA+8XxPD4JMHcpKsJfJX81zqHsPyfqAkzr0RWG SE7cJ5aTK5bvw2/hoQI+OrAgefiOLPHgRk+4zBKyT4UCnMc0XrYLczepVcewCMLL4K FL2QQIRhyBgasrHrPUb2mri30MJGeZL9+y0RBl3bBJMY0S2rzEHPTbp3XM90AkZF76 U5SLMDfJwDpMOekbwHGH99sMe/aTa+fzg74C2+xc3RxB25JCvB/J/7eUc2FG2hibD0 AnokW8PXXZTUCAV8gkQaKQmLXX0Xbojj6vnuKz0WbIroaPOIQNmx2rgk8tgQm9LXL+ kO1B4XuWJ0reA== From: Lorenzo Bianconi Date: Fri, 24 Jul 2026 16:18:57 +0200 Subject: [PATCH net-next v11 2/3] net: airoha: fix ETS QoS stats counter underflow and cross-channel corruption Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260724-airoha-ethtool-priv_flags-v11-2-686dea300a40@kernel.org> References: <20260724-airoha-ethtool-priv_flags-v11-0-686dea300a40@kernel.org> In-Reply-To: <20260724-airoha-ethtool-priv_flags-v11-0-686dea300a40@kernel.org> To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Lorenzo Bianconi Cc: Simon Horman , Alexander Lobakin , linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, netdev@vger.kernel.org, Jacob Keller X-Mailer: b4 0.14.3 airoha_qdma_get_tx_ets_stats() has two bugs: - The hardware counters read via airoha_qdma_rr() are 32-bit values but are stored in u64 locals and subtracted from u64 baselines. When a 32-bit hardware counter wraps around, the subtraction produces a large underflow value passed to _bstats_update(). - The baseline counters (cpu_tx_packets, fwd_tx_packets) are stored as single per-device fields, but airoha_qdma_get_tx_ets_stats() is called with different channel values (0-3). Each call reads a different channel's hardware counter but overwrites the same baseline, corrupting the delta computation for other channels. Fix both by: - Narrowing the counter locals and baselines to u32 so that 32-bit unsigned subtraction handles wrap-around naturally. - Grouping the baselines into a per-channel qos_stats array so each channel tracks its own previous counter value independently. - Splitting the delta addition into two statements so the first u32 delta is widened to u64 on assignment and the second is added in u64 arithmetic, preventing overflow when both deltas are large. Fixes: 20bf7d07c956 ("net: airoha: Add sched ETS offload support") Reviewed-by: Simon Horman Reviewed-by: Alexander Lobakin Reviewed-by: Jacob Keller Signed-off-by: Lorenzo Bianconi --- drivers/net/ethernet/airoha/airoha_eth.c | 18 +++++++++++------- drivers/net/ethernet/airoha/airoha_eth.h | 7 ++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/net/ethernet/airoha/airoha_eth.c b/drivers/net/ethernet/airoha/airoha_eth.c index d9a44a11d8db..8165084daaf5 100644 --- a/drivers/net/ethernet/airoha/airoha_eth.c +++ b/drivers/net/ethernet/airoha/airoha_eth.c @@ -2521,16 +2521,20 @@ static int airoha_qdma_get_tx_ets_stats(struct net_device *netdev, int channel, { struct airoha_gdm_dev *dev = netdev_priv(netdev); struct airoha_qdma *qdma = dev->qdma; + u32 cpu_tx_packets, fwd_tx_packets; + u64 tx_packets; - u64 cpu_tx_packets = airoha_qdma_rr(qdma, REG_CNTR_VAL(channel << 1)); - u64 fwd_tx_packets = airoha_qdma_rr(qdma, - REG_CNTR_VAL((channel << 1) + 1)); - u64 tx_packets = (cpu_tx_packets - dev->cpu_tx_packets) + - (fwd_tx_packets - dev->fwd_tx_packets); + cpu_tx_packets = airoha_qdma_rr(qdma, REG_CNTR_VAL(channel << 1)); + fwd_tx_packets = airoha_qdma_rr(qdma, + REG_CNTR_VAL((channel << 1) + 1)); + tx_packets = (u32)(cpu_tx_packets - + dev->qos_stats[channel].cpu_tx_packets); + tx_packets += (u32)(fwd_tx_packets - + dev->qos_stats[channel].fwd_tx_packets); _bstats_update(opt->stats.bstats, 0, tx_packets); - dev->cpu_tx_packets = cpu_tx_packets; - dev->fwd_tx_packets = fwd_tx_packets; + dev->qos_stats[channel].cpu_tx_packets = cpu_tx_packets; + dev->qos_stats[channel].fwd_tx_packets = fwd_tx_packets; return 0; } diff --git a/drivers/net/ethernet/airoha/airoha_eth.h b/drivers/net/ethernet/airoha/airoha_eth.h index 24dd16fc509a..447a5a9552bb 100644 --- a/drivers/net/ethernet/airoha/airoha_eth.h +++ b/drivers/net/ethernet/airoha/airoha_eth.h @@ -580,9 +580,10 @@ struct airoha_gdm_dev { struct airoha_eth *eth; DECLARE_BITMAP(qos_sq_bmap, AIROHA_NUM_QOS_CHANNELS); - /* qos stats counters */ - u64 cpu_tx_packets; - u64 fwd_tx_packets; + struct { + u32 cpu_tx_packets; + u32 fwd_tx_packets; + } qos_stats[AIROHA_NUM_QOS_CHANNELS]; unsigned long flags; int nbq; -- 2.55.0