Skip to contentSkip to conversation

wifi: mt76: add some npu missing fixes

PATCH New Lorenzo Bianconi 1 participant 3 messages Nov 22

Subsystems (2)
lore
Share & tools
lore

Lorenzo Bianconi

author maintainer
#0 lore raw

cover wifi: mt76: add some npu missing fixes

Hold queue spinlock during mt76 npu napi callback. Do not run mt76_npu_queue_cleanup() for not allocated queues.

---
Lorenzo Bianconi (2):
wifi: mt76: npu: acquire queue lock running npu napi
wifi: mt76: npu: do not run mt76_npu_queue_cleanup() for not allocated queues

 drivers/net/wireless/mediatek/mt76/npu.c | 45 ++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 11 deletions(-)

---
base-commit: 7ec7ce74c41551aa39e0c1d6a69c4a2c66377c32
change-id: 20261122-mt76-npu-fixes-344a4f51ee79

Best regards,

Signature · 1 line
Lorenzo Bianconi <[email protected]>
author maintainer
#1 lore raw

PATCH 1/2 wifi: mt76: npu: acquire queue lock running npu napi

The NPU RX NAPI path updates the ring indices (head/tail/queued) and the pending reassembly state in mt76_npu_fill_rx_queue() and mt76_npu_rx_process_entry() without taking q->lock, while the cleanup and reset paths do. A NAPI poll racing with a queue reset/cleanup can therefore corrupt the ring. Protect the ring bookkeeping with q->lock in both helpers, and drop the lock before calling rx_skb() and mt76_rx_poll_complete() since those can sleep.

Move the dma_idx update out of mt76_npu_rx_process_entry() into mt76_npu_fill_rx_queue(), after the ring has been refilled, so slots are handed back to the NPU only once they point to new buffers. Updating dma_idx as soon as a frame is dequeued lets the NPU DMA into descriptors that still reference buffers already passed up the stack (or still owned by a partially reassembled frame), leading to memory corruption under RX load. Add the missing write barrier before the dma_idx update so the new descriptor addresses are visible to the NPU before it may reuse the slots.

Fixes 7fb554b1b623 ("wifi: mt76: Introduce the NPU generic layer")
---
 drivers/net/wireless/mediatek/mt76/npu.c | 37 +++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 8 deletions(-)
drivers/net/wireless/mediatek/mt76/npu.c +29 -8
index 3ca83985376d..fbefc86001ff 100644
@@ -18,6 +18,8 @@ int mt76_npu_fill_rx_queue(struct mt76_dev *dev, struct mt76_queue *q)
 {
 	int nframes = 0;
 
+	spin_lock_bh(&q->lock);
+
 	while (q->queued < q->ndesc - 1) {
 		struct airoha_npu_rx_dma_desc *desc = (void *)q->desc;
 		struct mt76_queue_entry *e = &q->entry[q->head];
@@ -40,6 +42,11 @@ int mt76_npu_fill_rx_queue(struct mt76_dev *dev, struct mt76_queue *q)
 		nframes++;
 	}
 
+	wmb();
+	Q_WRITE(q, dma_idx, q->tail);
+
+	spin_unlock_bh(&q->lock);
+
 	return nframes;
 }
 
@@ -70,9 +77,13 @@ static int mt76_npu_rx_process_entry(struct napi_struct *napi)
 	enum mt76_rxq_id qid = napi - dev->napi;
 	struct mt76_queue *q = &dev->q_rx[qid];
 	struct airoha_npu_rx_dma_desc *desc = (void *)q->desc;
-	int nframes = q->pending;
+	int nframes, ret = 0;
+	struct sk_buff *skb;
 	u32 info = 0;
 
+	spin_lock_bh(&q->lock);
+
+	nframes = q->pending;
 	do {
 		struct mt76_queue_entry *e = &q->entry[q->tail];
 		u32 ctrl = READ_ONCE(desc[q->tail].ctrl);
@@ -80,12 +91,14 @@ static int mt76_npu_rx_process_entry(struct napi_struct *napi)
 
 		if (!q->queued) {
 			q->pending = nframes;
-			return -EBUSY;
+			ret = -EBUSY;
+			goto error_unlock;
 		}
 
 		if (!FIELD_GET(NPU_RX_DMA_DESC_DONE_MASK, ctrl)) {
 			q->pending = nframes;
-			return -EBUSY;
+			ret = -EBUSY;
+			goto error_unlock;
 		}
 
 		dma_rmb();
@@ -102,8 +115,10 @@ static int mt76_npu_rx_process_entry(struct napi_struct *napi)
 
 		if (!q->rx_head) {
 			q->rx_head = napi_build_skb(e->buf, q->buf_size);
-			if (!q->rx_head)
-				return -ENOMEM;
+			if (!q->rx_head) {
+				ret = -ENOMEM;
+				goto error_unlock;
+			}
 
 			__skb_put(q->rx_head, len);
 			skb_reset_mac_header(q->rx_head);
@@ -123,14 +138,20 @@ static int mt76_npu_rx_process_entry(struct napi_struct *napi)
 		q->queued--;
 	} while (--nframes > 0);
 
-	dev->drv->rx_skb(dev, qid, q->rx_head, &info);
-	mt76_rx_poll_complete(dev, qid, napi);
+	skb = q->rx_head;
 	q->rx_head = NULL;
 	q->pending = 0;
 
-	Q_WRITE(q, dma_idx, q->tail);
+	spin_unlock_bh(&q->lock);
+
+	dev->drv->rx_skb(dev, qid, skb, &info);
+	mt76_rx_poll_complete(dev, qid, napi);
 
 	return 0;
+
+error_unlock:
+	spin_unlock_bh(&q->lock);
+	return ret;
 }
 
 void mt76_npu_check_ppe(struct mt76_dev *dev, struct sk_buff *skb,
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/2 wifi: mt76: npu: do not run mt76_npu_queue_cleanup() for not allocated queues

In reply to Lorenzo Bianconi

mt76_npu_deinit() unconditionally calls mt76_npu_queue_cleanup() on the NPU RX queues, even when the NPU is not active and the queues were never allocated. In that case q->lock was never initialized by mt76_dma_alloc_queue() and mt76_npu_queue_cleanup() takes a zeroed, uninitialized spinlock. This is caught by CONFIG_DEBUG_SPINLOCK ("bad magic") and lockdep, and is reachable on any mt76 device once CONFIG_MT76_NPU is enabled, not just on NPU-capable ones.

Only call mt76_npu_queue_cleanup() when the queue was actually allocated, i.e. when ndesc is set, matching the check already present in mt76_dma_rx_cleanup(). Since mt76_dma_alloc_queue() initializes q->lock before setting ndesc, a non-zero ndesc guarantees the lock is valid.

Run the cleanup while still holding dev->mutex so it is serialized with the rest of the NPU teardown, keeping the dev->mutex -> q->lock order used by mt76_npu_rx_queue_init().

Fixes 7fb554b1b623 ("wifi: mt76: Introduce the NPU generic layer")
---
 drivers/net/wireless/mediatek/mt76/npu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/wireless/mediatek/mt76/npu.c +5 -3
index fbefc86001ff..af5bc3da26ab 100644
@@ -554,8 +554,10 @@ void mt76_npu_deinit(struct mt76_dev *dev)
 	if (ppe_dev)
 		airoha_ppe_put_dev(ppe_dev);
 
-	mutex_unlock(&dev->mutex);
+	if (dev->q_rx[MT_RXQ_NPU0].ndesc)
+		mt76_npu_queue_cleanup(dev, &dev->q_rx[MT_RXQ_NPU0]);
+	if (dev->q_rx[MT_RXQ_NPU1].ndesc)
+		mt76_npu_queue_cleanup(dev, &dev->q_rx[MT_RXQ_NPU1]);
 
-	mt76_npu_queue_cleanup(dev, &dev->q_rx[MT_RXQ_NPU0]);
-	mt76_npu_queue_cleanup(dev, &dev->q_rx[MT_RXQ_NPU1]);
+	mutex_unlock(&dev->mutex);
 }
Signature · 1 line
2.56.1