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 13/20] wifi: mt76: read roc_phy and roc_link once without the mutex
Date: Mon, 23 Nov 2026 14:49:12 +0100	[thread overview]
Message-ID: <20261123134920.154270-13-nbd@nbd.name> (raw)
In-Reply-To: <20261123134920.154270-1-nbd@nbd.name>

mt7996_tx() tests mvif->roc_phy and roc_phy->roc_link without a lock
and loads each field again for its use, and mt76_vif_cleanup() does the
same with roc_phy. mt76_roc_complete() clears both fields under
dev->mutex, so a second load can return NULL, which mt7996_tx() and
mt76_vif_cleanup() then dereference. mt7996_vif_link_roc_abort() also
loads roc_phy before it takes dev->mutex.

Load roc_phy once with READ_ONCE() outside dev->mutex, and store it with
WRITE_ONCE(). It points to a phy, which lives as long as the device.
Make roc_link an RCU pointer, since mt7996_tx() reads it under the RCU
read lock and an offchannel link is freed with kfree_rcu().

A ROC of another interface can start on the phy between the two loads.
mt7996_tx() thus takes the wcid only from a link of its own interface.

Fixes: a8f424c1287c ("wifi: mt76: add multi-radio remain_on_channel functions")
Fixes: 69d54ce7491d ("wifi: mt76: mt7996: switch to single multi-radio wiphy")
Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
 drivers/net/wireless/mediatek/mt76/channel.c   | 14 +++++++-------
 drivers/net/wireless/mediatek/mt76/mac80211.c  |  6 ++++--
 drivers/net/wireless/mediatek/mt76/mt76.h      |  2 +-
 .../net/wireless/mediatek/mt76/mt7996/main.c   | 18 ++++++++++--------
 4 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/drivers/net/wireless/mediatek/mt76/channel.c b/drivers/net/wireless/mediatek/mt76/channel.c
index db2d6dfc12a9..6d9ca6dfecb7 100644
--- a/drivers/net/wireless/mediatek/mt76/channel.c
+++ b/drivers/net/wireless/mediatek/mt76/channel.c
@@ -394,14 +394,14 @@ mt76_vif_roc_phy(struct mt76_dev *dev, struct ieee80211_vif *vif)
 
 void mt76_roc_complete(struct mt76_phy *phy)
 {
-	struct mt76_vif_link *mlink = phy->roc_link;
+	struct mt76_vif_link *mlink = mt76_dereference(phy->roc_link, phy->dev);
 	struct mt76_dev *dev = phy->dev;
 
 	if (!phy->roc_vif)
 		return;
 
 	if (mlink && mlink->mvif->roc_phy == phy)
-		mlink->mvif->roc_phy = NULL;
+		WRITE_ONCE(mlink->mvif->roc_phy, NULL);
 	if (phy->chanctx && phy->main_chandef.chan && phy->offchannel &&
 	    !test_bit(MT76_MCU_RESET, &dev->phy.state)) {
 		__mt76_set_channel(phy, &phy->main_chandef, false);
@@ -409,7 +409,7 @@ void mt76_roc_complete(struct mt76_phy *phy)
 	} else {
 		phy->offchannel = false;
 	}
-	phy->roc_link = NULL;
+	rcu_assign_pointer(phy->roc_link, NULL);
 	mt76_put_vif_phy_link(phy, phy->roc_vif, mlink);
 	phy->roc_vif = NULL;
 	if (!test_bit(MT76_RESTART, &dev->phy.state))
@@ -475,18 +475,18 @@ int mt76_remain_on_channel(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 		goto restart_mac_work;
 	}
 
-	mlink->mvif->roc_phy = phy;
+	WRITE_ONCE(mlink->mvif->roc_phy, phy);
 	phy->roc_vif = vif;
-	phy->roc_link = mlink;
+	rcu_assign_pointer(phy->roc_link, mlink);
 
 	offchannel = mt76_offchannel_chandef(phy, chan, &chandef);
 	if (offchannel)
 		mt76_offchannel_notify(phy, true);
 	ret = __mt76_set_channel(phy, &chandef, offchannel);
 	if (ret) {
-		mlink->mvif->roc_phy = NULL;
+		WRITE_ONCE(mlink->mvif->roc_phy, NULL);
 		phy->roc_vif = NULL;
-		phy->roc_link = NULL;
+		rcu_assign_pointer(phy->roc_link, NULL);
 		mt76_put_vif_phy_link(phy, vif, mlink);
 		goto out;
 	}
diff --git a/drivers/net/wireless/mediatek/mt76/mac80211.c b/drivers/net/wireless/mediatek/mt76/mac80211.c
index e554f2eea7cc..a42a950c20cb 100644
--- a/drivers/net/wireless/mediatek/mt76/mac80211.c
+++ b/drivers/net/wireless/mediatek/mt76/mac80211.c
@@ -2247,11 +2247,13 @@ void mt76_vif_cleanup(struct mt76_dev *dev, struct ieee80211_vif *vif)
 {
 	struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
 	struct mt76_vif_data *mvif = mlink->mvif;
+	struct mt76_phy *phy;
 
 	rcu_assign_pointer(mvif->link[0], NULL);
 	mt76_abort_scan(dev);
-	if (mvif->roc_phy && mvif->roc_phy->roc_vif == vif)
-		mt76_abort_roc(mvif->roc_phy);
+	phy = READ_ONCE(mvif->roc_phy);
+	if (phy && phy->roc_vif == vif)
+		mt76_abort_roc(phy);
 }
 EXPORT_SYMBOL_GPL(mt76_vif_cleanup);
 
diff --git a/drivers/net/wireless/mediatek/mt76/mt76.h b/drivers/net/wireless/mediatek/mt76/mt76.h
index d82d22c14c9f..648f79c4b6d0 100644
--- a/drivers/net/wireless/mediatek/mt76/mt76.h
+++ b/drivers/net/wireless/mediatek/mt76/mt76.h
@@ -887,7 +887,7 @@ struct mt76_phy {
 
 	struct delayed_work roc_work;
 	struct ieee80211_vif *roc_vif;
-	struct mt76_vif_link *roc_link;
+	struct mt76_vif_link __rcu *roc_link;
 
 	struct delayed_work scan_work;
 	struct {
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/main.c b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
index f2bc33db35fb..c39e0ac9176c 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
@@ -2140,17 +2140,19 @@ static void mt7996_tx(struct ieee80211_hw *hw,
 	}
 
 	if (mvif) {
-		struct mt76_vif_link *mlink;
+		struct mt76_vif_link *mlink, *roc_link;
+		struct mt76_phy *roc_phy;
 
 		mlink = rcu_dereference(mvif->mt76.link[link_id]);
 		if (mlink && mlink->wcid)
 			wcid = mlink->wcid;
 
-		if (mvif->mt76.roc_phy &&
-		    (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)) {
-			mphy = mvif->mt76.roc_phy;
-			if (mphy->roc_link)
-				wcid = mphy->roc_link->wcid;
+		roc_phy = READ_ONCE(mvif->mt76.roc_phy);
+		if (roc_phy && (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)) {
+			mphy = roc_phy;
+			roc_link = rcu_dereference(roc_phy->roc_link);
+			if (roc_link && roc_link->mvif == &mvif->mt76)
+				wcid = roc_link->wcid;
 		} else if (mlink) {
 			mphy = mt76_vif_link_phy(mlink);
 		}
@@ -3081,7 +3083,7 @@ static void
 mt7996_vif_link_roc_abort(struct mt7996_dev *dev, struct ieee80211_vif *vif)
 {
 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
-	struct mt76_phy *phy = mvif->mt76.roc_phy;
+	struct mt76_phy *phy = READ_ONCE(mvif->mt76.roc_phy);
 	struct mt76_vif_link *mlink;
 	bool abort;
 
@@ -3089,7 +3091,7 @@ mt7996_vif_link_roc_abort(struct mt7996_dev *dev, struct ieee80211_vif *vif)
 		return;
 
 	mutex_lock(&dev->mt76.mutex);
-	mlink = phy->roc_link;
+	mlink = mt76_dereference(phy->roc_link, &dev->mt76);
 	abort = phy->roc_vif == vif && mlink && !mlink->offchannel;
 	mutex_unlock(&dev->mt76.mutex);
 
-- 
2.47.3


  parent reply	other threads:[~2026-11-23 13:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:49 [PATCH mt76-next 01/20] wifi: mt76: mt7925: undo the BSS of a failed interface add Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 02/20] wifi: mt76: mt7925: keep the embedded link's BMC wcid after a failed add Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 03/20] wifi: mt76: mt7915: read the PS sync state of mt7986 and mt7981 Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 04/20] wifi: mt76: mt7915: leave the BIP check of a group frame to mac80211 Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 05/20] wifi: mt76: mt7921: " Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 06/20] wifi: mt76: mt7925: " Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 07/20] wifi: mt76: mt7615: " Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 08/20] wifi: mt76: mt7925: use wcid_to_sta() in mt7925_mac_write_txwi_8023() Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 09/20] wifi: mt76: log the firmware status of a failed MCU command Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 10/20] wifi: mt76: mt7996: clear the ADM counters only in the airtime poll Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 11/20] wifi: mt76: mt7915: send mt7981 only the group units its firmware holds Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 12/20] wifi: mt76: take the DFS state from the off-channel flag Felix Fietkau
2026-11-23 13:49 ` Felix Fietkau [this message]
2026-11-23 13:49 ` [PATCH mt76-next 14/20] wifi: mt76: mt7996: add only the active links of a station MLD's peer Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 15/20] wifi: mt76: mt7996: resume the radar TX stop at an operating channel set Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 16/20] wifi: mt76: mt7996: divide by the full TWT wake interval Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 17/20] wifi: mt76: mt7915: " Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 18/20] wifi: mt76: mt7996: send the SRG bitmaps in one block per band Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 19/20] wifi: mt76: connac: set the band of an RX block ack scoreboard reset Felix Fietkau
2026-11-23 13:49 ` [PATCH mt76-next 20/20] wifi: mt76: mt7915: reset the CCA statistics of band 1 on mt7916 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=20261123134920.154270-13-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