Under channel context emulation a monitor retune releases the old context, which configures the driver to the default channel, and then adds a new one, which configures it again. On a device where every channel change is a full chip reset, such as the AR9271 over USB, each retune costs two resets. When the monitor link is the sole user of its emulated context, replace the context with one for the new channel and configure the driver once. Removing the last context is unchanged, so the device still returns to the default channel when the monitor goes away, and drivers with real channel contexts keep their current sequence. Signed-off-by: Nerijus Bendžiūnas --- Changes since v2: the last-context removal is left untouched; the retune replaces the monitor's own context with one for the new channel and configures the driver once. The in-code comment is gone. Tested on an AR9271 pair (ath9k_htc) with a 13-channel hop loop, 500 hops per arm, the same driver in both arms: drv_config per hop median hop frame delivery stock mac80211 2.00 133.4 ms 97.1 % patched 1.00 92.6 ms 98.0 % A regulatory domain change while the monitor sits on a channel the new domain forbids (idle, right after a retune, and in the middle of a hop loop) produces the same driver calls and the same final interface state with and without the patch. 300 channel changes under a 500 frame/s injection load on the retuned interface: no failures, no kernel warnings. On an Intel iwlwifi card, which has real channel contexts, a monitor hop produces the same driver calls with and without the patch (200 hops traced, identical sequence). The patch was written with the help of an LLM coding assistant (Claude); the author reviewed the code and ran the tests above. net/mac80211/cfg.c | 7 +++++ net/mac80211/chan.c | 54 ++++++++++++++++++++++++++++++++++++++ net/mac80211/ieee80211_i.h | 3 +++ 3 files changed, 64 insertions(+) diff --git a/net/mac80211/cfg.c b/net/mac80211/cfg.c index 08ee5ae64939..ed313298161f 100644 --- a/net/mac80211/cfg.c +++ b/net/mac80211/cfg.c @@ -1104,7 +1104,14 @@ static int ieee80211_set_monitor_channel(struct wiphy *wiphy, &chanreq.oper)) return 0; + ret = ieee80211_link_retune_chanctx(&sdata->deflink, &chanreq); + if (!ret) + goto done; + ieee80211_link_release_channel(&sdata->deflink); + if (ret < 0) + return ret; + ret = ieee80211_link_use_channel(&sdata->deflink, &chanreq, IEEE80211_CHANCTX_SHARED); if (ret) diff --git a/net/mac80211/chan.c b/net/mac80211/chan.c index 9cba79507767..12cd850814bf 100644 --- a/net/mac80211/chan.c +++ b/net/mac80211/chan.c @@ -2513,6 +2513,60 @@ int ieee80211_link_change_chanreq(struct ieee80211_link_data *link, return 0; } +int ieee80211_link_retune_chanctx(struct ieee80211_link_data *link, + const struct ieee80211_chan_req *chanreq) +{ + struct ieee80211_local *local = link->sdata->local; + struct ieee80211_chanctx_user_iter iter; + struct ieee80211_chanctx *old_ctx, *new_ctx; + int ret; + + lockdep_assert_wiphy(local->hw.wiphy); + + if (!local->emulate_chanctx) + return 1; + + old_ctx = ieee80211_link_get_chanctx(link); + if (!old_ctx) + return 1; + + if (old_ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE || + old_ctx->will_be_used) + return 1; + + for_each_chanctx_user_all(local, old_ctx, &iter) { + if (iter.link != link || iter.reserved) + return 1; + } + + new_ctx = ieee80211_alloc_chanctx(local, chanreq, old_ctx->mode, + old_ctx->conf.radio_idx); + if (!new_ctx) + return -ENOMEM; + + new_ctx->conf.rx_chains_static = old_ctx->conf.rx_chains_static; + new_ctx->conf.rx_chains_dynamic = old_ctx->conf.rx_chains_dynamic; + + ret = drv_add_chanctx(local, new_ctx); + if (ret) { + kfree(new_ctx); + return ret; + } + + list_replace_rcu(&old_ctx->list, &new_ctx->list); + rcu_assign_pointer(link->conf->chanctx_conf, &new_ctx->conf); + ieee80211_link_update_chanreq(link, chanreq); + old_ctx->driver_present = false; + + ieee80211_remove_wbrf(local, &old_ctx->conf.def); + ieee80211_add_wbrf(local, &new_ctx->conf.def); + ieee80211_recalc_chanctx_min_def(local, new_ctx); + ieee80211_recalc_txpower(link, false); + kfree_rcu(old_ctx, rcu_head); + + return 0; +} + void ieee80211_link_release_channel(struct ieee80211_link_data *link) { struct ieee80211_sub_if_data *sdata = link->sdata; diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h index d186d1274303..74b08320e692 100644 --- a/net/mac80211/ieee80211_i.h +++ b/net/mac80211/ieee80211_i.h @@ -2877,6 +2877,9 @@ int __must_check ieee80211_link_change_chanreq(struct ieee80211_link_data *link, const struct ieee80211_chan_req *req, u64 *changed); +int __must_check +ieee80211_link_retune_chanctx(struct ieee80211_link_data *link, + const struct ieee80211_chan_req *chanreq); void __ieee80211_link_release_channel(struct ieee80211_link_data *link, bool skip_idle_recalc); void ieee80211_link_release_channel(struct ieee80211_link_data *link); -- 2.56.0