>From c5f32daa0f9e2322ad7ff5f098946d4364e8bb65 Mon Sep 17 00:00:00 2001 From: Lee Keon-Hyeong Date: Fri, 27 Nov 2026 11:42:51 +0900 Subject: [PATCH net v2 2/2] mac802154: fix locking for association response TX The association response worker calls ieee802154_mlme_tx_locked() without holding RTNL. I found this during an LLM-assisted review and confirmed the RTNL warning with mac802154_hwsim. Taking RTNL in the worker causes association between two PHYs on the same host to time out because the initiator holds RTNL while waiting for the response. Use a per-PHY mutex to serialize MLME TX with interface open and close. Hold the mutex from the interface state check until TX completes. This lets the worker send the response without taking RTNL. The close path can still wait for TX to complete while holding RTNL. On v7.4-rc3 with CONFIG_PROVE_LOCKING=y, two mac802154_hwsim PHYs on the same host associated, disassociated and associated again without an RTNL warning. With a one-second delay added to TX, I brought the interface down during an MLME transmission. The transmission completed before the device stopped. I did not run the in-tree selftests. Fixes: 601f160b61b2 ("mac802154: Handle association requests from peers") Signed-off-by: Lee Keon-Hyeong --- v2: Use a per-PHY mutex instead of taking RTNL in the response worker. Split the skb leak fix into a separate patch. v1: https://lore.kernel.org/netdev/20261121150835.256812-1-leegun25@proton.me/ net/mac802154/ieee802154_i.h | 9 ++++++--- net/mac802154/iface.c | 10 +++++++++- net/mac802154/main.c | 2 ++ net/mac802154/scan.c | 2 +- net/mac802154/tx.c | 37 +++++++++++++++++------------------- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/net/mac802154/ieee802154_i.h b/net/mac802154/ieee802154_i.h index 992ce6698..ba3cb1388 100644 --- a/net/mac802154/ieee802154_i.h +++ b/net/mac802154/ieee802154_i.h @@ -39,6 +39,9 @@ struct ieee802154_local { int open_count; + /* Serialize MLME TX with interface open and close. */ + struct mutex hw_mutex; + /* As in mac80211 slaves list is modified: * 1) under the RTNL * 2) protected by slaves_mtx; @@ -196,12 +199,12 @@ void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb); void ieee802154_xmit_sync_worker(struct work_struct *work); int ieee802154_sync_and_hold_queue(struct ieee802154_local *local); int ieee802154_mlme_op_pre(struct ieee802154_local *local); +int ieee802154_mlme_tx_hw(struct ieee802154_local *local, + struct ieee802154_sub_if_data *sdata, + struct sk_buff *skb); int ieee802154_mlme_tx(struct ieee802154_local *local, struct ieee802154_sub_if_data *sdata, struct sk_buff *skb); -int ieee802154_mlme_tx_locked(struct ieee802154_local *local, - struct ieee802154_sub_if_data *sdata, - struct sk_buff *skb); void ieee802154_mlme_op_post(struct ieee802154_local *local); int ieee802154_mlme_tx_one_locked(struct ieee802154_local *local, struct ieee802154_sub_if_data *sdata, diff --git a/net/mac802154/iface.c b/net/mac802154/iface.c index 31353795f..d84e7a99a 100644 --- a/net/mac802154/iface.c +++ b/net/mac802154/iface.c @@ -186,6 +186,8 @@ static int mac802154_slave_open(struct net_device *dev) ASSERT_RTNL(); + mutex_lock(&local->hw_mutex); + set_bit(SDATA_STATE_RUNNING, &sdata->state); if (!local->open_count) { @@ -200,11 +202,13 @@ static int mac802154_slave_open(struct net_device *dev) } local->open_count++; + mutex_unlock(&local->hw_mutex); netif_start_queue(dev); return 0; err: /* might already be clear but that doesn't matter */ clear_bit(SDATA_STATE_RUNNING, &sdata->state); + mutex_unlock(&local->hw_mutex); return res; } @@ -309,13 +313,17 @@ static int mac802154_slave_close(struct net_device *dev) mac802154_stop_beacons_locked(local, sdata); netif_stop_queue(dev); - local->open_count--; + mutex_lock(&local->hw_mutex); + + local->open_count--; clear_bit(SDATA_STATE_RUNNING, &sdata->state); if (!local->open_count) ieee802154_stop_device(local); + mutex_unlock(&local->hw_mutex); + return 0; } diff --git a/net/mac802154/main.c b/net/mac802154/main.c index 8ed6de111..61d075922 100644 --- a/net/mac802154/main.c +++ b/net/mac802154/main.c @@ -93,6 +93,7 @@ ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops) INIT_LIST_HEAD(&local->rx_mac_cmd_list); spin_lock_init(&local->rx_lock); mutex_init(&local->iflist_mtx); + mutex_init(&local->hw_mutex); tasklet_setup(&local->tasklet, ieee802154_tasklet_handler); @@ -177,6 +178,7 @@ void ieee802154_free_hw(struct ieee802154_hw *hw) BUG_ON(!list_empty(&local->interfaces)); mutex_destroy(&local->iflist_mtx); + mutex_destroy(&local->hw_mutex); wpan_phy_free(local->phy); } diff --git a/net/mac802154/scan.c b/net/mac802154/scan.c index d393b1f4e..d15ba5d96 100644 --- a/net/mac802154/scan.c +++ b/net/mac802154/scan.c @@ -779,7 +779,7 @@ mac802154_send_association_resp_locked(struct ieee802154_sub_if_data *sdata, return ret; } - ret = ieee802154_mlme_tx_locked(local, sdata, skb); + ret = ieee802154_mlme_tx_hw(local, sdata, skb); if (ret) { dev_warn(&sdata->dev->dev, "No ASSOC RESP ACK received from %8phC\n", &teaddr); diff --git a/net/mac802154/tx.c b/net/mac802154/tx.c index af5003b93..baff40bac 100644 --- a/net/mac802154/tx.c +++ b/net/mac802154/tx.c @@ -137,31 +137,26 @@ int ieee802154_mlme_op_pre(struct ieee802154_local *local) return ieee802154_sync_and_hold_queue(local); } -int ieee802154_mlme_tx_locked(struct ieee802154_local *local, - struct ieee802154_sub_if_data *sdata, - struct sk_buff *skb) +int ieee802154_mlme_tx_hw(struct ieee802154_local *local, + struct ieee802154_sub_if_data *sdata, + struct sk_buff *skb) { - /* Avoid possible calls to ->ndo_stop() when we asynchronously perform - * MLME transmissions. - */ - ASSERT_RTNL(); + int ret; - /* Ensure the device was not stopped, otherwise error out */ - if (!local->open_count) { - kfree_skb(skb); - return -ENETDOWN; - } + mutex_lock(&local->hw_mutex); - /* Warn if the ieee802154 core thinks MLME frames can be sent while the - * net interface expects this cannot happen. - */ - if (WARN_ON_ONCE(!netif_running(sdata->dev))) { + if (!local->open_count || !ieee802154_sdata_running(sdata)) { kfree_skb(skb); - return -ENETDOWN; + ret = -ENETDOWN; + goto out; } ieee802154_tx(local, skb); - return ieee802154_sync_queue(local); + ret = ieee802154_sync_queue(local); + +out: + mutex_unlock(&local->hw_mutex); + return ret; } int ieee802154_mlme_tx(struct ieee802154_local *local, @@ -171,7 +166,7 @@ int ieee802154_mlme_tx(struct ieee802154_local *local, int ret; rtnl_lock(); - ret = ieee802154_mlme_tx_locked(local, sdata, skb); + ret = ieee802154_mlme_tx_hw(local, sdata, skb); rtnl_unlock(); return ret; @@ -188,8 +183,10 @@ int ieee802154_mlme_tx_one_locked(struct ieee802154_local *local, { int ret; + ASSERT_RTNL(); + ieee802154_mlme_op_pre(local); - ret = ieee802154_mlme_tx_locked(local, sdata, skb); + ret = ieee802154_mlme_tx_hw(local, sdata, skb); ieee802154_mlme_op_post(local); return ret; -- 2.56.0