wifi: rtw88: three fixes around the firmware recovery
v2 cover wifi: rtw88: three fixes around the firmware recovery
From: Mehmet Fide <[email protected]>
Three fixes for what a firmware crash test showed on an RTL8822CE. They do not depend on the channel switch series, but patch 1 and patch 2 of that series (v11) both change rtw_fw_update_beacon_work():
the one applied second needs both conditions in its test, and I will rebase it when asked.
Tested on rtw-next on an i.MX8MP board with PROVE_LOCKING, DEBUG_ATOMIC_SLEEP and kmemleak, with hostapd. The firmware crashes were triggered from debugfs on the RTL8822CE:
- patch 1: three crashes with an associated station interface lost five reserved pages before and none after; six crashes with an AP interface lost three before and none after. A test build that queues the beacon update work between the reset and the reconfiguration warned "Should not have an empty reserved page" with v1 and did not with v2
- patch 2: an unbind after the crashes warned "Have pending ack frames!" before and did not after. With a test build that drops every TX report of the firmware, the unbind of the RTL8822CE warned before; after, neither it nor the RTL8822BU did
- patch 3: each of three crashes with an associated station gave the "sleeping function called from invalid context" report before and none did after; the link was back after every crash
With the three patches, AP, association, power save, firmware crash and scan tests on the RTL8822CE, RTL8822CU, RTL8822BU and RTL8821CU (two adapters) on both bands gave no kernel report and no kmemleak report, also after the rebase.
v2:
- patch 1: the beacon update work does nothing while RTW_FLAG_RESTARTING is set (Luka Gejak)
- patches 2 and 3: Reviewed-by Luka Gejak, no change
- rebased on rtw-next
v1: https://lore.kernel.org/linux-wireless/[email protected]/
Mehmet Fide (3):
wifi: rtw88: free the reserved pages of an interface on firmware
recovery
wifi: rtw88: report the frames purged from the TX report queue
wifi: rtw88: reset the stations outside of RCU on firmware recovery
drivers/net/wireless/realtek/rtw88/fw.c | 6 ++++-- drivers/net/wireless/realtek/rtw88/main.c | 8 +++----- drivers/net/wireless/realtek/rtw88/tx.c | 20 ++++++++++++++++---- drivers/net/wireless/realtek/rtw88/tx.h | 1 + 4 files changed, 24 insertions(+), 11 deletions(-)
base-commit: 61a5eb87b2d9b4d5e353780b781cf691f39ba9e1
Signature · 1 line
2.56.0
PATCH v2 1/3 wifi: rtw88: free the reserved pages of an interface on firmware recovery
From: Mehmet Fide <[email protected]>
The recovery after a firmware crash resets the driver state of every interface, and mac80211 then adds the interfaces again without removing them first. rtw_ops_add_interface() starts with an empty reserved page list, so the pages the interface had before the crash are lost: the next page build takes them off the build list and nothing frees them.
kmemleak after a firmware crash with an AP interface:
unreferenced object 0xffff0000cbe22c60 (size 96):
comm "hostapd", pid 25545, jiffies 4296463067
backtrace (crc cbdc21c4):
kmemleak_alloc+0xcc/0xe4
__kmalloc_cache_noprof+0x234/0x3bc
rtw_add_rsvd_page+0x38/0xa8 [rtw88_core]
rtw_add_rsvd_page_bcn+0x30/0x58 [rtw88_core]
rtw_ops_add_interface+0x144/0x2cc [rtw88_core]
rtw_ops_change_interface+0x6c/0x80 [rtw88_core]
drv_change_interface+0x100/0x258 [mac80211]Free the pages where the recovery resets the interface.
rtw_ops_set_tim() can queue the beacon update work between this reset and the reconfiguration, for a frame buffered for a dozing station. The work would then find no page to build and warn about an empty reserved page. Let it do nothing while RTW_FLAG_RESTARTING is set: the reconfiguration downloads the beacon again.
---
Notes:
v2: the beacon update work does nothing while RTW_FLAG_RESTARTING is set
(Luka Gejak): rtw_ops_set_tim() can queue it between the reset and the reconfiguration, where it found no page and warned
drivers/net/wireless/realtek/rtw88/fw.c | 6 ++++-- drivers/net/wireless/realtek/rtw88/main.c | 1 + 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw88/fw.c +4 -2
@@ -1800,8 +1800,10 @@ void rtw_fw_update_beacon_work(struct work_struct *work) update_beacon_work);mutex_lock (&rtwdev->mutex); -rtw_fw_download_rsvd_page (rtwdev); -rtw_send_rsvd_page_h2c (rtwdev); +if (!test_bit (RTW_FLAG_RESTARTING , rtwdev->flags)) { +rtw_fw_download_rsvd_page (rtwdev); +rtw_send_rsvd_page_h2c (rtwdev); + }mutex_unlock (&rtwdev->mutex); }
drivers/net/wireless/realtek/rtw88/main.c +1 -0
@@ -627,6 +627,7 @@ static void rtw_reset_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)rtw_bf_disassoc (rtwdev, vif,NULL );rtw_vif_assoc_changed (rtwvif,NULL );rtw_txq_cleanup (rtwdev, vif->txq); +rtw_remove_rsvd_page (rtwdev, rtwvif);rtw_release_macid (rtwdev, rtwvif->mac_id); }
Signature · 1 line
2.56.0
Re: [PATCH rtw-next v2 1/3] wifi: rtw88: free the reserved pages of an interface on firmware recovery
Hi Mehmet,
Thanks for the update. Three comments below.
drivers/net/wireless/realtek/rtw88/fw.c void rtw_fw_update_beacon_work(struct work_struct *work) · line 1803
Quoted 5 lines · Mehmet Fide
drivers/net/wireless/realtek/rtw88/fw.c +4
-0@@ -1800,8 +1800,10 @@ void rtw_fw_update_beacon_work(struct work_struct *work) +if (!test_bit (RTW_FLAG_RESTARTING , rtwdev->flags)) { +rtw_fw_download_rsvd_page (rtwdev); +rtw_send_rsvd_page_h2c (rtwdev); + }
Could a skipped TIM update be retried at reconfig_complete()? After reconfig's last beacon download, sta_info_cleanup() can still expire a buffered frame and queue this work. If it runs before the flag clears, the beacon keeps the old TIM bit.
[ ... ]
drivers/net/wireless/realtek/rtw88/fw.c void rtw_fw_update_beacon_work(struct work_struct *work) · line 1803
Quoted 1 line
drivers/net/wireless/realtek/rtw88/fw.c +1
-0+if (!test_bit (RTW_FLAG_RESTARTING , rtwdev->flags)) {
Also, if the last interface closes before restart work takes the wiphy lock, open_count is zero and reconfig_complete() is skipped. The flag survives normal reopening, so the worker still skips TIM updates. Could that exit be covered too?
[ ... ]
drivers/net/wireless/realtek/rtw88/main.c static void rtw_reset_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif) · line 627
Quoted 2 lines
drivers/net/wireless/realtek/rtw88/main.c +1
-0@@ -627,6 +627,7 @@ static void rtw_reset_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif) +rtw_remove_rsvd_page (rtwdev, rtwvif);
As far as I can see, rtw_core_stop() drops the driver mutex without holding wiphy. A concurrent beacon change can then reach:
ieee80211_change_beacon()
rtw_ops_bss_info_changed()
rtw_fw_download_rsvd_page()The lists are already empty, and mac80211 restart has not begun yet. This seems to leave a path to the empty reserved page warning. Could the direct download path be covered too?
Best regards,
Luka Gejak
Re: [PATCH rtw-next v2 1/3] wifi: rtw88: free the reserved pages of an interface on firmware recovery
Hi Luka,
Quoted 4 lines
Could a skipped TIM update be retried at
reconfig_complete()? After reconfig's last beacon download,sta_info_cleanup()can still expire a buffered frame and queue this work. If it runs before the flag clears, the beacon keeps the old TIM bit.
Yes, that update was lost. v3 drops the RTW_FLAG_RESTARTING test from the work. Instead rtw_fw_download_rsvd_page() and rtw_send_rsvd_page_h2c() do nothing while no port is in use: the recovery clears the ports with the pages, and rtw_ops_add_interface() sets them again. With a test build that queues the work during the reconfiguration, v2 skipped the download and v3 did it.
Quoted 4 lines
Also, if the last interface closes before restart work takes the wiphy lock,
open_countis zero andreconfig_complete()is skipped. The flag survives normal reopening, so the worker still skips TIM updates. Could that exit be covered too?
With the interface closed in that window, the flag stayed set after reopening, and writing fw_crash returned -EINPROGRESS; rtw_fw_recovery() would ignore a real crash the same way. The pages no longer depend on the flag in v3, and a new patch clears it in rtw_ops_stop().
Quoted 6 lines
As far as I can see,
rtw_core_stop()drops the driver mutex without holding wiphy. A concurrent beacon change can then reach:ieee80211_change_beacon() rtw_ops_bss_info_changed() rtw_fw_download_rsvd_page()
Two hostapd update_beacon calls in that window warned twice with v2. The port test covers this path as well, and v3 did not warn.
Mehmet
PATCH v2 2/3 wifi: rtw88: report the frames purged from the TX report queue
In reply to Mehmet FideFrom: Mehmet Fide <[email protected]>
A frame whose sender asked for its TX status waits in the TX report queue for the report of the firmware. When no report comes, the purge timer frees the frames with skb_queue_purge(), and rtw_core_deinit() does the same. mac80211 is not told: the sender gets no status, and the copy mac80211 keeps for the status of a frame from user space stays in its list until ieee80211_free_hw() warns about it. On an RTL8822CE with hostapd, after a firmware crash and an unbind of the driver:
rtw88_8822ce 0000:01:00.0: failed to get tx report from firmware [...]
Have pending ack frames!
WARNING: net/mac80211/main.c:1761 at ieee80211_free_ack_frame+0x4c/0x70 [mac80211], CPU#1: sh/24725 Call trace: ieee80211_free_ack_frame+0x4c/0x70 [mac80211] (P) idr_for_each+0x6c/0xf4 ieee80211_free_hw+0x30/0x8c [mac80211] rtw_pci_remove+0xd8/0xf0 [rtw88_pci] pci_device_remove+0x44/0xd4
Take the frames off the queue under its lock and hand them to ieee80211_purge_tx_queue(), which reports each one as not sent.
---
Notes:
v2: Reviewed-by Luka Gejak, no change
drivers/net/wireless/realtek/rtw88/main.c | 5 +---- drivers/net/wireless/realtek/rtw88/tx.c | 20 ++++++++++++++++---- drivers/net/wireless/realtek/rtw88/tx.h | 1 + 3 files changed, 18 insertions(+), 8 deletions(-)
drivers/net/wireless/realtek/rtw88/main.c +1 -4
@@ -2225,7 +2225,6 @@ void rtw_core_deinit(struct rtw_dev *rtwdev)struct rtw_fw_state *fw = &rtwdev->fw;struct rtw_fw_state *wow_fw = &rtwdev->wow_fw;struct rtw_rsvd_page *rsvd_pkt, *tmp; -unsigned long flags;rtw_wait_firmware_completion (rtwdev);
@@ -2237,9 +2236,7 @@ void rtw_core_deinit(struct rtw_dev *rtwdev)destroy_workqueue (rtwdev->tx_wq);timer_delete_sync (&rtwdev->tx_report.purge_timer); -spin_lock_irqsave (&rtwdev->tx_report.q_lock, flags); -skb_queue_purge (&rtwdev->tx_report.queue); -spin_unlock_irqrestore (&rtwdev->tx_report.q_lock, flags); +rtw_tx_report_purge (rtwdev);skb_queue_purge (&rtwdev->coex.queue);skb_queue_purge (&rtwdev->c2h_queue);
drivers/net/wireless/realtek/rtw88/tx.c +16 -4
@@ -176,21 +176,33 @@ static void rtw_tx_report_enable(struct rtw_dev *rtwdev, pkt_info->report = true; } +void rtw_tx_report_purge (struct rtw_dev *rtwdev) +{ +struct rtw_tx_report *tx_report = &rtwdev->tx_report; +struct sk_buff_head purge; +unsigned long flags; + +__skb_queue_head_init (&purge); + +spin_lock_irqsave (&tx_report->q_lock, flags); +skb_queue_splice_init (&tx_report->queue, &purge); +spin_unlock_irqrestore (&tx_report->q_lock, flags); + +ieee80211_purge_tx_queue (rtwdev->hw, &purge); +} +void rtw_tx_report_purge_timer (struct timer_list *t) {struct rtw_dev *rtwdev =timer_container_of (rtwdev, t, tx_report.purge_timer);struct rtw_tx_report *tx_report = &rtwdev->tx_report; -unsigned long flags;if (skb_queue_len (&tx_report->queue) ==0 )return ;rtw_warn (rtwdev,"failed to get tx report from firmware\n" ); -spin_lock_irqsave (&tx_report->q_lock, flags); -skb_queue_purge (&tx_report->queue); -spin_unlock_irqrestore (&tx_report->q_lock, flags); +rtw_tx_report_purge (rtwdev); }void rtw_tx_report_enqueue (struct rtw_dev *rtwdev,struct sk_buff *skb,u8 sn)
drivers/net/wireless/realtek/rtw88/tx.h +1 -0
@@ -98,6 +98,7 @@ void rtw_tx_pkt_info_update(struct rtw_dev *rtwdev, struct sk_buff *skb);void rtw_tx_fill_tx_desc (struct rtw_dev *rtwdev,struct rtw_tx_pkt_info *pkt_info,struct sk_buff *skb); +void rtw_tx_report_purge (struct rtw_dev *rtwdev);void rtw_tx_report_enqueue (struct rtw_dev *rtwdev,struct sk_buff *skb,u8 sn);void rtw_tx_report_handle (struct rtw_dev *rtwdev,struct sk_buff *skb,int src); void rtw_tx_rsvd_page_pkt_info_update(struct rtw_dev *rtwdev,
Signature · 1 line
2.56.0
PATCH v2 3/3 wifi: rtw88: reset the stations outside of RCU on firmware recovery
In reply to Mehmet FideFrom: Mehmet Fide <[email protected]>
The recovery after a firmware crash removes the driver state of every station with rtw_sta_remove(), called from an atomic station iterator. rtw_sta_remove() cancels the rate work of the station with cancel_work_sync(), which can sleep. With a station associated:
BUG: sleeping function called from invalid context at kernel/workqueue.c:4722 in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 14, name: kworker/u16:1 RCU nest depth: 1, expected: 0 Workqueue: phy35 rtw_fw_recovery_work [rtw88_core] Call trace: __might_resched+0x13c/0x1f0 __might_sleep+0x48/0x7c __cancel_work_sync+0xa8/0xb4 cancel_work_sync+0x14/0x20 rtw_sta_remove+0x34/0x10c [rtw88_core] rtw_reset_sta_iter+0x20/0x48 [rtw88_core] ieee80211_iterate_stations_atomic+0x80/0x144 [mac80211] rtw_fw_recovery_work+0x1dc/0x2b8 [rtw88_core]
Use rtw_iterate_stas(), which collects the stations under RCU and calls the iterator after it. The recovery holds the driver mutex it asks for.
---
Notes:
v2: Reviewed-by Luka Gejak, no change
drivers/net/wireless/realtek/rtw88/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/realtek/rtw88/main.c +1 -1
@@ -664,7 +664,7 @@ static void __fw_recovery_work(struct rtw_dev *rtwdev)WARN (1 ,"firmware crash, start reset and recover\n" );rtw_iterate_keys_atomic (rtwdev,NULL , rtw_reset_key_iter, rtwdev); -rtw_iterate_stas_atomic (rtwdev, rtw_reset_sta_iter, rtwdev); +rtw_iterate_stas (rtwdev, rtw_reset_sta_iter, rtwdev);rtw_iterate_vifs_atomic (rtwdev, rtw_reset_vif_iter, rtwdev);bitmap_zero (rtwdev->hw_port,RTW_PORT_NUM );rtw_enter_ips (rtwdev);
Signature · 1 line
2.56.0
Re: [PATCH rtw-next v2 3/3] wifi: rtw88: reset the stations outside of RCU on firmware recovery
Hi Mehmet,
Thanks for the update. One comment below.
drivers/net/wireless/realtek/rtw88/main.c static void __fw_recovery_work(struct rtw_dev *rtwdev) · line 668
Quoted 7 lines · Mehmet Fide
drivers/net/wireless/realtek/rtw88/main.c +1
-1@@ -664,7 +664,7 @@ static void __fw_recovery_work(struct rtw_dev *rtwdev)WARN (1 ,"firmware crash, start reset and recover\n" );rtw_iterate_keys_atomic (rtwdev,NULL , rtw_reset_key_iter, rtwdev); -rtw_iterate_stas_atomic (rtwdev, rtw_reset_sta_iter, rtwdev); +rtw_iterate_stas (rtwdev, rtw_reset_sta_iter, rtwdev);rtw_iterate_vifs_atomic (rtwdev, rtw_reset_vif_iter, rtwdev);
I followed the allocation failure case from v1 a bit further:
https://lore.kernel.org/r/[email protected]/
As far as I can see, skipping a station also skips rtw_txq_cleanup(). For a queue outside the voice access category, wake_tx_queue() queues tx_work. If that work has not run yet, the omitted station's queue can stay on rtwdev->txqs until reconfiguration. rtw_sta_add() then calls rtw_txq_init(), which does this without unlinking it:
INIT_LIST_HEAD(&rtwtxq->list);
The device list still points at that node, but the node now points to itself. __rtw_tx_work() seems to keep visiting it while holding txq_lock. Its list_del_init() cannot repair the device list because the node has already lost its old links.
rtw_enter_ips() does not drain rtwdev->tx_wq, and the flush in ieee80211_restart_work() is of mac80211's separate workqueue. So I think this can hang the TX worker, beyond the station count and MAC ID leak I mentioned before.
Could recovery use an iteration that cannot silently skip stations?
Best regards,
Luka Gejak
Re: [PATCH rtw-next v2 3/3] wifi: rtw88: reset the stations outside of RCU on firmware recovery
Hi Luka,
Quoted 1 line
Could recovery use an iteration that cannot silently skip stations?
v3 adds a patch before this one: rtw_iterate_stas() links the stations through a list node in struct rtw_sta_info instead of allocating an entry, so the collection cannot fail. All callers hold rtwdev->mutex, and no station iterator collects again.
I could not get the hang on the rig. With a test build that skips the station during the recovery and leaves its BE queue on rtwdev->txqs, the station came back without a list report or a lockup; I assume the TX work ran before the station was added again and took the queue off the list. The skip itself is real, and the new patch removes it.
Mehmet