Skip to contentSkip to conversation

wifi: mt76: unlink TWT flows when MCU ADD fails

PATCH New Zhao Li 1 participant 4 messages Jul 29 – Aug 25

Subsystems (3)
lore
Share & tools
lore

Zhao Li

author
#0 lore raw

cover wifi: mt76: unlink TWT flows when MCU ADD fails

mt7915 and mt7996 link a TWT flow into the device list before asking the MCU to add the agreement. If that request fails, the accounting masks remain clear but the embedded flow node stays linked.

A retry can corrupt the list by reinitializing the same linked node. If the station is removed instead, the mask-gated teardown skips the flow, leaving a dangling node for later scheduling and debugfs walks. The two patches restore the list to its pre-request state on the MCU ADD failure path, split per driver because their Fixes targets differ.

MCU DELETE failures are deliberately outside this series. Deciding whether to drop host bookkeeping while firmware may retain an agreement requires separate ownership and accounting treatment.

Zhao Li (2):
wifi: mt76: mt7915: unlink TWT flow on MCU add failure
wifi: mt76: mt7996: unlink TWT flow on MCU add failure

 drivers/net/wireless/mediatek/mt76/mt7915/mac.c | 4 +++-
 drivers/net/wireless/mediatek/mt76/mt7996/mac.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

base-commit: 57aa1718d5953dd532137d43b696c68545c2e0b3

Signature · 1 line
2.50.1 (Apple Git-155)
author
#1 lore raw

PATCH 1/2 wifi: mt76: mt7915: unlink TWT flow on MCU add failure

mt7915_mac_add_twt_setup() links a new flow into dev->twt_list before asking the MCU to add the agreement, but exits without unlinking the flow if the request fails. The flow-ID and table masks are only committed after MCU success, so the still-linked flow is invisible to the mask-gated teardown paths.

If the station retries the setup, the same slot is cleared and its embedded list node reinitialized while it is still linked, corrupting dev->twt_list and potentially preventing a scheduling walk from terminating. If the station disassociates instead, teardown skips the flow and station storage can be freed while the node remains linked. A later scheduling or debugfs twt_stats walk can then dereference the freed flow, resulting in a use-after-free.

Unlink the flow with list_del_init() on the MCU ADD failure path. This restores the list to its pre-request state and leaves the node safe for a retry. Mask and agreement accounting remain untouched because none of it was committed before the failure.

Fixes 3782b69d03e7 ("mt76: mt7915: introduce mt7915_mac_add_twt_setup routine")
Assisted-by Codex:gpt-5.6-sol
Assisted-by Kimi:K3
---
 drivers/net/wireless/mediatek/mt76/mt7915/mac.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt7915/mac.c +3 -1
index 334c19ab2b22..1ad54a5bb374 100644
@@ -2345,8 +2345,10 @@ void mt7915_mac_add_twt_setup(struct ieee80211_hw *hw,
 	}
 	flow->tsf = le64_to_cpu(twt_agrt->twt);
 
-	if (mt7915_mcu_twt_agrt_update(dev, msta->vif, flow, MCU_TWT_AGRT_ADD))
+	if (mt7915_mcu_twt_agrt_update(dev, msta->vif, flow, MCU_TWT_AGRT_ADD)) {
+		list_del_init(&flow->list);
 		goto unlock;
+	}
 
 	setup_cmd = TWT_SETUP_CMD_ACCEPT;
 	dev->twt.table_mask |= BIT(table_id);
Signature · 1 line
2.50.1 (Apple Git-155)
author
#2 lore raw

PATCH 2/2 wifi: mt76: mt7996: unlink TWT flow on MCU add failure

In reply to Zhao Li

mt7996_mac_add_twt_setup() links a new flow into dev->twt_list before asking the MCU to add the agreement, but exits without unlinking the flow if the request fails. The flow-ID and table masks are only committed after MCU success, so the still-linked flow is invisible to the mask-gated teardown paths.

If the station retries the setup, the same slot is cleared and its embedded list node reinitialized while it is still linked, corrupting dev->twt_list and potentially preventing a scheduling walk from terminating. If the station disassociates instead, teardown skips the flow and station storage can be freed while the node remains linked. A later scheduling or debugfs twt_stats walk can then dereference the freed flow, resulting in a use-after-free.

Unlink the flow with list_del_init() on the MCU ADD failure path. This restores the list to its pre-request state and leaves the node safe for a retry. Mask and agreement accounting remain untouched because none of it was committed before the failure.

Fixes 98686cd21624 ("wifi: mt76: mt7996: add driver for MediaTek Wi-Fi 7 (802.11be) devices")
Assisted-by Codex:gpt-5.6-sol
Assisted-by Kimi:K3
---
 drivers/net/wireless/mediatek/mt76/mt7996/mac.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt7996/mac.c +3 -1
index 0eebc8182ca9..445bd7d63cc8 100644
@@ -3207,8 +3207,10 @@ void mt7996_mac_add_twt_setup(struct ieee80211_hw *hw,
 	flow->tsf = le64_to_cpu(twt_agrt->twt);
 
 	if (mt7996_mcu_twt_agrt_update(dev, &msta->vif->deflink, flow,
-				       MCU_TWT_AGRT_ADD))
+				       MCU_TWT_AGRT_ADD)) {
+		list_del_init(&flow->list);
 		goto unlock;
+	}
 
 	setup_cmd = TWT_SETUP_CMD_ACCEPT;
 	dev->twt.table_mask |= BIT(table_id);
Signature · 1 line
2.50.1 (Apple Git-155)
author
#3 lore raw

PATCH v2 1/1 wifi: mt76: mt7996: unlink TWT flow if the MCU rejects the agreement

mt7996_mac_add_twt_setup() links a new flow into dev->twt_list before asking the MCU to add the agreement, but exits without unlinking the flow if the request fails. The flow-ID and table masks are only committed after MCU success, so the still-linked flow is invisible to the mask-gated teardown paths.

If the station retries the setup, the same slot is cleared and its embedded list node reinitialized while it is still linked, corrupting dev->twt_list. If the station disassociates instead, teardown skips the flow and station storage can be freed while the node remains linked. A later scheduling or debugfs twt_stats walk can then dereference the freed flow, resulting in a use-after-free.

Unlink the flow when the MCU rejects the agreement. This restores the list to its pre-request state while leaving the unchanged mask and agreement accounting untouched.

Fixes 98686cd21624 ("wifi: mt76: mt7996: add driver for MediaTek Wi-Fi 7 (802.11be) devices")
Assisted-by Codex:gpt-5.6-sol
Assisted-by Kimi:K3

---
Changes in v2:
- Rebase onto wireless/main commit 2812e64e1575.
- Send mt7996 as a standalone patch; mt7915 is fixed by commit 16a04441eab0 ("wifi: mt76: mt7915: unlink TWT flow if the MCU rejects the agreement").
- Use list_del() to match the accepted mt7915 fix.

---
 drivers/net/wireless/mediatek/mt76/mt7996/mac.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt7996/mac.c +3 -1
index 0eebc8182ca9..4d3829e4220d 100644
@@ -3207,8 +3207,10 @@ void mt7996_mac_add_twt_setup(struct ieee80211_hw *hw,
 	flow->tsf = le64_to_cpu(twt_agrt->twt);
 
 	if (mt7996_mcu_twt_agrt_update(dev, &msta->vif->deflink, flow,
-				       MCU_TWT_AGRT_ADD))
+				       MCU_TWT_AGRT_ADD)) {
+		list_del(&flow->list);
 		goto unlock;
+	}
 
 	setup_cmd = TWT_SETUP_CMD_ACCEPT;
 	dev->twt.table_mask |= BIT(table_id);
Signature · 1 line
2.50.1 (Apple Git-155)