]> www.infradead.org Git - users/griffoul/linux.git/commitdiff
net: hibmcge: fix the division by zero issue
authorJijie Shao <shaojijie@huawei.com>
Wed, 6 Aug 2025 10:27:57 +0000 (18:27 +0800)
committerJakub Kicinski <kuba@kernel.org>
Fri, 8 Aug 2025 18:48:49 +0000 (11:48 -0700)
When the network port is down, the queue is released, and ring->len is 0.
In debugfs, hbg_get_queue_used_num() will be called,
which may lead to a division by zero issue.

This patch adds a check, if ring->len is 0,
hbg_get_queue_used_num() directly returns 0.

Fixes: 40735e7543f9 ("net: hibmcge: Implement .ndo_start_xmit function")
Signed-off-by: Jijie Shao <shaojijie@huawei.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/hisilicon/hibmcge/hbg_txrx.h

index 2883a5899ae29e41a54837bc753f2bec9a477781..8b6110599e10db28e1302ed2708c985adab82bf4 100644 (file)
@@ -29,7 +29,12 @@ static inline bool hbg_fifo_is_full(struct hbg_priv *priv, enum hbg_dir dir)
 
 static inline u32 hbg_get_queue_used_num(struct hbg_ring *ring)
 {
-       return (ring->ntu + ring->len - ring->ntc) % ring->len;
+       u32 len = READ_ONCE(ring->len);
+
+       if (!len)
+               return 0;
+
+       return (READ_ONCE(ring->ntu) + len - READ_ONCE(ring->ntc)) % len;
 }
 
 netdev_tx_t hbg_net_start_xmit(struct sk_buff *skb, struct net_device *netdev);