wifi: rtw88: usb: Copy instead of cloning the RX skb
PATCH 1/2 wifi: rtw88: usb: Copy instead of cloning the RX skb
"iperf3 -c 192.168.0.1 -R --udp -b 0" shows about 40% of datagrams are lost. Many torrents don't download faster than 3 MiB/s, probably because the Bittorrent protocol uses UDP. This is somehow related to the use of skb_clone() in the RX path.
Don't use skb_clone(). Instead allocate a new skb for each 802.11 frame received and copy the data from the big (32768 byte) skb.
With this patch, "iperf3 -c 192.168.0.1 -R --udp -b 0" shows only 1-2% of datagrams are lost, and torrents can reach download speeds of 36 MiB/s.
Tested with RTL8812AU and RTL8822CU.
--- drivers/net/wireless/realtek/rtw88/usb.c | 52 ++++++++++++++---------- 1 file changed, 31 insertions(+), 21 deletions(-)
drivers/net/wireless/realtek/rtw88/usb.c +31 -21
@@ -7,6 +7,7 @@#include <linux/mutex.h> #include "main.h" #include "debug.h" +#include "mac.h" #include "reg.h" #include "tx.h" #include "rx.h"
@@ -546,49 +547,58 @@ static void rtw_usb_rx_handler(struct work_struct *work) { struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work);struct rtw_dev *rtwdev = rtwusb->rtwdev; -const struct rtw_chip_info *chip = rtwdev->chip; -u32 pkt_desc_sz = chip->rx_pkt_desc_sz;struct ieee80211_rx_status rx_status; -u32 pkt_offset, next_pkt, urb_len;struct rtw_rx_pkt_stat pkt_stat; -struct sk_buff *next_skb; +struct sk_buff *rx_skb;struct sk_buff *skb; +u32 pkt_desc_sz = rtwdev->chip->rx_pkt_desc_sz; +u32 max_skb_len = pkt_desc_sz +PHY_STATUS_SIZE *8 + +IEEE80211_MAX_MPDU_LEN_VHT_11454 ; +u32 pkt_offset, next_pkt, skb_len;u8 *rx_desc;int limit;for (limit =0 ; limit <200 ; limit++) { - skb =skb_dequeue (&rtwusb->rx_queue); -if (!skb) + rx_skb =skb_dequeue (&rtwusb->rx_queue); +if (!rx_skb)break ;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 (skb); +dev_kfree_skb_any (rx_skb);continue ; } - urb_len = skb->len; + rx_desc = rx_skb->data; do { - rx_desc = skb->data; rtw_rx_query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + pkt_stat.shift; - next_pkt = round_up(pkt_stat.pkt_len + pkt_offset, 8); + skb_len = pkt_stat.pkt_len + pkt_offset; + if (skb_len > max_skb_len) { + rtw_dbg(rtwdev, RTW_DBG_USB, + "skipping too big packet: %u\n", + skb_len); + goto skip_packet; + } - if (urb_len >= next_pkt + pkt_desc_sz) - next_skb = skb_clone(skb, GFP_KERNEL); - else - next_skb = NULL; + skb = alloc_skb(skb_len, GFP_KERNEL); + if (!skb) { + rtw_dbg(rtwdev, RTW_DBG_USB, + "failed to allocate RX skb of size %u\n", + skb_len); + goto skip_packet; + } + + skb_put_data(skb, rx_desc, skb_len); if (pkt_stat.is_c2h) { - skb_trim(skb, pkt_stat.pkt_len + pkt_offset); rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset, skb); } else { skb_pull(skb, pkt_offset); - skb_trim(skb, pkt_stat.pkt_len); rtw_update_rx_freq_for_invalid(rtwdev, skb, &rx_status, &pkt_stat);
@@ -597,12 +607,12 @@ static void rtw_usb_rx_handler(struct work_struct *work)ieee80211_rx_irqsafe (rtwdev->hw, skb); } - skb = next_skb; -if (skb) -skb_pull (skb, next_pkt); +skip_packet: + next_pkt =round_up (skb_len,8 ); + rx_desc += next_pkt; + }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len); - urb_len -= next_pkt; - }while (skb); +dev_kfree_skb_any (rx_skb); } }
Signature · 1 line
2.47.0
PATCH 2/2 wifi: rtw88: usb: Preallocate and reuse the RX skbs
The USB driver uses four USB Request Blocks for RX. Before submitting one, it allocates a 32768 byte skb for the RX data. This allocation can fail, maybe due to temporary memory fragmentation. When the allocation fails, the corresponding URB is never submitted again. After four such allocation failures, all RX stops because the driver is not requesting data from the device anymore.
Don't allocate a 32768 byte skb when submitting a USB Request Block (which happens very often). Instead preallocate 8 such skbs, and reuse them over and over. If all 8 are busy, allocate a new one. This is pretty rare. If the allocation fails, use a work to try again later. When there are enough free skbs again, free the excess skbs.
Also, use WQ_BH for the RX workqueue. With a normal or high priority workqueue the skbs are processed too slowly when the system is even a little busy, like when opening a new page in a browser, and the driver runs out of free skbs and allocates a lot of new ones.
Move C2H_ADAPTIVITY to the c2h workqueue instead of handling it directly from rtw_fw_c2h_cmd_rx_irqsafe(), which runs in the RX workqueue. It reads hardware registers, which is not "irqsafe" with USB.
This is more or less what the out-of-tree Realtek drivers do, except they use a tasklet instead of a BH workqueue.
Tested with RTL8723DU, RTL8821AU, RTL8812AU, RTL8812BU, RTL8822CU, RTL8811CU.
--- drivers/net/wireless/realtek/rtw88/fw.c | 7 +-- drivers/net/wireless/realtek/rtw88/usb.c | 73 ++++++++++++++++++++---- drivers/net/wireless/realtek/rtw88/usb.h | 3 + 3 files changed, 67 insertions(+), 16 deletions(-)
drivers/net/wireless/realtek/rtw88/fw.c +3 -4
@@ -332,6 +332,9 @@ void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)case C2H_RA_RPT :rtw_fw_ra_report_handle (rtwdev, c2h->payload, len);break ; +case C2H_ADAPTIVITY : +rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); +break ;default :rtw_dbg (rtwdev,RTW_DBG_FW ,"C2H 0x%x isn't handled\n" , c2h->id);break ;
@@ -367,10 +370,6 @@ void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,rtw_fw_scan_result (rtwdev, c2h->payload, len);dev_kfree_skb_any (skb);break ; -case C2H_ADAPTIVITY : -rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); -dev_kfree_skb_any (skb); -break ;default :/* pass offset for further operation */ *((u32 *)skb->cb) = pkt_offset;
drivers/net/wireless/realtek/rtw88/usb.c +61 -12
142 lines not on this page · raw message
drivers/net/wireless/realtek/rtw88/usb.h +3 -0
@@ -38,6 +38,7 @@#define RTW_USB_RXAGG_TIMEOUT 10#define RTW_USB_RXCB_NUM 4 +#define RTW_USB_RX_SKB_NUM 8#define RTW_USB_EP_MAX 4
@@ -81,7 +82,9 @@ 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; };static inline struct rtw_usb_tx_data *rtw_usb_get_tx_data (struct sk_buff *skb)
Signature · 1 line
2.47.0
Ping-Ke Shih
maintainerQuoted 4 lines · Bitterblue Smith
"iperf3 -c 192.168.0.1 -R --udp -b 0" shows about 40% of datagrams are lost. Many torrents don't download faster than 3 MiB/s, probably because the Bittorrent protocol uses UDP. This is somehow related to the use of
skb_clone()in the RX path.
Using skb_clone() can improve throughput is weird to me too. Do you check top with 100% cpu usage?
Quoted 63 lines
Don't use
skb_clone(). Instead allocate a new skb for each 802.11 frame received and copy the data from the big (32768 byte) skb.With this patch, "iperf3 -c 192.168.0.1 -R --udp -b 0" shows only 1-2% of datagrams are lost, and torrents can reach download speeds of 36 MiB/s.
Tested with RTL8812AU and RTL8822CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/usb.c | 52 ++++++++++++++---------- 1 file changed, 31 insertions(+), 21 deletions(-)drivers/net/wireless/realtek/rtw88/usb.c +10
-8@@ -7,6 +7,7 @@#include <linux/mutex.h> #include "main.h" #include "debug.h" +#include "mac.h" #include "reg.h" #include "tx.h" #include "rx.h" @@ -546,49 +547,58 @@ static void rtw_usb_rx_handler(struct work_struct *work) { struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work);struct rtw_dev *rtwdev = rtwusb->rtwdev; -const struct rtw_chip_info *chip = rtwdev->chip; -u32 pkt_desc_sz = chip->rx_pkt_desc_sz;struct ieee80211_rx_status rx_status; -u32 pkt_offset, next_pkt, urb_len;struct rtw_rx_pkt_stat pkt_stat; -struct sk_buff *next_skb; +struct sk_buff *rx_skb;struct sk_buff *skb; +u32 pkt_desc_sz = rtwdev->chip->rx_pkt_desc_sz; +u32 max_skb_len = pkt_desc_sz +PHY_STATUS_SIZE *8 + +IEEE80211_MAX_MPDU_LEN_VHT_11454 ; +u32 pkt_offset, next_pkt, skb_len;u8 *rx_desc;int limit;for (limit =0 ; limit <200 ; limit++) { - skb =skb_dequeue (&rtwusb->rx_queue); -if (!skb) + rx_skb =skb_dequeue (&rtwusb->rx_queue); +if (!rx_skb)break ;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 (skb); +dev_kfree_skb_any (rx_skb);continue ; } - urb_len = skb->len; + rx_desc = rx_skb->data;
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 581
Quoted 10 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-2do { - rx_desc = skb->data; rtw_rx_query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + pkt_stat.shift; - next_pkt = round_up(pkt_stat.pkt_len + pkt_offset, 8); + skb_len = pkt_stat.pkt_len + pkt_offset; + if (skb_len > max_skb_len) {
This seems a new rule introduced by this patch. Do you really encounter this case? Maybe this is the cause of slow download throughput?
Quoted 49 lines
drivers/net/wireless/realtek/rtw88/usb.c +19
-12+rtw_dbg (rtwdev,RTW_DBG_USB , +"skipping too big packet: %u\n" , + skb_len); + goto skip_packet; + } -if (urb_len >= next_pkt + pkt_desc_sz) - next_skb =skb_clone (skb,GFP_KERNEL ); -else - next_skb =NULL ; + skb =alloc_skb (skb_len,GFP_KERNEL ); +if (!skb) { +rtw_dbg (rtwdev,RTW_DBG_USB , +"failed to allocate RX skb of size %u\n" , + skb_len); + goto skip_packet; + } + +skb_put_data (skb, rx_desc, skb_len);if (pkt_stat.is_c2h) { -skb_trim (skb, pkt_stat.pkt_len + pkt_offset);rtw_fw_c2h_cmd_rx_irqsafe (rtwdev, pkt_offset, skb); }else {skb_pull (skb, pkt_offset); -skb_trim (skb, pkt_stat.pkt_len);rtw_update_rx_freq_for_invalid (rtwdev, skb, &rx_status, &pkt_stat);@@ -597,12 +607,12 @@ static void rtw_usb_rx_handler(struct work_struct *work)ieee80211_rx_irqsafe (rtwdev->hw, skb); } - skb = next_skb; -if (skb) -skb_pull (skb, next_pkt); +skip_packet: + next_pkt =round_up (skb_len,8 ); + rx_desc += next_pkt; + }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len); - urb_len -= next_pkt; - }while (skb); +dev_kfree_skb_any (rx_skb); } } --2.47.0
Ping-Ke Shih
maintainerRE: [PATCH 2/2] wifi: rtw88: usb: Preallocate and reuse the RX skbs
In reply to Bitterblue SmithQuoted 13 lines · Bitterblue Smith
The USB driver uses four USB Request Blocks for RX. Before submitting one, it allocates a 32768 byte skb for the RX data. This allocation can fail, maybe due to temporary memory fragmentation. When the allocation fails, the corresponding URB is never submitted again. After four such allocation failures, all RX stops because the driver is not requesting data from the device anymore.
Don't allocate a 32768 byte skb when submitting a USB Request Block (which happens very often). Instead preallocate 8 such skbs, and reuse them over and over. If all 8 are busy, allocate a new one. This is pretty rare. If the allocation fails, use a work to try again later. When there are enough free skbs again, free the excess skbs.
Quoted 8 lines
Also, use
WQ_BHfor the RX workqueue. With a normal or high priority workqueue the skbs are processed too slowly when the system is even a little busy, like when opening a new page in a browser, and the driver runs out of free skbs and allocates a lot of new ones.Move
C2H_ADAPTIVITYto the c2h workqueue instead of handling it directly fromrtw_fw_c2h_cmd_rx_irqsafe(), which runs in the RX workqueue. It reads hardware registers, which is not "irqsafe" with USB.
This part should be in a separated patch.
Quoted 54 lines
This is more or less what the out-of-tree Realtek drivers do, except they use a tasklet instead of a BH workqueue.
Tested with RTL8723DU, RTL8821AU, RTL8812AU, RTL8812BU, RTL8822CU, RTL8811CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/fw.c | 7 +-- drivers/net/wireless/realtek/rtw88/usb.c | 73 ++++++++++++++++++++---- drivers/net/wireless/realtek/rtw88/usb.h | 3 + 3 files changed, 67 insertions(+), 16 deletions(-)drivers/net/wireless/realtek/rtw88/fw.c +3
-4@@ -332,6 +332,9 @@ void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)case C2H_RA_RPT :rtw_fw_ra_report_handle (rtwdev, c2h->payload, len);break ; +case C2H_ADAPTIVITY : +rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); +break ;default :rtw_dbg (rtwdev,RTW_DBG_FW ,"C2H 0x%x isn't handled\n" , c2h->id);break ;@@ -367,10 +370,6 @@ void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,rtw_fw_scan_result (rtwdev, c2h->payload, len);dev_kfree_skb_any (skb);break ; -case C2H_ADAPTIVITY : -rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); -dev_kfree_skb_any (skb); -break ;default :/* pass offset for further operation */ *((u32 *)skb->cb) = pkt_offset;drivers/net/wireless/realtek/rtw88/usb.c +1
-1@@ -585,7 +585,7 @@ static void rtw_usb_rx_handler(struct work_struct *work) goto skip_packet; } - skb =alloc_skb (skb_len,GFP_KERNEL ); + skb =alloc_skb (skb_len,GFP_ATOMIC );if (!skb) { rtw_dbg(rtwdev, RTW_DBG_USB, "failed to allocate RX skb of size %u\n",
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 619
Quoted 9 lines
drivers/net/wireless/realtek/rtw88/usb.c +5
-1@@ -612,7 +612,11 @@ static void rtw_usb_rx_handler(struct work_struct *work) rx_desc += next_pkt; }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len); -dev_kfree_skb_any (rx_skb); +if (skb_queue_len (&rtwusb->rx_free_queue) >= +RTW_USB_RX_SKB_NUM -RTW_USB_RXCB_NUM ) +dev_kfree_skb_any (rx_skb); +else +skb_queue_tail (&rtwusb->rx_free_queue, rx_skb);
Why not just queue and reuse rx_skb for all? That would be simpler.
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 629
Quoted 9 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-0} }@@ -621,23 +625,57 @@ static void rtw_usb_read_port_complete(struct urb *urb);static void rtw_usb_rx_resubmit (struct rtw_usb *rtwusb,struct rx_usb_ctrl_block *rxcb) {struct rtw_dev *rtwdev = rtwusb->rtwdev; +struct sk_buff *rx_skb; +gfp_t priority =in_interrupt () ?GFP_ATOMIC :GFP_KERNEL ;
I remember in_interrupt() is not recommended. Can't we pass necessary gfp_t via function argument by callers?
Quoted 6 lines
drivers/net/wireless/realtek/rtw88/usb.c +1
-3int error; - rxcb->rx_skb =alloc_skb (RTW_USB_MAX_RECVBUF_SZ ,GFP_ATOMIC ); -if (!rxcb->rx_skb) -return ; + rx_skb =skb_dequeue (&rtwusb->rx_free_queue);
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 642
Quoted 10 lines
drivers/net/wireless/realtek/rtw88/usb.c +10
-0+if (!rx_skb) + rx_skb =alloc_skb (RTW_USB_MAX_RECVBUF_SZ , priority); + +if (!rx_skb) + goto try_later; + + rxcb->rx_skb = rx_skb; + +skb_reset_tail_pointer (rxcb->rx_skb); + rxcb->rx_skb->len =0 ;
How about moving these initialization upward before ' rxcb->rx_skb = rx_skb;'? Statements can be shorter, and it is reasonable to initialize data before assignment.
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 649
Quoted 8 lines
drivers/net/wireless/realtek/rtw88/usb.c +1
-1usb_fill_bulk_urb (rxcb->rx_urb, rtwusb->udev,usb_rcvbulkpipe (rtwusb->udev, rtwusb->pipe_in), rxcb->rx_skb->data,RTW_USB_MAX_RECVBUF_SZ , rtw_usb_read_port_complete, rxcb); - error =usb_submit_urb (rxcb->rx_urb,GFP_ATOMIC ); + error =usb_submit_urb (rxcb->rx_urb, priority);
Not sure if 'priority' fits the meaning. Maybe many kernel code just uses 'gfp'.
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 655
Quoted 7 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-1if (error) { -kfree_skb (rxcb->rx_skb); +skb_queue_tail (&rtwusb->rx_free_queue, rxcb->rx_skb); +if (error != -ENODEV )rtw_err (rtwdev,"Err sending rx data urb %d\n" , error);
Since here rxcb->rx_skb != NULL, will it be a problem? The rxcb will not be submitted again? Should all error cases go to try_later label?
Quoted 115 lines
drivers/net/wireless/realtek/rtw88/usb.c +39
-5+ +if (error == -ENOMEM ) + goto try_later; + } + +return ; + +try_later: + rxcb->rx_skb =NULL ; +queue_work (rtwusb->rxwq, &rtwusb->rx_urb_work); +} + +static void rtw_usb_rx_resubmit_work (struct work_struct *work) +{ + struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_urb_work); +struct rx_usb_ctrl_block *rxcb; +int i; + +for (i =0 ; i <RTW_USB_RXCB_NUM ; i++) { + rxcb = &rtwusb->rx_cb[i]; + +if (!rxcb->rx_skb) +rtw_usb_rx_resubmit (rtwusb, rxcb); } }@@ -653,8 +691,7 @@ static void rtw_usb_read_port_complete(struct urb *urb) urb->actual_length < 24) { rtw_err(rtwdev, "failed to get urb length:%d\n", urb->actual_length); - if (skb) - dev_kfree_skb_any(skb); + skb_queue_tail(&rtwusb->rx_free_queue, skb); } else {skb_put (skb, urb->actual_length);skb_queue_tail (&rtwusb->rx_queue, skb);@@ -662,6 +699,8 @@ static void rtw_usb_read_port_complete(struct urb *urb) }rtw_usb_rx_resubmit (rtwusb, rxcb); }else { +skb_queue_tail (&rtwusb->rx_free_queue, skb); +switch (urb->status) {case -EINVAL :case -EPIPE :@@ -679,8 +718,6 @@ static void rtw_usb_read_port_complete(struct urb *urb)rtw_err (rtwdev,"status %d\n" , urb->status);break ; } -if (skb) -dev_kfree_skb_any (skb); } }@@ -868,16 +905,26 @@ static struct rtw_hci_ops rtw_usb_ops = {static int rtw_usb_init_rx (struct rtw_dev *rtwdev) {struct rtw_usb *rtwusb =rtw_get_usb_priv (rtwdev); +struct sk_buff *rx_skb; +int i; - rtwusb->rxwq =create_singlethread_workqueue ("rtw88_usb: rx wq" ); + rtwusb->rxwq =alloc_workqueue ("rtw88_usb: rx wq" ,WQ_BH ,0 );if (!rtwusb->rxwq) {rtw_err (rtwdev,"failed to create RX work queue\n" );return -ENOMEM ; }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++) { + rx_skb =alloc_skb (RTW_USB_MAX_RECVBUF_SZ ,GFP_KERNEL ); +if (rx_skb) +skb_queue_tail (&rtwusb->rx_free_queue, rx_skb); + }return 0 ; }@@ -902,6 +949,8 @@ static void rtw_usb_deinit_rx(struct rtw_dev *rtwdev)flush_workqueue (rtwusb->rxwq);destroy_workqueue (rtwusb->rxwq); + +skb_queue_purge (&rtwusb->rx_free_queue); }static int rtw_usb_init_tx (struct rtw_dev *rtwdev)drivers/net/wireless/realtek/rtw88/usb.h +3
-1@@ -38,6 +38,7 @@#define RTW_USB_RXAGG_TIMEOUT 10#define RTW_USB_RXCB_NUM 4 +#define RTW_USB_RX_SKB_NUM 8#define RTW_USB_EP_MAX 4@@ -81,7 +82,9 @@ 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; };static inline struct rtw_usb_tx_data *rtw_usb_get_tx_data (struct sk_buff *skb) --2.47.0
Quoted 9 lines · Ping-Ke Shih
Quoted 4 lines · Bitterblue Smith
"iperf3 -c 192.168.0.1 -R --udp -b 0" shows about 40% of datagrams are lost. Many torrents don't download faster than 3 MiB/s, probably because the Bittorrent protocol uses UDP. This is somehow related to the use of
skb_clone()in the RX path.Using
skb_clone()can improve throughput is weird to me too. Do you check top with 100% cpu usage?
I checked the CPU usage now and the results are interesting. In short, patch 1/2 slightly raises the CPU usage, and patch 2/2 lowers it a lot.
I used this command: "top -b -d 20 -n 2" while running iperf3 in reverse mode. During every test the RX speed was approximately 490 Mbps. I tested with RTL8812AU. I used the numbers from the second "frame" printed by each top command. I added up all the processes that had CPU usage of at least 0.1%. I removed processes that I think are irrelevant (kwin_wayland, konsole, weechat, plasmashell).
Before patch 1/2:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
21192 asdf 20 0 17988 3468 2828 S 26.6 0.0 0:05.90 iperf3
34 root 20 0 0 0 0 S 8.7 0.0 1:06.89 ksoftirqd/1
17 root 20 0 0 0 0 S 4.7 0.0 0:44.58 ksoftirqd/0
40 root 20 0 0 0 0 S 3.9 0.0 0:25.15 ksoftirqd/3
28 root 20 0 0 0 0 R 3.7 0.0 0:31.46 ksoftirqd/2
20964 root 20 0 0 0 0 R 3.5 0.0 0:01.21 kworker/u16:1-rtw88_usb: tx wq
20716 root 20 0 0 0 0 I 3.4 0.0 0:02.74 kworker/u16:3-flush-259:0
11253 root 20 0 0 0 0 I 2.9 0.0 0:43.42 kworker/u16:4-events_unbound
20572 root 0 -20 0 0 0 I 2.7 0.0 0:03.05 kworker/u17:0-rtw_tx_wq
21194 root 0 -20 0 0 0 I 2.7 0.0 0:00.59 kworker/u17:1-rtw_tx_wq62.8
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
21798 asdf 20 0 17988 3408 2768 S 33.7 0.0 0:08.41 iperf3
34 root 20 0 0 0 0 S 7.5 0.0 1:21.17 ksoftirqd/1
17 root 20 0 0 0 0 S 5.8 0.0 0:54.08 ksoftirqd/0
28 root 20 0 0 0 0 S 5.7 0.0 0:38.42 ksoftirqd/2
40 root 20 0 0 0 0 S 5.5 0.0 0:29.69 ksoftirqd/3
21261 root 20 0 0 0 0 I 3.0 0.0 0:02.32 kworker/u16:0-rtw88_usb: tx wq
21237 root 0 -20 0 0 0 I 2.6 0.0 0:00.71 kworker/u17:4-rtw_tx_wq
20716 root 20 0 0 0 0 I 2.4 0.0 0:05.97 kworker/u16:3-rtw88_usb: rx wq
20964 root 20 0 0 0 0 I 2.4 0.0 0:04.65 kworker/u16:1-rtw88_usb: rx wq
21244 root 0 -20 0 0 0 I 0.7 0.0 0:00.24 kworker/u17:12-rtw_tx_wq69.3
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
21853 asdf 20 0 17988 3700 2932 S 34.3 0.0 0:08.38 iperf3
17 root 20 0 0 0 0 S 7.0 0.0 0:55.93 ksoftirqd/0
34 root 20 0 0 0 0 S 6.8 0.0 1:22.92 ksoftirqd/1
40 root 20 0 0 0 0 S 5.4 0.0 0:31.05 ksoftirqd/3
28 root 20 0 0 0 0 S 4.9 0.0 0:39.62 ksoftirqd/2
21261 root 20 0 0 0 0 I 3.1 0.0 0:03.22 kworker/u16:0-rtw88_usb: rx wq
21833 root 0 -20 0 0 0 I 2.4 0.0 0:00.67 kworker/u17:0-rtw_tx_wq
20427 root 20 0 0 0 0 I 2.2 0.0 0:12.37 kworker/u16:2-events_unbound
21834 root 0 -20 0 0 0 I 1.1 0.0 0:00.30 kworker/u17:2-rtw_tx_wq
67.2With patch 1/2:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22222 asdf 20 0 17988 3804 3036 S 29.4 0.0 0:08.38 iperf3
34 root 20 0 0 0 0 R 10.3 0.0 1:26.13 ksoftirqd/1
28 root 20 0 0 0 0 R 7.9 0.0 0:42.00 ksoftirqd/2
20964 root 20 0 0 0 0 I 5.8 0.0 0:06.66 kworker/u16:1-rtw88_usb: rx wq
17 root 20 0 0 0 0 S 5.0 0.0 0:57.49 ksoftirqd/0
21261 root 20 0 0 0 0 I 4.2 0.0 0:04.55 kworker/u16:0-rtw88_usb: tx wq
40 root 20 0 0 0 0 S 4.1 0.0 0:32.39 ksoftirqd/3
20716 root 20 0 0 0 0 I 2.8 0.0 0:08.07 kworker/u16:3-rtw88_usb: tx wq
21237 root 0 -20 0 0 0 I 2.5 0.0 0:01.32 kworker/u17:4-rtw_tx_wq
21834 root 0 -20 0 0 0 I 1.0 0.0 0:00.61 kworker/u17:2-rtw_tx_wq
21225 root 20 0 0 0 0 I 0.1 0.0 0:00.33 kworker/2:2-events73.1
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22265 asdf 20 0 17988 3596 2828 S 30.5 0.0 0:08.17 iperf3
34 root 20 0 0 0 0 S 10.5 0.0 1:28.96 ksoftirqd/1
28 root 20 0 0 0 0 S 9.1 0.0 0:44.54 ksoftirqd/2
20427 root 20 0 0 0 0 I 5.9 0.0 0:13.72 kworker/u16:2-rtw88_usb: rx wq
20716 root 20 0 0 0 0 I 5.7 0.0 0:09.78 kworker/u16:3-rtw88_usb: tx wq
17 root 20 0 0 0 0 S 3.9 0.0 0:58.72 ksoftirqd/0
40 root 20 0 0 0 0 S 3.5 0.0 0:33.53 ksoftirqd/3
21833 root 0 -20 0 0 0 I 3.2 0.0 0:01.76 kworker/u17:0-rtw_tx_wq
20964 root 20 0 0 0 0 I 1.1 0.0 0:07.34 kworker/u16:1-flush-259:0
21834 root 0 -20 0 0 0 I 0.4 0.0 0:00.92 kworker/u17:2-rtw_tx_wq73.8
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22303 asdf 20 0 17988 3808 3040 S 31.3 0.0 0:08.58 iperf3
28 root 20 0 0 0 0 R 10.0 0.0 0:47.50 ksoftirqd/2
34 root 20 0 0 0 0 S 9.9 0.0 1:31.69 ksoftirqd/1
20716 root 20 0 0 0 0 I 5.2 0.0 0:11.26 kworker/u16:3-rtw88_usb: rx wq
17 root 20 0 0 0 0 S 4.0 0.0 0:59.98 ksoftirqd/0
20427 root 20 0 0 0 0 I 4.0 0.0 0:15.06 kworker/u16:2-rtw88_usb: tx wq
20964 root 20 0 0 0 0 I 3.8 0.0 0:08.73 kworker/u16:1-rtw88_usb: tx wq
40 root 20 0 0 0 0 S 2.7 0.0 0:34.49 ksoftirqd/3
21833 root 0 -20 0 0 0 I 1.9 0.0 0:02.47 kworker/u17:0-rtw_tx_wq
21834 root 0 -20 0 0 0 I 1.8 0.0 0:01.49 kworker/u17:2-rtw_tx_wq
74.6With patches 1/2 and 2/2:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23151 asdf 20 0 17988 3656 2888 S 28.9 0.0 0:08.86 iperf3
22400 root 0 -20 0 0 0 I 2.7 0.0 0:00.86 kworker/u17:6-rtw_tx_wq
21261 root 20 0 0 0 0 I 2.3 0.0 0:06.47 kworker/u16:0-rtw88_usb: tx wq
22401 root 0 -20 0 0 0 I 1.3 0.0 0:00.41 kworker/u17:7-rtw_tx_wq
20427 root 20 0 0 0 0 I 1.2 0.0 0:16.02 kworker/u16:2-rtw88_usb: tx wq
34 root 20 0 0 0 0 S 0.5 0.0 1:32.23 ksoftirqd/1
20964 root 20 0 0 0 0 I 0.4 0.0 0:09.30 kworker/u16:1-rtw88_usb: tx wq
22498 root 20 0 0 0 0 I 0.1 0.0 0:00.07 kworker/1:1-events37.4
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23213 asdf 20 0 17988 3360 2720 S 30.7 0.0 0:08.69 iperf3
23169 root 0 -20 0 0 0 I 3.3 0.0 0:00.93 kworker/u17:1-rtw_tx_wq
21261 root 20 0 0 0 0 I 1.5 0.0 0:07.17 kworker/u16:0-rtw88_usb: tx wq
23219 root 20 0 0 0 0 I 1.0 0.0 0:00.21 kworker/u16:3-rtw88_usb: tx wq
34 root 20 0 0 0 0 S 0.6 0.0 1:32.59 ksoftirqd/1
20964 root 20 0 0 0 0 I 0.6 0.0 0:09.46 kworker/u16:1-rtw88_usb: tx wq
23189 root 0 -20 0 0 0 I 0.6 0.0 0:00.26 kworker/u17:9-rtw_tx_wq
20427 root 20 0 0 0 0 I 0.5 0.0 0:16.48 kworker/u16:2-rtw88_usb: tx wq
22283 root 20 0 0 0 0 I 0.1 0.0 0:00.03 kworker/1:0-events38.9
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23266 asdf 20 0 17988 3608 2968 S 27.5 0.0 0:07.55 iperf3
21833 root 0 -20 0 0 0 R 2.7 0.0 0:03.09 kworker/u17:0+rtw_tx_wq
20964 root 20 0 0 0 0 I 1.6 0.0 0:10.03 kworker/u16:1-events_unbound
23272 root 20 0 0 0 0 I 1.5 0.0 0:00.31 kworker/u16:4-rtw88_usb: tx wq
22401 root 0 -20 0 0 0 I 1.1 0.0 0:00.97 kworker/u17:7-rtw_tx_wq
34 root 20 0 0 0 0 S 0.5 0.0 1:32.83 ksoftirqd/1
20427 root 20 0 0 0 0 I 0.5 0.0 0:16.78 kworker/u16:2-rtw88_usb: tx wq
23228 root 20 0 0 0 0 I 0.1 0.0 0:00.08 kworker/1:2-events
35.5Quoted 70 lines
Quoted 70 lines
Don't use
skb_clone(). Instead allocate a new skb for each 802.11 frame received and copy the data from the big (32768 byte) skb.With this patch, "iperf3 -c 192.168.0.1 -R --udp -b 0" shows only 1-2% of datagrams are lost, and torrents can reach download speeds of 36 MiB/s.
Tested with RTL8812AU and RTL8822CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/usb.c | 52 ++++++++++++++---------- 1 file changed, 31 insertions(+), 21 deletions(-)drivers/net/wireless/realtek/rtw88/usb.c +10
-9@@ -7,6 +7,7 @@#include <linux/mutex.h> #include "main.h" #include "debug.h" +#include "mac.h" #include "reg.h" #include "tx.h" #include "rx.h" @@ -546,49 +547,58 @@ static void rtw_usb_rx_handler(struct work_struct *work) { struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work);struct rtw_dev *rtwdev = rtwusb->rtwdev; -const struct rtw_chip_info *chip = rtwdev->chip; -u32 pkt_desc_sz = chip->rx_pkt_desc_sz;struct ieee80211_rx_status rx_status; -u32 pkt_offset, next_pkt, urb_len;struct rtw_rx_pkt_stat pkt_stat; -struct sk_buff *next_skb; +struct sk_buff *rx_skb;struct sk_buff *skb; +u32 pkt_desc_sz = rtwdev->chip->rx_pkt_desc_sz; +u32 max_skb_len = pkt_desc_sz +PHY_STATUS_SIZE *8 + +IEEE80211_MAX_MPDU_LEN_VHT_11454 ; +u32 pkt_offset, next_pkt, skb_len;u8 *rx_desc;int limit;for (limit =0 ; limit <200 ; limit++) { - skb =skb_dequeue (&rtwusb->rx_queue); -if (!skb) + rx_skb =skb_dequeue (&rtwusb->rx_queue); +if (!rx_skb)break ;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 (skb); +dev_kfree_skb_any (rx_skb);continue ; } - urb_len = skb->len; + rx_desc = rx_skb->data; do { - rx_desc = skb->data; rtw_rx_query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + pkt_stat.shift;
Quoted 7 lines
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 581
Quoted 3 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-1- next_pkt =round_up (pkt_stat.pkt_len + pkt_offset,8 ); + skb_len = pkt_stat.pkt_len + pkt_offset; +if (skb_len > max_skb_len) {This seems a new rule introduced by this patch. Do you really encounter this case? Maybe this is the cause of slow download throughput?
No, I never saw this case. It just seemed like a good idea to check the size passed to alloc_skb. Maybe it's not needed?
Quoted 50 lines
Quoted 49 lines
drivers/net/wireless/realtek/rtw88/usb.c +19
-12+rtw_dbg (rtwdev,RTW_DBG_USB , +"skipping too big packet: %u\n" , + skb_len); + goto skip_packet; + } -if (urb_len >= next_pkt + pkt_desc_sz) - next_skb =skb_clone (skb,GFP_KERNEL ); -else - next_skb =NULL ; + skb =alloc_skb (skb_len,GFP_KERNEL ); +if (!skb) { +rtw_dbg (rtwdev,RTW_DBG_USB , +"failed to allocate RX skb of size %u\n" , + skb_len); + goto skip_packet; + } + +skb_put_data (skb, rx_desc, skb_len);if (pkt_stat.is_c2h) { -skb_trim (skb, pkt_stat.pkt_len + pkt_offset);rtw_fw_c2h_cmd_rx_irqsafe (rtwdev, pkt_offset, skb); }else {skb_pull (skb, pkt_offset); -skb_trim (skb, pkt_stat.pkt_len);rtw_update_rx_freq_for_invalid (rtwdev, skb, &rx_status, &pkt_stat);@@ -597,12 +607,12 @@ static void rtw_usb_rx_handler(struct work_struct *work)ieee80211_rx_irqsafe (rtwdev->hw, skb); } - skb = next_skb; -if (skb) -skb_pull (skb, next_pkt); +skip_packet: + next_pkt =round_up (skb_len,8 ); + rx_desc += next_pkt; + }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len); - urb_len -= next_pkt; - }while (skb); +dev_kfree_skb_any (rx_skb); } } --2.47.0
Re: [PATCH 2/2] wifi: rtw88: usb: Preallocate and reuse the RX skbs
In reply to Ping-Ke ShihQuoted 19 lines · Ping-Ke Shih
Quoted 18 lines · Bitterblue Smith
The USB driver uses four USB Request Blocks for RX. Before submitting one, it allocates a 32768 byte skb for the RX data. This allocation can fail, maybe due to temporary memory fragmentation. When the allocation fails, the corresponding URB is never submitted again. After four such allocation failures, all RX stops because the driver is not requesting data from the device anymore.
Don't allocate a 32768 byte skb when submitting a USB Request Block (which happens very often). Instead preallocate 8 such skbs, and reuse them over and over. If all 8 are busy, allocate a new one. This is pretty rare. If the allocation fails, use a work to try again later. When there are enough free skbs again, free the excess skbs.
Also, use
WQ_BHfor the RX workqueue. With a normal or high priority workqueue the skbs are processed too slowly when the system is even a little busy, like when opening a new page in a browser, and the driver runs out of free skbs and allocates a lot of new ones.
Quoted 6 lines
Quoted 3 lines
Move
C2H_ADAPTIVITYto the c2h workqueue instead of handling it directly fromrtw_fw_c2h_cmd_rx_irqsafe(), which runs in the RX workqueue. It reads hardware registers, which is not "irqsafe" with USB.This part should be in a separated patch.
Do you mean just C2H_ADAPTIVITY or WQ_BH as well?
Quoted 57 lines
Quoted 57 lines
This is more or less what the out-of-tree Realtek drivers do, except they use a tasklet instead of a BH workqueue.
Tested with RTL8723DU, RTL8821AU, RTL8812AU, RTL8812BU, RTL8822CU, RTL8811CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/fw.c | 7 +-- drivers/net/wireless/realtek/rtw88/usb.c | 73 ++++++++++++++++++++---- drivers/net/wireless/realtek/rtw88/usb.h | 3 + 3 files changed, 67 insertions(+), 16 deletions(-)drivers/net/wireless/realtek/rtw88/fw.c +3
-4@@ -332,6 +332,9 @@ void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)case C2H_RA_RPT :rtw_fw_ra_report_handle (rtwdev, c2h->payload, len);break ; +case C2H_ADAPTIVITY : +rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); +break ;default :rtw_dbg (rtwdev,RTW_DBG_FW ,"C2H 0x%x isn't handled\n" , c2h->id);break ;@@ -367,10 +370,6 @@ void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,rtw_fw_scan_result (rtwdev, c2h->payload, len);dev_kfree_skb_any (skb);break ; -case C2H_ADAPTIVITY : -rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); -dev_kfree_skb_any (skb); -break ;default :/* pass offset for further operation */ *((u32 *)skb->cb) = pkt_offset;drivers/net/wireless/realtek/rtw88/usb.c +1
-1@@ -585,7 +585,7 @@ static void rtw_usb_rx_handler(struct work_struct *work) goto skip_packet; } - skb =alloc_skb (skb_len,GFP_KERNEL ); + skb =alloc_skb (skb_len,GFP_ATOMIC );if (!skb) { rtw_dbg(rtwdev, RTW_DBG_USB, "failed to allocate RX skb of size %u\n",@@ -612,7 +612,11 @@ static void rtw_usb_rx_handler(struct work_struct *work) rx_desc += next_pkt; }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len);
Quoted 9 lines
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 619
Quoted 6 lines
drivers/net/wireless/realtek/rtw88/usb.c +5
-1-dev_kfree_skb_any (rx_skb); +if (skb_queue_len (&rtwusb->rx_free_queue) >= +RTW_USB_RX_SKB_NUM -RTW_USB_RXCB_NUM ) +dev_kfree_skb_any (rx_skb); +else +skb_queue_tail (&rtwusb->rx_free_queue, rx_skb);Why not just queue and reuse
rx_skbfor all? That would be simpler.
I didn't want to let it allocate too many skbs. I didn't find any kind of limit in the official drivers, so maybe it would be fine.
Quoted 13 lines
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 629
Quoted 9 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-0} }@@ -621,23 +625,57 @@ static void rtw_usb_read_port_complete(struct urb *urb);static void rtw_usb_rx_resubmit (struct rtw_usb *rtwusb,struct rx_usb_ctrl_block *rxcb) {struct rtw_dev *rtwdev = rtwusb->rtwdev; +struct sk_buff *rx_skb; +gfp_t priority =in_interrupt () ?GFP_ATOMIC :GFP_KERNEL ;I remember
in_interrupt()is not recommended. Can't we pass necessarygfp_tvia function argument by callers?
Yes, I can do that.
Quoted 41 lines
Quoted 6 lines
drivers/net/wireless/realtek/rtw88/usb.c +1
-3int error; - rxcb->rx_skb =alloc_skb (RTW_USB_MAX_RECVBUF_SZ ,GFP_ATOMIC ); -if (!rxcb->rx_skb) -return ; + rx_skb =skb_dequeue (&rtwusb->rx_free_queue);drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 642
Quoted 10 lines
drivers/net/wireless/realtek/rtw88/usb.c +10
-0+if (!rx_skb) + rx_skb =alloc_skb (RTW_USB_MAX_RECVBUF_SZ , priority); + +if (!rx_skb) + goto try_later; + + rxcb->rx_skb = rx_skb; + +skb_reset_tail_pointer (rxcb->rx_skb); + rxcb->rx_skb->len =0 ;How about moving these initialization upward before '
rxcb->rx_skb=rx_skb;'? Statements can be shorter, and it is reasonable to initialize data before assignment.drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 649
Quoted 8 lines
drivers/net/wireless/realtek/rtw88/usb.c +1
-1usb_fill_bulk_urb (rxcb->rx_urb, rtwusb->udev,usb_rcvbulkpipe (rtwusb->udev, rtwusb->pipe_in), rxcb->rx_skb->data,RTW_USB_MAX_RECVBUF_SZ , rtw_usb_read_port_complete, rxcb); - error =usb_submit_urb (rxcb->rx_urb,GFP_ATOMIC ); + error =usb_submit_urb (rxcb->rx_urb, priority);Not sure if 'priority' fits the meaning. Maybe many kernel code just uses 'gfp'.
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_read_port_complete(struct urb *urb); · line 655
Quoted 7 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-1if (error) { -kfree_skb (rxcb->rx_skb); +skb_queue_tail (&rtwusb->rx_free_queue, rxcb->rx_skb); +if (error != -ENODEV )rtw_err (rtwdev,"Err sending rx data urb %d\n" , error);
Quoted 3 lines
Since here
rxcb->rx_skb!= NULL, will it be a problem? The rxcb will not be submitted again? Should all error cases go totry_laterlabel?
Right, the rxcb will be submitted again only if the error was ENOMEM I don't know what other errors can be considered temporary. I never ran into the case where the error is not ENODEV.
Quoted 27 lines
Quoted 27 lines
drivers/net/wireless/realtek/rtw88/usb.c +23
-0+ +if (error == -ENOMEM ) + goto try_later; + } + +return ; + +try_later: + rxcb->rx_skb =NULL ; +queue_work (rtwusb->rxwq, &rtwusb->rx_urb_work); +} + +static void rtw_usb_rx_resubmit_work (struct work_struct *work) +{ + struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_urb_work); +struct rx_usb_ctrl_block *rxcb; +int i; + +for (i =0 ; i <RTW_USB_RXCB_NUM ; i++) { + rxcb = &rtwusb->rx_cb[i]; + +if (!rxcb->rx_skb) +rtw_usb_rx_resubmit (rtwusb, rxcb); } }
Ping-Ke Shih
maintainerQuoted 7 lines · Bitterblue Smith
Quoted 6 lines · Ping-Ke Shih
Quoted 4 lines · Bitterblue Smith
"iperf3 -c 192.168.0.1 -R --udp -b 0" shows about 40% of datagrams are lost. Many torrents don't download faster than 3 MiB/s, probably because the Bittorrent protocol uses UDP. This is somehow related to the use of
skb_clone()in the RX path.
Quoted 6 lines
Quoted 3 lines
Using
skb_clone()can improve throughput is weird to me too. Do you check top with 100% cpu usage?I checked the CPU usage now and the results are interesting. In short, patch 1/2 slightly raises the CPU usage, and patch 2/2 lowers it a lot.
Originally I just wanted to know if throughput is a limit of CPU bound. Anyway good to know this patchset can improve CPU usage.
Quoted 78 lines
Quoted 70 lines
Quoted 70 lines
Don't use
skb_clone(). Instead allocate a new skb for each 802.11 frame received and copy the data from the big (32768 byte) skb.With this patch, "iperf3 -c 192.168.0.1 -R --udp -b 0" shows only 1-2% of datagrams are lost, and torrents can reach download speeds of 36 MiB/s.
Tested with RTL8812AU and RTL8822CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/usb.c | 52 ++++++++++++++---------- 1 file changed, 31 insertions(+), 21 deletions(-)drivers/net/wireless/realtek/rtw88/usb.c +10
-9@@ -7,6 +7,7 @@#include <linux/mutex.h> #include "main.h" #include "debug.h" +#include "mac.h" #include "reg.h" #include "tx.h" #include "rx.h" @@ -546,49 +547,58 @@ static void rtw_usb_rx_handler(struct work_struct *work) { struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work);struct rtw_dev *rtwdev = rtwusb->rtwdev; -const struct rtw_chip_info *chip = rtwdev->chip; -u32 pkt_desc_sz = chip->rx_pkt_desc_sz;struct ieee80211_rx_status rx_status; -u32 pkt_offset, next_pkt, urb_len;struct rtw_rx_pkt_stat pkt_stat; -struct sk_buff *next_skb; +struct sk_buff *rx_skb;struct sk_buff *skb; +u32 pkt_desc_sz = rtwdev->chip->rx_pkt_desc_sz; +u32 max_skb_len = pkt_desc_sz +PHY_STATUS_SIZE *8 + +IEEE80211_MAX_MPDU_LEN_VHT_11454 ; +u32 pkt_offset, next_pkt, skb_len;u8 *rx_desc;int limit;for (limit =0 ; limit <200 ; limit++) { - skb =skb_dequeue (&rtwusb->rx_queue); -if (!skb) + rx_skb =skb_dequeue (&rtwusb->rx_queue); +if (!rx_skb)break ;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 (skb); +dev_kfree_skb_any (rx_skb);continue ; } - urb_len = skb->len; + rx_desc = rx_skb->data; do { - rx_desc = skb->data; rtw_rx_query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + pkt_stat.shift;Quoted 7 lines
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 581
Quoted 3 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-1- next_pkt =round_up (pkt_stat.pkt_len + pkt_offset,8 ); + skb_len = pkt_stat.pkt_len + pkt_offset; +if (skb_len > max_skb_len) {This seems a new rule introduced by this patch. Do you really encounter this case? Maybe this is the cause of slow download throughput?
Quoted 2 lines
No, I never saw this case. It just seemed like a good idea to check the size passed to
alloc_skb. Maybe it's not needed?
I think it is fine.
Asking some questions before, I just tried to find a cause why 40% datagram get lost as you mentioned in commit message, but I can't.
Quoted 51 lines
Quoted 51 lines
Quoted 50 lines
drivers/net/wireless/realtek/rtw88/usb.c +19
-12+rtw_dbg (rtwdev,RTW_DBG_USB , +"skipping too big packet: %u\n" , + skb_len); + goto skip_packet; + } -if (urb_len >= next_pkt + pkt_desc_sz) - next_skb =skb_clone (skb,GFP_KERNEL ); -else - next_skb =NULL ; + skb =alloc_skb (skb_len,GFP_KERNEL ); +if (!skb) { +rtw_dbg (rtwdev,RTW_DBG_USB , +"failed to allocate RX skb of size %u\n" , + skb_len); + goto skip_packet; + } + +skb_put_data (skb, rx_desc, skb_len);if (pkt_stat.is_c2h) { -skb_trim (skb, pkt_stat.pkt_len + pkt_offset);rtw_fw_c2h_cmd_rx_irqsafe (rtwdev, pkt_offset, skb); }else {skb_pull (skb, pkt_offset); -skb_trim (skb, pkt_stat.pkt_len);rtw_update_rx_freq_for_invalid (rtwdev, skb, &rx_status, &pkt_stat);@@ -597,12 +607,12 @@ static void rtw_usb_rx_handler(struct work_struct *work)ieee80211_rx_irqsafe (rtwdev->hw, skb); } - skb = next_skb; -if (skb) -skb_pull (skb, next_pkt); +skip_packet: + next_pkt =round_up (skb_len,8 ); + rx_desc += next_pkt; + }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len); - urb_len -= next_pkt; - }while (skb); +dev_kfree_skb_any (rx_skb); } } --2.47.0
Ping-Ke Shih
maintainerRE: [PATCH 2/2] wifi: rtw88: usb: Preallocate and reuse the RX skbs
In reply to Bitterblue SmithQuoted 20 lines · Bitterblue Smith
Quoted 19 lines · Ping-Ke Shih
Quoted 18 lines · Bitterblue Smith
The USB driver uses four USB Request Blocks for RX. Before submitting one, it allocates a 32768 byte skb for the RX data. This allocation can fail, maybe due to temporary memory fragmentation. When the allocation fails, the corresponding URB is never submitted again. After four such allocation failures, all RX stops because the driver is not requesting data from the device anymore.
Don't allocate a 32768 byte skb when submitting a USB Request Block (which happens very often). Instead preallocate 8 such skbs, and reuse them over and over. If all 8 are busy, allocate a new one. This is pretty rare. If the allocation fails, use a work to try again later. When there are enough free skbs again, free the excess skbs.
Also, use
WQ_BHfor the RX workqueue. With a normal or high priority workqueue the skbs are processed too slowly when the system is even a little busy, like when opening a new page in a browser, and the driver runs out of free skbs and allocates a lot of new ones.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Move
C2H_ADAPTIVITYto the c2h workqueue instead of handling it directly fromrtw_fw_c2h_cmd_rx_irqsafe(), which runs in the RX workqueue. It reads hardware registers, which is not "irqsafe" with USB.This part should be in a separated patch.
Do you mean just
C2H_ADAPTIVITYorWQ_BHas well?
Just C2H_ADAPTIVITY.
With patch subject, people can't understand this change.
Quoted 68 lines
Quoted 58 lines
Quoted 58 lines
This is more or less what the out-of-tree Realtek drivers do, except they use a tasklet instead of a BH workqueue.
Tested with RTL8723DU, RTL8821AU, RTL8812AU, RTL8812BU, RTL8822CU, RTL8811CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/fw.c | 7 +-- drivers/net/wireless/realtek/rtw88/usb.c | 73 ++++++++++++++++++++---- drivers/net/wireless/realtek/rtw88/usb.h | 3 + 3 files changed, 67 insertions(+), 16 deletions(-)drivers/net/wireless/realtek/rtw88/fw.c +3
-4@@ -332,6 +332,9 @@ void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)case C2H_RA_RPT :rtw_fw_ra_report_handle (rtwdev, c2h->payload, len);break ; +case C2H_ADAPTIVITY : +rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); +break ;default :rtw_dbg (rtwdev,RTW_DBG_FW ,"C2H 0x%x isn't handled\n" , c2h->id);break ;@@ -367,10 +370,6 @@ void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,rtw_fw_scan_result (rtwdev, c2h->payload, len);dev_kfree_skb_any (skb);break ; -case C2H_ADAPTIVITY : -rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); -dev_kfree_skb_any (skb); -break ;default :/* pass offset for further operation */ *((u32 *)skb->cb) = pkt_offset;drivers/net/wireless/realtek/rtw88/usb.c +1
-1@@ -585,7 +585,7 @@ static void rtw_usb_rx_handler(struct work_struct *work) goto skip_packet; } - skb =alloc_skb (skb_len,GFP_KERNEL ); + skb =alloc_skb (skb_len,GFP_ATOMIC );if (!skb) { rtw_dbg(rtwdev, RTW_DBG_USB, "failed to allocate RX skb of size %u\n",@@ -612,7 +612,11 @@ static void rtw_usb_rx_handler(struct work_struct *work) rx_desc += next_pkt; }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len);Quoted 9 lines
drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 619
Quoted 6 lines
drivers/net/wireless/realtek/rtw88/usb.c +5
-1-dev_kfree_skb_any (rx_skb); +if (skb_queue_len (&rtwusb->rx_free_queue) >= +RTW_USB_RX_SKB_NUM -RTW_USB_RXCB_NUM ) +dev_kfree_skb_any (rx_skb); +else +skb_queue_tail (&rtwusb->rx_free_queue, rx_skb);Why not just queue and reuse
rx_skbfor all? That would be simpler.
Quoted 2 lines
I didn't want to let it allocate too many skbs. I didn't find any kind of limit in the official drivers, so maybe it would be fine.
The case ' skb_queue_len(&rtwusb->rx_free_queue) >= 8 - 4' is rare? If so, I think this is fine. If not, to repeatedly allocate and free would cause memory fragment, and higher probability to failed to allocate memory with GFP_ATOMIC. Also seemingly additional 4 persistent rx_skb is not a big cost.
Quoted 96 lines · Ping-Ke Shih
Quoted 7 lines · Bitterblue Smith
Quoted 6 lines · Ping-Ke Shih
Quoted 4 lines · Bitterblue Smith
"iperf3 -c 192.168.0.1 -R --udp -b 0" shows about 40% of datagrams are lost. Many torrents don't download faster than 3 MiB/s, probably because the Bittorrent protocol uses UDP. This is somehow related to the use of
skb_clone()in the RX path.Quoted 6 lines
Quoted 3 lines
Using
skb_clone()can improve throughput is weird to me too. Do you check top with 100% cpu usage?I checked the CPU usage now and the results are interesting. In short, patch 1/2 slightly raises the CPU usage, and patch 2/2 lowers it a lot.
Originally I just wanted to know if throughput is a limit of CPU bound. Anyway good to know this patchset can improve CPU usage.
Quoted 78 lines
Quoted 77 lines
Quoted 63 lines
Don't use
skb_clone(). Instead allocate a new skb for each 802.11 frame received and copy the data from the big (32768 byte) skb.With this patch, "iperf3 -c 192.168.0.1 -R --udp -b 0" shows only 1-2% of datagrams are lost, and torrents can reach download speeds of 36 MiB/s.
Tested with RTL8812AU and RTL8822CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/usb.c | 52 ++++++++++++++---------- 1 file changed, 31 insertions(+), 21 deletions(-)drivers/net/wireless/realtek/rtw88/usb.c +10
-8@@ -7,6 +7,7 @@#include <linux/mutex.h> #include "main.h" #include "debug.h" +#include "mac.h" #include "reg.h" #include "tx.h" #include "rx.h" @@ -546,49 +547,58 @@ static void rtw_usb_rx_handler(struct work_struct *work) { struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work);struct rtw_dev *rtwdev = rtwusb->rtwdev; -const struct rtw_chip_info *chip = rtwdev->chip; -u32 pkt_desc_sz = chip->rx_pkt_desc_sz;struct ieee80211_rx_status rx_status; -u32 pkt_offset, next_pkt, urb_len;struct rtw_rx_pkt_stat pkt_stat; -struct sk_buff *next_skb; +struct sk_buff *rx_skb;struct sk_buff *skb; +u32 pkt_desc_sz = rtwdev->chip->rx_pkt_desc_sz; +u32 max_skb_len = pkt_desc_sz +PHY_STATUS_SIZE *8 + +IEEE80211_MAX_MPDU_LEN_VHT_11454 ; +u32 pkt_offset, next_pkt, skb_len;u8 *rx_desc;int limit;for (limit =0 ; limit <200 ; limit++) { - skb =skb_dequeue (&rtwusb->rx_queue); -if (!skb) + rx_skb =skb_dequeue (&rtwusb->rx_queue); +if (!rx_skb)break ;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 (skb); +dev_kfree_skb_any (rx_skb);continue ; } - urb_len = skb->len; + rx_desc = rx_skb->data;drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 581
Quoted 10 lines
drivers/net/wireless/realtek/rtw88/usb.c +2
-2do { - rx_desc = skb->data; rtw_rx_query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + pkt_stat.shift; - next_pkt = round_up(pkt_stat.pkt_len + pkt_offset, 8); + skb_len = pkt_stat.pkt_len + pkt_offset; + if (skb_len > max_skb_len) {This seems a new rule introduced by this patch. Do you really encounter this case? Maybe this is the cause of slow download throughput?
Quoted 8 lines
Quoted 2 lines
No, I never saw this case. It just seemed like a good idea to check the size passed to
alloc_skb. Maybe it's not needed?I think it is fine.
Asking some questions before, I just tried to find a cause why 40% datagram get lost as you mentioned in commit message, but I can't.
By the way, I saw 30-40% datagrams lost with RTL8822CE as well. But the PCI driver is not using skb_clone so I'm not sure what is going on there.
Re: [PATCH 2/2] wifi: rtw88: usb: Preallocate and reuse the RX skbs
In reply to Ping-Ke ShihQuoted 102 lines · Ping-Ke Shih
Quoted 20 lines · Bitterblue Smith
Quoted 19 lines · Ping-Ke Shih
Quoted 18 lines · Bitterblue Smith
The USB driver uses four USB Request Blocks for RX. Before submitting one, it allocates a 32768 byte skb for the RX data. This allocation can fail, maybe due to temporary memory fragmentation. When the allocation fails, the corresponding URB is never submitted again. After four such allocation failures, all RX stops because the driver is not requesting data from the device anymore.
Don't allocate a 32768 byte skb when submitting a USB Request Block (which happens very often). Instead preallocate 8 such skbs, and reuse them over and over. If all 8 are busy, allocate a new one. This is pretty rare. If the allocation fails, use a work to try again later. When there are enough free skbs again, free the excess skbs.
Also, use
WQ_BHfor the RX workqueue. With a normal or high priority workqueue the skbs are processed too slowly when the system is even a little busy, like when opening a new page in a browser, and the driver runs out of free skbs and allocates a lot of new ones.Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Move
C2H_ADAPTIVITYto the c2h workqueue instead of handling it directly fromrtw_fw_c2h_cmd_rx_irqsafe(), which runs in the RX workqueue. It reads hardware registers, which is not "irqsafe" with USB.This part should be in a separated patch.
Do you mean just
C2H_ADAPTIVITYorWQ_BHas well?Just
C2H_ADAPTIVITY.With patch subject, people can't understand this change.
Quoted 68 lines
Quoted 67 lines
Quoted 55 lines
This is more or less what the out-of-tree Realtek drivers do, except they use a tasklet instead of a BH workqueue.
Tested with RTL8723DU, RTL8821AU, RTL8812AU, RTL8812BU, RTL8822CU, RTL8811CU.
Signed-off-by Bitterblue Smith <[email protected]>--- drivers/net/wireless/realtek/rtw88/fw.c | 7 +-- drivers/net/wireless/realtek/rtw88/usb.c | 73 ++++++++++++++++++++---- drivers/net/wireless/realtek/rtw88/usb.h | 3 + 3 files changed, 67 insertions(+), 16 deletions(-)drivers/net/wireless/realtek/rtw88/fw.c +3
-4@@ -332,6 +332,9 @@ void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)case C2H_RA_RPT :rtw_fw_ra_report_handle (rtwdev, c2h->payload, len);break ; +case C2H_ADAPTIVITY : +rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); +break ;default :rtw_dbg (rtwdev,RTW_DBG_FW ,"C2H 0x%x isn't handled\n" , c2h->id);break ;@@ -367,10 +370,6 @@ void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,rtw_fw_scan_result (rtwdev, c2h->payload, len);dev_kfree_skb_any (skb);break ; -case C2H_ADAPTIVITY : -rtw_fw_adaptivity_result (rtwdev, c2h->payload, len); -dev_kfree_skb_any (skb); -break ;default :/* pass offset for further operation */ *((u32 *)skb->cb) = pkt_offset;drivers/net/wireless/realtek/rtw88/usb.c +1
-1@@ -585,7 +585,7 @@ static void rtw_usb_rx_handler(struct work_struct *work) goto skip_packet; } - skb =alloc_skb (skb_len,GFP_KERNEL ); + skb =alloc_skb (skb_len,GFP_ATOMIC );if (!skb) { rtw_dbg(rtwdev, RTW_DBG_USB, "failed to allocate RX skb of size %u\n",drivers/net/wireless/realtek/rtw88/usb.c static void rtw_usb_rx_handler(struct work_struct *work) · line 619
Quoted 9 lines
drivers/net/wireless/realtek/rtw88/usb.c +5
-1@@ -612,7 +612,11 @@ static void rtw_usb_rx_handler(struct work_struct *work) rx_desc += next_pkt; }while (rx_desc + pkt_desc_sz < rx_skb->data + rx_skb->len); -dev_kfree_skb_any (rx_skb); +if (skb_queue_len (&rtwusb->rx_free_queue) >= +RTW_USB_RX_SKB_NUM -RTW_USB_RXCB_NUM ) +dev_kfree_skb_any (rx_skb); +else +skb_queue_tail (&rtwusb->rx_free_queue, rx_skb);Why not just queue and reuse
rx_skbfor all? That would be simpler.
Quoted 10 lines
Quoted 2 lines
I didn't want to let it allocate too many skbs. I didn't find any kind of limit in the official drivers, so maybe it would be fine.
The case '
skb_queue_len(&rtwusb->rx_free_queue)>= 8 - 4' is rare? If so, I think this is fine. If not, to repeatedly allocate and free would cause memory fragment, and higher probability to failed to allocate memory withGFP_ATOMIC. Also seemingly additional 4 persistentrx_skbis not a big cost.
I feel like it's rare for me, but I never counted how often it happens.
Someone else reported that in a period of 14.5 hours additional skbs were allocated and freed 295 times. This is their log:
https://github.com/user-attachments/files/17792914/dmesg_grep_rtw_8821au.txt
This computer has i7-6700 CPU with 32 GB of RAM.
Another user reported that additional skbs were allocated and freed 3824 times in a period of 1.25 hours. This is their log:
https://github.com/user-attachments/files/18008735/log.txt
This computer is a Raspberry Pi 3B with 1 GB RAM.
I agree that another 4 skbs would be fine. I don't know what the upper limit should be. The user with the Raspberry Pi 3B also tested with no limit and reported that the driver allocated 17 extra skbs. That seems like too many to me.