[PATCH] wifi: mt76: npu: fix RX packet reassembly across NAPI polls
Lorenzo Bianconi
lorenzo at kernel.org
Tue Nov 17 05:43:46 PST 2026
mt76_npu_dequeue() builds an skb from the descriptors that make up a
single NPU RX packet. When a packet spans multiple descriptors and not
all of them are ready yet, the function frees the partially built skb
and returns NULL without advancing q->tail. The descriptors already
consumed are then processed again on the next NAPI poll, even though
their buffers have already been handed to the skb and recycled, leading
to a use-after-free/double-free and possible memory corruption.
Rework RX processing to keep the partially built skb and the number of
outstanding fragments across polls: store the skb in q->rx_head and the
remaining fragment count in the new q->pending field. The new
mt76_npu_rx_process_entry() consumes a whole packet, resuming a
previously started one if needed, and advances q->tail for every
consumed descriptor. Only fully received packets are passed up the
stack.
Release any pending skb in mt76_npu_queue_cleanup() and add the missing
DMA read barrier before accessing the descriptor length and info.
Fixes: 7fb554b1b623 ("wifi: mt76: Introduce the NPU generic layer")
Tested-by: Florian Maurer <fmaurer at disroot.org>
Signed-off-by: Lorenzo Bianconi <lorenzo at kernel.org>
---
drivers/net/wireless/mediatek/mt76/mt76.h | 1 +
drivers/net/wireless/mediatek/mt76/npu.c | 88 ++++++++++++++++++-------------
2 files changed, 51 insertions(+), 38 deletions(-)
diff --git a/drivers/net/wireless/mediatek/mt76/mt76.h b/drivers/net/wireless/mediatek/mt76/mt76.h
index 9e851dea71d9..f52a0e2246be 100644
--- a/drivers/net/wireless/mediatek/mt76/mt76.h
+++ b/drivers/net/wireless/mediatek/mt76/mt76.h
@@ -249,6 +249,7 @@ struct mt76_queue {
int ndesc;
int queued;
int buf_size;
+ int pending;
bool stopped;
bool blocked;
diff --git a/drivers/net/wireless/mediatek/mt76/npu.c b/drivers/net/wireless/mediatek/mt76/npu.c
index c4c7c0af6321..3ca83985376d 100644
--- a/drivers/net/wireless/mediatek/mt76/npu.c
+++ b/drivers/net/wireless/mediatek/mt76/npu.c
@@ -56,61 +56,81 @@ void mt76_npu_queue_cleanup(struct mt76_dev *dev, struct mt76_queue *q)
q->tail = (q->tail + 1) % q->ndesc;
q->queued--;
}
+ if (q->rx_head) {
+ dev_kfree_skb(q->rx_head);
+ q->rx_head = NULL;
+ q->pending = 0;
+ }
spin_unlock_bh(&q->lock);
}
-static struct sk_buff *mt76_npu_dequeue(struct mt76_dev *dev,
- struct mt76_queue *q,
- u32 *info)
+static int mt76_npu_rx_process_entry(struct napi_struct *napi)
{
+ struct mt76_dev *dev = mt76_priv(napi->dev);
+ 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 i, nframes, index = q->tail;
- struct sk_buff *skb = NULL;
+ int nframes = q->pending;
+ u32 info = 0;
+
+ do {
+ struct mt76_queue_entry *e = &q->entry[q->tail];
+ u32 ctrl = READ_ONCE(desc[q->tail].ctrl);
+ int len;
- nframes = FIELD_GET(NPU_RX_DMA_PKT_COUNT_MASK, desc[index].info);
- nframes = max_t(int, nframes, 1);
+ if (!q->queued) {
+ q->pending = nframes;
+ return -EBUSY;
+ }
- for (i = 0; i < nframes; i++) {
- struct mt76_queue_entry *e = &q->entry[index];
- int len = FIELD_GET(NPU_RX_DMA_DESC_CUR_LEN_MASK,
- desc[index].ctrl);
+ if (!FIELD_GET(NPU_RX_DMA_DESC_DONE_MASK, ctrl)) {
+ q->pending = nframes;
+ return -EBUSY;
+ }
- if (!FIELD_GET(NPU_RX_DMA_DESC_DONE_MASK, desc[index].ctrl)) {
- dev_kfree_skb(skb);
- return NULL;
+ dma_rmb();
+ len = FIELD_GET(NPU_RX_DMA_DESC_CUR_LEN_MASK, ctrl);
+ info = desc[q->tail].info;
+ if (nframes == q->pending && !nframes) {
+ nframes = FIELD_GET(NPU_RX_DMA_PKT_COUNT_MASK, info);
+ nframes = max_t(int, nframes, 1);
}
dma_sync_single_for_cpu(dev->dma_dev, e->dma_addr[0],
e->dma_len[0],
page_pool_get_dma_dir(q->page_pool));
- if (!skb) {
- skb = napi_build_skb(e->buf, q->buf_size);
- if (!skb)
- return NULL;
+ if (!q->rx_head) {
+ q->rx_head = napi_build_skb(e->buf, q->buf_size);
+ if (!q->rx_head)
+ return -ENOMEM;
- __skb_put(skb, len);
- skb_reset_mac_header(skb);
- skb_mark_for_recycle(skb);
+ __skb_put(q->rx_head, len);
+ skb_reset_mac_header(q->rx_head);
+ skb_mark_for_recycle(q->rx_head);
} else {
- struct skb_shared_info *shinfo = skb_shinfo(skb);
+ struct skb_shared_info *shinfo = skb_shinfo(q->rx_head);
struct page *page = virt_to_head_page(e->buf);
int nr_frags = shinfo->nr_frags;
if (nr_frags < ARRAY_SIZE(shinfo->frags))
- skb_add_rx_frag(skb, nr_frags, page,
+ skb_add_rx_frag(q->rx_head, nr_frags, page,
e->buf - page_address(page),
len, q->buf_size);
}
- *info = desc[index].info;
- index = (index + 1) % q->ndesc;
- }
- q->tail = index;
- q->queued -= i;
+ q->tail = (q->tail + 1) % q->ndesc;
+ q->queued--;
+ } while (--nframes > 0);
+
+ dev->drv->rx_skb(dev, qid, q->rx_head, &info);
+ mt76_rx_poll_complete(dev, qid, napi);
+ q->rx_head = NULL;
+ q->pending = 0;
+
Q_WRITE(q, dma_idx, q->tail);
- return skb;
+ return 0;
}
void mt76_npu_check_ppe(struct mt76_dev *dev, struct sk_buff *skb,
@@ -155,18 +175,10 @@ static int mt76_npu_rx_poll(struct napi_struct *napi, int budget)
goto out;
while (done < budget) {
- struct sk_buff *skb;
- u32 info = 0;
-
- skb = mt76_npu_dequeue(dev, &dev->q_rx[qid], &info);
- if (!skb)
+ if (mt76_npu_rx_process_entry(napi))
break;
-
- dev->drv->rx_skb(dev, qid, skb, &info);
- mt76_rx_poll_complete(dev, qid, napi);
done++;
}
-
mt76_npu_fill_rx_queue(dev, &dev->q_rx[qid]);
out:
if (done < budget && napi_complete(napi))
---
base-commit: 763b13f622e2758726e8e3ecaef819db8fa0430a
change-id: 20261117-mt76-npu-fixes-ed72cc316a6a
Best regards,
--
Lorenzo Bianconi <lorenzo at kernel.org>
More information about the Linux-mediatek
mailing list