wifi: rtw88: usb: use a threaded NAPI and GRO for RX
PATCH 1/1 wifi: rtw88: usb: use a threaded NAPI and GRO for RX
Process the RX URBs in a threaded NAPI poll, so that the receive path can take advantage of GRO.
---
RFT because the RTL8812AU I have does not enumerate over USB3, so the full impact of this patch could not be tested.
drivers/net/wireless/realtek/rtw88/usb.c | 37 +++++++++++++++++++----- drivers/net/wireless/realtek/rtw88/usb.h | 4 ++- 2 files changed, 32 insertions(+), 9 deletions(-)
drivers/net/wireless/realtek/rtw88/usb.c +29 -8
@@ -673,9 +673,9 @@ static void rtw_usb_tx_kick_off(struct rtw_dev *rtwdev)queue_work (rtwusb->txwq, &rtwusb->tx_work); } -static void rtw_usb_rx_handler (struct work_struct *work) +static int rtw_usb_napi_poll (struct napi_struct *napi,int budget) { - struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work); + struct rtw_usb *rtwusb = container_of(napi, struct rtw_usb, napi);struct rtw_dev *rtwdev = rtwusb->rtwdev;struct ieee80211_rx_status rx_status;struct rtw_rx_pkt_stat pkt_stat;
@@ -685,14 +685,16 @@ static void rtw_usb_rx_handler(struct work_struct *work)u32 max_skb_len = pkt_desc_sz +PHY_STATUS_SIZE *8 +IEEE80211_MAX_MPDU_LEN_VHT_11454 ;u32 pkt_offset, next_pkt, skb_len; -int limit, ret; +int done =0 , ret;u8 *rx_desc; -for (limit =0 ; limit <200 ; limit++) { +while (done < budget) { rx_skb =skb_dequeue (&rtwusb->rx_queue);if (!rx_skb)break ; + done++; +if (skb_queue_len (&rtwusb->rx_queue) >=RTW_USB_MAX_RXQ_LEN ) {dev_dbg_ratelimited (rtwdev->dev,"failed to get rx_queue, overflow\n" );dev_kfree_skb_any (rx_skb);
@@ -749,7 +751,7 @@ static void rtw_usb_rx_handler(struct work_struct *work) &pkt_stat);rtw_rx_stats (rtwdev, pkt_stat.vif, skb);memcpy (skb->cb, &rx_status,sizeof (rx_status)); -ieee80211_rx_irqsafe (rtwdev->hw, skb); +ieee80211_rx_napi (rtwdev->hw,NULL , skb, napi); } skip_packet:
@@ -762,6 +764,11 @@ static void rtw_usb_rx_handler(struct work_struct *work)else skb_queue_tail (&rtwusb->rx_free_queue, rx_skb); } + +if (done < budget) +napi_complete_done (napi, done); + +return done; }static void rtw_usb_read_port_complete (struct urb *urb);
@@ -840,7 +847,7 @@ static void rtw_usb_read_port_complete(struct urb *urb) }else {skb_put (skb, urb->actual_length);skb_queue_tail (&rtwusb->rx_queue, skb); -queue_work (rtwusb->rxwq, &rtwusb->rx_work); +napi_schedule (&rtwusb->napi); }rtw_usb_rx_resubmit (rtwusb, rxcb,GFP_ATOMIC ); }else {
@@ -1062,10 +1069,20 @@ static int rtw_usb_init_rx(struct rtw_dev *rtwdev)return -ENOMEM ; } + rtwusb->netdev =alloc_netdev_dummy (0 ); +if (!rtwusb->netdev) { +destroy_workqueue (rtwusb->rxwq); +return -ENOMEM ; + } + +strscpy (rtwusb->netdev->name,"rtw88-usb-rx" ); + rtwusb->netdev->threaded =1 ; +netif_napi_add (rtwusb->netdev, &rtwusb->napi, rtw_usb_napi_poll); +napi_enable (&rtwusb->napi); +skb_queue_head_init (&rtwusb->rx_queue);skb_queue_head_init (&rtwusb->rx_free_queue); -INIT_WORK (&rtwusb->rx_work, rtw_usb_rx_handler);INIT_WORK (&rtwusb->rx_urb_work, rtw_usb_rx_resubmit_work);for (i =0 ; i <RTW_USB_RX_SKB_NUM ; i++) {
@@ -1093,6 +1110,10 @@ static void rtw_usb_deinit_rx(struct rtw_dev *rtwdev) {struct rtw_usb *rtwusb =rtw_get_usb_priv (rtwdev); +napi_disable (&rtwusb->napi); +netif_napi_del (&rtwusb->napi); +free_netdev (rtwusb->netdev); +skb_queue_purge (&rtwusb->rx_queue);destroy_workqueue (rtwusb->rxwq);
@@ -1456,7 +1477,7 @@ void rtw_usb_disconnect(struct usb_interface *intf) rtwusb =rtw_get_usb_priv (rtwdev);rtw_usb_cancel_rx_bufs (rtwusb); -cancel_work_sync (&rtwusb->rx_work); +napi_synchronize (&rtwusb->napi);rtw_unregister_hw (rtwdev, hw);rtw_usb_deinit_tx (rtwdev);
drivers/net/wireless/realtek/rtw88/usb.h +3 -1
@@ -84,8 +84,10 @@ struct rtw_usb {struct rx_usb_ctrl_block rx_cb[RTW_USB_RXCB_NUM ];struct sk_buff_head rx_queue;struct sk_buff_head rx_free_queue; -struct work_struct rx_work;struct work_struct rx_urb_work; + +struct net_device *netdev; +struct napi_struct napi; };static inline struct rtw_usb_tx_data *rtw_usb_get_tx_data (struct sk_buff *skb)
base-commit: f496c65cc2870f2342ff37736dd31d0748588c72
Signature · 1 line
2.53.0
Hi Jie Feng,
Thanks for posting this. Two comments below.
On December 3, 2026 1:00:56 PM GMT+08:00, Ho Jie Feng <[email protected]> wrote:
[ ... ]
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 696
Quoted 6 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-0+while (done < budget) { rx_skb =skb_dequeue (&rtwusb->rx_queue);if (!rx_skb)break ; + done++;
As far as I can see, done counts completed URBs. With USB RX aggregation enabled, the inner loop can pass several packets to mac80211 for each increment, so one poll can process more packets than its budget allows.
Could the poll count packets here and resume a partially processed URB on the next call?
[ ... ]
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 754
Quoted 4 lines
drivers/net/wireless/realtek/rtw88/usb.c +1
-1rtw_rx_stats (rtwdev, pkt_stat.vif, skb);memcpy (skb->cb, &rx_status,sizeof (rx_status)); -ieee80211_rx_irqsafe (rtwdev->hw, skb); +ieee80211_rx_napi (rtwdev->hw,NULL , skb, napi);
I think this can change the order of RX and TX status in AP mode. rtw_tx_report_tx_status() still queues the status for a power save poll response through ieee80211_tx_status_irqsafe(). Before this change, a later RX frame went behind that status in the same mac80211 queue.
The next power save poll can now reach ieee80211_sta_pspoll() with that status still pending. If WLAN_STA_SP is still set, mac80211 ignores the poll. Could we preserve that ordering when moving RX to NAPI?
Best regards,
Luka Gejak
Hi Luka,
Quoted 15 lines · Luka Gejak
Hi Jie Feng,
Thanks for posting this. Two comments below.
On December 3, 2026 1:00:56 PM GMT+08:00, Ho Jie Feng <[email protected]> wrote:
[ ... ]
Quoted 5 lines
+ while (done < budget) { rx_skb = skb_dequeue(&rtwusb->rx_queue); if (!rx_skb) break;
Quoted 8 lines
Quoted 1 line
+ done++;
As far as I can see, done counts completed URBs. With USB RX aggregation enabled, the inner loop can pass several packets to mac80211 for each increment, so one poll can process more packets than its budget allows.
Could the poll count packets here and resume a partially processed URB on the next call?
Yeah this is a known bug. However, making it count packets here will make the patch more complicated, so I used this instead.
This patch is mainly to let people test the RX speed with and without GRO.
Quoted 7 lines
[ ... ]
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 754
Quoted 4 lines
rtw_rx_stats(rtwdev, pkt_stat.vif, skb); memcpy(skb->cb, &rx_status, sizeof(rx_status));drivers/net/wireless/realtek/rtw88/usb.c +1
-1-ieee80211_rx_irqsafe (rtwdev->hw, skb); +ieee80211_rx_napi (rtwdev->hw,NULL , skb, napi);
Quoted 8 lines
I think this can change the order of RX and TX status in AP mode.
rtw_tx_report_tx_status()still queues the status for a power save poll response throughieee80211_tx_status_irqsafe(). Before this change, a later RX frame went behind that status in the same mac80211 queue.The next power save poll can now reach
ieee80211_sta_pspoll()with that status still pending. IfWLAN_STA_SPis still set, mac80211 ignores the poll. Could we preserve that ordering when moving RX to NAPI?
That would be a bigger change, possibly to move both TX and RX status handling into the napi thread and put both in a queue.
Another problem with GRO is that it can reduce speed for forwarding, such as the common case in AP mode. Possibly fraglist GRO can help here.
https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Best regards,
Luka Gejak
Best regards,
Jie Feng
Quoted 8 lines · Ho Jie Feng
Process the RX URBs in a threaded NAPI poll, so that the receive path can take advantage of GRO.
Signed-off-by Ho Jie Feng <[email protected]>---
RFT because the RTL8812AU I have does not enumerate over USB3, so the full impact of this patch could not be tested.
Noticed this is a bit sparse on what to test.
For testers: test the receive speed of these network adapters when connected to USB3, because the top speed is higher than what USB2 can provide.
Also check if CPU usage decreased due to GRO when receiving.
Hi Jie Feng,
Thanks for explaining. One thought on the shared queue below.
On December 3, 2026 11:56:11 PM GMT+08:00, Jie Feng <[email protected]> wrote:
[ ... ]
Quoted 2 lines
That would be a bigger change, possibly to move both TX and RX status handling into the napi thread and put both in a queue.
The power save poll response gets its status through rtw_tx_report_tx_status() when the firmware report comes in. Could we feed the shared queue from there as well as from USB TX completion? The C2H handler takes rtwdev->mutex, so I'd keep it on the workqueue and pass just the finished TX status to NAPI.
Best regards,
Luka Gejak