Linux wireless drivers development
 help / color / mirror / Atom feed
From: Felix Fietkau <nbd@nbd.name>
To: linux-wireless@vger.kernel.org
Subject: [PATCH mt76-next 02/20] wifi: mt76: mt7925: free removed vif links after an RCU grace period
Date: Fri, 20 Nov 2026 18:44:41 +0100	[thread overview]
Message-ID: <20261120174459.1657382-2-nbd@nbd.name> (raw)
In-Reply-To: <20261120174459.1657382-1-nbd@nbd.name>

mt7925_change_vif_links() frees the mconf and mlink of a removed link
with devm_kfree() before it clears mvif->link_conf[] and
mvif->sta.link[], and without a grace period. Readers such as
mt7925_mcu_bss_absence_iter() hold RCU alone, so a change of the active
links can cause a use after free. The error path of a link add frees
the same way, and leaves dev->mt76.wcid[] naming the freed mlink.

Free the links with kfree_rcu() after every pointer to them is cleared,
as mt7996_vif_link_free() does. On the error path, also remove the BSS
of each link whose add completed, and make mt7925_mac_link_bss_add()
undo its setup of an allocated link when the firmware command fails.
The embedded link keeps its state until mt7925_remove_interface()
releases it.

Fixes: 69acd6d910b0 ("wifi: mt76: mt7925: add mt7925_change_vif_links")
Fixes: ac03e5b82bc6 ("wifi: mt76: mt7925: Enhance mt7925_mac_link_bss_add to support MLO")
Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
 .../net/wireless/mediatek/mt76/mt7925/main.c  | 70 ++++++++++++-------
 1 file changed, 45 insertions(+), 25 deletions(-)

diff --git a/drivers/net/wireless/mediatek/mt76/mt7925/main.c b/drivers/net/wireless/mediatek/mt76/mt7925/main.c
index d892434de54c..1fac66e8c50a 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7925/main.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7925/main.c
@@ -449,6 +449,12 @@ static int mt7925_mac_link_bss_add(struct mt792x_dev *dev,
 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, link_conf, &mconf->mt76,
 					  &mlink->wcid, true);
 	if (ret) {
+		if (mconf != &mvif->bss_conf) {
+			rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
+			mt76_wcid_cleanup(&dev->mt76, &mlink->wcid);
+			dev->mt76.vif_mask &= ~BIT_ULL(mconf->mt76.idx);
+			mvif->phy->omac_mask &= ~BIT_ULL(mconf->mt76.omac_idx);
+		}
 		mt7925_p2p_bmc_remove(dev, mconf);
 		goto out;
 	}
@@ -2474,6 +2480,25 @@ static void mt7925_link_info_changed(struct ieee80211_hw *hw,
 	mt792x_mutex_release(dev);
 }
 
+static void
+mt7925_vif_link_remove(struct mt792x_dev *dev, struct mt792x_vif *mvif,
+		       unsigned int link_id, struct mt792x_bss_conf *mconf,
+		       struct mt792x_link_sta *mlink, bool bss_remove)
+{
+	rcu_assign_pointer(mvif->link_conf[link_id], NULL);
+	rcu_assign_pointer(mvif->sta.link[link_id], NULL);
+
+	if (bss_remove)
+		mt792x_mac_link_bss_remove(dev, mconf, mlink);
+
+	if (mconf != &mvif->bss_conf) {
+		mt7925_p2p_bmc_remove(dev, mconf);
+		kfree_rcu(mconf, mt76.rcu_head);
+	}
+	if (mlink != &mvif->sta.deflink)
+		kfree_rcu(mlink, rcu_head);
+}
+
 static int
 mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 			u16 old_links, u16 new_links,
@@ -2487,6 +2512,7 @@ mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
 	struct ieee80211_bss_conf *link_conf;
+	unsigned long added = 0;
 	unsigned int link_id;
 	int err;
 
@@ -2502,15 +2528,8 @@ mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 		if (!mconf || !mlink)
 			continue;
 
-		if (mconf != &mvif->bss_conf) {
-			mt792x_mac_link_bss_remove(dev, mconf, mlink);
-			mt7925_p2p_bmc_remove(dev, mconf);
-			devm_kfree(dev->mt76.dev, mconf);
-			devm_kfree(dev->mt76.dev, mlink);
-		}
-
-		rcu_assign_pointer(mvif->link_conf[link_id], NULL);
-		rcu_assign_pointer(mvif->sta.link[link_id], NULL);
+		mt7925_vif_link_remove(dev, mvif, link_id, mconf, mlink,
+				       mconf != &mvif->bss_conf);
 	}
 
 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
@@ -2519,13 +2538,13 @@ mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 			mconf = &mvif->bss_conf;
 			mlink = &mvif->sta.deflink;
 		} else {
-			mconf = devm_kzalloc(dev->mt76.dev, sizeof(*mconf),
-					     GFP_KERNEL);
-			mlink = devm_kzalloc(dev->mt76.dev, sizeof(*mlink),
-					     GFP_KERNEL);
+			mconf = kzalloc_obj(*mconf);
+			mlink = kzalloc_obj(*mlink);
 			if (!mconf || !mlink) {
-				mt792x_mutex_release(dev);
-				return -ENOMEM;
+				kfree(mconf);
+				kfree(mlink);
+				err = -ENOMEM;
+				goto free_unpublished;
 			}
 		}
 
@@ -2554,6 +2573,8 @@ mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 		if (err < 0)
 			goto free;
 
+		added |= BIT(link_id);
+
 		if (mconf != &mvif->bss_conf) {
 			err = mt7925_set_mlo_roc(phy, &mvif->bss_conf,
 						 vif->active_links);
@@ -2581,18 +2602,17 @@ mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 	return 0;
 
 free:
+	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS)
+		mt7925_vif_link_remove(dev, mvif, link_id, mconfs[link_id],
+				       mlinks[link_id], added & BIT(link_id));
+	goto out;
+
+free_unpublished:
 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
-		rcu_assign_pointer(mvif->link_conf[link_id], NULL);
-		rcu_assign_pointer(mvif->sta.link[link_id], NULL);
-
-		if (mconfs[link_id] != &mvif->bss_conf) {
-			mt7925_p2p_bmc_remove(dev, mconfs[link_id]);
-			devm_kfree(dev->mt76.dev, mconfs[link_id]);
-		}
-		if (mlinks[link_id] != &mvif->sta.deflink)
-			devm_kfree(dev->mt76.dev, mlinks[link_id]);
+		kfree(mconfs[link_id]);
+		kfree(mlinks[link_id]);
 	}
-
+out:
 	mt792x_mutex_release(dev);
 
 	return err;
-- 
2.47.3


  reply	other threads:[~2026-11-20 17:45 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 17:44 [PATCH mt76-next 01/20] wifi: mt76: mt7925: free the vif links when all links go Felix Fietkau
2026-11-20 17:44 ` Felix Fietkau [this message]
2026-11-20 17:44 ` [PATCH mt76-next 03/20] wifi: mt76: remove a replayed station from the driver before the add Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 04/20] wifi: mt76: mt7925: run the reset link replay under the wiphy lock Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 05/20] wifi: mt76: mt7925: take the mutex before the link_info_changed lookup Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 06/20] wifi: mt76: mt7925: hold the mutex for the link lookup in conf_tx Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 07/20] wifi: mt76: mt7925: skip a polled link without a station Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 08/20] wifi: mt76: mt7925: skip a link without a link conf in the MLO PS update Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 09/20] wifi: mt76: mt7925: restore the beacon filter when monitor mode ends Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 10/20] wifi: mt76: usb: do not park the TX worker again under MT76_RESET Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 11/20] wifi: mt76: mt7925: keep the default link in the active links Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 12/20] wifi: mt76: mt7925: read the monitor state under the mutex for deep sleep Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 13/20] wifi: mt76: clear roc_link before the ROC link is freed Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 14/20] wifi: mt76: mt792x: handle all interfaces of a channel context Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 15/20] wifi: mt76: mt792x: set up the sniffer when a monitor gets its channel Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 16/20] wifi: mt76: buffer Disassociation frames for stations in power save Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 17/20] wifi: mt76: mt7921: read the monitor state under the mutex for deep sleep Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 18/20] wifi: mt76: mt7925: wake the device to unassign a channel context Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 19/20] wifi: mt76: mt7996: keep the firmware log settings across a reload Felix Fietkau
2026-11-20 17:44 ` [PATCH mt76-next 20/20] wifi: mt76: mt7915: " Felix Fietkau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120174459.1657382-2-nbd@nbd.name \
    --to=nbd@nbd.name \
    --cc=linux-wireless@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox