Skip to contentSkip to conversation

net: hns3: fix multiple bugs in reset/error paths

PATCH Applied Jijie Shao 5 participants 16 messages Nov 18 – Nov 26

Subsystems (4)
lore
Share & tools
lore

Jijie Shao

author maintainer
#0 lore raw

v5 cover net: hns3: fix multiple bugs in reset/error paths

This series contains four bug fixes for the HNS3 driver, all related to resource lifecycle management on reset, driver unload, or error paths.

Patch 1 fixes a vector resource leak in the error path of hns3_nic_alloc_vector_data, where a devm_kcalloc failure after successful get_vector() leaves allocated vectors unreleased.

Patch 2 fixes a page_pool leak in the error path of hns3_alloc_ring_memory, where an hns3_alloc_ring_buffers() failure for an RX ring frees the descriptor ring but not the page_pool allocated just before.

Patch 3 fixes an interface stall that occurs when the hardware RX queue is full and memory allocation fails. Without an RX interrupt, NAPI is never scheduled and the queue cannot drain. A delayed work re-arms NAPI polling on affected rings to break the deadlock.

Patch 4 fixes a use-after-free in debugfs read callbacks that can access priv->ring and ring->desc while reset, driver unload, or ethtool ring resize is freeing those resources. A mutex is added to synchronize debugfs readers against resource teardown.

A note on how these were found. Patch 1 came out of internal code review; it needs devm_kcalloc() to fail after get_vector() has succeeded, so we have not hit it at runtime. Patch 2 was reported by Sashiko against v2 and is likewise an error-path only issue that we have not triggered.

Patch 3 is a real customer problem. Under memory pressure the RX ring filled up, RX interrupts stopped coming in and the interface went dead. There was no stack trace - the interface going dead was the only symptom we had to go on.

Patch 4 was hit in the lab while reading the debugfs queue files during a reset/unload; it ended in a NULL pointer dereference and a hung machine.

All four were built and run on a Huawei Kunpeng EVB with the on-board hns3 NIC. Patches 1 and 2 were not exercised on their failure paths.

---
Changes in v5:
- Patch 3: cancel oom_task and free the bitmap together in hns3_uninit_all_ring(), before the rings are released.
- Patch 3: use only atomic bitops in hns3_oom_task() and drop

    bitmap_zero().

- Patch 3: count rx_oom_cnt for each NAPI poll that hits an RX allocation failure, and drop the duplicate update from hns3_oom_task_schedule().
- Patch 3: correct the description of the exit refill and the teardown ordering.
- Patch 4: document the page_pool_info and error-code changes in the commit message.
- Patch 1 and 2: no change.
v4: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- Patch 1: correct the reason for resetting priv->vector_num, as reported by Sashiko. hns3_nic_dealloc_vector_data() is not reachable after hns3_nic_alloc_vector_data() fails, so the reset is not protecting that path; it keeps the count from describing vectors that were already handed back.
- Patch 2: name hns3_fini_ring() instead of the non-existent hns3_free_ring_memory(), and drop "mirroring" because the destroy order differs. The pool is destroyed before the descriptor ring here, which is safe because hns3_alloc_ring_buffers() has already detached every buffer.
- Patch 3:
* Allocate rx_ring_oom_bm in hns3_init_all_ring() only after every ring has been set up, and free it in hns3_uninit_all_ring(). This fixes the leak and the NULL bitmap dereference reported for hns3_set_ringparam(), which calls hns3_init_all_ring() without a matching hns3_uninit_all_ring(); the allocation is reused when the pointer is already set.
* Cancel the delayed work in hns3_nic_uninit_vector_data() instead of hns3_nic_net_down(), so every teardown path cancels it before the bitmap is freed. This closes the use-after-free reported for the self-test and external loopback paths, which set DOWN without going through hns3_nic_net_down().
* Drop the cancel and bitmap_zero from hns3_nic_net_down(). A pending work that runs while DOWN is set zeroes the bitmap and exits, and one that runs after the interface is brought back up only schedules a single extra poll round.
* Always attempt the exit refill in hns3_clean_rx_ring(), so the descriptors cleaned by this poll stay armed and the head pointer is written as before.
* Document why rx_oom_cnt is not the standard rx-alloc-fail counter, as requested in review.
- Patch 4: describe both writer paths (client_uninit/reset_uninit vs set_ringparam) explicitly, note the -EBUSY user-visible change, and return -EBUSY from hns3_dbg_page_pool_info() instead of -EFAULT so that sibling readers agree.
v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Patch 1: simplify commit message; drop the accumulated-leak consequence paragraph and clarify that the vector_num reset avoids a NULL deref in hns3_nic_dealloc_vector_data().
- Patch 2: new patch, fixes a page_pool leak in the error path of

    hns3_alloc_ring_memory().

- Patch 3: rework the OOM bitmap from per-vector to per-ring, indexed by ring->queue_index. Add cancel_delayed_work_sync() in hns3_nic_net_down() after hns3_disable_irqs_and_tqps(). Remove the OOM marking in hns3_handle_rx_copybreak() since the packet is still delivered via the original page.
- Patch 4: add Fixes tag for the ring resize path. Add per-ring page_pool NULL check at hns3_dump_page_pool_info() entry.
v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
Patch 1:
- No change.
Patch 2:
- Clear per-vector OOM bitmap when device is DOWN to avoid spurious napi_schedule() on next up event.
- Move rx_oom_cnt accounting to hns3_ring_set_oom_state() with test_and_set_bit() for per-event counting.
Patch 3:
- Drop hclge_debugfs.c changes; mutex only protects enet-layer.
- Replace guard(mutex) with explicit mutex_lock/unlock.
- Remove redundant NULL checks; unify -EPERM to -EBUSY.
v1: https://lore.kernel.org/all/[email protected]/
---

Jijie Shao (4):
net: hns3: fix vector resource leak in hns3_nic_alloc_vector_data
net: hns3: fix page_pool leak in hns3_alloc_ring_memory error path
net: hns3: fix interface stuck after OOM with full hardware rx queue
net: hns3: fix use-after-free in debugfs read during reset/unload

 drivers/net/ethernet/hisilicon/hns3/hnae3.h   |   1 +
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 127 +++++++++++-------
 .../net/ethernet/hisilicon/hns3/hns3_enet.c   |  79 ++++++++++-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |  18 +++
 .../ethernet/hisilicon/hns3/hns3_ethtool.c    |   3 +
 5 files changed, 178 insertions(+), 50 deletions(-)

base-commit: 2a324a58a084c5cfd7e5a2f55e0786bee170a4a5

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v5 1/4 net: hns3: fix vector resource leak in hns3_nic_alloc_vector_data

When devm_kcalloc() fails for priv->tqp_vector after get_vector() has already acquired MSI-X vectors, the error path returns without calling put_vector() to release them, leaving the hclge/hclgevf bookkeeping (num_msi_used, num_msi_left, vector_status[]) inconsistent.

Call put_vector() for every acquired vector before returning the error, and reset priv->vector_num to 0 so the count never describes vectors that were already handed back.

Fixes dd38c72604dc ("net: hns3: fix for coalesce configuration lost during reset")

---
Changes in v4:
- Correct the reason for resetting priv->vector_num. Sashiko pointed out that hns3_nic_dealloc_vector_data() is not reachable after hns3_nic_alloc_vector_data() fails, so the reset is not protecting that path; it keeps the count from describing vectors that were already handed back.
v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Simplify commit message: drop the accumulated-leak consequence paragraph and clarify that the vector_num reset avoids a NULL deref in hns3_nic_dealloc_vector_data().
v2: https://lore.kernel.org/all/[email protected]/

---
 drivers/net/ethernet/hisilicon/hns3/hns3_enet.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +8 -2
index db4f584c4ce1..e2fb5899bba1 100644
@@ -4851,7 +4851,7 @@ static int hns3_nic_alloc_vector_data(struct hns3_nic_priv *priv)
 		devm_kzalloc_objs(&pdev->dev, *priv->tqp_vector, vector_num);
 	if (!priv->tqp_vector) {
 		ret = -ENOMEM;
-		goto out;
+		goto err_put_vector;
 	}
 
 	for (i = 0; i < priv->vector_num; i++) {
@@ -4862,7 +4862,13 @@ static int hns3_nic_alloc_vector_data(struct hns3_nic_priv *priv)
 		hns3_vector_coalesce_init(tqp_vector, priv);
 	}
 
-out:
+	devm_kfree(&pdev->dev, vector);
+	return 0;
+
+err_put_vector:
+	for (i = 0; i < vector_num; i++)
+		h->ae_algo->ops->put_vector(h, vector[i].vector);
+	priv->vector_num = 0;
 	devm_kfree(&pdev->dev, vector);
 	return ret;
 }
Signature · 1 line
2.43.0
reviewer Reviewed-by on v5 1/4
#2 lore raw

Re: [PATCH v5 net 1/4] net: hns3: fix vector resource leak in hns3_nic_alloc_vector_data

Quoted 11 lines · Jijie Shao

When devm_kcalloc() fails for priv->tqp_vector after get_vector() has already acquired MSI-X vectors, the error path returns without calling put_vector() to release them, leaving the hclge/hclgevf bookkeeping (num_msi_used, num_msi_left, vector_status[]) inconsistent.

Call put_vector() for every acquired vector before returning the error, and reset priv->vector_num to 0 so the count never describes vectors that were already handed back.

Fixes dd38c72604dc ("net: hns3: fix for coalesce configuration lost during reset")
Quoted 13 lines

---
Changes in v4:
- Correct the reason for resetting priv->vector_num. Sashiko pointed out that hns3_nic_dealloc_vector_data() is not reachable after hns3_nic_alloc_vector_data() fails, so the reset is not protecting that path; it keeps the count from describing vectors that were already handed back.
v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Simplify commit message: drop the accumulated-leak consequence paragraph and clarify that the vector_num reset avoids a NULL deref in hns3_nic_dealloc_vector_data().
v2: https://lore.kernel.org/all/[email protected]/

author maintainer
#3 lore raw

PATCH v5 2/4 net: hns3: fix page_pool leak in hns3_alloc_ring_memory error path

In reply to Jijie Shao

In hns3_alloc_ring_memory(), when hns3_alloc_ring_buffers() fails for an RX ring, the error path jumps to out_with_desc, which frees the descriptor ring but does not destroy the page_pool that was just allocated by hns3_alloc_page_pool(). The page_pool and its pages are leaked.

Destroy the page_pool in that error path as well. The pool is torn down before the descriptor ring here, which is the opposite of the order in hns3_fini_ring(); that is safe because hns3_alloc_ring_buffers() has already detached every buffer it attached, so hns3_free_desc() walks descriptors whose dma is zero and never hands a page back to the pool.

Fixes 93188e9642c3 ("net: hns3: support skb's frag page recycling based on page pool")

---
Changes in v4:
- Name hns3_fini_ring() instead of the non-existent hns3_free_ring_memory(), and drop "mirroring" because the destroy order differs. The pool is destroyed before the descriptor ring here, which is safe because hns3_alloc_ring_buffers() has already detached every buffer.
v3: https://lore.kernel.org/all/[email protected]/

---
 drivers/net/ethernet/hisilicon/hns3/hns3_enet.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +2 -0
index e2fb5899bba1..5677e32fc3da 100644
@@ -5075,6 +5075,8 @@ static int hns3_alloc_ring_memory(struct hns3_enet_ring *ring)
 	return 0;
 
 out_with_desc:
+	page_pool_destroy(ring->page_pool);
+	ring->page_pool = NULL;
 	hns3_free_desc(ring);
 out_with_desc_cb:
 	devm_kfree(ring_to_dev(ring), ring->desc_cb);
Signature · 1 line
2.43.0
reviewer Reviewed-by on v5 2/4
#4 lore raw

Re: [PATCH v5 net 2/4] net: hns3: fix page_pool leak in hns3_alloc_ring_memory error path

Quoted 14 lines · Jijie Shao

In hns3_alloc_ring_memory(), when hns3_alloc_ring_buffers() fails for an RX ring, the error path jumps to out_with_desc, which frees the descriptor ring but does not destroy the page_pool that was just allocated by hns3_alloc_page_pool(). The page_pool and its pages are leaked.

Destroy the page_pool in that error path as well. The pool is torn down before the descriptor ring here, which is the opposite of the order in hns3_fini_ring(); that is safe because hns3_alloc_ring_buffers() has already detached every buffer it attached, so hns3_free_desc() walks descriptors whose dma is zero and never hands a page back to the pool.

Fixes 93188e9642c3 ("net: hns3: support skb's frag page recycling based on page pool")
Quoted 8 lines

---
Changes in v4:
- Name hns3_fini_ring() instead of the non-existent hns3_free_ring_memory(), and drop "mirroring" because the destroy order differs. The pool is destroyed before the descriptor ring here, which is safe because hns3_alloc_ring_buffers() has already detached every buffer.
v3: https://lore.kernel.org/all/[email protected]/

author maintainer
#5 lore raw

PATCH v5 3/4 net: hns3: fix interface stuck after OOM with full hardware rx queue

In reply to Jijie Shao

NAPI polling on hns3 is driven by hardware RX interrupts. Under memory pressure, RX skb and frag allocations fail, packets are no longer reaped from the hardware rx ring. Once the ring fills, the hardware stops generating RX interrupts and NAPI is never scheduled again -- even after memory becomes available, leaving the interface deadlocked.

Break the deadlock by adding a software recovery path that re-arms NAPI via a 250ms delayed_work, independent of hardware interrupts. A per-ring bitmap tracks which RX rings are in OOM state; the work function schedules NAPI for the vector backing each affected ring. OOM state is marked when an RX allocation failure is detected in the poll path and cleared when the delayed_work fires.

This supersedes the busy-poll recovery from commit 68752b24f51a ("net: hns3: schedule the polling again when allocation fails"), which returned budget on allocation failure to keep NAPI polling and caused CPU spinning under prolonged memory pressure. Now hns3_clean_rx_ring() returns recv_pkts instead of budget on allocation failure, letting NAPI exit and the delayed_work take over. The exit refill is still attempted for the descriptors accumulated since the last refill, so the head pointer is written as before; descriptors from earlier iterations are not re-armed when an allocation failure short-circuits the in-loop refill.

Add a per-ring rx_oom_cnt ethtool counter, incremented for each NAPI poll that hits an RX allocation failure. It is a diagnostic counter for this recovery path, not the standard rx-alloc-fail qstat.

The bitmap is allocated in hns3_init_all_ring() and freed in hns3_uninit_all_ring(). It is allocated after every ring is set up, so a failure inside hns3_init_all_ring() leaves nothing behind, and it is reused because hns3_set_ringparam() calls hns3_init_all_ring() without hns3_uninit_all_ring(). The delayed_work is cancelled in hns3_uninit_all_ring() before the rings and the bitmap are released, and also in hns3_nic_uninit_vector_data() before the NAPI instances are torn down.

hns3_nic_net_down() does not cancel the work, so a pending work may still run after the interface goes down. If it runs while DOWN is set, it clears the per-ring bits without scheduling NAPI. If the interface is brought back up within the 250ms window, it sees the stale bits with DOWN cleared and schedules NAPI for those rings once, ending in a single extra poll round with no packet to clean. Both outcomes are harmless.

Fixes 68752b24f51a ("net: hns3: schedule the polling again when allocation fails")

---
Changes in v5:
- Cancel oom_task and free the bitmap together in hns3_uninit_all_ring(), before the rings are released.
- Use only atomic bitops in hns3_oom_task() and drop bitmap_zero().
- Count rx_oom_cnt for each NAPI poll that hits an RX allocation failure, and drop the duplicate update from hns3_oom_task_schedule().
- Correct the description of the exit refill and the teardown ordering.
v4: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- Allocate rx_ring_oom_bm in hns3_init_all_ring() only after every ring has been set up, and free it in hns3_uninit_all_ring(). This fixes the leak and the NULL bitmap dereference in the hns3_set_ringparam() path, which calls hns3_init_all_ring() without a matching hns3_uninit_all_ring(), and reuses the existing allocation on a ring resize.
- Cancel the delayed work in hns3_nic_uninit_vector_data() instead of hns3_nic_net_down(), so every teardown path cancels it before the bitmap is freed. The self-test and external loopback paths set DOWN without going through hns3_nic_net_down().
- Drop the cancel and bitmap_zero from hns3_nic_net_down(). A pending work that runs while DOWN is set zeroes the bitmap and exits, and one that runs after the interface is brought back up only schedules a single extra poll round.
- Always attempt the exit refill in hns3_clean_rx_ring(), so the descriptors cleaned by this poll stay armed and the head pointer is written as before.
- Document why rx_oom_cnt is not the standard rx-alloc-fail counter.
v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Rework the OOM bitmap from per-vector to per-ring, indexed by ring->queue_index, so each ring's OOM event is counted exactly once per oom_task firing cycle regardless of vector sharing.
- Add cancel_delayed_work_sync(&priv->oom_task) to hns3_nic_net_down(), placed after hns3_disable_irqs_and_tqps(), eliminating the stop/open window where a preempted worker could clear a bit newly set by NAPI on the reopened interface.
- Remove the hns3_ring_set_oom_state() call from hns3_handle_rx_copybreak(): napi_alloc_frag() failure returns -ENOMEM but the packet is still delivered via the original page, so it should not drive the OOM recovery work.
v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Clear per-vector OOM bitmap when device is DOWN to avoid spurious napi_schedule() on next up event.
- Move rx_oom_cnt accounting to hns3_ring_set_oom_state() with test_and_set_bit() for per-event counting.
- Expand changelog: reference commit 68752b24f51a, explain hns3_clean_rx_ring() return value change, document rx_oom_cnt per-event counting semantics.
v1: https://lore.kernel.org/all/[email protected]/

---
 .../net/ethernet/hisilicon/hns3/hns3_enet.c   | 60 ++++++++++++++++++-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   | 18 ++++++
 .../ethernet/hisilicon/hns3/hns3_ethtool.c    |  1 +
 3 files changed, 76 insertions(+), 3 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +57 -3
index 5677e32fc3da..60fc296020ab 100644
@@ -78,6 +78,8 @@ module_param(page_pool_enabled, bool, 0400);
 #define HNS3_MIN_TX_LEN		33U
 #define HNS3_MIN_TUN_PKT_LEN	65U
 
+#define HNS3_OOM_POLL_INTERVAL_MS	250
+
 /* hns3_pci_tbl - PCI Device ID Table
  *
  * Last entry must be all 0s
@@ -4161,6 +4163,7 @@ static int hns3_add_frag(struct hns3_enet_ring *ring)
 			if (unlikely(!new_skb)) {
 				hns3_rl_err(ring_to_netdev(ring),
 					    "alloc rx fraglist skb fail\n");
+				hns3_ring_set_oom_state(ring);
 				return -ENXIO;
 			}
 
@@ -4450,6 +4453,30 @@ static int hns3_handle_rx_bd(struct hns3_enet_ring *ring)
 	return 0;
 }
 
+static void hns3_oom_task(struct work_struct *work)
+{
+	struct hns3_nic_priv *priv = container_of(work, struct hns3_nic_priv,
+						  oom_task.work);
+	bool down = test_bit(HNS3_NIC_STATE_DOWN, &priv->state);
+	struct hnae3_handle *h = priv->ae_handle;
+	struct hns3_enet_ring *ring;
+	u16 i;
+
+	for (i = 0; i < h->kinfo.num_tqps; i++)
+		if (test_and_clear_bit(i, priv->rx_ring_oom_bm) && !down) {
+			ring = &priv->ring[i + h->kinfo.num_tqps];
+			napi_schedule(&ring->tqp_vector->napi);
+		}
+}
+
+static void hns3_oom_task_schedule(struct hns3_enet_ring *ring)
+{
+	struct hns3_nic_priv *priv = netdev_priv(ring_to_netdev(ring));
+
+	schedule_delayed_work(&priv->oom_task,
+			      msecs_to_jiffies(HNS3_OOM_POLL_INTERVAL_MS));
+}
+
 int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
 		       void (*rx_fn)(struct hns3_enet_ring *, struct sk_buff *))
 {
@@ -4471,6 +4498,9 @@ int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
 
 		/* Poll one pkt */
 		err = hns3_handle_rx_bd(ring);
+		if (unlikely(err == -ENOMEM))
+			failure = true;
+
 		/* Do not get FE for the packet or failed to alloc skb */
 		if (unlikely(!ring->skb || err == -ENXIO)) {
 			goto out;
@@ -4489,10 +4519,15 @@ int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
 	 * FBD number with head pointer
 	 */
 	if (unused_count > 0)
-		failure = failure ||
-			  hns3_nic_alloc_rx_buffers(ring, unused_count);
+		failure |= hns3_nic_alloc_rx_buffers(ring, unused_count);
+
+	if (unlikely(failure))
+		hns3_ring_set_oom_state(ring);
 
-	return failure ? budget : recv_pkts;
+	if (unlikely(hns3_ring_is_oom_state(ring)))
+		hns3_oom_task_schedule(ring);
+
+	return recv_pkts;
 }
 
 static void hns3_update_rx_int_coalesce(struct hns3_enet_tqp_vector *tqp_vector)
@@ -4787,6 +4822,7 @@ static int hns3_nic_init_vector_data(struct hns3_nic_priv *priv)
 			       hns3_nic_common_poll);
 	}
 
+	INIT_DELAYED_WORK(&priv->oom_task, hns3_oom_task);
 	return 0;
 
 map_ring_fail:
@@ -4886,6 +4922,8 @@ static void hns3_nic_uninit_vector_data(struct hns3_nic_priv *priv)
 	struct hns3_enet_tqp_vector *tqp_vector;
 	int i;
 
+	cancel_delayed_work_sync(&priv->oom_task);
+
 	for (i = 0; i < priv->vector_num; i++) {
 		tqp_vector = &priv->tqp_vector[i];
 
@@ -5201,6 +5239,16 @@ int hns3_init_all_ring(struct hns3_nic_priv *priv)
 		cond_resched();
 	}
 
+	/* hns3_set_ringparam() calls this without hns3_uninit_all_ring(). */
+	if (!priv->rx_ring_oom_bm) {
+		priv->rx_ring_oom_bm = bitmap_zalloc(h->kinfo.num_tqps,
+						     GFP_KERNEL);
+		if (!priv->rx_ring_oom_bm) {
+			ret = -ENOMEM;
+			goto out_when_alloc_ring_memory;
+		}
+	}
+
 	return 0;
 
 out_when_alloc_ring_memory:
@@ -5215,6 +5263,12 @@ static void hns3_uninit_all_ring(struct hns3_nic_priv *priv)
 	struct hnae3_handle *h = priv->ae_handle;
 	int i;
 
+	cancel_delayed_work_sync(&priv->oom_task);
+	if (priv->rx_ring_oom_bm) {
+		bitmap_free(priv->rx_ring_oom_bm);
+		priv->rx_ring_oom_bm = NULL;
+	}
+
 	for (i = 0; i < h->kinfo.num_tqps; i++) {
 		hns3_fini_ring(&priv->ring[i]);
 		hns3_fini_ring(&priv->ring[i + h->kinfo.num_tqps]);
drivers/net/ethernet/hisilicon/hns3/hns3_enet.h +18 -0
index 933e3527ed82..fe2e88b9eff7 100644
@@ -449,6 +449,7 @@ struct ring_stats {
 			u64 non_reuse_pg;
 			u64 frag_alloc_err;
 			u64 frag_alloc;
+			u64 rx_oom_cnt;
 		};
 		__le16 csum;
 	};
@@ -585,6 +586,8 @@ struct hns3_nic_priv {
 	struct hns3_enet_tqp_vector *tqp_vector;
 	u16 vector_num;
 	u8 max_non_tso_bd_num;
+	struct delayed_work oom_task;
+	unsigned long *rx_ring_oom_bm;
 
 	u64 tx_timeout_count;
 
@@ -711,6 +714,21 @@ static inline unsigned int hns3_page_order(struct hns3_enet_ring *ring)
 #define hns3_rl_usec_to_reg(int_rl) ((int_rl) >> 2)
 #define hns3_rl_round_down(int_rl) round_down(int_rl, 4)
 
+static inline void hns3_ring_set_oom_state(struct hns3_enet_ring *ring)
+{
+	struct hns3_nic_priv *priv = netdev_priv(ring_to_netdev(ring));
+
+	hns3_ring_stats_update(ring, rx_oom_cnt);
+	set_bit(ring->queue_index, priv->rx_ring_oom_bm);
+}
+
+static inline bool hns3_ring_is_oom_state(struct hns3_enet_ring *ring)
+{
+	struct hns3_nic_priv *priv = netdev_priv(ring_to_netdev(ring));
+
+	return test_bit(ring->queue_index, priv->rx_ring_oom_bm);
+}
+
 void hns3_ethtool_set_ops(struct net_device *netdev);
 int hns3_set_channels(struct net_device *netdev,
 		      struct ethtool_channels *ch);
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c +1 -0
index 7b92a3c73ef6..14642f81efe1 100644
@@ -62,6 +62,7 @@ static const struct hns3_stats hns3_rxq_stats[] = {
 	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
 	HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
 	HNS3_TQP_STAT("frag_alloc", frag_alloc),
+	HNS3_TQP_STAT("rx_oom_cnt", rx_oom_cnt),
 };
 
 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v5 net 3/4] net: hns3: fix interface stuck after OOM with full hardware rx queue

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] hns3_oom_task() (hns3_enet.c:4456-4470) reads HNS3_NIC_STATE_DOWN once into the local down at entry.
- [Medium] The patch adds a new private ethtool -S string, HNS3_TQP_STAT("rx_oom_cnt", rx_oom_cnt), to hns3_rxq_stats[] in…
- [Medium] rx_oom_cnt can be incremented twice in one NAPI poll, which contradicts the stated v5 semantics ("Count rx_oom_cnt for each NAPI…

--

net: hns3: fix interface stuck after OOM with full hardware rx queue

This patch adds a 250ms delayed_work that re-schedules NAPI for RX rings that hit an allocation failure. A per-ring bitmap tracks those rings. The patch also replaces the earlier approach of returning budget from hns3_clean_rx_ring() on allocation failure, and adds a per-ring rx_oom_cnt ethtool counter.

Quoted 4 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +0 -0
index 5677e32fc3da4..60fc296020ab2 100644

[ ... ]

Quoted 17 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +6 -0
@@ -4161,6 +4163,7 @@ static int hns3_add_frag(struct hns3_enet_ring *ring)
 			if (unlikely(!new_skb)) {
 				hns3_rl_err(ring_to_netdev(ring),
 					    "alloc rx fraglist skb fail\n");
+				hns3_ring_set_oom_state(ring);
 				return -ENXIO;
 			}
 
@@ -4450,6 +4453,30 @@ static int hns3_handle_rx_bd(struct hns3_enet_ring *ring)
 	return 0;
 }
 
+static void hns3_oom_task(struct work_struct *work)
+{
+	struct hns3_nic_priv *priv = container_of(work, struct hns3_nic_priv,
+						  oom_task.work);
+	bool down = test_bit(HNS3_NIC_STATE_DOWN, &priv->state);

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c static int hns3_handle_rx_bd(struct hns3_enet_ring *ring) · line 4470

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +10 -0
+	struct hnae3_handle *h = priv->ae_handle;
+	struct hns3_enet_ring *ring;
+	u16 i;
+
+	for (i = 0; i < h->kinfo.num_tqps; i++)
+		if (test_and_clear_bit(i, priv->rx_ring_oom_bm) && !down) {
+			ring = &priv->ring[i + h->kinfo.num_tqps];
+			napi_schedule(&ring->tqp_vector->napi);
+		}
+}

[Severity: High]
Can the stale down value here lose a recovery bit that was set after the interface came back up?

hns3_nic_net_down() doesn't cancel oom_task, so an instance can still be running while hns3_nic_net_up() runs. Consider this sequence:

hns3_oom_task()
down = test_bit(HNS3_NIC_STATE_DOWN, ...); /* true */
<preempted before reaching ring i>

hns3_nic_net_up()
clear_bit(HNS3_NIC_STATE_DOWN, &priv->state); hns3_enable_irqs_and_tqps(netdev);

NAPI poll on ring i, allocation fails hns3_clean_rx_ring()
hns3_ring_set_oom_state(ring); /* sets bit i */
hns3_oom_task_schedule(ring); /* queues new run */
return recv_pkts; /* NAPI completes */

hns3_oom_task() resumes
test_and_clear_bit(i, ...) returns true, but down is still true, so napi_schedule() is skipped and bit i is now clear

250ms later the newly queued instance finds bit i clear and does nothing.

hns3_clean_rx_ring() now returns recv_pkts instead of budget, so NAPI has already exited. If ring i has no armed RX buffers left, the hardware won't raise another RX interrupt. Wouldn't the ring then stay stuck? That is the condition this patch is meant to fix.

The commit message says:

If it runs while DOWN is set, it clears the per-ring bits without scheduling NAPI. [ ... ] Both outcomes are harmless.

That doesn't seem to hold for this interleaving. Two possible ways to close the window:

- return early without clearing any bits when DOWN is set, or
- re-check DOWN after each test_and_clear_bit() and set the bit again.

Would either of these work here?

Quoted 3 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +3 -0
+
+static void hns3_oom_task_schedule(struct hns3_enet_ring *ring)
+{

[ ... ]

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget, · line 4525

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +4 -2
@@ -4489,10 +4519,15 @@ int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
 	 * FBD number with head pointer
 	 */
 	if (unused_count > 0)
-		failure = failure ||
-			  hns3_nic_alloc_rx_buffers(ring, unused_count);
+		failure |= hns3_nic_alloc_rx_buffers(ring, unused_count);
+
+	if (unlikely(failure))
+		hns3_ring_set_oom_state(ring);

[Severity: Medium]
Can rx_oom_cnt be incremented twice in a single NAPI poll here?

The commit message says the counter is "incremented for each NAPI poll that hits an RX allocation failure". When the fraglist napi_alloc_skb() fails in hns3_add_frag(), the path looks like this:

hns3_clean_rx_ring()
hns3_handle_rx_bd()

    hns3_add_frag()
      hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ */
      return -ENXIO;
  goto out;
  failure |= hns3_nic_alloc_rx_buffers(ring, unused_count);
  if (unlikely(failure))
      hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ again */

-ENXIO doesn't match the new err == -ENOMEM check. But failure can still be true, either from a failed exit refill or from an earlier failed in-loop refill. Under the memory pressure that caused the fraglist failure, the refill is likely to fail too.

hns3_ring_set_oom_state() has no per-poll dedup, so in this case the counter goes up by two for one poll.

[ ... ]

drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c static const struct hns3_stats hns3_rxq_stats[] = { · line 65

Quoted 9 lines
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c +1 -0
index 7b92a3c73ef6d..14642f81efe1a 100644
@@ -62,6 +62,7 @@ static const struct hns3_stats hns3_rxq_stats[] = {
 	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
 	HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
 	HNS3_TQP_STAT("frag_alloc", frag_alloc),
+	HNS3_TQP_STAT("rx_oom_cnt", rx_oom_cnt),

[Severity: Medium]
Should this value go through the standard per-queue rx-alloc-fail qstat rather than a new ethtool -S string?

hns3_ring_set_oom_state() increments rx_oom_cnt on RX skb and buffer allocation failures. That is the event the alloc_fail field of struct netdev_queue_stats_rx tracks. Documentation/netlink/specs/netdev.yaml describes rx-alloc-fail as:

Number of times skb or buffer allocation failed on the Rx datapath. Allocation failure may, or may not result in a packet drop, depending on driver implementation and whether system recovers quickly.

The commit message says:

It is a diagnostic counter for this recovery path, not the standard rx-alloc-fail qstat.

Counting per poll instead of per allocation looks like a granularity choice, and the standard definition leaves that choice to the driver.

hns3 doesn't implement netdev_stat_ops or get_queue_stats_rx yet. Implementing them would expose this value through the standard uAPI.

The same failures already increment the existing sw_err_cnt ("dropped") counter in hns3_alloc_skb() and hns3_nic_alloc_rx_buffers().

This patch carries a Fixes: tag, and the new user-visible counter makes it harder to take through the net tree as-is. Could the counter be split out of this fix?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118130330.3026625-1-shaojijie%40huawei.com
author maintainer
#7 lore raw

Re: [PATCH v5 net 3/4] net: hns3: fix interface stuck after OOM with full hardware rx queue

Quoted 98 lines · on 2026/11/22 21:24, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] hns3_oom_task() (hns3_enet.c:4456-4470) reads HNS3_NIC_STATE_DOWN once into the local down at entry.
- [Medium] The patch adds a new private ethtool -S string, HNS3_TQP_STAT("rx_oom_cnt", rx_oom_cnt), to hns3_rxq_stats[] in…
- [Medium] rx_oom_cnt can be incremented twice in one NAPI poll, which contradicts the stated v5 semantics ("Count rx_oom_cnt for each NAPI…

--

net: hns3: fix interface stuck after OOM with full hardware rx queue

This patch adds a 250ms delayed_work that re-schedules NAPI for RX rings that hit an allocation failure. A per-ring bitmap tracks those rings. The patch also replaces the earlier approach of returning budget from hns3_clean_rx_ring() on allocation failure, and adds a per-ring rx_oom_cnt ethtool counter.

Quoted 4 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +0 -0
index 5677e32fc3da4..60fc296020ab2 100644

[ ... ]

Quoted 17 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +6 -0
@@ -4161,6 +4163,7 @@ static int hns3_add_frag(struct hns3_enet_ring *ring)
  			if (unlikely(!new_skb)) {
  				hns3_rl_err(ring_to_netdev(ring),
  					    "alloc rx fraglist skb fail\n");
+				hns3_ring_set_oom_state(ring);
  				return -ENXIO;
  			}
 
@@ -4450,6 +4453,30 @@ static int hns3_handle_rx_bd(struct hns3_enet_ring *ring)
  	return 0;
  }
 
+static void hns3_oom_task(struct work_struct *work)
+{
+	struct hns3_nic_priv *priv = container_of(work, struct hns3_nic_priv,
+						  oom_task.work);
+	bool down = test_bit(HNS3_NIC_STATE_DOWN, &priv->state);

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c static int hns3_handle_rx_bd(struct hns3_enet_ring *ring) · line 4470

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +10 -0
+	struct hnae3_handle *h = priv->ae_handle;
+	struct hns3_enet_ring *ring;
+	u16 i;
+
+	for (i = 0; i < h->kinfo.num_tqps; i++)
+		if (test_and_clear_bit(i, priv->rx_ring_oom_bm) && !down) {
+			ring = &priv->ring[i + h->kinfo.num_tqps];
+			napi_schedule(&ring->tqp_vector->napi);
+		}
+}

[Severity: High]
Can the stale down value here lose a recovery bit that was set after the interface came back up?

hns3_nic_net_down() doesn't cancel oom_task, so an instance can still be running while hns3_nic_net_up() runs. Consider this sequence:

hns3_oom_task()
down = test_bit(HNS3_NIC_STATE_DOWN, ...); /* true */
<preempted before reaching ring i>

hns3_nic_net_up()
clear_bit(HNS3_NIC_STATE_DOWN, &priv->state); hns3_enable_irqs_and_tqps(netdev);

NAPI poll on ring i, allocation fails hns3_clean_rx_ring()
hns3_ring_set_oom_state(ring); /* sets bit i */
hns3_oom_task_schedule(ring); /* queues new run */
return recv_pkts; /* NAPI completes */

hns3_oom_task() resumes
test_and_clear_bit(i, ...) returns true, but down is still true, so napi_schedule() is skipped and bit i is now clear

250ms later the newly queued instance finds bit i clear and does nothing.

hns3_clean_rx_ring() now returns recv_pkts instead of budget, so NAPI has already exited. If ring i has no armed RX buffers left, the hardware won't raise another RX interrupt. Wouldn't the ring then stay stuck? That is the condition this patch is meant to fix.

The commit message says:

If it runs while DOWN is set, it clears the per-ring bits without scheduling NAPI. [ ... ] Both outcomes are harmless.

Quoted 7 lines

That doesn't seem to hold for this interleaving. Two possible ways to close the window:

- return early without clearing any bits when DOWN is set, or
- re-check DOWN after each test_and_clear_bit() and set the bit again.

Would either of these work here?

I don't think this is reachable. hns3_nic_net_up() rebuilds the rings before it clears HNS3_NIC_STATE_DOWN:

	ret = hns3_nic_reset_all_ring(h);
	if (ret)
		return ret;
	clear_bit(HNS3_NIC_STATE_DOWN, &priv->state);

If the rebuild fails, DOWN stays set and net_open() returns an error, so the interface never comes up. If it succeeds, hns3_nic_reset_all_ring() calls hns3_reuse_buffer() on every descriptor, so the ring is full when the interface comes up.

Quoted 33 lines
Quoted 4 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +3 -0
+
+static void hns3_oom_task_schedule(struct hns3_enet_ring *ring)
+{

[ ... ]

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget, · line 4525

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +4 -2
@@ -4489,10 +4519,15 @@ int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
  	 * FBD number with head pointer
  	 */
  	if (unused_count > 0)
-		failure = failure ||
-			  hns3_nic_alloc_rx_buffers(ring, unused_count);
+		failure |= hns3_nic_alloc_rx_buffers(ring, unused_count);
+
+	if (unlikely(failure))
+		hns3_ring_set_oom_state(ring);

[Severity: Medium]
Can rx_oom_cnt be incremented twice in a single NAPI poll here?

The commit message says the counter is "incremented for each NAPI poll that hits an RX allocation failure". When the fraglist napi_alloc_skb() fails in hns3_add_frag(), the path looks like this:

hns3_clean_rx_ring()
hns3_handle_rx_bd()

     hns3_add_frag()
       hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ */
       return -ENXIO;
   goto out;
   failure |= hns3_nic_alloc_rx_buffers(ring, unused_count);
   if (unlikely(failure))
       hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ again */
Quoted 7 lines

-ENXIO doesn't match the new err == -ENOMEM check. But failure can still be true, either from a failed exit refill or from an earlier failed in-loop refill. Under the memory pressure that caused the fraglist failure, the refill is likely to fail too.

hns3_ring_set_oom_state() has no per-poll dedup, so in this case the counter goes up by two for one poll.

The second increment only happens when a second allocation actually failed. hns3_add_frag() counts its failed napi_alloc_skb() and returns -ENXIO, which doesn't set failure, so the exit refill still runs and bumps the counter again only if it fails too. Two increments mean two failures. I agree "for each NAPI poll" can read as at most once per poll, that's just imprecise wording.

Quoted 36 lines

[ ... ]

drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c static const struct hns3_stats hns3_rxq_stats[] = { · line 65

Quoted 9 lines
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c +1 -0
index 7b92a3c73ef6d..14642f81efe1a 100644
@@ -62,6 +62,7 @@ static const struct hns3_stats hns3_rxq_stats[] = {
  	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
  	HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
  	HNS3_TQP_STAT("frag_alloc", frag_alloc),
+	HNS3_TQP_STAT("rx_oom_cnt", rx_oom_cnt),

[Severity: Medium]
Should this value go through the standard per-queue rx-alloc-fail qstat rather than a new ethtool -S string?

hns3_ring_set_oom_state() increments rx_oom_cnt on RX skb and buffer allocation failures. That is the event the alloc_fail field of struct netdev_queue_stats_rx tracks. Documentation/netlink/specs/netdev.yaml describes rx-alloc-fail as:

Number of times skb or buffer allocation failed on the Rx datapath. Allocation failure may, or may not result in a packet drop, depending on driver implementation and whether system recovers quickly.

The commit message says:

It is a diagnostic counter for this recovery path, not the standard rx-alloc-fail qstat.

Counting per poll instead of per allocation looks like a granularity choice, and the standard definition leaves that choice to the driver.

hns3 doesn't implement netdev_stat_ops or get_queue_stats_rx yet. Implementing them would expose this value through the standard uAPI.

Quoted 6 lines

The same failures already increment the existing sw_err_cnt ("dropped") counter in hns3_alloc_skb() and hns3_nic_alloc_rx_buffers().

This patch carries a Fixes: tag, and the new user-visible counter makes it harder to take through the net tree as-is. Could the counter be split out of this fix?

I answered this in v4 and the reasoning still holds. hns3 has no netdev_stat_ops, alloc_fail is an exact failure count while this is a diagnostic for the recovery path, and the existing per-queue stats and customer tooling read ethtool -S. The new string is additive and doesn't change any existing one.

maintainer
#8 lore raw

Re: [PATCH v5 net 3/4] net: hns3: fix interface stuck after OOM with full hardware rx queue

Quoted 36 lines · Jijie Shao
Quoted 27 lines · on 2026/11/22 21:24, [email protected] wrote:

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget, · line 4525

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +4 -2
@@ -4489,10 +4519,15 @@ int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
        * FBD number with head pointer
        */
       if (unused_count > 0)
-        failure = failure ||
-              hns3_nic_alloc_rx_buffers(ring, unused_count);
+        failure |= hns3_nic_alloc_rx_buffers(ring, unused_count);
+
+    if (unlikely(failure))
+        hns3_ring_set_oom_state(ring);

[Severity: Medium]
Can rx_oom_cnt be incremented twice in a single NAPI poll here?

The commit message says the counter is "incremented for each NAPI poll that hits an RX allocation failure". When the fraglist napi_alloc_skb() fails in hns3_add_frag(), the path looks like this:

hns3_clean_rx_ring()
   hns3_handle_rx_bd()
     hns3_add_frag()
       hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ */ return -ENXIO;
   goto out;
   failure |= hns3_nic_alloc_rx_buffers(ring, unused_count); if (unlikely(failure)) hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ again */

Quoted 7 lines

-ENXIO doesn't match the new err == -ENOMEM check. But failure can still be true, either from a failed exit refill or from an earlier failed in-loop refill. Under the memory pressure that caused the fraglist failure, the refill is likely to fail too.

hns3_ring_set_oom_state() has no per-poll dedup, so in this case the counter goes up by two for one poll.

Quoted 6 lines

The second increment only happens when a second allocation actually failed.  hns3_add_frag() counts its failed napi_alloc_skb() and returns -ENXIO, which doesn't set failure, so the exit refill still runs and bumps the counter again only if it fails too.  Two increments mean two failures.  I agree "for each NAPI poll" can read as at most once per poll, that's just imprecise wording.

I agree another repost is not needed here. However patch 1, 2 and 4 are net-next material and only patch 3 is borderline net material.

To avoid a repost and splitting, I would apply everything to net-next.

/P

author maintainer
#9 lore raw

Re: [PATCH v5 net 3/4] net: hns3: fix interface stuck after OOM with full hardware rx queue

Quoted 45 lines · Paolo Abeni
Quoted 38 lines · Jijie Shao
Quoted 28 lines · on 2026/11/22 21:24, [email protected] wrote:

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c int hns3_clean_rx_ring(struct · line 4526

Quoted 11 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +4 -2
@@ -4489,10 +4519,15 @@ int hns3_clean_rx_ring(struct 
 hns3_enet_ring *ring, int budget,
        * FBD number with head pointer
        */
       if (unused_count > 0)
-        failure = failure ||
-              hns3_nic_alloc_rx_buffers(ring, unused_count);
+        failure |= hns3_nic_alloc_rx_buffers(ring, unused_count);
+
+    if (unlikely(failure))
+        hns3_ring_set_oom_state(ring);

[Severity: Medium]
Can rx_oom_cnt be incremented twice in a single NAPI poll here?

The commit message says the counter is "incremented for each NAPI poll that hits an RX allocation failure". When the fraglist napi_alloc_skb() fails in hns3_add_frag(), the path looks like this:

hns3_clean_rx_ring()
   hns3_handle_rx_bd()
     hns3_add_frag()
       hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ */ return -ENXIO;
   goto out;
   failure |= hns3_nic_alloc_rx_buffers(ring, unused_count); if (unlikely(failure)) hns3_ring_set_oom_state(ring);   /* rx_oom_cnt++ again */

Quoted 8 lines

-ENXIO doesn't match the new err == -ENOMEM check. But failure can still
be true, either from a failed exit refill or from an earlier failed in-loop refill. Under the memory pressure that caused the fraglist failure, the refill is likely to fail too.

hns3_ring_set_oom_state() has no per-poll dedup, so in this case the counter goes up by two for one poll.

Quoted 6 lines

The second increment only happens when a second allocation actually failed.  hns3_add_frag() counts its failed napi_alloc_skb() and returns -ENXIO, which doesn't set failure, so the exit refill still runs and bumps the counter again only if it fails too.  Two increments mean two failures.  I agree "for each NAPI poll" can read as at most once per poll, that's just imprecise wording.

Quoted 4 lines

I agree another repost is not needed here. However patch 1, 2 and 4 are net-next material and only patch 3 is borderline net material.

To avoid a repost and splitting, I would apply everything to net-next.

Applying the whole series to net-next is fine with me

Thanks,
Jijie Shao

reviewer Reviewed-by on v5 3/4
#10 lore raw

Re: [PATCH v5 net 3/4] net: hns3: fix interface stuck after OOM with full hardware rx queue

In reply to Jijie Shao
Quoted 46 lines · Jijie Shao

NAPI polling on hns3 is driven by hardware RX interrupts. Under memory pressure, RX skb and frag allocations fail, packets are no longer reaped from the hardware rx ring. Once the ring fills, the hardware stops generating RX interrupts and NAPI is never scheduled again -- even after memory becomes available, leaving the interface deadlocked.

Break the deadlock by adding a software recovery path that re-arms NAPI via a 250ms delayed_work, independent of hardware interrupts. A per-ring bitmap tracks which RX rings are in OOM state; the work function schedules NAPI for the vector backing each affected ring. OOM state is marked when an RX allocation failure is detected in the poll path and cleared when the delayed_work fires.

This supersedes the busy-poll recovery from commit 68752b24f51a ("net: hns3: schedule the polling again when allocation fails"), which returned budget on allocation failure to keep NAPI polling and caused CPU spinning under prolonged memory pressure. Now hns3_clean_rx_ring() returns recv_pkts instead of budget on allocation failure, letting NAPI exit and the delayed_work take over. The exit refill is still attempted for the descriptors accumulated since the last refill, so the head pointer is written as before; descriptors from earlier iterations are not re-armed when an allocation failure short-circuits the in-loop refill.

Add a per-ring rx_oom_cnt ethtool counter, incremented for each NAPI poll that hits an RX allocation failure. It is a diagnostic counter for this recovery path, not the standard rx-alloc-fail qstat.

The bitmap is allocated in hns3_init_all_ring() and freed in hns3_uninit_all_ring(). It is allocated after every ring is set up, so a failure inside hns3_init_all_ring() leaves nothing behind, and it is reused because hns3_set_ringparam() calls hns3_init_all_ring() without hns3_uninit_all_ring(). The delayed_work is cancelled in hns3_uninit_all_ring() before the rings and the bitmap are released, and also in hns3_nic_uninit_vector_data() before the NAPI instances are torn down.

hns3_nic_net_down() does not cancel the work, so a pending work may still run after the interface goes down. If it runs while DOWN is set, it clears the per-ring bits without scheduling NAPI. If the interface is brought back up within the 250ms window, it sees the stale bits with DOWN cleared and schedules NAPI for those rings once, ending in a single extra poll round with no packet to clean. Both outcomes are harmless.

Quoted 2 lines
Fixes 68752b24f51a ("net: hns3: schedule the polling again when allocation fails")
author maintainer
#11 lore raw

PATCH v5 4/4 net: hns3: fix use-after-free in debugfs read during reset/unload

In reply to Jijie Shao

The seq_file refactoring of hns3 debugfs removed the per-file entry state checks. Without them, debugfs show callbacks can access priv->ring, priv->tqp_vector, and ring->desc while these resources are being freed by reset, driver unload, or ethtool ring resize.

Plain state-bit checks leave a TOCTOU window between check and use.

Fix by adding a mutex (dbg_mutex) to struct hnae3_handle, used by enet-layer debugfs callbacks. Readers acquire the mutex, then check device state per iteration -- this lets an in-flight reader exit promptly when reset begins mid-dump, keeping reset latency bounded by one iteration, and also rejects a reader that arrives after the writer has finished tearing down resources (priv->ring is NULL by then). The dbg_uninit() call stays outside the mutex to avoid deadlock with debugfs_remove_recursive().

Two kinds of writer paths are protected:

* hns3_client_uninit() and hns3_reset_notify_uninit_enet() clear HNS3_NIC_STATE_INITED first so that readers running outside the mutex see the device as busy and return -EBUSY early, then take dbg_mutex before freeing resources.

* hns3_set_ringparam() calls ndo_stop() first (which sets HNS3_NIC_STATE_DOWN) and then takes dbg_mutex around the ring re-allocation. The busy check does not test the DOWN bit, so readers that arrive here are blocked by the mutex itself rather than by the INITED signal.

Userspace-visible changes: debugfs reads under /sys/kernel/debug/hns3/*/ that race with reset, driver unload, or ethtool ring resize now return -EBUSY instead of potentially reading freed or zero-filled memory. The per-iteration busy error is -EBUSY instead of -EPERM, and a NULL priv->ring now returns -EBUSY instead of -EFAULT. Reading page_pool_info when page pools are not initialized now succeeds and prints "page pool not initialized" per queue instead of failing with -EFAULT.

Fixes a723fb8efe29d3 ("net: hns3: refine for set ring parameters")
Fixes eced3d1c41db ("net: hns3: use seq_file for files in queue/ in debugfs")

---
Changes in v5:
- Document the page_pool_info and error-code changes in the commit message.
v4: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- Describe both writer paths (client_uninit/reset_uninit vs set_ringparam) explicitly and note the -EBUSY user-visible change.
- Return -EBUSY from hns3_dbg_page_pool_info() instead of -EFAULT

    when priv->ring is NULL, matching the sibling readers.

v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Add Fixes tag for a723fb8efe29d3 ("net: hns3: refine for set ring parameters"): the ring resize path it introduced is the writer that can free and rebuild rings, which is the root cause of the UAF window exposed by the seq_file refactoring.
- Add per-ring page_pool NULL check at the entry of hns3_dump_page_pool_info(), avoiding a NULL deref while holding dbg_mutex (which would deadlock reset on the mutex).
v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Drop hclge_debugfs.c changes; mutex only protects enet-layer debugfs callbacks.
- Replace guard(mutex) with explicit mutex_lock/unlock and "goto unlock" error-path labels.
- Remove redundant if (!priv->ring) checks in

    hns3_dbg_rx_queue_info() and hns3_dbg_tx_queue_info().

- Unify per-iteration return values from -EPERM to -EBUSY.
- Reorder local variables to reverse-Christmas-tree order.
- Reword changelog to drop the "PF-layer" description.
v1: https://lore.kernel.org/all/[email protected]/

---
 drivers/net/ethernet/hisilicon/hns3/hnae3.h   |   1 +
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 127 +++++++++++-------
 .../net/ethernet/hisilicon/hns3/hns3_enet.c   |   7 +
 .../ethernet/hisilicon/hns3/hns3_ethtool.c    |   2 +
 4 files changed, 92 insertions(+), 45 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hnae3.h +1 -0
index 4286af9239b0..e94cc33da864 100644
@@ -944,6 +944,7 @@ struct hnae3_handle {
 
 	u8 netdev_flags;
 	struct dentry *hnae3_dbgfs;
+	struct mutex dbg_mutex;	/* protect debugfs against reset/unload */
 
 	/* Network interface message level enabled bits */
 	u32 msg_enable;
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +82 -45

248 lines not on this page · raw message

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +7 -0
index 60fc296020ab..d9583fa0d102 100644
@@ -5452,6 +5452,7 @@ static int hns3_client_init(struct hnae3_handle *handle)
 	priv->min_tx_copybreak = 0;
 	priv->min_tx_spare_buf_size = 0;
 	set_bit(HNS3_NIC_STATE_DOWN, &priv->state);
+	mutex_init(&handle->dbg_mutex);
 
 	handle->msg_enable = netif_msg_init(debug, DEFAULT_MSG_LEVEL);
 
@@ -5565,6 +5566,7 @@ static int hns3_client_init(struct hnae3_handle *handle)
 	priv->ring = NULL;
 out_get_ring_cfg:
 	priv->ae_handle = NULL;
+	mutex_destroy(&handle->dbg_mutex);
 	free_netdev(netdev);
 	return ret;
 }
@@ -5588,6 +5590,7 @@ static void hns3_client_uninit(struct hnae3_handle *handle, bool reset)
 
 	hns3_free_rx_cpu_rmap(netdev);
 
+	mutex_lock(&handle->dbg_mutex);
 	hns3_nic_uninit_irq(priv);
 
 	hns3_clear_all_ring(handle, true);
@@ -5599,9 +5602,11 @@ static void hns3_client_uninit(struct hnae3_handle *handle, bool reset)
 	hns3_uninit_all_ring(priv);
 
 	hns3_put_ring_config(priv);
+	mutex_unlock(&handle->dbg_mutex);
 
 out_netdev_free:
 	hns3_dbg_uninit(handle);
+	mutex_destroy(&handle->dbg_mutex);
 	free_netdev(netdev);
 }
 
@@ -5878,6 +5883,7 @@ static int hns3_reset_notify_uninit_enet(struct hnae3_handle *handle)
 		return 0;
 	}
 
+	mutex_lock(&handle->dbg_mutex);
 	hns3_free_rx_cpu_rmap(netdev);
 	hns3_nic_uninit_irq(priv);
 	hns3_clear_all_ring(handle, true);
@@ -5890,6 +5896,7 @@ static int hns3_reset_notify_uninit_enet(struct hnae3_handle *handle)
 	hns3_uninit_all_ring(priv);
 
 	hns3_put_ring_config(priv);
+	mutex_unlock(&handle->dbg_mutex);
 
 	return 0;
 }
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c +2 -0
index 14642f81efe1..7005d8b2d010 100644
@@ -1252,6 +1252,7 @@ static int hns3_set_ringparam(struct net_device *ndev,
 	if (if_running)
 		ndev->netdev_ops->ndo_stop(ndev);
 
+	mutex_lock(&h->dbg_mutex);
 	hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num,
 				    new_ringparam.rx_desc_num);
 	hns3_change_rx_buf_len(ndev, new_ringparam.rx_buf_len);
@@ -1270,6 +1271,7 @@ static int hns3_set_ringparam(struct net_device *ndev,
 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
 			hns3_fini_ring(&tmp_rings[i]);
 	}
+	mutex_unlock(&h->dbg_mutex);
 
 	kfree(tmp_rings);
 
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH v5 net 4/4] net: hns3: fix use-after-free in debugfs read during reset/unload

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The comment on the new struct hnae3_handle::dbg_mutex field in drivers/net/ethernet/hisilicon/hns3/hnae3.h reads "protect debugfs…

--

net: hns3: fix use-after-free in debugfs read during reset/unload

This patch adds a dbg_mutex to struct hnae3_handle. The enet-layer debugfs show callbacks take it, and so do the reset, driver unload and ethtool ring resize paths, which keeps the readers away from priv->ring, priv->tqp_vector and ring->desc while those are being freed. The readers also check the device state on every iteration and return -EBUSY when the device is busy.

drivers/net/ethernet/hisilicon/hns3/hnae3.h struct hnae3_handle { · line 947

Quoted 9 lines
drivers/net/ethernet/hisilicon/hns3/hnae3.h +1 -0
index 4286af9239b01..e94cc33da8643 100644
@@ -944,6 +944,7 @@ struct hnae3_handle {
 
 	u8 netdev_flags;
 	struct dentry *hnae3_dbgfs;
+	struct mutex dbg_mutex;	/* protect debugfs against reset/unload */

[Severity: Low]
This isn't a bug, but is this comment a little narrower and a little broader than what the lock actually does?

On the writer side, hns3_set_ringparam() also takes h->dbg_mutex around hns3_init_all_ring(), the memcpy() restore and hns3_fini_ring() on the backup rings:

drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c static int hns3_set_ringparam(struct net_device *ndev, · line 1257

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c +1 -0
@@ -1252,6 +1252,7 @@ static int hns3_set_ringparam(struct net_device *ndev,
 	if (if_running)
 		ndev->netdev_ops->ndo_stop(ndev);
 
+	mutex_lock(&h->dbg_mutex);
 	hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num,
 				    new_ringparam.rx_desc_num);

Ring resize is neither a reset nor an unload, so the field comment does not mention it.

On the reader side, only the enet-layer seq_file callbacks take the mutex (coal_info, rx/tx_queue_info, queue_map, page_pool_info, bd_info). The hclge-layer readers that hns3_dbg_common_init_t2() registers through ops->dbg_get_read_func() live under the same handle->hnae3_dbgfs tree, and they never take it.

The commit message says the mutex is "used by enet-layer debugfs callbacks", but this comment is the only place in the tree that describes the lock's scope.

Could the comment say that the lock serializes the enet-layer debugfs readers against the paths that free or rebuild priv->ring, priv->tqp_vector and ring->desc (reset, unload and ring resize)? Then a future writer would know it has to take the lock, and an hclge-layer reader would not assume it is protected.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118130330.3026625-1-shaojijie%40huawei.com
author maintainer
#13 lore raw

Re: [PATCH v5 net 4/4] net: hns3: fix use-after-free in debugfs read during reset/unload

Quoted 61 lines · on 2026/11/22 21:24, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The comment on the new struct hnae3_handle::dbg_mutex field in drivers/net/ethernet/hisilicon/hns3/hnae3.h reads "protect debugfs…

--

net: hns3: fix use-after-free in debugfs read during reset/unload

This patch adds a dbg_mutex to struct hnae3_handle. The enet-layer debugfs show callbacks take it, and so do the reset, driver unload and ethtool ring resize paths, which keeps the readers away from priv->ring, priv->tqp_vector and ring->desc while those are being freed. The readers also check the device state on every iteration and return -EBUSY when the device is busy.

drivers/net/ethernet/hisilicon/hns3/hnae3.h struct hnae3_handle { · line 947

Quoted 9 lines
drivers/net/ethernet/hisilicon/hns3/hnae3.h +1 -0
index 4286af9239b01..e94cc33da8643 100644
@@ -944,6 +944,7 @@ struct hnae3_handle {
 
  	u8 netdev_flags;
  	struct dentry *hnae3_dbgfs;
+	struct mutex dbg_mutex;	/* protect debugfs against reset/unload */

[Severity: Low]
This isn't a bug, but is this comment a little narrower and a little broader than what the lock actually does?

On the writer side, hns3_set_ringparam() also takes h->dbg_mutex around hns3_init_all_ring(), the memcpy() restore and hns3_fini_ring() on the backup rings:

drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c static int hns3_set_ringparam(struct net_device *ndev, · line 1257

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c +1 -0
@@ -1252,6 +1252,7 @@ static int hns3_set_ringparam(struct net_device *ndev,
  	if (if_running)
  		ndev->netdev_ops->ndo_stop(ndev);
 
+	mutex_lock(&h->dbg_mutex);
  	hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num,
  				    new_ringparam.rx_desc_num);

Ring resize is neither a reset nor an unload, so the field comment does not mention it.

On the reader side, only the enet-layer seq_file callbacks take the mutex (coal_info, rx/tx_queue_info, queue_map, page_pool_info, bd_info). The hclge-layer readers that hns3_dbg_common_init_t2() registers through ops->dbg_get_read_func() live under the same handle->hnae3_dbgfs tree, and they never take it.

The commit message says the mutex is "used by enet-layer debugfs callbacks", but this comment is the only place in the tree that describes the lock's scope.

Quoted 5 lines

Could the comment say that the lock serializes the enet-layer debugfs readers against the paths that free or rebuild priv->ring, priv->tqp_vector and ring->desc (reset, unload and ring resize)? Then a future writer would know it has to take the lock, and an hclge-layer reader would not assume it is protected.

It states what the lock is for. I think a struct member comment isn't meant to enumerate every path that takes it, and covering the main cases should be enough.

reviewer Reviewed-by on v5 4/4
#14 lore raw

Re: [PATCH v5 net 4/4] net: hns3: fix use-after-free in debugfs read during reset/unload

In reply to Jijie Shao
Quoted 38 lines · Jijie Shao

The seq_file refactoring of hns3 debugfs removed the per-file entry state checks. Without them, debugfs show callbacks can access priv->ring, priv->tqp_vector, and ring->desc while these resources are being freed by reset, driver unload, or ethtool ring resize.

Plain state-bit checks leave a TOCTOU window between check and use.

Fix by adding a mutex (dbg_mutex) to struct hnae3_handle, used by enet-layer debugfs callbacks. Readers acquire the mutex, then check device state per iteration -- this lets an in-flight reader exit promptly when reset begins mid-dump, keeping reset latency bounded by one iteration, and also rejects a reader that arrives after the writer has finished tearing down resources (priv->ring is NULL by then). The dbg_uninit() call stays outside the mutex to avoid deadlock with debugfs_remove_recursive().

Two kinds of writer paths are protected:

* hns3_client_uninit() and hns3_reset_notify_uninit_enet() clear HNS3_NIC_STATE_INITED first so that readers running outside the mutex see the device as busy and return -EBUSY early, then take dbg_mutex before freeing resources.

* hns3_set_ringparam() calls ndo_stop() first (which sets HNS3_NIC_STATE_DOWN) and then takes dbg_mutex around the ring re-allocation. The busy check does not test the DOWN bit, so readers that arrive here are blocked by the mutex itself rather than by the INITED signal.

Userspace-visible changes: debugfs reads under /sys/kernel/debug/hns3/*/ that race with reset, driver unload, or ethtool ring resize now return -EBUSY instead of potentially reading freed or zero-filled memory. The per-iteration busy error is -EBUSY instead of -EPERM, and a NULL priv->ring now returns -EBUSY instead of -EFAULT. Reading page_pool_info when page pools are not initialized now succeeds and prints "page pool not initialized" per queue instead of failing with -EFAULT.

Quoted 4 lines
Fixes a723fb8efe29d3 ("net: hns3: refine for set ring parameters")
Fixes eced3d1c41db ("net: hns3: use seq_file for files in queue/ in debugfs")
#15 lore raw
In reply to Jijie Shao
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Wed, 18 Nov 2026 21:03:26 +0800 you wrote:

This series contains four bug fixes for the HNS3 driver, all related to resource lifecycle management on reset, driver unload, or error paths.

Patch 1 fixes a vector resource leak in the error path of hns3_nic_alloc_vector_data, where a devm_kcalloc failure after successful get_vector() leaves allocated vectors unreleased.

[...]

Here is the summary with links:
- [v5,net,1/4] net: hns3: fix vector resource leak in hns3_nic_alloc_vector_data

    https://git.kernel.org/netdev/net-next/c/23d8fa6b29eb
  - [v5,net,2/4] net: hns3: fix page_pool leak in hns3_alloc_ring_memory error path
    https://git.kernel.org/netdev/net-next/c/6f22fbbb9c55
  - [v5,net,3/4] net: hns3: fix interface stuck after OOM with full hardware rx queue
    https://git.kernel.org/netdev/net-next/c/72c054a2d6c5
  - [v5,net,4/4] net: hns3: fix use-after-free in debugfs read during reset/unload
    https://git.kernel.org/netdev/net-next/c/c699cd194801

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html