[PATCH net 2/3] net: hibmcge: fix the division by zero issue

Jijie Shao posted 3 patches 2 months ago
There is a newer version of this series
[PATCH net 2/3] net: hibmcge: fix the division by zero issue
Posted by Jijie Shao 2 months ago
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>
---
 drivers/net/ethernet/hisilicon/hibmcge/hbg_txrx.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_txrx.h b/drivers/net/ethernet/hisilicon/hibmcge/hbg_txrx.h
index 2883a5899ae2..2aecc73f3d49 100644
--- a/drivers/net/ethernet/hisilicon/hibmcge/hbg_txrx.h
+++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_txrx.h
@@ -29,6 +29,9 @@ 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)
 {
+	if (!ring->len)
+		return 0;
+
 	return (ring->ntu + ring->len - ring->ntc) % ring->len;
 }
 
-- 
2.33.0
Re: [PATCH net 2/3] net: hibmcge: fix the division by zero issue
Posted by Simon Horman 2 months ago
On Thu, Jul 31, 2025 at 09:47:48PM +0800, Jijie Shao wrote:
> 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>

Thanks,

Thinking aloud:

I see that hbg_get_queue_used_num() can be called for both RX and TX
rings via the debugfs code hbg_dbg_ring(). And that hbg_net_stop()
clears the RX and TX ring configuration using hbg_txrx_uninit().

So I agree that when the port is down ring-len will be 0.

Reviewed-by: Simon Horman <horms@kernel.org>
Re: [PATCH net 2/3] net: hibmcge: fix the division by zero issue
Posted by Jijie Shao 2 months ago
on 2025/8/1 18:11, Simon Horman wrote:
> On Thu, Jul 31, 2025 at 09:47:48PM +0800, Jijie Shao wrote:
>> 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>
> Thanks,
>
> Thinking aloud:
>
> I see that hbg_get_queue_used_num() can be called for both RX and TX
> rings via the debugfs code hbg_dbg_ring(). And that hbg_net_stop()
> clears the RX and TX ring configuration using hbg_txrx_uninit().

Yes, yes.

> So I agree that when the port is down ring-len will be 0.
>
> Reviewed-by: Simon Horman <horms@kernel.org>
>
>